matlab codes and report in 3 days
CEE 384 Numerical Methods for Engineers Summer 2018 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou
Page 1 of 3
ML Assignment #4
Released: 7/27/2018 Due: 8/2/2018
Please read “MATLAB Assignment Submission Guidelines” in Blackboard before submission. Not following the guidelines will result in loss of credit, even though you may have the correct answer(s). Write code of your own and answer the questions in this assignment.
Submit your code through Cody Coursework. Submit a report through Blackboard
Interpolation is the basis of this assignment. Problem 1: Spline Interpolation To maximize the catch of bass in a lake, it is suggested to throw the line to the depth of the thermocline layer. The characteristic feature of the thermocline layer is the sudden change in temperature. (Read textbook Ch 05.00C for the full story). Table 1 shows some measured temperature vs. depth data. In this problem, you will write MATLAB functions to implement quadratic spline interpolation and identify the depth of the thermocline.
Table 1 Temperature vs Depth for a Lake
Temperature, 𝑦(℃) Depth, 𝑥(𝑚)
19.1 0 19.1 –1 19 –2
18.8 –3 18.7 –4 18.3 –5 18.2 –6 17.6 –7 11.7 –8 9.9 –9 9.1 –10
1) Quadratic spline interpolation Explain the essential ideas of quadratic spline interpolation in the report. Use simple language that a bright high school senior can understand.
CEE 384 Numerical Methods for Engineers Summer 2018 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou
Page 2 of 3
Write a MATLAB function to perform quadratic spline interpolation. Do NOT call the built-in “spline” command and write you own code for quadratic spline interpolation by setting up the equations and solving for the unknown coefficients.
Your function should meet the following requirements: It should be named qspline The function should have one input argument: the query point 𝒙𝒒 ∈ [−𝟏𝟎, 𝟎]. The function should have one output argument, a 𝟑𝟏 × 𝟏 column vector that contains two
parts: 1) the coefficients of the piecewise polynomial in descending powers stored in the first 30
elements of the column vector. In other words, your code should return [𝑎0, 𝑏0, 𝑐0, 𝑎1, 𝑏1, 𝑐1, … ]𝑇 for the spline
𝑦 = { 𝑎0𝑥2 + 𝑏0𝑥 + 𝑐0 𝑥 ∈ [−10, −9] 𝑎1𝑥2 + 𝑏1𝑥 + 𝑐1 𝑥 ∈ [−9, −8]
… …
2) the estimated value at query point, stored in the last element of the vector. Use all of the data points. Assume the first spline is linear. Use “for” loops to help you set up the equations (in matrix form) instead of typing the equations
manually. Demonstrate this part of your code in the report. Use proper MATLAB built-in command to solve for the unknown coefficients once the equations
are setup in matrix form. Use a “for” loop to display the interpolated quadratic spline (the piece-wise equations).
Demonstrate this part of your code in the report. Display the estimated temperature at the query point in the command window. Use a “for” loop to plot the interpolated quadratic spline. Demonstrate this part of your code in
the report. Also mark the given data points as well as the query point in the figure. Properly label the figure, the axes, and the data points.
Your solution will be tested three times with randomly generated input arguments. 2) Compare with built-in command and identify the depth of the thermocline
a. Consult the MATLAB Help feature and explain the use of built-in command “spline(x,y,xq)”. Which interpolation method does this command use?
b. What is the output of built-in command “spline(x,y)”? How can the output of this command be used? How is the command different from command “spline(x,y,xq)”?
c. Use the built-in command “spline(x,y)” and plot the resulting spline using a “for” loop. In the same figure, plot the resulting quadratic spline from question 1). Compare the two splines and discuss your observations.
d. Base on the figure, how deep is the thermocline layer? Your answer could be a range. Explain your answer.
CEE 384 Numerical Methods for Engineers Summer 2018 Arizona State University School of Sustainable Engineering and The Built Environment Dr. Lou
Page 3 of 3
Problem 2: 4th-order Polynomial Interpolation Consider the same application as in Problem 1. In this problem, you will write MATLAB functions to implement polynomial interpolation and compare it with cubic spline interpolation and polynomial regression. Write a MATLAB function to perform 4th-order polynomial interpolation. After it has chosen 5 base data points correctly according to the input (query point), your code can then call the built-in polyfit command.
Your function should meet the following requirements: It should be named fourthInterp The function should have one input argument: the query point 𝑥𝑞 ∈ [−10, 0]. The function should have one output argument that is a 𝟏-by-𝟔 row vector that contains two
parts: 1. the five coefficients of the polynomial in descending powers stored in the first five
elements; 2. the estimated value at query point, stored in the last element.
Choose appropriate base points to fit a 4th-order polynomial based on the input (query point). Avoid “hard coding”. When two data points are of equal distance to the query point but only one can be chosen, choose the point further away from 0. Use an “if” statement to construct three cases that can accommodate all possible query points. Demonstrate this portion of you code in the report.
Display the interpolated polynomial equation and the estimated temperature at the query point in the command window.
Plot the following in the same figure: o Given data points o Results from cubic spline interpolation (use built-in “spline” command) o Results from fourth-order polynomial regression (use built-in “polyfit” command, recall
details from MATLAB Tutorial 4) o Chosen base points for the four-order polynomial interpolation (use a different color or
marker to highlight them) o The interpolated polynomial o The query point on and the fourth-order polynomial interpolation curve
Include title, axis titles, and legend in the figure. Your solution will be tested four times with known and randomly generated input arguments.
Test 1: 𝑥𝑞 = 2.5 Test 2: 𝑥𝑞 = 7.5 Test 3: with randomly generated query point Test 4: with randomly generated query point