0% found this document useful (0 votes)
107 views

MAE3456 - MEC3456 LAB 02: Due: 11:59PM (Sharp), Friday 19 March 2021 (End of Week 3)

This lab assignment involves completing individual MATLAB coding tasks to analyze experimental data using interpolation techniques like Lagrange polynomials and cubic splines. Students are instructed to download a ZIP file template containing M-files, individually complete the coding tasks in the files, and submit their completed work as a ZIP file by the deadline to avoid plagiarism penalties. The tasks involve using interpolation to estimate values from experimental datasets, constructing cubic spline models by hand and in MATLAB, and evaluating spline fits on synthetic data. Correct and well-commented code, along with accuracy of results and programming style will be graded out of 50 total marks.

Uploaded by

kain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
107 views

MAE3456 - MEC3456 LAB 02: Due: 11:59PM (Sharp), Friday 19 March 2021 (End of Week 3)

This lab assignment involves completing individual MATLAB coding tasks to analyze experimental data using interpolation techniques like Lagrange polynomials and cubic splines. Students are instructed to download a ZIP file template containing M-files, individually complete the coding tasks in the files, and submit their completed work as a ZIP file by the deadline to avoid plagiarism penalties. The tasks involve using interpolation to estimate values from experimental datasets, constructing cubic spline models by hand and in MATLAB, and evaluating spline fits on synthetic data. Correct and well-commented code, along with accuracy of results and programming style will be graded out of 50 total marks.

Uploaded by

kain
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

MAE3456

– MEC3456 LAB 02
Due: 11:59PM (Sharp), Friday 19th March 2021 (End of Week 3)
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 Lab02_Q+T.zip from Moodle and update the M-Files named Lab02_Q1a.m,
Lab02_Q1b.m, etc… with your Lab code. DO NOT rename the M-Files in the template and or 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.

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_02.zip (e.g. Rudman_23456789_Lab_02.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 the 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.
RECOMMENDATION: After uploading to Moodle, download your Lab to a NEW folder, extract it and
ensure it runs as expected. CHECK YOUR PDF IS INCLUDED. You can upload your submission as many
times as you like BEFORE the submission deadline. After this time, it is not possible to resubmit your
lab without incurring a late penalty.

MAE/MEC3456 – Computer Lab 01 Page 1 of 7


M ARKING S CHEME
This lab is marked out of 50 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.)

MAE/MEC3456 – Computer Lab 01 Page 2 of 7


Q UESTION 1 [8 M ARKS TOTAL ]

An experimental study to measure the blood flow rate inside the tissue found that the flow rate
changes with temperature. The data obtained from the experiment is summarized in Table 1.
Table 1: Blood flow rate inside the liver at different temperature elevation
Index, i 0 1 2 3 4 5 6
Temperature rise, DT(°C) 0 1 2 6 8 10 11
Blood flow rate (ml/min) 10 10.11 10.28 9.64 8.72 7.4 6.59

Q1a
Use the third order Lagrange polynomial interpolation to construct a cubic polynomial using the first
four data points from the table. [SHOW ALL WORKING]
Q1b
Use the cubic polynomial that you have constructed in Q1a to determine the blood flow rate when DT
= 8.5°C. [SHOW ALL WORKING]
Q1c
The solution that you get in Q1b is likely not to be accurate. Explain why. Suggest how you would
overcome this problem to obtain the most accurate estimate possible using a cubic polynomial.

Q UESTION 2 [10 M ARKS TOTAL ]

Consider the data in the Table 2 below:


Table 2
i 1 2 3 4
X 0.0 0.5 1.0 1.5
y 3.0 -2.0 1.0 1.7

Q2a
If you construct a cubic spline for the set of data in Table 2, list down the number of splines and the
number of unknowns that you will have.
Q2b
Using the details in Workshop 5 Playlist, OR the outline discussed in Workshop 5 (see in particular
Slides 8 and 11), construct by hand the system matrix that will allow you to determine the unknown
coefficients ci of your system matrix (it will be a 4x4 matrix). Assume natural spline boundary
conditions at both ends. Solve the resulting system matrix for the coefficients ci and work out the
remaining coefficients ai, bi and di. Construct the cubic spline equations based on the values of the
coefficients.

MAE/MEC3456 – Computer Lab 01 Page 3 of 7


Tip: You can solve the system matrix if you like with the aid of Matlab, although c1 and c4 are easily
found, and can be used to estimate the other two c’s. [SHOW ALL WORKING RELATED TO THE SET
UP OF THE SYSTEM MATRIX]

Q2c
Using the solution from Q2b, determine the value of y at x = 0.75 by hand. [SHOW ALL WORKING]

Q UESTION 3 [12 M ARKS TOTAL ]

In Question 3, you will be writing Matlab code that will perform a 1D spline interpolation across N
arbitrary points.

Q3a
Write a MATLAB function that calculates the spline coefficients as defined in Workshop 05. The
function header MUST be
function [a,b,c,d] = SplineCoeff(x,y)
Here, the input parameters are

• x - a vector of x-values of the input data (“n” points)


• y – a vector of the function values (i.e. y = f(x)) (“n” points)
• The output parameters are the spline coefficients a, b, c, d – each of which is a vector that is
(n-1) long.
NOTE: Write your code such that it implements natural spline boundary conditions. You may write
and solve the equations for the ci using either
1. a complete tridiagonal matrix (in which case the diag function might help) and regular MATLAB
matrix inversion OR
2. just the tridiagonal elements and the Thomas algorithm. (I have included Thomas.m in the
template, and have changed the nomenclature for the 3 diagonals (a,b,c) to (L,D,U) to avoid
confusion with (a,b,c,d) in the spline definition.)
Q3b
Write a MATLAB function that calculates the value of the interpolated y-values specified by the spline
coefficients. The function header MUST be
function [y] = SplineCalc(a,b,c,d,xi,xp)
where the input parameters are

