define a scalar function f to transform the following ik problem to a root-finding problem. in matlab

Suppose we have an inverse kinematics problem, i.e., we want to determine the joint angles of a robotic arm to reach a desired end-effector position and orientation. We can formulate this as finding the roots of a function that measures the error between the desired end-effector configuration and the actual one achieved by a specified set of joint angles.

We can define a function f that takes the joint angles q as input and returns a vector of errors e = f(q) representing the differences between the desired pose and the actual pose, in the x, y, z, and orientation components. For instance, considering a cartesian end-effector position and a quaternion-based orientation, we can write:

main.m
function e = f(q)
    % compute forward kinematics to obtain the end-effector pose    
    T = forward_kinematics(q);
    % extract position and orientation components
    p = T(1:3,4);
    q = quaternion(T(1:3,1:3));
    % compute error with respect to desired pose
    ep = pd - p;
    eq = quatmultiply(qd, quatinv(q));
    % concatenate position and orientation errors
    e = [ep; eq'];
end
396 chars
13 lines

Here, qd and pd denote the desired position and orientation, respectively, and forward_kinematics(q) is a function that computes the forward kinematics of the robotic arm based on the joint angles q.

Once we have defined the function f, we can use any root-finding or optimization algorithm in MATLAB to solve for the joint angles that minimize the error. For example, we can use the fminsearch function to find the minimum of the norm squared of the error vector:

main.m
q0 = [0, 0, 0, 0, 0, 0]; % initial guess for joint angles
q = fminsearch(@(q) norm(f(q))^2, q0);
97 chars
3 lines

Here, fminsearch takes as input an anonymous function that computes the norm squared of the error vector for a given set of joint angles q, and returns the optimal set of joint angles q that minimizes this quantity.

gistlibby LogSnag