Polyfit和Polyval执行插值 [英] Polyfit and polyval to perform interpolation

查看:151
本文介绍了Polyfit和Polyval执行插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

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

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

我想用一个10阶的多项式来估计它,以使该多项式在11个点处与图相交.

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

所以,我这样做了:

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,'...')

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,'...')

我认为polyfit将为我提供多项式的系数,该系数最多等于10阶,该多项式与点(x2,y2)相交(即11点) 那么y3本质上只是10阶多项式所处位置的y值,因此将它们全部绘制会得到10阶多项式,并将我的原始图与11个唯一点相交?

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?

我做错了什么?

我的结果:

推荐答案

您的计算正确,但是您没有以正确的方式绘制函数.生成的图中的蓝线是分段线性的.那是因为您仅在插值点x2上评估多项式p.然后,plot命令在这些点之间绘制线段,并向您显示意外的图形. 要获得预期的结果,您只需要像下面这样更密集地评估多项式即可:

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

这篇关于Polyfit和Polyval执行插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