Integral value

Posted on 2011-09-08
Medium Priority
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 1000 total points
ID: 36505658
From section 6.2 here http://www.tau.ac.il/~kineret/amit/scipy_tutorial/
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
When we purchase storage, we typically are advertised storage of 500GB, 1TB, 2TB and so on. However, when you actually install it into your computer, your 500GB HDD will actually show up as 465GB. Why? It has to do with the way people and computers…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Suggested Courses
Course of the Month12 days, 21 hours left to enroll

777 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