n = length(x)
m = length(xh)
yh = zeros(1,m)
c1 = ones(n-1,1)
c2 = ones(1,m)
for i=1:n
xp = x([1:i-1 i+1:n])
yh = yh + y(i)*prod((c1*xh-xp'*c2)./(x(i)-xp'*c2))
end
注:该程序只可一次计算实现一个插值计算。可实现多个插值计算的程序如下:
function yh=lagrange(x,y,xh)
n = length(x)
m = length(xh)
x = x(:)
y = y(:)
xh = xh(:)
yh = zeros(m,1)
c1 = ones(1,n-1)
c2 = ones(m,1)
for i=1:n,
xp = x([1:i-1 i+1:n])
yh = yh + y(i) * prod((xh*c1-c2*xp')./(c2*(x(i)*c1-xp')),2)
end
1、给出一列数据之后,作图如下:aa= randn(100,1)plot(aa)。
2、然后在做好的图中找到tools--basic fitting,打开如下对话框。
3、在打开的对话框中有多种数据插值方法,并可以给出插值的公式。使用cubic方法:于是可以看到插值后的曲线和插值公式。
4、一维插值相当于给出了xy的公式,比如我们上述命令中,aa的值为y,而aa中对应值的位置就是x。
5、还可以使用其他命令来进行数据插值。
6、matlab的interp1中还有nearest,next,previous,cubic等插值方法。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)