We import the libraries we are going to need
import numpy as np
import matplotlib.pylab as plt
from scipy.optimize import curve_fit
We save the url of our data
url='https://raw.githubusercontent.com/jmsevillam/Herramientas-Computacionales-UniAndes/master/Data/Velocities.dat'
We load our data
data=np.genfromtxt(url)
We plot the data of $v_x$ and $v_y$
v_x=data[:,0]
v_y=data[:,1]
fig=plt.figure(figsize=(10,5))
ax1=fig.add_subplot(121)
ax1.hist(v_x,bins=50)
ax1.set_xlabel("$v_x$")
ax1.set_ylabel("Frequency")
ax2=fig.add_subplot(122)
ax2.hist(v_x,bins=50)
ax2.set_xlabel("$v_x$")
ax2.set_ylabel("Frequency")
The total velocity $$ v=\sqrt{v_x^2+v_y^2} $$
vel=data[:,2]
histo=plt.hist(vel,bins=20)
We define our model as,
def f(x,a,s):
return a*x*np.exp(-0.5*(x**2.)/s)
We separate our histogram to do the fit
x_bins=(histo[1][:-1]+histo[1][1:])/2.
We do the fit using the Initial conditions
popt,pcov=curve_fit(f,x_bins,histo[0],p0=[1000,1000**2.])
And finally plot the histogram and the fit function
fig=plt.figure(figsize=(5, 3))
ax=fig.add_subplot(111)
ax.set_xlabel(u'Velocidad '+r'$V_x$')
ax.set_ylabel(u'Frecuencia')
ax.hist(vel,label='Histograma',bins=20)
ax.plot(x_bins,f(x_bins,*popt), label='Ajuste')
plt.legend()
This problem can be sovlved easily using the function np.linalg.eigvals()
, such that
m=1000
n=100
eigen_vals=[]
for i in range(n):
matrix=np.random.normal(0,1,(m,m))
matrix=matrix+matrix.T
eigen_vals=np.append(eigen_vals,np.linalg.eigvals(matrix))
And the results are
fig=plt.figure(figsize=(7, 5))
ax=fig.add_subplot(111)
ax.hist(eigen_vals,bins=100);
We generate the random walks
data=[]
for i in range(10000):
data.append(np.random.random(1000).mean())
We define our model
def f(x,a,b,c):
return a*np.exp(-(x-b)**2./c)
To calculate the histogram,
histogram=plt.hist(data,bins=20)
We separate the bins
x_bins=(histogram[1][:-1]+histogram[1][1:])/2.
print(histogram)
we do the fit,
popt,pcov=curve_fit(f,x_bins,histogram[0])
And at the end, we plot the histogram and the fit function
x_plot=np.linspace(x_bins[0],x_bins[-1],1000)
plt.hist(data,bins=20)
plt.plot(x_plot,f(x_plot,*popt))