I am trying to solve the following 1-D heat equation with provided boundary conditions using explicit scheme on Matlab. I have been trying to plot the results but I realized that my temperatures are not changing. How can I solve problem and include the boundary condition?

clc; clear all; close all; 

U_Initial= 30+273.15; %converting to kelvin
U_Wall = 20+273.15; %converting to kelvin
U_BC = 100+273.15; %converting to kelvin
dt= 0.01;
dr= 1;
Lambda= (a*dt)/(dr)^2;
r= 3:dr:100;
U = zeros(length(r),length(t));
U(:,1)= U_Initial;
for j= 1:1:length(t)-1
   for i=2:1:length(r)-1
     U(i,j+1)= U(i,j)+Lambda*((dr/r(i))*(U(i+1,j)-U(i-1,j))+ (U(i+1,j)-2*U(i,j)+U(i-1,j)));

1 Answer 1


This stackexchange doesn't really seek to solve these homework-like problems directly. However, I can offer some hints on how to proceed.

Are you sure the scale of a and lambda are correct? When I assign a=0.1, a reasonable profile develops. Consider non-dimensionalizing your problem so the variables all scale from 0 to 1. These kinds of errors tend to disappear when you formalize the math in this way. Once you have a non-dimensional solution, you can rescale easily.

Be aware that MATLAB also has built in PDE solvers. You might start here to read up on the general approach and getting your problem in the right form, and then use pdepe.

On a side note, look up linspace() as opposed to using the colon operator : for ranges with non-integer steps. The colon operator will leave out the last step in cases like 1:0.6:2, which produces [ 1 1.6 ]. Note that 2 is missing, which is probably not what you intended.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.