Question: **Create a new function in MATLAB function c = lstsqpoly(x, y, n) which is a modification of the function lstsqline in the way that it

**Create a new function in MATLAB function c = lstsqpoly(x, y, n) which is a modification of the function lstsqline in the way that it generates the least-squares polynomial of degree n. You could use a single for loop within your code to create the design matrix X whose form is defined by the degree of the polynomial. For example, for3n= the design matrix X should have the form:

X=[x1^3, x1^2, x1^1,1: x2^3, x2^2, x2^1, 1: xm^3, xm^2, xm^1, 1] **Type the function lstsqpoly in the diary file. **Run the function c = lstsqpoly(x, y, n) for each n = 1, 2, 3, 4 on the vectors from Exercise 5 x = [0, 2, 3, 5, 6] and y = [4, 3, 2, 1, 0] to find the least-squares polynomial for each of the four values of n. Your output for each n has to contain: the vectors c1 and c2 (see Exercise 5), the 2-norm of the residual vector number N, the least-squares polynomial P in x of degree n, the vector c, and the plot containing both the data points and the graph of the polynomial. % For each n, verify that the vectors of coefficients c, c1, and c2 matches. Write a comment about it. % Verify that your code for this exercise for n = 1 is consistent with the code for Exercise 5, that is, both functions give the same outputs for n = 1. Write a comment about it. % Based on the plots and the norms of the residual vectors, tell polynomial of what degree (1, 2, 3, or 4) fits your data the best. **Save the 4 plots as JPEG files, image1.jpg image4.jpg, and insert them in your word document for Project 4 following the part to which the image is relating.

This is the other function referenced:

function c=lstsqline(x,y) format rat x=x'; y=y'; a=x(1); m=length(x) b=x(m); X=[x,ones(m,1)]; c=lscov(X,y) c1=(inv(X'*X))*(X'*y) c2=(X'*X)\(X'*y) N=norm(y-X*c) plot(x,y,'*'),hold polyplot(a,b,c'); P=poly2sym(c) end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!