How to define a vector of symbolic functions

11 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Walter Roberson
Walter Roberson il 10 Dic 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 Commenti
Likun
Likun il 10 Dic 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Symbolic Math Toolbox in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by