Integral value

Posted on 2011-09-08
Last Modified: 2012-05-12

I am using Python but I hope somebody in this area will be able to help me.

I have generated some x and y(x) results. These are stored in Python lists. I am able to plot this data using Matplotlib.

What I want to do now is to get the integral between x = a and x = b. I have looked at the simps method in the scipy module but that looks like it will only give the integral for the values I supply for x and y. This is not what I want. What I need is to supply the data points I know and then supply x = a and x = b (which in general won't be the extreme values I have supplied) and then calculate the integral.

For example, suppose I have supplied data x = 0, 1, 2, 3, 4 and 5, and I calculated the corresponding y(x) values. What I would like to do then is calculate the integral beween, for example, x = 1.5 and x = 2.7.

Can anybody help? There must be a way to do this in Python using one of the standard modules e.g. scipy?

Thanks in advance

Question by:IssacJones
  • 3
  • 2
LVL 37

Expert Comment

ID: 36504666
Are you approximating this as a function or just the points? If just the points, then I take it you assume the graph has a straight line between points.

In that case y(2.7) = y(2)*.3 + y(3)*.7
y(1.5) = y(1)*.5 + y(2)*.5
And the integral between y(d) and y(e) is just (y(d) + y(e))*(e-d)/2
So you would just do that for each piece and sum them.
LVL 37

Expert Comment

ID: 36504687
So, in your example, get y(1.5) to y(2) and y(2) to y(2.7)
(y(1.5) + y(2))*.5/2 + (y(2) + y(2.7))*.7/2

Of course you would do this in some kind of a loop.

Author Comment

ID: 36504835
Hi Tommy

Thanks for the suggestion but I would prefer to use an inbuilt Python module to do the work.

LVL 37

Accepted Solution

TommySzalapski earned 250 total points
ID: 36505658
From section 6.2 here
You can use interpolate.splrep to create a spline (set s=0 if you don't want it to smooth the curve)
Then you can use interpolate.splint to get the integral.
So it should look something like
splineCurve = interpolate.splrep(xvals,yvals,s=0)
integralValue = interpolate.splint(a, b, splineCurve)

I can't test it (don't have Python), but my knowledge of Math and C++ makes me think that will work quite nicely for you.

Author Closing Comment

ID: 36506326
Thanks Tommy

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we…
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

680 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question