9 Some Mathematical Tools for Spreadsheet Calculations

24
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)

Transcript of 9 Some Mathematical Tools for Spreadsheet Calculations

Page 1: 9 Some Mathematical Tools for Spreadsheet Calculations

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)

Page 2: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 2/24

9SOME MATHEMATICAL TOOLS

FOR SPREADSHEET CALCULATIONS

This chapter describes some mathematical methods that are useful forspreadsheet calcula~ons. For the most part, they are methods that are applicable

to tables or arrays of data.

LOOKING UP VALUES IN TABLES

The VLOOKUP(/ookup value,- array, co/umnJ7dex~num,

match- type-logical) worksheet function is useful for obtaining values from

tables This section shows how to use VLOOKUP to obtain a value both from a

one-way table and a two-way table.

GETTING VALUES FROM A ONE-WAY TABLE

The spreadsheet shown in Figure 9-1, containing a list of element symbols and

atomic weights, is part of a one-way table (the table entries extend down to row

110). In a one-way table, the lookup values occupy a single row or column of the

table (here, the lookup values are found in column B).

We want to obtain the atomic weight of a particular element from the table, in

order to use it in a formula. The formula =VLOOKUP(symbol,$B$2:$D$110,2,O)returns the atomic weight corresponding to symbol. The formula

=VLOOKUP(symbo~,$B$2:$D$l10,3,0) returns the electron configuration.

1.00797 131

4.0026 ls2, I/ ". .LI. 6.939 [He] 231 :? <

Be 9.0122 [He] Z&ii ‘ ~E 10.811 [He] 2kiipl

c 12.01115 [He] 292 2p2

Figure 9-l. Portion of a data table of the elements.

169

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)

Page 3: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 3/24

170 Excel for Chemists

Figure 9-2. Portion of a data table of the elements.

Mien you use VLOOKUP, you must always “look up” in the first column of the

defined database and retrieve associated ~formation from a column to the right

in the same row; you cannot VLOOKUP to the left, for example. In the table shown

in Figure 9-2, you cannot use VLOOKUP to return the element name corresponding

to symbol. If you want to perform a lookup to the left of, or above, the lookup

value, you can either construct your own lookup-type function using the MATCH

function (see later in this chapter) or use the LOOKUP function.

GETTING VALUES FROM A TWO-WAY TABLE

A two-way table has two lookup values, usually in the lef~ost column and in

the top row. The value to be returned from the table is the value located at the

intersection of the row and column containing the two lookup values.

Once again we cm use VLOOKUP to obtain the value from the table. In the

preceding example, the value used for the column-index-num argument of

VLOOKUP was the fixed value 2, because we are always going to be returning a

value from column 2 of array. In the two-way table example shown in Figure 9-3,

column-index-n~rn is a variable; we must determine by means of an expression

the column from which the desired value is to be returned. The MATC t-l

expression can be used to obtain the relative column number in the table

corresponding to the lookup value.

To find the value in the table for T = 950°F and P = 5000 psia, the expression

=VLOOKUP(B44,$A$4:$P$34,~ATC~(B4~,$B$3:$P$3,1)+1 ,I)

where the T and P lookup values are entered in cells B44 and 845 respectively,

returns the value 0.035 for the viscosity. The same formula, using names instead

of references,

=VLOOKUP(T,T-Table,~ATC~(P,P-Ro~,l)+l ,l)

returns the value for the viscosity.

Page 4: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 4/24

z  c3 

i*J 

‘u 

0  r  cl

 cn ee 

I? 

F .::::. .s:: 

.,:.*. A:: 

:;‘i::;:... 

fgg 

T.&J 

/ p .,.,.: 

: :, ‘:’ . . :: 

.: ,. .: . . . . 

y . . . ..:. . 

$ggg 

:::@$g> 

&: :.s.:::.; 

. . . . . >.,:.:.:c 

g& 

ri~~~,.i 

@“# 

@J 

figj 

Page 5: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 5/24

172 Excel for Chemists

Iookup~value. The x values in the array must be in ascending order. Thus, for

any lookup value, you can use MATCH to find the pointer to the position of ~0,

the value of x in the table that is less than or equal to the lookup value, and then

use this pointer in the INDEX function to return values for x0, xl, yo and ~1, as in

the following formulas:

position

x0

Xl

YO=INDEX(YValues,position)

Y-l

The preceding formulas were applied in the example shown in Figure 9-4, to

perform table lookup with linear interpolation in the simple data table in A5:B8.

