︠2e09ed86-8418-4b5c-a7e3-774666b5449ai︠
%html
Calculus in SAGE: A Quickstart Guide
Jamie Mulholland
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
Differential Calculus:
- Differentiation
- Limits
- Solving Equations
- Newtons Method
Integral Calculus:
- Integration
- Sequences
- Series
- Taylor Series
Let us start with the basics.
1) Syntax and Arthmetic
︡aaacf842-0de3-4673-9cec-111fa4706bae︡{"html": "
\r\n
\r\n\r\nCalculus in SAGE: A Quickstart Guide\r\n
\r\n
\r\n\r\nJamie Mulholland
Department of Mathematics
Simon Fraser University\r\n
\r\n\r\n
\r\n
\r\n\r\nSage 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.\r\n\r\n
\r\nTo 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.\r\n
\r\n\r\nI'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. \r\n\r\n
\r\nContents:
\r\n\r\n Basics:\r\n - Syntax and Arthmetic
\r\n - Getting Help
\r\n - Storing Items in Memory
\r\n - Functions and Graphs
\r\n Differential Calculus:\r\n - Differentiation
\r\n - Limits
\r\n - Solving Equations
\r\n - Newtons Method
\r\n Integral Calculus:\r\n - Integration
\r\n - Sequences
\r\n - Series
\r\n - Taylor Series
\r\n
\r\n\r\n
\r\n\r\nLet us start with the basics.\r\n\r\n
\r\n\r\n 1) Syntax and Arthmetic"}︡
︠fbcd31e7-9e26-45f5-8bc0-c8c150b9b9d1︠
2+5
︡270d1522-d546-44ef-984e-06b28f134611︡{"stdout": "7"}︡
︠58fc8deb-0e1b-4df8-8b7a-06149e6d9096i︠
%html
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:
︡7f6df7e9-8a4a-438f-a5ce-bc7b470dbcee︡{"html": "
\r\nThe 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 \r\nto Sage. Pressing \"shift-enter\" then gets Sage to execute the command and the output is returned in blue.\r\n
\r\n\r\nHere are some more examples:"}︡
︠2ef66b30-c0d5-4d24-820b-8ad5a346ae35︠
2/3+5
︡5887a961-7f39-496f-a88d-f12e2bb10830︡{"stdout": "17/3"}︡
︠8c617d41-29ec-48dc-917a-069715c32ebd︠
(4/7)*(5/3)
︡6b4f6326-bf81-4c82-afae-6c555ad389aa︡{"stdout": "20/21"}︡
︠ac90ae73-bd03-4fc4-9867-f4e83921558ci︠
%html
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:
︡26558ba2-737a-4a9d-99da-68253c5b789e︡{"html": "
\r\nNotice Sage returns the exact value (i.e a fraction) not just a decimal approximation, which is what \r\nmost calculators would return. So Sage works with exact expressions, not just approximations.\r\n\r\n
\r\nNote: \" * \" represents multiplication
\r\n \" / \" represents division
\r\n \" ^ \" represents exponentiation \r\n\r\n
\r\nSage works with exact expression as seen here:"}︡
︠fa4a4acf-fe1f-426a-a45c-bb445003ba34︠
sqrt(2)+1
︡87dc0f5c-c06a-4331-bd5d-62697d758a03︡{"stdout": "sqrt(2) + 1"}︡
︠d43fdc1d-cbc0-49cb-a8d5-133ee869654ci︠
%html
If you would like a decimal approximation of $\sqrt{2}+1$ then use the "n" function. Or you could use its longer name "numerical_approx".
︡e9daccc0-0db7-4e97-b62e-ac470bb56244︡{"html": "
\r\nIf you would like a decimal approximation of $\\sqrt{2}+1$ then use the \"n\" function. Or you could use its longer name \"numerical_approx\".\r\n
"}︡
︠91c4286d-fac9-4444-922f-d71d3bbc282e︠
n(sqrt(2)+1)
︡68c26f33-47de-4e12-9eaf-87afaf3b5a1e︡{"stdout": "2.41421356237309"}︡
︠b38991a8-f48f-4ce9-87ed-407c750d70d4︠
numerical_approx(sqrt(2)+1)
︡26fa0c29-e9df-4fc0-99ba-24f33308d148︡{"stdout": "2.41421356237309"}︡
︠d3ffaaa6-e07c-4bc2-a2f0-ac334ba33e26i︠
%html
If you would like more digits then include the optional argument "digits" in the function call.
︡54c04190-00d5-4bb7-b83e-162dd944cc19︡{"html": "
\r\nIf you would like more digits then include the optional argument \"digits\" in the function call.\r\n
"}︡
︠25c1f051-68ef-4303-a538-3af1653cf4d4︠
n(sqrt(2)+1,digits=20)
︡61ecf7d7-caa8-4460-97f6-5f1ad539a1e4︡{"stdout": "2.4142135623730950488"}︡
︠af241c38-4e8a-452f-b1db-e2daa3a785b0i︠
%html
Sage also knows what $\pi$ is:
︡bde9eaf1-6c31-4b28-be9f-7e9d15e10a9c︡{"html": "
\r\nSage also knows what $\\pi$ is:"}︡
︠2b147c7d-6be8-440c-a574-65019437f66d︠
pi
︡a3f770f4-2093-4f3c-bb51-068a5f8c852b︡{"stdout": "pi"}︡
︠07fadcfd-cdb6-4417-a1f9-3e8d35a53000i︠
%html
Notice it represents it exactly. If you want an approximation then you must use the n function.
︡8198094c-6d20-4617-858b-7bc7c19e9635︡{"html": "
\r\nNotice it represents it exactly. If you want an approximation then you must use the n function.\r\n
"}︡
︠46661078-10e9-4b61-999c-a334b869a704︠
n(pi)
︡9aa3e39d-042f-4099-b4fc-ca6424bce79c︡{"stdout": "3.14159265358979"}︡
︠025caa1e-d3ce-4baa-83c7-c814aef17765i︠
%html
For you to try:
1) Evaluate $3^2+\frac{\pi}{2}$ 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
︡0149b9c3-a103-4de8-b6da-e786eb82e7de︡{"html": "
\r\nFor you to try:
\r\n1) Evaluate $3^2+\\frac{\\pi}{2}$ to 10 digits. To 20 digits.\r\n
\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n 2) Getting Help:\r\n
\r\nIf you ever need help on a topic just type
\r\n[topic name]?
\r\nFor example
\r\nsin?
\r\nwould give you help on the sine function.\r\n\r\n
\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n\r\n 3) Storing Items in Memory"}︡
︠62be2791-603e-49ec-807a-4fa1d67e9651︠
a=3
︡4ecf98a9-34a1-43f9-862a-d1d225c399b9︡︡
︠a0112b4c-75f2-4dad-abb7-871cfaebc534︠
a
︡8e0a4a88-9273-467b-bebd-44efcd094e02︡{"stdout": "3"}︡
︠f05caccc-3c1d-4ceb-a65c-34bb7fa32099i︠
%html
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.
︡a8c7f434-9166-4213-8883-93c6ded0ea19︡{"html": "
\r\nIn the first statement we stored the value 3 in the variable name a. In the next execution block we \r\ncalled the value of a back out of memory. \r\n
\r\n\r\nLet's do another."}︡
︠b1ff641e-394b-4149-a0bb-1584505f2a9a︠
b=5
︡951607c9-35c6-440e-b196-0d1e897f3105︡︡
︠04b427b6-4f1c-4ef3-900d-113515f8658f︠
a+b
︡f126d50b-daf8-4d01-918c-57fab7d6753d︡{"stdout": "8"}︡
︠d2c3f2b2-ce2a-438c-ad7b-16f68bff80c2i︠
%html
Look at that, Sage pulled a and b out of memory and then added them together.
We can overwrite variable names too.
︡733c1fe0-5e13-417c-b229-2357547df8ed︡{"html": "Look at that, Sage pulled a and b out of memory and then added them together.
\r\n\r\nWe can overwrite variable names too."}︡
︠144b41ea-450c-42dd-b380-1be36d8a389b︠
a=1/2
a
︡b48ba932-75e5-4ed8-8a77-64c87da8df78︡{"stdout": "1/2"}︡
︠da1f9283-e3d6-49ec-b642-e300245e894ai︠
%html
For you to try:
2) Store the value $\frac{3}{4}$ under the name $a$ and the value $-\frac{9}{2}$ under the name $b$. Then get Sage to compute $a+b$, $a-b$, $ab$, and $\frac{a}{b}$.
[back to top]
4) Functions and Graphs:
Let us declare $f$ to be the function given by $f(x)=x^2$.
︡0728bfbd-296e-4e6f-977d-237764ae0b05︡{"html": "
\r\nFor you to try:
\r\n2) Store the value $\\frac{3}{4}$ under the name $a$ and the value $-\\frac{9}{2}$ under the name $b$. Then get Sage to compute $a+b$, $a-b$, $ab$, and $\\frac{a}{b}$.\r\n
\r\n\r\n
\r\n\r\n\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n\r\n 4) Functions and Graphs:\r\n
\r\n\r\nLet us declare $f$ to be the function given by $f(x)=x^2$."}︡
︠82661f06-6eea-40ea-be2c-8e2e5c57112c︠
f(x)=x^2
f
︡d80aee3a-291f-4709-9422-3823f444974a︡{"stdout": "x |--> x^2"}︡
︠b2e1f109-983f-4dc9-b0f0-29619e724b7fi︠
%html
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 $f(5)$. Simple, this is done just as we would expect:
︡b88da9dd-385f-4ee4-99cf-dd8095232bec︡{"html": "
\r\nPerfect, we can just use good old function notation.\r\n
\r\n(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.)\r\n\r\n
\r\n\r\nNow what if we want to determine $f(5)$. Simple, this is done just as we would expect:\r\n
"}︡
︠e5e2c0cc-e369-45d0-b8b4-d97d75657d3e︠
f(5)
︡3ed1964b-6883-4b3f-8361-778a1364f2cb︡{"stdout": "25"}︡
︠252cbf51-1d7c-4a49-aaa4-d2c651f208cdi︠
%html
We can also input expression into the function.
︡68e3c71a-2ec0-447b-92b3-28a96368bc87︡{"html": "We can also input expression into the function."}︡
︠0d088230-f576-415f-b767-69813afd7147︠
f(x+h);
expand(f(x+h)); #(this is a comment) here we ask Sage to expand the expression for f(x+h)
︡2f2611f7-9d6a-4f5a-a140-6d61f453a911︡{"stdout": "(h + x)^2\nh^2 + 2*h*x + x^2"}︡
︠137f6b2c-9e37-43df-acbe-2cff867b364bi︠
%html
Let us create a new function with the parameter $a$, say $g(x)=a*x+1$. This is just a family of linear functions with the same y-intercept but different slopes (the slope is $a$ which is a parameter).
︡14054bb2-95f8-4cbc-a3d3-4f11733502c6︡{"html": "
Let us create a new function with the parameter $a$, say $g(x)=a*x+1$. This is just a family of linear functions with the same y-intercept but different slopes (the slope is $a$ which is a parameter).\r\n
"}︡
︠bd6274e8-48eb-4c7a-ac7a-0ec5ae7ac6f2︠
g(x)=a*x+1;
g
︡624317b2-342e-4c4b-a932-c11e19b4b96d︡{"stdout": "x |--> 1/2*x + 1"}︡
︠85059d76-adfc-46e4-a5b0-bcfa91406b82i︠
%html
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:
︡a611a40b-0f13-4cd1-a030-e6d09396f87a︡{"html": "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:"}︡
︠ab206d26-9101-460a-b9a1-1b89b53969d1︠
a=var('a') # here we declared a to be a variable and hence cleared its value.
g(x)=a*x+1
g
︡58fb35c8-09bc-41f1-83d8-51a45b24abda︡{"stdout": "x |--> a*x + 1"}︡
︠c4888eb4-101a-4073-800c-250068f35e5fi︠
%html
Notice we can evaluate $f$ at different points as follows.
︡7d5a8100-56fc-49bf-8e27-2df85312ba80︡{"html": "
\r\nNotice we can evaluate $f$ at different points as follows."}︡
︠bdd7fe1d-915a-4e65-923b-9341b4f7696a︠
f(sqrt(2))
︡460adc96-26a5-4b7c-a714-1361557c5b43︡{"stdout": "2"}︡
︠df571ead-5a1b-4c68-9b0f-cea43d14a9b6︠
f(pi)
︡7f9b279e-a2ee-463c-bb8a-3e56c21c3911︡{"stdout": "pi^2"}︡
︠00afab93-c272-436d-977b-687df8e94ac2i︠
%html
Again, Sage returns the exact values, no approximations here unless you ask for them.
Another example:
︡1d6ad797-3e88-4182-bc0b-d0c68ae9c43d︡{"html": "
\r\nAgain, Sage returns the exact values, no approximations here unless you ask for them.\r\n\r\n
\r\nAnother example:"}︡
︠83e55d5d-fedb-4b37-9b08-6e46bdfe7af6︠
g(x)=sin(x)
︡2fa38d97-89bb-4b9c-9dac-069143148d3a︡︡
︠4f7479e3-5951-46a1-a76c-6b439e7b1d15︠
g(0); g(pi/3); g(pi/4); g(pi/6);
︡314901cc-677c-4774-bca3-777ad0d3ee98︡{"stdout": "0\n1/2*sqrt(3)\n1/2*sqrt(2)\n1/2"}︡
︠ec1aa826-c2a1-46c2-b27b-a5bc702c1378i︠
%html
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?
︡f9b2d5e3-fc7f-4ea7-b3ad-0d8e146d4cae︡{"html": "
\r\nWonderful, it even knows the exact values of trigonometric functions at special angles.\r\n
\r\n\r\nHmmm... what about the sum of two functions? Or the product? Or the composition?"}︡
︠26a13e39-8788-4403-ae2e-bb04cc4fe3e6︠
(f+g)(1); (f*g)(2); f(g(x)); g(f(x));
︡6fccf48f-5bf5-4f6c-b5be-ac13374d5d6b︡{"stdout": "sin(1) + 1\n4*sin(2)\nsin(x)^2\nsin(x^2)"}︡
︠c29fc206-e0df-4e1c-812f-e286c3731d85i︠
%html
Yes! Sage can do these too. Notice the syntax for composition is exactly what we would expect: $(f\circ g)(x) = f(g(x))$.
Some functions Sage knows:
function |
Sage notation |
$\sqrt{x}$ |
sqrt(x) |
$e^x$ |
exp(x) |
$\ln{x}$ |
ln(x) or log(x) |
$\log_n(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 $h(x)=\cos{x}+e^{x^2}+x+3$ and evaluate $h(0)$ and $h(-\frac{\pi}{3})$.
How about plotting the functions $f$ and $g$ 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.
︡97b88a84-dc7c-4f6f-a7f1-899c653715d4︡{"html": "
\r\nYes! Sage can do these too. Notice the syntax for composition is exactly what we would expect: $(f\\circ g)(x) = f(g(x))$.\r\n
\r\nSome functions Sage knows:\r\n
\r\n\r\n \r\n function | \r\n Sage notation | \r\n
\r\n \r\n $\\sqrt{x}$ | \r\n sqrt(x) | \r\n
\r\n \r\n $e^x$ | \r\n exp(x) | \r\n
\r\n \r\n $\\ln{x}$ | \r\n ln(x) or log(x) | \r\n
\r\n \r\n $\\log_n(x)$ (base n logarithm) | \r\n log(x,n) | \r\n
\r\n \r\n sin(x) | \r\n sin(x) | \r\n
\r\n \r\n cos(x) | \r\n cos(x) | \r\n
\r\n \r\n tangent | \r\n tan(x) | \r\n
\r\n
\r\n\r\n
\r\nIt also knows arcsin, arccsos, acrtan, sinh, cosh, and many many more. \r\nRemember, if you ever need help on a topic just type: \r\n[topic name]?\r\n\r\n
\r\n
\r\nFor you to try:
\r\n3) Define $h(x)=\\cos{x}+e^{x^2}+x+3$ and evaluate $h(0)$ and $h(-\\frac{\\pi}{3})$.\r\n
\r\n\r\nHow about plotting the functions $f$ and $g$ that we defined above? \r\nThis is done using the \"plot\" command. \r\nNotice below that I have specified that the function only be plotted for x values from -1 to 1. You can \r\ninput whatever domain you like. \r\nRemember, type \"plot?\" to get more information on plotting. There are a lot of options you can use to \r\ncustomize your plots."}︡
︠225fdd87-2549-43da-aac3-2ecd39f10aab︠
plot(f,(-1,1))
︡8bc83fbe-3040-4b22-af3e-d5ee695e3b8d︡{"html": "
"}︡
︠10a235ff-9a9a-4e74-bb4b-92ffe0fe0ba4i︠
%html
Another couple of examples are:
︡ce6f2484-e3a2-4e5e-b2a3-3cf3868bf257︡{"html": "
\r\nAnother couple of examples are:\r\n
"}︡
︠76d46969-492a-4f60-b5e3-9deff22f50cc︠
plot(g,(-pi,2*pi),rgbcolor='red')
︡458e4ed1-5511-47a7-9b37-1be7f343d99a︡{"html": "
"}︡
︠d2e0454f-3a31-4a03-a345-f008cbb6e91e︠
plot(x^3+x+1,(-2,2),rgbcolor='#ff00ff',)
︡eb316133-dbed-4cf1-9688-d113fac7787f︡{"html": "
"}︡
︠dca2b15a-c0fb-41e5-ae3c-8ba20ce33c18i︠
%html
We can plot two functions on the same coordinate system.
︡1dbe5047-31bd-458f-86a8-f7691dde3a3a︡{"html": "
\r\nWe can plot two functions on the same coordinate system.\r\n
"}︡
︠6ec00606-d32c-4bbb-954f-2edc7756e44a︠
plot([x^2,0.5*x+1],(-2,2))
︡09b4cb16-7389-4c32-8a5f-3aa5e325cf0e︡{"html": "
"}︡
︠1130fd8c-46ba-46ae-a75e-8442688491ebi︠
%html
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.
︡23c6f07f-fc37-4e7e-9089-25b250454531︡{"html": "
\r\nHowever, 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.\r\n
"}︡
︠4c8f2f97-fe4f-4df1-b594-9c826bf2081c︠
p1=plot(x^2,(-2,2));
p2=plot(0.5*x+1,(-2,4),rgbcolor='green');
p=p1+p2;
p.show()
︡66ddf2d1-d130-484e-b2fc-fdd7568306db︡{"html": "
"}︡
︠29f74e3d-b172-4a53-a901-f4e8690d90eci︠
%html
For you to try:
4) Plot the function $h(x)=\cos{x}+e^{x^2}+x+3$ on the interval $[-1,1]$. Then plot the function $f(x)=x^2$ 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.
︡bb7cdac8-cf3b-44d6-8181-3a610bdcd3bc︡{"html": "
\r\n
\r\nFor you to try:
\r\n4) Plot the function $h(x)=\\cos{x}+e^{x^2}+x+3$ on the interval $[-1,1]$. Then plot the function $f(x)=x^2$ on the same set of axis but color it purple.\r\n
\r\n\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n\r\n
\r\n 5) Differentiation:\r\n
\r\n\r\nAfter all this is Calculus, so we should show how to use Sage to do differentiation.\r\n\r\n
"}︡
︠24e1aa70-4643-4e3f-a248-0647bd2fa146︠
f(x)=x^4;
df=diff(f,x); # here we take the derivative of f with respect to x and call it df
df
︡c2d38a30-7f2f-4f8a-a60c-9780330ff93c︡{"stdout": "x |--> 4*x^3"}︡
︠501152f1-8fe5-4c2c-9747-69e0baf9f7b3i︠
%html
We can now evaluate the derivative at a point, say $x=3$.
︡0f45b3d1-f82a-4a63-807b-014a5d0fed61︡{"html": "
\r\nWe can now evaluate the derivative at a point, say $x=3$.\r\n
"}︡
︠7b8277e3-1f1b-4afd-a824-164b8f3781b7︠
f(3); df(3);
︡fe3f0e08-96ac-4713-a791-2da2d7158c49︡{"stdout": "81\n108"}︡
︠c68cda34-e4fd-447f-9852-e3137747da05i︠
%html
Notice that the command to differentiate explicitly mentions the variable $x$. This is the variable we used to define the function $f$. If we differentiate with respect to another variable, say $u$, then we would expect to get $0$ as a result (since f is constant with respect to $u$).
︡4792d521-5d81-4d7b-8271-34675b3651d7︡{"html": "
\r\nNotice that the command to differentiate explicitly mentions the variable $x$. This is the variable we used to define the function $f$. If we differentiate with respect to another variable, say $u$, then we would expect to get $0$ as a result (since f is constant with respect to $u$).\r\n
"}︡
︠477cf6d4-b66e-4d18-9239-f5bd41e92d3c︠
diff(f,u)
︡6d773bbf-91a7-4a19-9759-77e680826291︡{"stdout": "x |--> 0"}︡
︠95abf701-8ffa-4783-b856-fe9050a6899fi︠
%html
Another example:
︡5f076ecc-bbda-415b-a56d-8e024326965f︡{"html": "
\r\nAnother example:\r\n
"}︡
︠2a5dac8b-c7b6-4e9d-a424-265b2e8fe792︠
h(x)=2*sin(x);
dh=diff(h,x); # take derivative and store it under the name dh
dh; dh(pi/2); # call to print dh and the value of dh at pi/2
︡b6f4900d-7c89-437e-99a8-035bc6017035︡{"stdout": "x |--> 2*cos(x)\n0"}︡
︠2fdd29f7-11e4-4f88-99f7-ab8a122deb46i︠
%html
higher derivatives:
To compute the $n^{th}$ derivative of a function $f(x)$ use the command
diff(f,x,n)
︡c0189844-7b57-4070-9169-cadd2e0a3a78︡{"html": "
\r\n higher derivatives:\r\n
\r\n\r\nTo compute the $n^{th}$ derivative of a function $f(x)$ use the command
\r\ndiff(f,x,n)\r\n
"}︡
︠20d66123-19ab-4a3f-b83a-ddee008049d1︠
diff(f,x,2);
︡9a63a526-8301-493f-9add-ac8bead75e9d︡{"stdout": "x |--> 12*x^2"}︡
︠acb05872-846a-47b6-8a44-df9bb72735a5︠
diff(f,x,3);
︡abddb697-49a6-442d-a882-20bdb2931670︡{"stdout": "x |--> 24*x"}︡
︠f9e1a577-b889-44e6-9fe6-e8d0e75b042ci︠
%html
Sage handles complicated derivatives easily. Recall that we would do this example by using logarithmic differentiation.
︡7416a828-4f6d-458f-9ecd-68e47172f7ff︡{"html": "
\r\nSage handles complicated derivatives easily. Recall that we would do this example by using logarithmic differentiation."}︡
︠eae8b431-c3fb-4644-a05d-befea251ffb4︠
diff(x^x,x)
︡c02cd1d4-f29a-42fc-a62d-6d018f10581b︡{"stdout": "(log(x) + 1)*x^x"}︡
︠ddf84ec0-0fbd-482c-a9eb-52f597653dedi︠
%html
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 $\lim_{x \to a} f(x)$ is given by:
limit(f,x=a)
Here are some examples:
︡94f821d5-9c5e-4c56-9b8c-dba83cb4192f︡{"html": "
\r\nFor you to try:
\r\n5) Pick some derivative questions from the textbook and have Sage compute them.\r\n
\r\n\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n\r\n
\r\n 6) Limits:\r\n
\r\n\r\nSage can certainly do limits, and quite well I might add.\r\n
\r\nThe command to compute $\\lim_{x \\to a} f(x)$ is given by:
\r\nlimit(f,x=a)\r\n
\r\nHere are some examples:"}︡
︠f4ba991a-dfae-45a0-861b-213d8e7ece19︠
limit(x^2,x=2)
︡ac3d7f47-f837-4a14-9ada-0788caef40fc︡{"stdout": "4"}︡
︠b1435d42-5d82-4e5e-aec0-5f4c30e65035i︠
%html
Indeterminate Form Examples:
︡ea4f6d53-e832-47fa-9889-69c320e11729︡{"html": "Indeterminate Form Examples:"}︡
︠5db0e990-8ef1-421b-811e-1c81bb2bf8b8︠
limit((x^2-1)/(x-1),x=1)
︡e4c6923e-d11b-4e41-a7a2-40e383117f1c︡{"stdout": "2"}︡
︠4b4f4eed-ac68-49f2-acce-691f936cc18a︠
limit(sin(x)/x,x=0)
︡cfa643e8-cdbe-44f3-bbfa-6e6df93f2d41︡{"stdout": "1"}︡
︠83b465f4-d6ef-4e19-9919-89c4a2da255a︠
limit((x^3+3)/(2*x^3+2*x-1),x=infinity)
︡c02da776-2de5-4e3b-8124-e43865d2dfec︡{"stdout": "1/2"}︡
︠bd135e83-48be-4d6f-afea-862df6e7da33︠
limit(x^2/exp(x),x=infinity);
︡b91b3fc7-bc6a-4394-a76d-544e192ef634︡{"stdout": "0"}︡
︠927aeefe-1ace-4ead-b08b-d6eadb1e6bf0i︠
%html
To compute the right-hand limit. $lim_{x \to a^+} f(x)$ use the argument dir='plus' as follows:
︡5b313222-0716-4c78-a0f3-de3c69c77214︡{"html": "
\r\nTo compute the right-hand limit. $lim_{x \\to a^+} f(x)$ use the argument dir='plus' as follows:\r\n
"}︡
︠644f03d8-df86-447b-9832-86206a4778ba︠
limit((x+1)/(x-7),x = 7, dir='plus')
︡90425f83-0dc7-44dd-9d18-66236be57568︡{"stdout": "+Infinity"}︡
︠aa9ef76a-92a5-42f5-9bcc-163e44643a97i︠
%html
To compute the left-hand limit. $lim_{x \to a^-} f(x)$ use the argument dir='minus' as follows:
︡7983510d-9aaa-4330-9129-252266f6e3a4︡{"html": "
\r\nTo compute the left-hand limit. $lim_{x \\to a^-} f(x)$ use the argument dir='minus' as follows:\r\n
"}︡
︠781c85e9-1400-40ac-a45c-8ec9c1821d14︠
limit(cos(x)/(sin(x)-1),x=pi/2,dir='minus')
︡6e39a23c-01a0-4a8d-ae0b-5b2fc779be8b︡{"stdout": "-Infinity"}︡
︠f61a54b0-b2b4-4906-a1a8-e087dbbbf67bi︠
%html
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
︡2230291a-e41c-4af1-8686-6fcfe58428ae︡{"html": "
\r\nFor you to try:
\r\n6) 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.\r\n
\r\n\r\n\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n
\r\n 7) Solving Equations:\r\n
\r\n\r\nSolving Equations Exactly"}︡
︠916e4fbf-59a9-4f06-9b19-7d41ce16dd3a︠
x=var('x')
solve(x^2+3*x+2==0,x)
︡f640fadc-a4ac-4170-a429-f6fd813bcb02︡{"stdout": "[x == -2, x == -1]"}︡
︠d9364a2b-362c-4080-862a-50fb06c4589ci︠
%html
Solve for one variable in terms of others.
︡9a026be4-c6cc-4ed6-8246-1ba25407fccf︡{"html": "
\r\nSolve for one variable in terms of others.\r\n
"}︡
︠7651bca4-4f8b-4ca1-953f-7dea51044d50︠
x, b, c, = var('x, b, c')
solve(x^2+b*x+c==0,x)
︡b1c196c0-4c0c-488f-9e04-5af05d6aae0b︡{"stdout": "[x == -1/2*b - 1/2*sqrt(b^2 - 4*c), x == -1/2*b + 1/2*sqrt(b^2 - 4*c)]"}︡
︠36dd3947-8360-4e9e-896c-3fcf59180401i︠
%html
Solve for several variables.
︡4bdcabc7-53c8-49e0-9c7e-135700bda988︡{"html": "
\r\nSolve for several variables.\r\n
"}︡
︠c840a1c9-b740-445b-9a3e-9599b718a7d7︠
x, y = var('x, y')
solve([x+y==6,x-y==4],x,y)
︡bdd90483-602b-4bc4-9789-227a2d81ac30︡{"stdout": "[[x == 5, y == 1]]"}︡
︠673f2839-7fcd-489d-9690-c2ad2aa17fbci︠
%html
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< \theta < \frac{\pi}{2}$.
︡7b859172-ee5a-497a-9b26-27377bf63628︡{"html": "
\r\nSolving Equations Numerically\r\n
\r\nWhen 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< \\theta < \\frac{\\pi}{2}$.\r\n
"}︡
︠688e063b-03c8-4be4-bb68-eff0f6227c9e︠
theta=var('theta')
find_root(cos(theta)==sin(theta),0,pi/2)
︡e13bbe9c-8a91-4720-b776-3b92afe9b441︡{"stdout": "0.78539816339744839"}︡
︠80319842-5e23-4f4f-968b-19b2c52db9a0i︠
%html
[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 $x_1=1$ to approximate the root of $5x+\cos{x}=5$ which lies in the interval $[0,\frac{\pi}{2}]$ to four decimal places.
The function we with to find the root of is $f(x)=\cos{x}+5x-5$. So lets put this into Sage and also input Newton's Iteration formula:
︡d957345f-167d-4042-b64e-269cf21dcf23︡{"html": "
\r\n[back to top]\r\n
\r\n\r\n\r\n
\r\n 8) Newton's Method:\r\n
\r\n\r\nLet us see how we can use out newfound knowledge to get Sage to solve a Newton's method problem.\r\n
\r\n\r\nProblem: Use Newton's Method with an initial guess $x_1=1$ to approximate the root of $5x+\\cos{x}=5$ which lies in the interval $[0,\\frac{\\pi}{2}]$ to four decimal places.\r\n
\r\nThe function we with to find the root of is $f(x)=\\cos{x}+5x-5$. So lets put this into Sage and also input Newton's Iteration formula:\r\n
"}︡
︠19420f32-f43b-447d-8314-9df3dd48313e︠
x, xn = var('x, xn');
f(x)=cos(x)+5*x-5;
df=diff(f,x);
NewtonIt(x)=x-(f/df)(x); #Newton's Iterative Formula
︡c77317a3-a8fe-4c38-8ef0-c52b483b1d4d︡︡
︠6f47ff32-4292-4451-b673-61b7389eca9fi︠
%html
Now that Newton's Iterative formula is ready to go, we can start to compute the sequence of approximations to the root.
︡fc3e0f51-cbd3-4053-be54-2f246d0ecb0f︡{"html": "
\r\nNow that Newton's Iterative formula is ready to go, we can start to compute the sequence of approximations to the root.\r\n
"}︡
︠1c65b899-398e-433c-a2e8-eb53b8510bab︠
x1=1;
x2=N(NewtonIt(x1)); x2
︡262c9f32-9a07-468d-949a-2f96224551f2︡{"stdout": "0.870073695796209"}︡
︠12f54bb9-52a3-46dc-96e2-96ccc46f1315i︠
%html
The third approximation is then:
︡2e3158af-36c8-4cd0-9478-1f9f94e02b2c︡{"html": "
\r\nThe third approximation is then:\r\n
"}︡
︠c9a0655e-85b2-409a-aee4-b41e6cc3daf9︠
x3=N(NewtonIt(x2)); x3
︡b59f8f3a-fac2-4431-8982-684e7529e13d︡{"stdout": "0.871221414262919"}︡
︠87f3c44c-ef57-416b-b881-8c736f63bc68i︠
%html
and the fourth is
︡88e42f2b-6354-45bb-bf03-2f7d0faf8804︡{"html": "
\r\nand the fourth is\r\n
"}︡
︠12a4346a-a83d-4aae-b8ef-89d598a11c07︠
x4=N(NewtonIt(x3)); x4
︡24f47a2e-b3a4-49a4-ae4f-31c1eea109ef︡{"stdout": "0.871221514495088"}︡
︠3f0bee63-d594-4284-8906-1f15fe8aae0ci︠
%html
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).
︡0090564b-5974-45b8-9c06-a6a0adf18014︡{"html": "
\r\nand so on.\r\n
\r\n\r\nWe could also use a loop to save us from having to do a lot of typing.
\r\nSage 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).\r\n
"}︡
︠825822fa-7c18-46fb-b650-88996c373996︠
xnlist=[1]
for i in range(0,5):
xnlist.append(N(NewtonIt(xnlist[i])))
xnlist
︡2626840f-a0ca-4646-858e-3c0c5e3c9773︡{"stdout": "[1, 0.870073695796209, 0.871221414262919, 0.871221514495088, 0.871221514495089, 0.871221514495089]"}︡
︠c39ec171-7215-4d1a-a343-cbb793e4244ci︠
%html
Since the fifth and sixth approximations agree to 14 decimal places then we have found the solution to the equation to 14 decimal places:
$0.87122151449508$.
[back to top]
9) Integration
The command for taking the integral of a function is simply "integral".
︡a94949a6-a570-42d0-bae2-7de8d42abb51︡{"html": "
\r\nSince the fifth and sixth approximations agree to 14 decimal places then we have found the solution to the equation to 14 decimal places:
\r\n$0.87122151449508$.\r\n\r\n
\r\n[back to top]\r\n
\r\n\r\n 9) Integration\r\n\r\n
\r\nThe command for taking the integral of a function is simply \"integral\".
"}︡
︠8206e931-7461-4c7a-8c0d-94fb6baa9d0c︠
f(x)=x^4;
integral(f(x),x); # here we take the indefinite integral (antidervivative) of f with respect to x
︡990de2c2-5605-4c19-a7aa-a95c6bf96813︡{"stdout": "1/5*x^5"}︡
︠be2adf4f-e569-4830-b6b1-5c3b86fd6bfei︠
%html
We can add limits of integration to compute the definite integral over an interval.
︡8c3ad090-f336-4cb4-813d-973ef74cc02c︡{"html": "
\r\nWe can add limits of integration to compute the definite integral over an interval.\r\n
"}︡
︠757368dd-9b8d-4c84-b0e3-5051df88e7b7︠
integral(f(x),x,1,3)
︡4eb74fdd-e915-4f51-aa76-28e255e8b375︡{"stdout": "242/5"}︡
︠cb9e72ff-5275-410d-8d0a-648d537876c2i︠
%html
Here is another example, one that requires the method of integration by parts if we were to do by hand.
︡2cc9cfbf-173d-4623-b5bd-a3109a90a704︡{"html": "
\r\nHere is another example, one that requires the method of integration by parts if we were to do by hand.\r\n
"}︡
︠01f99a47-4a6f-4f1f-ad06-f7770fa3617a︠
integral(x*cos(x),x)
︡c21d50d1-fb72-4c4c-a461-f57c52a02e28︡{"stdout": "x*sin(x) + cos(x)"}︡
︠a05c5879-9e7e-47de-abb8-fff2867b547di︠
%html
As we have seen, some functions do not have antiderivatives expressible in terms of elementary functions. For example the antiderivative of $e^{x^2}$ 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.
︡898784af-3916-4078-80f2-b19457e37f0e︡{"html": "
\r\nAs we have seen, some functions do not have antiderivatives expressible in terms of elementary functions. For example the antiderivative of $e^{x^2}$ is not able to be expressed using elementary functions: polynomials, exponentials, logarithms, trigonometric functions.
\r\nTo evaluate definite integrals of such functions we use numerical techniques: Riemann sums, Trapezoid Rule, Simpson's Rule, etc. \r\n
\r\nHere is an example where we compute the integral of a function numerically. Notice the function \"n\" returns the numerical value of the integral.\r\n
"}︡
︠af5d48a7-94e4-42f7-832a-d6077a5bbd52︠
h = integral(sin(x)/x, x, 1, pi/2); h
︡7747bdf1-7e8f-4a7c-8a75-8df0ce15cff5︡{"stdout": "integrate(sin(x)/x, x, 1, 1/2*pi)"}︡
︠c81fa2ba-3de2-49cb-99a5-ffeb34f2f99fi︠
%html
(Notice in returns exactly what we entered. This indicates the antiderivative is not expressible in terms of elementary functions.)
︡5ba235bf-5efb-41d9-9721-820cf1322787︡{"html": "(Notice in returns exactly what we entered. This indicates the antiderivative is not expressible in terms of elementary functions.)"}︡
︠9ef3325a-c1ee-4092-aab2-e5ea4d984dd9︠
n(h)
︡5d631271-6aa3-4343-ba75-3b04db8e8c0c︡{"stdout": "0.42467909778730545"}︡
︠5b4923df-1528-4f41-9617-978219c6ed31i︠
%html
Alternatively, we could do this all in one step as follows.
︡c9bd8aa4-fafd-4cab-beee-97bbc462039d︡{"html": "Alternatively, we could do this all in one step as follows."}︡
︠a718fe03-98bf-4e26-84ec-b3483a276f2c︠
n(integral(sin(x)/x, x, 1, pi/2))
︡9ba2d97d-6d7d-4ea4-8930-9ae8f24d36e6︡{"stdout": "0.42467909778730545"}︡
︠bd48de97-f98d-4196-9ea8-b78340c23d6bi︠
%html
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:
︡d918a9a9-a79b-491b-bfe5-1cdfaaede5b5︡{"html": "
\r\nHere 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. \r\n
\r\n\r\n
Interactive: Definite Integral:
"}︡
︠cc9a6929-b11c-400b-85fa-15a83f0aa4ebi︠
%hide
# Definite Integral
# Lauri Ruotsalainen, 2010
@interact
def definite_integral(
f = input_box(default = 3*x),
g = input_box(default = x^2),
interval = input_box(default=(0,3), label="Interval"),
x_range = input_box(default=(0,3), label = "Plotting range (x)"),
selection = selector(["f", "g", "f and g", "f - g"], label="Select")
):
f(x) = f; g(x) = g
f_plot = Graphics(); g_plot = Graphics(); h_plot = Graphics();
text = ""
# Plot function f.
if selection != "g":
f_plot = plot(f(x), x, x_range, color="blue", thickness=1.5)
# Color and calculate the area between f and the horizontal axis.
if selection == "f" or selection == "f and g":
f_plot += plot(f(x), x, interval, color="blue", fill=True, fillcolor="blue", fillalpha=0.15)
text += "$\int_%s^%s(\color{Blue}{f(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(f(x)), #produces latex code for expression f
f(x).nintegrate(x, interval[0], interval[1])[0]
)
# Plot function g. Also color and calculate the area between g and the horizontal axis.
if selection == "g" or selection == "f and g":
g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
g_plot += plot(g(x), x, interval, color="green", fill=True, fillcolor="yellow", fillalpha=0.5)
text += "\n$\int_%s^%s(\color{Green}{g(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(g(x)), #produces latex code for expression g
g(x).nintegrate(x, interval[0], interval[1])[0]
)
# Plot function f-g. Also color and calculate the area between f-g and the horizontal axis.
if selection == "f - g":
g_plot = plot(g(x), x, x_range, color="green", thickness=1.5)
g_plot += plot(g(x), x, interval, color="green", fill=f(x), fillcolor="red", fillalpha=0.15)
h_plot = plot(f(x)-g(x), x, interval, color="red", thickness=1.5, fill=True, fillcolor="red", fillalpha=0.15)
text = "$\int_%s^%s(\color{Red}{f(x)-g(x)})\,dx=\int_%s^%s(%s)\,dx=%s$" % (
interval[0], interval[1],
interval[0], interval[1],
latex(f(x)-g(x)), #produces latex code for expression f-g
(f(x)-g(x)).nintegrate(x, interval[0], interval[1])[0]
)
show(f_plot + g_plot + h_plot, gridlines=True)
html(text)
︡98fcd86d-04df-4e76-9915-b6f9f5c86655︡︡
︠ca597e21-1a66-4ada-9ab9-1f9dd137cc88i︠
%html
[back to top]
10) Sequences
...coming soon...
︡1022701e-eff5-441b-8148-e66c6eb708e1︡{"html": "
\r\n[back to top]\r\n
\r\n\r\n 10) Sequences\r\n\r\n
\r\n...coming soon..."}︡
︠ab8807e5-9b9c-416c-8eee-d82b00f06b2fi︠
%html
[back to top]
11) Series
...coming soon...
︡1ed26623-42bb-4ced-9604-8edbf3b5b82b︡{"html": "
\r\n[back to top]\r\n
\r\n\r\n 11) Series\r\n\r\n
\r\n...coming soon..."}︡
︠89c264b9-d9f3-4f01-bb51-fba0f9637c1ci︠
%html
[back to top]
12) Taylor Series
...coming soon...
︡a0115bae-690d-44b8-bcd8-ab9fa543f16d︡{"html": "
\r\n[back to top]\r\n
\r\n\r\n 12) Taylor Series\r\n\r\n
\r\n...coming soon..."}︡
︠d14d0efb-409e-49ef-a67a-fa3046254b46i︠
%html
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!
︡664e9045-a05a-438f-a7f5-4e149644fafb︡{"html": "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!"}︡