Advertisement
cd62131

complex solution by Newton's method

May 16th, 2018
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.83 KB | None | 0 0
  1. #include <complex.h>
  2. #include <float.h>
  3. #include <math.h>
  4. #include <stdio.h>
  5.  
  6. static inline complex double f(complex double z) {
  7.     return z * z + 5 * z + 7;
  8. }
  9.  
  10. static inline complex double df(complex double z) {
  11.     return 2 * z + 5;
  12. }
  13.  
  14. int main(void) {
  15.     for (int i = -10; i <= 10; ++i) {
  16.         for (int j = -10; j <= 10; ++j) {
  17.             if (i == 0 || j == 0) {
  18.                 continue;
  19.             }
  20.             int c = 0;
  21.             complex double z0 = i / 10. + j / 10. * I;
  22.             complex double z = z0 - f(z0) / df(z0);
  23.             for (; fabs(z - z0) > DBL_EPSILON;
  24.                 ++c, z0 = z, z = z0 - f(z0) / df(z0)) {
  25.                 ;
  26.             }
  27.             printf("z = %f %+f * I, c = %d, z0 = %f %+f * I\n",
  28.                 creal(z), cimag(z), c, i / 10., j / 10.);
  29.         }
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement