Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Sage beginners guide - Finch Kraigh copy.pdf
Скачиваний:
13
Добавлен:
10.02.2015
Размер:
5.86 Mб
Скачать

1

What Can You Do with Sage?

Sage is a powerful tool—but you don't have to take my word for it. This chapter will showcase a few of the things that Sage can do to enhance your work. At this point, don't expect to understand every aspect of the examples presented in this chapter. Everything will be explained in more detail in the later chapters. Look at the things Sage can do, and start to think about how Sage might be useful to you. In this chapter, you will see how Sage can be used for:

Making simple numerical calculations

Performing symbolic calculations

Solving systems of equations and ordinary differential equations

Making plots in two and three dimensions

Analysing experimental data and fitting models

Getting started

You don't have to install Sage to try it out! In this chapter, we will use the notebook interface to showcase some of the basics of Sage so that you can follow along using a public notebook server. These examples can also be run from an interactive session if you have installed Sage.

What Can You Do with Sage?

Go to http://www.sagenb.org/ and sign up for a free account. You can also browse worksheets created and shared by others. If you have already installed Sage, launch the notebook interface by following the instructions in Chapter 3. The notebook interface should look like this:

Create a new worksheet by clicking on the link called New Worksheet:

[ 10 ]

Chapter 1

Type in a name when prompted, and click Rename. The new worksheet will look like this:

Enter an expression by clicking in an input cell and typing or pasting in an expression:

[ 11 ]

What Can You Do with Sage?

Click the evaluate link or press Shift-Enter to evaluate the contents of the cell.

A new input cell will automatically open below the results of the calculation. You can also create a new input cell by clicking in the blank space just above an existing input cell. In Chapter 3, we'll cover the notebook interface in more detail.

UsingSageasapowerfulcalculator

Sage has all the features of a scientific calculator—and more. If you have been trying to perform mathematical calculations with a spreadsheet or the built-in calculator in your operating system, it's time to upgrade. Sage offers all the built-in functions you would expect. Here are a few examples:

[ 12 ]

Chapter 1

If you have to make a calculation repeatedly, you can define a function and variables to make your life easier. For example, let's say that you need to calculate the Reynolds number, which is used in fluid mechanics:

You can define a function and variables like this:

Re(velocity, length, kinematic_viscosity) = velocity * length / kinematic_viscosity

v = 0.01 L = 1e-3 nu = 1e-6

Re(v, L, nu)

When you type the code into an input cell and evaluate the cell, your screen will look like this:

Now, you can change the value of one or more variables and re-run the calculation:

[ 13 ]

What Can You Do with Sage?

Sage can also perform exact calculations with integers and rational numbers. Using the predefined constant pi will result in exact values from trigonometric operations. Sage will even utilize complex numbers when needed. Here are some examples:

Symbolicmathematics

Much of the difficulty of higher mathematics actually lies in the extensive algebraic manipulations that are required to obtain a result. Sage can save you many hours, and many sheets of paper, by automating some tedious tasks in mathematics. We'll start with basic calculus. For example, let's compute the derivative of the following equation:

The following code defines the equation and computes the derivative:

var('x')

f(x) = (x^2 - 1) / (x^4 + 1) show(f)

show(derivative(f, x))

[ 14 ]

Chapter 1

The results will look like this:

The first line defines a symbolic variable x (Sage automatically assumes that x is always a symbolic variable, but we will define it in each example for clarity). We then defined a function as a quotient of polynomials. Taking the derivative of f(x) would normally require the use of the quotient rule, which can be very tedious to calculate. Sage computes the derivative effortlessly.

Now, we'll move on to integration, which can be one of the most daunting tasks in calculus.

Let's compute the following indefinite integral symbolically:

The code to compute the integral is very simple:

f(x) = e^x * cos(x) f_int(x) = integrate(f, x) show(f_int)

The result is as follows:

To perform this integration by hand, integration by parts would have to be done twice, which could be quite time consuming. If we want to better understand the function we just defined, we can graph it with the following code:

f(x) = e^x * cos(x) plot(f, (x, -2, 8))

[ 15 ]

What Can You Do with Sage?

Sage will produce the following plot:

Sage can also compute definite integrals symbolically:

To compute a definite integral, we simply have to tell Sage the limits of integration:

f(x) = sqrt(1 - x^2)

f_integral = integrate(f, (x, 0, 1)) show(f_integral)

The result is:

This would have required the use of a substitution if computed by hand.

[ 16 ]

Chapter 1

Have a go hero

There is actually a clever way to evaluate the integral from the previous problem without doing any calculus. If it isn't immediately apparent, plot the function f(x) from 0 to 1 and see if you recognize it. Note that the aspect ratio of the plot may not be square.

The partial fraction decomposition is another technique that Sage can do a lot faster than you. The solution to the following example covers two full pages in a calculus textbook — assuming that you don't make any mistakes in the algebra!

f(x) = (3 * x^4 + 4 * x^3 + 16 * x^2 + 20 * x + 9) / ((x + 2) * (x^2 + 3)^2)

g(x) = f.partial_fraction(x) show(g)

The result is as follows:

We'll use partial fractions again when we talk about solving ordinary differential equations symbolically.

Linearalgebra

Linear algebra is one of the most fundamental tasks in numerical computing. Sage has many facilities for performing linear algebra, both numerical and symbolic. One fundamental operation is solving a system of linear equations:

Although this is a tedious problem to solve by hand, it only requires a few lines of code in

Sage:

A = Matrix(QQ, [[0, -1, -1, 1], [1, 1, 1, 1], [2, 4, 1, -2], [3, 1, -2, 2]])

B = vector([0, 6, -1, 3]) A.solve_right(B)

[ 17 ]

What Can You Do with Sage?

The answer is as follows:

Notice that Sage provided an exact answer with integer values. When we created matrix A, the argument QQ specified that the matrix was to contain rational values. Therefore, the result contains only rational values (which all happen to be integers for this problem). Chapter 5 describes in detail how to do linear algebra with Sage.

Solvinganordinarydifferentialequation

Solving ordinary differential equations by hand can be time consuming. Although many differential equations can be handled with standard techniques such as the Laplace transform, other equations require special methods of solution. For example, let's try to solve the following equation:

The following code will solve the equation:

var('x, y, v') y=function('y', x) assume(v, 'integer')

f = desolve(x^2 * diff(y,x,2) + x*diff(y,x) + (x^2 - v^2) * y == 0, y, ivar=x)

show(f)

The answer is defined in terms of Bessel functions:

It turns out that the equation we solved is known as Bessel's equation. This example illustrates that Sage knows about special functions, such as Bessel and Legendre functions. It also shows that you can use the assume function to tell Sage to make specific assumptions when solving problems. In Chapter 7, we will explore Sage's powerful symbolic capabilities.

[ 18 ]

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]