loop and if conditions for calculating average

3 ビュー (過去 30 日間)
Tamir Suliman
Tamir Suliman 2016 年 11 月 29 日
編集済み: bio lim 2016 年 11 月 30 日
I was trying to use if condition in the program below to assign all values of deltaE that are less than zero to Ev adn their corresponding E values then print out the acceptance counter It doesnt work can any one review and update the program if possible ? please check the algorithm below
clc,clear
format shortG
E=0;accept=0;
step=1;
xv=0;
m_trials=100;
counter=1;
xi=100; %set at each temperature the initial coordinate value x to 100.
kc=0.1;kb=1; %Constants
dx=10; %Set maximum positiondisplacement during MC move
Temp=0;
% while (counter < m_trials )
for counter=1:m_trials
counter=counter+1;
d(counter+1)=(rand()-0.5)*dx; %For each particle picking a random displacement
xv(counter+1)=d(counter)+xi;
E(counter+1)=kc*xv(counter).^2
deltaE(counter+1)=E(counter+1)-E(counter)
Temp=Temp+0.1;
if deltaE<0
Ev=deltaE
p = exp(-deltaE/Temp)
disp('Accept the new configuration')
accept = accept+1
break;
else
disp('Rject the new Configuration')
end
step=step+1;
E=sum(E)/m_trials
end

回答 (1 件)

bio lim
bio lim 2016 年 11 月 29 日
編集済み: bio lim 2016 年 11 月 29 日
First of all, you are missing end to your for loop. Also, you don't need break in your if statement.
Fixing those two, I am getting output of:
Rject the new Configuration
E =
10.644
  2 件のコメント
Tamir Suliman
Tamir Suliman 2016 年 11 月 29 日
Yes but the condition hasnt changed would like to store the values of Deltas and Es in to the new variables then displaying those values after iterating through all the elements
bio lim
bio lim 2016 年 11 月 30 日
編集済み: bio lim 2016 年 11 月 30 日
Correct me if I'm wrong, but I am assuming you are talking about the two variables DeltaE and E.
If you take a look at:
E=sum(E)/m_trials
it is defined inside the loop. Which means, at every iteration, i.e.,
for counter=1:m_trials
the value for E is going to be changed. As for DeltaE, I see no problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by