You are on page 1of 16

III.

 Differential Calculus 

Derivative 
The derivative of a function, F(x), is the instantaneous change of the function with respect to the
instantaneous change in the variable(s). Another way of saying this is that the derivative is the slope of a
tangent line to the curve defined by the function. Since the tangent line varies with the choice of variable,
the derivative is itself a function

The derivative is given several symbols, but the mathematical equation that defines it (the so-called limit
definition) is:

dF ( x) F ( x  x)  F ( x)
F ' ( x)  Fx ( x)   lim
dx x  0 x

We will not discuss limits too much, since they are never used to actually compute derivatives.
Just remember that the limit is the value that the function f(x) approaches or converges to when x gets
closer to some particular point, or infinity.
That means the function can have a limit even at points where it does not exist, or can have a completely
different value and limit.
However, for a continuous function, the limit equals the function value, i.e.

lim f ( x)  f (a)
xa

The definition of the derivative by the limit in the above equation is also how it is actually done
numerically (on the computer), so keep it in mind.
 

1
Second derivative 
The derivative of a function is itself a function. The derivative of this new function is the "second
derivative" of the initial function. The second derivative describes how the slopes of tangent lines change
as one moves from low x to high x from a given point.

F ' ' ( x)  Fxx ( x) 


d 2 F ( x)
 lim
F ( x  x)  F ( x)  F ( x)  F ( x  x)
dx 2 x  0 x 2
F ( x  x)  F ( x  x)  2 F ( x)
 lim
x 0 x 2

Again not this last equation for later numerical calculations of second derivatives.

The Differential of a Function   
The derivative is the change in the function divided by the change in the variable. But, if we wanted to
know the amount by which the function changes as a result of changing the variable, we are asking about
the differential of the function, dF(x):
d F(x)
d F(x) = dx
dx
This seemingly simple idea implies that the derivative IS a fraction!!

 
Differentiation formulas 
 
Derivatives of Sums: When two function are added, the derivative of the sum is equal to the sum
of the derivatives:
d F(x) + G(x) d F(x) d G(x)
= +
dx dx dx

Derivatives of Products: When two functions are multiplied, the differentiation is done in the
following way:
d F(x)•G(x) d F(x) d G(x)
= G(x) + F(x)
dx dx dx
This is usually called the product rule for differentiation.

The Quotient Rule: A special case of the product rule is when the one of the functions is divided by
the other, i.e. F(x)/G(x). To do this, we will write the quotient as:
F(x)
= F(x) [G(x)]–1
G(x)
This allows us to recognize that, indeed, this is a product. Therefore, the derivative is:
d F(x)• G(x) –1 d F(x) d G(x) –1
= G(x) –1 + F(x)
dx dx dx

What do we do about d[G(x)]–1/dx? This falls into the category of a function of a function. Therefore,
we can write:
d G(x) –1 d G(x) –1 d G(x) d G(x)
= = –1 G(x) –2
dx d G(x) dx dx
2
Thus, the derivative of the quotient is:
d F(x)• G(x) –1 d F(x) d G(x)
= G(x) –1 + F(x) –1 G(x) –2
dx dx dx
d F(x) d G(x) G(x) Fx (x) – F(x) Gx (x)
= G(x) –2 G(x) – F(x) =
dx dx G(x)2

The last expression is known as the "quotient rule", although it is nothing more than the product rule with
the chain rule.

Derivatives of simple functions: Every derivative can be solved using the limit definition, but
the process tends to be tedious. Several general results have been found that should be second nature.
Here is a list of functions whose derivatives should be known:

d (x p ) d (sin( x))
 px p 1 for any p (integer or not)  cos( x)
dx dx
d (e x ) d (cos( x))
 ex   sin( x)
dx dx
d (a x ) d (tan( x)) 1
 a x ln(a ) 
dx dx cos 2 ( x)
d (ln( x)) 1 d (cot( x )) 1
  2
dx x dx sin ( x )
d (log a ( x)) 1

dx x ln(a )

