This page represents a collection of notes and tutorials for common functions, methods, definitions, etc. used in our class. This is not comprehensive and is intended as an aid for the course. For additional help, use the SAGE cheat sheet, SAGE tutorial, or stackoverflow.

There are many ways to define functions in SAGE. The two main methods are:

- defining a function explictly in terms of variables or
- defining a variable and then using it in a function formula.

In [1]:

# first method f(x) = x^2+3

In [2]:

show(f)

$x \ {\mapsto}\ x^{2} + 3$

In [3]:

# second method y = var('y') g = sin(y)

In [4]:

show(g)

$\sin\left(y\right)$

Note that the `show`

command simply produces a nicely formated output using $\LaTeX.$

There are several ways to differentiate a function using SAGE. If there are multiple variables, you will have to explicitly state the variable you are differentiating with respect to.

In [5]:

# as a method f.diff()

x |--> 2*x

In [6]:

# as a function derivative(f)

x |--> 2*x

In [7]:

# for multiple variables h(x,y) = x^2+y h.diff(y)

(x, y) |--> 1

The function `solve`

has the format: `solve(list of desired equations, variables to solve for {comma separated, not a list})`

.

In [8]:

# single variable example solve(3*x+1==0,x)

[x == (-1/3)]

In [9]:

# multiple variable example solve([x==y,x^2+y^2==1],x,y)

[[x == -1/2*sqrt(2), y == -1/2*sqrt(2)], [x == 1/2*sqrt(2), y == 1/2*sqrt(2)]]

The output of the solve command is a list where each element is a solution to the set of equations. Any expression of the form `variable == function/formula/number`

is a symbolic expression object in SAGE. The right hand side of the equation given by this object can be extracted using the `rhs`

method. (See the example below.)

In [10]:

# the sol varible below is the list of solutions sol = solve([x==y,x^2+y^2==1],x,y)

In [11]:

show(sol)

$\left[\left[x = -\frac{1}{2} \, \sqrt{2}, y = -\frac{1}{2} \, \sqrt{2}\right], \left[x = \frac{1}{2} \, \sqrt{2}, y = \frac{1}{2} \, \sqrt{2}\right]\right]$

Our system of equations has two solutions. We will extract the $y$ values of the first solution. (Note that you do not need to use all of the code below to accomplish this task. Here we show every intermediate step so as not to confuse.)

In [12]:

# remember that python/SAGE uses zero as the first index firstSol = sol[0]

In [13]:

show(firstSol)

$\left[x = -\frac{1}{2} \, \sqrt{2}, y = -\frac{1}{2} \, \sqrt{2}\right]$

In [14]:

yEquation = firstSol[1]

In [15]:

show(yEquation)

$y = -\frac{1}{2} \, \sqrt{2}$

In [16]:

yEquation.rhs()

-1/2*sqrt(2)

In [17]:

# single line version of the above work show(sol[0][1].rhs())

$-\frac{1}{2} \, \sqrt{2}$

The sensitivity of $y$ with respect to $x$ is a measure for the percentage change in $y$ given a percentage change in $x$. It is a unitless quantity defined by $S(y,x)=\dfrac{dy}{dx}\cdot\dfrac{x}{y}.$ If $S(y,x)$ is close to 0, $y$ is not sensitive with respect to $x$. A negative $S(y,x)$ means that $y$ and $x$ have an inverse relationship (as $x$ goes up, $y$ goes down and vice versa).

In [18]:

# Note that we use X and Y instead of x and y to avoid problems with the definition of x and y in previous lines Y(X) = X^2+sin(X)

In [19]:

S(X) = Y.diff()*X/Y

In [20]:

# The method n is for numerical evaluations S(2).n()

1.46002690481524

The above shows that, when $X=2$, if $X$ increases by 1%, then $Y$ increases by 1.46%.

A plot in CoCalc is an object with various attributes. Graphics objects can be added together to produce multiple function plots on a single graph. Plots have many optional arguments that can add color, axes labels, and legends. Note that labels can be written in $\LaTeX.$ Later on we will combine other types of plots together (like `list_plot`

) and use the `aspect_ratio`

optional argument to change how plots are displayed. These graphical objects are built on matplotlib, a module commonly used for plotting by data scientists that use Python.

In [21]:

p1 = plot(x^2,(x,-2,2),axes_labels=['$x$','$y$'],legend_label='$x^2$') p2 = plot(x,(x,-2,2),color='red',legend_label='$x$') p1+p2