Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
467 views

Numerical Methods

This document provides an introduction and table of contents for notes on numerical methods for solving physical problems. It will make extensive use of Excel, particularly the Solver tool, to approximate solutions to systems of equations. The notes cover topics like accuracy and errors in computations, solving nonlinear equations, systems of equations, interpolation, differentiation and integration, and solving ordinary differential equations numerically. Visual Basic and Excel macros are also discussed as a programming approach.

Uploaded by

mojiryhamid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
467 views

Numerical Methods

This document provides an introduction and table of contents for notes on numerical methods for solving physical problems. It will make extensive use of Excel, particularly the Solver tool, to approximate solutions to systems of equations. The notes cover topics like accuracy and errors in computations, solving nonlinear equations, systems of equations, interpolation, differentiation and integration, and solving ordinary differential equations numerically. Visual Basic and Excel macros are also discussed as a programming approach.

Uploaded by

mojiryhamid
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 32

November 14, 2008

Numerical methods
John D. Fenton
Institut fr Hydromechanik, Universitt Karlsruhe Kaiserstrasse 12, 76131 Karlsruhe, Germany

Abstract
These notes provide an introduction to numerical methods for the solution of physical problems. Extensive use will be made of Excel Solver for the solving or approximating the solution of systems of equations.

Table of Contents
1. 2. Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 3 3 3 3 5

Accuracy, errors and computer arithmetic 2.1 Accuracy . . . . . . . . 2.2 Rounding . . . . . . . 2.3 Errors . . . . . . . . . Excel functions . . . . . . . . . . . . . . . . . . . . . . . .

3. 4.

Solutions of nonlinear equations 4.1 The problem . . . . . 4.2 Methods . . . . . . 4.3 Excel Solver . . . . . Systems of equations . . . . 5.1 Solution by optimisation . 5.2 Systems of linear equations 5.3 Nonlinear equations . .

. 8 . 8 . 8 . 12 . . . . 13 13 14 15

5.

6.

Interpolation of data . . . . . . 6.1 The nature of the problem . . 6.2 Scaling of the dependent variable Approximation of data Curve tting by Excel . . . . . . . . . . . . . . . . . . . .

. 16 . 16 . 16 . 17 . 18 . 18 . 18 . 19 . 20 . 21

7. 8. 9.

Differentiation and integration 9.1 Differentiation . . . 9.2 Integration . . . .

10.

Numerical solution of ordinary differential equations 10.1 Eulers method . . . . . . . . . . 1

Numerical methods

John D. Fenton

10.2 10.3 10.4 10.5 11.

Improved accuracy at almost no cost Richardson extrapolation Higher-order schemes . . . . . . . . . . . . . Higher-order differential equations . . . . . . . . . Boundary value problems . . . . . . . . . . . . . . . . . . . . .

. . . . . . .

. . . . . . .

. . . .

23 24 25 26

A programming language Visual Basic and Excel Macros 11.1 Macros . . . . . . . . . . . . . . 11.2 Visual Basic . . . . . . . . . . . . .

. 27 . 28 . 30

Accompanying documents
The following Excel documents can be found in the same directory as this le, and are referred to in the following text:
File name 1-F UNCTIONS . XLS 2-S OLUTION . XLS 3-S OLVER . XLS 4-F ITTING . XLS 5-D IFF - EQNS . XLS Description Basic Excel functions Solution of a single equation in a single variable Solver applied to solution of equations, interpolation, and approximation A curve tting example where using Excel Trendline gave poor results Different problems solved by different methods

1. Introduction
Through the use of numerical methods many problems can be solved that would otherwise be thought to be insoluble. In the past, solving problems numerically often meant a great deal of programming and numerical problems. Programming languages such as Fortran, Basic, Pascal and C have been used extensively by scientists and engineers, but they are often difcult to program and to debug. Modern commonly-available software has gone a long way to overcoming such difculties. Matlab, Maple, Mathematica, and MathCAD for example, are rather more user-friendly, as many operations have been modularised, such that the programmer can see rather more clearly what is going on. However, spreadsheet programs provide engineers and scientists with very powerful tools. The two which will be referred to in these lectures are Microsoft Excel and OpenOfce.org Calc. Spreadsheets are much more intuitive than using high-level languages, and one can easily learn to use a spreadsheet to a certain level. Yet often users do not know how to translate powerful numerical procedures into spreadsheet calculations. It is the aim of these lectures to present the theory of the most useful numerical methods and to show how to implement them, usually in a spreadsheet, but occasionally also in a programming language, for sometimes spreadsheets are not adequate for large-scale computations. The two spreadsheets we have mentioned are:
Microsoft Excel widely known and used. Some of its effectiveness for numerical computations comes from a pair of modules, Goal Seek and Solver, which obviate the need for much programming and computations.

Goal Seek, is easy to use, but it is limited with it one can solve a single equation, however complicated or however many spreadsheet cells are involved, whether the equation is linear or nonlinear. Solver is much more powerful. It was originally designed for optimisation problems, where one has to nd values of a number of different parameters such that some quantity is minimised, usually the sum of errors of a number of equations. With this tool one can nd such optimal solutions, or solutions of one or many equations, even if they are nonlinear. In this course of lectures we will use it to simplify many procedures.

Numerical methods

John D. Fenton

It is somewhat annoying, however, that Solver is not automatically installed. You should open Excel, then click on the Tools tab. If Solver is not there you will have to click on Add-ins, and proceed to install it.
OpenOfce.org Calc Open Ofce is a shareware version of Microsoft Ofce, with a word processor, spreadsheet, presentation program, and drawing program; it can be downloaded from the site, http://www.openofce.org/ but at some 65MB is probably too big for student downloads. The spreadsheet is called Calc. It has most of the features of Excel, including Goal Seek, but at the time of writing, its release 2.3 still does not have a version of Solver, although one is under development. As far as this course is concerned, this is a signicant disadvantage. While that can often be worked around, in this course we will concentrate on Excel and will use that as a generic name for the two programs.

2. Accuracy, errors and computer arithmetic


2.1 Accuracy
Excel stores and calculates with 15 digit accuracy. This is equivalent to double precision in some programming languages, and is accurate enough that most calculations do not suffer from signicant loss of accuracy. Whenever numbers are stored on a machine a small error usually occurs. Excel can store 10 10 numbers in the range from 22 = 21024 10308 to 22 = 21024 10308 . If the number is less than the minimum it stores it as 0, if greater than the maximum it records it as an overow in the form #NUM!. Unlike programming languages, Excel does not distinguish between integers and oating point numbers

2.2 Rounding
Excel displays numbers rounded to the accuracy of the display. For example if you evaluate 2/3 and the cell has been formatted to display 4 decimal places, it will appear as 0.6667. If you need to round a number there is a function ROUND(number,decimal_places) which rounds a number to a specied number of decimal places. If decimal_places is 0, then the number is rounded to the nearest integer, which is often useful in programming. Example: ROUND(3.14159, 3) gives 3.142.

2.3 Errors
1. Blunders: These are not really errors, but are mistakes, such as typing the wrong digit. 2. Errors in the model: A mathematical model in civil and environmental engineering does not usually represent every aspect of a real problem, such as the neglect of turbulence in hydraulics. 3. Errors in the data: Most data from a physical problem contain errors or uncertainties, due to the limited accuracy of the measuring device. 4. Truncation error: This is the error made when a limiting process is truncated before one has reached the limiting value such as when an innite series is broken off after a nite number of terms. Example: computing sin x from the rst three terms of its power series expansion x x3 /3!+x5 /5!. 5. Roundoff error: This is the error caused by the limited accuracy of the computer, and a roundoff error occurs whenever numbers are stored and arithmetic operations performed. In this course we will be concerned mainly with the last two types of errors.

Numerical methods

John D. Fenton

2.3.1 Absolute and relative errors Let x be the approximate value of a number whose exact value is X . The absolute error in x is e = x X . The relative error in x is r = e/x = (x X )/X . The relative error is often given as a percentage. A number which is correct to n decimal places has an error of less than 1/2 in the nth decimal place, n Absolute error |e| 6 1 2 10 A number which is correct to n signicant digits has an error of less than 1/2 in the nth digit, 1n Relative error |r| 6 1 2 10 Example: Consider X = 23.494 and x = 23.491. x is correct to 4 signicant gures and 2 decimal places:
|e| = 0.003 <
1 2

|r| 0.00013 <

102
1 2

103 .

2.3.2 Roundoff error If we subtract two nearly equal numbers this leads to a loss of signicant digits which can cause serious error if used in further calculations. As an example, consider the quadratic formula for solving ax2 + bx + c = 0: b b2 4ac , x= 2a and if b is a large number, then b b2 4ac and subtracting the two to give the smaller root will be inaccurate due to roundoff error. For example, take a = 1, b = 104 , and c = 1. Evaluating the discriminant b2 4ac to 10 gures gives 9999.9998, and evaluating b + b2 4ac using mathematical software gave 2.000 000 02 104 of which only the rst gure 2 is signicant. Here as an example of different procedures we might adopt in other problems we obtain the smaller root by ve different methods: 1. Using Excel (with 15 gure accuracy) to evaluate the expression for the smaller root gives 0.9965 107 , which we suspect is liable to roundoff error. 2. Here we devise another method of evaluating it by taking a power series expansion in c for the 3 1 2 1 cancelling root, using the fact that (1 + )1/2 = 1 + 1 2 8 + O , see Footnote : p b |b| 1 4ac/b2 x = 2a ! 1 4ac 2 b |b| 1 4ac 1+ 2 2 + ... = 2a 2a b 8 b2 a c = 3 c2 + O c3 |b| b = 107 + 1021 + . . . . Hence we have shown that to some 20 gures, the solution is 107 , whereas using direct evaluation gave a relative error of
0.9965 107 1 107 = 0.0035, 1 107
1 Note the notation O(. . .) which we use in this course it is a Landau order symbol, and is spoken is of the order of, and it is such that a function f () is said to be O(g()) as 0 if |f ()/g ()| is bounded as 0 . It describes the essential behaviour in that limit.

Numerical methods

John D. Fenton

or 0.35%. This accurate method required a deal of knowledge and effort, however. 3. Knowing that b is so large in magnitude such that x is small in magnitude suggests re-arranging the equation x2 107 x + 1 = 0 in the form suitable for iterative (repeated) solution
x= 1 + x2 , 107

so that starting with an approximate solution x = 0, gives the successive approximations x = 107 , 107 + 1021 , . . ., in exactly the same way as in method 2. This method does not always work, as will be seen below. 4. Finally, as an example of the power and utility of Solver, and without requiring a great deal of mathematical or computational knowledge, Solver was used to obtain the root, by choosing an approximate small value of x = 0 and running Solver to nd the value of x which satised the equation x2 107 x + 1 = 0, which gave an answer of 1.00000000000001 107 , correct to the full accuracy of Excel. The numerical optimisation method which Solver uses had no roundoff error problems here. 5. Encouraged by that, the other simpler method implemented in Excel, Goal Seek, was used, which was possible as the problem requires only the solution for a single quantity. A value of 1.00000000101755 107 was obtained, with an error in the 10th gure. Goal Seek contains no means for varying the accuracy of the solution, unlike Solver.

3. Excel functions
Accompanying document Excel workbook 1-F UNCTIONS . XLS sets out the main commands which are necessary at this stage. In the experience of the lecturer, Excel commands are accurate and robust although there has been some discussion about some of the statistical routines.

Numerical methods

John D. Fenton

Figure 3-1. Elementary functions

Numerical methods

John D. Fenton

Figure 3-2. Advanced functions

Numerical methods

John D. Fenton

4. Solutions of nonlinear equations


4.1 The problem
The problem is: if we have one equation in one unknown, and we need to nd one, some, or all of the values of that unknown which satisfy the equation. That is, we have to solve f (x) = 0, where f (x) is a known function of x. This is nding the value of x where the graph of y = f (x) intersects the x axis. There may be more than one solution. Examples
x2=0 A linear equation, with a simple solution x = 2. A quadratic we might be able to factorise it, or use the formula for solving quadratics. A higher degree polynomial we might be able to factorise and solve, but in general it is unlikely A transcendental equation, which we are most unlikely to be able to solve. x2 4x + 3 = 0

sin x x + 1 = 0

x5 3x3 + x 1 = 0

In the case of the last two equations we have to resort to numerical methods.

4.2 Methods
A number of different methods are presented here, for each has certain characteristics which favours its use in certain situations. For the purposes of this lecture course, however, we will see that Goal Seek and Solver are the simplest and best to use, but in general any one of the others might have advantages. 4.2.1 Trial and error This involves simply guessing a value of x, evaluate f (x), compare with zero, and then guess again. It is often used for one-off problems, but we often need to be much more systematic about it. 4.2.2 Newtons method

y = f (x)

y = f (x) = 0

f (x0 )

x2

x1

x0

This method, if it converges, does so very quickly. Instead of just computing f (x), the derivative f 0 (x) is also calculated, and it is assumed that the next estimate of the root, or solution, is where the tangent crosses the x axis. The values of f (x) and f 0 (x) there are calculated and the process repeated until it converges. From the gure, at iteration n
tan n = df f (xn ) (xn ) = f 0 (xn ) = , dx xn xn+1 f (xn ) . f 0 (xn )

from which an expression for the next estimate xn+1 is obtained


xn+1 = xn f (xn ) = xn + n f 0 (xn )

where n =

(4.1)

The convergence of this method is quadratic, such that the number of correct digits doubles at each step. 8

Numerical methods

John D. Fenton

However, if a double root occurs, such that the curve just touches the graph at the root and then curves away again, convergence is less rapid. Usually the correction at each stage n = f (xn )/f 0 (xn ) is calculated and monitored, and when it is less than a certain amount in magnitude, the process terminated.
Example 1: With this method we can develop, for example, an algorithm to calculate the square root of a number. Let the function f (x) = x2 N = 0, where N is the number whose square root we want. Now, f 0 (x) = 2x, and substituting into equation (4.1): x2 1 N nN xn+1 = xn = , xn + 2xn 2 xn which is a simple and perhaps obvious algorithm: take the mean of your estimate and the number divided by that quantity. Now, lets nd the square root of 100, starting with 100/2 as the rst estimate: N 1 Step xn 2 xn + xn 1 100 1 50 2 50 + 50 = 26.0 1 100 2 26.0 2 26 + 26 = 14.923 1 100 3 14.923 2 14.923 + 14.923 = 10.812 1 100 4 10.812 2 10.812 + 10.812 = 10.0305 100 5 10.0305 1 2 10.0305 + 10.0305 = 10.00005

4.2.3 The secant method


y y = f (x)

y = f (x) = 0 f (x1 )

f (x0 )

x3

x2

x1

x0

Newtons method is unpleasant to apply if the function f () is complicated, such that differentiation is a problem. In such cases it is appropriate to approximate the derivative by the secant approximation to the tangent. This means that f 0 (xn ) is approximated by
f 0 (xn ) f (xn ) f (xn1 ) f (xn ) f (xn1 ) = , xn xn1 n1 f (xn ) n1 . f (xn ) f (xn1 )

and the scheme requires two starting values (x0 , f (x0 )) and (x1 , f (x1 )) and it becomes
xn+1 = xn + n

where n =

Numerical methods

John D. Fenton

Example 2: We repeat the above problem using the secant method, using 0 and 50 as the rst two estimates. Application of the method is shown in the following table. Step 0 1 2 3 ... 9 10 xn 0. 50 2 + 1.846 = 3.846 ... 9.999979 10.000000 50 48.0 = 2.0 n =
N x2 n n1 2 x2 n xn1

50
1002500 25000 50 = 48.0 10022 22 502 48 = 1.846 1003.8462 3.8462 22 1.846 = 14.575

...

0.000021 0.000000

