# Topic 3 Derivatives

## 3.1 Derivative Functions

What makes derivative so important in modern mathematics is the ideal of linearly approximating curves using tangent lines. Geometrically, the derivative of a function \(f\) at a point \(x=a\) is the slope of the line tangent to \(f\) at \(x=a\). Using limits (if it exists), the derivative is defined as \[ f'(a):=\lim\limits_{x\to a}\frac{f(x)-f(a)}{x-a}. \]

Consider \(a\) as a variable, we may define a function called the derivative function. In terms of limits, the derivative function of a function \(f\), denoted by \(f'\) is given by \[ f'(x)=\lim\limits_{h\to 0}\frac{f(x+h)-f(x)}{h}. \]

Using limit laws, we can show that differentiable functions are continuous.

Geometrically, the graph of differentiable function locally is flat without any hole or jump.

All elementary functions are differentiable over their domain. But there are also many functions which are not differential everywhere in their domain.

**Example 3.1 **
Use the graph of the function \(f(x)=|x-1|\) to identify a \(x\) value where \(f\) is not differential. Verify your finding using the definition of differentiability.

*Solution. *
First plot the function.

```
f:= x-> abs(x);
plot(f(x), x=-2..2);
```

You will see the function is not flat near \(x=1\). To verify that, we calculate the limit of the difference quotient for all \(x\) and then evaluate the resulting function at \(x=1\).

```
diffquot:=(f(x+h)-f(x))/h;
derlimit:= limit(diffquot, h=0);
evalf(subs(x=1, derlimit));
```

**Exercise 3.1 **
Determine whether the function
\[
f(x)=\begin{cases}
x\sin\left(\frac1x\right) & x\neq 0\\
0 & x=0
\end{cases}
\]
is differential at \(x=0\) using the definition of the differentiability.

**Exercise 3.2 **
Determine whether the function
\[
f(x)=\begin{cases}
x^2\cos\left(\frac1x\right) & x\neq 0\\
0 & x=0
\end{cases}
\]
is differential at \(x=0\) using the definition of the differentiability.

**Exercise 3.3 **
Using the definition to find the derivative for \(y=\sin x\).

## 3.2 Calculating Derivatives

Calculating a derivative in Maple is as easy as calculating a limit. The command for differentiation is `diff(function, variable)`

. For higher derivatives, you may simply repeat the variable or use `[variable$n]`

to indicate the \(n\) times differentiation.

**Example 3.2 **
Calculate the first derivative and the second derivative for the function
\[
f(x)=\frac{2}{\sqrt{2\sin^2 x + 1}}.
\]

*Solution. *
First represent the function rule simply by `f`

.

`f:=2/(sqrt(2*(sin(x))^2+1))`

Calculate the first derivative and denote the derivative by `f1`

.

`f1:=diff(f, x);`

Calculate the second derivative and denote the derivative by `f2`

. You may use one of the following three commands.

```
f2:=diff(f1, x);
f2:=diff(f, x, x);
f2:=diff(f, [x$2]);
```

Maple also has a tutoring command for differentiation: `DiffTutor(function, variable)`

which is again supported by the subpackage `Student[Calculus1]`

. However, `DiffTutor`

only works for the first derivative.

**Example 3.3 **
Calculate the derivative for \(g(x)=\frac{x^2-2x-x^{-3}}{\sqrt{x}}\) by hand and compare your calculation the the result given by `DiffTutor`

.

*Solution. *
By hand, we may simplify the expression using rational exponents first and then apply derivative rules.
\[
g'(x)=(x^{\frac32}-2x^{\frac12}-x^{-\frac72})'=\frac32\sqrt{x}-\frac{1}{\sqrt{x}}+\frac{7}{2x^4\sqrt{x}}=\frac{3x^5-2x^4+7}{2x^4\sqrt{x}}.
\]

To see the result from `DiffTutor`

, we use the following command. Remember to load the `Student[Calculus1]`

first.

```
with(Student[Calculus1]):
DiffTutor((x^2-2*x-x^(-3))/sqrt(x), x);
```

Here is how does the output look like

**Exercise 3.4 **
Calculate the first derivative for the function \(y=2x^{-1}-3\sqrt{x}\) by hand and by Maple. Compare two results. Are they different? If so, can you explain the difference?

**Exercise 3.5 **
Calculate the first derivative for the function \(y=x\sin^{-1}(x)\) by hand and by Maple. Compare two results. Are they different? If so, can you explain the difference?

**Exercise 3.6 **
Calculate the first derivative for the function \(y=\frac{\cos(x)+\sin x}{x}\) by hand and by Maple. Compare two results. Are they different? If so, can you explain the difference?

**Exercise 3.7 **
Calculate the \(3\)-th derivative for the function \(y=\sin x\cos x\) by hand and by Maple. Can you find a formula for the \(n\)-th derivative of the function?

## 3.3 Chain Rule

Let \(f(x)\) and \(g(x)\) be two differentiable functions. Then the derivative of the composite function \(f(g(x))\) can be calculated using the following formula \[ (f(g(x)))'=f'(g(x))g'(x). \] Why there is an extra factor \(g'(x)\)? This is mainly because \(g(x+h)\approx g(x)+g'(x)h\).

Now let’s use Maple to understand the chain rule. In Maple, the symbol for composition is `@`

, that is, in Maple the composition \(f\circ g\) is given by `f@g`

.

Since we will evaluate derivative functions, in addition to the command `diff(expression, variable)`

, we will also use `D(function)(variable)`

to find the derivative function. One major difference between those two commands is that `D`

is designed to differentiate functions, whereas `diff`

is for differentiating expressions.

**Example 3.4 **
Let \(f(x)=\sin x\), \(g(x)=2x\) and \(F(x)=2\sin x\cos x\). Find \(f'(g(x))\), \((fg)'(x)\), \((f\circ g)'(x)\) and \(F'(x)\)?
Compare the derivatives and draw a conclusion.

*Solution. *
We first define the functions.

```
f:=x->sin(x);
g:=x->2*x;
F:=x->2*sin(x)*cos(x);
```

Find the derivative functions

```
Der_f_g:=D(f)(g(x)); # f'(g(x))
Der_fg:=diff(f(x)g(x), x); # (fg)'(x)
Der_fog:=D(f@g)(x); # (fog)'(x)
Der_F:=D(F)(x); # F'(x)
```

To compare the derivatives, we use the command `expand`

and `simplify`

to rewrite the expressions.

```
simplify(expand(Der_f_g));
simplify(expand(Der_fg));
simplify(expand(Der_fog));
simplify(expand(Der_F));
```

From the outputs, we see that \((f\circ g)'(x)=F'(x)\). Why they are the same? This is because \(\sin(2x)=2\sin x\cos x\)!.

*Remark. *
You may also use `D(unapply(f(x)*g(x), x))(x)`

to calculate the derivative of the product function \((fg)(x)=f(x)g(x)\).

The chain rule is almost unavoidable in calculation of derivatives. Sometimes, using chain rule will simplify the calculation.

**Example 3.5 **
Consider the function \(f(x)=\frac{1}{\sin x+\cos x}\).

- Find the derivative function \(f'(x)\) using
`DiffTutor`

. Which rule of derivative was applied first? - Find the point where the tangent line of \(f\) is horizontal over the domain \((-\frac{\pi}{2}, \frac{\pi}{2})\).
- Find an equation of the tangent line at \((0, 1)\).
- Plot the tangent line and the function together over the domain \((-\frac12, \frac12)\).

*Solution. *
You may use quotient rule to find the derivative. However, the chain rule may be a better choice because \(\frac{1}{\sin x+\cos x}=(\sin x+\cos x)^{-1}\). Let’s try `DiffTutor`

.

```
restart; # Use `restart` to clear the internal memory.
with(Student[Calculus1]);
f:=1/(sin(x)+cos(x));
DiffTutor(f, x);
```

You see that the chain rule was applied first.

To find the points where the tangent line is horizontal, we need to solve the equation \(f'(x)=0\).

`solve(diff(f, x)=0)`

To find the tangent line, we find the derivative \(f'(0)\). One way is to use `unapply`

.

`unapply(diff(f, x), x)(0)`

You may also use `eval(expression, variable=value)`

.

`eval(diff(f, x), x=0)`

Since \(f'(0)=-1\), the tangent line at \((0, 1)\) is defined by \(y=-x+1\).

`y:= -x+1`

Now let’s verify visually that \(y=-x+1\) is the tangent line.

`plot([f, y], x=-1/2..1/2)`

**Exercise 3.8 **
Let \(f(x)=\sin x\), \(g(x)=\frac{\pi}{2}-x\) and \(h(x)=\cos x\). Find \(f'(g(x))\), \((fg)'(x)\), \((f\circ g)'(x)\) and \(h'(x)\). Compare the derivatives and draw a conclusion.

**Exercise 3.9 **
Let \(f(x)=x^2\) and \(g(x)=\cos x\) and \(h(x)=\frac{1}{2}(\cos(2x)+1)\). Find \(f'(g(x))\), \((fg)'(x)\), \((f\circ g)'(x)\) and \(h'(x)\). Compare the derivatives and draw a conclusion.

**Exercise 3.10 **
Consider the function \(F(x)=\sin^2(\frac{\pi(x^2+1)}{3})\).

- Define three functions \(f\), \(g\) and \(h\) so that \(F(x)=f(g(h(x)))\).
- Describe how the derivative function \(F'(x)\) was calculated by
`DiffTutor`

. - Find the point where the tangent line of \(F\) is horizontal over the domain \((0, 1)\).
- Find an equation of the tangent line of \(F\) at \((1, \frac34)\).
- Plot the tangent line and the function together over the domain \((\frac12, \frac32)\).

## 3.4 Implicit Differentiation

Implicit differentiation is an application of the chain rule. It provides a way to find the slope of a tangent line of a function implicitly defined by an equation, that is, the dependent variable is not isolated.

In Maple, there are two useful commands that help us understand implicit defined functions.
The command `implicitplot(equation, domain, range, options)`

supported by the package `plots`

can be used to graph an implicitly defined function.
The command `implicitdiff(function, dependent variable, independent variable)`

can be used to find derivatives implicitly.

**Example 3.6 **
Graph the function \(y\) of \(x\) defined by \(x^2+2y^2=2x+4y\) with it tangent line at \((2, 0)\) over the domain \((1, 3)\) and range \((-1, 2)\).

*Solution. *
Let’s first assign an name to the equation. (Run `restart`

first if `x`

and `y`

were previously used an names.)

```
restart;
eqnf:=x^2+2*y^2=2*x+4*y;
```

Now let’s find the derivative function, and its value at \((0, 1)\).

```
D_eqnf:=implicitdiff(eqnf, y, x);
slope:=eval(D_eqnf, {x=2, y=0});
```

Let’s define the tangent line

`tangentline:= y=slope*(x-2)+0`

Now we are ready to plot the function with the tangent line together.

```
with(plots);
implicitplot([eqnf, tangentline], x=1..3, y=-1..2, color=[red, blue]);
```

*Remark. *
Another way, which is more flexible, to put two or more graphs together is to use the command `display(graph1, graph2)`

which is supported by the package `plots`

. For example, the following commands will show two curves in a single picture.

```
with(plots):
g1 := implicitplot(x^2+y^2=1, x=-1..1, y=0..1):
g2 := plot(1-abs(x), x=-1..1):
display(g1, g2, title="Two Together");
```

Note that we use colone `:`

at the end of a command to hide the output.

**Exercise 3.11 **
For the ellipse \(x^2-xy+y^2=4\), find the locations of all horizontal tangent lines and plot them implicitly on the same graph as the relation over the interval \(-3 \leq x \leq 3\) and \(-3 \leq y \leq 3\).

**Exercise 3.12 **
Find an equation of the line tangent to the curve \(x^2 + (x-y)^3 = 9\) at \(x=1\). (You may want to use `solve`

and `subs`

to find the \(y\) when \(x=1\)).

**Exercise 3.13 **
Find all points \((x, y)\) on the curve of \(|x|^{2/3} + |y|^{2/3} = 8\) where lines tangent to the curve at \((x, y)\) is perpendicular to the line \(x-y=1\). (Use `?solve`

to learn how to solve a system of equations in Maple).

## 3.5 Rates of Change and Derivatives

Given a function \(y=f(x)\), the average rate of change of \(f\) is the difference quotient \[ \frac{\Delta y}{\Delta x}=\frac{f(x+\Delta x)-f(x)}{\Delta x}. \] The instantaneous rate of change is the limit \(\lim\limits_{\Delta x\to 0}\frac{\Delta y}{\Delta x}\) which is exactly the derivate of \(f\). However, in science, economy and many other field, the same concept bears different names. For example, the velocity is the instantaneous rate of change of the position function; the marginal cost is the derivative of the cost function.

Now let’s use Maple to help us understand such kind of application of derivatives.

**Example 3.7 **
The position function of a particle moving along a straight line after \(t\) seconds is \(s(t)= 2t^3-6t^2-18t+1\) meters.

- Find the time \(t\) that the particle is at the rest.
- Find the distance the particle moved in the first 5 seconds.
- Plot the \(s(t)\), \(v(t)\) and \(a(t)\) for \(0\le t\le 5\) together.
- When the particle is speeding up?
- Confirm you answer in 3. using graph of the speed function.

*Solution. *
Let’s first define if position function

`s:= t->2*t^3-6*t^2-18*t+1`

The particle at rest when the velocity is 0. To find \(t\), we find \(v(t)\) first and then solve \(t\) from \(v(t)=0\).

```
v:=D(s);
solve({v(t)=0, t>0}, t); # assuming t>0.
```

From the output, you will find that after 3 second the particle is temporarily at rest.

The particle moves forward if \(v(t)>0\) and backward if \(v(t)<0\). You may use the previous output or apply the command `solve`

for those two inequalities. Either way, you will find from \(t=0\) to \(t=3\), \(v(t)<0\) and \(v(t)>0\) for \(3<t\le 5\). So the total distance should be calculated as

`totdist:= abs(s(3)-s(0))+abs(s(5)-s(3))`

The acceleration function \(a(t)=v'(t)\). To plot those three functions together, you may use `plot`

.

```
a:=D(v);
plot([s(t), v(t), a(t)], t=0..5, color=[black, blue, red]);
```

The speed function of the particle is \(|v(t)|\). When \(v(t)>0\) and \(a(t)>0\), the particle is speeding up. When \(v(t)<0\) and \(a(t)<0\), the particle is also speeding up. So the particle is speeding up if \(v(t)a(t)>0\). Similarly, the particle is slowing down if \(v(t)a(t)<0\).

` solve({v(t)*a(t)>0, t>0}, t)`

It shows that the particle is speeding up when \(t<1\) or \(t>3\). This can also be seen from the graph of the speed function.

` plot(abs(v(t)), t=0..5)`

**Exercise 3.14 **
The position function of a particle moving along a straight line after \(t\) seconds is \(s(t) = t^3 - 6t^2 - 15t+2\) meters.

- Find the time \(t\) that the particle is at the rest.
- Find the distance the particle moved in the first 5 seconds.
- Plot the \(s(t)\), \(v(t)\) and \(a(t)\) for \(0\le t\le 5\) together.
- When the particle is speeding up?
- Confirm you answer in 3. using graph of the speed function.

**Exercise 3.15 **
Suppose that the profit obtained from the sale of \(x\) calculators is given by \(P(x)=-0.05x^2+4x+25\). Use the marginal profit function to estimate the profit from the sale of the 51st calculator.

## 3.7 Linearizations

Let \(f\) be the function differentiable at \(x = a\). The **linearization** of \(f\) at \(x = a\) is defined to be the function \(L(x)=f'(a)(x-a)+f(a)\). For any value \(b\) near \(a\), the function value \(f(b)\) is approximately the same as \(L(b)\). This method is called **linear approximation**. The tangent line approximation is fundamental to almost every application of the derivative.

**Example 3.9 **
Find the linearization \(L(x)\) of the function \(f(x) = \sqrt[3](x+7)\) at \(x=1\) and use this linearization to estimate \(f(0.99)\). How large is the error?

*Solution. *
We first find the slope fr the linearization which is the derivative \(f'(1)\).

```
f:=x->surd(x+7, 3); # or f:=x->(x+7)^(1/3)
m:=simplify(D(f)(1)); # this is the slope (simplified).
```

Now we define the linearization \(L(x)\) and estimate \(f(0.99)\) using \(L(0.99)\).

```
L:=x->m*(x-1)+f(1);
L(0.99);
```

The error may be calculated based on the output of the follow command.

`f_L:=f(0.99)-L(0.99);`

The results shows that the linear approximation is slightly over estimated with an error less than \(4\times 10^{-7}\).

**Exercise 3.19 **
Find the linear approximation \(L(x)\) of the function \(f(x) = \frac{\sqrt{x}}{x+1}\) at \(x = 1\). Use this linearization to approximate \(f(1.02)\).

**Exercise 3.20 **
Find the linear approximation \(L(x)\) of the function \(f(x) = \cos(2x)\) at \(x = 0\). Use this linearization to approximate \(f(0.1)\).

**Exercise 3.21 **
Find the linear approximation \(L(x)\) of the function \(f(x) = \sqrt{x^2+5}\) at \(x = 2\). Use this linearization to approximate \(f(2.03)\).

## 3.8 Newton’s Method

In science and engineering, many problems may be eventually reduced to nonlinear equations which likely has no algebraic (analytic) solution. In such a situation, numerical solutions are hoped for applications. We’ve seen a method to find a numerical solution using the intermediate value theorem. However, it is not very effective. Using linearization, a root-finding algorithm was developed by Newton and other mathematicians. The idea is to use the \(x\)-coordinates of tangent lines to approximate a root of an equation. Let’s see an example first using the Maple command `NewtonsMethod(function, starting point, options)`

which is again supported by the subpackage `Student[Calculus1]`

.

**Example 3.10 **
Starting at \(x=-1.5\), obtain a solution of \(\sin x-\frac{x}{2}=0\) by Newton’s method.

*Solution. *
First load `Student[Calculus1]`

use `with()`

.

`with(Student[Calculus1])`

Now apply the `NewtonsMethod command`

`NewtonsMethod(sin(x)-x/2, x=-1.5)`

To see the graph, you may add the option `output=plot`

.

`NewtonsMethod(sin(x)-x/2, x=-1.5, showroot=true, output=plot)`

How does Newton’s methods work? It’s an iteration process. Consider the equation \(f(x)=0\). Suppose \(f\) is differentiable. To find a solution, we pick an initial value \(x_0\) first. The \(x\)-value \(x_1\) of the \(x\)-intercept of the linearization \(L_0(x)=f'(x_0)(x-x_0)+f(x_0)\) should produce a value that is closer. The value \(x_1\) is given by the formula \[ x_1=x_0-\frac{f(x_0)}{f'(x_0)}. \] Iteratedly applying the above idea, we get the iteration formula \[ x_{n}=x_{n-1}-\frac{f(x_{n-1})}{f'(x_{n-1})}. \]

*Remark. *
How many times should be iterated? It depends on the sizes of acceptable errors and a maximum number of iteration.

If the \(x\)-values produced from two successive iterates is sufficiently small, say \(|x_n-x_{n-1}|<e_1\), and the function value at \(x=x_n\) is sufficiently small, say \(f(x_n)<e_2\), where \(e_1\) and \(e_2\) are acceptable errors, then we may say \(f(x)=0\) has a solution at \(x=x_n\).

If after iterated a maximum number of times and a solution was not found, then we may have to change the initial value or using other methods.

If it happens that \(f'(x_n) = 0\), then the iteration process fails.

The above algorithm may be realized using the following Maple codes.

```
tol := 10^(-3);
N := 100;
f := x -> sin(x) - 1/2*x;
m := D(f);
newton := x -> evalf(x - f(x)/m(x)); # Newton's formula
x := -1.5;
for i to N do
x := newton(x);
if abs(x - newton(x)) < tol and abs(f(x)) < tol then
break;
eli i = N then
error "Newton's method did not converge";
end if;
end do;
print(x);
```

If you don’t want to check the size of error, the code can be simplified using the composite symbol `@`

in Maple.

```
restart;
f := x -> sin(x) - 1/2*x;
m := D(f);
newton := x -> evalf(x - f(x)/m(x));
N:=100
x[N]:=evalf((newton@@N)(-1.5)); # @@N means that newton compose with itself N times.
```

**Exercise 3.22 **
Using Newton’s Method to find solutions of the polynomial equation \(x^3-x^2+1=0\).

**Exercise 3.23 **
Using Newton’s Method to find solutions of the equation \(\cos x=\frac{x}{2}\).

**Exercise 3.24 **
Using Newton’s Method to find the solution of the equation \(\tan x-2x=0\) in the interval \([1, 2]\). Among 1 and 2,which is a better initial value? Why?