Clear Filters
Clear Filters

Evaluating symbolic function with a vector

85 views (last 30 days)
I know this has been asked before, but none of the answers I've found have been helpful.
Here's the simple example of what I'm trying to do:
syms x1 x2
f(x1,x2) = x1 + x2;
x = [ 1 1];
f(x)
Error using indexing
Invalid argument at position 2. Symbolic function expected 2 input arguments but received 1.
I would expect this to return:
ans =
2
Which, of course, is not working, as seen in the error stated above.
Any insights on how can I define a function that accepts a vector of values for many variables?

Accepted Answer

Walter Roberson
Walter Roberson on 21 Apr 2023
MATLAB does not permit defining symbolic functions that accept vectors of values and distributes the values into multiple parameters.
In the special case that it is acceptable to run the calculation numerically instead of symbolically, then use
syms x1 x2
f(x1,x2) = x1 + x2;
F = matlabFunction(f, 'vars', {[x1 x2]})
F = function_handle with value:
@(in1)in1(:,1)+in1(:,2)
x = [ 1 1];
F(x)
ans = 2
Notice that you had to use the 'vars' option and that you needed to pass a cell array to the the 'vars' option. Each cell entry lists variables that are to be bundled into a single input parameter.
Notice that once you have used matlabFunction to bundle multiple inputs into one parameter, you cannot then invoke the function with multiple parameters -- F(x(1), x(2)) would be an error.
  2 Comments
Diego
Diego on 21 Apr 2023
This was incredibly helpful! Thanks!
Walter Roberson
Walter Roberson on 21 Apr 2023
Additional note:
If the function produces a non-scalar output (given scalar inputs) then there is nothing more that can be done. However, if the function produces a scalar output for scalar inputs, then much of the time matlabFunction will emit a version of the function that can be passed multiple values for each parameter and will produce an output based on each corresponding inputs. For example given x1^2+x2 then matlabFunction would emit code that did x1.^2+x2 and so could be passed matrix or vector (or, of course, scalar) inputs.
When matlabFunction is able to vectorize the function, then if you bundle multiple inputs together into one parameter, the way it operates to unpack the multiple values corresponds to the shape of the vector (or even array) of variables in the cell array entry. So in the case of [x1 x2] you specify the variables as differerent columns of that entry, and the packed code will extract each column of 2D arrays of inputs as corresponding to the inputs. In If you had passed [x1; x2] to 'vars' then you would have specified x1 and x2 as rows there, and matlabFunction would create the code to extract by rows -- F(DATA) would treat DATA(1,:) as x1, DATA(2,:) as x2 in such a case.
Not everything can be vectorized. For example if you had int() and a variable as the upper bounds or lower bounds, then the integral() call that matlabFunction generated would not be able to handle multiple upper or lower bounds being passed in.

Sign in to comment.

More Answers (1)

Chunru
Chunru on 21 Apr 2023
% take x1 and x2 as two variables
syms x1 x2
f(x1,x2) = x1 + x2;
x = [ 1 1];
f(x(1), x(2))
ans = 
2

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!