You are on page 1of 22

Methods in Computational Physics

Methods in Computational Physics


PH 4002

Numerical Methods
Part I

D.U.J. Sonnadara
Department of Physics, University of Colombo

Part I: Numerical Methods 1


Methods in Computational Physics

Basic mathematical operations


Three numerical operations, namely, differentiation, integration
and root finding are central to most computer modeling of
physical systems.

Suppose that we have the ability to calculate the value of a


function, f(x), at any value of the independent variable x. In
differentiation we seek one of the derivatives of f at a given
differentiation,
value of x. In integration;
integration we calculate the area between two
specific limits. In root finding we seek the values of x at which f
vanishes.

If f is known analytically, it is possible to derive explicit formulas


to evaluate the derivative or the integral. However, it is often
possible that we cannot use analytical methods due to either
complicated numerical procedures involved in evaluating f or we
know f only at discrete points. In addition, roots of most
functions cannot be found analytically.
2

Part I: Numerical Methods 2


Methods in Computational Physics

Numerical differentiation
Let us suppose that we are interested in calculating the derivative at x=0,
f(0). Assume that we know f on an equally-spaced lattice of x values.
f n = f ( xn ); xn = nh (n = 0, 1, 2 ....)

The goal is to compute an approximate value for f(0) in terms of fn.

Part I: Numerical Methods 3


Methods in Computational Physics

Differentiation
Let us use the Taylor series and expand f in the neighborhood of x=0.
x2 x3
f ( x) = f 0 + xf '+ f ' '+ f ' ' '+.........
2! 3!
where all derivatives are evaluated at x=0.

By substitution we get;
h2 h3
f 1 = f ( x = h) = f 0 hf '+ f ' ' f ' ' '+O(h 4 )
2 6
4h3
f 2 = f ( x = 2h) = f 0 2hf '+2h f ' '
2
f ' ' '+O(h 4 )
3
where O(h4) means terms of order h4 or higher.
4

Part I: Numerical Methods 4


Methods in Computational Physics

Differentiation
Upon subtracting f-1 from f+1 we get;

2h3 f +1 f 1 h 2
f +1 f 1 = 2hf '+ f ' ' '+O(h5 ) i.e. f ' = f ' ' '+O(h 4 )
6 2h 6

The term involving f vanishes as h becomes small.

f +1 f 1
Hence; f '= + O(h 2 )
2h
This 3-point formula would be exact if f were a second degree polynomial
in the 3-point interval [-h, +h], because the third and all higher order
derivatives would then vanish. The error term, (order of h2) can be made
as small as possible by choosing smaller and smaller values for h.
5

Part I: Numerical Methods 5


Methods in Computational Physics

Differentiation
The use the symmetric difference about x=0 is more accurate than the
forward or backward difference formulas;
f +1 f 0 f 0 f 1
f '= + O ( h) f '= + O ( h)
h h
These 2-point formulas are based on the assumption that f is well
approximated by a linear function over the interval between x=0 and
x=h.

It is possible to improve the 3-point formula by relating f to lattice


points further away from x=0.

For example; the 5-point formula can be given by;

f '=
1
[ f 2 8 f 1 + 8 f +1 f + 2 ] + O(h 4 )
12h
which cancels all derivatives in the Taylor series through fourth order.
6

Part I: Numerical Methods 6


Methods in Computational Physics

Example - 1
Evaluate f(x=1) when f(x) = sin (x).
Exact answer: cos(1) = 0.540302

clear;
x=1; h=0.2; exact=cos(x);
for i=1:5;
h=h/2; ix(i)=h;
fp3=(sin(x+h)-sin(x-h))/(2*h);
diff1(i)=exact-fp3;
fp5=(sin(x-2*h)-8*sin(x-h)+8*sin(x+h)-sin(x+2*h))/(12*h);
diff2(i)=exact-fp5;
end;
disp(ix); disp(diff1); disp(diff2);

Part I: Numerical Methods 7


Methods in Computational Physics

Example - 1
PROGRAM OUTPUT

h= 0.1 0.05 0.025 0.0125 0.0063

3-point*1e-3 0.9001 0.2251 0.0563 0.0141 0.0035

5-point*1e-5 0.1799 0.0113 0.0007 0.0000 0.0000

Formulas for higher derivatives of f can be constructed by taking


appropriate combinations. For example we can show that;
f +1 2 f 0 + f 1 = h 2 f ' '+O ( h 4 )
f +1 2 f 0 + f 1
Hence, f ''= + O(h 2 )
h2
8

