Kartik
Kartik

Reputation: 9873

Python odeint usage

Can someone tell me how to put in the parameters for the odeint function for the python 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

Upvotes: 0

Views: 3327

Answers (2)

pv.
pv.

Reputation: 35145

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)

Upvotes: 1

mjbommar
mjbommar

Reputation: 499

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.

Upvotes: 3

Related Questions