Johan Sestiger - 6 months ago 38

Python Question

I basically evaluated a integral because the Simpsons and other integral approximations from scipy don't give accurate answers. After creating the array called "bounds" with the different bounds to evaluate the integral I want to run through the array with a for loop and use the consecutive upper and lower bounds (or in the for loop the current and previous item after skipping the first item) of the integral in the function. Here is what I did

`import numpy as np`

turns = [0, 200, 400, 600, 800, 1000, 1200, 1400]

bounds = np.cumsum(turns)

print('Lower and Upper Integral bounds ', bounds)

peer = []

for t in bounds[1:]:

peer.append((0.304*(t**2/2))-(0.304*(bounds[bounds.index(#previous item)]**2/2)))

print('peer ', peer)

Output

`Lower and Upper Integral bounds [ 0 200 600 1200 2000 3000 4200 5600]`

AttributeError: 'numpy.ndarray' object has no attribute 'index'

Desired output values

`Lower and Upper Integral bounds [ 0 200 600 1200 2000 3000 4200 5600]`

peer [6080, 48640, 164160, 389120, 760000, 1313280, 2085440]

Answer

Try this

```
import numpy as np
turns = [0, 200, 400, 600, 800, 1000, 1200, 1400]
bounds = np.cumsum(turns)
print('Lower and Upper Integral bounds ', bounds)
peer = []
for index,t in enumerate(bounds[1:]):
peer.append((0.304*(t**2/2))-(0.304*(bounds[index]**2/2)))
print('peer ', peer)
```

Source (Stackoverflow)