The intermediate values shown in B13:BW in Figure 9-5 were used to develop,

in a stepwise manner, the follow~g ~terpolation formula used in B18.

=y~O+(LookupValuemx~O)*(y~l~y~O)/(x~l~x~O)

A chart of the data with some interpolated values is shown in Figure 9-6.

Figure 9-4. Data table for interpolation.

Figure 9-5. Intermediate values for stepwise development of interpolation formula.

Page 6: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 6/24

Chapter 9 Some Ma~ematical Tools for Spreadsheet Calculations 173

700

600

500

400

300

200

100

Figure 9-6. Chart showing some interpolated values

The formulas for the ~termediate values in B13:B17 can be copied and

pasted into the interpolation formula in B18 to create a single “megaformula” (allin one cell, of course):

=INDEX(YValues,MATCH(LookupValue,XValues,l))+(LookupValue-lNDEX(

XValues,MATCH(LookupValue,XValues,l)))*(lNDEX(YValues,MATCH(

LookupValue,XValues,l)+l)-INDEX(YValues,MATCH(LookupValue,

XValues,l)))l(lNDEX(XValues,MATCH(LookupValue,XValues,l)+l)-

lND~X(XValues,MATCH(LookupValue,XValues,~)))

If the x values in the data table are in descending order, the match-type

argument in the MATCH function must be changed to -1.

INTERP~LAT~~NMETH~DS: CUBICSimple linear interpolation is not always adequate for data tables with

extensive curvature. Cubic i~~e~p~~~~i~# uses the values of four adjacent data

points to evaluate the coefficients of the cubic equation y = a + bx + CA? + dx3.

A compact and elegant implementation of cubic ~terpolation in the form of

an Excel 4.0 Macro Language custom function was provided by Or& Aslightly modified version, in VBA (Visual Basic for Applications) language, is

provided here (Figure 9-7). The syntax of the custom function is

Cubiclnterp(~~~~~-x~, known_ys, x-value).

William J. Orvis, Excel 4 for Scientists alzd ~~gi~eers~ Sybex Inc,, Alameda, CA, 1993.

Page 7: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 7/24

174 Excel for Chemists

Function Cubiclnterp(XValues, YValues, X)

’ Performs cubic interpolation, using an array of XValues, YValues.

’ The XValues must be in ascending order.

’ Based on XLM code from “Excel for Chemists”, page 239,

’ which was based on W. J. On/is’ code.

Row = App~icat~on.Mat~h(X, XValues, 1)

If Row c 2 Then Row = 2

If Row > XValuesCount - 2 Then Row = XValues.Cou~t - 2

Forl=Row~~ToRow+2

Q=l

ForJ=Row-1 ToRow+

If I <> J Then Q = Q * (X - XValues(J)) / (XValues(l) - XValues(J))

Next J

Y = Y + Q * YValues(l)

Next I

Cubiclnterp = Y

End Function

Figure 9-7. Cubic interpolation function macro.

The cubic interpolation function can be used to produce a smooth curve

through data points. Figure 9-8 illustrates a portion of a spreadsheet with

experimental spectrophotometric data taken at 5 nm intervals in columns A and

B (the data are in rows 6-86), and a portion of the interpolated values, at 1 nm

intervals, in columns C and D. The formula in cell D24 is

= Cubiclnterp ($A$6:$A$86,$B$6:$B$86,C24)”

The smoothed curve through the data points is illustrated in Figure 9-9.

Figure 9-8. kterpolation by using a cubic interpolation unction macro.

Page 8: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 8/24

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)

Page 9: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 9/24

176 Excel for Chemists

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.

Page 10: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 10/24

Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 177

Figure 9-12. Second derivative of titration data, near the end-point.

Since the derivative has been calculated over the finite volume AV = V,+l -

Vn, the most suitable

in Figure 9-10, is:

volume to use when plotting the ApH/AV values, as shown

Vv,+1+ vn

average = 2 P-4)

The maximum in ApH/AV indicates the location of the inflection point of thetitration (Figure 9-11). The second derivative, A( ApH/AV) /AV, which is

calculated by means of the spreadsheet shown in Figure 9-12, can be used to

locate the inflection point more precisely. The second derivative passes through

zero at the inflection point. Linear interpolation can be used to calculate the

point at which the second derivative is zero (Figure 9-13).

2000 r

V, mL

Figure 9-13. Second derivative of titration data, near the end-point.

Page 11: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 11/24

178 Excel for Chemists

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.

Page 12: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 12/24

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)

