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.
[[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.)
# the sol varible below is the list of solutionssol=solve([x==y,x^2+y^2==1],x,y)
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.)
# remember that python/SAGE uses zero as the first indexfirstSol=sol
# single line version of the above workshow(sol.rhs())
How to compute the sensitivity of y with respect to x¶
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)=dxdy⋅yx. 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).
# Note that we use X and Y instead of x and y to avoid problems with the definition of x and y in previous linesY(X)=X^2+sin(X)
# The method n is for numerical evaluationsS(2).n()
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.
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.)