Lab 01
Lab 01
Lab 01
Due date: Sunday 18th March 2018, 9.00pm. Lab01 must be submitted together with
Lab02.
This lab should be completed INDIVIDUALLY. Plagiarism will result in a mark of zero. Plagiarism
includes letting others copy your work and using code 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 or modify
run_all.m or Lab01.m. Check your solutions to the questions by running run_all.m and
ensuring all questions are answered as required.
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.)
T ASK 1 [N OT ASSESSED ]
Read the Appendix at the end of this assignment sheet and ensure you can do all of the tutorial
exercises.
T ASK 2 [N OT ASSESSED ]
Truncation error is the error introduced by the use of an approximate expression in place of an exact
mathematical expression or formula. For example, the function ln(2+x) can be expressed as a Taylor
series as:
¥
(-1)i+1
ln(2 + x) = å (1+ x)i
i=1
i
1 1 1 1 1
= (1+ x) - (1+ x)2 + (1+ x)3 - (1+ x)4 + (1+ x)5 - (1+ x)6 + ...
2 3 4 5 6
Write an M-file to implement this formula for “n” terms so that it computes and displays the values
as each term in the series is added to the expression. For example, if n=5, compute and display to
the MATLAB command window the sequence of values:
ln(2 + x) » 1+ x
1
ln(2 + x) » (1+ x) - (1+ x)2
2
1 1
ln(2 + x) » (1+ x) - (1+ x)2 + (1+ x)3
2 3
1 1 1
ln(2 + x) » (1+ x) - (1+ x)2 + (1+ x)3 - (1+ x)4
2 3 4
1 1 1 1
ln(2 + x) » (1+ x) - (1+ x)2 + (1+ x)3 - (1+ x)4 + (1+ x)5
2 3 4 5
In addition to the function estimate, also print (on the same line) the percentage truncation error.
Put this calculation inside a while loop that asks the user for the number of terms to be used. Nest
that while loop inside another while loop that asks the user to input a value of x.
As a test case, use your program to compute ln(1.5) for 1, 2, 3, ... and 8 terms. Plot the error in
ln(1.5) as a function of n on a log-log plot with blue circles.
Note: Please try to draw a flow chart or an “algorithm” description before you start to code.
T ASK 4 [N OT ASSESSED ]
Piece-wise functions are sometimes useful when the relationship between a dependent and
independent variables changes with time and thus cannot be adequately represented by a single
equation. For example, the velocity of a rocket might be described by:
ì0 t <0
ï
ï11t 2 - 5t 0 £ t £ 10
ï
v(t) = í1100 - 5t 10 £ t £ 20
ï
ï50t + 2(t - 20) 20 £ t £ 30
2
Write a MATLAB function that calculates the velocity given a value of time. Use this function to
calculate the velocity from t=-5 to 50 at an increment of 0.5. Plot these values with a red line and
ensure your plot has suitable axis labels and plot title.
Note: Please try to draw a flow chart or an algorithm description before you start to code.
1é ù
i-1 n
(
xi
m+1)
= êbi - å aij x j - å aij x j ú
m+1 m
aii êë j=1 j=i+1 úû
You are provided with a MATLAB function set_A(N)that sets the coefficients A and the rhs vector
b you should use when running your code. You set the matrix for an NxN system by choosing an
appropriate N.
ii) Modify the m-file Lab_01_Q1a.m and perform Gauss Seidel iteration to convergence on the
system defined by setA(N) with N=20, 40, 80 and 160. Use an initial solution guess of x=0.
iii) For each N record the number of iterations to convergence (convergence is defined here as
max_diff to decrease below a tolerance of 10-6 ). Use the MATLAB functions tic and toc to time how
long it takes to converge fir each different N. For each N, display on the screen a single line which
prints the values of N, the number of iterations and the time taken to converge) with suitable
description – i.e. don’t just write 3 numbers). Plot the run times as a function of N in a properly
labeled figure using magenta circles and log-log coordinates.
iv) Write a short statement to the command window about what you observe in the plot and what
you think it means. What ENG1060 (or other) numerical techniques might help you quantify this
relationship? Write a short statement to the command window stating what this technique is.
v) Even if your code converges, how can you determine if you have found the correct answer for x?
Write a short paragraph to the command window stating how and then implement this in the code
in Lab_01_Q1a.m and demonstrate the answer you find is correct.
Q1b
i) Repeat Q1a i) except using successive over-relaxation (SOR) instead of Gauss-Seidel. Write a new
function called SOR and a new running script in the file Lab_01_Q1b.m. The function header for SOR
MUST be
function [xm1,max_diff] = SOR(A,b,xm,omega)
ii) Repeat Q1a ii) and iii) with SOR and Plot the run times as a function of N in the SAME figure as
Q1a so the execution times of Gauss-Seidel and SOR can be compared. Make sure the different data
are clearly identifiable as being either Gauss-Seidel or SOR.
iii) Write a short paragraph to the command window describing what you find and which method is
best and why.
Q1c
Using your SOR function, write a new script file Lab_01_Q1c.m in which your SOR function is used
with coefficients from setA(N), a value of N=200 and relaxation parameters of 1 to 1.9 in increments
of 0.1. Plot the run time as a function of omega in a new plot.
Write a short paragraph to the command window describing what happens if you set omega = 2.0.
Q1d
In lectures we saw that a Gauss-Seidel iteration could be written in matrix form as
m+1 -1 m+1 m
x = D (b – Lx – Ux )
m+1
This isn’t very useful because x appears on both sides of the equation. By hand, show the matrix
algebra (and final formula) required to write this equation as
m+1
x = RHS
m+1
where RHS is NO longer a function of x .
(END OF LAB)