Cubic Roots¶
- cubic_roots(first_constant, second_constant, third_constant, fourth_constant, precision=4)¶
Calculates the roots of a cubic function
- Parameters
first_constant (int or float) – Coefficient of the cubic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
second_constant (int or float) – Coefficient of the quadratic term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
third_constant (int or float) – Coefficient of the linear term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
fourth_constant (int or float) – Coefficient of the constant term of the original cubic function; if zero, it will be converted to a small, non-zero decimal value (e.g., 0.0001)
precision (int, default=4) – Maximum number of digits that can appear after the decimal place of the resultant roots
- Raises
TypeError – First four arguments must be integers or floats
ValueError – Last argument must be a positive integer
- Returns
roots – List of the x-coordinates of all of the x-intercepts of the original function
- Return type
list of float
Notes
Standard form of a cubic function: \(f(x) = a\cdot{x^3} + b\cdot{x^2} + c\cdot{x} + d\)
Cubic formula: \(x_k = -\frac{1}{3a}\cdot(b + \xi^k\cdot{\eta} + \frac{\Delta_0}{\xi^k\cdot{\eta}})\)
\(\Delta_0 = b^2 - 3ac\)
\(\Delta_1 = 2b^3 - 9abc +27a^2d\)
\(\xi = \frac{-1 + \sqrt{-3}}{2}\)
\(\eta = \sqrt[3]{\frac{\Delta_1 \pm \sqrt{\Delta_1^2 - 4\Delta_0^3}}{2}}\)
\(k \in \{ 0, 1, 2 \}\)
Examples
- Import cubic_roots function from regressions library
>>> from regressions.analyses.roots.cubic import cubic_roots
- Calculate the roots of a cubic function with coefficients 2, 3, 5, and 7
>>> roots_first = cubic_roots(2, 3, 5, 7) >>> print(roots_first) [-1.4455]
- Calculate the roots of a cubic function with coefficients 7, -5, -3, and 2
>>> roots_second = cubic_roots(7, -5, -3, 2) >>> print(roots_second) [-0.6431, 0.551, 0.8064]
- Calculate the roots of a cubic function with all inputs set to 0
>>> roots_zeroes = cubic_roots(0, 0, 0, 0) >>> print(roots_zeroes) [-1.0]