Skip to content

Commit f6acbdb

Browse files
author
Hrithik Raj
authored
Add files via upload
1 parent 13cd912 commit f6acbdb

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

Newton-Raphson.cpp

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/* Newton Raphson Method */
2+
#include <iostream>
3+
#include <iomanip>
4+
#include <math.h>
5+
using namespace std;
6+
float f(float x){
7+
return x*log10(x)-1.2;
8+
}
9+
float df(float x){
10+
return log10(x) + 0.43429;
11+
}
12+
int main(){
13+
int itr,maxitr;
14+
float h,x0,x1,aerr;
15+
cout << "Enter x0,allowed error,"
16+
<< "maximum iterations" << endl;
17+
cin >> x0 >> aerr >> maxitr;
18+
cout << fixed;
19+
for (itr=1;itr<=maxitr;itr++){
20+
h = f(x0)/df(x0);
21+
x1 = x0-h;
22+
cout << "Iteration no." << setw(3) << itr
23+
<< "X = " << setw(9) << setprecision(6)
24+
<< x1 << endl;
25+
if (fabs(h) < aerr){
26+
cout << "After" << setw(3) << itr
27+
<< "iterations, root = "
28+
<< setw(8) << setprecision(6) << x1;
29+
return 0;
30+
}
31+
x0 = x1;
32+
}
33+
cout << "Iterations not sufficient,"
34+
<< "solution does not converge" << endl;
35+
return 1;
36+
}

0 commit comments

Comments
 (0)