How can I make my loop works?

1 次查看(过去 30 天)
Anderson
Anderson 2014-9-28
编辑: Stephen23 2014-9-29
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 个评论
Stephen23
Stephen23 2014-9-29
编辑:Stephen23 2014-9-29
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...

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2014-9-28
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 个评论
Anderson
Anderson 2014-9-29
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.
Iain
Iain 2014-9-29
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by