using System; using static System.Math; using static System.Console; using System.Collections; using System.Collections.Generic; public partial class ode{ public static vector rk23 ( Func F, /* equation */ double a, vector ya, /* initial condition: {a,y(a)} */ double b, double acc=1e-3, double eps=1e-3, double h=0.1, List xlist=null, List ylist=null, int limit=999 ){ return driver( F, a,ya,b, acc, eps, h, xlist, ylist, limit, rkstep23 );} public static vector driver( Func F, /* equation */ double a, vector ya, double b, double acc, double eps, double h, List xlist, List ylist, int limit, Func< Func, double,vector,double,vector[] > stepper ) {// Generic ODE driver int nsteps=0; if(xlist!=null) {xlist.Clear(); xlist.Add(a);} if(ylist!=null) {ylist.Clear(); ylist.Add(ya);} do{ if(a>=b) return ya; if(nsteps>limit) { Error.Write($"ode.driver: nsteps>{limit}\n"); return ya; } if(a+h>b) h=b-a; vector[] trial=stepper(F,a,ya,h); vector yh=trial[0]; vector er=trial[1]; vector tol = new vector(er.size); for(int i=0; itol[i])ok=0; if(ok==1){ a+=h; ya=yh; h=hnew; nsteps++; if(xlist!=null) xlist.Add(a); if(ylist!=null) ylist.Add(ya); } else { h=hnew; Error.WriteLine($"driver: bad step at {a}"); } }while(true); }// driver }// class