How to Plot for multiple values of a variable

조회 수: 3 (최근 30일)
Saumya Nagar 17BME0447
Saumya Nagar 17BME0447 2021년 4월 9일
답변: Star Strider 2021년 4월 9일
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

답변 (1개)

Star Strider
Star Strider 2021년 4월 9일
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')
.

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by