Find approximate solution for \(f(x) = 0\) using Newton-Raphson interpolation algorithm.
More...
#include <complex.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
|
double complex | func (double complex x) |
|
double complex | d_func (double complex x) |
|
int | main (int argc, char **argv) |
|
Find approximate solution for \(f(x) = 0\) using Newton-Raphson interpolation algorithm.
- Author
- Krishna Vedala
◆ ACCURACY
◆ d_func()
double complex d_func |
( |
double complex |
x | ) |
|
Return first order derivative of the function. \(f'(x)\)
◆ func()
double complex func |
( |
double complex |
x | ) |
|
Return value of the function to find the root for. \(f(x)\)
◆ main()
int main |
( |
int |
argc, |
|
|
char ** |
argv |
|
) |
| |
main function
40 double complex cdelta = 1;
46 double complex root = (rand() % 100 - 50) + (rand() % 100 - 50) * I;
48 unsigned long counter = 0;
50 while (delta >
ACCURACY && counter < ULONG_MAX)
55 delta = fabs(cabs(cdelta));
57 #if defined(DEBUG) || !defined(NDEBUG)
58 if (counter % 50 == 0)
60 double r = creal(root);
61 double c = cimag(root);
63 printf(
"Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter,
64 r, c >= 0 ?
'+' :
'-', c >= 0 ? c : -c, delta);
69 double r = creal(root);
70 double c = fabs(cimag(root)) <
ACCURACY ? 0 : cimag(root);
72 printf(
"Iter %5lu: Root: %4.4g%c%4.4gi\t\tdelta: %.4g\n", counter, r,
73 c >= 0 ?
'+' :
'-', c >= 0 ? c : -c, delta);