Skip to main content
\( \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section4.1Solving Differential Equations

A differential equation for \(y\) as a function of \(t\) is an equation of the form \(\frac{dy}{dt}=f(t,y)\), where \(f(t,y)\) represents a function of both \(t\) and \(y\). An initial value is a value for \(y\) at some particular point \(y(t_0)\). A differential equation with a corresponding initial value has a solution if there is a function \(y(t)\) for which this equation is true for all values \(t\) in some interval and the initial value is true.

Some particular classes of differential equations have known exact solutions. More often, we need to use a computer to find an approximation for solutions. One such method was developed by mathematicians C. Runge and M. W. Kutta. Sage has an implementation of this method to generate approximate values of the solution function given the rate formula and an initial condition.

First, we might want to look at the values of the function. The following sample code numerically solves a differential equation with a given formula for the differential equation rate and an initial value y0 and creates a table of values.

You can specify the interval of time end_points and the spacing between the points dt (step size). A smaller step size generally makes the approximation more accurate but requires more computational resources. (Going too small can lead to unexpected numerical errors.) To avoid displaying every computed point, you can set the value for showEvery to a desired value. Using showEvery = 1 would display all computed points.

Important: A solution may not exist on the full interval you ask for. So you may need to experiment with smaller intervals and smaller step sizes for certain problems.

Often, we want to create a graph of the solution to the differential equation. Sage makes this easy as well. Instead of printing out the table of values, we will use a list_plot. Setup is identical to creating a table. Only the last lines are different.