286 CHAPTER 14. DIFFERENTIAL EQUATIONS
we calcu la t e y ( x+h ) by y2 + ( delta ) / 15 ( page 57 5 , Numerical R . )
I f delt a i s not smaller than . . . we ca lc ula te a new s tepsize using
h_new=( Safety ) h_old ( . . . / de lta ) ^ (0 .2 5) where " Safety " i s constant
( page 577 N.R . )
and s t a r t again with ca lc u la t in g y ( x+h ) . . .
/
int i ;
double t_h , h_alt , h_neu , hh , errmax ;
double yout [2] , y_h [2 ] , y_m [2] , y1 [2] , y2 [ 2 ] , d e lt a [ 2 ] , yscal [2] ;
const double eps =1.0 e 6;
const double saf et y =0.9;
const double errcon =6.0e 4;
const double ti ny =1.0e 30;
t_h =0;
y_h [0]= y [ 0 ] ; / / phi
y_h [1]= y [ 1 ] ; / / v
h_neu= d elt a_t_ roo f ;
ofstream fout ( ) ;
fout . s e tf ( ios : : s c i e n t i f i c ) ;
fout . preci si o n (20) ;
for ( i =0; i <=n ; i ++){
/ The error i s scaled against yscal
We use a yscal of the form yscal = fabs ( y [ i ]) + fabs (h
deri v a t i v es [ i ])
(N. R . page 567)
/
deri v a t i v es ( t_h , y_h , yout ) ;
yscal [0]= fabs (y [0] ) + fabs ( h_neu yout [0] ) + t iny ;
yscal [1]= fabs (y [1] ) + fabs ( h_neu yout [1] ) + t iny ;
/ the do while loop is used u n t i l the /
do{
/ Calculating y2 by two ha lf step s /
h_ al t=h_neu ;
hh= h_alt 0. 5;
rk4_step ( t_h , y_h , y_m , hh ) ;
rk4_step ( t_h+hh , y_m , y2 , hh ) ;
/ Calculating y1 by one normal step /
rk4_step ( t_h , y_h , y1 , h _alt ) ;
/ Now we have two values for phi and v at the time t_h + h in
y2 and y1
We can now cal cu la t e the de lt a for phi and v
/