0% found this document useful (0 votes)
66 views15 pages

'Enter The Value of Delta X: ': Function

This document contains MATLAB code for solving a 2D heat equation using different numerical methods including point Gauss-Seidel, line Gauss-Seidel, alternating direction implicit (ADI), point Gauss-Seidel with successive over-relaxation (SOR), and line Gauss-Seidel with SOR. The code defines the grid, initializes the temperature values, implements the different solution algorithms in loops, and plots or displays the results.

Uploaded by

Sanket Shah
Copyright
© Attribution Non-Commercial (BY-NC)
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)
66 views15 pages

'Enter The Value of Delta X: ': Function

This document contains MATLAB code for solving a 2D heat equation using different numerical methods including point Gauss-Seidel, line Gauss-Seidel, alternating direction implicit (ADI), point Gauss-Seidel with successive over-relaxation (SOR), and line Gauss-Seidel with SOR. The code defines the grid, initializes the temperature values, implements the different solution algorithms in loops, and plots or displays the results.

Uploaded by

Sanket Shah
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 15

6/11/12 2:22 PM

C:\Users\SANKET\Desktop\assgn\assgn10\pgs_lgs_123100004.m

1 of 3

function pgs_lgs_123100004
clc;
clear;
del_x=input('enter the value of delta x: ');
del_y=del_x;
B=(del_x/del_y);
A=1/(2*(1+B^2));
fprintf('Enter the initial guess for the temparature: ');
a=input('');
h=1;
l=2;
nx=(l/del_x);
ny=(h/del_y);
%specifying the initial condition
for i=2:nx
for j=2:ny
u(i,j,1)=a;
end
end
%specifying the boundary conditions
for i=1:nx+1
u(i,1,1:1000)=0;
u(i,ny+1,1:1000)=400;
end
for j=1:ny+1
u(1,j,1:1000)=100;
u(nx+1,j,1:1000)=200;
end
fprintf('\nsolution using Point Gauss Seidel:\n');
p_gseidel(nx,ny,u,A,B);
fprintf('\nsolution using Line Gauss Seidel:\n');
l_gseidel(nx,ny,u,A,B);
end
function p_gseidel(nx,ny,u,A,B)
k=1;
while 1
for i=2:nx
for j=2:ny
u(i,j,k+1)=A*(u(i+1,j,k)+u(i-1,j,k)+(B^2)*u(i,j+1,k)+(B^2)*u(i,j-1,k+1));
end
end
for i=2:nx
for j=2:ny
o(i,j)=abs(u(i,j,k+1)-u(i,j,k));
end
end
q=max(o);
if q<0.001
break
end

6/11/12 2:22 PM

C:\Users\SANKET\Desktop\assgn\assgn10\pgs_lgs_123100004.m

k=k+1;
end
disp(u(1:nx+1,1:ny+1,k));
end
function l_gseidel(nx,ny,u,A,B)
k=1;
while 1
for i=1:nx-2
a(i)=1;
c(i)=1;
b(i)=-1/A;
b(nx-1)=-1/A;
end
%l : indicates a row of the right vector
%d : right vector
for j=2:ny
e(2,j)=(-1)*(B^2)*(u(2,j+1,k)+u(2,j-1,k+1))-u(1,j,k+1);
e(nx,j)=(-1)*(B^2)*(u(nx,j+1,k)+u(nx,j-1,k+1))-u(nx+1,j,k+1);
for i=3:nx-1
e(i,j)=(-1*B^2)*(u(i,j+1,k)+u(i,j-1,k+1));
end
for l=2:nx
d(l-1)=e(l,j);
end
x=TDMAsolver(a,b,c,d);
for i=2:nx
u(i,j,k+1)=x(i-1);
end
end
e(2,2)
for i=2:nx
for j=2:ny
o(i,j)=abs(u(i,j,k+1)-u(i,j,k));
end
end
q=max(o);
if q<0.001
break
end
k=k+1;
end
disp(u(1:nx+1,1:ny+1,k));
end

% subroutine to solve the tridiagonal system


function [x]=TDMAsolver(a,b,c,d)
n = length(d);

2 of 3

6/11/12 2:22 PM

C:\Users\SANKET\Desktop\assgn\assgn10\pgs_lgs_123100004.m

for i = 2:n-1
temp = b(i) - a(i-1) * c(i-1);
c(i) = c(i) / temp;
d(i) = (d(i) - a(i-1) * d(i-1))/temp;
end
d(n) = (d(n) - a(n-1) * d(n-1))/( b(n) - a(n-1) * c(n-1));
% back substitution
x(n) = d(n);
for i = n-1:-1:1
x(i) = d(i) - c(i) * x(i + 1);
end
end

3 of 3

6/11/12 2:31 PM

MATLAB Command Window

1 of 1

enter the value of delta x: 0.1


Enter the initial guess for the temparature: 0
solution using Point Gauss Seidel:
100.0000 100.0000 100.0000 100.0000
100.0000 100.0000 100.0000
0
55.2736
80.5614
95.9299
184.2913 246.8419 400.0000
0
40.5333
71.0423
95.0851
237.3859 303.0763 400.0000
0
35.8176
67.9902
96.9607
268.5492 328.0777 400.0000
0
34.7475
68.1413 100.2168
286.9808 340.6855 400.0000
0
35.0317
69.6118 103.8216
298.2028 347.6840 400.0000
0
35.7686
71.4543 107.2001
305.2586 351.8480 400.0000
0
36.5893
73.2385 110.1101
309.8232 354.4499 400.0000
0
37.3513
74.8023 112.5031
312.8390 356.1288 400.0000
0
38.0149
76.1184 114.4300
314.8485 357.2270 400.0000
0
38.5912
77.2287 115.9894
316.1634 357.9311 400.0000
0
39.1224
78.2178 117.3075
316.9478 358.3344 400.0000
0
39.6820
79.2147 118.5422
317.2514 358.4595 400.0000
0
40.3920
80.4189 119.9084
317.0036 358.2526 400.0000
0
41.4685
82.1624 121.7319
315.9667 357.5479 400.0000
0
43.3204
85.0322 124.5411
313.6231 355.9726 400.0000
0
46.7818
90.1064 129.2131
308.9459 352.7199 400.0000
0
53.7013
99.3997 137.1623
299.9587 345.9614 400.0000
0
68.6242 116.6299 150.4560
282.9735 331.1673 400.0000
0 104.1661 148.0404 171.3660
251.7703 295.7344 400.0000
200.0000 200.0000 200.0000 200.0000
200.0000 200.0000 200.0000
>>

100.0000

100.0000

100.0000

100.0000

108.0737

119.9570

133.8318

152.9375

116.4082

137.9230

162.4330

193.6272

124.5519

152.8948

184.3509

221.7532

131.9452

164.7547

200.3240

240.4863

138.2594

173.8564

211.7055

252.8887

143.4165

180.7082

219.7548

261.1617

147.5010

185.8074

225.4460

266.7464

150.6725

189.5771

229.4777

270.5568

153.1118

192.3535

232.3335

273.1661

154.9940

194.3945

234.3395

274.9276

156.4833

195.8942

235.7051

276.0436

157.7406

196.9966

236.5458

276.5959

158.9432

197.8086

236.8882

276.5448

160.3178

198.4091

236.6560

275.6935

162.1893

198.8564

235.6354

273.6083

165.0440

199.1939

233.4227

269.4826

169.5815

199.4543

229.3806

261.9548

176.6668

199.6627

222.6917

248.9982

186.9678

199.8390

212.7259

228.3735

200.0000

200.0000

200.0000

200.0000

6/11/12 2:23 PM

C:\Users\SANKET\Desktop\assgn\assgn10\ADI_123100004.m

