︠010e0d50-b17c-41b5-84ed-fc395c5f9230i︠ %md # Introduction to Sage Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools. ## Basic Computations Let's start with an easy calculation. Below you will see a *Sage cell*, with `2+2` already entered. Click inside the cell, then hit `Shift-Return` to evaluate the cell and get the result: ︡c599cc0c-8bd3-41d1-8612-91fad239679f︡{"md":"# Introduction to Sage\n\nSage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.\n\n## Basic Computations\n\nLet's start with an easy calculation. Below you will see a *Sage cell*, with `2+2` already entered. Click inside the cell, then hit `Shift-Return` to evaluate the cell and get the result:\n"}︡ ︠db62b200-0f69-497e-ba5b-c0cdc3211b67s︠ 2*(5-3)^2 ︡fafd881c-e9f9-4c76-b6ff-e92061c978b6︡{"stdout":"8\n"}︡ ︠b4479b19-43c9-4d4c-afe7-5d13af63cba1︠ ︡e25f87d7-a760-4a8b-a83a-33ee8ef256eb︡ ︠b27388bb-d55a-459d-be8d-63cc7f45c219i︠ %md (You can also click on a gray line to get a *new* Sage cell in which to enter your own calculation. Try it out!) As the above example shows, Sage can be used as a calculator. But Sage is a very *good* calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type `sin(pi/3)` and press [Shift]-[Enter] to see a little of what Sage can do. ︡33ac8f78-3b5b-404b-aeab-febd06ee41ff︡{"md":"(You can also click on a gray line to get a *new* Sage cell in which to enter your own calculation. Try it out!)\n\nAs the above example shows, Sage can be used as a calculator. But Sage is a very *good* calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type `sin(pi/3)` and press [Shift]-[Enter] to see a little of what Sage can do. \n"}︡ ︠24c80c0d-562c-4e6d-8e10-12840d9d0284s︠ sin(pi/3) ︡e3f70403-1ae5-46a3-a6be-77b4cfeb687a︡{"stdout":"1/2*sqrt(3)\n"}︡ ︠bb7c4053-a81f-4fa0-aafa-7cd9bbe94a68i︠ %md That's really good! But if you want to see a numerical approximation to the number $\sin(\pi/3)$, you can use the Sage function `n`. You can also use the symbol `_` to refer to the *last* computation that Sage did. So if the last thing you computed was `sin(pi/3)` above, you can type `n(_)` to find a numerical approximation. (If you have entered something else since then, just type `n(sin(pi/3))` in a new cell.) ︡99fa3b3a-bd24-43ec-8820-e2222a2f08ef︡{"md":" That's really good! But if you want to see a numerical approximation to the number $\\sin(\\pi/3)$, you can use the Sage function `n`. You can also use the symbol `_` to refer to the *last* computation that Sage did. So if the last thing you computed was `sin(pi/3)` above, you can type `n(_)` to find a numerical approximation. (If you have entered something else since then, just type `n(sin(pi/3))` in a new cell.)"}︡ ︠fdd9cce3-7979-434c-8f75-5a8315089a66s︠ n(_) ︡8ed02560-a032-46cc-81af-c463ddd67b55︡{"stdout":"0.866025403784439\n"}︡ ︠20b6e6f7-1249-4d97-8fb3-918693cc5928i︠ %md If you want more accuracy, you can add the argument `digits` inside the `n` function, like this: ︡b01bc7fe-7f32-4c06-86c6-98579b919bb7︡{"md":"If you want more accuracy, you can add the argument `digits` inside the `n` function, like this:"}︡ ︠968f1533-c006-48b4-b93e-de5a9ef563b0s︠ n(sqrt(2), digits=50) ︡a7956132-8b2a-41fc-87f9-109cb784180c︡{"stdout":"1.4142135623730950488016887242096980785696718753769\n"}︡ ︠0c98db6a-65b5-48fb-b38e-073cb826c9bci︠ %md Sage is familiar with many functions you know about. A few notes about some special ones: * The inverse trig functions are written as `arcsin`, `arctan`, etc. * The natural logarithm can be written as `ln` or as `log`. If you want a logarithm to another base, such as base 10, specify the base with the `base` argument: `log(x, base=10)`, for example. * The natural exponential function, $e^x$, can be written as `exp(x)`. ︡5c36a837-de46-44e6-a549-d871ed685c88︡{"md":"Sage is familiar with many functions you know about. A few notes about some special ones:\n\n * The inverse trig functions are written as `arcsin`, `arctan`, etc.\n * The natural logarithm can be written as `ln` or as `log`. If you want a logarithm to another base, such as base 10, specify the base with the `base` argument: `log(x, base=10)`, for example.\n * The natural exponential function, $e^x$, can be written as `exp(x)`.\n"}︡ ︠2ebe751b-cb2f-4738-bae6-9206bba41c35s︠ log(64, base=2) ︡79c21c8c-3074-4cf8-9fc8-0d44f6afd2cf︡{"stdout":"6\n"}︡ ︠1e12df54-a0c5-4026-8eb2-3df2f43b5b5ci︠ %md **Practice** Can you find (to at least six decimal places) the angle which makes $\sin(x)$ equal to 1/3? ︡fe835922-d9b5-4086-a7d4-0a79e862af1d︡{"md":"**Practice** Can you find (to at least six decimal places) the angle which makes $\\sin(x)$ equal to 1/3?\n"}︡ ︠a742fc85-954f-4dfd-8d53-1a15cb452d58︠ ︡02f91cdf-fa57-4fd1-8944-7d42c6a56641︡{"md":"## Algebra\n\nSage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both `x` and `t` to be symbolic variables with the statement below:\n"}︡ ︠f3a68067-b97a-4ab2-a9d0-05b2fe7028cds︠ var("x, t") ︡35f3a3cb-179d-4ff0-b83a-54841ae7cf07︡{"stdout":"(x, t)\n"}︡ ︠34675c4e-ca7d-4100-87db-984c8615db06i︠ %md Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication **must** be indicated with a `*`; it cannot be represented by juxtaposition!) ︡6ef3d0e3-2494-4002-99df-e13ec367cd4d︡{"md":"Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication **must** be indicated with a `*`; it cannot be represented by juxtaposition!)"}︡ ︠1bb1b138-94af-43d2-8599-63428dad2a5bs︠ expand((x + 5)*(2*x - 1)) ︡d6717d82-d467-49d2-b6a0-3d5fe1e23a60︡{"stdout":"2*x^2 + 9*x - 5\n"}︡ ︠df27a327-92f0-4f6a-bc9d-2cce565a5b8fs︠ show(_) ︡4bba3047-584b-419a-89f1-8bbb1333f24c︡{"html":"
$\\displaystyle 2 \\, x^{2} + 9 \\, x - 5$
"}︡ ︠98f8e250-1385-4f91-87ce-043bcaddce85s︠ factor(6*x^3 + 26*x^2 - 20*x) ︡89700637-516a-4b20-88aa-4ec63eb1dff1︡{"stdout":"2*(3*x - 2)*(x + 5)*x\n"}︡ ︠83bcb222-0320-42f9-b4fb-3b897dfba053s︠ factor(17363829) ︡b3bb075c-c202-434e-a5ec-f44e6f951a55︡{"stdout":"3 * 7 * 826849\n"}︡ ︠b05ddad1-7fa7-4899-beb6-79e99e8f76fes︠ solve( x^2 + x == 6, x) ︡2bbd0f32-6f17-4852-adb4-329e7cdd7ca0︡{"stdout":"[x == -3, x == 2]"}︡{"stdout":"\n"}︡ ︠cbbcbb96-a487-49e0-b1c3-108d6766ca58s︠ p = (x^2 + 3*x + 5)/(x^2 -5*x+6) ︡131ea9f0-092f-4870-b4d9-4bf48dab3a97︡ ︠ba0693ac-4de5-4b05-bcf6-82979b3eeac9s︠ p.partial_fraction() ︡bf8366df-8c06-4fbe-879d-65d51074f1f3︡{"stdout":"-15/(x - 2) + 23/(x - 3) + 1\n"}︡ ︠553f61fc-8e2c-4dc1-8c96-bf393652f1bds︠ show(_) ︡85fd6e19-8957-4af7-8271-1fe674171f71︡{"html":"
$\\displaystyle -\\frac{15}{x - 2} + \\frac{23}{x - 3} + 1$
"}︡ ︠c5de58f9-d62b-4f00-a981-a24673cdbdbb︠ ︡0578b36e-d16b-4993-9ac6-db59586c10ba︡{"md":"It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function $f(x) = x^2 + 2$:"}︡ ︠796e3e9d-447b-40bd-9dec-28d6faa83751s︠ f(x) = x^2 + 2 ︡ab493208-3948-46ee-b80d-d17a128c007a︡ ︠27e2f578-61c7-417b-ba68-53c2af9f7f6fi︠ %md When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function $f$, if you wish: ︡20df9598-c5cf-4174-863d-e6a3554360b0︡{"md":"When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function $f$, if you wish:"}︡ ︠a7846dce-594b-43b0-9e67-ada07062da0bs︠ f(2) ︡b9bd25db-30a9-478e-af72-4229b9ff0a82︡{"stdout":"6\n"}︡ ︠a1b0c032-576c-4fba-ab55-70e24ab64bb8i︠ %md There are *many* other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more. ︡e131cccc-19f4-4263-99ad-82bcf332469f︡{"md":"There are *many* other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more."}︡ ︠dece9604-c03d-402b-aac0-00e6b24b7345i︠ %md ## Basic Calculus Sage can also carry out some basic symbolic calculus. Here are a few examples: ︡6f425c49-2501-4daa-b35f-bf6734f2babf︡{"md":"## Basic Calculus\n\nSage can also carry out some basic symbolic calculus. Here are a few examples:\n\n"}︡ ︠54b2bd96-1f18-4a15-a95d-62c8ab2536aes︠ var("x") derivative(3*exp(x^2)+log(x), x) ︡f3ef714e-e5a2-4f59-9e57-97e9d7dccefb︡{"stdout":"x\n"}︡{"stdout":"6*x*e^(x^2) + 1/x\n"}︡ ︠2fe0bff4-ddba-49d5-9db9-f268baeffdecs︠ derivative(x^2*t^2, x) ︡3e8ef92d-5464-429c-9650-c6d66df9c427︡{"stdout":"2*t^2*x\n"}︡ ︠504e14f7-5610-4c12-8db6-f917bcc6e3eds︠ integrate(x^3 + x + 1, x) ︡e9b814d0-bd1f-40d3-ac65-6b1d6806a8ab︡{"stdout":"1/4*x^4 + 1/2*x^2 + x\n"}︡ ︠cb867abf-240a-4dda-a0ed-9855093661bfs︠ integrate(x, (x, 0, 1)) ︡3202a27c-e4b4-4234-8c86-7caecf95e9f0︡{"stdout":"1/2\n"}︡ ︠d1bbcce0-ec27-4428-aceb-38393506dc58s︠ taylor(cos(x), x, 0, 7) ︡66824517-b9e7-4d43-b061-62dd2a942063︡{"stdout":"-1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1\n"}︡ ︠e84e67bd-7346-4c43-b086-1501c9f04ee0s︠ show(_) ︡c46a20c7-45fb-48cc-adad-f4efc5ae7d23︡{"html":"
$\\displaystyle -\\frac{1}{720} \\, x^{6} + \\frac{1}{24} \\, x^{4} - \\frac{1}{2} \\, x^{2} + 1$
"}︡ ︠0ad9f49b-c1b2-4609-b95f-0f4adba0e82b︠ ︡e41237a5-9e09-4136-ada9-997d0ec75bae︡{"md":"## Plotting\n\nIt is easy to plot a function in Sage. Here is an example of plotting the function $f(x) = 3 x^2 + 2 x - 1$ on the interval $[-2, 2]$:\n"}︡ ︠d15c1063-f362-45af-afe4-21344abbfee2s︠ plot(3*x^2+2*x-1, (x, -4, -1)) ︡9a7283ad-0a34-4a63-adbf-3d94f9a4fe52︡{"once":false,"file":{"show":true,"uuid":"b8c0f4da-3e64-489a-a098-bcf8accf2bf0","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_OspE5c.svg"}}︡{"html":"
"}︡ ︠7afad21f-4e55-4847-b30b-abdf98cf31eai︠ %md Sometimes the graph does not show the sorts of details you would like. Try the following to plot $\tan(x)$ on $[-\pi, \pi]$: ︡f3a67442-da5e-4b2e-a516-8f915a1a558d︡{"md":"Sometimes the graph does not show the sorts of details you would like. Try the following to plot $\\tan(x)$ on $[-\\pi, \\pi]$:"}︡ ︠a61fdf6c-37ce-4520-9a7f-edd549f5c2d8s︠ plot(tan(x), (x, -pi, pi)) ︡7bf7a536-f82c-4947-888f-edd81e0e75da︡{"once":false,"file":{"show":true,"uuid":"b1cdfba1-4a7d-4043-ad24-9925aa22311a","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_6H1Jl2.svg"}}︡{"html":"
"}︡ ︠5ec8ee5c-ce14-47c3-aa6c-39a1accd210di︠ %md The $y$ values get so big that we cannot see much else. We can add the extra arguments `ymin` and `ymax` to the plot function to fix the window of the plot: ︡9475dbc7-ee87-465c-9bd0-96bb4ad94c7c︡{"md":"The $y$ values get so big that we cannot see much else. We can add the extra arguments `ymin` and `ymax` to the plot function to fix the window of the plot:"}︡ ︠41ca0588-eacc-427b-8e88-8a619972ae5es︠ plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2) ︡32557d3e-cd75-47b6-9357-35f5892ce352︡{"once":false,"file":{"show":true,"uuid":"63f5154f-4040-4a20-8d17-8b3431269203","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_Ui8Qzv.svg"}}︡{"html":"
"}︡ ︠650f694a-9bcb-40d0-ac84-f0191ca956d5i︠ %md By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the `detect_poles` option: ︡f0394fad-6424-4b72-9149-32a14bb993b9︡{"md":"By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage \"connecting the dots\" as it plots the graph. We can adjust the behavior at these points using the `detect_poles` option:"}︡ ︠ee482318-1eca-4f55-a604-728c8ae01de7s︠ plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2, detect_poles="show") ︡f9af3336-cae3-45b6-8100-63f81ca74f2a︡{"once":false,"file":{"show":true,"uuid":"255a2476-26f9-4ae2-af3c-ede53096b571","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_nvD6lU.svg"}}︡{"html":"
"}︡ ︠af6086e0-99f8-4232-a6fb-8a19fc5e98b3i︠ %md ## 3D Plotting We are more interested in plots of functions of several variables. Here's how to plot a surface: ︡95461224-a080-4dfb-a5a1-1282d697609b︡{"md":"## 3D Plotting\n\nWe are more interested in plots of functions of several variables. Here's how to plot a surface:\n"}︡ ︠4efb6806-d36d-42c0-9fc7-b7180758548bs︠ var("x, y") plot3d(sin(x*y), (x, 0, 4), (y, 0, 4)) ︡8a04731d-903e-43ec-8583-36b2e21e7705︡{"stdout":"(x, y)\n"}︡{"file":{"uuid":"987b9b2b-adf5-41ea-ac0e-c5fa5e1ade1e","filename":"987b9b2b-adf5-41ea-ac0e-c5fa5e1ade1e.sage3d"}}︡{"html":"
"}︡ ︠a3fdd059-0966-4eff-9a76-2b38ea97a8bdi︠ %md A useful trick is to plot an *implicit surface*, where a function is equal to a given value. Here we can plot a sphere, by plotting where $x^2 + y^2 + z^2 = 1$: ︡ee71821e-ae37-4947-89d7-2de21fbe472e︡{"md":"A useful trick is to plot an *implicit surface*, where a function is equal to a given value. Here we can plot a sphere, by plotting where $x^2 + y^2 + z^2 = 1$:\n"}︡ ︠84a01a4c-a32c-4841-8952-31cad3f9d8d4s︠ var("x, y, z") implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour = [1]) ︡8eaa4a07-0c0c-4e12-9be8-5afa22196537︡{"stdout":"(x, y, z)\n"}︡{"file":{"uuid":"cb91ec69-073a-4ff4-8fda-a0166b6c17e0","filename":"cb91ec69-073a-4ff4-8fda-a0166b6c17e0.sage3d"}}︡{"html":"
"}︡ ︠fccfa904-7a89-4f69-8849-53dd3997ab66i︠ %md Sage is also capable of plotting parametric plots in space, using `parametric_plot3d`: ︡40dc53fe-86f2-47df-96f3-73836cb4c2e6︡{"md":"Sage is also capable of plotting parametric plots in space, using `parametric_plot3d`:"}︡ ︠7edbcf15-d448-4e3a-a49b-67d302e208d2s︠ var("t") parametric_plot3d((3*cos(t), 3*sin(t), t), (t, 0, 6*pi)) ︡83c85b2b-8f7b-45fd-9f6b-32ca3b6e3858︡{"stdout":"t\n"}︡{"file":{"uuid":"09517d0c-d03f-4c20-b449-f60dfb552fe2","filename":"09517d0c-d03f-4c20-b449-f60dfb552fe2.sage3d"}}︡{"html":"
"}︡ ︠a9e57e24-0eee-43ba-9946-caac0343ed48i︠ %md To combine two plots, just add them: ︡31f074b4-10ab-43a6-98ed-451bb8fef2f5︡{"md":"To combine two plots, just add them:"}︡ ︠88efc613-741a-47ce-835a-e89fd7dcf952s︠ p1 = plot3d(x^2+y^2, (x, -1.2, 1.2), (y, -1.2, 1.2)); p2 = plot3d(1 - y^2, (x, -1.2, 1.2), (y, -1.2, 1.2), color="green"); p1+p2 ︡594b6645-dbec-45ca-96a9-44243d4e7079︡{"file":{"uuid":"a7f37902-e35c-4c04-b0c4-73b4fe669bb3","filename":"a7f37902-e35c-4c04-b0c4-73b4fe669bb3.sage3d"}}︡{"html":"
"}︡ ︠91dbbe97-0fe2-4e37-bed6-9151189b8fabi︠ %md ## Vectors and Matrices Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the `vector` function: ︡e8061175-d093-4072-b548-6ef659912d95︡{"md":"## Vectors and Matrices\n\nSage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the `vector` function:\n"}︡ ︠e2bd10b2-8616-4f30-8ccb-89f8af61ffdfs︠ v1 = vector([1, 2, 3]); v2 = vector([3, -1, 1]) ︡4f161043-3211-4231-8c1d-8004ad51548c︡ ︠76228a3d-7fea-44aa-a84d-cbedbc406c59s︠ show(v1) ︡92b59ba1-67c4-41c3-9a10-80d58756491f︡{"html":"
$\\displaystyle \\left(1,\\,2,\\,3\\right)$
"}︡ ︠302dda75-5278-43b5-8f61-baba7e28c5a5i︠ %md Sage understands common operations, and even knows about dot and cross products: ︡64044314-dfd4-43b1-ac1e-9aa0ddf8e253︡{"md":"Sage understands common operations, and even knows about dot and cross products:\n"}︡ ︠7d017f9d-8e8e-4ce2-b152-3c5a1d05505as︠ v1 + 2*v2 v1.cross_product(v2) v1.dot_product(v2) v1 * v2 ︡9deb7e36-7f7c-4b0a-9022-f74632e187b7︡{"stdout":"(7, 0, 5)\n"}︡{"stdout":"(5, 8, -7)\n"}︡{"stdout":"4\n"}︡{"stdout":"4\n"}︡ ︠9161ce03-3668-4665-9896-0c25d42531cai︠ %md Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by ``QQ``. Then we enter the matrix as a list of lists. We define a matrix ``m`` ︡0e2cab36-1837-45ce-9b72-a4ab9a99eee5︡{"md":"Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by ``QQ``. Then we enter the matrix as a list of lists. We define a matrix ``m``\n"}︡ ︠65df599a-ea3e-4e56-8a62-72b6b7a11d35s︠ m = matrix(QQ, [[3, 2, 0], [1, -1, 1], [-1, 1, -5]]); show(m) ︡517a80e3-cea0-4ce5-a458-7db08c54ae0d︡{"html":"
$\\displaystyle \\left(\\begin{array}{rrr}\n3 & 2 & 0 \\\\\n1 & -1 & 1 \\\\\n-1 & 1 & -5\n\\end{array}\\right)$
"}︡ ︠7ec58fee-d247-4917-aa50-c5294b57f5b5︠ %md Sage recognizes many operations we can perform on matrices. act_on_polynomial Here are some you might be familiar with: ︡8b86d41e-10dd-4c4e-869a-c1d9215d92d1︡ ︠44d55b69-263c-472e-8bcc-c47d1006e240s︠ m.rref() ︡e94c527c-aef1-4aa0-bd40-a656a1832105︡{"stdout":"[1 0 0]\n[0 1 0]\n[0 0 1]\n"}︡ ︠4b51815d-9a09-4001-8d37-c25d2a68e2a9s︠ m.eigenvalues() ︡6ebee666-af73-48cb-b75f-071bea4c498c︡{"stdout":"[-5.305282147755060?, -1.105324652427052?, 3.410606800182113?]\n"}︡ ︠d0445f93-494b-486c-882f-19177f4e30b9i︠ %md ## Programming Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not: ︡61c0db8d-c3ab-456d-8b48-ae9c569785c4︡{"md":"## Programming\n\nSage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:\n"}︡ ︠0451ae92-0aec-4c32-a906-d7cc07825606s︠ def iseven(number): if (number % 2 == 0): return true else: return false ︡3b83df66-8429-4999-9b13-4722797f4ff8︡ ︠81197672-69b1-45ba-9c37-7f18bbd11af5s︠ iseven(64.5) ︡ea219f62-2dbb-4b4c-8a11-8956858d1592︡{"stdout":"False\n"}︡ ︠b268cbd3-c78a-4391-a889-158427e31debi︠ %md If you are familiar with Python, Sage uses the same syntax. ︡51be8236-4f99-4eae-8b75-a39f3ab89b60︡{"md":"If you are familiar with Python, Sage uses the same syntax.\n"}︡ ︠9ee11943-f99b-4500-9851-48edde9c2ba8is︠ %md This is text. We can include *italic*, or **bold**. ︡d1cc14c6-ac2b-4a61-96c6-ff60fd8625ee︡{"md":"This is text. We can include *italic*, or **bold**.\n"}︡ ︠38c2f491-bf15-45ea-97e1-4abc52b95a6bi︠ ︡05009d64-98dc-47e2-8b76-a12733ac1752︡{"md":"## Writing Text\n\nFinally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using *markdown*, a simple language for formatted text. To start a markdown cell, put `%md` as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish\n\n * To type *italicized* text, surround the words with an asterisk: `*italicized*`\n * To make text **bold**, use two asterisks: `**bold**`\n * Trickier: You can format a polynomial like $f(x) = 3 x^3 - x^2 + 2x + 1$ like this: `$f(x) = 3 x^3 - x^2 + 2x + 1$`\n \n(That last example is very tricky. There is an entire language for mathematical typesetting called $\\LaTeX$ which is being used there. You are not required to use this notation, but you are welcome to try if you wish.) \n\nAll of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.\n"}︡ ︠c70d2a49-a9a8-49cb-bcd1-27096bf89fc7si︠ %md (Alternatively, you can also start a cell with `%html`, and Sage will recognize and render HTML as well.) ︡73f7ef05-41a8-4f91-adcc-036940076330︡{"md":"(Alternatively, you can also start a cell with `%html`, and Sage will recognize and render HTML as well.)\n"}︡ ︠e29555e4-ec1c-4a27-bcf8-e383d8a50249i︠ %md ## Other References Here are some useful references if you would like to explore Sage further: * The [Official Sage Tutorial](http://doc.sagemath.org/html/en/tutorial/index.html) * A lengthy (but very good) [Sage Tutorial](http://www-rohan.sdsu.edu/~mosulliv/Teaching/sdsu-sage-tutorial/index.html) at San Diego State University. * Gregory Bard at University of Wisconsin, Stout, has a [page of information about Sage](http://www.gregorybard.com/SAGE.html), including some videos and his new book, *Sage for Undergraduates*. ︡cb862fba-4c87-414e-a481-d0f806f96ba2︡{"md":"## Other References\n\nHere are some useful references if you would like to explore Sage further:\n\n * The [Official Sage Tutorial](http://doc.sagemath.org/html/en/tutorial/index.html)\n * A lengthy (but very good) [Sage Tutorial](http://www-rohan.sdsu.edu/~mosulliv/Teaching/sdsu-sage-tutorial/index.html) at San Diego State University.\n * Gregory Bard at University of Wisconsin, Stout, has a [page of information about Sage](http://www.gregorybard.com/SAGE.html), including some videos and his new book, *Sage for Undergraduates*.\n"}︡ ︠9191765f-9f46-481d-a4a0-36c5fee1ffa7s︠ f(x) = ((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3)) table([(x, f(x)) for x in [-.59, -.599, -.5999, -.61, -.601, -.6001, -.60001]], header_row = ("x", "f(x)")) ︡c8102c2f-1f06-424b-828a-1a27609c6cf7︡{"stdout":" x f(x)\n+--------------------+-------------------+\n -0.590000000000000 -28.4380000000017\n -0.599000000000000 -287.639800000051\n -0.599900000000000 -2879.63998000200\n -0.610000000000000 29.1579999999997\n -0.601000000000000 288.359800000013\n -0.600100000000000 2880.35998001141\n -0.600010000000000 28800.3599988519\n"}︡ ︠9456f034-0e4b-4d44-a9a8-8ca2207bf0f9s︠ f(2) ︡6d0f86e6-ecef-4856-ab66-a0c005572c50︡{"stdout":"10/13\n"}︡ ︠4416abad-da9b-434a-a855-b73f14b672b1s︠ [(x, f(x)) for x in [1, 2, 3]] ︡20c11e6d-ebb1-4d2e-9a8f-b875545f7f1c︡{"stdout":"[(1, 1/2), (2, 10/13), (3, 1)]\n"}︡ ︠0cfb85c3-7f03-48fb-b106-d3a5fa1c7f85s︠ plot((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3),(x, -1, 1), ymax=10, ymin=-10) ︡21144a50-85cf-4cd5-86fa-c14c3ebb0e61︡{"once":false,"file":{"show":true,"uuid":"30869320-aac9-43df-8055-0a2fd7070d6d","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_lEL07B.svg"}}︡{"html":"
"}︡ ︠453f3f90-8462-40e3-a09b-2ffeb52a362b︠ ︡31779f78-ec54-4b16-86cc-14e872d11a57︡ ︠ac0acac6-10db-439e-8e50-0cadc689cb7ai︠ %md # Introduction to Sage Sage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools. ## Basic Computations Let's start with an easy calculation. Below you will see a *Sage cell*, with `2+2` already entered. Click inside the cell, then hit `Shift-Return` to evaluate the cell and get the result: ︡7ee6cdeb-792a-4627-91c0-bac15172bb47︡{"md":"# Introduction to Sage\n\nSage is an open-source (and free) mathematical software system. SageMathCloud is a way to use Sage through a web browser, without having to install Sage on your own computer. We will use Sage in this course to explore some Calculus using software tools.\n\n## Basic Computations\n\nLet's start with an easy calculation. Below you will see a *Sage cell*, with `2+2` already entered. Click inside the cell, then hit `Shift-Return` to evaluate the cell and get the result:\n"}︡ ︠516e74ad-643f-4754-b1f1-878cf3a8991cs︠ 2*(5-3)^2 ︡5e3a8a5f-c6d6-49e0-8d26-19b650bc2c49︡{"stdout":"8\n"}︡ ︠2d079fbc-6291-47d4-b5ae-e315163ecfcf︠ ︡8f5086f9-2fa5-451b-bc19-05400c05330a︡ ︠4e0fd119-a236-4192-a11a-fc000fc5496ei︠ %md (You can also click on a gray line to get a *new* Sage cell in which to enter your own calculation. Try it out!) As the above example shows, Sage can be used as a calculator. But Sage is a very *good* calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type `sin(pi/3)` and press [Shift]-[Enter] to see a little of what Sage can do. ︡01bcd70c-ffaa-4ca2-878c-6a5ced3dda14︡{"md":"(You can also click on a gray line to get a *new* Sage cell in which to enter your own calculation. Try it out!)\n\nAs the above example shows, Sage can be used as a calculator. But Sage is a very *good* calculator, which knows about all sorts of mathematical functions and constants. To see an example, let's start a new cell by clicking on a gray line below. Then type `sin(pi/3)` and press [Shift]-[Enter] to see a little of what Sage can do. \n"}︡ ︠3c36ed69-a8a4-474d-a3b4-abd9184adf31s︠ sin(pi/3) ︡2658662a-94de-4831-9d5e-b372cd87bc64︡{"stdout":"1/2*sqrt(3)\n"}︡ ︠b2106466-7dd2-4235-a6e7-3b72766289cei︠ %md That's really good! But if you want to see a numerical approximation to the number $\sin(\pi/3)$, you can use the Sage function `n`. You can also use the symbol `_` to refer to the *last* computation that Sage did. So if the last thing you computed was `sin(pi/3)` above, you can type `n(_)` to find a numerical approximation. (If you have entered something else since then, just type `n(sin(pi/3))` in a new cell.) ︡cbf51c06-4db9-4580-a746-e2bdf11670f5︡{"md":" That's really good! But if you want to see a numerical approximation to the number $\\sin(\\pi/3)$, you can use the Sage function `n`. You can also use the symbol `_` to refer to the *last* computation that Sage did. So if the last thing you computed was `sin(pi/3)` above, you can type `n(_)` to find a numerical approximation. (If you have entered something else since then, just type `n(sin(pi/3))` in a new cell.)"}︡ ︠f37bb280-87ef-4e53-8936-d0298dff4dd2s︠ n(_) ︡583356e4-e3be-448f-8053-671b62eca3ef︡{"stdout":"0.866025403784439\n"}︡ ︠6e52495f-bb22-472e-951b-992a68c3f902i︠ %md If you want more accuracy, you can add the argument `digits` inside the `n` function, like this: ︡4788f0f4-373f-4553-a457-a583e813a9e2︡{"md":"If you want more accuracy, you can add the argument `digits` inside the `n` function, like this:"}︡ ︠dc309eb6-3150-4566-bc0c-ed027f7e1c50s︠ n(sqrt(2), digits=50) ︡5a5ee277-9a97-4f83-aa58-4956e7a26086︡{"stdout":"1.4142135623730950488016887242096980785696718753769\n"}︡ ︠1ffef5ae-6794-4eda-8d7e-0656c57b940di︠ %md Sage is familiar with many functions you know about. A few notes about some special ones: * The inverse trig functions are written as `arcsin`, `arctan`, etc. * The natural logarithm can be written as `ln` or as `log`. If you want a logarithm to another base, such as base 10, specify the base with the `base` argument: `log(x, base=10)`, for example. * The natural exponential function, $e^x$, can be written as `exp(x)`. ︡00bb3d01-2ba1-4cae-b49f-a6330d01ac20︡{"md":"Sage is familiar with many functions you know about. A few notes about some special ones:\n\n * The inverse trig functions are written as `arcsin`, `arctan`, etc.\n * The natural logarithm can be written as `ln` or as `log`. If you want a logarithm to another base, such as base 10, specify the base with the `base` argument: `log(x, base=10)`, for example.\n * The natural exponential function, $e^x$, can be written as `exp(x)`.\n"}︡ ︠4b3603c1-884a-4156-9bd7-775ea89dcf49s︠ log(64, base=2) ︡c2979d7d-a895-42ff-a026-b1a7587b92ae︡{"stdout":"6\n"}︡ ︠4e618176-4dbe-4b95-8e85-7b49de7d0265i︠ %md **Practice** Can you find (to at least six decimal places) the angle which makes $\sin(x)$ equal to 1/3? ︡1ec48fb9-784d-4c4c-92d5-83b6f5c404bf︡{"md":"**Practice** Can you find (to at least six decimal places) the angle which makes $\\sin(x)$ equal to 1/3?\n"}︡ ︠69ee66a3-9150-4243-8105-78b85b4076a4︠ ︡93294790-59f9-422f-bf00-6ffbe2d16f30︡{"md":"## Algebra\n\nSage can do symbolic algebra, but you must first declare any variables you want to use. Let's declare both `x` and `t` to be symbolic variables with the statement below:\n"}︡ ︠4df10e18-57d1-4085-b339-8b9228a407ccs︠ var("x, t") ︡61cbb6e3-70df-4063-99f2-fa80c387cb48︡{"stdout":"(x, t)\n"}︡ ︠db4fed60-8349-427c-adf1-704d90b69477i︠ %md Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication **must** be indicated with a `*`; it cannot be represented by juxtaposition!) ︡c3fbd025-9b06-4676-a4a5-b2cdb31693aa︡{"md":"Here are a few examples of what Sage can do with algebraic expressions: (Note in the examples below, multiplication **must** be indicated with a `*`; it cannot be represented by juxtaposition!)"}︡ ︠0bea58ff-782b-4208-ad00-c50159156096s︠ expand((x + 5)*(2*x - 1)) ︡6ec182c9-bffb-4d71-98a7-de624ddbd101︡{"stdout":"2*x^2 + 9*x - 5\n"}︡ ︠04a5cdc7-167a-42c6-807e-dccc76d3dc76s︠ show(_) ︡e506502f-5760-46c9-bdd6-d93868156388︡{"html":"
$\\displaystyle 2 \\, x^{2} + 9 \\, x - 5$
"}︡ ︠4515e293-15ad-48b7-ae99-47203f9436aes︠ factor(6*x^3 + 26*x^2 - 20*x) ︡b51e8bfd-a77a-40c1-b27f-cd57bde3e8f2︡{"stdout":"2*(3*x - 2)*(x + 5)*x\n"}︡ ︠a10db033-4586-48db-a300-14f195c1a27as︠ factor(17363829) ︡b0edbbe2-67e5-4cd6-9564-3f97e8c70ca3︡{"stdout":"3 * 7 * 826849\n"}︡ ︠cfca4ede-6f32-45b3-8af8-2ec8bd0db5c2s︠ solve( x^2 + x == 6, x) ︡c52af760-dc84-4b1d-9934-7d1583209eb0︡{"stdout":"[x == -3, x == 2]"}︡{"stdout":"\n"}︡ ︠791be916-cb17-4d2f-abda-d6a6ef64a330s︠ p = (x^2 + 3*x + 5)/(x^2 -5*x+6) ︡5bb35847-cc04-4813-bf49-cd03a06f5325︡ ︠29ae49cd-8bf8-4d73-96b8-1ce9f8027fccs︠ p.partial_fraction() ︡88b585cc-aabc-4c35-ae4a-1362ae587cfa︡{"stdout":"-15/(x - 2) + 23/(x - 3) + 1\n"}︡ ︠689697f8-f712-46e6-906e-e3317709879cs︠ show(_) ︡00dc39b7-5f42-4553-be5a-e079d144ec5d︡{"html":"
$\\displaystyle -\\frac{15}{x - 2} + \\frac{23}{x - 3} + 1$
"}︡ ︠1500ae21-69dc-46ff-b395-1aa72ff96b46︠ ︡045927bb-f52c-4380-940f-9e4b0cd839bb︡{"md":"It is also easy to define a simple function, so that you can reuse it many times, if you wish. Here's an example of creating a function $f(x) = x^2 + 2$:"}︡ ︠fa232cb0-24e6-47b5-9b3d-b037791d9203s︠ f(x) = x^2 + 2 ︡5acf02e5-920c-4da6-a151-8dc0c82bcd80︡ ︠c046e9fd-7d67-4464-a667-a080c4797571i︠ %md When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function $f$, if you wish: ︡f0fbfe1c-4208-47b5-a716-9a7511e0620a︡{"md":"When you run the cell above, nothing seems to happen, because all it does is define the function. But now you can use the function $f$, if you wish:"}︡ ︠a7e28afd-0231-4fa6-8fd9-a5c852ddd2f8s︠ f(2) ︡673a630d-cbe2-4c93-983e-2a556086b7d0︡{"stdout":"6\n"}︡ ︠c9fb8bb4-bff7-402c-9c2a-fa374acea81ai︠ %md There are *many* other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more. ︡b1237eb5-e799-409e-89b0-a1cf3e108aeb︡{"md":"There are *many* other types of algebraic problems that Sage can solve. See some of the references at the end if you would like to learn more."}︡ ︠dd931a69-9a34-4fee-ad44-15ec1892c754i︠ %md ## Basic Calculus Sage can also carry out some basic symbolic calculus. Here are a few examples: ︡84ba5ca7-b5ff-4a5d-8d98-48c6853c6e9c︡{"md":"## Basic Calculus\n\nSage can also carry out some basic symbolic calculus. Here are a few examples:\n\n"}︡ ︠745539ad-7bea-4419-a6c0-32c65fc5aa65s︠ var("x") derivative(3*exp(x^2)+log(x), x) ︡413c322b-fabd-48f0-8cdf-9bac09328a63︡{"stdout":"x\n"}︡{"stdout":"6*x*e^(x^2) + 1/x\n"}︡ ︠2adbfcc1-3dae-4766-9cdb-7b4438086fafs︠ derivative(x^2*t^2, x) ︡f6363d23-3ccb-4661-bdaf-669b98208a64︡{"stdout":"2*t^2*x\n"}︡ ︠2ce968b0-f0ad-4d63-9f68-7d6f8d4c05a7s︠ integrate(x^3 + x + 1, x) ︡fa1ec462-928d-403e-a4f3-55c1ddcd7a99︡{"stdout":"1/4*x^4 + 1/2*x^2 + x\n"}︡ ︠01d665db-3eb2-4060-b710-557f9760e4d2s︠ integrate(x, (x, 0, 1)) ︡dd30e8a8-f87a-489a-8631-eab62ac86777︡{"stdout":"1/2\n"}︡ ︠6827f564-4615-45c4-8b27-e8cba406ec5bs︠ taylor(cos(x), x, 0, 7) ︡f4f86c12-d5d9-4842-be7f-ec48c5c108b1︡{"stdout":"-1/720*x^6 + 1/24*x^4 - 1/2*x^2 + 1\n"}︡ ︠78d37829-e234-4b19-a154-8aa88a84257bs︠ show(_) ︡e67b7af9-2530-46f2-80a1-acb85a17637e︡{"html":"
$\\displaystyle -\\frac{1}{720} \\, x^{6} + \\frac{1}{24} \\, x^{4} - \\frac{1}{2} \\, x^{2} + 1$
"}︡ ︠1a28dbef-5cb5-4d40-b135-854312ced7d3︠ ︡7b9ea99e-57fc-461f-8759-968f8c576571︡{"md":"## Plotting\n\nIt is easy to plot a function in Sage. Here is an example of plotting the function $f(x) = 3 x^2 + 2 x - 1$ on the interval $[-2, 2]$:\n"}︡ ︠f73bb3ca-9b70-4a9b-a0d0-8e2f655b4adfs︠ plot(3*x^2+2*x-1, (x, -4, -1)) ︡f0e81ba1-d2b8-456c-9436-07226cd656e1︡{"once":false,"file":{"show":true,"uuid":"b8c0f4da-3e64-489a-a098-bcf8accf2bf0","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_OspE5c.svg"}}︡{"html":"
"}︡ ︠5b49ccd9-123d-4da6-bd4b-b49093b754aai︠ %md Sometimes the graph does not show the sorts of details you would like. Try the following to plot $\tan(x)$ on $[-\pi, \pi]$: ︡b4e7d7f5-16df-495e-b511-cbe2e4485d9f︡{"md":"Sometimes the graph does not show the sorts of details you would like. Try the following to plot $\\tan(x)$ on $[-\\pi, \\pi]$:"}︡ ︠76e5dce1-00a3-4265-ae74-45a9847a8d79s︠ plot(tan(x), (x, -pi, pi)) ︡6e9dcddc-cfe5-415b-8f38-d79d6c8c2186︡{"once":false,"file":{"show":true,"uuid":"b1cdfba1-4a7d-4043-ad24-9925aa22311a","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_6H1Jl2.svg"}}︡{"html":"
"}︡ ︠ede8e3be-4611-4246-bda5-d080508eff9di︠ %md The $y$ values get so big that we cannot see much else. We can add the extra arguments `ymin` and `ymax` to the plot function to fix the window of the plot: ︡019f55e4-8f7d-4873-bd4c-fa3da339e35a︡{"md":"The $y$ values get so big that we cannot see much else. We can add the extra arguments `ymin` and `ymax` to the plot function to fix the window of the plot:"}︡ ︠dcc7f0c0-eb7f-4b86-9c75-8a52202e7b92s︠ plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2) ︡c5d717e7-9d33-4485-9908-ea9ad91da62b︡{"once":false,"file":{"show":true,"uuid":"63f5154f-4040-4a20-8d17-8b3431269203","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_Ui8Qzv.svg"}}︡{"html":"
"}︡ ︠341673e6-4379-453f-8733-5e663ebc3bcci︠ %md By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage "connecting the dots" as it plots the graph. We can adjust the behavior at these points using the `detect_poles` option: ︡894af5be-d5c8-4f72-99ab-1da1e603fd0e︡{"md":"By the way, those vertical lines probably look like Sage is plotting the asymptotes. Actually, it's just the result of Sage \"connecting the dots\" as it plots the graph. We can adjust the behavior at these points using the `detect_poles` option:"}︡ ︠434a09dd-13c1-463c-b30d-e8b0419183aas︠ plot(tan(x), (x, -pi, pi), ymin=-2, ymax = 2, detect_poles="show") ︡332b763a-3196-4664-9676-ab18d9c159ee︡{"once":false,"file":{"show":true,"uuid":"255a2476-26f9-4ae2-af3c-ede53096b571","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_nvD6lU.svg"}}︡{"html":"
"}︡ ︠8311aa15-0715-48bb-b649-f8e051c18331i︠ %md ## 3D Plotting We are more interested in plots of functions of several variables. Here's how to plot a surface: ︡e9e928cb-3813-49f0-8114-0dc4ea8d38c5︡{"md":"## 3D Plotting\n\nWe are more interested in plots of functions of several variables. Here's how to plot a surface:\n"}︡ ︠c858b121-5982-45b2-87e3-a30429c4c74bs︠ var("x, y") plot3d(sin(x*y), (x, 0, 4), (y, 0, 4)) ︡e053f4d8-3b52-4ec5-b7f3-95dcf3ee5907︡{"stdout":"(x, y)\n"}︡{"file":{"uuid":"987b9b2b-adf5-41ea-ac0e-c5fa5e1ade1e","filename":"987b9b2b-adf5-41ea-ac0e-c5fa5e1ade1e.sage3d"}}︡{"html":"
"}︡ ︠71dd30cb-ac83-4b26-bbf3-6985ca4d3eafi︠ %md A useful trick is to plot an *implicit surface*, where a function is equal to a given value. Here we can plot a sphere, by plotting where $x^2 + y^2 + z^2 = 1$: ︡592733f2-35c0-4e9b-910b-f12ca385dc7e︡{"md":"A useful trick is to plot an *implicit surface*, where a function is equal to a given value. Here we can plot a sphere, by plotting where $x^2 + y^2 + z^2 = 1$:\n"}︡ ︠0d0d6b31-a7be-4cd2-aa82-2e03204dda32s︠ var("x, y, z") implicit_plot3d(x^2 + y^2 + z^2, (x, -2, 2), (y, -2, 2), (z, -2, 2), contour = [1]) ︡def2cf0c-e3e1-415f-b4fe-5e6125e29127︡{"stdout":"(x, y, z)\n"}︡{"file":{"uuid":"cb91ec69-073a-4ff4-8fda-a0166b6c17e0","filename":"cb91ec69-073a-4ff4-8fda-a0166b6c17e0.sage3d"}}︡{"html":"
"}︡ ︠5b6eebdd-a7c8-43c8-ae70-3257293edad1i︠ %md Sage is also capable of plotting parametric plots in space, using `parametric_plot3d`: ︡ac3e3239-14a5-4d25-a864-d8fa3d284d56︡{"md":"Sage is also capable of plotting parametric plots in space, using `parametric_plot3d`:"}︡ ︠a6ef742a-9ed5-4949-b1eb-852343fda9c4s︠ var("t") parametric_plot3d((3*cos(t), 3*sin(t), t), (t, 0, 6*pi)) ︡39b7755f-56b4-4b91-9e1d-0e7f68d8efc2︡{"stdout":"t\n"}︡{"file":{"uuid":"09517d0c-d03f-4c20-b449-f60dfb552fe2","filename":"09517d0c-d03f-4c20-b449-f60dfb552fe2.sage3d"}}︡{"html":"
"}︡ ︠d465d685-6cb8-4475-a870-459fc212bab4i︠ %md To combine two plots, just add them: ︡79f61f3f-129b-4aac-8e75-0013adf64c75︡{"md":"To combine two plots, just add them:"}︡ ︠33bcb1af-e77d-4218-9a27-754e3a98b67cs︠ p1 = plot3d(x^2+y^2, (x, -1.2, 1.2), (y, -1.2, 1.2)); p2 = plot3d(1 - y^2, (x, -1.2, 1.2), (y, -1.2, 1.2), color="green"); p1+p2 ︡a64e8d65-c9f1-4b6b-92cc-486bdebf93e2︡{"file":{"uuid":"a7f37902-e35c-4c04-b0c4-73b4fe669bb3","filename":"a7f37902-e35c-4c04-b0c4-73b4fe669bb3.sage3d"}}︡{"html":"
"}︡ ︠ee9c32dd-e6cf-4c63-b892-0e96418a9db2i︠ %md ## Vectors and Matrices Sage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the `vector` function: ︡72f863f8-e307-4802-9e83-5df4e5c4cc77︡{"md":"## Vectors and Matrices\n\nSage is familiar with vectors and vector operations. You can define a vector as a list of numbers using the `vector` function:\n"}︡ ︠edf3dd68-8308-4167-aa6d-8ca1e92dceacs︠ v1 = vector([1, 2, 3]); v2 = vector([3, -1, 1]) ︡db5cfadb-542a-4526-8d07-02a857a1903f︡ ︠1dbddb31-26ab-41e6-9d9e-942bc38a0ad6s︠ show(v1) ︡58943946-6594-416f-970e-3ca643882701︡{"html":"
$\\displaystyle \\left(1,\\,2,\\,3\\right)$
"}︡ ︠1cf88ca8-9e98-43b5-9484-b07650b403f6i︠ %md Sage understands common operations, and even knows about dot and cross products: ︡44e67172-8f3f-49c0-b8ee-9ab1e5c980dd︡{"md":"Sage understands common operations, and even knows about dot and cross products:\n"}︡ ︠dae21806-727a-4c96-952c-2d12a0c7ecc5s︠ v1 + 2*v2 v1.cross_product(v2) v1.dot_product(v2) v1 * v2 ︡cb1f619c-687f-4a9f-b373-81d287c0232f︡{"stdout":"(7, 0, 5)\n"}︡{"stdout":"(5, 8, -7)\n"}︡{"stdout":"4\n"}︡{"stdout":"4\n"}︡ ︠c4a6d93d-6ccb-4e7b-a4ed-0582dbc2d830i︠ %md Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by ``QQ``. Then we enter the matrix as a list of lists. We define a matrix ``m`` ︡d5a3b4df-85a8-4957-bc43-7be45c23c62d︡{"md":"Matrices are created using the matrix command, but you need to tell sage what kinds of entries the matrix should have. We will use rational numbers, indicated by ``QQ``. Then we enter the matrix as a list of lists. We define a matrix ``m``\n"}︡ ︠30e9f8e2-9985-45e5-b8d0-c731712d4194s︠ m = matrix(QQ, [[3, 2, 0], [1, -1, 1], [-1, 1, -5]]); show(m) ︡cdaa5009-146f-4422-bbe0-39189a4e9432︡{"html":"
$\\displaystyle \\left(\\begin{array}{rrr}\n3 & 2 & 0 \\\\\n1 & -1 & 1 \\\\\n-1 & 1 & -5\n\\end{array}\\right)$
"}︡ ︠b63e8146-1604-428f-9df3-b7c756b91b1d︠ %md Sage recognizes many operations we can perform on matrices. act_on_polynomial Here are some you might be familiar with: ︡0993c7d6-3baa-407f-abf8-77a88141ba4f︡ ︠31a8a6a2-8b58-42be-8992-d6479dc1f026s︠ m.rref() ︡e4ed033e-45aa-416c-baba-f23d622001f1︡{"stdout":"[1 0 0]\n[0 1 0]\n[0 0 1]\n"}︡ ︠571a7274-3f48-4a22-8668-b0d911dba492s︠ m.eigenvalues() ︡9fa71bf7-4cfc-4d53-a793-f7c19b7cd3f3︡{"stdout":"[-5.305282147755060?, -1.105324652427052?, 3.410606800182113?]\n"}︡ ︠f8a73952-ee70-4a3f-aa8e-27d91cc7a8adi︠ %md ## Programming Sage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not: ︡21b945c0-7b9f-4865-aef1-88d64515d2ca︡{"md":"## Programming\n\nSage is a full featured programming language. Here is a simple example of a function defined to test if a function is even or not:\n"}︡ ︠f99274ce-d647-486c-aba7-994761549fc2s︠ def iseven(number): if (number % 2 == 0): return true else: return false ︡eedaf506-8c1c-4f82-9bdf-078cc71b0203︡ ︠a12fd5b6-1eec-4a25-8c75-d390bbfb6195s︠ iseven(64.5) ︡c8d7b493-8a83-4256-9d75-5ea18644ed1e︡{"stdout":"False\n"}︡ ︠35375237-c27c-403e-8685-a2ba66d49718i︠ %md If you are familiar with Python, Sage uses the same syntax. ︡da3b8dc9-3b88-45c3-ba9f-46fbd90a6c33︡{"md":"If you are familiar with Python, Sage uses the same syntax.\n"}︡ ︠dd3ede1b-62c3-4fcb-8041-56b7c96cddeeis︠ %md This is text. We can include *italic*, or **bold**. ︡1c84ffe0-c1ed-44f7-816a-8add179eec83︡{"md":"This is text. We can include *italic*, or **bold**.\n"}︡ ︠ab385ce0-d041-4686-a550-a598fb2be7a0i︠ ︡39ddac83-f3c7-431f-9f59-a3340c81aea4︡{"md":"## Writing Text\n\nFinally, to write your responses to some Sage problems, you will want to be able to put in some text explanations. The easiest way to do this is using *markdown*, a simple language for formatted text. To start a markdown cell, put `%md` as the first text in the cell. Then you can type any text you want, and when you evaluate that cell, it will mostly just turn in to text. Here are a few simple formatting commands you can add, if you wish\n\n * To type *italicized* text, surround the words with an asterisk: `*italicized*`\n * To make text **bold**, use two asterisks: `**bold**`\n * Trickier: You can format a polynomial like $f(x) = 3 x^3 - x^2 + 2x + 1$ like this: `$f(x) = 3 x^3 - x^2 + 2x + 1$`\n \n(That last example is very tricky. There is an entire language for mathematical typesetting called $\\LaTeX$ which is being used there. You are not required to use this notation, but you are welcome to try if you wish.) \n\nAll of the text you see in this document is really in a markdown cell. If you double click on one of those cells, you can see exactly how I created that text, if you are interested.\n"}︡ ︠2a0ee8d8-d0c0-4a0a-88b3-eff8cd2b3f96si︠ %md (Alternatively, you can also start a cell with `%html`, and Sage will recognize and render HTML as well.) ︡53255cbe-5c75-4650-a87c-5cc9df4aeb00︡{"md":"(Alternatively, you can also start a cell with `%html`, and Sage will recognize and render HTML as well.)\n"}︡ ︠2f2a80f6-c9d8-4c01-895d-c165118b2bd9i︠ %md ## Other References Here are some useful references if you would like to explore Sage further: * The [Official Sage Tutorial](http://doc.sagemath.org/html/en/tutorial/index.html) * A lengthy (but very good) [Sage Tutorial](http://www-rohan.sdsu.edu/~mosulliv/Teaching/sdsu-sage-tutorial/index.html) at San Diego State University. * Gregory Bard at University of Wisconsin, Stout, has a [page of information about Sage](http://www.gregorybard.com/SAGE.html), including some videos and his new book, *Sage for Undergraduates*. ︡e04cfdbf-f5a9-4105-9931-32d4b5d2e7d4︡{"md":"## Other References\n\nHere are some useful references if you would like to explore Sage further:\n\n * The [Official Sage Tutorial](http://doc.sagemath.org/html/en/tutorial/index.html)\n * A lengthy (but very good) [Sage Tutorial](http://www-rohan.sdsu.edu/~mosulliv/Teaching/sdsu-sage-tutorial/index.html) at San Diego State University.\n * Gregory Bard at University of Wisconsin, Stout, has a [page of information about Sage](http://www.gregorybard.com/SAGE.html), including some videos and his new book, *Sage for Undergraduates*.\n"}︡ ︠cea39f9d-c79f-49fc-b425-15bcfbeb5945s︠ f(x) = ((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3)) table([(x, f(x)) for x in [-.59, -.599, -.5999, -.61, -.601, -.6001, -.60001]], header_row = ("x", "f(x)")) ︡632431db-50b8-4cc9-b811-de9be16ebc78︡{"stdout":" x f(x)\n+--------------------+-------------------+\n -0.590000000000000 -28.4380000000017\n -0.599000000000000 -287.639800000051\n -0.599900000000000 -2879.63998000200\n -0.610000000000000 29.1579999999997\n -0.601000000000000 288.359800000013\n -0.600100000000000 2880.35998001141\n -0.600010000000000 28800.3599988519\n"}︡ ︠86e1f82d-a90f-40f0-b76c-ab6095fad117s︠ f(2) ︡d6af2f12-7520-4156-8eb5-faacf1653356︡{"stdout":"10/13\n"}︡ ︠bdb6497e-d668-4d0b-a9ba-1eb409e76a13s︠ [(x, f(x)) for x in [1, 2, 3]] ︡9cd16287-c630-476a-a9a2-235f78f59444︡{"stdout":"[(1, 1/2), (2, 10/13), (3, 1)]\n"}︡ ︠3f610c45-8b1b-4ebf-8412-e18127ef424ds︠ plot((2*x^3 + 7*x^2 + 3*x)/(10*x^2 + 11*x + 3),(x, -1, 1), ymax=10, ymin=-10) ︡81c3bde6-f5fb-4532-b66c-cb6a0fb4ba9d︡{"once":false,"file":{"show":true,"uuid":"30869320-aac9-43df-8055-0a2fd7070d6d","filename":"/projects/00553573-d2b2-4727-a251-9bda800a9d74/.sage/temp/compute3-us/3777/tmp_lEL07B.svg"}}︡{"html":"
"}︡ ︠d5b4196a-e5f8-47f4-8aef-16c2d0aa0f06︠