Vous êtes sur la page 1sur 52

1

Basic MATLAB functions and operations used in


ME 205: Computer Methods in Engineering.
Complied by
Paul F. Joseph and Chandramouli Tangella
August 6, 2003
Introduction.
This handout provides a review of some basic MATLAB functions and operations for the
following topics from ME 205:
1. Taylor Series. Page 1
2. Polynomials. Page 4
3. Zeros of functions. Page 5
4. Matrix algebra. Page 8
5. Solution of a System of Linear algebraic equations. Page 12
6. Curve fitting (Not covered Spring 2003). Page 15
7. Interpolation. Page 18
8. Numerical integration. Page 21
9. Numerical differentiation. Page 28
10. Numerical solution of ordinary differential equations Page 31
This list covers most of the functions used in ME 205, but represents a very small
fraction of the MATLAB functions that are available. The ability to find what is needed
using a manual or online help is very important. The handout also includes some
theoretical background for most of the topics.
1. Taylor Series.
The Taylor series plays a central role in establishing formulas in numerical analysis.
This series is based on representing a function by a power series expanded about the
point a,
k
k
k
) a x ( c ) x ( f =

=0
, (1.1)
where c
k
are constants independent of x. In such a representation there is a concern for
the following: 1) convergence of the infinite series, 2) continuity of the function and its
derivatives and 3) the existence of points where the function is singular. An example of a
simple function that illustrates all these concerns is 1/(1 - x). By algebraic manipulation,
this function can be rewritten as
x
x
x x x x
x
n
n

+ + + + + + =

+
1
1
1
1
1
3 2
L , (1.2)
for any positive integer n. The power series of this function expanded about zero (a = 0)
is
L + + + + + = =

=
4 3 2
0
1
1
1
x x x x x
x
k
k
. (1.3)
In order to have convergence of (1.3), the remainder term from (1.2), i.e.,
x 1
x
R
n
n

= , (1.4)
2
must approach zero as n increases. Therefore the series converges only for 1 x 0 < .
Furthermore, the series has continuous derivatives for values of x within this range and
has a singular point (blows up) at x = 1 (see Figure 1.1). In this course, for values of x
in the region of interest, it will be assumed that the series converges, all derivatives of the
function are continuous, and no singularities exist. Given this assumption, the c
k
constants in (1.1) were determined by Brook Taylor in 1715 to give what is now called
the Taylor series expanded about the point x = a,
k
k
k
) a x (
! k
) a ( f
) x ( f =

=0
. (1.5)
In this formula f
k
(a) is the k
th
derivative of f with respect to x evaluated at x = a. An
example of a Taylor series is given by (1.3). Since a = 0 the series is also referred to as a
Maclaurin series. Another example of such a Maclaurin series (or equivalently a Taylor
series expanded about x = 0) is
L + + =
7 5 3
7
1
5
1
3
1
x
!
x
!
x
!
x ) x sin( . (1.6)
This series converges for all values of x, i.e., there are no singular points. A calculator
actually uses (1.6) to obtain the sine of x. This is possible because the infinite series can
be truncated to a finite number of terms with negligible error. This important point
makes the Taylor series useful in numerical analysis.
When (1.5) is truncated to n
th
order, the terms that are omitted comprise the error term
or the remainder, R
n
. Introduction of the remainder converts (1.5) into the following n
th
order Taylor series,
n
n n
R ) a x )( a ( f
! n
) a x )( a ( f ) a ( f ) x ( f + + + + =
1
L , (1.7)
where from (1.5)

+ =
=
1 n k
k
k
n
) a x (
! k
) a ( f
R . (1.8)
The interpretation of (1.7) is that the Taylor series gives a prediction of a quantity (the
function f) at a point in space or time (the value x) based on information (f and its
derivatives) evaluated at a nearby point in space or time (the value a). The accuracy of
the prediction (value of R
n
relative to f(x)) depends on two things: 1) how much
information is known (value of n: the more terms the better) and 2) how near the point is
(value of x a: the smaller the better).
The bottom line is that if the difference (x a) is small enough, the terms in (1.5) become
less and less important as k increases. This is seen very clearly in the example (1.3) for x
near zero. Therefore, the more terms in the series, the more accurately the series
represents the function. Similarly, for a given number of terms, the smaller the value of
(x a), the more accurate the series. Both of these points are illustrated in Figure 1.1.
Getting back to the remainder, for given values of n and x, it can be proven that the
infinite series (1.8) can be expressed as the single term
3
1
1
1
+
+

