MATLAB Answers

Sabrina
0

I am getting a blank graph when I try to plot a while loop. I am not sure why. My code is below. Thank you MATLAB

Asked by Sabrina
on 16 Sep 2019 at 17:20
Latest activity Answered by Walter Roberson
on 16 Sep 2019 at 17:39
%Surface Area (m^3)
SA=4.096e-6;
%Cooling rate goes from 20 to 100W
disp('Outputs')
CR=20;
hold on
while (CR<=100)
HF=CR/SA;
fprintf('Cooling Rate = %g Heat Flux = %g\n',CR,HF)
CR=CR+10;
plot(CR,HF,'c-','LineWidth',2)
end
hold off

  0 Comments

Sign in to comment.

2 Answers

Answer by Adam Danz
on 16 Sep 2019 at 17:31
 Accepted Answer

On each iteration of your while-loop CR and HF produce 1 coordinate but you're asking to draw a line which requres 2 coordinates.
Plotting within a loop is generally avoidable. You could collect your data within a loop and then plot it after the loop. Better yet, you don't need a loop at all.
%Surface Area (m^3)
SA=4.096e-6;
%Cooling rate goes from 20 to 100W
CR=20 : 10 : 100;
HF=CR/SA;
plot(CR,HF,'c-','LineWidth',2)

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 16 Sep 2019 at 17:39

You are plotting one point at a time but you have no marker specified.
MATLAB never deliberately joins points from different plot() calls.
You can specify a marker. Or you can save all of the points and plot them all after the loop. Or you can use animatedLine()

  0 Comments

Sign in to comment.