Skip to main content

Scientific Computing

Essential Maths

Differentiati... []

Scientific Computing

Essential Maths

Differentiati... []

This material has been adapted from material by Fergus Cooper from the "Essential Mathematics" module of the SABS R³ Center for Doctoral Training.

This material has been adapted from material by Fergus Cooper from the "Essential Mathematics" module of the SABS R³ Center for Doctoral Training.

Creative Commons License
This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

This course material was developed as part of UNIVERSE-HPC, which is funded through the SPF ExCALIBUR programme under grant number EP/W035731/1

Creative Commons License

Differentiation 2


YouTube lecture recording from October 2020

The following YouTube video was recorded for the 2020 iteration of the course. The material is still very similar:

Youtube lecture thumbnail


Product, Chain and Quotient Rules

Linear approximation and the derivative

By definition, the derivative of a function f(x)f(x) is

f(x)=limh0f(x+h)f(x)hf'(x) = \lim_{h \rightarrow 0} \frac{f(x+h) - f(x)}{h}

This means that for small hh, this expression approximates the derivative. By rearranging this, we have

f(x+h)f(x)+hf(x)f(x + h) \approx f(x) + h f'(x)

In other words, f(x+h)f(x+h) can be approximated by starting at f(x)f(x) and moving a distance hh along the tangent f(x)f'(x).

Example

Estimate 5218\sqrt{5218}

To do this, we first need an f(x)f(x) that is easy to calculate, and close to 5218. For this we can take that 702=490070^2 = 4900.

To calculate the approximation, we need   f(x)  \;f'(x)\;, where   f(x)=x  \;f(x) = \sqrt{x}\;.

f(x)=12xf'(x) = \frac{1}{2 \sqrt{x}} > f(x)=12xf'(x) = \frac{1}{2 \sqrt{x}}

Since 5218 = 4900 + 318, we can set x=4900x = 4900 and h=318h = 318.

Using the approximation, we have:

f(5218)f(4900)+318×f(4900)f(5218) \approx f(4900) + 318 \times f'(4900) > f(5218)70+318×114072.27f(5218) \approx 70 + 318 \times \frac{1}{140} \approx 72.27 > 5218=72.2357252\sqrt{5218} = 72.2357252 - not a bad appriximation!

Standard derivatives

It's useful to know the derivatives of all the standard functions, and some basic rules.

ddx(xn)=nxn1\frac{d}{dx} (x^n) = n x^{n-1} > ddx(sinx)=cosx\frac{d}{dx} (\sin x) = \cos x > ddx(cosx)=sinx\frac{d}{dx} (\cos x) = -\sin x > ddx(ex)=ex\frac{d}{dx} (e^x) = e^x

To understand the derivative of sin and cos, consider their graphs, and when they are changing positively (increasing), negatively (decreasing) or not at all (no rate of change).

Graph of sin and cos

Other Differentiation Rules

Differentiation of sums, and scalar multiples

(f(x)±g(x))=f(x)±g(x)(f(x) \pm g(x))' = f'(x) \pm g'(x) (af(x))=af(x)(a f(x))' = a f'(x)

Differentiation of products

While differentiating sums, and scalar multiples is straightforward, differentiating products is more complex

(f(x)g(x))f(x)g(x)(f(x) g(x) )' \neq f'(x) g'(x) > (f(x)g(x))=f(x)g(x)+g(x)f(x)(f(x) g(x) )' = f'(x) g(x) + g'(x) f(x)

Example

To illustrate that this works, consider y=(2x31)(3x3+2x)y = (2x^3 - 1)(3x^3 + 2x)

If we expand this out, we have that y=6x6+4x43x32xy = 6x^6 + 4x^4 - 3x^3 - 2x

From this, clearly, y=36x5+16x39x22y' = 36 x^5 + 16x^3 - 9 x^2 - 2

To use the product rule, instead we say y=f×gy = f \times g, where f=2x31f = 2x^3 - 1, and g=3x3+2xg = 3x^3 + 2x. Therefore

f(x)=6x2f'(x) = 6x^2 > g(x)=9x2+2g'(x) = 9x^2 + 2 > y=fg+gf=6x2(3x3+2x)+(9x2+2)(2x31)y' = f'g + g'f = 6x^2 (3x^3 + 2x) + (9x^2 + 2)(2x^3 - 1) > y=18x5+12x3+18x5+4x39x22=36x5+16x39x22y' = 18x^5 + 12x^3 + 18x^5 + 4x^3 - 9x^2 - 2 = 36x^5 + 16x^3 - 9x^2 - 2

