[Go to the Notes on Diffy Qs home page]

Notes on Diffy Qs - Sage demos for section 1.7

Press the Activate or Evaluate button below to launch each Sage demonstration. You will be able to interact with the first demonstration after it launches and change numbers with sliders. You may have to wait a little before the graph or result appears. Be patient. After the first demonstration there are a few separate Sage demos where you can edit the code to experiment with Euler's method.

Euler's method demo

Euler's method for one of three problems

\((1) \qquad y' = 5x + y - 5, \quad y(0) = 1, \)

\((2) \qquad y' = 2-y, \quad y(0) = 0, \)

\((3) \qquad y' = \left(5+\frac{1}{0.001+(x-1.5)^2}\right)(3-y)(2-y)^2, \quad y(0) = 1.9, \)

using several different step sizes h. The actual solution is also graphed in green to compare. The slider will modify the step size. Notice how the third example does not seem to get much better as we reduce the step size, especially for \(x ≥ 1.5 .\)

Euler's method with Sage

First let's just make a table.

Now let's plot the result.

A challenge for those that know a little Python/Sage would be to modify the two examples above to do the improved Euler method (described just above Exercise 1.7.104), or the Runge-Kutta method (described just above Exercise 1.7.7). Be careful to keep the correct indentation as Sage is really Python under the hood and the indentation is important.

How about plotting the actual solution. In this code we ask Sage to symbolically solve, so Sage will only plot the solution if it can find the actual solution analytically, and it will give you an error otherwise. This works for the examples (1) and (2) above, but not for example (3).

Sage has Runge-Kutta built in, so we could just use that instead of solving exactly, let's do that for the example (3) from above. That is going to be very close to the real answer for most examples you throw at it. You can also try playing around with the step parameter below, and you can see that even Runge-Kutta gets confused with a large enough step size.


The original code is due to Ryan Burkhart, but is now heavily modified.


Valid HTML 5 Valid CSS!