Index exceeds the number of array elements (101).
Show older comments
But i get this message below. (message in bold)
incorrectCheck the temperature vector with unknown time vector
Index exceeds the number of array elements (101).
Error in tutorial3 (line 42) t = time(i); % time in seconds Error in Test1
Could you help me with this?
function [T,aliveFlag] = heat_eqn(timeend,nt)
% Description: This file implements the Fourier Series solution to the heat equation.
%
% Input parameters:
% - timeend - time of simulation in seconds
% - nt - number of points in time vector
%
% Output parameters:
% - T - temperature profile along persons head
% - aliveFlag - 1 if person is alive, 0 if the person is dead
L = 0.12; % Size of the domain
rstart = 0; % Start of computational domain (m)
rend = L; % End of computational domain (m)
nr = 101; % Number of grid points along the radius vector
dr = (rend-rstart)/(nr-1); % Spatial step size, delta r (m)
r = rstart:dr:rend; % Vector of grid points in r
% Temporal (t) parameters
timestart = 0; % Starting time (0 seconds)
dt = (timeend-timestart)/(nr-1); % Temporal step size, delta t (s)
time = timestart:dt:timeend; % Vector of times
% Phyiscal parameters
T1 = -41; % Temperature at r=0
T2 = 37; % Temperature at r=L
A = T2-T1; % Amplitude of the saw tooth wave
k = 0.527; % Thermal conductivity (W/m-K)
rho = 1000; % Density (kg/m^3)
gamma = 3600; % Heat capacity (J/kg-K)
c = sqrt(k/(rho*gamma)); % Heat equation constant c^2=k/(rho*sigma)
% Calculate B coefficients using a for loop
nfs = 1000; % Number of fourier terms
B = zeros(1,nfs); % Initialise B vector
lambda=zeros(1,nfs); % Initialise lambda vector
for n = 1:nfs;
B(n)=(2*A)/(n*pi); % Calculate B coefficients
lambda(n)=(n*pi*c)/L;
end
%% Solve for T using three for loops in time, space, and Fourier series
% Loop through time
for i = 1:nt % For each time
t = time(i); % time in seconds
% Vector of zeros to initialise the Fourier series solution.
% This should be re-initialised at each new time step.
T = zeros(1,nr);
% Loop through space
for j = 1:nr; % For each grid point
T(j) = (78*r(j))/L-41; % Add the steady state solution
% Loop through the Fourier series
for n = 1:nfs;
% Calculate series sum for T at r(j) and t
p=(n*pi)/L;
T(j) = T(j)+(156/(n*pi))*sin(n*pi*r(j)/L)*exp(-1*p^2*c^2*t);
end
end
end
aliveFlag= mean(T)>22;
end
1 Comment
KSSV
on 16 Mar 2019
What input you have tried?
Answers (1)
KALYAN ACHARJYA
on 16 Mar 2019
Edited: KALYAN ACHARJYA
on 16 Mar 2019
I dont think there is any coding error. Please try in following way
function [T,aliveFlag] = heat_eqn(timeend,nt)
% Description: This file implements the Fourier Series solution to the heat equation.
% Input parameters:
% - timeend - time of simulation in seconds
% - nt - number of points in time vector
% Output parameters:
% - T - temperature profile along persons head
% - aliveFlag - 1 if person is alive, 0 if the person is dead
L = 0.12; % Size of the domain
rstart = 0; % Start of computational domain (m)
rend = L; % End of computational domain (m)
nr = 101; % Number of grid points along the radius vector
dr = (rend-rstart)/(nr-1); % Spatial step size, delta r (m)
r = rstart:dr:rend; % Vector of grid points in r
% Temporal (t) parameters
timestart = 0; % Starting time (0 seconds)
dt = (timeend-timestart)/(nr-1); % Temporal step size, delta t (s)
time = timestart:dt:timeend; % Vector of times
% Phyiscal parameters
T1 = -41; % Temperature at r=0
T2 = 37; % Temperature at r=L
A = T2-T1; % Amplitude of the saw tooth wave
k = 0.527; % Thermal conductivity (W/m-K)
rho = 1000; % Density (kg/m^3)
gamma = 3600; % Heat capacity (J/kg-K)
c = sqrt(k/(rho*gamma)); % Heat equation constant c^2=k/(rho*sigma)
% Calculate B coefficients using a for loop
nfs = 1000; % Number of fourier terms
B = zeros(1,nfs); % Initialise B vector
lambda=zeros(1,nfs); % Initialise lambda vector
for n = 1:nfs;
B(n)=(2*A)/(n*pi); % Calculate B coefficients
lambda(n)=(n*pi*c)/L;
end
%% Solve for er series
% Loop thT using three for loops in time, space, and Fourirough time
for i = 1:nt % For each time
t = time(i); % time in seconds
% Vector of zeros to initialise the Fourier series solution.
% This should be re-initialised at each new time step.
T = zeros(1,nr);
% Loop through space
for j = 1:nr; % For each grid point
T(j) = (78*r(j))/L-41; % Add the steady state solution
% Loop through the Fourier series
for n = 1:nfs;
% Calculate series sum for T at r(j) and t
p=(n*pi)/L;
T(j) = T(j)+(156/(n*pi))*sin(n*pi*r(j)/L)*exp(-1*p^2*c^2*t);
end
end
end
aliveFlag=mean(T)>22;
end
Save the function as heat_eqn.m and call the function file in different script or from command window-
>>
[Temp_profile,Flag]=heat_eqn(10,5)
%............................^..^ choose the value as per your requirements
Temp_profile =
Columns 1 through 13
-41.0000 36.9646 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 14 through 26
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 27 through 39
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 40 through 52
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 53 through 65
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 66 through 78
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 79 through 91
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Columns 92 through 101
37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000 37.0000
Flag =
1
From the last line of the code
aliveFlag=mean(T)>22;
AliveFlage is 1 if the Temp_profile mean is more than 22, which is seen from the Temp_profile vector, there from aliveFlag giving 1 as per description in your code.
5 Comments
changsu namgung
on 16 Mar 2019
KALYAN ACHARJYA
on 17 Mar 2019
Can you show the pass values to the function heat_eqn(10,5)?
See my above example, its working.
changsu namgung
on 17 Mar 2019
KALYAN ACHARJYA
on 17 Mar 2019
Edited: madhan ravi
on 17 Mar 2019
I have tried on 2015a, no issue. sorry I dont have any other version or Matlab grader .
changsu namgung
on 18 Mar 2019
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!