fork download
  1. /* Рунге-Кутта для X'=f(t,X) X - вектор.
  2.   Параметры: t, x - массив X
  3.   n - размерность,
  4.   f- ф-ция-вектор - возвращает рез-ты в массив Y
  5.   f(t,x,Y)
  6.   mem - память 5*n*sizeof(double) или NULL...
  7. */
  8. int RKn(int n, double*t, double* x, double h,
  9. void (*f)(int, double, double*, double*), double * mem)
  10. {
  11. double *k1,*k2,*k3,*X,*Y;
  12. int j;
  13. if (mem) k1 = mem;
  14. else if ((k1 = (double*)malloc(5*sizeof(double)*n))==0) return 0;
  15.  
  16. Y = (X = (k3 = (k2 = k1+n)+n)+n)+n;
  17. memcpy(X,x,sizeof(double)*n);
  18.  
  19. f(n,*t,X,Y);
  20. for(j=0; j<n; j++) X[j] = x[j] + (k1[j]=h*Y[j])/2.;
  21. f(n,*t+h/2.,X,Y);
  22. for(j=0; j<n; j++) X[j] = x[j] + (k2[j]=h*Y[j])/2.;
  23. f(n,*t+h/2.,X,Y);
  24. for(j=0; j<n; j++) X[j] = x[j] + (k3[j]=h*Y[j]);
  25. f(n,*t+=h,X,Y);
  26. for(j=0; j<n; j++) x[j]+=(k1[j]+2.*(k2[j]+k3[j])+h*Y[j])/6.;
  27. free(k1);
  28.  
  29. return 1;
  30. };
  31.  
  32.  
  33.  
  34. void f(int n, double t, double*x, double*Y)
  35. {
  36. Y[0] = -52*x[0]-100*x[1]+exp(-t);
  37. Y[1] = x[0] + sin(t);
  38. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c: In function 'RKn':
prog.c:14:29: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
     else if ((k1 = (double*)malloc(5*sizeof(double)*n))==0) return 0;
                             ^
prog.c:14:29: warning: incompatible implicit declaration of built-in function 'malloc'
prog.c:14:29: note: include '<stdlib.h>' or provide a declaration of 'malloc'
prog.c:17:5: warning: implicit declaration of function 'memcpy' [-Wimplicit-function-declaration]
     memcpy(X,x,sizeof(double)*n);
     ^
prog.c:17:5: warning: incompatible implicit declaration of built-in function 'memcpy'
prog.c:17:5: note: include '<string.h>' or provide a declaration of 'memcpy'
prog.c:27:5: warning: implicit declaration of function 'free' [-Wimplicit-function-declaration]
     free(k1);
     ^
prog.c:27:5: warning: incompatible implicit declaration of built-in function 'free'
prog.c:27:5: note: include '<stdlib.h>' or provide a declaration of 'free'
prog.c: In function 'f':
prog.c:36:30: warning: implicit declaration of function 'exp' [-Wimplicit-function-declaration]
     Y[0] = -52*x[0]-100*x[1]+exp(-t);
                              ^
prog.c:36:30: warning: incompatible implicit declaration of built-in function 'exp'
prog.c:36:30: note: include '<math.h>' or provide a declaration of 'exp'
prog.c:37:19: warning: implicit declaration of function 'sin' [-Wimplicit-function-declaration]
     Y[1] = x[0] + sin(t);
                   ^
prog.c:37:19: warning: incompatible implicit declaration of built-in function 'sin'
prog.c:37:19: note: include '<math.h>' or provide a declaration of 'sin'
/usr/lib/gcc/i586-linux-gnu/5/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: error: ld returned 1 exit status
stdout
Standard output is empty