-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBroydenPlugin.cpp
More file actions
51 lines (45 loc) · 1.46 KB
/
BroydenPlugin.cpp
File metadata and controls
51 lines (45 loc) · 1.46 KB
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
#include "BroydenPlugin.hpp"
#include "Traits.hpp"
#include "FactoryTraits.hpp"
namespace Solver{
NumericalSolutionType Broyden::solve() const{
double Dfx0{dfx0};
double X0{x0};
double fx0{fun(x0)};
unsigned int iter{0u};
double deltax;
double X1;
double fx1;
double diff{opt.tol_abs+1};
while(diff >= opt.tol_abs && iter < opt.maxIter){
++iter;
deltax = -fx0/Dfx0;
X1=X0+deltax;
fx1 = fun(X1);
Dfx0 += (fx1*deltax)/(deltax*deltax);
diff = std::abs(deltax);
X0 = X1;
fx0 = fx1;
}
if (iter == opt.maxIter && diff > opt.tol_abs){ // If does not converge
return std::make_pair(X1, false);
}else{ // If converge
return std::make_pair(X1, true);
}
}
// Implementation of plugin architecture
namespace
{
struct LoadF
{
LoadF(){
// Adding the method (Definited by the SolverBase(_fun) constructor)
// to solverFactory
solverFactory["Broyden"] = [](FunctionTraits::FunctionType _fun) {
return std::make_unique<Broyden>(_fun);
};
}
};
const LoadF loadf; // local variable when created loads the stuff
} // namespace
}// namespace Solver