Note that the method was somewhat slowly convergent as both numerator and denominator of the expression went to zero in this special case. However, it still worked and the method has the decided advantage for complicated functions that it is not necessary to obtain the derivative of the function. 4.2.4 Fixed point, direct, or simple iteration This is a simple and obvious method and it is simple enough that if it works it can be very useful but it goes wrong in about 50% of cases! We will investigate the conditions below. Once again, our problem is to solve the equation f (x) = 0. However, if we can re-arrange this in the form x = g (x), where g (x) is some function of x, then this gives the scheme
xn+1 = g (xn ),

meaning, assume some initial value x0 , evaluate g (x0 ) and use this value as the next estimate x1 and so on.
Example 3: Use direct iteration to solve the equation x3 x 1 = 0. This obviously suggests the scheme xn+1 = x3 n 1.We start with x0 = 1: xn 1 0 1 2 9 13 1 = 0 x3 n1

03 1 = 1

13 1 = 2 23 1 = 9 93 1 = 730

It is obvious that the scheme is unstable and is not converging to a solution. Let us now try re-arranging 1/3 the equation in the form: xn+1 = (xn + 1) , with results: xn 1 1.259 1.3121 1.3223 1.3243 (xn + 1)
1/3

(1 + 1)1/3 = 1.259 (1.259 + 1)1/3 = 1.3121 (1.3121 + 1)1/3 = 1.3223 (1.3223 + 1) (1.3243 + 1)
1/3 1/3

= 1.3243 = 1.3246

It can be seen that the process is converging quite well.

10

Numerical methods

John D. Fenton

y y = gA (x)

y=x

y y = gB (x)

y=x

y=x

y = gC (x) 0 0 0

x
0 Case A: |gA (x)| > 1, unstable

x 0 Cases B and C: gB,C (x) < 1, stable

Figure 4-1. Unstable and stable behaviour of direct iteration scheme for different gradients of the function

Why did one method work and not the other? The reason can be explained graphically, if we consider solving the equation x = g (x) to be equivalent to solving the pair of simultaneous equations
y = x,

and y = g (x),

so that we plot the two graphs the rst is simply a straight line of gradient 1 passing through the origin, and the problem is to determine where the second graph crosses it. The iteration procedure for different cases is shown in Figure 4-1. In Case A, the function has a gradient which is greater than 1, and although we started near the solution at point 0, we were taken away from it. In the next Case B the function still has a negative slope, but it is less than 1 in magnitude and it can be seen how the solution spirals in to converge. The last Case C is for a positive slope which is less than 1, and the process converges. These gures demonstrate the condition for convergence, that the direct iteration scheme xn+1 = g (xn ) is stable if the gradient of the curve is less than one in magnitude in the vicinity of the root being sought, that is, dg (xn ) < 1 for stability. dx 4.2.5 The bisection method
Successive intervals in which the solution lies 0 1 2 3 4 5

f (x) = 0

This is a non-gradient method which uses a simple algorithm which can handle the most difcult of functions. The method will always converge to one solution provided a range which contains the solution is selected at the start. It proceeds by halving the range, then testing whether the solution is in the left or right half, and then repeating, halving the interval at each step. It is similar to a dictionary game, where one player selects a word, and another player has to guess the word by taking half the dictionary, asking if the word is in the rst or second half of the dictionary, then in which half of that half and so on. If there are N words, then it should be possible to get the word in m tries, where N = 2m , or m = log2 N . In the case of the a typical dictionary, with something like 80000 words, m = log2 80000 16, i.e. 16 11

Numerical methods

John D. Fenton

tries. The method is to bracket the solution initially, i.e. to nd a0 and b0 such that the solution lies between the two, and then calculate the mid-point xm = (a0 + b0 )/2 and by the sign of f (xm ), determine whether the solution is in the left or right half, and then to repeat until the interval is small enough. The algorithm can be written, where a and b are the left and right ends of the initial interval and is the required accuracy:
fa = f (a), fb = f (b) if(fa fb > 0) exit and choose another a or b while (b a > ) xm = (a + b)/2 fm = f (xm ) if (fm fa < 0 ) b = xm else a = xm , fa = fm endif Calculate the values of the function at the ends The function seems not to change sign in the interval Repeat until the interval is small enough Calculate the mid-point Calculate the function there Does the sign of the function change in the left half? If so, reset the right boundary to the mid-point Otherwise set the left boundary to the mid-point

This method is not so easily programmed in a spreadsheet itself. Rather it is easily programmed in Visual Basic, the programming language which is available behind the spreadsheet. This can be accessed any time by using Alt-F11 in Excel Workbook 2-S OLUTION . XLS.

4.3 Excel Solver


8

f (x) 0

-4

-8

-2

-1

0 x

Figure 4-2. Cubic function f (x) = x3 x 1, with difcult behaviour for root-nding

Workbook 2-S OLUTION . XLS shows the use of Excel Solver to obtain the solution of a single equation in a single unknown for the example used above, f (x) = x3 x 1, to give an introduction to its use and to help us feel familiar with it. This simple cubic actually has a very nasty property, in that it has one solution, but the nature of the function, with a turning point where there is no solution is such as to throw off both Newtons method and Excel Solver, unless one starts sufciently close to the solution. Plotting the function is always a good idea. Generally, however, using Excel Solver seems to be the best and simplest way of solving equations, most of which have rather better properties than the example given.

12

Numerical methods

John D. Fenton

5. Systems of equations
5.1 Solution by optimisation
Solver can be programmed to solve a much wider family of problems. It was originally designed for optimisation problems, where one has to nd values of a number of different parameters such that some quantity is minimised, usually the sum of errors of a number of equations. With this tool one can nd such optimal solutions, or solutions of one or many equations, even if they are nonlinear. All one has to do is to formulate the problem as one or more equations. Let our system of equations be f (x) = 0, where f and x are vectors such that
f (x) = [fi (xj , j = 1, . . . , N ) = 0, i = 1, . . . , M ] ,

namely we have a system of M equations fi in N unknowns xj . If the equations are linear, a number of linear algebra methods such as matrix inversion, etc. are possible, but the problems are usually more easily soluble with Solver. This even applies to the more general case, where the equations are nonlinear. In this process, , the sum of the squares of the errors for all the equations are evaluated:
=
M X i=1

fi2 (xj ),

and the values of the xi found such that will be a minimum. If M = N it should be possible to nd a solution such that = 0 and the equations are solved. If there are fewer variables than equations, N < M , then it will not be possible to solve the equations, but minimising produces a useful solution anyway. This is particularly so in the case of nding approximating functions.In the formulations of the present problems, we have to write all the equations with all terms taken over to one side, and then to implement an optimisation procedure such as Solver. It does not matter how complicated the equations are it is just necessary to be able to evaluate them. Most methods for minimising are gradient methods, such that in the N -dimensional solution space, a direction is determined, and the minimum of the error function in that direction found, at the minimum a new direction found, usually orthogonal to the previous one, and so on. For two dimensions it can be shown as nding the minimum value of a surface, as in Figure 5-1; the three dimensional case can be imagined as nding the hottest point in a room by successively travelling in a number of different directions, nding the maximum in each, and then setting off at right angles, and so on. In either case the gradients are not obtained analytically but numerically by evaluating the functions at different values.

Figure 5-1. Representation of typical search method for the minimum of a function of two variables

This procedure usually means that we have simply to write down the equations, with initial trial values of the unknowns, and call the optimising routine. It is very general and powerful, and we can usually write down the equations in the simplest form, so that no algebraic manipulation is necessary or favourable. It 13

Numerical methods

John D. Fenton

can be seen that it works simply, and no attention has to be paid to the solution process.

5.2 Systems of linear equations