%ADI
clc;
clear;
del_x=input('enter the value of delta x:');
g=input('enter the initial guess:');
del_y=del_x;
h=1;
l=2;
nx=(l/del_x);
ny=(h/del_y);
x(1)=0;
y(1)=0;
for i=2:nx+1
x(i)=x(i-1)+del_x;
end
for i=2:ny+1
y(i)=y(i-1)+del_y;
end
g=input('enter the x cordinate of the point where temp is needed:');
h=input('enter the y cordinate of the point where temp is needed:');
for i=1:+1:nx+1
p=i;
if abs(g-x(i))<0.001
break
end
end
for i=1:ny+1
l=i;
if abs(y(i)-h)<0.001
break
end
end
A=1;
B=4;
C=1;
a(3:nx)=A;
b(2:nx)=-B;
c(2:nx-1)=A;
for j=3:+1:nx
a(j)=(a(j)/b(j-1));
b(j)=(b(j)-(a(j)*c(j-1)));
end
k=1;
for i=2:nx
for j=2:ny
u(i,j)=g;
end
end
while 1
for i=1:nx+1
%initial conditions
u(i,1)=0;
u(i,ny+1)=400;
end

1 of 2

6/11/12 2:23 PM

C:\Users\SANKET\Desktop\assgn\assgn10\ADI_123100004.m

for j=1:ny+1
u(1,j)=100;
u(nx+1,j)=200;
end
for i=1:nx+1
for j=1:ny+1
u1(i,j)=u(i,j);
end
end
for j=2:ny
for i=2:nx
f(i)=-(u(i,j+1)+u(i,j-1));
end
f(2)=f(2)-u(1,j);
f(nx)=f(nx)-u(nx+1,j);
for i=3:nx
f(i)=f(i)-a(i)*f(i-1);
end
f(nx)=f(nx)/b(nx);
for i=nx-1:-1:2
f(i)=(f(i)-c(i)*f(i+1))/b(i);
end
for i=2:+1:nx
u(i,j)=f(i);
end
end
u(p,l)=T(k);
for i=2:nx
for j=2:ny
o(i,j)=abs(u1(i,j)-u(i,j));
end
end
q=max(o);
if q<0.01
break
end
k=k+1;
end
t(1)=0;
for i=2:+1:k
t(i)=t(i-1)+1;
end
plot(T,t);
disp(k);
fprintf('steady state solution is\n')
disp(u(1:nx+1,1:ny+1));

2 of 2

6/11/12 2:24 PM

C:\Users\SANKET\Desktop\assgn\assgn10\pgssor_123100004.m

%point gauss siedel with sor


clc;
clear;
del_x=input('enter the value of delta x:');
del_y=del_x;
h=1;
l=2;
nx=(l/del_x);
ny=(h/del_y);
x(1)=0;
y(1)=0;
g=input('enter the initial guess:');
for i=1:nx
x(i+1)=x(i)+del_x;
end
for i=1:ny
y(i+1)=y(i)+del_y;
end
g=input('enter the x cordinate of the point where temp is needed:');
h=input('enter the y cordinate of the point where temp is needed:');
for i=1:+1:nx+1
p=i;
if abs(g-x(i))<0.001
break
end
end
for i=1:ny+1
l=i;
if abs(y(i)-h)<0.001
break
end
end
c=input('give the variation from optimum w in %:');
a=(1/2)*(cos(pi/nx-1)+cos(pi/ny-1));
w=(1/a)*(2-2*(1-a)^(0.5));
w1=(w+c*w/100);
k=1;
for i=2:nx
for j=2:ny
u(i,j)=g;
end
end
while 1
for i=1:nx+1
%initial conditions
u(i,1)=0;
u(i,ny+1)=400;
end
for j=1:ny+1
u(1,j)=100;
u(nx+1,j)=200;
end

1 of 2

6/11/12 2:24 PM

