/* ************************************************************************ * rungen.c: 4th order Runge-Kutta solution * * * ************************************************************************ */ #include #include #define N 2 /* number of equations */ #define MIN 0.0 /* minimum x */ #define MAX 10.0 /* maximum x */ main() { void rungen(double x, double y[], double dx); double f(double x, double y[], int i); double x, y[N], dx, h, yreal, yerror; int j; FILE *output; /* save data in rk4.dat */ output = fopen("rungen.dat","w"); printf("Input: dx\n"); scanf("%lf", &dx); printf("dx=%g\n", dx); x = MIN; y[0] = 0.0; /* initial position */ y[1] = 1.0; /* initial velocity */ fprintf(output, "%f\t%f\n", x, y[0]); while(x <= MAX) { rungen(x, y, dx); x += dx; yreal=sin(x); yerror=y[0]-yreal; fprintf(output, "%f\t%f\t%f\t%e\n", x, y[0], yreal, yerror); } printf("data stored in rungen.dat\n"); fclose(output); } /*-----------------------end of main program--------------------------*/ /* Runge-Kutta subroutine */ void rungen(double x, double y[], double step) { double f(double x, double y[], int i); double h=step/2.0, /* the midpoint */ t1[N], t2[N], t3[N], /* temporary storage */ k1[N], k2[N], k3[N],k4[N]; /* for Runge-Kutta */ int i; for (i=0; i