python中一个复多项式方程的求根算法

python中一个复多项式方程的求根算法,python,newtons-method,Python,Newtons Method,我试图用一个简单的算法来解下面的方程。我不确定我使用的算法是否是最好的,但这是我能想到的唯一方法。 在这个方程中,除了p以外的所有东西都是已知的,我正试图解决这个问题。N是计数数组,i是通道号。S(n)是具有特定n的概率,C是(n,r)的二项式系数。Pi是i信道中的概率,Pj是距离i为D的先前信道中的概率。代码本身不起作用,但我相信主要问题在于我试图解决它的方式 import numpy as np import matplotlib.pyplot as plt import math as

我试图用一个简单的算法来解下面的方程。我不确定我使用的算法是否是最好的,但这是我能想到的唯一方法。

在这个方程中,除了p以外的所有东西都是已知的,我正试图解决这个问题。N是计数数组,i是通道号。S(n)是具有特定n的概率,C是(n,r)的二项式系数。Pi是i信道中的概率,Pj是距离i为D的先前信道中的概率。代码本身不起作用,但我相信主要问题在于我试图解决它的方式

import numpy as np
import matplotlib.pyplot as plt
import math as ms
from scipy.misc import derivative
import scipy as sp

def next_guess(f, x):
    slop = derivative(f, x, dx = 0.01)
    return x - float(f(x))/slop

def my_newton(f, guess):
    for i in range(30):
        #print(guess)
        guess = next_guess(f, guess)
    return guess


def binomial(n, r):

    dif = ms.factorial(n - r)

    n = ms.factorial(n)
    r = ms.factorial(r)

    return (n/(r*dif))

def wrap_func(x, S = np.array([0.1, 0.5, 0.2, 0.1, 0.1]), D = 1, N = np.array([10, 15, 20, 1, 13])):
    if type(x) == float:
        z = np.zeros(1)
    else:
        z = np.zeros(x.shape[0])
    N_tot = N.sum()
    n_max = S.shape[0]
    for i in range(z.shape[0]):
        z[i] += my_newton(func(x, S = S, D = 1, N = N[i], n_max = n_max, N_tot = N_tot, i = i), i/100)

    return z    



def func(x, S = np.array([0.1, 0.5, 0.2, 0.1, 0.1]), D = 1, N = 0, n_max = 5, N_tot = 10, i = 0):
    S_sum = 0
    binom_sum = 0
    y = 0

    for n in range(n_max):
        S_sum += S[n] 
        for r in range(n):
            binom_sum += binomial(n, r)

            y += S_sum * binom_sum * (x**r) * (1 - x - summ_x(x, D, i, S, N, n_max, N_tot))**(n-r)

    return N_tot * y - N     

def summ_x(x, D, i, S, N, n_max, N_tot):
    j_min = max(i - D - 1, 0)
    j_max = i - 1
    x_values = 0
    if i == 0:
        return x_values
    else:
        for j in range(j_min, j_max):
            x_values += func(x, S, D, N, n_max, N_tot, i)

        return x_values

x = np.linspace(0, 1, 1000)
S = np.array([0.1, 0.5, 0.2, 0.1, 0.1])
N = np.random.choice(50, size = 1000)
print(my_newton(wrap_func, 0.1))

plt.plot(x, wrap_func(x, S = S, D = 1, N = N ))
plt.axhline(0, lw = 0.5, color = 'grey')
#plt.plot(my_newton(wrap_func, 1), wrap_func(my_newton(wrap_func, 1), S = S, D = 1, N = N), 'd')
plt.show()

SciPy中有许多根查找例程可能会有所帮助。退房