Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Trouble with while loop

1 visualización (últimos 30 días)
Nadine Nassar
Nadine Nassar el 17 de Nov. de 2020
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Hello,
I'm having trouble fixing the while loop. The idea is to increment x while the error is larger than 1%, the loop doesn't seem to stop and outputs a very large value for the error (2e6). Everything is running smoothly until I reach the part where I have to compare Pb_calc to Pb and im not sure how to loop over them.
Thank you
k=1.4;
A_throat = 1.0841e+03;
r_throat = sqrt(A_throat/pi);
Me = 0.12;
A = (k+1)/(2*(k-1));
B= k/(k-1);
C = 1/(k-1);
D = k-1;
E = k+1;
Pb = 2e6;
P_0 = 2.5e6;
L=100;
r=0;
delta_M = 0.1; %Increment between each mach number
M1_guess = 1+1e-4; %Initial guess for the subsonic mach number, must be less than one to ease convergence
iter_sub = 0;
iter_p=0;
error = 1e-6;
error1=6000;
x =1;
while error1>1000
for z=1:1:100
r_position = sqrt((r_throat^2)*(1 + (2.5*((x/L)^2)))); %Radius at assumed shock location
A_positon = pi*(r_position^2); %Area at assumed shock location
shock_area = A_positon/A_throat; %Area ratio at the assumed shock location
mach_shock_eqn = @(Ms,shock_area) ((1/Ms)*((2+(D*Ms^2))/E)^A) - shock_area; %Mach-Area equation
dx=0.1;
for i=1:1:100 %Number of iterations that will be done to reach the true value, 100 is the max number (abused)
for j=1:1:100 %number of iterations
M_1= mach_shock_eqn(M1_guess,shock_area); %Mach number at initial guess
M_2= mach_shock_eqn(M1_guess+delta_M,shock_area); %Mach number at next value
if (M_1*M_2 >0)
M1_guess = M1_guess + delta_M; %New guess
end
if (abs(M_1-M_2) <=error)
iter_sub =i;
end
end
end
M_shock1 = M1_guess; %Mach number before the shock
M_shock2 = sqrt((M_shock1^2 + 2*C)/(2*B*M_shock1^2 -1)); %Mach number after the shock
P1 = P_0/((1+(0.5*D*(M_shock1^2)))^B); %Pressure before the shock
P2 = P1*(M_shock1/M_shock2)*((2+(D*(M_shock1^2)))/(2+(D*(M_shock2^2))))^0.5; %Pressure after the shock
P_02 = P2 *(1+0.5*D*(M_shock2^2))^B;
Pb_calc = P_02 / ((1+(0.5*D*(Me^2)))^B);
if (Pb_calc<Pb)
x=x+dx;
else if (Pb_calc > Pb)
x=x-dx;
else
x = x;
break
end
end
error1 = abs(Pb-Pb_calc)
end
end
y=x

Respuestas (1)

Cris LaPierre
Cris LaPierre el 18 de Nov. de 2020
Editada: Cris LaPierre el 18 de Nov. de 2020
You appear to have one too many ends in your code. That's not going to fix your infinite loop, but it needs to be fixed, too.
Ultimately, your value for error1 does not change significantly. Perhaps check your equations, as well as the logic you use.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by