Patrick Bailey
Patrick Bailey

Reputation: 25

Plotting with a For loop in Matlab

I've made a code which uses a for loop to determine values of ti and t. When I plot the variables I get all my points as a scatter plot and I want to plot a line of best fit as the data in linear. However, the data is being plotting as each point being their own data set. is there a way to collect the data into a matrix to plot a linear graph

clc

clear all close all

%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) 

Upvotes: 0

Views: 171

Answers (1)

danny
danny

Reputation: 306

Gather the data output in your for loop into a single array, then call plot once with that. The following can be inserted in place of your for loop:

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)

Note that now the time data for your axis in the plot is in a vector and the ydata too. You were plotting scalars in a for loop.

Upvotes: 1

Related Questions