Posted by
Jason Zhang-9 on
Apr 30, 2008; 3:30pm
URL: http://quantlib.414.s1.nabble.com/need-help-with-Solver1D-class-tp5976.html
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