266 CHAPTER 14. DIFFERENTIAL EQUATIONS
o f i l e . close ( ) ; / / close output f i l e
return 0 ;
} / / End of main function
/ / Read in from screen the number of steps ,
/ / i n i t i a l pos it i on and i n i t i a l speed
void i n i t i a l i s e ( double& in itial_ x , double& in it i al _v , int &
number_of_steps )
{
cout < < ;
cin > > i n i t i a l _ x ;
cout < < ;
cin > > i n i t i a l _ v ;
cout < < ;
cin > > number_of_steps ;
} / / end of fun ctio n i n i t i a l i s e
/ / t h is funct ion se t s up the deri v a t i v es for t h i s s pe cial case
void d e riva t i ve s ( double t , double y , double dydt )
{
dydt [0]= y [ 1 ] ; / / d eriv a t i ve of x
dydt [1]= y [ 0 ] ; / / d e riv a t i ve of v
} / / end of func tion d e r i vat i v e s
/ / f un ctio n to write out the f i n a l r e s ul t s
void output ( double t , double y , double E0)
{
o f i l e < < s e t i o s f l a g s ( ios : : showpoint | ios : : uppercase ) ;
o f i l e < < setw (15) < < se t p r e c i s ion (8) < < t ;
o f i l e < < setw (15) < < se t p r e c i s ion (8) < < y [ 0 ] ;
o f i l e < < setw (15) < < se t p r e c i s ion (8) < < y [ 1 ] ;
o f i l e < < setw (15) < < se t p r e c i s ion (8) < < cos ( t ) ;
o f i l e < < setw (15) < < se t p r e c i s ion (8) <<
0.5 y [0] y [0]+0.5 y [1] y[1] E0 < < endl ;
} / / end of fun ctio n output
/ This func tion upgrades a fu nc ti on y ( input as a pointer )
and returns the r e s u l t yout , also as a pointer . Note that
these variab le s are declared as arrays . I t also r ec ei ve s as
input the s tarting value for the d e r iva t i v e s in the pointer
dydx . I t r ecei ves also the variable n which represe nt s the
number of d i f f e r e n t i a l equations , the step size h and
the i n i t i a l value of x . I t r ec ei ve s also the name of the
fu nc tion derivs where the given d e riva t ive i s computed
/