A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://TheAlgorithms.github.io/C-Plus-Plus/de/dd3/newton__raphson__method_8cpp.html below:

TheAlgorithms/C++: numerical_methods/newton_raphson_method.cpp File Reference

Loading...

Searching...

No Matches

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

#include <cmath>
#include <cstdint>
#include <ctime>
#include <iostream>
#include <limits>

Go to the source code of this file.

static double  eq (double i) static double  eq_der (double i) int  main () constexpr double  EPSILON = 1e-10   system accuracy limit
constexpr int16_t  MAX_ITERATIONS = INT16_MAX   Maximum number of iterations.

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)} \]

See also
bisection_method.cpp, false_position.cpp

Definition in file newton_raphson_method.cpp.

◆ eq()

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

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

Definition at line 30 of file newton_raphson_method.cpp.

30 {

31 return (std::pow(i, 3) - (4 * i) - 9);

32}

◆ eq_der() double eq_der ( double i ) static

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

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

Definition at line 40 of file newton_raphson_method.cpp.

40 {

41 return ((3 * std::pow(i, 2)) - 4);

42}

◆ main()

Main function

Definition at line 45 of file newton_raphson_method.cpp.

45 {

46 std::srand(std::time(nullptr));

47

48 double z = NAN, c = std::rand() % 100, m = NAN, n = NAN;

49 int i = 0;

50

51 std::cout << "\nInitial approximation: " << c;

52

53

57

58 z = c - (m / n);

59 c = z;

60

61 if (std::abs(m) < EPSILON) {

62 break;

63 }

64 }

65

66 std::cout << "\n\nRoot: " << z << "\t\tSteps: " << i << std::endl;

67 return 0;

68}

#define MAX_ITERATIONS

Maximum number of iterations to check.

static double eq(double i)

static double eq_der(double i)

◆ EPSILON ◆ MAX_ITERATIONS int16_t MAX_ITERATIONS = INT16_MAX constexpr

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4