Mathematica如何创建一个InterpolatingFunction对象?

问题描述:

Mathematica如何创建一个InterpolatingFunction对象?例如:Mathematica如何创建一个InterpolatingFunction对象?

test1 = FunctionInterpolation[Sin[x],{x,0,2*Pi}] 

TEST1的FullForm很长,但是与 对应的y值主要x值。然而,插值是非线性 (因为我没有设置InterpolationOrder - > 1)

我知道Mathematica使用三次样条,部分原因是默认 InterpolationOrder是3,也因为:

Plot[D[test1[t],t,t,t,t] /. t->x, {x,0,2*Pi}] 

显示四阶导数是一致的0.

那么,Mathematica如何计算这个三次样条?

我的目标是在Perl中使用FunctionInterpolation对象。

编辑:谢谢你,萨沙!这正是我想要的,有一个小小的 小故障。以下是我尝试以易于转换为Perl的 方式重新实现Hermite插值(也可在 https://github.com/barrycarter/bcapps/blob/master/bc-approx-sun-ra-dec.m#L234处获得)。

问题:最后3个图有很小但非零的值。我不能 告诉我是否实施了Hermite错误,或者这只是一个数字 小故障。

(* the Hermite <h>(not Hermione)</h> polynomials *) 

h00[t_] = (1+2*t)*(1-t)^2 
h10[t_] = t*(1-t)^2 
h01[t_] = t^2*(3-2*t) 
h11[t_] = t^2*(t-1) 

(* 

This confirms my understanding of InterpolatingFunction by calculating 
the value in a different, Perl-friendly, way; this probably does NOT 
work for all InterpolatingFunction's, just the ones I'm using here. 

f = interpolating function, t = value to evaluate at 

*) 

altintfuncalc[f_, t_] := Module[ 
{xvals, yvals, xint, tisin, tpos, m0, m1, p0, p1}, 

(* figure out x values *) 
xvals = Flatten[f[[3]]]; 

(* and corresponding y values *) 
yvals = Flatten[f[[4,3]]]; 

(* and size of each x interval; there are many other ways to do this *) 
(* <h>almost all of which are better than this?</h> *) 
xint = (xvals[[-1]]-xvals[[1]])/(Length[xvals]-1); 

(* for efficiency, all vars above this point should be cached *) 

(* which interval is t in?; interval i = x[[i]],x[[i+1]] *) 
tisin = Min[Max[Ceiling[(t-xvals[[1]])/xint],1],Length[xvals]-1]; 

(* and the y values for this interval, using Hermite convention *) 
p0 = yvals[[tisin]]; 
p1 = yvals[[tisin+1]]; 

(* what is t's position in this interval? *) 
tpos = (t-xvals[[tisin]])/xint; 

(* what are the slopes for the intervals immediately before/after this one? *) 
(* we are assuming interval length of 1, so we do NOT divide by int *) 
m0 = p0-yvals[[tisin-1]]; 
m1 = yvals[[tisin+2]]-p1; 

(* return the Hermite approximation *) 
(* <h>Whoever wrote the wp article was thinking of w00t</h> *) 
h00[tpos]*p0 + h10[tpos]*m0 + h01[tpos]*p1 + h11[tpos]*m1 
] 

(* test cases *) 

f1 = FunctionInterpolation[Sin[x],{x,0,2*Pi}] 
f2 = FunctionInterpolation[x^2,{x,0,10}] 
f3 = FunctionInterpolation[Exp[x],{x,0,10}] 

Plot[{altintfuncalc[f1,t] - f1[t]},{t,0,2*Pi}] 
Plot[{altintfuncalc[f2,t] - f2[t]},{t,0,10}] 
Plot[{altintfuncalc[f3,t] - f3[t]},{t,0,10}] 
+1

它可能使用Hermite插值作为默认值(而不是Splines),至少这是Interpolation []的作用 – 2011-04-16 14:02:26

通常它使用分段Hermite cubic interpolation。不过,我不确定节点的选择。看起来他们是在整个区间内统一选择的。我相信在假设函数平滑的情况下,对于所请求的精度的间隔的下界有结果,但我没有具体的细节。