Mathematica如何创建插值函数对象?
Mathematica如何创建插值函数对象?例如:Mathematica如何创建插值函数对象?,math,wolfram-mathematica,interpolation,Math,Wolfram Mathematica,Interpolation,Mathematica如何创建插值函数对象?例如: test1 = FunctionInterpolation[Sin[x],{x,0,2*Pi}] test1的完整形式很长,但主要是x值,带有 对应的y值。但是,插值不是线性的 (因为我没有设置插值顺序->1) 我知道Mathematica使用三次样条曲线,部分原因是默认的 插值顺序为3,但也因为: Plot[D[test1[t],t,t,t,t] /. t->x, {x,0,2*Pi}] 表示四阶导数一致为0 那么,Mathemat
test1 = FunctionInterpolation[Sin[x],{x,0,2*Pi}]
test1的完整形式很长,但主要是x值,带有
对应的y值。但是,插值不是线性的
(因为我没有设置插值顺序->1)
我知道Mathematica使用三次样条曲线,部分原因是默认的
插值顺序为3,但也因为:
Plot[D[test1[t],t,t,t,t] /. t->x, {x,0,2*Pi}]
表示四阶导数一致为0
那么,Mathematica如何计算这个三次样条曲线呢
我的目标是在Perl中使用FunctionInterpolation对象
编辑:谢谢你,萨沙!那正是我想要的,有一个辅修
差错下面是我在一个例子中重新实现Hermite插值的尝试
易于转换为Perl的方式(也可在
)
问题是:最后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}]
(*赫敏(非赫敏)多项式*)
h00[t_]=(1+2*t)*(1-t)^2
h10[t_]=t*(1-t)^2
h01[t_u3;]=t^2*(3-2*t)
h11[t_]=t^2*(t-1)
(*
这证实了我对插值函数的理解
以一种不同的、对Perl友好的方式创建值;这可能不是
适用于所有插值函数,仅适用于我在这里使用的插值函数。
f=插值函数,t=要计算的值
*)
AltinTuncalc[f_uu,t_u]:=模块[
{xvals,yvals,xint,tisin,tpos,m0,m1,p0,p1},
(*算出x值*)
xVAL=展平[f[[3]];
(*和相应的y值*)
yvals=变平[f[[4,3]];
(*和每个x间隔的大小;有许多其他方法可以做到这一点*)
(*几乎所有这些都比这个好?*)
xint=(xVAL[[1]]-xVAL[[1]])/(长度[xVAL]-1);
(*为了提高效率,应缓存高于此点的所有变量*)
(*哪个区间是t英寸?;区间i=x[[i]],x[[i+1]]*)
tisin=Min[Max[Ceiling[(t-xvals[[1]])/xint],1],长度[xvals]-1];
(*和该间隔的y值,使用Hermite约定*)
p0=yvals[[tisin]];
p1=yvals[[tisin+1]];
(*t在此间隔内的位置是什么?*)
tpos=(t-xvals[[tisin]])/xint;
(*在此之前/之后间隔的斜率是多少?*)
(*我们假设区间长度为1,因此不除以int*)
m0=p0 yvals[[tisin-1]];
m1=yvals[[tisin+2]]-p1;
(*返回Hermite近似值*)
(*写wp文章的人想到的是w00t*)
h00[tpos]*p0+h10[tpos]*m0+h01[tpos]*p1+h11[tpos]*m1
]
(*测试用例*)
f1=函数插值[Sin[x],{x,0,2*Pi}]
f2=函数插值[x^2,{x,0,10}]
f3=函数插值[Exp[x],{x,0,10}]
图[{altintfuncalc[f1,t]-f1[t]},{t,0,2*Pi}]
图[{altintfuncalc[f2,t]-f2[t]},{t,0,10}]
图[{altintfuncalc[f3,t]-f3[t]},{t,0,10}]
通常它使用分段。不过,我不确定节点的选择。他们似乎是在整个时间间隔内被统一选择的。我确信在假设平滑函数的情况下,对于要求精度的区间下限会有结果,但我没有详细信息。可能是默认使用了Hermite插值(而不是样条曲线),至少插值[]就是这样做的