2016-11-27 9 views
0

シンプソンとscipyからの他の整数近似が正確な答えを出さないので、基本的に積分を評価しました。積分を評価するために異なる境界を持つ "bounds"という配列を作成した後、forループを使って配列を実行し、連続した上限と下限を使用したい(またはforループで最初と最後の項目をスキップした後item)を関数内の積分値と置き換えます。ここで私は配列内の最初の項目をスキップし、forループのpythonの現在と前の項目を使用

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) 

出力

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

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

所望の出力値

Lower and Upper Integral bounds [ 0 200 600 1200 2000 3000 4200 5600] 
peer [6080, 48640, 164160, 389120, 760000, 1313280, 2085440] 
+0

「scipyは正確な結果を得られません」と非常に驚いています。 –

+0

私はまだscipyからすべての統合オプションを試していません。正確な答えを返す関数があると確信しています。私はちょうど手で積分を行うことがより速く、最良の積分関数を探すのではなく、境界を代用することがわかりました。おかげで –

+0

確かに、関数が何をしているかを100%認識しているので、関数を自分で記述する方が簡単な方が良い場合もあります。私は、scipyが最初にうまくいかなかった理由とその理由を不思議に思っていました。違反は意図されていませんでした。 –

答えて

0

あなたはtolist()
に使用することができ、この

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) 
-1

を試してみたものです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) 

そして、あなたはbounds[bounds.index(t)-1]bounds[bounds.index(t-1)]を変更する必要があります。
結果は次のとおりです。

('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]) 
関連する問題