So both rules produce the same result. While for simple examples the product rule requires more work, as functions get more complex it saves a lot of time.

Differentiating a function of a function - The Chain Rule

One of the most useful rules is differentiating a function that has another function inside it y=f(g(x))y = f(g(x)). For this we use the chain rule:

y=f(g(x))y = f(g(x)) > y(x)=f(g(x))  g(x)=dfdgdgdxy'(x) = f'(g(x))\; g'(x) = \frac{df}{dg} \frac{dg}{dx}

Example 1: y=(5x2+2)4y = (5x^2 + 2)^4

We can write this as y=g4y = g^4, where g=5x2+2g = 5x^2 + 2. Given this, we have that

dydg=4g3=4(5x2+2)3\frac{dy}{dg} = 4g^3 = 4(5x^2 + 2)^3 > dgdx=10x\frac{dg}{dx} = 10x

This means that

dydx=dydgdgdx=4(5x2+2)310x=40x(5x2+2)3\frac{dy}{dx} = \frac{dy}{dg} \frac{dg}{dx} = 4 (5x^2 + 2)^3 10 x = 40 x (5x^2 + 2)^3

This extends infinitely to nested functions, meaning ddx(a(b(c))=dadbddx(b(c))=dadbdbdcdcdx\frac{d}{dx}(a(b(c)) = \frac{d a}{d b} \frac{d}{dx} (b(c)) = \frac{d a}{db} \frac{d b}{dc}\frac{dc}{dx}

Differentiating the ratio of two functions - The Quotient Rule

If y(x)=f(x)g(x)y(x) = \frac{f(x)}{g(x)}, then by using the product rule, and setting h(x)=(g(x))1h(x) = (g(x))^{-1}, we can show that

y(x)=fggfg2y'(x) = \frac{f'g - g'f}{g^2}

Example

y=3x14x+2y = \frac{3x-1}{4x + 2}

f=3x1,f=3f = 3x - 1, \rightarrow f' = 3 > g=4x+2,g=4g = 4x + 2, \rightarrow g' = 4 > y=fggfg2=3(4x+2)4(3x1)(4x+2)2y' = \frac{f'g - g'f}{g^2} = \frac{3(4x+2) - 4(3x-1)}{(4x+2)^2} > y=12x+612x+4(4x+2)2=10(4x+2)2y' = \frac{12x + 6 - 12 x + 4}{(4x+2)^2} = \frac{10}{(4x+2)^2}

Differentiating inverses - implicit differentiation

For any function y=f(x)y = f(x), with a well defined inverse f1(x)f^{-1}(x) (not to be confused with (f(x))1)(f(x))^{-1})), we have by definition that

x=f1(f(x))=f1(y)x = f^{-1}(f(x)) = f^{-1}(y).

This means that we can apply the chain rule

ddx(x)=ddx(f1(y))=ddy(f1(y))dydx\frac{d}{dx}(x) = \frac{d}{dx}(f^{-1}(y)) = \frac{d}{dy}(f^{-1}(y)) \frac{dy}{dx}

But since ddx(x)=1\frac{d}{dx}(x) = 1

ddy(f1(y))=1dydx\frac{d}{dy}(f^{-1}(y)) = \frac{1}{\frac{dy}{dx}}

Example: y=ln(x)y = ln(x)

If y=ln(x)y = ln(x), this means that f1(y)=ey=xf^{-1}(y) = e^y = x

By definition (f1(y))=eyf^{-1}(y))' = e^y, as eye^y doesn't change under differentiation. This means that

ddx(ln(x))=1ddy(f1(y))=1ey\frac{d}{dx}(ln(x)) = \frac{1}{\frac{d}{dy}(f^{-1}(y))} = \frac{1}{e^y}

But since y=ln(x)y = ln(x):

ddx(ln(x))=1eln(x)=1x\frac{d}{dx}(ln(x)) = \frac{1}{e^{ln(x)}} = \frac{1}{x}

Example - Differentiating using sympy

In Python, there is a special package for calculating derivatives symbolically, called sympy.

This can quickly and easily calculate derivatives (as well as do all sorts of other analytic calculations).

import sympy as sp x = sp.symbols('x') # This creates a variable x, which is symbolically represented as the string x. # Calculate the derivative of x^2 sp.diff(x**2, x)

2x\displaystyle 2 x

sp.diff(sp.cos(x), x)

sin(x)\displaystyle - \sin{\left(x \right)}

f = (x+1)**3 * sp.cos(x**2 - 5) sp.diff(f,x)

