-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBisectionPlugin.cpp
More file actions
57 lines (53 loc) · 1.34 KB
/
BisectionPlugin.cpp
File metadata and controls
57 lines (53 loc) · 1.34 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
52
53
54
55
56
57
#include "BisectionPlugin.hpp"
#include "Traits.hpp"
#include "FactoryTraits.hpp"
#include <iostream>
namespace Solver{
NumericalSolutionType Bisection::solve() const{
point a = opt.a; // to allow modification in the while-loop
point b = opt.b;
double ya = fun(a);
double yb = fun(b);
if(ya * yb > 0){ // If the the function does not change sign at the two end values
std::cerr << std::endl
<< "Function must change sign at the two end values"
<< std::endl;
return std::make_pair(0, false);
}
double delta = b - a;
double yc{ya};
double c{a};
while(std::abs(delta) > 2 * opt.tol_abs)
{
c = (a + b) / 2.;
yc = fun(c);
if(yc * ya < 0.0)
{
yb = yc;
b = c;
}
else
{
ya = yc;
a = c;
}
delta = b - a;
}
return std::make_pair((a + b) / 2., true);
}
// Implementation of plugin architecture
namespace
{
struct LoadF
{
LoadF(){
// Adding the method (Definited by the SolverBase(_fun) constructor)
// to solverFactory
solverFactory["Bisection"] = [](FunctionTraits::FunctionType _fun) {
return std::make_unique<Bisection>(_fun);
};
}
};
const LoadF loadf; // local variable when created loads the stuff
} // namespace
}// namespace Solver