C:\Users\SANKET\Desktop\assgn\assgn10\pgssor_123100004.m

for i=1:nx+1
for j=1:ny+1
u1(i,j)=u(i,j);
end
end
for j=2:ny
for i=2:nx
u(i,j)=(1-w1)*(u(i,j))+(w/4)*(u(i+1,j)+u(i-1,j)+u(i,j+1)+u(i,j-1));
end
end
T(k)=u(p,l);
for i=1:nx+1
for j=1:ny+1
e(i,j)=abs(u1(i,j)-u(i,j));
end
end
d=max(e);
if d<0.01
break
end
k=k+1;
end
disp(k);
t(1)=0;
for i=2:+1:k
t(i)=t(i-1)+1;
end
plot(t,T);
fprintf('steady state solution is\n')
disp(u);

2 of 2

22

20

18

16

14

12

10

2
0

10

15

20

25

11

10

1
0

10

15

20

25

30

180

160

140

120

100

80

60

40

20

0
0

10

20

30

40

50

60

70

80

6/11/12 2:24 PM

C:\Users\SANKET\Desktop\assgn\assgn10\lgssor_123100004.m

%line gauss siedel with SOR


clc;
clear;
del_x=input('enter the value of delta x:');
g=input('enter the initial guess:');
w=input('enter the coefficient of SOR:');
del_y=del_x;
h=1;
l=2;
nx=(l/del_x);
ny=(h/del_y);
x(1)=0;
y(1)=0;
for i=2:nx+1
x(i)=x(i-1)+del_x;
end
for i=2:ny+1
y(i)=y(i-1)+del_y;
end
g=input('enter the x cordinate of the point where temp is needed:');
h=input('enter the y cordinate of the point where temp is needed:');
for i=1:+1:nx+1
p=i;
if abs(g-x(i))<0.001
break
end
end
for i=1:ny+1
l=i;
if abs(y(i)-h)<0.001
break
end
end
A=w;
B=4;
C=w;
a(3:nx)=A;
b(2:nx)=-B;
c(2:nx-1)=A;
for j=3:+1:nx
a(j)=(a(j)/b(j-1));
b(j)=(b(j)-(a(j)*c(j-1)));
end
k=1;
for i=2:nx
for j=2:ny
u(i,j)=g;
end
end
while 1
for i=1:nx+1
%initial conditions
u(i,1)=0;

1 of 2

6/11/12 2:24 PM

C:\Users\SANKET\Desktop\assgn\assgn10\lgssor_123100004.m

u(i,ny+1)=400;
end
for j=1:ny+1
u(1,j)=100;
u(nx+1,j)=200;
end
for i=1:nx+1
for j=1:ny+1
u1(i,j)=u(i,j);
end
end
for j=2:ny
for i=2:nx
f(i)=-4*(1-w)*u1(i,j)-w*(u(i,j+1)+u(i,j-1));
end
f(2)=f(2)-u(1,j);
f(nx)=f(nx)-u(nx+1,j);
for i=3:nx
f(i)=f(i)-a(i)*f(i-1);
end
f(nx)=f(nx)/b(nx);
for i=nx-1:-1:2
f(i)=(f(i)-c(i)*f(i+1))/b(i);
end
for i=2:+1:nx
u(i,j)=f(i);
end
end
T(k)=u(p,l);
for i=2:nx
for j=2:ny
o(i,j)=abs(u1(i,j)-u(i,j));
end
end
q=max(o);
if q<0.01
break
end
k=k+1;
end
k
fprintf('steady state solution is\n')
disp(u(1:nx+1,1:ny+1));
contourf(y,x,u);
t(1)=0;
for i=2:+1:k
t(i)=t(i-1)+1;
end

2 of 2

180

160

140

120

100

80

60

40

20

0
0

10

12

14

16

18

20

300

250

200

150

100

50

0
0

10

20

30

40

50

60

70

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy