C02 Chapter Contents
C02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentC02AKF

Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

## 1  Purpose

C02AKF determines the roots of a cubic equation with real coefficients.

## 2  Specification

 SUBROUTINE C02AKF ( U, R, S, T, ZEROR, ZEROI, ERREST, IFAIL)
 INTEGER IFAIL REAL (KIND=nag_wp) U, R, S, T, ZEROR(3), ZEROI(3), ERREST(3)

## 3  Description

C02AKF attempts to find the roots of the cubic equation
 $uz3+rz2+sz+t=0,$
where $u$, $r$, $s$ and $t$ are real coefficients with $u\ne 0$. The roots are located by finding the eigenvalues of the associated $3$ by $3$ (upper Hessenberg) companion matrix $H$ given by
 $H= 0 0 -t/u 1 0 -s/u 0 1 -r/u .$
The eigenvalues are obtained by a call to F08PEF (DHSEQR). Further details can be found in Section 8.
To obtain the roots of a quartic equation, C02ALF can be used.

## 4  References

Golub G H and Van Loan C F (1996) Matrix Computations (3rd Edition) Johns Hopkins University Press, Baltimore

## 5  Parameters

1:     U – REAL (KIND=nag_wp)Input
On entry: $u$, the coefficient of ${z}^{3}$.
Constraint: ${\mathbf{U}}\ne 0.0$.
2:     R – REAL (KIND=nag_wp)Input
On entry: $r$, the coefficient of ${z}^{2}$.
3:     S – REAL (KIND=nag_wp)Input
On entry: $s$, the coefficient of $z$.
4:     T – REAL (KIND=nag_wp)Input
On entry: $t$, the constant coefficient.
5:     ZEROR($3$) – REAL (KIND=nag_wp) arrayOutput
6:     ZEROI($3$) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{ZEROR}}\left(i\right)$ and ${\mathbf{ZEROI}}\left(i\right)$ contain the real and imaginary parts, respectively, of the $i$th root.
7:     ERREST($3$) – REAL (KIND=nag_wp) arrayOutput
On exit: ${\mathbf{ERREST}}\left(i\right)$ contains an approximate error estimate for the $i$th root.
8:     IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to $0$, $-1\text{​ or ​}1$. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value $-1\text{​ or ​}1$ is recommended. If the output of error messages is undesirable, then the value $1$ is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is $0$. When the value $-\mathbf{1}\text{​ or ​}\mathbf{1}$ is used it is essential to test the value of IFAIL on exit.
On exit: ${\mathbf{IFAIL}}={\mathbf{0}}$ unless the routine detects an error or a warning has been flagged (see Section 6).

## 6  Error Indicators and Warnings

If on entry ${\mathbf{IFAIL}}={\mathbf{0}}$ or $-{\mathbf{1}}$, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
${\mathbf{IFAIL}}=1$
 On entry, ${\mathbf{U}}=0.0$.
${\mathbf{IFAIL}}=2$
The companion matrix $H$ cannot be formed without overflow.
${\mathbf{IFAIL}}=3$
The iterative procedure used to determine the eigenvalues has failed to converge.

## 7  Accuracy

If ${\mathbf{IFAIL}}={\mathbf{0}}$ on exit, then the $i$th computed root should have approximately $\left|{\mathrm{log}}_{10}\left({\mathbf{ERREST}}\left(i\right)\right)\right|$ correct significant digits.

The method used by the routine consists of the following steps, which are performed by routines from LAPACK in Chapter F08.
 (a) Form matrix $H$. (b) Apply a diagonal similarity transformation to $H$ (to give ${H}^{\prime }$). (c) Calculate the eigenvalues and Schur factorization of ${H}^{\prime }$. (d) Calculate the left and right eigenvectors of ${H}^{\prime }$. (e) Estimate reciprocal condition numbers for all the eigenvalues of ${H}^{\prime }$. (f) Calculate approximate error estimates for all the eigenvalues of ${H}^{\prime }$ (using the $1$-norm).

## 9  Example

This example finds the roots of the cubic equation
 $z3+3⁢z2+9z-13=0.$

### 9.1  Program Text

Program Text (c02akfe.f90)

### 9.2  Program Data

Program Data (c02akfe.d)

### 9.3  Program Results

Program Results (c02akfe.r)