pythonnumpycalculus

How to use numpy Polynomial.integ to find area between curve and horizontal line


I'm using Polynomial from numpy to calculate a best fit line like this:

from numpy.polynomial import Polynomial
...
# get best-fit polynomial for values
best_fit_polynomial = Polynomial.fit(x_points, y_points, 4)

The graph of the values looks like this:

Graph of readings and a polynomial best fit

How do I find the area between the best fit line in orange and say a horizontal line at Y=70?

Going off of this I'm guessing I need to do something like:

i = best_fit_polynomial.integ()
first = i(best_fit_polynomial_where_Y_is_70_on_the_left)
second = i(best_fit_polynomial_where_Y_is_70_on_the_right)
return second - first

I'm having trouble remembering any calculus at all so I'm not sure if I'm even heading in the right direction. Should I be using something instead of numpy?


Solution

  • I don't think you are doing it the good way. Your code will compute the area between the best-fit line in orange and the line at Y = 0 as it is usually done when integrating a function.

    You can try to do a vertical translation of your function, such that the line at Y = 70 becomes the line at Y = 0. If the function of your best fit line is p, you can achieve it by doing:

    p = p - 70
    

    After that, you can use the call notation to compute the integral as follow:

    i = p.integ()
    integrand = i(where_p_is_zero_at_right) - i(where_p_is_zero_at_left)
    #here p is the updated p