Matrix of symbolic functions

I would like to define a matrix of symbolic functions (not variables) in Matlab. In the workspace, I would like it to be an N-by-M size member of the symfun class (where N

and M

are positive integers).

+1


source to share


3 answers


You cannot create a matrix of elements symfun

(perhaps for the same reason that one cannot create a matrix of function descriptors ), but you can create a symbolic function that returns a matrix of symbolic expressions:

syms x y z;
Afun = symfun([x+y y-z;y/x z-1],[x y z])
B = Afun(sym(pi),cos(y),z^2)

      

Of course, you won't be able to access the elements directly Afun

until you evaluate it, although you can use formula

to retrieve them:

Amat = formula(Afun);
Amat(1)

      

Can be combined symfun

into a matrix, provided they all have the same input arguments (no arguments should be used). However, concatenation still doesn't form matrices symfun

- it just concatenates the formulas themselves, so you still end up with one symfun

as above.

Another option is to create a matrix of symbolic expressions, for example:



syms x y z;
A = [2*x    3*y^2   x+z;
     -y^3+1 sin(x)  sym('pi');
     3.5    exp(-z) 1/x];

      

which can be evaluated with subs

:

B = subs(A,{x,y,z},{sym(pi),cos(y),z^2})

      

And the usual matrix operations work, for example:

B = subs(A(2,:),{x,y,z},{sym(pi),cos(y),z^2})

      

+5


source


I don't know how to create a matrix, but a cell is possible:

c={symfun(x+y, [x y]),symfun(x+2*y, [x y]);symfun(x+3*y, [x y]),symfun(x+4*y, [x y])}

      



Perhaps this is enough in your case.

+1


source


If, for example, you want to order some anonymous symbolic functions in a vector, you can do the following:

z = sym([]);    %declare z as an empty symbolic array

N = 6;          %array size

for i = 1:N
   syms(sprintf('z%d(t)', i)) %declare each element in the array as a single symbolic function

   zz = symfun(sym(sprintf('z%d(t)', i)), t); %declare each element to a symbolic "handle"

   z = [z;zz]; %paste the symbolic "handle" into an array 
end

      

Be aware that Matlab treats z as a symbolic 1x1 function even if it contains more elements. z will still behave like a vector, so you can use it like a regular vector in matrix-vector operations.

0


source







All Articles