Bisection.h File Reference


Detailed Description

This file contains the bisection algorithm and the bisection_secant algorithm.

Definition in file Bisection.h.

#include <iostream>
#include <vector>
#include <stdexcept>
#include "Secant.h"

Include dependency graph for Bisection.h:

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  Bisection< functor, real >
 : This is an encapsulation of the Bisection algorithm. More...
class  Bisection_Secant< functor, real >
 this class is a child class of Bisection. The algorithm converges faster because it changes from the bisection to the secant algorithm /// on every other iteration. More...

Functions

template<class functor, class real>
void stradle_value (real &X0, real &X1, real &FX0, real &FX1, real Fsolve, functor &obj, real(functor::*f)(real), bool initialize_fx0=true, bool initialize_fx1=true, int direction=0, real MinX=0, real MaxX=0)
 This function solves to find a pair of X values, such that.


Function Documentation

template<class functor, class real>
void stradle_value ( real &  X0,
real &  X1,
real &  FX0,
real &  FX1,
real  Fsolve,
functor &  obj,
real(functor::*)(real)  f,
bool  initialize_fx0 = true,
bool  initialize_fx1 = true,
int  direction = 0,
real  MinX = 0,
real  MaxX = 0 
)

This function solves to find a pair of X values, such that.

  1. _F(X_a)<0 for some X_a in the interval [X0 , X1]
  2. _F(X_b)>0 for some X_b in the interval [X0 , X1]

Assumes:

  1. _f is assumed to be monotonic. _f can be either monotonically increasing, or decreasing.
  2. _f is defined on the range 0 - infinity.
Changes: X0, X1, FX0, FX1
Throws: domain error if X0==X1 or range error if _f violates the monotonic condition
Description: This template function solves for X0 and X1 so that f(X0) < Fsolve < f(X1)

Definition at line 28 of file Bisection.h.

Referenced by option_pair::call_implied_sigma(), binomial_option::call_implied_sigma(), main(), option_pair::put_implied_sigma(), and binomial_option::put_implied_sigma().

00041 {
00042   bool is_increasing, check_bounds;
00043 
00044   if (initialize_fx0) FX0 =(obj.*f)( X0);
00045   if (initialize_fx1) FX1 =(obj.*f)( X1);
00046 
00047   check_bounds = MinX!=0 || MaxX!=0;
00048 
00049   if (X0==X1) throw domain_error( "X0==X1 in stradle function");
00050 
00051   if (X0 > X1) {
00052     real tmp;
00053     tmp = X0;    X0 = X1;  X1 = tmp;
00054     tmp = FX0; FX0 = FX1; FX1 = tmp;
00055   }
00056 
00057   if (FX0 < Fsolve && Fsolve < FX1) return;
00058 
00059   if (direction == 0) {
00060     is_increasing = FX1 > FX0;
00061   } else {
00062     is_increasing = direction > 0;
00063   }
00064 
00065   if (check_bounds && (X1 > MaxX || X0 < MinX)) {
00066     throw domain_error( "X1 > MaxX || X0 < MinX in stradle_value");
00067   }
00068 
00069   if (is_increasing) {    // solve fx0 < fsolve < fx1
00070     while (FX0 > Fsolve) {
00071       if (FX1 < FX0) throw range_error( "FX1 < FX0 for monotonic increasing function  at label A in stradle_value");
00072       FX1 = FX0;
00073       X0 *= .5;
00074       if (check_bounds && X0 < MinX) {
00075         throw domain_error( "X0 < MinX in stradle_value");
00076       }
00077       FX0 = (obj.*f)( X0);
00078     }
00079     while (Fsolve > FX1) {
00080       if (FX1 < FX0) throw range_error( "FX1 < FX0 for monotonic increasing function  at label B in stradle_value");
00081       FX0 = FX1;
00082       X1 *= 2;
00083       if (check_bounds && X1 > MaxX) {
00084         throw domain_error( "X1 > MaxX in stradle_value");
00085       }
00086       FX1 = (obj.*f)( X1);
00087     }
00088   } else {  // solve fx0 > fsolve > fx1
00089     while (FX0 < Fsolve) {
00090       if (FX1 > FX0) throw range_error( "FX1 > FX0 for monotonic decreasing function at label C in stradle_value");
00091       FX1 = FX0;
00092       X0 *= .5;
00093       if (check_bounds && X0 < MinX) {
00094         throw domain_error( "X0 < MinX in stradle_value");
00095       }
00096       FX0 = (obj.*f)( X0);
00097     }
00098     while (Fsolve < FX1) {
00099       if (FX1 > FX0) throw range_error( "FX1 > FX0 for monotonic decreasing function at label D in stradle_value");
00100       FX0 = FX1;
00101       X1 *= 2;
00102       if (check_bounds && X1 > MaxX) {
00103         throw domain_error( "X1 > MaxX in stradle_value");
00104       }
00105       FX1 = (obj.*f)( X1);
00106     }
00107   }
00108 }


Generated on Fri Jan 7 12:36:18 2011 for public_options by  doxygen 1.5.1