Skip to main content
\(\newcommand{\Z}{\mathbb{Z}} \newcommand{\reals}{\mathbb{R}} \newcommand{\real}[1]{\mathbb{R}^{#1}} \newcommand{\fe}[2]{#1\mathopen{}\left(#2\right)\mathclose{}} \newcommand{\cinterval}[2]{\left[#1,#2\right]} \newcommand{\ointerval}[2]{\left(#1,#2\right)} \newcommand{\cointerval}[2]{\left[\left.#1,#2\right)\right.} \newcommand{\ocinterval}[2]{\left(\left.#1,#2\right]\right.} \newcommand{\point}[2]{\left(#1,#2\right)} \newcommand{\fd}[1]{#1'} \newcommand{\sd}[1]{#1''} \newcommand{\td}[1]{#1'''} \newcommand{\lz}[2]{\frac{d#1}{d#2}} \newcommand{\lzn}[3]{\frac{d^{#1}#2}{d#3^{#1}}} \newcommand{\lzo}[1]{\frac{d}{d#1}} \newcommand{\lzoo}[2]{{\frac{d}{d#1}}{\left(#2\right)}} \newcommand{\lzon}[2]{\frac{d^{#1}}{d#2^{#1}}} \newcommand{\lzoa}[3]{\left.{\frac{d#1}{d#2}}\right|_{#3}} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\sech}{\operatorname{sech}} \newcommand{\csch}{\operatorname{csch}} \newcommand \dd[1] { \,\textrm d{#1} } \newcommand \de[2] { \frac{\mathrm d{#1}}{\mathrm d{#2}} } \newcommand \intl[4]{ \int\limits_{#1}^{#2}{#3}\dd{#4} } \newcommand\at[2]{\left.#1\right|_{#2}} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section2.4Declaring Variables and Plotting

Sage allows us to solve equations symbolically. To make this possible we must define variables that are used in the symbolic manipulation.

We will demonstrate this using a complete differential equation example. Taking this approach has the additional advantage of introducing some other Sage features.

Don't worry if you have not yet covered differential equations in your studies. What follows is more to demonstrate SageMath capabilities rather than the maths. Come back here once you have solved some DE and it will then make more sense.

Subsection2.4.1Use SageMath to solve differential equations

First we have to know how to describe a differential equation (DE) with Sage. The syntax may not be obvious but the are ample examples in the documentation.

Sage can solve a large class of first-order and second-order Ordinary Differential Equations (ODEs) as well as Initial Value Problems (IVPs). The chapter on differential equations Chapter 4 will go into more details. This particular example just uses DE to illustrate how variables are declared and used.

For this first example, I'll express the differential equation using common mathematical notation, then enter it into Sage using Sage's notation. Here's the mathematical notation for our example:

\begin{equation}y(t) + rcy'(t) = 0\label{men-1}\tag{2.4.1}\end{equation}\begin{equation}y(0) = 1\label{men-2}\tag{2.4.2}\end{equation}

This expression says that the unknown function (the function to be found) produces the sum of y(t) and r c y'(t) for any time t, and that the initial value of the function at time zero is 1. (The meaning of r and c will become clear after a bit.) This is an example of a differential equation that specifies an initial value, as many do. In this case I intend to scale the result after acquiring the result, and this initial value makes that easy. Here is how I submit this differential equation to Sage:

(From P Lutus - Applying Sage to physics)

Or in the prettier format:

Let us break this entry down:

  • var('r c t') tells Sage to pre-declare the variables that will be used.
  • \(y = function('y')(t)\) tells Sage that \(y\) is a function of \(t\), e.g. as though every time we type \(y\) we mean \( y(t)\). This is a way to identify \(y\) as a function, not just a variable, and associate it with \(t\).
  • \(de = y + r*c*diff(y,t) == 0\) creates a variable \(de\) that contains one of the statements of the DE.
  • \(des = desolve(de,[y,t],[0,1])\) invokes the Sage DE solver using our definition \(de\) as an argument, and:
  • \([y,t]\) identifies \(y\) as the function of interest and \(t\) as the dependent variable
  • \([0,1]\) sets the initial conditions: at \(t = 0, y = 1\), or as we expressed it above, "\(y(0) =1\)".

Now let us plot this

Let us now take a more general solution.