# Contents

## Idea

To give a first impression on what kind of functions there are in Sage for numerically solving differential equations. There is support for solving differential equations symbolically in Sage as well but that will not be treated here.

## Details

We assume you got a new worksheet up and named, so the first you see is the notebook page. If you are like me then you try out all buttons and menues and see what they contain and then I look for the help button, which in the Sage notebook worksheet is up to the right as a blue text link. Press that and you will get a separate browser window with all type of help. Scroll down to the “Key and Mouse Bindings” and leave it open ! Read about how to “Evaluate input” and “Tab completion” at some point.

So let us think about ways to solve the Lotka-Volterra in Sage: A system of two first order ordinary differential equations. We could try the reference manual, but we can also use tab completion which is one of many short cuts that makes you focus on research, not reading manuals. So You could perhaps try writing “ode” or just “de” like I did and then press tab:

So that was a lot of interesting functions, some relevant, like the venerable runge-kutta , which I scrolled down to and its shown by a darker blue. But I saw another function called “desolvers”. Select that in the list and it will end up in as text in the input cell and type a “?”. Scroll up in the help window and you will see that you can read about “Help About”

So there is more information on some of the available solvers in Sage for differential equations. I intend to make something more interactive and i know that neither Sage euler or runge-kutta are slow as they are implemented in Python. But I just read on Ask Sage that there is another possibility, which uses a GNU Scientific Library (GSL) and is much faster.

But for now let us use the runge-kutta solver, as we are going to use Python list comprehensions which requires some attention if you haven’t come across them before. First we are going to need symbolic variables in Sage, x,y,t which are declared in the first line. I use an Lotka-Volterra example which was shown when I typed “desolve_system_rk4?”. The arguments required is a list of the right hand side of the equations for $\dot x,\dot y$, dependent variables, initial conditions and independent variable.

P contains the result in the form of a list of [t,x,y] values. So first we want to see the phase diagram and use a list comprehension and a list slice to get rid of the t-value in each. The list comprehension that achieves this is:

 [e1: for e in P] 

It is intentionally reminiscent of how you specify mathematical sets,eg: $\{e^2 | e \in P\}$. The Sage function for drawing a line is just named “line()”. We can use the above list comprehension as argument to line() and it will actually plot lines between all the points (x,y) in the list of results P.

What would happen if you try to change the last line to line(P)?

If you have Java installed properly for your browser you see the time evolution of the Lotka-Volterra function. If you see it try to hover over the graph and right click. You should see a JMol menu which allows you to zoom,spin, measure and more advanced things. Sage uses Jmol by default when it finds a 3-d graphic object and shows it. If you have problem with Jmol you can try changing the last line to:

 line(P).show(viewer=‘tachyon’) #or in 4.7 u can use viewer= ‘canvas3d’ 

Tachyon is a ray-tracer, which also ships with Sage, and we will not cover that here any further but I know you will !

Can you make a plot over how x or y varies over time - using the same idea of list slicing? See the published worksheet, below where I also changed variable names to something more appropriate