How can I make my loop works?
1 view (last 30 days)
Show older comments
How can I make my loop works?
I know P0 (my initial guess)
P0 =
3.5378 -2.3325
-2.3325 3.5378
X =
1.2448 -0.4685
-0.4685 1.2448
Hcore =
-1.1204 -0.9583
-0.9583 -1.1204
g(1,1,1,1) = 0.7746 g(2,2,2,2) = 0.7746 g(1,1,2,2) = 0.5697 g(2,2,1,1) = 0.5697 g(2,1,1,1) = 0.4441 g(1,2,1,1) = 0.4441 g(1,1,2,1) = 0.4441 g(1,1,1,2) = 0.4441 g(2,2,2,1) = 0.4441 g(2,2,1,2) = 0.4441 g(2,1,2,2) = 0.4441 g(1,2,2,2) = 0.4441 g(2,1,2,1) = 0.2970 g(1,2,2,1) = 0.2970 g(1,2,1,2) = 0.2970 g(2,1,1,2) = 0.2970
and I am trying to find a P1 when
(2.^-2).*(P1(i,j)-P0(i,j))^2)^(1/2)) < 10^-7.
My loop should use P0 to find a new P1, and then P0 becomes P1, when it tries to find another P1 again...until the criteria is satisfied.
for i=1:2
for j=1:2
while (((2.^-2).*(P1(i,j)-P0(i,j))^2)^(1/2)) < 10^-7 %convergence criteria
for i=1:2
for j=1:2
for k=1,2
for l=1,2
F1 = Hcore + (P1(i,j)).*(g(k,l,j,i) - 0.5.*g(k,i,j,l))
Ft1 = (ctranspose(X))*F1*X
[Ct1, Dt1]=eig(Ft1)
C1 = X*Ct1
P1 = 2.*C1*(ctranspose(C1))
end
end
end
end
end
end
end
1 Comment
Stephen23
on 29 Sep 2014
Edited: Stephen23
on 29 Sep 2014
Your initial value P0 never gets referred to inside the loop. Fix this, and you probably fix the problem.
Note: you should avoid using i and j as the names of variables, as these are the names of the inbuilt imaginary unit . Accidental overloading of inbuilt functions and variables can cause all sorts of hard-to-diagnose problems...
Answers (1)
Image Analyst
on 28 Sep 2014
I don't see P0 mentioned inside your while loop at all. I don't even see where P0 is initialized. But I'm sure this will solve the problem: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
4 Comments
Iain
on 29 Sep 2014
If P0 is setup before the loops start, and you're using the check "(((2.^-2).*(P1(i,j)-P0(i,j))^2)^(1/2)) < 10^-7", then what you're checking for is P1 is being approximately equal to the very original P0, and not the immediately prior P0.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!