Jocomol
Jocomol

Reputation: 322

Python not able to calculate a seventh polynomial equation

I have to calculate an equation to translate coordinates in two different fields. With Geogebra I've come up whit this equations which I checked and is correct:

https://i.sstatic.net/StSCs.jpg <-- Link to the equation

The input and output of this equation are as follows:

Input Output
0 6
1 7
2 8
3 11
4 12
5 13
6 16
7 17
8 18

As already stated the equation is correct.

To test this equation I've implemented it into python


import math
import colorful

def polynom(x):
    return 0.0036 * pow(x,7) - 0.1 * pow(x,6) + 1.1 * pow(x,5) - 6 * pow(x,4) + 16.7583 * pow(x,3) - 21.9 * pow(x,2) + 11.1381 * x + 6


# To test the equation with the input and the expected output
for i in range (0,9):
    result = polynom(i)
    if result == exp[i]:
        print(colorful.green(str(i) + " : " + str(result)))
    else:
        print(colorful.red(str(i) + " : " + str(result)))

Strangely this script gives me the following output:

0 : 6.0
1 : 7.0
2 : 8.003399999999996
3 : 11.061599999999942
4 : 12.465999999999973
5 : 15.228000000000364
6 : 23.99099999999936
7 : 40.51839999999771
8 : 77.90159999999871

Whereby only two are correct while the rest give increasingly odd solutions.

Now my questions:

Upvotes: 0

Views: 63

Answers (3)

murkle
murkle

Reputation: 572

I think you've copied the equation from GeoGebra without increasing the Rounding setting. Try with 15 significant figures

Upvotes: 0

Paplusc
Paplusc

Reputation: 1130

The results that python is giving you are correct. What is wrong is your table with input and expected output.

I checked the results of: 0.0036 * xˆ7 - 0.1 * xˆ6 + 1.1 * xˆ5 - 6 * xˆ4 + 16.7583 * xˆ3 - 21.9 * xˆ2 + 11.1381 * x + 6 in wolfram website: equation result link for P(x)=8 and is giving me the same results as your python test does.

I think you need to recalculate your output expectations.

Upvotes: 1

user2261062
user2261062

Reputation:

Having run the equation in Excel, the results look correct. Here is the case for x = 3

So, yes, it looks like the results you have are the expected ones.

enter image description here

Upvotes: 0

Related Questions