# TITLE: Euler's Method and Runge-Kutta 4th order method # for Solving Differential Equations # AUTHOR: Roy F.A. Maclean # EMAIL: rfamgm at gmail # WEB: http://www.spiderpixel.co.uk/caspro # DATE: 28Nov1996, 3Jun1999 # MAKE: CASIO # MODEL: fx7700GE or greater # # This program gives numerical solutions to diff equations of the form # y'=f(t,y) (a first order diffeq in y as a function of t) # with y(0) given as an initial value # # When the program runs, you enter the start and end times and the initial # value and the number of steps to use and whether to use # euler's method or Runge-Kutta 4th order method. # # The output will be a matrix of values containing the # approximate values at time intervals 'h'. But not including the initial # value. Then these values are plotted. # # The problem with the Euler method is that to get good accuracy you # have to use a lot of steps so it is mostly of theoretical interest. # It is more useful to use the Runge-Kutta method. # # To run this program you must first store the RHS of the eqn f(t,y) # in function memory f1 and setup Mat A as an Nx1 matrix, where N # is the number of steps to be used. # example: store the function below in function memory f1 # Y-T^2+1 # Give t(0)=0 and t(end)=2 and initial value=0.5 and no.of steps=10 ## Program 0 ## This program is repeated below without comments, ## for compatibility with Xchange. "T0"?->S~T:"T END"?->U "INITIAL VALUE"?->V~Z "NO OF STEPS"?->N Lbl 0 "EULER(1) OR RK4(2)"?->Q Q<>1=>Q<>2=>Goto 0 (U-S)/N->H 1->J:Lbl 1 Q=1=>Y+Hf1->Y Q=2=>Prog 1 T+H->T Y->Mat A[J,1] YY->V:Y>W=>Y->W ; finds min & max values Isz J:J<=N=>Goto 1 Mat A_ ; displays values ; rest of program plots the values Range S,U,0,V,W,0 ; miss this line if you want to set your own range Plot S,Z ; plots initial value 1->J:Lbl 2 Plot S+JH,Mat A[J,1] Line ; miss this line out if you just want dots Isz J J<=N=>Goto 2 @@ Program 1 T->E:Y->F Hf1->A E+H/2->T:F+A/2->Y Hf1->B F+B/2->Y Hf1->C E+H->T:F+C->Y Hf1->D E->T F+(A+2B+2C+D)/6->Y @@ Program 0 "T0"?->S~T:"T END"?->U "INITIAL VALUE"?->V~Z "NO OF STEPS"?->N Lbl 0 "EULER(1) OR RK4(2)"?->Q Q<>1=>Q<>2=>Goto 0 (U-S)/N->H 1->J:Lbl 1 Q=1=>Y+Hf1->Y Q=2=>Prog 1 T+H->T Y->Mat A[J,1] YY->V:Y>W=>Y->W Isz J:J<=N=>Goto 1 Mat A_ Range S,U,0,V,W,0 Plot S,Z 1->J:Lbl 2 Plot S+JH,Mat A[J,1] Line Isz J J<=N=>Goto 2