Creating and calculating polynomials¶
This demo covers basic operations with polynomials: creating, calculating, and finding roots.
To work with polynomials - their creation, calculation, finding roots and mathematical operations on them in Engee you can use the library Polynomials.jl
. Let's download and connect it for this example.
import Pkg
Pkg.add("Polynomials")
using Polynomials;
Creating a polynomial¶
In order to create a polynomial of the form
$4 \cdot x^2 - 4 \cdot x + 1$ we will use the function Polynomial()
, to which we pass the vector of its coefficients, starting from the free term.
Polynomial([1, -4, 4])
You can also create a polynomial by passing a variable of coefficients to the function - as a vector or matrix $1 \times (n+1)$. The result of the function (polynomial) can be passed to the variable for further operations.
p = [4 0 0 -3 2 33];
y = Polynomial(p)
The function of creating a polynomial can be calculated element by element. The result of such a call will be a vector of polynomials, and a vector consisting of vectors of coefficients will have to be passed to the function.
P1 = [1, 2, 3, 0, 1];
P2 = [1, 0, -1];
P3 = [0, -1, 4, -2];
P = [P1, P2, P3];
Y = Polynomial.(P)
By default, the polynomial variable is x
. To change the variable, it is necessary to set it explicitly:
Polynomial([1,2,3], :t)
You can create a polynomial by the values of its roots, using the function fromroots()
. The form of the polynomial assignment $(x-4) \cdot (x-1) \cdot (x-2)$ will be reproduced in the following form.
u = fromroots([4,1,2])
Calculating the polynomial¶
The calculation of polynomials is done using the call notation. Let's calculate the previously created polynomial $y=4-3 \cdot x^3 + 2 \cdot x^4 + 33 \cdot x^5$ at the point $x = 1$.
y(1)
To calculate the polynomial created by the element-by-element call, it is necessary to refer to the element of the created vector. For example, let's calculate the polynomial $Y_2 = -x^2+1$ at the point $x = 0,1$.
Y[2](0.1)
Finding the roots of the polynomial¶
The function roots()
. is used to find the roots of a polynomial. The polynomial must be passed to it - for example, through the create function:
roots(Polynomial([1, 0, -1]))
Let's calculate the roots of the function created earlier using the values of the roots: $u = (x-4) \cdot (x-1) \cdot (x-2)$.
roots(u)
As can be seen from the result of the function calculation, the obtained root values are not equal to the given values, which is explained by the error in calculating floating point numbers.
The result of calculating the function of finding roots can be a vector of complex numbers. For example, let's calculate the roots for the previously given polynomial $y=4-3 \cdot x^3 + 2 \cdot x^4 + 33 \cdot x^5$.
roots(y)
Roots can also be calculated element by element, for a vector of polynomials $Y$:
roots.(Y)
Conclusion¶
In this example we have looked at the basic approaches of basic operations with polynomials: creating, calculating and finding roots. For a more detailed study of the topic and to improve your skills of working with polynomials in Engee, we recommend you to refer to the relevant sections of the documentation: Polynomials.jl Library.