Matlab code for diffusion problem.
An Introduction to Computational Fluid Dynamics: The Finite Volume Method
Book by H. K. Versteeg and W. Malalasekera
(4.1 (page 119)
3. www.engineeringwithsandeep.com| Student Assignment
Problem Statement
Problem: From
An Introduction to Computational Fluid Dynamics: The Finite Volume Method
Book by H. K. Versteeg and W. Malalasekera
(4.1 (page 119) )
• Consider the problem of source-free heat conduction in an insulated rod
whose ends are maintained at constant temperatures of 100°C and 500°C
respectively.
Calculate the steady state temperature distribution in the rod. Thermal
conductivity k equals 1000 W/m.K, cross-sectional area A is 10 × 10-3 m2.
4. www.engineeringwithsandeep.com| Student Assignment
FVM Matlab code
%author shivam choubey
%date 25.05.2021
%version I
%all units in meter
%code is valid for no source and 1
D conduction problem only
%if change value , the result is
nearly same as actual value
clear all;
close all;
n=50;%Number of part
L=0.5;
k=1000;
area=0.01;
dx=L/n;
dx_cor=dx/2;
A=zeros(n);
B=ones(n,1);
TA=100;%temp of first node
TB=500;%temp of end node
Su=(2*k*area)/dx; % Sb term coff.
%value for centre node first in
our case 2 3 4 points
for i=2:n-1
A(i,i-1)=-(k*area)/dx;
A(i,i+1)=-(k*area)/dx;
A(i,i)=2*(k*area)/dx;
B(i,1)=0;
End
% now for 1 term and nth term
A(1,1)=3*(k*area)/dx;
A(1,2)=-(k*area)/dx;
A(n,n-1)=-(k*area)/dx;
A(n,n)=3*(k*area)/dx;
%Right hand term
B(1)=Su*TA;
B(n)=Su*TB;
%find value of nodes
T=inv(A)*B;
disp("Temp at every node is : ")
T
%plot
for i=1:n
m(i,1)=i*dx;
end
plot(m,T,"*r")
hold on
Part1
Part2
5. www.engineeringwithsandeep.com| Student Assignment
%exact soluation is
T_real=zeros(n,1);
r=dx;
for i=1:n
T_real(i,1)=800*r+100
r=r+dx
end
plot(m,T_real,"-b")
legend("Numerical sol." ,"Real sol.")
xlabel("Distance")
ylabel("Temp is °c")
Part3
Output
only for node 5
n =
5
Temp at every node is :
T =
140.0000
220.0000
300.0000
380.0000
460.0000