Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newton_Raphson_Method #1731

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions C++/Maths/Newton_Raphson.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// C++ program for implementation of Newton Raphson Method for Solving Equations

#include <iostream>
#include <bits/stdc++.h>
#define EPSILON 0.001
using namespace std;

// Bisection Method.
// We are taking a function whose roots we want to find.
// 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
// Its an Iterative method thats why this while loop is used.
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;
}

// Main Function / Main code to implement above function
int main()
{
double x0;
cout << "Enter Initial Guess:" << endl;
cin >> x0; // Enter Initial Guess here
newtonRaphson(x0);
return 0;
}
Loading