Home > Mobile >  How to get the coefficients of the polynomial in python
How to get the coefficients of the polynomial in python

Time:09-17

I need to create a function get_polynom that will take a list of tuples (x1, y1), (x2, y2), ..., (xn, yn) representing points and find the coefficients of the polynomial c0, c1, ..., cn.

I can't manage to understand the task, the only tip I have is the provided part of the function:

import numpy as np


def get_polynom(coords):
    ...
    return np.linalg.solve(a, b)

Have somebody done something similar? Just a little explanation of what exactly is expected would be great!

Thanks in advance!

CodePudding user response:

A polynomial is a function f(x) = cn x^n ... c1 x c0. With the pair of tuples you get n 1 equations of the form f(xi) = yi for i going from 1 to n 1. If you substitute xi and yi in the first equation, you obtain a linear system of n equations, with the unknowns cn to c0. Writing this in matrix form, you get A*C = B.

The a argument of the np.linalg.solve represents the A matrix, in this case the "weights" of the polynomial coefficients - that is the powers of x (e.g. one row will be [xi^n, xi^(n-1), ..., xi, 1]). The b argument will be the vector of yi.

Note that if your polynomial is of degree n, you need n 1 tuples to solve for its coefficients.

  • Related