Asked by salim tasan
on 13 Apr 2019

The Question:

following points can be expressed with an 8th-order polynomial. Determine the coefficients of the polynomial by using the least squares method and y values

for x = 3.25 and x = 1.85. Plot the polynomial.

x = [1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0];

y = [0.2919 0.0050 0.1732 0.6418 0.9801 0.8770 0.4272 0.0444 0.0805];

My approach is below. I do not know if I did understand the question very well, so I am asking for a guide here. if it is correct

x = [1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5 5.0];

y = [0.2919 0.0050 0.1732 0.6418 0.9801 0.8770 0.4272 0.0444 0.0805];

fit = polyfit(x,y,8);

x_fit = 0:1.0:5.0;

y_fit = polyval(fit, x_fit);

figure;

plot(x,y);

hold on;

plot(x_fit, y_fit);

grid on;

xlabel('x');

ylabel('y');

legend('Data', 'Polyfit', 'Location', 'best');

title(sprintf('x = 3.25 y is %.2f & x = 1.85 y is %.2f',polyval(fit,3.25),polyval(fit,1.85)))

Answer by John D'Errico
on 13 Apr 2019

Accepted Answer

Sort of correct. But I see at least two subtle problems.

First, you have 9 data points. So a polynomial with as many coefficients as you have data points will be an interpolating polynomial. This is where you need to be careful though, as some people use the words order and degree to be synonyms. In fact, this used to be the case, that they were synonyms. But today it is common that the order of a polynomial is the number of coefficients, whereas the degree is the highest power of the independent variable. That means we would have

degree + 1 = order

So by order 8, that would tend to imply a polynomial of degree 7 (thus the highest power of x would be 7.) As such, it would be a least squares fit, not an interpolating polynomial on 9 data points (thus one more data point than you would have coefficients to fit.) And that is what you get by use of polyfit as you have done. So your use of polyfit was correct, if you intended to fit a polynomial of DEGREE 7.

For example, polyfit(x,y,1) will produce a polynomial of degree 1, with two coefficients, thus order 2. The model would be y=a!*x+a2.

The important issue is how does your teacher use the word order? What do they intend on this question?

The second question is how you generated xfit. We see this:

x_fit = 0:1.0:5.0;

y_fit = polyval(fit, x_fit);

How many points does the vector xfit have? Only 6 of them. In fact, it has fewer points than your original data, and they are more sparse. So when you use plot to plot a curve, you want to use LOTS of points, to fill in the curve. Instead, you plotted only a few points, and plot will connect them with straight lines. That is a bad idea. As well, you went down to 0, when your data only went down to 1 in x. So you are using polyval to extrapolate the polynomial. And extrapolating high order polynomials is a REALLY bad idea.

Instead, you want to do something like this:

x_fit = linspace(1,5,100);

y_fit = polyval(fit, x_fit);

figure;

plot(x,y);

hold on;

plot(x_fit, y_fit);

grid on;

xlabel('x');

ylabel('y');

legend('Data', 'Polyfit', 'Location', 'best');

title(sprintf('x = 3.25 y is %.2f & x = 1.85 y is %.2f',polyval(fit,3.25),polyval(fit,1.85)))

Another possible problem, is if you will use the curve fitting toolbox ever, then it is a bad idea to define a variable named fit, as that will make it difficult to use the FUNCTION named fit from the curve fitting toolbox.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 1 Comment

## Star Strider (view profile)

## Direct link to this comment

https://de.mathworks.com/matlabcentral/answers/456134-coefficients-of-the-polynomial-the-least-squares-method#comment_693589

Sign in to comment.