me205pfj

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)

description

me205pfj

Transcript of me205pfj

Page 1: me205pfj

1

Basic MATLAB functions and operations used inME 205: Computer Methods in Engineering.

Complied byPaul F. Joseph and Chandramouli Tangella

August 6, 2003

Introduction.This handout provides a review of some basic MATLAB functions and operations for thefollowing topics from ME 205:

1. Taylor Series. Page 12. Polynomials. Page 43. Zeros of functions. Page 54. Matrix algebra. Page 85. Solution of a System of Linear algebraic equations. Page 126. Curve fitting (Not covered Spring 2003). Page 157. Interpolation. Page 188. Numerical integration. Page 219. Numerical differentiation. Page 2810. Numerical solution of ordinary differential equations Page 31

This list covers most of the functions used in ME 205, but represents a very smallfraction of the MATLAB functions that are available. The ability to find what is neededusing a manual or online help is very important. The handout also includes sometheoretical 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 thepoint a,

k

kk )ax(c)x(f −= ∑

=0

, (1.1)

where ck are constants independent of x. In such a representation there is a concern forthe following: 1) convergence of the infinite series, 2) continuity of the function and itsderivatives and 3) the existence of points where the function is singular. An example of asimple function that illustrates all these concerns is 1/(1 - x). By algebraic manipulation,this function can be rewritten as

x

xxxxx

x

nn

−++++++=

+

11

1

1 132 L , (1.2)

for any positive integer n. The power series of this function expanded about zero (a = 0)is

L+++++==− ∑

=

432

0

11

1xxxxx

x k

k . (1.3)

In order to have convergence of (1.3), the “remainder” term from (1.2), i.e.,

x1

xR

n

n −= , (1.4)

Page 2: me205pfj

2

must approach zero as n increases. Therefore the series converges only for 1x0 <≤ .

Furthermore, the series has continuous derivatives for values of x within this range andhas a singular point (“blows up”) at x = 1 (see Figure 1.1). In this course, for values of xin the region of interest, it will be assumed that the series converges, all derivatives of thefunction are continuous, and no singularities exist. Given this assumption, the ck

constants in (1.1) were determined by Brook Taylor in 1715 to give what is now calledthe Taylor series expanded about the point x = a,

k

k

k

)ax(!k

)a(f)x(f −= ∑

=0

. (1.5)

In this formula f k(a) is the kth derivative of f with respect to x evaluated at x = a. Anexample of a Taylor series is given by (1.3). Since a = 0 the series is also referred to as aMaclaurin series. Another example of such a Maclaurin series (or equivalently a Taylorseries expanded about x = 0) is

L+−+−= 753

7

1

5

1

3

1x

!x

!x

!x)xsin( . (1.6)

This series converges for all values of x, i.e., there are no singular points. A calculatoractually uses (1.6) to obtain the sine of x. This is possible because the infinite series canbe truncated to a finite number of terms with negligible error. This important pointmakes the Taylor series useful in numerical analysis.

When (1.5) is truncated to “nth order,” the terms that are omitted comprise the error termor the remainder, Rn. Introduction of the remainder converts (1.5) into the following nth

order Taylor series,

nnn R)ax)(a(f

!n)ax)(a(f)a(f)x(f +−++−′+=

1L , (1.7)

where from (1.5)

∑∞

+=

−=1nk

kk

n )ax(!k

)a(fR . (1.8)

The interpretation of (1.7) is that the Taylor series gives a prediction of a quantity (thefunction f) at a point in space or time (the value x) based on information (f and itsderivatives) evaluated at a nearby point in space or time (the value a). The accuracy ofthe prediction (value of Rn relative to f(x)) depends on two things: 1) how muchinformation 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) becomeless and less important as k increases. This is seen very clearly in the example (1.3) for xnear zero. Therefore, the more terms in the series, the more accurately the seriesrepresents 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 theinfinite series (1.8) can be expressed as the single term

Page 3: me205pfj

3

11

1+

+

−+

= nn

