0% found this document useful (0 votes)
105 views

Numerical Method For Engineering 3

The document describes using the Liebmann method to solve the 2D Laplace equation numerically with Neumann and Dirichlet boundary conditions. It provides code to calculate the temperature distribution across a 2D grid given boundary temperatures, thermal conductivity, and the relaxation parameter lambda. The code iterates until the solution converges within a specified tolerance, and outputs the temperature field, flux, and relaxation parameter that results in the fewest iterations.

Uploaded by

Cer No Rus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
105 views

Numerical Method For Engineering 3

The document describes using the Liebmann method to solve the 2D Laplace equation numerically with Neumann and Dirichlet boundary conditions. It provides code to calculate the temperature distribution across a 2D grid given boundary temperatures, thermal conductivity, and the relaxation parameter lambda. The code iterates until the solution converges within a specified tolerance, and outputs the temperature field, flux, and relaxation parameter that results in the fewest iterations.

Uploaded by

Cer No Rus
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

NUMERICAL METHOD FOR ENGINEERING

EKC 245
AHLI KUMPULAN

NAMA
EKMAL HARRIS B ABDUL MALEK

NO.MATRI
K
115836

MOHD KHAIRI B OTHMAN

117893

HASIF B MIOR HAKIM

115841

MOHAMAD AZZAM B CHE RAHIM

117890

TAJUK SOALAN
:
NAMA PENSYARAH
:
TARIKH HANTAR PROJEK
:

NUMERICAL METHOD
DR TYE CHIN THIAN
26 MEI 2014

