以数组值为上限的数组数值积分
Numerical integration of array with array value as upper limit
所以我有以下代码:
import numpy as np
import scipy.special as spec
import scipy.integrate as integrate
a = array
b = constant * a
c = b*a/constant
e3 = spec.expn(3,c)
现在,我需要计算 e3 从 0 到 a 的积分.由于 e3 是 c 的函数,而后者又是 a 的函数,所以我有一个很难解决这个问题。
我试过使用 loop、integrate.quad 或 both 来解决这个问题但我不断收到各种错误。
我对在 python 中集成 sutff 还很陌生,因此我们将不胜感激。
这是一个可能的解决方案,请检查我是否正确理解问题以及它是否有用...
import scipy.special as spec
from scipy.integrate import quad
import numpy as np
array = np.array([0, 1, 2, 3, 4, 5]) # array[i] >= 0 for all i in [0, len(array) - 1]. For example, array = np.array([1, 2, 3, 4, 5])
constant = 3 # >= 0. For example, constant = 3
a = array
b = constant * a # multiply numpy array by a scalar (it is possible with numpy arrays (: )
c = b * a / constant
e3 = spec.expn(3, c)
# Some usefull explanations:
print('Output:', e3) # return a ndarray with the value of the integral e3 en each point specified in c (numpy array)
# Output: [1.09691967e-01 2.76136095e-03 1.04786279e-05 5.96855753e-09 4.97790975e-13]
# For evaluate the integral e3 from 0 to a[len(a) - 1]:
result1 = e3[len(a) - 1] - e3[0]
print('Output:', result1)
# Output: -0.499999999950223
# Other thing is the integral of e3 from 0 to a[len(a) - 1]. You can use quad (return the value and an estimted error)
# See https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html
result2 = quad(lambda x: spec.expn(3, x), 0, c[len(a) - 1])
print('Output:', result2)
# Output: (0.3333333333328521, 4.440892098500626e-16)
# Note that integration limits are floats not arrays.
所以我有以下代码:
import numpy as np
import scipy.special as spec
import scipy.integrate as integrate
a = array
b = constant * a
c = b*a/constant
e3 = spec.expn(3,c)
现在,我需要计算 e3 从 0 到 a 的积分.由于 e3 是 c 的函数,而后者又是 a 的函数,所以我有一个很难解决这个问题。
我试过使用 loop、integrate.quad 或 both 来解决这个问题但我不断收到各种错误。
我对在 python 中集成 sutff 还很陌生,因此我们将不胜感激。
这是一个可能的解决方案,请检查我是否正确理解问题以及它是否有用...
import scipy.special as spec
from scipy.integrate import quad
import numpy as np
array = np.array([0, 1, 2, 3, 4, 5]) # array[i] >= 0 for all i in [0, len(array) - 1]. For example, array = np.array([1, 2, 3, 4, 5])
constant = 3 # >= 0. For example, constant = 3
a = array
b = constant * a # multiply numpy array by a scalar (it is possible with numpy arrays (: )
c = b * a / constant
e3 = spec.expn(3, c)
# Some usefull explanations:
print('Output:', e3) # return a ndarray with the value of the integral e3 en each point specified in c (numpy array)
# Output: [1.09691967e-01 2.76136095e-03 1.04786279e-05 5.96855753e-09 4.97790975e-13]
# For evaluate the integral e3 from 0 to a[len(a) - 1]:
result1 = e3[len(a) - 1] - e3[0]
print('Output:', result1)
# Output: -0.499999999950223
# Other thing is the integral of e3 from 0 to a[len(a) - 1]. You can use quad (return the value and an estimted error)
# See https://docs.scipy.org/doc/scipy/reference/generated/scipy.integrate.quad.html
result2 = quad(lambda x: spec.expn(3, x), 0, c[len(a) - 1])
print('Output:', result2)
# Output: (0.3333333333328521, 4.440892098500626e-16)
# Note that integration limits are floats not arrays.