why I get the error of "Unable to find variables in equations." in line 29?
9 次查看(过去 30 天)
显示 更早的评论
I have the function in line 17 (calculating M2) and it works but the same function in line 29 (calculating Mx) doesnt work and i get the error above?
how can i fix it?
clc
clear
syms M2;
syms Mx;
L=input('enter the L value\n');
D=input('enter the D value\n');
P1=input('enter the P1 value\n');
T1=input('enter the T1 value\n');
M1=input('enter the M1 value\n');
f=0.005;
L1=0.1;
Ls1=(((1-M1^2)/(1.4*(M1^2)))+((2.4/2.8)*log((2.4*(M1^2))/(2+0.4*(M1^2)))))*(D/(4*f));
Z = sprintf('L* is = %s.',Ls1);
disp(Z);
if L<Ls1
Ls2=Ls1-L;
M2=vpasolve((((1-M2^2)/(1.4*(M2^2)))+((2.4/2.8)*log((2.4*(M2^2))/(2+0.4*(M2^2)))))*(D/(4*f))- Ls2,M2);
P2=P1*(M1/M2)*(((2+0.4*(M1^2))/(2+0.4*(M2^2)))^0.5);
T2=T1*((2+0.4*(M1^2))/(2+0.4*(M2^2)));
X = sprintf('M2 is = %s.',M2);
Y = sprintf('T2 is = %s and P2 is = %d .',T2,P2);
disp(X);
disp(Y);
else
M2=1;
while L1<L
L2=L-L1;
Lsx=Ls1-L1;
Mx=vpasolve((((1-Mx^2)/(1.4*(Mx^2)))+((2.4/2.8)*log((2.4*(Mx^2))/(2+0.4*(Mx^2)))))*(D/(4*f))- Lsx,Mx);
0 个评论
采纳的回答
Walter Roberson
2019-3-31
You define
syms Mx
then you do
Mx=vpasolve(an expression in Mx, Mx);
Before you do that first vpasolve(), Mx holds a reference to the symbolic variable named Mx . After the assignment, Mx holds a reference to the symbolic numeric result of the vpasolve(), roughly sym(1.934) .
The next iteration of the loop, Mx is the sym(1.934), and the expression in Mx evaluates to a symbolic numeric result, and the Mx at the end of the call is sym(1.934)-ish, so you have asked to vpasolve() and passed two symbolic numeric values that have no unresolved symbolic variable names. vpasolve() cannot figure out what you want to solve for.
3 个评论
Walter Roberson
2019-3-31
Ideally you should avoid assignment a value to any variable you have defined as a symbolic variable: there is just too much risk of the kind of error you just encountered, or risk that you will program something like
A = expression in Mx
Mx = new value
and then expect A to operate as if it had the new value of Mx.
For example if you had assigned to
sol_Mx = vpasolve(....,Mx);
and had then used sol_Mx in the later lines, then the next iteration through the loop, Mx would still be the symbolic variable it originally was and the problem would not have occurred.
The alternative to the ideal is...
Insert
syms Mx
inside the while loop.
更多回答(1 个)
madhan ravi
2019-3-31
syms M2 Mx
L=input('enter the L value\n');
D=input('enter the D value\n');
P1=input('enter the P1 value\n');
T1=input('enter the T1 value\n');
M1=input('enter the M1 value\n');
f=0.005;
L1=0.1;
Ls1=(((1-M1^2)/(1.4*(M1^2)))+((2.4/2.8)*log((2.4*(M1^2))/(2+0.4*(M1^2)))))*(D/(4*f));
Z = sprintf('L* is = %s.',Ls1);
disp(Z);
if L<Ls1
Ls2=Ls1-L;
M2=vpasolve((((1-M2^2)/(1.4*(M2^2)))+((2.4/2.8)*log((2.4*(M2^2))/(2+0.4*(M2^2)))))*(D/(4*f))- Ls2,M2);
P2=P1*(M1/M2)*(((2+0.4*(M1^2))/(2+0.4*(M2^2)))^0.5);
T2=T1*((2+0.4*(M1^2))/(2+0.4*(M2^2)));
X = sprintf('M2 is = %s.',M2);
Y = sprintf('T2 is = %s and P2 is = %d .',T2,P2);
disp(X);
disp(Y);
else
M2=1;
L2=L-L1;
Lsx=Ls1-L1;
Mx=vpasolve((((1-Mx^2)/(1.4*(Mx^2)))+((2.4/2.8)*log((2.4*(Mx^2))/(2+0.4*(Mx^2)))))*(D/(4*f))- Lsx,Mx);
end
3 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Calculus 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!