Very commonly throughout engineering one encounters systems of linear simultaneous equations. These can be solved by direct methods such as Gaussian elimination, or LU decomposition into upper and lower triangular form. Computationally, a very popular method for 20 years has been Singular value decomposition, which also works well on equations which are poorly conditioned, such that the solution is very sensitive to numerical accuracy. Of course, other procedures for solving systems of linear equations are matrix methods, but these are often not as computationally efcient as the previously mentioned ones. Solver, however, can be used very simply to solve linear equations as well. An example is given in the Workbook 3-S OLVER . XLS. This contains some simple examples in Worksheet L INEAR EQUATIONS. In the rst example we compare the use of typing out the equation with matrix notation. A well-conditioned system two approaches Consider the system of equations

x+0yz = 1 y = 1 x+0y+z = 1

1. Firstly, the equations x z 1 = 0, y 1 = 0, and x + z 1 = 0 are typed and solved. This system is well-conditioned, in that the solutions are not sensitive to numerical operations. In 3-dimensional space such as here, that happens when two (or even three) of the three intersecting planes are very similar, such that the actual intersection point is poorly dened. 2. Whereas the writing of the three equations was simple and obvious, it is sometimes easier to write the equations as a matrix equation 1 0 1 x 1 0 1 0 y = 1 . 1 0 1 z 1

Solver can be used to actually solve the system of equations or to nd the solution such that the sum of the squares of the errors in each equation is a minimum. It seems that this latter method is also T used to nd the solution. In this case Solver obtains the solution to six gures 1 1 0 . in this case we consider 1 0 1 x 1 0 1 0 y = 1 , 1 0 1 z 1

A singular system:

where the last row is simply -1 times the rst row. Solving this is equivalent to nding where one plane crosses two planes which are coincident so that there is an innity of solutions along a line where the planes intersect. Solver found the solution [2.111.1]T , and several others besides, just depending where the initial solution was. A poorly-conditioned system: in this case the system of equations is such that the system is not exactly singular, but almost so, such that solutions depend very much on the numerical accuracy.

14

Numerical methods

John D. Fenton

5.3 Nonlinear equations

In this case Solver had great trouble, and found a solution but with quite a large error, and it was not possible to rene this because of the poorly-conditioned nature of the system. It was interesting, however, that the matrix inverse facility provided as part of Excel gave the correct solution correct to 16 T decimal places, 4 60 180 140 .

Consider the example which is simple but is notoriously poorly-conditioned: 1 1 1 1 1 x 2 3 4 1 1 1 1 y 1 2 3 4 5 1 1 1 1 z = 1 . 3 4 5 6 1 1 1 1 1 4 5 6 7

In many physical problems, however, the system is not poorly conditioned, and Solver can obtain highly accurate solutions. As it uses a scheme for minimising the sum of errors of a system of equations it does not matter whether the system is linear or nonlinear, which the next example in the spreadsheet shows.
Example 4: Find where the parabola y = x2 crosses the circle x2 + y 2 = 22 . In Solver we have two variable cells for x and y , plus a cell for the sum of the squares of the errors of the equations. However, it is usually clearer to set up a cell for the evaluation of each of the equations plus one cell where the sum of the squares is evaluated, and this is the cell that is minimised or solved. A single command can be used: SUMSQ(...). Here is the initial setup on Worksheet N ONLINEAR S YSTEMS in Workbook 3-S OLVER . XLS, with an initial estimate of x = 1, y = 1: Column C Solution x 1 Column D y 1

Row 5

Mathematics: y x2 x2 + y 2 22 Sum of squares:

Row 9 Row 10 Row 11

Evaluate functions, each of which should be zero for a solution Excel contents Result =C5-B5*B5 0 = B5*B5+C5*C5-4 -2 =SUMSQ(D9:D10) 4

Now, running Solver with the Target Cell D11, Changeable Cells B5:C5, and seeking a Solution or Minimising the sum of the errors the method converges, x = 1.24962..., y = 1.56155.... Unpleasantly, with the initial solution x = 0, y = 0, it does not converge so one has to be sufciently close to a solution to converge to it. For problems that are not so nonlinear, this is not such a problem. Note this did not nd another solution which was the negative of both those numbers, unless an initial estimate of the solution closer to that one was assumed. If it nds one solution, it provides no information as to how many exist or where the other ones are.

Exercise: consider the problem of nding the intersection of the function y = x1/3 with the circle 2 2 x + y = 1. Solver can nd this very accurately as x = 0.563623889 and y = 0.826031289.

15

Numerical methods

John D. Fenton

6. Interpolation of data
An important problem is the determination of a mathematical function which passes through a number of data points. This is equivalent to the solution of a number of equations, equal to the number of data points and to the number of coefcients in the interpolating function. Usually such problems are linear, for example, determining the coefcients ai in the polynomial y = a0 + a1 x + a2 x2 + a3 x3 + . . ..

6.1 The nature of the problem


Consider the following pairs of x, y points: (0, 0), (1, 1), and (2, 4). With three data points, this suggests using a quadratic equation y = ax2 + bx + c, where we can solve the equation at three points to give the values of a, b, and c. We obtain the three equations
0 = c 1 = a+b+c 4 = 4a + 2b + c

These are easily solved: a = 1, b = 0, and c = 0, which comes as no surprise for these points. Hence we have the situation in the following example, where the function that interpolates the data points passes through each. It is capable of analytical differentiation or integration.
5 4 3 y 2 1 0 Data points Interpolating function Should not be used to extrapolate

0.5

1 x

1.5

Figure 6-1. Interpolation of three data points by a second-degree polynomial

6.2 Scaling of the dependent variable


However there are some subtleties in the numerical operations which can make the results unreliable. It is always a good idea to scale the independent variable back to the range [0, 1] or even better [1, 1], rather than in absolute terms, especially in civil engineering problems where the values of x might be huge, corresponding to distances along a road, railway or river. This is suggested by Figure 6-2, which shows that over the interval [1, 1] the monomials x, x2 , x3 show diverse behaviour, whereas on the interval [1000, 1200] they show a similar behaviour. What this means is that if the basis functions are different, they are better able, with smaller coefcients, to interpolate or approximate a function. Thus, if the original range is [xmin , xmax ] and both values are large, one can introduce the variable
X = a + bx and x = X a xmax + xmin , where a = b xmax xmin and b = 2 , xmax xmin

which brings X to the range [1, 1]. 16

Numerical methods

John D. Fenton

1 0.5 y 0 -0.5 -1 x x2 x3 -1 -0.5 0 x 0.5 1 1000 y

x/1000 (x/1000)2 (x/1000)3

1050

1100 x

1150

1200

Figure 6-2. Comparison of variation of polynomials on the interval [1, 1] and on [1000, 1200], showing the greater diversity of behaviour in the former case, with a better ability to represent functions

For example, consider the problem of nding the interpolating quadratic which passes through three points on a road curve near the 20 km mark: (20200, 0), (20300, 100), and (20400, 150). Assuming a function of the form y = a0 + a1 x + a2 x2 and using Solver gave terrible results, as would almost any other method. If on the other hand, the results are scaled as above, then Solver obtains the results to 6 gures: a0 = 99.99996, a1 = 74.99995, and a2 = 25.00000. This is in Worksheet I NTERPOLATION in Workbook 3-S OLVER . XLS.

7. Approximation of data
Irregular data points Interpolating 6th degree polynomial Approximating 2nd degree polynomial

94

y 90

86

1993

1994

1995 t

1996

1997

1998

Figure 7-1. Irregular data points, showing the superiority of approximation to interpolation

Another problem of interpolation is that it can only be used for smooth regular data points, such as in the numerical solution of equations. For experimental data, with irregularities, it is completely inappropriate, such as the gure here shows. It is much better to approximate the data by functions that contain fewer degrees of freedom, such as lower-degree polynomials, such that they do not pass through every point. They do not interpolate, they approximate. If we use Solver, almost any method of approximation becomes possible. Mostly, this will consist of a relatively low-order polynomial. One advantage of using optimisation methods, as done by Solver is

17

Numerical methods

John D. Fenton

that nonlinear methods can be used. As an example, consider the function


y = a0 + a1 ea2 X ,

