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

Practice with the commands polyfit and polyval. Write a function in MATLAB calle

ID: 3726202 • Letter: P

Question

Practice with the commands polyfit and polyval.

Write a function in MATLAB called MYCURVEFIT that determines the y value of a best fit polynomial equation based on a single input, an x value. Predict the y value by fitting a 3rd order polynominal y=f(x) to the data points shown below. The input will be a single x value in the range of 0 to 20. The output should be a single y value obtained by evaluating the best fit third order polynomial at x.

X=0:0.5:20

Y=[14,16,18,22,28,35,50,68,90,117,154,200,248,309,378,455,550,654,770,900,1044,1200,1378,1560,...
1778,2004,2250,2500,2800,3100,3434,3786,4158,4555,4978,5424,5900,6401,6930,7474,8074]

You need to copy and paste the data above into your function

Explanation / Answer

mycurvefit.m file

function y = mycurvefit(x)
%initially given data
y1=[14,16,18,22,28,35,50,68,90,117,154,200,248,309,378,455,550,654,770,900,1044,1200,1378,1560,1778,2004,2250,2500,2800,3100,3434,3786,4158,4555,4978,5424,5900,6401,6930,7474,8074];
%give n=3 for third order polynomial
n = 3;
%using polyfit for fitting the function value
p = polyfit(x,y1,n);
%returning the y value
y = p(3);
end

test.m file

%Initializing the x value to be between 0 to 20
x=0:0.5:20;
%Calling the mycurvefit function
y = mycurvefit(x);
%printing the results to the screen
fprintf('The y value by fitting a 3rd order polynominal y=f(x) is %.3f',y);

Output:

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote