-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.cpp
52 lines (45 loc) · 2.53 KB
/
main.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
#include <fmt/format.h>
#include "iteration.h"
#include "brent.h"
#include "dekker.h"
#include "newton.h"
#include "bisection.h"
#include "secant.h"
int main() {
auto f = [](double x){
return x*x*x-x*x-x-1;
};
auto fPrime = [](double x) {
return 3*x*x-2*x-1;
};
const double epsilon = 1e-10;
Iteration *bisection = new Bisection(epsilon, f);
Iteration *secant1 = new Secant(epsilon, f);
Iteration *secant2 = new Secant(epsilon, f);
Iteration *newton = new Newton(epsilon, f, fPrime);
Iteration *dekker1 = new Dekker(epsilon, f);
Iteration *dekker2 = new Dekker(epsilon, f);
Iteration *brent1 = new Brent(epsilon, f);
Iteration *brent2 = new Brent(epsilon, f);
fmt::print("Solving f(x)=0 for x^3-x^2-x-1 with Bisection, Secant, Newton, Dekker and Brent Method\n\n");
const double bisectionResults = bisection->solve(0, 2);
const double secantResults1 = secant1->solve(1.5, 2);
const double secantResults2 = secant2->solve(0, 2);
const double newtonResults = newton->solve(1.5);
const double dekkerResults1 = dekker1->solve(1.5, 2);
const double dekkerResults2 = dekker2->solve(0, 2);
const double brentResults1 = brent1->solve(1.5, 2);
const double brentResults2 = brent2->solve(0, 2);
fmt::print("\n");
fmt::print("Comparison of Effort\n");
fmt::print("{:<20}|{:<15}|{:<20}|{:<10}\n", "Method", "Start/Range", "No. of Iterations", "Approximated Root");
fmt::print("--------------------------------------------------------------------------\n");
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Bisection", "[0, 2]", bisection->numberOfIterations(), bisectionResults);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Secant Method", "[1.5, 2]", secant1->numberOfIterations(), secantResults1);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Secant Method", "[0, 2]", secant2->numberOfIterations(), secantResults2);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Newton Method", "[x^0=1.5]", newton->numberOfIterations(), newtonResults);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Dekker Method", "[1.5, 2]", dekker1->numberOfIterations(), dekkerResults1);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Dekker Method", "[0, 2]", dekker2->numberOfIterations(), dekkerResults2);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Brent Method", "[1.5, 2]", brent1->numberOfIterations(), brentResults1);
fmt::print("{:<20}|{:<15}|{:<20}|{:<10.15f}\n", "Brent Method", "[0, 2]", brent2->numberOfIterations(), brentResults2);
}