数值积分方法问题python

时间:2018-11-12 19:57:24

标签: python numerical-methods

我正在尝试整合以下公式:

enter image description here

下面是我尝试使用针对f(a) = sin(a)的自制方案执行此集成。

def func(x):
    return math.sin(x)

def integration (f, n, r, a, dtheta ):

    summation = 0
    theta = 0
    while theta <= 2*np.pi:

        f_arg = a + r*np.exp(1j*theta)
        second = np.exp(-1j*theta*n)

        summation += f(f_arg) * second * dtheta
        theta += dtheta

    return math.factorial(n)*summation / (2*np.pi*r**n)

integration(func, n=1, r=1, a=0, dtheta=2*np.pi/10000)

n=1的一阶导数(f(a) = sin(a))是f'(a) = cos(a)。当在a = 0处求值时,应该给出cos(0) = 1,但事实并非如此。我要去哪里错了?

1 个答案:

答案 0 :(得分:3)

您的问题似乎是df[] <- lapply(df, strrep, 2) replace(df, df ==11, '01') 函数,该函数不支持复杂的参数:

math.sin

它还会引发警告(但不是错误...):

i = np.exp(.5j * np.pi)
math.sin(i), np.sin(i)
(6.123233995736766e-17, (9.44864380126377e-17+1.1752011936438014j))

使用ComplexWarning: Casting complex values to real discards the imaginary part 可以解决问题。

通常,通过更多地使用numpy,实现可能更易于表达(更易于调试),例如

np.sin