why I get the error of "Unable to find variables in equations." in line 29?

37 ビュー (過去 30 日間)
fatemeh p
fatemeh p 2019 年 3 月 31 日
コメント済み: fatemeh p 2019 年 3 月 31 日
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);

採用された回答

Walter Roberson
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
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.
fatemeh p
fatemeh p 2019 年 3 月 31 日
it worked, thanks a lot

サインインしてコメントする。

その他の回答 (1 件)

madhan ravi
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 件のコメント
Walter Roberson
Walter Roberson 2019 年 3 月 31 日
What are some sample inputs for us to test with?
fatemeh p
fatemeh p 2019 年 3 月 31 日
of cource
this code producing the ouput mach number,pressure and temperture of a pipe with extra friction in aerodynamics
for a L=1.8, T1=288, P1=1, D=0.1, M1=2 it should go into while loop.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by