Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 4666

The units we worked with in the previous worksheet were actually variables, just like xx or yy. 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.

var('y,z, F_1, F_2, m_1, m_2, a_1, a_2') #Declaring some variables to use F_net = F_1 + F_2 F_net A=10 #Don't need to declare variables if you assign values to them B=23 C= A*B #Notice the value of C is not printed when you assign it to A*B C #Now the value of C will be printed in the output below
(y, z, F_1, F_2, m_1, m_2, a_1, a_2) F_1 + F_2 230

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 F1F_1 and F2F_2.

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

pretty_print('The net force is F_net = ', F_net) #pretty_print C= A*B #Notice the value of C is not printed when you assign it to A*B pretty_print('The values of A*B is ',C) pretty_print('You can write a sentence with multiple values like C = ', C, "by separating each variable or string of text with a comma. Thus the net force F_net is ", F_net)
The net force is F_net = F1+F2\displaystyle F_{1} + F_{2}
The values of A*B is 230\displaystyle 230
You can write a sentence with multiple values like C = 230\displaystyle 230 by separating each variable or string of text with a comma. Thus the net force F_net is F1+F2\displaystyle F_{1} + F_{2}

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

#Input your work here

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 f(x)f(x) 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 characters

  • Format is function_name(variable1,variable2,)= equation such as g(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)

var('G') F_g(m_1,m_2,r) = G*m_1*m_2/r^2 pretty_print('The gravitational force is F_g = ', F_g(m_1,m_2,r))
G
The gravitational force is F_g = Gm1m2r2\displaystyle \frac{G m_{1} m_{2}}{r^{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 mEarth=5.97×1024 kgm_{Earth} = 5.97\times10^{24}\ kg

    • The radius of the Earth is rEarth=6.37×106 mr_{Earth} = 6.37\times10^{6}\ m

    • The gravitational constant is G=6.67×1011 Nm2/kg2G = 6.67\times10^{-11}\ N m^2/kg^2

  • 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

#Input your work here

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 rr, we have to input values for GG, m1m_1, and m2m_2. If you only care about the shape of the graph you can input 11 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 function

  • You can substitute as many values as you want, separating each value with a comma

  • To create a plot use the plot() command

    • show() must be used to display the plot on the window

    • You can specify the variable, and minimum and maximum values for plotting

      • e.g. plot(g(x),x,0,1000)

F_plot(r) = F_g(100,5.97*10^24,r).substitute(G=6.67*10^(-11)) #m_1 and m_2 are arguments of the function but G is not pretty_print(F_plot(r)) p = plot(F_plot(r),r,6*10^6,10^8) #'p' is the name we give to this plot. Plotting from surface of Earth out to 10^8 m from center show(p) #Display graph named 'p' on the screen
3.98199000000000×1016r2\displaystyle \frac{3.98199000000000 \times 10^{16}}{r^{2}}

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, another q, and a third plot r, then type s=p+q+r and then show(s) to plot all three

  • Add title='MyTitle' to add MyTitle` as the name of the graph

  • You 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=[] command

  • To create a lengend for your graph, specify the name of each plot using legend_label=

k_1=3 k_2=7 g(x)= 1/2*k_1*x^2 h(x)=1/2*k_2*x^2 p1=plot(g(x),x,0,10,title='Potential Energy of Two Springs', color='red',axes_labels=['Position x','Energy'],legend_label='k_1') p2=plot(h(x),x,0,10,color='green',legend_label='k_2') P=p1+p2 show(P)

For You To Try

  • Plot the gravitational potential energy using Ugrav=mghU_{grav} = m g h 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

#Input your work here