MGCL V10  V10
MGCL V10
 全て クラス 名前空間 関数 変数 型定義 列挙型 列挙値 フレンド グループ ページ
nlbit.h
1 /********************************************************************/
2 /* Copyright (c) 2015 DG Technologies Inc. and Yuzi Mizuno */
3 /* All rights reserved. */
4 /********************************************************************/
5 #ifndef _MGNLBIT_HH_
6 #define _MGNLBIT_HH_
7 
11 #include <math.h>
12 
20 template<class func>
21 double mgNlbit(
22  func& fn,
23  double xl,
25  double xr,
26  double eps,
27  int itr,
28  int& ier
29 ){
31  ier = 0;
32  double epsHalf = eps*.5;
33  double x = xl;
34  double fl = fn(x);
35  if (fabs(fl)<=eps) return x;
36  x = xr;
37  double fr = fn(x);
38  if(fabs(fr)<=eps) return x;
39  if(fr * fl > 0.) {
40  x = (xl + xr) * .5;
41  ier = 2;
42  return x;
43  }
44 
45  for(int n=1; n<=itr; ++n){
46  x=(xl+xr)*.5;
47  double f = fn(x);
48  if(fabs(f)<=eps)
49  return x;
50 
51  if(f*fl < 0.){
52  xr = x;
53  fr = f;
54  }else{
55  xl = x;
56  fl = f;
57  }
58  double df=fl-fr;
59  if(fabs(df)<=epsHalf){
60  x=(xr+xl)*.5;
61  return x;
62  }
63  double dx = (xr-xl)*fl/df;
64  x = xl+dx;
65  f = fn(x);
66  if (fabs(f)<=eps)
67  return x;
68 
69  if(fl*f<0.){
70  xr = x;
71  fr = f;
72  }else{
73  xl = x;
74  fl = f;
75  }
76  }
77  ier = 1;
78  return x;
79 }
80  // end of ALGORITHM group
82 #endif
double mgNlbit(func &fn, double xl, double xr, double eps, int itr, int &ier)
Compute the solution of fn(x)=0. mgNlbit can be applied when known there exists a solution between (x...
Definition: nlbit.h:21