clear;
clc;
% Parameters
L1 = 1; % Length of link 1
L2 = 1; % Length of link 2
dt = 0.01; % Time step size
t = 0:dt:1; % Time vector
% Initial joint angles
theta1 = 0;
theta2 = pi;
% Preallocate arrays for incremental changes in joint angles
dTheta1 = zeros(1, length(t)-1);
dTheta2 = zeros(1, length(t)-1);
% Initial end-effector position
x_init = L1 * cos(theta1) + L2 * cos(theta1 + theta2);
y_init = L1 * sin(theta1) + L2 * sin(theta1 + theta2);
% Final end-effector position
x_final = 1;
y_final = 1;
% Total incremental steps in x and y directions
deltaX_total = x_final - x_init;
deltaY_total = y_final - y_init;
deltaX_step = deltaX_total / (length(t) - 1);
deltaY_step = deltaY_total / (length(t) - 1);
% Initialize end-effector position
x = x_init;
y = y_init;
% Loop to compute incremental joint angles for each time step
for i = 1:length(t)-1
% Update end-effector position incrementally
x = x + deltaX_step;
y = y + deltaY_step;
% Compute Jacobian matrix
J = [-L1*sin(theta1) - L2*sin(theta1 + theta2), -L2*sin(theta1 + theta2);
L1*cos(theta1) + L2*cos(theta1 + theta2), L2*cos(theta1 +
theta2)];
% Define the position change vector
deltaXY = [deltaX_step; deltaY_step];
% Solve for deltaTheta without calculating the inverse
deltaTheta = J \ deltaXY;
% Store incremental joint angle changes
dTheta1(i) = deltaTheta(1);
dTheta2(i) = deltaTheta(2);
% Update joint angles for the next iteration
theta1 = theta1 + dTheta1(i);
theta2 = theta2 + dTheta2(i);
end
% Store results in a matrix and display them
dTheta = [dTheta1; dTheta2];
disp('Delta theta values (dTheta1 and dTheta2) for each step:');
disp(dTheta);
Result:
Warning: Matrix is close to singular or badly scaled. Results may be
inaccurate. RCOND = 1.224647e-16.
> In matlabAssignment2c (line 50)
Delta theta values (dTheta1 and dTheta2) for each step:
1.0e+13 *
Columns 1 through 10
-8.1656 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
-0.0000 0.0000 0.0000 -0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 11 through 20
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 21 through 30
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 31 through 40
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 41 through 50
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 51 through 60
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 61 through 70
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 71 through 80
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 81 through 90
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
Columns 91 through 100
-0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -0.0000 -
0.0000 -0.0000 -0.0000
0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
0.0000 0.0000 0.0000
>>