How to find real roots of a 4th degree polynomial with arduino uno?


I am making a robotic arm and the inverse kinematics to find where to move the servos requires solving a 4th degree polynomial with known coefficients. I only need real solutions but do need all the real solutions not just one.

It needs to be pretty accurate but doesn't have to be perfect, so methods that very closely approximate the roots would be fine. Thanks!

Precompute the roots with a CAS and make a lookup table. It will be orders of magnitude quicker.

The coefficients of the polynomial equation depend on variables inputed by the user. Also what is a CAS?

Can you show us this in context?

You may want to use fixed point math.

You could look at Newton's method or other algorithms. Like binary search.

Are the equations known at compile time?


It is a computer algebra system. Something like yacas. Your polynomials will be something like

ax^4 + bx^3 + cx^2 + dx + e = 0

will they not? If any of {b, c, d, e} is guaranteed to always be 0, this can be simplified. The user gives the values of the coefficients and the lookup table spits out the real roots.

Can be solved exactly. Scroll down to "general formula for roots"

Yeah I remember hearing about newton's method in calculus class. Idk how to code it tho, to find all real roots. And if there are no real roots it will just get stuck in an infinite loop no?

Basically I set up a system of equations based on known lengths and positions (the user inputs a desired x and y coordinate). I solved it for the y co ordinate of one part of the arm, which then allows me to know where all the parts of the arm are located in space. If there are multiple real, positive solutions that corresponds to multiple ways to move the arm to the same end position.

Okay thank you! Is there a library that does this cause thats SO MUCH to code lmao. Also a=1 so that makes some of it easier ig.

Also I was using an ESP 32 for the robotic arm at first because it is way faster. However, I thought 3.3v instead of 5v signals was giving the servos issues but i think the problem might have been something else. so i switched to an arduino uno. either way i could probably switch back to the esp32 if i need the additional computational power. sadly i do not have a 3.3v to 5v logic level converter on hand either

Coding the quartic solution builds character, and you will have a warm feeling of accomplishment when done.

And, you can post the result on the Arduino forum, to bask in members' admiration!


ok :slight_smile:

If you want to try run-time computation, maybe this should help.

1 Like



I'm gonna see if my esp32 works with the servos i have, then im gonna try all this stuff, brb

if you don't want to brag about your achievements nor build character you can have a look there too

I was gonna try to get the ESP32 to work with the arduino uno but thats way too much work im gonna get my arduino mega for this brb. Cause the servos hate the 3.3V and dont work with it!

If that equation is a result of trying to position that arm , you might get a better result by using the equations of motion of each arm part and solving those in turn .
If have to keep in mind the accuracy of positioning you will get with the mechanics you use ( repeatability , etc) and some of the values of each term in your equation may have zero affect on the position of the arm and can be eliminated making the job easier .

You can use a simple 3.3V / 5V voltage levelshifter or even some transistors to create a 5V signal from a 3.3V signal.

This website has the formulas

Well quite a lot to code
I guess the numeric approximation using newton will be easier to code
p_4(x) shall be the polynomic function p_4(x) = ax^4 + bx^3 + cx^2 + dx + e
and solutions of ax^4 + bx^3 + cx^2 + dx + e = 0

The basic thing is to calculate the slope of a point
slope m = ( p_4(x+h) - p_4(x) ) / h
and use this slope to get a linear function
l(x) = m x + b where b must be calculated from the given point
and then solve the linear equation mx + b = 0 which gives the next approximation for the next iteration until you have an x-value that results in p_4(x) is near to zero
quite some code bt not that complex as you can devide the whole calculation into many easy to code steps

I was looking up newtons method and it is a lot easier than described above

it boils down to

x_n+1 = x_n - f(x_n) / f ' (x_n)

f ' (x) can be calculated with the polynomrule of differentiation
If there is f(x) = ax^n
f ' (x) = a * n * x^(n-1)

or you could do a numerical approximation for f ' (x) with
f ' (x) = [ f(x + h) - f(x) ]/ h

best regards Stefan