Thursday, January 24, 2013

MATLAB - Laplace Equation

lap
%=========================================================================
%*************** MATLAB code to solve Laplace Equation *******************
%***** Solution to steady state temperatureat at the interior points *****
%************************ By Mahesha MG, MIT *****************************
%Date: 22/01/2013
%=========================================================================
clear;
clc;
e=1e-4;
%=========================================================================
% ****************************** Input ***********************************
l=input('Enter length in meter: ');
w=input('Enter width in meter: ');
gs=input('Enter grid size in meter: ');
lt=input('Enter temperature at left side:');
rt=input('Enter temperature at right side:');
tt=input('Enter temperature at top side:');
bt=input('Enter temperature at bottom side:');
%=========================================================================
%******************* Assigning boundary values ***************************
m=l/gs;
n=w/gs;
a=zeros(m+1,n+1);
for i=2:n
    a(i,1)=lt;
    a(i,m+1)=rt;
end
for i=2:m
    a(1,i)=tt;
    a(n+1,i)=bt;
end
%=========================================================================
%*********************** Gauss Seidel Method *****************************
chk=1;
while (chk)
    for i=2:m
     for j=2:n
         temp=(a(i-1,j)+a(i+1,j)+a(i,j-1)+a(i,j+1))/4;
         if (abs(temp-a(i,j))<e)
             chk=0;
         else
             chk=1;
             a(i,j)=temp;
         end
     end
    end
    contourf(a)
    getframe();
end
%=========================================================================

No comments:

Post a Comment