How can I make my loop works?
Afficher commentaires plus anciens
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 commentaire
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...
Réponses (1)
Image Analyst
le 28 Sep 2014
0 votes
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 commentaires
Anderson
le 29 Sep 2014
Image Analyst
le 29 Sep 2014
Modifié(e) : Image Analyst
le 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.
Anderson
le 29 Sep 2014
Iain
le 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.
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!