CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/141-Labs/Lab 01 - New Users Tour.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 01 - New User's Tour

To take the tour, click in each executable cell, the ones with In [ ]:\text{In [ ]:} beside them, type the appropriate code, and execute the cell. You execute a cell by pressing Shift + Enter\textbf{Shift + Enter}. Note that simply pressing Enter\textbf{Enter} will go to the next line in the executable cell and will not execute the code.

SageMath is a free, open-source math software that supports research and teaching in algebra, geometry, number theory, cryptography, numerical computation, and related areas. SageMath is based on the programming language Python. We will use SageMath to perform calculations and give insight into the many topics covered in Calculus.

SageMath as a Calculator

SageMath's most basic function is to act as a conventional calculator. For example, we can use SageMath to simplify the expression 3(2+12)21523\frac{3(2+\frac{1}{2})^2}{\frac{1}{5} - \frac{2}{3}} We can simply input the expression into an executable cell exactly as we would a calculator. One thing to be careful about in SageMath is you must use *\textbf{*} to represent multiplication.

(3*(2+1/2)^2)/(1/5 - 2/3)

By default, SageMath does not display the output in a "nice" way. You can make SageMath display the output in a more convenient manner by using the show\textbf{show}(\dots) command.

show((3*(2+1/2)^2)/(1/5 - 2/3))

Since the expression only contains rational numbers, SageMath gives the output as a rational number. If we wanted a decimal approximation of the output, we could use the round(exp, n)\textbf{round(exp, n)} command, where nn is the number of decimal places we want displayed.

round(-1125/28, 10)

A second way in which we could get a decimal approximation for a rational expression is to make at least one of the numbers a floating-point number. This means, make one of the numbers a decimal.

(3.0*(2+1/2)^2)/(1/5 - 2/3)

Predefined Functions and Constants

SageMath has many of the common math functions and constants predefined. Most of these can be referenced by their normal notation. Note how SageMath has no issue calculating 100\sqrt{100}, 543\sqrt[3]{54}, cos(π)\cos(\pi) and arcsin(12)\arcsin\left(\dfrac{1}{2}\right).

sqrt(100)
(54)^(1/3)
cos(pi)
arcsin(1/2)

SageMath gave exact answers for 543\sqrt[3]{54} and arcsin(12).\arcsin\left(\frac{1}{2}\right). In the two cells below, use the ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{show(\dotsParseError: KaTeX parse error: Expected 'EOF', got '}' at position 2: )}̲ command to better display the output of these two calculations.

If you wish to add more cells to the notebook, simply click the +\textbf{+} button in the toolbar above. You can also click on Insert\textbf{Insert} in the toolbar above and choose either Insert Cell Above\textbf{Insert Cell Above} or Inserct Cell Below\textbf{Inserct Cell Below}. This will insert a cell either above or below the current selected cell. By default, this will add an executable cell. Go ahead and add two new executable cells and use these cells to approximate the values of 543\sqrt[3]{54} and arcsin(12)\arcsin\left(\frac{1}{2}\right) to 5 decimal places.

You can also add a Markdown cell to the notebook. To do this, simply add a new cell and change the type from Code to Markdown. A Markdown cell is a cell which allows text. In addition to text, you can type in HTML and LaTeX code and the cell will compile this code into text. If you do not know HTML or LaTeX, then you can use the Markdown cells plainly as a cell for basic text. Use the Insert\textbf{Insert} button to add a Markdown cell at the very beginning of the document. In the cell, type your name, your section number, and any other information your TA would like you to include in your lab submission.

Assigning Expressions to Names

SageMath allows us to assign an expression to a name so that it can easily be referenced throughout the entire worksheet. For example, we can assign π2\dfrac{\pi}{2} to the name aa by doing the following.

a = pi/2

Note that SageMath supresses the output whenever you are making an assignment. We can check that aa really has been assigned to π2\dfrac{\pi}{2} by entering into an input cell by itself.

a

Now, whenever we use aa throughout the entire worksheet, it will be referring to π2\dfrac{\pi}{2}. If we wanted to reassign aa to something else, all we have to do is set it equal to the new value.

a = pi/3 a

