-2

I am trying to calculate the area under each peak in a graph that I plotted with a set of x and y co-ordinates,

I don't have a function for (x,y), and so I haven't been able to find an appropriate method to do the same.

the co-ordinates are

{
 [10 10]
 [11  1]
 [12  7]
 [14  4]
 [16  8]
 [17  5]]}

And y=0 for all the unmarked x values

The graph plotted with the (x,y) co-ordinates

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Amogha Varsha
  • 89
  • 1
  • 1
  • 8

2 Answers2

0

You have to process one by one for each trapezoid.

Area_1 = ( y1 + y2 ) * 1 / 2

example : (10 + 1 ) * 1 / 2

MarianD
  • 13,096
  • 12
  • 42
  • 54
kelalaka
  • 5,064
  • 5
  • 27
  • 44
  • This doesn't really answer how you would program it, though. For example, do you loop two points at a time? Not each area is a trapezoid either, some are triangles, so `(y1+y2)` could be 0, but the area is not zero between x=13-15 – OneCricketeer Sep 06 '18 at 13:57
  • Yes, not all area is a perfect trapezoid. Triangle doesn't change the result. if y1+y2 is zero result is zero! There are no points like x =13 or x = 15. I simply gave the method. If he knows the Python rest should be easy, right? – kelalaka Sep 06 '18 at 14:05
  • Look at the graph, though, there's a triangle. Area under that curve should be `1/2 * 2 * 4 = 4`, not zero – OneCricketeer Sep 06 '18 at 14:06
  • Area for Trapezoid is actually can calculate an area of Triangle. Just keep on side 0 for the formula in the Trapezoid. – kelalaka Sep 06 '18 at 14:13
0

A bit simple and correct?

points = [[10, 10],
          [11, 1],
          [12, 7],
          [14, 4],
          [16, 8],
          [17, 5]]

areas = []
areas.append( points[0][0]/2.0 )

for i in range(0, points[-1][0] - points[0][0]-2):

    if ( points[i+1][0] == points[i][0]+1 ):
        areas.append( (points[i+1][1] + points[i][1] )/2.0)
    elif ( points[i+1][0] >= points[i][0]+2):
        areas.append( (points[i][1] )/2.0)
        areas.append( (points[i+1][1] )/2.0)

areas.append( points[-1][1]/2.0 )    

print(areas)
>[5.0, 5.5, 4.0, 3.5, 2.0, 2.0, 4.0, 6.5, 2.5]
kelalaka
  • 5,064
  • 5
  • 27
  • 44