n )ax()!n(

)(fR

ξ, (1.9)

where the special point ξ lies somewhere between x and a. The following importantnotation can be used to represent the remainder:

])ax[(OR nn

1+−= , (1.10)

which is read, “order of (x - a) to the n + 1 power.” This provides information on howaccurate 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. Asshown by (1.9), as long as the n + 1st 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

])ax[(O)ax)(a(f)a(f)x(f 2−+−′+= .If the remainder term is omitted, the equation should be expressed as approximate, i.e.,

)ax)(a(f)a(f)x(f −′+≅ .This approximation is a straight line tangent to the function f(x) at x = a. The more termstaken in the series, the more the approximation “wraps around” the actual function. It isimportant 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 exactcurve. Observe that convergence occurs for –1 < x < 1.

Now consider the following limit that relates some basic calculus to the remainder term:

x

)xsin(

x

lim

0→.

L’Hospitals rule can be applied to evaluate this “zero over zero” limit, which is done asfollows:

11000

=→

=→

=→

)xcos(

x

lim

xdx

d

)xsin(dx

d

x

lim

x

)xsin(

x

lim.

Page 4: me205pfj

4

Equation (1.6) will give the same result by dividing both sides by x and taking the limit,

L+−+−= 642

7

1

5

1

3

11 x

!x

!x

!x

)xsin(

17

1

5

1

3

11

00642 =

+−+−

→=

→Lx

!x

!x

!x

lim

x

)xsin(

x

lim.

Similarly the series (1.6) with the notation (1.10) can be used to be just as sure of thefinal result as follows,

)x(Ox)xsin( 3+=

)x(Ox

)xsin( 21+=

[ ] 1100

2 =+→

=→

)x(Ox

lim

x

)xsin(

x

lim.

Now to see what MATLAB can do. The MATLAB command to generate an nth orderTaylor series for the function f(x) centered about the point x = a, i.e.,

nn )ax)(a(f!n

)ax)(a(f!

)ax)(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 inaddition to evaluating the above series. For example, the MATLAB code for a secondorder 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. nn x)x(f!n

x)x(f!

x)x(f)x(f)xx(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 ni

niiii

1

2

1 21 ++′′+′+=+ L

where xi+1 – xi = h is the same as x – a and xi plays the role of a. You should be able touse all of these expressions and be able to convert from one to another.

2. PolynomialsAn nth order polynomial of the form

0011

1 =++++= −− axaxaxa)x(p n

nn

nn L , (2.1)

occurs frequently in engineering and science. This is the most common type of nonlinearequation, where x is unknown and the ak coefficents are known. The most common

Page 5: me205pfj

5

polynomial is second order, also referred to as a quadratic equation, and is given by02 =++ cbxax .

This case has the well-known closed form solution,

a

acbbx

2

42 −±−= .

It is observed that if b2 – 4ac < 0, there are two complex roots which are complexconjugates of each other. There are also closed form solutions for third and fourth orderpolynomials 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 nth

order polynomial defined in (2.1). Three of the important functions are:Defining a polynomial. An nth order polynomial is defined by its n+1 coefficients. Anarray of size n+1 is used to represent an nth order polynomial as follows:a = [an, an+1,…,a1, a0].

Evaluating a polynomial: Given the coefficient vector a = [an, an+1,…,a1, a0], the valueof the polynomial pn(x) is given by: polyval(a,x).

Roots of a polynomial. The roots of a polynomial are obtained using roots(a). An nth

order polynomial will have the n roots, xi, i=1,…n. If the coefficients of the polynomialare real, then complex roots must occur in conjugate pairs, i.e., if 1 + i is a root, then sotoo must 1 - i. Knowing the roots, the polynomial can be factored as:

02101

11 =−−−=

++++ −− )xx()xx)(xx(

a

axaxaxan

n

nn

nn L

L.

Example: Determine the roots of the third order polynomial, 3x3 – 2x2 + 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 totaldigits instead of the default of four digits beyond the decimal point.

3. Zeros of FunctionsMany problems in science and engineering reduce to the solution of the single nonlinearequation f(x) = 0, or to the set of n nonlinear equations fi(x1, x2, … , xn) = 0 , i = 1, …, n.There are numerous methods that are designed to solve such problems. Considering thecase of a single equation, some of the methods include: graphical, bisection, false-position, Newton-Raphson, and the secant method. They all have advantages anddisadvantages. This topic is important in numerical methods because within a relativelysimple context, many key concepts in numerical analysis are demonstrated.

Three important concepts are the initial guess, iterations and convergence. Given that the

Page 6: me205pfj

6

goal is to determine the special value of x that makes f(x) = 0, all of the methods muststart with a guess of a value of x for which f(x) is not zero. This is the called the “initialguess,” which will later be designated as x0. What distinguishes one method fromanother is how the guess is modified to become an improved estimate. This process oftaking the old x and creating a new x is called an “iteration.” For the method to besuccessful the new x should “converge” to the solution after a certain number ofiterations, 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 itillustrates 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 = xi.

nii

niiiii )xx)(x(f

!n)xx)(x(f

!)xx)(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 xi+1.This gives:

)xx)(x(f)x(f)x(f iiii −′+≅= +10 .

Solving for xi+1 gives the formula for the iteration process,

)x(f

)x(fxx

i

iii ′

−=+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 xi [that is, f’(xi)] is extrapolated downto the x-axis to provide an estimate of the root at xi+1.

Page 7: me205pfj

7

Formula (3.2) can easily be obtained from the Figure as follows:

1

0

+−−

=∆∆

=′=ii

ii xx

)x(f

x

f)x(fslope .

Solving for xi+1 gives the same result as equation (3.2). The iterative process involves thefollowing steps:

1. Make an initial guess, x = x0.2. Determine an improved guess, x1 using equation (3.2)3. Now use x1 as the “old guess” in equation (3.2) to obtain a better

approximation, x2.4. Repeat the iterative process until either f(xi+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 functionis required. Most of the other methods do not require a derivative. Now to consider thesolution of f(x) = 0 using MATLAB.

The MATLAB “fzero” command can be used for the single unknown case. There are noguarantees of success in such cases, especially with multiple equations. Usually, thecritical 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[ ]( ) 0401

16889 16810 =−−= − ./xex

).(.)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.

Page 8: me205pfj

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.mOn line 103 ==> [tol, trace, calltype, FunFcn, varargin] = ...

>> fzero(char(g),12) % The function g must be converted into a string using charans = 14.78020383166106

Method 4. With an m-file. From the command window in MATLAB click on file, thennew, 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 112function 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 guessof 12, then evaluates f(x) at the numerical answer to see how close it is to zero. It is agood 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 thatis appropriate for the application.

For the case of more than one unknown, a numerical method must be chosen and thenprogrammed. A good method to start with is by Newton-Raphson (see, for example, thetext 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 andvertical columns. When a matrix has only one row or column it is, respectively, a row orcolumn vector. Otherwise it is said to be a matrix of size m×n, which has m rows and ncolumns. Matrix operations are considered in this section. In MATLAB there are also“array operations” which differ from matrix operations. For example, division of onearray by another array is defined, but division of a matrix by another matrix is notdefined. 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 byrow with the elements in the row separated either by a space or a comma, and the rows

Page 9: me205pfj

9

separated by semicolons. The entire matrix is enclosed in square brackets. For example,the (2×3) matrix A,

[ ]

=

654

321A ,

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 theelement in the ith row and jth column of [A], i.e., aij. For the above example, A(1,3) = 3.This is a very important command in basic programming.

Dimension of a matrixThe 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 thenumber 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 thelength 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] appendsrow vector V to the rows of A.

Addition and subtraction of matricesAddition and subtraction of matrix A and matrix B are done element by element, wherethe dimensions of A and B must be the same. The commands are simply A+B and A-Bwhere 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).

Page 10: me205pfj

10

Multiplication of matricesMultiplication of two matrices can be performed only if the first matrix has as manycolumns as the number of rows in the second matrix. The resulting matrix will have thenumber of rows of the first matrix and the number of columns as the second matrix. Thiscan be illustrated for [C] = [A][B] as follows:

[ ] [ ] [ ] lmmnln BAC ××× = .This is interpreted as a matrix A with n rows and m columns is multiplied by a matrix Bwith m rows and l columns, results in a matrix C with n rows and l columns. TheMATLAB command is simply A*B where A and B have already been defined asmatrices. For example, given the matrix A above,>> size(A)ans = 2 3shows 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*BC = 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 ofB, i.e., C(2×4) = A(2×3)*B(3×4).

Transpose of a matrixThe Transpose of a matrix involves transforming its rows into columns and its columnsinto rows. To obtain the transpose of matrix C the command in MATLAB is C'. Usingthe 2×4 matrix C from the above example shows how the rows and columns are reversed:>> C'ans = 38 83 44 98 50 113 56 128

Page 11: me205pfj

11

If the elements of C are complex numbers, the transposed matrix also consists of thecomplex conjugates of the elements.

Square matrixA square matrix is any matrix where the number of rows is equal to the number ofcolumns. 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 linearequations and n unknowns. For example, the matrix D above could be associated with The three equation and three unknown system, [D]{x} = {b} where:

732 321 =++ xxx

43 31 =+− xx

02 321 =+− xxx

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) matrixIf 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. TheMATLAB 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

Page 12: me205pfj

12

Determinant of a matrixThe command to evaluate the determinant of a square matrix is det(D). For an inversematrix to be defined, the determinant must be nonzero. See Appendix A for more detailson a determinant of a matrix.

Rank of a matrixIt is first necessary to define a subdeterminant. A subdeterminant is the determinantof a square matrix that is created from a larger matrix by canceling rows and columns.As an example, a 3×3 matrix has nine different 2×2 subdeterminants. The rank of amatrix A is defined as follows (see Palm): An m×n matrix A has a rank r if and only ifthe determinant of A contains a nonzero r×r subdeterminant and every squaresubdeterminant with r+1 or more rows is zero. Note that the rank is always greaterthan or equal to one. Therefore, if the rank of an n×n matrix is less than n, thedeterminant of the matrix is zero. The MATLAB function to determine the rank of amatrix A is simply rank(A). The rank is used to determine the character of a solutionto 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:

111212111 bxaxaxaxa nnii =+++++ LL

222222121 bxaxaxaxa nnii =+++++ LL

MMM

knknikikk bxaxaxaxa =+++++ LL2211

MMM

nnnnininn bxaxaxaxa =+++++ LL2211

In matrix form this system can be expressed as [A]{x} = {b}, where

=

nnninn

knkikk

ni

ni

aaa

aaaa

aaaa

aaaa

]A[

LL

MMMMMM

LL

MMMMMM

LL

LL

21

21

222221

111211

,

=

n

i

x

x

x

x

}x{

M

M2

1

,

=

n

k

b

b

b

b

}b{

M

M2

1

The matrix [A] is the coefficient matrix where the aki are constant coefficients, thevector {x} contains the n unknowns, xi, and {b} is the “right hand side vector,” whichcontains the known constants, bk. Note that the subscripts k and i range from 1 to n.An alternative way to express the n equations is as follows:

n,,kfor,bxa ki

n

iki K1

1

==∑=

.

Page 13: me205pfj

13

Assuming that a unique solution exists, there are different ways of solving theseequations 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 rowechelon form,” the n×n matrix, [I], is the identity matrix, and the last columnof the n×(n+1) matrix, S, is the solution. See Appendix B for more on GaussElimination.

Each of these methods are applied to the following example, the solution of which is x1 = 0.05, x2 = 1.45 and x3 = 1.35:

732 321 =++ xxx

43 31 =+− xx

02 321 =+− xxx

>> A=[1,2,3;-1,0,3;2,-1,1];>> b=[7;4;0];>> % using the inverse method:>> inv(A)*bans = 0.0500 1.4500 1.3500>> %using the backslash command>> A\bans = 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, infinitelymany solutions or no solution. The following rank test can be used to determinewhich 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 linearcombination of the first two equations (the first equation minus the second equation

Page 14: me205pfj

14

gives the third equation).732 321 =++ xxx

43 31 =+− xx

322 21 =+ xxWriting this as [A]{x} = {b}, MATLAB can be used to determine that there are an infinitenumber 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, thedeterminant should be zero:>> det(A)ans = 0

If the right hand side of the third equation is changed from 3 to, for example, 4, asfollows:

4x2x2 21 =+ ,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. Forexample, the equation

1131211 bzayaxa =++can be compared to the equation of a plane,

( )[ ] 0000 =−+−+−⋅ k)zz(j)yy(ixxnrrrr

,

that goes through the point (x0,y0,z0) and is perpendicular to the vector,

knjninn zyx

rrrr++= .

A geometrical interpretation of the three types of solutions follows:a) Solution is unique. If the ranks of the coefficient matrix and the augmented matrixare both 3, the three planes intersect at one point to give a unique solution. Twoplanes intersect to form a line and the third plane cuts through the line at one point.

Page 15: me205pfj

15

b) Infinite number of solutions. If the ranks of both the coefficient matrix and theaugmented matrix are 2, the three planes intersect along a line (infinite number ofsolutions), 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 thecoefficient matrix, there is no common point that lies on all three planes. Theequations correspond to parallel planes or parallel lines created by intersecting planes.

To solve for a system that has infinitely many solutions, the rref MATLAB commandcan be used. Consider the example where the third equation is obtained by taking thefirst and subtracting the second.

732 321 =++ xxx

43 31 =+− xx

322 21 =+ xxFirst the solution is obtained algebraically. The second equation gives

43 31 −= xx ,

while the first equation, given this result for x1, gives( ) 73243 323 =++− xxx

which leads to: 2113 32 /xx +=Therefore any set of values that satisfies the following relationships is a solution:

43 31 +−= xx

2/11x3x 32 +=

33 xx = .

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 0This tells us that x3 can be anything, while x1 =-4 + 3x3 and x2 = 5.5 – 3x3.

6. Curve Fitting (Regression analysis)Curve fitting is a technique of obtaining a functional relationship, y = f(x), that bestfits a set of M data points, (xj, yj), j = 1,…,M. The basic problem is that of regression,where the x values are inputs (independent variable) and the y values are measured asdata (dependent variable). It is assumed that the x values are correct, and it isunderstood that there is error associated with the measurement made for y. The curvefit approximates the data, it does not go through all of the data points. (In the nextsection interpolation will be covered, which obtains a curve that does go through allthe points.)

The standard curve fitting approach is the method of least squares. This methodinvolves the following steps, which in MATLAB is referred to as “linear-in-the-parameters” regression:

Page 16: me205pfj

16

Step 1. Choose a functional form in terms of L unknown coefficents, ci, i = 1,2,…,L, thatis used to represent the M data points, (xj,yj) for j = 1,…,M.

)x(fc)x(f i

L

ii∑

=

=1

.

The problem is to determine the L ci coefficients to obtain a “best fit.”Step 2. Introduce the sum of the squares of the error between each data point and thecurve fit, that is used to quantify the quality of the fit:

[ ]2

1∑

=

−=M

jjjr )x(fyS , where )x(fc)x(f ji

L

iij ∑

=

=1

Step 3. Obtain L equations by minimizing Sr with respect to the L ci coefficients. This isassumed to be the best fit of the data given the chosen form of f(x):

L,...,k,c

S

k

r 10 ==∂∂

.

Step 4. Complete the associated algebra to obtain the linear system of L equations and Lunknowns,

)x(fy)x(f)x(fc jk

M

jjjkj

M

ji

L

ii ∑∑∑

===

=111

, k=1,…,L,

which is simply [A]{c} = {b}, where

)x(fyband)x(f)x(fa jk

M

jjkjkj

M

jiki ∑∑

==

==11

.

The most common choice of a fitting function in the least squares, linear regressionapproach is a polynomial. In this case,

,xaxaaxa)x(fc)x(f nn

pn

ppi

L

ii +++=== ∑∑

==

L1001

where L = n + 1 and fi(x) = xi-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 ofthe polynomial, and a is the vector of n+1 coefficients as defined in the aboveequation. Consider the following MATLAB example (from the MATLAB Helplibrary) using the six data points (M = 6),

j 1 2 3 4 5 6xj 0 0.3 0.8 1.1 1.6 2.3

yj 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.

Page 17: me205pfj

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 problemdescribed in Steps 1-4 by using the backslash operator, which solves a system of Mequations in L unknowns in the least squares sense when M > L. In other words, thebackslash operator takes care of Steps 2-4 “automatically,” i.e., the backslash operator isa built-in function for a least squares linear regression analysis. All that is necessary is todefine the equations. For example, consider the case of f(x) = a1 + a2e

-x + a3xe-x, wherea1, a2 and a3 are to be determined in the least squares sense using the six data points. Foreach data point there is an equation, which leads to a system of six equations in threeunknowns. Consider this as [D]{a} = {y} where, in MATLAB, a = D\y will give thesolution. All that is required is construction of the 6×3 D matrix, given that {y} is thedata in a column vector. In the notation of the derivation on the previous page, dji = fi(xj),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

Page 18: me205pfj

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 window’s tool (see Palm) thatsimplifies the process of curve fitting and data analysis. As with all the topics in thishandout, there are many more built-in functions available for curve fitting and dataanalysis.

7. Interpolation.Interpolation is different from curve fitting in that an interpolated curve passesthrough all the data points. Therefore, interpolation should be used when the data isknown to be accurate. Interpolation, like curve fitting, is used to predict valuesbetween data points, not outside of the range of data, which is extrapolation.

There are numerous interpolation formulae. In basic interpolation, since the curve isforced to go through the data, the unknown parameters in the curve must be equal tothe number of data points used. For example, in linear interpolation, a straight linehas two parameters (a and b in ax + b) so two data points are required. In cubicinterpolation four data points are required (a, b, c, and d in ax3 + bx2 + cx +d). Thereare also interpolation methods that make use of derivative conditions at the datapoints, such as cubic splines. Cubic splines will be discussed after presenting one ofthe basic interpolation methods.

The Lagrange 2, 3 and 4-point formulas will be presented using the notation in thefollowing table:

Page 19: me205pfj

19

i -1 0 1 2xi x-1 x0 x1 x2

f(x) = f(xi) = fi f-1 f0 f1 f2

The data must be equally spaced, where the spacing is h, i.e., xi +h = xi+1 for all i.The formulas correspond to linear, quadratic and cubic polynomials that are forced togo through 2, 3 or 4 data points, respectively. For example, forcing the line f(x) = ax+ b to go through the two points (x0,f0) and (x1,f1) leads to:

baxf += 00

baxf += 11 ,which gives

10

0110

10

10

xx

fxfxb,

xx

ffa

−−

=−−

= , so 10

0110

10

10

xx

fxfxx

xx

ff)x(f

−−

+−−

= .

Substituting x = x0 + ph and using h = x1 – x0, gives the Lagrange 2-point formula.2-point: 100 1 pff)p()phx(f +−=+ , valid for x0 ≤ x ≤ x1 or 0 ≤ p ≤ 1.

Note how this formula can be obtained directly using this choice of p, i.e., the curve isforced to go through the two points (p = 0,f0) and (p = 1,f1). The 3-point and 4-pointformulas can be constructed in a similar manner. They are

3-point: 102

10 2

11

2

1f

)p(pf)p(f

)p(p)phx(f

++−+

−=+ − , valid

for x-1 ≤ x ≤ x1 or -1 ≤ p ≤ 1,

4-point: 0

2

10 2

21

6

21f

)p)(p(f

)p)(p(p)phx(f

−−+

−−−=+ −

2

2

1 6

1

2

21f

)p(pf

)p)(p(p −+

−+− , valid for x-1 ≤ x ≤ x2 or -1 ≤ p ≤ 2.

The relationship between x and p is, x = x0 +ph or p = (x – x0)/h. The independentvariable p is used to obtain more compact expressions. In all cases the curve goesthrough the data used to establish the formula, i.e., when p = -1, 0, 1, or 2, f(xp) = fp.For example, for the 4-point case, it is observed that when p = 2, f(x0 + 2h) = f(x2) =f2. In order to use these expressions, it is necessary to first choose the pointcorresponding to x0, then determine the value of p that corresponds to the point ofinterest using x = x0 + ph. Example: Use 4-point interpolation to approximatesin(0.25) = 0.247404 given the following table of data where h = 0.1.

xi 0.1 0.2 0.3 0.4 0.5 fi 0.099833 0.198669 0.295520 0.389418 0.479426

Choose x0 = 0.2 so that x = 0.25 is in the center of the data. In this case p = 0.5 basedon x = 0.25 = 0.2 + ph, where h = 0.1. Then

19866902

2501500998330

6

25015050250

2

.).)(.(

.).)(.(.

).(f−−

+−−−

=

247403038941806

150502955200

2

25015050 2

..).(.

.).)(.(.

=−

+−+

− .

Page 20: me205pfj

20

Values of x0 = 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 interpolationcan be accomplished using interp1(xj,yj,x), where the data vector is stored in the set ofpoints, (xj,yj), and x corresponds to the value or values at which the linearapproximation is to be made. For example, consider the case above where two pointinterpolation for sin(0.25) gives:

24709502955200501986690501250 ..*..).().(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.24709476872820Note that this MATLAB function is programmed to use the two surrounding points x0

= 0.2 and x1 = 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’) orspline(xj,yj,x), (see Chapra and Canale, Section 18.6). This method strings together aseries of cubic polynomials, where each polynomial links two neighboring data pointsby 1) going through the two end points, 2) matching slopes with adjacent splines at thetwo end points, and 3) matching second derivatives (curvature) at the endpoints. Thisgives a very accurate estimate of the value of the function between data points asshown for the sin(0.25) example.>> interp1(xj,yj,0.25,'spline')ans = 0.24740384086582This value is correct to six digits (0.247404). Cubic splines can also be used to makea 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)