Page 13: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 13/24

180 Excel for Chemists

10I

Simpson’s Rule

method

7

6

* 5

-

1.5 2

X

1

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)

Page 14: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 14/24

Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 181

50 100 150 200

X

Figure 9-17. A curve that is the sum of two Gaussian curves.

The area of each Gaussian curve is equal to 1.000; thus the total area under

the curve shown in Figure 9-17 is 2.000. The Excel formula used to calculate y is

as follows (m is the position u and s is the standard deviation 0):

=EXP(-O.~*((X-curvl m)!curvl s)/\2)/(SQRT(Z*Pl())*curvl s)+EXP(-0.5” ((x-curv2 m)/curv2 s)A2)/(SQRT(2*Pl())*cu~2 s)

The area under the curve, between the limits x = 50 and x = 200, was

calculated by using each of the preceding equations (9-5, 9-6 and 9-7): the

rectangular approximation, the trapezoidal approximation and Simpson’s rule.

In each case a constant x increment of 10 was used. A portion of the spreadsheet

is shown in Figure 9-18.

The formulas in row 9, used to calculate the area increment, are as follows:

=I O”F9 (rectangular approximation)

=I O*(F8+F9)/2 (trapezoidal approximation)

=I O*(F8+4*F9+F10)/6 (Simpson’s rule)

CL00562 ii.05621 " __CL03

0.02507 0.25073 ty.15

a.04259 ._ 0.4259+,, 0.33

054

347 ;,

834‘ .,

0.08#07

0.24751

'0.37'687

cm3067 0.30673 0.36633 0.30464

Figure 9-18. Portion of a spreadsheet for calculating the area under a curve.

Page 15: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 15/24

182 Excel for Chemists

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.

Page 16: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 16/24

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

Page 17: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 17/24

184 Excel for Chemists

THE RUNGE-KUTTA METHODS

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

method term) are shown on the following page.

. . , . , , , . , . , , , :, . . ‘ .

___31

” : , , ’ , , , , I , ’ ‘ , : . , : ; .

, , ‘ : . , , . . . . “..’

k

‘,..

1 : EF

: : : . .

1

: . , , , , ‘ , . .. . : , , , :,’ . . ,‘,

c.1,.’. , . ” :. ‘.‘..

,‘, ,.. . .:: y. , . ,

. . i , : . : .

..l. ( . . : . : : . : . , . ,

, . . / : : ‘ , , , . I , ,. , . : . ’ &ye-K&a +im

: ,:..:,.:.. .,,’,2’,: : , . ,j: ,: ,‘,:..V’ .,,

,.. .'.'. . ” . , rate constant= 5.OE-03 k)'. ,.':',,. ..,;.'.. ,'d3 .. time increment= 20 WO

"....c,,4"' "..',,...,,: ;I.: :"'T.,.:

TA2 TA3 TA4

-0.0200 -0.0190 -0.0191 -0.0181

-0.0172 -0.0172 -0.0164

-0.0164 -0.0156 -0.0156 -0.0148

ml e’[-kt)

0.2000 0.2000

tl.1810 0.1810

0.1637 0.1637

0.1482 0.1482

.-l;~:@:‘. 30 -0.0148 -0.0141 -0.0141 -0.0134 0.1341 0.1341

qjj/i/:g 1 00 -0.0134 -0.0127 -0.0128 -0.0121 0.1213 0.1213

;1_2i.::" 1 20 -~Jjl 21 -0.0115 -0.0116 -0.0110 0.1098 0.1098

,.,,.:“:p&~:~;:1 40 -O.#llO -0.0104 -0.0105 -0.0099 0.0993 0.0993+ "" "

Figure 9-21. The Rung-Kutta method.

Page 18: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 18/24

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.

Page 19: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 19/24

186 Excel for Chemists

can now be assigned to the array of values in column B; the former formulas

(now in cells C7-G7) contain A-t in place of F6, and cell B7 contains the formula

=G6.

In essence, the fourth order Runge-Kutta method performs four calculation

steps for every time interval. In the solution by Euler’s method, decreasing the

time increment to 5 seconds, to perform four times as many calculation steps, still

only reduces the error to 0.9% after 1 half-life.

ARRAYS,MATRICES ANDDETERMINANTSSpreadsheet calculations lend themselves almost automatically to the use of

arrays of values. As you‘ve seen, arrays in Excel can be either one- or two-

dimensional. For the solution of many types of problem, it is convenient to

manipulate an entire rectangular array of values as a unit. Such an array is