Liebmann Method:
Neumann boundary condition
function A=nma_laplaceRectNuemann(~,ny,Left,Top,Right,lambda,tol);
clear all
clc
Top=input('Top Temperature: ');
Left=input('Left Temperature: ');
Right=input('Right Temperature: ');
disp('Only valid for square grid only')
Square_Grid=input('Number of Interval;');
nx_int =Square_Grid*Square_Grid ;
ny_int =nx_int ;
w=input('Final boundry X :');
p=input('Final boundry Y :');
tol=input( 'Tolerance: ');
K=input('Thermal conductivity=');
disp('We will calculate the value of lamda for the shortest itteration
number')
lambdav = [1.0:0.1:1.9];
for k = 1:1:length(lambdav)
lambda = lambdav(k);
nx = nx_int-1;
ny = ny_int-1;
TRUE = 1;
FALSE = 0;
A = zeros(nx+2,ny+2);
A_old = A;
A(1,:) = Top;
%Condition
A(:,1) = Left ;
A(end,:) = 0;
A(:,end) = Right;
%Input Value of boundry
xo = 0.0;
xf = w;
yo = 0.0;
yf = p;
dx = (xf - xo)/nx_int;
dy = (yf - yo)/ny_int;
A(1,1) = 0;
A(end,1) = Left;
A(1,end) = 0;
A(end,end) = Right;
A_old = A;
converged = FALSE;
nIter = 0;
xgrid = [xo:dx:xf];
ygrid = [yo:dy:yf];
while(~converged)
nIter = nIter+1;
%fprintf(Iteration number %d\n,nIter);
for(i=2+nx:-1:2)
for(j=2:1:2+ny-1)
if( i == 2+nx ) %insulated edge condition
A(i,j) = A(i,j+1) + A(i,j-1) + 2*A(i-1,j);

else
A(i,j) = A(i,j+1) + A(i,j-1) + A(i-1,j) + A(i+1,j);
end
A(i,j) = A(i,j)/4;
A(i,j) = lambda*A(i,j) + (1-lambda)*A_old(i,j);
end
end
[max_row , max_row_index] = max(A(2:end-1,2:end-1));
[max_element , max_col_index] = max(max_row);
pos = [max_row_index(1)+1,max_col_index+1];
epsilonA = abs(A(pos(1),pos(2)) A_old(pos(1),pos(2)))/A(pos(1),pos(2))*100;
if( epsilonA < tol )
converged = TRUE;
end
A_old = A;
%A
%epsilonA
end
fprintf(1,'lamdba = %f iteration = %i , error = %f
\n',lambda,nIter,epsilonA);
%A=A(2:end-1,2:end-1);
q=zeros(nx+2,ny+2);
for i=2:ny+1
for j=2:nx+1
qx=-K/(2*dx)*(A(i+1,j)-A(i-1,j));
qy=-K/(2*dy)*(A(i,j+1)-A(i,j-1));
q(i,j)=sqrt((qx^2)+(qy^2));
if qx>0
Theta=atan(qy/qx);
elseif qx<0
Theta=atan(qy/qx)+pi;
end
end
end
xmin = xgrid(1);
xmax = xgrid(nx);
ymin = ygrid(1);
ymax = ygrid(ny);
ncontourlines =1000;
Amin = min(min(A));
Amax = max(max(A));
if (abs(Amin-Amax) < tol/100000000);
fprintf(1,'Solution is a flat plane with value = %f \n',Amin)
else
plot_dimensions = 1;
if (plot_dimensions == 3)
contour3(xgrid, ygrid,A,ncontourlines);
axis([xmin xmax ymin ymax Amin Amax])
else
subplot(2,1,1),contour(xgrid, ygrid,A,ncontourlines);
subplot(2,1,2),mesh(q)
axis([xmin xmax ymin ymax])
end
end
xlabel('x');
ylabel('y');
colorbar
end
subplot(2,1,2),mesh(q)
xlabel('x');

ylabel('y');
colorbar
disp(' ')
disp('Flux Q')
fprintf([repmat('%f\t', 1, size(q, 2)) '\n'], q');
disp(' ')
fprintf(1,'Theta= %.2i ',Theta);

Top Temperature: 100


Left Temperature: 75
Right Temperature: 50
Only valid for square grid only
Number of Interval;2
Final boundry X :1
Final boundry Y :1
Tolerance: 0.0001
Thermal conductivity=0.49
We will calculate the value of lamda for the shortest
itteration number
lamdba = 1.000000 iteration = 30 , error = 0.000092
lamdba = 1.100000 iteration = 24 , error = 0.000083
lamdba = 1.200000 iteration = 18 , error = 0.000082
lamdba = 1.300000 iteration = 14 , error = 0.000018
lamdba = 1.400000 iteration = 16 , error = 0.000019
lamdba = 1.500000 iteration = 13 , error = 0.000064
lamdba = 1.600000 iteration = 28 , error = 0.000081
lamdba = 1.700000 iteration = 38 , error = 0.000005
lamdba = 1.800000 iteration = 55 , error = 0.000054
lamdba = 1.900000 iteration = 117 , error = 0.000028
Flux Q
0.000000
0.000000
0.000000
0.000000
0.000000

0.000000
24.665425
10.604254
7.695731
0.000000

0.000000
28.084848
18.060128
13.287459
0.000000

0.000000
47.312528
26.100679
18.567717
0.000000

0.000000
0.000000
0.000000
0.000000
0.000000

Theta= 1.31e+000
ans =
0
75.0000
75.0000
75.0000
75.0000

100.0000
83.4109
76.0151
72.8076
71.9075

100.0000
82.6284
72.8418
68.3072
67.0144

100.0000
74.2615
64.4168
60.5652
59.5359

0
50.0000
50.0000
50.0000
50.0000

1
0.8
0.6
0.4
0.2
0

0.1

0.2

0.3

0.4

0.5

0.6

0.7

0.8

0.9

40

50

30
20
0
6

2
y

2
x

10
0

Liebmann Method
Dirichlet boundary condition:
function U = ell_liebmann
clear all
clc
xo =input('Initial value of x: ');
xf = input('Final value of x: ');
yo = input('initial value of y: ');
yf = input('Final value of y: ');
a=input('Bottom:');
b=input('Top:');
c=input('Left:');
d=input('Right:');
Oo=input('interval of x: ');
oO=input('interval of y: ');
lambdav = [1.0:0.1:1.9];
for k = 1:1:length(lambdav)
lambda = lambdav(k);
% number of intervals
nx_int = Oo;
ny_int = oO;
% number of nodes
nx = nx_int + 1;
ny = ny_int + 1;
% grid points in x and y
dx = (xf - xo)/nx_int;
dy = (yf - yo)/ny_int;
xgrid = [xo:dx:xf];
ygrid = [yo:dy:yf];
% initialize U and Uold
U = zeros(nx,ny);
Uold = zeros(nx,ny);
% Fill in Uold with four dirichlet BCs
for j = 1:1:ny
% BC for x = xo
Uold(1,j) = a;
% BC for x = xf
Uold(nx,j) =b;
% BC for y = yo
Uold(j,1) = c;
% BC for y = yf
Uold(j,ny) = d;
end
% fill in the interior nodes of Uold with initial guess
for i = 2:1:nx-1
for j = 2:1:ny-1
%Uold(i,j) = 0.0;
Uold(i,j) = 56.25;
end
end
U = Uold;
%
% iteratively solve
%
maxit = 100;
err = 100;
tol = 1.0e-6;
icount = 0;
while (icount < maxit && err > tol)

icount = icount + 1;
Uold = U;
for i = 2:1:nx-1
for j = 2:1:ny-1
% apply Liebmann's method
U(i,j) = (U(i+1,j) + U(i-1,j) + U(i,j+1) + U(i,j-1) )*0.25;
U(i,j) = lambda*U(i,j) + (1-lambda)*Uold(i,j);
end
end
err = 0.0;
for i = 2:1:nx-1
for j = 2:1:ny-1
err = err + (U(i,j) - Uold(i,j))^2;
end
end
err = sqrt(err/((nx-2)*(ny-2)));
end
fprintf(1,' lamdba = %f iteration = %i , error = %f \n',lambda, icount,
err);
% plot
xmin = xgrid(1);
xmax = xgrid(nx);
ymin = ygrid(1);
ymax = ygrid(ny);
ncontourlines = 500;
Umin = min(min(U));
Umax = max(max(U));
if (abs(Umin-Umax) < 1.0e-8);
fprintf(1,'Solution is a flat plane with value = %f \n',Umin)
else
plot_dimensions = 1;
if (plot_dimensions == 3)
contour3(xgrid, ygrid,U,ncontourlines);
axis([xmin xmax ymin ymax Umin Umax])
else
contour(xgrid, ygrid,U,ncontourlines);
axis([xmin xmax ymin ymax])
end
xlabel('x');
ylabel('y');
colorbar
end
end

Initial value of x: 0
Final value of x: 1
initial value of y: 0
Final value of y: 1
Bottom:0
Top:100
Left:75
Right:50

interval of x: 4
interval of y: 4
lamdba = 1.000000 iteration = 24 , error = 0.000001
lamdba = 1.100000 iteration = 18 , error = 0.000001
lamdba = 1.200000 iteration = 13 , error = 0.000001
lamdba = 1.300000 iteration = 16 , error = 0.000000
lamdba = 1.400000 iteration = 20 , error = 0.000001
lamdba = 1.500000 iteration = 26 , error = 0.000001
lamdba = 1.600000 iteration = 35 , error = 0.000001
lamdba = 1.700000 iteration = 49 , error = 0.000001
lamdba = 1.800000 iteration = 78 , error = 0.000001
lamdba = 1.900000 iteration = 100 , error = 0.000640

ans =

75.0000

75.0000

42.8573

33.2594

33.9280

50.0000

75.0000

63.1699

56.2501

52.4552

50.0000

75.0000

78.5721

76.1159

69.6427

50.0000

75.0000

100.0000

100.0000

100.0000

50.0000

1
0.9

90

0.8

80

0.7

70

0.6

60

0.5

50

0.4

40

0.3

30

0.2

20

0.1

10

0.2

0.4

0.6

0.8

You might also like