b311480e |
1 | // Copyright (c) 1997-1999 Matra Datavision |
2 | // Copyright (c) 1999-2012 OPEN CASCADE SAS |
3 | // |
4 | // The content of this file is subject to the Open CASCADE Technology Public |
5 | // License Version 6.5 (the "License"). You may not use the content of this file |
6 | // except in compliance with the License. Please obtain a copy of the License |
7 | // at http://www.opencascade.org and read it completely before using this file. |
8 | // |
9 | // The Initial Developer of the Original Code is Open CASCADE S.A.S., having its |
10 | // main offices at: 1, place des Freres Montgolfier, 78280 Guyancourt, France. |
11 | // |
12 | // The Original Code and all software distributed under the License is |
13 | // distributed on an "AS IS" basis, without warranty of any kind, and the |
14 | // Initial Developer hereby disclaims all such warranties, including without |
15 | // limitation, any warranties of merchantability, fitness for a particular |
16 | // purpose or non-infringement. Please see the License for the specific terms |
17 | // and conditions governing the rights and limitations under the License. |
18 | |
7fd59977 |
19 | #include <math_NewtonFunctionRoot.ixx> |
20 | #include <math_FunctionWithDerivative.hxx> |
21 | |
22 | |
23 | math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F, |
24 | const Standard_Real Guess, |
25 | const Standard_Real EpsX , |
26 | const Standard_Real EpsF , |
27 | const Standard_Real A, |
28 | const Standard_Real B, |
29 | const Standard_Integer NbIterations ){ |
30 | EpsilonX = EpsX; |
31 | EpsilonF = EpsF; |
32 | Binf = A; |
33 | Bsup = B; |
34 | Itermax = NbIterations; |
35 | Done = Standard_False; |
36 | X = RealLast(); |
37 | DFx = 0; |
38 | Fx = RealLast(); |
39 | It = 0; |
40 | Perform(F, Guess); |
41 | } |
42 | |
43 | |
44 | math_NewtonFunctionRoot::math_NewtonFunctionRoot (const Standard_Real A , |
45 | const Standard_Real B, |
46 | const Standard_Real EpsX , |
47 | const Standard_Real EpsF , |
48 | const Standard_Integer NbIterations ){ |
49 | |
50 | Binf = A; |
51 | Bsup = B; |
52 | EpsilonX = EpsX; |
53 | EpsilonF = EpsF; |
54 | Itermax = NbIterations; |
55 | Done = Standard_False; |
56 | X = RealLast(); |
57 | DFx = 0; |
58 | Fx = RealLast(); |
59 | It = 0; |
60 | } |
61 | |
62 | |
63 | math_NewtonFunctionRoot::math_NewtonFunctionRoot (math_FunctionWithDerivative& F, |
64 | const Standard_Real Guess, |
65 | const Standard_Real EpsX , |
66 | const Standard_Real EpsF , |
67 | const Standard_Integer NbIterations ){ |
68 | EpsilonX = EpsX; |
69 | EpsilonF = EpsF; |
70 | Itermax = NbIterations; |
71 | Binf = RealFirst(); |
72 | Bsup = RealLast(); |
73 | Done = Standard_False; |
74 | X = RealLast(); |
75 | DFx = 0; |
76 | Fx = RealLast(); |
77 | It = 0; |
78 | Perform(F, Guess); |
79 | } |
80 | |
81 | |
82 | void math_NewtonFunctionRoot::Perform(math_FunctionWithDerivative& F, |
83 | const Standard_Real Guess) { |
84 | |
85 | Standard_Real Dx; |
86 | Standard_Boolean Ok; |
87 | Standard_Real AA, BB; |
88 | |
89 | //-------------------------------------------------- |
90 | //-- lbr le 12 Nov 97 |
91 | //-- la meilleure estimation n est pas sauvee et on |
92 | //-- renvoie une solution plus fausse que Guess |
93 | Standard_Real BestX=X,BestFx=RealLast(); |
94 | //-- |
95 | |
96 | if ( Binf < Bsup) { |
97 | AA = Binf; |
98 | BB = Bsup; |
99 | } |
100 | else { |
101 | AA = Bsup; |
102 | BB = Binf; |
103 | } |
104 | |
105 | Dx = RealLast(); |
106 | Fx = RealLast(); |
107 | X = Guess; |
108 | It = 1; |
109 | while ( (It <= Itermax) && ( (Abs(Dx) > EpsilonX) || |
110 | (Abs(Fx) > EpsilonF) ) ) { |
111 | Ok = F.Values(X,Fx,DFx); |
112 | |
113 | Standard_Real AbsFx = Fx; if(AbsFx<0) AbsFx=-AbsFx; |
114 | if(AbsFx<BestFx) { |
115 | BestFx=AbsFx; |
116 | BestX =X; |
117 | } |
118 | |
119 | if (Ok) { |
120 | if (DFx == 0.) { |
121 | Done = Standard_False; |
122 | It = Itermax + 1; |
123 | } |
124 | else { |
125 | Dx = Fx/DFx; |
126 | X -= Dx; |
127 | // Limitation des variations de X: |
128 | if (X <= AA) X = AA; |
129 | if (X >= BB) X = BB; |
130 | It++; |
131 | } |
132 | } |
133 | else { |
134 | Done = Standard_False; |
135 | It = Itermax + 1; |
136 | } |
137 | } |
138 | X = BestX; |
139 | |
140 | if (It <= Itermax) { |
141 | Done = Standard_True; |
142 | } |
143 | else |
144 | { |
145 | Done = Standard_False; |
146 | } |
147 | } |
148 | |
149 | |
150 | void math_NewtonFunctionRoot::Dump(Standard_OStream& o) const { |
151 | |
152 | o <<"math_NewtonFunctionRoot "; |
153 | if (Done) { |
154 | o << " Status = Done \n"; |
155 | o << " Location found = " << X <<"\n"; |
156 | o << " function value at this minimum = " << Fx <<"\n"; |
157 | o << " Number of iterations = " << It <<"\n"; |
158 | } |
159 | else { |
160 | o << "Status = not Done \n"; |
161 | } |
162 | } |
163 | |
164 | |
165 | |