From 0dc72ccc9f6724932e8447ac6b943460593bf43e Mon Sep 17 00:00:00 2001 From: Swarali-glitch <71065651+Swarali-glitch@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:02:55 +0530 Subject: [PATCH 1/2] Create Newton_Raphson.cpp This is a simple program to find the roots of an equation using the Newton Raphson method in C++ --- Newton_Raphson.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 Newton_Raphson.cpp diff --git a/Newton_Raphson.cpp b/Newton_Raphson.cpp new file mode 100644 index 0000000..9e8e1ef --- /dev/null +++ b/Newton_Raphson.cpp @@ -0,0 +1,39 @@ +#include +#define EPSILON 0.001 +using namespace std; + +// An example function whose solution is determined using +// Bisection Method. The function is x^3 - x^2 + 2 +double func(double x) +{ + return x*x*x - x*x + 2; +} + +// Derivative of the above function which is 3*x^x - 2*x +double derivFunc(double x) +{ + return 3*x*x - 2*x; +} + +// Function to find the root +void newtonRaphson(double x) +{ + double h = func(x) / derivFunc(x); + while (abs(h) >= EPSILON) + { + h = func(x)/derivFunc(x); + + // x(i+1) = x(i) - f(x) / f'(x) + x = x - h; + } + + cout << "The value of the root is : " << x; +} + +// Driver program to test above +int main() +{ + double x0 = -20; // Initial values assumed + newtonRaphson(x0); + return 0; +} From d09b4207729494207360f312e7c5c40939ddb0d4 Mon Sep 17 00:00:00 2001 From: Swarali-glitch <71065651+Swarali-glitch@users.noreply.github.com> Date: Sun, 18 Oct 2020 21:03:59 +0530 Subject: [PATCH 2/2] Update Newton_Raphson.cpp --- Newton_Raphson.cpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/Newton_Raphson.cpp b/Newton_Raphson.cpp index 9e8e1ef..42bf457 100644 --- a/Newton_Raphson.cpp +++ b/Newton_Raphson.cpp @@ -2,20 +2,16 @@ #define EPSILON 0.001 using namespace std; -// An example function whose solution is determined using -// Bisection Method. The function is x^3 - x^2 + 2 double func(double x) { return x*x*x - x*x + 2; } -// Derivative of the above function which is 3*x^x - 2*x double derivFunc(double x) { return 3*x*x - 2*x; } -// Function to find the root void newtonRaphson(double x) { double h = func(x) / derivFunc(x); @@ -23,17 +19,15 @@ void newtonRaphson(double x) { h = func(x)/derivFunc(x); - // x(i+1) = x(i) - f(x) / f'(x) x = x - h; } cout << "The value of the root is : " << x; } -// Driver program to test above int main() { - double x0 = -20; // Initial values assumed + double x0 = -20; newtonRaphson(x0); return 0; }