Solving the Inverse Kinematics problem using a neural network
24 views (last 30 days)
Show older comments
Hello, I apologize for disturbing you.
I kindly ask you to help me with the task:
To solve the inverse kinematics of a 3-link planar manipulator, it is necessary to create and train a neural model. The creation and training of the neural model should be carried out using the Matlab package "Neural Net Fitting" (located in the Apps section).
To solve this problem, I built the manipulator's workspace through forward kinematics using Robotics Toolbox (45,000 points, singular points were excluded from the sample).
To train the neural network, I normalized the data of the coordinates of points and angles. But when constructing a given trajectory (circle and others), the network deviates greatly from the trajectory. I tried changing the number of points for generating the workspace (increased and decreased), changed the number of hidden layers and neurons in them, types of activation functions in the layers, but each time the neural network does not follow the trajectory.
I really ask for your help, tell me what I'm doing wrong, where the error has crept in. All the code and the created neural networks are attached to the letter (the code and the neural network were created in the online version of Matlab R2025a).
Thank you in advance.
3 Comments
William Rose
on 5 Aug 2025
Edited: Walter Roberson
on 16 Aug 2025
@po,
Thank you for explaining how you normalize your data. I now suspect that ther normalization which you are doing is not the cause of the dificulty you are experiencing. I say that because I tried solving this problem without normalizing the angles or the positions, and I have also failed to find a good solution, thus far.
You wrote: "You suggest training a neural network not just on the coordinates of the final effector position (x, y, z) and the corresponding angles, but adding the Euler angle to the training sample? Am I understanding you correctly?"
Your understanding is correct, if "Euler angle" means the angle of the third link, relative to the base coordinate system. I call this angle "phi" in my code. This angle is also called the "pose" by some authors. Therefore I sometimes refer to it as the pose in my code comments. The diagram below shows the pose angle, ϕ, and the three joint angles,
. I propose that the inputs to the network should be
, and the outputs, which the network should learn how to predict, are
.

I have never used the deep learning toolbox or the robotics toolbox. When I look at your code, I do not understand it very well. I tried running the three scripts you attached. I got an error in every case.
>> workplace3_dobot__no_singular
Unrecognized function or variable 'Link'.
Error in workplace3_dobot__no_singular (line 11)
L(1) = Link([0, L_1, 43.5, -pi/2]);
>> testing__NN
Unrecognized function or variable 'Link'.
Error in testing__NN (line 10)
L(1) = Link([0, L_1, 43.5, -pi/2]);
>> NN_2layer
Unrecognized function or variable 'coords_normalized'.
Error in NN_2layer (line 9)
x = coords_normalized'; % (3 x N)
When I loaded trained_net.mat or trained_net2.mat, I got a warning error:
>> load('trained_net.mat')
Warning: Variable 'net' originally saved as a network cannot be instantiated as an
object and will be read in as a uint32.
Answers (1)
William Rose
on 5 Aug 2025
@po,
Since I was unable to run your code, I tried making my own code to solve the inverse kinematics problem of a three-link planar manipulator, using a neural network. I wrote one function and three scripts.
Function endPosPose.m implements the forward kinematics equations: given the link lengths and joint angles, it computes the end effector position and pose angle. The inputs to endPosPose() are the link length vector (L1,L2,L3) and the joint angle vector (t1,t2,t3). This function returns a vector containining (xe,ye,phi), which are the end effector position and the pose angle, shown in the diagram in my comment above.
Script threelinkManipulatorForward.m generates training data for the network. It calls function endPosPose.m.
Script trainFFNeuralNet.m defines and trains a feed-forward neural network.
Script predictFFNeuralNet.m uses the trained feed-forward network to predict joint angles needed to move along a desired trajectory. It uses the joint angles from the neural network to compute the end-effector position. It makes a plot and displays statistics about the errors between the actual trajectory and the desired trajectory. This script calls function endPosPose.m.
My attempt to use a neural network to solve the inverse kinematics problem of a three-link planar manipulator has been a failure, thus far. Two figures below illustrate examples of the failure. Each diagram shows the points on the desired trajectory in red, and the actual trajectory points in blue. Green line connect corresponding desired and actual points. In both cases, the desired trajectory consists of 24 equally spaced points on the unit circle.
Figure 1, below: Net1 is used to compute joint angles. The actual trajectory lies on a semicircular arc to the right of the desired circle, and with a radius that is too large.

Figure 2, below: Net4 is used to compute joint angles. The actual trajectory lies on the left half of the unit circle. When the desired pont is in the left-hand plane, the network prediction is not too bad. But when the desired point is in the right hand plane, the network prediction results in an actual position on the oppostite half of the circle.

In both cases above, a network with 50 neurons in one hidden layer was used.
The difference between Net1 and Net4 is that they were trained on different data. The training data for Net1 specified all angles in the range (-pi,+pi). The training data for Net4 specified all angles in the range (0, 2*pi).
I later tried using 100 neurons, instead of 50, in the hidden layer. This caused only a very small improvement. I also tried using two hidden layers, with 50 and 30 neurons. This also caused only a small improvement.
Future ideas:
- Use a non-default activation function.
- Use a non-default training algorithm.
- Use more neurons or more layers or both.
- Train three separate networks simultaneously - one for each of the three joint angles.
The attached pdf document contains more details about my attempt to solve the inverse kinematics problem of a three-link planar manipulator using a neural network.
4 Comments
William Rose
on 16 Aug 2025
@po,
Thank you for the link to the robotics toolbox and the information about using R2025 instead of R2024.
I understand that the Robotics Toolbox to which you refer is NOT the Matlab Robotics System toolbox (which I have, v.24.1). Your script uses functions from a non-Matlab toolbox by Peter Corke called the Robotic Toolbox, latest version is v.10.4. I do not want to install a non-Matlab toolbox to work on this question.
I cannot get the latest matlab (R2025), since I have retired. Therefore I use R2024a.
I have updated the pdf document attached above, to include the analytic solution to the inverse kinematic probelm. I wrote a function that implements the analytic solution to the inverse kinematics problem (attached). Perhaps it will be useful in determining why the neural net solution to the inverse probelm is not working. I am alo attaching a script I wrote to test the inverse kinematics function and demonstrate its use.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!