Matrix of symbolic functions
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})
source to share
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.
source to share