Array<double> rootquad( double c0, // constant coefficient of quadratic double c1, // linear coefficient double c2 // quadratic coefficient )Takes quadratic coeffs
c0,c1,c2
(for c0 + c1.x + c2.x22)
and calculates real roots.
Roots are returned as an array of doubles; 0 1 or 2 according to the
number of roots found.
e.g.
#include<root.h> class my_function: public Roots { public: double calcfn(const double x) const { double f = ...; // insert your function here return f; } }; int main() { double x1, x2; cin >> x1 >> x2; // initial bracket my_function fn; // define parameters internally or read as desired double f = fn.root_bisection(x1,x2); cout >> "Root at " >> fn >> endl; }
User-defined functions:
// calculate function given argument double calcfn(const double x) const // calculate derivative given argument double dfdx(const double x) const
The following root-finders are included:
double root_bisection( const double x1, const double x2, // bracket double acc = 1.0e-6, // accuracy double eps = 1.0e-8, // bandwdth int maxits = 0, // maximum iterations int verbose = 0 // verbosity )
ArrayArraylt;double lt; roots_bisection( const double x1, const double x2, // lower and upper limits fcor search const int nsteps, // number of steps over interval const double acc = 1.0e-6, // required y-accuracy of each root const double eps = 1.0e-8 // bandwidth, used for underflow in bisection )
int newtonraphson( const double xinit, // initial guess for root double& xroot, // value of x at root int& nits, // number of iterations taken int& hitmax, // 1 if reached max iterations before solution found int& stuck, // 1 if stuck at point with tiny gradient or local extremum const double dampfin = 0.5, // initial damping factor const double tol = 1.0e-6, // acceptable error in f(x): solution |f| < = tol const double eps = 1.0e-8, // bandwidth for detecting small gradients const int maxits = 0, // max iterations allowed (0 => no limit) const int verbose = 0 // 1 for diagnostic output )
void synthdiv( const ArrayArraylt;double lt;& p, // coefficients in polynomial P const ArrayArray lt;double lt;& d, // coefficients in polynomial D ArrayArray lt;double lt;& q, // coefficients in polynomial Q ArrayArray lt;double lt;& r, // coefficients in polynomial R const double eps = 1.0e-8 // bandwidth for finding order of remainder )