Chain Rule: Many functions are so-called "composite functions", meaning they are functions of
functions, which we can represent by G(F(x)). To take the derivative of G with respect to x requires us to
first differentiate G with respect to F, then differentiate F with respect to x. This is called the chain rule,
and it is written:
d G(F(x)) d G(F(x)) d F(x)
=
dx d F(x) dx
In practice, the function F(x) is usually set equal to another variable, say F(x) = y. This gives G(F(x)) =
G(y). This allows us to write the more common form of the chain rule:
d G(y) d G(y) d y
=
dx dy dx

Once again, we can see that the derivatives are fractions!

3
Implicit Differentiation: Not all functions are written in terms of y = F(x). Some functions, for
example the function describing a circle, is a function of both x and y. It is possible to differentiate the
entire function with respect to x. In doing so, we will use the chain rule for the y = y(x) dependency, and
solve for the exact form of dy/dx.
d F(x,y) d f(x) d g(y) d y
F(x,y) = f(x) + g(y)  = +
dx dx dy dx

d y d F(x,y) d f(x) d g(y)


 = – /
dx dx dx dy
Example - The equation of a circle is x2 + y2 = r2, where r is a constant (the radius
of the circle). Taking the derivatives gives:
d (x2 ) d (y2 ) d (y2 ) d y d y d (r2 )
+ = 2x + = 2x + 2y =
dx dx dy dx dx dx

Since r is a constant, we can write it as r•x0, which when we take its derivative gives 0•r•x0–1 = 0. Thus,
we have:
dy dy dy
2x + 2y = 0  2y = –2x  = – 2x = – x
y
dx dx dx 2y
In other words, it depends on both x and y. Of course we could try to determine the function y=y(x) and
differentiate it, but in most cases, this is much more complicated

Note 1: We can always find functions in terms of x and y and differentiate implicitly.

Note 2: We could have also differentiated with respect to y instead of x and arrive at dx/dy implicitly.
But, dx/dy = 1/[dy/dx], so the end result will have the same meaning.

Example ­ Differentiating an inverse trigonometric function: 
Functions like arcsin (sin–1) and arccos (cos–1) can be differentiated using the relationship
dy/dx = (dx/dy)–1. This example demonstrates this for y = arctan(x):
sin(y)
y = tan–1 (x)  tan(y) = tan(tan–1 (x)) = x  x = tan(y) =
cos(y)
sin(y) cos(y)[cos(y)] – sin(y)[–sin(y)] cos (y) + sin2(y)
2
 dx = d = =
dy dy cos(y) cos2 (y) cos2 (y)
sin2 (y) dy
=1+ = 1 + tan2(y) = 1 + x2  = 1
cos2(y) dx 1 + x2

General rule for derivatives of inverse functions:

d ( f 1 ( x)) 1 1
 
dx f  y  df ( y ) dy

Example:

d arcsin( x) 1 1 1 1
     
dx d sin( y ) / dy cos( y ) 1  sin ( y )
2
1 x2

