Example #1
0
/* test
 * tests a zero finder (z) for a function (f), using message msg.
 * tests the function on the interval [a,b].
 * modified from vzeroin.c, a test for Brent's zeroin function (from netlib)
 */
double test(double a, double b, double (*z)(double a, double b,
     double(*f)(double x), double t), double (*f)(double x), char *msg) {
	double root;
	counter = 0;
	printf("\nFor function %s\nin [%g,%g] root is\t%.9e\n",msg,a,b,
	       (root=zeroin(a,b,f,0.0)) );
	printf("No. of iterations\t\t%ld\n", counter);
	printf("Function value at the root found\t%.4e\n", (*f)(root));
	return root;
}
Example #2
0
File: binom.c Project: cran/binom
void binom_bayes(int *x,
                 int *n,
                 double *a,
                 double *b,
                 double *alpha,
                 double *lcl,
                 double* ucl,
                 int *len,
                 int *maxit,
                 double *tol,
                 int *error) {
  int i, j, first, down;
  double lcl_x, ucl_x, lcl_y, ucl_y;
  double y1, y2, y3;
  double px1, px2, sig;
  double mode, xx;
  double x1, x2;
  double lx1, lx2, ux1, ux2;
  double p[3];
  for(j = 0; j < len[0]; j++) {
    lcl_x = lcl[j];
    ucl_x = ucl[j];
    lcl_y = dbeta(lcl_x, a[j], b[j], NCP);
    ucl_y = dbeta(ucl_x, a[j], b[j], NCP);
    y3 = fmax(lcl_y, ucl_y);
    y1 = 0;
    mode = (a[j] - 1)/(a[j] + b[j] - 2);
    first = (lcl_y < ucl_y ? 0 : 1);
    x1 = first ? mode : 0;
    x2 = first ? 1 : mode;
    p[0] = y3;
    p[1] = a[j];
    p[2] = b[j];
    xx = zeroin(dbeta_shift, x1, x2, p, tol[0], maxit[0]);
    if(first) {
      ucl_x = xx;
    } else {
      lcl_x = xx;
    }
    px1 = pbeta(lcl_x, a[j], b[j], LOWER_TAIL, LOG_P);
    px2 = pbeta(ucl_x, a[j], b[j], UPPER_TAIL, LOG_P);
    sig = px1 + px2;
    down = 0;
    i = 0;
    while(fabs(sig - 2 * alpha[j]) > tol[0] && i < maxit[0]) {
      y2 = (y1 + y3) * 0.5;
      if(down) {
        if(dbeta(lcl_x, a[j], b[j], 0) < y2)
	  lcl_x = mode;
        lx1 = 0;
	lx2 = lcl_x;
        if(dbeta(ucl_x, a[j], b[j], 0) < y2)
	  ucl_x = mode;
        ux1 = ucl_x;
	ux2 = 1;
      } else {
        if(dbeta(lcl_x, a[j], b[j], 0) > y2)
	  lcl_x = 0;
        lx1 = lcl_x;
	lx2 = mode;
        if(dbeta(ucl_x, a[j], b[j], 0) > y2)
	  ucl_x = 1;
        ux1 = mode;
	ux2 = ucl_x;
      }
      p[0] = y2;
      lcl_x = zeroin(dbeta_shift, lx1, lx2, p, tol[0], maxit[0]);
      ucl_x = zeroin(dbeta_shift, ux1, ux2, p, tol[0], maxit[0]);
      px1 = pbeta(lcl_x, a[j], b[j], LOWER_TAIL, LOG_P);
      px2 = pbeta(ucl_x, a[j], b[j], UPPER_TAIL, LOG_P);
      sig = px1 + px2;
      if(sig > 2 * alpha[j]) {
        down = 0;
        y3 = y2;
      } else {
        down = 1;
        y1 = y2;
      }
      i++;
    }
    error[j] = (i >= maxit[0] ? 1 : 0);
    lcl[j] = lcl_x;
    ucl[j] = ucl_x;
  }
}