0

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

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Hello, Would it be possible for you to tell me how I can use this within a function. By that I mean can I define a function like def calc(x, a, b): and paste that code in there? I cant figure out how to place the function within the odeint if a function is specified in the parameters. It says x isnt declared
Hi thanks. I have been playing with the same thing but i cant figure out how to put in other functions.. like x**2 from 0-1.. please check out this thread stackoverflow.com/questions/5372321/…
Hi Kartik, you need to calculate the derivative yourself, either analytically or numerically.
1

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) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.