How can I turn this Explicit method code to Implicit method code ?

25 views (last 30 days)
I want to turn my matlab code for 1D heat equation by explicit method to implicit method. How can I do this easily?
% 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 10s, 20 grid points
and a .2s time step.
L=1; % Length of modeled domain [m]
t=10; % time [s]
k=.001;
n=20; % Number of gridpoints
nt=500;
dx=L/n; % Spacing of grid
dt=.2; % Timestep [s]
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
dlmwrite('Trial_1.csv',T1,'Delimiter','\n') % Saves data into a csv file
Please help.

Answers (1)

Torsten
Torsten on 17 Oct 2018
L = 1; % Length of modeled domain [m]
nx = 200; % Number of gridpoints
t = 500; % time [s]
nt = 15;
k = .001;
dx = L/(nx-1); % Spacing of grid
dt = t/nt; % Timestep [s]
alpha = k*dt/dx^2;
T0 = 400*ones(nx,1);
T1 = 300*ones(nx,1);
T0(1)= 300;
T0(end) = 300;
A = full(gallery('tridiag',nx-2,-alpha,1+2*alpha,-alpha));
for j = 1:nt
b = [T0(2)+alpha*T0(1);T0(3:nx-2);T0(end-1)+alpha*T0(end)];
T1(2:end-1) = A\b;
T0 = T1;
end
  2 Comments
Md Badrul Hasan
Md Badrul Hasan on 17 Oct 2018
A = full(gallery('tridiag',nx-2,-alpha,1+2*alpha,-alpha));
for j = 1:nt
b = [T0(2)+alpha*T0(1);T0(3:nx-2);T0(end-1)+alpha*T0(end)];
T1(2:end-1) = A\b;
I could not understand the operation here. Can you please elaborate ? Thanks for your reply.
Torsten
Torsten on 17 Oct 2018
Read and learn:
https://en.wikipedia.org/wiki/Finite_difference_method#Implicit_method
Best wishes
Torsten.

Sign in to comment.

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!