出于某種原因,使用 numpytrapz和 scipy 會cumtrapz產生不同的解決方案。
x = np.linspace(-2, 4, num=20)
y = (x)
y_int = integrate.cumtrapz(y, x, initial=0)
y_tr = np.trapz(y, x, axis = 0)
display(y_tr)
display(y_int)
rule給出的最終值trapz是 給出的兩倍cumtrapz。
-5.551115123125783e-17
array([ 0.00000000e 00, -3.98891967e-01, -7.53462604e-01, -1.06371191e 00,
-1.32963989e 00, -1.55124654e 00, -1.72853186e 00, -1.86149584e 00,
-1.95013850e 00, -1.99445983e 00, -1.99445983e 00, -1.95013850e 00,
-1.86149584e 00, -1.72853186e 00, -1.55124654e 00, -1.32963989e 00,
-1.06371191e 00, -7.53462604e-01, -3.98891967e-01, -2.77555756e-16])
是否有一個原因?
uj5u.com熱心網友回復:
原因似乎是分配記憶體和執行浮動操作的方式。
兩者-2.77555756e-16和-5.551115123125783e-17本質上都是 0。通過將限制從 更改為運行相同的代碼-2給出4正確答案:
x = np.linspace(-2, 4, num=20)
y = (x)
y_int = integrate.cumtrapz(y, x, initial=0)
y_tr = np.trapz(y, x, axis = 0)
display(y_tr)
display(y_int)
給予:
6.000000000000001
array([ 0. , -0.58171745, -1.06371191, -1.44598338, -1.72853186,
-1.91135734, -1.99445983, -1.97783934, -1.86149584, -1.64542936,
-1.32963989, -0.91412742, -0.39889197, 0.21606648, 0.93074792,
1.74515235, 2.65927978, 3.67313019, 4.7867036 , 6. ])
結果在每種情況下都是 6。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/511874.html
標籤:麻木的scipy
