Вы находитесь на странице: 1из 6

MEC3456 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.

S UBMITTING YOUR A SSIGNMENT


Submit your assignment online using Moodle. You must include the following attachments:
A ZIP file (NOT .rar or any other format) named in the following way:
Surname_StudentID_Lab_1+2.zip (e.g. Rudman_23456789_Lab_1+2.zip)
The zip file should contain the following:
a. All MATLAB m-files for lab tasks: run_all.m, LabNN.m, Q1a.m, Q1b.m, etc…
b. Any additional MATLAB function files required by your code
c. All data files needed to run the code, including any input data provided to you
d. Any hand calculations or written responses asked for - scanned in as a SINGLE PDF file
YOUR ZIP FILE WILL BE DOWNLOADED FROM MOODLE AND ONLY THOSE FILES INCLUDED IN YOUR
SUBMISSION WILL BE MARKED
We will extract (unzip) your ZIP file and mark your lab based on the output of run_all.m. and any
hand calculations or written responses. It is your responsibility to ensure that everything needed to
run your solution is included in your ZIP file. It is also your responsibility to ensure that everything
runs seamlessly on the (Windows-based) lab computers (especially if you have used MATLAB on a
Mac OS or Linux system).

ENG1060 Assignment Page 1 of 6


M ARKING S CHEME
This lab is marked out of 20 and full marks is worth 5% of your total Laboratory mark for the
semester (NOTE: Labs are worth 30% of the total unit assessment). Code will be graded using the
following criteria:
1) run_all.m produces results automatically (no additional user interaction needed except where
asked explicitly – NOTE, I have included pause commands in run_all so that intermediate answers
can easily be viewed by the demonstrators – please don’t remove them)
2) Your code produces correct results (printed values, plots, etc…) and is well written.
3) Programming style, efficiency of algorithm and quality of output (figures, tables, written text ...)

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.)

ENG1060 Assignment Page 2 of 6


For this lab ONLY, 4 NON-ASSESSED tasks are given that will NOT be
marked. They are included to provide you with an indication of
whether your MATLAB programming skills are up to the minimum
standard expected for the rest of the unit. Although they will not be
assessed, you can ask demonstrators for assistance in coding them.

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.

ENG1060 Assignment Page 3 of 6


T ASK 3 [N OT ASSESSED ]
The Fibonacci numbers are the following sequence of numbers
1, 1, 2, 3, 5, 8, 13, 21, 34, ....
By definition, the first two Fibonacci number are 1, and the next number in the sequence is the sum
of the previous two. Mathematically, the nth Fibonacci number Fn is defined by a recurrence relation
Fn = Fn-1 + Fn-2
(With the first two numbers F0=1 and F1=1. )
Write an M-file to find all the Fibonacci numbers that are less than 1000.
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

ï1520e -0.2(t-30) t > 30


î

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.

Q UESTION 1 [20 M ARKS TOTAL ]


Background
The Jacobi, Gauss-Seidel and Successive Over Relaxation (SOR) methods are methods for iteratively
solving systems of linear equations. We looked at these in Lecture 3 and started coding the Jacobi
method in class (I hope!!). You may use the Jacobi code discussed in lectures as a starting point for
this question if you wish, although I recommend starting afresh.

ENG1060 Assignment Page 4 of 6


Q1a
i) Write a MATLAB function (GS1) that performs a single iteration of the Gauss-Seidel (G-S) method.
The function header MUST be
function [xm1,max_diff] = GS(A,b,xm)
Input parameters to the function are the matrix of coefficients (A) the right hand side vector (b) and
the current solution (xm). Outputs are the updated solution (xm1) and the maximum change in x
(max_diff) as discussed in lectures. Here, max_diff is defined as
æ x m+1 - x m ö
max_diff = max ç i m+1 i ÷
i ç ÷
è xi ø
(Be wary of divide by zero!!) In the function GS, you MUST use the following element-by-element
formula for the G-S iteration as written below, however you are allowed to use MATLAB matrix-
vector operations if you wish.

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)

ENG1060 Assignment Page 5 of 6


Input parameters A, b and xm are the same as for function GS, N takes the same values (N=20, 40, 80
and 160) and omega is the relaxation parameter 0 <  < 2. NOTE:  < 1 is under-relaxation,  =1 is
Gauss-Seidel and  > 1 is over-relaxation. Choose a relaxation parameter of 1.5.

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 .

Poor Programming Practices [-5 Marks]


(Includes, but is not limited to, poor coding style or insufficient comments or
unlabeled figures, etc.)

(END OF LAB)

ENG1060 Assignment Page 6 of 6

Вам также может понравиться