-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterval_newton.cpp
More file actions
33 lines (28 loc) · 944 Bytes
/
interval_newton.cpp
File metadata and controls
33 lines (28 loc) · 944 Bytes
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
#include "Interval.h"
#include "EmptyIntersectionException.h"
#include "FAD.h"
using std::pair;
template<typename T>
T func(T x) { return exp(x) - 10; }
template<class Fun>
Interval IntervalNewtonStep(Fun f, const Interval &x) {
double mid = x.center(); // enclosure choice
Interval N = mid - (Interval(f(mid)) / autodiff(f, x).get_dx());
return N && x;
}
template<class Fun>
void IntervalNewton(Fun f, Interval x, const unsigned long TOL) {
Interval x0 = x;
while (x.width() > TOL) {
try { x = IntervalNewtonStep(f, x); } catch (EmptyIntersectionException &warning) {
std::cout << "No zeros in the given domain.\n";
return;
}
std::cout << x << "\n";
}
if (x < x0) std::cout << "There is a unique zero in the final interval.\n";
}
int main() {
IntervalNewton([](auto x) { return func(x); }, Interval(-1, 1), static_cast<unsigned long>(0.001));
return 0;
}