Calculation of the area under the mathematical function

I have a series of data that I approximated using a polynomial of degree 2 in Python. I want to calculate the area under this polynomial between 0 and 1.

Is there a calculus or similar package from numpy that I can use, or just just make a simple function to integrate these functions?

I don't understand a bit what is the best approach for defining mathematical functions.

Thanks.

+4
source share
5 answers

If you integrate only polynomials, you do not need to represent a common mathematical function, use numpy.poly1d , which has an integ method for integration.

 >>> import numpy >>> p = numpy.poly1d([2, 4, 6]) >>> print p 2 2 x + 4 x + 6 >>> i = p.integ() >>> i poly1d([ 0.66666667, 2. , 6. , 0. ]) >>> integrand = i(1) - i(0) # Use call notation to evaluate a poly1d >>> integrand 8.6666666666666661 

To integrate arbitrary numeric functions, you must use scipy.integrate with regular Python functions for functions. To integrate functions analytically you should use sympy . This is not like what you want either in this case, especially not in the latter.

+8
source

Look, Ma, there is no import!

 >>> coeffs = [2., 4., 6.] >>> sum(coeff / (i+1) for i, coeff in enumerate(reversed(coeffs))) 8.6666666666666661 >>> 

Our guarantee: Works for a polynomial of any positive degree or your money back!

Update in our research laboratory: extended warranty; s / positive / non-negative / :-)

Refresh . Here is an industrial-robust version that is reliable under conditions of random ints in coefficients without calling a function in a loop and uses neither enumerate() nor reversed() in the installation:

 >>> icoeffs = [2, 4, 6] >>> tot = 0.0 >>> divisor = float(len(icoeffs)) >>> for coeff in icoeffs: ... tot += coeff / divisor ... divisor -= 1.0 ... >>> tot 8.6666666666666661 >>> 
+5
source

It may be superfluous to resort to universal digital integration algorithms for your special case ... if you are working on algebra, there is a simple expression that gives you scope.

You have a polynomial of degree 2: f (x) = ax 2 + bx + c

You want to find the area under the curve for x in the range [0,1] .

The simplest F (x) = ax 3/3 + bx 2/2 + cx + C

Area under the curve from 0 to 1: F (1) - F (0) = a / 3 + b / 2 + c

So, if you are only calculating the area for the interval [0,1] , you can consider using this simple expression, rather than resorting to general-purpose methods.

+4
source

The ' quad ' in scipy.integrate is a universal method for integrating functions of one variable over a certain interval. In a simple case (for example, described in your question) you pass your function and the lower and upper limits, respectively. 'quad' returns a tuple consisting of an integral result and an upper margin of error.

 from scipy import integrate as TG fnx = lambda x: 3*x**2 + 9*x # some polynomial of degree two aoc, err = TG.quad(fnx, 0, 1) 

[Note: after I posted this answer, posted before mine, and which represents polynomials using "poly1d" in Numpy. My script just above can also take a polynomial in this form:

 import numpy as NP px = NP.poly1d([2,4,6]) aoc, err = TG.quad(px, 0, 1) # returns (8.6666666666666661, 9.6219328800846896e-14) 
+1
source

If integrating quadratic or cubic polynomials from get-go, the alternative to deriving explicit integral expressions is to use the Simpson rule; it is a deep fact that this method accurately integrates polynomials of degree 3 and lower.

Borrow Mike Graham's example (I haven't used Python after a while, sorry if the code looks elusive):

 >>> import numpy >>> p = numpy.poly1d([2, 4, 6]) >>> print p 2 2 x + 4 x + 6 >>> integrand = (1 - 0)(p(0) + 4*p((0 + 1)/2) + p(1))/6 

uses the Simpson rule to calculate the integrand value. You can see for yourself that the method works as advertised.

Of course, I did not simplify the expression for integrand to indicate that 0 and 1 can be replaced with arbitrary values โ€‹โ€‹of u and v , and the code will still work to find the integral of the function from u to v .

+1
source

Source: https://habr.com/ru/post/1302683/


All Articles