开发者

Python odeint usage

Can someone tell me how to put in the parameters for the odeint function for the pyth开发者_StackOverflow中文版on to get the integral.

I am trying to get the integral of e^x from 0 to 2 but I am not sure how to put in the parameters. The documentation isnt all that clear to me.

Thanks


Here's an example that assumes y(0) = 1.

import scipy
import scipy.integrate


def integrateExp(y0, a, b):
  limits = [a, b]
  integral = scipy.integrate.odeint(lambda y, t : scipy.exp(t), y0, limits)
  return integral[1]

print integrateExp(1, 0, 2)

The first argument should take (y,t) and return the corresponding derivative. I used a lambda here since d/dt exp(t) is trivial.


If you want just to calculate integrals, rather than to solve differential equations, you can also use

from numpy import exp
from scipy.integrate import quad

def f(x):
    return exp(x)

result, error = quad(f, 0, 2)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