Numerical Methods
Numerical Methods
Numerical Methods
(These notes are based mainly on the 6th Edition of the textbook
Numerical Methods for Engineers by S.C. Chapra and R.P. Canale.)
Error Definitions
Et = True Error = True value Approximate value
True relative error =
True percent relative error is given by t where
100
where
100 =
100
Computers can hold only a finite number of significant digits and this leads to round-off errors.
Fractional quantities are typically represented in computers using the floating-point form m b e where m
is the mantissa (or significand), b is the base of the number system and e is the exponent. Floating-point
numbers can be stored in single or double precision format according to IEEE 754 standard. By default,
Matlab stores all numeric quantities, values as double-precision floating-point. Single and double
precision correspond to about 7 and 16 significant decimal digits, respectively. When storing floating-point
numbers and doing arithmetic with floating-point numbers, double precision should be used to limit roundoff errors.
In numerical analyses which involve iterative procedures, computations can be repeated until | | <
where
is the prespecified tolerance or error criterion. It can be shown that when the criterion
s (0.5 10 2 n ) % is met (i.e. when | | < ), then the result is correct to at least n significant digits. Note
that this is a conservative criterion and it can yield results which are correct to more than n significant digits
(see Example 3.2 in the textbook).
Another criterion that gives a measure of accuracy in terms of significant digits is as follows. The number
x is said to approximate xt to n significant digits if
xt x
5 10 n
xt
The above criterion is appropriate to use if | | can be calculated (i.e. the true value is known). Hence, the
number x is said to approximate xt (the true value) to n significant digits when t (5 10 2 n )%
Taylor Series
Taylor Series is an infinite power-series and it predicts the value of the function at one point using the value
of the function and its derivatives at another point as follows:
(
) = ( )+
( ) +
) ( )+
( )
+
2!
( )
( )
+ +
3!
) around (about)
( )
+ +
3!
The remainder term (the truncation error for the nth-order approximation)
=
)(
)!
where
Zero-order approximation:
) ( )
First-order approximation:
) ( )+
Second-order approximation:
<
<
and =
( )
+
!
( )
( )
+
2!
( ) +
( )
( )
( )
is given by:
(h is the step size)
( )
) ( )+
( )
( ) +
The nth-order Taylor Series expansion will be exact for an nth-order polynomial.
In general, the truncation error is decreased by including additional terms in the expansion but we should
also limit the step size. We can also decrease the truncation error if we reduce the step size sufficiently (note
that when the step size is reduced, the term ( ) ( ) will change). We usually select the step size as
| | < 1. For example, consider two successive terms in the Taylor Series expansion which involve the
factors and , respectively. If the step size is taken as 10 (where p is a positive integer), then the
ratio /
equals 10 . This shows that if
increases, then the ratio /
also increases.
Lets make a formal definition of Taylor Series as follows:
If ( ) has derivatives of all orders at
(i.e. if
( )(
!
is called the Taylor series of
of Taylor series.
. If
( )
( ) exists for
( )
= 0, then we use the term Maclaurin series instead
( )(
Taylor Polynomials
Taylor polynomials are partial sums of the Taylor series of a function about a given point = . We use
Taylor polynomials to obtain approximations for a given function . The nth-order Taylor polynomial of
about = is denoted by which is given as follows.
( ) = ( )+
( )( ) +
( )
( )
( ) +
2!
( )(
( )=
( )
( ) + +
3!
( )
( )
( )
!
( )
Recall that the infinite series obtained by taking the limit of ( ) as is called the Taylor series of
about = . Notice that matches and its first n derivatives at = i.e.
( ) = ( ),
( )=
( ),
( )
( )=
( )(
For example, the 2nd-order Taylor Series approximation of a function around point =
is equivalent
to constructing a 2nd-order polynomial ( ) as given below. In other words, ( ) is the 2nd-order Taylor
polynomial of around = .
( )= ( )+
Note that
( )= ( ),
( ) = ( ) , and
( )=
Fig. 1.1
( )(
)+
( )
(
2!
( ) = ( ).
)(
( + 1)!
( ) is the nth-
( )
)= ( ,
)+
where =
1
!
2!
+ 2
and all the partial derivatives are evaluated at the base point. Here, you
=
(Note that
= 1,
<
=1,
1
( + 1)!
+1
)(
<
<
and
<
) (
( , )
and
( )(
) = ( )+ ( )( )
where is on the line segment joining and . Note that ( ) is the gradient vector of at and we have
a dot product operation between ( ) and ( ). Here , and can be regarded as both points and
vectors. (You should refresh your knowledge on vectors and vector calculus). If is close to , then is
close to , then we can make the following approximation if the first partial derivatives of are continuous
( ) ( )+ ( )( )
Therefore, the linearisation of
at
is defined as
( ) = ( )+ ( )( )
Note: Gradient is the vector of all of the first partial derivatives of a scalar-valued function of several
variables. Hence, if is a scalar-valued function of variables , , ,
then the gradient of is a row
( )
( )
( )
) = lim
( ,,
+ ,
,,
) ( ,,
with respect to
is
Example:
( ,
)=
Solution:
( ) = ( )+
( )=2+
Example:
( )(
)+
) (
( )(
)+
] = [6
at
Solution: ( , , ) = [
,
). We have
) (
)=1+
( ) around (1,0).
( , , )=3
( ,
at ( ,
12
) = (1,2, 1) = [ (1,2, 1)
(1,2, 1)
) = (1,2, 1)
]
(1,2, 1)] = [48
36 96]
( , , ) = (1,2, 1) + (1,2, 1) ( 1, 2, + 1)
= 24 + (48, 36, 96) ( 1, 2, + 1)
= 24 + 48( 1) + 36( 2) 96( + 1)
= 48 + 36 96 192
Error Propagation
Consider a function ( , ) and let and be the approximations of x and y respectively. The problem is that
x and y are not known exactly, hence the exact value of ( , ) is unknown. If and are close enough to x
and y respectively, then a Taylor Series can be written to estimate ( , ) by dropping all second and higherorder terms as follows (note that the partial derivatives are evaluated at ( , )) :
( , ) ( , )+( )
+( )
where
( , ) = | ( , ) ( , )| , = | | and = | | .
and are the estimates of the errors in x and y respectively, whereas ( , ) represents the estimate
of the resulting error in ( , ).
Similarly, for n independent variables
analysis reveals,
( ,
,,
,,
with errors
,,
, a first-order error
++
Example: Consider the following formula for a stream through a rectangular channel where Q = flow rate
(m3/s), n = roughness coefficient, B = width (m), S = slope, and H = depth (m). For a particular case, the
width and the depth of the stream are 20 m and 0.3 m respectively. However, the roughness and the slope can
only be known with a precision of 10%. That is to say, the roughness coefficient is known to be about 0.03
with a range from 0.027 to 0.033 and the slope is known to be about 0.0003 with a range from 0.00027 to
0.00033. Apply a first-order error analysis to estimate the uncertainty (or error) in the flow rate.
=
)/
( +2 )
(
Solution:
We have
= 20,
Note that
= 0.3,
= 0.03,
)/
( +2 )
(
= 50.74 ,
and
)/
( +2 )
(
0.5
/
= 0.00003
are evaluated at ( , ).
= 2536.9
Therefore, the error in the flow rate will be about 0.228 m3/s
Condition Number
A computations condition is an indication of its sensitivity to uncertainty. A first-order Taylor Series
expansion ( ) ( ) + ( )( ) can be used to describe the condition number (CN) for functions of
single variable as follows where is an approximation of . Define,
[ ( )] =
( ) ( )
( )
( )( )
( )
[ ]=
[ ( )]
=
[ ]
( )
( )
where [ ( )] is the relative error of ( ) with respect to ( ) and [ ] is the relative error of x with respect
to . CN is the ratio of these two relative error terms. When CN=1, the functions relative error is identical to
the relative error in x. Functions having | | values much larger than 1 are said to be ill-conditioned i.e.
such functions will amplify severely the uncertainty in x. If | | is not much larger than 1, then the function
is said to be well-conditioned. For values of | | less than 1, the functions relative error is attenuated in
comparison to the relative error in x.
Example: Compute the condition numbers for ( ) = | 1| + 1 at the given x-values.
Solution:
| 1|
1
( )=
1
2 | 1|
[ ( )]
=
=
[ ]
( )
( )
= 1 since
| 1|
1
| 1| + 1
(2.5) = 0.233
(1.5) = 0.184
(0.01) = 0.00252
(1.5) = 0.621
( )=
2 | 1|
(0.5) = 0.207 ,
(0.5) = 0.092
(1.0001) = 49.51
(2.5) = 0.459
Operators can be either unary or binary i.e. they have one or two operands, respectively. A unary operator is
an operator which acts on only one operand. - is a unary operator when used to negate a value, as in b=-2.
A binary operator is an operator which operates on two operands.
Operator precedence is the order in which Matlab evaluates an expression. Within each precedence level as
shown below, operators have equal precedence and are evaluated from left to right. The operator precedence
rules for the operators in Matlab are shown in the below list, ordered from the highest precedence level to the
lowest precedence level:
1) Parentheses
2) Transpose (.'), power (.^), complex conjugate transpose ('), matrix power (^)
3) Unary plus (+), unary minus (-), logical negation (~)
(Note: A unary operator is an operator which acts on only one operand. - is a unary operator when used to
negate a value, as in b=-2. In Matlab, when we type -2^2, we get -4; and when we type (-2)^2, we get 4.)
4) Multiplication (.*), right division (./), left division (.\), matrix multiplication (*), matrix right division (/),
matrix left division (\)
5) Addition (+), subtraction (-)
6) Colon operator (:)
7) Less than (<), less than or equal to (<=), greater than (>), greater than or equal to (>=), equal to (==), not
equal to (~=)
8) Element-wise AND (&)
9) Element-wise OR (|)
10) Short-circuit AND (&&)
11) Short-circuit OR (||)