Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

Introduction to Sage for Differential Equations

Views: 1134

Introduction to Sage

Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.

Basic Computations

Let's start with an easy calculation. Below you will see a Sage cell, with 2+2 already entered. Click inside the cell, then hit Shift-Return to evaluate the cell and get the result:

2+2

As the above example shows, Sage can be used as a calculator. But Sage is a very good calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type sin(pi/3) and press [Shift]-[Enter] to see a little of what Sage can do.

sin(pi/3)

That's really good! But if you want to see a numerical approximation to the number sin(π/3)\sin(\pi/3), you can use the Sage function n. You can also use the symbol _ to refer to the last computation that Sage did. So if the last thing you computed was sin(pi/3) above, you can type n(_) to find a numerical approximation. (If you have entered something else since then, just type n(sin(pi/3)) in a new cell.)

n(_)

If you want more accuracy, you can add the argument digits inside the n function, like this:

n(sqrt(2), digits=50)

Sage is familiar with many functions you know about. A few notes about some special ones:

  • The inverse trig functions are written as arcsin, arctan, etc.

  • The natural logarithm can be written as ln or as log. If you want a logarithm to another base, such as base 10, specify the base with the base argument: log(x, base=10), for example.

  • The natural exponential function, exe^x, can be written as exp(x).

Algebra

Sage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both x and t to be symbolic variables with the statement below:

var("x, t")
(x, t)

Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication must be indicated with a *; it cannot be represented by juxtaposition!)

expand((x + 5)*(2*x - 1))
2*x^2 + 9*x - 5
factor(6*x^3 + 26*x^2 - 20*x)
solve( x^2 + x == 6, x)

It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function f(x)=x2+2f(x) = x^2 + 2:

f(x) = x^2 + 2

When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function ff, if you wish:

f(2)

There are many other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more.

Basic Calculus

Sage can also carry out some basic symbolic calculus. Here are a few examples:

var("x") derivative(3*exp(x^2)+log(x), x) integrate(x^3 + x + 1, x) integrate(x, (x, 0, 1))

This means that we can try to use Sage to help us verify that something is a solution to a differential equation:

var("x", "c") f(x) = 2/3 + c*exp(-3*x) derivative(f(x), x)+ 3*f(x)

Plotting

It is easy to plot a function in Sage. Here is an example of plotting the function f(x)=3x2+2x1f(x) = 3 x^2 + 2 x - 1 on the interval [2,2][-2, 2]:

plot(3*x^2+2*x-1, (x, -2, 2))

Sometimes the graph does not show the sorts of details you would like. Try the following to plot tan(x)\tan(x) on [π,π][-\pi, \pi]:

plot(tan(x), (x, -pi, pi))

The yy values get so big that we cannot see much else. We can add the extra arguments ymin and ymax to the plot function to fix the window of the plot:

plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2)

(By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph.)

Plotting a Slope Field

Sage can plot slope fields using the command plot_slope_field(f(x, y), (x, xstart, xend), (y, ystart, yend)), where f(x, y) is the function yy' is set equal to and we wish to view a window with xx between xstart and xend and yy between ystart and yend. (Here we are using xx and yy as the independent and dependent variables, respectively.)

Note that xx and yy need to be defined as variables in Sage for this to work.

var("x", "y") plot_slope_field(y, (x, -2, 2), (y, -2, 2))

The above shows a slope field for the equation y=yy' = y on [2,2]×[2,2][-2, 2] \times [-2, 2]. (Solutions take the form y=Cexy = Ce^x.) However, it will probably look better if we make the xx- and yy-axes scaled the same by adding the option aspect_ratio=1 to the end of the command:

var("x", "y") plot_slope_field(y, (x, -2, 2), (y, -2, 2), aspect_ratio=1)

Note that you can combine two plots by adding them. It is sometimes useful to assign a plot to a variable so you can combine plots later. (Note also that assigning a plot to a variable keeps the plot from displaying when first generated.)

y1 = plot(exp(x), (x, -2, 2), ymin=-2, ymax=2) y2 = plot(-2*exp(x), (x, -2, 2), ymin=-2, ymax=2) sf = plot_slope_field(y, (x, -2, 2), (y, -2, 2), aspect_ratio=1) y1+y2+sf

Exploration Problem

Plot a slope field for dy/dx=(3/2)3y+e3x/2dy/dx = (3/2) - 3 y + e^{-3x/2}. (Try a region near the origin, like [2,2]×[2,2][-2, 2]\times[-2, 2].)

Solve the differential equation (you know how to do this!), and plot several particular solutions on the slope field.

What patterns do you notice about the solutions? What conjectures do you have about other solutions to this equation? What evidence can you gather to support your conclusions?

Other References

Here are some useful references if you would like to explore Sage further: