#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *dvector(long i, long j);
void free_dvector(double *a, long i);
void FUNC(double x, double *y, double *f);
void rk4m(double *y, double *f, int N, double a, double b, int n, void (*F)());
int main(void) {
int n = 100, N = 2;
double *y, *f, a = 0.0, b = 10.0;
y = dvector(1, N);
f = dvector(1, N);
y[1] = 0.1;
y[2] = 0.0;
rk4m(y, f, N, a, b, n, FUNC);
free_dvector(y, 1);
free_dvector(f, 1);
return 0;
}
void rk4m(double *y, double *f, int N, double a, double b, int n, void (*F)()) {
double *k1, *k2, *k3, *k4, *tmp, x, h;
int i, j;
k1 = dvector(1, N);
k2 = dvector(1, N);
k3 = dvector(1, N);
k4 = dvector(1, N);
tmp = dvector(1, N);
h = (b - a) / n;
x = a;
printf("%.3lf\t %.3lf\t %.10lf\n", x
, y
[1], y
[2]); for (i = 0; i < n; i++) {
(*F)(x, y, f);
for (j = 1; j <= N; j++) k1[j] = h * f[j];
for (j = 1; j <= N; j++) tmp[j] = y[j] + k1[j] / 2.0;
(*F)(x + h / 2.0, tmp, f);
for (j = 1; j <= N; j++) k2[j] = h * f[j];
for (j = 1; j <= N; j++) tmp[j] = y[j] + k2[j] / 2.0;
(*F)(x + h / 2.0, tmp, f);
for (j = 1; j <= N; j++) k3[j] = h * f[j];
for (j = 1; j <= N; j++) tmp[j] = y[j] + k3[j];
(*F)(x + h, tmp, f);
for (j = 1; j <= N; j++) k4[j] = h * f[j];
for (j = 1; j <= N; j++) y[j] = y[j] + (k1[j] + 2 * k2[j] + 2 * k3[j] + k4[j]) / 6.0;
x += h;
printf("%.3lf\t %.10lf\t %.10lf\n", x
, y
[1], y
[2]); }
free_dvector(k1, 1);
free_dvector(k2, 1);
free_dvector(k3, 1);
free_dvector(k4, 1);
free_dvector(tmp, 1);
}
void FUNC(double x, double *y, double *f) {
f[1] = y[2];
f
[2] = cos(x
) - 4 * y
[2] - 3 * y
[1];}
double *dvector(long i, long j) {
double *a;
if ((a
= malloc((j
- i
+ 1) * sizeof(double))) == NULL
) { }
return a - i;
}
void free_dvector(double *a, long i) {
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmRvdWJsZSAqZHZlY3Rvcihsb25nIGksIGxvbmcgaik7CnZvaWQgZnJlZV9kdmVjdG9yKGRvdWJsZSAqYSwgbG9uZyBpKTsKdm9pZCBGVU5DKGRvdWJsZSB4LCBkb3VibGUgKnksIGRvdWJsZSAqZik7CnZvaWQgcms0bShkb3VibGUgKnksIGRvdWJsZSAqZiwgaW50IE4sIGRvdWJsZSBhLCBkb3VibGUgYiwgaW50IG4sIHZvaWQgKCpGKSgpKTsKCmludCBtYWluKHZvaWQpIHsKICAgIGludCBuID0gMTAwLCBOID0gMjsKICAgIGRvdWJsZSAqeSwgKmYsIGEgPSAwLjAsIGIgPSAxMC4wOwogICAgeSA9IGR2ZWN0b3IoMSwgTik7CiAgICBmID0gZHZlY3RvcigxLCBOKTsKICAgIHlbMV0gPSAwLjE7CiAgICB5WzJdID0gMC4wOwogICAgcms0bSh5LCBmLCBOLCBhLCBiLCBuLCBGVU5DKTsKICAgIGZyZWVfZHZlY3Rvcih5LCAxKTsKICAgIGZyZWVfZHZlY3RvcihmLCAxKTsKICAgIHJldHVybiAwOwp9Cgp2b2lkIHJrNG0oZG91YmxlICp5LCBkb3VibGUgKmYsIGludCBOLCBkb3VibGUgYSwgZG91YmxlIGIsIGludCBuLCB2b2lkICgqRikoKSkgewogICAgZG91YmxlICprMSwgKmsyLCAqazMsICprNCwgKnRtcCwgeCwgaDsKICAgIGludCBpLCBqOwogICAgazEgPSBkdmVjdG9yKDEsIE4pOwogICAgazIgPSBkdmVjdG9yKDEsIE4pOwogICAgazMgPSBkdmVjdG9yKDEsIE4pOwogICAgazQgPSBkdmVjdG9yKDEsIE4pOwogICAgdG1wID0gZHZlY3RvcigxLCBOKTsKICAgIGggPSAoYiAtIGEpIC8gbjsKICAgIHggPSBhOwogICAgcHJpbnRmKCIlLjNsZlx0ICUuM2xmXHQgJS4xMGxmXG4iLCB4LCB5WzFdLCB5WzJdKTsKICAgIGZvciAoaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAoKkYpKHgsIHksIGYpOwogICAgICAgIGZvciAoaiA9IDE7IGogPD0gTjsgaisrKSBrMVtqXSA9IGggKiBmW2pdOwogICAgICAgIGZvciAoaiA9IDE7IGogPD0gTjsgaisrKSB0bXBbal0gPSB5W2pdICsgazFbal0gLyAyLjA7CiAgICAgICAgKCpGKSh4ICsgaCAvIDIuMCwgdG1wLCBmKTsKICAgICAgICBmb3IgKGogPSAxOyBqIDw9IE47IGorKykgazJbal0gPSBoICogZltqXTsKICAgICAgICBmb3IgKGogPSAxOyBqIDw9IE47IGorKykgdG1wW2pdID0geVtqXSArIGsyW2pdIC8gMi4wOwogICAgICAgICgqRikoeCArIGggLyAyLjAsIHRtcCwgZik7CiAgICAgICAgZm9yIChqID0gMTsgaiA8PSBOOyBqKyspIGszW2pdID0gaCAqIGZbal07CiAgICAgICAgZm9yIChqID0gMTsgaiA8PSBOOyBqKyspIHRtcFtqXSA9IHlbal0gKyBrM1tqXTsKICAgICAgICAoKkYpKHggKyBoLCB0bXAsIGYpOwogICAgICAgIGZvciAoaiA9IDE7IGogPD0gTjsgaisrKSBrNFtqXSA9IGggKiBmW2pdOwogICAgICAgIGZvciAoaiA9IDE7IGogPD0gTjsgaisrKSB5W2pdID0geVtqXSArIChrMVtqXSArIDIgKiBrMltqXSArIDIgKiBrM1tqXSArIGs0W2pdKSAvIDYuMDsKICAgICAgICB4ICs9IGg7CiAgICAgICAgcHJpbnRmKCIlLjNsZlx0ICUuMTBsZlx0ICUuMTBsZlxuIiwgeCwgeVsxXSwgeVsyXSk7CiAgICB9CiAgICBmcmVlX2R2ZWN0b3IoazEsIDEpOwogICAgZnJlZV9kdmVjdG9yKGsyLCAxKTsKICAgIGZyZWVfZHZlY3RvcihrMywgMSk7CiAgICB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