code 1
code 1
1. Problem Setup
gamma = 1.4; % Specific heat ratio for air
P0 = 101325; % Stagnation (total) pressure, Pa
T0 = 300; % Stagnation (total) temperature, K
R = 287; % Gas constant for air, J/kg-K
% ---- (B) Build M(x) along the nozzle using isentropic area relation ----
M_profile = zeros(size(x_vals));
for j = 1:length(x_vals)
% Local area ratio:
A_local = A_of_x(j);
% A* known from the nozzle geometry (the minimum)
ARatio = A_local / A_throat;
% ---- (D) Mass Flow Rate (if choked, depends on throat) ----
if isChoked
% Choked formula at the throat:
Tt = T0 / (1 + (gamma-1)/2 * M_throat(i)^2);
at = sqrt(gamma * R * Tt);
rho0 = P0/(R*T0);
% Velocity at throat:
Vthroat = M_throat(i)*at;
A_th = A_throat; % throat area
mdot = rho0 * Vthroat * A_th; % approximate isentropic
else
% Not choked, approximate subsonic entire nozzle
% Just take final cross-section as example (not strictly correct)
T_exit = T0 / (1 + (gamma-1)/2 * M_exit(i)^2);
a_exit = sqrt(gamma * R * T_exit);
rho0 = P0/(R*T0);
Vexit = M_exit(i)*a_exit;
A_ex = A_of_x(end);
mdot = rho0 * Vexit * A_ex;
end
mdot_array(i) = mdot;
end
figure(3);
plot(x_vals, P_profile, 'b-o','LineWidth',2);
xlabel('x'); ylabel('P(x)/P_{0}');
title(['Pressure Ratio vs x for P_{\infty}/P_{0} = ',num2str(Pinf_over_P0)]);
grid on;
end
6. Plot the Summaries vs P_{\infty}/P_{0}
figure(4);
plot(Pinf_over_P0_list, mdot_array, 'ko-','LineWidth',2);
xlabel('P_{\infty}/P_{0}');
ylabel('Mass Flow Rate (kg/s)');
title('m_{dot} vs P_{\infty}/P_{0}'); grid on;
figure(5);
plot(Pinf_over_P0_list, M_inlet, 'r-o','LineWidth',2); hold on;
plot(Pinf_over_P0_list, M_throat, 'g-o','LineWidth',2);
plot(Pinf_over_P0_list, M_exit, 'b-o','LineWidth',2);
legend('M_{inlet}','M_{throat}','M_{exit}');
xlabel('P_{\infty}/P_{0}'); ylabel('Mach Number');
title('M_{inlet}, M_{throat}, M_{exit} vs P_{\infty}/P_{0}'); grid on;
figure(6);
plot(Pinf_over_P0_list, Pexit_over_P0, 'm-o','LineWidth',2);
xlabel('P_{\infty}/P_{0}');
ylabel('P_{exit}/P_{0}');
title('Exit Pressure Ratio vs P_{\infty}/P_{0}');
grid on;
--- Supporting Functions ---
function F = areaMachEq(M, ARatio, gamma)
% Isentropic area--Mach equation residual: we want F=0
% ARatio = (1/M)*[ (2/(gamma+1))*(1+((gamma-1)/2)*M^2 ) ]^((gamma+1)/(2*(gamma-1)))
term = (2/(gamma+1))*(1+(gamma-1)/2*M^2);
F = ARatio - (1/M)*term^((gamma+1)/(2*(gamma-1)));
end
A_throatLocal = min(A_of_x);
M_profile = zeros(size(x_vals));
for j = 1:length(x_vals)
ARatio = A_of_x(j)/A_throatLocal;
if isChoked
% subsonic for x < throat, supersonic for x > throat
if j <= idx_throat
M_sub = fsolve(@(M) areaMachEq(M,ARatio,gamma), 0.2, ...
optimset('Display','off'));
M_profile(j) = M_sub;
else
M_sup = fsolve(@(M) areaMachEq(M,ARatio,gamma), 2.0, ...
optimset('Display','off'));
M_profile(j) = M_sup;
end
else
% fully subsonic
M_sub = fsolve(@(M) areaMachEq(M,ARatio,gamma), 0.2, ...
optimset('Display','off'));
M_profile(j) = M_sub;
end
end
% Isentropic P/P0
P_profile = (1 + (gamma-1)/2 .* M_profile.^2).^(-gamma/(gamma-1));
end