Page 21: me205pfj

21

The MATLAB command interp2(xj,yj,zj,x,y) is for linear interpolation of the twodimensional function, z = f(x,y).

8. Numerical IntegrationConsider 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 beintegrated analytically or f(x) is only known at the discrete points, xi, i = 0,1,2,…,n. Bothcases occur frequently in engineering analysis. We first consider methods that followfrom the previous section on interpolation. A uniform spacing of h is assumed betweenpoints, giving a total of n equally spaced intervals, and n+1 pairs of xi and fi. Thefollowing figure from Chapra and Canale (Figure 21.8) gives a representation for the so-called Newton-Cotes closed integration formulas.

Page 22: me205pfj

22

Trapezoid Rule:Step 1. Assume linear interpolation between neighboring points given by the Lagrange2-point formula. Use the first two points in the interval.

100 1 pff)p()phx(f)x(f +−=+= .

Step 2. Determine the area under the curve corresponding to the portion, x0 ≤ x ≤ x1.

( )[ ] [ ]10

1

0

10

1

0

0 21

1

0

ffh

dppfphdp)phx(fhdx)x(fx

x

+=+−=+= ∫∫ ∫ .

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

[ ] [ ] [ ] [ ]=+++++++++≅ −+∫ nnii

b

a

ffh

ffh

ffh

ffh

dx)x(f 112110 2222LL

=

++ ∑

=

1

10 2

2

n

ini fff

h. (8.1)

In MATLAB, the trapz(x,f) function will perform this integration.

Simpson’s 1/3 Rule.Step 1. Assume quadratic interpolation between every set of three points given by theLagrange 3-point formula. Use the first three points in the interval to give,

212

01 2

11

2

1f

)p(pf)p(f

)p(p)phx(f)x(f

++−+

−=+= .

Step 2. Determine the area under the curve corresponding to the portion, x0 ≤ x ≤ x2.

dpf)p(p

f)p(f)p(p

hdp)phx(fhdx)x(fx

x∫∫ ∫−−

+

+−+−

=+=1

1

212

0

1

1

1 2

11

2

12

0

[ ]210 43

fffh

++= .

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 shouldbe divisible by two, i.e., n should be an even number.

[ ] [ ] [ ]=+++++++++≅ −−∫ nnn

b

a

fffh

fffh

fffh

dx)x(f 12432210 43

43

43

L

=

+++ ∑ ∑

=

=

1

531

2

6420 24

3

n

,,i

n

,,,inii ffff

h. (8.2)

In a similar fashion additional formulas can be obtained that correspond to higher orderpolynomials. For example, using cubic interpolation with the 4-point Lagrange formula,Simpson’s 3/8 rule can be obtained. The formula for the area in Step 2 is

[ ]3210

x

x

ff3f3f8

h3dx)x(f

3

0

+++=∫ ,

Page 23: me205pfj

23

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

++++≅ ∑ ∑∑∫

=

=

=

2n

,7,4,1i

3n

,9,6,3ini

1n

,8,5,2iii0

b

a

ff2f3f3f8

h3dx)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 thefollowing example some programming is required. In the example sin(x) will beintegrated from 0 to π, which has a closed form value of 2. The three integrationapproaches given by equations (8.1), (8.2) and (8.3) are used to complete the followingTable.

n TrapezoidRule

Simpson’s1/3 Rule

Simpson’s 3/8 Rule

6 1.9540972 2.0008632 2.002009812 1.9885638 2.0000526 2.000119424 1.9971434 2.0000033 2.000007448 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 rulesum=f(1)+f(n+1);for i=2:1:nsum=sum+2*f(i);endTrap=h/2*sum% we could also use the MATLAB trapz(x,f) function

% Simpson’s 1/3 rulesum1=0;for i=2:2:nsum1=sum1+4*f(i);endsum2=0;for i=3:2:n-1sum2=sum2+2*f(i);endSimp13=h/3*(f(1)+sum1+sum2+f(n+1))

Page 24: me205pfj

24

% Simpson’s 3/8 rulesum1=0;for i=2:3:n-1sum1=sum1+3*f(i);endsum2=0;for i=3:3:nsum2=sum2+3*f(i);endsum3=0;for i=4:3:n-2sum3=sum3+2*f(i);endSimp38=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. Itcan be shown that the error of the trapezoid rule is on the order h2, while those of the 1/3and 3/8 rules are h4. This means that if h is cut in half, the error of the trapezoid ruleshould reduce by a factor of four, while the others should reduce by a factor of 16. Forexample, as n increases from 12 to 24, h is reduced by a factor of two and the relativepercent errors for the three methods reduce by, respectively, factors of 4.003, 15.94 and16.14. In general the 3/8 rule is slightly better than the 1/3 rule, but not in this case forthese values of n. The bottom line is that Simpson’s 1/3 rule is very good.

In cases when the function is known, such as this example where f(x) = sin(x), theMATLAB quad function can be used to use Simpson’s rule in an adaptive way. Thismeans that the built-in function automatically decreases the interval until an errorcondition is met. While this is good for accuracy and casual use, it is not efficient forlarge scale computations. The MATLAB command is quad(f,a,b,tol), where f, a and bhave been defined, and tol is the absolute error. The default in quad(f,a,b) is 10-6. Forthe sine function example in Table 8.1,>> quadl('sin(x)',0,pi,10^(-3))ans = 1.99999997747113

Page 25: me205pfj

25

>> quad('sin(x)',0,pi)

ans =

1.99999999639843

>> quad('sin(x)',0,pi,10^(-9))

ans =

1.99999999999913We now consider methods where the function must be available for evaluation at anypoint. For example, the function can be written in a MATLAB m-file. For such casesthere are several methods to evaluate integrals. One of the important approaches is Gaussquadrature. In this approach the integral is first rewritten as