where in this case, nding the a0 , a1 , and a2 is a nonlinear problem. We have used the transformed variable X again, although it is often not necessary. In the accompanying Excel worksheet 3-S OLVER . XLS such a problem is solved. It is interesting that analytically nonlinear problems are very difcult indeed, but for optimisation methods and Solver they are no more difcult than linear problems. This can be used in a very similar way to interpolation of data as described above in Section 6, but where the number of degrees of freedom (the number of coefcients of the approximating function) are rather fewer than the number of data points. In this case Excel cannot solve the equations exactly, as there are not enough coefcients, but it can determine the coefcients such that the sum of the squares of the errors is minimised.

8. Curve tting by Excel


There is a facility in Excel by which trendlines can be very simply added to sets of experimental points, which are none other than approximating data by a relatively low-degree function as we described immediately above. Generally the program behind it is very robust, and certainly uses the subtraction of a mid-value of x as we described in Section 6. One right clicks on one of the plotted points and is given the option of adding what Excel calls a trendline, and one is given the option of several different types of function. It can only do linear problems, which is not a big disadvantage, except, for example, if we wanted the exponential t shown above. If one needs the actual function which Excel has obtained, that can be displayed on the gure as well. However, it has a aw, in that however robustly it calculates the approximating function internally, it displays it in expanded form, and sometimes with few signicant digits in the coefcients, so that round-off errors might be huge. For example, it might calculate the function obtained above internally as y = 100.001 + 0.75 (x 20300) 0.0025001 (x 20300)2 , but when asked for the function it expands it as, for example here, it should expand to 1. 045 391 208 106 + 102. 254 06 x 0.002 500 1 x2 , but unless specically required it displays only few gures, and in the above case it actually just displayed y = 1E + 06 + 102.25 x 0.0025 x2 , which gives nothing like the desired results. One can change the number of digits by right-clicking on the formula and changing the Number of the Format of the line, but it is better to do the approximation oneself, in a form such that one has control of it. To do the approximation oneself, simply use Solver as described above. The above example has been programmed in 4-F ITTING . XLS.

9. Differentiation and integration


The calculation of derivatives and integrals from numerical data is very important. As integration is a smoothing operation, its results are very much more robust than differentiation, which is well-known to be sensitive to "noisy" data.

9.1 Differentiation
Consider the Taylor expansion for a function f (x) about a point x:
f (x + h) = f (x) + h h2 d2 f df (x) + (x) + . . . . dx 2! dx2

(9.1)

We can re-arrange this, neglecting the term in h2 to give


f (x + h) f (x) df (x) + O(h), dx h

(9.2)

which is a forward difference approximation. This states that to obtain the value of a derivative, calculate 18

Numerical methods

John D. Fenton

the value of the function at a nearby point x + h, subtract the value at x and divide by h. The error is shown by the term O(h) which means that it is directly proportional to the distance h. Results are shown by the dotted line on Figure 9-1, where it can be seen that the gradient of this approximation is not a particularly accurate approximation to the gradient of the tangent at x, that which is required.
Forward difference Centre difference Tangent

xh

x+h

Figure 9-1. Approximations to the slope of the curve at x in terms of function values at x h, x, and x + h

Now consider equation (9.1), which we write, replacing h by h:


f (x h) = f (x) h h2 d2 f df (x) + (x) + . . . . dx 2! dx2 df (x) + O(h3 ), dx

(9.3)

Now if we subtract equation 9.3 from equation 9.1:


f (x + h) f (x h) = 2h

and re-arranging gives


f (x + h) f (x h) df (x) = + O(h2 ). (9.4) dx 2h Results are shown by the dashed line on Figure 9-1, and it can be seen that this centred difference approximation is rather more accurate, as shown mathematically by the error term O(h2 ), which means that if the step size is halved the error will be one quarter.

These approximations, discretising in both space and time are widely used for numerical solution of partial differential equations throughout engineering.

9.2 Integration
Linear function dened by two points Quadratic function dened by three points

xh

x+h

Figure 9-2. Interpolating functions for integration in terms of function values at x h, x, and x + h

Figure 9-2 shows two different approximations for integrating the function by nding the area between 19

Numerical methods

John D. Fenton

the function and the x axis. The rst is the trapezoidal rule, which consists of taking the function value at two points, tting a straight line between them (the dotted line on the gure) and nding the area of that trapezium. It is
x +h Z x

f (x) dx

h (f (x) + f (x + h)) . 2

(Trapezoidal rule)

On the other hand, if three points are used, a quadratic function can be used to interpolate the three, shown dashed in the gure, and the area under the quadratic obtained. The result is Simpsons rule
x +h Z xh

f (x) dx

h (f (x h) + 4f (x) + f (x + h)) . 3

(Simpsons rule)

In fact this result is also exact for a cubic function, and the accuracy of Simpsons rule goes like O(h3 ), which is surprisingly accurate.

x0

x1

x2

xN

Figure 9-3. Typical subdivision for numerical integration

Of course in practice we need to nd the area over a nite range, and so we use compound versions of these, as shown in Figure 9-3. The trapezoidal rule can be written ! x ZN n 1 X h f (x0 ) + f (xN ) + 2 f (x) dx f (xi ) . (Compound trapezoidal rule) 2
x0 i=1

The compound Simpsons rule can only be applied for even numbers of panels (odd numbers of points, from 0 to N , such that N is an even number). It is x ZN N 1 N 2 X X h f (x) dx f (x0 ) + f (xN ) + 4 f (xi ) + 2 f (xi ) . 3
x0 i=1, odd i=2, even

(Compound Simpsons rule)

10. Numerical solution of ordinary differential equations


Consider the problem of solving the rst-order differential equation
dy = f (t, y ) dt

(10.1)

such that we know the rate of change of a quantity as a function of time and the quantity itself. The problem is to obtain a solution for y (t), possibly in a general form, but in practical problems usually where an initial condition y (t0 ) = y0 is known. In many problems this can be solved analytically, for example in the case of radioactive decay, where the rate of change of the quantity of material is

20

Numerical methods

John D. Fenton

proportional to the quantity present,


dy = k y, (10.2) dt where k is the constant of proportionality. In this case we can separate the variables on each side of the equation and integrate: Z Z dy = k dt, y ln y = kt + C,

and substituting in the initial condition y(t0 ) = y0 to determine the constant of integration C we nd C = ln y0 + kt0 and the solution can be written
y = y0 ek(tt0 ) ,

showing the well-known exponential decay. In general such an integration is not possible, for example, the differential equation dy/dt = y 2 t, and we have to solve the equation numerically, which usually means obtaining a sequence of numerical values y1 , y2 , . . . corresponding to specied values t1 , t2 , . . ..

10.1 Eulers method


y Dashed lines show the gradient at any point (t, y ) dy/dt = f (t, y ) Exact solution Numerical solution

y0

t t0 t1

t t2

t t3 t

Figure 10-1. The nature of the numerical solution of a differential equation

Consider the plot of the gradient dy/dt = f (t, y ) as shown by the dashed lines on Figure 10-1, such that for any t and y the value of the derivative can be calculated. Numerical solution begins at the given initial point (t0 , y0 ), and the problem is to advance the solution in time along a curve across the plane shown in the gure. The simplest (Euler2 ) scheme to advance the solution from one computational solution point (ti , yi ) to another (ti+1 , yi+1 ) is to consider a step t in t such that ti+1 = ti + t, for all i, and to approximate the derivative in equation (10.1) by a simple forward difference approximation
y f (t, y ) , t

Leonhard Euler (1707-1783), Swiss mathematician and physicist, remarkable genius.

21

Numerical methods

John D. Fenton

such that considering points i and i + 1 we have


yi+1 yi f (ti , yi ) , t

such as shown on Figure 10-1. This gives the explicit expression for the new value of yi+1 :
yi+1 yi + t f (ti , yi ) ,

(10.3)

