Symbol calculation and numerical values

26 vues (au cours des 30 derniers jours)
Pouyan Msgn
Pouyan Msgn le 12 Juil 2019
Commenté : John D'Errico le 14 Juil 2019
Hi!
I have my code here :
clc
clear all
syms k w m e s
eqn=k==[w*sqrt(e*m/2)*sqrt(sqrt(1+(s/(w*e))^2)-1)]
k=1; e=0.0006; m=2000; w=2*pi*10^6;
sol=solve(eqn,s)
It will solve s for me, but if I want now a value for s, how should I do that? I do not just want to have S as a symbol, but when I have values for other parameters S must also get a numeric value. How can I do this in Matlab?

Réponse acceptée

John D'Errico
John D'Errico le 12 Juil 2019
You are doing things in the wrong order.
k,e,m,w are not unknowns. They are knowns. So why have you set them up as symbolic?
Next, subtract k from the right hand side. Don't set the two equal. This lets you plot the thing. ALWAYS PLOT EVERYTHING! Do that before you just throw it into a solver.
syms s
k=1; e=0.0006; m=2000; w=2*pi*10^6;
eqn = (w*sqrt(e*m/2)*sqrt(sqrt(1+(s/(w*e))^2)-1)) - k;
fplot(eqn,[-.1,.1])
yline(0);
grid on
I've plotted it over a fairly small domain. It does cross the line at y==0.
untitled.jpg
ssol = solve(eqn,s)
ssol =
-(1288490188800*pi*3^(1/2)*18206206649565666255441706068998^(1/2))/27309309974347922922410255680009
(1288490188800*pi*3^(1/2)*18206206649565666255441706068998^(1/2))/27309309974347922922410255680009
vpa(ssol)
ans =
-0.0010954451150103438340309550004199
0.0010954451150103438340309550004199
Yes, you could have set everything up as symbolic, then substitute the values of k,e,m,w in at the end.
syms k w m e s
eqn = (w*sqrt(e*m/2)*sqrt(sqrt(1+(s/(w*e))^2)-1)) - k
eqn =
w*((s^2/(e^2*w^2) + 1)^(1/2) - 1)^(1/2)*((e*m)/2)^(1/2) - k
ssol = solve(eqn,s)
ssol =
-(2*k*(k^2 + e*m*w^2)^(1/2))/(m*w)
(2*k*(k^2 + e*m*w^2)^(1/2))/(m*w)
So then read the help for subs.
help subs
  2 commentaires
Pouyan Msgn
Pouyan Msgn le 12 Juil 2019
Thanks but I've got these values on k w m e latter.... before that, I didnt know the values of k w e m, then I solved out s:
clc
clear all
syms k w m e s
eqn=k==[w*sqrt(e*m/2)*sqrt(sqrt(1+(s/(w*e))^2)-1)]
k=1; e=0.0006; m=2000; w=2*pi*10^6;
sol=solve(eqn,s)
so s was:
sol =
-(2*k*(k^2 + e*m*w^2)^(1/2))/(m*w)
(2*k*(k^2 + e*m*w^2)^(1/2))/(m*w)
But latter I've got values on k w m e, I knew it was easy to calculate the numeric value of s by hand or calculator but I was searching for direct method in Matlab. Something like str2num or eval but it did not work. Thank you anyway
John D'Errico
John D'Errico le 14 Juil 2019
But I showed you how to solve it directly, two different ways.
You can leave those variables as symbolic, then use solve as I did. Then use subs, as I told you to do, substituting in the values for k,e,m,w.
Or you can set those values as constants first, then solve for s. Either way works. So what is the problem?

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Symbolic Math Toolbox dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by