ASKSAGE: Sage Q&A Forum - Individual question feedhttp://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 12 Dec 2011 06:33:17 -0600Is there a way to solve a differential equation in sage with adaptive step size?http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/Is there a way to solve a differential equation in sage with adaptive step size?Sat, 09 Oct 2010 18:04:03 -0500http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/Answer by mhampton for <p>Is there a way to solve a differential equation in sage with adaptive step size?</p>
http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?answer=11724#post-id-11724Yes, the default algorithm of `ode_solver` is Runge-Kutta-Fehlberg 4-5, which is an adaptive step-size algorithm. The `ode_solver` class is wrapping routines from the GNU Scientific Library (GSL).
I recommend reading the documentation of ode_solver, there are a variety of other methods available. Here's a simple example solving a Lotka-Volterra equation:
T = ode_solver()
T.function = lambda t, y: [y[0]-y[0]*y[1], -y[1]+y[0]*y[1]]
sol_lines = Graphics()
for i in srange(0.1,1.1,.1):
T.ode_solve(y_0=[i,i],t_span=[0,10],num_points=1000)
y = T.solution
sol_lines = sol_lines + line([x[1] for x in y], rgbcolor = (i,0,1-i))
show(sol_lines+point((1,1),rgbcolor=(0,0,0)), figsize = [6,6], xmax = 6, ymax = 6)
Mon, 11 Oct 2010 01:47:29 -0500http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?answer=11724#post-id-11724Comment by Shashank for <p>Yes, the default algorithm of <code>ode_solver</code> is Runge-Kutta-Fehlberg 4-5, which is an adaptive step-size algorithm. The <code>ode_solver</code> class is wrapping routines from the GNU Scientific Library (GSL). </p>
<p>I recommend reading the documentation of ode_solver, there are a variety of other methods available. Here's a simple example solving a Lotka-Volterra equation:</p>
<pre><code>T = ode_solver()
T.function = lambda t, y: [y[0]-y[0]*y[1], -y[1]+y[0]*y[1]]
sol_lines = Graphics()
for i in srange(0.1,1.1,.1):
T.ode_solve(y_0=[i,i],t_span=[0,10],num_points=1000)
y = T.solution
sol_lines = sol_lines + line([x[1] for x in y], rgbcolor = (i,0,1-i))
show(sol_lines+point((1,1),rgbcolor=(0,0,0)), figsize = [6,6], xmax = 6, ymax = 6)
</code></pre>
http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?comment=22580#post-id-22580Thanks a lot. I don't know why I was using "desolve_system_rk4". It is very slow compared to "ode_solver".Wed, 13 Oct 2010 09:15:27 -0500http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?comment=22580#post-id-22580Answer by Joaquim Puig for <p>Is there a way to solve a differential equation in sage with adaptive step size?</p>
http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?answer=12995#post-id-12995If you had your solution already coded for "desolve_system_rk4" you could try "desolve_system_odeint" which has the same syntax and uses an implicit method by default (you can change the method, of course). It should be much faster than the rk4.Mon, 12 Dec 2011 06:33:17 -0600http://ask.sagemath.org/question/7664/is-there-a-way-to-solve-a-differential-equation-in-sage-with-adaptive-step-size/?answer=12995#post-id-12995