Note that this time, SageMath did display the new value of aa. This is because we both assigned aa to its new value and told SageMath to return aa in the same input cell. SageMath allows multiple lines of code in the same cell and will execute all lines, however, it will only display the last line of code.

a a + 1 a + 2

If you wish to display mutliple lines, you can use the print()\textbf{print}(\dots) command.

print(a) print(a + 1) print(a + 2)

Caution:\textbf{Caution:} Be very careful about the names you choose for your assignment variables. SageMath will allow you to use a name that it has already defined and it does not\textbf{not} warn you that you are overwriting a predefined command. For example, if you do the assignment sin = 5, then the next time you use sin, it will think you mean 5 instead of the trig function. In order to recover the trig function sin, you need to reset SageMath by using the command reset().\textbf{reset}(). This will clear all variables and functions you previously assigned, so you will need to re-execute those codes to reassign the variables and functions.

Creating Functions

We can also create functions in SageMath. For example, in order to create the function f(x)=x2f(x) = x^2, we do the following.

def f(x): return x^2

Simlilar to assignments, SageMath will not display any output when creating a function. It is good practice to call the function after creating it in order to verify that you created the intended function.

def f(x): return x^2 f(x)

Now that we have f(x)f(x) defined, we can evaluate the function at different inputs.

f(4)
f(x) + 4
f(f(x))

What if we wanted to use the variable tt in place of xx?

f(t)

SageMath gives an error saying that tt is not defined. The only variable that SageMath has predefined is xx. If we want to use other variables, such as tt, then we will need to make tt a variable by using the var\textbf{var} command.

var('t')

Now, SageMath will have no problem evaluating f(t)f(t).

f(t)

Rather than just doing normal calculations, we can also have SageMath perform calculus.

var('h') limit((f(x+h) - f(x))/h,h=0)
diff(f(x))
diff(f(x),2)
integrate(f(x),x)

Plotting

The last part of SageMath which we will explore in this tour is its ability to plot graphs of functions. First, use the cell below to create the function g(x)=sin(x)x.g(x) = \dfrac{\sin(x)}{x}.

Now, plot g(x)g(x) by using the function plot(g(x))\textbf{plot(g(x))}.

plot(g(x))

The function plot()\textbf{plot()} can take inputs other than just g(x)g(x). The other inputs allow you to change specific characteristics about the graph of the funciton such as the xx-range and yy-range, the color, the linestyle, etc. To see a detailed description about the plot function, or about any function, simply follow its name with ?\textbf{?} and execute the cell.

plot?

We can run the following code to display the graph of g(x)g(x) in a window that goes from [5,5][-5,5] on the xx-axis and [1,1][-1,1] on the yy-axis, changes the graph color to red, and changes the linestyle to dashed.

plot(g(x),xmin = -5, xmax = 5, ymin = -1, ymax = 1, color = 'red', linestyle = 'dashed')

SageMath also has the ability to graph multiple functions at once. When plotting multiple functions at the same time, surround them in brackets. Also, you can change properties for each function by also surrounding the properties in brackets. The following command plots both g(x)g(x) and its derivative g(x)g'(x) from [5,5][-5,5] on the xx-axis and [1,1][-1,1] on the yy-axis. It also plots g(x)g(x) in green with dashes and g(x)g'(x) in orange with dots. Additonally, we add a legend to distinguish between the two functions in the graph.

plot([g(x), diff(g(x))], xmin = -5, xmax = 5, ymin = -1, ymax = 1, color = ['green', 'orange'], linestyle = ['dashed', 'dotted'], legend_label = ['g(x)', "g'(x)"])

Converting Notebook to PDF for Submission

Once you are done with a lab, you will submit a PDF version of the notebook to Blackboard for your TA to grade. The easiest way to convert the Notebook file to a PDF file is to choose ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{File \rightarrowParseError: KaTeX parse error: Expected 'EOF', got '}' at position 15: Print Preview}̲ from the toolbar at the top of the page. Then, press ParseError: KaTeX parse error: Unexpected end of input in a macro argument, expected '}' at end of input: \textbf{CTRL +ParseError: KaTeX parse error: Expected 'EOF', got '}' at position 3: P}̲ and choose to Save as PDF\textbf{Save as PDF}. You can upload the PDF to Blackboard.

Note: These instructions may differ depending on Web Browser and Operating System.