Vectorized code running slower than normal code?



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
.



Relevant Pages

  • Re: Carlesons proof ???
    ... solve that PDE when the temperature at the boundary point,sin) ... We always take this to mean the limit of the partial sums, ... sum f_n, the spike is narrow, and its width tends to zero as n ... Arguably it is the last notion of convergence which best corresponds ...
    (sci.math)
  • Re: How to keep part at constant temperature
    ... The loop will probably be tricky to ... Use duty cycle modulation to drive the heaters to avoid e^2 ... be able to slowly ramp up the duty cycle to the heating resistors till ... temperature at 40C. ...
    (sci.electronics.design)
  • Re: loops?!?
    ... simple temperature conversion program, and it works great. ... I can't get it to loop. ... System.out.println("Welcome to the temperature converter program"); ... System.out.println("Please choose the temperature format you wish to convert from, C or c for Celsius, F or f for Fahrenheit"); ...
    (comp.lang.java.help)
  • Re: How to keep part at constant temperature
    ... These parts are very temperature sensitive, so I'm hoping to find a way ... The loop will probably be tricky to ... slap a Minco heater or a few power fets on the bottom and a ... surface-mount thermistor on top, close that loop at 60C, and populate ...
    (sci.electronics.design)
  • Re: Thermocouple Problem...
    ... you need to look at the metals and the ... have a different temperature at each end**, ... meter with a Manganin swamp resistor, the loop looks like (use Courier ... the only voltages left are those generated in the Chromel ...
    (sci.electronics.design)

Loading