Part I: Numerical Methods 8


Methods in Computational Physics

Numerical integration
In integration, we are interested in calculating the definite integral of f
between two limits, a<b. We can arrange these values to be points of
lattice separated by an even number of lattice spacing. i.e. N=(b-a)/h.

It is sufficient for us to derive a formula for the integral from -h to +h


since this can be repeated as many times as needed.
b a+2h a+4h b

f ( x)dx =
a a
f ( x)dx +
a+2h
f ( x)dx + ............. + f ( x)dx
b2 h

The basic idea is to approximate f between h and +h by a function that


can be integrated exactly. The approximate integral is;
+h
h

h
f ( x)dx =
2
( f 1 + 2 f 0 + f +1 ) + O(h 3 )

which is the well-known trapezoidal rule.


9

Part I: Numerical Methods 9


Methods in Computational Physics

Integration
A better approximation can be obtained by using difference formulas
for f and f derived earlier through Taylor series expansion. For |x|<h
we get
f +1 f 1 f 2 f 0 + f 1 2
f ( x) = f 0 + x + +1 2
x + O ( x 3
)
2h 2h
which can be integrated to obtain
+h +h
f +1 2 f 0 + f 1 x 3

h = + + O(h )
5
f ( x ) dx 0
f x 2
2h 3 h
+h
h

h
f ( x)dx =
3
( f +1 + 4 f 0 + f 1 ) + O(h 5 )

which is the Simpsons rule accurate up to two orders of magnitude


higher than trapezoidal rule.
10

Part I: Numerical Methods 10


Methods in Computational Physics

Integration
Note that the error in the previous expression is actually better than
would be expected naively since x3 term gives no contribution to the
integral.
a+2h
h
Finally we get;
a
f ( x)dx =
3
( f (a ) + 4 f (a + h) + f (a + 2h))

Thus;

f ( x)dx =
h
[ f (a) + 4 f (a + h) + 2 f (a + 2h) + 4 f (a + 3h) + ......4 f (b h) + f (b)]
3
a
n 1 n2


h
= [ f (a) + f (b) + 4 f ( a + i h) + 2 f ( a + i h )]
3 i =1 i =2
odd even
11

Part I: Numerical Methods 11


Methods in Computational Physics

Example - 2
1


Calculate e x dx
0
using Simpsons rule.

Exact answer: e 1 = 1.718282


clear; n=2; exact=exp(1)-1
for k=1:5;
sum=exp(0); n=n*2; h=1/n; fac=2;
for i=1:n-1;
if (fac==2); fac=4; else; fac=2; end;
x=i*h; sum=sum+exp(x)*fac;
end;
sum=sum+exp(1); result=h*sum/3; diff(k)=exact-result;
end;
disp(diff);
12

Part I: Numerical Methods 12


Methods in Computational Physics

Example 2
PROGRAM OUTPUT

h= 0.25 0.125 0.0625 0.0313 0.0156

Simpsons*1e-4 0.3701 0.0233 0.0015 0.0001 0.0000

Higher order formulas can be derived by retaining more terms in the


Taylor expansion.

However, the coefficients of the values of f at various lattice points


can have both negative and positive signs in higher-order formulas
making round-off errors a potential problem. Thus, it is usually safer
to improve the accuracy by using a low-order method and making h
smaller rather than resorting to a higher-order formula.
13

Part I: Numerical Methods 13


Methods in Computational Physics

Finding roots
The aim is to find the root of a function f(x) that we can compute
for arbitrary x. One method is, if the approximate location (x=x0)
of the root is known, guess a trial value of x (less than the root)
and increase the trial value by small positive steps, backing up
and halving the step size each time f changes sign.

The values of x generated by this procedure evidently converge


to x0, so that the search can be terminated once the step size
falls below the required tolerance.

One must be careful when using this method, since if the initial
step size is too large it is possible to step over the root desired
when f has several roots.

14

Part I: Numerical Methods 14


Methods in Computational Physics

Example - 3
A program to find the positive root of the function f(x) = x2 - 5 to a
tolerance of 10-6 using x=1 as the initial guess and initial step size of 0.5.

clear;
tolx=1e-6; x=1; dx=0.5; EXACT = 2.2361
iter=0; fold = x*x-5.; iterations 1 through 5:
while abs(dx)>tolx, 1.5000 2.0000 2.5000 2.2500 2.1250
iter = iter + 1;
iterations 6 through 10:
x = x + dx; fnew=x*x-5.;
2.2500 2.1875 2.2500 2.2188 2.2500
roots(iter) = x;
if (fold*fnew<0); iterations 11 through 15:
x=x-dx; dx=dx/2; 2.2344 2.2500 2.2422 2.2383 2.2363 etc.
end;
end; Total of 33 iterations are required.
disp(roots);

