Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

Backward Euler Method

2 . For deltat = 0.00001 at time = 0.01,0.02,0.05,0.1,0.2


3 . For deltat = 0.00004 at time = 0.01,0.02,0.05,0.1,0.2

4. For deltat = 0.00016 at time = 0.01,0.02,0.05,0.1,0.2


5. For deltat = 0.00064 at time = 0.01,0.02,0.05,0.1,0.2

For deltat = deltax = 0.001 at time = 0.01,0.02,0.05,0.1,0.2


CRANK NICOLSON METHOD

Solution of the given parabolic heat equation using crank Nicolson method for deltat =
deltax = 0.001

A ) for deltat = deltax = 0.001 at time = 0.01 B) for deltat = deltax = 0.001 at time = 0.02

C ) for deltat = deltax = 0.001 at time = 0.05 A ) for deltat = deltax = 0.001 at time = 0.1
C ) for deltat = deltax = 0.001 at time = 0.2

Solution using Crank Nicolson method


The output for Jacobi iteration method
The output for Gauss Seidel iteration method
The output for successive over relaxation method

Conclusion is that the number of iterations in successive over relaxation method is much less as compared to Jacobi
and gauss seidel method and hence the computation time is also very less thus SOR is the most effective method out
of the above three methods .
1
2
3 % Backward Euler method For solving 1D heat diffusion equation
4 % Given
5 b=1;
6 deltax=0.001;
7 L=1;
8 J=L/deltax+1;
9 deltat=deltax^2;
10 c1=1;c2=0;
11 c3=0;c4=1;
12 u_eb=[];
13
14 nt=fix(0.2/deltat);
15 mu=deltat/deltax^2;
16 a=[0;-mu*ones(J-2,1);-2*mu*c4];
17 b=[(1+2*mu)*c2-2*c1*mu*deltax;(1+2*mu)*ones(J-2,1);(1+2*mu)*c4+2*c3*mu*deltax];
18 c=[-2*mu*c2;-mu*ones(J-2,1);0];
19
20 u=zeros(J,1);
21 x=0:deltax:L;
22
23 % Initial Conditions
24 figure(1);
25 plot(x,u,'LineWidth',2);
26 xlabel('Bar Length');
27 ylabel('Bar Temperature');
28 title('Backward Euler method');
29 grid on;
30
31 q1_o=0;
32 q1_n=1;
33 q2_o=0;
34 q2_n=0;
35
36
37
38
39
40 %time loop
41
42 for it=1:nt
43 %use Thomas algorithm
44 r(1,1)=c2*u(1)-2*mu*deltax*q1_n;
45
46 for j=2:J-1
47 r(j,1)=u(j);
48 end
49
50 r(J,1)=c4*u(J)+2*mu*deltax*q2_n;
51 u=thomas(a,b,c,r);
52
53 if(it==fix(nt/20)) || (it==fix(nt/10)) || (it==fix(nt/4)) || (it==fix(nt/2)) || (it == nt)
54 figure(2);
55 plot(x,u,'LineWidth',2);
56 hold on;
57 xlabel('Bar Length');
58 ylabel('Bar Temperature');
59 end
60
61 q1_o=q1_n;
62 q2_o=q2_n;
63 end
64
65 u_eb=[u_eb;deltat,u(end)];
66 fprintf ( ' \n The value of Temperature for deltat = %f \n ', deltat);
67 disp(u_eb);
68
69
70
1 % CRANK NICOLSON METHOD For solving 1D heat diffusion equation
2 % Given
3
4 b=1;
5 deltax=0.001;
6 L=1;
7 J=L/deltax+1;
8
9 deltat = deltax^2;
10 c1=1;c2=0;
11 c3=0;c4=1;
12 u_cn=[];
13
14 nt=fix(0.2/deltat);
15 mu=deltat/(2*(deltax^2));
16 disp(mu);
17 a=[0;-mu*ones(J-2,1);-2*mu*c4];
18 b=[(1+2*mu)*c2-2*c1*mu*deltax;(1+2*mu)*ones(J-2,1);(1+2*mu)*c4+2*c3*mu*deltax];
19 c=[-2*mu*c2;-mu*ones(J-2,1);0];
20
21 u=zeros(J,1);
22 x=0:deltax:L;
23 figure(1);
24
25 plot(x,u,'LineWidth',2);
26 xlabel('Bar Length');
27 ylabel('Bar Temperature');
28 title('Crank-Nicholson Method');
29 grid on;
30
31 q1_o=0;
32 q1_n=1;
33 q2_o=0;
34 q2_n=0;
35
36
37
38 %time loop
39
40 for it=1:nt
41
42 %use Thomas algorithm
43
44 r(1)= ((c2 * (1 - 2*mu)) + (c1 * 2 * mu * deltax ))*u(1) + (c2 * 2*mu*u(2)) - 2*mu*deltax*( q1_o
+q1_n) ;
45
46 for j=2:J-1
47
48 r(j,1)= mu * u(j-1) + (1-2*mu)*u(j) + mu*u(j+1);
49
50 end
51 r(J) = (c4 * (1-2*mu) + c3*2*mu*deltax)*u(J) + c4 * 2*mu*u(J-1) - 2*mu*deltax*( q2_o +q2_n);
52 u = thomas(a,b,c,r);
53
54 if(it==fix(nt/20)) || (it==fix(nt/10)) || (it==fix(nt/4)) || (it==fix(nt/2)) || (it == nt)
55
56 figure(2);
57 plot(x,u,'LineWidth',2);
58 hold on;
59 xlabel('Bar Length');
60 ylabel('Bar Temperature');
61
62
63 end
64 q1_o=q1_n;
65 q2_o=q2_n;
66 end
67
68 u_cn = [u_cn;deltat,u(end)];
69 u_cn
70
71
72
73
74
1 % GAUSS SIEDEL iteration METHOD
2
3 clear all
4 tic
5 nx=100;
6 ny=100;
7 x=linspace(0,1,nx+1);
8 y=linspace(0,1,ny+1);
9 dxy=0.01;
10 qx=-0.5;qy=0.5;
11 u_old=zeros(nx+1,ny+1);u=zeros(nx+1,ny+1);
12 u_old(1,:)=ones(ny+1,1);
13 u_old(:,1)=ones(ny+1,1);
14 u_old(nx+1,2:ny+1)=qx*dxy+u_old(nx,2:ny+1);
15 u_old(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u_old(2:nx+1,ny);
16 u = u_old;
17 om =0.5*(1-(4/nx)) ;
18 om
19 del=zeros(nx,ny);
20 f=zeros(nx,ny);
21 iter=0;
22 delta=100;
23 while delta > 0.000001
24 for i=2:nx
25 for j=2:ny
26 del(i,j)=(-f(i,j)*dxy^2+u(i,j+1)+u_old(i,j-1)+u_old(i+1,j)+u(i-1,j)-4*u_old(i,j));
27 u(i,j)=u_old(i,j)+ om*del(i,j);
28 end
29 end
30 u(1,:)=ones(ny+1,1);
31 u(:,1)=ones(ny+1,1);
32 u(nx+1,2:ny+1)=qx*dxy+u(nx,2:ny+1);
33 u(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u(2:nx+1,ny);
34 delta=max(max(abs(del)));
35 iter=iter+1;
36 u_old=u;
37 if iter > 100000
38 break
39 end
40 end
41 u
42 iter
43 delta
44 toc
45 figure
46 [c,s]=contour(x,y,u');
47 clabel(c,s)
1
2 %% QUESTION 2
3
4 % Jacobi iteration
5 clear all
6 tic
7 nx=100;
8 ny=100;
9 x=linspace(0,1,nx+1);
10 y=linspace(0,1,ny+1);
11 dxy=0.01;
12 qx=-0.5;qy=0.5;
13 u_old=zeros(nx+1,ny+1);u=zeros(nx+1,ny+1);
14 u_old(1,:)=ones(ny+1,1);
15 u_old(:,1)=ones(ny+1,1);
16 u_old(nx+1,2:ny+1)=qx*dxy+u_old(nx,2:ny+1);
17 u_old(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u_old(2:nx+1,ny);
18 del=zeros(nx,ny);
19 f=zeros(nx,ny);
20
21 iter=0;
22 delta=100;
23 while delta > 0.000001
24 for i=2:nx
25 for j=2:ny
26 del(i,j)=(-f(i,j)*dxy^2+u_old(i,j+1)+u_old(i,j-1)+u_old(i+1,j)+u_old(i-1,j)-4*u_old(i,j));
27 u(i,j)=u_old(i,j)+1/4*del(i,j);
28 end
29 end
30 u(1,:)=ones(ny+1,1);
31 u(:,1)=ones(ny+1,1);
32 u(nx+1,2:ny+1)=qx*dxy+u(nx,2:ny+1);
33 u(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u(2:nx+1,ny);
34 delta=max(max(abs(del)));
35 iter=iter+1;
36 u_old=u;
37 if iter > 100000
38 break
39 end
40 end
41 iter
42 delta
43 toc
44 figure
45 [c,s]=contour(x,y,u');
46 clabel(c,s)
1 % SOR method
2
3
4 clear all
5 tic
6 nx=100;
7 ny=100;
8 x=linspace(0,1,nx+1);
9 y=linspace(0,1,ny+1);
10 dxy=0.01;
11 qx=-0.5;qy=0.5;
12 u_old=zeros(nx+1,ny+1);u=zeros(nx+1,ny+1);
13 u_old(1,:)=ones(ny+1,1);
14 u_old(:,1)=ones(ny+1,1);
15 u_old(nx+1,2:ny+1)=qx*dxy+u_old(nx,2:ny+1);
16 u_old(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u_old(2:nx+1,ny);
17 u = u_old;
18 del=zeros(nx,ny);
19 f=zeros(nx,ny);
20 iter=0;
21 delta=100;
22
23 omega = 1/2 * (1 - (4/nx));
24 disp(omega);
25 while delta > 0.000001
26 for i=2:nx
27 for j=2:ny
28 del(i,j)=(-f(i,j)*dxy^2+u(i,j+1)+u_old(i,j-1)+u_old(i+1,j)+u(i-1,j)-4*u_old(i,j));
29 u(i,j)=u_old(i,j)+ omega * del(i,j);
30
31 end
32 end
33 u(1,:)=ones(ny+1,1);
34 u(:,1)=ones(ny+1,1);
35 u(nx+1,2:ny+1)=qx*dxy+u(nx,2:ny+1);
36 u(2:nx+1,ny+1)=(qy+0.25.*x(2:nx+1)').*dxy+u(2:nx+1,ny);
37 delta=max(max(abs(del)));
38 iter=iter+1;
39
40 u_old=u;
41 if iter > 100000
42 break
43 end
44 end
45
46 iter
47 delta
48 toc
49 figure
50 [c,s]=contour(x,y,u');
51 clabel(c,s)
1 function x=thomas(a,b,c,r)
2 %Setup for L-U decomposition
3 f(1,1)=b(1);
4 g(1,1)=c(1)/f(1,1);
5 %Pre-allocate e for speed
6 e=zeros(length(b),1);
7 %loop to create e,f, and g column vectors
8 for j=2:length(b)
9 e(j,1)=a(j);
10 f(j,1)=b(j)-e(j,1)*g(j-1,1);
11 g(j,1)=c(j)/f(j,1);
12
13 end
14
15 %Solve for y
16 y=zeros(length(b),1);
17 y(1,1)=r(1)/f(1,1);
18
19 for j=2:length(b)
20 y(j,1)=(r(j)-e(j,1)*y(j-1))/(f(j,1));
21 end
22
23 %Solve for x
24 x=zeros(length(b),1);
25
26 x(end,1)=y(end,1);
27
28 for j=length(b)-1:-1:1
29 x(j,1)=y(j,1)-g(j,1)*x(j+1,1);
30 end

You might also like