简体   繁体   English

计算曲线下面积时面积的负值

[英]Negative Values of Area when calculating area Under a Curve

I have obtained coefficients of a polynomial using polyval in python.我在python中使用polyval获得了多项式的系数。 I used the following code to calculate area of the polynomial.我使用以下代码来计算多项式的面积。 The curve appeared above y=0 line when visualised.可视化时,曲线出现在 y=0 线上方。 But the area when calculated appeared to be negative.但是计算出来的面积似乎是负的。 Could you please someone help me what is the issue with the following code.您能否请人帮助我以下代码有什么问题。

import numpy as np
# insert the coefficents of the polynomial
p = np.array([ 2.60349395e+09, -3.34913329e+10,  1.87588633e+11, -5.85791667e+11,
           1.04784500e+12, -8.03652884e+11, -8.37573977e+11,  3.17205629e+12,
           -4.33851893e+12,  3.48269443e+12, -1.71966834e+12,  4.86703099e+11,
           -6.07938187e+10])

def f(x):
    return ((p[0]*(x**12))+(p[1]*(x**11))+(p[2]*(x**10)))+(p[3]*(x**9))+(p[4]*(x**8))+
            (p[5]*(x**7))+(p[6]*(x**6))+(p[7]*(x**5))+(p[8]*(x**4))+(p[9]*(x**3))+
            (p[10]*(x**2))+(p[11]*(x**1))+(p[12]))

N = 1000
#insert the initial value of independent variable
x1=1
#insert the final value of the independent variable
x2 = 1.578687

dx = (x2 - x1)/N

A = 0
t=x1

while t<=x2:
  dA=f(t)*dx
  A = A +dA
  t = t + dx

# Area under the curve
print("A=",A)

The area is negative because the function f(t) is strictly negative in that interval.面积为负,因为函数f(t)在该区间内严格为负。 You can see that by quickly plotting the function making use of np.arange and the matplotlib package.您可以通过使用np.arangematplotlib包快速绘制函数来看到这一点。

import numpy as np
import matplotlib.pyplot as plt
# insert the coefficents of the polynomial
p = np.array([ 2.60349395e+09, -3.34913329e+10,  1.87588633e+11, -5.85791667e+11,
          1.04784500e+12, -8.03652884e+11, -8.37573977e+11,  3.17205629e+12,
          -4.33851893e+12,  3.48269443e+12, -1.71966834e+12,  4.86703099e+11,
          -6.07938187e+10])

def f(x):
   return (p[0]*(x**12) + p[1]*(x**11) + p[2]*(x**10)+ p[3]*(x**9) + p[4]*(x**8) +
           p[5]*(x**7) + p[6]*(x**6) + p[7]*(x**5) + p[8]*(x**4) + p[9]*(x**3) +
           p[10]*(x**2) + p[11]*x+p[12])

plt.plot(np.arange(0, 2, 0.001), f(np.arange(0, 2,  0.001))) # plotting

在此处输入图片说明

You can also use exceptions to check for negative values of f(t) when integrating:您还可以在积分时使用异常来检查f(t)负值:

N = 1000
#insert the initial value of independent variable
x1 = 1
#insert the final value of the independent variable
x2 = 1.578687

dx = (x2 - x1)/N

A = 0
t = x1

while t<=x2:
    if f(t) < 0:
        # raise exception if negative value is encountered, integration will stop.
        raise Exception("Negative values encountered") 
    else:
        dA = f(t)*dx
        A = A +dA
        t = t + dx
        


# Area under the curve
print("A=",A)

Output:输出:

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-38-2aea71dc1318> in <module>
     12 while t<=x2:
     13     if f(t) < 0:
---> 14         raise Exception("Negative values encountered")
     15     else:
     16         dA = f(t)*dx

Exception: Negative values encountered

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM