Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

Printed by

May 17, 13 14:38

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

May 17, 13 14:38

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

function [a,b,c,d] = set_matrix_eqn(n,dx,x)


%
% a = zeros(n);
for i=1:n
a(i)=1.;
b(i)=2;
c(i)=1;
d(i)=exp(x(i))*dx*dx;
end
a(1)=0.;
b(1)=1.;
c(1)=0.;
d(1)=0.;
a(n)=0.;
b(n)=1.;
c(n)=0.;
d(n)=1.;

Friday May 17, 2013

1DconductionMcode.m

1/1

You might also like