Skip to main content
\(\newcommand{\Z}{\mathbb{Z}} \newcommand{\reals}{\mathbb{R}} \newcommand{\real}[1]{\mathbb{R}^{#1}} \newcommand{\fe}[2]{#1\mathopen{}\left(#2\right)\mathclose{}} \newcommand{\cinterval}[2]{\left[#1,#2\right]} \newcommand{\ointerval}[2]{\left(#1,#2\right)} \newcommand{\cointerval}[2]{\left[\left.#1,#2\right)\right.} \newcommand{\ocinterval}[2]{\left(\left.#1,#2\right]\right.} \newcommand{\point}[2]{\left(#1,#2\right)} \newcommand{\fd}[1]{#1'} \newcommand{\sd}[1]{#1''} \newcommand{\td}[1]{#1'''} \newcommand{\lz}[2]{\frac{d#1}{d#2}} \newcommand{\lzn}[3]{\frac{d^{#1}#2}{d#3^{#1}}} \newcommand{\lzo}[1]{\frac{d}{d#1}} \newcommand{\lzoo}[2]{{\frac{d}{d#1}}{\left(#2\right)}} \newcommand{\lzon}[2]{\frac{d^{#1}}{d#2^{#1}}} \newcommand{\lzoa}[3]{\left.{\frac{d#1}{d#2}}\right|_{#3}} \newcommand{\abs}[1]{\left|#1\right|} \newcommand{\sech}{\operatorname{sech}} \newcommand{\csch}{\operatorname{csch}} \newcommand \dd[1] { \,\textrm d{#1} } \newcommand \de[2] { \frac{\mathrm d{#1}}{\mathrm d{#2}} } \newcommand \intl[4]{ \int\limits_{#1}^{#2}{#3}\dd{#4} } \newcommand\at[2]{\left.#1\right|_{#2}} \newcommand{\lt}{ < } \newcommand{\gt}{ > } \newcommand{\amp}{ & } \)

Section3.3Derivatives

Here are some basic examples to give a quick overview (without explanation) of how SageMath can be helpful for calculating derivatives:

The following examples are taken from Essential Calculus - James Stewart, Chapter 2 [3.10.1].

All the examples,and more, can been found in this (public) Cloud SageMath Worksheet.

Some of the examples from that worksheet are recreated here.

Subsection3.3.1Examples From Chapter 2

Subsubsection3.3.1.1Section 2.1

Section 2.1, Exercise 25 - 30, Page 82

Section 2.1, Exercise 34, Page 82

Section 2.1, Exercise 49, Page 83

Section 2.1, Exercise 50, Page 83

Subsubsection3.3.1.2Section 2.2

Section 2.2, Exercise 44, Page 94

Subsubsection3.3.1.3Section 2.3

Section 2.3, Exercises 29, Page 105

Section 2.3, Exercises 30, Page 105

Subsubsection3.3.1.4Section 2.4

Section 2.4, Exercises 27, Page 112

We have performed the same action - plot a graph and its tangent line - several times now. Time to encapsulate it in our own function! Run the next sage cell example to define the function plot_tangentline.

This is a Python function that takes three arguments:

  1. \(f\) the function we want to plot.
  2. \(pt\) the point on the function for which we want to draw the tangent line.
  3. \(xrange\) the range of x coordinates that we want the graph to span.

Running the next sage cell will not produce any visible results but will just stores this user defined method in memory for use in other cells. The next few examples make use of this user defined function to save a bit of typing.

Remember to run this before running any other Sage Cell that makes use of it.

Definition3.3.1Utility method plot_tangentline()

Redo the previous example using our method:

Now wasn't that a lot easier! No for the rest.

Section 2.4, Exercises 28 - 31, Page 112

Section 2.4, Exercise 36, Page 113, Nth Derivative of a function

The second argument to the diff method is the derivative order: 1 for 1st derivative, 2 for second derivative, etc.

Section 2.4, Exercise 52, Page 114

This demonstrates the use of some predefined names: omega and mu. These will get formatted to \(\omega\) and \(\mu\) when you use show(). Find out about LaTex to see what the other possibilities are, for example, xi for \(\xi\) and Xi for uppercase \(\Xi\), and so on.

Subsection3.3.2Section 2.6 Implicit Differentiation

Section 2.6, Example 2 - The Folium Of Descartes

\begin{equation*}x^3+y^3 = 6 x y\end{equation*}

To plot this function we use the SageMath implicit_plot method.

Lets explore this function a bit more with SageMath. Some of this may not make anysense just now but will become clearer as course proceeds.

Now that we have familiarised our self with the shape of this equation lets now use sage to find the equation of the line at \((3,3)\). Implicit differentiation is required here to compute \(\frac{dy}{dx}\) so that we can find the gradient at this point.

Next find the horizontal axis of the curve by using the solve() passing in our expression and the partial derivative (set equal to zero - although this is assumed if we do not explicitly say so).

The solutions are printed using a for loop (learn some Python!) and an if statement that prints results only if \(x\) and \(y\) are real - in the 'ring' RR. There are two solutions, which we can see from the plot that this is correct. Take some time to understand the code used to do the plotting.

Definition3.3.2Utility method point_with_coords()
Definition3.3.3Utility method cross_hair()

Section 2.6, Exercise 3, Page 127

Summary

Differentiate the full equation with respect to \(x\). Use the chain rule for \(y\) variable results in: \begin{equation*}x \ {\mapsto}\ 3 \, x^{2} + 3 \, y^{2} \frac {dy}{dx} = 0\end{equation*} Rearranging gives: \begin{equation*}\frac {dy}{dx} = - \frac {x^2}{y^2}\end{equation*}

The remaining equations in this section will be solved without explanation.

The following examples make use of this predefined function. It is not very robust and assumes that the \(expr\) argumnet is sage Expression type with variable \(x\) and \(y\).

Make sure you run this before running the other examples. There will be no output but the function will be stored in memory available for use in other cells.

Definition3.3.4
Utility method my_implicit_derivative()

Section 2.6, Exercise 4, Page 127

Section 2.6, Exercise 5, Page 127

Section 2.6, Exercise 6, Page 127

Section 2.6, Exercise 7 - 16, Page 127

Section 2.6, Exercise 19, Page 127

Section 2.6, Exercise 20, Page 127

Section 2.6, Exercise 21 - Cardioid, Page 127

Section 2.6, Exercise 23 - Lemniscate, Page 127