8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 1/24 PART III SPREADS1 IEET MATHEMATICS Excel for Chemists: A Comprehensive Guide. E. Joseph Billo Copyright 2001 by John Wiley & Sons, Inc. ISBNs: 0-471-39462-9 (Paperback); 0-471-22058-2 (Electronic)
24
Embed
9 Some Mathematical Tools for Spreadsheet Calculations
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 175
0.600
s 0.590
i 0.580
5: 0.570
0.600
Q) 0.590ii!
2 0.580
ti2 0.570
a 0.560
0.550
390 400 410
Wavelength, nm
420
a 0.560
0.550
Wavelength, nm
420
Figure 9-9. (Left) Chart created using data points onlr
.interpolated using a cubic
(Right) Chart with smooth curveinterpo ation function.
The cubic interpolation function forces the curve to pass through all the
known data points. If there is any experimental scatter in the data, the result will
not be too pleasing, A better approach for data with scatter is to find the
coefficients of a least-squares line through the data points, as described in
Chapter 11 or 12.
NUMERICAL DIFFERENTIATION
The process of finding the derivative or slope of a function is the basis of
di~~~~ti~Z ~~1~~1~s. Since you will be dealing with spreadsheet data, you will be
concerned not with the algebraic differentiation of a function, but with obtaining
the derivative of a data set or the derivative of a worksheet formula by numeric
methods.
Often a function depends on more than one variable. The p~~ti~Z d~ri~~ti~e ofthe function F(x,y,z), e.g., SF/&x, is the slope of the function with respect to x,
while y and z are held constant.
FIRST AND SECOND DERIVATIVES OF A DATA SET
The simplest method to obtain the first derivative of a function represented
by a table of x, y data points is to calculate Ay/Ax. The first derivative or slope of
the curve at a given data point x~, yn can be calculated using either of the
following formulas:
4!slope = ti = Yn+l -Yn%z+l -%I
(9-2)
slope =Yn - Yn-1
x7-l - %-1P-3)
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Figure 9-10. First derivative of titration data, near the end-point.
The second derivative, d2y/dx 2, of a data set is calculated in a similar
manner, namely by calculating A(Ay/~) /AX.
Calculation of the first or second derivative of a data set tends to emphasize
the “noise” in the data set; that is, small errors in the measurements become
relatively much more important.
Points on a curve of X, y values for which the first derivative is either amaximum, a minimum or zero are often of particular importance and are termed
critical points.
The spreadsheet shown in Figure 9-10 uses pH titration data to illustrate the
calculation of the first derivative of a data set .
1.500 2.000 2.500
V, mL
Figure 9-11. First derivative of titration data, near the end-point.
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
There are more sophisticated equations for numerical differentiation. These
equations use three, four or five points instead of two points to calculate the
derivative. Since they usually require equal intervals between points, they are ofless generality. Their main advantage is that they minimize the effect of “noise”.
DERIVATIVES OF A FUNCTION
The first derivative of a formula in a worksheet cell can be obtained with a
high degree of accuracy by evaluating the formula at x and at x + AX. Since Excel
carries 15 significant figures, AX can be made very small. Under these conditions
AF/Ax approximates d.F/dx very well.
The spreadsheet fragment shown in Figure 9-14 illustrates the calculation ofthe first derivative of a function (F = x3 - 3x2 - 130x + 150) by evaluating the
function at x and at x + Ax. Here a value of Ax of 1 x 10mg was used; alternatively
Ax could be obtained by using a worksheet formula such as =I E-9*x. For
comparison, the first derivative was calculated from the exnression from
diffeiential calculus: F’ = 3x2 - 6x - 130.1
The Excel formulas in cells B12, Cl 2, D12 and El 2 are
= t*xA3+u*xA2+v*x + w
= t*(x+delta)A3 +u*(x+delta)A2 +v*(x+delta) + w
Figure 9-14. Calculating the first derivative of a function.
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 179
=(C12-B12)Melta
=3*t*xA2+2*u*x+vFigure 9-15 shows a chart of the function and its first derivative.
800
600
400
4sz 200‘C I
ti 0
u,
-200
-400
-600
ILI.II
-10 -5 0
X
Figure 945. The function F = x3 - 3x2 - 130x + 150 and its first derivative.
NUMERICAL INTEGRATION
A common use of numerical integration is to determine the area under acurve. We will describe three methods for determining the area under a curve:
the rectangle method, the trapezoid method and Simpson’s method. Each
involves approximating the area of each portion of the curve delineated by
adjacent data points; the area under the curve is the sum of these individual
segments.
The simplest approach is to approximate the area by the rectangle whoseheight is equal to the value of one of the two data points, illustrated in Figure 9-
16.
As the x increment (the interval between the data points) decreases, thisrather crude approach becomes a better approximation to the area. The area
under the curve bounded by the limits xinitial and xfinal is the sum of the
individual rectangles, as given by equation 9-5.
area = Z y&i+1 - xi) P-5)
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Figure 9-16. Graphical illustration of methods of calculating the area unaer a curve.
2.5
For a better approximation you can use the average of the two y values as the
height of the rectangle. This is equivalent to approximating the area by a
trapezoid rather than a rectangle. The area under the curve is given by equation
9-6 .
area = C yi +:i+l (Xi+1 - Xi) P-6)
Simpson’s rule approximates the curvature of the function by means of aquadratic equation. To evaluate the coefficients of the quadratic requires the use
of the y values for three adjacent data points. The x values must be equally
spaced.
area =c Yi + 4Yi+l + Yi+2
6(Xi+1 - Xi) P-7)
AN EXAMPLE: FINDING THE AREA UNDER A CURVE
The curve shown in Figure 9-17 is the sum of two Gaussian curves, withposition and standard deviation of = 90, CT= 10 and ~=130, CT= 20, respectively.
The equation used to calculate each Gaussian curve is
Y=expf -(x - p)2/202]
cn/5iP-8)
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
The area increments were summed and the area under the curve, calculated
by the three methods, is shown in Figure 9-19. All three methods of calculation
appear to give acceptable results in this case.
Figure 9-19. Area under a curve, calculated by three different methods.
DIFFERENTIAL EQUATIONS
Certain chemical problems, such as those involving chemical kinetics, can be
expressed by means of differential equations. For example, the coupled reaction
scheme
kl k3A-Be c
k2 k4results in the simultaneous equations
aA1- = -kl[A] + k;lfB]
dt
dEBI- = kl[A] - k2[B] - k$B] + k4[C]
dt
4Cl- = k$B] - k&]
dt
To “solve” this system of simultaneous equations, we want to be able tocalculate the value of [A], [B] and [C] for any value of t. For all but the simplest
of these systems of equations, obtaining an exact or analytical expression is
difficult or sometimes impossible. Such problems can always be solved by
numerical methods, however. Numerical methods are completely general. They
can be applied to systems of differential equations of any complexity, and they
can be applied to any set of initial conditions. Numerical methods require
extensive calculations but this is easily accomplished by spreadsheet methods.
In this chapter we will consider only ordinary differential equations, that is,
equations involving only derivatives of a single independent variable. As well,
we will discuss only initial-value problems - differential equations in which
~formation about the system is known at t = 0. Two approaches are common:
Euler’s method and the Runge-Kutta (RK) methods.
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 183
EULER’S METHOD
Let us use as an example the simulation of the first-order kinetic processA --I,B with initial concentration Co = 0.2000 mol/L and rate constant k = 5 x 10”
WIs l
We’ll simulate the change in concentra~on vs. time over the interval from t =
0 to t = 600 seconds, in increments of 20 seconds.
The differential equation for the disappearance of A is d[A]/dt = -k[A].
Expressing this in terms of finite differences, the change in concentration A[A]
that occurs during the time interval from t = 0 to t = At is A[A] = -k[A]At. Thus, if
the concentration of A at t = 0 is 0.2000 M, then the concentration at t = 0 + At is
[A] = 0.2000 - (5 x 10*3)(0.2000)(20) = 0.1800 M. The formula in cell 87 is
=B6-k*BG*DX.
The concentration at subsequent time intervals is calculated in the same way.
The advantage of this method, known as Euler’s method, is that it can be
easily expanded to handle systems of any complexity. Euler’s method is not
particularly useful, however, since the error introduced by the approximation
d[A]/dt = A[A]/At is compounded with each additional calculation. Compare
the Euler’s method result in column B of Figure 9-20 with the analytical
expression for the concentration, [A]t = [A]Oewkt, in column C. At the end of
approximately one half-life (seven cycles of calculation in this example), the errorhas already increased to 3.6%. Accuracy can be increased by decreasing the size
of At, but only at the expense of increased computation. A much more efficient
way of increasing the accuracy is by means of a series expansion. The Runge-
Kutta methods, which we describe next, comprise the most commonly
approach.
0.1482
0.1341
Figure 9-20. Euler’s method.
used
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
The Runge-Kutta methods for numerical solution of the differential equationdy/dx = F(x, y) involve, in effect, the evaluation of the differential function at
intermediate points between xi and xi+l. The value of yi+l is obtained by
appropriate summation of the intermediate terms in a single equation. The most
widely used Runge-Kutta formula involves terms evaluated at xi, xi + Ax/2 and
xi + AX. Thefourth-order Runge-Kutta equations for dy/dx = F(x, y) are
Tl + 2T2 + 2T3 + T4Yi+l = yi +
6 (9-9)
where Tl = F (xi yi) AX (9-10)
T2 F(Ax Tl-- xi+- ,yi+- Ax2 2 >
T3Ax T2--
F( xi +-, yi +-2 2 ) AX
(9-11)
(9-12)
T4 = F (Xi + AX, J/i + T3) AX (9-13)
If more than one variable appears in the expression, then each is corrected by
using its own set of T1 to T4 terms.
The spreadsheet in Figure 9-21 illustrates the use of the RK method tosimulate the first-order kinetic process A + B with initial concentration [A]0 =
0.2000 and rate constant k = 5 x 10m3. Th e 1d’ff erential equation is d[A]f/dt =
-k[A]t. This equation is one of the simple form dy/dx = F(y), and thus only the yi
terms of T1 to T4 need to be evaluated. The RK terms (note that T1 is the Euler
Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 185
l-1 = -kfA]t Ax (9-14)
l-2 = -k([A]t + Q/2) Ax (9-15)
T3 = -k([A]t + 7’2/2) Ax (9-16)
T4 = -k([A]t + I-3) Ax (9-17)
The RK equations in cells 87, C7, 07, E7 and F7, respectively, are:
=-k*FG*DX
=-k*( FG+TAI l2)“DX
=-k*( F6+TA2/2)*DX
=-k*(FG+TA3)*DX
If you use the names TAl ,..*, TA4, TBl,..., TB4, etc., you’ll find that (i) the
nomenclature is expandable to systems requiring more than one set of Runge-
Kutta terms, (ii) the names are accepted by Excel, whereas Tl is not a valid name,
and (iii) you can use AutoFill to generate the column labels TAI ,..., TA4.
Compare the RK result in column F of Figure 9-21 with the analytical
expression for the concentration, [A]t = [A]gemkt, in column G. After one half-life(row 13) the RK calculation differs from the analytical expression by only
0.00006%. (Compare this with the 3.6% error in the Euler method calculation at
the same point.) Even after 10 half-lives (not shown), the RK error is only
0.0006%.
If the spreadsheet is constructed as shown in Figure 9-21, you can’t use a
formula in which a name is assigned to the concentration array in column F. This
is becasue the formula in 87, for example, will use the concentration in F7 instead
of the required F6. An alternative arrangement that permits using a name for the
concentration [A]t is shown in Figure 9-22. Each row contains the concentrationat the beginning and at the end of the time interval. The name A t-
Figure 9-22. Alternative spreadsheet layout for the Runge-Kutta method.
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 187
:
Qn2*.*
.:0
arnn
The values comprising the array are called matrix elements. Mathematical
operations on matrices have their own special rules.
A s9~~~e rn~t~i~ has the same number of rows and columns. If all the
elements of a square matrix are zero except those on the main diagonal (all,a22,-9, @nn), the matrix is termed a diagonal matrix. A diagonal matrix whose
diagonal elements are all 1 is a unit matrix.
A matrix which contains a single column of m rows or a single row of n
columns is called a nectar.
A determinant is simply a square matrix. There is a procedure for the
numerical evaluation of a determinant, so that an N x N matrix can be reduced to
a single numerical value. The value of the determinant has properties that make
it useful in certain tests and equations. (See, for example, “Solving Sets of
Simultaneous Linear Equations” in Chapter 10.)
AN INTRODUCTION TO MATRIX ALGEBRA
Matrix algebra provides a powerful method for the manipulation of sets of
numbers. Many mathematical operations - addition, subtraction,
multiplication, division, etc. - have their counterparts in matrix algebra. Our
discussion will be limited to the manipulations of square matrices. For purposes
of illustration, two 3 x 3 matrices will be defined, namely
and
s
V
Y
t
1I
2
w = 0
z 3
0
3
2
2
3
11
The following examples illustrate addition, subtraction, multiplication and
division using a constant.
Addition or subtraction of a constant:
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations
You may occasionally need to chart a action that involves angles. Insteadthe familiar Cartesian coordinate system (x, y and z coordinates), such
often use the polar coordinate system, in which the coordinates are two
angles, 8 and (p, and a distance r. The two coordinate systems are related by the
equations x = r sin 8 cos +, y = r sin 0 sin Q, 2 = OS 8. Angle 8 is the anglebetween the vector r and the Cartesian z axis is the angle between the
projection of r on the X, y plane and the x axis. Since Excelk trigonometric
functions only consider X- and y axes, the simplified relationships are, for angles
inthex,yplane:x=rcos~,y=rsinQ.
As an example of transformation of polar to Cartesian coordinates, we’llgraph the wave unction for the d~2-~2 orbital in the X, y plane. The angular
component of the wave unction in the X, y plane is
(9-18)
and Q, can be equated to the radial vector r for the conversion of polar to
Cartesian coordinates.
In the spreadsheet fragment shown in Figure 9-23, column A contains anglesfrom 0 to 360 in 2-degree increments.. Column B converts the angles to radians
(required by the COS worksheet function) using the relationship = A4 * P I () / 18 0
in row 4. The formulas in cells C4,04 and E4 are:
=SQRT( 1 ~I( 1 ~~P~()))*COS(Z*
=c4*cos(E34)
=C4*SIN(B4).
The chart of the x and y values is shown in Figure 9-24.
Figure 9-23, Converting from polar to Cartesian coordinates
8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations