Python 绘制具有不同函数的分岔图

Python 绘制具有不同函数的分岔图,python,numpy,matplotlib,chaos,Python,Numpy,Matplotlib,Chaos,我正在为x的两个不同函数绘制分叉图。我的困惑在于试图使用numpy数组并将它们传递给sequence函数中的条件。我可以成功地使用序列(r,x)绘制图形。另一种分岔算法工作得很好,但它在序列中不使用条件 我尝试过使用numpy.vectorize(sequence)和numpy.where(…),但也失败了 #numpy.where(...) attempt def sequence(r, x): x1 = numpy.where(0 < x and x < 1/2, 0, 2

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

我尝试过使用
numpy.vectorize(sequence)
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]
#numpy.where(…)尝试
def序列(r,x):
x1=numpy,其中(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()```
def序列(r,x): 如果0=(迭代-最后一次): ax1.绘图(r,x',k',alpha=.25) ax1.set_xlim(2.5,4) ax1.集合标题(“分叉图”) ax2.axhline(0,颜色='k',lw=.5,alpha=.5) 图(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.集合标题(“李雅普诺夫指数”) plt.紧_布局() plt.show()```
我需要的是位and、
&
而不是逻辑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]
def序列(r,x): x1=numpy,其中((0这里的一个问题是行
0
。这里应该使用numpy比较,而不是Python
,它在元素方面起作用(&,逻辑_和,等等)。因此,对于numpy,您需要编写
(0
很有趣。这就是它所需要的。按位和将在逻辑和上工作的原因是什么?我想这与numpy数组和类似的工作方式有关。此外,我忘了在组合这两个数组的位置添加行,然后返回带有删除的0的数组。这看起来也对吗?谢谢你们的帮助,伙计们!当你们这样做时,
a和b
python执行
bool(a)和bool(b)
。要执行元素,必须让numpy显式执行:
np.logical\u和(a,b)