# Roots for polynomianl ## Roots for quadratic equation In this problem we try to solve a quadractic equation of the form of \begin{equation} ax^2 + bx + c = 0. \tag{1} \end{equation} We can factorize (1) to \begin{equation} (x+\frac{b}{2a})^2=\frac{b^2-4ac}{4a^2}. \tag{2} \end{equation} So the solution to x is \begin{equation} x = \frac{-b\pm \sqrt{b^2-4ac}}{2a} \tag{3} \end{equation} Fisrtly, you enter three coefficient (a, b, c) which are floating point number. Then I tranfrom data type REAL to COMPLEX by using intrinsic function *cmplx*. Lastly, I calculate the roots of this quadractic equation by eq (3). To present the result I seperate real part and imaginary part by using another intrinsic function *real* and *aimag*. ### Code ```fortran ! Purpose: Finding roots for quadratic equation ! ! Methods: Using quadratic root formula ! ! Input: d, e, f (a real number floating point coef) ! ! Output: x1, x2 (complex solution) ! ! Date Programer Description of change ! ==== ========= ===================== ! 9/27/20 Morris Original Code !======================================================== PROGRAM quadratic IMPLICIT NONE REAL :: d, e, f COMPLEX :: a, b, c COMPLEX :: x1, x2 CHARACTER(len=15) :: str1, str2, str3 write(6, *) 'Enter the coef of a quafractic equation (ax^2+bx+c=0)' read(5, *) d, e, f write(6, *) 'The coef of the equation is:' write(str1, '(F6.2)') d write(str2, '(F6.2)') e write(str3, '(F6.2)') f write(6, *) 'a = '//str1 write(6, *) 'b = '//str2 write(6, *) 'c = '//str3 a = cmplx(d, 0.0) b = cmplx(e, 0.0) c = cmplx(f, 0.0) x1 = (-b + sqrt(b**2 - 4*a*c)) / (2*a) x2 = (-b - sqrt(b**2 - 4*a*c)) / (2*a) write(6, *) 'The solution to this equation is' write(6, *) 'x1 = (', real(x1), ') + (', aimag(x1), ')i' write(6, *) 'x2 = (', real(x2), ') + (', aimag(x2), ')i' END PROGRAM quadratic ``` ### Result Output information are presented below in which I calculate the roots of \begin{equation} 5x^2 + 6x + 3 = 0. \tag{4} \end{equation} And it gives the solution of \begin{equation} x = \frac{-6 \pm \sqrt{-24}}{10} = -0.6 \pm 2\sqrt{6}i \tag{5} \end{equation} ![](https://i.imgur.com/soa138q.png) ## Roots for cubic equation In this problem we try to solve a cubic equation of the form of \begin{equation} ax^3 + bx^2 + cx + d = 0. \tag{6} \end{equation} First we define following relation \begin{equation} Q = \frac{3(\frac{c}{a})-(\frac{b}{a})^2}{9} \tag{7} \end{equation} \begin{equation} R = \frac{9(\frac{b}{a})(\frac{c}{a})-27(\frac{d}{a})-2(\frac{b}{a})^2}{54} \tag{8} \end{equation} \begin{equation} S = \sqrt[3]{R + \sqrt{Q^3 + R^2}} \tag{9} \end{equation} \begin{equation} T = \sqrt[3]{R - \sqrt{Q^3 + R^2}} \tag{10} \end{equation} Then the solution to x is \begin{align*} x_1 &= S + T - \frac{1}{3}(\frac{b}{a})\\ x_2 &= -\frac{1}{2}(S + T) - \frac{1}{3}(\frac{b}{a}) + \frac{1}{2}i\sqrt{3}(S-T)\\ x_3 &= -\frac{1}{2}(S + T) - \frac{1}{3}(\frac{b}{a}) - \frac{1}{2}i\sqrt{3}(S-T)\\ \tag{11} \end{align*} Fisrtly, you enter four coefficient (a, b, c, c) which are floating point number. Then I calculate these parameter(7) to (10). Lastly, I calculate the roots of this cubic equation by using above equation. To present the result I seperate real part and imaginary part by using another intrinsic function *real* and *aimag*. I futher check the validity using below relation. \begin{align*} &x_1 + x_2 + x_3 = -b/a \\ &x_1x_2+x_2x_3+x_3x_1=c/a\\ &x_1x_2x_3=-d/a \tag{12} \end{align*} ### Code ```fortran !======================================================== ! Purpose: Finding roots for cubic equation ! ! Methods: Cubic equation formula ! ! Input: a, b, c, d(coef. of the equation) ! ! Date Programer Description of change ! ==== ========= ===================== ! 9/27/20 Morris Original Code !======================================================== PROGRAM cubic IMPLICIT NONE COMPLEX, PARAMETER :: i=(0., 1.) REAL :: a, b, c, d REAL :: a1, a2, a3 REAL :: q, r, s, t COMPLEX :: x1, x2, x3 CHARACTER(len=15) :: str1, str2, str3, str4 write(6, *) 'Enter the coef of a cubic equation (ax^3+bx^2+cx+d=0)' read(5, *) a, b, c, d write(6, *) 'The coef of the equation is:' write(str1, '(F6.2)') a write(str2, '(F6.2)') b write(str3, '(F6.2)') c write(str4, '(F6.2)') d write(6, *) 'a = '//str1 write(6, *) 'b = '//str2 write(6, *) 'c = '//str3 write(6, *) 'd = '//str4 a1 = b / a a2 = c / a a3 = d / a q = (3. * a2 - a1**2) / 9. r = (9. * a1 * a2 - 27 * a3 - 2 * a1**3) / 54 s = (r + (q**3 + r**2)**0.5)**(1./3.) t = (r - (q**3 + r**2)**0.5)**(1./3.) x1 = s + t - a1 / 3. x2 = -0.5 * (s + t) - a1 / 3. + 0.5 * i * sqrt(3.) * & (s - t) x3 = -0.5 * (s + t) - a1 / 3. - 0.5 * i * sqrt(3.) * & (s - t) write(6, *) '=================================================' write(6, *) 'The solution to this equation is' write(6, *) 'x1 = (', real(x1), ') + (', aimag(x1), ')i' write(6, *) 'x2 = (', real(x2), ') + (', aimag(x2), ')i' write(6, *) 'x3 = (', real(x3), ') + (', aimag(x3), ')i' write(6, *) '=================================================' write(6, *) '=================================================' write(6, *) 'below check the if it is a right solution(zero means right)' write(6, *) 'x1 + x2 + x3 + b/a : ' write(6, '(F6.3)') abs(x1+x2+x3+a1) write(6, *) 'x1x2 + x2x3 + x3x1 - c/a : ' write(6, '(F6.3)') abs(x1*x2 + x2*x3 + x3*x1 - a2) write(6, *) 'x1x2x3 + d/a : ' write(6, '(F6.3)') abs(x1*x2*x3 + a3) write(6, *) '=================================================' END PROGRAM cubic ``` ### Result Output information are presented below in which I calculate the roots of \begin{equation} 5.5x^3 + x^2 - 2x + 3 = 0.\tag{13} \end{equation} ![](https://i.imgur.com/19wituF.png)