Creating symbolic function with array for argument
2 次查看(过去 30 天)
显示 更早的评论
Hi,
I am trying to understand how to use mutli dimension array with symbolic function. I am not sure why I get an error for the symolic function when the input argument is two dimension.
x = sym ('x', [2 1]);
y = sym( 'y', [1 2]);
z = sym( 'z', [2 5]);
f(x,y,z)=(x(1,1)+y(1,1)+z(1,1));
if I change the variable to
x = sym ('x', [1 1]);
y = sym( 'y', [1 2]);
z = sym( 'z', [1 5]);
f(x,y,z)=(x(1,1)+y(1,1)+z(1,1));
it works fine, I am not sure I understand why
6 个评论
VBBV
2024-3-8
symfun operates using scalars more efficiently. The function f has three independent variables, x y and z . So, the function needs inputs to three variables
x = sym ('x');
y = sym( 'y');
z = sym( 'z');
f(x,y,z)=symfun(x+y+z,[x,y,z])
X=[1 ; 1];
Y=[1 1];
Z=[1 2 3 4 5; 6 7 8 9 0];
f(X(1,1),Y(1,1),Z(1,1)) + f(X(2,1),Y(1,1),Z(1,1)) + f(X(1,1),Y(1,1),Z(1,1)) + f(X(1,1),Y(1,2),Z(1,1)) + ...
f(X(1,1),Y(1,1),Z(1,1)) + f(X(1,1),Y(1,1),Z(2,5))
采纳的回答
Walter Roberson
2024-3-8
You cannot create symfun that expect non-scalars as parameters.
symfun takes all of the provided parameters, horzcat()'s them together, and creates individual parameters for the results.
x = sym ('x', [1 2]);
y = sym ('y', [1 2]);
f(x, y) = x(1) + x(2) + y(1) + y(2)
Notice that it has become a function of four parameters.
x = sym ('x', [2 1]);
y = sym ('y', [2 1]);
f(x, y) = x(1) + x(2) + y(1) + y(2)
Notice the only difference here is the orientation of x and y, but it just fails.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Variables, Expressions, Functions, and Preferences 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!