Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
483 views

Here we learn about how SAGE handles and thinks of variables, constants, expressions, and formulas. First, the only non-numerical character sage thinks is a variable is the letter, xx. Anything else must be declared as a variable. Note that sage thinks of them as variables, but we might think they are constants--same difference! I will declare y, a, m, b, B, y1, y2, x1 and x2 as variables. Keep in mind that sage is case-sensitive! You will also note the # sign followed by a message in brown. These are called annotations. sage ignores them in its calculation. I add them to help explain what's going on in the code lines.

y, a, m, b, B, y1, y2, x1,x2=var('y, a, m, b, B, y1, y2, x1,x2') #don't forget the ' signs!!! show(y, a, m, b, B, y1, y2, x1,x2)
y\displaystyle y a\displaystyle a m\displaystyle m b\displaystyle b B\displaystyle B y1\displaystyle y_{1} y2\displaystyle y_{2} x1\displaystyle x_{1} x2\displaystyle x_{2}

We use a single = sign for assigning values or names to variables, constants, functions, nearly anything math-related. Here are 2 simple assignment statements where c is given the value 2 and d the value 5. THESE ARE NOT EQUATIONS! Both will remain equal to 2 and 5 within this block of commands.

c=2; d=5 #declaring the assigned values for c and d #doing a few computations. . . c+d c*d c/d c^(1/d) show(d^(1/c))

Any formula you have ever learned can be programmed into your worksheet. Here's two examples from geometry and algebra.

b,h=var('b,h') #the area of a triangle b=4;h=13 Area=b*h/2 Area m=3/4;b=5#the slope is 3/4 and the y-intercept is 5 y=m*x+b #equation of a line y

If you use a variable that hasn't been previously declared you will get a nasty red error message. Ignore all the high powered computer-speak and look at the first and last lines of the error message. The first line tells you what line of your code an error is first detected and the last line tells you what kind of error it is. As you gain experience with sage, you will become more familiar with finding and fixing errors.

show(pi) show(pi*r^2) #note that i never delcared r as a variable!

Here's a block of statements that assigns a name to an equation. Note the double equals sign in the equation itself:

x,y,a,b,c=var('x,y,a,b,c') quadeq=(y==a*x^2+b*x+c) quadeq show(quadeq)
y == a*x^2 + b*x + c
y=ax2+bx+c\displaystyle y = a x^{2} + b x + c

Now formulas are essentially functions of as many variables(constants) as you want. This block creates a formula that calculates the area of a trapezoid. It's actually a function of h, b1 and b2 I then assign values to h, b1 and b2 to get an area.

h,b1,b2=var('h,b1,b2') traparea(h,b1,b2)=h*(b1+b2)/2 show(traparea) traparea(3,5,7)
(h,b1,b2)  12(b1+b2)h\displaystyle \left( h, b_{1}, b_{2} \right) \ {\mapsto} \ \frac{1}{2} \, {\left(b_{1} + b_{2}\right)} h
18

Most of the time you will want to treat your assignment names as functions, specifying as I did above, what variables they are functions of.

Recall above that I used a double equals sign to indicate an equation. Of course we typically solve equations, right? Here are several examples of equations and their solutions. Note the syntax!

x=var('x') #I have to tell SAGE what variable to solve for! solve(3*x+17==32,x)
[x == 5]
x=var('x') show(solve(x^2-3*x+2==0,x))
x,y,m,b=var('x,y,m,b') show(solve(y==m*x+b,x)) show(solve(y==m*x+b,b))
[x=bym\displaystyle x = -\frac{b - y}{m}]
[b=mx+y\displaystyle b = -m x + y]
#this one solves the general triangle area formula for the height. TA,h,b=var('TA,h,b') show(TA==b*h/2) solve(TA==b*h/2,h) show(solve(TA==b*h/2,h))
TA=12bh\displaystyle \mathit{TA} = \frac{1}{2} \, b h
[h == 2*TA/b]
[h=2TAb\displaystyle h = \frac{2 \, \mathit{TA}}{b}]

###Your Turn!!!

  1. Assign the values, 5, 7, 10 to the variables, a1, a2 and a3, respectively. Then find (1)the sum of a1, a2 and a3, (2)the square root of the product of a1, a2 and a3.

  2. Define the function, f(x)=2x+3f(x)=2x+3, then evaluate f(5)f(\sqrt{5})

  3. Define the function, quadsolve, which is the quadratic formula, b+b24ac2a\frac{-b+\sqrt{b^2-4ac}}{2a} that uses the values for 'a', 'b' and 'c'. Use this function to find one of the solutions to the equation, 4x212x+1=04x^2-12x+1=0. Use the 'show' command to print the result.

  4. Use the solve command to let SAGE solve the above quadratic equation for both solutions.

  5. Create and solve the equation, z=barxxsz=\frac{barx-x}{s} for xx. Note that barxbarx is a single variable.

factor(x^2+3*x+2)
(x + 2)*(x + 1)