Engee documentation
Notebook

Differentiating and integrating polynomials

This example shows the use of the functions derivative() and integrate() from the Polynomials.jl library to analytically find derivatives and integrals of polynomials.

Let's connect the library Polynomials.jl:

In [ ]:
using Polynomials

Differentiating polynomials

Let's define a polynomial $p(x) = x^3-4x^2+7$

In [ ]:
p = Polynomial([7, 0, -4, 1])
Out[0]:
7 - 4∙x2 + x3

Find the first derivative of the polynomial $q_1(x)=p'(x)=3x^2-8x$:

In [ ]:
q_1 = derivative(p)
Out[0]:
-8∙x + 3∙x2

Find the second derivative of the polynomial $q_2(x) = q_1'(x)= p''(x) = 6x-8$:

In [ ]:
q_2 = derivative(p, 2)
Out[0]:
-8 + 6∙x

Find the derivative of the rational expression $\frac{a(x)}{b(x)}$, where $a(x)$ and $b(x)$ are polynomials:

In [ ]:
a = Polynomial([5, 3, 1]);
b = Polynomial([6, 4, 2]);
ab = a // b
Out[0]:
(5 + 3*x + x^2) // (6 + 4*x + 2*x^2)

The first derivative of such an expression will be equal to:

$$c(x) = \left( \frac{a(x)}{b(x)} \right)' = \left( \frac{x^2+3x+5}{2x^2+4x+6} \right)' = \frac{-2x^2-8x-2}{4x^4+16x^3+40x^2+48x+36}$$

In case the function derivative() returns one value when calculating the derivative of a rational function, the value obtained will also be a rational function:

In [ ]:
c = derivative(ab)
Out[0]:
(-2 - 8*x - 2*x^2) // (36 + 48*x + 40*x^2 + 16*x^3 + 4*x^4)

If the function derivative() returns two values when calculating the derivative of a rational function, then we will get polynomials of the numerator and denominator of the resulting expression:

$$c(x) = \frac{c_n(x)}{c_d(x)} = \frac{-2x^2-8x-2}{4x^4+16x^3+40x^2+48x+36}$$ $$c_n(x) = -2x^2-8x-2$$ $$c_d(x) = 4x^4+16x^3+40x^2+48x+36$$

In [ ]:
c_n, c_d = derivative(ab)
[c_n, c_d]
Out[0]:
2-element Vector{Polynomial{Int64, :x}}:
 Polynomial(-2 - 8*x - 2*x^2)
 Polynomial(36 + 48*x + 40*x^2 + 16*x^3 + 4*x^4)

Integration of polynomials

Let's find the integral of the polynomial

$$s_0(x) = \int q_1(x) dx = \int \left(3x^2-8x\right) dx = x^3-4x^2 :$$

In [ ]:
s_0 = integrate(q_1)
Out[0]:
-4.0∙x2 + 1.0∙x3

Let us find the integral of the same polynomial, but with the addition of the free coefficient:

$$s(x) = \int q_1(x) dx +C= \int \left(3x^2-8x\right) dx +7 = x^3-4x^2+7$$

In [ ]:
s = integrate(q_1, 7)
Out[0]:
7.0 - 4.0∙x2 + 1.0∙x3

Conclusion

In this demo we have looked at how to differentiate and integrate polynomials using the Polynomials.jl library.