Sage ODE Cheatsheet
Here’s a very brief overview of how to do a few ODE-related things in Sage, mostly intended as a reference for me, but maybe you’ll find it useful too.
First Order ODEs
Here’s how to find the general solution to the ODE y’ = x + y.
= x+y
f(x,y) = function('y')(x)
y = diff(y, x) == f(x,y) # or deq = diff(y,x) - f(x,y)
deq = desolve(deq, y)
sol sol.show()
Here’s how to find the solution that satisfies the initial value condition y(0) = -1/2 and how to plot it on the interval [-2,2]. Keeping the same first few commands as above,
= desolve(deq, y, ics=[0, -0.5])
sol = plot(sol, -2, 2)
p # or p.show() sol.show()
Here’s how to generate a slope field of the ODE together with a plot of the numerical solution to this ODE satisfying the same initial value condition.
= x+y
f(x,y) = var('y')
y = plot_slope_field(f, (x, -2, 2), (y, -2, 2))
p += desolve_rk4(f, y, ics=[0,-0.5], ivar=x, output='plot', end_points=[-2,2], thickness=2)
p p.show()
Linear Algebra
…
Systems of First Order ODEs
Here’s how to plot a vector field corresponding to the autonomous nonlinear system x’ = x + y and y’ = xy.
= var('y')
y +y,x*y), (x,-2,2), (y,-2,2)) plot_vector_field((x
…
References
Here are some other resources with more information.