Vectorized code running slower than normal code?
- From: Mike <mstachowsky@xxxxxxxxx>
- Date: Sun, 22 Jul 2007 22:30:57 -0400
Hello all,
I wrote a piece of code using a for loop nested in a while loop.
when I ran it, it took about 6.5 seconds to reach convergence (it is
an iterative method of solving the one dimensional Laplace equation).
I replaced the for loop with vectorized code...and it takes about 6
times longer to run. what's the deal? below is the code, with the
for loop commented out.
%RadRod1Dvec.m
%
%The purpose of this program is to figure out how much of a problem
will be
%radiation coming from the surrounding areas to the heat path. It
will use
%the iterative method of converging onto a solution, and will assume
that
%the steady state of temperature has already been reached.
%
%The numbers used in this program are either taken from the
specifications
%we've so far come up with, or are arbitrary and can be changed
later.
%
%NOTE: This program is identical in output to RadRod1D.m, however it
uses
%vector methods in preparation for two dimensional models of the
radiator.
%check performance
tic
%Generate the rod
L = 0.07; %0.07m length
deltaX = L/100;
x = 0:deltaX:L; %the "mesh" for the 1D simulation
%Generate the temperature matrices
Temp = zeros(1,length(x)); %set the initial temperature to 230K.
This is our initial, arbitrary guess.
oldTemp = ones(1,length(x)) * 230; %we need to keep a record of the
old temperature for the iterations to work
Temp(end) = 230;
oldTemp(end) = 230;
%general constants
k_Copper = 385; %385W/mK thermal conductivity
conductiveArea = (pi * (0.015/2)^2); %assume the heat path is a
cylinder of radius 1.5cm
radiatingArea = (pi * 0.015) * deltaX; %the surface area of each
element
emissivity = 0.9; %the emissivity of the rod. completely arbitrary
at this point
Tamb = 300; %300K ambient temperature
Stephan_Boltzmann = 5.67e-8;
n = 1; %a counter variable for testing
convergence = 5e-8; %the convergence criterion: when the average
temperature of the present run is within "convergence" of the average
%temperature of the last run. I chose a very small number because
this
%code runs quickly anyway
R = deltaX/(k_Copper * conductiveArea); %a convenient variable for
the thermal resistance between two elements
heatLoad = 3; %assume 3W has to be dissipated from the CCDs. we can
change this later
%a variable to calculate the average temperature difference. This is
just
%for housekeeping
avgVars = mean(Temp) - mean(oldTemp);
%A bit of a strange thing happening: the bow is going the wrong way.
This
%may be due to the qi being positive when in fact it should be
negative.
%I'll keep it negative for now, but it just requires the changing of
a
%negative sign. For a neat note: if I do make the qi negative, the
bow
%does exactly what I'd expect it to.
qi = -emissivity * radiatingArea * Stephan_Boltzmann .*
(oldTemp(:).^4 - Tamb^4);
while(abs(avgVars) > convergence)
%set the initial conditions
Temp(1) = (heatLoad - emissivity * radiatingArea *
Stephan_Boltzmann * (oldTemp(1)^4 - Tamb^4) + (oldTemp(2)/R))* R;
%where above we assumed that the heat into the first element is
%constant
qi = -emissivity * radiatingArea * Stephan_Boltzmann .*
(oldTemp(:).^4 - Tamb^4);
Temp(2:end-1) = (qi(2:end-1)' + (Temp(1:end-2)./R +
oldTemp(3:end)./R))./(2./R);
% for i = 2:(length(x)-1)
% qi = -emissivity * radiatingArea * Stephan_Boltzmann *
(oldTemp(i)^4 - Tamb^4);
% Temp(i) = (qi + (Temp(i-1)/R + oldTemp(i+1)/R))/(2/R);
% end
%increment the count
n = n + 1;
%recompute the difference between the last two runs
avgVars = mean(Temp) - mean(oldTemp);
%change the old temperature to the new one
oldTemp = Temp;
end
%plot everything
figure(1)
clf %clear the existing figure
plot(x,Temp)
title('Temperature Variations Across Heat Path')
xlabel('Length [m]')
ylabel('Temperature [K]')
hold on
y = Temp(1):(-(Temp(1) - Temp(end))/100):230;
plot(x,y,'r')
legend('Actual', 'Reference line')
%t
.
- Prev by Date: Re: Comparing Two Models
- Next by Date: Re: out of memory problem
- Previous by thread: decimal comma instead decimal point
- Next by thread: Re: how to impose text on a graphic image and save t o file?
- Index(es):
Relevant Pages
|
Loading