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

Making Symbolic Mathematics Easy

Summary

We covered a lot of material in this chapter. Sage makes it fast and easy to do tedious symbolic tasks like computing integrals and Laplace transforms for complicated functions. In fact, the hardest part is making sure that you have defined your functions and expressions correctly.

Specifically, we covered:

Working with symbolic expressions

Manipulating symbolic expressions to put them in the form you want

Performing basic calculus operations like computing limits, derivatives, and integrals

Finding series representations, and computing their sums

Computing Laplace transforms

Finding exact solutions to ordinary differential equations

Sage has powerful symbolic capabilities. However, many real-world problems simply don't have analytical solutions. In other cases, Sage might not be able to find an analytical solution, even when it exists—software is never perfect! Some symbolic operations may consume so much memory or CPU time that they become impractical. Integrals, systems of

equations, and differential equations often require numerical methods of solution. Sage also has powerful numerical capabilities, which we'll explore in the next chapter.

[ 206 ]

8

SolvingProblemsNumerically

The previous chapter described how to use Sage to solve many difficult problems in symbolic mathematics. While this capability is very useful, many real-world problems do not lend themselves to symbolic computation. Some differential equations don't have closed-form solutions, and not every integral can be computed in terms of elementary functions. In other cases, a function value may have to be computed from a look-up table that was derived from experimental results, which precludes symbolic computation. In this chapter, we will demonstrate some of the tools in Sage that allow us to solve problems numerically.

We will learn how to:

Find the roots of an equation

Compute integrals and derivatives numerically

Find minima and maxima of functions

Compute discrete Fourier transforms, and apply window functions

Numerically solve an ordinary differential equation (ODE), and systems of ODEs

Use optimization techniques to fit curves and find minima

Explore the probability tools in Sage

Let's get started!

Solving Problems Numerically

SageandNumPy

OnepotentialsourceofconfusioninthischapteristhatSageincorporatesfunctionsfrom NumPy,Maxima,theGNUScientificLibrary(GSL),andothersources.Wheneverpossible,we willusefunctionsinSage.However,sometimesweneedtogotoNumPytoperformaparticular calculation.Tominimizethepossibilityofconfusion,donot usethesyntaxfrom numpy import *.ThisimportseverynamefromNumPyintoSage,overridingsomepre-definedSagefunctions andobjects.Usethesyntaxshownintheexamples,whichkeepsNumPyfunctionsseparate fromSagefunctions.

Solvingequationsandfindingrootsnumerically

We've already looked at solving systems of linear equations in Chapter 5, when we learned about linear algebra. We created matrices using integers or symbols, but you can just as easily create vectors and matrices with real numbers or floating-point numbers. Chapter 5 also covered some numerical operations on matrices, such as computing the QR factorization and singular value decomposition. Now, we will learn how to find roots of equations numerically in Sage.

Timeforaction–findingrootsofapolynomial

Let's start by finding the roots of a polynomial.

g(x) = expand((x^2 - 1)^3 * (x^2 + 1) * (x - 2)); g.show()

print("Root at x = {0}".format(g.find_root(-2,2))) print("Root at x = {0}".format(g.find_root(-2,0))) print("Root at x = {0}".format(g.find_root(0.5,1.5)))

plt = plot(g, (x, -1.2, 2.01)) show(plt, figsize=(4, 3))

[ 208 ]

Chapter 8

The output is shown in the following screenshot:

What just happened?

We defined a fairly complicated polynomial equation with a number of real and imaginary roots. We can see that the function will have real roots at 1, -1, and 2 simply by looking at the factored form of the function, which can be confirmed by looking at the plot. When trying out a new numerical method, it's always a good idea to start with a problem that you know the answer to, so that you can evaluate the accuracy and reliability of the method. The find_root function is a relatively simple way to find a single root within a given domain, specified by the given end points. In the first call, we gave find_root a wide span that contained three roots, and it happened to find the root at x=2. In the next two calls, we used a narrower span that only included a single root in each span. Finding roots numerically is relatively simple, but you have to understand the equation you are working with to find the correct root.

[ 209 ]

Solving Problems Numerically

Findingminimaandmaximaoffunctions

Sometimes, we are interested in the minima or maxima of a function, rather than the zero crossings. For example, an engineer might define a function that estimates the cost of a product. Finding the minimum of this function will help the engineer design a product with the lowest cost. Conversely, one might want to maximize a function that represents the performance of a system. The problem of finding minima and maxima is a form of numerical optimization, which we'll cover later in the chapter.

Timeforaction–minimizingafunctionofonevariable

We'll define another function of one variable and let Sage find the minimum:

var('x')

 

f = lambda x:

3 * x^3 - 7 * x^2 + 2

minval, x_min

= find_minimum_on_interval(f, 0, 3)

