newton_raphson_method.cpp File Reference

Solve the equation \(f(x)=0\) using Newton-Raphson method for both real and complex solutions. More...

`#include <cmath>`
`#include <ctime>`
`#include <iostream>`
`#include <limits>`
Include dependency graph for newton_raphson_method.cpp:

## Functions

static double | eq (double i)
static double | eq_der (double i)
int | main ()

## Variables

constexpr double | EPSILON = 1e-10
| system accuracy limit
constexpr int16_t | MAX_ITERATIONS = INT16_MAX
| Maximum number of iterations.

## Detailed Description

Solve the equation \(f(x)=0\) using Newton-Raphson method for both real and complex solutions.

The \((i+1)^\text{th}\) approximation is given by:

\[ x_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)} \]

Author
  Krishna Vedala

See also
  bisection_method.cpp, false_position.cpp

## Function Documentation

## eq()

static double eq | ( | double | i | ) |

static

define \(f(x)\) to find root for. Currently defined as:

\[ f(x) = x^3 - 4x - 9 \]

{
return (std::pow(i, 3) - (4 * i) - 9); // original equation
}
std::pow
T pow(T... args)
Here is the call graph for this function:

## eq_der()

static double eq_der | ( | double | i | ) |

static

define the derivative function \(f'(x)\) For the current problem, it is:

\[ f'(x) = 3x^2 - 4 \]

{
return ((3 * std::pow(i, 2)) - 4); // derivative of equation
}
Here is the call graph for this function:

## main()

int main | ( | void |  | ) |

Main function

{
std::srand(std::time(nullptr)); // initialize randomizer
double z = NAN, c = std::rand() % 100, m = NAN, n = NAN;
int i = 0;
std::cout << "\nInitial approximation: " << c;
// start iterations
for (i = 0; i < MAX_ITERATIONS; i++) {
m = eq(c);
n = eq_der(c);
z = c - (m / n);
c = z;
if (std::abs(m) < EPSILON) { // stoping criteria
break;
}
}
std::cout << "\n\nRoot: " << z << "\t\tSteps: " << i << std::endl;
return 0;
}
std::cout
std::endl
T endl(T... args)
eq
static double eq(double i)
Definition: newton_raphson_method.cpp:29
eq_der
static double eq_der(double i)
Definition: newton_raphson_method.cpp:39
MAX_ITERATIONS
constexpr int16_t MAX_ITERATIONS
Maximum number of iterations.
Definition: newton_raphson_method.cpp:21
EPSILON
constexpr double EPSILON
system accuracy limit
Definition: newton_raphson_method.cpp:20
std::rand
T rand(T... args)
std::srand
T srand(T... args)
std::time
T time(T... args)
Here is the call graph for this function: