While loop setting error calculation is not calculating properly.

1 回表示 (過去 30 日間)
Shawn Alcorn
Shawn Alcorn 2022 年 8 月 31 日
移動済み: Voss 2022 年 8 月 31 日
While loop within code is suppose to adjust betaIter until thetaTanIter is within a certain value of tangent of 10 degrees. The results I am getting are the exact opposite of what the actual results are suppose to be. The first value of beta is correct but the proceeds in the wrong direction. Beta in degrees should go from 39 degrees to about 12 degrees. Once the beta issue is fixed then all other problems should be corrected as well.
close all
clear all
clc
% Known Static Variables
gamma = 1.4;
theta = 10; %Degrees
theta_Rads = theta*(pi/180);% Radians
P1 = 1; % ATM
T1 = 298.15; % K
i = 1;
betaIter_deg = 2;
betaIter = betaIter_deg*(pi/180); % Initial guess
thetaTan = tan(theta_Rads);
for M1 = 2:1:50
M1_save(i) = M1;
thetaTanIter = 2*cot(betaIter)*(((M1^2)*(sin(betaIter)^2)-1)/(((M1^2)*(gamma+cos(2*betaIter)))+2));
while abs(thetaTan - thetaTanIter) > .00001
betaIter_deg = betaIter_deg + .0001;
betaIter = betaIter_deg *(pi/180);
thetaTanIter = 2*cot(betaIter)*(((M1^2)*(sin(betaIter)^2)-1)/(((M1^2)*(gamma+cos(2*betaIter)))+2));
end
beta = betaIter_deg;
beta_save(i) = beta;
betaRads = beta*(pi/180);
M2(i) = (1/sin(betaRads-theta_Rads))*sqrt((1+((gamma-1)/2)*(M1^2)*(sin(betaRads)^2))/((gamma*(M1^2)*(sin(betaRads)^2))-((gamma-1)/2)));
P2P1(i) = ((2*gamma)/(gamma+1))*((M1^2)*(sin(betaRads)^2)-1);
rho2rho1(i) = ((gamma+1)*(M1^2)*(sin(betaRads)^2))/((gamma-1)*(M1^2)*(sin(betaRads)^2)+2);
T2T1(i) = P2P1/rho2rho1;
betaTheta(i) = beta_save(i)/theta;
i = i+1;
end
plot(M1_save,M2)
xlabel('Upstream Mach Number (M_1)')
ylabel('Downstream Mach Numer (M_2)')
title('M_{1} vs M_{2} - \theta = 10^{\circ}')
figure
plot(M1_save,P2P1)
xlabel('Upstream Mach Number (M_1)')
ylabel('{P2}/{P1}')
title('M_1 vs {P2}/{P1}')
figure
plot(M1_save,T2T1)
xlabel('Upstream Mach Number (M_1)')
ylabel('{T2}/{T1}')
title('M_1 vs {T2}/{T1}')
figure
plot(M1_save,beta_save)
xlabel('Upstream Mach Number (M_1)')
ylabel('\beta (Deg)')
title('M_1 vs \beta')
figure
plot(M1_save,betaTheta)
xlabel('Upstream Mach Number (M_1)')
ylabel('{\beta}/{\theta}')
title('M_1 vs {\beta}/{\theta}')
  2 件のコメント
Torsten
Torsten 2022 年 8 月 31 日
編集済み: Torsten 2022 年 8 月 31 日
You forgot to reset the values for a new M1:
% Known Static Variables
gamma = 1.4;
P1 = 1; % ATM
T1 = 298.15; % K
i = 1;
theta = 10; %Degrees
theta_Rads = theta*(pi/180);% Radians
thetaTan = tan(theta_Rads);
for M1 = 2:1:50
betaIter_deg = 2;
betaIter = betaIter_deg*(pi/180); % Initial guess
M1_save(i) = M1;
thetaTanIter = 2*cot(betaIter)*(((M1^2)*(sin(betaIter)^2)-1)/(((M1^2)*(gamma+cos(2*betaIter)))+2));
while abs(thetaTan - thetaTanIter) > .00001
betaIter_deg = betaIter_deg + .0001;
betaIter = betaIter_deg *(pi/180);
thetaTanIter = 2*cot(betaIter)*(((M1^2)*(sin(betaIter)^2)-1)/(((M1^2)*(gamma+cos(2*betaIter)))+2));
end
beta = betaIter_deg;
beta_save(i) = beta;
betaRads = beta*(pi/180);
M2(i) = (1/sin(betaRads-theta_Rads))*sqrt((1+((gamma-1)/2)*(M1^2)*(sin(betaRads)^2))/((gamma*(M1^2)*(sin(betaRads)^2))-((gamma-1)/2)));
P2P1(i) = ((2*gamma)/(gamma+1))*((M1^2)*(sin(betaRads)^2)-1);
rho2rho1(i) = ((gamma+1)*(M1^2)*(sin(betaRads)^2))/((gamma-1)*(M1^2)*(sin(betaRads)^2)+2);
T2T1(i) = P2P1/rho2rho1;
betaTheta(i) = beta_save(i)/theta;
i = i+1;
end
plot(M1_save,M2)
xlabel('Upstream Mach Number (M_1)')
ylabel('Downstream Mach Numer (M_2)')
title('M_{1} vs M_{2} - \theta = 10^{\circ}')
figure
plot(M1_save,P2P1)
xlabel('Upstream Mach Number (M_1)')
ylabel('{P2}/{P1}')
title('M_1 vs {P2}/{P1}')
figure
plot(M1_save,T2T1)
xlabel('Upstream Mach Number (M_1)')
ylabel('{T2}/{T1}')
title('M_1 vs {T2}/{T1}')
figure
plot(M1_save,beta_save)
xlabel('Upstream Mach Number (M_1)')
ylabel('\beta (Deg)')
title('M_1 vs \beta')
figure
plot(M1_save,betaTheta)
xlabel('Upstream Mach Number (M_1)')
ylabel('{\beta}/{\theta}')
title('M_1 vs {\beta}/{\theta}')
Shawn Alcorn
Shawn Alcorn 2022 年 8 月 31 日
M1 changes with each iteration of the for loop. It doesnt need to be reset. It only changes when the for loop starts over.

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

回答 (1 件)

Torsten
Torsten 2022 年 8 月 31 日
移動済み: Voss 2022 年 8 月 31 日
betaIter_deg (and thus betaIter) need to be reset because they are incremented in the while-loop.
  1 件のコメント
Shawn Alcorn
Shawn Alcorn 2022 年 8 月 31 日
移動済み: Voss 2022 年 8 月 31 日
that was it thank you. I had that reset earlier and must have accidentally removed it during debugging.

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

カテゴリ

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

タグ

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by