Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

Chapter Contents
Chapter Introduction
NAG Toolbox

## Purpose

nag_zeros_quadratic_real (c02aj) determines the roots of a quadratic equation with real coefficients.

## Syntax

[zsm, zlg, ifail] = c02aj(a, b, c)
[zsm, zlg, ifail] = nag_zeros_quadratic_real(a, b, c)

## Description

nag_zeros_quadratic_real (c02aj) attempts to find the roots of the quadratic equation $a{z}^{2}+bz+c=0$ (where $a$, $b$ and $c$ are real coefficients), by carefully evaluating the ‘standard’ closed formula
 $z=-b±b2-4ac 2a .$
It is based on the function QDRTC from Smith (1967).
Note:  it is not necessary to scale the coefficients prior to calling the function.

## References

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

## Parameters

### Compulsory Input Parameters

1:     $\mathrm{a}$ – double scalar
Must contain $a$, the coefficient of ${z}^{2}$.
2:     $\mathrm{b}$ – double scalar
Must contain $b$, the coefficient of $z$.
3:     $\mathrm{c}$ – double scalar
Must contain $c$, the constant coefficient.

None.

### Output Parameters

1:     $\mathrm{zsm}\left(2\right)$ – double array
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.
2:     $\mathrm{zlg}\left(2\right)$ – double array
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.
3:     $\mathrm{ifail}$int64int32nag_int scalar
${\mathbf{ifail}}={\mathbf{0}}$ unless the function detects an error (see Error Indicators and Warnings).

## Error Indicators and Warnings

Errors or warnings detected by the function:

Cases prefixed with W are classified as warnings and do not generate an error of type NAG:error_n. See nag_issue_warnings.

W  ${\mathbf{ifail}}=1$
On entry, ${\mathbf{a}}=0.0$. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the root $-c/b$ and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=2$
On entry, ${\mathbf{a}}=0.0$ and ${\mathbf{b}}=0.0$. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the largest machine representable number (see nag_machine_real_largest (x02al)) and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=3$
On entry, ${\mathbf{a}}=0.0$ and the root $-c/b$ overflows. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the largest machine representable number (see nag_machine_real_largest (x02al)) and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=4$
On entry, ${\mathbf{c}}=0.0$ 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, $b$ is so large that ${b}^{2}$ is indistinguishable from ${b}^{2}-4ac$ and the root $-b/a$ overflows. In this case, ${\mathbf{zsm}}\left(1\right)$ contains the root $-c/b$ and ${\mathbf{zsm}}\left(2\right)$ contains zero.
${\mathbf{ifail}}=-99$
${\mathbf{ifail}}=-399$
Your licence key may have expired or may not have been installed correctly.
${\mathbf{ifail}}=-999$
Dynamic memory allocation failed.
If ${\mathbf{ifail}}>{\mathbf{0}}$ on exit, then ${\mathbf{zlg}}\left(1\right)$ contains the largest machine representable number (see nag_machine_real_largest (x02al)) and ${\mathbf{zlg}}\left(2\right)$ contains zero.

## 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.

## Example

This example finds the roots of the quadratic equation ${z}^{2}+3z-10=0$.
```function c02aj_example

fprintf('c02aj example results\n\n');

% Roots of x^2 + 3*x - 10 = 0

a =   1;
b =   3;
c = -10;
[zsm, zlg, ifail] = c02aj(a, b, c);

if (zsm(2) == 0)
% two real roots
z(1) = zsm(1);
z(2) = zlg(1);
else
% two complex roots
z(1) = zsm(1) + i*zsm(2);
z(2) = zlg(1) + i*zlg(2);
end
disp(z');

```
```c02aj example results