Starting from:
$30

$24

Homework 4 Solution

Please submit to CANVAS a .zip le that includes the following Matlab functions:




poly least squares.m







test least squares.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 of 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
(xi)]2 :


err =[yi
(2)
=1










Exercise 2 Use the function you coded in Exercise 1 to determine the least squares polynomial ap-proximants of the attached data set AMD data 2013 2018.dat (daily closing prices of the Advanced Micro Devices Inc. stock from from 2/19/13 to 2/19/18). To this end, write a Matlab/Octave function test least squares.m of the form







function [x,p1,p2,p4,p8] = test least squares()







Output:




x: Vector of 1000 evenly-spaced evaluation nodes in [0; 1] (including endpoints), i.e., x(j) = (j-1)/999 with j=1,...,1000.




1






p1, p2, p4, p8: Least squares polynomial models (1) of the AMD stock price with degrees




= 1; 2; 4; 8, respectively, evaluated at x.



The function should also return one gure with the plots of the data points fxi; yigi=1;2;::: in the le (in blue) and the least-squares polynomial models p1, p2, p4 and p8 you computed above (in red).




Hint: To load the AMD data in Matlab/Octave use the command load() (see the Matlab/Octave documentation for further details).




























































More products