how i can solve the infinit loop
2 views (last 30 days)
Show older comments
tasneem bawaneh
on 1 Jan 2021
Commented: tasneem bawaneh
on 1 Jan 2021
when i try to run this code it go to infint loop how i can fix it
clc; clear
% Set stopping tolerance
eps=0.0001;
syms X1 X2 X3 X4 X5 X6 X7 U1 U2
g1=( 6.25-5*cos(-X1)+15*sin(-X1)-1.24*cos(-X2)+3.75*sin(-X2))-X7;
g2=(10.83-5*cos(X1)+15*sin(X1)-1.6667*cos(X1-X2)+5*sin(X1-X2)-1.6667*X5*cos(X1-X3)+...
5*X5*sin(X1-X3)-2.5*X6*cos(X1-X4)+7.5*X6*sin(X1-X4))-0.68674+0.2;
g3=(12.916-1.25*cos(X2)+3.75*sin(X2)-1.6667*cos(X2-X1)+5*sin(X2-X1)-...
10*X5*cos(X2-X3)+30*X5*sin(X2-X3))-0.52151+0.45;
g4=(-1.6667*X5*cos(X3-X1)+5*X5*sin(X3-X1)-10*X5*cos(X3-X2)+30*X5*sin(X3-X2)+...
12.916*X5^2-1.25*X6*X5*cos(X3-X4)+3.75*X6*X5*sin(X3-X4))+0.4;
g5=(-2.5*X6*cos(X4-X1)+7.5*X6*sin(X4-X1)-1.25*X6*X5*cos(X4-X3)+...
3.75*X6*X5*sin(X4-X3)+3.75*X6^2)+0.6;
g6=(-5*X5*cos(X3-X1)-1.6667*X5*sin(X3-X1)-30*X5*cos(X3-X2)-10*X5*sin(X3-X2)+38.75*X5^2-...
3.75*X6*X5*cos(X3-X4)-1.25*X6*X5*sin(X3-X4))+0.05;
g7=(-7.5*X6*cos(X4-X2)-2.5*X6*sin(X4-X2)-3.75*X6*X5*cos(X4-X4)-1.25*X6*X5*sin(X4-X3)+...
11.25*X6^2)+0.1;
J1=diff(g1,X1);
J2=diff(g1,X2);
J3=diff(g1,X3);
J4=diff(g1,X4);
J5=diff(g1,X5);
J6=diff(g1,X6);
J7=diff(g1,X7);
J8=diff(g2,X1);
J9=diff(g2,X2);
J10= diff(g2,X3);
J11=diff(g2,X4);
J12=diff(g2,X5);
J13=diff(g2,X6);
J14=diff(g2,X7);
J15= diff(g3,X1);
J16=diff(g3,X2);
J17=diff(g3,X3);
J18=diff(g3,X4);
J19=diff(g3,X5);
J20=diff(g3,X6);
J21=diff(g3,X7);
J22= diff(g4,X1);
J23=diff(g4,X2);
J24=diff(g4,X3);
J25=diff(g4,X4);
J26=diff(g4,X5);
J27=diff(g4,X6);
J28=diff(g4,X7);
J29= diff(g5,X1);
J30=diff(g5,X2);
J31=diff(g5,X3);
J32=diff(g5,X4);
J33=diff(g5,X5);
J34=diff(g5,X6);
J35=diff(g5,X7);
J36= diff(g6,X1);
J37=diff(g6,X2);
J38=diff(g6,X3);
J39=diff(g6,X4);
J40=diff(g6,X5);
J41=diff(g6,X6);
J42=diff(g6,X7);
J43=diff(g7,X1);
J44=diff(g7,X2);
J45=diff(g7,X3);
J46=diff(g7,X4);
J47=diff(g7,X5);
J48=diff(g7,X6);
J49=diff(g7,X7);
J=[J1 J2 J3 J4 J5 J6 J7;
J8 J9 J10 J11 J12 J13 J14;
J15 J16 J17 J18 J19 J20 J21;
J22 J23 J24 J25 J26 J27 J28;
J29 J30 J31 J32 J33 J34 J35 ;
J36 J37 J38 J39 J40 J41 J42;
J43 J44 J45 J46 J47 J48 J49];
X1=0;X2=0;X3=0;X4=0;X5=1;X6=1;X7=0 ;U1=0.68674;U2=0.52151; delf=100;
while abs(delf) > eps
% Solve the power flow equations to get update on x1 and x2
J1=double(subs(J));
b1=double(subs(g1));
b2=double(subs(g2));
b3=double(subs(g3));
b4=double(subs(g4));
b5=double(subs(g5));
b6=double(subs(g6));
b7=double(subs(g7));
b=[b1;b2;b3;b4;b5;b6;b7];
maxb=max(b);
while maxb > 0.001
delx=-1*inv(J1)*b;
new=[X1;X2;X3;X4;X5;X6;X7]+delx
X1=new(1); X2=new(2);
X3=new(3); X4=new(4);
X5=new(5);
X6=new(6); X7=new(7);
b1=double(subs(g1));
b2=double(subs(g2));
b3=double(subs(g3));
b4=double(subs(g4));
b5=double(subs(g5));
b6=double(subs(g6));
b7=double(subs(g7));
b=[b1;b2;b3;b4;b5;b6;b7];
maxb=max(b);
end
% Compute Lambda
lam=-1*inv(J1')*[0;0;0;0;0;0;200+120*X7];
% Compute steepest ascent direction
delf1=150+150*U1+[0;-1;0;0;0;0;0]'*lam;
delf2=180+140*U2+[0;0;-1;0;0;0;0]'*lam;
% Compute the new value of the control
unew1=U1-0.004*delf1;
unew2=U2-0.004*delf2;
u=[unew1;unew2];
end
X1,X2,X3,X4,X5,X6,X7,U1,U2
0 Comments
Accepted Answer
Cris LaPierre
on 1 Jan 2021
You get an infinite loop when the criteria for your while loop is always true. Here, I suspect the issue is that you never change the value of delf or eps. Since 100 is always > 0.0001, your outer while loop in infinite.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!