필터 지우기
필터 지우기

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