termed a rn~~~i~. (In Excel, the terms “range”, ” array ” and “matrix” are virtually

interchangeable.) An m x n matrix (m rows and n columns) of values isillustrated on the following page-

Page 20: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 20/24

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:

Page 21: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 21/24

188 Excel for Chemists

Multiplication or division by a constant: qA =

Addition or subtraction of two matrices (both must contain the same number of

rows and columns):

A+B=

Performing matrix algebra with Excel is very simple. Let’s begin by

assuming that the matrices A and B have been defined by selecting the 3R x 3C

arrays of cells containing the values and naming them by using Define Name.

To add a constant (e.g., 3) to matrix A, simply select a range of cells the same size

as the matrix, enter the formula =A+3, then press COMM~N~+R~TU~ or

C~NTR~L+SHI~+RETU~ (Mac~tosh) or CONTROL+SHI~+ENTER (Widows).

Subtraction of a constant, multiplication or division by a constant, or addition of

two matrices also is performed by using standard Excel algebraic operators.Multiplication of two matrices can be either SC~Z~~multiplication or ~~c~~~

multiplication. Scalar multiplication of two matrices consists of multiplying

correspond~g elements, i.e.,

Thus it’s clear that both matrices must have the same dimensions m x n.Scalar multiplication is commutative, that is, A*B = B*A

The vector multiplication of two matrices is somewhat more complicated:

AwB= ~~ % ~I*[~ ; :;;[

ar+bu+cx as+bv+cy at+bw+cz

dr+eu+ fx ds+ev+fi dt+ew+ fz

gr+hu+ix gs+hv+iy gt+hw+iz

]

Vector multiplication is not commutative, that is A*B # B-A.

Vector multiplication can be accomplished easily by the use of one of Excel’s

worksheet functions for matrix algebra, ~~~LT( ~~~~~~7, ~~f~~x2). For the

matrices A and B defined above,

Page 22: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 22/24

Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 189

Two matrices can be

B has columns.

vector-multiplied when A has the same number of rows

B-A =

Vector multiplication of two matrices is possible only if the matrices are

conformable, that is, if the number of columns of A is equal to the number of rows

of B. The opposite condition, if the number of YOWSof A is equal to the number

of columns of B, is not equivalent. The following examples, involving

multiplication of a matrix and a vector, illustrate the possibilities:

MMULT (4 x 3 matrix, 3 x 1 vector) = 3 x 1 result vector

MMULT (4 x 3 matrix, 1 x 4 vector) = #VALUE!

MMULT (1 x 4 vector, 4 x 3 matrix) = 1 x 4 result vector

and

The transpose of a matrix, indicated by a

columns of a matrix are interchanged, i.e.,

d

ef

prime (‘), is produced when rows

The transpose is obtained by using the worksheet function

TRA~SPOSE(a~~ay) or the Transpose option in the Paste Special... menu

command (see “Using Paste Special to Transpose Rows and Columns” in Chapter

1) .

The process of rna~ri~ i~~e~si~~ is analogous to obtaining the reciprocal of a

number a. The matrix relationship that corresponds to the algebraic relationship

a x (l/a) = 1 is

AA-l-1 -

where A-l is the inverse matrix and I is the unit matrix. The process for

inverting a matrix “manually” (i.e., using pencil, paper and calculator) is

complicated, but the operation can be carried out readily by using Excel’s

worksheet unction M~~V~RSE(a~~ay). The inverse of the matrix B above is:

The “pencil-and-paper” evaluation of a determinant of N

also complicated, but it can be done simply by using the

MDETERM(array). ,The function returns a single numerical

and thus you do not have to use ~O~TRO~+S~~~T+E~TE

determinant of B, represented by 1B I, is 12.

rows x N colu

worksheet fu

i value, not an

Ii. The value

mns is

nction

array,

of the

Page 23: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 23/24

190

POLARTOCARTESIANCOORDINATES

Excel for Chemists

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

Page 24: 9 Some Mathematical Tools for Spreadsheet Calculations

8/7/2019 9 Some Mathematical Tools for Spreadsheet Calculations

http://slidepdf.com/reader/full/9-some-mathematical-tools-for-spreadsheet-calculations 24/24

Chapter 9 Some Mathematical Tools for Spreadsheet Calculations 191

Figure 9-24. Angular wave function for dx2 - y2 orbital.

USEFUL REFERENCE

K. Jeffrey Johnson, ~#~e~~~~~ Me~~~~s in C~rnis~~, Marcel Dekker, New

York, 1980.