and solution proceeds as shown in the gure, in general deviating somewhat from the exact solution. This is the simplest but least accurate of all methods. If we were to examine the approximation of the scheme in equation (10.3) we would nd that the neglected terms are of a magnitude proportional to the square of the time step, (t)2 . This is called the truncation error, because we have truncated a series approximation to the differential equation, here after the very rst term. To carry out the calculations to a certain point in time t, the number of time steps necessary is t/t, and so multiplying the truncation error at each step by the number of steps we conclude that the global error at a particular time varies like Error
1 (t)2 t. t

We have used the symbol "" or tilde to show how the error varies with t in the limit of small t. As errors here go like this rst power of the time step (t)1 , we call this a rst-order scheme. If we were to halve the time step, the error at a certain time would also be halved.
Example 5: Solve the equation for radioactive decay, equation (10.2) using Eulers method, with k = 2, with initial condition t = 0, y = 1, using (a) t = 0.2 and (b) t = 0.1. 1 0.8 0.6 y 0.4 0.2 0

Exact Euler t = 0.2 Euler t = 0.1

0.5

1.5 t

2.5

3.5

Figure 10-2. Comparison of results from Eulers method This was done in spreadsheet 5-D IFF - EQNS . XLS and the results are shown in the gure. It can be seen that as the computational step was halved, then the error at any point was also roughly halved. This is characteristic of this rst-order method. We note that the results give a rough idea of the scale of the solution, but are not quite accurate enough to be considered satisfactory.

As a general rule in computations it is good practice to demonstrate that the numerical solution is a sufciently accurate one by considering smaller steps, to show that the process has converged. In most problems a relatively slow convergence to the exact solution as in the above example is not acceptable. A simple way of overcoming the problem of insufcient accuracy is to take successively smaller time steps, until the process has converged to the desired accuracy. To gain better accuracy most simply, one 22

Numerical methods

John D. Fenton

can just take smaller steps t. However higher accuracy methods such as Richardson extrapolation and Heuns method are usually considered better, and which will be described below.

10.2 Improved accuracy at almost no cost Richardson extrapolation


A simple way of obtaining results of a higher order of accuracy is to use Eulers method for two different time steps, and then to use Richardson extrapolation to the limit, where a simple numerical calculation gives a signicantly more accurate solution, extrapolating the results to the limit of zero computational step. This is a beautifully simple procedure for obtaining more accurate solutions from computational schemes. Two variants were developed in the early 20th century by Richardson3 and Aitken4 , the latter where the order of accuracy of the scheme is not known. The idea behind Richardson extrapolation is that we can write the accuracy of the scheme as
yexact (t) = y(t; t) + at + . . . ,

(10.4)

where y (t; t) means the numerical solution at t obtained using a time step t, and where we have neglected higher order powers of t. We do not know the coefcient a, or the exact solution yexact (t). However, if we carry out a solution for two different time steps and write equation (10.4) for each case, truncated at the stage shown, then we can solve the pair of equations for yexact (t), whose value will now be an approximation, as we truncated the series, but where the neglected terms are O((t)2 ). The coefcient a is of no interest. The expression is
yexact (t) t1 y (t; t2 ) t2 y (t; t1 ) . t1 t2

(10.5)

In the common case where if a step t is used, and then twice as many steps of half the size t/2, then an estimate of the exact result with Error (t)2 , is simply the combination of the two
yexact (t) 2 y (t; t/2) y(t; t),

(10.6)

which can be evaluated at each of the original time steps.


Example 6: Estimate an improved solution of the differential equation in example 5 at t = 1.
Euler, t = 0.2 Value Error 0.0778 42% Euler, t = 0.1 0.1074 21% Richardson = 2 0.1074 0.0778 = 0.1370 +1.3% Exact 0.1353

It can be seen that with original results no better than 42% and 21%, we have obtained a result to 1.3%, accurate enough for practical purposes, and indistinguishable from the exact curve in the gure in the previous example.

The method is remarkable and simple. In principle it can be used in almost any type of numerical calculation, including numerical integration and differentiation. If there is uncertainty about the order of accuracy of the method, then Aitkens method should be used. In the case 2 of integration by the Trapezoidal Rule, it can be shown that the order of the neglected terms is O h , where h is the step size, as used previously. In this case, applying the method here gives the scheme:
Texact 1 (4T (h/2) T (h)) , 3

in which form it is known as Romberg Integration.

3 Lewis Fry Richardson (1881-1953), British physicist, meteorologist, and mathematician, the father of computational uid mechanics, peace studies, and other elds. 4 A. C. Aitken, New Zealand mathematician and calculating genius.

23

Numerical methods

John D. Fenton

10.3 Higher-order schemes


10.3.1 Heuns method
Euler yi +1 exact yi +1 Heun yi +1

= =
yi

ti

ti+1

Figure 10-3. One step of the Heun scheme

In Eulers method there was no attempt made to include information from the point ti+1 at which the solution is being obtained. Heuns method uses Eulers method but uses the result yi +1 to calculate the gradient there and then, whereas Eulers method just uses f (ti , yi ) for the gradient over the interval, Heuns method uses the mean of that value and the approximately calculated value at ti+1 . The scheme can be written (cf. equation 10.3):
yi +1 = yi + t f (ti , yi ) , t . f (ti , yi ) + f ti+1 , yi yi+1 yi + +1 2

(10.7) (10.8)

This is shown graphically on Figure 10-3. The dotted line has a gradient f (ti , yi ) giving the point (10.7), Eulers method. At this point ti+1 , yi +1 marked by an open circle, corresponding to equation the gradient is calculated from the differential equation, giving f ti+1 , yi , and the mean of the two +1 gradients calculated and used (10.8). On Figure 10-3 this has been shown by transferring a in equation dashed line of gradient f ti+1 , yi back to the original point and then drawing a dotted line which is +1 the mean of the two gradients and then using this to calculate the yi+1 . This method has a local error of O t3 , and hence a global error of O t2 , more accurate than Eulers method. Results for this are shown on spreadsheet 5-D IFF - EQNS . XLS.
Example 7: Repeat example 5 using Heuns method. When executed using a spreadsheet and plotted, the results were indistinguishable from the exact solution. The value obtained at t = 1 for comparison with the results from Richardsons method was y (1) = 0.1374, compared with the exact result of 0.1353, and an error of 1.6%, very much better than Eulers method, but comparable with the results from the Richardson enhancement.

10.3.2 Predictor-corrector method Trapezoidal method This is simply an iteration of the last method, whereby the steps in equations (10.7) and (10.8) are equal to the updated value of y repeated several times, at each stage setting yi i+1 . This gives a slightly +1 more accurate method. On Figure ?? this would correspond to calculating the gradient at the updated point yi+1 , transferring this back to the original point again, calculating the mean of this and that back at the original point and re-calculating the estimated yi+1 , so that the sequence of approximations on Figure ?? would be successive points at ti+1 gradually converging to the best solution obtainable with 24

Numerical methods

John D. Fenton

this method.
Example 8: Repeat the above examples but iterating Heuns procedure at each step The results were, as expected, even more accurate than in Example 7, and indistinguishable from the exact solution when plotted. The value obtained at t = 1 for comparison with the results in Example 6 was y (1) = 0.1344, compared with the exact result of 0.1353, and an error of 0.7%, more accurate than, but comparable with, the results from the Richardson enhancement and Heuns method.

10.3.3 Runge-Kutta methods These are a family of methods which are capable of high accuracy. Heuns method is actually the secondorder component of the family. A popular version is the fourth-order member, for which reference can be made to any book on numerical methods. They require the ability to calculate the right side of the differential equation at a number of points intermediate between ti and ti+1 .

10.4 Higher-order differential equations


There are many problems where second or higher-order differential equations have to be solved. The procedure in this case is to convert the nth-order differential equation to n separate rst order equations by introducing the derivatives as extra variables. Then, the procedures considered above can be used. Consider the second-order differential equation with two initial conditions, d2 y dy dy 0 (t0 ) = y0 = F t, y, with y(t0 ) = y0 , . 2 dt dt dt Introducing the variable u = dy/dt, the system becomes du 0 dt = F (t, y, u) , with y (t0 ) = y0 and u(t0 ) = y0 . dy = u dt
0, Eulers equation (10.3) applied to each differential equation becomes, with y (t0 ) = y0 and u(t0 ) = y0

