Polyfit and polyval to perform interpolation

怎甘沉沦 提交于 2019-12-12 05:49:52

问题


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:


回答1:


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);



回答2:


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



来源:https://stackoverflow.com/questions/30075007/polyfit-and-polyval-to-perform-interpolation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!