-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewtonRaphsonNR.cpp
57 lines (50 loc) · 1.09 KB
/
newtonRaphsonNR.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
52
53
54
55
56
57
/* Numerical Methods
Lab 3
Netwon Raphson Method
Anish Bhusal
072BCT505
*/
#include<iostream>
#include<cmath>
using namespace std;
float f(float x)
{
return ((x*x*x)+3*x*x-2);
}
float df(float x)
{
return (3*x*x+3*x);
}
int main()
{
float x0,xN;
cout<<"Enter the value of approximate root x0 :";
cin>>x0;
int count=0; //checking oscillations
bool rootfound;
do
{
xN=x0-f(x0)/df(x0);
x0=xN;
count++;
if(count==50)
{
cout<<"Oscillation occured : Couldn't locate exact root in 50 iterations"<<endl;
rootfound=false;
break;
}
if(df(x0)==0)
{
cout<<"The tangent line is parallel to x-axis: Couldn't locate root"<<endl;
rootfound=false;
break;
}
rootfound=true;
}
while((fabs(f(x0)))>0.0005);
if(rootfound==true)
{
cout<<"The root of the equation is "<<x0<<endl;
}
return 0;
}