使用Scherrer方程计算晶粒尺寸

时间:2016-10-11 08:18:07

标签: python numpy physics

我正在尝试用Scherrer方程计算粒度,但我已经陷入了FWHM。

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))

Corresponding graphs looks likes as in the picture

信息:Scherrer equationFull width at half maximumRelated question

3 个答案:

答案 0 :(得分:3)

这是一个工作示例,假设您具有正态分布。我在Jupyter控制台中运行它,所以如果你不这样做,你必须跳过“魔术线”(%matplotlib notebook)并在最后添加plt.show()

%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')

Example for the FWHM (blue vertical lines) in a normal distribution.

答案 1 :(得分:0)

我很抱歉将此作为答案分享,但评论并未提供我上传图片以澄清问题。所以我用下面的图片说明了问题(顺便说一下,我无法编辑或删除我不情愿写的评论)。 我运行你的代码时得到了这个图表 I got this graph when I run your code

This is the graph what I'm looking for

此图表说明了我正在寻找的内容

答案 2 :(得分:0)

我不知道如何在python中解决这个问题(至少目前为止)。所以我在Matlab中做到了。

    clear all
    clc
    A = dlmread('YOUR DATAS'); %Firstly add to path
    plot(A(:,1),A(:,2)) %Plotting the graph
    hold on

    min_peak = input('Just write a value that is higher than minimum peak values: ');
%This value must be between requested peaks and non-requested peaks (you can see this in graph)
    [yval, yval_i] = findpeaks(A(:,2),'MinPeakHeight',min_peak); %Finding peaks
    scatter(A(yval_i,1), yval); %Showing peaks
    Beta = []; 
    xval = [];
    for k = 1:size(yval_i,1) %Finding x values corresponding to y (peak) values
        xval1 = A(yval_i(k),1);
        xval = [xval xval1];
    end
        Theta = xval / 2; 

    for i = 1:size(yval,1) %Finding half of max. peak values
        yval_i1 = yval_i(i,1);
        while (yval(i,1))/2 < A(yval_i1+1,2)
        yval_i1 = yval_i1+1;
        end

        yval_i2 = yval_i(i,1);
        while (yval(i,1))/2 < A(yval_i2-1,2)
        yval_i2 = yval_i2-1;
        end

        plot(A(yval_i2,1)*ones(size(A(:,2))), A(:,2));
        plot(A(yval_i1,1)*ones(size(A(:,2))), A(:,2));
    %     hold on
    %     scatter(A(yval_i1,1),A(yval_i1,2))
    %     scatter(A(yval_i2,1),A(yval_i2,2))
        B = abs(A(yval_i1,1)-A(yval_i2,1));
        Beta = [Beta B];
    end
    Beta

    K = 0.94;
    Lambda = 1.5406e-10;

    To = [];
    for j = 1:size(Beta,2)
    To1 = (K*Lambda)/(Beta(j)*cos(Theta(j)));
    To = [To To1];
    end
    To = abs(To)