∫∫+

−=

1

12dt)t(f

abdx)x(f

b

a

,

by using the substitution,

22

abt

abx

++

−= ,

to convert the integral from a to b to –1 to +1. The function )t(f is given by

)x(fab

tab

f)t(f =

+

+−

=22

.

The integral is now in a standard form, to be approximated as follows:

)t(fwdt)t(f i

N

ii∑∫

=

+

≅1

1

1

,

For a given choice of N, the method provides the special “weights,” wi and “GaussPoints,” ti that are needed to perform the sum. The weights and Gauss points are chosento make the integral exact for a polynomial of degree 2N-1. For example, the 2-pointGauss formula is exact for a cubic equation, and is given by:

+

−=≅ ∑∫

=

+

− 3

1

3

12

1

1

1

ff)t(fwdt)t(f ii

i ,

where w1 = w2 =1 and t1 = -t2 = -1/√3. Consider an example to show that it is exact for acubic polynomial.

[ ]4

798312

2

6

3

2

4

312623

7

2

2347

2

23 =+− +=+−+

=

−=−∫

x

x

xxxxdxxxx (from Maple)

Using the Gauss 2-point formula,

[ ] [ ])/(f)/(fdt)t(f)(

dxxxx 31312

9

2

2712623

1

1

7

2

23 +−=−−

=+−+ ∫∫−−

,

where )x(f)t(f = and

2

5

32

9

2

27

3

1

2

27

22 11 +−=−+

+−−−

=

+

+−

=)()(ab

tab

x

Page 26: me205pfj

26

2

5

32

9

2

27

3

1

2

27

22 22 +=−+

+−−

=

+

+−

=)()(ab

tab

x .

Therefore, once again using Maple for calculations, the answer is the same as above.

[ ]4

7983

2

5

32

9

2

5

32

9

2

912623

7

2

23 =

++

+−=+−+∫

ffdxxxx .

The Gauss approach is very easy to program if the weights and Gauss points areavailable, as in the example below:

[ ]∫ −−−+−4

1

511 14723 dx)x)(x(xxx .

This integral is of the form,

)r(gwdxr)r(g i

n

ii∑∫

=

+

≅−1

1

1

21

where the Gauss points and weights are given by

++=

+=

1112

n

isin

nw,

n

icosr ii

πππ.

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

[ ]∫ −−−+−4

1

511 14723 dx)x)(x(xxx

Now let 22

abr

abx

++

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

[ ] ∫∫−

=−−−+−1

1

24

1

2511 1

214723 drr)x(g

abdx)x)(x(xxx

Now program it.fprintf(‘ n Gauss Quadrature \n’) % Table heading.fprintf(‘ ==========================\n’)a=1; b=4; n=0;

for icase = 1:6n = n + 2;

% Obtain the r and w values for this n.for i=1:nr(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:nsum=sum + w(i)*g(i);

Page 27: me205pfj

27

endsum=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 thequadl function performs Lobatto Quadrature, which is related to Gauss Quadrature. Themain 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 inMATLAB so an error can be specified and the program keeps refining until the errorcondition is met. Using this approach for the previous example gives:clearformat longsoln1 = 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+006soln2 = 3.318860027346433e+006soln3 = 3.318860027347609e+006 (Try this – it takes a few seconds to calculate)

Page 28: me205pfj

28

9. Differentiation.Numerical differentiation is important because it is required to solve differentialequations that cannot be treated analytically. While MATLAB has many built-infunctions to solve differential equations, it does not have many functions forcalculating derivatives. Nevertheless, it is an important operation that is oftenrequired. There are two cases to consider. First when the data is not accurate andsecond when the data is accurate.

When the data, yj = f(xj) for j = 1,…,M, is not accurate, an accurate estimate of theslope, dy/dx, can be very difficult to obtain. One approach is to obtain a curve fit ofthe data as described in Section 6, and then take the derivative. For example, if it isknown 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 resultfor the derivative. However, inaccuracy in the data will be amplified in calculatingthe 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 wherethe data, (xi, yi = f(xi)), corresponds to the uniform spacing, xi+1 – xi = h for all i. Thegoal is to approximate the derivatives of a function at some point xi, for example thefirst derivative, dy/dx =f′(xi), in terms of values of the function at and near the point xi.These approximations are made using the Taylor series,

)h(Oh)x(f!n

h)x(f!

h)x(f)x(f)x(f nni

niiii

121

1

2

1 ++ +++′′+′+= L . (8.1)

First the above expression is solved for f′(xi),

)h(Oh)x(f!

h)x(f!h

)x(f)x(f)x(f ii

iii

3231

3

1

2

1+−′′−

−=′ + . (8.2)

The more terms included in the approximation, the more accurate it is. Starting with afirst order approximation where terms of order h and higher are ignored gives:

)h(Oh

)x(f)x(f)x(f ii

i +−

=′ +1 . (Forward difference)

This is the first order, forward difference formula since it involves values of thefunction at and “forward” of the point xi. To create the backward difference formula,use negative h in the Taylor series (8.1) to obtain

)h(Oh)x(f!

h)x(f!

h)x(f)x(f)x(f iiiii4332

1 3

1

2

1+−′′+′−=− , (8.3)

which, solved for the first derivative, gives

)h(Oh)x(f!

h)x(f!h

)x(f)x(f)x(f ii

iii

3231

3

1

2

1+−′′+

−=′ − . (8.4)

Ignoring terms of order h and higher gives the backward difference formula to firstorder as

)h(Oh

)x(f)x(f)x(f ii

i +−

=′ −1 . (Backward difference)

Page 29: me205pfj

29

The central difference formula, which involves information from points less than andgreater than the point under consideration, is obtained by solving for the firstderivative after subtracting (8.3) from (8.1) to obtain

)h(Oh

)x(f)x(f)x(f ii

i211

2+

−=′ −+ . (Central difference)

Note how the order h term corresponding to the second derivative cancels, giving amore accurate result than either the forward or backward difference formulas. Thisresult is simply the average of the forward and backward difference formulas.

In a similar manner, higher order formulas and higher order derivatives can bedetermined. Some of these formulas are listed below. More formulas can be found innumerical methods books such as Chapra and Canale. Note that while the centraldifference formulas are most accurate, they cannot be applied at the two ends. At theleft end forward difference formulas apply, while at the right end, backward differenceexpressions must be used.Forward Differences:

)h(Oh

)x(f)x(f)x(f ii

i +−

=′ +1

)h(Oh

)x(f)x(f)x(f)x(f iii

i212

2

34+

−+−=′ ++

)h(Oh

)x(f)x(f)x(f)x(f iii

i ++−

=′′ ++2

12 2

)h(Oh

)x(f)x(f)x(f)x(f)x(f iiii

i2

2

123 254+

+−+−=′′ +++

Backward Differences:

)h(Oh

)x(f)x(f)x(f ii

i +−

=′ −1

)h(Oh

)x(f)x(f)x(f)x(f iii

i221

2

43+

+−=′ −−

)h(Oh

)x(f)x(f)x(f)x(f iii

i ++−

=′′ −−2

212

)h(Oh

)x(f)x(f)x(f)x(f)x(f iiii

i2

2

321 452+

−+−=′′ −−−

Central Differences:

)h(Oh

)x(f)x(f)x(f ii

i211

2+

−=′ −+

)h(Oh

)x(f)x(f)x(f)x(f)x(f iiii

i42112

12

88+

+−+−=′ −−++

)h(Oh

)x(f)x(f)x(f)x(f iii

i2

2

11 2+

+−=′′ −+

)h(Oh

)x(f)x(f)x(f)x(f)x(f)x(f iiiii

i42112

12

163016+

−+−+−=′′ −−++

Page 30: me205pfj

30

Consider the following numerical example, which is obtained from y = f(x) = sin(x).xi 0.0 0.1 0.2 0.3 0.4 0.5 0.6

fi 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 andsecond derivatives of sin(x) at x = 0.3. These values are entered in Table 9.1.

nO(hn)

Order ofderivative

ForwardDifference

BackwardDifference

CentralDifference

Exact

1 1st 0.938980 0.9685102 1st 0.958430 0.958585 0.9537454 1st 0.955333∞ 1st 0.9553361 2nd -0.389000 -0.1985002 2nd -0.298800 -0.297300 -0.295300

4 2nd -0.295558 ∞ 2nd -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 andbackward 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 MATLABprogram 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.5fdfofd=diff(f)./diff(x);% Backward difference, first order, first derivative at x = 0.1,0.2,…,0.6bdfofd=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 longfdfofd(4)bdfofd(3)cdsofd(3)ans = 0.93898000000000ans = 0.96851000000000ans = 0.95374500000000

Page 31: me205pfj

31

10. Numerical solution of ordinary differential equations10.1 Some Definitions:Ordinary differential equation (ODE) – an equation with a function as an unknownthat 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 cylindricalfixed drum with static coefficient of friction, µs, for impending slip between the beltand the drum. The tension of the belt T as a function of position θ is given by

0=− Td

dTsµ

θ.

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

02

2

2

22 =

∂∂

+∂∂

=∇y

T

x

TT

Order of a differential equation – The highest derivative in the equation corresponds tothe order of a differential equation. For example, the following differential equationfrom EM 304 for the deflection, v, of a beam with loading, w=w(x), and uniformbending stiffness, EI, is fourth order:

EI

)x(w

dx

vd−=

4

4

.

Constant coefficient ODE – A Differential equation in which the coefficients of theterms in the equation, that multiply the unknown function or its derivatives, areconstants. For example:

)xcos(xydx

dy

dx

yd37 3

2

2

+=−+ .

Variable coefficient ODE – case where at least one coefficient is not constant, such as

7=+ xfdx

df.

Homogeneous differential equation – A differential equation where all terms involvethe unknown function, i.e., there is no lone term that is a function of only theindependent variable. The previous three examples are nonhomogeneous. Forexample, the following equation is a non-constant coefficient, homogeneous, ordinarydifferential equation:

072

2

=−+ ydx

dyx

dx

yd.

Linear and nonlinear differential equations – The differential equation is linear if thesolution of the homogeneous part of the differential equation does not change if theunknown is replaced by any non-zero constant multiplied by the unknown. All of theexamples used in the previous definitions are linear. Examples of nonlineardifferential equations:

Page 32: me205pfj

32

02

2

=+ )ycos(dx

yd, 7xy

dx

dy2

=−

, )xcos(ydx

dyy

dx

yd=++ 7

2

2

.

Homogeneous and particular solutions – When a differential equation is linear thesuperposition principle can be used, meaning that the sum of any two solutions mustalso be a solution. In order to obtain the complete solution to a givennonhomogeneous, differential equation, (and not just a part of it), it is convenient toconsider two different solutions. The “homogeneous solution” corresponds to onlythe homogeneous part of the equation, while the remainder is referred to as the“particular solution.” For example:

xydx

yd84

2

2

=+

has the general solutionx)xcos(B)xsin(A)x(y 222 ++= ,

where A and B are arbitrary constants. The homogenous solution corresponds to thesine and cosine terms, while the particular solution is 2x.

General solution – The “complete” solution to a differential equation before anyadditional constraints, boundary conditions or initial conditions are applied to theequation. A general solution to an nth order differential equation will have n arbitraryconstants. In the above example the order of the equation is 2 and there are twoconstants, A and B.

It is time to address boundary conditions and initial conditions. These conditionsconvert 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, whereboundary and initial conditions are incorporated into the differential equation in orderto obtain a solution.

10.2 Boundary Conditions and Initial Conditions.Once a general solution to an nth order differential equation is obtained (analytically),it is necessary to determine the n arbitrary constants. Given that there are nunknowns, n conditions are required that can be converted into n equations. Theseconditions must involve the unknown function and/or its derivatives up to n-1. Giventhe example above:

x)xcos(B)xsin(A)x(y 222 ++= ,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)

50 =)(y

000

=′==

)(ydx

dy

x

These conditions give the equations:B)(y == 50

Page 33: me205pfj

33

22222 +−= )xsin(B)xcos(Adx

dy, so 2200

0

+==′==

A)(ydx

dy

x

Therefore, B = 5 and A = -1. The solution to the problem is then:x2)x2cos(5)x2sin()x(y ++−= .

This solution satisfies both the governing differential equation and the initialconditions.Boundary conditions (conditions are specified at two different points).

50 =)(y22 =)/(y π

These conditions give the equations:B)(y == 50

4224 /A)/(y ππ +==Therefore, B = 5 and A = 2 - π/2. The solution to the problem is then:

x)xcos()xsin()/()x(y 225222 ++−= π .

In science and engineering boundary and initial conditions are very important physicalrequirements. This means that the function and its derivatives up to n-1, where n isthe order of the differential equation, have a physical meaning that must beunderstood in order to solve a differential equation. For example, for the beambending equation used earlier,

EI

)x(w

dx

vd−=

4

4

,

the function v is displacement, i.e., distance the beam deflects due to the load, the firstderivative is the slope of the beam, the second derivative is related to the bendingmoment 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 usingthese derivatives. A simpler example will be used to illustrate this point. Considerdetermining the position of a ball as a function of time, given that the ball is throwneither straight up or down from some position at some time. The governingdifferential equation, which neglects air resistance, is

gdt

yd−=

2

2

,

where y is positive upward and measures position in feet relative to some fixedreference point, say the ground, t is time in seconds, and g is the gravitationalconstant, 32.2 ft/sec2. The general solution to this equation is

BAtgt)t(y ++−= 2

2

1.

Note the homogeneous (At + B) and particular (-gt2/2) solutions that make up thegeneral solution. In order to predict position, more information is needed since A andB are arbitrary constants. Clearly one cannot predict position as a function of time ifthe position and speed the ball had when it was thrown are not known. Theseconditions translate into

00 y)(y = (This specifies the position from where the ball was thrown)

Page 34: me205pfj

34

00

0 vdt

dy)(v

t

===

(This specifies the speed of the ball when it was thrown)

With these two conditions the following solution is determined:

002

2

1ytvgt)t(y ++−= ,

i.e., the constants A and B have physical meaning, A is the initial speed and B is theinitial position. The example can also be posed as a boundary value problem using thefollowing conditions:

00 y)(y = (This defines the position from where the ball was initially thrown)

11 y)t(y = (This defines the position of the ball at time t1).The solution to this case is:

012111 2

1yAtgty)t(y ++−== , so

1

2101 2

1

t

gt)yy(A

+−= .

This gives

01

2101

2 2

1

2

1yt

t

gt)yy(gt)t(y +

+−+−= .

Another way of thinking about this case, is how fast must the ball be thrown at t = 0such that the ball will arrive at a certain place (y = y1) at a certain time (t = t1). Theanswer is the constant A. A boundary value problem can be solved by a “shootingmethod,” which makes use of an initial value solution or method. In this procedureone can guess the value of A until the condition 11 y)t(y = is met. This will bediscussed later within the context of an example.

10.3 The Euler Method – Numerical Solution of Differential EquationsNext consider the numerical solution of first order differential equations of the initialvalue type. The general case is the following:

)y,x(fdx

dy= with 00 y)(y = ,

where the known function f = f(x,y) can be linear or nonlinear and y0 is known as aninitial value. Start with the simplest approximation of the first derivative, the firstorder, forward difference formula

)h(Oh

)x(y)x(y

dx

dy)x(y ii

xxi

i

+−

==′ +

=

1 . (10.1)

Therefore, one can predict the value of y at x1 as follows:

)y,x(fh

yy

h

)x(y)x(y)x(y 00

01010 =

−=

−≅′

which gives)y,x(hfyy 0001 += .

Note that this approximates y1 using only information at x0. By successive applicationof this formula, any value of yi can be predicted. For example,

)y,x(hfyy 1112 += ,

Page 35: me205pfj

35

leading to)y,x(hfyy iii1i +=+ . (10.2)

This is the Euler method, which is the simplest of many methods. Now consider theexample of the parachute problem from pages 14 – 18 of Chapra and Canale. Thegoverning first order differential equation is

vm

cg

dt

dv−= , (10.3)

subject to the initial condition,00 =)(v .

For this case the function, f(t,v) = g – (c/m)v. The exact solution of (10.3) is

[ ]t)m/c(ec

gm)t(v −−= 1 .

Applying the Euler method (10.2) to the governing equation (10.3) leads to theapproximation,

−+=+ iii v

m

cghvv 1 .

Successive application starting from t = t0 = 0 gives v(t) = v(ti) =vi for any t. This isprogrammed 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:nvn(i)=vn(i-1)+h*(g-c/m*vn(i-1)); % This is the Euler approximation.endplot(t,v,tn,vn)legend(‘Exact', [’Numerical for h = ‘num2str(h)],2)title(‘Parachute Problem'); xlabel('time (seconds)'); ylabel('velocity (m/s)')

Page 36: me205pfj

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), thefollowing formula is used:

[ ]43211 226

kkkkh

yy ii ++++=+ , (10.4)

where)y,x(fk ii=1

++= hky,hxfk ii 12 2

1

2

1

++= hky,hxfk ii 23 2

1

2

1

)hky,hx(fk ii 34 ++= .

Again the important point is that the value of y at xi+1 is being determined frominformation at xi. As long as the initial value of y is known, one can start the methodto obtain y1 from x0 and y0. Then successive application gives any y > y0.

10.4 Second Order Differential EquationsAs an example of a second order differential equation, consider the case of acantilever beam with large deflections due to a concentrated force F at the right end asshown in Figure 10.1. The governing nonlinear differential equation is:

)cos(EI

F

ds

θ=

2

2

, (10.5)

where EI is the bending stiffness of the beam, θ is the slope of the beam and s is avariable measured along the length of the deformed shape of the beam. The boundaryconditions are:

00 =)(θ (The wall forces the bean to extend straight out from the wall)

0==Lsds

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

Page 37: me205pfj

37

(10.4) and (10.5) to

02

2

2

=− )cos(EI

FL

dt

θ(10.7)

subject to the boundary conditions,

0001

===tdt

d,)(

θθ . (10.8a,b)

The nondimensional parameter FL2/EI takes into account load (F), geometry (L) andmaterial properties (EI). This differential equation presents two significant changes fromthe parachute example:1) it is a boundary value problem instead of an initial valueproblem, and 2) it is second order differential equation instead of first order. These twoissues are addressed below.

