// main.cpp
#include <iostream>
#include <cmath>
#include "NewtonRaphson.h"
// (X^3 + a*exp(X) + b) defined as a function:
// one solution is -1.9387
double func(double x_in)
{
const double A = 2.0;
const double B = 7.0;
return x_in * x_in * x_in + A * exp(x_in) + B;
}
// the same function defined as a function object
class FuncObj
{
private:
double _a;
double _b;
public:
FuncObj(double a_in, double b_in)
: _a(a_in), _b(b_in) {};
double operator() (double x_in)
{
return x_in * x_in * x_in + _a * exp(x_in) + _b;
}
double solve(double x_in)
{
return NewtonRaphson(*this, x_in, 0.2, 0.0001, 20);
}
};
void main() { // use the Newton-Raphson library in three ways
FuncObj fo(2.0, 7.0);
cout << "Call Newton-Raphson indirectly: x = "
<< fo.solve(-10.0) << endl;
cout << "Call Newton-Raphson directly via FuncObj: x = "
<< NewtonRaphson(fo, -10.0, 0.2, 0.0001, 20) << endl;
cout << "Call Newton-Raphson directly via Func Ptr: x = "
<< NewtonRaphson(func, -10.0, 0.2, 0.0001, 20) << endl;
}
End of Listing