Evaluate symbolic matrix in MATLAB - matlab

I defined 2 symbolic matrix in MATLAB, for example
w = sym('w',[10,10])
Then I do some operations on it and get function E dependent to symbolic matrix w and v. Now, I want to evaluate E numerically with numerical w and v.
How can I do this?

Simple example on R2017a:
>> syms E(v,w)
>> E(v,w) = v*w + v;
>> E(3,4)
ans =
15
On earlier versions, I believe symfun is the command to use.

We can use such code:
>> w=sym('w',[10 10]);
>> d=sym('d',[10 1]);
>> E=W*d + ...(some other operations)
>> define a numerical matrix f and vector x)
>> subs(subs(E,w,f),d,x)
This code performed in R2014a and had a correct answer.

Related

how to rearrange differential equation solution using matlab?

I have an equation:
dC(t)/dt = -K*C + G
I used MATLAB to solve this equation and I got this solution:
C(t) = G/K + (Co-G/K)exp(-Kt/V)
How can I rearrange this equation to get K=?
Because your equation has K inside and outside the exponential, you can't get a nice closed form solution, so the best you hope to achieve is a numerical approximation.
>> syms t C(t) K G C0
>> D=dsolve(diff(C)==-K*C+G,C(0)==C0) %// solve the ODE with an initial condition
D =
(G - exp(-K*t)*(G - C0*K))/K
%// Solve for k given particular values of the other variables
>> k=solve(subs(C(t)==D,{G,t,C,C0},{1,2,1,0.5}),K)
k =
0.91228212986814722960889983912519
If you ignore the initial condition, you can get an equation, but it is in terms of the Lambert W function, and is really not useful for anything.
>> syms t C(t) K G
>> D=dsolve(diff(C)==-K*C+G)
D =
(G - C2*exp(-K*t))/K
>> solve(C==D,K)
ans =
(G + (C(t)*lambertw(0, -(C2*t*exp(-(G*t)/C(t)))/C(t)))/t)/C(t)

How to plot a equation like ay + bx + c = 0 in matlab?

I've already searched it here but I couldn't found it the way I was looking for.
I kind managed to do it using Symbolic Math but I don't understand it quite well. For exemple, after doing that
syms y x
ezplot(-y + x + 1 == 0)
i get a nice graph, but can I use this expression later to calculate its value? like, first I want to plot -y + x + 1 == 0 and at another moment I want to solve f(3) for exemple, where f(x) = x + 1 (same equation).
I know I can write a function to do that, but as a function I don't know how to plot it. In the other way, I know how to plot using symbolic math, but I don't know how to calculate it after.
I'm writing a PLA algorithm and them I need to generate the 'a', 'b' 'c' for the equation, that why I need to know how to plot and solve in a "systematic code" way, and not typing one by one.
Thanks in advance!
The equation you gave us is a straight line, so a polynomial. The coefficients are y= -b/a*x -c/a.
% ay + bx + c = 0 reads y = -b/a*x - c/a*1
a = -1;
b = 1;
c = 1;
p = [-b/a, -c/a]; % polynomial representing your equation
% plot like this
x = linspace(-2,2, 50);
figure
plot(x, polyval(p,x)) % evaluate polynomial p at the positions x
% find the solution
roots(p) # -1
If you need or want to use ezplot, you can put the polyval-expression in an inline function and you can call ezplot with that handle:
f = #(x) polyval(p, x); % the function
ezplot(f)
Just define f to be a function of the symbolic variable x:
>> syms x
>> f = x+1;
Then you can use f as the input to ezplot:
>> ezplot(f)
which produces the graph
On the other hand, to solve the equation f(x)=0 use solve as follows:
>> solve(f)
ans =
-1
ezplot and solve can be used with string inputs as well, but the string has to be different in either case. To plot the graph:
>> ezplot('x+1');
To solve the equation:
>> solve('x+1=0')
ans =
-1

Confusions about Multiplication of Complex Value in MatLab

