0

我正在为 x 的几个不同函数绘制分岔图。我的困惑在于尝试使用 numpy 数组并将它们传递给序列函数中的条件。我可以成功地sequence(r, x)用来绘制图表。另一种分岔算法工作得很好,但它不使用其序列中的条件。

我试过使用numpy.vectorize(sequence)and numpy.where(...),但我也失败了。

#numpy.where(...) attempt
def sequence(r, x):
   x1 = numpy.where(0 < x and x < 1/2, 0, 2 * r * x)
   x2 = numpy.where(1/2 < x and x < 1, 0, 2 * r * (1 - x)

   x = x1 + x2

   return x[x != 0]

这是其余的:

def sequence(r, x):
    if 0 < x and x < 1/2:
        return 2 * r * x
    
    if 1/2 < x and x < 1:
        return 2 * r * (1 - x)

def plot_bifurcation2():
    n = 10000
    r = np.linspace(.4, .7, n)
    iterations = 1000
    last = 100

    x = 1e-6 * np.ones(n)
    lyapunov = np.zeros(n)

    fig, (ax1, ax2) = plt.subplots(2, 1, figsize = (8, 9), sharex = True)

    for i in range(iterations):
        x = sequence(r, x)
        lyapunov += np.log(abs(r - 2 * r * x))

        if i >= (iterations - last):
            ax1.plot(r, x, ',k', alpha = .25)
        
    ax1.set_xlim(2.5, 4)
    ax1.set_title("Bifurcation diagram")

    ax2.axhline(0, color = 'k', lw = .5, alpha = .5)
    ax2.plot(r[lyapunov < 0], lyapunov[lyapunov < 0] / iterations, '.k', alpha = .5, ms = .5)
    ax2.plot(r[lyapunov >= 0], lyapunov[lyapunov >= 0] / iterations, '.k', alpha = .5, ms = .5)
    ax2.set_title("Lyapunov exponent")

    plt.tight_layout()
    plt.show()```
4

1 回答 1

0

我需要的是按位与,&而不是逻辑与,and

def sequence(r, x):
   x1 = numpy.where((0 < x) & (x < 1/2), 0, 2 * r * x)
   x2 = numpy.where((1/2 < x) and (x < 1), 0, 2 * r * (1 - x))

   x = x1 + x2

   return x[x != 0]
于 2020-10-06T01:15:37.883 回答