-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNewtonRaphson.cpp
51 lines (41 loc) · 1.01 KB
/
NewtonRaphson.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
#include <iostream>
#include <iomanip>
#include <math.h>
#include <stdlib.h>
#define f(x) x *x - 3 * x + 2
#define g(x) 2 * x - 3
using namespace std;
int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
cout << setprecision(6) << fixed;
cout << "Enter initial guess: ";
cin >> x0;
cout << "Enter tolerable error: ";
cin >> e;
cout << "Enter maximum iteration: ";
cin >> N;
cout << endl
<< "*********************" << endl;
cout << "Newton Raphson Method" << endl;
cout << "*********************" << endl;
do
{
g0 = g(x0);
f0 = f(x0);
if (g0 == 0.0)
{
cout << "Mathematical Error.";
exit(0);
}
x1 = x0 - f0 / g0;
cout << "Iteration-" << step << ":\t x = " << setw(10) << x1 << " and f(x) = " << setw(10) << f(x1) << endl;
x0 = x1;
step = step + 1;
f1 = f(x1);
} while (fabs(f1) > e);
cout << endl
<< "Root is: " << x1;
return 0;
}