Need some help with a while loop

조회 수: 1 (최근 30일)
Cantor Set
Cantor Set 2018년 12월 30일
편집: Stephen23 2018년 12월 30일
x0=[0.8 0.8 0.2 0.2]';
tol=1e-6;
m0=[0.5 0.5]';
syms x y z p m1 m2
h1=y-(x^3)-(z^2);
h2=(x^2)-y-p^2;
h=[h1 h2]';
f=-x;
L=f+m1*h1+m2*h2;
h0=subs(h, {x,y,z,p}, [x0(1,1) ,x0(2,1) ,x0(3,1) ,x0(4,1)]);
h0=double(h0);
g1=gradient(h1, [x,y,z,p]);
g2=gradient(h2, [x,y,z,p]);
J=[g1 g2]';
J0=subs(J, {x,y,z,p}, [x0(1,1) ,x0(2,1) ,x0(3,1), x0(4,1)]);
J0=double(J0);
n=size(J0,1);
m=size(J0,2);
DL=gradient(L, [x,y,z,p,m1,m2]);
DL=subs(DL, x, x0(1,1));
DL=subs(DL,y,x0(2,1));
DL=subs(DL,z,x0(3,1));
DL=subs(DL,p,x0(4,1));
DL=subs(DL,m1, m0(1,1));
DL=subs(DL,m2,m0(2,1));
DL=double(DL);
DLd=DL(1:n,1);
H=hessian(L,[x,y,z,p]);
H0=subs(H,x,x0(1,1));
H0=subs(H0,y,x0(2,1));
H0=subs(H0,z,x0(3,1));
H0=subs(H0,p,x0(4,1));
H0=subs(H0,m1,m0(1,1));
H0=subs(H0,m2,m0(2,1));
H0=double(H0);
[Q,R]=qr(J0);
Z=Q(1:n,m+1:n);
qz=Z'*DLd;
qh=h0;
while norm(qz)+norm(qh)>tol
E=[Z'*H0; J0'];
V=[Z'*DLd; h0];
s0=E\-V;
x0=x0+s0;
J0=subs(J,x,x0(1,1));
J0=subs(J0,y,x0(2,1));
J0=subs(J0,z,x0(3,1));
J0=subs(J0,p,x0(4,1));
J0=double(J0);
[Q, R]=qr(J0);
Y=Q(1:n,1:m);
Z=Q(1:n,m+1:n);
r=R(1:m, 1:m);
T0=[-1 0 0 0 ]';
m0=r\-(Y'*T0);
qz;
qh;
end
newvect=[double(x0); double(m0)]
MATLAB does't return any errors, however, the code seems to loop infinitely many times I guess. I have no clue why?
Is there anything wrong with the code??
  댓글 수: 2
madhan ravi
madhan ravi 2018년 12월 30일
편집: madhan ravi 2018년 12월 30일
I don't even see any loop , I maybe blind but there lot of bugs
m=size(J0,2):
^--should be ;
Q is 2 X 2 but you try access until 4 columns (m is 4)
Cantor Set
Cantor Set 2018년 12월 30일
Hahah. Sorry I edited it. I don't know why it is not an option to copy the code from the MATLAB editor and just paste it here :/

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2018년 12월 30일
편집: Stephen23 2018년 12월 30일
"however, the code seems to loop infinitely many times I guess. I have no clue why? Is there anything wrong with the code??"
Your loop condition depends on three variables: qz, qh and tol. You don't change any of those variables inside the loop. So if the loops starts you will never exit from the loop because you do not change any of those three variables inside the loop and so the condition will remain exactly as it was when you first entered the loop.
  댓글 수: 2
Cantor Set
Cantor Set 2018년 12월 30일
The loop depends on qz, qh and tol.
but then qz depends on Z' and DLd. But then Z' depends on Z which depends on Q which depends on J0 which depends on x0 and x0 changes inside the loop so qz changes inside the loop.
qh depends on h0 and h0 depeds on x0. And x0 changes inside the loop so is qh.
Stephen23
Stephen23 2018년 12월 30일
편집: Stephen23 2018년 12월 30일
"x0 changes inside the loop so qz changes inside the loop."
"qh depends on h0 and h0 depeds on x0. And x0 changes inside the loop so is qh."
Nope. Because MATLAB is entirely pass by value, if you do not change the values of qx, qh, or tol inside the loop then their values will not magically change just because some other variable changes. Of course this is easy to check yourself: simply display their values on each iteration and you will see that qz and qh do not change.
You seem to be writing code based on rules of another language. This will not help you to understand MATLAB.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by