| Download
All published worksheets from http://sagenb.org
Project: sagenb.org published worksheets
Views: 168730Image: ubuntu2004
Calculus in SAGE: A Quickstart Guide
Jamie Mulholland
Department of Mathematics
Simon Fraser University
Department of Mathematics
Simon Fraser University
Sage is a free open-source mathematics software system licensed under the GPL. It combines the power of many existing open-source packages into a common Python-based interface. Their mission is to create a viable free open source alternative to Magma, Maple, Mathematica and Matlab. Sage is extremely powerful and is extensively used at the research level in mathematics.
To download a free copy of Sage point your browser to http://www.sagemath.org/. Choose to download from the SFU mirror site. Installation instructions are included in the download. I will point out that Sage can run from the command line, or through a notebook (what you are reading right now). The notebook runs in your web browser.
I'll assume now that you have opened the Sage program and are using the notebook interface (this includes having just opened this page in your web browser). You can view the interactive Sage tutorial by clicking Help, then click Tutorial. However, for those who wish for an even quicker tutorial the document you are reading now will give you a quick look at how Sage can tackle exercises in Calculus.
Contents:
-
Basics:
- Syntax and Arthmetic
- Getting Help
- Storing Items in Memory
- Functions and Graphs
- Differentiation
- Limits
- Solving Equations
- Newtons Method
- Integration
- Sequences
- Series
- Taylor Series
Differential Calculus:
Integral Calculus:
Let us start with the basics.
1) Syntax and Arthmetic
7
The text in black (i.e. the text next to the red vertical bar in the box, a.k.a execution block, is the "input". This is a command that you are giving to Sage. Pressing "shift-enter" then gets Sage to execute the command and the output is returned in blue.
Here are some more examples:
17/3
20/21
Notice Sage returns the exact value (i.e a fraction) not just a decimal approximation, which is what most calculators would return. So Sage works with exact expressions, not just approximations.
Note: " * " represents multiplication
" / " represents division
" ^ " represents exponentiation
Sage works with exact expression as seen here:
sqrt(2) + 1
If you would like a decimal approximation of then use the "n" function. Or you could use its longer name "numerical_approx".
2.41421356237309
2.41421356237309
If you would like more digits then include the optional argument "digits" in the function call.
2.4142135623730950488
Sage also knows what is:
pi
Notice it represents it exactly. If you want an approximation then you must use the n function.
3.14159265358979
For you to try:
1) Evaluate to 10 digits. To 20 digits.
[back to top]
2) Getting Help:
If you ever need help on a topic just type
[topic name]?
For example
sin?
would give you help on the sine function.
[back to top]
3) Storing Items in Memory
3
In the first statement we stored the value 3 in the variable name a. In the next execution block we called the value of a back out of memory.
Let's do another.
8
Look at that, Sage pulled a and b out of memory and then added them together.
We can overwrite variable names too.
We can overwrite variable names too.
1/2
For you to try:
2) Store the value under the name and the value under the name . Then get Sage to compute , , , and .
[back to top]
4) Functions and Graphs:
Let us declare to be the function given by .
x |--> x^2
Perfect, we can just use good old function notation.
(Note the second f is there just so it will display the value of f. I've just done this so you can see it read in and stored the value of f properly. We don't need to do this from now on.)
Now what if we want to determine . Simple, this is done just as we would expect:
25
We can also input expression into the function.
(h + x)^2
h^2 + 2*h*x + x^2
Let us create a new function with the parameter , say . This is just a family of linear functions with the same y-intercept but different slopes (the slope is which is a parameter).
x |--> 1/2*x + 1
This may not be what we expected. What happened? Oh right, I already stored a value for the variable "a" above. It seems to have just used it here. Ok, so how can we clear the value from a variable and use the variable in a function? We do this as follows:
x |--> a*x + 1
Notice we can evaluate at different points as follows.
2
pi^2
Again, Sage returns the exact values, no approximations here unless you ask for them.
Another example:
0
1/2*sqrt(3)
1/2*sqrt(2)
1/2
Wonderful, it even knows the exact values of trigonometric functions at special angles.
Hmmm... what about the sum of two functions? Or the product? Or the composition?
sin(1) + 1
4*sin(2)
sin(x)^2
sin(x^2)
Yes! Sage can do these too. Notice the syntax for composition is exactly what we would expect: .
Some functions Sage knows:
function | Sage notation |
sqrt(x) | |
exp(x) | |
ln(x) or log(x) | |
(base n logarithm) | log(x,n) |
sin(x) | sin(x) |
cos(x) | cos(x) |
tangent | tan(x) |
It also knows arcsin, arccsos, acrtan, sinh, cosh, and many many more. Remember, if you ever need help on a topic just type: [topic name]?
For you to try:
3) Define and evaluate and .
How about plotting the functions and that we defined above? This is done using the "plot" command. Notice below that I have specified that the function only be plotted for x values from -1 to 1. You can input whatever domain you like. Remember, type "plot?" to get more information on plotting. There are a lot of options you can use to customize your plots.
Another couple of examples are:
We can plot two functions on the same coordinate system.
However, if you want to color the graphs differently, or use different domains for each function, then you can do this by creating the plots individually, then creating an new plot which is the join of the two plots (using the + operator), and then use the 'show' method to display the plots.
For you to try:
4) Plot the function on the interval . Then plot the function on the same set of axis but color it purple.
[back to top]
5) Differentiation:
After all this is Calculus, so we should show how to use Sage to do differentiation.
x |--> 4*x^3
We can now evaluate the derivative at a point, say .
81
108
Notice that the command to differentiate explicitly mentions the variable . This is the variable we used to define the function . If we differentiate with respect to another variable, say , then we would expect to get as a result (since f is constant with respect to ).
x |--> 0
Another example:
x |--> 2*cos(x)
0
higher derivatives:
To compute the derivative of a function use the command
diff(f,x,n)
x |--> 12*x^2
x |--> 24*x
Sage handles complicated derivatives easily. Recall that we would do this example by using logarithmic differentiation.
(log(x) + 1)*x^x
For you to try:
5) Pick some derivative questions from the textbook and have Sage compute them.
[back to top]
6) Limits:
Sage can certainly do limits, and quite well I might add.
The command to compute is given by:
limit(f,x=a)
Here are some examples:
4
Indeterminate Form Examples:
2
1
1/2
0
To compute the right-hand limit. use the argument dir='plus' as follows:
+Infinity
To compute the left-hand limit. use the argument dir='minus' as follows:
-Infinity
For you to try:
6) Pick some limit questions from the textbook and have Sage compute them. In particular choose some questions from the section on L'Hospital's Rule.
[back to top]
7) Solving Equations:
Solving Equations Exactly
[x == -2, x == -1]
Solve for one variable in terms of others.
[x == -1/2*b - 1/2*sqrt(b^2 - 4*c), x == -1/2*b + 1/2*sqrt(b^2 - 4*c)]
Solve for several variables.
[[x == 5, y == 1]]
Solving Equations Numerically
When solve can't find the exact solution, find_root can approximate the solution. But you need to specify a interval in which to look. In the next example we look in .
0.78539816339744839
[back to top]
8) Newton's Method:
Let us see how we can use out newfound knowledge to get Sage to solve a Newton's method problem.
Problem: Use Newton's Method with an initial guess to approximate the root of which lies in the interval to four decimal places.
The function we with to find the root of is . So lets put this into Sage and also input Newton's Iteration formula:
Now that Newton's Iterative formula is ready to go, we can start to compute the sequence of approximations to the root.
0.870073695796209
The third approximation is then:
0.871221414262919
and the fourth is
0.871221514495088
and so on.
We could also use a loop to save us from having to do a lot of typing.
Sage is implemented using Python so we use Python commands to construct loops. So two quick points about loops and list in Python you should know are: In the for loop below i runs over integer values in the range [0,5), i.e. over the number 0, 1, 2, 3, 4. The elements of a list in Python are indexed by 0, 1, 2, etc. (i.e. the first element has index 0, not 1).
[1, 0.870073695796209, 0.871221414262919, 0.871221514495088, 0.871221514495089, 0.871221514495089]
Since the fifth and sixth approximations agree to 14 decimal places then we have found the solution to the equation to 14 decimal places:
.
[back to top]
9) Integration
The command for taking the integral of a function is simply "integral".
1/5*x^5
We can add limits of integration to compute the definite integral over an interval.
242/5
Here is another example, one that requires the method of integration by parts if we were to do by hand.
x*sin(x) + cos(x)
As we have seen, some functions do not have antiderivatives expressible in terms of elementary functions. For example the antiderivative of is not able to be expressed using elementary functions: polynomials, exponentials, logarithms, trigonometric functions.
To evaluate definite integrals of such functions we use numerical techniques: Riemann sums, Trapezoid Rule, Simpson's Rule, etc.
Here is an example where we compute the integral of a function numerically. Notice the function "n" returns the numerical value of the integral.
integrate(sin(x)/x, x, 1, 1/2*pi)
(Notice in returns exactly what we entered. This indicates the antiderivative is not expressible in terms of elementary functions.)
0.42467909778730545
Alternatively, we could do this all in one step as follows.
0.42467909778730545
Here is an interactive applet in which you can input one, or two, functions and it will compute the definite integral and display a nice plot.
Interactive: Definite Integral:
This has been a very brief introduction to Sage to get you up and running (ok, maybe a slow jog). You can find out a lot more about Sage and Python by working through the interactive Sage tutorial by clicking Help, then click Tutorial. Have fun!