如何在python中将生成的函数返回到数学表达式的形式并对其进行求导等操作?

发布于 01-15 20:13 字数 1428 浏览 3 评论 0原文

使用scipy中的curve_fitting后,对散点数据进行高斯近似拟合,代码如下:

x = np.linspace(1,len(y),len(y)) 
n = len(x)
mean = sum(x*y)/n
sigma = np.sqrt(sum(y*(x-mean)**2)/n)

def gaus(x,a,x0,sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))/(sigma*np.sqrt(2*np.pi))
popt,pcov = curve_fit(gaus,x,y,maxfev = 200000) 

当我调用它时,生成的p1只是一个与x对应的数组:

p1 = gaus(x,*popt) < /代码> ,返回的数组为:

[0.09933219 0.10139629 0.10350315 0.10565368 0.10784877 0.11008935
 0.11237635 0.11471073 0.11709347 0.11952557 0.12200806 0.12454196
 0.12712835 0.1297683  0.13246293 0.13521337 0.13802076 0.14088628
 0.14381113 0.14679655 0.14984377 0.15295407 0.15612876 0.15936917
 0.16267665 0.16605259 0.1694984  0.17301552 0.17660543 0.18026962
 0.18400963 0.18782703 0.19172341 0.19570039 0.19975966 0.20390289
 0.20813183 0.21244823 0.21685392 0.22135072 0.22594052 0.23062523
 0.23540682 0.24028728 0.24526864 0.250353   0.25554246 0.26083921
 0.26624545 0.27176344 0.27739549 0.28314393 0.28901118 0.29499968
 0.30111193 0.30735049 0.31371794 0.32021696 0.32685025 0.33362057
 0.34053076 0.34758369 0.3547823  0.36212959 0.36962863 0.37728255
 0.38509452 0.39306781 0.40120574 0.4095117  0.41798914 0.42664161
 0.4354727  0.4444861  0.45368554 0.46307487 0.472658   0.4824389
 0.49242166 0.50261042 0.51300944 0.52362302 0.53445559 0.54551166
 0.55679582]  

这种情况下,如何求出生成函数的一阶导数表达式、二阶导数等?

After using curve_fitting from scipy,the scatter data is fitted by Gaussian approximation,the code is as follows:

x = np.linspace(1,len(y),len(y)) 
n = len(x)
mean = sum(x*y)/n
sigma = np.sqrt(sum(y*(x-mean)**2)/n)

def gaus(x,a,x0,sigma):
    return a*np.exp(-(x-x0)**2/(2*sigma**2))/(sigma*np.sqrt(2*np.pi))
popt,pcov = curve_fit(gaus,x,y,maxfev = 200000) 

When I call it, the generated p1 is just an array corresponding to x:

p1 = gaus(x,*popt)
,The returned array is:

[0.09933219 0.10139629 0.10350315 0.10565368 0.10784877 0.11008935
 0.11237635 0.11471073 0.11709347 0.11952557 0.12200806 0.12454196
 0.12712835 0.1297683  0.13246293 0.13521337 0.13802076 0.14088628
 0.14381113 0.14679655 0.14984377 0.15295407 0.15612876 0.15936917
 0.16267665 0.16605259 0.1694984  0.17301552 0.17660543 0.18026962
 0.18400963 0.18782703 0.19172341 0.19570039 0.19975966 0.20390289
 0.20813183 0.21244823 0.21685392 0.22135072 0.22594052 0.23062523
 0.23540682 0.24028728 0.24526864 0.250353   0.25554246 0.26083921
 0.26624545 0.27176344 0.27739549 0.28314393 0.28901118 0.29499968
 0.30111193 0.30735049 0.31371794 0.32021696 0.32685025 0.33362057
 0.34053076 0.34758369 0.3547823  0.36212959 0.36962863 0.37728255
 0.38509452 0.39306781 0.40120574 0.4095117  0.41798914 0.42664161
 0.4354727  0.4444861  0.45368554 0.46307487 0.472658   0.4824389
 0.49242166 0.50261042 0.51300944 0.52362302 0.53445559 0.54551166
 0.55679582]  

In this case, how can I find it's first derivative expression, the second derivative and so on for the generated function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情废物2025-01-22 20:13:57

这可以使用 scipy.interpolate.InterpolatedUnivariateSpline 来实现。

首先,您需要创建数据的样条线,如下所示:

from scipy.interpolate import InterpolatedUnivariateSpline

spl = InterpolatedUnivariateSpline(x, p1)

然后,您可以使用 spl 对象传递 xn (数字的导数),在 x 处获得一个新的样条线作为 np.ndarray ,其第 n 个导数为:

der1 = spl(x, 1)
der2 = spl(x, 2)

This can be achieved using scipy.interpolate.InterpolatedUnivariateSpline.

First, you need to create a spline of your data as:

from scipy.interpolate import InterpolatedUnivariateSpline

spl = InterpolatedUnivariateSpline(x, p1)

Afterward, you can use the spl object to pass x and n (the number of derivative), to get a new spline as np.ndarray at x with its nth derivative as:

der1 = spl(x, 1)
der2 = spl(x, 2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文