given the forward kinematics fk and the given desired end-effector location , inverse kinematics is to find the joint angle in matlab

Here is an example of how to implement inverse kinematics in MATLAB given the forward kinematics and desired end-effector location:

main.m
% Given the forward kinematics function fk(theta1,theta2,...,thetaN)
% and desired end-effector location P_desired

theta_initial = [0; 0; 0]; % initialize joint angles to zero
P_current = fk(theta_initial(1),theta_initial(2),...,theta_initial(N)); % calculate current end-effector location

while norm(P_current - P_desired) > tolerance % tolerance is some small value
    J = jacobian(theta_initial); % calculate Jacobian matrix
    delta_theta = inv(J) * (P_desired - P_current); % calculate change in joint angles
    theta_initial = theta_initial + delta_theta; % update joint angles
    P_current = fk(theta_initial(1),theta_initial(2),...,theta_initial(N)); % calculate current end-effector location
end

joint_angles = theta_initial; % final joint angles
763 chars
15 lines

Note that jacobian() is a function that calculates the Jacobian matrix given the current joint angles. The forward kinematics function fk(theta1,theta2,...,thetaN) takes in the joint angles and returns the corresponding end-effector location. The tolerance value determines how close the final end-effector location needs to be to the desired location.

gistlibby LogSnag