︠ab87d20e-31d3-4b26-9225-5bddcf971c6ei︠ %html

Note: This worksheet is a modification of the worksheet found here.

Lab 2: Introduction to Sage

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": "

Note: This worksheet is a modification of the worksheet found here.

\n

Lab 2: Introduction to Sage

\n

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.

\n

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.\"

\n

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.

"}︡ ︠390e610c-3f65-4c1f-9abc-6cf1be3a737bi︠ %html

Variables, Solving, Factoring, Simplifying, Plotting

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.

︡c09da6c9-247f-44cd-8365-29ffd86bd76d︡{"html": "

Variables, Solving, Factoring, Simplifying, Plotting

\n

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︠ %html

Some 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

\n

Inserting new Sage cells

\n

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.\"

\n\n

Inserting a new block of text

\n

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.\"

\n

Add an html cell at the very top of this worksheet and type in your names.

\n\n

Declare another variable in the cell below.  Pick your favorite letter, but you should avoid $e$.  Why?

"}︡ ︠492d934d-d292-4442-88b2-07983e5cdf1di︠ %html

Now 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︠ %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.

︡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︠ %html

You 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︠ %html

Note:

  1. You can use # to make comments inside a Sage cell.  Sage will ignore anything to the right of #.  For example, Sage ignored "#without the * between 3 and x, there is an error." in the previous cell.
  2. You can make Sage display its output in nice format by checking the box at the top of the worksheet that says "typeset."  Do that now and then re-evaluate the second to last cell.

Now, you try one below.  Try to use the variable that you declared earlier.

 

︡149e1c4a-78ee-421c-8cc7-d0b7c723484a︡{"html": "

Note:

\n
    \n
  1. You can use # to make comments inside a Sage cell.  Sage will ignore anything to the right of #.  For example, Sage ignored \"#without the * between 3 and x, there is an error.\" in the previous cell.
  2. \n
  3. You can make Sage display its output in nice format by checking the box at the top of the worksheet that says \"typeset.\"  Do that now and then re-evaluate the second to last cell.
  4. \n
\n

Now, 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︠ %html

OK, 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︠ %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.

︡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": ""}︡ ︠338a164a-0035-49b4-acf2-3fa75af0a565i︠ %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": ""}︡ ︠7fa1cb9b-570a-4d2a-9c60-9eb352bd190ci︠ %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 \"\", line 1, in \n File \"/home/sage/sagenb/sage_notebook/worksheets/bmwoodruff/11/code/21.py\", line 6, in \n exec compile(ur'f' + '\\n', '', 'single')\n File \"\", line 1, in \n \nNameError: name 'f' is not defined"}︡ ︠0cdda350-964f-442b-8fa7-5802d29620c8i︠ %html

Function Notation

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": "

Function Notation

\n

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": "
{(5 \\, x^{2} + 4)}^{2} - 15 \\, x^{2} - 10
"}︡ ︠4eaff667-1e48-4797-8dfa-995429ec8da6︠ expand(g(f(x))).show() g(f(x)).expand().show() #Either method works. ︡d00bdbbd-d763-4bd3-9ab0-c44bd98e26a8︡{"html": "
25 \\, x^{4} + 25 \\, x^{2} + 6
"}︡{"html": "
25 \\, x^{4} + 25 \\, x^{2} + 6
"}︡ ︠43dc6640-28b6-481b-95a2-a9fcfb68f9c4i︠ %html

Experiment in the cells below.

