How to Plot a Function Contained in a for Loop (Plots not showing up)

11 views (last 30 days)
Hey guys I have a code in which a function is contained within a for loop. I'm trying to plot the output values against the number of times my for loop runs. When I try to plot my graphs I get them, however, there's no actual plots on the graph. All I get is an empty graph however the x and y coordinates are ranged correctly. Anyone have any idea how I can make the values appear on the graphs? I've also going into the graphs Property Editor and tried assign values there as well but to no avail. When I tried to plot something simple I was about to get a line on my graph so not too sure what's wrong. Thanks in advance!
for r = 10:1:20
[massratio,nth] = DesignProj(r)
end
%massratio versus r
figure(1)
plot(r,massratio)
xlabel('Pressure Ratio')
ylabel('Mass Ratio')
title('Mass ratio versus Pressure Ratio')
%nth versus mass
figure(2)
plot(nth,massratio)
xlabel('Thermal Efficency')
ylabel('Mass Ratio')
title('Thermal Efficency versus Pressure Ratios')
The Function is as follows
function [ massratio, nth ] = DesignProj( r )
T8 = 300; % K
h8 = 300.19; % kj/kg
Pr8 = 1.386;
nC = 0.82;
T10 = 1100; % K
h10 = 1161.07;
Pr10 = 167.1;
nT = 0.86;
T12 = 420;
h12 = 421.26;
h1 = 191.81;
v1 = 0.00101; % m^3/kg
P2 = 800;
P1 = 10;
h3 = 720.87;
v3 = 0.001115;
P4 = 5000;
P3 = 800;
P5 = 5000; % kpa
T5 = 350; % C
h5 = 3069.3;
s5 = 6.4516;
P6 = 800;
s6s = 6.4516;
s6f = 2.0457;
s6fg = 4.6160;
h6f = 720.87;
h6fg = 2047.5;
P7 = 10;
s7 = 6.4516;
s7f = 0.6492;
s7fg = 7.4996;
h7f = 191.81;
h7fg = 2392.1;
wdotnet = 280000; % kw or kj/s
% Interpolation Values
y1 = 15.31;
y2 = 14.38;
x1 = 596.52;
x2 = 586.04;
Pr9 = r*Pr8;
h9s = x2 + ((x1-x2).*(Pr9-y2))/(y1-y2);
h9 = h8 + ((h9s-h8)/nC);
Pr11 = (1/r).*Pr10;
h11s = x2 + ((x1-x2).*(Pr11-y2))/(y1-y2);
h11 = h10 - nT.*(h10-h11s);
w1in = v1.*(P2-P1);
h2 = h1 + w1in;
w2in = v3.*(P4-P3);
h4 = h3 + w2in;
x6s = (s6s-s6f)/s6fg;
h6s = h6f+x6s.*h6fg;
h6 = h5-nT.*(h5-h6s);
x7s = (s7-s7f)/s7fg;
h7s = h7f+x7s.*h7fg;
h7 = h5-nT.*(h5-h7s);
massratio = (h5-h4)/(h11-h12);
y = (h3-h2)/(h6-h2);
wT = nT.*(h5-h6+(1-y).*(h6-h7));
wNS = wT-(1-y).*w1in-w2in;
wNG = (h10-h11)-(h9-h8);
wN = wNG+(1/massratio).*wNS;
mA = wdotnet/wN;
qdotin = mA.*(h10-h9);
nth = wdotnet/qdotin;
end

Accepted Answer

Geoff Hayes
Geoff Hayes on 22 Nov 2015
Marshall-Richard - stepping through the code with the MATLAB Debugger would go a long way to helping you solve this problem. Consider your for loop
for r = 10:1:20
[massratio,nth] = DesignProj(r)
end
You are iterating from 10 through to 20 with a step size of one which is the r you pass in to your function DesignProj. The outputs from this function are the massratio and nth parameters...which get overwritten on subsequent iterations of your for loop! So when you go to plot the values with
plot(r,massratio)
or
plot(nth,massratio)
you are plotting single values from the last iteration of your for loop. Either you call plot on each iteration of the for loop or collect the output from DesginProj into an array and plot that array data. If attempting the latter, then you could do something like the following
r = 10:1:20;
massratioData = zeros(length(r),1);
nthData = zeros(length(r),1);
for k = 1:length(r)
[massratioData(k),nthData(k)] = DesignProj(r(k));
end
%massratio versus r
figure(1)
plot(r,massratioData)
xlabel('Pressure Ratio')
ylabel('Mass Ratio')
title('Mass ratio versus Pressure Ratio')
%nth versus mass
figure(2)
plot(nthData,massratioData)
xlabel('Thermal Efficency')
ylabel('Mass Ratio')
title('Thermal Efficency versus Pressure Ratios')
  1 Comment
Marshall-Richard Cuico
Marshall-Richard Cuico on 23 Nov 2015
This was exactly what I was looking for and the explanation was spot on! I'm still having trouble gaining the correct amount of knowledge to be able to do this on my own so thank you for your time and effort to be able to go through my code and guide me to the correct solution.
Thank you Geoff Hayes!
Marshall-Richard Cuico

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!