# Two MATLAB questions about syms

2 views (last 30 days)
yishuai on 6 Feb 2023
Edited: yishuai on 7 Feb 2023
I use matlab syms to define a function
where as follows
syms x y [5,1] matrix;
syms z;
f = (y-z*x).'*(y-z*x)
Now, I have two questions:
1. How to expand the expression of f using MATLAB?
It seems that
expand(f)
does not work! and the other functions such as simplify, collect do not work neither. So I want to know how to expand and simplify this expression in a correct way?
2. How to define the function via an output of diff?
I have tried to define a function first as
syms x y w [5,1] matrix;
syms z t;
f = @(x,y,z) (y-z*x).'*(y-z*x);
phi = @(t) f(x+t*w,y,z);
Then I compute the derivative of ϕ using diff:
diff(phi(t),t);
But I don't know how to make the resulted expression as a function of t (so that I can evaluate the expression of )? subs seems not work!
For the moment, I just copy the expression of computed by diff and define it manually. Hoping to get some better way to do it.
Thanks a lot for any comments and suggestions.

Walter Roberson on 6 Feb 2023
Edited: Walter Roberson on 6 Feb 2023
You are using the "matrix" keyword, which treats the names as matrices . In order to "expand" it you would have to convert to individual symbols
syms x y [5,1] matrix;
syms z;
f = (y-z*x).'*(y-z*x)
f =
expanded = symmatrix2sym(f)
ans =
But I don't know how to make the resulted expression as a function of t
You cannot do that. You cannot use a symmatrix in a symfun at this time.
yishuai on 7 Feb 2023
Edited: yishuai on 7 Feb 2023
@Walter Roberson Thanks for your comments. Indeed, it's not very suitable to convert it as individual symbols to me since what I really need is to get such expression in matrix operations, and I have to do some similar computations to more complicated function f in my next step, where the dimension of each syms is something like for matrix and for vector. Since nothing is changed if the matrix size is dimentionally compatible, so I can simplify it as something like . But in viewing of the so-complicated individual expressions, I really don’t want to manually summarize the matrix operations from individual ones... Thanks a lot again for your kind comments.