ui+1 = ui + t F (ti , yi , ui ) , yi+1 = yi + t ui

(10.9)

We could continue the process to differential equations of arbitrary order, v = du/dt = d2 y/dt2 , hence d3 y/dt3 = dv/dt, etc.
Example 9: Consider a vibrating mass or pendulum with equation d2 y + y = 0, dt2 with initial conditions y (0) = 0 and y 0 (0) = 1.It is easily shown that the analytical solution is y = sin t, but here we will solve it numerically as far as t = using steps of t = 0.1. Letting u = dy/dt, the equations become du dt = y dy dt = u and Eulers method (10.9) becomes ui+1 = ui t yi yi+1 = yi + t ui

Results are shown in Figure 10-4. Again it can be seen that Eulers method is not accurate, although that

25

Numerical methods

John D. Fenton

could easily be remedied by taking shorter steps or using Richardson extrapolation. Heuns method is much more accurate for this problem too.

1 0.8 0.6 0.4 0.2 0

Exact Euler Heun

0.5

1.5 t

2.5

Figure 10-4. Comparison of results from solving a 2nd order differential equation

10.5 Boundary value problems


All the previous problems and methods studied have been Initial Value Problems (IVPs), where all conditions are known at a particular initial point. In practice there are many problems which have boundary conditions not just at one point. Examples including vibrating beams and many others. These problems, known as Boundary Value Problems (BVPs), are rather more difcult to solve. 10.5.1 Shooting methods The traditional way of solving such a problem is to turn it into an initial value problem and solve it iteratively, that is, try a number of different conditions at a single point and vary them until the remaining condition(s) are satised. This method is rather more difcult to implement on a spreadsheet. 10.5.2 A spectral method developed for this course With the ability of Excel Solver to handle systems of equations, the prospect now becomes a possibility of routinely solving such problems by using series of known functions (a spectral representation), where all that is necessary to do is to nd the coefcients of those functions. Let
y (t) = a0 0 (t) + a1 1 (t) + a2 2 (t) + . . . ,

(10.10)

where we will choose the functions i (t) before we begin. Let us suppose we have a general differential equation dy d2 y D t, y, , 2 , . . . = 0, (10.11) dt dt where D(. . .) is a function of all the variables shown. If we substitute y (t) from equation (10.10) into this, the result is a function of all the unknown coefcients, which is possibly nonlinear. We can nd the optimal values of those coefcients by minimising the sum of the squared errors of equation (10.11) at a number of points, for which Excel Solver is a very powerful tool. A useful aspect of this method is that, instead of a table of computed function values, the end result is a table of coefcients such that we can evaluate a function at any point, which is especially useful for 26

Numerical methods

John D. Fenton

plotting purposes.
Example 10: We start with a problem where the solution is the same as the previous example, but where the problem is set up as a rather more difcult boundary value problem: consider the second-order differential equation d2 y + y = 0, dt2 with boundary conditions y (0) = 0 and y (/2) = 1, which has the solution y = sin t. We assume a fth-degree polynomial in t: y (t) = a0 + a1 t + a2 t2 + a3 t3 + a4 t4 + a5 t5 , d2 y = 2a2 + 6a3 t + 12a4 t2 + 20a5 t3 . dt2 In fact, this special (linear) problem could be solved in a true spectral sense by requiring the error in the differential equation to be zero for all t and solving the resulting system of equations obtained by requiring the coefcient of each power of t to be zero, however the results are relatively poor (although it is easily shown that a0 = a2 = a4 = 0). Instead we prefer to develop a more general method. At the boundary points we write the two conditions in terms of errors 0 and N : 0 N
3 4 5 = y (t0 ) 0 = a0 + a1 t0 + a2 t2 and 0 + a3 t0 + a4 t0 + a5 t0 , 2 3 4 = y (tN ) 1. = aN + a1 tN + a2 tN + a3 tN + a4 tN + a5 t5 N 1.

(10.12)

PN Dening the total error of our approximation E = i=0 wi 2 i ,where the wi are weights which generally will be all equal to 1, but additional weight might be given to the boundary conditions to make sure that they are satised more accurately. We use Excel Solver to minimise the value of the total error E by optimisation, varying the values of the coefcients aj until an optimal solution is obtained. This is easily programmed, and is given in spreadsheet 5-D IFF - EQNS . XLS. In the present example, as we have a total of 6 unknown coefcients, it is necessary to use 6 or more computational points. We use N = 7, one more than necessary. The results are shown plotted in the gure, and it can be seen that the computed solution coincides with the exact solution to plotting accuracy, even with the relatively fewer computational points used.

For N points ti intermediate between the end points, substituting into the differential equation (10.12) gives an expression for the error at each point i : i = (2a2 + 6a3 ti + . . .) + a0 + a1 ti + a2 t2 i + ... .

There are some potential problems with this, but they are easily overcome. Particularly for civil and environmental engineering problems, where the time or space values are likely to be large (building dimension in millimetres, road and river length in metres, time in minutes over many hours, etc.), it is very important to scale the independent variable (time or space) such that the actual computational domain is [1, 1] or [0, 1]5 . An elegant away around some of the problems is to use orthogonal functions such as Chebyshev or trigonometric functions, but usually scaling to the domains shown is enough. Of course, the methods used in this section can be used also for initial value problems, and might even be the recommended way to solve them.

11. A programming language Visual Basic and Excel Macros


Visual Basic (VBA) is a typical high-level programming language, such as Fortran, C++, or Pascal. It can be entered easily via the use of Excel Macros, and this is where we start.
5

Fenton, J. D. (1994) Interpolation and numerical differentiation in civil engineering problems, Civ. Engng Trans, Inst. Engnrs Austral. CE36, 331337.

27

Numerical methods

John D. Fenton

1 0.8 0.6 0.4 0.2 0

Exact 5th degree polynomial Computational points

0.2

0.4

0.6

0.8 t

1.2

1.4

1.6

Figure 10-5. Results from solving boundary value ("two-point") problem with spectral method

11.1 Macros
Some of these notes are taken from Dr Graham Moores Excel Learning Website Xlr8tr, on http://www. ecr.unimelb.edu.au/xlr8tr/examples/macros.shtml Introduction: Macros are used in Excel to automate tasks that are performed regularly, or for problems which require "tailoring" to specic needs. The Visual Basic language is used to write scripts, and these scrips are run as "Macros". It is possible to record a Macro in Excel, or you can write it from scratch in Visual Basic code. Often it is best to record a section of the Macro, and then change the code to suit your particular needs. Recording a Macro: When a Macro is recorded, Excel follows the steps you take in Excel, and converts each of these commands into a Visual Basic script. When the Macro is run, the Visual Basic script is run, and this tells Excel to execute the commands you have chosen.
Go to Tools/Macro/Record New Macro The dialogue box will ask for a name for the Macro and you also have the option of selecting a shortcut key. If you enter a key in this eld, you are able to press Control+(the key you select) to run the Macro. This prevents having to use the menu every time you wish to run a Macro. For a Macro that graphs data, you may want to call it "graph" and give it the shortcut key "g". You also have the choice of specifying a Worksheet where your Macro will be stored. It is probably best to choose Personal Worksheet, which runs every time you run Excel. Otherwise the worksheet which contains the Macro has to be open if it is to be available. Once you are satised, click "OK" and then perform the steps (as you would normally in Excel) that you wish to automate. It is a good idea to have the steps clear in your mind and maybe even run through them before you record your Macro. Excel will convert these steps into Visual Basic commands. When you are nished, go Tools/Macro/Stop Recording.

How to run a Macro:


If you open an Excel le that has a Macro embedded in it, a warning box may appear. You must click "Enable Macros" to allow the Macros to run. If you wish to run a Macro:

28

Numerical methods

John D. Fenton

