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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Suggested Solutions

Title # Comments Views Activity
How do I count sequences in Python with itertools groupby? 3 119
phython 2 60
python - find anything after $ question. 9 81
suitable program for data manipulation 7 62
Foreword (May 2015) This web page has appeared at Google.  It's definitely worth considering! How to Know You are Making a Difference at EE In August, 2013, one …
Sequence is something that used to store data in it in very simple words. Let us just create a list first. To create a list first of all we need to give a name to our list which I have taken as “COURSE” followed by equals sign and finally enclosed …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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