1
double p1::root(double (*pf)(double k), int a, int b, double e)

我不知道该怎么做,我知道我必须循环精确定位中点等

double p1::root(double (*pf)(double k), int a, int b, double e) {


  // void nrerror(char error_text[]);                                           
  int j;
  float dx, f, fmid, xmid, rtb;

  f = (*pf)(a);
  fmid = (*pf)(b);
  //if (f*fmid >= 0.0) nrerror("root must be bracketed for bisection in rtbis")\
;                                                                               
      rtb = f < 0.0 ? (dx=b-a,a) : (dx=a-b,b);
      for(j = 1;j <40; j++) {
        fmid = (*pf)(xmid = rtb+(dx *= .5));
      if (fmid <= 0.0) rtb = xmid;
      if (fabs(dx) < e || fmid == 0.0) return rtb;
      }
      // nrerror("too many bisections in rtbis");                               


  return 0.0;
}




double p1::test_function(double k) {
  return (pow(k, 3) -2);
}

然后主要我有这个

 double (*pf)(double k);
pf = &p1::test_function;

//double result = p1::root(pf, a, b, e);
4

1 回答 1

0

也许数字食谱会给你一个想法。提示:它是递归的。

于 2011-03-20T16:55:05.690 回答