Solved

Integral value

Posted on 2011-09-08
5
279 Views
Last Modified: 2012-05-12
Hi

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

Issac
0
Comment
Question by:IssacJones
  • 3
  • 2
5 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
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.
0
 
LVL 37

Expert Comment

by:TommySzalapski
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.
0
 

Author Comment

by:IssacJones
ID: 36504835
Hi Tommy

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

Issac
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 250 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.
0
 

Author Closing Comment

by:IssacJones
ID: 36506326
Thanks Tommy
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Whisker Plot 1 38
Python error - Need Help 12 82
Downgrade Python 3.5 to 3.3 30 326
how to split multiple lines delimiter : 8 54
How to Win a Jar of Candy Corn: A Scientific Approach! I love mathematics. If you love mathematics also, you may enjoy this tip on how to use math to win your own jar of candy corn and to impress your friends. As I said, I love math, but I gu…
Dictionaries contain key:value pairs. Which means a collection of tuples with an attribute name and an assigned value to it. The semicolon present in between each key and values and attribute with values are delimited with a comma.  In python we can…
Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
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 …

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now