Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Matlab code should be included in your work. You should be able to explain your

ID: 3603820 • Letter: M

Question

Matlab code should be included in your work. You should be able to explain your code. 1. (6 points) Given the data points as follows x -[4.0 4.2 4.5 4.7 5.1 5.55.9 6.3 6.8 7.1] y =[102.56 113.18 130.11 142.05 167.53 195.14 224.87 256.73 299.50 326.72] a. Construct the least squares polynomial of degree 1, pi(x) b. Construct the least squares polynomial of degree 2, p2(x) c. Construct the least squares polynomial of degree 3, ps(x) d. Construct the least squares approximation of the form be"X, pe(x) Plot all the four least squares approximation functions as well as the data points in ONE figure.

Explanation / Answer

the required code is as below :

-----------------------------------------------------------------------------------------------------

% given data set
x = [ 4.0 4.2 4.5 4.7 5.1 5.5 5.9 6.3 6.8 7.1 ]

y = [102.56 113.18 130.11 142.05 167.53 195.14 224.87 256.73 299.50 326.72]

% holds the fit for degree 1

p1_x = polyfit(x,y,1)

% holds the fit for degree 2
p2_x = polyfit(x,y,2)

% holds the fit for degree 3
p3_x = polyfit(x,y,3)


% plotting the data
plot(x,y)
% holding the plottd graph
hold on

% plotting the ploynomial of degree 1
plot(x,p1_x(1)*x + p1_x(2))
% plotting the ploynomial of degree 2
plot(x,p2_x(1)*x.^2+p2_x(2)*x+p2_x(3))
% plotting the ploynomial of degree 3
plot(x,p3_x(1)*x.^3+p3_x(2)*x.^2+p3_x(3)*x+p3_x(4))

--------------------------------------------------------------------------------------------

/* hope this helps */

/* if any queries please comment */

/* thank you */