// RootFindLinear_t.cpp #include "RootFindLinear.h" #include #include #include #include using std::string; using std::cout; using std::cerr; using std::endl; #ifndef DEFECT_CMATH_NOT_STD using std::sin; using std::asin; using std::fabs; #endif // Class to solve sin(x) = val; class RootFindSin : public RootFindLinear { private: double _val; public: RootFindSin(double val) { _val = val; }; StatusDouble evaluate(double x) const { return StatusDouble(0,sin(x)-_val); } }; //********************************************************************** int main( ) { string component = "RootFindLinear"; string ok_prefix = component + " (I): "; string error_prefix = component + " test (E): "; cout << ok_prefix << "---------- Testing component " + component + ". ----------" << endl; // Make sure assert is enabled. bool assert_flag = false; assert ( ( assert_flag = true, assert_flag ) ); if ( ! assert_flag ) { cerr << "Assert is disabled" << endl; return 1; } //******************************************************************** cout << ok_prefix << "Test with sin" << endl; assert( RootFindSin::OK == 0 ); double y0 = 0.345; double x0 = asin(y0); RootFindSin sfind(y0); StatusDouble sd = sfind.solve(0.0,0.8); cout << sd << endl; double xf = sd.value(); double yf = sin(xf); cout << "Predict:" << x0 << " " << y0 << endl; cout << " Found:" << xf << " " << yf << endl; assert( sd.status() == RootFindSin::OK ); double dif = fabs(xf - x0); assert( dif < 1.e-10 ); //******************************************************************** cout << ok_prefix << "------------- All tests passed. -------------" << endl; return 0; //******************************************************************** }