Advertisement
cd62131

complex solution

May 17th, 2018
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.12 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3.  
  4. typedef struct {
  5.     double re, im;
  6. } Complex;
  7.  
  8. int main(void) {
  9.     Complex z;
  10.     int retry = 1;
  11.     do {
  12.         --retry;
  13.         Complex z0;
  14.         printf("初期値のRe:"); scanf_s("%lf", &z0.re);
  15.         printf("初期値のIm:"); scanf_s("%lf", &z0.im);
  16.         int i = 0;
  17.         double a = 0.00001;
  18.         do {
  19.             i++;
  20.             z.re = z0.re;
  21.             z.im = z0.re;
  22.             double f_re = z0.re * z0.re - z0.im * z0.im + 5 * z0.re + 7;
  23.             double f_im = 2 * z0.re * z0.im + 5 * z0.im;
  24.             double df_re = 2 * z0.re + 5;
  25.             double df_im = 2 * z0.im;
  26.             double denom = df_re * df_re + df_im * df_im;
  27.             z0.re = z0.re - (f_re * df_re + f_im * df_im) / denom;
  28.             z0.im = z0.im - (- f_re * df_im + f_im * df_re) / denom;
  29.         } while (fabs(z.re - z0.re) > a && fabs(z.im - z0.im) > a);
  30.         z.re = z0.re;
  31.         z.im = z0.im;
  32.  
  33.         printf("Re=%f , Im=%f, 回数は%d\n", z.re, z.im, i);
  34.  
  35.         puts("続けるなら0を");
  36.         scanf_s("%d", &retry);
  37.     } while (retry > 0);
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement