MATLAB: 1D Heat Conduction using explicit Finite Difference Method

1d heat conductionMATLAB

Hello I am trying to write a program to plot the temperature distribution in a insulated rod using the explicit Finite Central Difference Method and 1D Heat equation. The rod is heated on one end at 400k and exposed to ambient temperature on the right end at 300k. I am using a time of 1s, 11 grid points and a .002s time step. When I plot it gives me a crazy curve which isn't right. I think I am messing up my initial and boundary conditions. Here is my code.
L=1;
t=1;
k=.001;
n=11;
nt=500;
dx=L/n;
dt=.002;
alpha=k*dt/dx^2;
T0(1)=400;
for j=1:nt
for i=2:n
T1(i)=T0(i)+alpha*(T0(i+1)-2*T0(i)+T0(i-1));
end
T0=T1;
end
plot(x,T1)

Best Answer

It seems your initial condition and boundary conditon (x = L) are missing in the code. Try
L=1;
t=1;
k=.001;
n=11;
nt=500;
dx=L/n;
dt=.002;
alpha=k*dt/dx^2;
T0=400*ones(1,n);
T1=300*ones(1,n);
T0(1) = 300;
T0(end) = 300;
for j=1:nt
for i=2:n-1
T1(i)=T0(i)+alpha*(T0(i+1)-2*T0(i)+T0(i-1));
end
T0=T1;
end
plot(T1)
Related Question