0

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) 
2
  • You should include the code that you have (preferably as a minimal example). It can help clarify your intent and any issues you're having. Commented Oct 11, 2014 at 4:51
  • I have now can you take a look? Commented Oct 14, 2014 at 23:24

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I updated the question maybe you could provide some more insight?
It would be massive help to be able to execute the code, could you supply the values of the variables called in the anonymous function as they are when entering the loop? You are plotting a 2D matrix in command plot(q), that's why you see as many curves as there are columns in the matrix. I cannot understand what is you want to obtain a 'line of best fit' for, but type "help polyfit" and input 1 for the third argument. Please clarify what you want to fit in terms of the variables in your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.