/*** Integrates the pendlum using Runge-Kutta method ***/ /*** rk4 from numerical recipes ***/ #include #include #include #define select select_nr /* otherwise: twice defined */ #include "nr.h" #include "nrutil.h" /** sets f[]=derivates at point x of variables y[] **/ /** y[1]=phi, y[2]=omega **/ void derivs(float x, float *y, float *f) { f[1] = y[2]; f[2] = -sin(y[1]); } int main(int argc, char **argv) { float y[3]; /* phase space variables: unused,phi,omega */ float f[3]; /* derivatives */ float h; /* stepsize */ int n, n_max; /* step, maximum step */ float t; /* time */ sscanf(argv[1], "%f", &h); /* first argument = stepsize */ sscanf(argv[2], "%d", &n_max); /*2nd argument = numb. of steps */ y[1]=0; y[2]=1.0; t = 0; derivs(t, y, f); for(n=1; n<=n_max; n++) /* iterate over all times */ { printf("%f %f %f\n", t, y[1], y[2]); /* print results */ rk4(y, f, 2, t, h, y, derivs); /* call Runge-Kutta */ t += h; } return(0); }