Solving for a variable within a loop?

6 views (last 30 days)
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!

Accepted Answer

Star Strider
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
Manderson
Manderson on 12 Nov 2015
Still no luck using solMe(i) = solMev(end);, that is, the code is only outputting the smaller of the two values. I'll keep messing with it but if I don't get anywhere I might just abandon it. I have so many other things to do that I can't afford to get caught up on a small thing like this. Again, I'll let you know if I do end up getting it to work.
You've been a great help and I have learned a few things along the way. I cannot thank you enough for all you have done. I don't know one person who would have put so much time and effort into a code they had no attachment to, haha. Take care :)
Star Strider
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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!