Creating symbolic function with array for argument

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));
Error using sym/cat>checkDimensions
CAT arguments dimensions not consistent.

Error in sym/cat>catMany (line 33)
[resz, ranges] = checkDimensions(sz,dim);

Error in sym/cat (line 25)
ySym = catMany(dim, args);

Error in sym/horzcat (line 19)
ySym = cat(2,args{:});

Error in indexing (line 1033)
C = symfun(sym(R),[inds{:}]);
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 commentaires

VBBV
VBBV le 8 Mar 2024
Modifié(e) : VBBV le 8 Mar 2024
x = sym ('sh', [2 1]);
y = sym( 'ao', [1 2]);
z = sym( 'aa', [2 5]);
f=(x(1,1)+y(1,1)+z(1,1)) % The LHS of equation is creating problem since x, y, z are symbolic arrays !!
f = 
Yes, but if I understand proprelly it is not a function anymore. I want to be able to evaluate the symbolic function for specific values in a later stage without converting it.
Do you mean something like this ?
x = sym ('sh');
y = sym( 'ao');
z = sym( 'aa');
f(x,y,z) = symfun(x + y + z,[x,y,z]) %
f(sh, ao, aa) = 
f(1,2,1)
ans = 
4
The RHS of the below equation is already evaluated for specfic points in the symbolic arrays
f=(x(1,1)+y(1,1)+z(1,1));
Thank you for your time, maybe I simplified it to much for the question. but the input argument need to be 2D array because the real function is more complicated. maybe here is a better example.
x = sym ('x', [2 1]);
y = sym( 'y', [1 2]);
z = sym( 'z', [2 5]);
f(x,y,z)=symfun(x(1,1)+x(2,1)+y(1,1)+y(1,2)+z(1,1)+z(2,5),[x,y,z]);
Error using sym/cat>checkDimensions
CAT arguments dimensions not consistent.

Error in sym/cat>catMany (line 33)
[resz, ranges] = checkDimensions(sz,dim);

Error in sym/cat (line 25)
ySym = catMany(dim, args);

Error in sym/horzcat (line 19)
ySym = cat(2,args{:});
X=[1 ; 1];
Y=[1 1];
Z=[1 2 3 4 5; 6 7 8 9 0];
f(X,Y,Z)
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])
f(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))
ans = 
17
thank you

Connectez-vous pour commenter.

 Réponse acceptée

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)
f(x1, x2, y1, y2) = 
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)
Error using symfun.validateArgNames
Second argument must be a scalar or vector of unique symbolic variables.

Error in symfun (line 102)
y.vars = symfun.validateArgNames(inputs);

Error in indexing (line 1033)
C = symfun(sym(R),[inds{:}]);
Notice the only difference here is the orientation of x and y, but it just fails.

Plus de réponses (0)

Produits

Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by