#!/usr/bin/env python # coding: utf-8 #

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 # # \begin{array}{|l|l|l|} # \hline # \hfill \textbf{Command} \hfill & \hfill \textbf{Description} \hfill & \hfill \textbf{Example} \hfill \\ # \hline # \textit{function}\textbf{?} & \text{Displays the documentation for the function or constant} & \textit{plot}\textbf{?} \\ # \hline # \textbf{plot}(\dots) & \text{Plots the function(s) according to the default options} & \textbf{plot}(x^2, \text{xmin $= -10$, xmax $= 7$, ymin $= 0$, ymax $= 10$, color = 'blue')} \\ # & \text{or the options specified} & \textbf{plot}([x^2, x^3], \text{linestyle = ['dashed', '-.'], # legend_label = }[x^2, x^3])\\ # \hline # \end{array} # # ### Related Course Material # Section 1.4 # ## Example 1 # SageMath has many built in functions, commands, and constants such as $\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. # In[ ]: get_ipython().run_line_magic('pinfo', '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 $\textbf{plot}$ command and look through all of the available options you have to customize your graph of a function. # In[ ]: # We will use this information to plot $\cos(x)$ as an orange dashed line in the viewing window $[-2\pi, 2\pi] \times [-1,1]$. # In[ ]: plot(cos(x), xmin = -2*pi, xmax = 2*pi, ymin = -1, ymax = 1, color = 'orange', linestyle = 'dashed') # ## Example 2 # # Plot the function $f(x) = \sqrt{x-2} + 1$ as a green dotted line with the $x$-range being $2 \leq x \leq 11$. # In[ ]: # Note that if you did not specify a range for the $y$-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) = (x-3)^3 + 2$ from $0 \leq x \leq 6$. Change the line thickness to $2$ (the default is 1), fill in the area between $f(x)$ and the $x$-axis, and add a legend. # In[ ]: # ## Example 4 # # Suppose we wish to plot multiple functions on the same graph. We can do this using $\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) = x^2$ as a green line and the function $g(x) = x^4$ as an orange line. It will plot them on the domain $-1 \leq x \leq 1$, will shade in the area between them, and will add a legend. # In[ ]: 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) = 2\sin(4x), g(x) = 2 + \cos\left(\frac{x}{2}\right),$ and $h(x) = \sin(x)^2$ on the viewing window $[-\pi, \pi] \times [-4, 4].$ Make each function have a different color and different linestyle. Color in the area between $f(x)$ and $h(x)$ and add a legend. # In[ ]: