Your cart is currently empty!
Please submit to CANVAS a .zip le that includes the following Matlab functions: poly least squares.m test least squares finance.m test least squares interp.m Exercise 1 Write a Matlab function poly least squares.m that implements the least squares method we discussed in class to approximate a data set in terms…
Please submit to CANVAS a .zip le that includes the following Matlab functions:
poly least squares.m
test least squares finance.m
test least squares interp.m
Exercise 1 Write a Matlab function poly least squares.m that implements the least squares method we discussed in class to approximate a data set in terms of a polynomial model of degree M. The function should be in the form
function [a,err] = poly | least | squares(x,y,M) | |||
Input: | |||||
x: vector of nodes x=[x(1) … x(N)] | |||||
y: vector of data points y=[y(1) … y(N)] corresponding to [x(1) … | x(N)] | ||||
M: degree of the polynomial model | |||||
(x) = a(1) + a(2)x + a(3)x2 + + a(M+1)xM | (1) |
Output:
a: vector of coe cients representing the polynomial (1)
err: Error between the model and the data in the 2-norm
N | |
Xi | |
err =[yi(xi)]2 : | (2) |
=1 |
Exercise 2 Use the function you coded in Exercise 1 to determine the least squares polyno-mial approximant of the attached nancial data set Dow Jones 2012 2017.dat (normalized closing price of the Dow Jones index from 2-14-2012 to 2-13-2017). To this end, write a Matlab function test least squares finance.m that plots in the same gure the data points fxi; yigi=1;:::;n (in blue) and the least-squares polynomial model (1) for M = 1; 2; 4; 8. Assume that the domain of the polynomial is the same as domain of the data points, i.e., plot the polynomial model in the interval [0; 1].
1
Exercise 3 Show that the least squares polynomial approximant can be an interpolant. To this end, apply the function you coded in Exercise 1 to the following data set
xj = 1 + 2 | j | ; | yj = | 1 | ;j = 0; :::; 15: | (3) | ||
15 | 2 + sin | 20xj2 |
Set M=15 in (1), and write a Matlab function test least squares interp.m that returns a plot the data points (3) (blue circles) and a plot of the polynomial model (1) (red line) in the same gure.