Jacobian and Newton Rhapson Matrix running infintely
Show older comments
I have this code that I am trying to use to find what the values of theta 4, 5, 6, and length 3 are by estimating using the loops below, I based the code off of an example piece of code, but when it runs it doesn't stop, I thought there could be an issue with my while loop, but I haven't been able to see it
syms the4 the5 the6 L3
f1 = 1.8*cos(the6)+2.69*cos(the5)-1.36*cos(the4)-2.1023;
f2 = 1.8*sin(the6)+2.69*sin(the5)-1.36*sin(the4)-3.2373;
f3 = 0.6365+L3*cos(the4);
f4 = L3*sin(the4)-0.6907; %setting up eqs
J = [diff(f1,the4), diff(f1,the5), diff(f1, the6), diff(f1, L3)
diff(f2,the4), diff(f2,the5), diff(f2, the6), diff(f2, L3)
diff(f3,the4), diff(f3,the5), diff(f3, the6), diff(f3, L3)
diff(f4,the4), diff(f4,the5), diff(f4, the6), diff(f4, L3)]; %finding jacobian
F = [f1;f2;f3;f4];
X = [the4,the5,the6,L3]'; %setting up the X, also variables to be solved
X_new = X-J^-1*F; %Newtown Rhapson equation
the40 = (3*pi)/4; %initial guesses
the50 = pi/3;
the60 = pi/2;
L30 = 1;
epsilon = 1e-10; %used to stop code from running infinitely, gets us close enough to answer
iter_count = 0;
iter_max = 100;
Fvalue = subs(F,{the4,the5,the6,L3},{the40,the50,the60,L30}); %replaces the old x with new x in F
while(iter_count<=iter_max)
iter_count = iter_count+1;
X_new_value = subs(X_new, [the4,the5,the6,L3],[the40,the50,the60,L30]) %continues to replace x values, closer to approximate
the40 = X_new_value(1);
the50 = X_new_value(2);
the60 = X_new_value(3);
L30 = X_new_value(4);
Fvalue = subs(F,[the4,the5,the6,L3],[the40,the50,the60,L30]);
Fvalue = double(Fvalue);
end
if iter_count>iter_max
disp('No solutions are found');
else
disp(['theta_4 = ',num2str(double(theta_40))])
disp(['theta_5 = ',num2str(double(theta_50))])
disp(['theta_6 = ',num2str(double(theta_60))])
disp(['L3 = ' ,num2str(double(L30))])
end
1 Comment
KALYAN ACHARJYA
on 15 Feb 2021
Sir, I don't think it is running infintely, but it takes time. please check code is taking high time at iter=4
iter_count =
1
Elapsed time is 0.052071 seconds.
iter_count =
2
Elapsed time is 0.150448 seconds.
iter_count =
3
Elapsed time is 2.922127 seconds.
iter_count =
4
.......
Sure, the iteration will be terminated at iter_max. Better to focus on improving the code or change in imlementation, so that it can takes less time.
Answers (1)
Alan Stevens
on 15 Feb 2021
Runs much quicker without symbolics (the Jacobian is easy to obtain by hand). Like so
F = @(X) [1.8*cos(X(3))+2.69*cos(X(2))-1.36*cos(X(1))-2.1023;
1.8*sin(X(3))+2.69*sin(X(2))-1.36*sin(X(1))-3.2373;
0.6365+X(4)*cos(X(1));
X(4)*sin(X(1))-0.6907]; %setting up eqs
J = @(X) [1.36*sin(X(1)), -2.69*sin(X(2)), -1.8*sin(X(3)), 0;
-1.36*cos(X(1)), 2.69*cos(X(2)), 1.8*cos(X(3)), 0;
-X(4)*sin(X(1)), 0, 0, cos(X(1));
X(4)*cos(X(1)), 0, 0, sin(X(1))]; % Jacobian
the40 = (3*pi)/4; %initial guesses
the50 = pi/3;
the60 = pi/2;
L30 = 1;
X = [the40; the50; the60; L30];
epsilon = 1e-10; %used to stop code from running infinitely, gets us close enough to answer
iter_count = 0;
iter_max = 100;
error = 1;
while iter_count<=iter_max && error>epsilon
iter_count = iter_count+1;
X0 = X;
X = X0 - J(X0)\F(X0);
error = max(abs(X-X0));
end
if iter_count>iter_max
disp('No solutions are found');
else
disp(['theta_4 = ',num2str(X(1))])
disp(['theta_5 = ',num2str(X(2))])
disp(['theta_6 = ',num2str(X(3))])
disp(['L3 = ' ,num2str(X(4))])
end
Categories
Find more on Switches and Breakers 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!