Prev Next Index-> contents reference index search external Up-> CppComplexStep SecondOrder CppComplexStep-> Theory ExampleFun CentralStep ComplexStep FirstOrder SecondOrder Makefile SecondOrder Headings-> Description Local Include Files Running Program Source Code

Comparing Real and Complex Step Second Order Derivatives

Description
Running the SecondOrder program (as described below) results in the output below. In this output, h is the step size, f_xx(x) is the derivative that is being approximated, rStepStep(x) is the central difference approximation for the second derivative, and cStepStep(x) is the complex step method approximation for the second derivative.
f_xx(x)                = 20

h                      = 0.001
f_xx(x) - rStepStep(x) = -7.99973e-06
f_xx(x) - cStepStep(x) = 8e-06

h                      = 0.0001
f_xx(x) - rStepStep(x) = -1.00495e-07
f_xx(x) - cStepStep(x) = 8e-08

h                      = 1e-05
f_xx(x) - rStepStep(x) = -6.0957e-06
f_xx(x) - cStepStep(x) = 8e-10

h                      = 1e-06
f_xx(x) - rStepStep(x) = 0.00022039
f_xx(x) - cStepStep(x) = 7.99716e-12

Local Include Files
The program SecondOrder uses the following local include files ExampleFun.h , CentralStep.h , ComplexStep.h .

Running Program
The following commands compile and run SecondOrder and place the output in SecondOrder.out:

g++ -g SecondOrder.cpp -o SecondOrder
SecondOrder > SecondOrder.out

Source Code

# include <iostream>
# include <complex>

# include "ExampleFun.h"
# include "CentralStep.h"
# include "ComplexStep.h"

int main(void)
{    using std::cout;
using std::endl;
using std::complex;

// real and complex types
typedef double         Real;
typedef complex<Real>  Comp;

// real and complex function types
typedef Real (*RealFun) (const Real &);
typedef Comp (*CompFun) (const Comp &);

// complex of complex type and function type
typedef complex<Comp>  CompComp;
typedef CompComp (*CompCompFun) (const CompComp &);

// first order difference types
typedef CentralStep<RealFun,     Real> RealStep;
typedef ComplexStep<CompCompFun, Comp> CompStep;

// second order difference types
typedef CentralStep<RealStep, Real> RealStepStep;
typedef ComplexStep<CompStep, Real> CompStepStep;

// real and complex of complex versions of ExampleFun
Real     (*rf)  (const Real &x)      = ExampleFun<Real>;
CompComp (*ccf) (const CompComp &x)  = ExampleFun<CompComp>;

Real x    = 1.;
Real f_xx = ExampleFun_xx(x);
cout << "f_xx(x)                = " << f_xx << endl;

Real rh = 1e-2;
Comp ch = Comp(rh, 0.);
int i;
for(i = 0; i < 4; i++)
{    // next step size
rh = rh / 10.;
ch = ch / 10.;

// second order real step method
RealStep         rStep(rf,    rh);
RealStepStep rStepStep(rStep, rh);

// second order complex step method
CompStep         cStep(ccf,   ch);
CompStepStep cStepStep(cStep, rh);

cout << endl;
cout << "h                      = " << rh << endl;
cout << "f_xx(x) - rStepStep(x) = "
<<  f_xx - rStepStep(x) << endl;
cout << "f_xx(x) - cStepStep(x) = "
<<  f_xx - cStepStep(x) << endl;
cout << endl;
}

return 0;
}

Input File: SecondOrder.cpp