#include #include double exact(double t) { return sin(t); } double dy(double t, double y) { return cos(t); } double rk4_step(double y, double t, double h) { double k1 = h * dy(t, y); double k2 = h * dy(t + 0.5 * h, y + 0.5 * k1); double k3 = h * dy(t + 0.5 * h, y + 0.5 * k2); double k4 = h * dy(t + h, y + k3); return y + (k1 + k4)/ 6.0 + (k2+k3) / 3.0; } double loop (int steps, double h, int n, double y, double t) { if (n < steps) return loop(steps, h, n+1, rk4_step(y,t,h), t+h); else return y; } int main() { double h = 0.1; double y = loop(102, h, 1, 1.0, 0.0); double err = fabs(y - exact(102 * h)); int large = 10000000; double y2 = loop(large, h, 1, 1.0, 0.0); printf("%d\n", (fabs(y2 - (exact(large * h))) < 2. * err)); return 0; }