print("Min on

interval [0,3]: f({0}) = {1}".format(x_min, minval))

maxval, x_max

= find_maximum_on_interval(f, -1, 1)

print("Max on

interval [-1,1]: f({0}) = {1}".format(x_max, maxval))

f_plot = plot(f, (x, -1, 2.5))

min_point = point((x_min, minval), color='red', size=50) max_point = point((x_max, maxval), color='black', size=50) show(f_plot + min_point + max_point, figsize=(4, 4))

The results are shown in the following screenshot:

[ 210 ]

Chapter 8

What just happened?

We defined a function that represents a cubic polynomial using the lambda construct. Recall from Chapter 4 that lambda is a shorthand way of defining a Python function. We used a Python function, rather than a callable symbolic expression, because numerical methods are designed to work with functions that return real numbers. The functions find_minimum_ on_interval and find_maximum_on_interval work in a similar way to find_root. Each function accepts the endpoints of an interval and finds the minimum or maximum of the function on that interval, and returns a tuple that contains the (x,y) coordinates of the minimum or maximum. These functions also accept the keyword argument tol to specify the tolerance determines when the algorithm has converged on a maximum or minimum (the default is 1.48e-8). The keyword argument maxfun sets a limit on the maximum number of function evaluations (default 500) that will be used to find the point of interest. Finally, we used the point graphics function to illustrate the points that we found.

Functionsofmorethanonevariable

Finding minima of a function of multiple variables is a more challenging problem because each independent variable adds a new dimension to the search space. Sage uses a more sophisticated function for minimizing functions of two or more variables.

Timeforaction–minimizingafunctionofseveralvariables

Now, we'll minimize a function of two variables, and use a contour plot to illustrate the results:

var('x, y')

f = 100 * (y - x^2)^2 + (1 - x)^2 + 100 * (2 - y^2)^2 + (1 - y)^2 min = minimize(f, [0,0], disp=0)

plt = contour_plot(f, (x, -0.3, 2), (y, -0.3, 2), fill=False, cmap='hsv', labels=True)

pt = point(min, color='red', size=50) show(plt+pt, aspect_ratio=1, figsize=(4, 4))

[ 211 ]

Solving Problems Numerically

The plot is shown below:

What just happened?

We started out by defining a callable symbolic expression that represents a polynomial function of two variables. We then used minimize to find a minimum of the function near the point (0,0). minimize works a little differently from the functions in the previous examples. Rather than specifying limits on the domain of the problem, you need to pass an initial guess to minimize so that it knows where to start searching for a minimum. Like the functions find_root and find_minimum_on_interval, minimize will only find a minimum in the vicinity of its starting point (if it exists), which is known as a local minimum. We used the keyword argument disp=0 to prevent the function from displaying text that summarizes its solution process.

We will pause here to clarify the concept of local and global minima. A local minimum is the lowest value that a function takes on over a portion of its domain. A function can have many local minima. A function's global minimum is the lowest value of the function over its entire domain. There is no general algorithm that can find the global minimum of an arbitrary function. Therefore, minimize (and all the other minimum-finding functions in Sage) are only guaranteed to find a local minimum (assuming one exists), which may happen to also be the global minimum.

[ 212 ]

Chapter 8

Minimizingafunctionofseveralvariablesissignificantlymorecomplicatedthanminimizinga functionofonevariable.minimize isactuallyaninterfacetoseveralminimizationalgorithms. Ifthefunctiontobeminimizedissymbolic,thedefaultalgorithmistheBroyden-Fletcher- Goldfarb-Shannon(bfgs)algorithm.IfthefunctionisaPythonfunction,thesimplexalgorithm isthedefault.Thefollowingtablesummarizestheoptionsthatcanbepassedto minimize:

Keyword

Description

disp

0 disables text output (default is 1)

algorithm

A string value that specifies algorithm:

 

'default' chooses default (see text)

 

'simplex' chooses the simplex method

 

'powell' chooses Powell's conjugate gradient descent method

 

'bfgs' chooses Broyden-Fletcher-Goldfarb-Shannon (bfgs)

 

'cg' chooses conjugate-gradient (requires gradient)

 

'ncg' chooses Newton-conjugate-gradient (requires gradient and Hessian)

gradient

Function that computes the gradient

hessian

Function that computes the Hessian

If the function to be minimized is symbolic, you do not have to provide the Hessian or the gradient because Sage will compute them symbolically. If the function to be minimized is a Python function and you choose an algorithm that requires the Hessian or the gradient, you will have to provide functions that compute the Hessian and/or the gradient. The Sage reference manual has an example that demonstrates how to minimize a Python function with an algorithm that requires a gradient: http://www.sagemath.org/doc/ reference/sage/numerical/optimize.html

Numericalapproximationofderivatives

In the previous chapter, we learned how to use Sage to compute derivatives of symbolic functions. Now, we will learn how to approximate derivatives numerically.

Timeforaction–approximatingderivativeswithdifferences

Let's start by defining a function of one variable. We'll use NumPy to estimate the derivative numerically, and we'll plot the estimate with matplotlib.

import numpy as np

 

 

 

import

matplotlib.pyplot as plt

import

matplotlib as mpl

 

 

 

mpl.rc('font', size=10)

# set default font size

 

 

 

 

 

 

 

 

[ 213 ]

 

 

 

 

 

Solving Problems Numerically

dx = 0.01

x = np.arange(0, 2, dx) f = power(x, 3)

dfdx = 3*power(x, 2)

plt.figure(figsize=(4, 4)) plt.plot(x, f, label='f(x)')

plt.plot(x, dfdx, color='red', label='Analytical df/dx')

df = np.diff(f)

plt.plot(x[:-1], df/dx, color='black', label='Numerical df/dx')

plt.xlabel('x')

plt.ylabel('f(x)')

plt.legend(loc='best')

plt.savefig('diff.png')

plt.close()

The output is shown in the following screenshot:

[ 214 ]

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