label set_fn_number # Call this with the number of the entry label to the function sto fn_entry_label_int # Store in a memory address rtn label simpson_int # Simpson's rule integration. Push lower limit, upper limit, and number of points sto steps_int # number of steps to perform sto holdsteps_int # This value won't be overwritten x==0 rtn # don't run if asked to perform zero steps clx sto highlimit_int # The upper limit of the integration clx sto lowlimit_int # The lower limit of the integration x<>i sto callers_i_int # store the calling routine's I register, if any x<>i clx 0.000000000000000e+000 sto integral_int # initialize to zero clx rcl highlimit_int rcl lowlimit_int - rcl steps_int / 2.000000000000000e+000 / sto stepsize_int # distance between function invocations clx rcl lowlimit_int sto current_x_int # make a current x for f(x) clx label mainloop_int rcl fn_entry_label_int x<>i clx rcl current_x_int gosub (i) # run the function being integrated rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx 2.000000000000000e+000 * sto+ integral_int clx rcl stepsize_int sto+ current_x_int clx rcl current_x_int gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx 4.000000000000000e+000 * sto+ integral_int clx rcl stepsize_int sto+ current_x_int clx 1.000000000000000e+000 sto- steps_int clx rcl steps_int cf notfinished_int x!=0 sf notfinished_int clx f? notfinished_int goto mainloop_int rcl lowlimit_int gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx sto- integral_int # I overdid it in the loop, take one off clx rcl highlimit_int gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx sto+ integral_int clx rcl highlimit_int rcl lowlimit_int - 6.000000000000000e+000 / rcl holdsteps_int / sto* integral_int clx rcl integral_int rcl callers_i_int # restore the caller's I register x<>i clx rtn label root_int # Find a zero of the function based on the guess provided sto trialx_int clx rcl fn_entry_label_int x<>i sto callers_i2_int clx label mainloop_root_int rcl trialx_int sto last_trialx_int gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx rcl trialx_int gosub deriv_int / sto- trialx_int clx rcl trialx_int round rcl last_trialx_int round x==y # iterate until there's no printable change from last time goto finished_root_int clx clx goto mainloop_root_int label finished_root_int clx clx rcl callers_i2_int x<>i clx rcl trialx_int rtn label deltasize_int # delta-x / x to use for differentiating sto new_delta_int rtn label deriv_int # get the derivative of the function rcl fn_entry_label_int x<>i sto callers_i3_int clx rcl new_delta_int x!=0 goto deriv_setup_int 5.000000000000000e-005 # Default value of delta-x / x sto new_delta_int clx label deriv_setup_int clx ENTER ENTER ENTER rcl new_delta_int * 2.000000000000000e+000 / + gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx x<>y ENTER rcl new_delta_int * 2.000000000000000e+000 / - gosub (i) rcl fn_entry_label_int # in case the function killed I, reload it x<>i clx - rcl new_delta_int / x<>y / rtn