The mathematical problem given by (10.7) with (10.8a,b) can also be posed as an initialvalue 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 isunknown. 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 orderequation (10.7) is converted to a set of two first order differential equations by using thestate 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

β 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 beapproximated as

iii

tt hdt

d

i

βθθθ

=−

≅ +

=

1

)cos(EI

FL

hdt

di

ii

tt i

θβββ 2

1 =−

≅ +

=

Page 38: me205pfj

38

which lead to the approximations

ii1i hβ+θ=θ +

)cos(EI

FLh iii θββ

2

1 +=+ .

Successive application starting from t = t0 = 0 gives θ(t) = θ(ti) =θi and β(t) = β(ti) = βi

for any t. This is programmed in MATLAB below. There is no exact solution for thiscase.

% 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:ntheta(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));endbc=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 willnot 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 specialvalue of q that makes this derivative zero must be determined using, for example, thebisection method. The plots below are obtained from the above program using q = 0.7and 0.8, which provide an upper and lower bound for the correct value of q.

Page 39: me205pfj

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 performediterations=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));endbc=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 < 0qu = qr; % Root is between ql and qr so qu is replaced by qrelseql = qr; % Root is between qr and qu so ql is replaced by qrbcl=bc;end

endend % This is the end of for loop for bisection method. Next print results.

