QuantLib
: a free/open-source library for quantitative finance
fully annotated source code - version 1.34
Loading...
Searching...
No Matches
ql
math
solvers1d
falseposition.hpp
Go to the documentation of this file.
1
/* -*- mode: c++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
3
/*
4
Copyright (C) 2000, 2001, 2002, 2003 RiskMap srl
5
6
This file is part of QuantLib, a free-software/open-source library
7
for financial quantitative analysts and developers - http://quantlib.org/
8
9
QuantLib is free software: you can redistribute it and/or modify it
10
under the terms of the QuantLib license. You should have received a
11
copy of the license along with this program; if not, please email
12
<quantlib-dev@lists.sf.net>. The license is also available online at
13
<http://quantlib.org/license.shtml>.
14
15
This program is distributed in the hope that it will be useful, but WITHOUT
16
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17
FOR A PARTICULAR PURPOSE. See the license for more details.
18
*/
19
20
/*! \file falseposition.hpp
21
\brief false-position 1-D solver
22
*/
23
24
#ifndef quantlib_solver1d_falseposition_h
25
#define quantlib_solver1d_falseposition_h
26
27
#include <
ql/math/solver1d.hpp
>
28
29
namespace
QuantLib
{
30
31
//! False position 1-D solver
32
/*! \test the correctness of the returned values is tested by
33
checking them against known good results.
34
35
\ingroup solvers
36
*/
37
class
FalsePosition
:
public
Solver1D
<FalsePosition> {
38
public
:
39
template
<
class
F>
40
Real
solveImpl
(
const
F
&
f
,
41
Real
xAccuracy)
const
{
42
43
/* The implementation of the algorithm was inspired by
44
Press, Teukolsky, Vetterling, and Flannery,
45
"Numerical Recipes in C", 2nd edition,
46
Cambridge University Press
47
*/
48
49
Real
fl, fh, xl, xh;
50
// Identify the limits so that xl corresponds to the low side
51
if
(
fxMin_
< 0.0) {
52
xl =
xMin_
;
53
fl =
fxMin_
;
54
xh =
xMax_
;
55
fh =
fxMax_
;
56
}
else
{
57
xl =
xMax_
;
58
fl =
fxMax_
;
59
xh =
xMin_
;
60
fh =
fxMin_
;
61
}
62
63
Real
del, froot;
64
while
(
evaluationNumber_
<=
maxEvaluations_
) {
65
// Increment with respect to latest value
66
root_
= xl+(xh-xl)*fl/(fl-fh);
67
froot =
f
(
root_
);
68
++
evaluationNumber_
;
69
if
(froot < 0.0) {
// Replace appropriate limit
70
del = xl-
root_
;
71
xl =
root_
;
72
fl = froot;
73
}
else
{
74
del = xh-
root_
;
75
xh =
root_
;
76
fh = froot;
77
}
78
// Convergence criterion
79
if
(std::fabs(del) < xAccuracy || (
close
(froot, 0.0)))
80
return
root_
;
81
}
82
83
QL_FAIL
(
"maximum number of function evaluations ("
84
<<
maxEvaluations_
<<
") exceeded"
);
85
}
86
};
87
88
}
89
90
#endif
QuantLib::FalsePosition
False position 1-D solver.
Definition:
falseposition.hpp:37
QuantLib::FalsePosition::solveImpl
Real solveImpl(const F &f, Real xAccuracy) const
Definition:
falseposition.hpp:40
QuantLib::Solver1D
Base class for 1-D solvers.
Definition:
solver1d.hpp:67
QuantLib::Solver1D< FalsePosition >::evaluationNumber_
Size evaluationNumber_
Definition:
solver1d.hpp:227
QuantLib::Solver1D< FalsePosition >::xMin_
Real xMin_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< FalsePosition >::fxMin_
Real fxMin_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< FalsePosition >::root_
Real root_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< FalsePosition >::fxMax_
Real fxMax_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< FalsePosition >::xMax_
Real xMax_
Definition:
solver1d.hpp:225
QuantLib::Solver1D< FalsePosition >::maxEvaluations_
Size maxEvaluations_
Definition:
solver1d.hpp:226
f
F f
Definition:
defaultdensitystructure.cpp:32
QL_FAIL
#define QL_FAIL(message)
throw an error (possibly with file and line information)
Definition:
errors.hpp:92
QuantLib::Real
QL_REAL Real
real number
Definition:
types.hpp:50
QuantLib
Definition:
any.hpp:35
QuantLib::close
bool close(const Quantity &m1, const Quantity &m2, Size n)
Definition:
quantity.cpp:163
F
Real F
Definition:
sabr.cpp:200
solver1d.hpp
Abstract 1-D solver class.
Generated by
Doxygen
1.9.5