Need some help with a while loop
Show older comments
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 Comments
madhan ravi
on 30 Dec 2018
Edited: madhan ravi
on 30 Dec 2018
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
on 30 Dec 2018
Accepted Answer
More Answers (0)
Categories
Find more on Code Performance in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!