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>
- int N=5;
- typedef double (*mytype)(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);
- }
- double* StepEuler (double x, double *y, double step, int N, double (*func[])(double, double ))
- {
- for (int i=0; i<N; i++)
- {
- y[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, *k2, *k3, *k4;
- k1 = malloc(N * sizeof(double));
- k2 = malloc(N * sizeof(double));
- k3 = malloc(N * sizeof(double));
- k4 = malloc(N * sizeof(double));
- for (int i = 0; i < N; ++i)
- k1[i] = func[i](x,y[i]);
- double *dop = malloc(N * sizeof(double));
- for (int i = 0; i < N; ++i)
- {
- dop[i] = y[i] + k1[i]*step/2;
- k2[i] = func[i]((x+step/2),(dop[i]));
- }
- for (int i = 0; i < N; ++i)
- {
- dop[i] = y[i] + k2[i]*step/2;
- k3[i] = func[i]((x+step/2),(dop[i]));
- }
- for (int i = 0; i < N; ++i)
- {
- dop[i] = y[i] + k1[i]*step;
- k4[i] = func[i]((x+step),(dop[i]));
- }
- for (int i = 0; i < N; ++i)
- y[i] = y[i]+ step*(k1[i]+2*k2[i]+2*k3[i]+k4[i])/6;
- return y;
- }
- void main()
- {
- mytype *mass = malloc(N * sizeof(mytype));
- mass[0]=f0;
- mass[1]=f1;
- mass[2]=f2;
- mass[3]=f3;
- mass[4]=f4;
- FILE *fe, *fk;
- fe=fopen ("Euler.txt", "w");
- //double (*mass[]) (double, double*, int) ={f0, f1, f3, f2, f4};
- double x, xk, h, x0, y1, y2, y3, y4, y5;
- printf("Input interval");
- scanf("%lf%lf", &x, &xk);
- x0=x;
- printf("Input step");
- scanf("%lf", &h);
- printf("Input start conditions");
- scanf("%lf%lf%lf%lf%lf", &y1, &y2,&y3,&y4,&y5);
- double *y = malloc(N * sizeof(double));
- *y=y1;
- *(y+1)=y2;
- *(y+2)=y3;
- *(y+3)=y4;
- *(y+4)=y5;
- for (;x < xk; x =x+ h) {
- printf("%lf\t", x);
- fprintf(fe, "%lf\t", x);
- for (int i = 0; i < N; ++i) {
- printf("%lf\t", y[i]);
- fprintf(fe, "%lf\t", y[i]);
- }
- y = StepEuler(x, y, h, N, mass);
- printf("\n");
- fprintf(fe, "\n");
- }
- fclose(fe);
- *y=y1;
- *(y+1)=y2;
- *(y+2)=y3;
- *(y+3)=y4;
- *(y+4)=y5;
- x=x0;
- fk=fopen ("RK.txt", "w");
- for (;x < xk; x =x + h) {
- printf("%lf\t", x);
- fprintf(fk, "%lf\t", x);
- for (int i = 0; i < N; i++) {
- printf("%lf\t", y[i]);
- fprintf(fk, "%lf\t", y[i]);
- }
- y = StepRK(x, y, h, N, mass);
- printf("\n");
- fprintf(fk, "\n");
- }
- fclose(fe);
- fclose(fk);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement