""" Contains several methods that solve Differential Equations. """ def euler(de, x, tstart, tend, steps): """ Propagates x by solving diffeq using Eulers method. de is a funciton of t and x and return dxdt. x is list with initial values. tstart starting time tend ending time steps number of steps """ t=tstart dt=float(tend-tstart)/steps for i in range(steps): dx=de(t,x) x=[a+b*dt for (a,b) in zip(x,dx)] t+=dt return x def second(de, x, tstart, tend, steps): """ Propagates x by solving diffeq using Second Order method. de is a funciton of t and x and return dxdt. x is list with initial values. tstart starting time tend ending time steps number of steps """ t=tstart dt=float(tend-tstart)/steps for i in range(steps): dx=de(t,x) #derivative at initial point xt=[a+b*dt for (a,b) in zip(x,dx)] #step using euler t+=dt dx2=de(t,xt) # deriv at forward step x=[a+(b+c)*dt/2 for (a,b,c) in zip(x,dx,dx2)] #step forward with average return x def runge(de, x, tstart, tend, steps): """ Propagates x by solving diffeq using 4th order Runge-Kutta Method de is a funciton of t and x and return dxdt. x is list with initial values. tstart starting time tend ending time steps number of steps """ t=tstart dt=float(tend-tstart)/steps for i in range(steps): # evaluate derivative at initial point to get k1 # half step forward with k1 and evaluate deriv to get k2 # half step forward with k2 and evaluate deriv to get k3 # full step forward with k3 and evaluate deriv to get k4 k1=de(t,x) k2=de(t+dt/2,[a+b*dt/2.0 for a,b in zip(x,k1)]) k3=de(t+dt/2,[a+b*dt/2.0 for a,b in zip(x,k2)]) k4=de(t+dt,[a+b*dt for a,b in zip(x,k3)]) # full step forward with average of k1,k2,k3,k4 for new point x=[a+(b1+2*b2+2*b3+b4)/6.0*dt for (a,b1,b2,b3,b4) in zip(x,k1,k2,k3,k4)] t+=dt return x