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:
using Polynomials
Differentiating polynomials¶
Let's define a polynomial $p(x) = x^3-4x^2+7$
p = Polynomial([7, 0, -4, 1])
Find the first derivative of the polynomial $q_1(x)=p'(x)=3x^2-8x$:
q_1 = derivative(p)
Find the second derivative of the polynomial $q_2(x) = q_1'(x)= p''(x) = 6x-8$:
q_2 = derivative(p, 2)
Find the derivative of the rational expression $\frac{a(x)}{b(x)}$, where $a(x)$ and $b(x)$ are polynomials:
a = Polynomial([5, 3, 1]);
b = Polynomial([6, 4, 2]);
ab = a // b
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:
c = derivative(ab)
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$$
c_n, c_d = derivative(ab)
[c_n, c_d]
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 :$$
s_0 = integrate(q_1)
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$$
s = integrate(q_1, 7)
Conclusion¶
In this demo we have looked at how to differentiate and integrate polynomials using the Polynomials.jl library.