# include # define g 9.8 main() { /* Step 1. Document and Declare Variables This program uses a discrete in time mathematical model defined by Equations (3a,b) and (4a,b) of http://www.krellinst.org/AiS/textbook/unit2/rock.html . The input variables are: z0, the initial height of the rock v0, the initial velocity of the rock h, the time step imax, the maximum number of time steps to calculate The output variables are: ti, the time value at the i-th grid point zi, the rock height at ti vi, the rock velocity at ti The auxiliary variables used are: vi_left, the velocity at time ti -.5*h vi_right, the velocity at time ti + .5*h i, a loop counter */ double vo,z0,h; int imax; double ti,vi,zi; double vi_left,vi_right; int i; /* Step 2. Initialize numerical solution at t = 0 */ ti = 0; printf("enter the rock's initial height in meters "); scanf("%lf",&zi); printf("enter the rock's initial velocity in meters/second "); scanf("%lf",&vi); printf("enter the the time step in seconds "); scanf("%lf",&h); printf("enter the the maximum number of time steps to compute "); scanf("%d",&imax); /* Step 3. Calculate velocity at t = .5*h */ vi_left = vi -.5*g*h; /* Step 4. Begin time stepping */ for(i=0;i<=imax;i++) { /* Step 5. Advance solution one time step */ ti = ti + h; zi = zi + vi_left*h; vi_right = vi_left - g*h; vi = .5*(vi_left + vi_right); /* Step 6. Output numerical solution */ if(zi <= 0){ printf("the rock is on the ground\n"); break; } printf("%lf %lf %lf\n",ti,zi,vi); /* Step 7. Prepare for next time step */ vi_left = vi_right; } }