hide long namesshow long names
hide short namesshow short names
Integer type:  int32  int64  nag_int  show int32  show int32  show int64  show int64  show nag_int  show nag_int

PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

NAG Toolbox: nag_zeros_quadratic_real (c02aj)

 Contents

    1  Purpose
    2  Syntax
    7  Accuracy
    9  Example

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 az2+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:     a – double scalar
Must contain a, the coefficient of z2.
2:     b – double scalar
Must contain b, the coefficient of z.
3:     c – double scalar
Must contain c, the constant coefficient.

Optional Input Parameters

None.

Output Parameters

1:     zsm2 – double array
The real and imaginary parts of the smallest root in magnitude are stored in zsm1 and zsm2 respectively.
2:     zlg2 – double array
The real and imaginary parts of the largest root in magnitude are stored in zlg1 and zlg2 respectively.
3:     ifail int64int32nag_int scalar
ifail=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  ifail=1
On entry, a=0.0. In this case, zsm1 contains the root -c/b and zsm2 contains zero.
   ifail=2
On entry, a=0.0 and b=0.0. In this case, zsm1 contains the largest machine representable number (see nag_machine_real_largest (x02al)) and zsm2 contains zero.
   ifail=3
On entry, a=0.0 and the root -c/b overflows. In this case, zsm1 contains the largest machine representable number (see nag_machine_real_largest (x02al)) and zsm2 contains zero.
   ifail=4
On entry, c=0.0 and the root -b/a overflows. In this case, both zsm1 and zsm2 contain zero.
   ifail=5
On entry, b is so large that b2 is indistinguishable from b2-4ac and the root -b/a overflows. In this case, zsm1 contains the root -c/b and zsm2 contains zero.
   ifail=-99
An unexpected error has been triggered by this routine. Please contact NAG.
   ifail=-399
Your licence key may have expired or may not have been installed correctly.
   ifail=-999
Dynamic memory allocation failed.
If ifail>0 on exit, then zlg1 contains the largest machine representable number (see nag_machine_real_largest (x02al)) and zlg2 contains zero.

Accuracy

If ifail=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.

Further Comments

None.

Example

This example finds the roots of the quadratic equation z2+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);

disp('Roots of the quadratic equation:');

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

Roots of the quadratic equation:
     2
    -5


PDF version (NAG web site, 64-bit version, 64-bit version)
Chapter Contents
Chapter Introduction
NAG Toolbox

© The Numerical Algorithms Group Ltd, Oxford, UK. 2009–2015