polynomial equation parameters

ghz 12hours ago ⋅ 1 views

I have some 2D sampling points for which I need the polynomial equation. I only found something like this:

from scipy.interpolate import barycentric_interpolate
// x and y are given as lists
yi = barycentric_interpolate(x, y, xi)

But in this case I can only get the y-values belonging to certain xi-values - that's not what I want. I need the equation (the parameter for the polynomial equation). How can I receive this?

Answer

To obtain the polynomial equation (in the form of coefficients) from a set of 2D sampling points, you can use polynomial interpolation. In this case, the method you want is Lagrange interpolation or Least Squares fitting to fit a polynomial equation to your data points.

Scipy provides tools for both Lagrange interpolation and polynomial fitting. Here’s how you can use these methods to obtain the polynomial coefficients for 2D data:

1. Lagrange Interpolation using scipy.interpolate.lagrange

This method constructs a polynomial that exactly passes through all the given points.

import numpy as np
from scipy.interpolate import lagrange

# Your data points
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])

# Perform Lagrange interpolation
poly = lagrange(x, y)

# Print the polynomial coefficients
print(poly)

# You can access the coefficients (p(x) = a_n*x^n + ... + a_1*x + a_0)
print(poly.coefficients)  # This will show the coefficients of the polynomial

2. Polynomial Fitting using numpy.polyfit

If you are looking for the polynomial coefficients of the best-fit curve (for cases where you might not need to pass through every point exactly), you can use numpy.polyfit, which fits a polynomial of a given degree to your data points.

import numpy as np

# Your data points
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])

# Fit a polynomial of degree 3 (for example, to get a cubic fit)
coefficients = np.polyfit(x, y, 3)

# Print the polynomial coefficients
print("Polynomial coefficients:", coefficients)

# You can create the polynomial from coefficients
polynomial = np.poly1d(coefficients)

# Evaluate the polynomial at any x-value
y_pred = polynomial(2.5)  # Example: Evaluate at x=2.5
print(f"Value at x=2.5: {y_pred}")

3. Polynomial Interpolation (for multiple variables, e.g., f(x, y))

If you have a more complex 2D grid of points and need to fit a 2D polynomial (e.g., for data like f(x, y)), you can use scipy.interpolate.bisplrep or scipy.interpolate.interp2d. However, for purely polynomial-based fitting, you can use np.polyfit in a 2D sense by reshaping your data appropriately.

import numpy as np
import matplotlib.pyplot as plt
from numpy.polynomial.polynomial import Polynomial

# Your data points (for 2D case)
x = np.array([1, 2, 3, 4])
y = np.array([1, 4, 9, 16])

# Fit a 2D polynomial (e.g., degree 2 polynomial)
coeffs = np.polyfit(x, y, 2)  # fit a second-degree polynomial
polynomial = np.poly1d(coeffs)

# Print polynomial
print("Fitted Polynomial:", polynomial)

# Plotting
plt.scatter(x, y, color='red')
plt.plot(x, polynomial(x), label='Fitted Polynomial', color='blue')
plt.legend()
plt.show()

Summary:

  1. Lagrange Interpolation (scipy.interpolate.lagrange) provides an exact polynomial fit, and you can access the coefficients of that polynomial.
  2. Least Squares Fitting (numpy.polyfit) fits a polynomial to data (which might not pass exactly through all points, but minimizes the overall error).
  3. For 2D data interpolation, you can use scipy.interpolate.interp2d for interpolation or numpy.polyfit (in a flattened form) for polynomial fitting.

If you are only interested in finding the exact polynomial equation (with the coefficients) for a given set of points, scipy.interpolate.lagrange or numpy.polyfit should be sufficient. If you need to handle 2D or multi-variable data, you will need to reshape your data or use specialized 2D interpolation methods.