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

2D DirichMcode

This document contains MATLAB code to solve the 2D steady heat equation using an explicit finite difference method. It sets up a grid, initializes the temperature field u to be zero everywhere except the boundaries, and iteratively updates u using a weighted average of the neighboring points until the error is below a threshold. It plots the temperature field after each 10 iterations and at the end to visualize the solution.

Uploaded by

Ashish Kotwal
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)
27 views1 page

2D DirichMcode

This document contains MATLAB code to solve the 2D steady heat equation using an explicit finite difference method. It sets up a grid, initializes the temperature field u to be zero everywhere except the boundaries, and iteratively updates u using a weighted average of the neighboring points until the error is below a threshold. It plots the temperature field after each 10 iterations and at the end to visualize the solution.

Uploaded by

Ashish Kotwal
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

Apr 25, 08 10:41 steadyheat2d.m Page 1/1


clear all;
lx=1.; ly=2.;
mx=20; my=40;
nx=mx+1; ny=my+1;
dx=lx/mx; dy=ly/my;
beta2=(dx*dx)/(dy*dy);
x=linspace(0,lx,nx);
y=linspace(0,ly,ny);
u=zeros(nx,ny); % initial, zero
u(1,:)=0; %left b.c.
u(nx,:)=0; %right b.c.
u(:,1)=0.; %bottom b.c.
u(:,ny)=100; %top b.c.
unew=u;

q=zeros(nx,ny);
%q(n/22:n/2+2,n/22:n/2+2)=1.;
set(gca,zlim,[0 1]);
surf(x,y,u);
shading interp;
pause;
err_tol=0.001;
err=2*err_tol;
iter=0;
omega=1.8;
%while err>err_tol && iter<1000
while (err>err_tol) && iter<1000
for i=2:mx
for j=2:my
% unew(i,j)=(u(i1,j)+u(i+1,j)+beta2*(u(i,j1)+u(i,j+1))+q(i,j)*dx*dx
)/(2+2*beta2);
%unew(i,j)=(unew(i1,j)+u(i+1,j)+beta2*(unew(i,j1)+u(i,j+1))+q(i,j)
*dx*dx)/(2+2*beta2);
unew(i,j)=(1omega)*u(i,j)+omega/(2*(1+beta2))*(unew(i1,j)+u(i+1,j)
+beta2*unew(i,j1)+beta2*u(i,j+1)+q(i,j)*dx*dx);
end;
end;
err=0;
%for i=2:nx1
% for j=2:ny1
% err=err+abs(unew(i,j)u(i,j));
% end;
%end;
err=sum(sum(abs(unewu)));
if mod(iter,10)==0
pause;
iter
err
surf(x,y,unew);
shading interp;
end;
u=unew;
iter=iter+1;
end;
surf(x,y,u);

Friday May 17, 2013 steadyheat2d.m 1/1

You might also like