• a,b,c,d – the spline coefficients determined by SplineCoeff


• xi – the x-values of the original data
• xp – the value at which you are trying to find the spline value

MAE/MEC3456 – Computer Lab 01 Page 4 of 7


Q3c
Modify the template Lab_02_Q3c.m to include the code that interpolates the value of y at xp = 0.75.
based on the data in Table 2. You are required to print your spline coefficients with one decimal point
accuracy to the command window in the order:
a1, b1, c1, d1
a2, b2, c2, d2
a3, b3, c3, d3

Print also the value of y at xp = 0.75 with 3 decimal points.


Q3d
Modify the template Lab_02_Q3d.m to create a data set that is given by x = linspace(0,4p,20) and y
given by:
y(x) = cos(x)*sin(2*x)
Undertake the following tasks:

• Fit a spline to the 20 data points above using natural BCs at both ends.
• Calculate the spline value at 100 equally spaced points (including BOTH end points).
• Calculate the percentage error obtained for each point.
• Plot the natural spline using these 100 interpolated (x,y) values using a blue curve to represent
the interpolated data. On the same figure, plot the original data points using red dots.
• Using the yyaxis function, plot the percentage error at the 100 corresponding points using a
red curve and the right-hand-side y-axis.
• Print a short statement to the command window commenting what you observe and explain
why you obtain such a result.

Q UESTION 4 [14 M ARKS TOTAL ]

A two-dimensional interpolation over a uniform grid can be carried out by performing 1D spline
interpolation twice, once in the x direction where all the necessary yp values are gathered, and once
in the y direction to obtain the final output. In this question, you will be constructing a 2D spline using
the 1D spline functions that you have written in Question 3.

Q4a
Write a MATLAB function that executes all the steps required to do spline interpolation (i.e. it
calculates the spline coefficients AND estimates the value). The function header should look like
function [yp] = Spline1D(xi,yi,xp)
Here, the inputs are
• xi – the x-values of the data.
• yi – the y-values that are to be interpolated

MAE/MEC3456 – Computer Lab 01 Page 5 of 7


• xp – the x-value we want to interpolate to
The output, yp, is the interpolated value of y.
Hint: You should use SplineCoeff and SplineCalc that you wrote in Q2 inside Spline1D).
Q4b
Taking note of how a two-dimensional spline interpolation is carried out, write a MATLAB function
that does 2D spline interpolation from a grid to a single point (i.e. the function does everything needed
to calculate the coefficients and do the interpolation). Your function header should look like:
function [zp] = Spline2D(xi,yi,zi,xp,yp)
Here, the inputs are
• xi – the x-values of the data.
• yi – the y-values that are to be interpolated
• zi – the function (z) values defined on the grid defined by (xi,yi)
• xp, yp – the (x,y) point we want to interpolate to
The output, zp, is the interpolated value of the function z.
NOTE: write your code so that it interpolates in the x-direction first and then y.
Q4c
A 2D uniformly distributed grid point can be easily set up in Matlab using the command:
[x,y] = meshgrid(xi,yi);
In this case, xi and yi are two vectors in the x- and y-directions that can be constructed using the
function linspace. The output [x,y] is a matrix where the row entries represent constant y values
and the column entries represent constant x values.
In the template Lab_02_Q4c.m, write a Matlab code that will generate a 21 x 21 uniformly distributed
grid points such that xiÎ[-1, 1] and yiÎ[-1, 1]. For each of the grid point, evaluate the value of z based
on the function:
𝑧(𝑥, 𝑦) = (𝑥 ( + 𝑦 ( )
Now, generate another set of uniformly distributed grid points over the domain xiÎ[-1, 1] and yiÎ[-1,
1], but this time with a size of 51 x 51. In other words, there will be 51 points across the x-direction
and another 51 points across the y-direction. Hint: you may to use the MATLAB function meshgrid
to generate this second set of 2D arrays.

Continue with Lab_02_Q4c.m and write a Matlab code that will perform a spline interpolation across
the 51 x 51 data points using the 21 x 21 data points.
Plot the values of zi evaluated directly from the function based on the given data (i.e. the 21 x 21
points) in a surface plot using the Matlab function surf(xi,yi,zi).
In a different figure, plot the interpolated values of zp across the interpolation points (i.e. the 51 x 51
points) in a surface plot using the Matlab function surf(xp,yp,zp).

MAE/MEC3456 – Computer Lab 01 Page 6 of 7


Print a short statement to the command window what you think will happen if you repeated the
same process by using 2D Lagrange interpolation. Explain your answer.

Q UESTION 5 (BONUS QUESTION) [10 M ARKS TOTAL ]

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 50 marks for the lab.
Write a 1-D Lagrange polynomial interpolation function, and repeat Q4 with Lagrange polynomials
instead of splines. (Most of this question can be done by copying the Q4 code except for the 1D
interpolation). If you do this question, you can check if your answer to the last part of Q4 is correct.

Poor Programming Practices [-5 Marks]


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

(END OF LAB)

MAE/MEC3456 – Computer Lab 01 Page 7 of 7

You might also like