Info

This question is closed. Reopen it to edit or answer.

I am trying to do an iteration loop, but it repeats iteration 2. What happens???

1 view (last 30 days)
The problem should be in the if loop in the middle.
clc
clear all
format long
number_of_sites = 3;
r1 = rand(number_of_sites, 1);
sumr=sum(r1);
r2=r1./sumr;
x = [0:1:100];
y(1) = 1;
i = 1;
size(x);
J=[ 0.006 45.24; 0.008 43.96; 0.016 40.48; 0.023 38.81; 0.031 37.74; 0.039 37.17; 0.051 36.78; 0.072 36.64; 0.098 36.71];
energy=[1: 1: 100]'*10^-3;
am=5.5*10^-19;
sigma=10^-4;
macroscopicenergies = [30 40 50]*10^-3;
number_of_probes=5;
probes_energy = [10 20 30 40 50]'*10^-3 ;
[alpha, probes]=size(probes_energy);
kb=1.3806488*10^-23;
T=300;
q1=exp((2*am*((probes_energy(1,:).*energy).^0.5)/(kb*T)));
q2=exp((2*am*((probes_energy(2,:).*energy).^0.5)/(kb*T)));
q3=exp((2*am*((probes_energy(3,:).*energy).^0.5)/(kb*T)));
q4=exp((2*am*((probes_energy(4,:).*energy).^0.5)/(kb*T)));
q5=exp((2*am*((probes_energy(5,:).*energy).^0.5)/(kb*T)));
sumq1=sum(q1);
sumq2=sum(q2);
sumq3=sum(q3);
sumq4=sum(q4);
sumq5=sum(q5);
qq1=q1./sumq1;
qq2=q2./sumq2;
qq3=q3./sumq3;
qq4=q4./sumq4;
qq5=q5./sumq5;
while (i<max(size(x)));
dp0=10^-3;
n=[1:1:i];
if i==1;
sf1(i,:)=(((r2(1))/(sigma*((2*pi)^0.5)))*exp(-0.5*(((energy-macroscopicenergies(1))/sigma).^2)));
else
if i>=2
sf1(i,:)=((((r2(1))/(sigma*((2*pi)^0.5)))*exp(-0.5*(((energy-macroscopicenergies(1))/sigma).^2)))-sssa(:,i-1));
end
end
f1=sf1';
f1q1(:,i)=(f1(:,i)).*qq1;
f1q2(:,i)=(f1(:,i)).*qq2;
f1q3(:,i)=(f1(:,i)).*qq3;
f1q4(:,i)=(f1(:,i)).*qq4;
f1q5(:,i)=(f1(:,i)).*qq5;
sf1q1(i,:)=sum(f1q1(:,i));
sf1q2(i,:)=sum(f1q2(:,i));
sf1q3(i,:)=sum(f1q3(:,i));
sf1q4(i,:)=sum(f1q4(:,i));
sf1q5(i,:)=sum(f1q5(:,i));
f1q1t(:,i)=((f1q1(:,i))./sf1q1(i,:))*dp0;
f1q2t(:,i)=((f1q2(:,i))./sf1q2(i,:))*dp0;
f1q3t(:,i)=((f1q3(:,i))./sf1q3(i,:))*dp0;
f1q4t(:,i)=((f1q4(:,i))./sf1q4(i,:))*dp0;
f1q5t(:,i)=((f1q5(:,i))./sf1q5(i,:))*dp0;
sPu10(:,i)=0.2*(f1q1t(:,i)+ f1q2t(:,i)+ f1q3t(:,i)+ f1q4t(:,i)+ f1q5t(:,i));
ssPu10=sPu10';
ssa=zeros(100,101);
ssa(:,n)=sum(sPu10(:,i));
sssa=ssa(:,n);
%sa=[sum(Pu10(n,:))]'
i = i + 1;
end
My code is as follow, the problem should arise from the if loop in the middle.

Answers (1)

Image Analyst
Image Analyst on 2 Aug 2014
Put this line right after the while statement and you'll see that iteration 2 does NOT get repeated.
fprintf('Iteration #%d\n', i);
In the command window...
Iteration #1
Iteration #2
Iteration #3
Iteration #4
Iteration #5
Iteration #6
Iteration #7
and so on.
  7 Comments

Community Treasure Hunt

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

Start Hunting!