-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathroots.cpp
82 lines (72 loc) · 2.75 KB
/
roots.cpp
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
// Copyright (c) 2023- Juraj Szitas
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "nlsolver.h" // NOLINT
// experimental solvers
// uni-variate optimizer
using nlsolver::rootfinder::bisection;
using nlsolver::rootfinder::brent;
using nlsolver::rootfinder::chandrupatla;
using nlsolver::rootfinder::false_position;
using nlsolver::rootfinder::itp;
using nlsolver::rootfinder::ridders;
using nlsolver::rootfinder::tiruneh;
int main() {
// uni-variate optimization example
std::cout << "Brent" << std::endl;
// Guerrero<double> box_cox_lambda;
struct Problem {
std::array<double, 2> bounds =
//{1, 10};
//{-2, 1};
{-4, 8}; // 4/3};//4/3};
double operator()(const double x) {
// return std::pow(x,2)/12 + x -4;
// return (x+3)*std::pow(x-1,2);
return std::pow(x, 3) - x - 2.;
}
double upper() { return bounds[1]; }
double lower() { return bounds[0]; }
};
Problem p;
double x = p.lower();
brent(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
std::cout << "Bisection" << std::endl;
x = p.lower();
bisection(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
std::cout << "False position" << std::endl;
x = p.lower();
false_position(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
std::cout << "Ridders" << std::endl;
x = p.lower();
ridders(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
std::cout << "Tiruneh" << std::endl;
x = (p.upper() + p.lower()) / 2;
tiruneh(p, x, {x - 0.1, x, x + 0.1}).print();
std::cout << "x: " << x << std::endl;
std::cout << "ITP" << std::endl;
x = p.lower();
itp(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
std::cout << "Chandrupatla" << std::endl;
x = p.lower();
chandrupatla(p, x, p.lower(), p.upper()).print();
std::cout << "x: " << x << std::endl;
}