Threads
newton_raphson_ ( 23136 ) - newton_raphson_ ( 23136 ) stack: main(numerical_methods/newton_raphson_method.cpp:44)
/**
* \file
* \brief Solve the equation \f$f(x)=0\f$ using [Newton-Raphson
* method](https://en.wikipedia.org/wiki/Newton%27s_method) for both real and
* complex solutions
*
* The \f$(i+1)^\text{th}\f$ approximation is given by:
* \f[
* x_{i+1} = x_i - \frac{f(x_i)}{f'(x_i)}
* \f]
*
* \author [Krishna Vedala](https://github.com/kvedala)
* \see bisection_method.cpp, false_position.cpp
*/
#include
#include
#include
#include
constexpr double EPSILON = 1e-10; ///< system accuracy limit
constexpr int16_t MAX_ITERATIONS = INT16_MAX; ///< Maximum number of iterations
/** define \f$f(x)\f$ to find root for.
* Currently defined as:
* \f[
* f(x) = x^3 - 4x - 9
* \f]
*/
static double eq(double i) {
return (std::pow(i, 3) - (4 * i) - 9); // original equation
}
/** define the derivative function \f$f'(x)\f$
* For the current problem, it is:
* \f[
* f'(x) = 3x^2 - 4
* \f]
*/
static double eq_der(double i) {
return ((3 * std::pow(i, 2)) - 4); // derivative of equation
}
/** Main function */
int main() {
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;
}
Variables All
No. | From | Name | Value |
---|---|---|---|
NA | NA | NA | NA |
Process | Thread Filter |
---|---|
23136 newton_raphson_ | 23136 newton_raphson_ |
No. | PN | PID | TID | TN | Message |
---|---|---|---|---|---|
1 | newton_raphson_ | 23136 | 23136 | newton_raphson_ | |
2 | newton_raphson_ | 23136 | 23136 | newton_raphson_ | Initial approximation: 97 |
3 | newton_raphson_ | 23136 | 23136 | newton_raphson_ | |
4 | newton_raphson_ | 23136 | 23136 | newton_raphson_ | Root: 2.70653 Steps: 14 |
END | 0 | 0 | 0 | 0 | 0 |
×
Commands and Shortcuts
No. | Command | Shortcuts | Description |
---|---|---|---|
1 | GB | Alt + LEFT, Alt + A | Go Backward |
2 | GF | Alt + RIGHT, Alt + D | Go Foreward |
3 | PPE | Alt + UP, Alt + W | Previous Process End |
4 | NPS | Alt + DOWN, Alt + S | Next Process Start |
5 | PB | Ctrl + LEFT, Ctrl + A | current Process Backward |
6 | PF | Ctrl + RIGHT, Ctrl + D | current Process Foreward |
7 | PPTE | Ctrl + UP, Ctrl + W | go to current Process's Previous Thread's End |
8 | PNTS | Ctrl + DOWN, Ctrl + S | go to current Process's Next Thread's Start |
9 | TB | LEFT, A | current Thread Backward |
10 | TF | RIGHT, D | current Thread Foreward |
11 | LU | UP, W | go Line Up of current code block in current thread |
12 | LD | DOWN, S | go Line Down of current code block in current thread |
13 | LP | Shift + UP, Shift + W | go to the occurrence of current line in Previous Loop |
14 | LD | Shift + DOWN, Shift + S | go to the occurrence of current line in Next Loop |
15 | BS | Home | go to code Block Start |
16 | BE | End | go to code Block End |
Project: | algorithms-cpp |
Update: | 20240212 |
Commit: | 2dadbf73f |
Source Code: | numerical_methods.newton_raphson_method |
BuildTool: | cpp17 |
Compiler: | cpp17 |
Runtime: | Openjdk17 |
System: | MySystemD |
Kernel: | Linux5.10.211 |
Cpu: | Intel:Corei7-7700K |
Machine: | AwesomeMachine |