︠ab87d20e-31d3-4b26-9225-5bddcf971c6ei︠ %html
The point to this worksheet is to introduce you to some of the basic commands in Sage. The simplest way I know of to learn how to use things like Sage is to copy examples from someone else, and then change them to fit what you need.
Throughout this worksheet I have include blank "Sage cells" in which you can type examples and try new things. To evaluate a cell, just click on the cell and then click "evaluate."
Let's try a simple example. In the empty cell below, type in some complicated expression of numbers (no variables right now) involving addition, subtraction, multiplication, and division. Use $*$ for multiplication and $/$ for division. When you are done, click "evaluate." If you get an error, you must have typed something in incorrectly; just try again.
︡1670b2a0-fc55-4874-871e-e4722805d0a7︡{"html": "The point to this worksheet is to introduce you to some of the basic commands in Sage. The simplest way I know of to learn how to use things like Sage is to copy examples from someone else, and then change them to fit what you need.
\nThroughout this worksheet I have include blank \"Sage cells\" in which you can type examples and try new things. To evaluate a cell, just click on the cell and then click \"evaluate.\"
\nLet's try a simple example. In the empty cell below, type in some complicated expression of numbers (no variables right now) involving addition, subtraction, multiplication, and division. Use $*$ for multiplication and $/$ for division. When you are done, click \"evaluate.\" If you get an error, you must have typed something in incorrectly; just try again.
"}︡ ︠390e610c-3f65-4c1f-9abc-6cf1be3a737bi︠ %htmlThe first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. (Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) Without doing this, Sage will not let me use $x$ and $y$ without giving an error. The %auto command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.
︡c09da6c9-247f-44cd-8365-29ffd86bd76d︡{"html": "The first thing you should always do when beginning a new worksheet is to define the variables you plan to use. Since $x$ and $y$ are usually variables in our work, I'll start by stating they are variables. (Note: Sage always assumes $x$ is variable, but I think this is the only one it assumes anything about.) Without doing this, Sage will not let me use $x$ and $y$ without giving an error. The %auto command makes this cell evaluate every time you load this worksheet, so that you can use $x$ and $y$ as variables without having to evaluate this cell.
"}︡ ︠817e64a0-8497-439d-8f64-43a39f25d1dfa︠ %auto var('x,y') ︡18ae324a-d05d-4be7-9e84-a31996f50865︡{"stdout": "(x, y)"}︡ ︠e718178f-efb0-4b2a-b641-d5c93daa52bai︠ %htmlSome Basics
Inserting new Sage cells
You can insert a new cell above or below any existing cell or block of text. To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple horizontal line pop up. Click on this horizontal line to produce a new Sage cell. Try that just below right now and then type in some other complicated expression of numbers and then click "evaluate."
Inserting a new block of text
You can also insert a new block of text (also called an "html cell") above or below any existing Sage cell or other block of text. To insert a new html cell, you Shift+click the purple horizontal line that pops up instead of just clicking on it. This will pop open a tiny text editor for you to type in. There are lots of things you can do inside an html block, but for now, we will keep it simple. You can always edit an existing html cell by simply double-clicking on the corresponding cell. After you are done editing an html cell, click "Save changes."
Add an html cell at the very top of this worksheet and type in your names.
Declare another variable in the cell below. Pick your favorite letter, but you should avoid $e$. Why?
︡b667b5c4-5610-4c11-8a3b-7c1b2893110c︡{"html": "Some Basics
\nInserting new Sage cells
\nYou can insert a new cell above or below any existing cell or block of text. To insert a new cell, put your cursor above (respectively, below) the cell or block of text where you want to insert a new cell until you see a purple horizontal line pop up. Click on this horizontal line to produce a new Sage cell. Try that just below right now and then type in some other complicated expression of numbers and then click \"evaluate.\"
\n\nInserting a new block of text
\nYou can also insert a new block of text (also called an \"html cell\") above or below any existing Sage cell or other block of text. To insert a new html cell, you Shift+click the purple horizontal line that pops up instead of just clicking on it. This will pop open a tiny text editor for you to type in. There are lots of things you can do inside an html block, but for now, we will keep it simple. You can always edit an existing html cell by simply double-clicking on the corresponding cell. After you are done editing an html cell, click \"Save changes.\"
\nAdd an html cell at the very top of this worksheet and type in your names.
\n\nDeclare another variable in the cell below. Pick your favorite letter, but you should avoid $e$. Why?
"}︡ ︠492d934d-d292-4442-88b2-07983e5cdf1di︠ %htmlNow let's solve an equation.
︡be04f66f-c4b3-4b4f-8d57-d144fe185922︡{"html": "Now let's solve an equation.
"}︡ ︠09cd98f0-b290-47e2-96df-e5bfdd70640e︠ solve(x^2-4*x+3==0,x) ︡ba6707ea-9619-4dc7-a2e3-bb90e62ac95a︡{"stdout": "[x == 3, x == 1]"}︡ ︠35777faf-8e24-4308-aa69-e2f130f47419i︠ %htmlNow, I want you to try one. Mimic the example above, being sure to use * for multiplication and two equal signs. If you forget to put a times sign between two things, Sage will give you an error.
︡75b010e7-3c24-4e24-bb3f-054bcb49e7a0︡{"html": "Now, I want you to try one. Mimic the example above, being sure to use * for multiplication and two equal signs. If you forget to put a times sign between two things, Sage will give you an error.
"}︡ ︠f4dd14f2-d1a9-45f5-872a-face9fdca504i︠ %htmlYou can quickly factor polynomials in Sage using factor. Evaluate each of the next two cells.
︡fc5edcf0-a930-4671-aca3-610aca154f01︡{"html": "You can quickly factor polynomials in Sage using factor. Evaluate each of the next two cells.
"}︡ ︠996955e5-3aa9-4a18-8879-7816932d3305︠ factor(x^2+3*x+2) ︡710ef1ce-dc1d-4ddb-97da-95344d13dab7︡{"stdout": "(x + 1)*(x + 2)"}︡ ︠8c4d1bcf-b1c8-467e-a6bb-07959bfeec8e︠ factor(x^2+3x+2) #without the * between 3 and x, there is an error. ︡c837f19a-445f-4ecf-8d43-d2e203d39c17︡{"stdout": "line 4\n factor(x**_sage_const_2 +3x+_sage_const_2 ) #without the * between 3 and x, there is an error.\n ^\nSyntaxError: invalid syntax"}︡ ︠c23cbdb5-aeea-4665-8a31-24619f7fde33i︠ %htmlNote:
Now, you try one below. Try to use the variable that you declared earlier.
︡149e1c4a-78ee-421c-8cc7-d0b7c723484a︡{"html": "
Note:
\nNow, you try one below. Try to use the variable that you declared earlier.
\n"}︡ ︠528d99dd-20e1-4c31-bbc2-dea37ed87cffi︠ %html
You can also use the factor command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms.
︡686b5157-be61-4b80-be0e-b56907486119︡{"html": "You can also use the factor command to simplify rational expressions, as it will factor both the numerator and denominator and then cancel common terms.
"}︡ ︠dbdf0f39-2a50-43a3-b72f-bf78ad9f6a27︠ factor((x^2+4*x+3)/(x^2-9)) ︡c13f3b2c-1994-4c3b-b447-18702a7690ae︡{"stdout": "(x + 1)/(x - 3)"}︡ ︠fb197f43-6ee2-40bd-abaa-dfc734b77ed7i︠ %htmlOK, your turn. Try a new one below.
︡da2e6e7d-5e62-451c-bfa9-ff49ee6a28f9︡{"html": "OK, your turn. Try a new one below.
"}︡ ︠5f5f42f9-707b-4f3c-ad80-73101f960b14i︠ %htmlIf you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. Here I store a rational expression as f, then simplify it, print it, and then plot it. Notice that you can often write f.[something] to perform commands.
︡b04e1cbc-8451-42fc-9d8f-f6321e403e3b︡{"html": "If you plan to use the same expression many times, you can save yourself lots of time by storing it as a variable. Here I store a rational expression as f, then simplify it, print it, and then plot it. Notice that you can often write f.[something] to perform commands.
"}︡ ︠45f2030c-75a9-45d5-9f89-6ff3294cf914︠ f=(x^2+4*x+3)/(x^2-9) f.full_simplify() ︡a60f393e-c324-4d1a-bd09-aee9eadf1188︡{"html": "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\frac{{\\left(x + 1\\right)}}{{\\left(x - 3\\right)}}"}︡ ︠84d174bf-2f99-4b2c-bc2e-5819d0a23a70︠ f ︡6c67cbdb-3822-4faa-9e8a-17388dd59f62︡{"html": "\\newcommand{\\Bold}[1]{\\mathbf{#1}}\\frac{{\\left(x^{2} + 4 \\, x + 3\\right)}}{{\\left(x^{2} - 9\\right)}}"}︡ ︠2952c89a-4df9-4eae-a280-92e9b21fccd6︠ plot(f,(x,-2,4)) ︡b743c486-a324-4cc2-a8bd-0c66e311ccde︡{"html": "Since the plot above had a vertical asymptote, you can use ymin and ymax to give a top and bottom range to your plot. Both commands below produce the same output.
︡faa370cd-510e-4a9b-b9d2-79d2a578ae20︡{"html": "Since the plot above had a vertical asymptote, you can use ymin and ymax to give a top and bottom range to your plot. Both commands below produce the same output.
"}︡ ︠25b58fe5-918e-476b-91ee-9bc9b263312a︠ plot(f,(x,-2,4),ymin=-10,ymax=10) ︡cf7c8bcb-98b9-4908-bb4e-0abbac1869ab︡{"html": "Once you have defined a variable, if you want to remove it from Sage's memory use the del() function. The following command removes f from memory, so typing f from now on should give an error.
︡87c318a4-de3e-4933-b516-fa548643aead︡{"html": "Once you have defined a variable, if you want to remove it from Sage's memory use the del() function. The following command removes f from memory, so typing f from now on should give an error.
"}︡ ︠b608f4c8-f34b-434f-bbd0-cd0d9233e582︠ del(f) ︡f8cc7bc7-1e96-4cb0-967b-cb65f85ed154︡︡ ︠7cdbaffd-1f17-4bb8-b447-a69b12918825︠ f ︡931af800-1de2-443e-ad17-e9b0768f7311︡{"stderr": "Traceback (most recent call last):\n File \"To use function notation in Sage (so that you can write something like f(2)) just write f(x)= before writing the function. Then you can use f(2), f(a), f(x+h) or any kind of function notation that you are familar with from algebra.
︡0fcc7f44-1b0d-4db8-a5f9-1fa8579c9a68︡{"html": "To use function notation in Sage (so that you can write something like f(2)) just write f(x)= before writing the function. Then you can use f(2), f(a), f(x+h) or any kind of function notation that you are familar with from algebra.
"}︡ ︠b31f3bb7-17e8-430e-ab0e-f66c4b1a5e7b︠ f(x)=5*x^2+4 ︡c6c9c24b-f311-430c-b1c6-d9a82b96a3bb︡︡ ︠4664e710-e5d7-4dd7-b0ee-b9657a9b5147︠ f(x) ︡e4210bdc-7ae3-42c1-873c-b5f8719c6e9f︡{"stdout": "5*x^2 + 4"}︡ ︠1d8327cd-54d3-490d-945b-76cff9eaa3d7︠ f(4) ︡401c2474-892f-4100-95ff-b74d65c64aa0︡{"stdout": "84"}︡ ︠5e246a0e-9c2a-44f6-b584-5f6e73e08122︠ a=4 ︡71cc66e7-b3fb-459a-a7e4-6f0514c32d11︡︡ ︠d3105b66-e241-4879-806c-2ee2c440c49c︠ f(a) ︡c562219d-def5-4927-bbff-aebfe27984df︡{"stdout": "84"}︡ ︠4ae90c6a-7c0b-43e2-afb3-c70f1ed09dbf︠ g(x)=x^2-3*x+2 f(g(x)) ︡95dc0613-1ac5-4007-bd0c-481a31b8287b︡{"stdout": "5*(x^2 - 3*x + 2)^2 + 4"}︡ ︠2db0a056-f0d7-4447-bac2-8d0cb04853a1︠ g(f(x)).show() ︡15272cc2-9e4c-49e0-ab00-37d7cc35a443︡{"html": "Experiment in the cells below.
︡fc1918c8-1974-4299-8c13-6721c26a9046︡{"html": "Experiment in the cells below.
"}︡ ︠af0ff832-dc08-4e1c-aa35-ab67283e8f22i︠ %htmlSage will try to return symbolic answers when it can. Use .show() to get a pretty output. use .n() to get a numeric value. Below I show you mutliple ways to use the n() function.
︡740b502b-19d7-4bd2-826c-801c13a72e94︡{"html": "Sage will try to return symbolic answers when it can. Use .show() to get a pretty output. use .n() to get a numeric value. Below I show you mutliple ways to use the n() function.
"}︡ ︠f920cae9-9496-4f85-8a1a-17252424a9a4︠ sqrt(2) ︡9631c37b-a2e6-4320-83f1-fe1d51408f43︡{"stdout": "sqrt(2)"}︡ ︠74d0b076-be3a-4fee-a715-8a737993c69c︠ sqrt(2).show() ︡ba1bb0ae-0dfc-4b40-866f-6e60f75753de︡{"html": "Try similar things with the number $e$.
︡2ae53526-66dc-4f3e-8323-db1854a78869︡{"html": "Try similar things with the number $e$.
"}︡ ︠6431cb59-3361-4b6d-bc4b-f1687c0aa471i︠ %htmlLet's look at some more plotting examples.
︡3d177327-399d-4bbf-ae51-b1694e1558f5︡{"html": "Let's look at some more plotting examples.
"}︡ ︠f3c422e0-c7a6-40e5-9be8-954e2e9bf6e2︠ #this is the basic plotting command plot(x^2, (x, -2, 3)) ︡6f02e69d-e834-4623-8b40-708779b9d48f︡{"html": "Now let's look at trigonometry. Sage will give you an exact value, and simplify it when possible. Since the exact value of sin(2) is unknown, it just returns sin(2) unless you ask for a number.
︡168e2437-fd07-4ab4-b52b-39f4ffb9a5dc︡{"html": "Now let's look at trigonometry. Sage will give you an exact value, and simplify it when possible. Since the exact value of sin(2) is unknown, it just returns sin(2) unless you ask for a number.
"}︡ ︠9c060d0c-20dd-4b37-801f-61fe1da5dad5︠ cos(pi/2) ︡d6fa408f-49ad-48fc-acfa-feac2ebd2572︡{"stdout": "0"}︡ ︠85ecf421-1c22-4864-8a49-1dc5b9559d98︠ cos(pi/6) ︡a90e55d9-f70c-498b-9670-8742427b714f︡{"stdout": "1/2*sqrt(3)"}︡ ︠cfc85879-e831-4236-b7c4-ed3f2711643a︠ sin(2) ︡3624c79f-42aa-4fa7-8902-d0b077477116︡{"stdout": "sin(2)"}︡ ︠a6f950de-a1e9-4ad9-a2ec-0f5cb2fb4fbe︠ sin(2).n() ︡941424b8-622a-4a95-8e87-9e8d8f2711f6︡{"stdout": "0.909297426825682"}︡ ︠ce183a12-9ab8-4f55-b0f1-69d540703fea︠ cos(pi/12).show() ︡e8b42760-2abd-4d58-9bd3-88673a067c31︡{"html": "This final example shows you a feature of Sage called interact, which allows you to create a slider to change variables. Drag the sliders around and experiment.
︡6e7165c4-e48b-4ff9-a9c2-3832b54cd522︡{"html": "This final example shows you a feature of Sage called interact, which allows you to create a slider to change variables. Drag the sliders around and experiment.
"}︡ ︠cf31efbc-7558-4a81-ae53-bcccec488f1f︠ @interact def _(a=(-2,2),b=(1/2,3,1/4),c=(-pi,pi,pi/6),d=(-2,2)): show(plot(a*sin(b*x+c)+d,(x,0,6),ymin=-3,ymax=3)) ︡4c0c5bef-b4e6-41f9-845d-69f843c97329︡︡ ︠5ca7dd6f-4a36-4955-91ae-46a124d0b525i︠ %htmlNow, using the "Sage Quick Reference: Calculus" sheet that I gave you, see if you can do some differentiation and integration. You can also access the reference sheet here.
I want you to do at least 3 different examples below. Perhaps you can try some of your homework problems. Ideally, I'd like to try some definite and indefinite integrals.
Note: When Sage computes an indefinite integral, it omits the $+C$ on the end.
︡8bcf1eec-e92c-4c2e-b90c-47d737beaa87︡{"html": "Now, using the \"Sage Quick Reference: Calculus\" sheet that I gave you, see if you can do some differentiation and integration. You can also access the reference sheet here.
\nI want you to do at least 3 different examples below. Perhaps you can try some of your homework problems. Ideally, I'd like to try some definite and indefinite integrals.
\nNote: When Sage computes an indefinite integral, it omits the $+C$ on the end.
"}︡ ︠8ab5b6d3-13ce-4896-9866-d612b5f51b1ci︠ %htmlWhen you are done, you should click "Save" at the top right of the worksheet. If you click "Save & quit", the worksheet will close and you will be returned to your "home" directory. Just click "Save" for now.
I want you to email me a copy of this worksheet. To this, you should select "File..." at the top left of the worksheet. Select "Save worksheet to file..." and save the worksheet some place on your computer (and remember where you put it). Name the file something like, "Lab 2 (Tom, Dick, Jane)" (without the quotes). Now, send that file to me as an attachment to an email. Remember, I only need one worksheet per group.
︡39e754ca-7549-4995-ab9b-d4debe4488cc︡{"html": "When you are done, you should click \"Save\" at the top right of the worksheet. If you click \"Save & quit\", the worksheet will close and you will be returned to your \"home\" directory. Just click \"Save\" for now.
\nI want you to email me a copy of this worksheet. To this, you should select \"File...\" at the top left of the worksheet. Select \"Save worksheet to file...\" and save the worksheet some place on your computer (and remember where you put it). Name the file something like, \"Lab 2 (Tom, Dick, Jane)\" (without the quotes). Now, send that file to me as an attachment to an email. Remember, I only need one worksheet per group.
"}︡