How to define a vector of symbolic functions

11 ビュー (過去 30 日間)
Likun
Likun 2013 年 12 月 10 日
コメント済み: Walter Roberson 2013 年 12 月 10 日
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.

採用された回答

Walter Roberson
Walter Roberson 2013 年 12 月 10 日
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 件のコメント
Likun
Likun 2013 年 12 月 10 日
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 2013 年 12 月 10 日
x0c = num2cell(x0);
f(x0c{:})

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by