CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
calculuslab

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: calculuslab/Calculus_Lab
Path: blob/main/141-Labs/Lab 03 - Functions and Plots.ipynb
Views: 491
Kernel: SageMath 9.2

Lab 03 - Functions and Plots

Overview

In your Calculus course, you will encounter many types of functions. The most beneficial way of understanding how a function behaves is to study its graph. In this lab, we will learn how to use SageMath to create graphs of various functions.

Important SageMath Commands Introduced in this Lab

ParseError: KaTeX parse error: Undefined control sequence: \hfill at position 31: …l|l|l|} \hline \̲h̲f̲i̲l̲l̲ ̲\textbf{Command…
Section 1.4

Example 1

SageMath has many built in functions, commands, and constants such as tan(x),expand(),π,\tan(x), \textbf{expand}(\dots), \pi, etc. One way to learn more about these is to read the SageMath documentation. The easiest way to do this in the notebook is to use the ?\textbf{?} command.

tan?

This command opens up a new window at the bottom of the screen filled with useful information and examples on how to use the function or command. Use SageMath to bring up the documentation for the plot\textbf{plot} command and look through all of the available options you have to customize your graph of a function.

We will use this information to plot cos(x)\cos(x) as an orange dashed line in the viewing window [2π,2π]×[1,1][-2\pi, 2\pi] \times [-1,1].

plot(cos(x), xmin = -2*pi, xmax = 2*pi, ymin = -1, ymax = 1, color = 'orange', linestyle = 'dashed')

Example 2

Plot the function f(x)=x2+1f(x) = \sqrt{x-2} + 1 as a green dotted line with the xx-range being 2x112 \leq x \leq 11.

Note that if you did not specify a range for the yy-values, then SageMath chooses the smallest range which will completely fit the graph.

Example 3

Using the documentation as a reference, plot the function f(x)=(x3)3+2f(x) = (x-3)^3 + 2 from 0x60 \leq x \leq 6. Change the line thickness to 22 (the default is 1), fill in the area between f(x)f(x) and the xx-axis, and add a legend.

Example 4

Suppose we wish to plot multiple functions on the same graph. We can do this using plot\textbf{plot} by including lists of functions instead of a single function. Also, if we wish to alter the options for each function in our graph, we can include the options as a list as well.

The code below will plot the function f(x)=x2f(x) = x^2 as a green line and the function g(x)=x4g(x) = x^4 as an orange line. It will plot them on the domain 1x1-1 \leq x \leq 1, will shade in the area between them, and will add a legend.

plot([x^2,x^4], xmin = -1, xmax = 1, color = ['green', 'orange'], fill = [x^4, false], legend_label = [x^2, x^4])

Example 5

On the same graph, plot the functions f(x)=2sin(4x),g(x)=2+cos(x2),f(x) = 2\sin(4x), g(x) = 2 + \cos\left(\frac{x}{2}\right), and h(x)=sin(x)2h(x) = \sin(x)^2 on the viewing window [π,π]×[4,4].[-\pi, \pi] \times [-4, 4]. Make each function have a different color and different linestyle. Color in the area between f(x)f(x) and h(x)h(x) and add a legend.