Skip to main content

Scientific Computing

Essential Maths

Indices, logs... []

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 1


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


Gradients

We often want to know about the rate at which one quantity changes over time. Examples:

  1. The rate of disappearance of substrate with time in an enzyme reaction.

  2. The rate of decay of a radioactive substance (how long will it have activity above a certain level?)

  3. The rate of bacterial cell growth over time.

  4. How quickly an epidemic is growing.

Defining the gradient

  • The gradient of a curve at a point PP is the slope of the tangent of the curve at that point.

  • The tangent is the line that "just touches" (but doesn't cross) the curve.

  • The gradient is also known as the rate of change or derivative, and the process of finding the gradient is called differentiation.

  • The gradient of the curve   y=f(x)  \;y = f(x)\; is denoted in a few different ways, the three most common are:

y,f(x),dydx.y', \quad f'(x), \quad \frac{dy}{dx}.

Example, y=x2y = x^2

At x=1x=1 the gradient is shallow and sloping up to the right:

Gradient of x squared

At x=2x=-2 the gradient is steeper, and sloping down to the right:

Gradient of x squared

Example, y=log(x)y = \log(x)

For this function, the gradient is always sloping up to the right, but gets shallower as xx increases:

Gradient of log x

Algebraic example

If we want to find y(x)y'(x) for y=x3+2y = x^3 + 2:

Gradient=y2y1x2x1=ΔyΔx \text{Gradient} = \frac{y_2 - y_1}{x_2-x_1} = \frac{\Delta y}{\Delta x}

Try with

x1=1.5,  1.9,  1.99,  x_1 = 1.5,\;1.9,\;1.99,\;\ldots

x2=2.5,  2.1,  2.01,  x_2 = 2.5,\;2.1,\;2.01,\;\ldots

We can use Python as a calculator to evaluate these differences:

x_1 = 1.5 x_2 = 2.5 y_1 = x_1**3 + 2 y_2 = x_2**3 + 2 print((y_2-y_1)/(x_2-x_1)) x_1 = 1.9 x_2 = 2.1 y_1 = x_1**3 + 2 y_2 = x_2**3 + 2 print((y_2-y_1)/(x_2-x_1)) x_1 = 1.99 x_2 = 2.01 y_1 = x_1**3 + 2 y_2 = x_2**3 + 2 print((y_2-y_1)/(x_2-x_1))
12.25 12.010000000000003 12.00009999999997

As the difference between x1x_1 and x2x_2 gets smaller, the gradient stabilises. The value it converges to is the gradient at the midway point of x1x_1 and x2x_2.

Calculating gradients exactly

GradientΔyΔx=f(x+h)f(x)h\text{Gradient} \approx \frac{\Delta y}{\Delta x} = \frac{f(x+h) - f(x)}{h}

This is called a finite difference approximation to the gradient. The approximation becomes more accurate the smaller h is.

When using the approximation, we denote the changes as ΔyΔx\frac{\Delta y}{\Delta x}, in the limit as h goes to 0, this becomes dydx\frac{dy}{dx}.

In this way, ddx\frac{d}{dx} is an operator, acting on yy.

Note, the dds cannot be cancelled out, as they aren't variables, they denote an infinitely small change.

Notice how, as the finite difference gets smaller and smaller, the approximation to the gradient (the green line) gets closer and closer to the true gradient (the orange line):

Finite difference approximation 1

Finite difference approximation 2

Example

Find the gradient of y=f(x)=x3+2y = f(x) = x^3 + 2.

dydx=f(x+h)f(x)h\frac{dy}{dx} = \frac{f(x+h) - f(x)}{h} > dydx=(x+h)3+2(x3+2)h\frac{dy}{dx} = \frac{(x+h)^3 + 2 - (x^3 + 2)}{h} > dydx=x3+3x2h+3xh2+h3+2x32h\frac{dy}{dx} = \frac{x^3 + 3x^2 h + 3xh^2 + h^3 + 2 - x^3 - 2}{h} > dydx=3x2h+3xh2+h3h\frac{dy}{dx} = \frac{3x^2h + 3xh^2 + h^3}{h} > dydx=3x2+3xh+h3\frac{dy}{dx} = 3x^2 + 3xh + h^3

Now this is only exactly right when h0h \rightarrow 0. So letting that happen, we have dydx=3x2\frac{dy}{dx} = 3x^2

Derivative of polynomial functions

Using techniques like the one above (which is called differentiation from first principles), one can generalise the connection between powers of xx and their derivatives:

If y=axny = a x^n, then its derivative is dydx=y(x)=anxn1\frac{dy}{dx} = y'(x) = a n x^{n-1}

Examples to try

  1. y=x4y = x^4

  2. y=7x5y = 7x^5

  3. y=x2=1x2y = x^{-2} = \frac{1}{x^2}

  4. y=1/x=(1/x)1/2=x1/2y = \sqrt{1/x} = (1/x)^{1/2} = x^{-1/2}

Summing and multiplying derivatives

Summing

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

e.g.

y=x2+x3,y=2x+3x2y = x^2 + x^3, \quad y' = 2x + 3x^2

Multiplying (by a scalar)

(af(x))=af(x) (a f(x))' = a f'(x)

e.g.

y=6x3,y=63x2=18x2y = 6x^3, \quad y' = 6 \cdot 3x^2 = 18 x^2

This only works for scalars.

In most circumstances (f(x)g(x))f(x)g(x)(f(x) g(x))' \neq f(x)' g(x)'

e.g.

y=xx=x2,y1y = x\cdot x = x^2, \quad y' \neq 1

Higher-order derivatives

You can take a derivative of a function multiple times in a row. This is usually denoted either y(x),    f(x)  y''(x),\;\;f''(x)\; or   d2ydx2  \;\frac{d^2 y}{dx^2}\; for second-order derivatives (differentiating twice), and similar for higher orders.

e.g.

y=x3y = x^3 > y=3x2y' = 3x^2 > y=d2ydx2=6xy'' = \frac{d^2 y}{dx^2} = 6 x

Interpreting derivatives

The sign of the first derivative   f(x)  \;f'(x)\; tells us how   f(x)  \;f(x)\; is growing

  • Positive gradient: If   y>0  \;y' > 0\; then   y  \;y\; is increasing at   x  \;x\;

  • Negative gradient: If   y<0  \;y' < 0\; then   y  \;y\; is decreasing at   x  \;x\;

  • Zero gradient: If   y=0  \;y' = 0\; then   y  \;y\; is not changing (flat) at   x  \;x\;

Extreme values (turning points and points of inflection)

  1. Local maximum:   dydx=0,  \;\frac{dy}{dx} = 0,\; and   d2ydx2<0  \;\frac{d^2y}{dx^2} < 0\;

  2. Local minimum:   dydx=0,  \;\frac{dy}{dx} = 0,\; and   d2ydx2>0  \;\frac{d^2y}{dx^2} > 0\;

  3. Inflection:   d2ydx2=0  \;\frac{d^2y}{dx^2} = 0\;

Example: Find the stationary points of   y=2x35x24x  \;y = 2x^3 - 5x^2 - 4x\;

To do this, we need to know both   y(x)  \;y'(x)\; and   y(x)  \;y''(x)\;.

y(x)=6x210x4y'(x) = 6x^2 - 10x - 4 > y(x)=12x10y''(x) = 12x - 10

Stationary points occur when   y(x)=0  \;y'(x) = 0\;

6x210x4=06x^2 - 10x - 4 = 0 > (3x+1)(2x4)=0(3x + 1)(2x - 4) = 0 > x=1/3,  2x = -1/3,\;2

At x=1/3x = -1/3:

y(1/3)=12×1/310=14<0y''(-1/3) = 12 \times -1/3 - 10 = -14 < 0

So this point is a maximum.

At x=2x = 2

y(2)=12×210=14>0y''(2) = 12 \times 2 - 10 = 14 > 0

So this point is a mimimum.

Inflection points occur whenever y(x)=0y''(x) = 0

y(x)=12x10=0y''(x) = 12x - 10 = 0 > x=1012=56x = \frac{10}{12} = \frac{5}{6}

This is an inflection point.

Turning points

Note: Points of inflection do not require that   y(x)=0  \;y'(x) = 0\;, only that   y(x)=0  \;y''(x) = 0\;.

Points of inflection are important in biology as they define conditions where a response (e.g. reaction rate) is most or least sensitive to a change in conditions (e.g. the concentration of a metabolite).

Reminder on curve sketching

  • Aim to evaluate and identify key values of the function (i.e. turning points, points of inflection)

  • Look at the limit behaviour as   x±  \;x \to \pm \infty\; and as   x  \;x\; approaches any points where the function is undefined (e.g.   x0  \;x \to 0\; for   y=1/x  \;y = 1/x\;).

  • Determine the first and second order derivatives to find turning points and points of inflection.

Real life example

The number nn (in thousands) of bacteria on an agar plate at time tt (in days) is given by the expression:

n=15.42+6tt2n = 15.42 + 6t - t^2

  1. Find the time at which the greatest number of bacteria are present on the plate.

  2. Find the number of bacteria on the plate at this time.

Solution

To do this we must find the turning points of the function.

  1. Find the time at which the greatest number of bacteria are present on the plate

    • n(t)=15.42+6tt2n(t) = 15.42 + 6t - t^2

    • n(t)=62tn'(t) = 6 - 2t

    • n(t)=0    62t=0    t=3n'(t) = 0 \quad\implies\quad6-2t=0\quad\implies t=3

    To show this is a maximum, we need to check n(t)n''(t)

    n(t)=2n''(t) = -2

    Therefore, n(t)<0n''(t)<0, for t=3t = 3. This means that a maximum occurs at t=3t = 3 days.

  2. Find the number of bacteria on the plate at this time

    n(3)=15.42+6×332=24.42n(3) = 15.42 + 6 \times 3 - 3^2 = 24.42

    The greatest number of bacteria on the plate is 24,420.

Real life example 2

The growth rate RR of a cell colony with NN cells at time tt can be represented by the equation

R=dNdt=kNbN2R = \frac{d N}{d t} = kN - bN^2

For this example take the constants kk and bb as k=3.8k = 3.8/hr, and b=0.01b = 0.01/hr. This is called a logistic model.

  1. What is the equilibrium size of the population?

  2. What population size leads to the largest growth rate?

Solution

  1. The equilibrium will occur when the population stops changing, i.e. when R=0R = 0. Meaning:

    R=3.8N0.01N2=0R = 3.8 N - 0.01 N^2 = 0

    N(3.80.01N)=0N (3.8 - 0.01 N) = 0

    We can disregard the N=0N = 0 solution, as it represents population extinction. This means that

    N=3.80.01=380N = \frac{3.8}{0.01} = 380.

  2. To find the largest growth rate, we want the maximal value of R(N)R(N). This means we need to find R(N)=0R'(N) = 0.

    R(N)=3.8N0.01N2R(N) = 3.8 N - 0.01 N^2

    R(N)=3.80.02NR'(N) = 3.8 - 0.02 N

    If R(N)=0R'(N) = 0

    3.80.02N=03.8 - 0.02N = 0

    N=190N = 190

    Since R(N)=0.02<0R''(N) = -0.02 < 0, we can be sure that this is a maximum.

Introductory problems

Introductory problems 1

Use the formula dydx=limh0(f(x+h)f(x)h)\displaystyle \frac{{\rm d}y}{{\rm d}x}=\lim*{h\rightarrow 0}\left({f(x+h)-f(x)\over h}\right) to calculate the derivatives of the functions below. Check your answers by using the standard rules for differentiation:

  1. y=3x+3\displaystyle y = 3x + 3

  2. y=4x23x+2\displaystyle y = 4x^2 - 3x + 2

  3. y=2x35\displaystyle y = 2x^3-5

  4. y=1x2\displaystyle y=\frac{1}{x^2}\qquad (harder)

Introductory problems 2

Find the gradient at the given points of the following curves:

  1. y=x34wherex=1\displaystyle y = x^3 - 4 \qquad\rm{where}\qquad x = 1

  2. y=3x3+4x3wherex=2\displaystyle y = 3x^3 + 4x - 3 \qquad\rm{where}\qquad x = -2

Introductory problems 3

Find the xx and yy coordinates of the points on the given curves at which the gradient is zero and find out whether they are maxima, minima or points of inflexion:

  1. y=(x3)(x4)\displaystyle y = (x - 3)(x - 4)

  2. y=x34x2+2x2\displaystyle y = x^3 - 4x^2 + 2x - 2

  3. y=4x+1x\displaystyle y = \frac{4x+1}{x}

  4. y=162x3\displaystyle y = 16 - 2x^3

Main problems

Main problems 1

One hour after taking xmgx\,\rm{mg} of a drug, the body temperature, TT, in ^\circC of a patient is given by: T=T00.00625 x2(18x),T=T_0-0.00625~x^2(18-x), where T0T_0 is the initial body temperature.

  1. Determine the value of xx that produces the greatest drop in body temperature, and the magnitude of that temperature change.

  2. Sketch TT as a function of the concentration.

Main problems 2

The formula for the Lennard Jones potential between two non polar atoms is given in terms of the positive constants AA and BB and the internuclear distance, RR, as: V(R)=AR12BR6V(R)={A\over R^{12}} - {B \over R^6}

  1. Use this formula to calculate dVdR\displaystyle \frac{{\rm d}V}{{\rm d}R} as a function of RR.

  2. Show that the potential where the gradient is zero is V(R)=B24A\displaystyle V(R)=\frac{-B^2}{4A}.

  3. Find mathematically whether this point is a maximum, minimum or point of inflexion.

Main problems 3

The number nn (in thousands) of bacteria on an agar plate at time tt days is given by the expression: n=21.35+1.34tt2n = 21.35 + 1.34t - t^2

  1. Draw a graph of the function n(t)n(t) between t=0t=0 and t=7t=7 days. Give one reason why this function might be a reasonable model for the number of bacteria on the plate at time tt. Are there any values of tt for which this is probably not a good model?

  2. Calculate the time at which the greatest number of bacteria are present on the plate and show that this must be a maximum number.

  3. By finding the roots of the equation for nn, find the two times at which the value of nn is zero and say why only one of these times is physically reasonable. Mark and label the maximum point on your graph together with the point at which the number of bacteria is zero.

  4. Find the rates at which the bacteria are growing when t=0.8t=0.8 and t=3.5t=3.5 days.

Main problems 4

Being able to change direction rapidly helps fish to avoid predators. We can describe the distance xx (in cm) travelled by time tt seconds after a stimulus by x=ktb,x = kt^b, where for rainbow trout k=300cms1.6k=300\,{\rm cm}\,{\rm s}^{-1.6}, b=1.60b=1.60, and for green sunfish k=210cms1.71k=210\,{\rm cm}\,{\rm s}^{-1.71}, b=1.71b=1.71.

  1. Compare the distance travelled, and the instantaneous velocity and acceleration for these species at t=0.1t=0.1\,s.

  2. Compare the velocities in fish lengths per second, given that the lengths of trout and sunfish are 14.414.4\,cm and 8.08.0\,cm respectively, commenting on your answer.

Main problems 5

A researcher measured the concentration cc of a protein _in vitro* and obtained the readings below:

time (min)0123456
c (mM)11.917.064.402.571.811.030.72

She surmised that the protein was being degraded according to the reaction scheme

AkA\xrightarrow{k}

under mass action kinetics.

  1. Use a suitable transformation to draw a straight-line graph of the data, in order to test her hypothesis.

  2. Find the maximum rate of decay and the time at which this occurs.

  3. Find the concentration of protein remaining after 10 minutes.

Extension problems

Extension problems 1

Find the values of xx for which the following functions have stationary values and using your results, sketch a graph of each function:

  1. y=2exx\displaystyle y = 2e^x - x

  2. y=ex2x1\displaystyle y = e^x - 2x - 1

Use Python to check your results.

Extension problems 2

The graph shows the rate of CO2*{2} emissions per year since 1800, with three fitted lines in red.

CO2 graph

A climate scientist thinks that a quadratic curve could be a better fit to the data, with the x-axis as years since 1850, and the y-axis as the rate of CO2_{2} emissions per year. The curve would have the equation

y=Ax2+Bx+C\displaystyle y = Ax^{2} + Bx + C

  1. By using the points (10,3),(106,14),(166,42)(10,3), (106,14), (166,42) taken from the best fit lines, evaulate the coefficients AA, BB, and CC in this model.

  2. Find the minima of this quadratic curve. Use this to assess the suitability of the quadratic model as a fit for the data.

Extension problems 3

A protein degrades according to the formula

p(t)=12kt+1P0p(t) = \frac{1}{2kt + \frac{1}{P_0}}

where pp is the protein concentration, P0P_0 is the initial concentration, kk is a constant, and tt is time.

Find the rate for this reaction and deduce a plausible reaction schema. Hint: you may find it useful to express the derivative in terms of p(t)p(t).

Extension problems 4

From first principles (as in the first question on this sheet) prove the formulas for differentiation of sums, differences and scalar multiples.