finding an equation of polynom by given point in 3D (python)

493 views Asked by At

Suppose we have a curve which is given by 3D points, for example:

(0.43, 0.55, 32.49), (0.61, 0.77, 31.24), (0.77, 1.01, 29.99), (0.88, 1.23, 28.75), (0.93, 1.41, 27.5), (0.91, 1.51, 26.25), (0.90, 1.59, 25), (0.81, 1.60, 23.75), (0.68, 1.58, 22.5), (0.46, 1.52, 21.25)

We try to figure out if there is a way to find the equation of the curve (it's coefficients and independent variable) by code in python, in a way that we can continue this curve to wherever we want? We had a few ideas to get the desired result by a projection to 2d with PCA and then taking a regression, but the result was poor

1

There are 1 answers

0
MangoNrFive On

One way is to fit two sets of coordinates independently with one axis as the base. You can find the polynomial coefficients in x_from_z and y_from_z.

One caveat though: This works for this example, but may not work for other coordinates. For other examples, you may have to play around with the axis, that you use as a base (only z worked in this case), and the degree of the polynom for the polyfit-function (3 worked, but 4 has a better fit in this case). In some cases it may not work at all.

import matplotlib.pyplot as plt
import numpy as np


points = [
    (0.43, 0.55, 32.49),
    (0.61, 0.77, 31.24),
    (0.77, 1.01, 29.99),
    (0.88, 1.23, 28.75),
    (0.93, 1.41, 27.5),
    (0.91, 1.51, 26.25),
    (0.90, 1.59, 25),
    (0.81, 1.60, 23.75),
    (0.68, 1.58, 22.5),
    (0.46, 1.52, 21.25)
    ]

x, y, z = zip(*points)

x_from_z = np.poly1d(np.polyfit(z, x, 4))
y_from_z = np.poly1d(np.polyfit(z, y, 4))

z_new = np.linspace(20, 35, 100)
x_new = x_from_z(z_new)
y_new = y_from_z(z_new)


fig = plt.figure(figsize=(12, 12))
ax = fig.add_subplot(projection='3d')

ax.scatter(x, y, z)
ax.plot(x_new, y_new, z_new, c="k")

plt.xlabel("x")
plt.ylabel("y")

plt.show()

enter image description here