Professional Documents
Culture Documents
1 D Conduction M Code
1 D Conduction M Code
1DconductionMcode.m
Page 1/2
clear all;
close all;
% set segments m=4, so the number of nodes n=(m+1)
m=4;
n=m+1;
%set dx
dx=1.0/m;
@generate_grid;
x=generate_grid(n,dx);
@set_matrix_eqn;
[a,b,c,d]=set_matrix_eqn(n,dx,x);
@solver_tdma;
temperature=solver_tdma(a,b,c,d);
%check the temperature at the n nodes
temperature;
plot(x,temperature,o);
xlabel(distance);
ylabel(temperature);
hold on
%plot the exact soln
xx=0:0.01:1;
t_exact=exp(xx)+exp(1.0)*xx+1;
plot(xx,t_exact,r);
legend(numerical result,exact temperature);function x = generate_grid(n,dx)
%
%x = zeros(n);
x(1)=0;
for i=2:n
x(i)=x(i1)+dx;
end
1DconductionMcode.m
Page 2/2
function x = solver_tdma(a,b,c,f)
% x = tridiag(a,b,c,f)
%
% Solve an nxn tridiagonal system with subdiagonal a, diagonal b,
% superdiagonal c, and rhs f
%
n = length(f);
x = zeros(size(f));
c(1)=c(1)/b(1);
f(1)=f(1)/b(1);
% Forward elimination
for i=2:n
p = 1.0/(b(i)c(i1)*a(i));
c(i) = c(i)*p;
f(i) = (f(i) a(i)*f(i1))*p ;
end
% Back substitution
x(n) = f(n);
for i=n1:1:1
x(i) = (f(i) c(i)*x(i+1));
end
1DconductionMcode.m
1/1