+
=
n
n
n
) a x (
)! n (
) ( f
R

, (1.9)
where the special point lies somewhere between x and a. The following important
notation can be used to represent the remainder:
] ) a x [( O R
n
n
1 +
= , (1.10)
which is read, order of (x - a) to the n + 1 power. This provides information on how
accurate the series is. The interpretation is that the remainder is some constant times (x -
a)
n+1
. In computational analysis, the actual value of the constant is not known. As
shown by (1.9), as long as the n + 1
st
derivative of the function is well-behaved
between x and a, the constant, and therefore the error, can be judged based on (x a)
n+1
.
As an example of the order of notation, a first order Taylor series of a function f,
expanded about the point a, can be expressed as
] ) a x [( O ) a x )( a ( f ) a ( f ) x ( f
2
+ + = .
If the remainder term is omitted, the equation should be expressed as approximate, i.e.,
) a x )( a ( f ) a ( f ) x ( f + .
This approximation is a straight line tangent to the function f(x) at x = a. The more terms
taken in the series, the more the approximation wraps around the actual function. It is
important to visualize this point, so the following plots are provided for the function,
1/(1 x).
Figure 1.1 Taylor series representations of the function 1/(1 x) expanded about x
= 0. First order, second order, and eighth order series are compared to the exact
curve. Observe that convergence occurs for 1 < x < 1.
Now consider the following limit that relates some basic calculus to the remainder term:
x
) x sin(
x
lim
0
.
LHospitals rule can be applied to evaluate this zero over zero limit, which is done as
follows:
1
1 0 0 0
=

) x cos(
x
lim
x
dx
d
) x sin(
dx
d
x
lim
x
) x sin(
x
lim
.
4
Equation (1.6) will give the same result by dividing both sides by x and taking the limit,
L + + =
6 4 2
7
1
5
1
3
1
1 x
!
x
!
x
! x
) x sin(
1
7
1
5
1
3
1
1
0 0
6 4 2
=
(

+ +

L x
!
x
!
x
! x
lim
x
) x sin(
x
lim
.
Similarly the series (1.6) with the notation (1.10) can be used to be just as sure of the
final result as follows,
) x ( O x ) x sin(
3
+ =
) x ( O
x
) x sin(
2
1+ =
| | 1 1
0 0
2
= +

) x ( O
x
lim
x
) x sin(
x
lim
.
Now to see what MATLAB can do. The MATLAB command to generate an n
th
order
Taylor series for the function f(x) centered about the point x = a, i.e.,
n n
) a x )( a ( f
! n
) a x )( a ( f
!
) a x )( a ( f ) a ( f ) x ( f + + + +
1
2
1
2
L
is: taylor(f,n+1,a). Note that this command must evaluate n derivatives of the function in
addition to evaluating the above series. For example, the MATLAB code for a second
order Taylor series of sin(x) centered around x = /6 is:
% syms x declares x as a symbolic variable (as opposed to a number or array of numbers).
>> syms x % Allows some MAPLE commands (see Table 9.3-1 in Palm) to be used in
% MATLAB Palm, Chapter 9.
>> ft=taylor(sin(x),3,pi/6)
ft = 1/2+1/2*3^(1/2)*(x-1/6*pi)-1/4*(x-1/6*pi)^2
>> % use the subs command to evaluate this Taylor series at x = 0.5
>> subs(ft,x,0.5) % subs is a MAPLE command that evaluates ft at x = 0.5.
ans = 0.4794
Equivalent and very important forms of the Taylor series are:
1.
n n
x ) x ( f
! n
x ) x ( f
!
x ) x ( f ) x ( f ) x x ( f + + + + = +
1
2
1
2
L ,
where x plays the role of a and x = x a,
2. , h ) x ( f
! n
h ) x ( f
!
h ) x ( f ) x ( f ) x ( f
n
i
n
i i i i
1
2
1
2
1
+ + + + =
+
L
where x
i+1
x
i
= h is the same as x a and x
i
plays the role of a. You should be able to
use all of these expressions and be able to convert from one to another.
2. Polynomials
An n
th
order polynomial of the form
0
0 1
1
1
= + + + + =

a x a x a x a ) x ( p
n
n
n
n n
L , (2.1)
occurs frequently in engineering and science. This is the most common type of nonlinear
equation, where x is unknown and the a
k
coefficents are known. The most common
5
polynomial is second order, also referred to as a quadratic equation, and is given by
0
2
= + + c bx ax .
This case has the well-known closed form solution,
a
ac b b
x
2
4
2

= .
It is observed that if b
2
4ac < 0, there are two complex roots which are complex
conjugates of each other. There are also closed form solutions for third and fourth order
polynomials as well, however, when n > 4, the solution must be obtained numerically.
There are several built-in functions within MATLAB that are useful for treating the n
th
order polynomial defined in (2.1). Three of the important functions are:
Defining a polynomial. An n
th
order polynomial is defined by its n+1 coefficients. An
array of size n+1 is used to represent an n
th
order polynomial as follows:
a = [a
n
, a
n+1
,,a
1
, a
0
].
Evaluating a polynomial: Given the coefficient vector a = [a
n
, a
n+1
,,a
1
, a
0
], the value
of the polynomial p
n
(x) is given by: polyval(a,x).
Roots of a polynomial. The roots of a polynomial are obtained using roots(a). An n
th
order polynomial will have the n roots, x
i
, i=1,n. If the coefficients of the polynomial
are real, then complex roots must occur in conjugate pairs, i.e., if 1 + i is a root, then so
too must 1 - i. Knowing the roots, the polynomial can be factored as:
0
2 1
0 1
1
1
= =
+ + + +

) x x ( ) x x )( x x (
a
a x a x a x a
n
n
n
n
n
n
L
L
.
Example: Determine the roots of the third order polynomial, 3x
3
2x
2
+ 4x + 7 = 0.
>> a=[3,-2,4,7];
>> roots(a)
ans =
0.7694 + 1.4434i
0.7694 - 1.4434i
-0.8721
If more digits are required in the print out, type format long, which will give 16 total
digits instead of the default of four digits beyond the decimal point.
3. Zeros of Functions
Many problems in science and engineering reduce to the solution of the single nonlinear
equation f(x) = 0, or to the set of n nonlinear equations f
i
(x
1
, x
2
, , x
n
) = 0 , i = 1, , n.
There are numerous methods that are designed to solve such problems. Considering the
case of a single equation, some of the methods include: graphical, bisection, false-
position, Newton-Raphson, and the secant method. They all have advantages and
disadvantages. This topic is important in numerical methods because within a relatively
simple context, many key concepts in numerical analysis are demonstrated.
Three important concepts are the initial guess, iterations and convergence. Given that the
6
goal is to determine the special value of x that makes f(x) = 0, all of the methods must
start with a guess of a value of x for which f(x) is not zero. This is the called the initial
guess, which will later be designated as x
0
. What distinguishes one method from
another is how the guess is modified to become an improved estimate. This process of
taking the old x and creating a new x is called an iteration. For the method to be
successful the new x should converge to the solution after a certain number of
iterations, i.e., the method has to find the answer within a reasonable amount of
(computer) time. The Newton-Raphson method will be provided as an example, since it
illustrates these three concepts so well, and also provides a review of the Taylor series.
Consider a Taylor series expansion of the function centered at the current estimate, x = x
i
.

n
i i
n
i i i i i
) x x )( x ( f
! n
) x x )( x ( f
!
) x x )( x ( f ) x ( f ) x ( f + + + + =
1
2
1
2
L (3.1)
Now do three things:
1. Truncate the Taylor series to a first order Taylor series (n=1).
2. Let the truncated Taylor series approximate the equation, f(x) = 0.
3. Since this is an approximation, the x in equation (3.1) is just an improved
guess, not the final answer. Therefore, replace x by x
i+1
.
This gives:
) x x )( x ( f ) x ( f ) x ( f
i i i i
+ =
+1
0 .
Solving for x
i+1
gives the formula for the iteration process,
) x ( f
) x ( f
x x
i
i
i i

=
+1
. (3.2)
The following plot from Chapra and Canale (Figure 6.5) illustrates the method.
Figure 6.5. Graphical depiction of the Newton-Raphson method.
A tangent to the function of x
i
[that is, f(x
i
)] is extrapolated down
to the x-axis to provide an estimate of the root at x
i+1
.
7
Formula (3.2) can easily be obtained from the Figure as follows:
1
0
+

= =
i i
i
i
x x
) x ( f
x
f
) x ( f slope .
Solving for x
i+1
gives the same result as equation (3.2). The iterative process involves the
following steps:
1. Make an initial guess, x = x
0
.
2. Determine an improved guess, x
1
using equation (3.2)
3. Now use x
1
as the old guess in equation (3.2) to obtain a better
approximation, x
2
.
4. Repeat the iterative process until either f(x
i+1
) = 0 to sufficient accuracy or the
value of x stops changing to within a specified tolerance.
A key disadvantage of the Newton-Raphson method is that the derivative of the function
is required. Most of the other methods do not require a derivative. Now to consider the
solution of f(x) = 0 using MATLAB.
The MATLAB fzero command can be used for the single unknown case. There are no
guarantees of success in such cases, especially with multiple equations. Usually, the
critical element is the value(s) of the initial guess(es). For the case of one equation (f(x)
= 0) and one unknown (x), the MATLAB fzero command has the following format:
fzero(f,x0), where x0 is the initial guess to be used in the numerical solver.
Parachute problem example from Chapra and Canale: Solve the equation
| |
( ) 0 40 1
1 68 8 9
1 68 10
= =
. / x
e
x
) . ( .
) x ( f .
Method 1. Without creating an m-file:
>> format long % Use extra digits so we see how good fzero really is!
>> fzero('9.8*68.1/x*(1-exp(-10*x/68.1))-40',12)
ans =
14.78020383166105
Method 2. Without creating an m-file, but with creating an inline function.
>> h=inline('9.8*68.1/x*(1-exp(-10*x/68.1))-40')
h =
Inline function:
h(x) = 9.8*68.1/x*(1-exp(-10*x/68.1))-40
>> fzero(h,12) % Solve for h = 0 using fzero.
ans =
14.78020383166105
>> h(ans) % Check fzero to see if h = 0 at this value of x.
ans =
7.105427357601002e-015
Method 3. Without creating an m-file, but with symbolic algebra.
>> syms x
>> g = 9.8*68.1/x*(1-exp(-10*x/68.1))-40;
>> fzero(g,12) %This will produce an error as shown below.
8
??? Error using ==> fzero (parse_call)
If FUN is a MATLAB object, it must have an feval method.
Error in ==> C:\MATLAB6R12.1\toolbox\matlab\funfun\fzero.m
On line 103 ==> [tol, trace, calltype, FunFcn, varargin] = ...
>> fzero(char(g),12) % The function g must be converted into a string using char
ans =
14.78020383166106
Method 4. With an m-file. From the command window in MATLAB click on file, then
new, then m-file to create the following m file and save it as, for example, parachute.m:
% Function for the parachute problem from Chapra and Canale, page 112
function y = parachute(x)
y = 9.8*68.1/x*(1-exp(-10*x/68.1))-40;
The following MATLAB code first determines the root, x using fzero for an initital guess
of 12, then evaluates f(x) at the numerical answer to see how close it is to zero. It is a
good idea to check the results when possible.
>> format long % Print extra digits to see how good fzero really is.
>> fzero(@parachute,12)
ans =
14.78020383166105
>> format short % Switch back to four decimal point output.
>> parachute(14.78020383166105) % This should show that fzero found the answer.
ans =
1.4211e-014
That is close enough to zero! So the root is 14.78 plus or minus the number of digits that
is appropriate for the application.
For the case of more than one unknown, a numerical method must be chosen and then
programmed. A good method to start with is by Newton-Raphson (see, for example, the
text be Chapra and Canale). The Maple command, fsolve is also very useful.
4. Matrix Algebra.
An array or matrix is a list of numbers or expressions arranged in horizontal rows and
vertical columns. When a matrix has only one row or column it is, respectively, a row or
column vector. Otherwise it is said to be a matrix of size mn, which has m rows and n
columns. Matrix operations are considered in this section. In MATLAB there are also
array operations which differ from matrix operations. For example, division of one
array by another array is defined, but division of a matrix by another matrix is not
defined. A table of array operations, copied from Palm, is given on the next page.
Defining a matrix. To create a matrix (or an array) in MATLAB type the matrix row by
row with the elements in the row separated either by a space or a comma, and the rows
9
separated by semicolons. The entire matrix is enclosed in square brackets. For example,
the (23) matrix A,
| |
(

=
6 5 4
3 2 1
A ,
is entered in MATLAB as follows:
>> A=[1,2,3;4,5,6] % or A=[1 2 3 ; 4 5 6]
A =
1 2 3
4 5 6
Accessing elements of a matrix.
The elements of a matrix can be accessed by the command, A(i,j), which refers to the
element in the i
th
row and j
th
column of [A], i.e., a
ij
. For the above example, A(1,3) = 3.
This is a very important command in basic programming.
Dimension of a matrix
The command size(A) returns the number of rows and columns of the matrix A. Typing
[ x, y ] = size(A) results in storing the value of the number of rows of A in x and the
number of columns of A in y.
Appending a row or column to an existing matrix.
A row can be appended to an existing matrix provided the row has same length as the
length of the rows of the existing matrix, and similarly for columns. The command
A=[ A U ] augments the column vector U to the columns of A, while A = [A;V] appends
row vector V to the rows of A.
Addition and subtraction of matrices
Addition and subtraction of matrix A and matrix B are done element by element, where
the dimensions of A and B must be the same. The commands are simply A+B and A-B
where A and B have already been defined as matrices. If a matrix is added to a scalar,
MATLAB invokes the array operation and adds the scalar to all elements of the matrix
(see Table 2.3-1).
10
Multiplication of matrices
Multiplication of two matrices can be performed only if the first matrix has as many
columns as the number of rows in the second matrix. The resulting matrix will have the
number of rows of the first matrix and the number of columns as the second matrix. This
can be illustrated for [C] = [A][B] as follows:
| | | | | |
l m m n l n
B A C

= .
This is interpreted as a matrix A with n rows and m columns is multiplied by a matrix B
with m rows and l columns, results in a matrix C with n rows and l columns. The
MATLAB command is simply A*B where A and B have already been defined as
matrices. For example, given the matrix A above,
>> size(A)
ans =
2 3
shows that A has three columns. Therefore, it is required that B have three rows.
Consider the following example from MATLAB:
>> B=[1,2,3,4;5,6,7,8;9,10,11,12]
B =
1 2 3 4
5 6 7 8
9 10 11 12
>> size(B)
ans =
3 4
>> C=A*B
C =
38 44 50 56
83 98 113 128
>> size(C)
ans =
2 4
The resulting matrix C=A*B has the number of rows of A and the number of columns of
B, i.e., C(24) = A(23)*B(34).
Transpose of a matrix
The Transpose of a matrix involves transforming its rows into columns and its columns
into rows. To obtain the transpose of matrix C the command in MATLAB is C'. Using
the 24 matrix C from the above example shows how the rows and columns are reversed:
>> C'
ans =
38 83
44 98
50 113
56 128
11
If the elements of C are complex numbers, the transposed matrix also consists of the
complex conjugates of the elements.
Square matrix
A square matrix is any matrix where the number of rows is equal to the number of
columns. For example, the matrix D below is a square matrix.
>> D=[1,2,3;-1,0,3;2,-1,1]
D =
1 2 3
-1 0 3
2 -1 1
The most important example of a square matrix is associated with a system of n linear
equations and n unknowns. For example, the matrix D above could be associated with
The three equation and three unknown system, [D]{x} = {b} where:
7 3 2
3 2 1
= + + x x x
4 3
3 1
= + x x
0 2
3 2 1
= + x x x
and {x} and {b} are the column vectors

=
0
4
7
3
2
1
} b { ,
x
x
x
} x { .
In MATLAB the {b} vector is defined as
>> b=[7;4;0]
b =
7
4
0
Inverse of a (square) matrix
If D is a square matrix, then the matrix D
-1
is its inverse if [D][D]
-1
= [D]
-1
[D] = [I],
where [I] is the identity matrix with ones along the diagonal and zeros elsewhere. The
MATLAB command to obtain the inverse of D is inv(D).
>> inv(D)
ans =
0.1500 -0.2500 0.3000
0.3500 -0.2500 -0.3000
0.0500 0.2500 0.1000
>> D*inv(D)
ans =
1.0000 0 -0.0000
0 1.0000 0.0000
0.0000 0 1.0000
12
Determinant of a matrix
The command to evaluate the determinant of a square matrix is det(D). For an inverse
matrix to be defined, the determinant must be nonzero. See Appendix A for more details
on a determinant of a matrix.
Rank of a matrix
It is first necessary to define a subdeterminant. A subdeterminant is the determinant
of a square matrix that is created from a larger matrix by canceling rows and columns.
As an example, a 33 matrix has nine different 22 subdeterminants. The rank of a
matrix A is defined as follows (see Palm): An mn matrix A has a rank r if and only if
the determinant of A contains a nonzero rr subdeterminant and every square
subdeterminant with r+1 or more rows is zero. Note that the rank is always greater
than or equal to one. Therefore, if the rank of an nn matrix is less than n, the
determinant of the matrix is zero. The MATLAB function to determine the rank of a
matrix A is simply rank(A). The rank is used to determine the character of a solution
to a system of equations, as explained at the end of Section 5.
5. Solution of a System of Linear Algebraic Equations.
The general form of a system of n linear algebraic equations and n unknowns is:
1 1 1 2 12 1 11
b x a x a x a x a
n n i i
= + + + + + L L
2 2 2 2 22 1 21
b x a x a x a x a
n n i i
= + + + + + L L
M M M
k n kn i ki k k
b x a x a x a x a = + + + + + L L
2 2 1 1
M M M
n n nn i ni n n
b x a x a x a x a = + + + + + L L
2 2 1 1
In matrix form this system can be expressed as [A]{x} = {b}, where
(
(
(
(
(
(
(
(

=
nn ni n n
kn ki k k
n i
n i
a a a
a a a a
a a a a
a a a a
] A [
L L
M M M M M M
L L
M M M M M M
L L
L L
2 1
2 1
2 2 22 21
1 1 12 11
,

=
n
i
x
x
x
x
} x {
M
M
2
1
,

=
n
k
b
b
b
b
} b {
M
M
2
1
The matrix [A] is the coefficient matrix where the a
ki
are constant coefficients, the
vector {x} contains the n unknowns, x
i
, and {b} is the right hand side vector, which
contains the known constants, b
k
. Note that the subscripts k and i range from 1 to n.
An alternative way to express the n equations is as follows:
n , , k for , b x a
k i
n
i
ki
K 1
1
= =

=
.
13
Assuming that a unique solution exists, there are different ways of solving these
equations in MATLAB. Three are given below.
Use of the inverse, {x} = [A]
-1
{b} which in MATLAB is given by inv(A)*b.
Use of the backslash command, which in MATLAB is given by A\b.
Use of Gaussian Elimination to reduce the augmented matrix, [A b] to [I x] as
follows: S = rref([A b]). The built-in function, rref stands for reduced row
echelon form, the nn matrix, [I], is the identity matrix, and the last column
of the n(n+1) matrix, S, is the solution. See Appendix B for more on Gauss
Elimination.
Each of these methods are applied to the following example, the solution of which is
x
1
= 0.05, x
2
= 1.45 and x
3
= 1.35:
7 3 2
3 2 1
= + + x x x
4 3
3 1
= + x x
0 2
3 2 1
= + x x x
>> A=[1,2,3;-1,0,3;2,-1,1];
>> b=[7;4;0];
>> % using the inverse method:
>> inv(A)*b
ans =
0.0500
1.4500
1.3500
>> %using the backslash command
>> A\b
ans =
0.0500
1.4500
1.3500
>> %using the rref command
>> rref([A b])
ans =
1.0000 0 0 0.0500
0 1.0000 0 1.4500
0 0 1.0000 1.3500
Cases when a unique solution does not exist use of the rank of a matrix.
The system of linear equations, [A]{x} = {b}, can have a unique solution, infinitely
many solutions or no solution. The following rank test can be used to determine
which type it is:
a) The system has a unique solution if rank(A) = rank([A b]) = n.
b) The system has infinitely many solutions if rank(A) = rank([A b]) < n.
c) The system has no solution if rank(A) rank([A b]).
For example, consider a system of three equations, where the third equation is a linear
combination of the first two equations (the first equation minus the second equation
14
gives the third equation).
7 3 2
3 2 1
= + + x x x
4 3
3 1
= + x x
3 2 2
2 1
= + x x
Writing this as [A]{x} = {b}, MATLAB can be used to determine that there are an infinite
number of solutions.
>> A=[1,2,3;-1,0,3;2,2,0];
>> b=[7;4;3];
>> rank(A)
ans =
2
>> rank([A b])
ans =
2
This corresponds to case b where rank(A) = rank([A b]) < n. Since rank(A) < 3, the
determinant should be zero:
>> det(A)
ans =
0
If the right hand side of the third equation is changed from 3 to, for example, 4, as
follows:
4 x 2 x 2
2 1
= + ,
then there will be no solution:
>> b2=[7;4;4];
>> rank([A b2])
ans =
3
In this case the rank test says there is no solution since rank(A) rank([A b2]), (2 3).
As a geometrical interpretation in three dimensions, each equation forms a plane. For
example, the equation
1 13 12 11
b z a y a x a = + +
can be compared to the equation of a plane,
( ) | | 0
0 0 0
= + + k ) z z ( j ) y y ( i x x n
r r r
r
,
that goes through the point (x
0
,y
0
,z
0
) and is perpendicular to the vector,
k n j n i n n
z y x
r r r
r
+ + = .
A geometrical interpretation of the three types of solutions follows:
a) Solution is unique. If the ranks of the coefficient matrix and the augmented matrix
are both 3, the three planes intersect at one point to give a unique solution. Two
planes intersect to form a line and the third plane cuts through the line at one point.
15
b) Infinite number of solutions. If the ranks of both the coefficient matrix and the
augmented matrix are 2, the three planes intersect along a line (infinite number of
solutions), and if both the ranks are one, the three equations produce the same plane.
c) No solution. When the rank of the augmented matrix is larger than the rank of the
coefficient matrix, there is no common point that lies on all three planes. The
equations correspond to parallel planes or parallel lines created by intersecting planes.
To solve for a system that has infinitely many solutions, the rref MATLAB command
can be used. Consider the example where the third equation is obtained by taking the
first and subtracting the second.
7 3 2
3 2 1
= + + x x x
4 3
3 1
= + x x
3 2 2
2 1
= + x x
First the solution is obtained algebraically. The second equation gives
4 3
3 1
= x x ,
while the first equation, given this result for x
1
, gives
( ) 7 3 2 4 3
3 2 3
= + + x x x
which leads to: 2 11 3
3 2
/ x x + =
Therefore any set of values that satisfies the following relationships is a solution:
4 3
3 1
+ = x x
2 / 11 x 3 x
3 2
+ =
3 3
x x = .
Gauss elimination using MATLAB gives the same result:
>> A=[1,2,3;-1,0,3;2,2,0];
>> b=[7;4;3];
>> rref([A b])
ans =
1.0000 0 -3.0000 -4.0000
0 1.0000 3.0000 5.5000
0 0 0 0
This tells us that x
3
can be anything, while x
1
=-4 + 3x
3
and x
2
= 5.5 3x
3
.
6. Curve Fitting (Regression analysis)
Curve fitting is a technique of obtaining a functional relationship, y = f(x), that best
fits a set of M data points, (x
j
, y
j
), j = 1,,M. The basic problem is that of regression,
where the x values are inputs (independent variable) and the y values are measured as
data (dependent variable). It is assumed that the x values are correct, and it is
understood that there is error associated with the measurement made for y. The curve
fit approximates the data, it does not go through all of the data points. (In the next
section interpolation will be covered, which obtains a curve that does go through all
the points.)
The standard curve fitting approach is the method of least squares. This method
involves the following steps, which in MATLAB is referred to as linear-in-the-
parameters regression:
16
Step 1. Choose a functional form in terms of L unknown coefficents, c
i
, i = 1,2,,L, that
is used to represent the M data points, (x
j
,y
j
) for j = 1,,M.
) x ( f c ) x ( f
i
L
i
i
=
=
1
.
The problem is to determine the L c
i
coefficients to obtain a best fit.
Step 2. Introduce the sum of the squares of the error between each data point and the
curve fit, that is used to quantify the quality of the fit:
| |
2
1

=
=
M
j
j j r
) x ( f y S , where ) x ( f c ) x ( f
j i
L
i
i j
=
=
1
Step 3. Obtain L equations by minimizing S
r
with respect to the L c
i
coefficients. This is
assumed to be the best fit of the data given the chosen form of f(x):
L ,..., k ,
c
S
k
r
1 0 = =

.
Step 4. Complete the associated algebra to obtain the linear system of L equations and L
unknowns,
) x ( f y ) x ( f ) x ( f c
j k
M
j
j j k j
M
j
i
L
i
i
= = =
=
1 1 1
, k=1,,L,
which is simply [A]{c} = {b}, where
) x ( f y b and ) x ( f ) x ( f a
j k
M
j
j k j k j
M
j
i ki
= =
= =
1 1
.
The most common choice of a fitting function in the least squares, linear regression
approach is a polynomial. In this case,
, x a x a a x a ) x ( f c ) x ( f
n
n
p
n
p
p i
L
i
i
+ + + = = =

= =
L
1 0
0 1
where L = n + 1 and f
i
(x) = x
i-1
. For this case MATLAB has the built-in function, a =
polyfit(xj,yj,n), where xj and yj are the data vectors of dimension M, n is the order of
the polynomial, and a is the vector of n+1 coefficients as defined in the above
equation. Consider the following MATLAB example (from the MATLAB Help
library) using the six data points (M = 6),
j 1 2 3 4 5 6
x
j
0 0.3 0.8 1.1 1.6 2.3
y
j
0.5 0.82 1.14 1.25 1.35 1.40
% The M= 6 data points are in the arrays xj and yj.
xj=[0,0.3,0.8,1.1,1.6,2.3];
yj=[0.5,0.82,1.14,1.25,1.35,1.4];
% The x vector will be used to plot the curve fits for comparison with the data.
x=[0:0.05:2.3];
% Now obtain the coefficients for a straight line (n = 1 which is the same as L = 2)
a1=polyfit(xj,yj,1);
% y1 is used for plotting the curve fits.
% polyval(a,x) evaluates the polynomial with coefficients defined by a at the points in x.
17
y1=polyval(a1,x);
% now repeat for n = 2, i.e., a quadratic fit of the data.
a2=polyfit(xj,yj,2);
y2=polyval(a2,x);
% now plot the data and the two curve fits.
plot(xj,yj,o,x,y1,x,y2)
legend(data,first order,second order)
title(Data points and linear and quadratic curve fits for 0 < x < 2.3)
xlabel(x)
ylabel(y)
In order to use other fitting functions, MATLAB can solve the least squares problem
described in Steps 1-4 by using the backslash operator, which solves a system of M
equations in L unknowns in the least squares sense when M > L. In other words, the
backslash operator takes care of Steps 2-4 automatically, i.e., the backslash operator is
a built-in function for a least squares linear regression analysis. All that is necessary is to
define the equations. For example, consider the case of f(x) = a
1
+ a
2
e
-x
+ a
3
xe
-x
, where
a
1
, a
2
and a
3
are to be determined in the least squares sense using the six data points. For
each data point there is an equation, which leads to a system of six equations in three
unknowns. Consider this as [D]{a} = {y} where, in MATLAB, a = D\y will give the
solution. All that is required is construction of the 63 D matrix, given that {y} is the
data in a column vector. In the notation of the derivation on the previous page, d
ji
= f
i
(x
j
),
where j = 1,,M and i = 1,,L. This example is given below:
>> xj=[0;0.3;0.8;1.1;1.6;2.3]; % these are the M=6 points at which data is known.
>> yj=[0.5;0.82;1.14;1.25;1.35;1.40]; % these are the corresponding data values.
>> D=[ones(size(xj)) exp(-xj) xj.*exp(-xj)]; % this is the M=6 by L=3 D matrix.
>> a=D\yj % this will give the answer (performs steps 2-4 of derivation)
a =
1.3974
-0.8988
18
0.4097
>> x=[0:0.05:2.3];
>> yp=a(1)+a(2)*exp(-xp)+a(3)*xp.*exp(-xp);
>> plot(xj,yj,'o',x,yp)
>> legend('data','y(x)=a1+a2*exp(-x)+a3*x*exp(-x)')
>> title('Data points and non-polynomial fit')
>> xlabel('x')
>> ylabel('y')
MATLAB 6 includes Basic Fitting in its figures windows tool (see Palm) that
simplifies the process of curve fitting and data analysis. As with all the topics in this
handout, there are many more built-in functions available for curve fitting and data
analysis.
7. Interpolation.
Interpolation is different from curve fitting in that an interpolated curve passes
through all the data points. Therefore, interpolation should be used when the data is
known to be accurate. Interpolation, like curve fitting, is used to predict values
between data points, not outside of the range of data, which is extrapolation.
There are numerous interpolation formulae. In basic interpolation, since the curve is
forced to go through the data, the unknown parameters in the curve must be equal to
the number of data points used. For example, in linear interpolation, a straight line
has two parameters (a and b in ax + b) so two data points are required. In cubic
interpolation four data points are required (a, b, c, and d in ax
3
+ bx
2
+ cx +d). There
are also interpolation methods that make use of derivative conditions at the data
points, such as cubic splines. Cubic splines will be discussed after presenting one of
the basic interpolation methods.
The Lagrange 2, 3 and 4-point formulas will be presented using the notation in the
following table:
19
i -1 0 1 2
x
i
x
-1
x
0
x
1
x
2
f(x) = f(x
i
) = f
i
f
-1
f
0
f
1
f
2
The data must be equally spaced, where the spacing is h, i.e., x
i
+h = x
i+1
for all i.
The formulas correspond to linear, quadratic and cubic polynomials that are forced to
go through 2, 3 or 4 data points, respectively. For example, forcing the line f(x) = ax
+ b to go through the two points (x
0
,f
0
) and (x
1
,f
1
) leads to:
b ax f + =
0 0
b ax f + =
1 1
,
which gives
1 0
0 1 1 0
1 0
1 0
x x
f x f x
b ,
x x
f f
a

= , so
1 0
0 1 1 0
1 0
1 0
x x
f x f x
x
x x
f f
) x ( f

= .
Substituting x = x
0
+ ph and using h = x
1
x
0
, gives the Lagrange 2-point formula.
2-point:
1 0 0
1 pf f ) p ( ) ph x ( f + = + , valid for x
0
x x
1
or 0 p 1.
Note how this formula can be obtained directly using this choice of p, i.e., the curve is
forced to go through the two points (p = 0,f
0
) and (p = 1,f
1
). The 3-point and 4-point
formulas can be constructed in a similar manner. They are
3-point:
1 0
2
1 0
2
1
1
2
1
f
) p ( p
f ) p ( f
) p ( p
) ph x ( f
+
+ +

= +

, valid
for x
-1
x x
1
or -1 p 1,
4-point:
0
2
1 0
2
2 1
6
2 1
f
) p )( p (
f
) p )( p ( p
) ph x ( f

+

= +

2
2
1
6
1
2
2 1
f
) p ( p
f
) p )( p ( p
+
+
, valid for x
-1
x x
2
or -1 p 2.
The relationship between x and p is, x = x
0
+ph or p = (x x
0
)/h. The independent
variable p is used to obtain more compact expressions. In all cases the curve goes
through the data used to establish the formula, i.e., when p = -1, 0, 1, or 2, f(x
p
) = f
p
.
For example, for the 4-point case, it is observed that when p = 2, f(x
0
+ 2h) = f(x
2
) =
f
2
. In order to use these expressions, it is necessary to first choose the point
corresponding to x
0
, then determine the value of p that corresponds to the point of
interest using x = x
0
+ ph. Example: Use 4-point interpolation to approximate
sin(0.25) = 0.247404 given the following table of data where h = 0.1.
x
i
0.1 0.2 0.3 0.4 0.5
f
i
0.099833 0.198669 0.295520 0.389418 0.479426
Choose x
0
= 0.2 so that x = 0.25 is in the center of the data. In this case p = 0.5 based
on x = 0.25 = 0.2 + ph, where h = 0.1. Then
198669 0
2
2 5 0 1 5 0
099833 0
6
2 5 0 1 5 0 5 0
25 0
2
.
) . )( . (
.
) . )( . ( .
) . ( f

+

=
247403 0 389418 0
6
1 5 0 5 0
295520 0
2
2 5 0 1 5 0 5 0
2
. .
) . ( .
.
) . )( . ( .
=

+
+
.
20
Values of x
0
= 0.1 or 0.3 could also be used with the 4-point formula. In these cases p
= 1.5 and 0.5, respectively.
Now consider some basic MATLAB functions for interpolation. Linear interpolation
can be accomplished using interp1(xj,yj,x), where the data vector is stored in the set of
points, (xj,yj), and x corresponds to the value or values at which the linear
approximation is to be made. For example, consider the case above where two point
interpolation for sin(0.25) gives:
247095 0 295520 0 5 0 198669 0 5 0 1 25 0 . . * . . ) . ( ) . ( f = + =
In MATLAB using the interp1 function:
>> format long
>> xj=[0.1,0.2,0.3,0.4,0.5];
>> yj=[sin(0.1),sin(0.2),sin(0.3),sin(0.4),sin(0.5)];
>> interp1(xj,yj,0.25)
ans =
0.24709476872820
Note that this MATLAB function is programmed to use the two surrounding points x
0
= 0.2 and x
1
= 0.3 to make the best estimate.
The most common form of higher order interpolation is cubic spline interpolation,
which can be implemented using the MATLAB command, interp1(xj,yj,x,spline) or
spline(xj,yj,x), (see Chapra and Canale, Section 18.6). This method strings together a
series of cubic polynomials, where each polynomial links two neighboring data points
by 1) going through the two end points, 2) matching slopes with adjacent splines at the
two end points, and 3) matching second derivatives (curvature) at the endpoints. This
gives a very accurate estimate of the value of the function between data points as
shown for the sin(0.25) example.
>> interp1(xj,yj,0.25,'spline')
ans =
0.24740384086582
This value is correct to six digits (0.247404). Cubic splines can also be used to make
a very smooth plot of a set of data as follows:
>> xj=[0:0.5:3.5];
>> yj=sin(xj);
>> x=[0:0.1:3.5]; y=spline(xj,yj,x);
>> plot(xj,yj,'o',x,y)
21
The MATLAB command interp2(xj,yj,zj,x,y) is for linear interpolation of the two
dimensional function, z = f(x,y).
8. Numerical Integration
Consider the evaluation of the integral,

b
a
dx ) x ( f ,
where the integral corresponds to the area under the curve of the plot of f vs. x.
Numerical integration is required when the function f(x) is too complicated to be
integrated analytically or f(x) is only known at the discrete points, x
i
, i = 0,1,2,,n. Both
cases occur frequently in engineering analysis. We first consider methods that follow
from the previous section on interpolation. A uniform spacing of h is assumed between
points, giving a total of n equally spaced intervals, and n+1 pairs of x
i
and f
i
. The
following figure from Chapra and Canale (Figure 21.8) gives a representation for the so-
called Newton-Cotes closed integration formulas.
22
Trapezoid Rule:
Step 1. Assume linear interpolation between neighboring points given by the Lagrange
2-point formula. Use the first two points in the interval.
1 0 0
1 pf f ) p ( ) ph x ( f ) x ( f + = + = .
Step 2. Determine the area under the curve corresponding to the portion, x
0
x x
1
.
( ) | | | |
1 0
1
0
1 0
1
0
0
2
1
1
0
f f
h
dp p f p h dp ) ph x ( f h dx ) x ( f
x
x
+ = + = + =

.
In the integral above the substitution, x = x
0
+ ph is made to change from integration in x
to integration in p. Note that the resulting formula is just the area of a trapezoid, which is
the width, h, times the average height, (f
0
+ f
1
)/2.
Step 3. Combine all the trapezoids that make up the total area from a to b.
| | | | | | | | = + + + + + + + + +
+

n n i i
b
a
f f
h
f f
h
f f
h
f f
h
dx ) x ( f
1 1 2 1 1 0
2 2 2 2
L L
=
(

+ +

=
1
1
0
2
2
n
i
n i
f f f
h
. (8.1)
In MATLAB, the trapz(x,f) function will perform this integration.
Simpsons 1/3 Rule.
Step 1. Assume quadratic interpolation between every set of three points given by the
Lagrange 3-point formula. Use the first three points in the interval to give,
2 1
2
0 1
2
1
1
2
1
f
) p ( p
f ) p ( f
) p ( p
) ph x ( f ) x ( f
+
+ +

= + = .
Step 2. Determine the area under the curve corresponding to the portion, x
0
x x
2
.
dp f
) p ( p
f ) p ( f
) p ( p
h dp ) ph x ( f h dx ) x ( f
x
x


(

+
+ +

= + =
1
1
2 1
2
0
1
1
1
2
1
1
2
1
2
0
| |
2 1 0
4
3
f f f
h
+ + = .
As for the trapezoid rule, this result is the width, h, times the average height.
Step 3. Combine all such areas to make up the total area from a to b. Note that n should
be divisible by two, i.e., n should be an even number.
| | | | | | = + + + + + + + + +

n n n
b
a
f f f
h
f f f
h
f f f
h
dx ) x ( f
1 2 4 3 2 2 1 0
4
3
4
3
4
3
L
=
(

+ + +

=
1
5 3 1
2
6 4 2
0
2 4
3
n
, , i
n
, , , i
n i i
f f f f
h
. (8.2)
In a similar fashion additional formulas can be obtained that correspond to higher order
polynomials. For example, using cubic interpolation with the 4-point Lagrange formula,
Simpsons 3/8 rule can be obtained. The formula for the area in Step 2 is
| |
3 2 1 0
x
x
f f 3 f 3 f
8
h 3
dx ) x ( f
3
0
+ + + =

,
23
which leads to the sum for the entire integral from a to b as
(

+ + + +

=
2 n
, 7 , 4 , 1 i
3 n
, 9 , 6 , 3 i
n i
1 n
, 8 , 5 , 2 i
i i 0
b
a
f f 2 f 3 f 3 f
8
h 3
dx ) x ( f . (8.3)
In this case n should be divisible by 3. To integrate cases where there are, say 7 intervals,
formula (8.3) can be combined with (8.2).
MATLAB does not have built-in functions for (8.2) and (8.3). Therefore, in the
following example some programming is required. In the example sin(x) will be
integrated from 0 to , which has a closed form value of 2. The three integration
approaches given by equations (8.1), (8.2) and (8.3) are used to complete the following
Table.
n Trapezoid
Rule
Simpsons
1/3 Rule
Simpsons
3/8 Rule
6 1.9540972 2.0008632 2.0020098
12 1.9885638 2.0000526 2.0001194
24 1.9971434 2.0000033 2.0000074
48 1.9992860 2.0000002 2.0000005
Table 8.1. Numerically determined values of the integral of sin(x)
evaluated from x = 0 to x = . The exact answer is 2.
Here is the program that gives results for all three integration approaches:
% Set up the data file for a given value of n.
n=6 % This value must be divisible by 6.
x=linspace(0,pi,n+1); % Creates n equal intervals with n+1 points including 0 and pi.
f=sin(x);
h=x(2)-x(1); % This gives the spacing.
% Trapazoid rule
sum=f(1)+f(n+1);
for i=2:1:n
sum=sum+2*f(i);
end
Trap=h/2*sum
% we could also use the MATLAB trapz(x,f) function
% Simpsons 1/3 rule
sum1=0;
for i=2:2:n
sum1=sum1+4*f(i);
end
sum2=0;
for i=3:2:n-1
sum2=sum2+2*f(i);
end
Simp13=h/3*(f(1)+sum1+sum2+f(n+1))
24
% Simpsons 3/8 rule
sum1=0;
for i=2:3:n-1
sum1=sum1+3*f(i);
end
sum2=0;
for i=3:3:n
sum2=sum2+3*f(i);
end
sum3=0;
for i=4:3:n-2
sum3=sum3+2*f(i);
end
Simp38=3*h/8*(f(1)+sum1+sum2+sum3+f(n+1))
n =
6
Trap =
1.95409723331371
Simp13 =
2.00086318967354
Simp38 =
2.00200984662856
Note how the accuracy of the 1/3 and 3/8 rules is better than that of the trapezoid rule. It
can be shown that the error of the trapezoid rule is on the order h
2
, while those of the 1/3
and 3/8 rules are h
4
. This means that if h is cut in half, the error of the trapezoid rule
should reduce by a factor of four, while the others should reduce by a factor of 16. For
example, as n increases from 12 to 24, h is reduced by a factor of two and the relative
percent errors for the three methods reduce by, respectively, factors of 4.003, 15.94 and
16.14. In general the 3/8 rule is slightly better than the 1/3 rule, but not in this case for
these values of n. The bottom line is that Simpsons 1/3 rule is very good.
In cases when the function is known, such as this example where f(x) = sin(x), the
MATLAB quad function can be used to use Simpsons rule in an adaptive way. This
means that the built-in function automatically decreases the interval until an error
condition is met. While this is good for accuracy and casual use, it is not efficient for
large scale computations. The MATLAB command is quad(f,a,b,tol), where f, a and b
have been defined, and tol is the absolute error. The default in quad(f,a,b) is 10
-6
. For
the sine function example in Table 8.1,
>> quadl('sin(x)',0,pi,10^(-3))
ans =
1.99999997747113
25
>> quad('sin(x)',0,pi)
ans =
1.99999999639843
>> quad('sin(x)',0,pi,10^(-9))
ans =
1.99999999999913
We now consider methods where the function must be available for evaluation at any
point. For example, the function can be written in a MATLAB m-file. For such cases
there are several methods to evaluate integrals. One of the important approaches is Gauss
quadrature. In this approach the integral is first rewritten as

+

=
1
1
2
dt ) t ( f
a b
dx ) x ( f
b
a
,
by using the substitution,
2 2
a b
t
a b
x
+
+

= ,
to convert the integral from a to b to 1 to +1. The function ) t ( f is given by
) x ( f
a b
t
a b
f ) t ( f = |
.
|

\
| +
+

=
2 2
.
The integral is now in a standard form, to be approximated as follows:
) t ( f w dt ) t ( f
i
N
i
i

=
+


1
1
1
,
For a given choice of N, the method provides the special weights, w
i
and Gauss
Points, t
i
that are needed to perform the sum. The weights and Gauss points are chosen
to make the integral exact for a polynomial of degree 2N-1. For example, the 2-point
Gauss formula is exact for a cubic equation, and is given by:
|
|
.
|

\
|
+
|
|
.
|

\
|
=

=
+

3
1
3
1
2
1
1
1
f f ) t ( f w dt ) t ( f
i
i
i
,
where w
1
= w
2
=1 and t
1
= -t
2
= -1/3. Consider an example to show that it is exact for a
cubic polynomial.
| |
4
7983
12
2
6
3
2
4
3
12 6 2 3
7
2
2 3 4
7
2
2 3
= +

+ = + +
=
=

x
x
x x x x dx x x x (from Maple)
Using the Gauss 2-point formula,
| | | | ) / ( f ) / ( f dt ) t ( f
) (
dx x x x 3 1 3 1
2
9
2
2 7
12 6 2 3
1
1
7
2
2 3
+ =

= + +


,
where ) x ( f ) t ( f = and
2
5
3 2
9
2
2 7
3
1
2
2 7
2 2
1 1
+ =
+
+

=
|
.
|

\
| +
+

=
) ( ) ( a b
t
a b
x
26
2
5
3 2
9
2
2 7
3
1
2
2 7
2 2
2 2
+ =
+
+

=
|
.
|

\
| +
+

=
) ( ) ( a b
t
a b
x .
Therefore, once again using Maple for calculations, the answer is the same as above.
| |
4
7983
2
5
3 2
9
2
5
3 2
9
2
9
12 6 2 3
7
2
2 3
=
(

|
|
.
|

\
|
+ +
|
|
.
|

\
|
+ = + +

f f dx x x x .
The Gauss approach is very easy to program if the weights and Gauss points are
available, as in the example below:
| |

+
4
1
5 11
1 4 7 2 3 dx ) x )( x ( x x x .
This integral is of the form,
) r ( g w dx r ) r ( g
i
n
i
i

=
+


1
1
1
2
1
where the Gauss points and weights are given by
(

+ +
=
(

+
=
1 1 1
2
n
i
sin
n
w ,
n
i
cos r
i i

.
The integration is exact when g(x) is a polynomial of order 2n 1 or lower. The point
of this example is to show that there are many special integration formulas for certain
types of integrals.
Solution: First change the integral from x = a to b into r = 1 to +1.
| |

+
4
1
5 11
1 4 7 2 3 dx ) x )( x ( x x x
Now let
2 2
a b
r
a b
x
+
+

= where a = 1 and b = 4. This gives:


| |



|
.
|

\
|
= +
1
1
2
4
1
2
5 11
1
2
1 4 7 2 3 dr r ) x ( g
a b
dx ) x )( x ( x x x
Now program it.
fprintf( n Gauss Quadrature \n) % Table heading.
fprintf( ==========================\n)
a=1; b=4; n=0;
for icase = 1:6
n = n + 2;
% Obtain the r and w values for this n.
for i=1:n
r(i) = cos(pi*i/(n+1)); w(i) = pi/(n+1)*(sin(i*pi/(n+1)))^2;
end
x = (b-a)/2*r + (b+a)/2; % Now switch r to x.
g = 3*x.^11 2*x.^5 + x - 7; % These are the function values at the Gauss points.
% Here is Gauss Quadrature:
sum = 0;
for i=1:n
sum=sum + w(i)*g(i);
27
end
sum=sum*((b-a)/2)^2;
fprintf( %4.0f %20.16e\n,n,sum)
end
MATLAB output:
n Gauss Quadrature
========================
2 2.2663650885922303e+006
4 3.3159920282531464e+006
6 3.3188600273476094e+006 (Note that it is exact for n = 6 since 2*6 1 = 11)
8 3.3188600273476094e+006
10 3.3188600273476080e+006
12 3.3188600273476094e+006
Results from other integration approaches are given below:
n Trapezoid rule Simpson 1/3 rule
================================
6 2.0519520e+006 2.4827795e+006
12 2.8082234e+006 3.0603139e+006
24 3.1280802e+006 3.2346991e+006
48 3.2497038e+006 3.2902450e+006
96 3.2941187e+006 3.3089236e+006
192 3.3100622e+006 3.3153767e+006
384 3.3157407e+006 3.3176335e+006
768 3.3177556e+006 3.3184273e+006
1536 3.3184693e+006 3.3187072e+006
3072 3.3187218e+006 3.3188060e+006
The MATLAB quad function works well for this problem, but is slow. In MATLAB the
quadl function performs Lobatto Quadrature, which is related to Gauss Quadrature. The
main difference is that in Lobatto quadrature the end points of the integral, i.e., +1 and
1, are used. As for the quad function, quadl performs the integration adaptively in
MATLAB so an error can be specified and the program keeps refining until the error
condition is met. Using this approach for the previous example gives:
clear
format long
soln1 = quad(@fhw9p2,1,4,10^(-03))
soln2 = quad(@fhw9p2,1,4,10^(-06))
soln3 = quad(@fhw9p2,1,4,10^(-09))
MATLAB output:
soln1 = 3.318860026509846e+006
soln2 = 3.318860027346433e+006
soln3 = 3.318860027347609e+006 (Try this it takes a few seconds to calculate)
28
9. Differentiation.
Numerical differentiation is important because it is required to solve differential
equations that cannot be treated analytically. While MATLAB has many built-in
functions to solve differential equations, it does not have many functions for
calculating derivatives. Nevertheless, it is an important operation that is often
required. There are two cases to consider. First when the data is not accurate and
second when the data is accurate.
When the data, y
j
= f(x
j
) for j = 1,,M, is not accurate, an accurate estimate of the
slope, dy/dx, can be very difficult to obtain. One approach is to obtain a curve fit of
the data as described in Section 6, and then take the derivative. For example, if it is
known or expected that a certain functional form should be able to express the data,
then the slope of the curve obtained using least squares should give a reasonable result
for the derivative. However, inaccuracy in the data will be amplified in calculating
the slope, so one has to be very careful. Integration of such data is less sensitive, i.e.,
integration is a smoothing operation, while differentiation does the opposite.
By far the more important application of differentiation is when the data is accurate,
such as in the numerical solution of a differential equation. Consider the case where
the data, (x
i
, y
i
= f(x
i
)), corresponds to the uniform spacing, x
i+1
x
i
= h for all i. The
goal is to approximate the derivatives of a function at some point x
i
, for example the
first derivative, dy/dx =f(x
i
), in terms of values of the function at and near the point x
i
.
These approximations are made using the Taylor series,
) h ( O h ) x ( f
! n
h ) x ( f
!
h ) x ( f ) x ( f ) x ( f
n n
i
n
i i i i
1 2
1
1
2
1
+
+
+ + + + + = L . (8.1)
First the above expression is solved for f(x
i
),
) h ( O h ) x ( f
!
h ) x ( f
! h
) x ( f ) x ( f
) x ( f
i i
i i
i
3 2 3 1
3
1
2
1
+

=
+
. (8.2)
The more terms included in the approximation, the more accurate it is. Starting with a
first order approximation where terms of order h and higher are ignored gives:
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
+

=
+1
. (Forward difference)
This is the first order, forward difference formula since it involves values of the
function at and forward of the point x
i
. To create the backward difference formula,
use negative h in the Taylor series (8.1) to obtain
) h ( O h ) x ( f
!
h ) x ( f
!
h ) x ( f ) x ( f ) x ( f
i i i i i
4 3 3 2
1
3
1
2
1
+ + =

, (8.3)
which, solved for the first derivative, gives
) h ( O h ) x ( f
!
h ) x ( f
! h
) x ( f ) x ( f
) x ( f
i i
i i
i
3 2 3 1
3
1
2
1
+ +

=

. (8.4)
Ignoring terms of order h and higher gives the backward difference formula to first
order as
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
+

=
1
. (Backward difference)
29
The central difference formula, which involves information from points less than and
greater than the point under consideration, is obtained by solving for the first
derivative after subtracting (8.3) from (8.1) to obtain
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
2 1 1
2
+

=
+
. (Central difference)
Note how the order h term corresponding to the second derivative cancels, giving a
more accurate result than either the forward or backward difference formulas. This
result is simply the average of the forward and backward difference formulas.
In a similar manner, higher order formulas and higher order derivatives can be
determined. Some of these formulas are listed below. More formulas can be found in
numerical methods books such as Chapra and Canale. Note that while the central
difference formulas are most accurate, they cannot be applied at the two ends. At the
left end forward difference formulas apply, while at the right end, backward difference
expressions must be used.
Forward Differences:
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
+

=
+1
) h ( O
h
) x ( f ) x ( f ) x ( f
) x ( f
i i i
i
2 1 2
2
3 4
+
+
=
+ +
) h ( O
h
) x ( f ) x ( f ) x ( f
) x ( f
i i i
i
+
+
=
+ +
2
1 2
2
) h ( O
h
) x ( f ) x ( f ) x ( f ) x ( f
) x ( f
i i i i
i
2
2
1 2 3
2 5 4
+
+ +
=
+ + +
Backward Differences:
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
+

=
1
) h ( O
h
) x ( f ) x ( f ) x ( f
) x ( f
i i i
i
2 2 1
2
4 3
+
+
=

) h ( O
h
) x ( f ) x ( f ) x ( f
) x ( f
i i i
i
+
+
=

2
2 1
2
) h ( O
h
) x ( f ) x ( f ) x ( f ) x ( f
) x ( f
i i i i
i
2
2
3 2 1
4 5 2
+
+
=

Central Differences:
) h ( O
h
) x ( f ) x ( f
) x ( f
i i
i
2 1 1
2
+

=
+
) h ( O
h
) x ( f ) x ( f ) x ( f ) x ( f
) x ( f
i i i i
i
4 2 1 1 2
12
8 8
+
+ +
=
+ +
) h ( O
h
) x ( f ) x ( f ) x ( f
) x ( f
i i i
i
2
2
1 1
2
+
+
=
+
) h ( O
h
) x ( f ) x ( f ) x ( f ) x ( f ) x ( f
) x ( f
i i i i i
i
4 2 1 1 2
12
16 30 16
+
+ +
=
+ +
30
Consider the following numerical example, which is obtained from y = f(x) = sin(x).
x
i
0.0 0.1 0.2 0.3 0.4 0.5 0.6
f
i
0.0 0.099833 0.198669 0.295520 0.389418 0.479426 0.564642
All of the difference formulas are used to obtain approximations to the first and
second derivatives of sin(x) at x = 0.3. These values are entered in Table 9.1.
n
O(h
n
)
Order of
derivative
Forward
Difference
Backward
Difference
Central
Difference
Exact
1 1
st
0.938980 0.968510
2 1
st
0.958430 0.958585 0.953745
4 1
st
0.955333
1
st
0.955336
1 2
nd
-0.389000 -0.198500
2 2
nd
-0.298800 -0.297300 -0.295300
4 2
nd
-0.295558
2
nd
-0.295520
Table 9.1 Derivatives of sin(x) at x = 0.3.
The MATLAB diff command can be used to obtain the first order forward and
backward difference formulas, as well as the second order central difference formula.
This command computes differences in the elements of a vector as follows:
x=[x1,x2,x3,,xn];
diff(x) = [x2-x1,x3-x2,,xn-(xn-1)]
Note that diff(x) has n-1 elements when x has n elements. Here is a MATLAB
program that uses the diff command to compute some derivatives for the data above:
>> x=[0,0.1,0.2,0.3,0.4,0.5,0.6];
f=[0.0,0.099833,0.198669,0.295520,0.389418,0.479426,0.564642];
% Forward difference, first order, first derivative at x = 0.0,0.1,,0.5
fdfofd=diff(f)./diff(x);
% Backward difference, first order, first derivative at x = 0.1,0.2,,0.6
bdfofd=diff(f)./diff(x);
% Central difference, second order, first derivative at x = 0.1,0.2,,0.5
[m,n]=size(x);
h=x(2)-x(1);
cdsofd=(f(3:n)-f(1:n-2))/(2*h);
% To compare with the above Table:
format long
fdfofd(4)
bdfofd(3)
cdsofd(3)
ans =
0.93898000000000
ans =
0.96851000000000
ans =
0.95374500000000
31
10. Numerical solution of ordinary differential equations
10.1 Some Definitions:
Ordinary differential equation (ODE) an equation with a function as an unknown
that involves the function and derivatives of the function with respect to one variable.
As an example from EM 201, consider the case of a belt wrapped around a cylindrical
fixed drum with static coefficient of friction,
s
, for impending slip between the belt
and the drum. The tension of the belt T as a function of position is given by
0 = T
d
dT
s

.
Partial differential equation (PDE) Same as above except that the function depends
on more than one variable and there are partial derivatives with respect to these
variables. For example, from ME 304 T = T(x,y) is the unknown temperature in the
two-dimensional, steady state heat conduction equation:
0
2
2
2
2
2
=

=
y
T
x
T
T
Order of a differential equation The highest derivative in the equation corresponds to
the order of a differential equation. For example, the following differential equation
from EM 304 for the deflection, v, of a beam with loading, w=w(x), and uniform
bending stiffness, EI, is fourth order:
EI
) x ( w
dx
v d
=
4
4
.
Constant coefficient ODE A Differential equation in which the coefficients of the
terms in the equation, that multiply the unknown function or its derivatives, are
constants. For example:
) x cos( x y
dx
dy
dx
y d
3 7
3
2
2
+ = + .
Variable coefficient ODE case where at least one coefficient is not constant, such as
7 = + xf
dx
df
.
Homogeneous differential equation A differential equation where all terms involve
the unknown function, i.e., there is no lone term that is a function of only the
independent variable. The previous three examples are nonhomogeneous. For
example, the following equation is a non-constant coefficient, homogeneous, ordinary
differential equation:
0 7
2
2
= + y
dx
dy
x
dx
y d
.
Linear and nonlinear differential equations The differential equation is linear if the
solution of the homogeneous part of the differential equation does not change if the
unknown is replaced by any non-zero constant multiplied by the unknown. All of the
examples used in the previous definitions are linear. Examples of nonlinear
differential equations:
32
0
2
2
= + ) y cos(
dx
y d
, 7 xy
dx
dy
2
=
|
.
|

\
|
, ) x cos( y
dx
dy
y
dx
y d
= + + 7
2
2
.
Homogeneous and particular solutions When a differential equation is linear the
superposition principle can be used, meaning that the sum of any two solutions must
also be a solution. In order to obtain the complete solution to a given
nonhomogeneous, differential equation, (and not just a part of it), it is convenient to
consider two different solutions. The homogeneous solution corresponds to only
the homogeneous part of the equation, while the remainder is referred to as the
particular solution. For example:
x y
dx
y d
8 4
2
2
= +
has the general solution
x ) x cos( B ) x sin( A ) x ( y 2 2 2 + + = ,
where A and B are arbitrary constants. The homogenous solution corresponds to the
sine and cosine terms, while the particular solution is 2x.
General solution The complete solution to a differential equation before any
additional constraints, boundary conditions or initial conditions are applied to the
equation. A general solution to an n
th
order differential equation will have n arbitrary
constants. In the above example the order of the equation is 2 and there are two
constants, A and B.
It is time to address boundary conditions and initial conditions. These conditions
convert a general solution into the solution that applies to a given physical problem.
It will then be time to discuss numerical solutions to differential equations, where
boundary and initial conditions are incorporated into the differential equation in order
to obtain a solution.
10.2 Boundary Conditions and Initial Conditions.
Once a general solution to an n
th
order differential equation is obtained (analytically),
it is necessary to determine the n arbitrary constants. Given that there are n
unknowns, n conditions are required that can be converted into n equations. These
conditions must involve the unknown function and/or its derivatives up to n-1. Given
the example above:
x ) x cos( B ) x sin( A ) x ( y 2 2 2 + + = ,
consider the different ways that these conditions can be specified:
Initial conditions (all conditions are specified at one value of x, say at x = 0)
5 0 = ) ( y
0 0
0
= =
=
) ( y
dx
dy
x
These conditions give the equations:
B ) ( y = = 5 0
33
2 2 2 2 2 + = ) x sin( B ) x cos( A
dx
dy
, so 2 2 0 0
0
+ = = =
=
A ) ( y
dx
dy
x
Therefore, B = 5 and A = -1. The solution to the problem is then:
x 2 ) x 2 cos( 5 ) x 2 sin( ) x ( y + + = .
This solution satisfies both the governing differential equation and the initial
conditions.
Boundary conditions (conditions are specified at two different points).
5 0 = ) ( y
2 2 = ) / ( y
These conditions give the equations:
B ) ( y = = 5 0
4 2 2 4 / A ) / ( y + = =
Therefore, B = 5 and A = 2 - /2. The solution to the problem is then:
x ) x cos( ) x sin( ) / ( ) x ( y 2 2 5 2 2 2 + + = .
In science and engineering boundary and initial conditions are very important physical
requirements. This means that the function and its derivatives up to n-1, where n is
the order of the differential equation, have a physical meaning that must be
understood in order to solve a differential equation. For example, for the beam
bending equation used earlier,
EI
) x ( w
dx
v d
=
4
4
,
the function v is displacement, i.e., distance the beam deflects due to the load, the first
derivative is the slope of the beam, the second derivative is related to the bending
moment in the beam and the third derivative is related to the shear force in the beam.
All four of these physical quantities are converted into mathematical conditions using
these derivatives. A simpler example will be used to illustrate this point. Consider
determining the position of a ball as a function of time, given that the ball is thrown
either straight up or down from some position at some time. The governing
differential equation, which neglects air resistance, is
g
dt
y d
=
2
2
,
where y is positive upward and measures position in feet relative to some fixed
reference point, say the ground, t is time in seconds, and g is the gravitational
constant, 32.2 ft/sec
2
. The general solution to this equation is
B At gt ) t ( y + + =
2
2
1
.
Note the homogeneous (At + B) and particular (-gt
2
/2) solutions that make up the
general solution. In order to predict position, more information is needed since A and
B are arbitrary constants. Clearly one cannot predict position as a function of time if
the position and speed the ball had when it was thrown are not known. These
conditions translate into
0
0 y ) ( y = (This specifies the position from where the ball was thrown)
34
0
0
0 v
dt
dy
) ( v
t
= =
=
(This specifies the speed of the ball when it was thrown)
With these two conditions the following solution is determined:
0 0
2
2
1
y t v gt ) t ( y + + = ,
i.e., the constants A and B have physical meaning, A is the initial speed and B is the
initial position. The example can also be posed as a boundary value problem using the
following conditions:
0
0 y ) ( y = (This defines the position from where the ball was initially thrown)
1 1
y ) t ( y = (This defines the position of the ball at time t
1
).
The solution to this case is:
0 1
2
1 1 1
2
1
y At gt y ) t ( y + + = = , so
1
2
1 0 1
2
1
t
gt ) y y (
A
+
= .
This gives
0
1
2
1 0 1
2 2
1
2
1
y t
t
gt ) y y (
gt ) t ( y +
+
+ = .
Another way of thinking about this case, is how fast must the ball be thrown at t = 0
such that the ball will arrive at a certain place (y = y
1
) at a certain time (t = t
1
). The
answer is the constant A. A boundary value problem can be solved by a shooting
method, which makes use of an initial value solution or method. In this procedure
one can guess the value of A until the condition
1 1
y ) t ( y = is met. This will be
discussed later within the context of an example.
10.3 The Euler Method Numerical Solution of Differential Equations
Next consider the numerical solution of first order differential equations of the initial
value type. The general case is the following:
) y , x ( f
dx
dy
= with
0
0 y ) ( y = ,
where the known function f = f(x,y) can be linear or nonlinear and y
0
is known as an
initial value. Start with the simplest approximation of the first derivative, the first
order, forward difference formula
) h ( O
h
) x ( y ) x ( y
dx
dy
) x ( y
i i
x x
i
i
+

= =
+
=
1
. (10.1)
Therefore, one can predict the value of y at x
1
as follows:
) y , x ( f
h
y y
h
) x ( y ) x ( y
) x ( y
0 0
0 1 0 1
0
=


which gives
) y , x ( hf y y
0 0 0 1
+ = .
Note that this approximates y
1
using only information at x
0
. By successive application
of this formula, any value of y
i
can be predicted. For example,
) y , x ( hf y y
1 1 1 2
+ = ,
35
leading to
) y , x ( hf y y
i i i 1 i
+ =
+
. (10.2)
This is the Euler method, which is the simplest of many methods. Now consider the
example of the parachute problem from pages 14 18 of Chapra and Canale. The
governing first order differential equation is
v
m
c
g
dt
dv
= , (10.3)
subject to the initial condition,
0 0 = ) ( v .
For this case the function, f(t,v) = g (c/m)v. The exact solution of (10.3) is
| |
t ) m / c (
e
c
gm
) t ( v

= 1 .
Applying the Euler method (10.2) to the governing equation (10.3) leads to the
approximation,
(

+ =
+ i i i
v
m
c
g h v v
1
.
Successive application starting from t = t
0
= 0 gives v(t) = v(t
i
) =v
i
for any t. This is
programmed below in MATLAB and compared to the exact solution for h=1 and 2.
Since the approximation (10.1) made in the solution procedure has an error of order h,
when h is halved, the difference from the exact solution is approximately halved.
g=9.8; c=12.5; m=68.1;
% The closed form solution to the parachute problem is v(t)
t=[0:0.01:40];
v=g*m/c*(1-exp(-c/m.*t));
% Now for the numerical solution using the Euler Method.
h=1; n=40/h+1; % input h and calculate the corresponding n.
tn=linspace(0,40,n);
vn(1)=0; % This is the initial condition.
for i=2:n
vn(i)=vn(i-1)+h*(g-c/m*vn(i-1)); % This is the Euler approximation.
end
plot(t,v,tn,vn)
legend(Exact', [Numerical for h = num2str(h)],2)
title(Parachute Problem'); xlabel('time (seconds)'); ylabel('velocity (m/s)')
36
There are numerous other methods that make use of higher accuracy formulas and various tricks.
One of the most popular is fourth order Runge-Kutta. In this procedure, instead of (10.1), the
following formula is used:
| |
4 3 2 1 1
2 2
6
k k k k
h
y y
i i
+ + + + =
+
, (10.4)
where
) y , x ( f k
i i
=
1
|
.
|

\
|
+ + = h k y , h x f k
i i 1 2
2
1
2
1
|
.
|

\
|
+ + = h k y , h x f k
i i 2 3
2
1
2
1
) h k y , h x ( f k
i i 3 4
+ + = .
Again the important point is that the value of y at x
i+1
is being determined from
information at x
i
. As long as the initial value of y is known, one can start the method
to obtain y
1
from x
0
and y
0
. Then successive application gives any y > y
0
.
10.4 Second Order Differential Equations
As an example of a second order differential equation, consider the case of a
cantilever beam with large deflections due to a concentrated force F at the right end as
shown in Figure 10.1. The governing nonlinear differential equation is:
) cos(
EI
F
ds
d

=
2
2
, (10.5)
where EI is the bending stiffness of the beam, is the slope of the beam and s is a
variable measured along the length of the deformed shape of the beam. The boundary
conditions are:
0 0 = ) ( (The wall forces the bean to extend straight out from the wall)
0 =
=L s
ds
d
(Zero bending moment at the end of the beam (s = L). (10.6)
Figure 10.1. A cantilevered beam undergoing large deflections.
The first step is to non-dimensionalize the equation by letting t = s/L. This converts
37
(10.4) and (10.5) to
0
2
2
2
= ) cos(
EI
FL
dt
d

(10.7)
subject to the boundary conditions,
0 0 0
1
= =
= t
dt
d
, ) (

. (10.8a,b)
The nondimensional parameter FL
2
/EI takes into account load (F), geometry (L) and
material properties (EI). This differential equation presents two significant changes from
the parachute example:1) it is a boundary value problem instead of an initial value
problem, and 2) it is second order differential equation instead of first order. These two
issues are addressed below.
The mathematical problem given by (10.7) with (10.8a,b) can also be posed as an initial
value problem subject to the initial conditions,
0 ) 0 ( =
L
d
EI
FL
dt
d
t
2
0
=
=

. (10.9)
The difficulty with this approach is that the value of d as defined in Figure 10.1 is
unknown. This value will be determined in an iterative fashion as a zero of a function
problem by satisfying the condition (10.8b). This is known as a shooting method.
Now for the issue of dealing with a second order differential equation. The second order
equation (10.7) is converted to a set of two first order differential equations by using the
state variable approach (see Palm, page 463). A new unknown is defined as follows
dt
d
) t (

= ,
which leads to the converted set of first order equations,

=
dt
d
) cos(
EI
FL
dt
d

2
= (10.10)
with initial conditions,
0 ) 0 ( =
L
d
EI
FL
) (
2
0 = . (10.11)
In terms of the condition (10.8b) is (1) = 0. The parameter d/L will be adjusted in
(10.11) until (1) = 0. Using the Euler method, the differential equations (10.10) can be
approximated as
i
i i
t t
h dt
d
i

+
=
1
) cos(
EI
FL
h dt
d
i
i i
t t
i


2
1
=

+
=
38
which lead to the approximations
i i 1 i
h + =
+
) cos(
EI
FL
h
i i i

2
1
+ =
+
.
Successive application starting from t = t
0
= 0 gives (t) = (t
i
) =
i
and (t) = (t
i
) =
i
for any t. This is programmed in MATLAB below. There is no exact solution for this
case.
% Euler Method.
const=3; % const is FL^2/EI.
h=0.01; % input h.
n=1/h+1;
t=linspace(0,1,n);
q=0.7; %Guess for the value of q = d/L.
theta(1)=0; beta(1)=-const*q; % These are the initial conditions.
for i=2:n
theta(i)=theta(i-1)+h*beta(i-1); % This is the Euler approximation for theta.
beta(i)=beta(i-1)+h*const*cos(theta(i-1));
end
bc=beta(n); % This boundary condition should be zero. If not, change guess for q=d/L.
disp([The value of the derivative at t = 1 for this choice of q is num2str(bc)])
plot(t,theta,t,beta,.-)
legend(theta(t),beta(t),3)
title([Beam Bending Problem using q = 0.7 and h = 0.01])
xlabel('t = s/L')
ylabel('slope of the beam (radians)')
This program will print out the slope at t = 1 (s = L), i.e., (1), which in general will
not be zero. The value of d/L must be adjusted until (1) = 0. In other words this is a
zero of a function problem. The input is q = d/L and the output is (1). The special
value of q that makes this derivative zero must be determined using, for example, the
bisection method. The plots below are obtained from the above program using q = 0.7
and 0.8, which provide an upper and lower bound for the correct value of q.
39
Using, for example, the bisection method, the revised Euler program looks like:
% Euler Method.
const=3.0; % const is FL^2/EI.
h=0.01; % input h.
n=1/h+1;
t=linspace(0,1,n);
% Bold part of this program implements the bisection method for beta(1) = 0.
converge=-1.0; %When the error criterion is satisfied converge is set to +1.
iterations=0;
% The root must be trapped between the following values of ql and qu.
ql=0.7; bcl=+1; % Only need to know if beta(1) is positive or negative.
qu=0.8;
qr=ql; % This is needed for the first iteration.
for m=1:30; % This means a maximum of 30 iterations will be performed
iterations=iterations+1;
qrold=qr; qr=0.5*(ql+qu);
theta(1)=0; beta(1)=-const*qr; % These are the initial conditions.
for i=2:n % The ODE is solved in this loop.
theta(i)=theta(i-1)+h*beta(i-1); % Euler approximation for theta.
beta(i)=beta(i-1)+h*const*cos(theta(i-1));
end
bc=beta(n); % This boundary condition should be zero.
errora=abs(100*(qr-qrold)/qr);
% This checks to see if the error criterion is satisfied.
if errora < 10^(-6); % This should give seven significant figures for q.
converge=1.0;
break % Terminates for loop since the error criterion is satisfied.
else
% This reduces the interval by a factor of two.
check = bc*bcl;
if check < 0
qu = qr; % Root is between ql and qr so qu is replaced by qr
else
ql = qr; % Root is between qr and qu so ql is replaced by qr
bcl=bc;
end
end
end % This is the end of for loop for bisection method. Next print results.
if converge < 0
fprintf(The program did not converge after %2.0f iterations,iterations)
else
fprintf(\nThe final value of q for h = %10.7f is %10.7f\n ,h,qr)
disp([The value of the derivative at t = 1 for this choice of q is num2str(bc)])
plot(t,theta,t,beta,.-)
legend(theta(t),beta(t),3); title([Beam Bending Problem for h = , num2str(h)])
xlabel('t = s/L'); ylabel('slope of the beam (radians)')
end
40
The output from the above program is presented below. Note how (1) = 0 for the
value of q determined by the program.
MATLAB output:
The final value of q for h = 0.0100000 is 0.7447091
The value of the derivative at t = 1 for this choice of q is 3.1212e-008
In the table below the above program is used to make a table of q as a function of h
(for FL
2
/EI = 3.0). The slope of the beam at the end, i.e., (1), is used to evaluate
convergence.
Step size h
for 1
st
order
solution
Value of
q = d/L
FL
2
/EI = 3.0
Slope at
s = L, (1)
0.1 0.737706 -1.064535
0.05 0.741411 -1.024842
0.01 0.744709 -0.993719
0.005 0.745142 -0.989864
0.001 0.745492 -0.986786
0.0005 0.745536 -0.986401
0.0001 0.745571 -0.986094
0.00005 0.745575 -0.986055
This shows that to obtain three significant figures of accuracy in the slope at the end
of the beam, a step size of approximately h = 0.0001 should be used. Three
significant figures in q is slightly easier to obtain.
10.5 Differential Equations Using MATLAB
MATLAB has numerous built-in functions that perform the successive applications of
formulas such as (10.2) and (10.4). For example, the beam problem can be solved
using the following MATLAB code described in four steps.
Step 1.
The first step is to write the given problem (Equations 10.7 and 10.8) as a set of two first
41
order differential equations. Once again using the state variable approach (see Palm,
page 463),
) t ( ) t ( y
1
=
dt
dy
dt
d
) t ( y
2
2
= =

,
leads to the converted set of first order equations,
2
1
y
dt
dy
=
) y cos(
EI
FL
dt
dy
1
2
2
= ,
with initial conditions,
0 ) 0 ( y
1
=
L
d
EI
FL
) 0 ( y
2
2
= .
The second condition will be enforced by finding the value of d that gives y
2
(1) = 0.
Step 2.
Code the system of first-order ODEs in MATLAB as the function rhsproj1
function dydt = rhsproj1(t,y)
dydt = [ y(2); 3.0*cos(y(1)) ]; % This right hand side is set up for FL^2/EI = 3.0.
Step 3.
Now deal with the initial conditions. Generally the initial conditions, i.e., y
1
(0) and y
2
(0),
are known. However, in this case the value of y
2
(0) is unknown since d is unknown.
Therefore its value must be guessed at the start, and subsequently refined in an iterative
process. With each successive iteration an improved guess is used until finally the
boundary condition at t = 1 (s = L) is met. As mentioned earlier, this amounts to solving
the equation f(d/L) = f(q) = 0, using the methods studied in the beginning of the course
(i.e., bisection method, Newton-Raphson method, etc.). The trick here is to let MATLAB
do all the work using the fzero command.
For this purpose first write a function diffatl, which has q = d/L as an input and the scalar
value of
1 t 1 t
1
2
dt
d
dt
dy
) 1 ( y
= =
= =

as an output. Such a function is defined below, where b is the output. Note that the
system of differential equations must be solved in order to obtain b. The solver is ode45.
function[b]=diffatl(q)
[s,y] = ode45(@rhsproj1,[0 1],[0; -3.0*q]); % This is for FL
2
/EI = 3.0, q = d/L.
res=[s, y(:,1), y(:,2)];
[s1,s2]=size(res);
b=res(s1,3); % This gives the value of y2(1) (entry in bottom row and right column)
Here ode45 solves a system of first order ordinary differential equations using the 4
th
and
42
5
th
order Runga-Kutta formulas. These formulas integrate the system of ODEs described
in the function rhsproj1 over the interval [t = 0,t = 1] = [0,1] with initial conditions
[y1(0), y2(0)] = [0; -FL^2/EI*(d/L)].
Step 4.
Now run the following program using the correct value of q = d/L to obtain a plot for
= (t) for FL
2
/EI = 3.0.
clc
format long g
q=0.95; % initial guess
D=fzero(@diffatl,q); % D is the value of q = d/L for which y2(1) = 0.
fprintf(The value of d/L associated with a zero derivative at t = 1 is %10.5f,D)
[s,y] = ode45(@rhsproj1,[0 1],[0; -3.0*(D)]); % Solve the ODEs with the correct q
plot(s,y(:,1)); % This plots y1(t) which is theta(t).
title(plot of theta of t = s/L for FL^2/EI = 3.0)
title([Beam Bending Problem for the Loading Parameter, FL^2/EI = 3.0])
xlabel('t = s/L')
ylabel('slope of the beam (radians)')
References:
1. S.C. Chapra and R. P. Canale, Numerical Methods for Engineers, 4
th
Ed., McGraw-
Hill, Inc., 2002.
2. W.J. Palm, Introduction to MATLAB 6 for Engineers, McGraw-Hill, Inc., 2001.
43
Appendix A - Determinant of a Matrix
22:
21 12 22 11
22 21
12 11
a a a a A ] A det[ ,
a a
a a
A = =
(

=
33:
(
(
(

=
33 32 31
23 22 21
13 12 11
a a a
a a a
a a a
A .
The determinant of a 3 by 3 matrix consists of the sum of three 2 by 2 determinants. The
general formula below shows that there are six choices for the three 2 by 2 sub
matrices.
) A det( ) ( a ] A det[
ij
j i
j
ij
+
=
= 1
3
1
for 3 2 1 or , i =
or ) A det( ) ( a ] A det[
ij
j i
i
ij
+
=
= 1
3
1
for 3 2 1 or , j = .
The notation det(A
ij
) corresponds to the determinant of a sub matrix (or minor) of A, that
is obtained by deleting the i
th
row and j
th
column of A. Consider the example for the top
equation with i = 3.
) A det( ) ( a ] A det[
j
j
j
j 3
3
3
1
3
1
+
=
=

22 21
12 11 3 3
33
23 21
13 11 2 3
32
23 22
13 12 1 3
31
1 1 1
a a
a a
) ( a
a a
a a
) ( a
a a
a a
) ( a
+ + +
+ + =
nn:
(
(
(
(
(
(
(
(

=
nn ni n n
kn ki k k
n i
n i
a a a
a a a a
a a a a
a a a a
] A [
L L
M M M M M M
L L
M M M M M M
L L
L L
2 1
2 1
2 2 22 21
1 1 12 11
The determinant of an nn matrix consists of the sum of n (n-1)(n-1) determinants. The
determinant of each (n-1)(n-1) determinant consists of the sum of n - 1 (n-2)(n-2)
determinants, etc. The general formula is:
) A det( ) ( a ] A det[
ij
j i
n
j
ij
+
=
= 1
1
for any i from 1 to n.
or ) A det( ) ( a ] A det[
ij
j i
i
ij
+
=
= 1
3
1
for any j from 1 to n.
44
Example for the determinant of a 4 by 4.
(
(
(
(

=
(
(
(
(

=
1 3 0 0
0 0 1 3
1 2 0 0
0 0 2 1
44 43 42 41
34 33 32 31
24 23 22 21
14 13 12 11
a a a a
a a a a
a a a a
a a a a
A
The general formula is ) A det( ) ( a ] A det[
ij
j i
j
ij
+
=
= 1
4
1
for 4 3 2 1 or , , i = .
Let i = 1, which corresponds to expanding across the first row of A.
) A det( ) ( a ] A det[
j
j
j
j 1
1
4
1
1
1
+
=
=
44 43 41
34 33 31
24 23 21
2 1
12
44 43 42
34 33 32
24 23 22
1 1
11
1 1
a a a
a a a
a a a
) ( a
a a a
a a a
a a a
) ( a
+ +
+ =
43 42 41
33 32 31
23 22 21
4 1
14
44 42 41
34 32 31
24 22 21
3 1
13
a a a
a a a
a a a
) 1 ( a
a a a
a a a
a a a
) 1 ( a
+ +
+ +
Using numbers from above,
| | { } | | { } 5 1 3 1 2 3 2 1 3 1 2 1 1
1 3 0
0 0 3
1 2 0
2
1 3 0
0 0 1
1 2 0
1 = = = ) ( ) ( ) ( ) ( ) ( ) ( ] A det[
Similarly, if the determinant is expanded across the second row.
| | { } | | { } 5 3 2 1 1 3 1 3 2 1 1 1 2 = + = ) ( ) ( ) ( ) ( ] A det[
Expand across the third row.
| | { } | | { } 5 3 1 1 2 1 1 3 1 1 2 2 3 = = ) ( ) ( ) ( ) ( ] A det[
Expand across the fourth row.
| | { } | | { } 5 3 2 1 1 2 1 3 2 1 1 1 3 = + = ) ( ) ( ) ( ) ( ] A det[
Expand down the first column.
| | { } | | { } 5 3 1 1 2 2 3 3 1 1 2 1 1 = + = ) ( ) ( ) ( ) ( ] A det[
Expand down the second column.
| | { } | | { } 5 3 1 1 2 1 1 3 1 1 2 3 2 = = ) ( ) ( ) ( ) ( ] A det[
Expand down the third column.
45
| | { } | | { } 5 3 2 1 1 1 3 3 2 1 1 1 2 = = ) ( ) ( ) ( ) ( ] A det[
Expand down the fourth column.
| | { } | | { } 5 3 2 1 1 2 1 3 2 1 1 3 1 = + = ) ( ) ( ) ( ) ( ] A det[
46
Appendix B - Gauss Elimination (Follows Chapra and Canale, pages 238 255).
Solution of the system of equations, [A]{x} = {b}, where
(
(
(
(
(
(
(
(

=
nn ni 2 n 1 n
kn ki 2 k 1 k
n 2 i 2 22 21
n 1 i 1 12 11
a a a
a a a a
a a a a
a a a a
] A [
L L
M M M M M M
L L
M M M M M M
L L
L L
,

=
n
i
2
1
x
x
x
x
} x {
M
M
,

=
n
k
2
1
b
b
b
b
} b {
M
M
,
can be obtained by Gauss elimination. First the [A] matrix is augmented by the right
hand side vector, {b}. The basic goal of Gauss Elimination is to row reduce the
original matrix to an upper triangular form. Then back substitution can be applied to
obtain the solution vector, {x}. Each step in the process uses one equation to obtain
zeros in a column below the diagonal element of this equation. In the first step, the
equation along the top row is used to obtain zeros as shown below. In the second step
the second equation will be used to obtain zeros below
22
a , etc.
(
(
(
(
(
(
(
(

(
(
(
(
(
(
(
(

=
n
k
2
1
nn ni 2 n
kn ki 2 k
n 2 i 2 22
n 1 i 1 12 11
n
k
2
1
nn ni 2 n 1 n
kn ki 2 k 1 k
n 2 i 2 22 21
n 1 i 1 12 11
b
b
b
b
a a a 0
a a a 0
a a a 0
a a a a
b
b
b
b
a a a a
a a a a
a a a a
a a a a
Ab
M
M
L L
M M M M M M
L L
M M M M M M
L L
L L
M
M
L L
M M M M M M
L L
M M M M M M
L L
L L
The coefficients,
ki
a , are obtained by addition of the first and k
th
equation as follows:
{ }
1 1 1 2 12 1 11
11
1
b x a ... x a ... x a x a
a
a
n n i i
k
= + + + + +
{ }
k n kn i ki k k
b x a ... x a ... x a x a = + + + + + +
2 2 1 1
{ }
k n kn i ki k
b x a ... x a ... x a x = + + + + + =
2 2 1
0 ,
giving the formula for all prime elements, where k corresponds to the row and i to the
column,
11
1
1
11
1
1
a
b
a b b ,
a
a
a a a
k k k
i
k ki ki
= = for n ,..., 1 i and n ,..., 2 k = = .
47
This process must be repeated n - 2 more times for an increasingly smaller prime
matrix until an upper triangular form is achieved. This requires another index, say j, that
replaces all occurrences of 1 in the above expressions. Letting the matrix Ab = A
augmented with b,
,
ab
ab
ab ab b a
jj
ji
kj ki ki
= for each j = 1,,n 1, 1 n ,..., j i and n ,..., 1 j k + = + = . (1)
In this expression, i is for the column, k is for the row, and j is for the diagonal element
below which the column of zeros is obtained. Back substitution is then used to obtain
{x}:
(

=

+ =
i
n
1 k i
ki k
kk
k
x a b
a
1
x , where k ranges from n - 1 to 1 and x
n
= b
n
/a
nn
. (2)
There are problems with this naive form of Gauss elimination. These problems include
division by zero, round-off error and ill-conditioning (see Chapra and Canale, 278-279).
As discussed in the book, most of these problems are solved by scaling each row of the
coefficient matrix and then partial pivoting rows at each stage (Example 9.10 in Chapra
and Canale). Naive Gauss elimination is programmed below using MATLAB. Then on
the next page scaling and partial pivoting are added to the program.
% Naive Gauss elimination.
%A=[2 2 3;-1 2 3;2 1 1]; b=[7;4;0]; % This is an example that works.
A=[2 -4 3;-1 2 3;2 1 1]; b=[7;4;0]; % This is an example that does not work.
disp(The solution using A\b in MATLAB is:)
xMATLAB=A\b
ab = [A b]
[n,m]=size(ab);
for j = 1:n-1 % Each j is a step producing a column of zeros below the diagonal.
jterm=ab(j,j);
for k = j+1:n % k corresponds to the row (the jth row and above are done)
kterm=ab(k,j); %This value must be saved outside of inner loop.
for i = j:n+1 % i corresponds to the column
ab(k,i) = ab(k,i) - kterm*ab(j,i)/jterm;
end
end
end
% Back substitution.
x(n) = ab(n,n+1)/ab(n,n); % This gets the first value of x
for kforward = 1:n-1 %for k = n-1:-1:1 would also work.
k = n kforward; % k moves from the bottom row to the top row.
sum = ab(k,n+1);
for i = k+1:n
sum = sum ab(k,i)*x(i); % x(i) must be known for i > k
end
x(k) = sum/ab(k,k);
end
disp(The Gauss Elimination solution is:)
48
x
49
MATLAB output that encounters division by zero in naive form of Gauss
elimination (using A=[2 -4 3;-1 2 3;2 1 1]; b=[7;4;0]; as input to above program)
Warning: Divide by zero.
The Gauss Elimination solution is:
1.0e+003 *
NaN
NaN
NaN
The solution to this system of equations using A\b in MATLAB is:
-1.4444
-1.2222
1.6667
% Gauss elimination with scaling and partial pivoting.
A=[2 -4 3;-1 2 3;2 1 1]; b=[7;4;0]; % This example requires partial pivoting.
ab = [A b]; % [A] and {b} must be input.
[n,m] = size(ab);
for j = 1:n-1 % Each j is a step producing a column of zeros below the diagonal.
% First scale each row from kscale = j to n.
for kscale = j:n % The kscaleth row is scaled
term = max(abs(ab(kscale,j:n))); % Gives max. [A] value in kscaleth row.
for iscale = j:n+1
ab(kscale,iscale) = ab(kscale,iscale)/term; % Scales kscaleth row
end
end
% Now do partial pivoting find row with largest first element.
pivotrow = j; jterm = ab(j,j);
for kpivot = j:n % Finds row with largest element in jth column.
if abs(ab(kpivot,j)) > abs(jterm)
jterm = ab(kpivot,j); pivotrow = kpivot;
end
end
% Now interchange jth row with row corresponding to pivotrow.
for ipivot = j:n+1
temp = ab(j,ipivot);
ab(j,ipivot) = ab(pivotrow,ipivot);
ab(pivotrow,ipivot) = temp;
end
% Back to Gauss elimination
jterm = ab(j,j);
for k = j+1:n % kk corresponds to the row (the jth row and above are done)
kterm = ab(k,j); %This value must be saved outside of inner loop.
for i = j:n+1 % ii corresponds to the column
ab(k,i) = ab(k,i) - kterm*ab(j,i)/jterm;
end
end
end
50
% Back substitution to obtain {x}
x(n) = ab(n,n+1)/ab(n,n); % This gets the first value of x
for kforward = 1:n-1
k = n kforward; % k moves from the bottom row to the top row.
sum = ab(k,n+1);
for i = k+1:n
sum = sum ab(k,i)*x(i); % x(i) must be known for i > k
end
x(k) = sum/ab(k,k);
end
disp(The Gauss Elimination solution is:)
x
Example to illustrate Gauss elimination (Eqn. 1 from page 1), scaling, partial pivoting,
and back substitution (Eqn. 2). The original matrix for n = 3 is taken to be,
2 -4 3 7
-1 2 3 4
2 -1 1 0
For j = 1 do the following three steps:
a. Scale all rows with respect to the largest element in the row from the coefficient
matrix.
0.5000 -1.0000 0.7500 1.7500 (This row was divided by 4, not 7)
-0.3333 0.6667 1.0000 1.3333 (This row was divided by 3)
1.0000 -0.5000 0.5000 0 (This row was divided by 2)
b. After partial pivoting (interchange 1
st
and 3
rd
rows). Interchange rows to bring
the largest number in the j
th
column to the j
th
row.
1.0000 -0.5000 0.5000 0
-0.3333 0.6667 1.0000 1.3333
0.5000 -1.0000 0.7500 1.7500
c. Obtain zeros below the diagonal in the first (j = 1) column using Gauss
elimination. Use Eqn. 1 to update the elements for k = 2 and 3. For each k , i ranges
from 1 to 4.
1.0000 -0.5000 0.5000 0
0 0.5000 1.1667 1.3333
0 -0.7500 0.5000 1.7500
Now j = 2. Since j = n 1 this is the last time the three steps are required.
a. Scale the second and third rows.
1.0000 -0.5000 0.5000 0
0 0.4286 1.0000 1.1429
0 -1.0000 0.6667 2.3333
b. Apply partial pivoting to the second and third rows.
1.0000 -0.5000 0.5000 0
51
0 -1.0000 0.6667 2.3333
0 0.4286 1.0000 1.1429
c. Obtain a zero below the diagonal in the second column. Use Eqn. 1 with k = 3
only and i ranging from 2 to 4. The matrix now has an upper triangular form.
1.0000 -0.5000 0.5000 0
0 -1.0000 0.6667 2.3333
0 0 1.2857 2.1429
Apply back substitution, given by Eqn. 2, to obtain the solution:
1.0e+003 *
-0.00144444444444
-0.00122222222222
0.00166666666667
52
Some other uses of Gauss elimination and additional methods associated with linear
systems:
Given an upper triangular form obtained using Gauss elimination, the determinant of
the matrix is easily calculated as the product of the diagonal elements (page 249 of
Chapra and Canale).
Given an upper triangular form obtained from Gauss elimination, it is easy to obtain
an LU decomposition of [A] where
] U ][ L [ ] A [ = .
The square matrix [U] is the upper triangular form resulting from Gauss elimination
and [L] is a lower triangular form with ones along the diagonal and lower triangular
elements easily obtained from the Gauss elimination process (see Chapra and
Canale, pages 266-267). An important use of this decomposition is for the efficient
solution of the same set of linear equations with different right hand sides, i.e.,
[A]{x} = {b
1
} and [A]{x} = {b
2
}. For example:
} b { } x ]{ A [ = becomes } b { } x ]{ U ][ L [ = .
Let } b { } y ]{ L [ = and obtain {y} by forward substitution.
Then obtain {x} from } y { } x ]{ U [ = by back substitution since {y} is known.
One way to obtain the inverse of a matrix [A] is by applying Gauss-Jordon to [A]
augmented with the identity matrix, i.e., [A I]. Gauss-Jordon (pages 259-260 of
Chapra and Canale) is a continuation of Gauss elimination from an upper
triangular form to the identity matrix. After applying Gauss-Jordon to [A I] the
resulting matrix is [I A
-1
]. However, a more efficient way is to use LU
decomposition to obtain the inverse. This methods takes advantage of the
decomposition for multiple right hand sides as illustrated in the example in Chapra
and Canale, pages 274-276
There are cases where iterative methods are preferable to direct methods for the
solution of a linear system of equations. Typical methods include Jacobi and
Gauss-Seidel, with Gauss-Seidel being the most popular (see Chapra and Canale
pages 289-296). The primary application of iterative methods is for very large
systems of equations, for which elimination methods are more likely to suffer from
problems of round-off error.
The condition number of a matrix can be used to quantify the degree of ill-
conditioning a system of equations may have (see Chapra and Canale, pages 278-
283). The more ill-conditioned a matrix is, the more round-off error can be
expected to occur. MATLAB has a command, cond(A) that evaluates the
condition number according to properties of a matrix (
2
A norm) described on
page 279 of Chapra and Canale. Roughly speaking, if 10
c
= cond(A), up to
approximately c digits of accuracy can be lost due to round-off error. See the
example on page 281 of Chapra and Canale.

Vous aimerez peut-être aussi