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.
The units we worked with in the previous worksheet were actually variables, just like or . In SageMath, all variables must be either assigned a numerical value or declared to be a variable using the var()
command.
Defining variables and functions
All variables must be explicitly declared (except for the variable 'x' and variables used as arguments of functions) using the var() command
Variables cannot start with numbers or contain spaces but they can contain underscores
Functions can be evaluated by inserting values for the arguments
To evaluate an input cell in this worksheet, put the cursor in the cell and hit Shift+Enter. The output will be displayed in the cell below the input cell.
To output text you can use the pretty_print()
command. This uses LaTeX to typeset the equations and make them easier to read. Use pretty_print()
any time you want text to show up in the output cell. Notice that the variables F_1 and F_2 are displayed as and .
Outputting Pretty Text
Use
pretty_print()
Everything to be printed should be inside parentheses
Text should be enclosed with quotation marks (either ' or " works)
Do display the value of a variable or function, do not enclose variable inside quotes
You can string quoted text and variables together by separating each part with a comma
For You To Try
Define the variables F_3, F_4, F_5, and F_6
Set F_net equal to the sum of all six forces
Use pretty_print to write out a sentence about the net force and print out the value of F_net
You can define functions in SageMath by giving the function a name and specifying the arguments of the function in parentheses after the function name. For example, to define a function you might type f(x)=3*x^2+2*x-1
. You can also have functions of multiple variables such as h(x,y,z)=3*x*y-z^3
Defining Functions
Function names follow same convention as variables; Must start with a letter, can contain numbers, can't contain spaces (only underscores
_
), and no special charactersFormat is
function_name(variable1,variable2,)= equation
such asg(a,b)=2*a+3*b
Functions can be evaluated at specific points by entering numbers in for the arguments such as
g(1,2)
For You To Try
Calculate the force exerted on a human while on the surface of the Earth.
The mass of the Earth is
The radius of the Earth is
The gravitational constant is
Calculate the force exerted on the same human while on the international space station which is roughly 400 km above the surface of the Earth
You can plot functions in SageMath but all variables except the plotting variable must have a value. If we want to plot the gravitational force vs. separation distance , we have to input values for , , and . If you only care about the shape of the graph you can input for all other variables. In this case we'll use the numbers for a human and the Earth.
Plotting Functions and Substitutions
To substitute a numerical value in for a variable, add
.substitute()
to the end of the functionYou can substitute as many values as you want, separating each value with a comma
To create a plot use the
plot()
commandshow()
must be used to display the plot on the windowYou can specify the variable, and minimum and maximum values for plotting
e.g.
plot(g(x),x,0,1000)
You can plot more than one function on a graph. You can also add labels to the graphs and change the line used to make the graphs. For this example we'll plot the potential energy of two different springs on the same graph.
Plotting Functions and Substitutions
To add multiple plots to a single graph, assign plots to variable names and add them together
e.g. If you call one plot
p
, anotherq
, and a third plotr
, then types=p+q+r
and thenshow(s)
to plot all three
Add
title='MyTitle' to add
MyTitle` as the name of the graphYou can change the color of a plot by specifying its color. Color names must be enclosed in quotes
e.g.
color='black'
You can name the x-axis and y-axis by using the
axes_labels=[]
commandTo create a lengend for your graph, specify the name of each plot using
legend_label=
For You To Try
Plot the gravitational potential energy using for three different objects
Label each object and create a legend
Label your axes
Include a title on your graph
Each plot should be a different color