python中的随机游动绘图
我一直在阅读Jesse M.Kinder&Philip Nelson的《Python物理建模学生指南》一书,有一个练习指导我构建布朗运动模拟器/随机行走模拟器并绘制它。我不知道为什么我的代码不起作用,我希望能得到您的帮助:python中的随机游动绘图,python,Python,我一直在阅读Jesse M.Kinder&Philip Nelson的《Python物理建模学生指南》一书,有一个练习指导我构建布朗运动模拟器/随机行走模拟器并绘制它。我不知道为什么我的代码不起作用,我希望能得到您的帮助: import numpy as np import matplotlib.pyplot as plt from numpy.random import random as rng def Brownian_motion(steps): """ this is
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng
def Brownian_motion(steps):
"""
this is a random walk function
define the number of steps to be taken as a integer value
"""
#these are the random numbers
steps_x = rng(steps)
steps_y = rng(steps)
#Here I change the random numbers to 1 or -1
pace_x = (steps_x < 0.5)
for i in pace_x:
if i == False:
pace_x[i] = -1
else:
pace_x[i] = 1
return pace_x
pace_y = (steps_y < 0.5)
for i in pace_y:
if i == False:
pace_y[i] = -1
else:
pace_x[i] = 1
return pace_y
plt.plot(np.cumsum(pace_x), np.cumsum(pace_y))
plt.show()
Brownian_motion(500)
它不会抛出错误,但我无法让它进行绘图
编辑:
这与我期望看到的类似:
循环结束时有不必要的返回语句,因此代码永远不会到达绘图。删除这些,布朗运动函数应该有机会完成执行。我不知道布朗运动模拟器/随机行走模拟器是什么,但代码中的问题是,在函数中有一个返回语句,实际上是2,它使函数停止而不执行绘图 评论它似乎工作,它策划的东西,我不知道这是你所期待的 守则:
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng
def Brownian_motion(steps):
"""
this is a random walk function
define the number of steps to be taken as a integer value
"""
#these are the random numbers
steps_x = rng(steps)
steps_y = rng(steps)
#Here I change the random numbers to 1 or -1
pace_x = (steps_x < 0.5)
#print(pace_x)
x_list = list()
y_list = list()
for i in pace_x:
if i == False:
#pace_x[i] = -1
x_list.append(-1)
else:
#pace_x[i] = 1
x_list.append(1)
#return pace_x
print("Hello there")
pace_y = (steps_y < 0.5)
for i in pace_y:
if i == False:
#pace_y[i] = -1
y_list.append(-1)
else:
#pace_x[i] = 1
y_list.append(1)
#return pace_y
plt.plot(x_list, y_list)
plt.show()
Brownian_motion(500)
建议:当Python出现问题时,尝试在代码中加入print函数调用,以检查您所期望的内容是否正确。例如,我将printHello行放在返回之后,发现它现在已被注释,但从未执行过。尝试从函数中删除返回,并将布尔值转换为整数
%matplotlib notebook
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng
def Brownian_motion(steps):
"""
this is a random walk function
define the number of steps to be taken as a integer value
"""
#these are the random numbers
steps_x = rng(steps)
steps_y = rng(steps)
#Here I change the random numbers to 1 or -1
pace_x = (steps_x < 0.5)*1
for i in pace_x:
if i == False:
pace_x[i] = -1
else:
pace_x[i] = 1
#return pace_x
pace_y = (steps_y < 0.5)*1
for i in pace_y:
if i == False:
pace_y[i] = -1
else:
pace_x[i] = 1
#return pace_y
plt.plot(np.cumsum(pace_x), np.cumsum(pace_y))
plt.show()
Brownian_motion(500)
使用numpy,您可以创建更高效的布尔切片。请注意,这不适用于Python列表/元组
import numpy as np
import matplotlib.pyplot as plt
from numpy.random import random as rng
def Brownian_motion(steps):
"""
this is a random walk function
define the number of steps to be taken as a integer value
"""
#these are the random numbers
steps_x = rng(steps)
steps_y = rng(steps)
pace_x = np.ones_like(steps_x)
idx = steps_x < 0.5
pace_x[idx] = -1
idy = steps_y < 0.5
pace_y = np.ones_like(steps_y)
pace_y[idy] = -1
plt.plot(np.cumsum(pace_x), np.cumsum(pace_y))
# plt.axis("equal")
# I would also add this. This way your plot won't be
# distorted.
plt.show()
a = Brownian_motion(500)
它给了你什么错误?没有,它没有给我错误。控制台是空的,我正在看。我正在努力……我会回来的:非常感谢!我正在努力准备下学期的大学物理研究,我需要把我的编码技能准备好完成任务。你为什么要做回程?这意味着你永远不会得到plt.showI做到了这一点,是的,这有助于完成它,但我似乎没有得到负值。我原以为我的if语句会解决这个问题,但它似乎不起作用。你能帮我一下吗?@LuisFLlano打印steps_x和pace_x的值,看看它们有什么。我有1和0,我需要这些0为-1。我想我的if语句可以胜任这项工作。我该如何修复它们?@LuisFLlano问题出在像pace_x[I]=-1这样的行上。i不是数组索引,而是真/假值。我认为您需要将-1/1追加到新的数据列表中,或者使用调试器。