- •Credits
- •About the Author
- •About the Reviewers
- •www.PacktPub.com
- •Preface
- •Getting started
- •More advanced graphics
- •Summary
- •Installing Sage
- •Starting Sage
- •Start Sage
- •Prerequisites
- •Installation
- •Summary
- •Command history
- •Working with code
- •Arithmetic operators
- •Strings
- •Functions
- •Functions with keyword arguments
- •Objects
- •Summary
- •Python 2 and Python 3
- •Running scripts
- •Strings
- •List comprehensions
- •Storing data in a dictionary
- •Summary
- •Vectors and vector spaces
- •Creating a vector space
- •Vector operators and methods
- •Decomposing matrices
- •Summary
- •Using graphics primitives
- •Summary
- •Substitutions
- •Finding roots
- •Derivatives
- •Integrals
- •Series and summations
- •Summary
- •Computing gradients
- •Constrained optimization
- •Probability
- •Summary
- •Making our tanks move
- •Unit testing
- •Summary
- •Introducing Python decorators
- •Making interactive graphics
- •Summary
- •Index
Chapter 7
What just happened?
We started out by defining a simple function with a discontinuity at zero. We used the function limit (or lim) to compute the limit as x approaches zero. The first argument to limit is a function, and the second argument is the value at which to compute the limit. If the dir keyword argument is present, a one-sided limit is computed from either above or below the specified value. The values '+', 'plus', or 'right' compute the limit from above, while '-', 'minus'¸ or 'left' compute the limit from below. If dir is omitted, a two-sided limit is computed. limit also accepts the keyword argument taylor, which is False by default. If taylor=True, then a Taylor series is used to approximate the
function when computing the limit. The second function we defined looks more complicated, although the limit calculation is straightforward. The third case demonstrated that Sage is able to handle indeterminate forms, where the function evaluates to 0/0 at the given point.
Derivatives
The derivative describes how a function responds to an infinitesimal change in one of its independent variables. Derivatives are used to compute rates of change.
Timeforaction–calculatingderivatives
Run the following code to see how to compute derivatives with Sage:
var('x, y')
f(x, y) = 3 * x^4 * y^3 + 9 * y * x^2 - 4 * x + 8 * y print("f(x,y):")
f.show()
dfdx(x, y) = diff(f, x) print("df/dx:") dfdx.show()
dfdy(x, y) = diff(f, y) print("df/dy:") dfdy.show()
print("Second derivative:") |
|
d2fdx2(x, y) = derivative(f, x, 2) |
# Synonym for diff |
d2fdx2.show() |
|
# Trigonometric functions |
|
g(x) = sqrt(x^3 + csc(x)) |
|
print("g(x):") |
|
g.show() |
|
dgdx(x) = g.diff(x) |
|
print("dg/dx:") |
|
dgdx.show() |
|
[ 195 ]
Making Symbolic Mathematics Easy
#Implicit differentiation
#The next line tells Sage that y is a function of x y(x) = function('y', x)
expr = 5 * y^2 + sin(y) == x^2 print("Expression:") expr.show()
#take the derivative and solve for dy/dx
dydx = solve(diff(expr), diff(y)) print("dy/dx:")
dydx[0].show()
The results are shown in the following screenshot:
[ 196 ]
Chapter 7
What just happened?
Sage is able to compute derivatives for many types of functions. You can use a function called diff (or differentiate or derivative), or an equivalent method with the same names. In the example, we used both the function form and the method form. When calling diff as a function, the first argument is a function which is differentiated against the variable specified in the second argument. If a third argument is present, it is the degree of the derivative (the default is one, for the first derivative). We started by defining a long polynomial function of two variables, x and y. We then differentiated with respect to each variable. We also computed the second derivative. We didn't have to change anything to compute the derivative of a trigonometric function. In the final part of the example, we demonstrated how to do implicit differentiation. The function y(x) was defined implicitly by a relational symbolic expression. We used the syntax y(x) = function('y', x)
to create a new symbolic function called y that is a function of x. The first argument of function is a string that contains the name of the new function. The next argument is the argument of the symbolic function (it can have multiple arguments). We then used the diff function to compute the derivative of the entire expression, and then used the solve function to isolate the derivative.
[ 197 ]
Making Symbolic Mathematics Easy
Integrals
Sage is able to integrate many symbolic functions that would need to be looked up in a table of integrals, or computed using laborious methods like integration by parts. Sage can easily compute symbolic integrals that cannot be found in any book.
Timeforaction–calculatingintegrals
Run the following example to see how to compute integrals with Sage:
var('x')
print("Elementary integrals:")
f = x^2 print(f.integrate(x)) print(integral(e^x,x)) print(integral(1/x,x)) print(integral(sinh(x), x))
print(integral(1/sqrt(1+x^2),x))
print("\nIntegration by parts:") print(integral(e^x*cos(x), x)) print(integral(sqrt(x^2-25)/x, x))
print("\nDefinite integral:") print(integral(1/(1+x^2), x, -1, 1))
print("\nImproper integral:") print(integral(1/(1+x^2), x, -infinity, infinity))
print("\nDivergent integral:") |
|
print(integral(1/(1-x), x, 1,2)) |
# Diverges |
The results are shown in the following screenshot:
[ 198 ]
