• API
• FAQ
• Tools
• Archive
SHARE
TWEET

Рабочая версия

a guest Dec 14th, 2018 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <malloc.h>
4. #include <math.h>
5.
6. int N=5;
7. typedef double (*mytype)(double, double); //тип указывающий на функцию
8.
9. double f0(double x, double y)
10. {
11.     return cos(x)/x+(-1.0)*(y+0)/x;
12. }
13.
14. double f1(double x, double y)
15. {
16.     return x/50;
17. }
18.
19. double f2 (double x, double y)
20. {
21.     return (-2.0)* (y+2)/ x;
22. }
23.
24. double f3 (double x, double y)
25. {
26.     return -cos(x);
27. }
28.
29. double f4 (double x, double y)
30. {
31.     return sin (x);
32. }
33.
34.
35. double* StepEuler (double x, double *y, double step, int N, double (*func[])(double, double ))
36. {
37.     for (int i=0; i<N; i++)
38.     {
39.         y[i]=y[i]+func[i](x, y[i])*step;
40.     }
41.     return y;
42. }
43. double* StepRK(double x, double *y, double step, int N, double (*func[])(double, double))
44. {
45.     double *k1, *k2, *k3, *k4;
46.     k1 = malloc(N * sizeof(double));
47.     k2 = malloc(N * sizeof(double));
48.     k3 = malloc(N * sizeof(double));
49.     k4 = malloc(N * sizeof(double));
50.
51.     for (int i = 0; i < N; ++i)
52.         k1[i] = func[i](x,y[i]);
53.
54.     double *dop = malloc(N * sizeof(double));
55.     for (int i = 0; i < N; ++i)
56.     {
57.         dop[i] = y[i] + k1[i]*step/2;
58.         k2[i] = func[i]((x+step/2),(dop[i]));
59.
60.     }
61.
62.     for (int i = 0; i < N; ++i)
63.     {
64.         dop[i] = y[i] + k2[i]*step/2;
65.         k3[i] = func[i]((x+step/2),(dop[i]));
66.
67.     }
68.
69.     for (int i = 0; i < N; ++i)
70.     {
71.         dop[i] = y[i] + k1[i]*step;
72.         k4[i] = func[i]((x+step),(dop[i]));
73.
74.     }
75.
76.     for (int i = 0; i < N; ++i)
77.         y[i] = y[i]+ step*(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6;
78.
79.     return y;
80. }
81.
82. void main()
83. {
84.     mytype *mass = malloc(N * sizeof(mytype));
85.     mass[0]=f0;
86.     mass[1]=f1;
87.     mass[2]=f2;
88.     mass[3]=f3;
89.     mass[4]=f4;
90.     FILE *fe, *fk;
91.     fe=fopen ("Euler.txt", "w");
92.
93.     //double (*mass[]) (double, double*, int) ={f0, f1, f3, f2, f4};
94.     double x, xk, h, x0, y1, y2, y3, y4, y5;
95.     printf("Input interval");
96.     scanf("%lf%lf", &x, &xk);
97.     x0=x;
98.     printf("Input step");
99.     scanf("%lf", &h);
100.     printf("Input start conditions");
101.     scanf("%lf%lf%lf%lf%lf", &y1, &y2,&y3,&y4,&y5);
102.     double *y = malloc(N * sizeof(double));
103.     *y=y1;
104.     *(y+1)=y2;
105.     *(y+2)=y3;
106.     *(y+3)=y4;
107.     *(y+4)=y5;
108.     for (;x < xk; x =x+ h) {
109.         printf("%lf\t", x);
110.         fprintf(fe, "%lf\t", x);
111.         for (int i = 0; i < N; ++i) {
112.             printf("%lf\t", y[i]);
113.             fprintf(fe, "%lf\t", y[i]);
114.         }
115.         y = StepEuler(x, y, h, N, mass);
116.         printf("\n");
117.         fprintf(fe, "\n");
118.     }
119.     fclose(fe);
120.     *y=y1;
121.     *(y+1)=y2;
122.     *(y+2)=y3;
123.     *(y+3)=y4;
124.     *(y+4)=y5;
125.     x=x0;
126.     fk=fopen ("RK.txt", "w");
127.     for (;x < xk; x =x + h) {
128.         printf("%lf\t", x);
129.         fprintf(fk, "%lf\t", x);
130.         for (int i = 0; i < N; i++) {
131.             printf("%lf\t", y[i]);
132.             fprintf(fk, "%lf\t", y[i]);
133.         }
134.         y = StepRK(x, y, h, N, mass);
135.         printf("\n");
136.         fprintf(fk, "\n");
137.     }
138.     fclose(fe);
139.     fclose(fk);
140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!

Top