1

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

horchler
  • 18,384
  • 4
  • 37
  • 73
  • read http://www.mathworks.com/help/symbolic/symfun.html and http://www.mathworks.com/help/symbolic/creating-symbolic-variables-and-expressions.html#bs_tekf-1 – mhmsa Feb 18 '14 at 18:42
  • @mhmsa: Did you read the documentation? Using these pages I did not manage to put a symfun into a matrix. Everything I manage to create is a symfun returning a matrix. – Daniel Feb 18 '14 at 18:45

3 Answers3

5

You can't create a matrix of symfun class elements (possibly for the same reason that one can't create a matrix of function handles), 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 directly access the elements of Afun until you evaluate it, though you can use formula to extract them:

Amat = formula(Afun);
Amat(1)

It is possible to concatenate symfuns into a matrix, provided that they all have the same input arguments (the arguments don't need to be used). However, the concatenation still does not form a matrix of symfuns – 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, e.g.:

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 using subs:

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

And normal matrix operations work, e.g.:

B = subs(A(2,:),{x,y,z},{sym(pi),cos(y),z^2})
Community
  • 1
  • 1
horchler
  • 18,384
  • 4
  • 37
  • 73
1

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])}

Maybe this is sufficient in your case.

Daniel
  • 36,610
  • 3
  • 36
  • 69
0

If you for example want to arrange some anonymous symbolic functions in a vector you can do as 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 an 1x1 symbolic function even though it contains more elements. z will still behave like a vector, so you can use it as a normal vector in matrix-vector operations.

LJdms
  • 1