P. 1
Pout

|Views: 0|Likes:

See more
See less

11/17/2012

pdf

text

original

# 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 2 of 3
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);
6/11/12 2:22 PM C:\Users\SANKET\Desktop\assgn\assgn10\pgs_lgs_123100004.m 3 of 3

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

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 100.0000
100.0000 100.0000 100.0000
0 55.2736 80.5614 95.9299 108.0737 119.9570 133.8318 152.9375
184.2913 246.8419 400.0000
0 40.5333 71.0423 95.0851 116.4082 137.9230 162.4330 193.6272
237.3859 303.0763 400.0000
0 35.8176 67.9902 96.9607 124.5519 152.8948 184.3509 221.7532
268.5492 328.0777 400.0000
0 34.7475 68.1413 100.2168 131.9452 164.7547 200.3240 240.4863
286.9808 340.6855 400.0000
0 35.0317 69.6118 103.8216 138.2594 173.8564 211.7055 252.8887
298.2028 347.6840 400.0000
0 35.7686 71.4543 107.2001 143.4165 180.7082 219.7548 261.1617
305.2586 351.8480 400.0000
0 36.5893 73.2385 110.1101 147.5010 185.8074 225.4460 266.7464
309.8232 354.4499 400.0000
0 37.3513 74.8023 112.5031 150.6725 189.5771 229.4777 270.5568
312.8390 356.1288 400.0000
0 38.0149 76.1184 114.4300 153.1118 192.3535 232.3335 273.1661
314.8485 357.2270 400.0000
0 38.5912 77.2287 115.9894 154.9940 194.3945 234.3395 274.9276
316.1634 357.9311 400.0000
0 39.1224 78.2178 117.3075 156.4833 195.8942 235.7051 276.0436
316.9478 358.3344 400.0000
0 39.6820 79.2147 118.5422 157.7406 196.9966 236.5458 276.5959
317.2514 358.4595 400.0000
0 40.3920 80.4189 119.9084 158.9432 197.8086 236.8882 276.5448
317.0036 358.2526 400.0000
0 41.4685 82.1624 121.7319 160.3178 198.4091 236.6560 275.6935
315.9667 357.5479 400.0000
0 43.3204 85.0322 124.5411 162.1893 198.8564 235.6354 273.6083
313.6231 355.9726 400.0000
0 46.7818 90.1064 129.2131 165.0440 199.1939 233.4227 269.4826
308.9459 352.7199 400.0000
0 53.7013 99.3997 137.1623 169.5815 199.4543 229.3806 261.9548
299.9587 345.9614 400.0000
0 68.6242 116.6299 150.4560 176.6668 199.6627 222.6917 248.9982
282.9735 331.1673 400.0000
0 104.1661 148.0404 171.3660 186.9678 199.8390 212.7259 228.3735
251.7703 295.7344 400.0000
200.0000 200.0000 200.0000 200.0000 200.0000 200.0000 200.0000 200.0000
200.0000 200.0000 200.0000

>>
6/11/12 2:23 PM C:\Users\SANKET\Desktop\assgn\assgn10\ADI_123100004.m 1 of 2
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
6/11/12 2:23 PM C:\Users\SANKET\Desktop\assgn\assgn10\ADI_123100004.m 2 of 2
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);
disp(u(1:nx+1,1:ny+1));

6/11/12 2:24 PM C:\Users\SANKET\Desktop\assgn\assgn10\pgssor_123100004.m 1 of 2
%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
6/11/12 2:24 PM C:\Users\SANKET\Desktop\assgn\assgn10\pgssor_123100004.m 2 of 2
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);
disp(u);

0 5 10 15 20 25
2
4
6
8
10
12
14
16
18
20
22
0 5 10 15 20 25 30
1
2
3
4
5
6
7
8
9
10
11
0 10 20 30 40 50 60 70 80
0
20
40
60
80
100
120
140
160
180
6/11/12 2:24 PM C:\Users\SANKET\Desktop\assgn\assgn10\lgssor_123100004.m 1 of 2
%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;
6/11/12 2:24 PM C:\Users\SANKET\Desktop\assgn\assgn10\lgssor_123100004.m 2 of 2
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

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

0 2 4 6 8 10 12 14 16 18 20
0
20
40
60
80
100
120
140
160
180
0 10 20 30 40 50 60 70
0
50
100
150
200
250
300

j. end disp(u(1:nx+1.k+1))-u(1.A.j. b(i)=-1/A.j-1.u.k+1)-u(i.k+1).1:ny+1.k)). e(nx. end for l=2:nx d(l-1)=e(l. b(nx-1)=-1/A.d).j)=abs(u(i.k+1)=x(i-1).6/11/12 2:22 PM C:\Users\SANKET\Desktop\assgn\assgn10\pgs_lgs_123100004.j.j+1. for i=3:nx-1 e(i. end end q=max(o).j-1.j.k)+u(2. c(i)=1.b. end % subroutine to solve the tridiagonal system function [x]=TDMAsolver(a.k)).1:ny+1.k)+u(nx. while 1 for i=1:nx-2 a(i)=1.k+1)). end function l_gseidel(nx.j+1.c.j.c.ny.m 2 of 3 k=k+1. for i=2:nx u(i.k)).b. end disp(u(1:nx+1. end x=TDMAsolver(a.B) k=1.j-1.k+1).k)+u(i. if q<0.j+1. end end e(2.k+1))-u(nx+1.j)=(-1*B^2)*(u(i. end %l : indicates a row of the right vector %d : right vector for j=2:ny e(2.j). .d) n = length(d).2) for i=2:nx for j=2:ny o(i.j)=(-1)*(B^2)*(u(2.j)=(-1)*(B^2)*(u(nx.001 break end k=k+1.

scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->