How can I make my loop works?

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
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...

Sign in to comment.

Answers (1)

Image Analyst
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

Hi, my P0 is
P0 =
3.5378 -2.3325
-2.3325 3.5378
But my loop does not go further to compute others P1 until the criteria convergence is satisfied.
I watched the video, but I still did not find the problem.
Thank you!
Image Analyst
Image Analyst on 29 Sep 2014
Edited: Image Analyst on 29 Sep 2014
And attach your m-file and any data files it reads in with the paper clip icon so people can actually run it and debug it for you.
And you said "should use P0 to find a new P1". So tell me, in that loop, does P0 ever go into any calculation of a new value for P1? Not as far as I can see.
Thank you for your help!
I attached the file to my question. Yes, P0 is a guess matrix used to compute P1. After that I try to use P1 to find another P ... until the convergence criteria is satisfied.
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 28 Sep 2014

Edited:

on 29 Sep 2014

Community Treasure Hunt

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

Start Hunting!