Reputation: 1
I got an assignment in class and was able to do a majority of it but it is giving me some trouble. If I manually copy and paste my code into the command window then I can get everything to run and plot but if just call on the function it will give me outputs but will not plot for some reason. any ideas?
I also tried my friends code which in my eyes is almost identical except, for some reason his allows him to call on it and it will plot. I've already been working on just this small amount for 6 hours today and any help is greatly appreciated.
Thanks!
function [totalheat1,totalheat2] = dheat(Tday,Tnight)
foot=10*sum('Todd');
height=9; %ft
Ctm=0.35; %BTU/lb degF
mt= 20000; %lbs
A= foot + 4*((sqrt(foot)*height)); %surface area of house (ft^2)
R=25; %degF*ft^2*hour/BTU
To=30; %degF
dt=0.1;
t=dt:dt:24;
n=24/dt;
Td=Tday;
Tn=Tnight;
%regime 1
for i=1:n
Q1(i)=dt*((A*(Td-To))/(R));
end
%regime 2
therm=[1:240];
therm(1:70)=Tn;
therm(71:100)=Td;
therm(101:160)=Tn;
therm(161:230)=Td;
therm(231:240)=Tn;
Td=therm(1);
for i=1:n
Q=dt*((A*(Td-To))/(R));
chgT=(Q)/(Ctm*mt);
Ti=Td-chgT;
if Ti< therm(i)
Q=(therm(i)-Ti)*(Ctm*mt);
if Q<3000
F(i)=Q;
temp(i)=Td;
else
F(i)=3000;
temp(i)=Ti+((3000)/(Ctm*mt));
end
else
F(i)=0;
temp(i)=Ti;
end
Td=temp(i);
end
totalheat1=sum(Q1);
totalheat2=sum(F);
figure(1)
plot(t,temp)
figure(2)
plot(t,Q1,t,F)
end
Upvotes: 0
Views: 143
Reputation: 136
First, Make sure you hit close all force
before calling your function.
This will close all open (but maybe not visible) figures.
Next, this snippet explicitly tells which figure and axes to use:
f1 = figure();
ax1 = axes(f1);
p1 = plot(ax1, t,temp);
f2 = figure();
ax2 = axes(f2);
p2 = plot(ax2, t, Q1, t, F);
If it still does not work:
try passing a struct
containing handles
to those figures/axes as a third parameter of your function. in a script:
s = struct();
s.f1 = figure();
s.ax1 = axes(f1);
s.f2 = figure();
s.ax2 = axes(f2);
dheat(Tday,Tnight,s)
and the function:
function [s,totalheat1,totalheat2] = dheat(Tday,Tnight,s)
and the plotting part would then look like:
p1 = plot(s.ax1, t,temp);
p2 = plot(s.ax2, t, Q1, t, F);
Personally, I would prefer returning the calculated values and calling plot specific things from a script. p1
and p2
could be omitted.
Upvotes: 1