-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.cpp
More file actions
115 lines (83 loc) · 2.36 KB
/
Copy pathroot.cpp
File metadata and controls
115 lines (83 loc) · 2.36 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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include "root.hpp"
using namespace std;
Root::Root() {}
double Root::f(double x) {
return x* x + 3 * x - 5;
}
double Root::f_derivative(double x) {
return x* x + 3 * x - 5;
}
double Root::g(double x) {
return x* x + 3 * x - 5;
}
void Root::bisection(double a, double b) {
double x, fx, fa, fb;
cout << fixed << setprecision(5);
cout << "Enter the value of a: ";
cin >> a;
cout << "Enter the value of b: ";
cin >> b;
cout << "\nIteration\t a\t\t b\t\t f(a)\t\t f(b)\t\t x\t\t f(x)" << endl;
int iteration = 1;
fa = f(a);
fb = f(b);
do {
x = (a + b) / 2;
fx = f(x);
cout << iteration << "\t\t" << a << "\t" << b << "\t" << fa << "\t" << fb << "\t\t" << x << "\t" << fx << endl;
if (fx < 0) {
a = x;
fa = fx;
} else {
b = x;
fb = fx;
}
iteration++;
} while (fabs(b - a) > 0.00001);
cout << "The root is " << x << endl;
}
void Root::newtonRaphson(double x0) {
double x, fx, fdx, x_next;
const double tolerance = 0.00001;
int iteration = 1;
cout << "Enter the initial guess: ";
cin >> x0;
x = x0;
cout << fixed << setprecision(5);
cout << "\nIteration\t x\t\t f(x)\t\t f'(x)" << endl;
do {
fx = f(x);
fdx = f_derivative(x);
if (fdx == 0) {
cout << "Error: Derivative is zero" << endl;
return;
}
x_next = x - (fx / fdx);
cout << iteration << "\t\t" << x << "\t" << fx << "\t" << fdx << endl;
if (fabs(x_next - x) < tolerance)
break;
x = x_next;
iteration++;
} while (true);
cout << "\nApproximate Root from Newton-Raphson: " << x << endl;
}
void Root::FixedPoint(double x0) {
const double tolerance = 0.00001;
int maxIterations = 100;
int iteration = 1;
double x = x0, x_next;
cout << "Enter the initial guess: ";
cin >> x0;
x = x0;
cout << fixed << setprecision(5);
cout << "\nIteration\t x\t\t g(x)\n";
do {
x_next = g(x);
cout << iteration << "\t\t" << x << "\t" << x_next << endl;
if (fabs(x_next - x) < tolerance)
break;
x = x_next;
iteration++;
} while (iteration <= maxIterations);
cout << "\nApproximate Root from Fixed-Point Iteration: " << x << endl;
}