evaluation of polynomials of order 15 gives unexpected results (OpenCV Coordinates Output Python)

Question:

I’ve created a formula with polynomial regression to calculate a drag point from an input. I tested the formula in a separate file with the input, and the output appears to be working as intended. However, in the actual file, it seems to be outputting tremendously incorrect numbers. My first thought was that the output style for OpenCV was in some odd format for the numbers, but when I printed the values they were just fine. It appears to work just fine with smaller formulas, but gives wild values for my created formula. Any help would be appreciated. Below are the formulae for calculating the x and y with a test input and a correct output, vs what I’m getting in the actual file. Thanks.

def triang_x(x1):
    x2 = (-6.8808613455609384e+005) + (3.2312735687925651e+003 * x1)
            + (-6.0019455279289815e+000 * x1**2) + (1.0786025430222985e-002 * x1**3)
           + (-2.8031333306353576e-005 * x1**4) + (4.7125204049478773e-008 * x1**5)
           + (-3.8733353649642116e-011 * x1**6) + (1.4733899082497896e-014 * x1**7)
           + (-5.1033986030610612e-018 * x1**8) + (2.7445807881521161e-021 * x1**9)
           + (1.4039325861603808e-024 * x1**10) + (-7.8303365296494140e-027 * x1**11)
           + (2.0700235162417034e-029 * x1**12) + (-2.5633522287710555e-032 * x1**13)
           + (1.4236656075804622e-035 * x1**14) + (-2.9388878284829885e-039 * x1**15)
    return x2

def triang_y(y1):
    y2 = ((2.9536073194970668e+004) + (-7.2584981060026985e+002 * y1)
            + (5.9991721893954519e+000 * y1**2) + (-1.4273839368311947e-002 * y1**3)
           + (-6.1205911580247642e-005 * y1**4) + (4.1603526512587676e-007 * y1**5)
           + (-6.9546008738218303e-010 * y1**6) + (-1.1072665851528698e-013 * y1**7)
           + (-6.4446469064614884e-016 * y1**8) + (8.0190196135612936e-018 * y1**9)
           + (-8.8768139841444641e-021 * y1**10) + (-1.3685149110264805e-023 * y1**11)
           + (1.3193560897991867e-026 * y1**12) + (5.4138560249698032e-029 * y1**13)
           + (-9.5141032455036651e-032 * y1**14) + (4.4497796299711634e-035 * y1**15))
    return y2
        
# Test Values are x:1116 y:398
#Correct Output is x:900.892612375319 y:889.0486684303542
#Output received in main is 10415680.385044796 -167144.0485716732
Asked By: phacenet

||

Answers:

The way your question is posed currently, I can’t make heads or tails of it.

I can tell you that your first function has an error: you’re trying to assign a sum of 16 summands but when you break that line, everything following will not be part of that sum. You should have gotten an IndentationError… Unless the code in your question is indented differently from what you actually ran on your own computer. If you indented that just right, you would simply have a bunch of 2-summand additions going off into nowhere because it’s perfectly legal in python to have an expression whose value you don’t assign to anything. To fix that, put parentheses ( and ) around the entire expression on the right-hand side of the assignment.

Your second function looks okay and appears to work as you want it. I can’t reproduce the "wrong values" you state in your question.

Beyond that… instead of writing such a huge python expression, simply use numpy to express the coefficients and the evaluation of the polynomial:

import numpy as np

coeffs_x = np.float64([
    -6.8808613455609384e+005,  # 0
    +3.2312735687925651e+003,  # 1
    -6.0019455279289815e+000,  # 2
    +1.0786025430222985e-002,  # 3
    -2.8031333306353576e-005,  # 4
    +4.7125204049478773e-008,  # 5
    -3.8733353649642116e-011,  # 6
    +1.4733899082497896e-014,  # 7
    -5.1033986030610612e-018,  # 8
    +2.7445807881521161e-021,  # 9
    +1.4039325861603808e-024,  # 10
    -7.8303365296494140e-027,  # 11
    +2.0700235162417034e-029,  # 12
    -2.5633522287710555e-032,  # 13
    +1.4236656075804622e-035,  # 14
    -2.9388878284829885e-039,  # 15
])
triang_x = np.polynomial.Polynomial(coeffs_x)

coeffs_y = np.float64([
    +2.9536073194970668e+004, # 0
    -7.2584981060026985e+002, # 1
    +5.9991721893954519e+000, # 2
    -1.4273839368311947e-002, # 3
    -6.1205911580247642e-005, # 4
    +4.1603526512587676e-007, # 5
    -6.9546008738218303e-010, # 6
    -1.1072665851528698e-013, # 7
    -6.4446469064614884e-016, # 8
    +8.0190196135612936e-018, # 9
    -8.8768139841444641e-021, # 10
    -1.3685149110264805e-023, # 11
    +1.3193560897991867e-026, # 12
    +5.4138560249698032e-029, # 13
    -9.5141032455036651e-032, # 14
    +4.4497796299711634e-035, # 15
])
triang_y = np.polynomial.Polynomial(coeffs_y)

print(triang_x(1116), triang_y(398))
# => 900.8926123741549 889.0486684304415
Answered By: Christoph Rackwitz