0

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]
  • I'm very surprised by "scipy doesn't give accurate results". – Imanol Luengo Nov 27 '16 at 10:50
  • I haven't tried all the integration options from scipy yet. I am sure there is a function that gives accurate answers. I just found it faster to do the integral by hand and just substitute the bounds, rather than look for the best integral function. Thanks –  Nov 27 '16 at 11:16
  • Sure, sometimes is easier/better to code functions yourself, as you are 100% aware of what functions are doing. I was just curious of how/why scipy didn't work in the first place, no offense was intended. – Imanol Luengo Nov 27 '16 at 13:33

2 Answers2

0

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)
praveenraj
  • 774
  • 1
  • 9
  • 20
-1

You can use to tolist()
https://stackoverflow.com/a/31517371/7215503

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.tolist().index(t)-1]**2/2)))

print('peer ', peer)

And you need to change bounds[bounds.index(t-1)] to bounds[bounds.index(t)-1].
Here is result.

('Lower and Upper Integral bounds ', array([   0,  200,  600, 1200, 2000, 3000, 4200, 5600]))
('peer ', [6080.0, 48640.0, 164160.0, 389120.0, 760000.0, 1313280.0, 2085440.0])
Community
  • 1
  • 1
hamzzi
  • 13
  • 4