{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# Notes and Tutorials\n",
"\n",
"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](https://wiki.sagemath.org/quickref), SAGE [tutorial](http://doc.sagemath.org/html/en/tutorial/index.html), or [stackoverflow](https://stackoverflow.com/).\n",
"\n",
"---\n",
"\n",
"## Defining functions using SAGE\n",
"\n",
"There are many ways to define functions in SAGE. The two main methods are:\n",
"\n",
"* defining a function explictly in terms of variables or\n",
"* defining a variable and then using it in a function formula."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# first method\n",
"f(x) = x^2+3"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 2,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"show(f)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# second method\n",
"y = var('y')\n",
"g = sin(y)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 4,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"show(g)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"Note that the show
command simply produces a nicely formated output using $\\LaTeX.$"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Differentiating functions in SAGE\n",
"\n",
"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."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x |--> 2*x"
]
},
"execution_count": 5,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# as a method\n",
"f.diff()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"x |--> 2*x"
]
},
"execution_count": 6,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# as a function\n",
"derivative(f)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"(x, y) |--> 1"
]
},
"execution_count": 7,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# for multiple variables\n",
"h(x,y) = x^2+y\n",
"h.diff(y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Solving an equation in SAGE\n",
"\n",
"The function solve
has the format: solve(list of desired equations, variables to solve for {comma separated, not a list})
."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[x == (-1/3)]"
]
},
"execution_count": 8,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# single variable example\n",
"solve(3*x+1==0,x)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[x == -1/2*sqrt(2), y == -1/2*sqrt(2)], [x == 1/2*sqrt(2), y == 1/2*sqrt(2)]]"
]
},
"execution_count": 9,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# multiple variable example\n",
"solve([x==y,x^2+y^2==1],x,y)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"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.)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# the sol varible below is the list of solutions\n",
"sol = solve([x==y,x^2+y^2==1],x,y)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 11,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"show(sol)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"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.)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# remember that python/SAGE uses zero as the first index\n",
"firstSol = sol[0]"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 13,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"show(firstSol)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"yEquation = firstSol[1]"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 15,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"show(yEquation)"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-1/2*sqrt(2)"
]
},
"execution_count": 16,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"yEquation.rhs()"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/html": [
""
]
},
"execution_count": 17,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# single line version of the above work\n",
"show(sol[0][1].rhs())"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## How to compute the sensitivity of $y$ with respect to $x$\n",
"\n",
"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)."
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"# 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\n",
"Y(X) = X^2+sin(X)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"S(X) = Y.diff()*X/Y"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.46002690481524"
]
},
"execution_count": 20,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"# The method n is for numerical evaluations\n",
"S(2).n()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"The above shows that, when $X=2$, if $X$ increases by 1%, then $Y$ increases by 1.46%."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Graphing in SAGE"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"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](https://matplotlib.org/), a module commonly used for plotting by data scientists that use Python."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "d5793f1e21635b970ba902445e6b07d3f6e659f7"
},
"execution_count": 21,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"p1 = plot(x^2,(x,-2,2),axes_labels=['$x$','$y$'],legend_label='$x^2$')\n",
"p2 = plot(x,(x,-2,2),color='red',legend_label='$x$')\n",
"p1+p2"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Finding Roots Numerically\n",
"\n",
"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 $f(x)=x^2-2$."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"1.4142135623731364"
]
},
"execution_count": 22,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"eq = x^2-2 == 0\n",
"eq.find_root(0,2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Solving a System of Equations\n",
"\n",
"The solve
command can find solutions to systems of equations. We will find the solution to the following system of equations:\n",
"\n",
"$$x+y=3$$\n",
"\n",
"$$x-y=5$$"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"y = var('y')\n",
"sol = solve([x+y==3,x-y==5],x,y)"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"[[x == 4, y == -1]]"
]
},
"execution_count": 24,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"sol"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"4"
]
},
"execution_count": 25,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"sol[0][0].rhs()"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"-1"
]
},
"execution_count": 26,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"sol[0][1].rhs()"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "e83e96b74f3f5d4d9664c23d94f2c8238d34f4d7"
},
"execution_count": 27,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"contour_plot(x+sin(y),(x,-2,2),(y,-2,2))"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "fa0815b56c6fb4bcbe6dd5e21eef2b12f6cbe83c"
},
"execution_count": 28,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"implicit_plot(x^2+y^2==1,(x,-2,2),(y,-2,2))"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## 3D Plotting\n",
"\n",
"Plotting 3D surfaces in a Jupyter notebook can cause issues. There are two alternatives that you can use:\n",
"\n",
"1. Use a Sage worksheet to plot in 3D.\n",
"2. Use the viewer='tachyon'
option when plotting in 3D in a Jupyter notebook. Unfortunately, this option renders a static 3D image."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "82f58816ae03577fd883f8915849d9b6bfeda043"
},
"execution_count": 29,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"p = plot3d(x^2+sin(y),(x,-2,2),(y,-2,2))\n",
"show(p,viewer='tachyon')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Finding Max/Mins of a Function in a Constrained Optimization Problem\n",
"\n",
"Suppose you want to maximize/minimize $f(x_1,x_2,\\dots,x_n)$ subject to some constraints $g_1(x_1,x_2,\\dots,x_n)\\leq c_1, g_2(x_1,x_2,\\dots,x_n)\\leq c_2, \\dots, g_k(x_1,x_2,\\dots,x_n)\\leq c_k$. Perform the following steps:\n",
"\n",
"1. Draw the feasible region (if possible).\n",
"2. Look for internal critical points ($\\nabla f=0$).\n",
"3. Use the method of Lagrange multipliers to find critical points on the boundary of the feasible region ($\\nabla f = \\lambda_1\\nabla g_1+\\lambda_2\\nabla g_2+\\dots+\\lambda_k\\nabla g_k$).\n",
"4. Test all candidates from parts 2 and 3. "
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Monte Carlo Methods\n",
"\n",
"The random()
function in CoCalc produces uniformly distributed values on the interval $[0,1)$. 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.)"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"text/plain": [
"0.17160677354490017"
]
},
"execution_count": 30,
"metadata": {
},
"output_type": "execute_result"
}
],
"source": [
"random()"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Newton's Method\n",
"\n",
"Sometimes we are interested in finding the solution to the following system of equations:\n",
"\n",
"\\begin{align*}\n",
"f_1(x_1,x_2,\\dots,x_n) &= 0\\\\\n",
"f_2(x_1,x_2,\\dots,x_n) &= 0\\\\\n",
"\\vdots \\\\\n",
"f_n(x_1,x_2,\\dots,x_n) &= 0\\\\\n",
"\\end{align*}\n",
"\n",
"Let $\\vec{x}=(x_1,x_2,\\dots,x_n)$ and $F(\\vec{x})=(f_1(\\vec{x}),f_2(\\vec{x}),\\dots,f_n(\\vec{x}))$. We can use Newton's Method here to approximate the solution using an iterative procedure.\n",
"\n",
"1. Use a graphical method to determine a point $\\vec{x}_0$ relatively close to the location of the true solution.\n",
"2. Compute the Jacobian $A$ given by $$A:=A(\\vec{x})=\\begin{bmatrix}\\frac{df_1}{dx_1} & \\frac{df_1}{dx_x} & \\cdots & \\frac{df_1}{dx_n}\\\\ \\frac{df_2}{dx_1} & \\frac{df_2}{dx_x} & \\cdots & \\frac{df_2}{dx_n}\\\\ \\vdots & \\vdots & \\ddots & \\vdots \\\\\\frac{df_n}{dx_1} & \\frac{df_n}{dx_x} & \\cdots & \\frac{df_n}{dx_n}\\end{bmatrix}$$\n",
"3. Let $$\\vec{x}_{n+1} = \\vec{x}_n - A^{-1}F(\\vec{x}_n)$$ where $A^{-1}$ is the inverse of $A$. Note also that $A$ must be recomputed at each step."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"## Loading SAGE code from another file\n",
"\n",
"Use the load()
function to import SAGE code from a \\*.sage file into another file (like a Jupyter notebook)."
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {
"collapsed": false
},
"outputs": [
],
"source": [
"load('/home/user/linProg.sage')"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": false
},
"source": [
"# That is all for now."
]
}
],
"metadata": {
"kernelspec": {
"display_name": "SageMath 8.3",
"language": "sagemath",
"metadata": {
"cocalc": {
"description": "Open-source mathematical software system",
"priority": 1,
"url": "https://www.sagemath.org/"
}
},
"name": "sage-8.3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
}
},
"nbformat": 4,
"nbformat_minor": 0
}