I'd like to create a function that takes in a function of n variables, foo
as a string, evaluated at a point x
.
I have the following code of what I would like to do.
function c = test(foo,n,x)
X=sym('x',[1,n]);
h(X(1:n)) = eval(foo);
H=matlabFunction(h);
A=num2str(x(1));
for i = 2:n
A=[A,',',' ',num2str(x(i));
end
c = H(eval(A));
end
The problem here is that the matlabFunction H will only take inputs as each argument seperated by a comma. It treats x, however, as a single input vector, and thus returns an error. For example, if I use
foo = 'X(1).^2 + X(2).^2'
, with n=2, x=[1,1], it won't be able to tell the difference between H([1,1]) and H(1,1)...hence the error. Of course it's easy to fix with n=2, but what if I were to have any number of variables for my function?
I hope this makes sense, thanks for any help. -DS