How to convert from symbol to double?

253 views (last 30 days)
Hi,
I am pretty new to MATLAB, so this might be a stupid question. However, I am encountering a problem when i try to convert a matrix of 1x1 symbols into a matrix with doubles, where the symbols are converted to doubles each. Each element in the matrix is a function of a lot of symbols, which I need somehow to convert into numbers. I have something like this:
syms x y;
f = function of x and y
x = 2;
y = 3;
f.
However, doing this gives me f as a function of the syms x and y, and not the numbers 2 and 3... Hope you can somehow help me, as Im kinda stuck in my coding.
Regards, Christian

Accepted Answer

Walter Roberson
Walter Roberson on 19 Jan 2013
double(subs(f))
  8 Comments
Walter Roberson
Walter Roberson on 7 May 2023
syms x1 x2 s t x x0 x10 x20 x30 y1 y2 y3 a p1 q1
The x variables are all symbolic so-far.
A = [-6 0 0;0 -1 5;0 -5 -1];
[W,D] = eig(A); % Right eigen vector
Lm = [exp(x1*t) 0 0;0 exp(conj(x1)*t) 0;0 0 exp(x2*t)];
You use some of the x variables in Lm
U = inv(W); % Left eigenvector
x0 = [x10;x20;x30]; % initial conditions
e = eig (A);
% K = A - e(3,1)*eye(3);
[V,D] = eig(A);
y = W*Lm*U; % y is the transition matrix
so they appear in y because they appear in Lm
Q = (subs(y,{exp(x1*t),exp(conj(x1)*t)},{exp(p1*t)*(cos(q1*t) + 1i*sin(q1*t)),exp(p1*t)*(cos(q1*t) - 1i*sin(q1*t))}));
% Q is the expansion of y after polar coordinate to cartesian
You substituted for particular expressions containing x1 -- but was that the only place that x1 appeared and are there other x variables in y?
y
y = 
Q
Q = 
So we can see from that that all of the x1 are now gone, but there is still an x2
Q1 = Q*x0;
Q1
Q1 = 
And you can see that you added some x variables to Q1 so it now has x2, x10, x20, x30
a = 3 ; % max value of initia conditions
t = 0:0.01:8;
for i = 1:3
x10 = 1; x20 = 1; x30 = 1;
if i == 1
x2 = real(e(3,1));
Q_1 = (Q1(1,1));
end
if (i == 2)
p1 = real(e(1,1));
q1 = imag(e(1,1));
Q_2 = Q1(2,1);
end
if (i == 3)
p1 = real(e(1,1));
q1 = imag(e(1,1));
Q_3 = Q1(3,1);
end
end
You created numeric x10, x20, x30 inside the loop, but you did not subs() them into anything so Q1 still has x2, x10, x20, x30 inside of it, and inside some of the elements you constructed from it. Likewise you created numeric t but did not substitute it, so some of your variables have symbolic t in them.
It is not clear why you bothered to loop over i there when you never index by i and the actions done for the various i are independent of each other.
symvar(Q_1)
ans = 
Your Q_1 contains three symbolic variables; you cannot use plot() with any symbolic variables.
If you were to subs(Q_1) at this point then you would get Q_1 evaluated with the current x2, current x10, current t, and you could double() that.
sQ_1 = double(subs(Q_1));
plot(t,sQ_1)
hold on
sQ_2 = double(subs(Q_2));
plot(t, sQ_2)
hold on
sQ_3 = double(subs(Q_3));
plot(t,sQ_3)
hold off
Kamilu Sanusi
Kamilu Sanusi on 7 May 2023
@Walter Roberson, thank you very very much for the assistance. I acually got the response in a non structural program flow. Your explanation gives a complete guide, and now i understand. Infact you are a RARE GEM! Thank you once again

Sign in to comment.

More Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 19 Jan 2013
Edited: Azzi Abdelmalek on 19 Jan 2013
Use
suyms x
f=cos(x);
x=10;
eval(f)
  3 Comments
Steven Lord
Steven Lord on 6 Sep 2019
double and subs, as shown in Walter's accepted answer for this question.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!