Fitting a curve to 5th degree polynomial

I need to fit a curve to 5th degree polynomial. I am able to do the same using Basic fitting tool. But I need to write the code for the same. I looked 'polyfit' and obtained the coefficients but I need a code to plot the curve obtained after fitting.

Answers (1)

pp = polyfit(x, y, 5);
plot(x, polyval(pp, x))

2 Comments

I think one will have to choose a finer grid than the x-vector in order to see how the polynomial behaves between the given interpolation points.
Maybe
pp = polyfit(x, y, 5);
xx=linspace(x(1),x(end),10*length(x));
figure
plot(x,y,'o')
hold on
plot(xx,polyval(pp,xx))
hold off
Best wishes
Torsten.
a 5th degree polynomial is going to be smooth over the range of the provided x values, so refining before plotting is not going to show anything of interest, likely. The difficulties are going to come past the ends of the defined range; interpolation with polynomials becomes increasingly problematic as the degree of the polynomial increases.

Sign in to comment.

Categories

Asked:

on 29 Jul 2015

Commented:

on 29 Jul 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!