2016-12-02 71 views
-1

我的家庭作业让我写一个代码,模拟泰勒教科书在经典力学中的数字。如果有人有兴趣知道,请拨打12.4012.41分岔图没有绘图/剧情只是没有出现

我能够重现他们中的一个是下面的代码(这可能是我确实遇到问题的代码很好的参考):

import nympy as np 
import matplotlib.pyplot as plt 

# We need to calculate the first fixed point 
r1=np.array(np.arange(0,4,0.09)) 
x1 = np.zeros((len(r1),1)) 

# Now calculating the second fixed point 
r2=np.array(np.arange(1,4,0.1)) 
x2 = (r2 -1)/r2 

# Now finding when the fixed points split up again 
r3=np.array(np.arange(3,4,0.1)) 
y1 = (((r3**2 - 2*r3 - 3)**0.5) + 1 + r3)/(2*r3) 
y2 = ((-(r3**2 - 2*r3 - 3)**0.5) + 1 + r3)/(2*r3) 

# Now finding the experimental values for 1/2 of a split 
x3 = [] 
for r in np.arange(0,4,0.09): 
    x = 0.666 
    for i in range(100): 
     x = (r**2) * x * (1.0 -x) - (r**3) * (x**2)*((1-x)**2) 
    x3.append(x) 

# Doing the same as above second 1/2 
x4 = [] 
for r in np.arange(0,4,0.09): 
    x = 0.8 
    for i in range(100): 
     x = (r**2) * x * (1.0 -x) - (r**3) * (x**2)*((1-x)**2) 
    x4.append(x) 

plt.plot(r1,x3,'bo', label='Experimental') 
plt.plot(r1,x4,'bo') 
plt.plot(r3,y2,'k-') 
plt.plot(r3,y1,'k-') 
plt.plot(r1,x1,'k-', label='Theoretical') 
plt.plot(r2,x2,'k-') 
plt.legend(loc=2) 
plt.show() 

,这里是第二图像的代码这似乎并不奏效。我不知道为什么。任何帮助,将不胜感激。这个数字只是没有绘制,我不知道为什么。

import numpy as np 
import matplotlib.pyplot as plt 

for r in n.arange(2.8,4,0.01): 
    x = 0.5 
    for i in range(150): 
     x = r*x*(1-x) 
     if i >= 125: 
      plt.plot(r,x,'k') 
plt.xlim (2.8,4) 
plt.show() 
+0

欢迎所以堆栈溢出。请阅读[如何提出一个好问题](http://stackoverflow.com/help/how-to-ask)。另外,请粘贴您的代码,而不是插入图片。在帖子中有一个“{}”按钮,可以将所有内容缩进四个空格,并显示为代码 –

+0

您是否在使用IPython笔记本? – Xevaquor

回答

0

有一个很好的理由你的代码不工作。

当一个数组预期用于x和y时,您使用标量值重复调用plt.plot:仅用一个点绘制一条线是相当困难的。 试试这个,你会看到你会得到一个空的情节:

plt.plot(0,1) 
plt.show(); 

我不知道如果这是你在找什么,最终,但这里有一个工作版本,你可以为至少使用初始点。请注意,我改变了循环的我:与其做i in range(150)其次是if i >=125,更好的只是i in range(125,150),你会避免不必要的重复:

fig, ax = plt.subplots(nrows=1, ncols=1) 
r1 = [] 
x1 = [] 
for r in np.arange(2.8,4,0.01): 
    x=0.5 
    # Instead of range(150) then checking if i >= 125... 
    for i in range(125,150): 
     x = r*x*(1-x) 
     r1.append(r) 
     x1.append(x) 

ax.plot(r1,x1,'k') 
plt.show() 

Result graph

0

我偶然发现了这个一岁后和意识到对OP报告的问题有一个简单的答案。

缺失的位是标记的说法。

plot(r,x,'k')替换为plot(r,x,'.',color='k')并且突然显示分叉图。

Bifurcation for logistic regression

import numpy as np 
import matplotlib.pyplot as plt 

plt.figure() 
for r in n.arange(2.8,4,0.01): 
    x = 0.5 
    for i in range(150): 
     x = r*x*(1-x) 
     if i >= 125: 
      plt.plot(r,x,'.',color='black',markersize=2) 
plt.xlim (2.8,4) 
plt.show() 

此代码是好的,但相当低效。拨打plot()只需要一次。

plt.figure() 
def mark(r,x,diagram,v): 
    N,M = np.shape(diagram) 
    rmin,rmax = r[0],r[-1] 
    for (i,j) in zip(r,x): 
     nx = int(j*(M-1)) 
     nr = int((i-rmin)/(rmax-rmin)*(N-1)) 
     diagram[nx,nr] = v 

r = np.arange(2.8,4,0.01) 
diagram = np.zeros((200,200)) 
x0 = 0.5 
x = np.ones_like(r)*x0 
for i in range(150): 
    x = np.multiply(np.multiply(r,x),(1-x)) 
    if i >= 125: 
     mark(r,x,diagram,1) 

plt.imshow(np.flipud(diagram), extent=[r[0],r[-1],0,1], 
      aspect=(r[-1]-r[0]),cmap=plt.cm.Greys) 
plt.show() 

Another bifurcation diagram

后者代码使用

CPU times: user 224 ms, sys: 29 µs, total: 224 ms 
Wall time: 261 ms 

虽然前者代码效率不高花费更长的时间超过30次

CPU times: user 8.59 s, sys: 91.7 ms, total: 8.68 s 
Wall time: 8.68 s