kaienfr
kaienfr

Reputation: 111

Two MATLAB questions about syms

I use matlab syms to define a function f(x,y,z) = ||y-zx||^2 where x and y are vectors of R^5, z is a scalar in R 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?

  2. How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?

Thanks a lot for any comments and suggestions.


What I tried are summarized below:

  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 and factor work neither. So I want to know how to expand and simplify this expression in the correct way?

  1. How to define the function t-> d f(x+tw,y,z)/d t via an output of diff?

I have tried to define the function phi(t) = f(x+tw,y,z) 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 using diff:

diff(phi(t),t);

But I don't know how to make the resulting expression as a function of t (so that I can evaluate the expression of phi'(1))? For the moment, I just copy the expression of phi'(t) computed by diff and define it manually. Hoping to get a better way to do so. Thanks a lot for any comments and suggestions.

Upvotes: 1

Views: 99

Answers (1)

kaienfr
kaienfr

Reputation: 111

I figured out a way to convert a symmatrix object to a string in Matlab. Once I have the string representation, I can replace the substring 't' with a desired value, and then evaluate the modified string to compute its value. Although this is not the final solution for defining a function of t, but it helps me achieve my objective automatically without having to manually copy expressions to define phi'(t).

BTW: simplfy,expand and collect functions for symmatrix objects are still unresolved problems.

Upvotes: 0

Related Questions