Numerical Methods Assignment 2
Transcript of Numerical Methods Assignment 2
-
8/11/2019 Numerical Methods Assignment 2
1/25
Steady state square plateStarting from a 3 x 3 square plate in the example, the code is written to allow for simple refinement.
The code is first tested without a hole in the centre.
Steady state square plate with 3x3 cells
Steady state square plate with 9x9 cells
-
8/11/2019 Numerical Methods Assignment 2
2/25
Steady state square plate with 27x27 cells
Steady state square plate with 81x81 cells
It was found that after several refinements to 81 x 81, the values in the original cells are within 0.05%of the previous refinement.
-
8/11/2019 Numerical Methods Assignment 2
3/25
Table showing percentage change in results from previous refinement for a steady state square
plate
Cell Number
(Based on 3x3
grid)
% Change in values from previous
refinement
3x3 9x9 27x27 81x811 0 1.1403 32.5492 0.0450
2 0 1.4195 0.1575 0.0169
3 0 2.9435 47.4714 0.0152
4 0 1.4863 28.6655 0.0168
5 0 1.6517 0.1925 0.0200
6 0 1.6562 39.7151 0.0180
7 0 1.6783 25.1864 0.0087
8 0 1.6330 0.1797 0.0216
9 0 1.0371 33.2635 0.0197
The results from several points were plotted out on a graph, to better visualise the effect of the grid
refinement.
Graph of temperature (C) against number of cells in grid
Steady state square plate with hole
For the case of the square plate with a hole, we have decided to set the boundaries at the hole to
30C for realism and simplicity of calculation since we can eliminate the number of unknowns in the
system of equations for those cells that have been replaced by the hole.
0
50
100
150
200
250
1 9 81 729 6561
Temperature(degreesCelcius)
Number of cells
Cell 1
Cell 5
Cell 9
-
8/11/2019 Numerical Methods Assignment 2
4/25
Steady state square plate with hole with 3x3 cells
Steady state square plate with hole with 9x9 cells
-
8/11/2019 Numerical Methods Assignment 2
5/25
Steady state square plate with hole with 27x27 cells
Steady state square plate with hole with 81x81 cells
It was found that after refinements to 81 x 81, the values in the original 8 cells (centre cell omitted,
as it is set at 30C) are within 5% of the previous refinement.
Table showing percentage change in results from previous refinement for a steady state square
plate with hole
Cell Number
(Based on 3x3
grid)
% Change in values from previous
refinement
3x3 9x9 27x27 81x81
1 0 22.4944 8.4426 1.7417
2 0 20.4441 10.4796 4.1920
3 0 12.1164 20.7259 3.6241
4 0 27.4608 44.7039 2.3313
6 0 11.8615 42.7340 4.2270
7 0 30.0400 70.4730 1.3141
8 0 20.9673 9.3775 3.3623
9 0 8.9047 49.9211 3.7554
Since a 27x27 grid showed fairly accurate results, with less than 5% change after refinement, we
decided to use this grid for the transient simulation, as the 81x81 grid would be too time-consuming.
The results from several points were plotted out on a graph, to better visualise the effect of the grid
refinement.
-
8/11/2019 Numerical Methods Assignment 2
6/25
Graph of temperature (C) against number of cells in grid
Transient square plate with holeA fully implicit and a Crank-Nicolson scheme were carried out for the transient problem. The
simulation was initialized with the plate at 30C. The time steps run were 0.5s, 0.05s, 0.005s and
0.0005s. The simulation was run for a 30-second duration, and the steadiness of the result was
ascertained by comparing to the data produced at 27 seconds.
Table showing percentage difference between data at 30s and 27s for fully implicit scheme
Cell
number
% difference between result at 27s and 30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s
1 0.0220 0.0176 0.0176 0.0176
2 0.0940 0.0783 0.0783 0.0769
3 0.2102 0.1768 0.1741 0.1741
4 0.0071 0.0057 0.0057 0.0057
6 0.2346 0.2016 0.1992 0.1969
7 0.0172 0.0158 0.0158 0.0158
8 0.1098 0.0981 0.0958 0.0958
9 0.3153 0.2739 0.2713 0.2713
0
20
40
60
80
100
120
1 9 81 729 6561
Temperature(degre
esCelcius)
Number of cells
Cell 1
Cell 4
Cell 7
Cell 9
-
8/11/2019 Numerical Methods Assignment 2
7/25
Table showing percentage difference between data at 30s and 27s for Crank-Nicolson scheme
Cell
number
% difference between result at 27s and 30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s
1 0.0176 0.0176 0.0176 0.0176
2 0.0769 0.0769 0.0769 0.0769
3 0.1741 0.1741 0.1741 0.1741
4 0.0057 0.0057 0.0057 0.0057
6 0.1992 0.1969 0.1969 0.1969
7 0.0158 0.0158 0.0158 0.0158
8 0.0958 0.0958 0.0958 0.0958
9 0.2687 0.2713 0.2713 0.2713
We can assume the result has reached steady-state, since the percentage difference is minimal for
both schemes. At higher values of time step, the Crank-Nicolson scheme showed greaterconvergence, which is affected little by the size of the time step.
Next, the effect of time step refinement on the results of the simulation was investigated. First, the
results of each scheme were compared against its previous time step refinement.
Table showing percentage difference in results at 30s from previous time step refinement for fully
implicit scheme
Cell
number
% difference from previous time-step
refinement at 30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s
1 0 0.0055 0.0011 0.0000
2 0 0.0256 0.0028 0.0000
3 0 0.0584 0.0053 0.0013
4 0 0.0014 0.0000 0.0000
6 0 0.0632 0.0070 0.0000
7 0 0.0053 0.0000 0.0000
8 0 0.0303 0.0023 0.0000
9 0 0.0818 0.0077 0.0026
-
8/11/2019 Numerical Methods Assignment 2
8/25
Table showing percentage difference in results at 30s from previous time step refinement for
Crank-Nicolson scheme
Cell
number
% difference from previous time-step
refinement at 30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s1 0 0.0000 0.0000 0.0000
2 0 0.0000 0.0000 0.0000
3 0 0.0013 0.0000 0.0000
4 0 0.0000 0.0000 0.0000
6 0 0.0023 0.0000 0.0000
7 0 0.0000 0.0000 0.0000
8 0 0.0000 0.0000 0.0000
9 0 0.0000 0.0000 0.0000
We notice that the time step refinement has little effect on the results of the simulation for both
schemes, and especially so for the Crank-Nicolson scheme, which showed almost zero change in
results.
Next, we analysed the graphs of temperature against time for cell 3 for each scheme.
Graph of temperature (C) against time (s) for cell 3 for different time step sizes for fully implicit
scheme
30
35
40
45
50
55
60
65
70
75
80
0 5 10 15 20 25 30
Temperature(degreesCelcius)
Time (s)
Graph of Temperature (degrees
Celcius) against Time (s) for Cell 3
dt=0.5
dt=0.05
dt=0.005
dt=0.0005
-
8/11/2019 Numerical Methods Assignment 2
9/25
Graph of temperature (C) against time (s) for cell 3 for different time step sizes for Crank-Nicolson
scheme
From the graphs, we notice very slight variations between the time steps, from about 3 to 18
seconds of the simulation in the fully implicit scheme. For the Crank-Nicolson scheme, no such
variation can be seen. Nevertheless, the lack of variation for both schemes implies that a time step
of 0.5s was good enough for the simulation.
Lastly, the results at 30 seconds for both schemes were compared against the steady-state results
attained earlier. This comparison was done for all time step sizes.
Table showing percentage difference in results at 30s from steady-state results for fully implicit
scheme
Cell
number
% difference from steady-state results at
30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s
1 0.0308 0.0253 0.0242 0.02422 0.1322 0.1066 0.1037 0.1037
3 0.3005 0.2422 0.2369 0.2356
4 0.0085 0.0071 0.0071 0.0071
6 0.3476 0.2846 0.2776 0.2776
7 0.0290 0.0237 0.0237 0.0237
8 0.1771 0.1468 0.1445 0.1445
9 0.4856 0.4043 0.3966 0.3941
30
35
40
45
50
55
60
65
70
75
80
0 5 10 15 20 25 30
Temperature(degreesCelcius)
Time (s)
Graph of Temperature (degrees
Celcius) against Time (s) for Cell 3
dt=0.5
dt=0.05
dt=0.005
dt=0.0005
-
8/11/2019 Numerical Methods Assignment 2
10/25
Table showing percentage difference in results at 30s from steady-state results for Crank-Nicolson
scheme
Cell
number
% difference from steady-state results at
30s
dt=0.5s dt=0.05s dt=0.005s dt=0.0005s1 0.0242 0.0242 0.0242 0.0242
2 0.1037 0.1037 0.1037 0.1037
3 0.2343 0.2356 0.2356 0.2356
4 0.0071 0.0071 0.0071 0.0071
6 0.2753 0.2776 0.2776 0.2776
7 0.0237 0.0237 0.0237 0.0237
8 0.1445 0.1445 0.1445 0.1445
9 0.3941 0.3941 0.3941 0.3941
Both schemes showed a strong similarity with the steady-state results at 30s. At larger time steps,
the Crank-Nicolson scheme performed slightly better than the fully implicit scheme.
The variation of temperature against time at cell 3 was compared between the fully implicit and
Crank-Nicolson schemes for a time step size of 0.005s.
Graph of temperature (C) against time (s) for cell 3 for a time step size of 0.005s for fully implicit
and Crank-Nicolson schemes.
There was little difference between the two schemes for this time step size. This implies that the
results of the simulation are not strongly dependent on the scheme type.
For a better visualisation of how the temperature distribution changes with time, the contour plots
at 3 second intervals are attached in Appendix A for both the schemes at a time step of 0.005s. The
MATLAB code used to produce the results are attached in Appendix B.
Discussions
30
35
40
45
50
55
60
65
70
75
80
0 10 20 30
Temperature(degre
esCelcius)
Time (s)
Crank-Nicolson
Fully Implicit
-
8/11/2019 Numerical Methods Assignment 2
11/25
Appendix A
Fully implicit
3s
6s
9s
-
8/11/2019 Numerical Methods Assignment 2
12/25
12s
15s
18s
-
8/11/2019 Numerical Methods Assignment 2
13/25
21s
24s
-
8/11/2019 Numerical Methods Assignment 2
14/25
27s
30s
-
8/11/2019 Numerical Methods Assignment 2
15/25
Crank-Nicolson Scheme
3s
6s
9s
-
8/11/2019 Numerical Methods Assignment 2
16/25
12s
15s
18s
-
8/11/2019 Numerical Methods Assignment 2
17/25
21s
24s
-
8/11/2019 Numerical Methods Assignment 2
18/25
27s
30s
-
8/11/2019 Numerical Methods Assignment 2
19/25
Appendix B
Steady state, no hole
clear alln=81*81; %change to appropriate size
A=zeros(n, n);BT=zeros(n,1);
k=1000; %conductivitydelta_x=0.1*3/sqrt(n);q=500*10^3;
%setting the corner boundaries%bottom left cornerA(1,1)=2;
A(1,1+sqrt(n))=-1;A(1,2)=-1;
%top right cornerA(n,n)=4;A(n,n-sqrt(n))=-1;A(n,n-1)=-1;
%top left cornerA(sqrt(n),sqrt(n))=4;A(sqrt(n),sqrt(n)*2)=-1;A(sqrt(n),sqrt(n)-1)=-1;
%bottom right cornerA(n-sqrt(n)+1,n-sqrt(n)+1)=2;A(n-sqrt(n)+1,n-sqrt(n)+2)=-1;A(n-sqrt(n)+1,n-sqrt(n)*2+1)=-1;
%setting the left side boundariesfori=2:(sqrt(n)-1)
A(i,i)=3;A(i,i+sqrt(n))=-1;A(i,i+1)=-1;A(i,i-1)=-1;
end
%setting the right side boundariesfori=(n-sqrt(n)+2):(n-1)
A(i,i)=3;A(i,i+1)=-1;A(i,i-1)=-1;A(i,i-sqrt(n))=-1;
end
%setting the top boundariesfori=2:(sqrt(n)-1)
A(i*sqrt(n),i*sqrt(n))=5;A(i*sqrt(n),(i-1)*sqrt(n))=-1;
A(i*sqrt(n),(i+1)*sqrt(n))=-1;A(i*sqrt(n),i*sqrt(n)-1)=-1;
-
8/11/2019 Numerical Methods Assignment 2
20/25
end
%setting the bottom boundariesfori=1:(sqrt(n)-2)
A(i*sqrt(n)+1,i*sqrt(n)+1)=3;A(i*sqrt(n)+1,(i-1)*sqrt(n)+1)=-1;
A(i*sqrt(n)+1,(i+1)*sqrt(n)+1)=-1;A(i*sqrt(n)+1,i*sqrt(n)+2)=-1;
end
%setting internal cellsfori=1:(sqrt(n)-2)
forj=2:(sqrt(n)-1)A(i*sqrt(n)+j,i*sqrt(n)+j)=4;A(i*sqrt(n)+j,i*sqrt(n)+j+sqrt(n))=-1; A(i*sqrt(n)+j,i*sqrt(n)+j-sqrt(n))=-1; A(i*sqrt(n)+j,i*sqrt(n)+j-1)=-1;A(i*sqrt(n)+j,i*sqrt(n)+j+1)=-1;
end
end
%setting top and left boundary temperaturesfori=1:sqrt(n)
BT(i*sqrt(n))=BT(i*sqrt(n))+200; %top boundaryBT(i)=BT(i)+q*delta_x/k; %left boundary
end
T=A\BT;T1=zeros(sqrt(n),sqrt(n));
fori=1:n
ifrem(i,sqrt(n))==0T1(sqrt(n),i/sqrt(n))=T(i);
elseT1(rem(i,sqrt(n)), ceil(i/sqrt(n)))=T(i);
endend
%out put array with original 9-cell temperaturesifn==9
Tout=T;elseifn==81
Tout=[T(11);T(14);T(17);T(38);T(41);T(44);T(65);T(68);T(71);]; elseifn==27*27
Tout=[T(135-4);T(135-4-9);T(135-4-9-9);T(378-4);T(378-4-9);T(378-4-9-9);T(621-4);T(621-4-9);T(621-4-9-9);];
elseifn==81*81Tout=[T(1134-13);T(1134-13-27);T(1134-13-54);T(3321-
13);T(3321-13-27);T(3321-13-54);T(5508-13);T(5508-13-27);T(5508-13-54);];end
endend
endclfcontourf(T1)
-
8/11/2019 Numerical Methods Assignment 2
21/25
dlmwrite('T_9.txt',Tout,'delimiter','\t')
Steady-state, with hole
clear all
n1=9;n=81*81; % set to appropriate size
A=zeros(n, n);BT=zeros(n,1);
k=1000; %conductivitydelta_x=0.1*3/sqrt(n);q=500*10^3;
k1=500; %conductivity of insulated centre
%setting the corner boundaries%bottom left cornerA(1,1)=2;A(1,1+sqrt(n))=-1;A(1,2)=-1;
%top right cornerA(n,n)=4;A(n,n-sqrt(n))=-1;A(n,n-1)=-1;
%top left corner
A(sqrt(n),sqrt(n))=4;A(sqrt(n),sqrt(n)*2)=-1;A(sqrt(n),sqrt(n)-1)=-1;
%bottom right cornerA(n-sqrt(n)+1,n-sqrt(n)+1)=2;A(n-sqrt(n)+1,n-sqrt(n)+2)=-1;A(n-sqrt(n)+1,n-sqrt(n)*2+1)=-1;
%setting the left side boundariesfori=2:(sqrt(n)-1)
A(i,i)=3;A(i,i+sqrt(n))=-1;A(i,i+1)=-1;A(i,i-1)=-1;
end
%setting the right side boundariesfori=(n-sqrt(n)+2):(n-1)
A(i,i)=3;A(i,i+1)=-1;A(i,i-1)=-1;A(i,i-sqrt(n))=-1;
end
%setting the top boundariesfori=2:(sqrt(n)-1)
A(i*sqrt(n),i*sqrt(n))=5;
-
8/11/2019 Numerical Methods Assignment 2
22/25
A(i*sqrt(n),(i-1)*sqrt(n))=-1;A(i*sqrt(n),(i+1)*sqrt(n))=-1;A(i*sqrt(n),i*sqrt(n)-1)=-1;
end
%setting the bottom boundaries
fori=1:(sqrt(n)-2)A(i*sqrt(n)+1,i*sqrt(n)+1)=3;A(i*sqrt(n)+1,(i-1)*sqrt(n)+1)=-1;A(i*sqrt(n)+1,(i+1)*sqrt(n)+1)=-1;A(i*sqrt(n)+1,i*sqrt(n)+2)=-1;
end
%setting internal cellsfori=2:(sqrt(n)-1)
forj=1:(sqrt(n)-2)A(i+j*sqrt(n),i+j*sqrt(n))=4;A(i+j*sqrt(n),i+(j+1)*sqrt(n))=-1;A(i+j*sqrt(n),i+(j-1)*sqrt(n))=-1;
A(i+j*sqrt(n),i+j*sqrt(n)-1)=-1;A(i+j*sqrt(n),i+j*sqrt(n)+1)=-1;
endend
%setting top and left boundary temperaturesfori=1:sqrt(n)
BT(i*sqrt(n))=BT(i*sqrt(n))+200; %top boundaryBT(i)=BT(i)+q*delta_x/k; %left boundary
end
%setting the cells in the hole to constant 30 degreesfori=1:(sqrt(n)/3)
forj=1:(sqrt(n)/3)A(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n),:)=0; A(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n), j+(sqrt(n)/3)+(i-
1+sqrt(n)/3)*sqrt(n))=1;BT(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n))=30;
endend
T=A\BT;
T1=zeros(sqrt(n),sqrt(n));fori=1:n
ifrem(i,sqrt(n))==0T1(sqrt(n),i/sqrt(n))=T(i);
elseT1(rem(i,sqrt(n)), ceil(i/sqrt(n)))=T(i);
endend
%output array wiith original 9-cell temperaturesifn==9
Tout=T;elseifn==81
-
8/11/2019 Numerical Methods Assignment 2
23/25
Tout=[T(11);T(14);T(17);T(38);T(41);T(44);T(65);T(68);T(71);];elseifn==27*27
Tout=[T(135-4);T(135-4-9);T(135-4-9-9);T(378-4);T(378-4-9);T(378-4-9-9);T(621-4);T(621-4-9);T(621-4-9-9);];
elseifn==81*81Tout=[T(1134-13);T(1134-13-27);T(1134-13-54);T(3321-
13);T(3321-13-27);T(3321-13-54);T(5508-13);T(5508-13-27);T(5508-13-54);];endend
endendclfcontourf(T1)%dlmwrite('T_9.txt',Tout,'delimiter','\t')
Transient, with hole
clear all
n=27*27;rho=8000;dt=0.005;theta=0.5; %theta=0.5 for Crank-Nicolson, 1 for fully implicit
A=zeros(n, n);BT=zeros(n,1);T=zeros(n,1); %initial condition arrayT(:,1)=30;dist=3;
k=1000; %conductivitydelta_x=0.1*3/sqrt(n);
delta_v=delta_x^2;q=500*10^3;
total_t=30;clf
fort=dt:dt:total_t
%setting the corner boundaries%bottom left cornerA(1,1)=rho*delta_v/dt+theta*delta_x*(2*k); A(1,1+sqrt(n))=-theta*k*delta_x;A(1,2)=-theta*k*delta_x;BT(1)=k*delta_x*q*delta_x/k+rho*delta_v/dt*T(1)-(1-theta)*(k*delta_x)*(2*T(1)-T(2)-T(1+sqrt(n)));
%top right cornerA(n,n)=rho*delta_v/dt+theta*delta_x*(4*k); A(n,n-sqrt(n))=-theta*k*delta_x;A(n,n-1)=-theta*k*delta_x;BT(n)=k*delta_x*200+rho*delta_v/dt*T(n)-(1-theta)*(k*delta_x)*(4*T(n)-T(n-sqrt(n))-T(n-1));
%top left cornerA(sqrt(n),sqrt(n))=rho*delta_v/dt+theta*delta_x*(4*k);
A(sqrt(n),sqrt(n)*2)=-theta*k*delta_x; A(sqrt(n),sqrt(n)-1)=-theta*k*delta_x;
-
8/11/2019 Numerical Methods Assignment 2
24/25
BT(sqrt(n))=k*delta_x*(200+q*delta_x/k)+rho*delta_v/dt*T(sqrt(n))-(1-theta)*(k*delta_x)*(4*T(sqrt(n))-T(sqrt(n)-1)-T(sqrt(n)*2));
%bottom right cornerA(n-sqrt(n)+1,n-sqrt(n)+1)=rho*delta_v/dt+theta*delta_x*(2*k); A(n-sqrt(n)+1,n-sqrt(n)+2)=-theta*k*delta_x;
A(n-sqrt(n)+1,n-sqrt(n)*2+1)=-theta*k*delta_x; BT(n-sqrt(n)+1)=rho*delta_v/dt*T(n-sqrt(n)+1)-(1-theta)*(k*delta_x)*(2*T(n-sqrt(n)+1)-T(n-sqrt(n)+2)-T(n-sqrt(n)*2+1));
%setting the left side boundariesfori=2:(sqrt(n)-1)
A(i,i)=rho*delta_v/dt+theta*delta_x*(3*k); A(i,i+1)=-theta*k*delta_x;A(i,i-1)=-theta*k*delta_x;A(i,i+sqrt(n))=-theta*k*delta_x;BT(i)=k*delta_x*q*delta_x/k+rho*delta_v/dt*T(i)-(1-
theta)*(k*delta_x)*(3*T(i)-T(i+1)-T(i-1)-T(i+sqrt(n))); end
%setting the right side boundariesfori=(n-sqrt(n)+2):(n-1)
A(i,i)=rho*delta_v/dt+theta*delta_x*(3*k); A(i,i+1)=-theta*k*delta_x;A(i,i-1)=-theta*k*delta_x;A(i,i-sqrt(n))=-theta*k*delta_x;BT(i)=rho*delta_v/dt*T(i)-(1-theta)*(k*delta_x)*(3*T(i)-T(i+1)-T(i-1)-
T(i-sqrt(n)));end
%setting the top boundariesfori=2:(sqrt(n)-1)
A(i*sqrt(n),i*sqrt(n))=rho*delta_v/dt+theta*delta_x*(5*k); A(i*sqrt(n),(i-1)*sqrt(n))=-theta*k*delta_x; A(i*sqrt(n),(i+1)*sqrt(n))=-theta*k*delta_x; A(i*sqrt(n),i*sqrt(n)-1)=-theta*k*delta_x; BT(i*sqrt(n))=k*delta_x*200+rho*delta_v/dt*T(i*sqrt(n))-(1-
theta)*(k*delta_x)*(5*T(i*sqrt(n))-T((i-1)*sqrt(n))-T((i+1)*sqrt(n))-T(i*sqrt(n)-1));end
%setting the bottom boundariesfori=1:(sqrt(n)-2)
A(i*sqrt(n)+1,i*sqrt(n)+1)=rho*delta_v/dt+theta*delta_x*(3*k); A(i*sqrt(n)+1,(i-1)*sqrt(n)+1)=-theta*k*delta_x; A(i*sqrt(n)+1,(i+1)*sqrt(n)+1)=-theta*k*delta_x; A(i*sqrt(n)+1,i*sqrt(n)+2)=-theta*k*delta_x; BT(i*sqrt(n)+1)=rho*delta_v/dt*T(i*sqrt(n)+1)-(1-
theta)*k*delta_x*(3*T(i*sqrt(n)+1)-T((i-1)*sqrt(n)+1)-T((i+1)*sqrt(n)+1)-T(i*sqrt(n)+2));end
%setting internal cellsfori=2:(sqrt(n)-1)
forj=1:(sqrt(n)-2)A(i+j*sqrt(n),i+j*sqrt(n))=theta*delta_x*(4*k)+(rho*delta_v/dt); %aPA(i+j*sqrt(n),i+(j+1)*sqrt(n))=-theta*k*delta_x; %aSA(i+j*sqrt(n),i+(j-1)*sqrt(n))=-theta*k*delta_x; %aNA(i+j*sqrt(n),i+j*sqrt(n)-1)=-theta*k*delta_x; %aWA(i+j*sqrt(n),i+j*sqrt(n)+1)=-theta*k*delta_x; %aE
-
8/11/2019 Numerical Methods Assignment 2
25/25
BT(i+j*sqrt(n))=(rho*delta_v/dt)*T(i+j*sqrt(n))-(1-theta)*(delta_x*k)*(4*T(i+j*sqrt(n))-T(i+(j+1)*sqrt(n))-T(i+(j-1)*sqrt(n))-T(i+j*sqrt(n)-1)-T(i+j*sqrt(n)+1));
endend
%setting the cells in the hole to constant 30 degreesfori=1:(sqrt(n)/3)
forj=1:(sqrt(n)/3)A(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n),:)=0; A(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n), j+(sqrt(n)/3)+(i-
1+sqrt(n)/3)*sqrt(n))=1;BT(j+(sqrt(n)/3)+(i-1+sqrt(n)/3)*sqrt(n))=30;
endend
T=A\BT;T1=zeros(sqrt(n),sqrt(n));
fori=1:n
ifrem(i,sqrt(n))==0T1(sqrt(n),i/sqrt(n))=T(i);
elseT1(rem(i,sqrt(n)), ceil(i/sqrt(n)))=T(i);
endendifn==9 %sets output cells for 3x3, 9x9, 27x27 and 81x81
Tout=T;elseifn==81
Tout=[T(11);T(14);T(17);T(38);T(41);T(44);T(65);T(68);T(71);];elseifn==27*27
Tout=[T(135-4);T(135-4-9);T(135-4-9-9);T(378-4);T(378-4-9);T(378-4-9-9);T(621-4);T(621-4-9);T(621-4-9-9);];
elseifn==81*81Tout=[T(1134-13);T(1134-13-27);T(1134-13-54);T(3321-
13);T(3321-13-27);T(3321-13-54);T(5508-13);T(5508-13-27);T(5508-13-54);];end
endend
endifrem(t,3)==0%set when to produce an output file and plot. just change thenumber in the rem function to how many seconds you want a plotfigurecontourf(T1)dlmwrite(sprintf('T_9_%f.txt',t),Tout,'delimiter','\t') %output file nameis T_9_(number of seconds simulation has run). Just copy and paste outputinto excel it will be in a columnendend