Solving for a variable within a loop?
6 views (last 30 days)
Show older comments
Situation: Rocket nozzle design.
Code:
% code
gamma=1.4;
R=287; %Gas constant
P0=22313; %Pascals
A_star=0.2; %Throat area in m^2
Ae=1:10;
Me=1;
for i=Ae
AeAs(i)=Ae(i)/A_star;
Ae_As(i)=(1/Me(i))*((2/(gamma+1))*(1+((gamma-1)/2)*(Me(i)^2)))^((gamma+1)/2*(gamma-1))==AeAs(i);
solMe(i)=solve(Ae_As(i),Me(i));
end
I have to vary the area ratio of the exit area of the nozzle (Ae) to the area of the throat (A_star). A_star is fixed, A_e varies. (I have it varying from 1:10). So there are 10 different values for Ae/A_star. Using the equation labeled as "Ae_As(i)", I can solve for Me (exit Mach number). I tried to do this using the solve function which can be seen in the solMe line. When I run this code, I get this error:
% code
Error using solve>processString (line 337)
' 1 ' is not a valid expression or equation.
Error in solve>getEqns (line 267)
eqns = processString(eqns, v, vc);
Error in solve (line 150)
[eqns,vars,options] = getEqns(varargin{:});
Error in Problem_2 (line 14)
solMe(i)=solve(Ae_As(i)==AeAs(i),Me(i));
Is there an easier way to do this? I've tried messing with it for hours and I'm getting nowhere.
Thanks in advance for your help!
0 Comments
Accepted Answer
Star Strider
on 10 Nov 2015
There are some errors in your code. I corrected them and got it to run using vpasolve:
syms Me
gamma=1.4;
R=287; %Gas constant
P0=22313; %Pascals
A_star=0.2; %Throat area in m^2
Ae=1:10;
% Me=1;
for i=Ae
AeAs(i)=Ae(i)/A_star;
Ae_As=(1/Me)*((2/(gamma+1))*(1+((gamma-1)/2)*(Me^2)))^((gamma+1)/2*(gamma-1))==AeAs(i);
solMe(i)=vpasolve(Ae_As,Me);
end
I declared ‘Me’ a symbolic variable to solve for rather than a constant, and then solved for it in the last assignment in the loop. The code runs. I leave it to you to determine if it produces the correct results, since the Mach numbers seem low to me. (However, I am not an aerodynamicist, so they could be entirely appropriate.)
18 Comments
Star Strider
on 12 Nov 2015
My pleasure, as always!
Rather than use the Symbolic Toolbox, use fzero. I would. It works! It’s also much faster and more efficient.
Solving problems like this and explaining them are fun for me (I’m one of the unpaid volunteers here), and I always learn things. I encounter problems I probably would never see in my own code or my own areas of research and expertise.
Meanwhile, submit a Bug Report to MathWorks about vpasolve returning a lower of two values, and your not being able to get the correct value, while fzero produces the correct result. Include the URL to this thread: http://www.mathworks.com/matlabcentral/answers/254029-solving-for-a-variable-within-a-loop. This thread tells the entire story, so you need do nothing more than briefly describe the problem and include the URL.
More Answers (0)
See Also
Categories
Find more on Special Values in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!