1

I am a beginner Python user.

There are two data files for the curve (x, y): https://drive.google.com/open?id=1ZB39G3SmtamjVjmLzkC2JefloZ9iShpO

How to find two areas under the curve, as shown in Figure: enter image description here

Black area (A) and red area (B)

I`m only know to how find total area:

from scipy.integrate import trapz

with open('./x_data.txt', 'rt') as f:
    x_file = f.read()

with open('./y_data.txt', 'rt') as f:
    y_file = f.read()

xlist = []
for line in x_file.split('\n'):
    if line: 
        xlist.append(float(line.strip()))
ylist = []
for line in y_file.split('\n'):
    if line: 
        ylist.append(float(line.strip()))

if len(xlist) != len(ylist):
    print(len(xlist), len(ylist))
    raise Exception('X and Y have different length')

xData = np.array(xlist)
yData = np.array(ylist)

area = trapz(y = yData, x = xData)
print("area =", area)
Sparrowq
  • 23
  • 1
  • 5

1 Answers1

0

You can use Simpsons rule or the Trapezium rule to calculate the area under a graph given a table of y-values at a regular interval.

from scipy.integrate import simps
from numpy import trapz

reference: Calculating the area under a curve given a set of coordinates, without knowing the function

Bill Chen
  • 1,699
  • 14
  • 24
  • 2
    OP is already using `trapz`. I think he is asking how to split that area on a specific coordinate. – 0x5453 Dec 05 '19 at 20:41
  • Yes, I understand that need to integrate from 0 to the value where Y = 500. But it is not clear how to point to that string in the file with the values. – Sparrowq Dec 05 '19 at 21:26
  • I know that Y=500 its 154 string in y_data.txt file. So i need integrate from bottom string to 154? – Sparrowq Dec 05 '19 at 21:39
  • Yes, you are supposed to give the sequence, if I didn't read is wrong from the documentation. Hope it helps. – Bill Chen Dec 05 '19 at 23:52