Skip to content

Polynomial

psambit9791 edited this page Dec 10, 2023 · 2 revisions

The Polynomial class provides utilities to manipulate polynomial functions using their coefficients.

For example, in the polynomial function $3x^2 + 8x + 2$, we can perform operations like evaluating at sepcific values of $x$, or calculating the derivative of the polynomial function.

Also, there is an use-case of computing the polynomial function given the $x$ and $y$ coordinates.

This class provides access ot methods which can compute these.

Polynomial Fitting

Given the X-coordinates = $[0.0, 1.0, 2.0, 3.0, 4.0, 5.0]$ and Y-coordinates = $[0.0, 0.8, 0.9, 0.1, -0.8, -1.0]$ and given a degree of the polynomial, we can compute the polynomial function using a least-squares fit.

CODE
double[] x = {0.0, 1.0, 2.0, 3.0,  4.0,  5.0};
double[] y = {0.0, 0.8, 0.9, 0.1, -0.8, -1.0};
int degree = 3;
double[] out = Polynomial.polyfit(x, y, degree);
Output:

$[0.087, -0.813, 1.693, -0.040]$ which corresponds to the polynomial function $0.087x^3 - 0.813x^2 + 1.693x - 0.04$

Polynomial Evaluation

Given a polynomial function, evaluate it at given X-coordinates.

CODE
double[] coeffs = {3.0, 0.0, 1.0};
double[] x = {5.0, 3.2, 1.2};
double[] out = Polynomial.polyval(coeffs, x);
Output:

For the polynomial function $3x^2 + 1$, the Y-coordinates are evaluated to $[76.0 , 31.72, 5.32]$ for X-coordinates $[5.0, 3.2, 1.2]$

Polynomial Derivative

Given a polynomial function, calculate its derivative.

CODE
double[] coeffs = {6, 3, 1, 1};
int deriv_order = 2;
double[] out = Polynomial.polyder(coeffs, deriv_order);
Output:

For the polynomial function $6x^3 + 3x^2 + x + 1$, the $2^{nd}$ order derivative is $[36, 6]$ which corresponds to $36x + 6$

Polynomial Anti-Derivative

Given a polynomial function, calculate its anti-derivative.

CODE
double[] coeffs = {140, 390, 80, 110};
double[] constants = {17, 34};
int anti_deriv_order = 2;
double[] out = Polynomial.polyint(coeffs, deriv_order, constants);
Output:

For the polynomial function $140x^3 + 390x^2 + 80x + 110$, the $2^{nd}$ order anti-derivative is $[7.0, 32.5, 13.33, 55, 17, 34]$ which corresponds to $7x^5 + 32.5x^4 + 13.33x^3 + 55x^2 + 17x + 34$

Clone this wiki locally