Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Notes and Tutorials
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.
Defining functions using SAGE
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.
Note that the show
command simply produces a nicely formated output using
Differentiating functions in SAGE
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.
Solving an equation in SAGE
The function solve
has the format: solve(list of desired equations, variables to solve for {comma separated, not a list})
.
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.)
Our system of equations has two solutions. We will extract the 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.)
How to compute the sensitivity of with respect to
The sensitivity of with respect to is a measure for the percentage change in given a percentage change in . It is a unitless quantity defined by If is close to 0, is not sensitive with respect to . A negative means that and have an inverse relationship (as goes up, goes down and vice versa).
The above shows that, when , if increases by 1%, then increases by 1.46%.
Graphing in SAGE
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 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.
Finding Roots Numerically
The solve
command cannot always find roots. The find_root
method can find roots numerically, but an interval must be supplied. In the example below, we find a root of .
Solving a System of Equations
The solve
command can find solutions to systems of equations. We will find the solution to the following system of equations:
3D Plotting
Plotting 3D surfaces in a Jupyter notebook can cause issues. There are two alternatives that you can use:
Use a Sage worksheet to plot in 3D.
Use the
viewer='tachyon'
option when plotting in 3D in a Jupyter notebook. Unfortunately, this option renders a static 3D image.
Finding Max/Mins of a Function in a Constrained Optimization Problem
Suppose you want to maximize/minimize subject to some constraints . Perform the following steps:
Draw the feasible region (if possible).
Look for internal critical points ().
Use the method of Lagrange multipliers to find critical points on the boundary of the feasible region ().
Test all candidates from parts 2 and 3.
Monte Carlo Methods
The random()
function in CoCalc produces uniformly distributed values on the interval . By multiplying and adding the appropriate values, you can modify this function to produce numbers in any given interval. (Note: Other Python modules like numpy have more sophisticated function for generating random data.)
Newton's Method
Sometimes we are interested in finding the solution to the following system of equations:
Let and . We can use Newton's Method here to approximate the solution using an iterative procedure.
Use a graphical method to determine a point relatively close to the location of the true solution.
Compute the Jacobian given by
Let where is the inverse of . Note also that must be recomputed at each step.
Loading SAGE code from another file
Use the load()
function to import SAGE code from a *.sage file into another file (like a Jupyter notebook).