Good evening everyone,
I want to create a function
f(x) = [f1(x), f2(x), ... , fn(x)]
in MatLab, with an arbitrary form and number for the fi. In my current case they are meant to be basis elements for a finite-dimensional function space, so for example a number of multi variable polynomials. I want to able to be able to set form (e.g. hermite/lagrange polynomials, ...) and number via arguments in some sort of "function creating" function, so I would like to solve this for arbitrary functions fi.
Assume for now that the fi
are fi:R^d -> R, so vector input to scalar output. This means the result from f
should be a n-dim vector containing the output of all n functions. The number of functions n could be fairly large, as there is permutation involved. I also need to evaluate the resulting function very often, so I hope to do it as efficiently as possible.
Currently I see two ways to do this:
Create a cell with each fi using a loop, using something like
funcell{i}=matlabFunction(createpoly(degree, x),'vars',{x})
and one of the functions from the symbolic toolbox and a symbolic x (vector). It is then possible to create the desired function with cellfun, e.g.
f=@(x) cellfun(@(v) v(x), funcell)
This is relatively short, easy and what can be found when doing searches. It even allows extension to vector output using'UniformOutput',false
andcell2mat
. On the downside it is very inefficient, first during creation because of matlabFunction and then during evaluation because of cellfun.The other idea I had is to create a string and use
eval
. One way to do this would be
stringcell{i}=[char(createpoly(degree, x)),';']
and then usestrjoin
. In theory this should yield an efficient function. There are two problems however. The first is the use ofeval
(mostly on principle), the second is inserting the correct arguments. The symbolic toolbox does not allow symbols of the form x(i), so the resulting string will not contain them either. The only remedy I have so far is some sort of string replacement on the xi that are allowed, but this is also far from elegant.
So I do have ways to do what I need right now, but I would appreciate any ideas for a better solution.