Advertisement
Guest User

Beautified

a guest
Dec 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <malloc.h>
  4. #include <math.h>
  5. #include <mem.h>
  6.  
  7. int N = 5;
  8.  
  9. typedef double (*pointer)(double, double); //pointer on function type (double, double) -> double
  10.  
  11. double f0(double x, double y)
  12. {
  13.     return cos(x)/x+(-1.0)*(y+0)/x;
  14. }
  15.  
  16. double f1(double x, double y)
  17. {
  18.     return x/50;
  19. }
  20.  
  21. double f2 (double x, double y)
  22. {
  23.     return (-2.0)* (y+2)/ x;
  24. }
  25.  
  26. double f3 (double x, double y)
  27. {
  28.     return -cos(x);
  29. }
  30.  
  31. double f4 (double x, double y)
  32. {
  33.     return sin (x);
  34. }
  35.  
  36. void myprint(FILE *f, double x) //print x into command line and write it into f
  37. {
  38.     printf("%lf\t", x);
  39.     fprintf(f, "%lf\t", x);
  40. }
  41.  
  42. void myprintend(FILE *f) //print "\n" into command line and write it into f after one iteration
  43. {
  44.     printf("\n");
  45.     fprintf(f, "\n");
  46. }
  47.  
  48. double* StepEuler (double x, double *y, double step, int N, double (*func[])(double, double ))
  49. {
  50.     for (int i=0; i<N; i++)
  51.         y[i] += func[i](x, y[i]) * step;
  52.     return y;
  53. }
  54.  
  55. double* StepRK(double x, double *y, double step, int N, double (*func[])(double, double))
  56. {
  57.     double *k1 = malloc(N * sizeof(double));
  58.     double *k2 = malloc(N * sizeof(double));
  59.     double *k3 = malloc(N * sizeof(double));
  60.     double *k4 = malloc(N * sizeof(double));
  61.     double *temp = malloc(N * sizeof(double));
  62.  
  63.     for (int i = 0; i < N; ++i)
  64.         k1[i] = func[i](x,y[i]);
  65.     for (int i = 0; i < N; ++i)
  66.     {
  67.         temp[i] = y[i] + k1[i]*step/2;
  68.         k2[i] = func[i](x + step/2, y[i] + k1[i]*step/2);
  69.     }
  70.     for (int i = 0; i < N; ++i)
  71.     {
  72.         temp[i] = y[i] + k2[i]*step/2;
  73.         k3[i] = func[i](x + step/2, y[i] + k2[i]*step/2);
  74.     }
  75.     for (int i = 0; i < N; ++i)
  76.     {
  77.         temp[i] = y[i] + k1[i]*step;
  78.         k4[i] = func[i](x + step, y[i] + k1[i]*step);
  79.     }
  80.  
  81.     for (int i = 0; i < N; ++i)
  82.         y[i] += step*(k1[i] + 2*k2[i] + 2*k3[i] + k4[i])/6;
  83.  
  84.     return y;
  85. }
  86.  
  87. void EulerMethod(double x, double end, double h, double *y, double (*funcs[])(double, double))
  88. {
  89.     FILE *f;
  90.     f = fopen("Euler.txt", "w");
  91.     for (; x < end; x += h)
  92.     {
  93.         myprint(f, x);
  94.         for (int i = 0; i < N; ++i)
  95.             myprint(f, y[i]);
  96.         y = StepEuler(x, y, h, N, funcs);
  97.         myprintend(f);
  98.     }
  99.     fclose(f);
  100. }
  101.  
  102. void RKMethod(double x, double end, double h, double *y, double (*funcs[])(double, double))
  103. {
  104.     FILE *f;
  105.     f = fopen("RK.txt", "w");
  106.     for (; x < end; x += h)
  107.     {
  108.         myprint(f, x);
  109.         for (int i = 0; i < N; ++i)
  110.             myprint(f, y[i]);
  111.         y = StepRK(x, y, h, N, funcs);
  112.         myprintend(f);
  113.     }
  114.     fclose(f);
  115. }
  116.  
  117. void main()
  118. {
  119.     pointer funcs[] = {f0, f1, f2, f3, f4};
  120.     double x, end, h;
  121.     double *y = malloc(N * sizeof(double));
  122.  
  123.     printf("Input interval");
  124.     scanf("%lf%lf", &x, &end);
  125.     printf("Input step");
  126.     scanf("%lf", &h);
  127.     printf("Input start conditions");
  128.     scanf("%lf%lf%lf%lf%lf", y, y+1, y+2, y+3, y+4);
  129.  
  130.     double *y0 = malloc(N * sizeof(double));
  131.     memcpy(y0, y, N * sizeof(double));
  132.  
  133.     EulerMethod(x, end, h, y, funcs);
  134.  
  135.     y = y0;
  136.     RKMethod(x, end, h, y, funcs);
  137. }
  138.  
  139. // fprintf(fo, "%f\t%f\t%f \n", fx[i][0], fx[i][1], fx[i][2]);
  140. //вычисление на одном шаге методом Рунге-Кутты, матрица xi --> xi+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement