MATLAB: Index exceeds matrix dimensions

finite differenceindex

Hi, I have been tasked to find solve a heat diffusion equation by a finite difference method, and this error pops up on my code. Any ideas?
%%Parameters
L = 50e-5; %m
alpha0 = 3.71e-3; %m^2/s
Q = 137653.6;
R = 8.314;
Tm = 0; %C

Ttop = 0; %C
tmax = 3.71e-3; % in seconds !!
T0 = 473.15;
t1 = (1:5);
r = 10;
Te = T0-r.*t1;
%%Discretisation
Dx = 50e-6;
Dtmax = 0.5*Dx^2/alpha0;
Dt = 0.5*Dtmax;
%time vector:
t = 0:Dt:tmax;
%space vector:
x = 0:Dx:L;
%number of nodes in time and space
N = length(t);
I = length(x);
%%initialisation
T = zeros(N,I);
%initial conditions:
T(1,:) = Tm;
T(1,1) = Ttop;
%%iterations
for n=1:N-1
%boundary conditions:
T(n+1, 1) = Ttop;
T(n+1, I) = Tm;
%interior points:
for in=2:I-1
a_ip = 0.5*(alpha0*exp(-(Q./R*Te(in+1))+ alpha0*exp(-(Q./R.*Te(in)))));%this is where the error lies
a_im = 0.5*(alpha0*exp(-(Q./R.*Te(in))+ alpha0*exp(-(Q./R.*Te(in-1))))) ;
T(n+1, in) = T(n, in) + (Dt/Dx^2)*(a_ip*(T(n,in+1) - T(n,in)) -...
a_im*(T(n,in) - T(n,in-1)));
end
end
%%plots
figure;
[c, hc] = contour(t/(1e6*24*3600*365), -x/1e3, T', 'k');
clabel(c, hc);
xlabel('time (My)')
ylabel('depth (km)')

Best Answer

Te is a vector of length 5. You are accessing it with index "in", which has a maximum value derived from length(x), but x is length 11 it appears to me.