2x(x+1)3sin(x25)+3(x+1)2cos(x25)\displaystyle - 2 x \left(x + 1\right)^{3} \sin{\left(x^{2} - 5 \right)} + 3 \left(x + 1\right)^{2} \cos{\left(x^{2} - 5 \right)}

f = (x+1)**3 * (x-2)**2 * (x**2 + 4*x + 1)**4 sp.diff(f, x)

(x2)2(x+1)3(8x+16)(x2+4x+1)3+3(x2)2(x+1)2(x2+4x+1)4+(x+1)3(2x4)(x2+4x+1)4\displaystyle \left(x - 2\right)^{2} \left(x + 1\right)^{3} \cdot \left(8 x + 16\right) \left(x^{2} + 4 x + 1\right)^{3} + 3 \left(x - 2\right)^{2} \left(x + 1\right)^{2} \left(x^{2} + 4 x + 1\right)^{4} + \left(x + 1\right)^{3} \cdot \left(2 x - 4\right) \left(x^{2} + 4 x + 1\right)^{4}

sp.expand(sp.diff(f, x)) # expand out in polynomial form

13x12+180x11+869x10+1250x92934x811504x79142x6+10092x5+23185x4+17068x3+6081x2+1058x+72\displaystyle 13 x^{12} + 180 x^{11} + 869 x^{10} + 1250 x^{9} - 2934 x^{8} - 11504 x^{7} - 9142 x^{6} + 10092 x^{5} + 23185 x^{4} + 17068 x^{3} + 6081 x^{2} + 1058 x + 72

Sympy documentation

You can look at the documentation for Sympy to see many other possibilities (e.g. we will use Sympy to do symbolic integration later on in this course)

Try out Sympy to verify your pen & paper answers to the problem sheets.

Introductory problems

Introductory problems 1

Differentiate the following functions, using the stated rules where indicated:

  1. Product rule: y=(3x+4x2)(8+3x2)\displaystyle y=(3x+4x^2)(8+3x^2)

  2. Product rule: y=(5x212)(4x1+2)\displaystyle y=(5x^2-12)(4x^{-1} +2)

  3. Product rule: y=xcosx\displaystyle y=x\,\cos x

  4. Product rule: y=(3x+4x3)sinx\displaystyle y=(3\sqrt{x} + 4x^3)\sin x

  5. Chain rule: y=(6x+2)4\displaystyle y=(6x+2)^4

  6. Chain rule: y=(5x3+10)1/2\displaystyle y=({5x^3+10})^{1/2}

  7. Any rules: y=113x\displaystyle y=\sqrt{{1\over{1-3x}}}

  8. Any rules: y=(3x+1)5x2x\displaystyle y=(3x+1)\sqrt{5x^2-x}

Introductory problems 2

Differentiate the following trigonometric functions with respect to xx:

  1. 3sin(x)+5cos(x)\displaystyle 3\sin(x) + 5\cos(x)

  2. cos(32x)\displaystyle \cos(3-2x)

  3. sin(x)\displaystyle \sqrt{\sin(x)}

  4. cos(x)4x\displaystyle \frac{\cos(x)}{4x}

Main problems

Main problems 1

Let y=x2y=x^2.

  1. Find the exact value of yy when x=2.1x=2.1.

  2. Now estimate the value of yy when x=2.1x=2.1 by using the linear approximation formula f(x1+h)=f(x1)+hf(x1)f(x_1+h)=f(x_1)+hf'(x_1) and letting x1=2.0x_1=2.0 and h=0.1h=0.1.

  3. Compare your estimate to the true value. Which is bigger? What is there about the shape of the graph of y=x2y=x^2 that accounts for this?

  4. Repeat parts 1. and b) for x=2.01x=2.01.

  5. Calculate the absolute error in each estimate. How does this error change with the value of hh?

Main problems 2

The energy, EE, carried by a photon of wavelength λ\lambda is given by

E=hcλE={hc\over \lambda}

where hh is Planck's constant (h=6.63×1034Jsh=6.63\times 10^{-34}\,\rm{Js}) and cc is the speed of light (c=3×108ms1c=3 \times 10^8\,\rm{ms}^{-1}).

  1. Calculate the energy carried by a photon of wavelength 500\thinspace nm.

  2. Sketch a graph to show how E varies with λ\lambda.

  3. Derive an expression for dEdλ\displaystyle \frac{{\rm d}E}{{\rm d}\lambda}, and calculate the slope of your graph when λ=500nm\lambda=500\,\rm{nm}.

  4. Hence, or otherwise, estimate the difference in energy between a photon of wavelength 500nm500\,\rm{nm} and one of wavelength 505nm505\,\rm{nm}.

