'Enter The Value of Delta X: ': Function
'Enter The Value of Delta X: ': Function
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
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
1 of 1
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
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
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