在Matlab中使用For循环绘图

时间:2014-10-10 16:38:07

标签: matlab graph

我创建了一个使用for循环来确定ti和t值的代码。当我绘制变量时,我得到了所有的点作为散点图,我想绘制一条最佳拟合线作为线性数据。但是,数据正在绘制,因为每个点都是他们自己的数据集。有没有办法将数据收集到矩阵中以绘制线性图

clc

清除所有 关闭所有

%Heat Transfer

% I is the current passing through the conductor
I=475;

% h is the coeffcient of convective heat [W/ m^2*K] transfer
h=10;

% D is the diameter of the conductor [m]
D=0.02364;


% o is the stefan-boltzman constant [W/m^2*K^4]
o= 5.67*(10^-8);

% e is the emissivity coeffcient of the conductor
e= 0.2;

% u is the absorbivity coeffcient of the conductor
u= 0.5;

% G is the solar irradiance
G= 1200;

% r is the resistivity of the conductor per metre
r= 0.0000864;

%Qs is the Solar irradiation into the conductor
%Qs=u*D*G;

%Qg is the columbic losses generated internally
%Qg=i^2*r; 

% Qc is the convective energy flowing out of the conductor
%Qc=h*pi*D*(t-ti);

% Qr is the radiative energy out of the conductor
% Qr=o*e*D*(t^4 - ti^4);
% ti is the ambient temperature [K] 
% t is the temperature of the conductor [K]

for ti= 213:1:313
a= @(t) (h*pi*D*(t-ti))+(o*e*D*pi*(t^4 - ti^4))-((u*D*G)+(I^2*r));
t= fzero(a,0);
%fprintf('The temperature of the conductor is: %.2f K when the outside temperature is %g K \n',t,ti)
hold on
end 


%I want to plot (ti,t) 

1 个答案:

答案 0 :(得分:1)

将for循环中的数据输出收集到一个数组中,然后用它调用一次。可以插入以下内容代替for循环:

timevect = 213:313;
yvect(1, length(timevect)) = 0;
for ii = 1:length(timevect)
    ti = timevect(ii);
    a= @(t) (h*pi*D*(t-ti))+(o*e*D*pi*(t^4 - ti^4))-((u*D*G)+(I^2*r));
    t = fzero(a,0);
yvect(ii) = t;
end
plot(timevect, yvect)

请注意,现在图中轴的时间数据位于向量和ydata中。你在for循环中绘制了标量。