Want to decrease time to get the result

When I tried to run my code, it takes more than a minute. I don't know why it takes much time like that.
Changing the tolerance might be one option.
Can anyone help me with this?
Thanks.
function a_new = manipul(a_initial)
options = optimoptions('fmincon','Display','iter');
A= [1 0 0; 0 1 0; 0 0 1];
B= [15; 15 ; 20];
a_new = fmincon(@rfs, a_initial, A, B,[],[],[],[],[], options);
initialR = SerialLink([0 0 4 0; 0 0 5 0;0 0 8 0], 'name', 'Initial Robot');
L(1) = Link([0 0 a_new(1,1) 0]);
L(2) = Link([0 0 a_new(1,2) 0]);
L(3) = Link([0 0 a_new(1,3) 0]);
newR = SerialLink(L, 'name', 'Optimized Robot');
T_1 = transl([30,20,0]);
T_2 = transl([20,25,0]);
T_3 = transl([27,8,0]);
T_4 = transl([23,5,0]);
T_5 = transl([25,23,0]);
q0 = [ 0 0 0 ];
q_final_1 = newR.ikcon(T_1); % Inverse kinematics solution with given disred position 1
q_final_2 = newR.ikcon(T_2); % Inverse kinematics solution with given disred position 2
q_final_3 = newR.ikcon(T_3); % Inverse kinematics solution with given disred position 3
q_final_4 = newR.ikcon(T_4); % Inverse kinematics solution with given disred position 4
q_final_5 = newR.ikcon(T_5); % Inverse kinematics solution with given disred position 5
t=0:.1:2;
traj1=jtraj(q0,q_final_1,t);
traj2=jtraj(q_final_1,q_final_2,t);
traj3=jtraj(q_final_2,q_final_3,t);
traj4=jtraj(q_final_3,q_final_4,t);
traj5=jtraj(q_final_4,q_final_5,t);
figure(1)
plot(30,20,'b*', 20,25,'b*', 27,8,'b*', 23,5, 'b*', 25,23, 'b*')
initialR.plot(traj1, 'workspace', [-40 40 -40 40 -40 40])
initialR.plot(traj2)
initialR.plot(traj3)
initialR.plot(traj4)
initialR.plot(traj5)
figure(2)
plot(30,20,'b*', 20,25,'b*', 27,8,'b*', 23,5, 'b*', 25,23, 'b*')
newR.plot(traj1)
newR.plot(traj2)
newR.plot(traj3)
newR.plot(traj4)
newR.plot(traj5)
end
function costFun = rfs(a_initial)
L(1) = Link([0 0 a_initial(1,1) 0]);
L(2) = Link([0 0 a_initial(1,2) 0]);
L(3) = Link([0 0 a_initial(1,3) 0]);
R = SerialLink(L,'name', 'My Robot');
% Expressing the desired position in homogeneous matrix
T_1 = transl([15,20,0]);
T_2 = transl([20,25,0]);
T_3 = transl([27,8,0]);
T_4 = transl([23,5,0]);
T_5 = transl([25,3,0]);
q_new_1 = R.ikcon(T_1); % Inverse kinematics solution with given disred position 1
q_new_2 = R.ikcon(T_2); % Inverse kinematics solution with given disred position 2
q_new_3 = R.ikcon(T_3); % Inverse kinematics solution with given disred position 3
q_new_4 = R.ikcon(T_4); % Inverse kinematics solution with given disred position 4
q_new_5 = R.ikcon(T_5); % Inverse kinematics solution with given disred position 5
T_real_1 = R.fkine(q_new_1); % Forward kinematics solution / initial position for the end effector
T_real_2 = R.fkine(q_new_2);
T_real_3 = R.fkine(q_new_3);
T_real_4 = R.fkine(q_new_4);
T_real_5 = R.fkine(q_new_5);
% Error between initial position and 5 desired position
costFun = (T_real_1.t(1,1) - T_1(1,4))^2 + (T_real_1.t(2,1) - T_1(2,4))^2 + (T_real_1.t(3,1) - T_1(3,4))^2 +...
(T_real_2.t(1,1) - T_2(1,4))^2 + (T_real_2.t(2,1) - T_2(2,4))^2 + (T_real_2.t(3,1) - T_2(3,4))^2 + ...
(T_real_3.t(1,1) - T_3(1,4))^2 + (T_real_3.t(2,1) - T_3(2,4))^2 + (T_real_3.t(3,1) - T_3(3,4))^2 + ...
(T_real_4.t(1,1) - T_4(1,4))^2 + (T_real_4.t(2,1) - T_4(2,4))^2 + (T_real_4.t(3,1) - T_4(3,4))^2 + ...
(T_real_5.t(1,1) - T_5(1,4))^2 + (T_real_5.t(2,1) - T_5(2,4))^2 + (T_real_5.t(3,1) - T_5(3,4))^2;
end

回答(0 个)

类别

帮助中心File Exchange 中查找有关 Just for fun 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by