# Stuck in infinite loop [closed]

x0=[0.8, 0.8, 0.2, 0.2]';
m0=[0.5,0.5]';
tol=1e-6;
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);
%the system h evaluated at the intial vector
J=[g1 g2];
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));
%J evaluated at the intial vector
J0=double(J0)
n=size(J0,1); %number of rows in J0
m=size(J0,2); %number of columns in J0
DL=subs(DL, y, x0(2,1));
DL=subs(DL, z, x0(3,1));
DL=subs(DL, p, x0(4,1));
DL=subs(DL, x, x0(1,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);
Y=Q(1:n,1:m);
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 %the new point
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]';
%T evaluated at the new vector x0
m0=r\-(Y'*T0);
qz=Z'*DLd;
h0 = subs(h, {x,y,z,p}, [x0(1,1), x0(2,1), x0(3,1), x0(4,1)]);
h0=double(h0);
qh=h0;
end
newvector=[double(x0); double(m0)];


EDIT: Made some corrections in the code and updated qz and qh inside the loop. However, the code seems to loop forever. the stopping criteria is never violated and x0 continues to change but diverging from the actual solution which is x=(1,1,0,0). I changed the tol to a relatively larger value which is 0.7 and the code generated an output that is near the solution. Could the double precision be causing that problem when the tol=1e-6?

## closed as off-topic by Anton Menshov, sssssssssssss, Mauro Vanzetto, GoHokies, Brian BorchersJan 1 at 20:42

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

• Can you describe what you want and the purpose the code? – nicoguaro Dec 30 '18 at 15:14
• I wish this question was better formatted and more specific about what you're trying to do. – Richard Dec 31 '18 at 2:37

The issue seems similar to the other question you have asked. You are not updating the qz and qh variables inside the loop, so the termination condition of the while loop is never reached.