[英]How can I simplify this more?
I am trying to apply numpy to this code I wrote for trapezium rule integration: 我正在尝试将numpy应用于我为梯形规则集成编写的这段代码:
def integral(a,b,n):
delta = (b-a)/float(n)
s = 0.0
s+= np.sin(a)/(a*2)
for i in range(1,n):
s +=np.sin(a + i*delta)/(a + i*delta)
s += np.sin(b)/(b*2.0)
return s * delta
I am trying to get the return value from the new function something like this: 我正在尝试从新函数中获取返回值,如下所示:
return delta *((2 *np.sin(x[1:-1])) +np.sin(x[0])+np.sin(x[-1]) )/2*x
I am trying for a long time now to make any breakthrough but all my attempts failed. 我已经尝试了很长时间了,但我的所有尝试都以失败告终。
One of the things I attempted and I do not get is why the following code gives too many indices for array
error? 我尝试但没有得到的一件事是为什么以下代码
too many indices for array
错误提供了too many indices for array
?
def integral(a,b,n):
d = (b-a)/float(n)
x = np.arange(a,b,d)
J = np.where(x[:,1] < np.sin(x[:,0])/x[:,0])[0]
Every hint/advice is very much appreciated. 每个提示/建议都非常感谢。
You forgot to sum over sin(x)
: 您忘了总结
sin(x)
:
>>> def integral(a, b, n):
... x, delta = np.linspace(a, b, n+1, retstep=True)
... y = np.sin(x)
... y[0] /= 2
... y[-1] /= 2
... return delta * y.sum()
...
>>> integral(0, np.pi / 2, 10000)
0.9999999979438324
>>> integral(0, 2 * np.pi, 10000)
0.0
>>> from scipy.integrate import quad
>>> quad(np.sin, 0, np.pi / 2)
(0.9999999999999999, 1.1102230246251564e-14)
>>> quad(np.sin, 0, 2 * np.pi)
(2.221501482512777e-16, 4.3998892617845996e-14)
I tried this meanwhile, too. 同时,我也尝试过。
import numpy as np
def T_n(a, b, n, fun):
delta = (b - a)/float(n) # delta formula
x_i = lambda a,i,delta: a + i * delta # calculate x_i
return 0.5 * delta * \
(2 * sum(fun(x_i(a, np.arange(0, n + 1), delta))) \
- fun(x_i(a, 0, delta)) \
- fun(x_i(a, n, delta)))
Reconstructed the code using formulas at bottom of this page https://matheguru.com/integralrechnung/trapezregel.html 使用本页底部的公式重建代码https://matheguru.com/integralrechnung/trapezregel.html
The summing over the range(0, n+1) - which gives [0, 1, ..., n] - is implemented using numpy. 使用numpy实现范围(0,n + 1)的求和-给出[0,1,...,n]。 Usually, you would collect the values using a for loop in normal Python.
通常,您将在普通Python中使用for循环来收集值。 But numpy's vectorized behaviour can be used here.
但是这里可以使用numpy的矢量化行为。 np.arange(0, n+1) gives a np.array([0, 1, ...,n]).
np.arange(0,n + 1)给出一个np.array([0,1,...,n])。
If given as argument to the function (here abstracted as fun
) - the function formula for x_0
to x_n
will be then calculated. 如果将其作为函数的参数(此处抽象为
fun
),则将计算x_0
到x_n
的函数公式。 and collected in a numpy-array. 并收集在一个numpy数组中。 So
fun(x_i(...))
returns a numpy-array of the function applied on x_0
to x_n
. 所以
fun(x_i(...))
将应用于x_0
的函数的numpy数组返回给x_n
。 This array/list is summed up by sum()
. 该数组/列表由
sum()
。
The entire sum()
is multiplied by 2
, and then the function value of x_0 and x_n subtracted afterwards. 将整个
sum()
乘以2
,然后将x_0和x_n的函数值相减。 (Since in the trapezoid formula only the middle summands, but not the first and the last, are multiplied by 2). (因为在梯形公式中,只有中间被乘数,而不是第一个和最后一个被乘以2)。 This was kind of a hack.
这有点像黑客。
The linked German page uses as a function fun(x) = x ^ 2 + 3
which can be nicely defined on the fly by using a lambda expression: 链接的德语页面使用
fun(x) = x ^ 2 + 3
作为函数,可以使用lambda表达式动态定义它:
fun = lambda x: x ** 2 + 3
a = -2
b = 3
n = 6
You could instead use a normal function definition, too: defun fun(x): return x ** 2 + 3
. 您也可以改用普通的函数定义:
defun fun(x): return x ** 2 + 3
。 So I tested by typing the command: 因此,我通过键入以下命令进行了测试:
T_n(a, b, n, fun)
Which correctly returned: 正确返回的是:
## Out[172]: 27.24537037037037
For your case, just allocate np.sin
to fun
and your values for a
, b
, and n
into this function call. 对于您的情况,只需将
np.sin
分配给fun
并将a
, b
和n
值分配给该函数调用。
Like: 喜欢:
fun = np.sin # by that eveywhere where `fun` is placed in function,
# it will behave as if `np.sin` will stand there - this is possible,
# because Python treats its functions as first class citizens
a = #your value
b = #your value
n = #your value
Finally, you can call: 最后,您可以致电:
T_n(a, b, n, fun)
And it will work! 它将起作用!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.