Reputation: 25367
I would like to pass an array or a vector to a symbolic function like this:
syms x y
rosenbrock(x, y) = 100 * (y - x^2)^2 + (1 - x)^2;
value = [0, -1];
rosenbrock(value)
but I can't find a way to do this. I have also tried to declare the input as []
rosenbrock([x, y]) = ...
but I'm still getting
Error using symfun/subsref (line 135) Symbolic function expected 2 inputs and received 1.
Upvotes: 2
Views: 2185
Reputation: 18484
If you want to pass in an array, then the input arguments to your function need to be designed to handle an array:
syms x
rosenbrock(x) = 100*(x(2) - x(1)^2)^2 + (1 - x(1))^2;
value = [0, -1];
rosenbrock(value)
With a function like the one in your question, you need to pass in two arguments that correspond to the indices of the vector value
:
syms x y
rosenbrock(x, y) = 100*(y - x^2)^2 + (1 - x)^2;
value = [0, -1];
rosenbrock(value(1), value(2))
This question isn't really specific to symbolic math – you would need to do the same thing for any Matlab function.
Upvotes: 1
Reputation: 313
actually, I ran into similar issues where you would definitely NOT want to have to a) design your function to handle an array OR b) pass the expressions like:
rosenbrock(value(1), value(2))
the solution is this:
v = num2cell(value);
rosenbrock(v{:});
Upvotes: 1