反转插值以给出与所需插值函数值关联的变量 [英] Invert interpolation to give the variable associated with a desired interpolation function value

查看:61
本文介绍了反转插值以给出与所需插值函数值关联的变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用scipy的插值函数反转插值函数.假设我创建了一个内插函数,

I am trying to invert an interpolated function using scipy's interpolate function. Let's say I create an interpolated function,

import scipy.interpolate as interpolate
interpolatedfunction = interpolated.interp1d(xvariable,data,kind='cubic')

当我指定a时,是否存在一些可以找到x的函数

Is there some function that can find x when I specify a:

interpolatedfunction(x) == a

换句话说,我希望内插函数等于a;使我的函数等于a的xvariable的值是多少?"

In other words, "I want my interpolated function to equal a; what is the value of xvariable such that my function is equal to a?"

我很高兴可以使用某些数字方案来实现此目的,但是有没有更简单的方法?如果插值函数在xvariable中是多值的怎么办?

I appreciate I can do this with some numerical scheme, but is there a more straightforward method? What if the interpolated function is multivalued in xvariable?

推荐答案

创建内插函数interp_fn后,您可以通过函数根查找x其中interp_fn(x) == a的值

After creating an interpolated function interp_fn, you can find the value of x where interp_fn(x) == a by the roots of the function

interp_fn2 = lambda x: interp_fn(x) - a

有许多选项可以找到scipy.optimize中的根.例如,要使用初始值从10开始的牛顿方法:

There are number of options to find the roots in scipy.optimize. For instance, to use Newton's method with the initial value starting at 10:

from scipy import optimize

optimize.newton(interp_fn2, 10)


实际示例

创建一个内插函数,然后找到fn(x) == 5


Actual example

Create an interpolated function and then find the roots where fn(x) == 5

import numpy as np
from scipy import interpolate, optimize

x = np.arange(10)
y = 1 + 6*np.arange(10) - np.arange(10)**2
y2 = 5*np.ones_like(x)
plt.scatter(x,y)
plt.plot(x,y)
plt.plot(x,y2,'k-')
plt.show()

# create the interpolated function, and then the offset
# function used to find the roots

interp_fn = interpolate.interp1d(x, y, 'quadratic')
interp_fn2 = lambda x: interp_fn(x)-5

# to find the roots, we need to supply a starting value
# because there are more than 1 root in our range, we need 
# to supply multiple starting values.  They should be 
# fairly close to the actual root

root1, root2 = optimize.newton(interp_fn2, 1), optimize.newton(interp_fn2, 5)

root1, root2
# returns:
(0.76393202250021064, 5.2360679774997898)

这篇关于反转插值以给出与所需插值函数值关联的变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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