How to define a vector of symbolic functions

25 views (last 30 days)
Hi all,
I have a vector of functions f(x), where f and x are vectors. For example,
f(x)=[x1^2+x2-x3; x2+x3^2; x1*x2];
How could I declare such function? Thank you.

Accepted Answer

Walter Roberson
Walter Roberson on 10 Dec 2013
You could try,
syms x
f = symfun([x1^2+x2-x3; x2+x3^2; x1*x2], x);
but that might instead create a single function that returns an array. More sure would be
f = arrayfun(@symfun, [x1^2+x2-x3; x2+x3^2; x1*x2], x)
You would have had to have defined x1, x2, and x3 before this, and it seems odd that you would define a function with argument x but not use x anywhere in the function.
  2 Comments
Likun
Likun on 10 Dec 2013
Thanks. The first approach
x=sym('x',[1 3]);
f = symfun([x1^2+x2-x3; x2+x3^2; x1*x2], x);
works well for my problem. The other problem is on evaluating the function. For the above case, it is easy to get the value of f, e.g. f(1, 2, 2). But my problem involves a vector x with dimension N that is impossible to write down each element explicitly. For example, running
x0=zeros(1,N)
f(x0)
gives the error:
Error using symfun/subsref (line 135)
Symbolic function expected N inputs and received 1.
Hope that you can give some idea on resolving this issue. Thanks.

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!