0% found this document useful (0 votes)
18 views

%length M %thermal Diffusivity /M 2 %number of Nodes %°C %°C

This document contains code to numerically solve a 1D heat equation using finite differences. It discretizes the spatial domain into 50 nodes, sets the boundary temperatures to 100C and 20C, and calculates the temperature at each node by constructing and solving a sparse linear system of equations. It then plots both the numerical solution and analytical solution on the same chart for comparison.

Uploaded by

Justin Randall
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

%length M %thermal Diffusivity /M 2 %number of Nodes %°C %°C

This document contains code to numerically solve a 1D heat equation using finite differences. It discretizes the spatial domain into 50 nodes, sets the boundary temperatures to 100C and 20C, and calculates the temperature at each node by constructing and solving a sparse linear system of equations. It then plots both the numerical solution and analytical solution on the same chart for comparison.

Uploaded by

Justin Randall
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

tic

L = 2; %length m
n2 = 10; %thermal diffusivity /m^2
n = 50; %number of nodes
dx = L/n;
Tinf = 20; %C
Tb = 100; %C

A = sparse(n,n+2);
B = sparse(n,1);
Tn = sparse(n,1);

%Numerical
for i = 1;
aW = 0; aE = 1/dx; SP = -2/dx-(n2*dx); aP = aE+aW-SP; Su = 2/dx*Tb+n2*dx*Tinf;
if abs(aP) < abs(aE) + abs(aW)
msgbox('abs(aP) < abs(aE) + abs(aW)')
end
A(i,i+1) = aP; A(i,i+2) = -aE; B(i) = Su;
end

for i = 2:n-1;
aW = 1/dx; aE = 1/dx; SP = -(n2*dx); aP = aE+aW-SP; Su = n2*dx*Tinf;
if abs(aP) < abs(aE) + abs(aW)
msgbox('abs(aP) < abs(aE) + abs(aW)')
end
A(i,i) = -aW; A(i,i+1) = aP; A(i,i+2) = -aE; B(i) = Su;
end

for i = n;
aW = 1/dx; aE = 0; SP = -(n2*dx); aP = aE+aW-SP; Su = n2*dx*Tinf;
if abs(aP) < abs(aE) + abs(aW)
msgbox('abs(aP) < abs(aE) + abs(aW)')
end
A(i,i) = -aW; A(i,i+1) = aP; A(i,i+2) = -aE; B(i) = Su;
end

%Numerical Answer
T = A\B;

%Boundary Temperatures
Tz = sparse(n+2,1);
Tz(1) = Tb; Tz(n+2)=Tinf;

%Complete Numerical Temperature Profile (with Boundary Temp.)


Tn = full(T+Tz);
X = [0,dx/2:dx:L-dx/2,L];

%Analytical
x = [0,dx/2:dx:L-dx/2,L];
Ta = cosh(sqrt(n2)*(L-x))/cosh(sqrt(n2)*L)*(Tb-Tinf)+Tinf;

% Charts
h(i)=plot(x,Ta);
hold
H(i)=plot(X,Tn,'r-*');
legend('T_{Analytical}','T_{Numerical}');
xlabel('Length (m)')
ylabel('Temperature (C)')
Title('Nodes = 50, Length = 2m, n^{2} = 10/m^{2}')
toc

You might also like