Escolar Documentos
Profissional Documentos
Cultura Documentos
Numerical Methods
Part I
D.U.J. Sonnadara
Department of Physics, University of Colombo
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 ....)
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
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
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
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.
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
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);
Example - 1
PROGRAM OUTPUT
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.
f ( x)dx =
a a
f ( x)dx +
a+2h
f ( x)dx + ............. + f ( x)dx
b2 h
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 )
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
Example - 2
1
Calculate e x dx
0
using Simpsons rule.
Example 2
PROGRAM OUTPUT
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.
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
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
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
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);
17
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
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
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
h
I = [ g (a) + g (b) + 4 g (a + i h) + 2 g (a + i h)]
3 i =1 i =2
odd even 21
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
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