避免重复调用插值 [英] Avoid repeated calls to Interpolation

查看:57
本文介绍了避免重复调用插值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在mathematica中内插一个函数.

I want to interpolate a function in mathematica.

该函数取决于参数a,实际上它是函数F的逆函数,该函数也取决于a,因此我按以下方法构建近似值

The function depends on a parameter a, in fact it is the inverse of a function F which also depends on a, so I build my approximation as follows,

approx = Interpolation[Table[{F[0.1 n, a], 0.1 n}, {n, -100, 100}]]

现在我可以简单地调用approx[x]来评估一点的逆函数.

now I can simply call approx[x] to evaluate the inverse function at a point.

相反,我想做这样的事情:定义一个带有参数的函数,

Instead I would like to do something like this: Define a function which takes a parameter,

G[x_,a_] = "construct the interpolating function,
            and return the value of the function at x"

然后写G [x,a]评估函数.否则,我将不得不对所有我感兴趣的参数重复插值,并且周围会有很多变量.我尝试将Interpolation []调用放入模块中,但是每次我调用G [x,a]时,它只会构造插值!我将如何避免这种情况?

Then write G[x,a] to evaluate the function. Otherwise I would have to repeat the interpolation for all the parameters I am interested in and have lots of variables lying around. I have tried putting the Interpolation[] call inside a module but that just constructs the interpolation every time I call G[x,a]! How would I avoid this?

感谢您的阅读.

推荐答案

尝试以下方法:

G[a_]:=G[a]=Interpolation[Table[{F[0.1 n, a], 0.1 n}, {n, -100, 100}]]

G[0.2]  (* particular value of G[a] *)

G[0.2][0.3] (* the value you want *)

您只会在第一次为a的每个特定值调用G时对其求值.

You will only evaluate G the first time you call it for each particular value of a.

这篇关于避免重复调用插值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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