関数の値の補間方法

学生向け

「関数の値を補間するプログラムで遊ばせたら、補間のアルゴリズムが複雑だったせいか、伝えたかったことがまったく伝わらなかった」という話を聞いたので、ちょっとやってみました。

Manipulate[
 f = Function[{x}, 1 + Sin[a x]];
 start = -Pi;
 end = Pi;
 
 (*サンプリング*)
 samples = Table[{x, f[x]}, {x, start, end, 2 (end - start)/n}];
 
 (*補間*)
 g = Interpolation@samples;
 
 (*描画*)
 plot1 = Plot[f[ x], {x, start, end}, PlotLabel -> "Original"];
 plot2 = ListPlot[samples, PlotLabel -> "Samples"];
 plot3 = Plot[g[x], {x, start, end}, PlotLabel -> "Interpolating Function"];
 GraphicsGrid[{{plot1}, {plot2}, {plot3}}],
 {a, 1, 32, 1},
 {{n, 32}, 1, 64, 1}]

CDF Playerがインストールされていれば実際に動かして試せます。


上から、オリジナルの関数・サンプリングデータ・補間結果です。

要は「サンプルをたくさん取ればいいってもんじゃないよ」という話で、標本化定理とかにつなげたかったのでしょう。そういう場合はMathematicaでやるのが簡単です。「補間」自体について学ばせたいときは、Javaのような低レベルな言語を使ってもいいでしょう。