The simplest is to press Control+(the shortcut key you selected), or, Go to Tools/Macro/Macros..(which brings up the Macro dialogue box), select the Macro you wish to run from the list, and click "Run", or Press "Alt+F8" (which brings up the Macro dialogue box), select the Macro you wish to run from the list and click "Run". This will execute the script. Once the script has been executed, the original data will have been changed or rearranged (depending on the purpose of the Macro) and therefore running the Macro again will have no effect. It is necessary to reset the data to the original format to see the Macro run again. The "undo" function will not reset the data. If you have more than one Excel workbook open at the same time, you can run Macros from one workbook on a sheet from the other. For example if there is a Macro stored in "Book1" called "Macro1", and you wish to use it , in "Book2", open both books,then from Book2 go to Tools/Macro/Macros...the Macro will be named and by the book it is stored in, followed by the "!" symbol, and then the Macro name, for example, "Book1.xls!Macro1". A warning: Personally I have found Macros very useful, but there are some unsatisfactory aspects. In particular is the use of absolute cell referencing when one records a Macro. For example if you want to record a Macro to copy two cells three places to the right, Excel records the absolute reference and only uses that. For example, if you record in C13 and C14 and wish to copy to C16 and then use it anywhere else in the Workbook, it will only return to C13 and do it there. There is supposed to be an alternative form of Relative referencing, that you can turn on with some difculty, using Tools/Options/General/R1C1 and a blank Worksheet, but I have found that it still records Macros in the Absolute referencing mode. How to view a Visual Basic script: The Macros for a particular workbook are saved with the Excel le they have been recorded/written for, and it is necessary to open the workbook in order to view the script. Once the Workbook containing a Macro is open, go to Tools/Macro/Macros... , select the name of the Macro you wish to view and then click "Edit". This will open up the Visual Basic Editor, and your script will be displayed. The "Project" window in the top left hand corner of the Visual Basic Editor lists the "VBAProject" for each workbook currently open, as well as the "Sheets" contained in the workbook and the "Modules". The Modules are where the scripts for the Macros are stored. The different scripts for the different Macros can be navigated by opening the desired Modules. Writing your own Macro: Recording a Macro is a useful skill to have, however for most engineering applications, the recorded Macro alone does not deliver a suitable result. It is often necessary to tailor a Macro the specic needs of the user, and this requires working with the Visual Basic programming language. NOTE: Visual Basic is a user-friendly language, and you do not need an intricate knowledge of it to use macros.........do not be afraid!!! The language is actually quite harmless, and most of the commands are English (or slightly modied) words, like "MsgBox" to display a message box and "Select" to select a cell. In this way, it is possible to read the code and get the general idea of what is going on. Macros can be written from scratch if you are a Visual Basic whizz. If you dont know this language thoroughly, or you want to speed up the process of writing a Macro, it is possible to record a Macro that contains the vital elements you desire, and then edit it to suit your needs. The best way to learn to write Macros is to carefully examine other peoples, and determine the elements you can extract to apply to your problem. It is recommended that you run through the following example Worksheets to see what the Macros can do, and how they are written. The selection of examples covers common engineering related problems, so you may like to modify these Macros to suit your needs. Errors in Macro scripts: If you try to run a Macro and there is an error in the script, an error 29

Numerical methods

John D. Fenton

message will appear: If you click "Debug", the Visual Basic Editor will open and the error in the script will he highlighted in yellow, where you may be able to x the problem. If you click "End", the normal Excel screen will appear, and the bug in the script will remain as it is. Examples: One that I use is a routine which takes a cell and copies it down until it meets another non-zero cell or the end of a data block.
Sub Copydown() Selection.Copy Range(Selection, Selection.End(xlDown)).Select ActiveSheet.Paste End Sub

The following site has some more extensive examples: http://www.ecr.unimelb.edu.au/xlr8tr/index. shtml You can download the example spreadsheets and then open the Visual Basic Editor to examine the scripts. The scripts are heavily annotated (comments in green after the ) and describe each element and its purpose.

11.2 Visual Basic


11.2.1 What Is Visual Basic? Visual Basic (VB, or VBA) is a high-level language, in that it contains many of the characteristics of other high-level languages such as Fortran, Pascal, C++. It evolved from the earlier DOS version called BASIC (Beginners Allpurpose Symbolic Instruction Code). It is a fairly easy programming language to learn. Most conveniently for us, it sits behind Excel, and that provides a convenient way into it, via Macros at rst, and then one can use it alone to do more complicated programming operations. As Excel is on most personal computers, VB is available to most of us, whereas with other high-level languages separate compilers are necessary. The current version wick works behind Excel is VB 6. After this version Microsoft has called it VB.NET. 11.2.2 Beginners Guides There are many of these on the Internet. There are many books available. Personally, the lecturer has always found using the software to be a better way than many books. A search on Google came up with the following on the rst page:
http://www.codepedia.com/1/BeginnersGuideToVB http://www.vbtutor.net/vbtutor.html http://visualbasic.about.com/.

11.2.3 Entering via Excel Tool/Macro/Macros Pressing Alt-F11 or Tool/Macro/Macros opens up the VB window. To insert a new Procedure, go Insert/Procedure, and choose probably to call it a "Sub", which is short for Subroutine, a particular program which can do a variety of things. The other details do not matter at this stage.

30

Numerical methods

John D. Fenton

If you have to return a numerical value for a Worksheet, for example, then call it a Function. It is a good idea to press Ctl-G to bring up the "Immediate" window, which shows results which can be cut and pasted, deleted, etc. 11.2.4 Features of the language and interface As an example, let us type the following, noting that anything after an apostrophe is treated just as a Comment. Sub Print_Example() String1 = "See results in the Immediate pane" A String, enclosed in quotes String2 = "Hello" Another string Debug.Print String1 This is quite a clumsy way to print here For i = 1 To 5 A "For" loop, which enables repetitive operations Debug.Print String2, i We print out the value of i as it repeats Next i Tells the computer to increment i and loop again until satised End Sub Debugging: Now, with the cursor somewhere in that program, we could press F5 and the program would run. However, almost always, we will have made some error, and we will instead "Debug" it by pressing F8, then we can step through the program line by line, checking our output as we go, by repeatedly pressing F8. To leap to a point further down, move the cursor to the desired line and then press Ctl-F8. If there is anything wrong, the program will tell us. In this case, all results appeared in the Immediate Window as we stepped through. Writing to les: Now we consider a rather more powerful approach, where we can write to a le directly. For example, we want to print out values of sin x for x from 0 to in 10 steps. Firstly we have to Open a le and give it a Stream number, 3 in this case, and after writing is complete we should close it: Open "C:/Testfile.txt" For Output Shared As #3 ............. Close #3 Repeated operations, Looping: Now we can set the value of and step through the values, printing out to Stream 3, and then closing the stream and le at the end. Pi = 3.141592653 For i = 0 To 10 Print #3, i, Sin(i*Pi/10) Next i Variable names must begin with a letter as the rst character. You cannot use a space, full stop (.), exclamation mark (!), or the characters @, &, $, # in the name. Names cant exceed 255 characters in length. If...Then...Else...End If Statements: These allow huge developments, enabling branching of the program execution depending on the value of an expression. If <condition> Then [statements] [Else elsestatements] For example if we have a number x, and we want to take the logarithm, we might guard against zero or negative values by:

31

Numerical methods

John D. Fenton

If x > 0 Then y = Log(x) Else Debug.Print "Attempted to take Log of 0 or negative number" End If Various logical or Boolean operators can be used: < (Less than), <= (Less than or equal to), > (Greater than), >= (Greater than or equal to), = (Equal to), <> (Not equal to). Watching values of quantities: So that we dont have to write out print statements for values of quantities, at the Debug stage we can use the Watch Window (View/Watch Window) and by rightclicking on the Expression column, we can add a Watch and monitor the value of any variable. This is very helpful. Finally: there is a huge amount in VB, however it is possible to use it at a relatively simple level one learns by going from the known to the unknown. Good luck!

32

You might also like