用scipy拟合微分方程 [英] Fit differential equation with scipy

查看:99
本文介绍了用scipy拟合微分方程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何适应followint scipy教程的微分功能

how can I fit the differential function of the followint scipy tutorial

Scipy微分方程教程?

最后,我想拟合一些数据点,这些数据点遵循一组总共有六个参数的两个微分方程组,但我想从一个简单的示例开始.到目前为止,我尝试了 scipy.optimize.curve_fit scipy.optimize.leastsq 函数,但是我什么都没得到.

In the end I want to fit some datapoints that follow a set of two differential equations with six parameters in total but I'd like to start with an easy example. So far I tried the functions scipy.optimize.curve_fit and scipy.optimize.leastsq but I did not get anywhere.

这就是我走了多远:

import numpy as np
import scipy.optimize as scopt
import scipy.integrate as scint
import scipy.optimize as scopt

def pend(y, t, b, c):
    theta, omega = y
    dydt = [omega, -b*omega - c*np.sin(theta)]
    return dydt


def test_pend(y, t, b, c):
    theta, omega = y
    dydt = [omega, -b*omega - c*np.sin(theta)]
    return dydt

b = 0.25
c = 5.0

y0 = [np.pi - 0.1, 0.0]
guess = [0.5, 4]
t = np.linspace(0, 1, 11)

sol = scint.odeint(pend, y0, t, args=(b, c))

popt, pcov = scopt.curve_fit(test_pend, guess, t, sol)

,并显示以下错误消息:

with the following error message:

ValueError: too many values to unpack (expected 2)

很抱歉,这可能是一个非常简单的问题,但我无法解决.所以先谢谢.

And I'm sorry as this is assumingly a pretty simple question but I don't get it to work. So thanks in advance.

推荐答案

您需要提供函数 f(t,b,c),该函数在中给出了一个参数或参数列表> t 返回参数处的函数值.这需要做一些工作,或者通过确定 t 的类型,或者通过使用一种可以以下两种方式起作用的构造:

You need to provide a function f(t,b,c) that given an argument or a list of arguments in t returns the value of the function at the argument(s). This requires some work, either by determining the type of t or by using a construct that works either way:

def f(t,b,c): 
    tspan = np.hstack([[0],np.hstack([t])])
    return scint.odeint(pend, y0, tspan, args=(b,c))[1:,0]

popt, pcov = scopt.curve_fit(f, t, sol[:,0], p0=guess)

返回 popt = array([0.25,5.]).

可以扩展它以容纳更多参数,

This can be extended to fit even more parameters,

def f(t, a0,a1, b,c): 
    tspan = np.hstack([[0],np.hstack([t])])
    return scint.odeint(pend, [a0,a1], tspan, args=(b,c))[1:,0]

popt, pcov = scopt.curve_fit(f, t, sol[:,0], p0=guess)

这会导致 popt = [3.04159267e + 00,-2.38543640e-07,2.49993362e-01,4.99998795e + 00] .

另一种可能性是显式计算目标解决方案的差异的平方范数,并将最小化应用于如此定义的标量函数.

Another possibility is to explicitly compute the square norm of the differences to the target solution and apply minimization to the so-defined scalar function.

 def f(param): 
     b,c = param
     t_sol = scint.odeint(pend, y0, t, args=(b,c))
     return np.linalg.norm(t_sol[:,0]-sol[:,0]);

res = scopt.minimize(f, np.array(guess))

res

      fun: 1.572327981969186e-08
 hess_inv: array([[ 0.00031325,  0.00033478],
                  [ 0.00033478,  0.00035841]])
      jac: array([ 0.06129361, -0.04859557])
  message: 'Desired error not necessarily achieved due to precision loss.'
     nfev: 518
      nit: 27
     njev: 127
   status: 2
  success: False
        x: array([ 0.24999905,  4.99999884])

这篇关于用scipy拟合微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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