︡fc1918c8-1974-4299-8c13-6721c26a9046︡{"html": "

Experiment in the cells below.

"}︡ ︠af0ff832-dc08-4e1c-aa35-ab67283e8f22i︠ %html

Getting Pretty Output and Numerical Values

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.

︡740b502b-19d7-4bd2-826c-801c13a72e94︡{"html": "

Getting Pretty Output and Numerical Values

\n

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": "
\\sqrt{2}
"}︡ ︠ad7d8390-f152-4055-abe6-62d7c4419cc2︠ sqrt(2).n() ︡b00eab4a-71aa-49a2-81e9-df91c2d922a3︡{"stdout": "1.41421356237310"}︡ ︠4ae50533-b96e-4b99-8869-e0769dd61ec1︠ n(sqrt(2)) ︡10ddcee7-0f1f-4667-9b47-6d53a8cdde26︡{"stdout": "1.41421356237310"}︡ ︠15347783-646b-4714-80b5-438c3e5e288a︠ sqrt(2).n(digits=50) ︡41d815b7-4972-4a36-9ee9-5f10db58010b︡{"stdout": "1.4142135623730950488016887242096980785696718753769"}︡ ︠e7b62c6a-aefc-4387-8fdc-ab105c4a3306︠ n(sqrt(2),digits=500) #Just for fun let's look at 500 decimals. ︡ad0f0da2-d55c-48d1-9079-7339eb9206fc︡{"stdout": "1.4142135623730950488016887242096980785696718753769480731766797379907324784621070388503875343276415727350138462309122970249248360558507372126441214970999358314132226659275055927557999505011527820605714701095599716059702745345968620147285174186408891986095523292304843087143214508397626036279952514079896872533965463318088296406206152583523950547457502877599617298355752203375318570113543746034084988471603868999706990048150305440277903164542478230684929369186215805784631115966687130130156185689872372"}︡ ︠97c6f9d6-2f69-485e-9f93-277f64932bf3i︠ %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︠ %html

More with Plotting

Let's look at some more plotting examples.

︡3d177327-399d-4bbf-ae51-b1694e1558f5︡{"html": "

More with Plotting

\n

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": ""}︡ ︠9e8e11d9-7c5f-45da-a9c7-2318b1382ba4︠ #You can include multiple functions in a list plot((x^2,sqrt(x),sin(x)), (x, 0, pi)) ︡a47b32cf-1973-4329-a09c-0ef0e721e131︡{"html": ""}︡ ︠55d4e9b2-5ca3-4ca9-8348-64eeb4177e89︠ #You can also combine plots by adding them. This allows me to make each plot a different color. plot(x^2, (x, 0, pi),color='red')+plot(sqrt(x), (x, 0, pi),color='green')+plot(sin(x), (x, 0, pi)) ︡9e56b367-38e6-46d7-8191-e914eddd2726︡{"html": ""}︡ ︠5a11c037-0ed3-4e7f-a4f5-11245ff3e83ci︠ %html

Trigonometry

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": "

Trigonometry

\n

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": "
\\frac{1}{12} \\, {(\\sqrt{3} + 3)} \\sqrt{6}
"}︡ ︠39949652-c581-4a4e-aa65-71964728f44d︠ g=cos(x+y) ︡fc8e56d4-68f9-4919-a879-1479b55295a6︡︡ ︠ddbfb24f-ae00-4d69-9a27-ac14b4c198cd︠ g.expand_trig() #You can use Sage to help you remember your trig identities. ︡a87be3fa-e8eb-4c44-ae82-2097db3ff768︡{"stdout": "-sin(x)*sin(y) + cos(x)*cos(y)"}︡ ︠a6ed460b-4c48-449d-9a89-16ca0e9480dc︠ #Let's graph a*sin(b*x+c)+d. a=3 b=4 c=pi/3 d=-2 f=a*sin(b*x+c)+d f.plot(-pi,2*pi) ︡8fda0268-122e-491e-9391-601025ec34c9︡{"html": ""}︡ ︠a63bfb08-d6c4-4c01-a002-b40213335ce9i︠ %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︠ %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.

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.

\n

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.

\n

Note:  When Sage computes an indefinite integral, it omits the $+C$ on the end.

"}︡ ︠8ab5b6d3-13ce-4896-9866-d612b5f51b1ci︠ %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.

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.

\n

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.

"}︡