C02 Chapter Contents
C02 Chapter Introduction
NAG Library Manual

# NAG Library Routine DocumentC02AHF

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

C02AHF determines the roots of a quadratic equation with complex coefficients.

## 2  Specification

 SUBROUTINE C02AHF ( AR, AI, BR, BI, CR, CI, ZSM, ZLG, IFAIL)
 INTEGER IFAIL REAL (KIND=nag_wp) AR, AI, BR, BI, CR, CI, ZSM(2), ZLG(2)

## 3  Description

C02AHF attempts to find the roots of the quadratic equation $a{z}^{2}+bz+c=0$ (where $a$, $b$ and $c$ are complex coefficients), by carefully evaluating the ‘standard’ closed formula
 $z=-b±b2-4ac 2a .$
It is based on the routine CQDRTC from Smith (1967).
Note:  it is not necessary to scale the coefficients prior to calling the routine.
Smith B T (1967) ZERPOL: a zero finding algorithm for polynomials using Laguerre's method Technical Report Department of Computer Science, University of Toronto, Canada

## 5  Parameters

1:     AR – REAL (KIND=nag_wp)Input
2:     AI – REAL (KIND=nag_wp)Input
On entry: AR and AI must contain the real and imaginary parts respectively of $a$, the coefficient of ${z}^{2}$.
3:     BR – REAL (KIND=nag_wp)Input
4:     BI – REAL (KIND=nag_wp)Input
On entry: BR and BI must contain the real and imaginary parts respectively of $b$, the coefficient of $z$.
5:     CR – REAL (KIND=nag_wp)Input
6:     CI – REAL (KIND=nag_wp)Input
On entry: CR and CI must contain the real and imaginary parts respectively of $c$, the constant coefficient.
7:     ZSM($2$) – REAL (KIND=nag_wp) arrayOutput
On exit: the real and imaginary parts of the smallest root in magnitude are stored in ${\mathbf{ZSM}}\left(1\right)$ and ${\mathbf{ZSM}}\left(2\right)$ respectively.
8:     ZLG($2$) – REAL (KIND=nag_wp) arrayOutput
On exit: the real and imaginary parts of the largest root in magnitude are stored in ${\mathbf{ZLG}}\left(1\right)$ and ${\mathbf{ZLG}}\left(2\right)$ respectively.
9:     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, $\left({\mathbf{AR}},{\mathbf{AI}}\right)=\left(0,0\right)$. In this case, ${\mathbf{ZSM}}\left(1\right)$ and ${\mathbf{ZSM}}\left(2\right)$ contain the real and imaginary parts respectively of the root $-c/b$.
${\mathbf{IFAIL}}=2$
On entry, $\left({\mathbf{AR}},{\mathbf{AI}}\right)=\left(0,0\right)$ and $\left({\mathbf{BR}},{\mathbf{BI}}\right)=\left(0,0\right)$. In this case, ${\mathbf{ZSM}}\left(1\right)$ contains the largest machine representable number (see X02ALF) and ${\mathbf{ZSM}}\left(2\right)$ contains zero.
${\mathbf{IFAIL}}=3$
On entry, $\left({\mathbf{AR}},{\mathbf{AI}}\right)=\left(0,0\right)$ and the root $-c/b$ overflows. In this case, ${\mathbf{ZSM}}\left(1\right)$ contains the largest machine representable number (see X02ALF) and ${\mathbf{ZSM}}\left(2\right)$ contains zero.
${\mathbf{IFAIL}}=4$
On entry, $\left({\mathbf{CR}},{\mathbf{CI}}\right)=\left(0,0\right)$ and the root $-b/a$ overflows. In this case, both ${\mathbf{ZSM}}\left(1\right)$ and ${\mathbf{ZSM}}\left(2\right)$ contain zero.
${\mathbf{IFAIL}}=5$
On entry, $\stackrel{~}{b}$ is so large that ${\stackrel{~}{b}}^{2}$ is indistinguishable from ${\stackrel{~}{b}}^{2}-4\stackrel{~}{a}\stackrel{~}{c}$ and the root $-b/a$ overflows, where $\stackrel{~}{b}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left|{\mathbf{BR}}\right|,\left|{\mathbf{BI}}\right|\right)$, $\stackrel{~}{a}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left|{\mathbf{AR}}\right|,\left|{\mathbf{AI}}\right|\right)$ and $\stackrel{~}{c}=\mathrm{max}\phantom{\rule{0.125em}{0ex}}\left(\left|{\mathbf{CR}}\right|,\left|{\mathbf{CI}}\right|\right)$. In this case, ${\mathbf{ZSM}}\left(1\right)$ and ${\mathbf{ZSM}}\left(2\right)$ contain the real and imaginary parts respectively of the root $-c/b$.
If ${\mathbf{IFAIL}}>{\mathbf{0}}$ on exit, then ${\mathbf{ZLG}}\left(1\right)$ contains the largest machine representable number (see X02ALF) and ${\mathbf{ZLG}}\left(2\right)$ contains zero.

## 7  Accuracy

If ${\mathbf{IFAIL}}={\mathbf{0}}$ on exit, then the computed roots should be accurate to within a small multiple of the machine precision except when underflow (or overflow) occurs, in which case the true roots are within a small multiple of the underflow (or overflow) threshold of the machine.

None.

## 9  Example

This example finds the roots of the quadratic equation ${z}^{2}-\left(3.0-1.0i\right)z+\left(8.0+1.0i\right)=0$.

### 9.1  Program Text

Program Text (c02ahfe.f90)

### 9.2  Program Data

Program Data (c02ahfe.d)

### 9.3  Program Results

Program Results (c02ahfe.r)