Clear Filters
Clear Filters

Hi beginner need help with first time trying to use "for loop" to make a graph

1 view (last 30 days)
function [dT] = getdT (h,w,l,a,Tout,Theater,M)
for dt = 1:1:200 %in all honesty i just saw other people write this i have no clue what the other 1 is for
dT = 0 ;
Tin = 20+dT ;
[ V,Awall,Aroof,m ] = buildingProperties( h, w, l, a);
[Qloss] = getQloss(Tin,Tout,dt,h,w,l,a);
[Qheat] = getQheater(Tin,Theater,M,dt);
c = 1005.4;
dti = Qheat- Qloss ;
dT = 1/(m*c)*dti;
disp(Tin)
figure(1)
%dt = 1:200
plot(dt,Tin)
xlabel('dt');
ylabel('Tin');
title('Plot of dT vs dt')
end
im trying to draw a standard plot graph between time(dt) and temperature (Tin) , dt from 1 second to 200, thanks for the help
  1 Comment
Bob Thompson
Bob Thompson on 3 Dec 2018
A few things:
1) Using 1:1:200 as your index is unnecessary. Defining an index takes the form of First:Interval:Last, where the default index is 1.
2) Tin will always be 20. Although you recalculate dT later in the loop, the value of dT is reset to 0 each time the loop runs, so Tin is just 20+0 every iteration. If you are looking to incrementally increase dT with each loop, you will need to move the dT=0 line outside the loop.
3) For each iteration of your loop, dt is a single value, so using plot(dt,Tin) is going to give you a single point each iteration. If this is what you want then that is fine, but because you do not have a hold command enabled you will only be writing a single point each loop, erasing the previous plot each time.
4) I would suggest indexing the dt and Tin values into an array within the loop, and then running the plot command once after the loop has completed. If you are looking for a series of points with connecting lines then plot(x,y) will work just fine, but if you want to have a scattering of individual points, then you can use the scatter() command.

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 3 Dec 2018
barry - since you are calling plot on each iteration of your for loop, then any graphics object that had been drawn on your previous iteration will be removed/deleted. You can get around this by using hold on but I don't think that you really need to do this.
Unless you have a requirement to show the updated plot on each iteration of the loop, you could just store (in an array) all the Tin values from each iteration and then do one plot outside of the loop.
You may want to review your code though
for dt = 1:1:200 %in all honesty i just saw other people write this i have no clue what the other 1 is for
dT = 0 ;
Tin = 20+dT ;
The dt and Dt are confusing - should they be different? And Tin will always be the same for each iteration of the loop. Is this intentional?
As for you comment, please review the MATLAB documentation to understand that the second one in
for dt = 1:1:200
is the step size.
  1 Comment
Geoff Hayes
Geoff Hayes on 4 Dec 2018
Try plotting after the loop has completed
Tin = zeros(1,200);
for dt = 1:1:200
% your code here
% set Tin
Tin(dt) = ...; % whatever this should be
end
plot(1:1:200,Tin);
xlabel('dt');
ylabel('Tin');
title('Plot of dT vs dt')
So we store all the Tin values on each iteration of the loop. Once the loop has completed, then we just call plot once with the collected data. Though this is where the code is a little confusing: are you plotting dt or dT? Why is the title Plot of dT vs dt.

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!