Clear Filters
Clear Filters

How to Plot for multiple values of a variable

1 view (last 30 days)
I am new to MATLAB and need to plot for three different numerical values of G and plot the result on a single plot. Is there an easy way to do it by putting a loop, if yes, can you please let me know how should I write it. Thanks in advance!
The code is given below
clc; clf
clear all;
format short;
syms M;
syms G;
G = input('Enter the value of Gamma of the gas used (for monoatomic=1.66, for diatomic=1.4) : ' );
%Need to plot for different values of G (1.4,1.66, 1.8);
Molecular_Weight=input('Enter the molecular weight of the gas used in grams/mole (for air=28.97): ' );
%Molecular_Weight=28.97;
R=8314.46261815324/Molecular_Weight;
Throat_Diameter=2.8;
Throat_Area=3.14159265.*(10.^(-6)).*((Throat_Diameter).^2)/4;
Exit_Diameter=5.76;
Exit_Area_inMeter=3.14159265.*(10.^(-6)).*((Exit_Diameter).^2)/4;
Diverging_Length= input('Enter the length of diverging section in mm: ' );
DFT=[1:1:Diverging_Length];
M_Local=zeros([1 numel(DFT)]);
for d=1:numel(DFT)
Distance_from_throat=DFT(d);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter).*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G-1).*M.^2)/(G+1)).^((G+1)/(G-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local(d) = fzero(problem); % Solve for supersonic M
end
plot(DFT,M_Local);
title('Distance from Throat vs Mach Number');
xlabel('Distance from Throat (in mm)');
ylabel('Mach Number');
hold on

Answers (1)

Star Strider
Star Strider on 9 Apr 2021
It would likely be easiest to define ‘G’ as a vector:
Gv = [1.4,1.66, 1.8];
and then add an additional loop for it:
for k = 1:numel(Gv)
G = Gv(k);
for d=1:numel(DFT)
Distance_from_throat=DFT(d);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter).*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G-1).*M.^2)/(G+1)).^((G+1)/(G-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local(d,k) = fzero(problem); % Solve for supersonic M
end
end
plot(DFT,M_Local);
title('Distance from Throat vs Mach Number');
xlabel('Distance from Throat (in mm)');
ylabel('Mach Number');
legend(compose('G = %.2f',Gv), 'Location','best')for k = 1:numel(Gv)
G = Gv(k);
for d=1:numel(DFT)
Distance_from_throat=DFT(d);
Section_Diameter=Throat_Diameter+((Exit_Diameter-Throat_Diameter).*Distance_from_throat)/Diverging_Length;
Section_Area=3.14159265.*(10.^(-6)).*((Section_Diameter).^2)/4;
ARatio=Section_Area/Throat_Area;
problem.objective = @(M) (1/M.^2).*(((2+(G-1).*M.^2)/(G+1)).^((G+1)/(G-1)))-ARatio.^2; % Objective function
problem.solver = 'fzero'; % Find the zero
problem.options = optimset(@fzero); % Default options
% Solve supersonic root
problem.x0 = [1+1e-6 50]; % Supersonic solver bounds
M_Local(d,k) = fzero(problem); % Solve for supersonic M
end
end
plot(DFT,M_Local);
title('Distance from Throat vs Mach Number');
xlabel('Distance from Throat (in mm)');
ylabel('Mach Number');
legend(compose('G = %.2f',Gv), 'Location','best')
.

Categories

Find more on Line Plots 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!