0% found this document useful (0 votes)
4 views2 pages

Finite Difference Matlab Notes

The document presents MATLAB programs for solving the 1D heat equation using both explicit and implicit finite difference schemes. The explicit scheme updates the solution iteratively based on a defined formula, while the implicit scheme formulates a system of linear equations that can be solved using matrix operations. Both methods initialize conditions and display the resulting temperature distribution over time.

Uploaded by

mohtolhyan123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views2 pages

Finite Difference Matlab Notes

The document presents MATLAB programs for solving the 1D heat equation using both explicit and implicit finite difference schemes. The explicit scheme updates the solution iteratively based on a defined formula, while the implicit scheme formulates a system of linear equations that can be solved using matrix operations. Both methods initialize conditions and display the resulting temperature distribution over time.

Uploaded by

mohtolhyan123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

MATLAB Programs for Finite Difference Schemes

Explicit Finite Difference Scheme (for 1D Heat Equation) The explicit finite difference scheme is
used to approximate the solution of the heat equation: u_t = α u_xx The explicit formula used is:
u(i,j+1) = u(i,j) + r*(u(i+1,j) - 2*u(i,j) + u(i-1,j)) where r = α∆t / (∆x)^2

% Explicit Finite Difference Scheme for Heat Equation

clc;
clear;

L = 1;
T = 0.2;
nx = 4;
nt = 4;
alpha = 1;

dx = L/nx;
dt = T/nt;

r = alpha*dt/(dx^2);

u = zeros(nx+1, nt+1);

for i = 1:nx+1
x = (i-1)*dx;
u(i,1) = sin(pi*x/L);
end

u(1,:) = 0;
u(nx+1,:) = 0;

for j = 1:nt
for i = 2:nx
u(i,j+1) = u(i,j) + r*(u(i+1,j) - 2*u(i,j) + u(i-1,j));
end
end

disp(u)

Implicit Finite Difference Scheme (for 1D Heat Equation) The implicit scheme is another numerical
method for solving the heat equation: u_t = α u_xx The scheme leads to a system of linear
equations at each time step, which can be written in matrix form and solved using MATLAB.

% Implicit Finite Difference Scheme for Heat Equation

clc;
clear;

L = 1;
T = 0.2;
nx = 4;
nt = 4;
alpha = 1;

dx = L/nx;
dt = T/nt;

r = alpha*dt/(dx^2);

u = zeros(nx+1, nt+1);

for i = 1:nx+1
x = (i-1)*dx;
u(i,1) = sin(pi*x/L);
end

u(1,:) = 0;
u(nx+1,:) = 0;
A = zeros(nx-1);

for i = 1:nx-1
A(i,i) = 1 + 2*r;
end

for i = 1:nx-2
A(i,i+1) = -r;
A(i+1,i) = -r;
end

for j = 1:nt
b = u(2:nx,j);
sol = A\b;
u(2:nx,j+1) = sol;
end

disp(u)

You might also like