Main problems 3

On February 10, 1990, the water level in Boston harbour was given by:

y=5+4.9cos(π6t)y=5 + 4.9 \cos\left({\pi\over 6} t\right)

where tt is the number of hours since midnight and yy is measured in feet.

  1. Sketch y.

  2. Find dydt\displaystyle \frac{{\rm d}y}{{\rm d}t}. What does it represent, in terms of water level?

  3. For 0t240\le t \le 24, when is dydt\displaystyle \frac{{\rm d}y}{{\rm d}t} zero? Explain what it means for dydt\displaystyle \frac{{\rm d}y}{{\rm d}t} to be zero.

Main problems 4

In laminar flow of blood through a cylindrical artery, the resistance RR is inversely proportional to the fourth power of the radius rr.

Use a linear approximation to show that if rr is decreased by 2%, RR will increase by approximately 8%.

Main problems 5

Imagine an ocean basin with an inlet on one side. We can model the salinity of the water as a function of the distance from the ocean inlet.

Let ss be the water salinity, and xx is the distance from the inlet. Then

s=s0αX(αXx)\displaystyle s = {s_{0}\alpha X\over(\alpha X - x)}

where s0s_{0} is the initial salinity at the inlet, XX is the width of the basin, and α\alpha is a constant.

Prove that the rate of increase of salinity with distance from the inlet is given by

dsdx=sαXx\displaystyle \frac{{\rm d}s}{{\rm d}x} = {s\over\alpha X - x}

Main problems 6

The sine and cosine functions can be written in the form of the following infinite series:

sinx=xx33!+x55!x77!+\sin x = x - {x^3\over3!} + {x^5\over5!} - {x^7\over7!} + \ldots > cosx=1x22!+x44!x66!+\cos x = 1 - {x^2\over2!} + {x^4\over4!} - {x^6\over6!} + \ldots

Differentiate these series term by term to verify the standard expressions for ddx(sinx)\displaystyle\frac{{\rm d}}{{\rm d}x}(\sin x) and ddx(cosx)\displaystyle\frac{{\rm d}}{{\rm d}x}(\cos x).

Extension problems

Extension problems 1

The focal length of the lens of the eye, f(t)f(t), can be controlled so that an object at distance u(t)u(t) in front of the eye can be brought to perfect focus on the retina at a constant v=1.8cmv = 1.8\,\rm{cm} behind the lens.

A fly is moving towards the eye at a speed of 0.7ms10.7\,\rm{ms}^{-1}.

Assuming that the optics of the eye lens obeys the thin lens formula

1f(t)=1u(t)+1v,{1\over f(t)}={1\over u(t)}+{1\over v},

find the rate of change of focal length required to keep the fly in perfect focus at a distance of 3m3\,\rm{m}.

Note: consider carefully what you are differentiating with respect to, and the physical interpretation of the mathematics.

Extension problems 2

A contaminated lake is treated with a bactericide.

The rate of change of harmful bacteria tt days after treatment is given by

dNdt=2000t1+t2\frac{{\rm d}N}{{\rm d}t} = -{2000t\over 1+t^2}

where N(t)N(t) is the number of bacteria in 1ml1\,\rm{ml} of water.

  1. State with a reason whether the count of bacteria increases or decreases during the period 0t100\leq t \leq 10.

  2. Find the minimum value of dNdt\displaystyle \frac{{\rm d}N}{{\rm d}t} during this period.

Extension problems 3

Consider a population of lions L(t)L(t) and zebra Z(t)Z(t) interacting over time tt. One type of model for this situation is

dZdt=aZbZLanddLdt=cL+dZL,\frac{{\rm d}Z}{{\rm d}t} = aZ - bZL \qquad \rm{and} \qquad \frac{{\rm d}L}{{\rm d}t} = -cL + dZL,

where aa, bb, cc and dd are positive constants.

  1. What values of dZdt\displaystyle \frac{{\rm d}Z}{{\rm d}t} and dLdt\displaystyle \frac{{\rm d}L}{{\rm d}t} correspond to stable populations?

  2. How would the statement 'zebra go extinct' be represented mathematically?

  3. For parameters a=0.05a=0.05, b=0.001b=0.001, c=0.05c=0.05, and d=0.00001d=0.00001, find all population pairs (Z,L)(Z,L) that yield stable populations. Is extinction inevitable?