#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double *dvector(long i, long j);
void free_dvector(double *a);
void FUNC(double x, double *y, double *f);
void rk4m(double *y, double *f, int N, double a, double b, double step, void (*F)(double, double *, double *));
int main(void) {
int N = 2;
double *y, *f, a = 0.0, b = 1.0, step = 0.01; // 終了値を 1.0、刻み幅を 0.01 に設定
y = dvector(0, N - 1);
f = dvector(0, N - 1);
y[0] = 0.1;
y[1] = 0.0;
rk4m(y, f, N, a, b, step, FUNC);
free_dvector(y);
free_dvector(f);
return 0;
}
void FUNC(double x, double *y, double *f) {
f[0] = y[1]; // dy1/dx = y2
f
[1] = cos(x
) - 4 * y
[1] - 3 * y
[0]; // dy2/dx = cos(x) - 4y2 - 3y1}
void rk4m(double *y, double *f, int N, double a, double b, double step, void (*F)(double, double *, double *)) {
double *k1, *k2, *k3, *k4, *tmp, x, h;
int j;
k1 = dvector(0, N - 1);
k2 = dvector(0, N - 1);
k3 = dvector(0, N - 1);
k4 = dvector(0, N - 1);
tmp = dvector(0, N - 1);
h = step;
x = a;
while (x <= b) {
printf("%.2lf\t %.10lf\t %.10lf\n", x
, y
[0], y
[1]);
F(x, y, f);
for (j = 0; j < N; j++) k1[j] = f[j];
for (j = 0; j < N; j++) tmp[j] = y[j] + h * k1[j] / 2.0;
F(x + h / 2.0, tmp, f);
for (j = 0; j < N; j++) k2[j] = f[j];
for (j = 0; j < N; j++) tmp[j] = y[j] + h * k2[j] / 2.0;
F(x + h / 2.0, tmp, f);
for (j = 0; j < N; j++) k3[j] = f[j];
for (j = 0; j < N; j++) tmp[j] = y[j] + h * k3[j];
F(x + h, tmp, f);
for (j = 0; j < N; j++) k4[j] = f[j];
for (j = 0; j < N; j++)
y[j] = y[j] + h * (k1[j] + 2 * k2[j] + 2 * k3[j] + k4[j]) / 6.0;
x += h; // x を更新
}
free_dvector(k1);
free_dvector(k2);
free_dvector(k3);
free_dvector(k4);
free_dvector(tmp);
}
double *dvector(long i, long j) {
double *a;
if ((a
= malloc((j
- i
+ 1) * sizeof(double))) == NULL
) { }
return a;
}
void free_dvector(double *a) {
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPG1hdGguaD4KCmRvdWJsZSAqZHZlY3Rvcihsb25nIGksIGxvbmcgaik7CnZvaWQgZnJlZV9kdmVjdG9yKGRvdWJsZSAqYSk7CnZvaWQgRlVOQyhkb3VibGUgeCwgZG91YmxlICp5LCBkb3VibGUgKmYpOwp2b2lkIHJrNG0oZG91YmxlICp5LCBkb3VibGUgKmYsIGludCBOLCBkb3VibGUgYSwgZG91YmxlIGIsIGRvdWJsZSBzdGVwLCB2b2lkICgqRikoZG91YmxlLCBkb3VibGUgKiwgZG91YmxlICopKTsKCmludCBtYWluKHZvaWQpIHsKICAgIGludCBOID0gMjsKICAgIGRvdWJsZSAqeSwgKmYsIGEgPSAwLjAsIGIgPSAxLjAsIHN0ZXAgPSAwLjAxOyAvLyDntYLkuoblgKTjgpIgMS4w44CB5Yi744G/5bmF44KSIDAuMDEg44Gr6Kit5a6aCgogICAgeSA9IGR2ZWN0b3IoMCwgTiAtIDEpOwogICAgZiA9IGR2ZWN0b3IoMCwgTiAtIDEpOwoKICAgIHlbMF0gPSAwLjE7IAogICAgeVsxXSA9IDAuMDsKCiAgICByazRtKHksIGYsIE4sIGEsIGIsIHN0ZXAsIEZVTkMpOwoKICAgIGZyZWVfZHZlY3Rvcih5KTsKICAgIGZyZWVfZHZlY3RvcihmKTsKCiAgICByZXR1cm4gMDsKfQoKdm9pZCBGVU5DKGRvdWJsZSB4LCBkb3VibGUgKnksIGRvdWJsZSAqZikgewogICAgZlswXSA9IHlbMV07IC8vIGR5MS9keCA9IHkyCiAgICBmWzFdID0gY29zKHgpIC0gNCAqIHlbMV0gLSAzICogeVswXTsgLy8gZHkyL2R4ID0gY29zKHgpIC0gNHkyIC0gM3kxCn0KCnZvaWQgcms0bShkb3VibGUgKnksIGRvdWJsZSAqZiwgaW50IE4sIGRvdWJsZSBhLCBkb3VibGUgYiwgZG91YmxlIHN0ZXAsIHZvaWQgKCpGKShkb3VibGUsIGRvdWJsZSAqLCBkb3VibGUgKikpIHsKICAgIGRvdWJsZSAqazEsICprMiwgKmszLCAqazQsICp0bXAsIHgsIGg7CiAgICBpbnQgajsKCiAgICBrMSA9IGR2ZWN0b3IoMCwgTiAtIDEpOwogICAgazIgPSBkdmVjdG9yKDAsIE4gLSAxKTsKICAgIGszID0gZHZlY3RvcigwLCBOIC0gMSk7CiAgICBrNCA9IGR2ZWN0b3IoMCwgTiAtIDEpOwogICAgdG1wID0gZHZlY3RvcigwLCBOIC0gMSk7CgogICAgaCA9IHN0ZXA7CiAgICB4ID0gYTsKCiAgICB3aGlsZSAoeCA8PSBiKSB7CiAgICAgICAgcHJpbnRmKCIlLjJsZlx0ICUuMTBsZlx0ICUuMTBsZlxuIiwgeCwgeVswXSwgeVsxXSk7CgogICAgICAgIEYoeCwgeSwgZik7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IE47IGorKykgazFbal0gPSBmW2pdOwoKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTjsgaisrKSB0bXBbal0gPSB5W2pdICsgaCAqIGsxW2pdIC8gMi4wOwogICAgICAgIEYoeCArIGggLyAyLjAsIHRtcCwgZik7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IE47IGorKykgazJbal0gPSBmW2pdOwoKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTjsgaisrKSB0bXBbal0gPSB5W2pdICsgaCAqIGsyW2pdIC8gMi4wOwogICAgICAgIEYoeCArIGggLyAyLjAsIHRtcCwgZik7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IE47IGorKykgazNbal0gPSBmW2pdOwoKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTjsgaisrKSB0bXBbal0gPSB5W2pdICsgaCAqIGszW2pdOwogICAgICAgIEYoeCArIGgsIHRtcCwgZik7CiAgICAgICAgZm9yIChqID0gMDsgaiA8IE47IGorKykgazRbal0gPSBmW2pdOwoKICAgICAgICBmb3IgKGogPSAwOyBqIDwgTjsgaisrKQogICAgICAgICAgICB5W2pdID0geVtqXSArIGggKiAoazFbal0gKyAyICogazJbal0gKyAyICogazNbal0gKyBrNFtqXSkgLyA2LjA7CgogICAgICAgIHggKz0gaDsgLy8geCDjgpLmm7TmlrAKICAgIH0KCiAgICBmcmVlX2R2ZWN0b3IoazEpOwogICAgZnJlZV9kdmVjdG9yKGsyKTsKICAgIGZyZWVfZHZlY3RvcihrMyk7CiAgICBmcmVlX2R2ZWN0b3IoazQpOwogICAgZnJlZV9kdmVjdG9yKHRtcCk7Cn0KCmRvdWJsZSAqZHZlY3Rvcihsb25nIGksIGxvbmcgaikgewogICAgZG91YmxlICphOwogICAgaWYgKChhID0gbWFsbG9jKChqIC0gaSArIDEpICogc2l6ZW9mKGRvdWJsZSkpKSA9PSBOVUxMKSB7CiAgICAgICAgcHJpbnRmKCLjg6Hjg6Ljg6rjgYznorrkv53jgafjgY3jgb7jgZvjgpNcbiIpOwogICAgICAgIGV4aXQoMSk7CiAgICB9CiAgICByZXR1cm4gYTsKfQoKdm9pZCBmcmVlX2R2ZWN0b3IoZG91YmxlICphKSB7CiAgICBmcmVlKGEpOwp9Cgo=