Hi,
I am quite new with QuantLib and recently I am learning to use the Solver1D class. I tried a very simple question of fiding the root to (x^2 + 4x + 4), which is -2. The code is very simple to write as follows: ########################################################################### #include <iostream> #include <stdlib.h> #include <ql/quantlib.hpp> using namespace std; using namespace QuantLib; class testclass { public: double operator()(double x) const { return x*x + 4*x + 4; } }; int main(void) { Brent solver; solver.setMaxEvaluations(10000); solver.setLowerBound(-100); solver.setUpperBound(100); double accuracy = 1.0e-4; double guess = 0; double step = 1.0e-3; double root = solver.solve(testclass(), accuracy, guess, step); cout << "the root is " << root << endl; system("PAUSE"); return 0; } ########################################################################### However, the compilation is okay but it throws exceptions when it runs. What's more, if I change the power to 3 (i.e., x^3 + 4x + 4) and it will work and give the right answer... It is not only with Brent solver but with other solvers as well. Can someone please let me know what's going wrong? Thanks. Jason Zhang ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
On Wed, 2008-04-30 at 15:30 +0000, Jason Zhang wrote:
> I am quite new with QuantLib and recently I am learning to use the Solver1D > class. I tried a very simple question of fiding the root to (x^2 + 4x + 4), > which is -2. The code is very simple to write as follows: > > [...] > > However, the compilation is okay but it throws exceptions when it runs. What's > more, if I change the power to 3 (i.e., x^3 + 4x + 4) and it will work and > give the right answer... It is not only with Brent solver but with other > solvers as well. > > Can someone please let me know what's going wrong? Thanks. Solvers need a function that crosses the zero---i.e., that goes from negative to positive values or the other way around. The function you're using is (x+2)^2, which touches the x-axis at x=-2 but is positive in any other point. Unfortunately, none of the methods implemented can find its root (all methods rely on bracketing the root first, i.e., finding two x1, x2 for which f(x1) < 0 and f(x2) > 0.) Luigi -- Westheimer's Discovery: A couple of months in the laboratory can frequently save a couple of hours in the library. ------------------------------------------------------------------------- This SF.net email is sponsored by the 2008 JavaOne(SM) Conference Don't miss this year's exciting event. There's still time to save $100. Use priority code J8TL2D2. http://ad.doubleclick.net/clk;198757673;13503038;p?http://java.sun.com/javaone _______________________________________________ QuantLib-users mailing list [hidden email] https://lists.sourceforge.net/lists/listinfo/quantlib-users |
Free forum by Nabble | Edit this page |