matlab 信号的原子产生,MATLAB随机产生原子结构代码
在計(jì)算材料學(xué)中極少的情況下,我們可能會(huì)需要隨機(jī)產(chǎn)生一個(gè)模擬盒子內(nèi)的原子結(jié)構(gòu),一般用于測(cè)試。
這樣說,其實(shí)只要rand一個(gè)數(shù)組就可以了,但是我們又希望這個(gè)結(jié)構(gòu)又能夠大致滿足一些物理上的限制,而不是純粹數(shù)學(xué)上的隨機(jī),比如原子間距要比較合適。
這里提供的程序就是考慮了原子間距,而產(chǎn)生隨機(jī)結(jié)構(gòu)的。
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
關(guān)于以上程序,有幾點(diǎn)是需要特別說明的:
這里模擬的盒子為正交,并且各軸長度相等的。
如果第四個(gè)參數(shù),即dmax不輸入,默認(rèn)賦boxlength。
最后一行將坐標(biāo)轉(zhuǎn)換成了分?jǐn)?shù)坐標(biāo)。如果需要絕對(duì)坐標(biāo),不除以boxlength就可以了。
在有些情況下,程序似乎不能結(jié)束。這是因?yàn)榈谝粋€(gè)原子的位置產(chǎn)生的不太好,使得在一個(gè)有限的盒子內(nèi),無法產(chǎn)生距離滿足要求的其他原子。這時(shí)可以把程序終止掉,重新運(yùn)行。如果這樣做幾次仍然不行,就要考慮是不是參數(shù)設(shè)置有問題,比如盒子太小,或者原子間距離太大。
總結(jié)
以上是生活随笔為你收集整理的matlab 信号的原子产生,MATLAB随机产生原子结构代码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 北京环球影城什么时候预售?
- 下一篇: php方行图片裁剪为圆形,如何将图片裁成