double f_eq_hyper(vec n_small, set_const *C){ int status; int iter = 0, max_iter = 3000; double x = 0.0, x_expect = 0.5; double xmin = -0.001, xmax = 1.0; gsl_function F; F.function = &func_f_eq_multi; func_f_eq_multi_params params = { n_small, C }; F.params = ¶ms; gsl_root_fsolver_set(s_f_eq, &F, xmin, xmax); do { iter++; status = gsl_root_fsolver_iterate(s_f_eq); x = gsl_root_fsolver_root(s_f_eq); xmin = gsl_root_fsolver_x_lower(s_f_eq); xmax = gsl_root_fsolver_x_upper(s_f_eq); status = gsl_root_test_interval(xmin, xmax, 0, 1e-9); } while (status == GSL_CONTINUE && iter < max_iter); if (status != 0) { cout << "error, status = " << status << endl; } return x; }
void test_f (const gsl_root_fsolver_type * T, const char * description, gsl_function *f, double lower_bound, double upper_bound, double correct_root) { int status; size_t iterations = 0; double r, a, b; double x_lower, x_upper; gsl_root_fsolver * s; x_lower = lower_bound; x_upper = upper_bound; s = gsl_root_fsolver_alloc(T); gsl_root_fsolver_set(s, f, x_lower, x_upper) ; do { iterations++ ; gsl_root_fsolver_iterate (s); r = gsl_root_fsolver_root(s); a = gsl_root_fsolver_x_lower(s); b = gsl_root_fsolver_x_upper(s); if (a > b) gsl_test (GSL_FAILURE, "interval is invalid (%g,%g)", a, b); if (r < a || r > b) gsl_test (GSL_FAILURE, "r lies outside interval %g (%g,%g)", r, a, b); status = gsl_root_test_interval (a,b, EPSABS, EPSREL); } while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS); gsl_test (status, "%s, %s (%g obs vs %g expected) ", gsl_root_fsolver_name(s), description, gsl_root_fsolver_root(s), correct_root); if (iterations == MAX_ITERATIONS) { gsl_test (GSL_FAILURE, "exceeded maximum number of iterations"); } /* check the validity of the returned result */ if (!WITHIN_TOL (r, correct_root, EPSREL, EPSABS)) { gsl_test (GSL_FAILURE, "incorrect precision (%g obs vs %g expected)", r, correct_root); } gsl_root_fsolver_free(s); }
CAMLprim value ml_gsl_root_fsolver_set(value s, value f, value lo, value hi) { CAMLparam1(s); struct callback_params *p=Fparams_val(s); p->closure=f; gsl_root_fsolver_set(Fsolver_val(s), &(p->gslfun.gf), Double_val(lo), Double_val(hi)); CAMLreturn(Val_unit); }
void Spectrometer::calibrate(){ if(calibration.size()!=2) return; double a=1e3; // grating step in nm double l1 = calibration[0].wavelength; double x1 = calibration[0].pixelNum-imageWidth/2.; double l2 = calibration[1].wavelength; double x2 = calibration[1].pixelNum-imageWidth/2.; struct calcFParams params = {l1,l2,x1,x2,a}; double f_lo= 200; double f_hi= 3000; gsl_function F; F.function = &Spectrometer::calcFFunc; F.params = ¶ms; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; //T = gsl_root_fsolver_brent; T = gsl_root_fsolver_bisection; s = gsl_root_fsolver_alloc(T); gsl_root_fsolver_set (s, &F, f_lo, f_hi); //f in pixels guessed to be between 300 and 5000 double r=500; int iter=0; int max_iter=100; int status; do{ iter++; status = gsl_root_fsolver_iterate(s); r = gsl_root_fsolver_root(s); f_lo = gsl_root_fsolver_x_lower(s); f_hi = gsl_root_fsolver_x_upper(s); status = gsl_root_test_interval (f_lo, f_hi, 0, 0.001); } while (status == GSL_CONTINUE && iter < max_iter); if(status==GSL_SUCCESS) { focal = r; pas = a; sin_angle_i = l1/pas + x1/sqrt(r*r+x1*x1); isXCalibrated = true; qDebug()<<"Calibration finished !"; qDebug()<<"focal : "<< focal; qDebug()<<"angle i : "<< 180/3.1415*asin(sin_angle_i); qDebug()<<"angle i2 : "<< 180/3.1415*asin( l2/pas + x2/sqrt(r*r+x2*x2)); } gsl_root_fsolver_free (s); }
double Chevalier::MachNumber(double r) { double Mx; double x = r/R; gsl_function func; int status; int iter = 0; int max_iter = 100; double M_lo = 1.0e-6; double M_hi = 5.; double answer; double fp[2]; fp[0] = gamma; fp[1] = x; //choose which solution to use if(x<=1.0) { M_lo = 1.0e-6; M_hi = 1.0; func.function = &mach_crossing_A; }else{ M_lo = 1.0; M_hi = 100.0; func.function = &mach_crossing_B; } func.params = &fp[0]; gsl_root_fsolver_set(grsolve, &func, M_lo, M_hi); do{ iter++; status = gsl_root_fsolver_iterate(grsolve); Mx = gsl_root_fsolver_root(grsolve); M_lo = gsl_root_fsolver_x_lower(grsolve); M_hi = gsl_root_fsolver_x_upper(grsolve); status = gsl_root_test_interval(M_lo,M_hi,0,1.0e-5); if(status==GSL_SUCCESS) { answer = Mx; } } while(status==GSL_CONTINUE && iter<max_iter); //return the answer return answer; }
static VALUE rb_gsl_fsolver_set(VALUE obj, VALUE func, VALUE xl, VALUE xh) { gsl_root_fsolver *s = NULL; gsl_function *fff = NULL; double xlow, xup; Need_Float(xl); Need_Float(xh); CHECK_FUNCTION(func); Data_Get_Struct(obj, gsl_root_fsolver, s); Data_Get_Struct(func, gsl_function, fff); xlow = NUM2DBL(xl); xup = NUM2DBL(xh); gsl_root_fsolver_set(s, fff, xlow, xup); return obj; }
/// Get the cDel for a given cvir double cosmology::getcDel(double cvir, double z, double Delta) { int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double c_lo = 0.01*cvir, c_hi = 10.0*cvir; gsl_function F; cDel_params p; p.cptr = this; p.cvir = &cvir; double omz=Omega(z); double dcz=Delta_crit(z); p.omegaz=&omz; p.dcz=&dcz; p.Delta=Δ F.function = &findcDel; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, c_lo, c_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); c_lo = gsl_root_fsolver_x_lower (s); c_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (c_lo, c_hi,0, 1e-6); if (status == GSL_SUCCESS) { //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return res; }
int main (void) { int status; int iter = 0, max_iter = 100; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double r = 0, r_expected = sqrt (5.0); double x_lo = 0.0, x_hi = 5.0; gsl_function F; struct quadratic_params params = {1.0, 0.0, -5.0}; F.function = &quadratic; F.params = ¶ms; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, x_lo, x_hi); printf ("using %s method\n", gsl_root_fsolver_name (s)); printf ("%5s [%9s, %9s] %9s %10s %9s\n", "iter", "lower", "upper", "root", "err", "err(est)"); do { iter++; status = gsl_root_fsolver_iterate (s); r = gsl_root_fsolver_root (s); x_lo = gsl_root_fsolver_x_lower (s); x_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (x_lo, x_hi, 0, 0.001); if (status == GSL_SUCCESS) printf ("Converged:\n"); printf ("%5d [%.7f, %.7f] %.7f %+.7f %.7f\n", iter, x_lo, x_hi, r, r - r_expected, x_hi - x_lo); } while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return status; }
void conf(Args *args, Result *result){ const gsl_root_fsolver_type *solverType; gsl_root_fsolver *s; gsl_function fun; double xLo, xHi; int status; /* preliminaries */ gsl_set_error_handler_off(); fun.function = &confFun; fun.params = result; solverType = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc(solverType); /* search for lower bound of delta */ result->type = 2; if(result->de < 0) xLo = -1; else xLo = 0; xHi = result->de; status = gsl_root_fsolver_set(s,&fun,xLo,xHi); if(status){ printf("WARNING: Lower confidence limit of \\Delta cannot be estimated; setting it to -1.\n"); result->dLo = -1.0; }else result->dLo = iterate(args, s, xLo, xHi); /* search for upper bound of delta */ xLo = result->de; xHi = 1.0; status = gsl_root_fsolver_set(s,&fun,xLo,xHi); if(status){ printf("WARNING: Upper confidence limit of \\Delta cannot be estimated; setting it to 1.\n"); result->dUp = 1; }else result->dUp = iterate(args, s, xLo, xHi); gsl_root_fsolver_free(s); }
// Iterates the solver until desired precision has been reached or a maximum // number of iterations have been performed. Real findRoot(gsl_function const& F, gsl_root_fsolver* solver, Real low, Real high, Real tol_abs, Real tol_rel, char const* funcName) { // low and high should constitute an interval that straddles a root. Real l(low); Real h(high); gsl_root_fsolver_set(solver, const_cast<gsl_function*>(&F), l, h); const unsigned int maxIter(100); unsigned int i(0); for (;;) { // iterate gsl_root_fsolver_iterate(solver); // get found bracketing interval l = gsl_root_fsolver_x_lower(solver); h = gsl_root_fsolver_x_upper(solver); // see if this is acceptable const int status(gsl_root_test_interval(l, h, tol_abs, tol_rel)); // stop finder if convergence or too much iterations if (status == GSL_CONTINUE) { if (i >= maxIter) { gsl_root_fsolver_free(solver); throw std::runtime_error(std::string(funcName) + ": failed to converge"); } } else { break; } ++i; } const Real root(gsl_root_fsolver_root(solver)); return root; }
int UnidimensionalRootFinder(gsl_function *F, double lower_bound, double upper_bound, double abs_error, double rel_error, int max_iterations, double *return_result) { const gsl_root_fsolver_type * T = gsl_root_fsolver_bisection; gsl_root_fsolver * s = gsl_root_fsolver_alloc(T); // Test if the limits straddle the root, // if they don't, we will return -1. if (GSL_SIGN(GSL_FN_EVAL(F, lower_bound)) == GSL_SIGN(GSL_FN_EVAL(F, upper_bound))) return -1; gsl_root_fsolver_set(s, F, lower_bound, upper_bound); int i = 0; double x_lower; double x_upper; do{ i++; int status = gsl_root_fsolver_iterate(s); if (status != GSL_SUCCESS){ printf("ERROR: No solution to the gap equation was found!\n"); exit(EXIT_FAILURE); } x_lower = gsl_root_fsolver_x_lower(s); x_upper = gsl_root_fsolver_x_upper(s); } while(GSL_CONTINUE == gsl_root_test_interval(x_lower, x_upper, abs_error, rel_error) && i <= max_iterations); double result = gsl_root_fsolver_root(s); void gsl_root_fsolver_free(gsl_root_fsolver * S); *return_result = result; return 0; }
/* compute the first N roots alpha_k of the equation ((A-1)/(A+1)) cos((H - Z B) alpha) = cos((H + Z B) alpha) where H and B are heights and A, Z are defined in terms of material constants */ int print_alpha_k(const int N) { int status, iter, k, max_iter = 200; double Z, A; double alpha, alpha_lo, alpha_hi, temp_lo; const gsl_root_fsolver_type *solvT; gsl_root_fsolver *solv; gsl_function F; struct coscross_params params; Z = sqrt((rho_BR * c_p_BR * k_ICE) / (rho_ICE * c_p_ICE * k_BR)); A = (k_BR / k_ICE) * Z; params.Afrac = (A - 1.0) / (A + 1.0); params.HZBsum = H0 + Z * B0; params.HZBdiff = H0 - Z * B0; F.function = &coscross; F.params = ¶ms; solvT = gsl_root_fsolver_brent; /* faster than bisection but still bracketing */ solv = gsl_root_fsolver_alloc(solvT); for (k = 0; k < N; k++) { /* these numbers bracket exactly one solution */ alpha_lo = (double(k) * pi) / params.HZBsum; alpha_hi = (double(k + 1) * pi) / params.HZBsum; gsl_root_fsolver_set(solv, &F, alpha_lo, alpha_hi); iter = 0; do { iter++; status = gsl_root_fsolver_iterate(solv); alpha = gsl_root_fsolver_root(solv); alpha_lo = gsl_root_fsolver_x_lower(solv); alpha_hi = gsl_root_fsolver_x_upper(solv); temp_lo = (alpha_lo > 0) ? alpha_lo : (alpha_hi/2.0); status = gsl_root_test_interval(temp_lo, alpha_hi, 0, ALPHA_RELTOL); } while ((status == GSL_CONTINUE) && (iter < max_iter)); if (iter >= max_iter) { printf("!!!ERROR: root finding iteration reached maximum iterations; QUITING!\n"); return ITER_MAXED_OUT; } printf("%19.15e,\n",alpha); /* DEBUG: printf("%19.15e (in orig bracket [%19.15e,%19.15e])\n",alpha, (double(k) * pi) / params.HZBsum, (double(k+1) * pi) / params.HZBsum); */ } gsl_root_fsolver_free(solv); return status; }
const Real findRoot( gsl_function& F, gsl_root_fsolver* solver, const Real low, const Real high, const Real tol_abs, const Real tol_rel, std::string funcName ) { Real l( low ); Real h( high ); gsl_root_fsolver_set( solver, &F, l, h ); const unsigned int maxIter( 100 ); unsigned int i( 0 ); while( true ) { gsl_root_fsolver_iterate( solver ); l = gsl_root_fsolver_x_lower( solver ); h = gsl_root_fsolver_x_upper( solver ); const int status( gsl_root_test_interval( l, h, tol_abs, tol_rel ) ); if( status == GSL_CONTINUE ) { if( i >= maxIter ) { gsl_root_fsolver_free( solver ); std::cerr << funcName << ": failed to converge." << std::endl; throw std::exception(); } } else { break; } ++i; } const Real root( gsl_root_fsolver_root( solver ) ); return root; }
int Solver1d::Main () { fsolver_type=gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (fsolver_type); gsl_root_fsolver_set (s, &F, x_lo, x_hi); do { iter++; status = gsl_root_fsolver_iterate (s); x_lo = gsl_root_fsolver_x_lower (s); x_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (x_lo, x_hi, epsabs, epsrel); } while (status == GSL_CONTINUE && iter < max_iter); return status; }
double _find_z(gsl_root_fsolver* s, gsl_function* F, double target, double x_lo, double x_hi) { ((double*) F->params)[2] = target; gsl_root_fsolver_set(s, F, x_lo, x_hi); int status = GSL_SUCCESS; do { if (gsl_root_fsolver_iterate(s) != GSL_SUCCESS) return INVALID_NUMBER; x_lo = gsl_root_fsolver_x_lower(s); x_hi = gsl_root_fsolver_x_upper(s); status = gsl_root_test_interval(x_lo, x_hi, 1e-5, 1e-3); } while (status == GSL_CONTINUE); return 0.5 * (x_lo + x_hi); }
/// Get the Mvir for a given MDel double cosmology::getMvir(double M, double z,double Delta) { int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double m_lo = 0.1*M, m_hi = 5.0*M; gsl_function F; mvir_params p; p.cptr = this; p.mDel = &M; p.z=&z; p.Delta=Δ F.function = &findmvir; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, m_lo, m_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); m_lo = gsl_root_fsolver_x_lower (s); m_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (m_lo, m_hi,0, 1.e-6); if (status == GSL_SUCCESS) { //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return res; }
static VALUE rb_gsl_function_rootfinder(int argc, VALUE *argv, VALUE obj) { int status, iter = 0, max_iter = 1000; const gsl_root_fsolver_type *T; gsl_root_fsolver *s = NULL; gsl_function *F = NULL; double r, a, b, epsabs = 0.0, epsrel = 1e-6; Data_Get_Struct(obj, gsl_function, F); switch (argc) { case 2: a = NUM2DBL(argv[0]); b = NUM2DBL(argv[1]); break; case 1: switch (TYPE(argv[0])) { case T_ARRAY: a = NUM2DBL(rb_ary_entry(argv[0], 0)); b = NUM2DBL(rb_ary_entry(argv[0], 1)); break; default: rb_raise(rb_eTypeError, "interval must be given by an array [a, b]"); } break; default: rb_raise(rb_eArgError, "interval must be given"); break; } T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, F, a, b); do { iter++; status = gsl_root_fsolver_iterate (s); r = gsl_root_fsolver_root (s); a = gsl_root_fsolver_x_lower (s); b = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (a, b, epsabs, epsrel); if (status == GSL_SUCCESS) break; } while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free(s); if (status == GSL_SUCCESS) { return rb_ary_new3(3, rb_float_new(r), INT2FIX(iter), INT2FIX(status)); } else { printf("not converged\n"); return Qfalse; } }
/// Get the M which has N_{+} number of haloes above mass M. double cosmology::getM(double Np,double z) { int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; // m is in log_{10}(m) double xm_lo = 9.0, xm_hi = 15.5; gsl_function F; np_params p; p.cptr = this; p.z = &z; p.Np = &Np; F.function = &findM; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, xm_lo, xm_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); xm_lo = gsl_root_fsolver_x_lower (s); xm_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (xm_lo, xm_hi,0, 1e-6); if (status == GSL_SUCCESS) { std::cout<<"# "<<"getM:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return pow(10.,res); }
/// Get the c200 for a given cvir double cosmology::getcDeltap_from_cDelta(double cDelta, double Delta, double Deltap) { int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double c_lo = 0.01*cDelta, c_hi = 10.0*cDelta; gsl_function F; cDelta_params p; double frac=Delta/Deltap; p.cDelta = &cDelta; p.frac = &frac; F.function = &findcDelp; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, c_lo, c_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); c_lo = gsl_root_fsolver_x_lower (s); c_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (c_lo, c_hi,0, 1e-6); if (status == GSL_SUCCESS) { //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return res; }
int f_eq_solve(vec n, set_const *C, double lo, double hi, double * res){ int status; int iter = 0, max_iter = 3000; double x = 0.0, x_expect = 0.5; double xmin = lo, xmax = hi; gsl_function F; F.function = &func_f_eq_multi; func_f_eq_multi_params params = { n, C }; F.params = ¶ms; //Like there is no root if function values have the same sign: double fmin = F.function(xmin, ¶ms); double fmax = F.function(xmax, ¶ms); if (fmin*fmax > 0){ return -10; } gsl_root_fsolver_set(s_f_eq, &F, xmin, xmax); do { iter++; status = gsl_root_fsolver_iterate(s_f_eq); //printf("status = %i\n", status); x = gsl_root_fsolver_root(s_f_eq); xmin = gsl_root_fsolver_x_lower(s_f_eq); xmax = gsl_root_fsolver_x_upper(s_f_eq); status = gsl_root_test_interval(xmin, xmax, 0, 1e-9); //printf("status = %i\n", status); } while (status == GSL_CONTINUE && iter < max_iter); if (status != 0) { cout << "error, status = " << status << endl; } //printf("func_feq_vec: nn = %f, np = %f, res = %f \n", n[0], n[1], x); *res = x; return status; }
/// Get the collapse redshift for a given variance double cosmology::getzcoll(double sigma) { int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double z_lo = 0.0, z_hi = 10.0; gsl_function F; coll_params p; p.cptr = this; p.sig = σ F.function = &findzcoll; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, z_lo, z_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); z_lo = gsl_root_fsolver_x_lower (s); z_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (z_lo, z_hi,0, 1e-6); if (status == GSL_SUCCESS) { //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return res; }
int main () { int status; int iterations = 0, max_iterations = 100; gsl_root_fsolver *s; double r = 0, r_expected = sqrt (5.0); double x_lower x = 0.0, x_upper = 5.0; gsl_function F; struct quadratic_params params = {1.0, 0.0, -5.0}; F.function = &quadratic; F.params = ¶ms; s = gsl_root_fsolver_alloc (gsl_root_fsolver_bisection); gsl_root_fsolver_set (s, &F, x); printf ("using %s method\n", gsl_root_fsolver_name (s)); printf ("%5s [%9s, %9s] %9s %9s %10s %9s\n", "iter", "lower", "upper", "root", "actual", "err", "err(est)"); do { iterations++; status = gsl_root_fsolver_iterate (s); r = gsl_root_fsolver_root (s); x_lower = gsl_root_fsolver_x_lower (s); x_upper = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (x, 0, 0.001); if (status == GSL_SUCCESS) printf ("Converged:\n"); printf ("%5d [%.7f, %.7f] %.7f %.7f %+.7f %.7f\n", iterations, x_lower, x_upper, r, r_expected, r - r_expected, x_upper - x_lower); } while (status == GSL_CONTINUE && iterations < max_iterations); }
double raytrace_free_distance(scope_ray ray, raytrace_geom geom, int surf){ /* Variable declarations */ /* Variables needed for GSL's root-finder algorithms */ int status; int iter = 0, max_iter = 100; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double r = 0; double x_lo = 0.0, x_hi = 5.0; gsl_function F; scope_root_params params = {ray, geom, surf}; /* If w/in Rowland Circle, reduce x_hi to 2.1 */ if(surf == OPTIC_SF) x_hi = 2.1; F.function = &raytrace_distroot; F.params = ¶ms; /* Solve using GSL's Brent's Method Solver */ // T = gsl_root_fsolver_brent; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc(T); gsl_root_fsolver_set(s, &F, x_lo, x_hi); do{ iter++; status = gsl_root_fsolver_iterate(s); r = gsl_root_fsolver_root(s); x_lo = gsl_root_fsolver_x_lower(s); x_hi = gsl_root_fsolver_x_upper(s); status = gsl_root_test_interval(x_lo, x_hi, 0, 1.e-14); } while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); return r; }
void test_f_e (const gsl_root_fsolver_type * T, const char * description, gsl_function *f, double lower_bound, double upper_bound, double correct_root) { int status; size_t iterations = 0; double x_lower, x_upper; gsl_root_fsolver * s; x_lower = lower_bound; x_upper = upper_bound; s = gsl_root_fsolver_alloc(T); status = gsl_root_fsolver_set(s, f, x_lower, x_upper) ; gsl_test (status != GSL_EINVAL, "%s (set), %s", T->name, description); if (status == GSL_EINVAL) { gsl_root_fsolver_free(s); return ; } do { iterations++ ; gsl_root_fsolver_iterate (s); x_lower = gsl_root_fsolver_x_lower(s); x_upper = gsl_root_fsolver_x_lower(s); status = gsl_root_test_interval (x_lower, x_upper, EPSABS, EPSREL); } while (status == GSL_CONTINUE && iterations < MAX_ITERATIONS); gsl_test (!status, "%s, %s", gsl_root_fsolver_name(s), description, gsl_root_fsolver_root(s) - correct_root); gsl_root_fsolver_free(s); }
/** * GSL's the Brent-Dekker method (referred to here as Brent's method) combines an * interpolation strategy with the bisection algorithm. This produces a fast algorithm * which is still robust.On each iteration Brent's method approximates the function * using an interpolating curve. On the first iteration this is a linear interpolation * of the two endpoints. For subsequent iterations the algorithm uses an inverse quadratic * fit to the last three points, for higher accuracy. The intercept of the interpolating * curve with the x-axis is taken as a guess for the root. If it lies within the bounds * of the current interval then the interpolating point is accepted, and used to generate * a smaller interval. If the interpolating point is not accepted then the algorithm falls * back to an ordinary bisection step. * * The best estimate of the root is taken from the most recent interpolation or bisection. * * @author Sharmila Prasad (9/15/2011) * * @param x_lo - Initial lower search interval * @param x_hi - Initial higher search interval * @param Func - Continuous function of one variable for the root finders to operate on * @param tolerance - Root Error Tolerance * @param root - Output calculated root * * @return int - Algorithm status */ int Photometry::brentsolver(double x_lo, double x_hi, gsl_function *Func, double tolerance, double &root){ int status; int iter=0, max_iter=100; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set(s, Func, x_lo, x_hi); do { iter++; status = gsl_root_fsolver_iterate(s); root = gsl_root_fsolver_x_lower(s); x_lo = gsl_root_fsolver_x_lower(s); x_hi = gsl_root_fsolver_x_upper(s); status = gsl_root_test_interval(x_lo, x_hi, 0, tolerance); } while (status != GSL_SUCCESS && iter < max_iter); gsl_root_fsolver_free(s); return status; }
static VALUE rb_gsl_fsolver_solve(int argc, VALUE *argv, VALUE *obj) { gsl_root_fsolver *s = NULL; gsl_function *F = NULL; double x, xl, xh, epsabs = 0.0, epsrel = 1e-6; int status, iter = 0, max_iter = 100; switch (argc) { case 3: Check_Type(argv[2], T_ARRAY); epsabs = NUM2DBL(rb_ary_entry(argv[2], 0)); epsrel = NUM2DBL(rb_ary_entry(argv[2], 1)); /* no break */ case 2: Check_Type(argv[1], T_ARRAY); xl = NUM2DBL(rb_ary_entry(argv[1], 0)); xh = NUM2DBL(rb_ary_entry(argv[1], 1)); break; default: rb_raise(rb_eArgError, "Usage: solve(f = Function, range = Array, eps = Array)"); break; } CHECK_FUNCTION(argv[0]); Data_Get_Struct(argv[0], gsl_function, F); Data_Get_Struct(obj, gsl_root_fsolver, s); gsl_root_fsolver_set(s, F, xl, xh); do { iter++; status = gsl_root_fsolver_iterate (s); x = gsl_root_fsolver_root (s); xl = gsl_root_fsolver_x_lower (s); xh = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (xl, xh, epsabs, epsrel); if (status == GSL_SUCCESS) break; } while (status == GSL_CONTINUE && iter < max_iter); return rb_ary_new3(3, rb_float_new(x), INT2FIX(iter), INT2FIX(status)); }
double f_eq(double nn, double np, set_const * C) { int status; int iter = 0, max_iter = 3000; double x = 0.0, x_expect = 0.5; double xmin = 1e-5, xmax = 1.0; gsl_function F; F.function = &func_f_eq; func_f_eq_params params = { nn, np, C }; F.params = ¶ms; //for (int i = 0; i < 100; i++){ // if (func_f_eq(0.01*i, ¶ms) < 0){ // xmin = 0.01*i; // } //} gsl_root_fsolver_set(s_f_eq, &F, xmin, xmax); do { iter++; status = gsl_root_fsolver_iterate(s_f_eq); x = gsl_root_fsolver_root(s_f_eq); xmin = gsl_root_fsolver_x_lower(s_f_eq); xmax = gsl_root_fsolver_x_upper(s_f_eq); status = gsl_root_test_interval(xmin, xmax, 0, 1e-9); } while (status == GSL_CONTINUE && iter < max_iter); if (status != 0) { cout << "error, status = " << status << endl; } //printf("func_feq_orig: nn = %f, np = %f, res = %f \n", nn, np, x); return x; }
void cosmology::pevolve_fixed(double cdel, int opt, double z, double &cdelz, double &fdelz, double zstart){ double fac; if(opt==1){ fac=pow(cdel*(1.+zstart)/(1.+z),3)/mu(cdel); }else if(opt==2){ fac=pow(cdel,3)/mu(cdel); fac=fac*pow(Eofz(zstart)/Eofz(z),2); fac=fac*Delta_crit(zstart)/Delta_crit(z); }else if(opt==3){ fac=pow(cdel,3)/mu(cdel); fac=fac*pow(Eofz(zstart)/Eofz(z),2); }else{ fprintf(stderr,"Option %d not supported yet, bailing out...",opt); exit(100); } int status; int iter = 0, max_iter = 100; double res; const gsl_root_fsolver_type *T; gsl_root_fsolver *s; double c_lo = 0.01*cdel, c_hi = 100000.0*cdel; gsl_function F; march_params p; p.fac = fac; F.function = &findcmarch; F.params = &p; T = gsl_root_fsolver_brent; s = gsl_root_fsolver_alloc (T); gsl_root_fsolver_set (s, &F, c_lo, c_hi); do { iter++; status = gsl_root_fsolver_iterate (s); res = gsl_root_fsolver_root (s); c_lo = gsl_root_fsolver_x_lower (s); c_hi = gsl_root_fsolver_x_upper (s); status = gsl_root_test_interval (c_lo, c_hi,0, 1e-6); if (status == GSL_SUCCESS) { //std::cout<<"# "<<"zcollapse:Brent converged after "<< iter<<" iterations"<<std::endl; } }while (status == GSL_CONTINUE && iter < max_iter); gsl_root_fsolver_free (s); cdelz=res; fdelz=mu(cdelz)/mu(cdel); }
double eta(double q[2], double qdot[2], double t, double eta0) { // calculates the auxiliary variable eta by solving Kepler's equation // the equation is solved by searching for a root // using the brent's algorithm root bracketer in GSL // definitions for root solver const gsl_root_fsolver_type *T = gsl_root_fsolver_brent; gsl_root_fsolver *s; gsl_function keq; // iteration limits for root solver double epsabs=0.0; double epsrel = 1e-10; size_t max_iter=100; size_t iter=0; int status; double xlo,xhi; double r; //compute e, c, omega3, initial theta3 double e = eccentricity(q, qdot); double c = c_aux(q, qdot); double omega3 = Omega3(q,qdot); double theta30 = theta3(eta0,q,qdot); // set up function parameters keplerEquationParamsType pkeq={ e * c / (c + B_ISO), omega3 * t + theta30}; //set up function for rootfinding keq.function = &keplerEquation; keq.params = &pkeq; //initialize rootfinder s=gsl_root_fsolver_alloc(T); //set it to bracket root of kepler eqn. The initial guess should be near eta=Omega3*t + theta30 xlo = omega3 * t + theta30 - M_PI; xhi = omega3 * t + theta30 + M_PI; gsl_root_fsolver_set(s, &keq, xlo, xhi); //iterate till root is solved do { iter++; status=gsl_root_fsolver_iterate(s); xlo=gsl_root_fsolver_x_lower(s); xhi=gsl_root_fsolver_x_upper(s); status=gsl_root_test_interval(xlo,xhi,epsabs,epsrel); } while (status==GSL_CONTINUE && iter<max_iter); r = gsl_root_fsolver_root(s); gsl_root_fsolver_free(s); return r; }
/** * Function: get_xvalue_from_tlength * The function computes the x-offset position from the reference point * for a given tracelength in a given beam. * The solution is numerically derived and therefore * does work for any reasonable tracefunction. * * Parameters: * @param actbeam - the beam to compute the x-offset * @param tlength - tracelength * * Returns: * @return xdiff - the x-offset */ double get_xvalue_from_tlength(beam *actbeam, double tlength) { int iter=0; int status; double xdiff=0.0; d_point x_interv; // define and initialize the solver const gsl_root_fsolver_type *T = gsl_root_fsolver_brent; gsl_root_fsolver *s = gsl_root_fsolver_alloc (T); gsl_function F; tlength_pars *tpars; // derive the x-interval from the beam boundaries x_interv = get_xinterv_from_beam(actbeam); // fprintf(stdout, "xpos: %f, ypos: %f\n", x_interv.x, x_interv.y); // allocate and fill the parameters tpars = (tlength_pars *) malloc(sizeof(tlength_pars)); tpars->actbeam = actbeam; tpars->tlength = tlength; // fille the GSL-function F.function = &zero_tlength; F.params = tpars; // set the boundaries for the solver gsl_root_fsolver_set (s, &F, x_interv.x, x_interv.y); // iterate to find the zeropoint do { // increment the iteration counter iter++; // iterate on the solver status = gsl_root_fsolver_iterate (s); // get a new guess from the solver xdiff = gsl_root_fsolver_root (s); // derive and set new boundaries x_interv.x = gsl_root_fsolver_x_lower (s); x_interv.y = gsl_root_fsolver_x_upper (s); // check the accuracy status = gsl_root_test_interval (x_interv.x, x_interv.y, 0, 0.0001); //--------------CAVEAT--------------------------------------- // until March 28th 08 the code, wriongly was: //status = gsl_root_test_interval (x_interv.x, x_interv.x, // 0, 0.0001); // somehow this made no difference..... //----------------------------------------------------------- } // check for the break condition while (status == GSL_CONTINUE && iter < MAX_ITER); // free the memory free(tpars); // free the memory gsl_root_fsolver_free (s); // return the result return xdiff; }