由于某种原因,Matlab不会绘制我的函数

问题描述 投票:0回答:1

我在课堂上得到了一项任务,并且能够完成大部分工作,但这给了我一些麻烦。如果我手动将我的代码复制并粘贴到命令窗口,那么我可以将所有内容运行并绘制,但如果只是调用该函数,它将为我提供输出但不会出于某种原因进行绘图。有任何想法吗?

我也尝试过我的朋友代码,在我看来几乎是相同的,除了因为某些原因他允许他调用它并且它会绘制。我今天已经在这个小小的工作量上工作了6个小时,非常感谢任何帮助。

谢谢!

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
matlab plot matlab-figure
1个回答
1
投票

首先,确保在调用函数之前点击close all force。这将关闭所有打开(但可能不可见)的数字。

接下来,此片段明确告知要使用的图形和轴:

f1 = figure();
ax1 = axes(f1);
p1 = plot(ax1, t,temp);

f2 = figure();
ax2 = axes(f2);
p2 = plot(ax2, t, Q1, t, F);

如果它仍然不起作用:尝试将包含structhandles传递给那些数字/轴作为函数的第三个参数。在脚本中:

s = struct();
s.f1 = figure();
s.ax1 = axes(f1);

s.f2 = figure();
s.ax2 = axes(f2);

dheat(Tday,Tnight,s)

和功能:

function [s,totalheat1,totalheat2] = dheat(Tday,Tnight,s)

然后绘图部分看起来像:

p1 = plot(s.ax1, t,temp);
p2 = plot(s.ax2, t, Q1, t, F);

就个人而言,我更愿意返回计算值并从脚本中调用特定于事件的事物。 p1p2可以省略。

© www.soinside.com 2019 - 2024. All rights reserved.