Solved

Integral value

Posted on 2011-09-08
5
298 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
[X]
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
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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

This article seeks to propel the full implementation of geothermal power plants in Mexico as a renewable energy source.
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 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…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

717 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