0

I have

x = linspace(-5,5,256) y = 1./(1+x.^2) plot(x,y,'...') %plot of (x,y)

I want to estimate this with a polynomial of order 10, such that the polynomial intersects the graph at 11 points.

So, I did this:

x2 = linspace(-5,5,11) y2 = 1./(1+x2.^2) p = polyfit(x2,y2,10) %finds coefficients of polynomial of degree 10 that fits x2,y2 y3 = polyval(p,x2) plot(x,y,x2,y3,'...')

I thought the polyfit would give me the coefficients for a polynomial up to order 10, which intersects the points (x2,y2) (i.e 11 points) then y3 is essentially just the y values of where the 10th order polynomial lands, so plotting them altogether would give me the 10th order polynomial, intersecting my original graph at 11 unique points?

What have I done wrong?

My result: enter image description here

elbarto
  • 211
  • 3
  • 15
  • 6
    You must evaluate `p` not only at the interpolation points `x2`, but instead more densely: `x3 = linspace(-5,-5,500); y3 = polyval(p,x3); plot(x3,y3);`. As you only evaluate at the interpolation points the plot is output with blue line segments between those evaluations. – knedlsepp May 06 '15 at 11:47
  • While the question and consequently the answer is borderline trivial, just in order to follow protocol ;), @knedlsepp, would you make an answer out of your comment so that elbarto can accept it? – A. Donda May 06 '15 at 15:03
  • @A.Donda: I'm sorry. You are right of course! – knedlsepp May 06 '15 at 15:50

2 Answers2

2

Your computations are correct, but you are not plotting the function the right way. The blue line in your generated plot is piecewise linear. That's because you are only evaluating your polynomial p at the interpolation points x2. The plot command then draws line segments between those points and you are presented with your unexpected plot. To get the expected result you simply have to evaluate your polynomial more densely like so:

x3 = linspace(-5,-5,500);
y3 = polyval(p,x3);
plot(x3,y3);
knedlsepp
  • 6,065
  • 3
  • 20
  • 41
  • I actually just ended up using `y3 = polyval(p,x)` and then plotted `(x,y3)` But yea, this was my underlying error. Thanks kned! – elbarto May 07 '15 at 11:16
0

Consider the points (1,3), (2,6.2) and (3,13.5). Use Matlab's builtin function polyfit to obtain the best parameters for fitting the model P = Poekt to this data