15

Part I: Numerical Methods 15


Methods in Computational Physics

Finding roots
A more efficient algorithm, Newton-Raphson, is available if we can
evaluate the derivative of f for arbitrary x. This method generates a
sequence of values xi converging to x0 under the assumption that f is
linear near x0.
i +1 f (xi )
x =x
i
f ' ( xi )
= x i x i

xo
xi+1 xi f(xi) = f(xi)/xi

xi
16

Part I: Numerical Methods 16


Methods in Computational Physics

Example - 4
clear;
tolx=1e-6; x=1; dx=0.5; iter=0;
while abs(dx)>tolx,
iter = iter + 1;
xold = x;
x = x - ((x*x-5.)/(2*x));
dx = xold - x; roots(iter) = x;
end;
disp(roots);

Required only 5 iterations.

3.0000 2.3333 2.2381 2.2361 2.2361

17

Part I: Numerical Methods 17


Methods in Computational Physics

Finding roots
The secant method provides a better compromise between the efficient
of Newton-Raphson and the trouble of evaluating the derivatives. If the
derivative of the above equation is approximated by the difference
formula,
f ( x i ) f ( x i 1 )
f ' (xi )
x i x i 1
Then we obtain the following 3-term recursion formula,

i +1 ( x i x i 1 )
x = x f (x )
i i

f ( x i ) f ( x i 1 )
Any two approximate values of x0 can be used for x0 and x1 to start the
algorithm, which terminates when the change in x from one iteration to
the next is less than the required tolerance.
18

Part I: Numerical Methods 18


Methods in Computational Physics

Finding roots
Geometrical representation of the secant method. If the initial guesses
are close to true root, convergence to the answer in secant method is
almost as rapid as Newton-Raphson method.
i +1 ( x i x i 1 )
x = x f (x )
i i
f ( x i ) f ( x i 1 )

= x i f ( x i ) = x i x i

xi-1
xo
f(xi)/xi=/
xi+1 xi

xi
19

Part I: Numerical Methods 19


Methods in Computational Physics

Example - 5
The MatLab program that uses secant method starting with values x0=1
and x1=1.5 is shown below.

clear;
tolx=1e-6; x1=1; x2=1.5; dx=x2-x1; iter=0;
while abs(dx)>tolx,
iter = iter + 1;
iterations 1 through 4
xsave = x2;
xf1 = x1*x1-5.; xf2 = x2*x2-5.; 2.6000 2.1707 2.2311 2.2361
x2 = x2 - ((xf2*dx)/(xf2-xf1));
iterations 5 through 6
x1 = xsave;
dx = x2 - x1; roots(iter) = x2; 2.2361 2.2361
end;
disp(roots); Required only 6 iterations.

20

Part I: Numerical Methods 20


Methods in Computational Physics

Annex 1: Multiple integrals


b d ( x)
Consider the double integral given by; I= f ( x, y) dy dx
a c( x)
Assume n equally spaced subintervals in x, each of width h. The inner
integrals can be evaluated as;
d (a) d ( a + h)
g (a) = f (a, y)ddy(bh) g (a + h) = f (a + h, y)dy ........
c(a) c(a + h) d (b )

g (b h) = f (b h, y )dy g (b) = f (b, y )dy


c (b h ) c (b )

Since x is fixed inner integrals become 1D integrals on y. Finally the


outer integral becomes;
n 1 n2


h
I = [ g (a) + g (b) + 4 g (a + i h) + 2 g (a + i h)]
3 i =1 i =2
odd even 21

Part I: Numerical Methods 21


Methods in Computational Physics

Annex 2: Singularities
There are integrals with finite limits which have an integrand that is
singular at one or both limits, but for which the integral exists and
finite. 1 0


1

1
dx dx
0
x 1
1 x

The best method for dealing with such singularities is to eliminate


them if possible.
1 1 0.01 1


1 1


dx = 2 x 1/ 2
=2 1 1 1
dx = dx + dx
x 0
0 x x 0.01
x

Question is the selection of and the step size h. One may consider
two parts with different step sizes to achieve the required accuracy or
use a technique such as Gauss Quadrature.
22

Part I: Numerical Methods 22

You might also like