diff --git a/cpp/NonLinear_Methods/newton_raphson.cpp b/cpp/NonLinear_Methods/newton_raphson.cpp new file mode 100644 index 0000000..65d0d52 --- /dev/null +++ b/cpp/NonLinear_Methods/newton_raphson.cpp @@ -0,0 +1,36 @@ + +// C++ program for implementation of Newton Raphson Method +#include +#define ERROR 0.001 +using namespace std; + +double func(double x) +{ + return x*x*x - x*x + 2; //Define a Function +} + +double derivative(double x) +{ + return 3*x*x - 2*x; //Define its derivative +} + +// Function to find the root +void NRaphson(double x) +{ + double h = func(x) / derivative(x); + while (abs(h) >= ERROR) + { + h = func(x)/derivative(x); + x = x - h; + } + + cout << "One of the roots is : " << x; +} + +int main() +{ + double x_0 = -10; // Initial Guess + NRaphson(x_0); + return 0; +} +