Professional Documents
Culture Documents
1 Introduction
Numerical methods have been √ used even before computers existed. They were used by the ancient Greeks
to calculate things such as 2. Today they are used to find approximate but accurate solutions to many
difficult problems. In this report, numerical methods will be used to find solutions to three problems
which are a heat flow system, a trampoline and a fluid flow over cylinders.
A general approach to modelling such systems would be through Laplace’s equation or Poisson’s
equation. Although Laplace’s equation underlies these systems, the boundary conditions specified in
each situations makes each solution unique.
Iterative methods are used to progressively reduce the error in the overall plot to a set tolerance. A
stencil takes the average of the values around a grid point.
When the data set is large, iterative methods on its own would be slow. In this modelling project,
successive over relaxation and multigrid methods were used to speed up convergence.
1
3 User interface and user input
The graphical user interface is started by running the ‘Guide.m’ file. After selecting the desired question,
an input dialog appears. The user is able to specify the changes in the input variables. If the user chooses
not do change anything, default values are used.
Input values by the user are checked if they lie within the given parameters of the question. For
example, if a non-numerical number is inserted, an error dialog will appear to remind the user of his
mistake. User inputs that do not lie within the boundaries of the grid are also rejected. If the user
chooses to close the dialog boxes before entering anything, errors do not appear as they are caught.
4 Methods tried
4.1 Vectorized Jacobi Method
The Jacobi method iterative equation is:
k+1
(∆y 2 (Ti+1,j
k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 ))
Ti,j = 2 2
(1)
2(∆x + ∆y )
The requirement for such an equation to be vectorized is that all the values at RHS must be non changing
in the process of an iteration. This is true as the time levels of the terms at RHS are the same. Vector-
ization eliminates the requirement of for loop as given in the example code, because for loops are very
slow in Matlab. The speed difference is shown in table 1.
2
This method converts T k and T k+1 into linear indexing array, the constant coefficients into a matrix.
The matrix, E has :
∆y 2
1. Constants 2(∆x2 +∆y 2 ) at diagonals -ny+1 and ny-1
∆x2
2. Constants 2(∆x2 +∆y 2 ) at diagonals -1 and 1
3. Constants 1 at diagonal elements where the boundary condition of T=100 and T=0 applies.
The new equation is: ET k = T k+1 . The matrix E is made in Matlab using diag function and is shown
in Figure 2. ˜ ˜
This method is extremely slow as a 3200x3200 matrix is very big, it has to do a lot of unnecessary
calculation as most of the matrix are zeros. It only ran 6252 times in 47.81 seconds. Even with Matlab
function sparse matrix, which ignores the zeros, it is still slower than normal Jacobi method, hence it is
not employed in the code.
Notice the RHS of the equation uses values from time values of k+1 which is different from the explicit
solution method. This means that this method uses updated solution in the same iteration. In the same
iteration, the solution calculated in the previous data block will be used to calculate the present solution
which is not the case in the Jacobi method where in the same iteration, all values used to calculate the
present solution are from the previous iteration. This can be visualized easier with figure 3 where the
k+1
values used to calculate Ti,j are from surrounding 4 values.
(a) (b)
Figure 3: (a) Jacobi method visualized (b) Gauss Seidel method visualized
3
Assuming the data is calculated top row to bottom row from left to right, GS uses solution calculated on
top and at the left in the current iteration and solution calculated at bottom and right from the previous
iteration. Now that the Gauss-Seidel equation is understood, the Successive over-Relaxation equation
(SOR) uses a slightly different equation than the Gauss-Seidel method, it has another coefficient, α,
known as relaxation parameter as shown.
h ∆y 2 (T k k+1 k+1
k+1 k i+1,j + Ti−1,j ) + ∆x2 (Ti,j+1
k
+ Ti,j−1 ) k
i
Ti,j = Ti,j +α − Ti,j (6)
2(∆x2 + ∆y 2 )
Note that the Gauss-Seidel equation is actually just Successive over-Relaxation but with α=1. This
equation can be intepreted as the solution for a point in the current iteration is the solution of that point
from the previous iteration adding a ‘correction’, the difference between the GS solution and the solution
from the previous iteration, which is multiplied by α. The main advantage of SOR and GS compared to
the Jacobi method is the data signal travels faster across the grid in one iteration so answers converge
faster. This is because SOR and GS uses solution in the present iteration which is closer to the end
answer, the solution calculated will too be closer to the end answer. Theoretically, relaxation parameters
takes value only between 0 and 2 without divergence of solution, relaxation parameters that are less than
1 are under-relaxed and reducing speed of convergence.[2] In SOR, as alpha increases, the correction
becomes larger hence the solution converges to the end answer with less iterations until a optimal alpha
value where more iterations are needed after that optimal alpha. If alpha is too large, the solution will
actually diverge from the end answer. To demonstrate this, a code is written with reference to a Matlab
code [4] to show a plot of iteration counts vs alpha by solving the default thermodynamic question with
alpha values from 1.985 to 1.993 by 0.001 steps as shown in figure 4.
6700
6600
6500
Iteration count
6400
6300
6200
6100
X: 1.991
6000 Y: 6053
1.985 1.986 1.987 1.988 1.989 1.99 1.991 1.992 1.993 1.994
Alpha
It can be seen that when α=1.991, the iteration required to converge to the end answer is 6053 which
is the lowest among the three relaxation methods. Generally, SOR is faster than GS, as the α accelerates
the calculation. The general shape of the answer between two of them are similar but there are major
differences at the boundary of circles in SOR due to α value. The difference in default question 1 is shown
in Figure 5. Note that the discontinuity is not eliminated fully in GS but minimized.
4
(a) (b)
1. It uses for loops which limits its potential as for loops in Matlab are slow.
2. It produces discontinuities at the boundary of circles.
3. The relaxation parameter, α differs with different grid resolution which might cause adverse effect on
the speed causing it to be slower than Jacobi method in some grid resolution. If the grid resolution
is not equal in the x direction and y direction, α drastically differs and even with values below 2, the
solution might diverge which led to an attempt to calculate the optimal Successive over-Relaxation
parameter.
5
1800 3500
1600
3000
1400
Theoretical Optimal Alpha Theoretical Optimal Alpha
2500
Iteration count
Iteration count
1200
1000 2000
800
1500
600
1000
400
200 500
1.88 1.9 1.92 1.94 1.96 1.98 2 1.91 1.92 1.93 1.94 1.95 1.96 1.97 1.98 1.99
Alpha Alpha
(a) (b)
Figure 6: (a) Comparation for grid size 100 (b) Comparation for grid size 200
The results agree well with the theoretical value as the alpha required for the minimum iteration is
close to the theoretical optimal alpha. However, this method is not employed in the 2D problems as the
calculation of eigenvalues of (320 × 160) × (320 × 160) matrix is too big and not allowed in Matlab. The
method employed to determine α is through trial and error with multiple combination of grid resolutions x
and y and using a conditional statement to choose alpha depending on the combination of grid resolution.
This method is not perfect, so if the answer output is a diverged answer, please change α in the user
input to 1 as Gauss Seidel is definitely stable even though slower or lower α from the default value.
6
Figure 8: Matlab Red Black Ordering equation
Surprisingly, RBO minimizes the discontinuities in SOR too, just like GS, even with an optimal alpha
value. Nonetheless, the discontinuity will be corrected at the last step with 100 iterations of Jacobi
method. This method is the fastest hence it is employed in the code.
Two methods were tested to fill in the intermediate values. This could be done by simple linear
interpolation or simply duplicating a single value four times as it is transferred into a finer grid. The
code is displayed in figure 9.
7
5 Differences in implementation between problems
5.1 Thermodynamics
In this problem, the boundary conditions are given by the temperature of the plates at the top and bottom
corners. The rest of the edges of the rectangular grid have similar temperature as the next innermost
value.The position of the array elements that lie in the circle are found using the ‘find’ function along
with the general formula of ((x − xcentreof circle )2 + (y − ycentreof circle )2 ) ≤ radius. The mean of the
temperature values within the circle is calculated and applied throughout the circle to simulate the effects
of the superconducting fluid.
5. The linearly indexed coordinates of the circle are obtained with self-written circle function(circle).
The value used in the cylinder follows the value of inflow velocity multiply the y coordinate of the
circle, the value will be assigned as a constant throughout the cylinder. This means that different
cylinder will have different constant value throughout.
The bolded line at the center is the rip coincident with the mesh boundary, the rip runs from
yripstart to yripend, with adjacent displacements xleft and xright. Notice that the displacements
8
adjacent to the rip are the same as the next inner displacement e.g. a=a, e=e. The values in i and
j is the mean value of the displacement in i and j, the same goes to k and l.
2. The way the displacements are calculated are slightly different compared to Question 1 and 2.
There is an extra forcing term which is used only when calculating displacements in the position of
the circular position of the gymnast’s feet, while outside the circular feet the equation used has no
forcing term. The equation used in the circle is:
h ∆y 2 (T k k
+ Ti−1,j ) + ∆x2 (Ti,j+1
k k
+ Ti,j−1 ) ∆x2 ∆y 2 mg i
k+ 12 k i+1,j k
Ti,j = Ti,j +α − Ti,j − (10)
2(∆x2 + ∆y 2 ) 2(∆x2 + ∆y 2 ) Ak
1 1 1 1
h ∆y 2 (T k+ 2 + T k+ 2 ) + ∆x2 (T k+ 2 + T k+ 2 ) ∆x2 ∆y 2 mg i
k+1 k i+1,j i−1,j i,j+1 i,j−1 k
Ti,j = Ti,j +α − Ti,j − (11)
2(∆x2 + ∆y 2 ) 2(∆x2 + ∆y 2 ) Ak
3. Question 3 the circle function that has another extra output (circ) which is the logical matrix
of the circle where it has the value of 0 outside the circular feet and value of 1 inside. "circ"
is used to include the forcing term only when calculating inside the circle. It is implemented by
∆x2 ∆y 2 mg ∆x2 ∆y 2 mg
multiplying with the forcing term, 2(∆x 2 +∆y 2 ) Ak , to become 2(∆x2 +∆y 2 ) Ak *circ so inside the circle,
the equation will have an extra term to minus compared to outside the circle.
6 Results
Figure 11: Surface plot and convergence plot against iteration count for thermodynamics
Figure 12: Surface plot and convergence plot against iteration count for fluid dynamics
9
Figure 13: Surface plot and convergence plot against iteration count for structural dynamics
7 Discussion
1. Different methods yield quite different solutions even though the general shape is similar. In the
default question 1, the circle temperature differs with different methods as shown in table 3.
This is strange as it would mean some of them are not accurate approximation of the actual solution.
However, when the circle obstruction is removed, all the solution yielded from different methods
are the exact same. This means that unnatural features like the circle and rip which are not related
to Laplace or Poisson equation does affect the answer significantly for different methods and that
theres no problem with the methods themselves.
2. The result of different methods are as shown in table 4.
According to the table, SOR is faster than RBO. However, it introduces large discontinuities into
the solution so it is not employed in the code. It is surprising to see that the Jacobi method has
lower iterations than GS due to the fact that GS uses updated solutions in a current iteration, GS
10
should be faster. The large time difference between GS and Jacobi is simply because the Jacobi
method is removed of for loop.
3. Removal of for loops and replaced with vectorizing methods provided the most significant time
reduction.[5] This is followed closely by multigrid methods which reduced a large number of itera-
tions.
4. Array preallocation is important for reducing computation time. If it is not done, MATLAB has to
copy the old values to the new memory location, it has the effect of increasing peak memory usage.
5. The convergence for all three questions takes less than 1 second if the error was not reset after each
multigrid iteration. If the error was reset, the computation time will increase. Other methods like
red black ordering Successive Over Relaxation method are explored. The changes in the code to
reset the error is shown in figure 16.
6. If SOR is not used, the simple linear interpolation method for the multigrid works better. If SOR
is used, the duplication method for the multigrid is better.
dT
7. It is noticed for dT0
in figure 18, a larger dT0 value will allow the fraction to decrease faster than
the specified tolerance, ‘tol’. However a larger dT0 value would mean that the values in the array
are still far away from the answers. A balance would need to be found for convergence to occur
faster.
8. As grid resolution changes, the optimal value for the optimal over-relaxation parameter would
change. Using an unoptimized value would delay convergence and in certain cases cause an incorrect
surface or contour plot.
9. When ‘sub2ind’ was used to convert subscripts to a index value, the time taken for convergence is
much longer.
10. Functions were created to find the position of the cylinders or circles. This made the code more
presentable especially in the fluid dynamics question.
11. The approach to solving all three questions were quite similar with adjustments needed to specify
the boundary conditions and edges of the circles, cylinders and the rip.
8 Conclusion
When iterative computation is performed on large arrays, it is necessary to implement multigrid methods
or improved relaxation methods when performing Laplace or Poisson equations in large grid sizes.Vectorizing
the code is important but there is little incentive to do so if it involves few iterative use of it.
Successive Over Relaxation methods are one of the fastest relaxation methods but they do not work
well near obstructions in the grid and require an optimal alpha value for it to be fast. Large alpha values
will diverge solutions while small alpha values will reduce speed. Red Black Ordering SOR is even faster
and minimizes discontinuities.
References
[1] Utah State University, Numerical Solution of Laplace Equation. Accessed 20 November 2017.
<http://ocw.usu.edu/Civil_and_Environmental_Engineering/Numerical_Methods_in
_Civil_Engineering/LaplaceDirichletTemperature>
[2] Computational Fluid Dynamics online, Successive over-relaxation method - SOR – CFD-Wiki, the free
CFD reference. Accessed 20 November 2017. <https://www.cfd-online.com/Wiki/Successive_
over-relaxation_method_-_SOR>
[3] J. K. Reid (1966), A method for finding the optimum successive over-relaxation parameter. Accessed
20 November 2017.
<https://academic.oup.com/comjnl/article-pdf/9/2/200/1728031/9-2-200.pdf>
[4] Mathworks UK, Successive Over Relaxation (SOR) of Finite Difference Method solution to Laplace’s
Equation. Accessed 30 November 2017.
<https://uk.mathworks.com/matlabcentral/fileexchange/34260>
[5] Stephen J.Chapman. (2008) Matlab Programming for Engineers. 4th Edition. Thompson. Chapter 4.
166-169
11