-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kochie.c
145 lines (134 loc) · 2.98 KB
/
Kochie.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "functions.c"
typedef double(*func_t)(double x, double *y);
func_t *func;
void deffunc(void)
{
func[0] = f191;
func[1] = f192;
}
double *hq(double *q, double h, double tmp, double a, int cnt, double *ans)
{
int i;
for (i = 0; i < cnt; i++)
{
ans[i] = q[i] + h * tmp / (2 * a);
}
return ans;
}
double *iter(double *y0, double x0, double a, double h, double *y, int cnt)
{
int i;
double tmp;
double *hlp = malloc (cnt * sizeof(double));
for (i = 0; i < cnt; i++)
{
tmp = func[i](x0, y0);
y[i] = y0[i] + ((1 - a) * tmp + a * func[i](x0 + h / (2 * a), hq(y0, h,tmp, a, cnt, hlp))) * h;
}
free(hlp);
return y;
}
double *hn(double *a, int cnt, double h, double k, double *ans)
{
int i;
for (i = 0; i < cnt; i++)
{
ans[i] = a[i] + h * k / 2;
}
return ans;
}
double *hm(double *a, int cnt, double h, double k, double *ans)
{
int i;
for (i = 0; i < cnt; i++)
{
ans[i] = a[i] + h * k;
}
return ans;
}
double *iter2(double *y0, double x0, double h, double *y, int cnt)
{
int i;
double k1,k2,k3,k4;
double *tmp = malloc (cnt * sizeof(double));
for (i = 0; i < cnt; i++)
{
k1 = func[i](x0, y0);
k2 = func[i](x0 + h/2, hn(y0, cnt, h, k1, tmp));
k3 = func[i](x0 + h/2, hn(y0, cnt, h, k2, tmp));
k4 = func[i](x0 + h, hm(y0, cnt, h, k3, tmp));
y[i] = y0[i] + (k1 + 2 * k2 + 2 * k3 + k4) * h / 6;
}
free(tmp);
return y;
}
void Runge_Kutta4(double x0, double *y0, int cnt, double l, int n)
{
int i, j;
printf("x = %lf ", x0);
for (i = 0; i < cnt; i++)
printf(" y%d(%lf) = %lf",i, x0, y0[i]);
printf("\n");
double *y = malloc(cnt * sizeof(double)), h = l / n;
for (i = 0; i < n; i++)
{
y = iter2(y0, x0, h, y, cnt);
x0 += h;
printf("x = %lf ", x0);
for (j = 0; j < cnt; j++)
printf(" y%d(%lf) = %lf", j, x0, y[j]);
printf("\n");
memcpy(y0, y, cnt * sizeof(double));
}
free(y);
}
void Runge_Kutta(double x0, double *y0, int cnt, double l, double a, int n)
{
int i, j;
printf("x = %lf ", x0);
for (i = 0; i < cnt; i++)
printf(" y%d = %lf",i, y0[i]);
printf("\n");
double *y = malloc(cnt * sizeof(double)), h = l / n;
for (i = 0; i < n; i++)
{
y = iter(y0, x0, a, h, y, cnt);
x0 += h;
printf("x = %lf ", x0);
for (j = 0; j < cnt; j++)
printf(" y%d = %lf",j, y[j]);
printf("\n");
memcpy(y0, y, cnt * sizeof(double));
}
free(y);
}
int main(void)
{
double a;
double x0, *y0, l;
int cnt, n;
scanf("%lf", &x0);
scanf("%d", &cnt);
y0 = malloc(cnt * sizeof(double));
double *y0q = malloc(cnt * sizeof(double));
func = malloc(cnt * sizeof(double));
int i;
for (i = 0; i < cnt; i++)
{
scanf("%lf", &y0[i]);
}
memcpy(y0q, y0, cnt * sizeof(double));
scanf("%lf", &l);
scanf("%d", &n);
scanf("%lf", &a);
deffunc();
puts("Метод Рунге-Кутта второго порядка\n");
Runge_Kutta(x0, y0, cnt, l, a, n);
puts("--------");
puts("Метод Рунге-Кутта четвертого порядка\n");
Runge_Kutta4(x0, y0q, cnt, l, n);
return 0;
}