MAE3456 - MEC3456 LAB 01: Due: 11:59PM (Sharp), Friday 12 March 2021 (End of Week 2)
MAE3456 - MEC3456 LAB 01: Due: 11:59PM (Sharp), Friday 12 March 2021 (End of Week 2)
– MEC3456 LAB 01
Due: 11:59PM (Sharp), Friday 12th March 2021 (End of Week 2)
This lab should be completed INDIVIDUALLY. Plagiarism will result in a mark of zero. Plagiarism
includes letting others copy your work and using code you did not write yourself without citing the
source. Collaborating with others to discuss algorithms and details of MATLAB syntax and structures
is acceptable (indeed encouraged), however you MUST write your own MATLAB code. All
assignments will be checked using plagiarism-detecting software and similarities in submitted code
will result in a human making a decision on whether the similarity constitutes plagiarism.
I NSTRUCTIONS
Download template.zip from Moodle and update the M-Files named Lab01_Q1a.m,
Lab01_Q1b.m, etc… with your Lab code. DO NOT rename the M-Files in the template and ONLY modify
run_all.m (except to add in running scripts that answer any bonus questions
I might ask). Once you have coded, check your solutions to the questions by running run_all.m
and ensuring all questions are answered as required.
M ARKING S CHEME
This lab is marked out of 20 and contributes 4% toward your total unit mark for the semester. Code
will be graded using the following criteria:
1) run_all.m produces results automatically (no additional user interaction needed except where
asked for explicitly)
2) Your code produces correct results (printed values, plots, etc…) and is well written.
3) Programming style, completeness of comments, efficiency of algorithm and quality of output
(figures, tables, written text ...) will be assessed in this lab and marks will be lost for poor style.
A SSIGNMENT H ELP
1) You can ask questions in the Discussion Forum on Moodle
2) Hints and additional instructions are provided as comments in the assignment template M-Files
3) Hints may also be provided during lectures
4) The questions have been split into sub-questions. It is important to understand how each sub-
question contributes to the whole, but each sub-question is effectively a stand-alone task that
does part of the problem. Each can be tackled individually.
5) I recommend you break down each sub-question into smaller parts too, and figure out what needs
to be done step-by-step. Then you can begin to put things together again to complete the whole.
6) To make it clear what must be provided as part of the solution, I have used bold italics and a
statement that (usually) starts with a verb (e.g. Write a function ..., Print the value..., etc.)
⎛ 4 −1 0 ⎞ ⎛ x1 ⎞ ⎛ 3 ⎞
⎜ ⎟⎜ ⎟ ⎜ ⎟ Eqn (1)
⎜ −1 4 −1 ⎟ ⎜ x2 ⎟ = ⎜ 2 ⎟
⎜⎝ 0 −1 4 ⎟⎠ ⎜ x ⎟ ⎜⎝ 3 ⎟⎠
⎝ 3 ⎠
Q1a
Write the 3 scalar equations that describe the Gauss-Seidel iteration for this system. Each equation
( )
should look like xil+1 = RHSi .
Q1b
Perform TWO iterations by hand using an initial guess of xi = 0. Clearly show what the first and second
estimates of xi are. [SHOW ALL WORKING]
Q1c
Rewrite the system you wrote in Q1a as a matrix equation that looks like Lx l+1 = Rx l + b , i.e. explicitly
describe what the matrices L, R and vector b are equal to.
Q2a
As part of the Lab template, you are given a MATLAB function, GS.m, that performs one iteration of a
Gauss-Seidel iteration. Your task in this question part is to write a MATLAB function that performs
ONE iteration of Successive Over-Relaxation (SOR). You can write your own function from scratch OR
you can take GS.m and modify it.
In either case, your function header MUST be
function [xnew] = SOR(A,b,xold,omega)
Here, A is the system matrix, b is the RHS column vector, xold and xnew are the l and l+1 iterations of
the unknown column vector x and omega is the relaxation parameter 0 < omega < 2. Make sure you
comment your function. (NOTE: omega=1 should give you identical results to Gauss-Seidel.)
Q2b
Take the template Lab01_Q2.m and modify it so that it does the following:
2. Performs a loop that iterates the Gauss-Seidel solver until Ax l+1 − b ≤ ε , where e is a
tolerance that you specify. Start with an initial guess of x = 0.
(NOTE: ( Ax l+1 − b ) is a column vector that would be ALL zeros if x satisfied the equation.
Convergence of the solver as specified above is equivalent to ensuring the length of that vector
is less than e.)
3. Keeps track of how many iterations are required to converge.
4. Places this loop inside an outer loop that uses values for e of [10-4, 10-6, 10-8, 10-10 , 10-12]
(start with an initial guess of x = 0 for all e).
5. Writes the number of iterations for each tolerance to the command window with suitable
description (i.e. DON’T just write the numbers).
6. Plots the number of iterations as a function of the tolerance, e. (Use log-linear coordinates
with the MATLAB function semilogx – see template).
7. In a separate outer loop, repeats steps 2-6 for you SOR function with omega=1.7 (plot the SOR
results in the same figure as the Gauss-Seidel results, but with different symbols/lines).
Finally, print a short comment to the command window that summarises your comparison of these
two different methods. Is the result what you expected? Why or why not?
Background
In workshop 3 we reviewed root finding that was covered in ENG1060. We also saw how multi-
dimensional root finding could be formulated as a matrix problem.
Q3a
Consider the following two equations:
y = cos x
Eqn 2
( x − 1) + ( y − 1)
2 2
=4
i) Write the explicit 2x2 system of equations you would need to solve for increments (dx, dy) in the
solution using the 2D Newton-Raphson method when the current guess is written as (xl, yl). (Create
your functions f (x,y), g (x,y) by moving EVERYTHING to the LHS of the equations above.)
Q3b
i) Write a MATLAB function which determines where two functions intersect using a Generalized
Modified Secant technique. Your function file should have the following function header line:
function [xr,yr,nit]= ModSec_2D(x0,y0,f,g,tol)
where: the return values xr, yr is the intersection point found by the function and nit is the number of
iterations the function took to find the root. The input parameters are the initial guess (x0,y0), the
two functions (f(x,y), g(x,y)) and the root-finding tolerance (tol).
∂ f ∂ f ∂g ∂g
As part of this function, you will need to estimate each of the 4 derivatives, , , using the
∂x ∂ y ∂x ∂x
modified secant method. This can be done inside the function OR as a separate stand-alone function
(or functions) or any other way as you wish. (NOTE: You cannot use symbolic differentiation – that is
NOT using the modified secant method).
It is possible that a root does not exist, so implement some kind of error checking inside this function
that prints a warning message if the function does not appear to be converging.
HINT: You can base your algorithm on the 2D Newton-Raphson method we discussed in class.
ii) Test your function by finding two different points where the two functions given in Equation 2
intersect. Use any initial guesses you like (you will need to hard code 2 different initial guesses to find
2 different roots) and choose a tolerance of 10-10.
Write the intersection values to the screen with 6 significant figures and the initial value you used to
find that root AND the number of iterations taken.
iii) How can you tell if the root you have found is correct to within the tolerance? Print a few sentences
to the command window describing how you can do this and demonstrate that it is true for the roots
you found.
iv) Print a short message to the screen that describes the convergence error checking you
implemented when writing the function.
This question is optional and can be used to offset marks lost elsewhere in THIS lab
only. NOTE that it is NOT possible to get more than 20 marks for the lab.
Write to the command window the number of iterations for G-S and SOR as a function of problem size
(N)
Plot the number of iterations as a function of problem size (N) for both methods (on the same plot in
a linear-linear plot).
Print a statement to the command window that summarises what you find. How do you think this
would compare to using Gaussian Elimination to solve this problem?