I noticed a confused computation of complex valued multiplication in Matlab. One simple example is as below:
syms x1 x2 x3 x4
s=[x1 x2]*[x3 x4]'
And the return value of s is like:
s=x1*conj(x3) + x2*conj(x4)
In my opinion s should be equal to x1*x3+x2*x4. So, what's the problem here?
Then, how should I do if I want to get a multiplication of two complex vector?
update: I find out it will be solved through using .' rather than . like:
s=[x1 x2]*[x3 x4]
The operator ' is the also called Complex conjugate transpose in Matlab ctranspose, which basically means that it applies conj and and transpose functions. Note that this operator is call Hermitian operator in mathematics.
What you actually want is the operator transpose that is shortcut as .'
In order to get the expected output, and given that you just want to multiply without conjugating the second vector, you should do:
>> syms x1 x2 x3 x4
>> s = [x1 x2]*[x3 x4].'
so your output will be:
x1*x3 + x2*x4
For further information you can check help ., to see the list of operators, help transpose and help ctranspose
Note that the ' operator in Matlab is the conjugate transpose, i.e. it both transposes a matrix and takes the complex conjugate:
>> (1+1i)'
ans =
1.0000 - 1.0000i
If you want the matrix transpose then you should use the .' operator:
>> (1+1i).'
ans =
1.0000 + 1.0000i
Maybe this will help explain:
>> syms x1 x2 x3 x4
>> s=[x1 x2]*[x3 x4]'
s =
x1*conj(x3) + x2*conj(x4)
>> s=[x1 x2]*[x3; x4]
s =
x1*x3 + x2*x4
>> [x3 x4]'
ans =
conj(x3)
conj(x4)
The ' version of transpose isn't doing what you want. Use transpose instead:
>> transpose([x3 x4])
ans =
x3
x4

how do you do symbolic differentiation on function handle?

Say if i define the following:
g = #(x) x/sqrt(x^2+1)
How do i get the derivative function for g, which i can then use to evaluate at different points?
I tried the symbolic math toolkit, and tried the following:
>> syms x
>> f = x/sqrt(x^2+1)
f =
x/(x^2 + 1)^(1/2)
>> diff(f)
ans =
1/(x^2 + 1)^(1/2) - x^2/(x^2 + 1)^(3/2)
However, i cannot figure out how to turn this into a function handle/evaluate at different points. However, i prefer doing differentiation on function_handle.
Thank you very much!
Jason
You can use matlabFunction to convert a symbolic equation to a function. For example:
syms x1 x2;
f1 = x1^2+x2^2;
Df1 = jacobian(f1, [x1 x2]);
Df1 = matlabFunction(Df1);
Then Df1(0, 0) returns [0 0] as expected.
The function matlabFunction was introduced in version 5.2 (R2009a) of the Symbolic Math Toolbox.

How do I solve a determinant in MATLAB?

As a simple example, let's say you have this matrix:
M = [omega 1;
2 omega];
and you need to solve for the values of omega that satisfy the condition det M = 0.
How do you do this in MATLAB?
It is surely something simple, but I haven't found the function yet.
For the general case where your matrix could be anything, you would want to create a symbolic representation of your matrix, compute the determinant, and solve for the variable of interest. You can do this using, respectively, the functions SYM, DET, and SOLVE from the Symbolic Math Toolbox:
>> A = sym('[w 1; 2 w]'); % Create symbolic matrix
>> solve(det(A),'w') % Solve the equation 'det(A) = 0' for 'w'
ans =
2^(1/2)
-2^(1/2)
>> double(ans) % Convert the symbolic expression to a double
ans =
1.4142
-1.4142
There are also different ways to create the initial matrix A. Above, I did it with one string expression. However, I could instead use SYMS to define w as a symbolic variable, then construct a matrix as you normally would in MATLAB:
syms w
A = [w 1; 2 w];
and now A is a symbolic matrix just as it was in the first example.
If you don't have the symbolic toolbox, then use the sympoly toolbox, found on the file exchange.
sympoly omega
roots(det([omega 1;2 omega]))
ans =
-1.4142
1.4142
Well the determinate is:
om * om - 1*2 = 0
So you would get: om*om = 2
The formal definition is: [a b ; c d] = ad - bc
I would look into simplifying the determinate, and finding a solver to solve for the unknowns.