0% found this document useful (0 votes)
41 views1 page

1 D Conduction M Code

This document contains the code for solving a 1D heat conduction problem using the finite difference method. It defines functions for generating a grid, setting up the tridiagonal matrix equation, and solving the matrix equation. The code generates a grid, sets up the matrix equation, solves it using TDMA, plots the numerical and exact solutions, and compares them.

Uploaded by

Nimesh Thakor
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)
41 views1 page

1 D Conduction M Code

This document contains the code for solving a 1D heat conduction problem using the finite difference method. It defines functions for generating a grid, setting up the tridiagonal matrix equation, and solving the matrix equation. The code generates a grid, sets up the matrix equation, solves it using TDMA, plots the numerical and exact solutions, and compares them.

Uploaded by

Nimesh Thakor
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
You are on page 1/ 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