-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRootFinding.cpp
More file actions
131 lines (94 loc) · 2.49 KB
/
Copy pathRootFinding.cpp
File metadata and controls
131 lines (94 loc) · 2.49 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double x) {
return 4 * x * x * x - 3 * x;
}
double f_derivative(double x) {
return 12 * x * x - 3;
}
double g(double x) {
return (x * x + 5) / 5;
}
void 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 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 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\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;
}
int main() {
double a, b;
bisection(a, b);
double x0;
newtonRaphson(x0);
double xn;
FixedPoint(xn);
return 0;
}