Shared01 - Review of Sage Assignment / Review of Sage Notes.sagewsOpen in CoCalc
This material was developed by Aaron Tresham at the University of Hawaii at Hilo and is Creative Commons License
licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Prerequisites:

  • Intro to Sage

Review of Sage

For those of you who had a Calculus 1 lab with me last semester, you are already familiar with Sage. This worksheet is a quick review of some of the key features we covered last semester.

If you have not used Sage before, I recommend working through the Calc 1 lab "Intro to Sage." Then return to this worksheet.

Graphing

You graph a function in Sage using the "plot" command.

Example 1

Graph \displaystyle f(x)=\frac{\sqrt{x^2+9}}{3x^2+2} .

Remember, every multiplication must be explicit in Sage. You must type 3*x^2 (3x^2 will not work).

Also, don't forget the parentheses. They are often required around the numerator and denominator of fractions.

I will give the function a name first, and then I will graph it.

f(x)=sqrt(x^2+9)/(3*x^2+2)  #First, define the function.
plot(f(x))                  #Now make a graph.

It is also possible to plot a function without giving it a name. However, since we usually do more than one thing with our functions, it is usually worth it to define the function first.

plot(sqrt(x^2+9)/(3*x^2+2))

The default plot window uses -1 \le x \le 1 , and Sage choose the range on the y-axis to fit the graph to the window.

If you want to specify a new window, use the xmin, xmax, ymin, and ymax options.

plot(f(x),xmin=-10,xmax=10,ymin=-1,ymax=2)

To graph more than one function, add plots together.

Example 2

Add a graph of g(x)=\sin(\ln(x)) to the graph of f .

Note: the domain of g is x > 0 , so I have set xmin=0 for the plot of g . If you have xmin less than 0, Sage will give you a warning.

g(x)=sin(ln(x))
plot(f(x),xmin=-10,xmax=10,ymin=-1,ymax=2)+plot(g(x),xmin=0,xmax=10,ymin=-1,ymax=2)

To distinguish between the two functions, you can change the color and/or the line style.

For example, to change the color to red, add color='red' to the plot (notice the quotation marks around the color name). Sage knows many colors; feel free to experiment.

To change the line style to dashed, add linestyle='dashed' to the plot (again, notice the quotation marks). You can also use 'dotted' or 'dashdot' instead.

plot(f(x),xmin=-10,xmax=10,ymin=-1,ymax=2)+plot(g(x),xmin=0,xmax=10,ymin=-1,ymax=2,color='red',linestyle='dashed')

For more about graphing, refer to the Calculus 1 lab "Graphing and Solving Equations."

Limits

The "limit" command is used to find limits of functions. To take a limit as x approaches a, you add x=a to the limit command.

Example 3

Find \displaystyle\lim_{x\to 1}\frac{x^2-1}{x-1}

f(x)=(x^2-1)/(x-1)
limit(f(x),x=1)
2

For one-sided limits, add dir='right' or dir='left' (notice quotation marks).

Find the following:

  • \displaystyle\lim_{x\to 1^+}\frac{x^2-1}{x-1}

  • \displaystyle\lim_{x\to 1^-}\frac{x^2-1}{x-1}

limit(f(x),x=1,dir='right') #right limit
limit(f(x),x=1,dir='left')  #left limit
2 2

Example 4

Find \displaystyle\lim_{t\to-4}\frac{t+4}{\sqrt{t+4}}

Any variable other than x has to be "declared." In this example, "%var t" tells Sage that t is a variable.

%var t
f(t)=(t+4)/sqrt(t+4)
limit(f(t),t=-4)
0

For more about limits, refer to the Calculus 1 lab "Limits."

Derivatives

You compute derivatives in Sage using the "derivative" command.

Example 5

Given f(x)=4x^6-8x^3+2x-1 , compute the following:

  • f'(x)

  • f''(x)

f(x)=4*x^6-8*x^3+2*x-1   #Don't forget all the multiplications.
derivative(f(x),x)       #First derivative
show(_)
24*x^5 - 24*x^2 + 2
\displaystyle 24 \, x^{5} - 24 \, x^{2} + 2
derivative(f(x),x,2)     #Second derivative
show(_)
120*x^4 - 48*x
\displaystyle 120 \, x^{4} - 48 \, x

If you want to compute particular values of the derivative, then define a new function equal to the derivative. Sage does not allow f', so I like to call my derivative df, for "derivative of f." You can use any name you want (just don't call it f again).

Example 6

Given f(x)=4x^6-8x^3+2x-1 , compute the following:

  • f'(1)

  • f''(-1)

f(x)=4*x^6-8*x^3+2*x-1
df(x)=derivative(f(x),x) #First, give the derivative function a name.
df(1)                    #Now use this function to calculate the value you want.
2
d2f(x)=derivative(f(x),x,2)  #I call my second derivative d2F
d2f(-1)
168

For more about derivatives, refer to the Calculus 1 lab "Differentiation."

Integrals

To compute an integral in Sage, use the "integral" command. Here is an indefinite integral (antiderivative). This requires two arguments: the function to be integrated and the variable of integration.

Example 7

Given f(x)=4x^6-8x^3+2x-1 , compute \displaystyle\int f(x)\, dx

f(x)=4*x^6-8*x^3+2*x-1
integral(f(x),x)
show(_)
4/7*x^7 - 2*x^4 + x^2 - x
\displaystyle \frac{4}{7} \, x^{7} - 2 \, x^{4} + x^{2} - x

Here is a definite integral. This requires two additional arguments: the lower and upper limits of integration.

Example 8

Given f(x)=4x^6-8x^3+2x-1 , compute \displaystyle\int_{-1}^{1} f(x)\, dx

f(x)=4*x^6-8*x^3+2*x-1
integral(f(x),x,-1,1)
-6/7

Example 9

Compute \displaystyle\int_{-A}^{A} at^2+bt+c\, dt

Don't forget to declare variables first.

%var a,b,c,t,A
integral(a*t^2+b*t+c,t,-A,A)
show(_)
2/3*A^3*a + 2*A*c
\displaystyle \frac{2}{3} \, A^{3} a + 2 \, A c

For more about integrals, refer to the Calculus 1 lab "Symbolic Integration."