## Why I can't plot a line with my function???

Man Ting Ching

### Man Ting Ching (view profile)

さんによって質問されました 2018 年 8 月 11 日

### Stephen Cobeldick (view profile)

さんによって 編集されました 2018 年 8 月 12 日
Ameer Hamza

### Ameer Hamza (view profile)

さんの 回答が採用されました
As shown below, my function can only plot dots instead of solid lines, do anyone knows why? Thank you
%Finding indoor temperature
function [dTin] = getTin_00(Tin) %Input of function should be Tin = 5 and Theater = 40 normally
dt =0.1;
m = 4.9;
Tout = 5;
Theater = 40;
M = 0.25;
c = 1005.4;
Ar = 2.8197;
Aw = 10.2042;
Uwall = 0.0069;
Uroof = 0.0179;
dQheater = (Theater-Tin)*M*c*dt;
dQloss = (Tin-Tout)*Ar*Uroof*dt+(Tin-Tout)*Aw*Uwall*dt;
dTin = (dQheater - dQloss)/(m*c);
for t = 0:0.1:200 %increment 0.1 is equal to dt
dQheater = (Theater-Tin)*M*c*dt;
dQloss = (Tin-Tout)*Ar*Uroof*dt+(Tin-Tout)*Aw*Uwall*dt;
dTin = (dQheater - dQloss)/(m*c);
Tin = Tin+dTin;
if Tin <= 21
Theater = 40;
end
if Tin >= 24
Theater = 0;
end
figure(1);
plot(t,Tin,'.r');
title('The change in temperature over 200 seconds');
xlabel('Time');
ylabel('Internal temperature');
hold on;
grid on;
axis tight;
disp(Tin)
end
end

Man Ting Ching

### Man Ting Ching (view profile)

2018 年 8 月 11 日
Thank you! Appreciate it :D

サインイン to comment.

## 1 件の回答

### Ameer Hamza (view profile)

2018 年 8 月 11 日
採用された回答

As Stephen pointed out, you are only plotting one point at a time, therefore the can't join to form a line. One way is to assign all the values in a vector and then plot it at the end of the loop. As an alternate approach, you can use line handle object to dynamically draw as in the following example
function [dTin] = getTin_00(Tin) %Input of function should be Tin = 5 and Theater = 40 normally
dt =0.1;
m = 4.9;
Tout = 5;
Theater = 40;
M = 0.25;
c = 1005.4;
Ar = 2.8197;
Aw = 10.2042;
Uwall = 0.0069;
Uroof = 0.0179;
dQheater = (Theater-Tin)*M*c*dt;
dQloss = (Tin-Tout)*Ar*Uroof*dt+(Tin-Tout)*Aw*Uwall*dt;
dTin = (dQheater - dQloss)/(m*c);
p = plot(0,0,'r');
p.XData = [];
p.YData = [];
title('The change in temperature over 200 seconds');
xlabel('Time');
ylabel('Internal temperature');
grid on
axis tight;
for t = 0:0.1:200 %increment 0.1 is equal to dt
dQheater = (Theater-Tin)*M*c*dt;
dQloss = (Tin-Tout)*Ar*Uroof*dt+(Tin-Tout)*Aw*Uwall*dt;
dTin = (dQheater - dQloss)/(m*c);
Tin = Tin+dTin;
if Tin <= 21
Theater = 40;
end
if Tin >= 24
Theater = 0;
end
p.XData = [p.XData t];
p.YData = [p.YData Tin];
disp(Tin)
end
end

Man Ting Ching

### Man Ting Ching (view profile)

2018 年 8 月 11 日
Thank you so much! I was just confusing how to assign the values into vector and got your reply.
Stephen Cobeldick

### Stephen Cobeldick (view profile)

2018 年 8 月 11 日
" One way is to assign all the values in a vector and then plot it at the end of the loop."
That is exactly what the MATLAB documentation recommends:
The code shown in this answer expands the data arrays on each loop iteration, which will be slow as MATLAB must move the data within memory on each iteration. This is a cause of many beginners writing slow code. Better practice would be to preallocate the matrix before the loop, assign the values to it inside the loop using indexing, then plot it after the loop.
Man Ting Ching

### Man Ting Ching (view profile)

2018 年 8 月 12 日
Thank you again! Appreciate it :)

サインイン to comment.