window.onafterprint
没有你读得太多,基本上是函数" many_walks(n,m)"给我一个包含m个数字的向量。我已经使用matplotlib绘制直方图并且它可以工作。
现在我想考虑我定义为func(x,s)的函数。请注意,它还有一个参数,即n(是的,与walk和many_walks相同)。我想将此函数应用于我的直方图,所以我尝试类似于here,但它不起作用。我的意思是我非常糟透,我不明白自己做错了什么,似乎我和发布的问题一样。我的目的是能够找到系数s,使得函数适合直方图并在同一图上绘制。
这是它给我的错误
import random import matplotlib.pyplot as plt from math import log, e, ceil, floor,sqrt,pi,exp import numpy as np from numpy import arange,array, empty import pdb from random import randint import copy from operator import add import scipy from scipy.optimize import curve_fit import numpy as np import matplotlib.pyplot as plt import math import scipy def walk(n): time = 0 i = 0 while i < n: if i == 0: r = random.random() t = -log(1-r,e) time = time+t i=i+1 elif i !=0 and i!=n-1 : r_2 = random.random() t_2 = -log(1-r_2,e) time = time+t_2 R = random.random() if 0 <= R < 0.5: i = i -1 elif 0.5 <= R <=1: i=i+1 else: i = n return time def many_walks(n,m): v_t = [] for i in range(m): w = walk(n) v_t.append(w) return v_t n = 20 m = 2000 bins = [10*i for i in range(200)] numpy_hist = plt.figure() plt.hist(many_walks(n,m), bins) def func(x,s): x = np.array(x) return (4/(x*np.sqrt(np.pi*n*s)))*np.exp(-(np.log(x)**2)/(2*n*s)) xx = np.linspace(0.1,2000,10000) popt, pcov = curve_fit(func, bins, many_walks(n,m))
TypeError:返回数组必须是ArrayType