matlab 信号的原子产生,MATLAB随机产生原子结构代码
在計算材料學中極少的情況下,我們可能會需要隨機產生一個模擬盒子內的原子結構,一般用于測試。
這樣說,其實只要rand一個數組就可以了,但是我們又希望這個結構又能夠大致滿足一些物理上的限制,而不是純粹數學上的隨機,比如原子間距要比較合適。
這里提供的程序就是考慮了原子間距,而產生隨機結構的。
function randStructure(numAtom,boxlength,dmin,dmax)
% Geanerate a structure randomly with the distance in specified range
%
% randStructure(numAtom,boxlength,dmin,dmax)
% numAtom: number of atoms in the structure.
% boxlength: length of the simulation box
% dmin: minimum of the distance between two atoms
% dmax: [optional] maximum of the distance between two atoms
%
% Recommendation:
%It's better to set dmax equaling to boxlength.
%
% Example:
% randStructure(10,4,2,5)
%
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% log:
% 2012-12-10: Complete
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if nargin == 3
dmax = boxlength;
end
%function randStructure()
%numAtom = input('Number of atoms:');
%boxlength = input('Scale factor:');
%dmin = input('minimum of the distance between two atoms:');
%dmax = input('maximum of the distance between two atoms:');
coord = rand(1,3)*boxlength; % the 1st atoms
n = 1; % number of atoms
while n < numAtom
tcoord = rand(1,3)*boxlength;
nlen = size(coord,1);
counter = 0;
for i=1:nlen
tdist = norm(coord(i,:)-tcoord);
if tdist >= dmin && tdist <= dmax
counter = counter + 1;
end
end
if counter == nlen
coord(end+1,:) = tcoord;
n = n + 1;
end
end
coord/boxlength
關于以上程序,有幾點是需要特別說明的:
這里模擬的盒子為正交,并且各軸長度相等的。
如果第四個參數,即dmax不輸入,默認賦boxlength。
最后一行將坐標轉換成了分數坐標。如果需要絕對坐標,不除以boxlength就可以了。
在有些情況下,程序似乎不能結束。這是因為第一個原子的位置產生的不太好,使得在一個有限的盒子內,無法產生距離滿足要求的其他原子。這時可以把程序終止掉,重新運行。如果這樣做幾次仍然不行,就要考慮是不是參數設置有問題,比如盒子太小,或者原子間距離太大。
總結
以上是生活随笔為你收集整理的matlab 信号的原子产生,MATLAB随机产生原子结构代码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京环球影城什么时候预售?
- 下一篇: php方行图片裁剪为圆形,如何将图片裁成