# Solving Inverse Kinematics with Gradient Descent

I am trying to implement my own inverse kinematics solver for a robot arm. My solution is a standard iterative one, where at each step, I compute the Jacobian and the pseudo-inverse Jacobian, then compute the Euclidean distance between the end effector and the target, and from these I then compute the next joint angles by following the gradient with respect to the end effector distance.

This achieves a reasonable, smooth path towards the solution. However, during my reading, I have learned that typically, there are in fact multiple solutions, particularly when there are many degrees of freedom. But the gradient descent solution I have implemented only reaches one solution.

So my questions are as follows:

1. How can I compute all the solutions? Can I write down the full forward kinematics equation, set it equal to the desired end effector position, and then solve the linear equations? Or is there a better way?

2. Is there anything of interest about the particular solution that is achieved by using my gradient descent method? For example, is it guaranteed to be the solution that can be reached the fastest by the robot?

3. Are there cases when the gradient descent method will fail? For example, is it possible that it could fall into a local minimum? Or is the function convex, and hence has a single global minimum?

1. You have to analytically compute all IK solutions. This is basically done with straightforward geometry. Most robotics textbooks with a section on manipulation will have a detailed explanation of this. But you might also want to check out the ikfast library.

2. I am not sure if your gradient decent solution is guaranteed to have any special properties. But I think it will probably be the closest solution in joint space except for in some special cases. Note that there may be reasons to choose a different IK solution. My answer to this post has a list of some metrics that you may be interested in: Evaluating the similarity of two 7 Degree of Freedom Arms.

3. Yes, I believe that there are circumstances that can cause gradient decent to fail. (But this probably depends on the degrees of freedom and workspace of your arm). See this thread for more info: Is the geometric inverse problem's solution "continuous" for a redundant robot?.

There are more approaches to solve the inverse kinematics equations.

If you want to continue to use the pseudo-inverse based approach and still obtain more then 1 solution you can flip the sign of joint angle 1 for example (if it is a puma type robot arm) in the initial guess and run the iterative solver again. Same for joint angle 3. You can have situations where it will not converge (e.g. out of reach, singular, etc.)

There are however method which analytically calculate all angles. These should also be faster then the iterative method. These are based on geometrically obtaining a formula for your joint angles and applying that formula once, without any iterations.