Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <malloc.h>
- #include <math.h>
- #include <mem.h>
- int N = 5;
- typedef double (*pointer)(double, double); //pointer on function type (double, double) -> double
- double f0(double x, double y)
- {
- return cos(x)/x+(-1.0)*(y+0)/x;
- }
- double f1(double x, double y)
- {
- return x/50;
- }
- double f2 (double x, double y)
- {
- return (-2.0)* (y+2)/ x;
- }
- double f3 (double x, double y)
- {
- return -cos(x);
- }
- double f4 (double x, double y)
- {
- return sin (x);
- }
- void myprint(FILE *f, double x) //print x into command line and write it into f
- {
- printf("%lf\t", x);
- fprintf(f, "%lf\t", x);
- }
- void myprintend(FILE *f) //print "\n" into command line and write it into f after one iteration
- {
- printf("\n");
- fprintf(f, "\n");
- }
- double* StepEuler (double x, double *y, double step, int N, double (*func[])(double, double ))
- {
- for (int i=0; i<N; i++)
- y[i] += func[i](x, y[i]) * step;
- return y;
- }
- double* StepRK(double x, double *y, double step, int N, double (*func[])(double, double))
- {
- double *k1 = malloc(N * sizeof(double));
- double *k2 = malloc(N * sizeof(double));
- double *k3 = malloc(N * sizeof(double));
- double *k4 = malloc(N * sizeof(double));
- double *temp = malloc(N * sizeof(double));
- for (int i = 0; i < N; ++i)
- k1[i] = func[i](x,y[i]);
- for (int i = 0; i < N; ++i)
- {
- temp[i] = y[i] + k1[i]*step/2;
- k2[i] = func[i](x + step/2, y[i] + k1[i]*step/2);
- }
- for (int i = 0; i < N; ++i)
- {
- temp[i] = y[i] + k2[i]*step/2;
- k3[i] = func[i](x + step/2, y[i] + k2[i]*step/2);
- }
- for (int i = 0; i < N; ++i)
- {
- temp[i] = y[i] + k1[i]*step;
- k4[i] = func[i](x + step, y[i] + k1[i]*step);
- }
- for (int i = 0; i < N; ++i)
- y[i] += step*(k1[i] + 2*k2[i] + 2*k3[i] + k4[i])/6;
- return y;
- }
- void EulerMethod(double x, double end, double h, double *y, double (*funcs[])(double, double))
- {
- FILE *f;
- f = fopen("Euler.txt", "w");
- for (; x < end; x += h)
- {
- myprint(f, x);
- for (int i = 0; i < N; ++i)
- myprint(f, y[i]);
- y = StepEuler(x, y, h, N, funcs);
- myprintend(f);
- }
- fclose(f);
- }
- void RKMethod(double x, double end, double h, double *y, double (*funcs[])(double, double))
- {
- FILE *f;
- f = fopen("RK.txt", "w");
- for (; x < end; x += h)
- {
- myprint(f, x);
- for (int i = 0; i < N; ++i)
- myprint(f, y[i]);
- y = StepRK(x, y, h, N, funcs);
- myprintend(f);
- }
- fclose(f);
- }
- void main()
- {
- pointer funcs[] = {f0, f1, f2, f3, f4};
- double x, end, h;
- double *y = malloc(N * sizeof(double));
- printf("Input interval");
- scanf("%lf%lf", &x, &end);
- printf("Input step");
- scanf("%lf", &h);
- printf("Input start conditions");
- scanf("%lf%lf%lf%lf%lf", y, y+1, y+2, y+3, y+4);
- double *y0 = malloc(N * sizeof(double));
- memcpy(y0, y, N * sizeof(double));
- EulerMethod(x, end, h, y, funcs);
- y = y0;
- RKMethod(x, end, h, y, funcs);
- }
- // fprintf(fo, "%f\t%f\t%f \n", fx[i][0], fx[i][1], fx[i][2]);
- //вычисление на одном шаге методом Рунге-Кутты, матрица xi --> xi+1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement