summaryrefslogtreecommitdiff
path: root/src/math/math_NewtonFunctionRoot.cxx
blob: 8b9e8e022bf49fb80e80442248b2cfb7a38a9008 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//static const char* sccsid = "@(#)math_NewtonFunctionRoot.cxx	3.2 95/01/10"; // Do not delete this line. Used by sccs.
#include <math_NewtonFunctionRoot.ixx>
#include <math_FunctionWithDerivative.hxx>


math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F, 
						  const Standard_Real Guess, 
						  const Standard_Real EpsX , 
						  const Standard_Real EpsF , 
						  const Standard_Real A,
						  const Standard_Real B,
						  const Standard_Integer NbIterations ){
  EpsilonX = EpsX;
  EpsilonF = EpsF;
  Binf = A;
  Bsup = B;
  Itermax = NbIterations;
  Done = Standard_False;
  X = RealLast();
  DFx = 0;
  Fx = RealLast();
  It = 0;
  Perform(F, Guess);
}


math_NewtonFunctionRoot::math_NewtonFunctionRoot (const Standard_Real A , 
						  const Standard_Real B, 
						  const Standard_Real EpsX , 
						  const Standard_Real EpsF , 
						  const Standard_Integer NbIterations ){
  
  Binf = A;
  Bsup = B;
  EpsilonX = EpsX;
  EpsilonF = EpsF;
  Itermax = NbIterations;
  Done = Standard_False;
  X = RealLast();
  DFx = 0;
  Fx = RealLast();
  It = 0;
}


math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F, 
						  const Standard_Real Guess, 
						  const Standard_Real EpsX , 
						  const Standard_Real EpsF , 
						  const Standard_Integer NbIterations ){
  EpsilonX = EpsX;
  EpsilonF = EpsF;
  Itermax = NbIterations;
  Binf = RealFirst();
  Bsup = RealLast();
  Done = Standard_False;
  X = RealLast();
  DFx = 0;
  Fx = RealLast();
  It = 0;
  Perform(F, Guess);
}


void   math_NewtonFunctionRoot::Perform(math_FunctionWithDerivative& F,
					const Standard_Real Guess) {
  
  Standard_Real Dx;
  Standard_Boolean Ok;
  Standard_Real AA, BB;
  
  //--------------------------------------------------
  //-- lbr le 12 Nov 97
  //-- la meilleure estimation n est pas sauvee et on 
  //-- renvoie une solution plus fausse que Guess
  Standard_Real BestX=X,BestFx=RealLast();  
  //-- 
  
  if ( Binf < Bsup) {
    AA = Binf;
    BB = Bsup;
  }
  else {
    AA = Bsup;
    BB = Binf;
  }
  
  Dx = RealLast();
  Fx = RealLast(); 
  X = Guess;
  It = 1;
  while ( (It <= Itermax) && ( (Abs(Dx) > EpsilonX) || 
			      (Abs(Fx) > EpsilonF) ) ) {
    Ok = F.Values(X,Fx,DFx);
    
    Standard_Real AbsFx = Fx; if(AbsFx<0) AbsFx=-AbsFx;
    if(AbsFx<BestFx) {
      BestFx=AbsFx; 
      BestX =X;
    }
    
    if (Ok) {
      if (DFx == 0.) { 
	Done = Standard_False;
	It = Itermax + 1;                     
      }
      else {
	Dx = Fx/DFx;
	X -= Dx;                       
	// Limitation des variations de X:
	if (X <= AA) X = AA;
	if (X >= BB) X = BB;
	It++;
      }
    }
    else { 
      Done = Standard_False;
      It = Itermax + 1;                     
    }
  }
  X = BestX;
  
  if (It <= Itermax) { 
    Done = Standard_True;
  }
  else 
  {
    Done = Standard_False;
  }
}  


void math_NewtonFunctionRoot::Dump(Standard_OStream& o) const {
  
  o <<"math_NewtonFunctionRoot ";
  if (Done) {
    o << " Status = Done \n";
    o << " Location found = " << X <<"\n";
    o << " function value at this minimum = " << Fx <<"\n";
    o << " Number of iterations = " << It <<"\n";
  }
  else {
    o << "Status = not Done \n";
  }
}