Using Scherrer equation for calculating the grain size
<p>I'm trying to calculate the grain size by Scherrer equation but I have stuck in FWHM. </p>

<pre><code>import numpy as np
#import math

k = 0.94
wave_length = 1.5406e-10

data = np.genfromtxt("G3.txt")

indice = np.argmax(data[:,1])
peak = (data[indice, :])
#D = (k*wave_length) / (beta*cos((math.radian(theta))
</code></pre>

<p><a href="http://i.stack.imgur.com/1l5Pq.png" rel="nofollow"><img src="http://i.stack.imgur.com/1l5Pq.png" alt="Corresponding graphs looks likes as in the picture"></a></p>

<p>Information: <a href="https://en.wikipedia.org/wiki/Scherrer_equation" rel="nofollow">Scherrer equation</a>,  <a href="https://en.wikipedia.org/wiki/Full_width_at_half_maximum" rel="nofollow">Full width at half maximum</a>, <a href="http://stackoverflow.com/questions/29877427/python-find-fwhm-of-curve">Related question</a></p>

<p>Here is a working example, assuming you have a normal distribution. I run this in a Jupyter console, so in case you don't, you have to skip the "magic line" (<code>%matplotlib notebook</code>) and add <code>plt.show()</code> at the very end.</p>

<pre><code>%matplotlib notebook
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np

numb = 500                                # data size
fwhm_in = 3                               # set FWHM for the artificial data
sigma = fwhm_in/2/np.sqrt(2*np.log(2))    # calculate sigma
xval = np.linspace(-10, 10, numb)         # calculate x and y values using the formula from Wikipedia (see link in question)
yval = (sigma*np.sqrt(2*np.pi))**(-1)*np.exp(-(xval)**2/(2*sigma**2))+np.random.normal(0, 0.03, numb)

def fitFunc(x, x0, sigm):                 # this defines the fit-function
    return (sigm*np.sqrt(2*np.pi))**(-1)*np.exp(-(x-x0)**2/(2*sigm**2))

guess = (0.5, 2)                          # tell the code with which values it should start the iteration. Close but not equal to the real values
fitParams, fitCovariance = curve_fit(fitFunc, xval, yval, guess) # do the actual fit
print(fitParams)

print('FWHM_calc = {:.3f}'.format(fwhm_in))
fwhm_fit = 2*fitParams[1]*np.sqrt(2*np.log(2))  # calculate the FWHM from the fitted sigma ( = fitParams[1], since fitParams[0] is the offset x0)
print('FWHM_fit = {:.3f}'.format(fwhm_fit))

plt.plot(xval,yval, 'r.', label='data')
plt.plot(xval, fitFunc(xval, fitParams[0], fitParams[1]), 'k-', label='fit', linewidth = 3)

plt.grid(True)
plt.legend()
ax = plt.gca()
ax.axvline(fwhm_fit/2, color='b')
ax.axvline(-fwhm_fit/2, color='b')
</code></pre>

<p><a href="https://i.stack.imgur.com/hxaB9.jpg" rel="nofollow"><img src="https://i.stack.imgur.com/hxaB9.jpg" alt="Example for the FWHM (blue vertical lines) in a normal distribution."></a></p>

python numpy physics