4
Extrema (maxima and minima) or functions 
Since the derivative is the slope of a tangent line, the point when the derivative equals zero (i.e. F'(x) = 0)
has a tangent line that is horizontal. If the derivative at one of the points immediately to the side of the
horizontal is a positive, and at the other it is a negative, the horizontal corresponds to a "local extremum".
There are two possible extrema: maximum and minimum.

If the second derivative is negative, then the function at the extremum is at a MAXIMUM.

If the second derivative is positive, the extremum is a MINIMUM.

If the second derivative is zero, the point represents an INFLECTION POINT, i.e. between a max and a
min., where the function changes shape (from “convex” to “concave” or vice versa)

Example: Find r where the Morse potential function has a minimum.
Morse potential is given by


V ( r )  De 1  e  r  re  2

Here, De is the dissociation energy (which is always positive) and   k / 2 De where k is harmonic
force constant (also positive).

Condition for minimum:

d
dr
d

V (r )  De 1  e  r  re 
dr
2
0

  
2 De 1  e  r  re   e  r  re      0
2 Dee   r  re 
1  e   r  re 
 0
e   r  re 
1  e  r  re 
 0

5
  r  re 
Because e is always greater than zero (remember the exponential functions - we talked about
them so much for a reason) the square bracket must be zero:

1  e  r re   0

or e  r re   1

taking a logarithm of both sides:


  ( r  re )  0

which finally gives r = re

The Morse potential has a minimum at re !

Is it really a minimum? To confirm that, calculate the second derivative and show that it is positive at re:

d2
dr 2
V (r ) 
d
dr
  
2 De e  r  re  1  e  r  re  
   
 2 De  e  r  re  1  e  r  re   e  r  re  e  r  re  
  r  re 
for r = re e  e 0  1 so substituting simplifies things:

d2
2
V (r )  2 De   (1  1)  1   2 De 2  k
dr rr e

which is always positive, so we indeed found a minimum.

Taylor series expansion 

Any function f(x) can be expanded into a polynomial series around some point x0 as:

df ( x) 1 d 2 f ( x) 1 d 3 f ( x) 1 d n f ( x)
f ( x)  f ( x0 )  ( x  x0 )  ( x  x0 ) 2  ( x  x0 ) 3  ...  ( x  x0 ) n  ...
dx x  x0 2 dx 2 x  x0
6 dx 3 x  x0
n! dx n x  x0


1 d n f ( x)
 n
( x  x0 ) n
n  0 n! dx x  x0

where x x0
means that the derivative is taken at x0. Note that the series is infinite. But for x close to x0 the
higher powers of (x  x0) get smaller and smaller and the series typically converges very quickly. As a
result, to approximate the value of a function close to some point, the series can be truncated after the first
few terms without much loss in accuracy. Taylor expansion is therefore often used to approximate
complicated functions by a polynomial, commonly linear or quadratic function. But remember - it works
only for small (x  x0) !
6
Some useful Taylor expansions:

for small x ( |x| << 1)


1 2 1 3 1
ex  1 x  x  x  ...  x n  ... ex  1 x
2 6 n!
1 3 1 5 1 7 sin( x)  x
sin( x)  x  x  x  x  ...
6 5! 7!
1 2 1 4 1 6
cos( x )  1  x  x  x  ... cos( x )  1 
1 2
x
2 4! 6! 2
1 2 1 3 1 4 ln(1  x)  x
ln(1  x)  x 
x  x  x  ...
2 3 4!
1 1 2 1 3 5 4
1 x  1 x  x  x  x  ... 1 x  1
1
x
2 8 16 128 2
1
 x  x 2  x 3  x 4  x 5  ... 1
1 x x
1 x
1
 1  2 x  3 x 2  4 x 3  5 x 4  ... 1
 1  2x
1  x 2
1  x 2
 

7
Numerical differentiation 
As we have already discussed multiple times, but keep discussing, numerically a function is an array of
point for x with the corresponding array of points for y. That means that all the operations, including
derivatives, will be operations on arrays of points.
The points are spaced by finite steps and the derivative therefore must be approximated by finite
differences:

Over the whole interval, we can write the derivative as finite differences over all points. If

y= [y(1) y(2) y(3) y(4) …. y(N-1) y(N)]

dy = [y(2)-y(1) y(3)-y(2) y(4)-y(3) … y(N)-y(N-1)]

If our x is equally spaced, which is often the case (especially when we generate it in MATLAB using
a:b:c or linspace) all (xk+1  xk) will be the same, so we can call

dx = x(2)-x(1)

and the numerical derivative becomes

dy/dx

8
We know derivative is a function, precisely defined for each point x where it is calculated.
There is a slight problem when calculating finite differences: you need two points k and k+1 of the
function to get one point finite difference. Which x should it go with? k or k+1?

Rigorously, because we are doing finite differences, the derivative is an average over the interval
bounded by the two points. The natural place to put this would therefore be right in the middle between
the points k and k+1.

Also as you can see, from both the equations above and the figure, you will get one less point when you
calculate the derivative than the number of points you had in your original function. Therefore, to
represent the numerical derivative, we would have to “rescale” the original x-axis, in addition to
calculating all numerical differences.
In MATLAB, the numerical derivative (finite differences) can be written simply, taking advantage of all
the gymnastics possible with arrays:

>> x=[-5 -4 -3 -2 -1 0 1 2 3 4 5];


x =
-5 -4 -3 -2 -1 0 1 2 3 4 5
>> y=x.^3;
y =
-125 -64 -27 -8 -1 0 1 8 27 64 125
>> dy=y(2:end)-y(1:end-1)
dy =
61 37 19 7 1 1 7 19 37 61
>> dx=x(2)-x(1)
dx =
1
>> dy_dx=dy/dx
dy_dx =
61 37 19 7 1 1 7 19 37 61
>> der_y=dy/dx
der_y =
61 37 19 7 1 1 7 19 37 61

Notice we have one less point in der_y than we had in y (or x).
>> length(y)
ans =
11
>> length(der_y)
ans =
10
9
Now we can plot the function, we have both x and y:

>> plot(x,y,'x-');
(plotted as connected blue crosses)

but to plot the derivative, we cannot use x and der_y, because they have difference numbers of points:

>> hold on
>> plot(x,der_y,'o-r');
??? Error using ==> plot
Vectors must be the same lengths.

We have to redefine x, but putting it the middle between the original points. That is done by “averaging”
each x point with the next: (x(1)+x(2))/2, (x(2)+x(3))/2, …
We can again write this very simply:

>> der_x=0.5*(x(1:end-1)+x(2:end))
der_x =
Columns 1 through 7
-4.5000e+000 -3.5000e+000 -2.5000e+000 -1.5000e+000 -5.0000e-001
5.0000e-001 1.5000e+000
Columns 8 through 10
2.5000e+000 3.5000e+000 4.5000e+000

And now we can plot away:

>> plot(der_x,der_y,'o-r');
>> grid on

10
It turns out, however, that you do not have to do all this. The numerical derivative is already pre-
programmed in MATLAB for you and called diff(). The following commands would do exactly the
same as we did “by hand” above:

>> dy=diff(y)
dy =
61 37 19 7 1 1 7 19 37 61

In fact, diff will do any order of finite differencing: diff(y,n) will calculate nth order.
For example, if I wanted second derivative

>> d2y=diff(y,2)
d2y =
-24 -18 -12 -6 0 6 12 18 24

Note that we lost a point again so we would have to “rescale x” again and then again for each higher
derivative.

However, it turns out that you do not have to do even that. There is a function in MATLAB which will
calculate the numerical derivatives and keep the number of points the same. This function, which I
strongly recommend to use, is called gradient().

Here we go again with our previous example:

>> x=[-5 -4 -3 -2 -1 0 1 2 3 4 5]
x =
-5 -4 -3 -2 -1 0 1 2 3 4 5
>> y=x.^3
y =
-125 -64 -27 -8 -1 0 1 8 27 64 125
>> dy=gradient(y)
dy =
61 49 28 13 4 1 4 13 28 49 61
>> dx=x(2)-x(1)
dx =
1
>> dy_dx=dy/dx
dy_dx =
61 49 28 13 4 1 4 13 28 49 61
>> figure(1);clf reset
>> plot(x,y,'x-k')
>> hold on
>> plot(x,dy_dx,'o--r')

and you will get

11
To calculate numerical derivatives, use gradient()function.
But remember to not forget to divide by x !!!!
You can put everything in one general command to calculate a numerical derivative:

>>dy_dx=gradient(y)/(x(2)-x(1));

Provided that you have evenly spaced x points.

To calculate higher derivatives, call gradient multiple times:

>>d2y_dx2=gradient(gradient(y))/(x(2)-x(1))/(x(2)-x(1));

and do not forget to divide by x2 !!!

12
Maxima and minima 
On paper, you look for extrema (maxima, minima) by calculating derivatives and setting them to zero.
On the computer, with a numerical representation of the function you do not have to do that. Since you
have an array (y) that represents your function, you can just look for the element of the array which has
the greatest value. The you find the corresponding element of the x array, which will tell you where the
maximum is.

Simple example: 

let’s say this is my (numerical) function:


x=[0 1 2 3 4 5 6 7 8 9 10]
y=[0 1 2 4 8 16 8 4 2 1 0]

The maximum is 16, it is the 6th element of the array 6 (its index is 6) and it corresponds to the 6th element
of x, which is 5. Therefore the function has a maximum at x =5 (and the maximum value is 16)

To find maxima, we can use the MATLAB function max()


To find minima, there is a MATLAB function min().
They both works the same way, the difference is obvious: max find the greatest value, min the smallest.

Both max and min return the value (greatest, smallest) of the array, but can also return their indices. That
is how we can use them to find the positions of the maxima and minima:

13
[max, imax]=max(y)

Will give you the maximum value in the FIRST variable (max), and its index in the SECOND variable
(imax) in the square bracket.
(Note that it is always the FIRST and the SECOND what matters, not how you call them!)

>> [max,imax]=max(y)
max =
16
imax =
6

Now I can use imax (the index of the maximum element) to find its position on x.

>> x(imax)
ans =
5

My maximum is therefore indeed at x = 5.

For a minimum, it would work the same way:

>> x=[0 1 2 3 4 5 6 7 8 9 10];


>> y=[15 10 6 3 1 0 1 3 6 10 15];
>> plot(x,y,'-o')
>> plot(x,y,'-o')
>> [v,index]=min(y)
v =
0
index =
6
>> min_on_x=x(index)
min_on_x =
5
>>

This will work if you have one maximum (minimum) or searching for the global one. However,
remember that this is only an approximate extremum of your function. Always keep in mind that the
function is discretized, i.e. given by a finite number of points. You may not have a point exactly where
the true minimum/maximum is. That means to get the maximum/minimum accurately, you need
reasonably fine “grid” (point spacing).

There are more sophisticated tools to find maxima and minima, for example the MATLAB function
fminbnd().

14
Taylor expansions 

From what we just learned we should be able to construct Taylor expansions of any function at any point
(except the end points) and of any order. Typically, we do Taylor expansions need the minima (or
maxima).
On of the most important Taylor expansions is the so-called HARMONIC APPROXIMATION, which
his nothing else but an expansion of a potential function near its minimum truncated after the quadratic
term. (The quadratic term is really the leading term at the minimum, because the linear term is always
zero why do you think that is?)

As an example, consider some arbitrary potential V, for which I have a function (named potential)
written somewhere:

>> x=0:250;
>> V=pontential(x);
>> plot(x,V);xlabel('x');ylabel('V');

Now I need to find a minium:

>> [min,ind]=min(V)
min =
-2.3778e-002
ind =
117

Now I need to calculate the second derivative AT THE MINIMUM (remember I do not need the first
derivative, because at minimum it is zero). I do it by using the gradient() function twice and then
taking my second derivative at the point with the index ind that I just found to be the point where my
minimum is.

15
I also need to calculate x to get my derivative (never forget to divide by those when computing
derivatives)

>> dx=x(2)-x(1);
>>d2V=gradient(gradient(V))/dx/dx;

2
And now my Taylor expansion: f ( x)  f ( x0 )  1 d f ( x) ( x  x0 ) 2
2 dx 2 x  x
0

(remember, the x0 is now the minimum, and that is given by x(ind) where ind is the index of my
minimum);

>> Taylor=V(ind)+0.5*d2V(ind)*(x-x(ind)).^2;

And now I can plot it over my original function to see how well it approximates my potential V:

>>hold on;
plot(x,Taylor,’--r’);

You can see the quadratic function is a really good approximation close to the minimum, but get worse as
we get further away. That is why you will hear in your P-chem that harmonic approximation is good for
small displacements from equilibrium position (small amplitude vibrations).

16