How to define a vector of symbolic functions

11 Ansichten (letzte 30 Tage)
Likun
Likun am 10 Dez. 2013
Kommentiert: Walter Roberson am 10 Dez. 2013
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.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 10 Dez. 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 Kommentare
Likun
Likun am 10 Dez. 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.
Walter Roberson
Walter Roberson am 10 Dez. 2013
x0c = num2cell(x0);
f(x0c{:})

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Symbolic Math Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by