if converge < 0fprintf(’The program did not converge after %2.0f iterations’,iterations)elsefprintf(‘\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

Page 40: me205pfj

40

The output from the above program is presented below. Note how β(1) = 0 for thevalue 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 FL2/EI = 3.0). The slope of the beam at the end, i.e., θ(1), is used to evaluateconvergence.

Step size hfor 1storder

solution

Value ofq = d/L

FL2/EI = 3.0

Slope ats = L, θ(1)

0.1 0.737706 -1.0645350.05 0.741411 -1.0248420.01 0.744709 -0.9937190.005 0.745142 -0.9898640.001 0.745492 -0.9867860.0005 0.745536 -0.9864010.0001 0.745571 -0.9860940.00005 0.745575 -0.986055

This shows that to obtain three significant figures of accuracy in the slope at the endof the beam, a step size of approximately h = 0.0001 should be used. Threesignificant figures in q is slightly easier to obtain.

10.5 Differential Equations Using MATLABMATLAB has numerous built-in functions that perform the successive applications offormulas such as (10.2) and (10.4). For example, the beam problem can be solvedusing 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

Page 41: me205pfj

41

order differential equations. Once again using the state variable approach (see Palm,page 463),

)t()t(y1 θ=

dt

dy

dt

d)t(y 2

2 ==θ

,

leads to the converted set of first order equations,

21 y

dt

dy=

)ycos(EI

FL

dt

dy1

22 = ,

with initial conditions,0)0(y1 =

L

d

EI

FL)0(y

2

2 −= .

The second condition will be enforced by finding the value of d that gives y2(1) = 0.

Step 2.Code the system of first-order ODEs in MATLAB as the function rhsproj1function 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., y1(0) and y2(0),are known. However, in this case the value of y2(0) is unknown since d is unknown.Therefore its value must be guessed at the start, and subsequently refined in an iterativeprocess. With each successive iteration an improved guess is used until finally theboundary condition at t = 1 (s = L) is met. As mentioned earlier, this amounts to solvingthe 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 MATLABdo 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 scalarvalue of

1t1t

12 dt

d

dt

dy)1(y

==

==θ

as an output. Such a function is defined below, where “b” is the output. Note that thesystem 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 FL2/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 4th and

Page 42: me205pfj

42

5th order Runga-Kutta formulas. These formulas integrate the system of ODEs describedin 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 FL2/EI = 3.0.clcformat long gq=0.95; % initial guessD=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 qplot(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, 4th Ed., McGraw-

Hill, Inc., 2002.2. W.J. Palm, Introduction to MATLAB 6 for Engineers, McGraw-Hill, Inc., 2001.

Page 43: me205pfj

43

Appendix A - Determinant of a Matrix

2×2: 211222112221

1211 aaaaA]Adet[,aa

aaA −==

=

3×3:

=

333231

232221

131211

aaa

aaa

aaa

A .

The determinant of a 3 by 3 matrix consists of the sum of three 2 by 2 determinants. Thegeneral formula below shows that there are six choices for the three 2 by 2 “submatrices.”

)Adet()(a]Adet[ ijji

jij

+

=−∑= 1

3

1 for 321 or,i =

or )Adet()(a]Adet[ ijji

iij

+

=−∑= 1

3

1 for 321 or,j = .

The notation det(Aij) corresponds to the determinant of a sub matrix (or minor) of A, thatis obtained by deleting the ith row and jth column of A. Consider the example for the topequation with i = 3.

)Adet()(a]Adet[ jj

jj 3

33

13 1 +

=−∑=

2221

12113333

2321

13112332

2322

13121331 111

aa

aa)(a

aa

aa)(a

aa

aa)(a +++ −+−+−=

n×n:

=

nnninn

knkikk

ni

ni

aaa

aaaa

aaaa

aaaa

]A[

LL

MMMMMM

LL

MMMMMM

LL

LL

21

21

222221

111211

The determinant of an n×n matrix consists of the sum of n (n-1)×(n-1) determinants. Thedeterminant of each (n-1)×(n-1) determinant consists of the sum of n - 1 (n-2)×(n-2)determinants, etc. The general formula is:

)Adet()(a]Adet[ ijji

n

jij

+

=−∑= 1

1 for any i from 1 to n.

or )Adet()(a]Adet[ ijji

iij

+

=−∑= 1

3

1 for any j from 1 to n.

Page 44: me205pfj

44

Example for the determinant of a 4 by 4.

=

=

1300

0013

1200

0021

44434241

34333231

24232221

14131211

aaaa

aaaa

aaaa

aaaa

A

The general formula is )Adet()(a]Adet[ ijji

jij

+

=−∑= 1

4

1 for 4321 or,,i = .

Let i = 1, which corresponds to expanding across the first row of A.

)Adet()(a]Adet[ jj

jj 1

14

11 1 +

=−∑=

444341

343331

242321

2112

444342

343332

242322

1111 11

aaa

aaa

aaa

)(a

aaa

aaa

aaa

)(a ++ −+−=

434241

333231

232221

4114

444241

343231

242221

3113

aaa

aaa

aaa

)1(a

aaa

aaa

aaa

)1(a ++ −+−+

Using numbers from above,

[ ]{ } [ ]{ } 5131232131211

130

003

120

2

130

001

120

1 −=×−×−−×−×−=−= )()()()()()(]Adet[

Similarly, if the determinant is expanded across the second row.[ ]{ } [ ]{ } 5321131321112 −=×−×+×−×−= )()()()(]Adet[

Expand across the third row.[ ]{ } [ ]{ } 5311211311223 −=×−×−×−×= )()()()(]Adet[

Expand across the fourth row.[ ]{ } [ ]{ } 5321121321113 −=×−×−+×−×−−= )()()()(]Adet[

Expand down the first column.[ ]{ } [ ]{ } 5311223311211 −=×−×+×−×−= )()()()(]Adet[

Expand down the second column.[ ]{ } [ ]{ } 5311211311232 −=×−×−×−×−−= )()()()(]Adet[

Expand down the third column.

Page 45: me205pfj

45

[ ]{ } [ ]{ } 5321113321112 −=×−×−−×−×−= )()()()(]Adet[

Expand down the fourth column.[ ]{ } [ ]{ } 5321121321131 −=×−×−+×−×= )()()()(]Adet[

Page 46: me205pfj

46

Appendix B - Gauss Elimination (Follows Chapra and Canale, pages 238 – 255).

Solution of the system of equations, [A]{x} = {b}, where

=

nnni2n1n

knki2k1k

n2i22221

n1i11211

aaa

aaaa

aaaa

aaaa

]A[

LL

MMMMMM

LL

MMMMMM

LL

LL

,

=

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 righthand side vector, {b}. The basic goal of Gauss Elimination is to row reduce theoriginal matrix to an upper triangular form. Then back substitution can be applied toobtain the solution vector, {x}. Each step in the process uses one equation to obtainzeros in a column below the diagonal element of this equation. In the first step, theequation along the top row is used to obtain zeros as shown below. In the second stepthe second equation will be used to obtain zeros below 22a′ , etc.

′′′

′′′

′′′

=

n

k

2

1

nnni2n

knki2k

n2i222

n1i11211

n

k

2

1

nnni2n1n

knki2k1k

n2i22221

n1i11211

b

b

b

b

aaa0

aaa0

aaa0

aaaa

b

b

b

b

aaaa

aaaa

aaaa

aaaa

Ab

M

M

LL

MMMMMM

LL

MMMMMM

LL

LL

M

M

LL

MMMMMM

LL

MMMMMM

LL

LL

The coefficients, kia′ , are obtained by addition of the first and kth equation as follows:

{ }11121211111

1 bxa...xa...xaxaa

annii

k =+++++−

{ }knknikikk bxa...xa...xaxa =++++++ 2211

{ }knknikik bxa...xa...xax ′=′++′++′+×= 2210 ,

giving the formula for all prime elements, where k corresponds to the row and i to thecolumn,

11

11

11

11 a

babb,

a

aaaa kkk

ikkiki −=′−=′ for n,...,1iandn,...,2k == .

Page 47: me205pfj

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, thatreplaces all occurrences of “1” in the above expressions. Letting the matrix Ab = Aaugmented with b,

,ab

abababba

jj

jikjkiki −=′ for each j = 1,…,n –1, 1n,...,jiandn,...,1jk +=+= . (1)

In this expression, i is for the column, k is for the row, and j is for the diagonal elementbelow which the column of zeros is obtained. Back substitution is then used to obtain{x}:

−= ∑

+=i

n

1kikik

kkk xab

a

1x , where k ranges from n - 1 to 1 and xn = bn/ann. (2)

There are problems with this “naive form” of Gauss elimination. These problems includedivision 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 thecoefficient matrix and then partial pivoting rows at each stage (Example 9.10 in Chapraand Canale). Naive Gauss elimination is programmed below using MATLAB. Then onthe 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\bab = [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 endend

% Back substitution.x(n) = ab(n,n+1)/ab(n,n); % This gets the first value of xfor 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:nsum = sum – ab(k,i)*x(i); % x(i) must be known for i > kend

x(k) = sum/ab(k,k);enddisp(‘The Gauss Elimination solution is:’)

Page 48: me205pfj

48

x’

Page 49: me205pfj

49

MATLAB output that encounters division by zero in naive form of Gausselimination (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 NaNThe 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 scaledterm = max(abs(ab(kscale,j:n))); % Gives max. [A] value in kscaleth row.

for iscale = j:n+1ab(kscale,iscale) = ab(kscale,iscale)/term; % Scales kscaleth rowend

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+1temp = 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 endend

Page 50: me205pfj

50

% Back substitution to obtain {x}x(n) = ab(n,n+1)/ab(n,n); % This gets the first value of xfor kforward = 1:n-1k = n – kforward; % k moves from the bottom row to the top row.sum = ab(k,n+1);

for i = k+1:nsum = sum – ab(k,i)*x(i); % x(i) must be known for i > kend

x(k) = sum/ab(k,k);enddisp(‘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 coefficientmatrix. 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 1st and 3rd rows). Interchange rows to bringthe largest number in the jth column to the jth 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 Gausselimination. Use Eqn. 1 to update the elements for k = 2 and 3. For each k , i rangesfrom 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

Page 51: me205pfj

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 = 3only 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

Page 52: me205pfj

52

Some other uses of Gauss elimination and additional methods associated with linearsystems:• 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 ofChapra and Canale).

• Given an upper triangular form obtained from Gauss elimination, it is easy to obtainan “LU decomposition” of [A] where

]U][L[]A[ = .The square matrix [U] is the upper triangular form resulting from Gauss eliminationand [L] is a lower triangular form with ones along the diagonal and lower triangularelements easily obtained from the Gauss elimination process (see Chapra andCanale, pages 266-267). An important use of this decomposition is for the efficientsolution of the same set of linear equations with different right hand sides, i.e.,[A]{x} = {b1} and [A]{x} = {b2}. 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 ofChapra and Canale) is a continuation of Gauss elimination from an uppertriangular form to the identity matrix. After applying Gauss-Jordon to [A I] theresulting matrix is [I A-1]. However, a more efficient way is to use LUdecomposition to obtain the inverse. This methods takes advantage of thedecomposition for multiple right hand sides as illustrated in the example in Chapraand Canale, pages 274-276

• There are cases where iterative methods are preferable to direct methods for thesolution of a linear system of equations. Typical methods include Jacobi andGauss-Seidel, with Gauss-Seidel being the most popular (see Chapra and Canalepages 289-296). The primary application of iterative methods is for very largesystems of equations, for which elimination methods are more likely to suffer fromproblems 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 beexpected to occur. MATLAB has a command, cond(A) that evaluates thecondition number according to properties of a matrix (

2A norm) described on

page 279 of Chapra and Canale. Roughly speaking, if 10c = cond(A), up toapproximately c digits of accuracy can be lost due to round-off error. See theexample on page 281 of Chapra and Canale.