-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathnewtonRaphsonApplication.cpp
More file actions
51 lines (38 loc) · 1.2 KB
/
newtonRaphsonApplication.cpp
File metadata and controls
51 lines (38 loc) · 1.2 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
#include <iostream>
#include <cmath>
using namespace std;
double function(double x) {
return x * x - 16.0; //f(x) = x^2 - 16
}
// The derivative of the function
double derivative(double x) {
return 2.0 * x; // f'(x) = x^2 - 16 is 2x
}
// Newton-Raphson method
double newtonRaphson(double initialGuess, double tolerance, int maxIterations) {
double x = initialGuess;
for (int i = 0; i < maxIterations; ++i) {
double f_x = function(x);
double f_prime_x = derivative(x);
if (abs(f_prime_x) < 1e-6) {
cout << "Derivative is too small. Cannot continue." << endl;
return x;
}
double x_next = x - (f_x / f_prime_x);
if (abs(x_next - x) < tolerance) {
cout << "Converged to a root after " << i + 1 << " iterations." << endl;
return x_next;
}
x = x_next;
}
cout << "Did not converge after " << maxIterations << " iterations." << endl;
return x;
}
int main() {
double initialGuess = 4.0;
double tolerance = 1e-6;
int maxIterations = 100;
double root = newtonRaphson(initialGuess, tolerance, maxIterations);
cout << "Approximate root: " << root << endl;
return 0;
}