Download as pdf or txt
Download as pdf or txt
You are on page 1of 415

AN INTRODUCTION TO NUMERICAL

METHODS USING MATHCAD

Mathcad Release 14

Khyruddin Akbar Ansari, Ph.D., P.E.

Professor of Mechanical Engineering


School of Engineering and Applied Science
Gonzaga University
Spokane, Washington
Copyright 2007 Khyruddin Akbar Ansari.
All rights reserved

No part of this book may be used or reproduced or distributed in any form , or by any means or
stored in a database or retrieval system, without the prior written permission of the author .

Mathcad screenshots are reprinted by permission of Parametric Technology Corporation (PTC).


Mathcad is a registered trademark of PTC, 140 Kendrick Street, Needham, MA 02494. All rights
reserved
Dedicated to my dear wife, Fatima
ABOUT THE AUTHOR

Khyruddin Akbar Ansari, who has been a professor of mechanical engineering at Gonzaga
University in Spokane, Washington, since 1986, received his bachelor’s degree in electrical
engineering from Osmania University, Hyderabad, India in 1964, his master’s degree in mechanical
engineering from the University of California at Berkeley in 1965 and his Ph.D. degree in
engineering mechanics from the University of Texas at Arlington in 1972. He did his doctoral
thesis work in the area of nonlinear vibrations of rotating blades under the guidance of the late
Professor Nils O. Myklestad. Professor Ansari has many years of varied industrial , teaching and
research experience, having been associated in the past with such organizations as Bell Helicopter,
Westinghouse Electric , Bell Aerospace, Brown and Root, King Fahd’s University of Petroleum
and Minerals and Battelle Pacific Northwest Laboratories .

At Gonzaga University, Professor Ansari is currently involved in the teaching of courses in the
areas of engineering mechanics, numerical methods, advanced engineering mathematics, vibrations,
system dynamics and controls , and machine design. His research, which has been varied, has
resulted in several published articles and contributions to books including chapters in the
Encyclopedia of Fluid Mechanics and Developments in Offshore Engineering.

Professor Ansari is a member of the American Society of Mechanical Engineers and the American
Society for Engineering Education. He is also a registered professional engineer.
Table of Contents: i

TABLE OF CONTENTS

Preface v

1. Basics of Mathcad. 1

1.1 Introduction 1
1.2 The Mathcad Screen 1
1.3 Exact Answers 3
1.4 Variables, Functions and Live math. 3
1.5 Feedback 4
1.6 Graphics 5
1.7 Graphing of Functions and Plotting of Data 5
1.8 Animations 7
1.9 The Mathcad Tutorials 7
1.10 Advantages of Mathcad 7
1.11 Computations in Mathcad 10
1.12 The Mathcad Window, Toolbars and Palettes 13
1.13 Mathcad Regions 15
1.14 Entering Math and Text 16
1.15 Mathcad Worksheets,Templates and Styles 17
1.16 Defining Variables 18
1.17 Defining Functions in Mathcad 19
1.18 Building and Editing Mathematical Expressions 21
1.19 Defining Range Variables 23
1.20 Defining Vectors and Matrices 24
1.21 Creating Graphs 28
1.22 Formatting Math, Text and Results 35
1.23 Using Units 39

2. Introduction to Numerical Methods. 43

2.1 The Use of Numerical Methods in Science and Engineering 43


2.2 Comparison of Numerical Methods with Analytical Methods 43
2.3 Sources of Numerical Errors and their Computation 44
2.4 Taylor Series Expansion 44
Problems 49

3. Roots of Equations. 53

3.1 Introduction 53
3.2 Methods Available 53
3.3 Bisection Method 53
3.4. The Regula Falsi or the False Position Method 58
3.5 Newton-Raphson Method 65
3.6 Use of Mathcad’s root and polyroots Functions 71
ii AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

3.7 Secant Method 72


3.8. Method of Successive Substitution 78
3.9 Multiple Roots and Difficulties in Computation 80
3.10. Solution of Systems of Nonlinear Equations 84
3.11 Solving Systems of Equations using Mathcad's
Given and Find Functions 86
3.12 Applications in Root-Finding 87
3.12.1 Maximum Design Load for a Column 87
3.12.2 Natural frequencies of Vibration of a Uniform Beam 89
3.12.3 Solving the Characteristic Equation in Control
Systems Engineering 91
3.12.4 Horizontal Tension in a Uniform Cable 93
Problems 96

4. Matrices and Linear Algebra. 103

4.1 Basic Matrix Operations 103


4.2 Use of Mathcad in Performing Matrix Operations 105
4.3 Solution of Linear Algebraic Equations by Using the Inverse 108
4.4 Solution of Linear Algebraic Equations by Cramer’s Rule 110
4.5 Solution of Linear Algebraic Equations Using the Function lsolve 112
4.6 The Eigenvalue Problem 114
4.7 Solving the Eigenvalue Problem with Mathcad 116
4.8 Application of the Eigenvalue Problem to Vibration Engineering 117
4.9 Application of the Eigenvalue Problem to Stress Analysis-
Determination of Principal Stresses and Principal Directions 125
4.10 Repeated Roots in the Determinantal Equation 128
4.11 Solution of Nonlinear Simultaneous Equations 131
Problems 134

5. Numerical Interpolation. 141

5.1 Linear Interpolation 141


5.2 The Method of Undetermined Coefficients 141
5.3 The Gregory-Newton Interpolating Polynomial 144
5.4 Interpolation Using Finite Differences 149
5.5 Newton’s Method Utilizing Finite Differences 151
5.6 The Lagrange Interpolating Polynomial 155
5.7 Interpolation Using Linear, Quadratic and Cubic Splines 158
5.8 Interpolation with Mathcad 159
5.9 Applications in Numerical Interpolation 168
5.9.1 Stress-Strain data for Titanium 168
5.9.2 Notch Sensitivity of Aluminum 169
5.9.3 Speech Interference Level 172
5.9.4 Load-Deflection Data for Elastomeric Mounts 175
Problems 177
Table of Contents: iii

6. Curve-Fitting. 183

6.1 The Need to Fit a Function to Measured Data 183


6.2 The Method of Least Squares. 184
6.3 Straight Line Regression 185
6.4 Curve-Fitting with a Quadratic Function 188
6.5 Curve-Fit with a Power Function 191
6.6 Curve-Fitting with an Exponential Function 194
6.7 Curve-Fitting with a Linear Combination of Known Functions 199
6.8 Curve-Fitting with Polynomials. 203
6.9 Use of Mathcad's Regression Functions for Curve-Fitting 207
6.9.1 Linear Regression with Mathcad 207
6.9.2 Nonlinear Regression with Mathcad 209
6.9.3 Use of the Function linfit 211
6.9.4 Use of the Function genfit 213
6.9.5 Use of the Mathcad Functions logfit, lnfit, pwrfit and expfit 215
6.9.6 More Examples with Mathcad 220
6.10 Applications in Curve-Fitting 236
6.10.1 Fatigue Failure Curve for Loading in the Finite Life Range 236
6.10.2 Temperature Response of an Object Placed in a
Hot Stream of Air 239
6.10.3 The Effect of Operating Temperature on the Strength of a
Mechanical Element 242
6.10.4 Drop-Testing of Packaged Articles 245
Problems 248

7. Numerical Differentiation 255

7.1 Introduction to Numerical Differentiation and


the Use of the Mathcad Derivative Operators 255
7.2 Method of Finite Differences 255
7.3 Interpolating Polynomial Method 259
7.4 Applications in Numerical Differentiation 262
7.4.1 Determination of Velocities and Accelerations
from Given Displacement Data 262
7.4.2 Determination of Shock Absorber Parameters, and Damper
and Spring Restoring Forces from Given Vehicle Displacement Data 266
Problems 271

8. Numerical Integration 277

8.1 Introduction to Numerical Integration and


the Use of the Mathcad Integral Operator 277
8.2 The Interpolating Polynomial Method 279
8.3 Trapezoidal Rule 280
8.4 Simpson’s Rules 283
iv AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

8.4.1 Simpson’s One-Third Rule 283


8.4.2 Simpson’s Three-Eighth Rule 286
8.4.3 Limitations of Simpson’s Rules 287
8.5 Romberg Integration 288
8.6 Applications in Numerical Integration 301
8.6.1 Centroid of a Rod Bent into the Shape of A Parabola 301
8.6.2 Moment of Inertia of a Beam of Semi-Elliptic Cross Section 302
8.6.3 Launch of a Projectile 303
8.6.4 Large Oscillations of a Simple Pendulum 304
Problems 306

9. Numerical Solution of Ordinary Differential Equations. 311

9.1 Introduction 311


9.2 Taylor Series Method 312
9.3 Euler’s Method 317
9.4 Modified Euler’s Method 323
9.5 Runge- Kutta Methods 329
9.5.1 Fourth-Order Runge-Kutta Method 329
9.5.2 Mathcad Solutions to a First-Order Differential Equation 335
9.6 Systems of Ordinary Differential Equations 341
9.7 Solution of Higher-Order Ordinary Differential Equations 349
9.8 Boundary-Value Problems and the Shooting Method 358
9.9 Applications in Numerical Solution of Ordinary Differential Equations 363
9.9.1 Response of an Electric R-L Circuit to a Unit-Step Voltage Input 363
9.9.2 Deflection Curve of a Cantilevered Beam with a Uniformly
Distributed Load 364
9.9.3 Temperature Response of a Solid Steel Ball Placed in a Hot
Stream of Air 366
9.9.4 Nonlinear Vibration of a Simple Pendulum 367
9.9.5 Transient Vibration of a Spring-Mass-Damper System
Excited by a Pulse Function 370
9.9.6 Nonlinear Vibration of a Damped System with a
Hardening Spring 373
9.9.7 Temperature Distribution in the Wall of a Pipe
Carrying a Hot Fluid 378
9.9.8 Response of an R-L Circuit with a Nonlinear Resistor 382
9.9.9 The Effect of Damping on the Step Response of a
Second-Order Control System 384
Problems 386

Bibliography 399
Index 401
Preface: v

PREFACE

This book, which is designed to be used in a first numerical methods course in a computer science,
mathematics, science , engineering or engineering technology curriculum, has been developed from
notes that I generated over several years in the process of teaching a one- semester / three-credit
sophomore level course in computer methods at Gonzaga University. It should also provide a
reliable source of reference material to practicing engineers and scientists and in other junior and
senior-level courses such as machine design, vibrations, system dynamics and controls, where
Mathcad can be effectively utilized as a software tool in problem solving. A principal goal of this
book , then, is to furnish the background needed to create Mathcad documents for the generation
of solutions to a variety of problems. Specific applications involving root-finding, interpolation,
curve-fitting, matrices, derivatives, integrals and differential equations are discussed and the broad
applicability of Mathcad demonstrated. The material contained herein should be easily grasped by
students with a background in calculus, elementary differential equations and some linear algebra,
and when utilized in a mathematics, science, engineering, computer science or engineering
technology course sequence, it should give a good basic coverage of numerical methods while
simultaneously demonstrating the general applicability of Mathcad to problem- solving and
solution documentation.

The first chapter discusses Mathcad basics while the second gives a general introduction to
numerical methods in science and engineering and presents the computation of numerical errors
along with the Taylor series as a basis of approximation in numerical analysis. Chapter 3 offers a
coverage of the popular methods of finding roots of equations such as Bisection, Regula-Falsi,
Newton-Raphson, Secant and Iteration . Solutions to systems of nonlinear algebraic equations
obtained by the iteration method are also presented. The applicability of root-finding to practical
problems such as those that occur in column design, vibration analysis and control systems
engineering is demonstrated. In the fourth chapter, matrices and linear algebra are dealt with along
with the solution of eigenvalue problems. Applications to the field of vibration engineering and
stress analysis are also discussed. Chapters 5 and 6 focus on numerical interpolation and curve-
fitting respectively. The common techniques of interpolation and the functions generally resorted
to for curve-fitting of data are covered quite thoroughly. The applicability of these to the analysis
of scientific and engineering data such as stress-strain, load-deflection and fatigue failure is
presented. Chapters 7 and 8 address numerical differentiation and integration. The concept of finite
differences is introduced and the common methods of differentiation and integration , such as the
interpolating polynomial method, trapezoidal and Simpson’s rules and Romberg integration are
covered. Applications of numerical differentiation and integration such as determination of
velocities and accelerations from given displacement data and computation of the moment of inertia
of a cross section are included. The last chapter, which is the ninth one, deals with coverage of
numerical solution of ordinary differential equations, and although it does not discuss each and every
technique that is available, it does focus on the popular ones such as Taylor series method, Euler,
modified Euler and Runge-Kutta methods. A variety of practical applications of numerical
procedures are included in this chapter, ranging from the response of an electric circuit to an input
vi: AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

voltage to the effect of damping on the response of a control system. Partial differential equations
is a topic that is typically dealt with in detail only in graduate level courses and , therefore, I did not
think it very appropriate to introduce it in this book which is really aimed at the needs of
undergraduate students. Mathcad documents are generated and examples showing the applicability
and use of Mathcad are presented throughout the book. Wherever appropriate, the use of Mathcad
functions offering shortcuts and alternatives to otherwise long and tedious numerical solutions is
also demonstrated. At the end of every chapter is included a set of problems to be solved covering
the material presented. A solutions manual that provides solutions to these exercises can be made
available to instructors.

Although Mathcad is a very powerful and versatile tool that combines text, graphics and equations
in a single worksheet, several figures and sketches in the book had to be done using AutoCad. The
help provided by Carlos G. Alfaro earlier in generating these figures and Ryan Lambert later in
making some additions and modifications to them is gratefully acknowledged. I am also thankful
to those of my students who have so kindly made useful suggestions and pointed out errors and
mistakes in the manuscript. Their input and feedback have certainly been helpful. Last but not least,
I am indebted to my wife, Fatima , for her patience, support and understanding during the
preparation of this book. Without her encouragement, its completion would still be wishful thinking.

Users of this book are welcome to e-mail any comments and suggestions to me at
ansari@gonzaga.edu , and, of course, I will be pleased to address these in subsequent versions /
editions.

Khyruddin . Akbar Ansari


October 2007
Chapter 1: Basics of Mathcad 1

CHAPTER 1

BASICS OF MATHCAD

1.1 INTRODUCTION

Mathcad 14 is the first global release of Mathcad presented by Parametric Technology


Corporation (PTC) with many significant enhancements since it acquired Mathsoft in April 2006.
This is a very powerful software that offers a wide variety of tools in the solution of problems
requiring the use of numerical analysis. New ways of using Mathcad are constantly being found
leading to overwhelming possibilities. The sections that follow give an overview of the features in
Mathcad 14 and will help the novice to get introduced to its problem-solving capabilities.
Mathcad 14 has a number of improvements and added capabilities designed to increase the
productivity of the user as well as Web connectivity. There is a wealth of information that can
be found by clicking on "Help" and going to "Tutorials" , where you can get an overview and a
quick tour of Mathcad , see the new features available in Mathcad 14, learn the fundamental
skills for working in Mathcad, and obtain on-line help if needed. By clicking on "Help", the
Mathcad Quicksheets can be accessed, which are ready-made recipes for performing a variety
of tasks in Mathcad. E-books and Reference Tables, user-contributed files, samples,
animations and graphs along with other example files for using and extending Mathcad can also
be accessed. By choosing "User Forums" under the "Help" menu, questions can be posted
for other Mathcad users. The Mathcad web site can always be accessed for updates, technical
support information and user files , and Mathcad documents can now be saved on the Web or
in Microsoft Word using Rich Text Format (RTF).

1.2 THE MATHCAD SCREEN


Mathcad has a full screen numerical and symbolic calculator, which is essentially a blank
workspace that you see when you open up a new Mathcad document. To use Mathcad's
calculating capabilities, type the expression you want to evaluate and then type an equals sign
to get the result:

57
435 + = 437.375
24

As shown in Figure 1.1. the palette buttons open up palettes that include a host of math
operations . An expression such as the following can be assembled quite easily.

⎛ π⎞
sin ⎜ ⎟
⎝ 8 ⎠ ⋅ e95.2 = 1.952 × 10 40
ln ( 76.5 )
2 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 1.1 The Math palettes


Chapter 1: Basics of Mathcad 3

Matrix arithmetic, calculus operations such as differentiation and integration, and essentially all
of the mathematical operations normally required in engineering and scientific analysis can be
easily done in Mathcad. Some examples are given below.

7
⌠ 2x 5
⎮ e ⋅ sin ( 4 ⋅ x ) dx = 2.641 × 10

2

−1
⎛⎜ 5 2 7 ⎞⎟ ⎛ −0.168 −0.072 0.304 ⎞ 7 + 9i
⎜ ⎟ = 0.128 + 0.761i
⎜4 8 9 ⎟ = ⎜ −0.312 0.152 0.136 ⎟ 13 − 7i
⎜7 3 6 ⎟ ⎜ −3 ⎟
⎝ ⎠ ⎝ 0.352 8 × 10 −0.256 ⎠

1.3 EXACT ANSWERS

If exact answers are needed in terms of fractions and radicals instead of decimals, the
"symbolic equals sign" (-->) on the fourth math palette can be used to obtain exact answers.
Some examples are given below.

59 9341
546 + →
17 17

⎛π⎞
sin ⎜ ⎟
⎝ 16 ⎠ ⋅ e90.5 → 9.0751625112586372168e37
ln ( 75.6)

⎛⎜ 13 − 1 − 6 ⎞⎟
−1 ⎜ 7 7 7 ⎟
⎛⎜ 5 2 3 ⎞⎟ ⎜ 61 2 33 ⎟
⎜4 8 9 ⎟ → ⎜ − − ⎟
⎜9 3 5 ⎟ ⎜ 7 7 7

⎝ ⎠ ⎜ 60 3 32 ⎟
⎜⎝ − 7 7 7 ⎟⎠
7 4 14 4 14
⌠ 2⋅ x cos ( 8) ⋅ e cos ( 28 ) ⋅ e e ⋅ sin ( 8) e ⋅ sin ( 28 )
⎮ e ⋅ sin ( 4 ⋅ x ) dx → − − +
⌡ 5 5 10 10
2

1.4 VARIABLES, FUNCTIONS AND LIVE MATH

When there is a need to evaluate an expression for several differen t inputs, the desired
variables and functions can be defined and calculated like built-in constants and functions.
This computational feature in Mathcad is called live math.
4 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

For example, if the area of a circle for various values of the radius is to be calculated, define a
radius variable r and give it a value.

r := 8

The := symbol can be inserted by typing a colon. Evaluate the area as

2
π ⋅ r = 201.062

The numerical value of r can now be changed to some other number . Click anywhere outside
the definition to recalculate the area . . This is live math .

If this area computation is to be usable in other parts of the worksheet, an area function must
be defined.

2
area ( r ) := π ⋅ r

Use this function as needed to calculate an area . Length units for the radius can also be
assigned as shown..

2
area ( 60 ⋅ cm) = 1.131 m

Live math works for symbolic answers as well. Put in a function , obtain the derivative of the
function, and then change the function to some other function. Click outside the definition to
see how Mathcad redoes the derivative symbolically.

1.5 FEEDBACK
There is continuous feedback in Mathcad. As you edit math expressions, the status line will
provide advice and information . When Mathcad cannot understand something, it will color the
expression red. Clicking on this red expression will give an error message indicating what is
wrong. To see an example, click on the expression below.

p+ q=

In the above, variables p and q were not defined. Therefore, Mathcad tells us so. Also,
Mathcad calculates from top to bottom and left to right.

p := 5 q := 9 <--This works ---> p + q = 14


Chapter 1: Basics of Mathcad 5

F ( 4) = <-This does not work

2
F ( x ) := x − 4

1.6 GRAPHICS

It has been demonstrated that Mathcad can be used as a calculator and as an equation solver.
It can also be used as a versatile visualization tool that supports a full set of plot types, an
animation facility, as well as simple image processing.

1.7 GRAPHING OF FUNCTIONS AND PLOTTING OF DATA

9
To create a plot in Mathcad, type the expression you would like to plot, say t . Then click the
graphing button on your palette bar (See Figure 1.2 ) to bring up the graphing palette and click
the upper left button for an XY plot. Click outside the graph or press [Enter] to see the result:

5
1× 10

4
5× 10

9
t 0

4
− 5× 10

5
− 1× 10
−4 −2 0 2 4
t

Parametric plots can also be done easily . .

0.5

sin( 5⋅ t ) 0

− 0.5

−1
−1 − 0.5 0 0.5 1
cos ( 7 ⋅ t )
6 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 1.2 The graph palette


Chapter 1: Basics of Mathcad 7

. . . and like all Mathcad calculations, plots are also live, which means that changes can be
easily made.

1.8 ANIMATIONS
Mathcad can create and play short animation clips by using the built-in variable FRAME and
the Animation dialog box. Choose Animation, and then Record from the Tools menu to bring
up this dialog box Plots as well as numerical results can be animated, and the animation
played back at different speeds.

1.9 THE MATHCAD TUTURIALS

The Mathcad tutorials offer a step-by-step guide to building and editing expressions, creating
and formatting graphs, using Mathcad's wordprocessing capability to enter and format text,
working with units and utilizing Mathcad's built-in functions and operators. For a Mathcad
novice, this is a good place to begin. The Mathcad tutorials can be accessed as indicated in
Figure 1.3 by choosing Help and then Tutorials .

1.10 ADVANTAGES OF MATHCAD

Mathcad offers a great way to work with equations, numbers, text, and graphs. Unlike other
programs, Mathcad uses the language of mathematics and does mathematics in a very natural
way. It works like a scratchpad and pencil combination . Mathcad's on-screen interface is a
blank worksheet on which equations, graph data or functions can be entered and combined
with text -- anywhere on the page.

In Mathcad, an equation looks exactly as in a textbook :

2
−b + b − 4⋅a⋅c
x=
2⋅a

Mathcad's equations and graphs are live, which means that any data, variable, or equation can
be changed with Mathcad doing an instant recalculation and redrawing of the graphs.

Mathcad can solve a wide range of problems, numerically as well as symbolically, and
equations and data can be evaluated with 2D and 3D plots.

Mathcad Quicksheets are full-featured worksheets that offer ready-made Mathcad templates
that can be customized to perform a wide range of mathematical tasks from solving equations
to graphing and calculus. They also demonstrate how some of Mathcad's special features like
programming and animation can be used. Choose Help and then QuickSheets to use this
capability.
8 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 1.3 Accessing the Mathcad tutorials


Chapter 1: Basics of Mathcad 9

Mathcad's Electronic Books have a wealth of mathematical knowledge and reference


material -- all live - which can be dragged and dropped into your worksheets. In order to do
this, choose Help and then E-books .

Mathcad has proven to be an extremely remarkable , effective and powerful tool for the
solution of problems. A wide range of problems can indeed be explored, formulated,
analyzed and optimized, after which the best solution can be documented and presented.

By choosing Help and then User Forums, users can connect with and share their work
with colleagues and other professionals around the world , and collaboration is easy . A
host of forums can be accessed as shown in Figure 1.4.

Figure 1.4 Accessing the Mathcad Collaboratory


10 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

1.11 COMPUTATIONS IN MATHCAD

Figure 1.5 The Insert/ Function buttons in Mathcad


Chapter 1: Basics of Mathcad 11

Several mathcad palettes can be found on the palette strip at the top of the window that give
access to Mathcad's mathematical operators. A click in your worksheet will place a red
crosshair cursor. Math operators can now be placed in the worksheet via these palettes. To
access Mathcad's built-in functions, go to the Insert menu as shown in Figure 1.5, and
select Function , or click on the Insert Function button on the toolbar

The red crosshair

EXAMPLES

In the following examples, the number of decimal places required may be put in by choosing
Result from the Format menu and then selecting Number Format.

Use the Arithmetic Palette (See Figure 1.1)


8 , and type = to obtain the answer. The basic
2.645 ⋅ 10 −7
= 1.0426928428 × 10 <-- operations are +, -, *, and / which are
5 6
30007 + 5 available on the keyboard .

⎛ 4 ⋅ π⎞ = 0.514 <- Examples of built-in functions (See


log ( 1768.985 ) ⋅ sin ⎜ ⎟
⎝ 79 ⎠ Figure 1.5)

3 6 − 5i 3 3
[ 14.95 + ( 5.7i) ] + e = 1.999 × 10 + 4.024i × 10 <-- Complex numbers.

Use of units- Choose Unit from


7600 ⋅ km 3 −1 <--- .
= 2.11 × 10 m ⋅ s the Insert menu (See Figure 1.5)
1 ⋅ hr

3 a Use of := by typing a colon


a := 8 a + = 513.414 <----
4 character or using the
Arithmetic Palette

sin ( x )
f ( x ) := f ( 37 ) = −0.045 <-- Use of a defined function
x
a+
6
12 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Use of the range operator in the


Arithmetic Palette. You can also use
z := 0 , .2 .. 1 <-- a semicolon (;). This will evaluate
any function of z for z values from
zero to 1 in increments of 0.2

z = f (z) = cos ( f ( z ) ) ⋅ z =
0 0 0
0.2 0.025 0.2 <-- Create these tables by
0.4 0.048 0.4 typing z=, f(z)=, etc.
0.6 0.07 0.599
0.8 0.088 0.797
1 0.103 0.995

x := 0 , 0.2 .. 20 <-- Define a range of values for the plot.

0.2

0.1 Use the X-Y Plot button in


f ( x) the Graph Palette, and type
0 <-- expressions to be plotted in the middle
cos( x) f ( x) placeholders on the x and y axes.
− 0.1 Several expressions separated by a
comma (,) can be entered in these
− 0.2 placeholders.
0 5 10 15 20
x

100
1
∑ n! ⋅ cos ( 2 ⋅ n)
= −2.28758499
n = 0
Use the Calculus Palette (See
Figure 1.1) to do sums, derivatives
25 and integrals. Click in your worksheet
⌠ <--
⎮ 1 to position the red crosshair, click on
⎮ dx = 0.842
3 x the proper button in the palette, then
⎮ 1 + x ⋅e
⌡ fill in each placeholder as appropriate .
0

x := 5
d (ex ⋅sin (x)) = −100.218
dx
Chapter 1: Basics of Mathcad 13

⎛⎜ 3 7 1 ⎞⎟ Matrices can be entered using the


Vector and Matrix palette or by going
A := ⎜ 1 5 6 ⎟ <--
to the Insert menu and choosing
⎜ 1 10 2 ⎟
⎝ ⎠ Matrix .

⎛⎜ 0.427 0.034 −0.316 ⎞⎟


−1
A = ⎜ −0.034 −0.043 0.145 ⎟ <-- Type A ^-1= to obtain the inverse
⎜ −0.043 0.197 −0.068 ⎟
⎝ ⎠

A = −117 <-- Use the Vector and Matrix Palette to


obtain the determinant.

t := 2.5 (4 )
root t − cos ( 3 ⋅ t) , t = 0.502 <-- Find the root of an
expression using the root
function, starting with the guess
of t =2.5

1.12 THE MATHCAD WINDOW, TOOLBARS AND PALETTES

The Mathcad Window and the Main Menu

This does math, graphics and symbolic algebra besides handling the editing and management
of a worksheet. The array of computational and formatting capabilities available can be seen by
clicking on each of the menus.

The Math Toolbar

This is below the main menu. It opens up palettes of math operators as described below.
14 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Calculator toolbar Boolean toolbar


Graph toolbar Programming toolbar

Matrix toolbar Greek toolbar

Evaluation toolbar Symbolic toolbar

Calculus toolbar
Just click on one of these as appropriate and start inserting math symbols into the
worksheet.

The Standard Toolbar

This provides shortcuts for many common tasks from opening and saving files, cutting and
deleting to spell checking and bringing up lists of built-in functions and units. Hover over each
button to see tooltips with a brief description.

The Formatting Toolbar

This formats your text and math at the click of a button. In case the Format Bar does not
show, go to the View Menu, select Toolbars and then Formatting.

The Resources Window and E- books

If you seek examples, want information that can be utilized in your Mathcad worksheets or wish to
access web information from within Mathcad, go to the Help menu, and then open Tutorials,
QuickSheets , Reference Tables or E-books. Tutorials includes Getting Started Primers,
Migration Guide and Features In-Depth, while Quicksheets are live examples showing the use of
Mathcad functions, graphs and programming features. Information on physical constants,
chemical and physical data and mathematical formulas in Mathcad format can be found in
Reference tables . Mathcad E-books can be accessed by opening E-books. These E-books
have the advantage that all equations are live in them and you can change the values of variables,
constants etc. to test and evaluate different results. A number of Mathcad E-books and articles are
also available in the Resources section on http://www.ptc.com/go/mathsoft/mathcad/.
Chapter 1: Basics of Mathcad 15

Controlling Calculation and The Status Line

The calculation mode, whether manual or automatic, is a property saved in your worksheet and
template files. Mathcad starts in the automatic mode and all calculations and results are updated
automatically. The word " Auto " can be seen in the message line at the bottom of the Mathcad
window. This line provides status alerts, tips, keyboard shortcuts, and other helpful information
along with the calculation status of the worksheet . Here, "auto" refers to the automatic
mode , in which Mathcad automatically recalculates any math expressions when changes in the
worksheet are made. When "WAIT" appears on the status line, the cursor changes to a flashing
lightbulb, indicating that Mathcad is still completing computations. Besides giving the page
number of the current worksheet, the message line will also indicate whether the Caps Lock or
the Num Lock key is depressed on your keyboard. In manual mode, Mathcad does not
compute or show results until recalculation is specifically requested. However, while in manual
mode, Mathcad does keep track of pending computations . Once a change is made that requires
recalculation, the word " Calc " appears on the status line to indicate to the user that the results
being displayed are not correct and that recalculation is necessary to ensure accuracy. The
screen can then be updated by going to the Tools menu and choosing Calculate and then
Calculate Now . Alternatively, click = on the Standard toolbar or press [F9]. To force
Mathcad to recalculate all equations in the worksheet, go to the Tools menu and choose
Calculate and then Calculate Worksheet .

1.13 MATHCAD REGIONS


Mathcad allows you to enter equations, text and graphs as separate objects anywhere in the
worksheet and each equation, text paragraph or plot is considered a region. A region can be
selected by clicking in math or text in your worksheet, after which it is indicated by a thin
rectangle around it. Moving the cursor to one of the edges of the region, will change it to a
small hand with which the region can be moved to anywhere in the worksheet. While clicking
in the math region will bring blue selecting lines under the material selected, clicking in a text
region will bring black boxes to each corner and the middle of each line. With these boxes
text regions can be resized as needed. To add a border around a region or regions, select the
region(s), then right-click and choose Properties from the menu. Then click on the Display tab
and check the box next to " Show Border ".
A math region looks like: A text region looks like:

x := 1535.56 What is shown on the left was


created by typing
x:1535.56
16 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Moving Regions in the Worksheet


This can be accomplished by clicking in a region and drag-selecting it to put it in a dashed
selection box and then moving the cursor to the edge where it shows up as a small black
hand. Now, use the mouse to move the region as necessary by holding down the left
mouse button and dragging it. Once the regions are positioned in the desired manner, the
mouse button may be released . Then, an empty part of the screen may be clicked on to
deselect the regions. Alternatively, choose Regions from the View menu, which will
highlight the boundaries of the region against a contrasting background. Cut and Paste
can also be used to move regions. Once regions are inserted in the worksheet, they can
be aligned horizontally or vertically by going to the Format menu and choosing Align
Regions.

How Mathcad Reads Your Document

Mathcad reads your document from left to right and top to bottom . For example, if the value of
a function , ln(y) is desired for y= 67, either of the following setups will work.

(a) y := 67 ln ( y ) = 4.205

(b) y := 67

ln ( y ) = 4.205

Deleting Selected Regions

Selected regions in dashed line boxes can be deleted by choosing Cut from the Edit menu.

Copying and Pasting Selected Regions

Selected regions can also be copied and put into any place within a document or into another
document by choosing the Copy and Paste commands from the Edit menu .

1.14 ENTERING MATH AND TEXT

Entering Math

Math can be entered by clicking the mouse in a blank space to see a red crosshair cursor.
Type

1+

which will get you


Chapter 1: Basics of Mathcad 17

on the screen. The little black box delimited by blue editing lines that you see is called a
placeholder . Continue typing. Type 6 in the placeholder and press the equals key (=) to see
the result.

1+ 6= 7

The Arithmetic Palette can be seen by clicking on the Math Palette. This is shown below

Math Palette button

Operation Keystroke Button

Addition +

Subtraction -

Multiplication *

Division /

Powers ^

Entering Text

A text region can be created by clicking in a blank space to see the red crosshair cursor.
Then choose Text Region from the Insert menu or type the double quote (") . The
crosshair transforms to an insertion point with a black text box around it.

1.15 MATHCAD WORKSHEETS, TEMPLATES AND STYLES

Creating and Using Templates

Mathcad styles and templates are similar to those of any word-processing software. When
you create a new worksheet in Mathcad, you can start with Mathcad's default choices, or you
18 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

can use a template containing customized formats. Several predefined templates are available
each with a variety of styles, and if necessary, new templates can also be created. Any
Mathcad worksheet can be saved as a new template. To create a new worksheet based on a
template, choose File/New , and then select a template from the list, or browse to your own
template directory. In the placeholders, substitute your own text or bitmaps in any of the
placeholders, or use the built-in styles. You can also revise them if needed. When you create
a worksheet based on a template, all of the formatting information, and any math, text and
image regions are copied into the new worksheet. By creating a new template or revising
another template , a customized format can be generated. Text styles and number formats
can be created, number formats, fonts and sizes can be set, bitmaps can be added , and also,
page numbers, filenames and dates can be inserted . Thus, by using templates, you maintain
consistency across multiple worksheets through definition of math styles, text styles, printing
margins, numerical result formats, units etc. To save a template, choose Save As from the
File menu and use the file extension .mct. The settings, styles and bitmaps saved will be
available for the next file you may want to create, leading to greater consistency in your files.

Using Styles

The use of Text Styles allows you to create a consistent appearance in your worksheets.
Styles in each template are available by choosing Style from the Format menu. Any specific
style with a defined font, size, etc. can be applied to a text region. To create or modify a style,
again choose Style from the Format menu . To save your styles for use in new files, you
must make a template file. Math Styles can be used to assign specific fonts, font sizes etc. to
mathematical expressions. There are predefined ones but additional styles can be defined and
applied. This can be accomplished by going to the Format menu and choosing Equation.

1.16 DEFINING VARIABLES

There are times when the value of a number may need to be changed several times in a
document . Such a number is termed a variable . Let Apples be, then, a variable . Type

Apples:50

to see on the screen

Apples := 50

By doing the above, we have given the value of 50 to the variable Apples . Similarly, another
variable Unitprice can be defined
Chapter 1: Basics of Mathcad 19

Unitprice := 0.35

A third variable can also be defined as

Totprice := Apples ⋅ Unitprice

Type

Totprice=

to see on the screen

Totprice = 17.5

Now, if new values need to be given to Apples and to Unitprice, change the numbers
above as necessary and the result which is Totprice will also change accordingly as
shown below.

Apples := 75 Unitprice := 0.40 Totprice := Apples ⋅ Unitprice

Totprice = 30

1.17 DEFINING FUNCTIONS IN MATHCAD

The value of a function depends on the values assigned to its arguments. For example, to
define a function f(x), type

f(x):x^7

to see on the screen

7
f ( x ) := x

Putting in a numerical value of 6 as the argument of f(x) will give


20 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

5
f ( 6) = 2.799 × 10

This can also be accomplished by doing

5
x := 6 f ( x ) = 2.799 × 10

If a range of values for the argument is given

x := 1 .. 6

f(x) will yield a table of numbers as shown below


x = f (x) =
1 1
2 128
3 2.187·103
4 1.638·104
5 7.813·104
6 2.799·105

In addition to defining a customized function, Mathcad's library of built-in functions can


also be utilized. To do this, select Function from the Insert menu, or click on the
function button on the toolbar

The Insert Function dialog box, shown below, will allow insertion of a function name
directly into the placeholder

Built-in functions can also be inserted directly from the keyboard. Some examples are given
below.

Trigonometry and Logarithms

ln ( 56 ) = 4.025 cos ( 50 ⋅ deg) = 0.455 ⋅ 2 asinh ( 0.95 ) = 0.846


Chapter 1: Basics of Mathcad 21

Matrix Functions

⎛1 0 0 0 0 0⎞
⎜ ⎟
⎜0 1 0 0 0 0⎟
⎜0 0 1 0 0 0⎟
identity ( 6) = ⎜ ⎟ cols ( identity ( 6) ) = 6
⎜0 0 0 1 0 0⎟
⎜0 0 0 0 1 0⎟
⎜ ⎟
⎝0 0 0 0 0 1⎠

1.18 BUILDING AND EDITING MATHEMATICAL EXPRESSIONS

Building Mathematical Expressions

In Mathcad, various parts of an expression are assembled by observing the rules of precedence
and some additional procedures that enable entering denominators, exponents and expressions
inside of radicals. The following examples will illustrate the process.

Example 1

To create
22 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

(3 )
f ( x ) := ( x + 6) ⋅ x − 5

Type
f(x):x+6[Spacebar]*(x^3[Spacebar]-5)

Example 2

To create

( 6−4)
f ( x ) := x + 765 ⋅ x

Type

f(x):x+765*(x^6-4)

The exponent operator is called a sticky operator because unless you get out by pressing
[Spacebar] , your keystrokes will "stick" to the exponent . This stickiness applies to
exponents, square roots, subscripts, and division.

Example 3

To create the expression

7
x + 675
780

Type

x^7[Spacebar] <-- Puts x 7 in blue editing lines

+675[Spacebar] <-- Puts the whole equation in the editing lines

/780[Enter] <-- Expression is complete

Example 4

To create
Chapter 1: Basics of Mathcad 23

7 675
x +
780

do the same as above but without pressing the spacebar after +675

Example 5

To create

17
7
t
x
450

type
x^17/t^7[Spacebar][Spacebar][Spacebar]/450

Editing Expressions

The equation editor functions very much like a text editor and goes from left to right. Most
problems dealing with editing equations stem from working with operators. Although Mathcad
automatically inserts parentheses wherever necessary, the Mathcad user must put in
parentheses himself in accordance with his own judgement to give clarity to expressions .
When expressions become complicated, it is definitely preferable to work with smaller and
more manageable subexpressions within them. The reader is referred to Chapter 4 of the
Mathcad 14 User's Guide for a detailed discussion on Editing Expressions. This can be
accessed by choosing Tutorials from the Help menu.

1.19 DEFINING RANGE VARIABLES

To assign a range of values to a variable x going from 0 to 30, say, with an increment
of 3, type

x:0,3;30

The result on the screen will be

x := 0 , 3 .. 30

To evaluate a function f(x) for x= 0 to 7 with an increment of 1, type

x:0;7 f(x):x^5[Spacebar]-6*x^3[Spacebar]+9
24 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

to see on the screen

5 3
x := 0 .. 7 f ( x ) := x − 6 ⋅ x + 9

Now, typing x= and f(x)= will give the following output table on the screen

x = f (x) =
0 9
1 4
2 -7
3 90
4 649
5 2.384·103
6 6.489·103
7 1.476·104

If the range is to have increments other than 1, which is the default value, the next value in the
range must be entered. The following will provide a range of x values going from 2 to 4 with
increments of 0.5 and the corresponding f(x) values.

x := 2 , 2.5 .. 4

x = f (x) =
2 -7
2.5 12.906
3 90
3.5 276.969
4 649

1.20 DEFINING VECTORS AND MATRICES

To create a vector or a matrix in Mathcad, the dimensions of the array must be chosen and the
placeholders must be filled in.
Chapter 1: Basics of Mathcad 25

Figure 1.6 Inserting matrices in Mathcad


26 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

To create a vector V , click in a blank space, Choose Matrix from the Insert menu (See
Figure 1.6 ) or click on the button inside the Vector and Matrix Palette . Then, fill in the
appropriate number of rows and columns, click on Insert and finally fill in the placeholders with
given values. To move from placeholder to placeholder inside the vector, use [Tab] or click on
the appropriate placeholder to select it

⎛⎜ 7 ⎞⎟
V := ⎜ 8 ⎟
⎜9 ⎟
⎝ ⎠

To access a particular element of a vector, use the subscript operator , which can be
created by typing a left square bracket ( [ ), or by using the Xn button in the Matrix
Palette .

In Mathcad , by default the first element has the index 0. Type

V[0=

to see on the screen

V0 = 7

The next two elements will have indices 1 and 2 Thus typing V[1= and V[2= will
produce on the screen

V1 = 8 V2 = 9

In order to obtain all the elements of the vector at the same time, the index can be defined
as a range variable as shown below::

i := 0 .. 2 giving Vi =
7 <--- Type V[i=
8
9

The elements of a vector can also be used as the arguments of a function as shown below.
Define a function f(x) as.
f ( x ) := sin ( 2 ⋅ x ) + cos ( 3 ⋅ x )

Let a vector V be a three-element vector as given below


Chapter 1: Basics of Mathcad 27

⎛⎜ 1.3 ⎞⎟
v := ⎜ −2.6 ⎟
⎜ 7.9 ⎟
⎝ ⎠

Then, typing i:0;2 and then f(v[i)= will show the following on the screen

i := 0 .. 2 ( )
f vi =
-0.21
0.937
0.046

The Vector and Matrix Palette contains most of the common matrix and vector operators.
These are listed below.

Palette
Operation Keystroke Button Display

Dot product [Shift]8 v ⋅w Displays like scalar


multiplication.

Cross product [Ctrl]8 v× w

Determinant | M

〈2〉
Column [Ctrl]6 M Returns the 2nd
column of M.

There is also a wide range of built-in functions in Mathcad that return information about the
size of an array and its elements. For a given matrix [M], various pieces of information can
be generated as shown below.

⎛⎜ 9 10 11 ⎞⎟
M := ⎜ 4 5 7 ⎟
⎜ 12 13 14 ⎟
⎝ ⎠
28 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Number of columns: cols ( M) = 3 Number of rows: rows ( M) = 3

Largest value in matrix: max ( M) = 14 Determinant of matrix: M =3

⎛⎜ 28.768 ⎞⎟
Eigenvalues of a matrix: eigenvals ( M) = ⎜ −0.176 ⎟
⎜ −0.592 ⎟
⎝ ⎠
1.21 CREATING GRAPHS

To create an x-y plot, choose Graph/ X-Y Plot from the Insert Menu as shown in Figure
1.7. Press [Enter] and fill in the placeholders on the x and y axes. Alternatively, type an
expression depending on one variable such as: sin(x) + cos (3x) and click on the X-Y Plot
button on the Graph toolbar . The resulting plot will be

x := −10 , −9.9 .. 10

sin( x) + cos( 3⋅ x)
− 10 −5 0 5 10

−1

−2

x
Chapter 1: Basics of Mathcad 29

Figure 1.7 Creating graphs in Mathcad

X-Y Plot Button


30 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Using Range Variables to Plot a Function

The range of values being plotted on the x-axis must, in general, be specified. If this range is
not prescibed by you, Mathcad will choose a default range for the dependent variable. For
example, let us plot

3 2
f ( x ) := x + 8 ⋅ x + 9 ⋅ x + 14

The graph of this function over the range x= 0 to x= 6 can be accomplished as follows.

3 2
x := 0 , 1 .. 6 f ( x ) := x + 8 ⋅ x + 9 ⋅ x + 14

Now create the plot by clicking in the worksheet window. Type @ to create the x-y plot, type
x in the middle placeholder on the horizontal axis and type f(x) in the middle placeholder
on the vertical axis. Press [Enter] . The following graph should then appear on the screen.

600

400

f ( x)

200

0
0 2 4 6
x

The plot generated above does not seem to be very smooth. In order to obtain a smoother
trace, change the definition of x to x := 0 , 0.1 .. 6. The smaller step enables Mathcad to
calculate more points. This will make the plot a lot smoother (see graph below ) because now
there are more points or dots being connected together.

Formatting of an x-y plot can be accomplished as follows. Double-click on it or choose Graph


from the Format menu to open up a dialog box. This dialog box will allow several options in
terms of grid lines, legends, trace types, markers, colors, axis limits etc.
Chapter 1: Basics of Mathcad 31

x := 0 , 0.1 .. 6

600

400

f ( x)

200

0 2 4 6
x

Plotting a Vector of Data Points

Let us say that we need to plot a vector of data points called Temp , which is the rising
outside temperature on different days of a week in a summer month. This vector which will
have 7 rows and 1 column can be created using the Matrix command on the Insert menu.

⎛⎜ 78 ⎞⎟
⎜ 82 ⎟
⎜ 84 ⎟
⎜ ⎟
Temp := ⎜ 86 ⎟
⎜ 88 ⎟
⎜ ⎟
⎜ 90 ⎟
⎜ 92 ⎟
⎝ ⎠

This data can be graphed by making the horizontal axis an index variable or a vector with
the same number of elements as the vector Temp . Define the index i as:

i := 0 .. 6

Create your plot by typing [@] and typing in

Temp[i
32 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

In the placeholder on the y axis, and i in the placeholder on the x axis. The resulting plot
should be

95

90

Tempi 85

80

75
0 2 4 6
i

Notice here that box symbols have been used on a dashed line.

Alternatively, two vectors of equal size can be plotted against each other. Thus, the second
vector which will be called day is defined as

⎛⎜ 1 ⎞⎟ ⎛⎜ 78 ⎞⎟
⎜2 ⎟ ⎜ 82 ⎟
⎜3 ⎟ ⎜ 84 ⎟
⎜ ⎟ ⎜ ⎟
day := ⎜ 4 ⎟ Temp = ⎜ 86 ⎟
⎜5 ⎟ ⎜ 88 ⎟
⎜ ⎟ ⎜ ⎟
⎜6 ⎟ ⎜ 90 ⎟
⎜7 ⎟ ⎜ 92 ⎟
⎝ ⎠ ⎝ ⎠

Now create the plot by typing @ , as was done previously, and type day in the
placeholder on the x-axis and Temp in the placeholder on the y-axis

The resulting plot should look like this


Chapter 1: Basics of Mathcad 33

100

85

70
0 5 10

Note that the symbol type used here is O's , and the grid lines have been turned off on both
axes.

Plotting a Function of Vector Elements

Sometimes, a function may need to be plotted over points that are not evenly spaced . As an
example, let us define a function

3 2
f ( x ) := 3 ⋅ x + 5 ⋅ x + 9 ⋅ x + 12

to be plotted over the range x as shown: -2.0, 0,3.50,10, 15. In order to do this, x has to be
defined as a vector of the given numbers in the prescribed range. This can be done using the
Matrices dialog box with the placeholders properly filled in as shown.

⎛⎜ −2.0 ⎞⎟
⎜ 0 ⎟

x := 3.5

⎜ ⎟
⎜ 10 ⎟
⎜ 15 ⎟
⎝ ⎠

The index variable must be defined as i := 0 .. 4

( )
Now, type @ and fill in the middle placeholders on the x and y axes with x i and f x i .
The resulting graph should be
34 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

4
1.5× 10

4
1× 10

( )
f xi 3
5× 10

−5 0 5 10 15

3
− 5× 10

xi

When plotting a function of a range variable , notice that the horizontal axis can be any set of
prescribed numbers and does not have to be in even increments

Plotting Several Functions on One Graph

Plotting multiple functions over a single domain on a single graph can be done by typing these
functions on the y- axis one after the other but separated by commas. For example, the
following should illustrate how the two functions sin(x) and cos(2x) can be plotted on the
same graph.

x := 0 , 0.1 .. 6

0.5
sin( x)

cos( 2x) 0 2 4 6
− 0.5

−1

x
Chapter 1: Basics of Mathcad 35

1.22 FORMATTING MATH, TEXT AND RESULTS

Using the Format Bar in the Mathcad window, the appearance of the text as well as the
math can be altered quite easily, and different fonts, sizes and styles can be readily selected.
If the Format Bar is hidden, ensure that the Formatting bar on the View/Toolbars menu
is checked.

Formatting Text

Using the drop-down lists in the Format Bar, different fonts as well as point sizes can be
selected . Appropriate buttons must be clicked to generate special effects like boldface,
italics, etc. Several options become available upon choosing Text from the Format menu .
(See Figure 1.8).

Text Styles and Templates

Text styles provide consistency in the appearance of worksheets and enable the application of
text formatting to the text regions. Available text styles depend on the template used to create
a worksheet. To examine the different templates and text styles provided in Mathcad, choose
New from the File menu and modify them or create new ones.

Formatting Math

In Mathcad different font tags can be applied to variables and to constants. To make
changes in the font, click on a variable or constant in a math region and use the Format Bar.
Alternatively, you can choose Equation from the Format menu.

Highlighting of equations can also be done in Mathcad. To do this, click on an equation and
choose Properties/Highlight Region from the Format menu in the main worksheet
window. Below is shown an example of a highlighted equation.

n
S=
∑ (ai ⋅ xi)
i = 1

In Mathcad, you can specify how answers can be displayed in terms of the desired decimal
places, precision etc. Also, fonts and their sizes can be controlled and highlighting of
equations can be done as desired.
36 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 1.8 Formatting text in Mathcad

Formatting Results and Graphs

To control the format of a result, double-click on the result , or click on the result and
choose Result from the Format menu . In the Number Format dialog box that shows
up, change the Exponential Threshold , and Number of decimal places as necessary
and click on OK . This will set the format only for this particular result. (See Figure 1.9 ).
However, if this needs to be done for the entire worksheet, click on a blank part of the
worksheet, and do the above .
Chapter 1: Basics of Mathcad 37

Figure 1.9 Formatting results in Mathcad


38 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The reformatting of graphs can be easily done using a dialog box generated by double-
clicking on the plot, or by going to Insert/Graph/X-Y Plot and clicking on the Traces
tab shown in Figure 1.10. Under the " Legend label " column, type the desired name of
the trace . Symbols, line types, colors, trace types, etc. can all be controlled by clicking
on the appropriate options. Finally, preview the changes and click OK to finalize them .
An example is shown at the end of this section.

Figure 1.10 Formatting graphs in Mathcad


Chapter 1: Basics of Mathcad 39

f ( x ) := sin ( x ) g ( x ) := cos ( 3 ⋅ x ) h ( x ) := f ( x ) + g ( x ) x := −10 , −9.9 .. 10

Graphing Functions and labelling Plots


2

1
f ( x)
y-values

g ( x) 0
h ( x)
−1

−2
− 10 −5 0 5 10
x

x- values
sin(x)
cos(3x)
sin(x)+cos(3x)

1.23 USING UNITS

Although units are not required to be put into Mathcad equations, a great feature within
Mathcad is its ability to track standard units during calculations and to automatically perform
conversions. The program will also flag incorrect and inconsistent dimensional calculations,
and mixing and matching of units can be done as desired.The default system in Mathcad is
the SI unit system

To define a variable in terms of the built-in unit kilometers , for example, just multiply the
given number by km. Type

R:4*km

to see on the screen

R := 4 ⋅ km
40 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 1.11 Inserting units in Mathcad


Chapter 1: Basics of Mathcad 41

The equation for a circular area of radius R now will automatically generate the result in terms
of the appropriate units

2 7 2
A := π ⋅ R A = 5.027 × 10 m

Note that the above result is automatically shown in terms of the base units of the default
unit system, which, in this case, is SI. To change this result to feet , for example, click in
the name of the unit that you want to replace, then drag-select it and type in the new desired
unit in its place. Finally click outside the equation to see the new result. Alternatively, click
in the placeholder next to the unit name , go to Insert/Unit (See Figure 1.11) , click on the
appropriate Dimension name, then double-click on the unit name desired , and finally click
outside the equation to see the desired result.

4
R = 1.312 × 10 ⋅ ft

To see a list of buit-in units in Mathcad, go to the Insert menu . The following equivalents can
be easily generated using the procedures described above.

4 2 −2
Work := 14000. ⋅ J Work = 1.4 × 10 m ⋅ kg ⋅ s Work = 13.269 ⋅ BTU

3
Power := 2000 ⋅ kW Power = 2.682 × 10 ⋅ hp
3
Force := 1500lbf Force = 6.672 × 10 ⋅ N

A calculation involving mixing of units is shown below. Although each term in the calculation is
expressed in a different unit of length, the result is calculated in the base SI unit for length,
namely, meters

6 ⋅ ft + 30 ⋅ in + 1.4 ⋅ m + 0.8 ⋅ yd = 4.722 m

The following calculation, obviously, cannot be completed and Mathcad indicates the same.

7 ⋅ ft + 6 ⋅ sec =

Click on the expression to see the error message,which is:

This value has units: Time, but must have units: Length
42 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

To change the unit system of the worksheet from SI to U.S. units, select Worksheet
Options from the Tools menu. Then, click the Unit System tab, and select U.S. This
option gives you results in "English System" measures.

In the following calculation, a mass is multiplied by an acceleration to give force in proper


units. The result should be in Newtons or an equivalent force unit.

m
mass := 5 ⋅ kg acc := 20 ⋅
2
sec

−2
Force := mass ⋅ acc Force = 100 m ⋅ kg ⋅ s

To display units, choose Result from the Format menu. Then, go to Unit Display and
check Format Units and Simplify units when possible, and results will display as shown
below.

−2 3
g := 9.807 ⋅ s ⋅m 60 ⋅ N ⋅ 20 ⋅ m = 1.2 × 10 J

m
8 ⋅ kg ⋅ 6 ⋅ = 48 N
2
sec
Chapter 2: Introduction to Numerical Methods 43

CHAPTER 2

INTRODUCTION TO NUMERICAL METHODS

2.1 THE USE OF NUMERICAL METHODS IN SCIENCE AND


ENGINEERING

Analysis of problems in engineering and the physical sciences typically involves four steps as
follows.

(1) Development of a suitable mathematical model that realistically represents a given physical
system.

(2) Derivation of the system governing equations using physical laws such as Newton's laws of
motion, conservation of energy, the laws governing electrical circuits etc.

(3) Solution of the governing equations, and

(4) Interpretation of the results.

Because real world problems are generally quite complex with the generation of closed-form
analytical solutions becoming impossible in many situations, there exists, most definitely, a
need for the proper utilization of computer-based techniques in the solution of practical
problems. The advancement of computer technology has made the effective use of numerical
methods and computer-based techniques very feasible, and thus, solutions can now be
obtained much faster than ever before and with much better than acceptable accuracy.
However, there are advantages as well as disadvantages associated with any numerical
procedure that is resorted to , and these must be kept in mind when using it.

2.2 COMPARISON OF NUMERICAL METHODS WITH ANALYTICAL


METHODS

While an analytical solution will be exact if it exists, a numerical method , on the other hand,
will generally require iterations to generate a solution, which is only an approximation and
which certainly cannot be considered exact by any means.

A disadvantage associated with analytical solution techniques is that they are generally
applicable only to very special cases of problems. Numerical solutions, on the contrary, will
solve complex situations as well.

While numerical techniques have several advantages including easy programming on a


computer and the convenience with which they handle complex problems, the initial
estimate of the solution along with the many number of iterations that are sometimes required
to generate a solution can be looked upon as disadvantages.
44 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

2.3 SOURCES OF NUMERICAL ERRORS AND THEIR COMPUTATION

It is indeed possible for miscalculations to creep into a numerical solution because of various
sources of error. These include inaccurate mathematical modeling, wrong programming, wrong
input, rounding off of numbers and truncation of an infinite series. Round-off error is the general
name given to inaccuracies that affect the calculation scene when a finite number of digits are
assigned to represent an actual number. In a long sequence of calculations, this round-off error
can accumulate, then propagate through the process of calculation and finally grow very rapidly
to a significant number. A truncation error results when an infinite series is approximated by a
finite number of terms, and, typically, upper bounds are placed on the size of this error.

The true error is defined as the difference between the computed value and the true value of a
number.

ETrue = XComp − XTrue


(2.1)

while the relative true error is the error relative to the true value

XComp − XTrue (2.2)


er =
XTrue

Expressed as a percentage, the relative true error is written as

XComp − XTrue
er = ⋅ 100 (%) (2.3)
XTrue

2.4 TAYLOR SERIES EXPANSION

The Taylor series is considered as a basis of approximation in numerical analysis. If the


value of a function of x is provided at " x 0 ", then the Taylor series provides a means of
evaluating the function at " x 0 + h", where " x 0 " is the starting value of the independent
variable and " h " is the difference between the starting value and the new value at which
the function is to be approximated

2 3
⎡d ⎤ h d2 3
( ) ( ) ( ) ( ) ( )
h d
f x 0 + h = f x 0 + h ⋅ ⎢ ⎡⎣f x 0 ⎤⎦⎥ + ⋅ f x0 + ⋅ f x 0 + ..................... (2.4)
⎣dx ⎦ 2! dx2 3! dx 3

This equation can be used for generating various orders of approximations as shown
below. The order of approximation is defined by the highest derivative included in the
series. For example, If only terms up to the second derivative are retained in the series,
the result is a second order approximation.
Chapter 2: Introduction to Numerical Methods 45

First order approximation:

( ) ( )
f x0 + h = f x0 + h ⋅
d
( )
f x0
(2.5)
dx

Second order approximation:

2
( ) ( )
f x0 + h = f x0 + h ⋅
d
( )
f x0 ⋅
d
2
( )
f x0 (2.6)
dx dx

Third order approximation:

2 2 3 3
( ) ( ) ( ) ( ) ( )
d h d h d
f x0 + h = f x0 + h ⋅ f x0 + ⋅ f x0 + ⋅ f x 0 + ........................... (2.7)
dx 2! 2 3! 3
dx dx

It is to noted that the significance of the higher order terms in the Taylor series increases with
the nonlinearity of the function involved as well as the difference between the " starting x" value
and the "x" value at which the function is to be approximated. Thus , the fewer the terms that
are included in the series, the larger will be the error associated with the computation of the
function value. If the function is linear , however, only terms up to the first derivative term need
to be included.

Example 2.1

Using the Taylor series expansion for

f(x) = - 0.15 x4 - 0.17 x 3 - 0.25 x 2 -0.25 x + 1.25

determine the zeroth, first, second, third, fourth and fifth order approximations of
f(x0 + h ) where x0 = 0 and h = 1,2, 3, 4,5 and compare these with the exact solutions.

h=1.0: Put in the function and generate its derivatives:

4 3 2
f ( x ) := −0.15 ⋅ x − 0.17 ⋅ x − 0.25 ⋅ x − 0.25x + 1.25
x0 := 0 h := 1.

3 2
fprime ( x ) := −0.60 ⋅ x − 0.51 ⋅ x − 0.50 ⋅ x − 0.25 <--Generate
derivatives
2
f2prime ( x ) := −1.8 ⋅ x − 1.02 ⋅ x − 0.50

f3prime ( x ) := −3.6 ⋅ x − 1.02 f4prime ( x ) := −3.6 f5prime ( x ) := 0.


46 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

2
h
term1 := f ( x0) term2 := h ⋅ fprime ( x0) term3 := ⋅ f2prime ( x0)
2
3 4 5
h h h
term4 := ⋅ f3prime ( x0) term5 := ⋅ f4prime ( x0) term6 := ⋅ f5prime ( x0)
6 24 120
ftaylor0 := term1 <---- one-term or zero-order approximation

ftaylor1 := term1 + term2 <---- first order approximation with two terms

ftaylor2 := term1 + term2 + term3 <---second order approximation with 3 terms

ftaylor3 := term1 + term2 + term3 + term4 <---third order approximation with 4 terms

ftaylor4 := term1 + term2 + term3 + term4 + term5 <---fourth order approximation with 5 terms

fifth order approximation


ftaylor5 := term1 + term2 + term3 + term4 + term5 + term6 <-----
with 6 terms
x := x0 + h x=1

These are the zero- fifth order


ftaylor0 = 1.25 ftaylor1 = 1 ftaylor2 = 0.75
<-- approximations of the given
ftaylor3 = 0.58 ftaylor4 = 0.43 ftaylor5 = 0.43 function f(x) using the Taylor
series.

f ( 1) = 0.43 <---EXACT ANSWER USING FUNCTION GIVEN.

err0 := f ( x ) − ftaylor0 err0 = −0.82

err1 := f ( x ) − ftaylor1 err1 = −0.57

err2 := f ( x ) − ftaylor2 err2 = −0.32 These are errors ( differences between exact
<-- values and approximations ) for the above
zero - fifth order approximations.
err3 := f ( x ) − ftaylor3 err3 = −0.15

err4 := f ( x ) − ftaylor4 err4 = 0

err5 := f ( x ) − ftaylor5 err5 = 0

Similarly, by using h= 2, 3, 4, 5 , the zeroth- fifth order approximations for f(2), f(3), f(4),
f(5) and the associated errors can be determined. These are given in Tables 2.1 and 2.2

Plots of the various Taylor series approximations of the given function and associated errors
are generated below and are presented in Figs. 2.1 and 2.2
Chapter 2: Introduction to Numerical Methods 47

x0 := 0 x := 0 , 0.01 .. 5

ftaylor0 ( x ) := f ( x0) <-- zeroth-order approximation

ftaylor1 ( x ) := ftaylor0 ( x ) + ( x − x0) ⋅ fprime ( x0) <---first-order approximation

2
( x − x0)
ftaylor2 ( x ) := ftaylor1 ( x ) + ⋅ f2prime ( x0) <--second-order approximation
2
3
( x − x0)
ftaylor3 ( x ) := ftaylor2 ( x ) + ⋅ f3prime ( x0) <---third-order approximation
6

4
( x − x0)
ftaylor4 ( x ) := ftaylor3 ( x ) + ⋅ f4prime ( x0) <---fourth-order approximation
24

5
( x − x0)
ftaylor5 ( x ) := ftaylor4 ( x ) + ⋅ f5prime ( x0) <---fifth- order approximation
120

Errors generated with the various approximations are as follows

Zero order approximation: err0 ( x ) := f ( x ) − ftaylor0 ( x )

First order approximation: err1 ( x ) := f ( x ) − ftaylor1 ( x )

Second order approximation: err2 ( x ) := f ( x ) − ftaylor2 ( x )

Third order approximation: err3 ( x ) := f ( x ) − ftaylor3 ( x )

Fourth order approximation: err4 ( x ) := f ( x ) − ftaylor4 ( x )

Fifth order approximation: err5 ( x ) := f ( x ) − ftaylor5 ( x )

The various approximations generated by the above calculations and the associated errors
are compared in the Table 2.1.
48 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Taylor series approx of given function


50
Function approximated by Taylor series

ftaylor0( x)

ftaylor3( x) 50

ftaylor5( x)

100
Zeroth order approx
Third order approx
Fifth order approx/ given function
150
0 1 2 3 4 5
x

x- value

Figure 2.1. Taylor series approximation of given function

x := 0 , 0.01 .. 5

Errors gen due to Taylor-series approx


50
errors as function of x

0
err0( x)

err3( x) 50

err5( x) Zero order approx


100 Third order approx
Fifth order approx
150
0 1 2 3 4 5
x

x- value

Figure 2.2. Errors associated with the various Taylor series approximations
Chapter 2: Introduction to Numerical Methods 49

The various approximations generated by the above calculations and the associated errors are
compared in the following tables.

Table 2.1
h := 1 .. 5
Various orders of approximation generated by Taylor series
x := 0 , 1 .. 5 approach versus true values of given function

zeroth first second third fourth fifth True


order order order order order order Value
x = =
ftaylor0 ( x ) ftaylor1 ( x ) =ftaylor2 ( x ) ftaylor3
= ( x ) =ftaylor4 ( x ) =ftaylor5 ( x ) =f ( x ) =
h= 0 1.25 1.25 1.25 1.25 1.25 1.25 1.25
1 1 1.25 1 0.75 0.58 0.43 0.43 0.43
2 2 1.25 0.75 -0.25 -1.61 -4.01 -4.01 -4.01
3 3 1.25 0.5 -1.75 -6.34 -18.49 -18.49 -18.49
4 4 1.25 0.25 -3.75 -14.63 -53.03 -53.03 -53.03
5 5 1.25 0 -6.25 -27.5 -121.25 -121.25 -121.25

Table 2.2

Errors associated with the different orders of approximation

zeroth first second third fourth fifth


order order order order order order

x = err0 ( x ) = err1 ( x ) = err2 ( x ) = err3 ( x ) = err4 ( x ) = err5 ( x ) =


h= 0 0 0 0 0 0 0
1 1 -0.82 -0.57 -0.32 -0.15 0 0
2 2 -5.26 -4.76 -3.76 -2.4 0 0
3 3 -19.74 -18.99 -16.74 -12.15 0 0
4 4 -54.28 -53.28 -49.28 -38.4 0 0
5 5 -122.5 -121.25 -115 -93.75 0 0

PROBLEMS

2.1. Using the Taylor series expansion for cos x, which is given as

f(x) = cos x = 1- x 2 / 2 + x 4 / 24 ,

determine the one-term, two-term and three-term approximations of f(x0 + h ) , where x 0 = 0


50 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

rad and h = 0. 1, 0.2 ....1.0 rad , and compare these with the exact solution. Using Mathcad,
generate plots of the various Taylor series approximations and associated errors as functions of
the independent variable x.

2.2 Develop a Taylor series expansion of the following function:

f(x) = x 5 - 6 x 4 + 3x 2 + 9 .

Use x =3 as the base and h as the increment. Using Mathcad, evaluate the series for h= 0.1,
0.2....1.0, adding terms incrementally as in Problem 2.1. Compare the various Taylor series
approximations obtained with true values in a table. Generate plots of the approximations and
associated errors as functions of x .

2.3 Given the following function:

f(x) = x 3 -3x 2 + 5 x + 10 ,

determine f ( x0 + h ) with the help of a Taylor series expansion, where x 0 = 2 and h = 0.4.
Compare the true value of f ( 2.4 ) with estimates obtained by resorting to (a) one term only (b)
two terms (c) three terms and (d) four terms of the series.

2.4 Given the following function

3 2
f ( x ) = 3 ⋅ x − 6 ⋅ x + 15 ⋅ x + 25

use a Taylor series expansion to determine the zeroth, first, second and third order
approximations of f(x0 +h) where x 0 = 2 and h = 0.5. Compare these with the exact
solution.

2.5 By developing a Taylor series expansion for

f(x) = e x

about x = 0 , determine the fourth-order approximation of e 2.5 and compare it with the exact
solution.

2.6. By developing a Taylor series expansion for

f(x) = ln(2-x)

about x = 0, determine the fourth-order approximation of ln (0.5) and compare it with the exact
solution
Chapter 2: Introduction to Numerical Methods 51

2.7. By developing a Taylor series expansion for

f(x) = x3 e - 5x

about x = 1, determine the third-order approximation of f(1.2) and compare it with the exact
solution.

2.8. By developing a Taylor series expansion for

f(x) = e cos x

about x =0 , determine the fourth- order approximation of f (2π) and compare it with the exact
solution. .

2.9. By developing a Taylor series expansion for

f(x) = (x - 2) 1/2

about x = 2, determine the third-order approximation of f (2.2), that is, (0.2) 1/2 , and compare it
with the exact solution.

2.10. Given the function

f(x) = x2 - 5 x 0.5 +6,

use a Taylor series expansion to determine the first, second , third and fourth order
approximations of f (2.5 ) by resorting to x 0 = 2 and h = 0.5 . Compare these with the exact
solution.

2.11. Given the function

f(x) = 6 x3 - 9 x2 +25 x + 40 ,

use a Taylor series expansion to determine the zeroth, first, second and third order
approximations of f (x0 + h ) where x 0 = 3 and h = 1 . Compare these with the exact solution.

2.12. Given the function

f(x) = 4 x4 - 7 x3 + 5 x2 - 6 x + 90

use a Taylor series expansion to determine the zeroth, first, second , third and fourth order
52 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

approximations of f (x 0 + h ) ) where x 0 = 3 and h = 0.5 . Compare these with the exact


solution. Calculate errors abnd generate calculations to three decimal places.

2.13. Given the function

f(x) = 8 x3 - 10 x2 + 25 x + 45 ,

use a Taylor series expansion to determine the zeroth, first, second and third order
approximations of f (x0 + h ) where x 0 = 2 and h = 1 . Compare these with the exact solution.

2.14. Given the function

f(x) = 1 + x + x 2 / 2! + x3 / 3! +x4 / 4!

use a Taylor series expansion to determine the zeroth, first, second , third and fourth order
approximations of f (x0 + h ) where x 0 = 0 and h = 0.5 . Compare these with the exact
solution. Generate answers correct to four decimal places.

2.15. Given the function

f(x) = x + x 3 / 3 + 2 x5 / 15

use a Taylor series expansion to determine the zeroth, first, second , third and fourth order
approximations of f (x0 + h ) where x 0 = 0 and h = 0.8 . Compare these with the exact solution
by computing percentage errors. Generate answers correct to four decimal places.

2.16. Given the function

f(x) = sin (x)

use a Taylor series expansion to determine the fifth order approximation of f (x0 + h ) where x 0
= 0 and h = 0.2 radians . Compare your answer with the true value. Generate answers correct
to four decimal places.

2.17. Given the function

f(x) = 3 x2 - 6 x 0.5 +9,

use a Taylor series expansion to determine the zeroth, first, second , third and fourth order
approximations of f (x0 + h ) whereo x 0 = 3 and h = 1 . Compare these with the exact
solution by computing percentage errors. Generate answers correct to four decimal places.
Chapter 3: Roots of Equations 53

CHAPTER 3
ROOTS OF EQUATIONS
3.1 INTRODUCTION

In many problems occurring in science and engineering, it is often necessary to find roots
or zeros of equations that are nonlinear. Nonlinear equations have no closed-form solutions
except in some very special cases, and thus, computer methods are indispensable in their
solution. Some examples of equations whose roots may need to be found are:

2 3 4
1 + 4 ⋅ x − 16 ⋅ x + 3 ⋅ x + 3 ⋅ x = 0 <-- a polynomial, a characteristic equation,
for instance

f (x) − α = 0 <-- α is a number and f(x) is a function of x

tan ( α ) = tanh ( 2 ⋅ x ) <-- a transcendental equation

3.2 METHODS AVAILABLE

There are several methods available for finding roots of equations. Some of these are:
(1) Direct Search, which is not a very efficient technique, (2) Bisection (3) False Position
(4) Newton-Raphson (5) Secant Method (6) Bairstow's Method, which is applicable only
to polynomials and (6) Successive Iteration or Fixed Point Iteration method . In this
chapter, however, only the Bisection, False Position, Newton-Raphson, Secant and
Successive Iteration methods will be addressed along with the functions used in Mathcad
to find roots.

3.3 BISECTION METHOD

This method can be resorted to when there is only one root occurring in a given range
of x. The method involves investigating a given range to seek a root and then bisecting
the region successively until a root is found. Other names for this technique are Interval
halving, Binary Chopping and Bolzano's Method.

Procedure for Finding Roots


1. Choose starting and end points xstart and xend

2. Compute: f(xstart) and f(xend)

3. Compute: f(xstart) times f(xend)


54 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

4. If the above product is negative, then the root lies between xstart and xend. If this
product is positive, reselect xstart and xend.

5. If f(xtart)* f(xend) < 0, compute the mid-point of the xstart-xend


range. Call it "xmid1" and repeat above steps, i.e., compute

f ( xstart) ⋅ f ( xmid1) and f ( xmid1) ⋅ f ( xend )

6. If f(xstart)*f(xmid1) < 0, the root lies between xstart and xmid1.


If (xmid1)*f(xend) < 0, then the root lies between xmid1 and xend.

7. Repeat the above procedure until convergence at a root value occurs.

Figure 3.1. Bisection Method

Computation of Error and Convergence Criterion

A convergence criterion has to be followed in order to determine if a root has indeed been
found. This is expressed in terms of the error ε or the percentage relative error
ε rel which are defined as
Chapter 3: Roots of Equations 55

ε = xmid i+1 − xmid i (3.1)

xmid i+1 − xmid i (3.2)


ε rel = ⋅ 100 percent
xmid i+ 1

where xmid i+1 and xmid i are the midpoints in the current and previous iterations. While,
in general, the relative error should not be greater than 5 %, an error of 0.01 % is the
largest that is tolerable for some classes of problems, that require immense precision.

The true error , ε True , is an indicator of the real accuracy of a solution and can be
evaluated only if the true solution, x True , is known. It is defined as

x True − xmid i
ε True = ⋅ 100 (3.3)
x True

Calculation of the true error clearly requires knowledge of the true solution, which, in general,
will not be known to us. Therefore, the quantity ε rel may have to be mostly used to
determine the error associated with a solution process.

Advantages and Disadvantages of Bisection

While Bisection is a simple, robust technique for finding one root in a given interval, when
the root is known to exist and it works even for non-analytic functions, its convergence
process is generally slow, making it a somewhat inefficient procedure.

Sometimes, a singularity may be identified as if it were a root, since the method does not
distinguish between roots and singularities, at which the function would go to infinity.
Therefore, as the method proceeds, a check must be made to see if the absolute value of
[f(xend)-f(xstart)], in fact, converges to zero. If this quantity diverges, the method is chasing
a singularity rather than a root .

When there are multiple roots, Bisection is not a desirable technique to use, since the
function may not change signs at points on either side of the roots. Therefore, a graph of
the function must first be drawn before proceeding to do the calculations.

Example 3.1

Obtain a root of f(x) in the range of 4 < x < 20

f(x)= ( 750.5 / x ) [ 1 - exp( -0.15245x) ] - 40


56 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Let us generate a table for x between 4 and 20 and draw a graph using Mathcad to explore
where the root may lie.

f ( x ) :=
750.5
(
⋅ 1− e
− 0.15245⋅ x ) − 40 x := 4 , 8 .. 20
x

x = f (x) = Graph of f(x) versus x


0 0 50
0 4 0 45.6584 40
1 8 1 26.1051 30
2 12 2 12.5031
f ( x) 20
3 16 3 2.8146
10
4 20 4 -4.2539
0
− 10
5 10 15 20
x

The above table and graph suggest that the root lies between xstart=16 and xend= 20. In an
attempt to minimize the number of iterations needed, we will obtain a root value that is correct
only to two decimal places, in this case.

xstart + xend
Iteration # 1: xstart := 16 xend := 20 xmid := xmid = 18
2

f ( xstart) ⋅ f ( xmid ) = −2.7773 f ( xmid ) ⋅ f ( xend ) = 4.1976

The root must lie beteen above xstart and xmid. Thus the new xstart is 16 and the new xend is
18

xstart + xend
Iteration # 2: xstart := 16 xend := 18 xmid := xmid = 17
2

f ( xstart) ⋅ f ( xmid ) = 2.366 f ( xmid ) ⋅ f ( xend ) = −0.8295

The root must lie beteen above xmid and xend. Thus the new xstart is 17 and the new
xend is 18

xstart + xend
Iteration # 3: xstart := 17 xend := 18 xmid := xmid = 17.5
2

f ( xstart) ⋅ f ( xmid ) = −0.0761 f ( xmid ) ⋅ f ( xend ) = 0.0893


Chapter 3: Roots of Equations 57

The root must lie beteen above xstart and xmid. Thus the new xstart is 17 and the new xend
is 17.5

xstart + xend
Iteration # 4: xstart := 17. xend := 17.5 xmid := xmid = 17.25
2

f ( xstart) ⋅ f ( xmid ) = 0.3115 f ( xmid ) ⋅ f ( xend ) = −0.0335

The root must lie beteen above xmid and xend. Thus the new xstart is 17.25 and the new
xend is 17.5

xstart + xend
Iteration # 5: xstart := 17.25 xend := 17.5 xmid := xmid = 17.375
2

f ( xstart) ⋅ f ( xmid ) = 0.0515 f ( xmid ) ⋅ f ( xend ) = −0.0126

The root must lie beteen above xmid and xend. Thus the new xstart is 17.38 and the new xend is
17.5

xstart + xend
Iteration # 6: xstart := 17.38 xend := 17.5 xmid := xmid = 17.44
2

−3 −3
f ( xstart) ⋅ f ( xmid ) = 2.5064 × 10 f ( xmid ) ⋅ f ( xend ) = −1.7497 × 10

The root must lie beteen above xmid and xend. Thus the new xstart is 17.44 and the new
xend is 17.5

xstart + xend
Iteration # 7: xstart := 17.44 xend := 17.5 xmid := xmid = 17.47
2

−4 −3
f ( xstart) ⋅ f ( xmid ) = −6.893 × 10 f ( xmid ) ⋅ f ( xend ) = 3.2291 × 10

The root must lie beteen above xstart and xmid. Thus the new xstart is 17.44 and the new
xend is 17.47

xstart + xend
Iteration # 8: xstart := 17.44 xend := 17.47 xmid := xmid = 17.455
2

−4 −4
f ( xstart) ⋅ f ( xmid ) = −1.5821 × 10 f ( xmid ) ⋅ f ( xend ) = 2.9199 × 10
58 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The root must lie beteen above xstart and xmid. Thus the new xstart is 17.44 and the new
xend is 17.455

xstart + xend
Iteration # 9: xstart := 17.44 xend := 17.455 xmid := xmid = 17.447
2

−4 −5
f ( xstart) ⋅ f ( xmid ) = 1.0756 × 10 f ( xmid ) ⋅ f ( xend ) = −4.5564 × 10

a check to see if this is


−3
f ( xstart) = 0.0193 f ( xmid ) = 5.5657 × 10 <-- small

Since f(xmid) is a very small quantitity, it is reasonable to assume that the xmid obtained
in iteration # 9 is, in fact, the required root. The % error in this case is, then

17.447 − 17.455
error := ⋅ 100 error = −0.0459 (percent)
17.447

Thus, the solution which has been obtained after 9 iterations is : x = 17.447

3.4 THE REGULA FALSI OR THE FALSE POSITION METHOD

The False Position method is similar to the Bisection method in that the size of the interval
containing the root is reduced with every step of the iteration process until the root is found.
The main difference is that while the interval size in the Bisection method is reduced by
bisecting it in each step of the iteration , this reduction of interval size is achieved by a
linear interpolation fitting the two end points. While the Bisection method is reliable, it is
slower than the False Position method in achieving convergence.

The equation of a straight line connecting two points (xstart,ystart) and (xend, yend) is

yend − ystart
y = yend + ⋅ ( x − xend ) (3.4)
xend − xstart
Chapter 3: Roots of Equations 59

f(x)
(xend, f(xend))

f(xend)

xnew

xstart x
xend

Root
f(xstart )

(xstart, f(xstart))

Figure 3.2. Regula Falsi method

The procedure for finding roots then is as follows. ( See Figure 3.2 )

1. Choose the starting and ending points xstart and xend as in the Bisection
method.

2. Compute f(xstart) and f(xend). Make sure that f(xstart) times f(xend) is still
a negative product. If it is not, then there is no root between xstart and xend.

3. The new end point xnew is located by setting y = 0 in Equation 3.4 and solving for x.

xend − xstart
xnew = xend − ⋅ yend (3.5)
yend − ystart

4. If f(xstart)*f(xnew) < 0, the root lies between xstart and xnew. However, if f(xnew)*f(xend) <
0, the root lies between xnew and xend.

5. Repeat the above procedure until convergence takes place.


60 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

A disadvantage of this method is that sometimes a root is approached from only one side and,
thus, one end of the interval does not change at all in successive iterations. This is called
stagnation of an end point. This is not desirable since it slows down the convergence process
especially when the initial interval is very large or when the function is highly nonlinear. Examples
3.2 and 3.3 show the occurrence of stagnation.

Example 3.2

Obtain the required root for the function of Example 3.1 by the False Position method
correct to four decimal places. The starting and ending points were 16 and 20 respectively.

f ( x ) :=
750.5
(
⋅ 1− e
− 0.15245⋅ x ) − 40
x

Iteration # 1: xstart := 16 xend := 20 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = 2.8146 yend = −4.2539 xnew := xend − ⋅ yend xnew = 17.5927
yend − ystart

f ( xstart) ⋅ f ( xnew ) = −0.73 f ( xnew ) ⋅ f ( xend ) = 1.1033

The root must lie beteen above xstart and xnew. Thus the new xstart is 16 and the new
xend is 17.5927

Iteration # 2: xstart := 16 xend := 17.5927 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = 2.8146 yend = −0.2593 xnew := xend − ⋅ yend xnew = 17.4584
yend − ystart

−3
f ( xstart) ⋅ f ( xnew ) = −0.0404 f ( xnew ) ⋅ f ( xend ) = 3.7171 × 10

The root must lie between above xstart and xnew. Thus the new xstart is 16 and the new
xend is 17.4584

Iteration # 3: xstart := 16 xend := 17.4584 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = 2.8146 yend = −0.0144 xnew := xend − ⋅ yend xnew = 17.451
yend − ystart

−3 −5
f ( xstart) ⋅ f ( xnew ) = −2.2312 × 10 f ( xnew ) ⋅ f ( xend ) = 1.143 × 10

The root must lie beteen above xstart and xnew. Thus the new xstart is 16 and the new xend
is 17.451
Chapter 3: Roots of Equations 61

Iteration # 4: xstart := 16 xend := 17.451 ystart := f ( xstart) yend := f ( xend )

−4 xend − xstart
ystart = 2.8146 yend = −8.5304 × 10 xnew := xend − ⋅ yend
yend − ystart

−4 −8
xnew = 17.4506 f ( xstart) ⋅ f ( xnew ) = −1.3196 × 10 f ( xnew ) ⋅ f ( xend ) = 3.9995 × 10

The root must lie beteen above xstart and xnew. Thus the new xstart is 16 and the new xend
is 17.4506

Iteration # 5: xstart := 16 xend := 17.4506 ystart := f ( xstart) yend := f ( xend )

−4 xend − xstart
ystart = 2.8146 yend = −1.1956 × 10 xnew := xend − ⋅ yend
yend − ystart

−5 − 10
xnew = 17.4505 f ( xstart) ⋅ f ( xnew ) = −1.8496 × 10 f ( xnew ) ⋅ f ( xend ) = 7.8568 × 10

The root must lie beteen above xstart and xnew. Thus the new xstart is 16 and the new xend
is 17.4505

Iteration # 6: xstart := 16 xend := 17.4505 ystart := f ( xstart) yend := f ( xend )

−5 xend − xstart
ystart = 2.8146 yend = 6.3811 × 10 xnew := xend − ⋅ yend
yend − ystart

−6 − 10
xnew = 17.4505 f ( xstart) ⋅ f ( xnew ) = 9.8712 × 10 f ( xnew ) ⋅ f ( xend ) = 2.238 × 10

−6
f ( xnew ) = 3.5072 × 10

The above process indicates that in this case, the root is being approached only from the
side of the original starting point and therefore stagnation has occurred. Since the xnew
computed in the sixth iteration is the same as the one in the fifth, and f(xnew) is a very
small quantitity, it is reasonable to say that the xnew (=17.4505) computed above is, in
fact, the required root.

Example 3.3

Obtain a root of f(x) by the False Position method in the range of 0 < x < 10 correct to
three decimal places.

3
f ( x ) := x + 9 ⋅ x − 200

Iteration # 1: xstart := 0 xend := 10 ystart := f ( xstart) yend := f ( xend )


62 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xend − xstart
ystart = −200 yend = 890 xnew := xend − ⋅ yend xnew = 1.8349
yend − ystart

4 5
f ( xstart) ⋅ f ( xnew ) = 3.5462 × 10 f ( xnew ) ⋅ f ( xend ) = −1.578 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 1.835 and the new
xend is 10

Iteration # 2: xstart := 1.835 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −177.3061 yend = 890 xnew := xend − ⋅ yend xnew = 3.1914
yend − ystart

4 5
f ( xstart) ⋅ f ( xnew ) = 2.4605 × 10 f ( xnew ) ⋅ f ( xend ) = −1.2351 × 10

The root must lie between above xnew and xend. Thus the new xstart is 3.191 and the new
xend is 10

Iteration # 3: xstart := 3.191 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −138.7887 yend = 890 xnew := xend − ⋅ yend xnew = 4.1096
yend − ystart

4 4
f ( xstart) ⋅ f ( xnew ) = 1.2992 × 10 f ( xnew ) ⋅ f ( xend ) = −8.3312 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 4.11 and the new
xend is 10

Iteration # 4: xstart := 4.11 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −93.5835 yend = 890 xnew := xend − ⋅ yend xnew = 4.6704
yend − ystart
3 4
f ( xstart) ⋅ f ( xnew ) = 5.2493 × 10 f ( xnew ) ⋅ f ( xend ) = −4.9922 × 10

The root must lie beteen above xnew and xstart. Thus the new xstart is 4.67 and the new
xend is 10

Iteration # 5: xstart := 4.67 xend := 10 ystart := f ( xstart) yend := f ( xend )


Chapter 3: Roots of Equations 63

xend − xstart
ystart = −56.1224 yend = 890 xnew := xend − ⋅ yend xnew = 4.9862
yend − ystart

3 4
f ( xstart) ⋅ f ( xnew ) = 1.7487 × 10 f ( xnew ) ⋅ f ( xend ) = −2.7732 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 4.986 and the new
xend is 10

Iteration # 6: xstart := 4.986 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −31.1731 yend = 890 xnew := xend − ⋅ yend xnew = 5.1557
yend − ystart

4
f ( xstart) ⋅ f ( xnew ) = 516.0963 f ( xnew ) ⋅ f ( xend ) = −1.4735 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 5.156 and the new
xend is 10

Iteration # 7: xstart := 5.156 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −16.5272 yend = 890 xnew := xend − ⋅ yend xnew = 5.2443
yend − ystart

3
f ( xstart) ⋅ f ( xnew ) = 141.6022 f ( xnew ) ⋅ f ( xend ) = −7.6254 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 5.244 and the new
xend is 10

Iteration # 8: xstart := 5.244 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −8.5964 yend = 890 xnew := xend − ⋅ yend xnew = 5.2895
yend − ystart

3
f ( xstart) ⋅ f ( xnew ) = 37.8307 f ( xnew ) ⋅ f ( xend ) = −3.9167 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 5.290 and the new
xend is 10

Iteration # 9: xstart := 5.290 xend := 10 ystart := f ( xstart) yend := f ( xend )


64 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xend − xstart
ystart = −4.3541 yend = 890 xnew := xend − ⋅ yend xnew = 5.3129
yend − ystart

3
f ( xstart) ⋅ f ( xnew ) = 9.6414 f ( xnew ) ⋅ f ( xend ) = −1.9707 × 10

The root must lie beteen above xnew and xend. Thus the new xstart is 5.313 and the new
xend is 10

Iteration # 10: xstart := 5.313 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −2.2078 yend = 890 xnew := xend − ⋅ yend xnew = 5.3246
yend − ystart

f ( xstart) ⋅ f ( xnew ) = 2.4707 f ( xnew ) ⋅ f ( xend ) = −995.9944

The root must lie beteen above xnew and xend. Thus the new xstart is 5.325 and the new
xend is 10

Iteration # 11: xstart := 5.325 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −1.0813 yend = 890 xnew := xend − ⋅ yend xnew = 5.3307
yend − ystart

f ( xstart) ⋅ f ( xnew ) = 0.5916 f ( xnew ) ⋅ f ( xend ) = −486.9595

The root must lie beteen above xnew and xend. Thus the new xstart is 5.331 and the new xend
is 10

Iteration # 12: xstart := 5.331 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −0.5163 yend = 890 xnew := xend − ⋅ yend xnew = 5.3337
yend − ystart

f ( xstart) ⋅ f ( xnew ) = 0.1348 f ( xnew ) ⋅ f ( xend ) = −232.3231

The root must lie beteen above xnew and xend. Thus the new xstart is 5.334 and the new
xend is 10

Iteration # 13: xstart := 5.334 xend := 10 ystart := f ( xstart) yend := f ( xend )


Chapter 3: Roots of Equations 65

xend − xstart
ystart = −0.2334 yend = 890 xnew := xend − ⋅ yend xnew = 5.3352
yend − ystart

f ( xstart) ⋅ f ( xnew ) = 0.0275 f ( xnew ) ⋅ f ( xend ) = −104.9753

The root must lie beteen above xnew and xend. Thus the new xstart is 5.335 and the new
xend is 10

Iteration # 14: xstart := 5.335 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −0.139 yend = 890 xnew := xend − ⋅ yend xnew = 5.3357
yend − ystart

−3
f ( xstart) ⋅ f ( xnew ) = 9.7667 × 10 f ( xnew ) ⋅ f ( xend ) = −62.5217

The root must lie beteen above xnew and xend. Thus the new xstart is 5.336 and the new
xend is 10

Iteration # 15: xstart := 5.336 xend := 10 ystart := f ( xstart) yend := f ( xend )

xend − xstart
ystart = −0.0446 yend = 890 xnew := xend − ⋅ yend xnew = 5.3362
yend − ystart

−3
f ( xstart) ⋅ f ( xnew ) = 1.0062 × 10 f ( xnew ) ⋅ f ( xend ) = −20.0659 f ( xnew ) = −0.0225

The above process indicates that in this case, stagnation occurs as well, but the root is
being approached only from the side of the original ending point. Since the xnew computed
in the fifteenth iteration is about the same as the one in the fourteenth, it is reasonable to say
that the xnew ( = 5.336 ) computed above is, in fact, the required root.

3.5 NEWTON-RAPHSON METHOD

This is the most widely used iterative method for locating roots. In this method, an initial
approximation of the root must be assumed, and calculations are started with a "good
initial guess" . If this initial guess is not a good one, then divergence may occur.

By starting with an approximation of the root value , x i , and constructing a tangent to the
function curve at x i , an improved guess x i+1 can be determined as shown in Figure 3.3.
From Figure 3.3, the slope of the function f(x) at x i can be seen to be
66 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )
−f x i
( )
fprime x i =
x i+ 1 − x i
(3.6)

which gives the new improved value of the guess as

( )
f xi
xi+1 = xi −
fprime ( x i)
( )
= g xi (3.7)

where fprime( x i ) is the slope of the function at x i

Figure 3.3. Newton-Raphson method

Procedure for Finding Roots

1. Make a good initial guess. Call it X Old .

2. Improve the guess using

(
f X Old )
X New = X Old − (
= g XOld ) (3.8)
(
fprime X Old )
Chapter 3: Roots of Equations 67

3. Keep improving the guess using Equation (3.8).

4. Solution is done when a new improved value X New is almost equal to the
previous value X Old

Advantages and Disadvantages of the Method

While the Newton-Raphson method is faster than the Bisection method, is applicable to the
complex domain as well and can be extended to simultaneous nonlinear equations, it may
not converge in some situations. The solution may oscillate about a local maximum or
minimum, and if an initial estimate is chosen such that the derivative becomes zero at some
point in the iteration process, then a division by zero takes place and convergence will never
occur. Although convergence will occur quite rapidly if the initial estimate is sufficiently
close to the root, it is possible for it to be be slow when it is far from the root. Also, if the
roots are complex, they will never be generated with real initial guesses.

A worthwhile feature of the Newton-Raphson method is that the numerical process will
correct itself automatically for minor errors. Thus, any errors that are made in computing the
next guess will simply generate a different point for drawing the tangent line and will not have
any effect on the final answer.

Convergence Criterion for the Newton-Raphson Method

It can be mathematically shown that in order for the Newton-Raphson method to converge
to a real root, the absolute value of the derivative of the g(xi) of Equation (3.7) must
always be less than 1 , that is,

( )
g xi <1

This condition must be satisfied if convergence is to be attained. However, in some cases, it


may not hold for the initial guess.

Example 3.4

Using the Newton- Raphson method, solve : f(x)= x 3 - 4.2 x -8.5 =0 for a real root between 2
and 3.

Let us first draw a graph of the function in the given range

3
f ( x ) := x − 4.2 ⋅ x − 8.5 x := 2 , 2.1 .. 3
68 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

10

f ( x) 0

−5

− 10
2 2.2 2.4 2.6 2.8
x

2
The derivative of the given function is fprime ( x ) := 3 ⋅ x − 4.2
, and thus,

2
f (x) fprime ( x ) − f ( x ) ⋅ fdblprime ( x )
fdblprime ( x ) := 6 ⋅ x , g ( x ) := x − , gprime ( x ) := 1 −
fprime ( x ) 2
fprime ( x )

where fdblprime(x) is the second derivative of f(x) and gprime(x) is the derivative of g(x). Start
with an initial estimate of 2.5

xold := 2.5 xnew := g ( xold ) xnew = 2.732 gprime ( xnew ) = 0.0206

−4
xold := xnew xnew := g ( xold ) xnew = 2.7091 gprime ( xnew ) = 2.1887 × 10

−8
xold := xnew xnew := g ( xold ) xnew = 2.7088 gprime ( xnew ) = 2.3959 × 10

xold := xnew xnew := g ( xold ) xnew = 2.7088 gprime ( xnew ) = 0

Clearly, the root is the converged value 2.7088, and convergence has been established with the
absolute value of the slope of g(x) being less than 1 in all the iterations.

Example 3.5

Using the Newton- Raphson method, solve : f(x)= x 3 +7x2 +19x+13 = 0 for all roots, real as
well as complex.

Let us first draw a graph of the function


Chapter 3: Roots of Equations 69

3 2
f ( x ) := x + 7 ⋅ x + 19 ⋅ x + 13

which suggests that there is a real root between 0 and -2.5

x := −5 , −4.999 .. 0

20

10

f ( x) − 10

− 20

− 30

− 40
−4 −2 0
x

2
The derivative of the given function is fprime ( x ) := 3 ⋅ x + 14 ⋅ x + 19 with

2
f (x) fprime ( x ) − f ( x ) ⋅ fdblprime ( x )
fdblprime ( x ) := 6 ⋅ x , g ( x ) := x − , gprime ( x ) := 1 −
fprime ( x ) 2
fprime ( x )

where fdblprime(x) is the second derivative of f(x) and gprime(x) is the first derivative of g(x).
Let us start with an initial estimate of -2.5 in an attempt to find the real root.

xold := −2.5 xnew := g ( xold ) xnew = −0.1818 gprime ( xnew ) = 0.0389

xold := xnew xnew := g ( xold ) xnew = −0.7721 gprime ( xnew ) = 0.095

xold := xnew xnew := g ( xold ) xnew = −0.9768 gprime ( xnew ) = 0.0164

−4
xold := xnew xnew := g ( xold ) xnew = −0.9997 gprime ( xnew ) = 1.9933 × 10
70 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

−8
xold := xnew xnew := g ( xold ) xnew = −1 gprime ( xnew ) = 2.6521 × 10

xold := xnew xnew := g ( xold ) xnew = −1 gprime ( xnew ) = 0

From the above calculations, it is clear that the real root is the converged value of (-1).
Let us now determine the complex roots by starting with an initial estimate of (-4+i)

xold := −4 + i xnew := g ( xold ) xnew = −2.9024 + 1.122i

xold := xnew xnew := g ( xold ) xnew = −2.2579 + 2.5753i

xold := xnew xnew := g ( xold ) xnew = −2.636 + 1.9589i

xold := xnew xnew := g ( xold )


xnew = −3.0346 + 1.9196i

xold := xnew xnew := g ( xold ) xnew = −2.9957 + 2.001i

xold := xnew xnew := g ( xold ) xnew = −3 + 2i

xold := xnew xnew := g ( xold ) xnew = −3 + 2i <-- Converged

Clearly, if we had started with an initial estimate of (-4-i ), we would have obtained the
complex conjugate of the above complex root, as demonstrated below. However, there is no
need to do this every time, since a complex root will always have a complex conjugate
associated with it.

xold := −4 − i xnew = −3 + 2i
xnew := g ( xold )

xold := xnew xnew := g ( xold ) xnew = −2.2579 − 2.5753i


Chapter 3: Roots of Equations 71

xold := xnew xnew := g ( xold ) xnew = −2.636 − 1.9589i

xold := xnew xnew := g ( xold ) xnew = −3.0346 − 1.9196i

xold := xnew xnew := g ( xold ) xnew = −2.9957 − 2.001i

xold := xnew xnew := g ( xold )


xnew = −3 − 2i

xold := xnew xnew := g ( xold ) xnew = −3 − 2i <-- Converged

3.6 USE OF MATHCAD'S root AND polyroots FUNCTIONS

Use of root

The function root (f(z),z) returns the value of "z" at which the function f(z) goes to zero.
Here, both f(z) and z are to be scalar quantities. The procedure to be used will be clear
from the following example.

Let us find the root of the function in Example 3.4 which is

3
f ( x ) := x − 4.2 ⋅ x − 8.5

A guess value for the root must be first defined :

x := 2.5

Type the following to define " a" as the root of the given function

a := root ( f ( x ) , x )
72 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Type " a= " to see the root computed.

a = 2.7088

Use of polyroots

The function polyroots is a function in Mathcad which will return all roots of a polynomial at
the same time, be they real or complex. It does not require a guess value. polyroots (v)
returns all roots of an nth degree polynomial whose coefficients are given in the vector "v"
with (n+1) components. By choosing "matrix" from the "insert" menu, type in a vector "v "
as shown , beginning with the constant term, making sure that you insert all coefficients
even if they are zero. Then, polyroots(v) returns all roots at once. The following steps will
find the root of the polynomial of Example 3.4.

3
f ( x ) := x − 4.2 ⋅ x − 8.5 <-- Given polynomial

⎛ −8.5 ⎞
⎜ ⎟ <--
This is a vector of the coefficients of the polynomial whose
⎜ −4.2 ⎟ roots are to be found. Include all coefficients, even zeros.
v :=
⎜ 0 ⎟ Begin with the constant term.
⎜ ⎟
⎝ 1 ⎠

⎛⎜ −1.3544 − 1.1417i ⎞⎟
polyroots ( v ) = ⎜ −1.3544 + 1.1417i ⎟ <-- Returns all roots at the same time.
⎜ ⎟
⎝ 2.7088 ⎠

3.7 SECANT METHOD

The Secant method is very similar to the Newton-Raphson method. The one difference is
that while the derivative fprime(x) is evaluated analytically in the Newton-Raphson method, it
is determined numerically in the Secant method .

From Figure 3.4, it is clear that an approximation for the slope of the given function f(x)
at x i can be written as

(( )
− f x i− 1 − f x i( ))
( )
fprime x i =
xi − xi−1
(3.9)
Chapter 3: Roots of Equations 73

Figure 3.4. Secant method

By the Newton-Raphson method, a new improved guess of the root is generated using
Equation 3.7, which is

( )
f xi
xi+1 = xi − (3.10)
fprime ( x i)

Combining Equations 3.9 and 3.10 yields the equation used in the Secant method, which is

( )(
f x i ⋅ xi−1 − xi )
xi+1 = xi − (3.11)
f ( x i− 1) − f ( x i)

Note that the format of Equation 3.11 does utilize the derivative but computes it numerically
rather than analytically. However, two initial estimates x i-1 and x i of the solution will be
required to start the iteration process as can be seen from Equation 3.11.

Procedure for Finding Roots

1. Make initial estimates x 0 and x 1 .

2. With i= 1, improve the guess x 2 using Equation 3.11, which is


74 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )(
f x i ⋅ xi−1 − xi )
xi+1 = xi −
f ( x i− 1) − f ( x i)

3. Continue obtaining improved estimates x 3 , x4 ...... using the above equation.

4. Solution is done when a new improved value x i+1 is almost equal to the previous
value x i

Advantages and Disadvantages of the Secant Method

The Secant method has a definite advantage when the derivative of the given function is
difficult or time-consuming to evaluate analytically. Among the disadvantages are
convergence to an unintended root at times, and divergence from the root if the initial
guesses are bad. Also, if f(x) is far from linear near the root, the iterations may start to
yield points far away from the actual root.

Example 3.6

Obtain a real root of f(x)= 1.15 x 3 + 1.2 x2 - 3.5 x - 3.12 between 0 and 10 by the Secant
Method.

We first draw a graph of the given function to see approximately where a real root lies in
the range given.

3 2
f ( x ) := 1.15 ⋅ x + 1.2 ⋅ x − 3.5 ⋅ x − 3.12

x := 0 , 0.01 .. 3

Graph of functon for x= 0 to 3


30

20 From the plot, it appears like


the root lies between 1 and 3.
f ( x) 10 We will, therefore, choose x0
to be 1 and x1 to be 2
0

− 10
0 1 2 3
x
Chapter 3: Roots of Equations 75

We will resort to Mathcad's vector notation ( subscript form ) in the use of the Secant Method
to determine the root of the given function f(x). To type a subscript, use the left bracket " [ "and
put an integer in the placeholder. Vector elements in Mathcad are ordinarily numbered
starting with the first element numbered as the zeroth element. A look at the Secant Method
suggests that this feature relates well with its recursive formula. Put in "N" as the expected
number of iterations. Then i goes from 1 to N . To find the required root, begin with the initial
estimates x 0 and x 1 and use the recursive formula of the Secant Method to obtain improved
estimates. Continue until convergence to a solution takes place.

N := 6 i := 1 .. N x 0 := 1 x 1 := 2

( )(
f x i ⋅ x i− 1 − x i )
x i+1 := x i − x 2 = 1.523926 x 3 = 1.662739 x 4 = 1.6996
f ( xi−1) − f ( xi)

x 5 = 1.6953 x 6 = 1.6954 x 7 = 1.6954

xi−1 = xi = xi+1 =
0 0 0
0 1 0 2 0 1.5239
1 2 1 1.5239 1 1.6627
2 1.5239 2 1.6627 2 1.6996
3 1.6627 3 1.6996 3 1.6953
4 1.6996 4 1.6953 4 1.6954 <---- Convergence has occurred here
5 1.6953 5 1.6954 5 1.6954

Let us solve this problem by Newton Raphson Method, using subscripts and compare answers

3 2 2
f ( x ) := 1.15 ⋅ x + 1.2 ⋅ x − 3.5 ⋅ x − 3.12 fprime ( x ) := 3.45 ⋅ x + 2.4 ⋅ x − 3.50

N := 4 i := 1 .. N x 1 := 2 <--starting estimate

( )
f xi
x i+1 := x i −
fprime ( x i)

x 2 = 1.743046 x 3 = 1.696894 x 4 = 1.6954 x 5 = 1.6954


76 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xi = xi+1 =
0 0
0 2 0 1.743
1 1.743 1 1.6969
2 1.6969 2 1.6954 <----Convergence has occurred here.
3 1.6954 3 1.6954

Let us verify the above root by resorting to the polyroots function

3 2 ⎛ −3.12 ⎞
f ( x ) := 1.15 ⋅ x + 1.2 ⋅ x − 3.5 ⋅ x − 3.12 ⎜ ⎟ ⎛⎜ −1.8941 ⎞⎟
⎜ −3.5 ⎟
v := polyroots ( v ) = ⎜ −0.8448 ⎟
⎜ 1.2 ⎟ ⎜ 1.6954 ⎟
⎜ ⎟ ⎝ ⎠
⎝ 1.15 ⎠

which indicates that there is one root between x = 0 and x = 10.

Example 3.7

Obtain all roots, real as well as complex, of the function f(x) of Example 3.5 by the Secant
method.

3 2
f ( x ) := x + 7 ⋅ x + 19 ⋅ x + 13

To determine the real root, we will consider initial estimates of 1 and 2 as follows.

N := 9 i := 1 .. N x 0 := 1 x 1 := 2

( )(
f x i ⋅ x i− 1 − x i)
x i+1 := x i − x 2 = 0.148936 x 3 = −0.267833 x 4 = −0.7285
f ( xi−1) − f ( xi)

x 5 = −0.9224 x 6 = −0.9904 x 7 = −0.9996 x 8 = −0.999998

x 9 = −1 x 10 = −1
Chapter 3: Roots of Equations 77

xi−1 = xi = xi+1 =
0 0 0
0 1 0 2 0 0.1489
1 2 1 0.1489 1 -0.2678
2 0.1489 2 -0.2678 2 -0.7285
3 -0.2678 3 -0.7285 3 -0.9224
4 -0.7285 4 -0.9224 4 -0.9904
5 -0.9224 5 -0.9904 5 -0.9996
6 -0.9904 6 -0.9996 6 -1
7 -0.9996 7 -1 7 -1
8 -1 8 -1 8 -1
<--- Convergence has occurred here

To find the complex roots, let us use initial estimates of (-4+i) and (-4+2i).

x 0 := −4 + i x 1 := −4 + 2i N := 7 j := 1 .. N

( )(
f xj ⋅ xj −1 − xj )
x j + 1 := x j − x 2 = −3.211618 + 1.456432i
f ( x j −1) − f ( xj )

x 3 = −2.929182 + 1.735276i x 4 = −2.9136 + 2.0766i

x 5 = −3.0167 + 1.9899i x 6 = −3.0009 + 2.0008i

x 7 = −3 + 2i x 8 = −3 + 2i

⎛⎜ −4 + i ⎞⎟ ⎛⎜ −4 + 2i ⎞⎟ ⎛⎜ −3.2116 + 1.4564i ⎞⎟
⎜ −4 + 2i ⎟ ⎜ −3.2116 + 1.4564i ⎟ ⎜ −2.9292 + 1.7353i ⎟
⎜ −3.2116 + 1.4564i ⎟ ⎜ −2.9292 + 1.7353i ⎟ ⎜ −2.9136 + 2.0766i ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
x j − 1 = ⎜ −2.9292 + 1.7353i ⎟ x j = ⎜ −2.9136 + 2.0766i ⎟ x j + 1 = ⎜ −3.0167 + 1.9899i ⎟
⎜ −2.9136 + 2.0766i ⎟ ⎜ −3.0167 + 1.9899i ⎟ ⎜ −3.0009 + 2.0008i ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ −3.0167 + 1.9899i ⎟ ⎜ −3.0009 + 2.0008i ⎟ ⎜ −3 + 2i ⎟
⎜ −3.0009 + 2.0008i ⎟ ⎜ ⎟ ⎜ ⎟
⎝ ⎠ ⎝ −3 + 2i ⎠ ⎝ −3 + 2i ⎠
78 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Convergence has occurred with a complex root determined as ( - 3 + 2i ). Clearly, the


other complex root will be the complex conjugate (-3 - 2i) .

Resorting to the polyroots function will confirm the roots generated above.

⎛ 13 ⎞
3 2
f ( x ) := x + 7 ⋅ x + 19 ⋅ x + 13
⎜ ⎟ ⎛⎜ −3 + 2i ⎞⎟
v :=
⎜ 19 ⎟ polyroots ( v ) = ⎜ −3 − 2i ⎟
⎜7 ⎟ ⎜ −1 ⎟
⎜ ⎟ ⎝ ⎠
⎝1 ⎠

3.8 METHOD OF SUCCESSIVE SUBSTITUTION

This method encompasses all iteration schemes for finding roots of nonlinear functions.
Applications of this technique can be found in the use of the Newton-Raphson method and
the Secant method. Another name for this method is Fixed Point Iteration. In this
method, the equation f(x) = 0 is written in the form x = g(x). Thus, if x 1 is an assumed
initial approximation to the root, then a successive approximation can be generated using

x i+ 1 = g ( x i ) (3.12)

Iteration must be pursued until convergence to a root is indicated

Advantages and Disadvantages of the Iteration Method

While the method provides a simple computational technique for finding roots of
equations, offers flexibility in the selection of the function g(x), and is easy to program, it
is still possible for the solution to diverge with improper choices of the function. To insure
convergence of the iteration, the function selected must be such that the absolute value of
its slope is always less than unity. Otherwise, divergence will occur.

Example 3.8

The function y(x)= x 2 - 3 x + e x - 4 has two roots, one positive and the other negative.
Determine the negative root by the method of successive substitution.

Rewrite the given equation as follows

2 x
y ( x ) := x − 3 ⋅ x + e − 4
Chapter 3: Roots of Equations 79

( )
2 x
x +e −4 1 x
g ( x ) := x := g ( x ) gprime ( x ) := ⋅ 2⋅x + e
3 3

Let the initial guess be 0. Using the above recursive relationship, we find revised values of x until
convergence occurs as shown below.

x := 0 g ( x ) = −1
Iteration # 1:

Iteration # 2: x := g ( x ) x = −1 g ( x ) = −0.8774 gprime ( x ) = 0.544

Iteration # 3: x := g ( x ) x = −0.8774 g ( x ) = −0.9381 gprime ( x ) = 0.4463

x := g ( x ) x = −0.9381 g ( x ) = −0.9095 gprime ( x ) = 0.495

x := g ( x ) x = −0.9095 g ( x ) = −0.9233 gprime ( x ) = 0.4721

x := g ( x ) x = −0.9233 g ( x ) = −0.9167 gprime ( x ) = 0.4832

x := g ( x ) x = −0.9167 g ( x ) = −0.9199 gprime ( x ) = 0.4779

x := g ( x ) x = −0.9199 g ( x ) = −0.9184 gprime ( x ) = 0.4804

x := g ( x ) x = −0.9184 g ( x ) = −0.9191 gprime ( x ) = 0.4792

x := g ( x ) x = −0.9191 g ( x ) = −0.9188 gprime ( x ) = 0.4798

Iteration # 11: x := g ( x ) x = −0.9188 g ( x ) = −0.9189 gprime ( x ) = 0.4795

Note that convergence has taken place at x = -0.919, and the absolute value of the slope
of the function selected is less than unity throughout the iteration process.

i := 0 , 1 .. 9 x 0 := 0 ( )
x i+1 := g x i
80 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

0
0 0
1 -1
2 -0.8774
3 -0.9381
4 -0.9095
x=
5 -0.9233
6 -0.9167
7 -0.9199
8 -0.9184
9
10
-0.9191
-0.9188
<--- Convergence occurs here ---> ( )
y x 10 = −5.0333 × 10
−4

3.9 MULTIPLE ROOTS AND DIFFICULTIES IN COMPUTATION

When the given function makes contact with the x- axis and has a slope of zero, multiple roots
can occur as indicated in Figure 3.5. These roots are difficult to compute by the methods
discussed in this chapter for the following reasons.

1. In the case of the Bisection method, the function does not change sign at the root.

2. In the case of Newton-Raphson and Secant methods, the derivative at the


multiple root is zero.

Figure 3.5. Case of multiple roots

Because the Newton-Raphson and Secant methods in their orginal forms are methods that
resort to linear convergence, they cannot be employed to generate multiple roots. The following
modification to the original Newton-Raphson equation has been suggested [ 4 ].
Chapter 3: Roots of Equations 81

( )
f xi
xi+1 = xi − m ⋅ (3.13)
fprime ( x i)

where m is the multiplicity of the root ( for example, m = 2 for a double root ). However, this
may not be a very practical route since it assumes prior knowledge about the multiplicity of
a root.

Another approach, also suggested [ 4 ] involves defining a new function u(x)

f (x)
u (x) = (3.14)
fprime ( x )

which has the same roots as the original given function f(x), and then resorting to the
following alternative form of the Newton-Raphson formula

( )
u xi
xi+1 = xi − (3.15)
uprime ( x i)

where uprime(x) represents the derivative of the new function u(x). Substitution of Equation
(3.14) into the Newton-Raphson equation, which is now Equation (3.15), yields the desired
formula

( )
f x i ⋅ fprime x i ( )
xi+1 = xi − (3.16)
( )
fprime x i
2
( ) ( )
− f x i ⋅ fdoubleprime x i

where fprime and fdoubleprime represent first and second derivatives respectively.

The modified formula for the Secant Method that will be suitable for the generation of multiple
roots can be obtained by simply substituting u(x) for f(x) in the Secant method formula giving

( )(
u x i ⋅ xi−1 − xi )
xi+1 = xi − (3.17)
u ( xi−1) − u ( xi)

Example 3.9

The function

f(x)= x3 - 4 x 2 + 5 x - 2 = (x-1) (x-1) (x-2)


82 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

has a double root at 1 and a single root at 2, which we will verify using Equation (3.16).

3 2
f ( x ) := x − 4 ⋅ x + 5 ⋅ x − 2

The first and second derivatives of the given function a re

2
fprime ( x ) := 3 ⋅ x − 8 ⋅ x + 5 fdoubleprime ( x ) := 6 ⋅ x − 8

In order to determine the multiple root by the modified form of the Newton-Raphson formula ,
let us employ Equation (3.16) and start with an initial estimate of 0.25.

( )
f x i ⋅ fprime x i( )
xi+1 = xi −
fprime x i( )2 − f (xi) ⋅ fdoubleprime (xi)

f ( xold ) ⋅ fprime ( xold )


xold := 0.25 xnew := xold − xnew = 1.0841
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1.0042
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

It is clear that the multiple root is the converged value of 1. To determine the single root, the
above approach can still be employed, but with a different starting estimate, as shown below.

f ( xold ) ⋅ fprime ( xold )


xold := 2.50 xnew := xold − xnew = 1.8182
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )
Chapter 3: Roots of Equations 83

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1.9101
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1.9809
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 1.9992
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

f ( xold ) ⋅ fprime ( xold )


xold := xnew xnew := xold − xnew = 2
2
fprime ( xold ) − f ( xold ) ⋅ fdoubleprime ( xold )

Thus, the single root is the converged value of 2. The following steps will now demonstrate the
application of the modified Secant Method formula, which is Equation 3.17, to the
determination of the multiple root at 1.

3 2 2 f ( x)
f ( x ) := x − 4 ⋅ x + 5 ⋅ x − 2 fprime ( x ) := 3 ⋅ x − 8 ⋅ x + 5 u ( x ) :=
fprime ( x )

i := 0 .. 5

( )(
u x i ⋅ x i− 1 − x i )
x i+1 := x i − x0 := 0.25 x1 := 0.26
u ( x i− 1) − u ( x i)

u ( x1) ⋅ ( x0 − x1)
x2 := x1 − x2 = 1.0835
u ( x0) − u ( x1)

u ( x2) ⋅ ( x1 − x2)
x3 := x2 − x3 = 0.9802
u ( x1) − u ( x2)

u ( x3) ⋅ ( x2 − x3)
x4 := x3 − x4 = 0.9991
u ( x2) − u ( x3)

u ( x4) ⋅ ( x3 − x4)
x5 := x4 − x5 = 1 <--- Convergence at the double root x =1
u ( x3) − u ( x4)

Application of the polyroots and root functions confirms the correctness of the above
analysis.
84 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛ −2 ⎞
⎜ ⎟ ⎛⎜ 0.9995 ⎞⎟
v :=
⎜5 ⎟ polyroots ( v ) = ⎜ 1.0005 ⎟
⎜ −4 ⎟ ⎜ 2 ⎟
⎜ ⎟ ⎝ ⎠
⎝1 ⎠

Guess x := 0.5 a := root ( f ( x ) , x ) generates a= 1

Guess x := 3 a := root ( f ( x ) , x ) generates a= 2

3.10 SOLUTION OF SYSTEMS OF NONLINEAR EQUATIONS

The methods discussed so far deal with finding the roots of functions of only a single
variable. In certain situations, it may be necessary to solve nonlinear equations in two or
more variables. In these cases, iteration can be resorted to as presented in the following
example.

Example 3.10

Solve: 1.1 x2 +1.2 xy = 10.5 ; y + 3.2 x y2 = 58.2

Let us put the given equations in the form

2
10.5 − 1.1 ⋅ x 2
x= y = 58.2 − 3.2 ⋅ x ⋅ y
1.2 ⋅ y

and make the following initial guesses:

x := 1 y := 3

2
10.5 − 1.1 ⋅ x 2
Then, x := and y := 58.2 − 3.2 ⋅ x ⋅ y yield
1.2 ⋅ y

x = 2.6111 y = −17

A second iteration gives


Chapter 3: Roots of Equations 85

2
10.5 − 1.1 ⋅ x 2
x := x = −0.1471 y := 58.2 − 3.2 ⋅ x ⋅ y y = 194.214
1.2 ⋅ y

Notice that the approach resorted to is diverging and will not yield a solution. Thus, the
computations must be repeated with equations set up in a different format as shown below.

Iteration #

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(1) x := 1 y := 3 x := x = 2.5045 y := y = 2.6244
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(2) x = 2.5045 y = 2.6244 x := x = 1.5411 y := y = 3.357
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(3) x = 1.5411 y = 3.357 x := x = 1.9753 y := y = 2.9456
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(4) x = 1.9753 y = 2.9456 x := x = 1.7883 y := y = 3.1073
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(5) x = 1.7883 y = 3.1073 x := x = 1.8664 y := y = 3.0372
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(6) x = 1.8664 y = 3.0372 x := x = 1.8335 y := y = 3.0663
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(7) x = 1.8335 y = 3.0663 x := x = 1.8473 y := y = 3.054
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(8) x = 1.8473 y = 3.054 x := x = 1.8415 y := y = 3.0591
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(9) x = 1.8415 y = 3.0591 x := x = 1.8439 y := y = 3.057
1.1 3.2 ⋅ x

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(10) x = 1.8439 y = 3.057 x := x = 1.8429 y := y = 3.0579
1.1 3.2 ⋅ x
86 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

10.5 − 1.2 ⋅ x ⋅ y 58.2 − y


(11) x = 1.8429 y = 3.0579 x := x = 1.8433 y := y = 3.0575
1.1 3.2 ⋅ x

Notice that after ten iterations convergence has taken place yielding the solution as : x =1.843
and y = 3.058. This example illustrates a very serious disadvantage associated with the
successive substitution method which is the dependence of convergence on the format in which
the equations are put and utilized in the iteration process. Also , even in situations where a
converged solution can be attained, initial estimates that are fairly close to the true solution
must be resorted to, because, otherwise, divergence may occur and a solution may never be
obtained. The results of the iterative process are summarized in the following table.

----------------------------------------------------------------------------------------------------------------------------
Iteration # x y
------------------------------------------------------------------------------------------------------------------------------
0 init=1.0 3.00
1 2.505 2.624
2 1.541 3.357
3 1.975 2.946
4 1.788 3.107
5 1.866 3.037
6 1.833 3.066
7 1.847 3.054
8 1.841 3.059
9 1.844 3.057
10 1.843 3.058
11 1.843 3.058
----------------------------------------------------------------------------------------------------------------------------------

3.11 SOLVING SYSTEMS OF EQUATIONS USING MATHCAD'S Given


AND Find FUNCTIONS

Mathcad allows you to solve a system of 50 simultaneous equations in 50 unknowns. The


procedure is as follows:

1. Provide initial guesses for all unknowns. These initial guesses give Mathcad a place to
start searching for the solution.

2. Type the word Given. This tells Mathcad that what follows is a system of equations. Given
can be typed in any combination of upper and lower case letters, and in any font. However, it
should not be typed in a text region.

3. Then type the equations and inequalities in any order below the word Given . Make sure
you separate the left and right sides of an equation by an " = ". Press [Ctrl]= to type " = ".
You can of course separate the left and right sides of an inequality with the appropriate symbol.

4. a:= Find(x,y.....) returns the solution to the system of equations.


Chapter 3: Roots of Equations 87

Example 3.11.

Using the Given and Find functions, solve

2 2
1.5 ⋅ x + 2 ⋅ y = 6.5. x + y = 2.75

Provide initial guesses: x := 1 y := 1

Given
2 2
1.5 ⋅ x + 2 ⋅ y = 6.5
x + y = 2.75

a := Find ( x , y )

⎛ 1.5 ⎞
a= ⎜ ⎟ <--- Solution
⎝ 1.25 ⎠

Alternatively, do the following sequence to obtain xval and yval as the solution

2 2
Given 1.5 ⋅ x + 2 ⋅ y = 6.5 x + y = 2.75

⎛ xval ⎞
⎜ ⎟ := Find ( x , y ) xval = 1.5 yval = 1.25 <--Solution
⎝ yval ⎠

3.12. APPLICATIONS IN ROOT-FINDING

The following are examples of situations where roots of nonlinear equations need to be
evaluated.

3.12.1 Maximum Design Load for a Column

A steel pipe with an outside diameter of 10.70 inches and a wall thickness of 0.375 inches is
to serve as a pin-ended column that is 22 feet long . It supports an eccentric load at an
eccentricity of e = 1.5 inches from the column center-line. The maximum load that the
column can carry with a factor of safety of 2.75 is to be determined. The modulus of
elasticity E for steel is 30 x 10 6 psi and its yield strength, σyld , is 40,000 psi .

The equation for the stress level σ max resulting from the application of the maximum load
P max is [ 3,18 ]
88 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Pmax ⎛ e⋅c ⎛ Pmax ⎞ ⎞⎟


⋅⎜1 +
L (3.18)
σmax = ⋅ sec ⎜ ⋅ ⎟
A ⎜ 2 ⎝ A ⋅E 2⋅K ⎠⎟
⎝ K ⎠

where A is the column cross-sectional area, e is the eccentricity at which the load is applied,
as shown in Figure 3.6 , c is the distance of the outermost fiber in the cross-section from the
neutral axis , K is the radius of gyration , L is the column length and E is the modulus of
elasticity. This relationship between σ max and P max is not linear and there is no proportionality
between the applied load and the stress that is produced. Thus, it must be solved iteratively
using trial and error. How this can be accomplished with Mathcad is demonstrated below.

Figure 3.6. Eccentrically Loaded Column

Here,
6
σyld := 40000. (psi) E := 30 ⋅ 10 (psi) FS := 2.75

do := 10.70 t := 0.375 di := do − 2 ⋅ t di = 9.95 <--- all dimensions in inches


Chapter 3: Roots of Equations 89

do
e := 1.50 L := 264. c := c = 5.35
2

I :=
64
(π 4
⋅ do − di
4 ) ( in 4 ) A :=
π
4
( 2
⋅ do − di
2 ) (in 2 )

I
A = 12.1639 I = 162.3057 K := K = 3.6528 (in )
A

With a factor of safety of 2.75, the maximum stress level that the column can have is

σyld 4
σmax := σmax = 1.4545 × 10 psi
FS

The corresponding maximum allowable load may be determined from Equation (3.18) using
the root function as follows.

Pmax
( )
L
g Pmax := ⋅
E ⋅A 2⋅K


))⎞⎟
Pmax e⋅c
( )
f Pmax := σmax −
A
⋅⎜1 +
2
( (
⋅ sec g Pmax
⎝ K ⎠

Pmax := 125000. (lbs) <--Guess value for the root

((
a := root f Pmax , Pmax ) )

5
a = 1.0221 × 10 (lbs) <-- Maximum allowable load column can carry

3.12.2. Natural Frequencies of Vibration of a Uniform Beam

For a uniform beam that is clamped at one end and free at the other, a shown in Figure 3.7 ,
the natural frequencies of vibration [20] are given by the equation

1 + cos ( βprime) ⋅ cosh ( βprime) = 0 (3.19)


90 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

where
(3.20)
βprime = β ⋅ L

1
4
⎛ ρ ⋅ ω2 ⎞
and β=⎜ ⎟ (3.21)
⎝ EI ⎠

in which ρ is the mass per unit length of the beam, EI is the flexural rigidity or bending stiffness
about the neutral axis , and ω refers to the natural frequencies . For a beam with a length of 20
feet, an EI value of 16 x 10 6 lb-in 2 , and a weight per unit length of 0.03 lb/ in , the natural
frequencies may be determined as shown below.

Figure 3.7. A Fixed-Free Beam

ρwt ⋅ 12
ρwt := 0.03 lb/in --> ρ := --> ρ = 0.0112 (lb- ft -2 sec 2 )
32.2

6
6 16 ⋅ 10 5
EI := 16 ⋅ 10 lb-in 2 --> EI := --> EI = 1.1111 × 10 (lb- ft 2 )
144

L := 20 ft

βprime := 1 <-- Guess value for root

f ( βprime) := 1 + cos ( βprime) ⋅ cosh ( βprime)

a := root ( f ( βprime) , βprime) a = 1.8751


Chapter 3: Roots of Equations 91

a
β := β = 0.0938
L

4
EI ⋅ β
ω := ω = 27.7106 (rad/sec) <-- First or fundamental
ρ natural frequency

βprime := 5 <-- Guess value for next root

f ( βprime) := 1 + cos ( βprime) ⋅ cosh ( βprime)

a := root ( f ( βprime) , βprime)

a = 4.6941

a
β := β = 0.2347
L

4
EI ⋅ β
ω := ω = 173.6595 (rad/sec) <-- Second natural frequency
ρ

Similarly by making appropriate guesses, higher natural frequencies can be determined


using the root function. Some of these are listed below.

Third natural frequency: 486.3 rad/sec


Fourth natural frequency: 952.8 rad/sec
Fifth natural frequency: 1575.1 rad/sec
Sixth natural frequency: 2353 rad/sec
Seventh natural frequency: 3286 rad/sec.

3.12.3. Solving the Characteristic Equation in Control Systems Engineering

A characteristic equation is an algebraic equation that is formulated from the differential


equation or equations of a control system [14] . Its solution, which often requires evaluation
of the roots of a polynomial of degree higher than two, is crucial in determining system
stability and assessing system transient response in terms of its time constant, natural
frequencies , damping qualities etc. An application involving the use of Mathcad' s polyroots
function in determining the roots of a characteristic polynomial of a control system is
presented below.
92 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

For the unity - feedback system shown in Figure 3.8, the characteristic polynomial is

3 2
s + 12 ⋅ s + 20 ⋅ s + K = 0

where K is a variable system parameter that can be selected based on the performance
requirements that may be prescribed. For varying values of this parameter, the roots of the
characteristic polynomial can be evaluated as shown below, and the system transient
response and stability studied , following which, an appropriate range for K may be
recommended.

Figure 3.8. Feedback Control System Block Diagram

⎛1 ⎞
⎜ ⎟ ⎛⎜ −10.0125 ⎞⎟
K := 1
v :=
⎜ 20 ⎟ r := polyroots ( v ) r = ⎜ −1.9359 ⎟
<-- Negative real roots
⎜ 12 ⎟ ⎜ −0.0516 ⎟
indicate a stable
⎜ ⎟ ⎝ ⎠ system
⎝1 ⎠

⎛ 10 ⎞
⎜ ⎟
K := 10 ⎜ 20 ⎟ ⎛⎜ −10.1216 ⎞⎟
v := <-- Negative
⎜ 12 ⎟ r := polyroots ( v ) r = ⎜ −0.9392 − 0.3255i ⎟
real parts of
⎜ ⎟ ⎜ −0.9392 + 0.3255i ⎟
⎝1 ⎠ ⎝ ⎠ root
indicate a
stable
system
Chapter 3: Roots of Equations 93

⎛ 100 ⎞
⎜ ⎟ ⎛⎜ −11.0084 ⎞⎟ <-- Negative
K := 100 v :=
⎜ 20 ⎟
r := polyroots ( v ) r = ⎜ −0.4958 + 2.9729i ⎟ real parts
⎜ 12 ⎟
⎜ ⎟ ⎜ −0.4958 − 2.9729i ⎟ of root
⎝ ⎠ indicate a
⎝ 1 ⎠
stable
system
⎛ 400 ⎞
⎜ ⎟ ⎛⎜ −12.8628 ⎞⎟
K := 400 v :=
⎜ 20 ⎟
r := polyroots ( v ) r = ⎜ 0.4314 + 5.5598i ⎟
<-- Positive
⎜ 12 ⎟ real
⎜ 0.4314 − 5.5598i ⎟
⎜ ⎟ ⎝ ⎠ parts of root
⎝ 1 ⎠ indicate an
unstable
system

3.12.4. Horizontal Tension in a Uniform Cable

Flexible cables are often used in suspension bridges, transmission lines, telephone lines ,
mooring lines and many other applications. A cable hanging under its own weight takes the
shape of a catenary in which the deflection y along the span is given by the following
relationship [ 8 ]

H ⎛ ⎛ w ⋅ x ⎞ − 1⎞
y= ⋅ ⎜ cosh ⎜ ⎟ ⎟ (3.22)
w ⎝ ⎝ H ⎠ ⎠

where x is the horizontal distance along the span a shown in Figure 3.9 , H is the horizontal
component of tension in the cable and w is the weight per unit length of the cable.

Figure 3.9. Catenary cable


94 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The constant H is obtained by using the boundary condition that y = h at x = L/2, which gives

H ⎛ ⎛ w ⋅ L ⎞ − 1⎞
h+ ⋅ ⎜ cosh ⎜ ⎟ ⎟ (3.23)
w ⎝ ⎝ 2⋅H ⎠ ⎠

Thus, the root of the following equation must be evaluated by using a method such as the
Secant method

h = g(H)
(3.24)
where

H ⎛ ⎛ w ⋅ L ⎞ − 1⎞
g(H) = ⋅ ⎜ cosh ⎜ ⎟ ⎟ (3.25)
w ⎝ ⎝ 2⋅H ⎠ ⎠

For a cable with a span of L= 22 meters, a weight per unit length of w =5.5 N/m and a
maximum sag of h= 6.5 meters,

h := 6.5 w := 5.5 L := 22

The intersection of the plot of g(H) with h= 6.5 in Figure 3.10 suggests that f(H) has a root
at about H = 50 Newtons.

H ⎛ ⎛ w ⋅ L ⎞ − 1⎞ H ⎛ ⎛ w ⋅ L ⎞ − 1⎞
g ( H ) := ⋅ ⎜ cosh ⎜ ⎟ ⎟ H := 10 , 11 .. 200 f ( H ) := h − ⋅ ⎜ cosh ⎜ ⎟ ⎟
w ⎝ ⎝ 2⋅H ⎠ ⎠ w ⎝ ⎝ 2⋅H ⎠ ⎠
Chapter 3: Roots of Equations 95

400

300
h
200
g( H)
100

0
0 50 100 150 200
H

Figure 3.10. Plot of g(H)

The use of the Secant method in obtaining the required root is shown below.

N := 7 i := 1 .. N H 0 := 25 H 1 := 35 <--initial required guesses

( )(
f H i ⋅ H i− 1 − H i )
H i+ 1 := H i − <--recursive formula of Secant method
f ( H i− 1) − f ( H i)

H 2 = 41.1846 H 3 = 49.0109 H 4 = 53.8261

H 5 = 55.9053 H 6 = 56.2878 <-- improved


estimates

H 7 = 56.31 H 8 = 56.3102

H i− 1 = Hi = H i+ 1 =
0 0 0
0 25 0 35 0 41.1846
1 35 1 41.1846 1 49.0109
2 41.1846 2 49.0109 2 53.8261
3 49.0109 3 53.8261 3 55.9053
4 53.8261 4 55.9053 4 56.2878
5 55.9053 5 56.2878 5 56.31
<----Convergence has occurred here
6 56.2878 6 56.31 6 56.3102

Mathcad's root function can also be used to determine the root of f(H) . This is done below.

Guess: H := 35 a := root ( f ( H ) , H ) a = 56.3102

Thus, by solving f(H)=0, the horizontal tension in the cable has been found as H= 56.31 N.
96 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

PROBLEMS
3.1. Obtain a root of the following polynomial lying in the range 1 to 3 by the Bisection
method

3 2
f ( x ) = x − 3.45 ⋅ x + 3.35 ⋅ x − 0.934

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and give it a
title . Verify your answer by using the root function. Also obtain all the roots of the polynomial
with the polyroots function.

3.2. Estimate a root of the polynomial

3 2
x − 3.5 ⋅ x + 3.28 ⋅ x − 0.924

between x = 2 and x = 3 using the Bisection method. Generate an answer that is good to
three decimal places.

3.3 Estimate a root of the polynomial

3 2
x − 6.1 ⋅ x + 11.26 ⋅ x − 6.336

between x = 1.5 and x = 2 using the Bisection method.

3.4. Obtain all roots of the following polynomial lying in the range -5.5 to 4.0 by the
Newton-Raphson method

3 2
f ( x ) = 1.1 ⋅ x + 1.15x − 17.1 ⋅ x + 15.2

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and give it a
title . Verify your answers by using the root function. Also obtain all the roots of the polynomial
with the polyroots function.

3.5. Obtain all roots of the following polynomial lying in the range -2.5 to 1.5 by the
Newton-Raphson method

3 2
f ( x ) = x + 4.25 ⋅ x − 4.05

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and give it a
title . Verify your answers by using the root function. Also obtain all the roots of the polynomial
with the polyroots function.

3.6. Estimate a root of the polynomial


Chapter 3: Roots of Equations 97

3 2
x − 2⋅x − 5 ⋅x + 6

between x = 3 and x = 4 using the Newton-Raphson method.

3.7. Using the Newton-Raphson method, estimate a real root of the polynomial

( − x) 3
f (x) = e −x

between x = 0 and x =1. Obtain an answer that is correct to at least three decimal
places.

3.8. Using the Newton-Raphson method, estimate a real root of the polynomial

3
f ( x ) = x − 5 ⋅ cos ( x ) − 1

between x = 1 and x =2. Obtain an answer that is correct to at least three decimal places.

3.9. Using the Newton-Raphson method, estimate a real root of the polynomial

f ( x ) = cos ( x ) − x

between x = 0 and x =4. Obtain an answer that is correct to three decimal places.

3.10. Using the Newton-Raphson method, estimate a real root of the polynomial

−x 2
f (x) = e −x

between x = 0 and x =1. Obtain an answer that is correct to four decimal places.

3.11. Obtain all roots of the following polynomial lying in the range 0 to 1.0 by the
Secant method

3 2
f ( x ) = x − 1.72 ⋅ x + 0.85 ⋅ x − 0.110

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and give
it a title . Verify your answers by using the root function. Also obtain all the roots of the
polynomial with the polyroots function.

3.12. Obtain all roots of the following polynomial lying in the range 0.0 to 1.0 by the
98 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Secant method

4 3 2
f ( x ) = x − 3.05 ⋅ x + 2.75 ⋅ x + 0.43 ⋅ x − 0.45

Using Mathcad, draw a graph of the polynomial for the given range of x.Label it and give it a
title . Verify your answers by using the root function. Also obtain all the roots of the polynomial
with the polyroots function.

3. 13. Using the Secant method, estimate a real root of the polynomial

3 2
x + x − 17 ⋅ x + 15

between x = 1.5 and x =4. Start with initial estimates of 1.5 and 4.0.

3.14. Using the Secant method, estimate a real root of the polynomial

4 3 2
x − 2 ⋅ x − 15 ⋅ x − 4 ⋅ x + 20

between x = 2 and x =6. Start with initial estimates of 4.0 and 4.5.

3.15. Using the Secant method, estimate a real root of the polynomial

2
f ( x ) = x − sin ( x ) − 1

between x = 1 and x =2.

3.16. Using the Secant method, estimate a real root of the polynomial

4 3 2
f ( x ) = 5 ⋅ x − 2 ⋅ x − 25 ⋅ x − 6 ⋅ x + 45

between x = 1 and x =2. Generate an answer that is correct to four decimal places.

3.17. Using the Secant method, estimate a real root of the polynomial

x
f ( x ) = 2 ⋅ cos ( x ) − e

between x = 0 and x =1. Generate an answer that is correct to three decimal places.

3.18. Using the Secant method, estimate a real root of the polynomial
Chapter 3: Roots of Equations 99

−x 2
f (x) = e −x

between x = 0 and x =1. Generate an answer that is correct to three decimal places.

3.19. Using the Secant method, estimate a real root of the polynomial

4 3 2
f ( x ) = x − 5 ⋅ x − 40 ⋅ x − 12 ⋅ x + 24

between x = 0 and x =2. Generate an answer that is correct to threer decimal places.

3.20. Obtain a root of the following polynomial lying in the range -10 to 10 by the
method of Successive Substitution

3 2
f ( x ) := x + 0.25 ⋅ x − 0.75 ⋅ x − 0.350

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and
give it a title. Notice what this plot suggests in terms of the interval where the roots may
lie. Then, draw a second graph for a narrower range (-0.60 to 1.40). Start with an initial
estimate of 0.9. Verify your answer by using the root function. Also obtain all the roots of
the polynomial with the polyroots function.

3.21. Obtain a root of the following polynomial lying in the range --5 to 25 by the
method of Successive Substitution

3 2
f ( x ) = x − 17.45 ⋅ x − 82.89 ⋅ x − 84.50

Using Mathcad, draw a graph of the polynomial for the given range of x. Label it and give
it a title . Start with a reasonable initial estimate. Verify your answer by using the root
function. Also obtain all the roots of the polynomial with the polyroots function.

3.22. Obtain the solution of the following system of nonlinear equations by iteration

x 1.05⋅ x
e − 1.1 ⋅ y = 0 and 1.1 ⋅ x ⋅ y − e =0

Start with an initial estimate of y= 2.2. Verify your answers with the Given and Find
functions.

3.23. Using the Given and Find functions of Mathcad, obtain the solution to the following
system of equations.

3.25x- 1.01y + 2.1z = -3.72 ; 2.12x + 3.15 y - 2.2z = -18.25 ; -x + 2.22 y + 4.15 z = -6
100 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

3.24. The dynamic response of an underdamped second-order system, which is a


common mathematical model in control system analysis, to a unit step function is given
by [ 14 ]

1 − ζ ⋅ ωn⋅ t ⎡ ⎛ 2 ⎞⎤
c ( t) = 1 − ⋅e ⋅ ⎣cos⎝ ωn ⋅ 1 − ζ ⋅ t − ϕ⎠⎦
2
1−ζ

where ζ is the system damping ratio, ω n is the undamped natural frequency, and φ is

⎛ ζ ⎞⎟
ϕ = atan ⎜
⎜⎝ 1 − ζ 2 ⎟⎠

A precise analytical relationship between the damping ratio and the rise time which is the time
required for the response to go from 10 % of the final value to 90 % ( that is, from 0.1 to 0.9 in
this case ) cannot be determined . However, using Mathcad's root function, the normalized
rise time which is ω n times the rise time, can be found for a specified damping ratio ζ by
solving for the values of ω n t that yield c(t) = 0.9 and c(t)= 0.1 . Subtracting these two values
yields the normalized rise time for the value of ζ considered . Using a range of damping ratios
from 0.1, 0.2, 0.3... .. 0.9 , construct a plot depicting normalized rise time as a function of
the damping ratio .

3.25. The frequency equation for the free undamped longitudinal vibration of a slender bar
of length L and mass m with one end fixed and with the other end carrying a mass M is
given by [ 21 ]

β ⋅ tan ( β) = α

where
L
β = ω⋅
c0

E
c0 =
ρ
Figure P 3.25.
Chapter 3: Roots of Equations 101

m
α=
M

in which E is Young's modulus for the material of the bar, ρ is the mass per unit volume and
ω is the natural frequency .

For a steel bar with mass ratio α = 0.70, E = 30 x 10 6 psi , L = 98 in., and ρ = 7.35x 10 -4
lb-sec 2 / in 4 , determine the first three natural frequencies.

3.26. For a uniform beam of length L with one end fixed and the other simply supported, the
natural frequencies of transverse vibration are given by the relationship [ 21 ]

tan ( k ⋅ L) = tanh ( k ⋅ L)

where
2 ρ⋅A
k = ω⋅
E ⋅I

Figure P 3.26

in which ω is the natural frequency , ρ is the mass density of the beam material, A is the
area of cross section of the beam , E is Young's modulus, and I is the moment of inertia of
the beam cross section.

Determine the first three natural frequencies of a 36 in. long steel beam whose area of
cross section A is 12.50 in 2 , moment of inertia I is 12.75 in 4, and ρ = 7.35x 10 -4 lb-sec 2 /
in 4 . E for steel is 30 x 10 6 psi.
102 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

NOTES:
Chapter 4: Matrices and Linear Algebra 103

CHAPTER 4

MATRICES AND LINEAR ALGEBRA


4.1 BASIC MATRIX OPERATIONS
Basic matrix operations are addition, subtraction and multiplication. Determination of the
inverse of a matrix is linked with the concepts of transpose, minor, cofactor and adjoint.

Addition and Subtraction:

Two matrices with the same number of rows and columns can be added by adding the
corresponding terms. Similarly, subtraction is done by subtracting the corresponding
terms

Multiplication:

If the product of two matrices A and B is C , the element C ij of C is obtained by


multiplying the elements of the ith row of A by the elements of the j th column of B in
accordance with

C ij =
∑ (aik ⋅bkj) (4.1)
k

Transpose:

The transpose of a matrix is one in which the rows and the columns are interchanged

Minor:

The minor M ij of the element a ij is the determinant obtained by deleting the i th row and
the j th column from the given determinant.

⎛⎜ 2 3 4 ⎞⎟
⎛5 9 ⎞
If A := ⎜ 5 7 9 ⎟, then, M12 := ⎜ ⎟ , that is, M12 = 31
⎜1 6 8 ⎟ ⎝1 8 ⎠
⎝ ⎠

Cofactor:

The cofactor C ij of the element a ij is given by:

i+j
C ij = ( −1) ⋅ Mij (4.2)
104 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

3
For the above case, C 12 := ( −1) ⋅ ( 31 ), that is, C 12 = −31

Adjoint Matrix:

The adjoint of a square matrix is the transpose of the matrix of its cofactors.

Inverse of a Matrix:

The inverse A -1 of a matrix A must satisfy the relationship

AA -1 =I, (4.3)

where I is the Identity or the Unit matrix which is a matrix with "1" along the diagonal and
zero elsewhere. The inverse A -1 is calculated as follows

A -1 = [adjoint (A)] / Det (A) , (4.4)

where Det (A) is the determinant of the given matrix, which must be non-zero for the inverse to
exist.

Example 4.1

Determine the inverse of the following matrix A

⎛⎜ 1 2 3 ⎞⎟
A := ⎜ 1 3 2 ⎟
⎜1 0 4 ⎟
⎝ ⎠

The determinant of this matrix is A = −1

The minors of A are

⎛3 2 ⎞ ⎛1 2 ⎞ ⎛1 3 ⎞
M11 := ⎜ ⎟ M12 := ⎜ ⎟ M13 := ⎜ ⎟
⎝0 4 ⎠ ⎝1 4 ⎠ ⎝1 0 ⎠

⎛2 3 ⎞ ⎛1 3 ⎞ ⎛1 2 ⎞
M21 := ⎜ ⎟ M22 := ⎜ ⎟ M23 := ⎜ ⎟
⎝0 4 ⎠ ⎝1 4 ⎠ ⎝1 0 ⎠
Chapter 4: Matrices and Linear Algebra 105

⎛2 3 ⎞ ⎛1 3 ⎞ ⎛1 2 ⎞
M31 := ⎜ ⎟ M32 := ⎜ ⎟ M33 := ⎜ ⎟
⎝3 2 ⎠ ⎝1 2 ⎠ ⎝1 3 ⎠

That is,

M11 = 12 M12 = 2 M13 = −3 M21 = 8 M22 = 1 M23 = −2

M31 = −5 M32 = −1 M33 = 1

i+ j
Multiplying the minors by ( −1) will yield the cofactor matrix C as :

⎛⎜ 12 −2 −3 ⎞⎟
C := ⎜ −8 1 2 ⎟
⎜ −5 1 1 ⎟
⎝ ⎠

The adjoint matrix is the transpose of the cofactor matrix, and the inverse of the matrix A
will now be the adjoint matrix divided by the determinant of A.

adj ( A)
InverseA =
A

or,
⎛⎜ 12 −8 −5 ⎞⎟
⎜ −2 1 1 ⎟
⎜ −3 2 1 ⎟ ⎛⎜ −12 8 5 ⎞⎟
⎝ ⎠
InverseA := InverseA = ⎜ 2 −1 −1 ⎟
−1 ⎜ 3 −2 −1 ⎟
⎝ ⎠
The correctness of the result can now be verified as follows. Multiplying A by its inverse
obtained above gives

⎛⎜ 1 2 3 ⎞⎟ ⎛⎜ −12 8 5 ⎞⎟ ⎛⎜ 1 0 0 ⎞⎟
⎜ 1 3 2 ⎟ ⋅ ⎜ 2 −1 −1 ⎟ = ⎜ 0 1 0 ⎟
⎜ 1 0 4 ⎟ ⎜ 3 −2 −1 ⎟ ⎜ 0 0 1 ⎟
⎝ ⎠⎝ ⎠ ⎝ ⎠

4.2 USE OF MATHCAD IN PERFORMING MATRIX OPERATIONS

To put in a matrix in your document, choose "Matrix" from the "Insert" menu, then enter
106 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

the number of rows and columns, click on "Insert" and start putting in the numbers in the
placeholders, as shown in Figure 4.1. How to perform matrix operations will be clear from
the following examples.

Figure 4.1. Matrix operations in Mathcad


Chapter 4: Matrices and Linear Algebra 107

Given the two matrices A and B:

⎛⎜ 1 0 2 ⎞⎟ ⎛⎜ 3 2 1 ⎞⎟
A := ⎜ 1 3 2 ⎟ B := ⎜ 1 2 3 ⎟
⎜7 8 9 ⎟ ⎜1 5 9 ⎟
⎝ ⎠ ⎝ ⎠
Addition:

⎛⎜ 4 2 3 ⎞⎟
A + B = ⎜2 5 5 ⎟
⎜ 8 13 18 ⎟
⎝ ⎠
Subtraction:

⎛⎜ −2 −2 1 ⎞⎟
A − B = ⎜ 0 1 −1 ⎟
⎜6 3 0 ⎟
⎝ ⎠

Multiplication:

⎛⎜ 5 12 19 ⎞⎟
<--Use the asterisk ( * ) to multiply two matrices
A ⋅ B = ⎜ 8 18 28 ⎟
⎜ 38 75 112 ⎟
⎝ ⎠

⎛⎜ 12 14 19 ⎟⎞
<-- Order in which multiplication is done is important.
B ⋅ A = ⎜ 24 30 33 ⎟
Note that [A] [B] is not equal to [B][A]
⎜ 69 87 93 ⎟
⎝ ⎠

Scalar subtraction:

⎛⎜ −4 −5 −3 ⎞⎟
A − 5 = ⎜ −4 −2 −3 ⎟
⎜2 3 4 ⎟
⎝ ⎠
108 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Scalar multiplication: ( Use the asterisk "*" )

⎛⎜ 5 0 10 ⎞⎟
A ⋅ 5 = ⎜ 5 15 10 ⎟
⎜ 35 40 45 ⎟
⎝ ⎠

Matrix inverse: Use : ^ -1

⎛⎜ −0.733 −1.067 0.4 ⎞⎟


−1
A = ⎜ −0.333 0.333 0 ⎟
⎜ 0.867 0.533 −0.2 ⎟
⎝ ⎠

Determinant of A : A = −15

Transpose of A: Use [Ctrl]1

⎛⎜ 1 1 7 ⎞⎟
T
A = ⎜0 3 8 ⎟
⎜2 2 9 ⎟
⎝ ⎠

Matrix subscripts:

Use " [ " to put in subscripts. Note that, in this case, subscripts will go from 0 to 2.
because, in Mathcad, vector and matrix elements are ordinarily numbered starting with row
zero and column zero. To make " one " the " ORIGIN " , go to the Tools menu, choose
Worksheet Options , as shown in Figure 4.2 , and change the array origin from 0 to 1.

4.3 SOLUTION OF LINEAR ALGEBRAIC EQUATIONS BY USING


THE INVERSE

For a system of linear algebraic equations that can be written in matrix form as

[A]{X} = {B} (4.5)

where the matrix [A] and the column vector {B} contain known constants, the inverse of the matrix
[A] can be utilized in the determination of the solution vector {X} as follows

{X} = [A] -1 {B} (4.6)


Chapter 4: Matrices and Linear Algebra 109

Figure 4.2. Changing the array origin in Mathcad


110 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 4.2

Solve: 2x + 4 y + z = -11
-x + 3 y -2 z = -16
2x - 3y + 5 z = 21

Putting the given equations in the form [A] {X}= {B}, where

⎛⎜ 2 4 1 ⎞⎟ ⎛⎜ −11 ⎞⎟
A := ⎜ −1 3 −2 ⎟ B := ⎜ −16 ⎟
⎜ 2 −3 5 ⎟ ⎜ 21 ⎟
⎝ ⎠ ⎝ ⎠

The inverse of [A] can be easily obtained as

⎛⎜ 0.474 −1.211 −0.579 ⎞⎟


−1
A = ⎜ 0.053 0.421 0.158 ⎟
⎜ −0.158 0.737 0.526 ⎟
⎝ ⎠
and [A] multiplied by [A]-1 will yield the identity matrix as shown

⎛⎜ 1 0 0 ⎞⎟
−1
A ⋅A = ⎜0 1 0 ⎟
⎜0 0 1 ⎟
⎝ ⎠

The solution can then be generated by performing the matrix operation

−1
X := A ⋅B

yielding

⎛⎜ 2 ⎞⎟
X = ⎜ −4 ⎟
⎜1 ⎟
⎝ ⎠

4.4 SOLUTION OF LINEAR ALGEBRAIC EQUATIONS BY


CRAMER'S RULE
As an alternative to the above procedure, the solution {X} to the matrix equation
(Equation 4.5)
Chapter 4: Matrices and Linear Algebra 111

[A] {X} = {B}

can be computed using Cramer's rule as shown below

Letting

⎛ A11 A12 A13 ⎞ ⎛ B1 ⎞


⎜ ⎟ ⎜ ⎟
A = ⎜ A21 A22 A23 ⎟ B = ⎜ B2 ⎟
⎜A ⎟ ⎜B ⎟
⎝ 31 A32 A33 ⎠ ⎝ 3⎠

the solution {X} can be computed using

⎛ B1 A12 A13 ⎞ ⎛ A11 B1 A13 ⎞


⎜ ⎟ ⎜ ⎟
⎜ B2 A22 A23 ⎟ ⎜ A21 B2 A23 ⎟
⎜B ⎟ ⎜A ⎟
⎝ 3 A32 A33 ⎠ ⎝ 31 B3 A33 ⎠
X1 = X2 =
⎛ A11 A12 A13 ⎞ ⎛ A11 A12 A13 ⎞
⎜ ⎟ ⎜ ⎟
⎜ A21 A22 A23 ⎟ ⎜ A21 A22 A23 ⎟
⎜A ⎟ ⎜A ⎟
⎝ 31 A32 A33 ⎠ ⎝ 31 A32 A33 ⎠
(4.7)

⎛ A11 A12 B1 ⎞
⎜ ⎟
⎜ A21 A22 B2 ⎟
⎜A ⎟
X3 =
⎝ 31 A32 B3 ⎠
⎛ A11 A12 A13 ⎞
⎜ ⎟
⎜ A21 A22 A23 ⎟
⎜A ⎟
⎝ 31 A32 A33 ⎠

For Example 4.2,

⎛⎜ 2 4 1 ⎞⎟ ⎛⎜ −11 ⎞⎟
A := ⎜ −1 3 −2 ⎟ B := ⎜ −16 ⎟ D := A D = 19
⎜ 2 −3 5 ⎟ ⎜ 21 ⎟
⎝ ⎠ ⎝ ⎠
112 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛ B1 A1 , 2 A1 , 3 ⎞ ⎛ A1 , 1 B1 A1 , 3 ⎞ ⎛ A1 , 1 A1 , 2 B1 ⎞
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ B2 A2 , 2 A2 , 3 ⎟ ⎜ A2 , 1 B2 A2 , 3 ⎟ ⎜ A2 , 1 A2 , 2 B2 ⎟
⎜B A ⎟ ⎜A ⎟ ⎜A ⎟ ,
⎝ 3 3 , 2 A3 , 3 ⎠ ⎝ 3 , 1 B3 A3 , 3 ⎠ ⎝ 3 , 1 A3 , 2 B3 ⎠
x := y := z :=
D D D

where the matrix elements have been specified in the format required by Mathcad. These
calculations result in

x=2 y = −4 z=1

4.5 SOLUTION OF LINEAR ALGEBRAIC EQUATIONS USING THE


FUNCTION lsolve

The lsolve function in Mathcad can be used to solve a system of linear algebraic equations .

Given:

[A] {X}= {B},

the function lsolve (A,B) will return the solution vector {X}, provided the matrix [A] is not
singular or nearly singular. For Example 4.2,

⎛⎜ 2 4 1 ⎞⎟ ⎛⎜ −11 ⎞⎟
A = ⎜ −1 3 −2 ⎟ B = ⎜ −16 ⎟
⎜ 2 −3 5 ⎟ ⎜ 21 ⎟
⎝ ⎠ ⎝ ⎠

⎛⎜ 2 ⎞⎟
X := lsolve ( A , B) gives X = ⎜ −4 ⎟
⎜1 ⎟
⎝ ⎠
Example 4.3

Application of Matrices to Electrical Circuit Analysis.

Formulate a set of linear equations that represent the relationship between voltage, current and
resistances for the circuit shown in Figure 4.3. Solve the simultaneous equations generated using
(1) the inverse (2) Cramer's Rule and (3) the Mathcad function lsolve
Chapter 4: Matrices and Linear Algebra 113

Figure 4.3. Electrical circuit example

The governing equations are:

i1 + i 2 = i3
7 i1 + 3 i 3 =6
7 i1 - 5 i2 =6

These must be put in the form: [A] {X} = {B} and solved for {X}

Using the inverse

⎛⎜ 1 1 −1 ⎞⎟ ⎛⎜ 0 ⎞⎟
A := ⎜ 7 0 3 ⎟ B := ⎜ 6 ⎟
⎜ 7 −5 0 ⎟ ⎜6 ⎟
⎝ ⎠ ⎝ ⎠

⎛⎜ 0.6761 ⎞⎟
−1 <--These are the currents i1 , i2 and
X := A ⋅B X = ⎜ −0.2535 ⎟
⎜ 0.4225 ⎟ i3
⎝ ⎠

Using Cramer's Rule

⎛⎜ 0 1 −1 ⎞⎟ ⎛⎜ 1 0 −1 ⎞⎟ ⎛⎜ 1 1 0 ⎞⎟
L := ⎜ 6 0 3 ⎟ M := ⎜ 7 6 3 ⎟ N := ⎜ 7 0 6 ⎟
⎜ 6 −5 0 ⎟ ⎜7 6 0 ⎟ ⎜ 7 −5 6 ⎟
⎝ ⎠ ⎝ ⎠ ⎝ ⎠

L M N
i1 := i2 := i3 := i1 = 0.676 i2 = −0.254 i3 = 0.4225
A A A
114 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Using Mathcad Function lsolve

⎛⎜ 0.676 ⎞⎟
X := lsolve ( A , B) X = ⎜ −0.254 ⎟ <--These are the currents i1 , i2 and i 3
⎜ 0.423 ⎟
⎝ ⎠

4.6 THE EIGENVALUE PROBLEM

Eigenvalue problems occur in a variety of physical situations. Some examples of eigenvalue


problems are given below.

1. Determination of natural frequencies and mode shapes of oscillating systems.


2. Computation of principal stresses and principal directions
3. Computation of principal moments of inertia and principal axes.
4. Buckling of structures.
5. Oscillations of electrical networks.

The eigenvalue problem can be mathematically stated as

[A] {X} = λ {X} (4.8)

where [A] is a known square matrix, {X} is a column vector of unknowns, and λ is an unknown
scalar quantity. The solution of the eigenvalue problem involves the determination of vectors
{X} and associated constants λ satisfying equation (4.8). These vectors {X} are termed
eigenvectors. With each eigenvector is associated a constant λ which is called an
eigenvalue.

Equation (4.8) can be written in the form

[A- λ I ] {X} = {0} (4.9)

A trivial solution to the above is

{X} = 0

which is not of interest to us. The condition for the existence of a non-trivial solution is

det ( A- λ I ) = 0 (4.10)
Chapter 4: Matrices and Linear Algebra 115

Expansion of the above determinant produces a polynomial in λ. For instance, if [A] is a 3x 3


matrix, then Equation (4.10) would give a cubic in λ. This polynomial is called the
characteristic polynomial and its roots are called eigenvalues. Associated with each
eigenvalue is a solution vector {X} called the eigenvector.

It can be shown that the adjoint of matrix (A- λiI) contains columns, each of which is the ith
eigenvector {X} i associated with the eigenvalue λ i multiplied by an arbitrary constant.
Thus, to determine an eigenvector corresponding to an eigenvalue λi , compute the adjoint of
(A- λiI) and choose any column of it as the eigenvector.

Example 4.4

⎛ 17 −6 ⎞
Compute the eigenvalues and eigenvectors of ⎜ ⎟
⎝ 45 −16 ⎠

⎛ 17 −6 ⎞
A := ⎜ ⎟
⎝ 45 −16 ⎠

The [A- λI] matrix can be written as

⎛ 17 − λ −6 ⎞
A − λ ⋅I = ⎜ ⎟
⎝ 45 −16 − λ ⎠

A non-trivial solution is obtained by setting the determinant of [ A - λ I ] equal to zero

⎛ 17 − λ −6 ⎞
⎜ ⎟ =0
⎝ 45 −16 − λ⎠

which gives λ 2 - λ - 2 = 0, or, λ 1 = 2, λ 2 = -1. The adjoint of the matrix (A- λ I ), in


this case, is

⎛ −16 − λ 6 ⎞
⎜ ⎟
⎝ −45 17 − λ ⎠

For λ 1 = 2, the corresponding eigenvector is the first or second column of the above
adjoint matrix with λ = λ 1 which gives
116 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

1 ⎛ −18 ⎞ ⎛2 ⎞ ⎛ 0.4 ⎞
{X} = ⎜ ⎟ or ⎜ ⎟ or ⎜ ⎟
⎝ −45 ⎠ ⎝5 ⎠ ⎝ 1 ⎠
as the first eigenvector.

Similarly, the second eigenvector is obtained by putting λ = λ 2 in any one column of the
adjoint matrix which gives

2 ⎛ −15 ⎞ ⎛1 ⎞ ⎛ 0.333 ⎞
{X} = ⎜ ⎟ or, ⎜ ⎟ or ⎜ ⎟
⎝ −45 ⎠ ⎝3 ⎠ ⎝ 1 ⎠

4.7 SOLVING THE EIGENVALUE PROBLEM WITH MATHCAD

The Mathcad function eigenvals(A) returns a vector containing the eigenvalues of the matrix A

⎛⎜ 1 1 1 ⎞⎟
Let A := ⎜ 1 2 2 ⎟
⎜1 0 3 ⎟
⎝ ⎠
Then,

⎛⎜ 3.879 ⎞⎟
eigenvals ( A) = ⎜ 0.468 ⎟
⎜ 1.653 ⎟
⎝ ⎠

The Mathcad function eigenvecs(A) returns a matrix containing normalized eigenvectors of


the square matrix A. The nth column of this matrix is an eigenvector corresponding to the nth
eigenvalue returned by eigenvals(A) .

⎛⎜ −0.433 −0.923 −0.535 ⎞⎟


eigenvecs ( A) = ⎜ −0.755 0.127 −0.746 ⎟
⎜ −0.493 0.364 0.397 ⎟
⎝ ⎠

The Mathcad function eigenvec(A,z) returns a matrix containing the normalized


eigenvector corresponding to the eigenvalue z of the square matrix A. For example, the
eigenvector associated with the eigenvalue 0.468 is
Chapter 4: Matrices and Linear Algebra 117

⎛⎜ 0.923 ⎞⎟
eigenvec ( A , 0.468 ) = ⎜ −0.127 ⎟
⎜ −0.364 ⎟
⎝ ⎠

For Example 4.4, the matrix given was

⎛ 17 −6 ⎞
A := ⎜ ⎟
⎝ 45 −16 ⎠

The eigenvalues and eigenvectors obtained with Mathcad are

⎛2 ⎞ ⎛ 0.371 0.316 ⎞ ⎛ 0.4 0.333 ⎞


eigenvals ( A) = ⎜ ⎟ eigenvecs ( A) = ⎜ ⎟ or ⎜ ⎟
⎝ −1 ⎠ ⎝ 0.928 0.949 ⎠ ⎝ 1 1 ⎠

4.8 APPLICATION OF THE EIGENVALUE PROBLEM TO VIBRATION


ENGINEERING

Figure 4.4. Mathematical model of two-story building

Consider the vibration problem of a two -story building which is mathematically modeled as the two
degree of freedom system shown in Figure 4.4, in which the building is represented by lumped
masses m and 2m and springs k and 2k .

After drawing suitable free body diagrams of the two masses m and 2m, the governing
118 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

differential equations of motion can be seen to be

2 m d2x1 /dt2 + 3 k x1 - k x2 = 0

(4.11)
m d 2x2 /dt2 +k ( x2 - x 1 ) = 0

Expressing the above equations in matrix form as


..
(4.12)
[M] {x} + [K ] {x} = {0}

where [M] is the mass or inertia matrix , and [K] is the stiffness matrix, and assuming that

{x} = {A}sin ωt (4.13)

we obtain a set of linear homogeneous algebraic equations that can be written in matrix form as

(4.14)
([K]- ω2 [ M ] ) {A} = 0

For a non-trivial solution, the determinant | K- ω2 M | must be zero. The problem can also be
posed slightly differently in the familiar form

|A-λI| = 0 (4.15)

where the λ 's ( λ = ω2 ) are the eigenvalues of the matrix [A] , which, in this case, is

[A] = [M] -1 [K] (4.16)

To find the natural frequencies and mode shapes in this case, we need to determine the
eigenvalues and eigenvectors of the matrix [A]. The square roots of the eigenvalues will then
give the natural frequencies and the corresponding eigenvectors will be the mode shapes.

Letting m = 1, and k =1 in this problem for convenience

m := 1 k := 1

Then, the mass and stiffness matrices

⎛ 2⋅m 0 ⎞ ⎛ 3 ⋅ k −k ⎞
M := ⎜ ⎟ K := ⎜ ⎟
⎝ 0 m⎠ ⎝ −k k ⎠
Chapter 4: Matrices and Linear Algebra 119

give

⎛2 0 ⎞ ⎛ 3 −1 ⎞
M= ⎜ ⎟ K = ⎜ ⎟
⎝0 1 ⎠ ⎝ −1 1 ⎠

The [A] matrix, which is called the dynamic matrix in the language of vibrations is ,

−1 ⎛ 1.5 −0.5 ⎞
A := M ⋅K which gives A= ⎜ ⎟
⎝ −1 1 ⎠

The eigenvalues of [A] are obtained from setting the determinant of [A-λI] equal to zero, as
follows

A − λ ⋅I = 0

This yields

( 1.5 − λ) ⋅ ( 1 − λ) − 0.5 = 0 , or, λ1 = 0.5 , and λ2 = 2

The adjoint of the matrix [A-λI] is

⎡( 1 − λ) 0.5 ⎤
⎢ ⎥
⎣ 1 ( 1.5 − λ) ⎦

In Vibration Engineering, eigenvectors are termed as natural modes or mode shapes, and
these are simply the configurations that the system takes when vibrating at a natural
frequency. Because the system considered here is a two-degree-of-freedom system, there
will be two natural frequencies and correspondingly two modes. While the natural
frequencies are simply the square roots of the eigenvalues, the associated eigenvectors or
modeshapes can be determined by putting λ = λ1 (for the first mode ) and λ = λ2 (for the
second mode ) in either column of the adjoint of [A- λ I]

This procedure generates the following mode shapes

⎛ 0.5 ⎞ ⎛1 ⎞
FirstMode := ⎜ ⎟ and SecondMode := ⎜ ⎟
⎝ 1 ⎠ ⎝1 ⎠

The determination of eigenvalues and eigenvectors using Mathcad is done below.


120 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛ 2 ⎞
lambda := eigenvals ( A ) lambda = ⎜ ⎟
⎝ 0.5 ⎠

By default, Mathcad arrays begin at element zero. To change this to begin at element 1,
choose "Worksheet Options" from the " Tools" menu, click on the "Built-In Variables"
tab and set "ORIGIN" to 1.

i := 1 .. 2 ωi := lambda i ωi =
1.414
0.707

In Vibration Engineering, the lowest natural frequency is always considered as the first or
fundamental natural frequency. In this case, the natural frequencies are

ω2 = 0.707 <--First natural frequency in rads/sec

ω1 = 1.414 <--Second natural frequency in rads/sec

The eigenvectors or mode shapes can be determined, using the Mathcad function eigenvecs
as follows

X := eigenvecs ( A)

This gives the following matrix of eigenvectors which is referred to as the modal matrix in
Vibrations language

⎛ 0.707 0.447 ⎞
X = ⎜ ⎟ <- Modal Matrix
⎝ −0.707 0.894 ⎠

A close inspection of the analysis will show that it is not possible to obtain unique values for the
eigenvector components. Therefore, it is traditional to select a value of unity for one of the
components of an eigenvector and compute the other one accordingly . This is called
normalization and should be clear from the calculation shown below.

The first natural mode, corresponding to the first natural frequency is


Chapter 4: Matrices and Linear Algebra 121

⎛ X1, 2 ⎞
⎜ ⎟
⎜ 0.8944 ⎟ ⎛ 0.5 ⎞
FirstMode := FirstMode = ⎜ ⎟
⎜ X2, 2 ⎟ ⎝ 1 ⎠
⎜ ⎟
⎝ 0.8944 ⎠

while the second natural mode, corresponding to the second natural frequency is

⎛ X1, 1 ⎞
⎜ ⎟
⎜ −0.7071 ⎟ ⎛ −1 ⎞
SecondMode := SecondMode = ⎜ ⎟
⎜ X2, 1 ⎟ ⎝1 ⎠
⎜ ⎟
⎝ −0.7071 ⎠

These mode shapes are sketched in Figure 4.5.

Figure 4.5. Modes shapes of the two-story building

Example 4.5.

Investigate the free vibration of a three-story building mathematically modeled as the


three-degree-of-freedom system shown in Figure 4.6.
122 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 4.6. Mathematical model of three-story building

The equations of motion of the mathematical model resorted to can be shown to be

2
m1 ⋅
d
(
x + k1 ⋅ x1 − k2 ⋅ x2 − x1 = 0
2 1
)
dt

2
m2 ⋅
d
( ) (
x + k2 ⋅ x2 − x1 − k3 ⋅ x3 − x2 = 0
2 2
) (4.17)
dt

2
m3 ⋅
d
(
x + k3 ⋅ x3 − x2 = 0
2 3
)
dt

These can be put in the following matrix format

[M] d2 {x} /dt2 + {K] { x } = { 0} (4.18)

where
Chapter 4: Matrices and Linear Algebra 123

⎛ m1 0 0 ⎞
⎜ ⎟
[M] = ⎜ 0 m2 0 ⎟
⎜ ⎟
⎝ 0 0 m3 ⎠
(4.19)

⎡( k1 + k2) −k 2 0 ⎤
⎢ ⎥
[K] = ⎢ −k 2 ( k 2 + k 3) −k 3 ⎥
⎢ ⎥
⎣ 0 −k 3 k3 ⎦

Letting m1 = 2x 10 6 kg , m2 = 14x 10 5 kg , m3 = 7x10 5 kg; and k1 = 12x10 8 N/ m,


k 2 = 8x 10 8 N/m , k3 = 4x 10 8 N/ m , the [A] matrix can be computed, using Mathcad ,
as follows

6 5 5 8 8 8
m1 := 2 ⋅ 10 m2 := 14 ⋅ 10 m3 := 7 ⋅ 10 k 1 := 12 ⋅ 10 k 2 := 8 ⋅ 10 k 3 := 4 ⋅ 10

⎛ m1 0 0 ⎞ ⎡( k1 + k2) −k 2 0 ⎤
⎜ ⎟ ⎢ ⎥
M := ⎜ 0 m2 0 ⎟ K := ⎢ −k 2 ( k 2 + k 3) −k 3 ⎥
⎜ 0 0 m ⎟ ⎢ 0 ⎥
⎝ 3⎠ ⎣ −k 3 k3 ⎦

⎛ 2 ⋅ 10 6 ⎞ ⎛ 2 × 10 9 −8 × 10 8 ⎞
⎜ 0 0 ⎟ ⎜ 0 ⎟
M := ⎜ 6 ⎟ K = −8 × 10 1.2 × 10 −4 × 10 ⎟
⎜ 8 9 8
⎜ 0 1.4 ⋅ 10 0 ⎟ ⎜ ⎟
⎜ 6⎟ ⎜ 8 8 ⎟
⎝ 0 0 0.7 ⋅ 10 ⎠ ⎝ 0 −4 × 10 4 × 10 ⎠

⎛ 1 × 10 3 −400 ⎞
−1 ⎜ 0 ⎟
A := M ⋅K A = ⎜ −571.429 857.143 −285.714 ⎟
⎜ ⎟
⎝ 0 −571.429 571.429 ⎠

The eigenvalues and the eigenvectors are

⎛ 1.495 × 10 3 ⎞
⎜ ⎟
lambda := eigenvals ( A) lambda = ⎜ 761.106 ⎟
⎜ ⎟
⎝ 172.146 ⎠
124 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Note that Mathcad has found the highest eigenvalue first. The natural frequencies in Hz and the
corresponding eigenvectors are computed below.

i := 1 , 2 .. 3

lambda i
f i := f 1 = 6.154 f 2 = 4.391 f 3 = 2.088 (Hz)
2⋅π

⎛⎜ −0.566 −0.467 0.267 ⎞⎟


X := eigenvecs ( A) X = ⎜ 0.701 −0.279 0.552 ⎟
⎜ −0.434 0.839 0.79 ⎟
⎝ ⎠

The normalized eigenvectors or mode shapes can be determined as shown.

⎛⎜ X 1 , 1 X1, 2 X1, 3 ⎞

⎜ X1, 1 X1, 2 X1, 3 ⎟
⎜ ⎟ ⎛⎜ 1 1 1 ⎞

X2, 1 X2, 2 X2, 3
Xnorm := ⎜ ⎟ Xnorm = ⎜ −1.238 0.597 2.07 ⎟
⎜ X1, 1 X1, 2 X1, 3 ⎟ ⎜ 0.766 −1.799 2.962 ⎟
⎜ ⎟ ⎝ ⎠
⎜ X3, 1 X3, 2 X3, 3 ⎟
⎜ X1, 1 ⎟
⎝ X1, 2 X1, 3 ⎠ Mode No: 3 2 1

Figure 4.7 shows the natural modes of the building as it vibrates at its natural
frequencies.

Figure 4.7. Mode shapes of three-story building


Chapter 4: Matrices and Linear Algebra 125

4.9 APPLICATION OF THE EIGENVALUE PROBLEM TO STRESS


ANALYSIS- DETERMINATION OF PRINCIPAL STRESSES AND
PRINCIPAL DIRECTIONS

If [σ ] is the state of stress at a point in a material given by the stress components σxx ,
σyy, σzz, σxy , σyz and σzx , the principal stresses are the eigenvalues λ of the stress matrix
and the principal directions, which are the x, y and z components of the unit vectors along
the normals to the principal planes , are the eigenvectors {η} of the stress matrix. Thus ,
the eigenvalue problem in this case is stated as

[σ] - λ [ I ]) { η } = {0} (4.20)

where the eigenvalues λ and the eigenvectors η are to be determined.

Example 4.6

The two-dimensional state of stress in a mechanical element, which is shown in Figure 4.8, is
given by the components:

σ xx = 80 MPa, σyy = 0 , and σ xy = 50 MPa.

Determine the principal stresses and their associated directions.

Figure 4.8. Two-dimensional state of stress

Because the stress matrix is symmetrical, σ yx must be equal to σ xy. Thus, the matrix of
stresses is
126 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛ 80 50 ⎞
σ := ⎜ ⎟ (MPa)
⎝ 50 0 ⎠

The principal stresses are the eigenvalues of the given stress matrix while the principal
directions are the associated eigenvectors. These are.

⎛ 104.031 ⎞
prinstress := eigenvals ( σ) prinstress = ⎜ ⎟ (MPa)
⎝ −24.031 ⎠
⎛ 0.901 −0.433 ⎞
prindir := eigenvecs ( σ) prindir = ⎜ ⎟
⎝ 0.433 0.901 ⎠

The principal direction associated with the maximum principal stress ( 104.031 MPa )
can be found in the form of a unit vector along the normal to the principal plane. Its x
and y components , prindir1, are computed below. Figure 4.9 shows the principal
stresses on the element and the associated principal directions.

⎛ 0.901 ⎞
prindir1 := eigenvec ( σ , 104.031) prindir1 = ⎜ ⎟
⎝ 0.433 ⎠

Figure 4.9. Principal stresses and principal directions for


two-dimensional stress example

Example 4.7

The three-dimensional state of stress in a mechanical element, which is shown in Figure


4.10, is given by:

σxx= 1000 psi, σyy = -1000 psi, σxy = 500 psi , σ yz = -200 psi, σ zx =100 psi , σ zz = 250
psi

Determine the principal stresses and their associated directions.


Chapter 4: Matrices and Linear Algebra 127

Figure 4.10. Three-dimensional state of stress

The stress matrix can be written as

⎛⎜ 1000 500 100 ⎞⎟


(psi)
σ := ⎜ 500 −1000 −200 ⎟
⎜ 100 −200 250 ⎟
⎝ ⎠

The principal stresses and principal directions can be determined using Mathcad as
shown below

⎛ −1.152 × 10 3 ⎞
⎜ ⎟ psi
prinstress := eigenvals ( σ) prinstress = ⎜ 3 ⎟
1.121 × 10
⎜ ⎟
⎝ 280.707 ⎠

⎛⎜ −0.23 0.973 −0.024 ⎞⎟


prindir := eigenvecs ( σ) prindir = ⎜ 0.961 0.224 −0.163 ⎟
⎜ 0.154 0.06 0.986 ⎟
⎝ ⎠

If only the principal direction associated with one of the principal stresses , say 280.707 psi, is
required, it can be found as follows.
128 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

prindir2 := eigenvec ( σ , 280.707 )

⎛⎜ −0.024 ⎞⎟ These are the the x and y


prindir2 = ⎜ −0.163 ⎟ <----- components of the unit
⎜ 0.986 ⎟ vector along the normal to the
⎝ ⎠ principal plane associated with the
principal stress, 280.707 psi

The principal stresses and their directions are shown in Figure 4.11. Note the orthogonality of the
principal stress directions with respect to one another.

180 180
acos ( 0.9730) ⋅ = 13.344 deg acos ( 0.224 ) ⋅ = 77.056 deg
π π

180
acos ( 0.06) ⋅ = 86.56 deg
π

Figure 4.11. Principal stresses and principal directions for three-dimensional


state of stress.

4.10 REPEATED ROOTS IN THE DETERMINANTAL EQUATION

When equal roots are encountered in the determinantal equation, the associated
eigenvectors are not unique and thus a linear combination of these eigenvectors may also be
Chapter 4: Matrices and Linear Algebra 129

considered as eigenvectors. The following examples will illustrate this situation.

Example 4.8

For the given matrix [A]

⎛⎜ 0 −1 1 ⎞⎟
A := ⎜ −1 0 1 ⎟
⎜ 1 1 0⎟
⎝ ⎠

the eigenvalues and eigenvectors are found using Mathcad as

⎛⎜ 1 ⎞⎟
<-- double root at λ = 1
lambda := eigenvals ( A) lambda = ⎜ −2 ⎟
⎜1 ⎟
⎝ ⎠

⎛⎜ 0.816 −0.577 −0.226 ⎞⎟


X := eigenvecs ( A) X = ⎜ −0.408 −0.577 0.793 ⎟
⎜ 0.408 0.577 0.566 ⎟
⎝ ⎠

It is clear that the first and third eigenvectors corresponding to the repeated root λ = 1 are
arbitrary, since they are both different , while the one corresponding to λ = - 2 is

⎛⎜ −1 ⎞⎟
⎜ −1 ⎟
⎜1 ⎟
⎝ ⎠

Example 4.9

The following problem is, clearly, a case of repeated roots with a double root at λ = 1
130 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛⎜ 1 −1 0 ⎞⎟
A := ⎜ 0 1 1 ⎟
⎜ 0 0 −1 ⎟
⎝ ⎠

The eigenvalues and eigenvectors are

⎛⎜ 1 ⎞⎟
lambda := eigenvals ( A) lambda = ⎜ 1 ⎟
⎜ −1 ⎟
⎝ ⎠

⎛⎜ 1 1 −0.218 ⎞⎟
X := eigenvecs ( A) X = ⎜ 0 0 −0.436 ⎟
⎜ 0 0 0.873 ⎟
⎝ ⎠

Note that the first and second eigenvectors corresponding to the repeated (double) root at λ
= 1 are, in fact , arbitrary since they have two components that are zero, while the one
corresponding to λ = -1 is the third one which is

⎛⎜ −1 ⎞⎟
⎜ −2 ⎟
⎜4 ⎟
⎝ ⎠

Example 4.10

For the following matrix [A]

⎛⎜ −14 1 0 ⎞⎟
A := ⎜ 0 2 0 ⎟
⎜ 1 0 2⎟
⎝ ⎠

the eigenvalues and eigenvectors are


Chapter 4: Matrices and Linear Algebra 131

⎛⎜ 2 ⎞⎟ ⎛ 0 0.998 0 ⎞
⎜ ⎟
eigenvals ( A) = ⎜ −14 ⎟ eigenvecs ( A) = ⎜ 0 − 15 ⎟
0 7.105 × 10
⎜ 2 ⎟ ⎜ ⎟
⎝ ⎠ ⎝ 1 −0.062 −1 ⎠

This is, again, a case of repeated roots in which the first and third eigenvectors corresponding
to the repeated roots are arbitrary. Thus, there is no unique eigenvector correponding to the
repeated root .

4.11 SOLUTION OF NONLINEAR SIMULTANEOUS EQUATIONS

There are no direct methods available for solving nonlinear simultaneous equations like you
have in the case of linear algebraic equations. In general, iterative procedures must be
resorted to, requiring initial guesses as a starting point. The following procedure utilizes an
adaptation of the Taylor series approach in obtaining a solution to a set of nonlinear
equations.

Consider the following two nonlinear equations:

f(x,y)= 0 ; g(x,y) = 0 (4.21)

which have the exact solution: x = x r and y = y r .

If x = x i and y = y i are approximate solutions, a Taylor series approximation about


( x i , y i ) gives

f( x r , y r ) = f(x i , y i ) + h ( partial deriv of f (x,y) with respect to x at (x i ,y i) ) +


j ( partial deriv of f(x,y) with respect to y at (x i , y i) ) + ... = 0
(4.22)

g (x r , y r ) = g( xi , y i ) + h ( partial deriv of g (x,y ) with respect to x at (x i , yi ) ) +


j ( partial deriv of g (x, y ) with respect to y at ( x i , yi ) ) + ....... = 0

where x r = x i + h , and yr=yi+j. (4.23)

If only linear terms are retained in the Taylor series, two linear equations are
generated in the two unknowns h and j, where h and j are approximations to ( x r - x i )
and ( yr - y i ) . Defining a, b, c and d as the partial derivatives in Equations (4.22) and
(4.23), p as f(x i , y i ) and q as g( xi , y i ) , these equations become
132 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

ah + bj = -p and ch + d j = -q (4.24)

When these two simultaneous equations are solved for h and j , we generate new
improved values for x and y, say x i+1 and y i+1 , where , x i + 1 = x i + h and y i+1 = yi +
j . These new improved values replace the old values x i and y i in the next step in an effort
to obtain still better solutions. This procedure is repeated until convergence occurs. The
following example will clarify the procedure.

Example 4.11

Determine the solution to: f(x,y) = x 2 + 3 y 2 - 54 = 0 , g(x, y ) = -4 x 2 + 3x y - 2 y + 15 = 0

Iteration Number 1
2 2 2
f ( x , y ) := x + 3 ⋅ y − 54 g ( x , y ) := −4 ⋅ x + 3 ⋅ x ⋅ y − 2 ⋅ y + 15

partfx ( x , y ) := 2 ⋅ x partfy ( x , y ) := 6 ⋅ y partgx ( x , y ) := −8 ⋅ x + 3 ⋅ y partgy ( x , y ) := 3 ⋅ x − 2

where the above are partial derivatives of f(x,y) and g(x,y) with respect to x and y.

xold := 1.5 yold := 2.0 <-- Assumed starting values of solution

p := f ( xold , yold ) q := g ( xold , yold )

a := partfx ( xold , yold ) b := partfy ( xold , yold ) c := partgx ( xold , yold )

d := partgy ( xold , yold )

a= 3 b = 12 c = −6 d = 2.5 p = −39.75 q = 11

Now solve the equations obtained from the linear Taylor series approximations, namely

ah+b j = -p and c h + d j = -q

in the form: [A] {H} = {P} using lsolve as follows

⎛a b ⎞ ⎛ −p ⎞ ⎛ 2.91 ⎞
A := ⎜ ⎟ P := ⎜ ⎟ H := lsolve ( A , P) H= ⎜ ⎟
⎝c d ⎠ ⎝ −q ⎠ ⎝ 2.585 ⎠

h := H 1 j := H 2 h = 2.91 j = 2.585

xnew := xold + h ynew := yold + j xnew = 4.41 ynew = 4.585


Chapter 4: Matrices and Linear Algebra 133

Iteration Number 2

xold := xnew yold := ynew

p := f ( xold , yold ) q := g ( xold , yold )

a := partfx ( xold , yold ) b := partfy ( xold , yold ) c := partgx ( xold , yold )

d := partgy ( xold , yold )

a = 8.821 b = 27.509 c = −21.528 d = 11.231 p = 28.516 q = −11.312

⎛a b ⎞ ⎛ −p ⎞ H := lsolve ( A , P) ⎛ 2.91 ⎞
A := ⎜ ⎟ P := ⎜ ⎟ H= ⎜ ⎟
⎝c d ⎠ ⎝ −q ⎠ ⎝ 2.585 ⎠

h := H 1 j := H 2 h = 2.91 j = 2.585

xnew := xold + h ynew := yold + j xnew = 7.321 ynew = 7.17

Following the same procedure as above, new improved values for x and y can be generated in
iterations 3, 4 and 5 as

Iteration Number 3: h := −0.1166 j := −0.0728 --> xnew := 3.3804 ynew := 3.7684

−3 −4
Iteration Number 4: h := −2.1741 ⋅ 10 j := −6.5547 ⋅ 10 --> xnew := 3.3782 ynew := 3.7677

−3 −4
Iteration Number 5: h := −2.1741 ⋅ 10 j := −6.5547 ⋅ 10 --> xnew := 3.3782 ynew := 3.7677

−3
f ( xnew , ynew ) = −1.075 × 10 a check to see if a solution
<---
has indeed been achieved
−4
g ( xnew , ynew ) = −2.085 × 10

Thus, it is clear that convergence has occurred in the fourth iteration because the values of
the functions f ( x, y ) and g (x, y ) are zero and the answers (xnew and ynew) obtained in the
fifth iteration are exactly the same as in the fourth. The solution is : x= 3.3782 and y =
3.7677. These answers may now be verified by using the given and find commands.

x := 1 y := 1 <--- Initial guesses required by Mathcad

2 2 2
Given x + 3 ⋅ y − 54.0 = 0 −4 ⋅ x + 3 ⋅ x ⋅ y − 2 ⋅ y + 15.0 = 0 <---- Press "Ctrl=" to
type " ="

⎛ xval ⎞
⎜ ⎟ := Find ( x , y ) xval = 3.378 yval = 3.768 <--- Solution
⎝ yval ⎠
134 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

PROBLEMS
4.1. Given the matrix

⎛⎜ −2 −1 −5 ⎞⎟
⎜1 1 4 ⎟
⎜0 3 3 ⎟
⎝ ⎠

find its inverse or else show that it is singular. Do calculations on a calculator.


Also check your answers with Mathcad

4.2. Given the matrix

⎛⎜ 0 0 −1 ⎞⎟
⎜ 1 12 0 ⎟
⎜ 1 −2 4 ⎟
⎝ ⎠

Find its inverse or else show that it is singular. Do calculations on a calculator. Also
check your answers with Mathcad.

4.3. Solve the following by Cramer's rule:

(a) x + y = 4 ; 2x - y = 2

(b) 5x - 3 y = 37 ; -2x + 7 y = - 38

4.4. Solve the following system of equations

8x-y-z =4 ; x +2 y -3 z = 0 ; 2 x -y +4 z =5

Do this problem on a calculator. Check your answers with (1) Mathcad, using the inverse
and (2) Mathcad's lsolve function.

4.5. Solve the following system of equations :

x+y+z=6; 2 x - y + 3z = 9 ; x+2y-z=2

Do this problem on a calculator. Check your answers with (1) Mathcad, using the inverse
and (2) Mathcad's lsolve function.
Chapter 4: Matrices and Linear Algebra 135

4.6. Solve the following system of equations :

x + y + 3 z = 96 ; 2 x - y + 7z = 6 ; x + 2 y - 5z = 2

Do this problem on a calculator. Check your answers with (1) Mathcad, using the inverse and (2)
Mathcad's lsolve function.

4.7. Solve the following by Cramer's rule:

x + y - 3z = 0 ; y - 4z = 0 ; x- y - z =5

Do this problem on a calculator. Check your answers with (1) Mathcad, using Cramer's
rule and (2) Mathcad's lsolve function.

4.8. Find the eigenvalues of the matrix

⎛1 3 ⎞
⎜ ⎟
⎝2 1 ⎠

Corresponding to each eigenvalue, find an eigenvector. Do this problem on a calculator.


Check your answers with Mathcad, using the eigenvals, eigenvecs and eigenvec
functions

4.9. Find the eigenvalues of the following matrix. Find an eigenvector corresponding to
each eigenvalue.

⎛ −5 0 ⎞
⎜ ⎟
⎝ 1 2⎠

Do this problem on a calculator. Check your answers with Mathcad, using the
eigenvals, eigenvecs and eigenvec functions .

4.10. Determine the eigenvalues and eigenvectors of the following matrices.

⎛3 4 ⎞ ⎛5 0 ⎞ ⎛6 0 ⎞ ⎛4 2 ⎞
(a) ⎜ ⎟ (b) ⎜ ⎟ (c) ⎜ ⎟ (d) ⎜ ⎟
⎝4 3 ⎠ ⎝0 5 ⎠ ⎝0 6 ⎠ ⎝5 1 ⎠

⎛ 10 0 ⎞ ⎛6 2 ⎞
(d) ⎜ ⎟ (e) ⎜ ⎟
⎝ 0 10 ⎠ ⎝8 1 ⎠
136 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

4.11. The differential equations of motion of the spring-mass system shown are :

2 2
m⋅
d
(
x + 2 ⋅ k ⋅ x1 − k ⋅ x2 = 0
2 1
) and 2⋅m ⋅
d
( )
x + 2 ⋅ k ⋅ x2 − k ⋅ x1 = 0
2 2
dt dt

Figure P 4.11

Obtain the natural frequencies ( eigenvalues ) and mode shapes ( eigenvectors) of the
system for m= 1 lb--sec 2 / ft and k = 1 lb/ ft

4.12 The two-dimensional state of stress in psi at a point on a machine element is


prescribed as:

⎛ 12 4 ⎞
σ=⎜ ⎟
⎝ 4 6⎠

Obtain the eigenvalues (principal stresses) and their associated directions (eigenvectors).

4.13. For the two pendulums shown which are coupled by means of a spring "k" , the
governing differential equations are:

2
m ⋅L ⋅
2 d
2 1
2
(
θ = −m ⋅ g ⋅ L ⋅ θ 1 − k ⋅ a ⋅ θ 1 − θ 2 )
dt

and

2
m ⋅L ⋅
2 d
2 2
2
(
θ = −m ⋅ g ⋅ L ⋅ θ 2 − k ⋅ a ⋅ θ 2 − θ 1 )
dt
Chapter 4: Matrices and Linear Algebra 137

Determine the natural frequencies (eigenvalues) and mode shapes (eigenvectors) of the system
for m = 1 lb- sec 2 / ft , L = 1 ft, g= 32.2 ft/ sec 2 , k = 1 lb/ ft and a = 0.5 ft

Figure P 4.13

4.14. The spring-mass system shown has the following mass and stiffness characteristics:

⎛⎜ 2 ⋅ m 0 0 ⎞⎟
M= ⎜ 0 m 0 ⎟
⎜ 0 0 2⋅m ⎟
⎝ ⎠

⎛⎜ 4 ⋅ k −k 0 ⎞⎟
K= ⎜ −k 2 ⋅ k −k ⎟
⎜ 0 −k 4 ⋅ k ⎟
⎝ ⎠

Figure P4.14

Use Mathcad to determine its natural frequencies (eigenvalues) and mode shapes
(eigenvectors) if m=1 kg and k= 1N/m
138 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

4.15. Determine the roots of the nonlinear simultaneous equations

f(x,y) = x 2 + y 2 -4.5 = 0 and g(x,y) = x 3 - 1.5 y = 0

using the Taylor series approach. Check your answer using the Given and Find functions
of Mathcad

4. 16. Determine the roots of the nonlinear simultaneous equations

f(x,y) = x 3 + y 3 - 5.5 = 0 and g(x,y) = x 4 - 3 y2 = 0

using the Taylor series approach. Check your answer using the Given and Find functions of
Mathcad .

4.17. A two-degree-of-freedom mathematical model of an automobile suspension system


is shown . With the system modeled as a rigid bar of mass " m " and mass moment of
inertia " I " connected to springs , the governing differential equations are:

W d2
⋅ x ( t) + 4000 ⋅ x ( t ) + 2000 ⋅ θ ( t) = 0
g dt 2

and

2
d
I⋅ θ ( t) + 2000 ⋅ x ( t) + 65000. ⋅ θ ( t) = 0
2
dt

R e fe r e n c e
x
L2
L1 C .G .
K1 K2

Figure P 4.17

where "x" represents the linear up and down ( bounce) motion and "θ" represents the
angular motion (pitch) of the system . Determine the natural frequencies ( eigenvalues )
and mode shapes ( eigenvectors ) if W = 3000 lbs , I = 300 lb-ft-sec 2 , and g =
acceleration due to gravity = 32.2 ft / sec 2
Chapter 4: Matrices and Linear Algebra 139

4.18. The governing differential equations of motion of the double pendulum shown in terms of
angular displacements θ1 (t) and θ2 (t) are

2 2
d d
L⋅ θ + 2⋅g⋅θ 1 − g⋅θ 2 = 0 and L⋅ θ + 2⋅g⋅θ 2 − 2⋅g⋅θ 1 = 0
2 1 2 2
dt dt

L
1

m
where "g" is the acceleration due to gravity
x1
= 32.2 ft/ sec 2 and L is the length of each L
pendulum. Using matrix methods , determine
the natural frequencies(eigenvalues) and 2
mode-shapes (eigenvectors) of the system
for L = 1 ft.
x2 m

Figure P 4.18

4.19. An automobile is math-modeled as the two-degree-of-freedom system shown above


in Figure P 4.17. Its up and down linear motion ( bounce ) is represented by the coordinate
x(t) while its angular motion ( pitch) is represented by the coordinate θ(τ). The governing
differential equations of motion are:

2
⎡⎣( K1 + K2) ⋅ x − ( K1 ⋅ L1 − K2 ⋅ L2) ⋅ θ⎤⎦ = 0
d
m⋅ x +
2
dt

2
JO ⋅
d
2 ⎣ ( ⎝ )
θ + ⎡ K1 ⋅ L1 − K2 ⋅ L2 ⋅ x + ⎛ K1 ⋅ L1 + K2 ⋅ L2 ⎞ ⋅ θ⎤ = 0
2 2
⎠ ⎦
dt

Determine the natural frequencies (eigenvalues) and mode-shapes (eigenvectors) of an


automobile with the following data:

m = 100 lb-sec2 / ft , JO = 1600 lb-ft-sec 2 , L1 = 5.5 ft , L 2 = 4.5 ft,

K1 = 2600 lbs / ft , K 2 = 2400 lbs/ ft.


140 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

4.20. A rigid rod of negligible mass and length 2L is pivoted at the middle point and is
constrained to move in the vertical plane by springs and masses shown. The governing
differential equations of motion of the system are given as:

2 2
d 2 d 2
m⋅ x + 2⋅k ⋅x + k ⋅L⋅θ = 0 , 4⋅m ⋅L ⋅ θ + k ⋅L⋅x + 2⋅k ⋅L ⋅θ = 0
2 2
dt dt

K
(t)
L 2m
2m
L

x (t)
K

Figure P 4.20

Determine the natural frequencies (eigenvalues) and mode-shapes (eigenvectors) of the system
for m = 1 kg, L = 1 meter, and k = 1 N/m .

4.21. The governing differential equations of motion of the double pendulum shown in
Figure P 4.18 in terms of coordinates of coordinates x 1 (t) and x 2 (t) are

2 2
d d
L⋅ x1 + 3 ⋅ g ⋅ x1 − g ⋅ x2 = 0 , L⋅ x + g ⋅ x2 − g ⋅ x1 = 0
2 2 2
dt dt

where "g" is the acceleration due to gravity = 32.2 ft/ sec 2 and L is the length of each
pendulum. Determine the natural frequencies (eigenvalues) and mode-shapes (eigenvectors) of
the system for L = 1 ft.

4. 22. Determine the roots of the nonlinear simultaneous equations

f(x,y) = x 2 + 2 x y - 64 = 0 and g(x,y) = 3 x 2 + 6 y2 - 264 = 0

using the Taylor series approach. Obtain answers that are correct to two decimal places.
Use starting values x old= 3 and y old = 5 . Check your answer using the Given and Find
functions of Mathcad .
Chapter 5: Numerical Interpolation 141

CHAPTER 5

NUMERICAL INTERPOLATION

In any field where measured or statistical data is involved, a need to interpolate between given
data points is bound to exist. Because measured or available data is , typically, not provided in
the form of an analytically determined function , the process known as interpolation must be
resorted to in order to obtain function values at points other thn the given data points. This
process involves the generation of a curve that must pass through the given data points and its
use in determining the function value at any intermediate point on this curve. As will be seen in
the sections following, a polynomial fitting n data points will be of order (n-1), that is one less
than the number of data points given. Thus, four data points will generate a cubic while three
data points will give a quadratic and so on.

5.1 LINEAR INTERPOLATION

Given two data points ( x k , y k ) and (x k+1 , y k+1 ), as shown in the figure below, the
connection between the coordinates of a point ( x, y) lying between the data points can be
expressed by the linear relationship

( )
y k ⋅ x k − x k+1 − y
( k+ 1) ⋅ ( x− xk)
y= (5.1)
x k − x k+1

5.2 THE METHOD OF UNDETERMINED COEFFICIENTS

Given the data points ( x k , y k ), k= 0...n, it is required to find an interpolating polynomial


P n ( x ) such that the following constraint equations are satisfied

( )
Pn x 0 = y 0
142 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )
Pn x 1 = y 1
(5.2)

( )
Pn x n = y n

Assuming the interpolating polynomial P n ( x ) is of the form

2 n
Pn( x ) = a0 + a1 ⋅ x + a2 ⋅ x + ............ + an ⋅ x , (5.3)

Equations (5.2) yield the following matrix equation

⎡1 x0 ( xo) 2 (x0)n ⎤⎥ ⎛ a0 ⎞
⎢ ⎛ y0 ⎞
⎢ ⎜ ⎟ ⎜ ⎟
⎢1 x1 ( x1) 2 (x1)n ⎥⎥ ⎜ a1 ⎟ ⎜ y1 ⎟
⎢ ⎥ ⋅⎜ ⎟ = ⎜ ⎟ (5.4)
⎢ ⎥⎜ ⎟ ⎜ ⎟
⎢ ⎥⎜ ⎟ ⎜ ⎟
⎢ ⎜ ⎟ ⎜ ⎟
n ⎥ ⎝ an ⎠ ⎝ y n ⎠
⎣1 xn ( xn) 2 ( xn) ⎦

Once the equations represented by the matrix equation (5.4) are solved for the n unknowns
a0 , a1 ,..... , a n , the interpolating polynomial P n ( x ) satisfying the constraint
equations is completely determined. In general, the higher the order of the interpolating
polynomial, the more accurate would be the results of the interpolation process.

EXAMPLE 5.1

Determine the polynomial f(x) that passes thru (0,2), (1,2.25) and (2,5.82), and
compute f(0.5) and f(1.5).

The given data is put in as follows. Go to the Insert menu, select Matrix, put in
the appropriate number of rows and columns and then fill in the placeholders.
Chapter 5: Numerical Interpolation 143

⎛⎜ 0 ⎞⎟ ⎛⎜ 2 ⎞⎟
X := ⎜ 1 ⎟ Y := ⎜ 2.25 ⎟
⎜2 ⎟ ⎜ 5.82 ⎟
⎝ ⎠ ⎝ ⎠

The order of the polynomial fitting 3 data points is 2 and the form, thus, is

f(x) = a + b x + c x 2 .

Therefore, the matrix equation that needs to be solved for the coefficients a, b and c is

⎛ 1 0 02 ⎞ a
⎜ ⎟ ⎛⎜ ⎞⎟ ⎛⎜ 2 ⎞⎟
⎜ 2 ⎟ ⋅ ⎜ b ⎟ = ⎜ 2.25 ⎟
⎜1 1 1 ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 2 ⎟ ⎝ c ⎠ ⎝ 5.82 ⎠
⎝ 1 2 2 ⎠

Using Cramer's rule, the solution to the above equation can be written as :

a = m1/ D , b= m2/D and c = m3/D,

where

⎛⎜ 2 0 0 ⎞⎟ ⎛⎜ 1 2 0 ⎞⎟ ⎛⎜ 1 0 2 ⎞⎟ ⎛⎜ 1 0 0 ⎞⎟
m1 := ⎜ 2.25 1 1 ⎟ m2 := ⎜ 1 2.25 1 ⎟ m3 := ⎜ 1 1 2.25 ⎟ D := ⎜ 1 1 1 ⎟
⎜ 5.82 2 4 ⎟ ⎜ 1 5.82 4 ⎟ ⎜ 1 2 5.82 ⎟ ⎜1 2 4 ⎟
⎝ ⎠ ⎝ ⎠ ⎝ ⎠ ⎝ ⎠

m1 m2 m3
a := b := c := a= 2 b = −1.41 c = 1.66
D D D

Thus, the interpolating polynomial generated is

2
f ( x ) := a + b ⋅ x + c ⋅ x with a= 2 b = −1.41 c = 1.66

It is plotted in Figure 5.1 below, which gives


144 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

f ( 0.5 ) = 1.71 f ( 1.5 ) = 3.62

x := 0 , 0.1 .. 2

Interpolated function
6
Given data points
Interpolation
5

Yi 4

f( x)
3

1
0 0.5 1 1.5 2
Xi , x

Figure 5.1. Interpolated function using undetermined coefficient method

5.3 THE GREGORY-NEWTON INTERPOLATING POLYNOMIAL

The form of the nth order Gregory-Newton interpolating polynomial is

( ) ( )( ) ( )( )(
f ( x ) = a1 + a2 ⋅ x − x 1 + a3 ⋅ x − x 1 ⋅ x − x 2 + a4 ⋅ x − x 1 ⋅ x − x 2 ⋅ x − x 3 ... )
( )( ) ( ) ( )( ) (
+ ....... + an ⋅ x − x 1 ⋅ x − x 2 .... x − x n− 1 + an + 1 ⋅ x − x 1 ⋅ x − x 2 .... x − x n ) (5.5)

where the a' s are unknown coefficients that can be determined as was done in the method of
undetermined coefficients. That is ,

( )
f xk = yk k := 1 , .. n + 1 (5.6)

where the x k and y k are the given data points that the interpolating polynomial must pass
through.
Chapter 5: Numerical Interpolation 145

To obtain a second order polynomial, put x= x1 , x= x 2 and x = x 3 in Equation (5.5)


yielding

a1 = y 1

y2 − y1
a2 =
x2 − x1 (5.7)

(
y 3 − a1 − a2 ⋅ x 3 − x 1 )
a3 =
( x 3 − x 1) ⋅ ( x 3 − x 2)

The interpolating polynomial is then determined as

( ) ( )(
f ( x ) = a1 + a2 ⋅ x − x 1 + a3 ⋅ x − x 1 ⋅ x − x 2 ) (5.8)

which is a second order polynomial passing through the three data points ( x1 , y1 ),
(x2 , y2 ) and (x3 , y3 ).

For obtaining a third order polynomial, a fourth data point will be needed , and a fourth order
polynomial will require the use of a fourth as well as a fifth data point . In general, determining
an nth order polynomial , then, will require (n+1 ) data points to be prescribed. Thus, the
coefficients a4 , a5 and a6 can be determined using

( ) ( )(
y 4 − a1 − a2 ⋅ x 4 − x 1 − a3 ⋅ x 4 − x 1 ⋅ x 4 − x 2 )
a4 = (5.9)
( x 4 − x 1) ⋅ ( x 4 − x 2) ⋅ ( x 4 − x 3)

( ) ( )( ) ( )(
y 5 − a1 − a2 ⋅ x 5 − x 1 − a3 ⋅ x 5 − x 1 ⋅ x 5 − x 2 − a4 ⋅ x 5 − x 1 ⋅ x 5 − x 2 ⋅ x 5 − x 3 )( )
a5 =
( x 5 − x 1) ⋅ ( x 5 − x 2) ⋅ ( x 5 − x 3) ⋅ ( x 5 − x 4)
(5.10)
146 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( ) ( )( ) ( )(
y 6 − a1 − a2 ⋅ x 6 − x 1 − a3 ⋅ x 6 − x 1 ⋅ x 6 − x 2 − a4 ⋅ x 6 − x 1 ⋅ x 6 − x 2 ⋅ x 6 − x 3 ... )( )
( )( )( )(
+ ( −1) ⋅ a5 ⋅ ⎡⎣ x 6 − x 1 ⋅ x 6 − x 2 ⋅ x 6 − x 3 ⋅ x 6 − x 4 ⎤⎦ )
a6 =
( x 6 − x 1) ⋅ ( x 6 − x 2) ⋅ ( x 6 − x 3) ⋅ ( x 6 − x 4) ⋅ ( x 6 − x 5)
(5.11)

Example 5.2 -

Given three data points: X: 100 200 400

Y: 975 1575 2054

Determine the Gregory -Newton Polynomial f(x) and evaluate f(251)

The form of this polynomial will be :

y = f(x) = a1 + a2(x-X1 + a3(x-X1)(x-X2)

with a1 = Y1

a2 = (Y2-Y1) / (X2-X1)

a3 = [ Y3- a1 -a2(X3-X1)] /[ (X3-X1)(X3-X2)]

The data is put in in vector form as follows.

⎛⎜ 100 ⎞⎟ ⎛⎜ 975 ⎞⎟
X := ⎜ 200 ⎟ Y := ⎜ 1575 ⎟
⎜ 400 ⎟ ⎜ 2054 ⎟
⎝ ⎠ ⎝ ⎠

and the coefficients a1, a2 and a3 are computed using

Y2 − Y1 (
Y3 − a1 − a2 ⋅ X3 − X1 )
a1 := Y1 a2 := a3 :=
X2 − X1 ( X3 − X1) ⋅ ( X3 − X2)
Chapter 5: Numerical Interpolation 147

a1 = 975 a2 = 6 a3 = −0.012

Thus, the interpolating polynomial generated is

( ) ( )(
f ( x ) := a1 + a2 ⋅ x − X1 + a3 ⋅ x − X1 ⋅ x − X2 )

which is plotted in Figure 5.2 below, giving

3
f ( 251) = 1.788 × 10

x := 100 , 105 .. 400

Gregory-Newton Interpolating Polynomial


2500

2000
Function values

Yi
1500
f( x)
1000 Given data points
Interpolation
500
100 150 200 250 300 350 400
Xi , x

x- values

Figure 5.2. Interpolated function obtained using Gregory-Newton


polynomial method

Example 5.3

Given: x: 0 100 200 300 400 500


y: 104.2 97.3 48.1 23.7 8.6 5.2

Determine a fifth order Gregory-Newton interpolating polynomial f(x)


and evaluate f(150 ), f(350) and f (450).
148 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Because six data points are given, the interpolating polynomial in this case will be of fifth
order, and will have the following form

f(x) = a1 + a2(x-x1) + a3(x-x1)(x-x2) + a4(x-x1)(x-x2)(x-x3)


+a5(x-x1)(x-x2)(x-x3)(x-x4) + a6x-x1)(x-x2)(x-x3)(x-x4)(x-x5)

where a1, a2 .... are computed as shown below.

x1 := 0 x2 := 100 x3 := 200 x4 := 300 x5 := 400 x6 := 500

y1 := 104.2 y2 := 97.3 y3 := 48.1 y4 := 23.7 y5 := 8.6 y6 := 5.2

y2 − y1 y3 − a1 − a2 ⋅ ( x3 − x1)
a1 := y1 a2 := a3 :=
x2 − x1 ( x3 − x1) ⋅ ( x3 − x2)

y4 − a1 − a2 ⋅ ( x4 − x1) − a3 ⋅ ( x4 − x1) ⋅ ( x4 − x2)


a4 :=
( x4 − x1) ⋅ ( x4 − x2) ⋅ ( x4 − x3)

y5 − a1 − a2 ⋅ ( x5 − x1) − a3 ⋅ ( x5 − x1) ⋅ ( x5 − x2) − a4 ⋅ ( x5 − x1) ⋅ ( x5 − x2) ⋅ ( x5 − x3)


a5 :=
( x5 − x1) ⋅ ( x5 − x2) ⋅ ( x5 − x3) ⋅ ( x5 − x4)

y6 − a1 − a2 ⋅ ( x6 − x1) − a3 ⋅ ( x6 − x1) ⋅ ( x6 − x2) − a4 ⋅ ( x6 − x1) ⋅ ( x6 − x2) ⋅ ( x6 − x3) ...


+ ( −1) ⋅ a5 ⋅ ( x6 − x1) ⋅ ( x6 − x2) ⋅ ( x6 − x3) ⋅ ( x6 − x4)
a6 :=
( x6 − x1) ⋅ ( x6 − x2) ⋅ ( x6 − x3) ⋅ ( x6 − x4) ⋅ ( x6 − x5 )

The above equations generate the following computed coefficients

−3 −5
a1 = 104.2 a2 = −0.069 a3 = −2.115 × 10 a4 = 1.118 × 10

−8 − 11
a5 = −3.442 × 10 a6 = 8.375 × 10

which are inserted into the interpolating polynomial

f ( x ) := a1 + a2 ⋅ ( x − x1) + a3 ⋅ ( x − x1) ⋅ ( x − x2) + a4 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ...


+ a5 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ...
+ a6 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ⋅ ( x − x5 )
Chapter 5: Numerical Interpolation 149

yielding

f ( 150) = 70.68 f ( 350) = 16.435 f ( 450) = 1.84

The interpolated curve f(x) is shown below in Figure 5.3 along with the given data points

⎛ 0 ⎞ ⎛ 104.2 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 100 ⎟ ⎜ 97.3 ⎟
⎜ 200 ⎟ ⎜ 48.1 ⎟
X := ⎜ ⎟ Y := ⎜ ⎟
⎜ 300 ⎟ ⎜ 23.7 ⎟
⎜ 400 ⎟ ⎜ 8.6 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 500 ⎠ ⎝ 5.2 ⎠

x := 0 , 5 .. 500

Interpolated function( Gregory-Newton)


150
Given data points
Interpolation

100
Function values

Yi

f( x)
50

0
0 100 200 300 400 500
Xi , x

x-values

Figure 5.3. Fifth order Gregory-Newton interpolating polynomial

5.4 INTERPOLATION USING FINITE DIFFERENCES

A finite difference table can furnish a way of developing an interpolating polynomial so long
as the known values of the independent variable are equally spaced. For an increment Δx in
the independent variable x, the first finite difference of the dependent variable y = f(x) is
150 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Δf = f(x+Δx)- f(x) (5.12)

The second finite difference is

Δ 2 f = Δ[Δf] =[f(x+2Δx)-f(x+Δx)]-[f(x+Δx)- f(x)] (5.13)

Similarly, the (n-1)th finite difference will be

Δn f = Δ[Δ n-1 f ] (5.14)

The finite differences as derived above are arranged in a table as shown in Table 5.1 which
can be used to generate an interpolating polynomial, as will be demonstrated later.

TABLE 5.1
Table of finite differences
--------------------------------------------------------------------------------------------------------------------------------
2 3 n
x f ( x) Δf Δ f Δ f Δ f
-------------------------------------------------------------------------------------------------------------------------------
x f ( x)

Δf ( x )

2
x + Δx f ( x + Δx ) Δ f ( x)

3
Δf ( x + Δx ) Δ f ( x)

2
x + 2 ⋅ Δx f ( x + 2 ⋅ Δx ) Δ f ( x + Δx )

Δf ( x + 2 ⋅ Δx )

x + 3 ⋅ Δx f ( x + 3 ⋅ Δx )
------------------------------------------------------------------------------------------------------------------------------------
Chapter 5: Numerical Interpolation 151

An alternate representation of the above is shown in Table 5.2, in which the independent
variable goes from x 0 to x 3 and successive finite differences up to the fourth are generated

TABLE 5.2

Alternate form of finite-difference table

------------------------------------------------------------------------------------------------------------------------------------

2 3 4
xi yi Δy i Δ yi Δ yi Δ yi

----------------------------------------------------------------------------------------------------------------------------------

x0 y0

Δy 0 = y 1 − y 0

2
x1 y1 Δ y 0 = Δy 1 − Δy 0

3 2 2
Δy 1 = y 2 − y 1 Δ y0 = Δ y1 − Δ y0

2 4 3 3
x2 y2 Δ y 1 = Δy 2 − Δy 1 Δ y0 = Δ y1 − Δ y0

Δy 2 = y 3 − y 2 3 2 2
Δ y1 = Δ y2 − Δ y1

x3 y3 2
Δ y 2 = Δy 3 − Δy 2

------------------------------------------------------------------------------------------------------------------------------------

5.5 NEWTON'S METHOD UTILIZING FINITE DIFFERENCES

Let ( x0 , y0 ), (x1 , y1 ) , ( x2 , y2 )and ( x3 , y3 ) be four given data points for which


a third degree Newton interpolating polynomial utilizing finite differences has to be determined .
Assume the interpolating polynomial to be

( ) ( )( ) ( )(
f ( x ) = C0 + C1 ⋅ x − x 0 + C2 ⋅ x − x 0 ⋅ x − x 1 + C3 ⋅ x − x 0 ⋅ x − x 1 ⋅ x − x 2 )( ) (5.15)
152 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

where the C's are undetermined coefficients that must be computed using the following constraint
equations that the polynomial must satisfy .

( )
y0 = f x0 ( )
y1 = f x1 ( )
y2 = f x2 ( )
y3 = f x3
(5.16)

which yield

y 0 = C0

(
y 1 = C0 + C1 ⋅ x 1 − x 0 )
(5.17)

( ) (
y 2 = C0 + C1 ⋅ x 2 − x 0 + C2 ⋅ x 2 − x 0 ⋅ x 2 − x 1 )( )

( ) ( )( ) ( )( )(
y 3 = C0 + C1 ⋅ x 3 − x 0 + C2 ⋅ x 3 − x 0 ⋅ x 3 − x 1 + C3 ⋅ x 3 − x 0 ⋅ x 3 − x 1 ⋅ x 3 − x 2 )

Using the finite differences

x 1 − x 0 = Δx x 2 − x 0 = 2Δx x 3 − x 0 = 2Δx (5.18)

the undetermined coefficients are obtained as

Δy 0
C0 = y 0 C1 =
Δx
(5.19)
2 3
Δ y0 Δ y0
C2 = C3 =
2 3
2! ⋅ Δx 3! ⋅ Δx

Defining

x − x 0 = n ⋅ Δx

x − x 1 = ( n − 1) ⋅ Δx (5.20)

x − x 2 = ( n − 2) ⋅ Δx
Chapter 5: Numerical Interpolation 153

where

x − x0
n= (5.21)
Δx

the interpolating polynomial of equation (5.15) can now be written as

n ⋅ ( n − 1) n ⋅ ( n − 1) ⋅ ( n − 2)
( ) ( )
f ( x ) = f x 0 + n ⋅ Δf x 0 +
2!
2
( )
⋅ Δ f x0 +
3!
3
( )
⋅ Δ f x 0 + ...............

(5.22)

From the above process, it can be seen that the form of an mth order interpolating polynomial will
be

n ⋅ ( n − 1) n ⋅ ( n − 1) ⋅ ( n − 2)
( ) ( )
f ( x ) = f x 0 + n ⋅ Δf x 0 +
2!
2
( )
⋅ Δ f x0 +
3!
3
( )
⋅ Δ f x 0 + ...... ...

n ⋅ ( n − 1) .( n − m + 1) m
+
m!
⋅ Δ f x0 ( ) (5.23)

Example 5.4

The following data is given on the radiation flux R in gram-calories per square centimeter per
day for a certain month in a given year as a function of latitude L
(deg N.)

L 0 20 40 60 80

R 895 858 722 489 235

Generate the finite difference table and obtain an interpolating polynomial using Newton's
method. Estimate the flux R at a latitude of 45 degrees.

The following finite difference table can be generated using the given data.
154 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

________________________________________________

Lat R delta R del 2 R del 3 R del 4 R


____________________________________________________

0 895
-37
20 858 -99
-136 2
40 722 -97 74
-233 76
60 489 -21
-254
80 235

____________________________________________________

The interpolating poly can now be written as:

R(L) = R(L0) + n del R(L0) + (1/2) n(n-1) del 2 R(L0) + (1/6) n(n-1)(n-2)del 3 R ( L0)
+ (1/24) n(n-1)(n-2)(n-3) del 4 R(L0)

where R(L0)= 895; del R (L0)=-37; del 2 R(L0)= -99; del 3 R(L0)=2;

del 4 R(L0)=74; n=( L- L0)/ del L = ( L- 0)/ 20 = L/20

Calculations are done below and the interpolating polynomial plotted in Figure 5.4
RL0 := 895 L0 := 0 delRL0 := −37 del2RL0 := −99 del3RL0 := 2 del4RL0 := 74

L − L0
delL := 20 n ( L) :=
delL

n ( L) ⋅ ( n ( L) − 1) ⋅ del2RL0 n ( L) ⋅ ( n ( L) − 1) ⋅ ( n ( L) − 2) ⋅ del3RL0
R ( L) := RL0 + n ( L) ⋅ delRL0 + + ...
2 6
n ( L) ⋅ ( n ( L) − 1) ⋅ ( n ( L) − 2) ⋅ ( n ( L) − 3) ⋅ del4RL0
+
24
Chapter 5: Numerical Interpolation 155

⎛⎜ 0 ⎞⎟ ⎛⎜ 895 ⎞⎟
⎜ 20 ⎟ ⎜ 858 ⎟
⎜ ⎟
LAT := 40
⎜ ⎟
FLUX := 722 <---
These are the given data points, in vector form,
⎜ ⎟ ⎜ ⎟ needed for the plot below
⎜ 60 ⎟ ⎜ 489 ⎟
⎜ 80 ⎟ ⎜ 235 ⎟
⎝ ⎠ ⎝ ⎠

L := 0 , 1 .. 80

Interpolated function by Newton's meth


1000
Given data points
Interpolation
800
Radiation flux

FLUX
600
R( L)

400

200
0 10 20 30 40 50 60 70 80
LAT , L
Latitude- degrees North

Figure 5.4. The Newton interpolating polynomial

The interpolation yields R ( 45 ) = 671.14

5.6 THE LAGRANGE INTERPOLATING POLYNOMIAL

In many practical problems, data measurements are recorded at unequal spacings or


intervals. In such situations, the Lagrangian interpolation method offers a viable means of
deducing an interpolating polynomial connecting the dependent variable with the
independent variable at intervals that are not necessarily constant.

By this method, given a data sample

(x1 , y1 ); (x2 , y2 ); .......... ( x n , y n )


156 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

an interpolating polynomial can be generated using

n
f ( x) =
∑ (wi ( x) . ⋅ yi ) (5.24)
i = 1

where

∏ (x − xj )
j = 1
wi ( x ) = j≠i (5.25)
n

∏ (xi − xj )
j = 1

For example, if three data points are prescribed, n is 3 and the interpolating polynomial will
be

3
f ( x) =
∑ (wi ( x) .yi )
i = 1

with

( x − x2) ⋅ (x − x3) ( x − x1) ⋅ (x − x3)


w1 ( x ) = w2 ( x ) =
( x1 − x2) ⋅ (x1 − x3) ( x2 − x1) ⋅ (x2 − x3)
(5.26)
( x − x1) ⋅ (x − x2)
w3 ( x ) =
( x3 − x1) ⋅ (x3 − x2)

Example 5.5

Given the following data

x: 96 207 375 450

y: 932 850 767 1235

Determine the Lagrange Interpolating Polynomial f(x) that passes through the above points .
Determine f(102) , f(322) and f(415)
Chapter 5: Numerical Interpolation 157

The given data is

x1 := 96 x2 := 207 x3 := 375 x4 := 450

y1 := 932 y2 := 850 y3 := 767 y4 := 1235

The Lagrange interpolating polynomial, in this case, will be a cubic of the following form

f ( x ) = w1 ( x ) ⋅ y1 + w2 ( x ) ⋅ y2 + w3 ( x ) ⋅ y3 + w4 ( x ) ⋅ y4

where

( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ( x − x1) ⋅ ( x − x3) ⋅ ( x − x4)


w1 ( x ) := w2 ( x ) :=
( x1 − x2) ⋅ ( x1 − x3) ⋅ ( x1 − x4) ( x2 − x1) ⋅ ( x2 − x3) ⋅ ( x2 − x4)

( x − x1) ⋅ ( x − x2) ⋅ ( x − x4) ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3)


w3 ( x ) := w4 ( x ) :=
( x3 − x1) ⋅ ( x3 − x2) ⋅ ( x3 − x4) ( x4 − x1) ⋅ ( x4 − x2) ⋅ ( x4 − x3)

Note that the equations for w1(x), w2(x) etc have to be written in Mathcad as shown above
and not in the form of Equation 5.25. Then, the interpolating polynomial is generated using

f ( x ) := w1 ( x ) ⋅ y1 + w2 ( x ) ⋅ y2 + w3 ( x ) ⋅ y3 + w4 ( x ) ⋅ y4

and is shown in Figure 5.5. It yields f ( 102) = 940.053 f ( 322) = 683.419

f ( 415 ) = 955.728

⎛ 96 ⎞ ⎛ 932 ⎞
⎜ ⎟ ⎜ ⎟
X :=
⎜ 207 ⎟
Y :=
⎜ 850 ⎟
⎜ 375 ⎟ ⎜ 767 ⎟ <--- Given data points in vector form, used in the plot below
⎜ ⎟ ⎜ ⎟
⎝ 450 ⎠ ⎝ 1235 ⎠
158 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x := 96 , 97 .. 450

Lagrange Interpolating Polynomial


1400
Given data points
Interpolation
1200
Function values

Y
1000
f( x)

800

600
100 150 200 250 300 350 400 450
X, x
X- Values

Figure 5.5. The Lagrange interpolating polynomial

5.7 INTERPOLATION USING LINEAR, QUADRATIC AND CUBIC


SPLINES.

For n data points that are provided, the highest- order interpolating polynomial that can be
generated will be of order (n-1) . Although, in general, the accuracy of the interpolation
process increases with the order of the polynomial , there are situations when the accuracy
can, in fact, decrease with the polynomial order. This can happen when the measured data
reflects abrupt changes in the dependent variable values for steady changes in the
independent variable . In these cases, accuracy can be improved by resorting to lower
order polynomials, commonly referred to as splines . Splines normally used are linear,
quadratic and cubic.

Linear Splines: A linear spline results from connecting adjacent data points with straight
lines. Given n data points (x1 , y1 ), (x2 , y2 ), ( x3 , y3 ),..... (x n , y n ) , the interpolation
function for x between x 1 and x 2 is

x − x1
f ( x) = y1 +
x2 − x1
(
⋅ y2 − y1 ) (5.27)

Similarly, for x between x 2 and x 3 the interpolation function will be


Chapter 5: Numerical Interpolation 159

x − x2
f ( x) = y2 +
x3 − x2
(
⋅ y3 − y2 )
(5.28)
It follows ,then, that for x between x n-1 and x n

x − xn−1
f ( x) = yn−1 +
x n − x n− 1
(
⋅ y n − yn−1 ) (5.29)

The resulting linear spline, however, must satisfy the following constraint equations

( ) ( )
f xi = fi+1 x i for i = 1 , 2..... , n − 1 (5.30)

Quadratic Splines: Quadratic splines are of the form : ax 2 + bx + c The conditions to be


satisfied are

(1) The spline function must pass through the given data points.
(2) The spline is to be continuous at the interior data points .
(3) The second derivative of the spline between the first two data points is to be zero.

Cubic Splines: Cubic splines have the form: ax 3 + b x 2 + cx + d and must satisfy the
following conditions

(1) The spline function must pass through the given data points
(2) The first derivatives of the spline must be continuous.
(3) The second derivatives of the spline must also be continuous.
(4) The second derivative must be zero at the first and second data points.

5.8 INTERPOLATION WITH MATHCAD

Interpolation in Mathcad allows you to either connect the data points with straight lines
( linear interpolation ) or with sections of a cubic polynomial ( cubic spline interpolation ).
These interpolation functions return a curve passing through the points you specify.

Linear interpolation: This is done using the linterp function as shown below.

linterp(vx,vy,x): Uses the data vectors vx and vy to return a linearly interpolated value of y
corresponding to x . The quantities vx and vy must be specified as vectors and vx must
contain real values in ascending order. The linterp function is intended for interpolation and not
for extrapolation.
160 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Cubic spline interpolation: Cubic spline interpolation enables you to pass a curve through a
set of points such that the first and second derivatives of the curve are continuous at each
point. Mathcad does this by taking three adjacent points and constructing a cubic polynomial
that passes through these points. These cubics are then connected together to make up the
interpolated curve.

In order to fit a cubic spline curve through a set of points, the procedure shown below
must be followed:

1. Create the vectors vx and vy containing the x and y coordinates of the points of
interest through which you want to go through. The elements of vx must be put in in
ascending order.

2. Generate the vector vs:= cspline( vx,vy) . This vector vs is a vector of intermediate
results to be used with interp .

3. To evaluate the spline at a point x1, do the following: interp(vs,vx,vy,x1) .

Note: Steps 2 and 3 can be combined by doing : interp(cspline(vx,vy),vx,vy,x1)

Mathcad has two other cubic spline functions as given below:

lspline(vx,vy) : This generates a spline curve that approaches a straight line at the endpoints.

pspline(vx,vy) : This generates a spline curve that approaches a parabola at the


endpoints.

The function interp(vs,vx,vy,x) returns the interpolated y value corresponding to x. The vector
vs is a vector of intermediate results obtained by utilizing the option of using lspline, pspline
or cspline on the vectors of given data , namely, vx and vy .

For Example 5.1 , the vx and vy vectors are

⎛⎜ 0 ⎞⎟ ⎛⎜ 2 ⎞⎟
vx := ⎜ 1 ⎟ vy := ⎜ 2.25 ⎟
⎜2 ⎟ ⎜ 5.82 ⎟
⎝ ⎠ ⎝ ⎠

As shown below, the linterp function is used to do a linear interpolation. The resulting spline
function is drawn in Figure 5.6 and the values of the interpolated function at
x= 0.5 and 1.5 are:
Chapter 5: Numerical Interpolation 161

x := 0 , 0.1 .. 2

vy
4
linterp( vx , vy , x)

2
0 0.5 1 1.5 2
vx , x

Figure 5.6. Interpolated function obtained with linterp

linterp ( vx , vy , 0.5 ) = 2.125 linterp ( vx , vy , 1.5 ) = 4.035

The interp function along with lspline, pspline and cspline can be utilized as follows to
generate cubic splines

vs1 := lspline ( vx , vy) vs2 := pspline ( vx , vy) vs3 := cspline ( vx , vy)

f1 ( x ) := interp ( vs1 , vx , vy , x ) f2 ( x ) := interp ( vs2 , vx , vy , x ) f3 ( x ) := interp ( vs3 , vx , vy , x )


flin ( x ) := linterp ( vx , vy , x )

Values of the interpolated cubic splines at x= 0.5 and 1.5 are

f1 ( 0.5 ) = 1.814 f2 ( 0.5 ) = 1.71 f3 ( 0.5 ) = 1.502 f1 ( 1.5 ) = 3.724 f2 ( 1.5 ) = 3.62

f3 ( 1.5 ) = 3.828 flin ( 0.5 ) = 2.125 flin ( 1.5 ) = 4.035

Figure 5.7 shows a comparison of the various splines generated using Mathca d
162 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x := 0 , 0.1 .. 2

Comparison of Mathcad splines


6
Given data points
Using lspline
Using pspline
5
Using cspline
vy Using linterp
f1( x)
function values

4
f2( x)

f3( x)
3
flin( x)

1
0 0.5 1 1.5 2
vx , x
x- Values

Figure 5.7. Comparison of spline functions obtained with Mathcad

For the problem of Example 5.2, interpolations using the Mathcad linterp and interp functions yield
the following. The results are sketched in Figure 5.8.

⎛⎜ 100 ⎞⎟ ⎛⎜ 975 ⎞⎟
vx := ⎜ 200 ⎟ vy := ⎜ 1575 ⎟
⎜ 400 ⎟ ⎜ 2054 ⎟
⎝ ⎠ ⎝ ⎠

vs := cspline ( vx , vy) fcspline ( x ) := interp ( vs , vx , vy , x ) flin ( x ) := linterp ( vx , vy , x )

3 3
fcspline ( 251) = 1.737 × 10 flin ( 251) = 1.697 × 10
Chapter 5: Numerical Interpolation 163

x := 100 , 105 .. 400

Interpolation with Mathcad


2200

2000

1800
function values

vy
1600
fcspline ( x)

flin( x) 1400

1200

Given data points


1000 Mathcad's cubic spline
Mathcad's linear interpolation with linterp
800
100 150 200 250 300 350 400
vx , x
x- values

Figure 5.8. Interpolation with Mathcad

For the problem of Example 5.3 , interpolation using the Mathcad spline functions is shown
below

⎛ 0 ⎞
⎜ ⎟ ⎛ 104.2 ⎞
⎜ ⎟
⎜ 100 ⎟ ⎜ 97.3 ⎟
⎜ 200 ⎟
vx := ⎜
⎜ 48.1 ⎟
⎟ vy := ⎜
<---vx and vy vectors

⎜ 300 ⎟ ⎜ 23.7 ⎟
⎜ 400 ⎟ ⎜ 8.6 ⎟
⎜ ⎟
⎝ 500 ⎠ ⎜ ⎟
⎝ 5.2 ⎠
flin ( x ) := linterp ( vx , vy , x )

Linear interpolation values at x= 150, x= 350 and x= 450 are


164 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

linterp ( vx , vy , 150) = 72.7 linterp ( vx , vy , 350) = 16.15 linterp ( vx , vy , 450) = 6.9

The linearly interpolated function is shown in Figure 5.9.

x := 0 , 5 .. 500

150
Given data points
Interpolated values

Mathcad's linear interpolation


vy 100

linterp( vx , vy , x)
50

0
0 100 200 300 400 500
vx , x
x- values

Figure 5.9. Linear interpolation for Example 5.3

The Gregory Newton polynomial for this problem has been derived earlier and is the function
f(x) shown below .

x1 := 0 x2 := 100 x3 := 200 x4 := 300 x5 := 400 x6 := 500


<--data points
y1 := 104.2 y2 := 97.3 y3 := 48.1 y4 := 23.7 y5 := 8.6 y6 := 5.2

−3
a1 := 104.2 a2 := −0.069 a3 := −2.115 ⋅ 10
<--Coefficients
obtained in
−5 −8 − 11 Example 5.3
a4 := 1.118 ⋅ 10 a5 := −3.442 ⋅ 10 a6 := 8.375 ⋅ 10

f ( x ) := a1 + a2 ⋅ ( x − x1) + a3 ⋅ ( x − x1) ⋅ ( x − x2) + a4 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ...


+ a5 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ...
+ a6 ⋅ ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ⋅ ( x − x5 )

vs1 := lspline ( vx , vy) vs2 := pspline ( vx , vy) vs3 := cspline ( vx , vy)


Chapter 5: Numerical Interpolation 165

<-- Mathcad
flin ( x ) := linterp ( vx , vy , x ) flspline ( x ) := interp ( vs1 , vx , vy , x ) splines

fpspline ( x ) := interp ( vs2 , vx , vy , x ) fcspline ( x ) := interp ( vs3 , vx , vy , x )

Figure 5.10 shows a comparison of the Mathcad spline functions with the
Gregory-Newton interpolation f(x)

⎛ 0 ⎞ ⎛ 104.2 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 100 ⎟ ⎜ 97.3 ⎟
⎜ 200 ⎟ ⎜ 48.1 ⎟
vx := ⎜ ⎟ vy := ⎜ ⎟ <--- Given data in vector form
⎜ 300 ⎟ ⎜ 23.7 ⎟
⎜ 400 ⎟ ⎜ 8.6 ⎟
⎜ ⎟ ⎜ ⎟ x := 0 , 5 .. 500
⎝ 500 ⎠ ⎝ 5.2 ⎠
Interpolation with Mathcad Splines and Gregory-Newton interpolation
150
Given data points
vy Linterp
interp/lspline
flin( x) interp/pspline
Interpolated values

100
flspline( x) interp/cspline
Gregory-Newton interpolation
fpspline( x)

fcspline ( x)
50
f( x)

0
0 100 200 300 400 500
vx , x
x values

Figure 5.10. Comparison of Mathcad splines with the


Gregory-Newton interpolating polynomial

The Mathcad interpolated values at x = 150, 350 and 450 are shown below.

flin ( 150) = 72.7 flin ( 350) = 16.15 flin ( 450) = 6.9

flspline ( 150) = 73.947 flspline ( 350) = 15.3 flspline ( 450) = 5.721

fpspline ( 150) = 73.207 fpspline ( 350) = 15.425 fpspline ( 450) = 5.069


166 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

fcspline ( 150) = 72.343 fcspline ( 350) = 15.48 fcspline ( 450) = 4.645

In the following paragraphs, Mathcad splines for the problem of Example 5.5 are generated
and compared with the Lagrange interpolating polynomial obtained earlier. The data given is

x1 := 96 x2 := 207 x3 := 375 x4 := 450

y1 := 932 y2 := 850 y3 := 767 y4 := 1235

( x − x2) ⋅ ( x − x3) ⋅ ( x − x4) ( x − x1) ⋅ ( x − x3) ⋅ ( x − x4)


w1 ( x ) := w2 ( x ) :=
( x1 − x2) ⋅ ( x1 − x3) ⋅ ( x1 − x4) ( x2 − x1) ⋅ ( x2 − x3) ⋅ ( x2 − x4)

( x − x1) ⋅ ( x − x2) ⋅ ( x − x4) ( x − x1) ⋅ ( x − x2) ⋅ ( x − x3)


w3 ( x ) := w4 ( x ) :=
( x3 − x1) ⋅ ( x3 − x2) ⋅ ( x3 − x4) ( x4 − x1) ⋅ ( x4 − x2) ⋅ ( x4 − x3)

f ( x ) := w1 ( x ) ⋅ y1 + w2 ( x ) ⋅ y2 + w3 ( x ) ⋅ y3 + w4 ( x ) ⋅ y4<-- Lagrange interpolating polynomial

f ( 102) = 940.053 f ( 322) = 683.419 f ( 415 ) = 955.728

The Mathcad vx and vy vectors are

⎛ x1 ⎞ ⎛ y1 ⎞
⎜ ⎟ ⎜ ⎟
vx :=
⎜ x2 ⎟ vy :=
⎜ y2 ⎟
⎜ x3 ⎟ ⎜ y3 ⎟
⎜ ⎟ ⎜ ⎟
⎝ x4 ⎠ ⎝ y4 ⎠

vs1 := lspline ( vx , vy) vs2 := pspline ( vx , vy) vs3 := cspline ( vx , vy)

flspline ( x ) := interp ( vs1 , vx , vy , x ) fpspline ( x ) := interp ( vs2 , vx , vy , x )


<-- Mathcad
splines

fcspline ( x ) := interp ( vs3 , vx , vy , x ) flin ( x ) := linterp ( vx , vy , x )

A comparison of the Mathcad spline functions generated with linterp, lspline, pspline and cspline
functions is shown in Figure 5.11.
Chapter 5: Numerical Interpolation 167

x := 96 , 97 .. 450

Lagrange Poly/ Mathcad Interpolations compared


1300
Given data points
interp/lspline
1200 interp/pspline
interp/cspline
vy linterp
1100
Lagrangian interpolation
flspline( x)

fpspline( x) 1000
y- Values

fcspline ( x)

flin( x) 900

f( x)
800

700

600
100 150 200 250 300 350 400 450
vx , x
x- Values

Figure 5.11. Comparison of Mathcad splines with Lagrangian interpolation

The various interpolations resorted to give the following interpolated function values f or x = 102,
322 and 415.

x=102: f ( 102) = 940.053 flin ( 102) = 927.568 flspline ( 102) = 930.335

fpspline ( 102) = 932.981 fcspline ( 102) = 940.053

x=322: f ( 322) = 683.419 flin ( 322) = 793.185 flspline ( 322) = 669.542

fpspline ( 322) = 684.098 fcspline ( 322) = 683.419

3
x=415: f ( 415 ) = 955.728 flin ( 415 ) = 1.017 × 10 flspline ( 415 ) = 985.191
168 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

fpspline ( 415 ) = 962.582 fcspline ( 415 ) = 955.728

5.9. APPLICATIONS IN NUMERICAL INTERPOLATION

In the examples that follow, data from various practical areas such as design of
machinery, noise control and vibration analysis, is utilized in interpolation procedure s to
generate function values at points other than the given data points.

5.9.1. Stress-Strain Data for Titanium

The stress versus strain data given in the table below is obtained from a tensile test of
annealed A-40 titanium [ 18 ]. A cubic spline can be developed using Mathcad as follows
and the stresses interpolated for any values of strain in the range provided.

The cubic spline interpolation generated together with the given data points is presented in
Figure 5.12. Notice that the spline generated passes through all the given data points as is
expected of an interpolating polynomial.

Strain (in/in) Stress (kpsi)


⎛ 0 ⎞
⎛ 0 ⎞ ⎜ ⎟
⎜ ⎟ ⎜ 5 ⎟
⎜ 0.00030 ⎟ ⎜ 10 ⎟
⎜ 0.00060 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 15 ⎟
⎜ 0.00090 ⎟ ⎜ 20 ⎟
⎜ 0.00120 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 25 ⎟
⎜ 0.00175 ⎟ ⎜ 30 ⎟
⎜ 0.00220 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 34.9 ⎟
⎜ 0.00285 ⎟ <--Given stress versus strain data
⎜ 39.9 ⎟
⎜ 0.00349 ⎟ σ := ⎜
ε := ⎜

⎟ ⎜ 44.9 ⎟
⎜ 0.00469 ⎟ ⎜ 49.9 ⎟
⎜ 0.00698 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 60.9 ⎟
⎜ 0.01610 ⎟ ⎜ 73.3 ⎟
⎜ 0.04807 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 78.2 ⎟ Vε := ε Vσ := σ
⎜ 0.07676 ⎟ ⎜ 86.2 ⎟
⎜ 0.14364 ⎟ ⎜ ⎟
⎜ ⎟ Vs := cspline ( V ε , Vσ)
⎜ 94.5 ⎟
⎜ 0.26677 ⎟ ⎜ 98.7 ⎟
⎜ 0.34470 ⎟ ⎜ ⎟
⎜ ⎟ ⎝ 118.2 ⎠
⎝ 0.72202 ⎠
Chapter 5: Numerical Interpolation 169

fspline ( ε ) := interp ( Vs , Vε , Vσ , ε ) <-- Generates a cubic spline function for given data

fspline ( 0.004 ) = 42.699


<-- interpolated values of stress at strains of 0.004 and 0.65
fspline ( 0.65 ) = 114.503

i := 1 .. 18 ε := 0 , 0.00010 .. 0.722

1 .10
3

100
fspline( ε )

Vσi
10

1
1 .10 1 .10
4 3
0.01 0.1 1
ε , Vεi

Figure 5.12. Mathcad cubic spline for given stress-strain data

5.9.2. Notch Sensitivity of Aluminum

The fatigue stress concentration factor K f is used with the nominal stress σ 0 to compute
the maximum resulting stress σ max at a discontinuity in a machine part via the following
relationship [18] .

σ max = Kf ⋅ σ 0 (5.31)

The fatigue stress concentration factor K f , which is, in fact , just a reduced value of the
geometrical stress concentration factor K t , is a function of the sensitivity of the material of
which the part is made to notches ( known as notch sensitivity q ). These quantities are
connected by the following relationship .
170 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

(
Kf = 1 + q ⋅ Kt − 1 ) (5.32)

Notch sensitivity charts which are graphs of notch sensitivity versus notch radius are
available for metals and alloys subjected to reversed bending or reversed axial loads. The
data given below provides notch sensitivity (q) versus notch radius (r) information for an
aluminum alloy and can be used to generate an interpolation from which notch sensitivity
values at points other than the given data points can be readily obt ained.

r ( in) 0.015 0.04 0.06 0.10 0.12 0.14 0.16

q 0.25 0.55 0.65 0.75 0.775 0.80 0.825

Since seven data points are given , the interpolating polynomial that can be generated will
be of order 6 , having the form

2 3 4 5 6
f ( r) = a + b ⋅ r + c ⋅ r + d ⋅ r + e ⋅ r + f ⋅ r + g ⋅ r

Resorting to the method of undetermined coefficients results in the following matrix equation

M ⋅C = A

where

⎛⎜ 1 2 3 4 5 6⎞
0.015 0.015 0.015 0.015 0.015 0.015 ⎟
⎜ 6 ⎟⎛a ⎞ ⎛⎜ 0.25 ⎞⎟
0.04 ⎟ ⎜ ⎟
2 3 4 5
⎜1 0.04 0.04 0.04 0.04 0.04
⎜b ⎟ ⎜ 0.55 ⎟
⎜ 6 ⎟
0.06 ⎟ ⎜ c ⎟ ⎜ 0.65 ⎟
2 3 4 5
⎜1 0.06 0.06 0.06 0.06 0.06
⎜ ⎟ ⎜ ⎟
M := ⎜ 1 2 3 4 5 6 ⎟
0.10C =⎟ ⎜ d ⎟ A := ⎜ 0.75 ⎟
⎜ 0.10 0.10 0.10 0.10 0.10
⎜ ⎟ ⎜ 0.775 ⎟
⎜ 2 3 4 5 6 ⎟⎜e ⎟ ⎜ ⎟
0.12 0.12 0.12 0.12 0.12
⎜1 0.12
⎟⎜ f ⎟ ⎜ 0.80 ⎟
⎜ 2 3 4 5 6 ⎟
⎜ ⎟ ⎜ 0.825 ⎟
0.14 0.14 0.14 0.14 0.14
⎜1 0.14
⎟⎝g ⎠ ⎝ ⎠
⎜1 2 3 4 5 6 ⎟
⎝ 0.16 0.16 0.16 0.16 0.16 0.16 ⎠

The coefficients in the column vector {C} can be obtained using the Mathcad function lsolve

C := lsolve ( M , A)
Chapter 5: Numerical Interpolation 171

⎛ −0.187 ⎞
⎜ ⎟
⎜ 40.462 ⎟
⎜ −922.431 ⎟
⎜ ⎟
4
C=
⎜ 1.27 × 10 ⎟
a := C1 b := C2 c := C3 d := C4 e := C5
⎜ 5

⎜ −1.01 × 10 ⎟ f := C6 g := C7
⎜ 5 ⎟
⎜ 4.228 × 10 ⎟
⎜ 5⎟
⎝ −7.169 × 10 ⎠

Thus, the interpolating polynomial f(x) is completely determined with

4 5
a = −0.187 b = 40.462 c = −922.431 d = 1.27 × 10 e = −1.01 × 10

5 5
f = 4.228 × 10 g = −7.169 × 10

2 3 4 5 6
f ( r) := a + b ⋅ r + c ⋅ r + d ⋅ r + e ⋅ r + f ⋅ r + g ⋅ r <---- interpolating polynomial obtained by
the method of undetermined
coefficients

Mathcad cubic spline interpolation can also be employed as shown below and the resulting
plot compared with the results of the undetermined coefficients method and with the given
data points. This is done in in Figure 5.13.

⎛⎜ 0.015 ⎞⎟ ⎛⎜ 0.25 ⎞⎟
⎜ 0.04 ⎟ ⎜ 0.55 ⎟
⎜ 0.06 ⎟ ⎜ 0.65 ⎟
⎜ ⎟ ⎜ ⎟
Vr := ⎜ 0.10 ⎟ Vq := ⎜ 0.75 ⎟
⎜ 0.12 ⎟ ⎜ 0.775 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.14 ⎟ ⎜ 0.80 ⎟
⎜ 0.16 ⎟ ⎜ 0.825 ⎟
⎝ ⎠ ⎝ ⎠

Vs := cspline ( Vr , Vq) fspline ( r) := interp ( Vs , Vr , Vq , r)


172 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

fspline ( 0.13) = 0.787 fspline ( 0.15 ) = 0.813 f ( 0.15 ) = 0.814 fspline ( 0.15 ) = 0.813

r := 0.015 , 0.016 .. 0.16 i := 1 .. 7

Mathcad interpolation/ notch sensitivity

0.8
Notch sensitivity, q

Vq i

f( r) 0.6
fspline ( r)

0.4 Given data points


Interpolation by undet coeff method
Mathcad cubic spline interpolation
0.2
0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16
Vr i , r

Notch radius, r, inches

Figure 5.13. Interpolation of notch sensitivity data

5.9.3 Speech Interference Level

In the field of Noise and Vibration Control, it often becomes necessary to determine the
effect of steady background noise on speech communication in a given setting. The
preferred speech interference level (PSIL) was established in an effort to study this effect
under the constraint that speech sounds would not be allowed to be reflected back to the
listener [ 2, 10 ]. For effective communication at a given voice level , the maximum
distance that there can be between the speaker and the listener is a function of the
preferred speech interference level existing at the location. The following data which is
provided for communication at the level of a normal male voice can be utilized in an
interpolation scheme to determine the maximum PSIL permitted for a given distance
between the speaker and the listener.

Dist (ft) 0 2 4 6 8 10 12

PSIL(dB) 83 62.5 56 52.5 50 47.5 46.5


Chapter 5: Numerical Interpolation 173

Because there are 7 data points prescribed, the interpolating polynomial to be generated will
be of order 6 and if a Gregory-Newton interpolating polynomial is to be determined , it will
have the form

(
f ( x ) = a1 + a2 ⋅ x − x 1 ) + a3 ⋅ ( x − x 1 ) ⋅ ( x − x 2 ) + a4 ⋅ ( x − x 1 ) ⋅ ( x − x 2 ) ⋅ ( x − x 3 ) ...
(
+ a5 ⋅ x − x 1 ) ⋅ (x − x2) ⋅ (x − x3) ⋅ (x − x4) ...
(
+ a6 ⋅ x − x 1 ) ⋅ (x − x2) ⋅ (x − x3) ⋅ (x − x4) ⋅ (x − x5) ...
(
+ a7 ⋅ x − x 1 ) ⋅ ( x − x2) ⋅ (x − x3) ⋅ ( x − x4) ⋅ (x − x5) ⋅ ( x − x6)
where the a' s are unknown coefficients to be determined.

The given data is

x 1 := 0 x 2 := 2 x 3 := 4 x 4 := 6 x 5 := 8 x 6 := 10 x 7 := 12

y 1 := 83 y 2 := 62.5 y 3 := 56. y 4 := 52.5 y 5 := 50 y 6 := 47.5 y 7 := 46.5

and the unknown coefficients can be determined using

a1 := y 1 y2 − y1 (
y 3 − a1 − a2 ⋅ x 3 − x 1 )
a2 := a3 :=
x2 − x1 ( x3 − x1) ⋅ (x3 − x2)

( )
( )(
y 4 − a1 − a2 ⋅ x 4 − x 1 − a3 ⋅ x 4 − x 1 ⋅ x 4 − x 2 )
a4 :=
(x4 − x1) ⋅ ( x4 − x2) ⋅ (x4 − x3)

( ) ( )( ) ( )(
y 5 − a1 − a2 ⋅ x 5 − x 1 − a3 ⋅ x 5 − x 1 ⋅ x 5 − x 2 − a4 ⋅ x 5 − x 1 ⋅ x 5 − x 2 ⋅ x 5 − x 3 )( )
a5 :=
(x5 − x1) ⋅ ( x5 − x2) ⋅ (x5 − x3) ⋅ ( x5 − x4)

( )( )(
Term4 := a4 ⋅ ⎡⎣ x 6 − x 1 ⋅ x 6 − x 2 ⋅ x 6 − x 3 ⎤⎦ )

( ) ( )(
y 6 − a1 − a2 ⋅ x 6 − x 1 − a3 ⋅ x 6 − x 1 ⋅ x 6 − x 2 − Term4 ... )
( )( )( )(
+ ( −1) ⋅ a5 ⋅ x 6 − x 1 ⋅ x 6 − x 2 ⋅ x 6 − x 3 ⋅ x 6 − x 4 )
a6 :=
( x6 − x1) ⋅ (x6 − x2) ⋅ ( x6 − x3) ⋅ (x6 − x4) ⋅ ( x6 − x5)
174 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( ) (
Termno4 := a4 ⋅ x 7 − x 1 ⋅ ⎡⎣ x 7 − x 2 ⋅ x 7 − x 3 ⎤⎦ )( )

( ) ( )(
y 6 − a1 − a2 ⋅ x 7 − x 1 − a3 ⋅ x 7 − x 1 ⋅ x 7 − x 2 − Termno4 ... )
( )( )( )(
+ ( −1) ⋅ a5 ⋅ ⎡⎣ x 7 − x 1 ⋅ x 7 − x 2 ⋅ x 7 − x 3 ⋅ x 7 − x 4 ⎤⎦ ... )
( )( )( )( )(
+ a6 ⋅ ⎡⎣ x 7 − x 1 ⋅ x 7 − x 2 ⋅ x 7 − x 3 ⋅ x 7 − x 4 ⋅ x 7 − x 5 ⋅ ( −1)⎤⎦ )
a7 :=
(x7 − x1) ⋅ ( x7 − x2) ⋅ (x7 − x3) ⋅ ( x7 − x4) ⋅ (x7 − x5) ⋅ ( x7 − x6)

which give

a1 = 83 a2 = −10.25 a3 = 1.75 a4 = −0.229

−3 −4
a5 = 0.023 a6 = −2.083 × 10 a7 = 2.279 × 10

The interpolating polynomial obtained by the Gregory-Newton method can now be obtained using

f ( x ) := a1 + a2 ⋅ x − ( ) + a3 ⋅ (x − x1) ⋅ (x − x2) + a4 ⋅ (x − x1) ⋅ (x − x2) ⋅ (x − x3) ...


x1
(
+ a5 ⋅ x − x 1 ) ( − x2) ⋅ (x − x3) ⋅ (x − x4) ...
⋅ x
(
+ a6 ⋅ x − x 1 ) ( − x2) ⋅ (x − x3) ⋅ (x − x4) ⋅ (x − x5) ...
⋅ x
(
+ a7 ⋅ x − x 1 ) ( − x2) ⋅ ( x − x3) ⋅ (x − x4) ⋅ ( x − x5) ⋅ (x − x6)
⋅ x

Mathcad cubic spline interpolation can also be employed as shown below and the resulting plot
compared with the results of the Gregory-Newton method and with the given data points. This is
done in in Figure 5.14.

⎛ x1 ⎞ ⎛ y1 ⎞
⎜ ⎟ ⎜ ⎟ ⎛⎜ 0 ⎞⎟ ⎛⎜ 83 ⎞⎟
⎜ x2 ⎟ ⎜ y2 ⎟ ⎜2 ⎟ ⎜ 62.5 ⎟
⎜ ⎟ ⎜ ⎟ ⎜4 ⎟ ⎜ 56 ⎟
⎜ x3 ⎟ ⎜ y3 ⎟ ⎜ ⎟ ⎜ ⎟
Vx := ⎜ x 4 ⎟ Vy := ⎜ y 4 ⎟ Vx = ⎜ 6 ⎟ Vy = ⎜ 52.5 ⎟
⎜ ⎟ ⎜ ⎟ ⎜8 ⎟ ⎜ 50 ⎟
⎜ x5 ⎟ ⎜ y5 ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ ⎜ ⎟ ⎜ 10 ⎟ ⎜ 47.5 ⎟
⎜ x6 ⎟ ⎜ y6 ⎟ ⎜ 12 ⎟ ⎜ 46.5 ⎟
⎜x ⎟ ⎜y ⎟ ⎝ ⎠ ⎝ ⎠
⎝ 7⎠ ⎝ 7⎠

Vs := cspline ( Vx , Vy) fspline ( x ) := interp ( Vs , Vx , Vy , x )

x := 0 , 1 .. 12 i := 1 , 2 .. 7
Chapter 5: Numerical Interpolation 175

Interpolation- speech communication


90
Given data points
Preferred speech interference level ( dB

Gregory-Newton interpolation
80 Mathcad's interp/cspline

Vy i
70
f( x)

fspline ( x) 60

50

40
0 2 4 6 8 10 12
Vxi , x

Distance from speaker to listener (ft)

Figure 5.14. Interpolation of speech communication data

5.9.4. Load-Deflection Data for Elastomeric Mounts

Elastomeric mounts are employed when small electrical and mechanical devices have to be
isolated from high forcing frequencies [ 9, 10 ]. They are especially useful in the protection of
delicate electronic instruments . Mount performance characteristics in the form of load
versus static deflection data are provided by manufacturers of vibration control products to
enable a designer to select an appropriate isolator for a given application.

The following data is available for an elastomeric mount.

Deflection (cm): 0 0 0.02 0.04 0.06 0.08 0.10

Load (N): 5 11 17.5 23.5 28 30.5

There are six data points prescribed. Thus, if a Lagrangian interpolating polynomial is to be
generated, it will be of order 5, having the form

6
L ( D) =
∑ (wi ( D) . ⋅ Li )
i = 1
where
176 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

∏ (D − Dj )
j = 1
wi ( D) = j≠i
6

∏ (Di − Dj )
j = 1

Here,

D1 := 0 D2 := 0.02 D3 := 0.04 D4 := 0.06 D5 := 0.08 D6 := 0.10

L1 := 5 L2 := 11. L3 := 17.5 L4 := 23.5 L5 := 28 L6 := 30.5

( D − D2) ⋅ ( D − D3 ) ⋅ ( D − D4) ⋅ ( D − D5 ) ⋅ ( D − D6 )
w1 ( D) :=
( D1 − D2) ⋅ ( D1 − D3 ) ⋅ ( D1 − D4) ⋅ ( D1 − D5 ) ⋅ ( D1 − D6 )

( D − D1) ⋅ ( D − D3 ) ⋅ ( D − D4) ⋅ ( D − D5 ) ⋅ ( D − D6 )
w2 ( D) :=
( D2 − D1) ⋅ ( D2 − D3 ) ⋅ ( D2 − D4) ⋅ ( D2 − D5 ) ⋅ ( D2 − D6 )

( D − D1) ⋅ ( D − D2 ) ⋅ ( D − D4) ⋅ ( D − D5 ) ⋅ ( D − D6 )
w3 ( D) :=
( D3 − D1) ⋅ ( D3 − D2 ) ⋅ ( D3 − D4) ⋅ ( D3 − D5 ) ⋅ ( D3 − D6 )

( D − D1) ⋅ ( D − D2 ) ⋅ ( D − D3) ⋅ ( D − D5 ) ⋅ ( D − D6 )
w4 ( D) :=
( D4 − D1) ⋅ ( D4 − D2 ) ⋅ ( D4 − D3) ⋅ ( D4 − D5 ) ⋅ ( D4 − D6 )

( D − D1 ) ⋅ ( D − D2) ⋅ ( D − D3 ) ⋅ ( D − D4) ⋅ ( D − D6 )
w5 ( D) :=
( D5 − D1) ⋅ ( D5 − D2) ⋅ ( D5 − D3) ⋅ ( D5 − D4) ⋅ ( D5 − D6)

( D − D1) ⋅ ( D − D2 ) ⋅ ( D − D3) ⋅ ( D − D4 ) ⋅ ( D − D5 )
w6 ( D) :=
( D6 − D1) ⋅ ( D6 − D2 ) ⋅ ( D6 − D3) ⋅ ( D6 − D4 ) ⋅ ( D6 − D5 )

The interpolating polynomial obtained by Lagrangian interpolation may now be obtained using

L ( D) := L1 ⋅ w1 ( D) + L2 ⋅ w2 ( D) + L3 ⋅ w3 ( D) + L4 ⋅ w4 ( D) + L5 ⋅ w5 ( D) + L6 ⋅ w6 ( D)

giving L ( 0.07 ) = 25.986


Chapter 5: Numerical Interpolation 177

Mathcad cubic spline interpolation can also be employed as shown below and the resulting
plot compared with the results of the Lagrangian interpolation and with the given data points.
This is done in in Figure 5.15

⎛ D1 ⎞ ⎛ L1 ⎞
⎜ ⎟ ⎜ ⎟
⎜ D2 ⎟ ⎜ L2 ⎟
⎜ D3 ⎟ ⎜ L3 ⎟
vD := ⎜ ⎟ vL := ⎜ ⎟
⎜ D4 ⎟ ⎜ L4 ⎟ vs := cspline ( vD , vL) fspline ( D) := interp ( vs , vD , vL , D)
⎜ D5 ⎟ ⎜ L5 ⎟
⎜ ⎟ ⎜ ⎟
⎝ D6 ⎠ ⎝ L6 ⎠

D := 0 , 0.01 .. 0.1 i := 1 , 2 .. 6

Interpolation-Load/deflect data
40

30
vL i
Load (N)

L( D) 20
fspline ( D )

10 Given data points


Lagrangian interpolation
Mathcad cubic spline interpolation
0
0 0.02 0.04 0.06 0.08 0.1
vDi , D

Static deflection (cm)


Figure 5.15. Interpolation of load-deflection data for elastomeric mount

PROBLEMS

In problems 5.1 through 5.15, draw graphs of the interpolating polynomial generated . Check
your results against functions obtained with Mathcad splines . Present these comparisons
as Mathcad plots with proper labels and titles.

5.1. Using the method of undetermined coefficients, derive an interpolating


polynomial f(x) of the form "a+ bx " , for the following data. Determine the interpolated
value of f (0.52).
178 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x 0.45 0.55 0.65 0.75

y 0.075 0.136 0.227 0.372

5.2. For the same data as in problem 1, use the method of undetermined
coefficients to derive an interpolating polynomial f(x) of form " a+bx + cx2 " . Calculate f
(0.63).

5.3. Using the method of undetermined coefficients, derive an interpolating polynomial


f(x) for the data given below.

x 0 20 40

y 892 853 725

Estimate f (37).

5.4. The following table gives the values of cos(x) for selected angles (x) in degrees

x 10 12 14 16

f(x)= cos(x) 0.0.98481 0.978150 .9703 0.96126

Using the method of undetermined coefficients, derive a cubic interpolating polynomial for
the given interval . Estimate the value of cos x for x = 12.75 deg and x = 13.5 deg. Compare
these results with the true values.

5.5. Given the following data set :

x 0.50 0.55 0.60 0.65

y 0.5823 0.6124 0.6435 0.7869

derive an interpolating polynomial f(x) using the Gregory-Newton method. Estimate f(0.535)
and f(0.625). Compare the accuracy of the estimated values if the true values are 0.6022
and 0.6978 respectively.

5.6. The deflection, δ, of a structure under loading is measured at a point for four
different values of the applied force,F. The measued data is given below.

F (kN) 0.5 1.5 2.0 2.5

δ (cm) 3.12 5.25 7.32 10.63

Using the Gregory-Newton method, compute the deflection for F = 1.75 kN.
Chapter 5: Numerical Interpolation 179

5.7. The amplitude of vibration of an automobile in the vertical direction, after passing over
a road bump is found to be as follows.

Time, t (seconds) 0 0.65 1.30 1.95

Amplitude, A (mm) 5.25 2.50 0.80 0.40

Using the Gregory-Newton method, compute the amplitude for t = 1.80 seconds.

5.8. The Charpy test [ 18 ] provides material toughness data under dynamic conditions.
In this test, which is helpful in comparing several materials and in determining
low-temperature brittleness and impact strength, the specimen is struck by a pendulum
released from a fixed height and the energy absorbed by the specimen, termed the impact
value , computed from the height of the swing after fracture. The following table gives the
impact value, V, as a function of temperature, T, for a certain material. Using the
Gregory-Newton method, derive an interpolating polynomial and estimate V for T = -25 deg.
F and - 75 deg. F

T (deg F) -200 -150 -100 -50 0

V (ft-lbs) 0 1.5 3 10 35

5.9. The data in the following table are the distances traveled by a motorcycle, which was
stationary at time t = 0, for selected times.

time, t (seconds) 0 2 4 6 8 10

Distance, D (ft) 0 10 50 150 330 610

Generate a finite-difference table and obtain an interpolating polynomial using Newton's


method. Estimate the distance D traveled at t = 7 seconds.

5.10. For the given data, generate both a finite-difference table and an interpolating
polynomial.

Angle, θ, (degrees) Cosine ( θ)

0 1.00000
10 0.98481
20 0.93969
30 0.86603
40 0.76604
50 0.64279
60 0.50000

Evaluate the error of the estimated value of cos (25 degrees). The true value is given to be
0.90631.
180 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

5.11. The following data gives the notch sensitivity , q , of a steel as a functionof the
notch radius , r [ 18 ]. Compute a finite-difference table and derive an interpolating
polynomial by Newton's method. Estimate q for r = 0.11 inches.

q 0.45 0.65 0.75 0.78 0.785

r ( in) 0.01 0.05 0.09 0.13 0.17

5.12. The deflection, δ, of a structure under loading is measured at a point for four different
values of the applied force, F. The measured data is as follows.

F (kn) 0.50 1.50 2.0 2.50

δ (cm) 3.0 5.2 7.3 10.5

Using Lagrangian interpolation , compute the deflection, δ, for F = 2.2 kN.

5.13. When a vibration problem is solved with nonlinearities included, the natural
frequencies of vibration become dependent on the amplitudes of vibration [ 9,16,20,21 ].
The following table relates the nonlinear natural frequency / linear natural frequency ratio,
r, of a rotating blade vibrating in the plane of rotation to its vibration amplitude , A.

A (in) 10 8.5 5 0

r (%) 99.9325 99.9575 99.9921 100

Derive a Lagrange interpolating polynomial and compute r for A = 4.25 in .

5.14. The behavior of a mooring line employed to control the excursions of a floating
ocean structure resembles that of a nonlinear spring with tension-displacement
characteristics which depend upon its length, weight, elastic properties, anchor holding
capacities and water depth [ 22 ]. In the table below, the horizontal component of mooring
line tension , H, is given as a function of the horizontal distance, X, between the ends of the
line.

X (ft) 440 470 485 492.5 495 498

H (Kips)0 5 10 42 150 440

Using Lagrangian interpolation, generate an interpolating polynomial and compute H for


X= 487 ft.
Chapter 5: Numerical Interpolation 181

5.15. The following table gives the pressure (P) versus temperature (T) relationship in the
liquid-vapor region for water [ 17 ]

T(deg Kelvin) 403.15 453.15 503.15 553.15 633.15

P (MPa) 0.2701 1.0021 2.795 6.412 18.651

Using Lagrangian interpolation, generate an interpolating polynomial and estimate P for T=


525 deg. K.

5.16. While the value of the endurance limit, S end , of a material is based on its
tensile strength, S ult , it is also dependent on the condition of its surface[19 ]. The following
data relates to a machined , unnotched specimen subjected to reversed bending.

S ult ( kpsi): 60 80 100 140 180 220

S end (kpsi): 22 30 38 50 60 64

Develop linear and cubic splines using linterp and interp for the above data. Also ,
graph the functions generated with proper labels and a title.
182 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

NOTES:
Chapter 6: Curve-Fitting 183

CHAPTER 6

CURVE-FITTING
6.1 THE NEED TO FIT A FUNCTION TO MEASURED DATA

Regression analysis plays an important role in analysis and interpretation of experimental as


well as statistical data and in their correlation with mathematical models. It involves the
determination of a function f(x) that would " best fit " a bunch of experimentally measured
values. ( x1, y1), (x2, y2) ....( xn,yn ). This function can be a linear function, a polynomial, a
nonlinear function, an exponential function , or a linear combination of known functions.
Some examples are shown in Figure 6.1. In curve- fitting a bunch of data points, typically
the number of data points would be much larger than the number of undetermined
coefficients in a given problem. Thus, there will be discrepancies between the function f(x)
determined and the data points given and it is very rare for a curve-fit to go through all the
given data points exactly. However, these differences are minimized by an adaptation of the
Method of Least Squares.

Figure 6.1. Examples of functions employed in curve-fitting


184 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Handling of Weighted Data

When dealing with engineering, scientific and statistical data, situations may be encountered when
certain data may be considered to be more accurate than other data and consequently must be
assigned more weight in the process of generating a curve-fit. One way to include this weighting is
to make multiple inclusions of the associated data point in the regression analysis. For example,
if the following data is given and the point (2, 20) is to be assigned a "weighting factor" of 3, this
data point must simply be considered thrice in coming up with a curve-fit as shown below.

x: 1 2 3 4
<--Given data
y: 10 20 30 40

x 1 2 2 2 3 4 <--- Information to be used


y: 10 20 20 20 30 40 for curve-fitting

6.2 THE METHOD OF LEAST SQUARES

With a well-chosen approximating function, a " least squares " fit will yield a good
representation of experimental data. Suppose you want to measure the distance between
two points in a field, and let us say that you do this " n " times. You will come up with " n "
measurements which are likely to be somewhat different from one another. Let these be d1 ,
d2 , d3 ,.....dn . If the true distance is "D", then the sum of the squares of the deviations from
the true distance "D" is

S = d1 − D( )2 + (d2 − D)2 + ................ + (dn − D)2 (6.1)

This sum " S " will be a maximum or a minimum when

d
S =0
dD

which yields
n

∑ (di) − n ⋅ D = 0 (6.2)
i = 1

∑ di
i = 1
or, D= (6.3)
n
Chapter 6: Curve-Fitting 185

2
d
For S to be a minimum, S > 0, which it is since it comes out to be 2n.
2
dD
Thus, S will be a mimimum when

∑ di
i
D= ,
n

that is, if " n " measurements are taken, the true distance will be the arithmetic mean of the
n measurements if the sum of the squares of the deviations is to be a minimum.

6.3 STRAIGHT LINE REGRESSION

Let us assume that a plot of the given data suggests that we should fit it with a linear function.
Let this function be

f ( x ) = C1 + C2 ⋅ x (6.4)

where C1 and C2 are coefficients to be determined. If the given data is

(x1 ,y1 ), ( x2 , y2 ).................(xn , yn ),

then the quantity "S", which is the sum of the squares of the deviations of the measured
values (y) from the function values f(x) is

( )2 ( )2
S = ⎡⎣y 1 − C1 + C2 ⋅ x 1 ⎤⎦ + ⎡⎣y 2 − C1 + C2 ⋅ x 2 ⎤⎦ + .......... + ⎡⎣y n − C1 + C2 ⋅ x n ⎤⎦ ( )2 (6.5)

For S to be a minimum, the partial derivatives of S with respect to C1 and C2 must be zeros, which
leads to

n n
n ⋅ C1 +
∑ x i ⋅ C2 =
∑ yi
i = 1 i = 1
(6.6)

n n n

∑ x i ⋅ C1 +
∑ ( ) 2
x i ⋅ C2 =
∑ ( x i ⋅ yi)
i = 1 i = 1 i = 1
186 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

In matrix form, this is

⎡ n ⎤ ⎡ n ⎤
⎢ n
⎢ ∑ xi ⎥
⎥ C

⎢ ∑ yi ⎥

⎢ i = 1 ⎥ ⋅ ⎛⎜ 1 ⎞⎟ = ⎢ i = 1 ⎥ (6.7)
⎢ n n ⎥ ⎝ C2 ⎠ ⎢ n ⎥
⎢ 2⎥
( xi) ⎥ ⎢ (xi ⋅ yi) ⎥⎥
⎢ ∑ xi
∑ ⎢∑
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦

Using Cramer's rule or the inverse of the matrix on the right hand side of the above equation or
the Mathcad function lsolve , the undetermined coefficients C1 and C2 can now be found.

Example 6.1

Given the following data:

xi yi

0.10 65.85
0.15 65.20
0.20 55.51
0.25 50.43
0.30 45.97
0.35 33.25
0.40 34.33
0.45 29.76
0.50 23.89
0.55 23.76
0.60 18.99

Fit a straight line y= f(x)= C1 + C2 x to the above data.

In this case, C1 and C2 are determined using

⎡ n ⎤ ⎡ n ⎤
⎢ n
⎢ ∑ xi ⎥
⎥ C

⎢ ∑ yi ⎥

⎢ i = 1 ⎥ ⋅ ⎛⎜ 1 ⎞⎟ = ⎢ i = 1 ⎥
⎢ n n ⎥ ⎝ C2 ⎠ ⎢ n ⎥
⎢ 2⎥
( xi) ⎥ ⎢ (xi ⋅ yi) ⎥⎥
⎢ ∑ xi
∑ ⎢∑
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦
Chapter 6: Curve-Fitting 187

Put in the given data as : j := 1 , 2 .. 10 x 1 := 0.10 x j+ 1 := x j + 0.05 n := 11

⎛ 65.85 ⎞
⎜ ⎟
⎜ 65.20 ⎟
⎜ 55.51 ⎟
⎜ 50.43 ⎟
⎜ ⎟
⎜ 45.97 ⎟
with y := ⎜ 33.25 ⎟
⎜ ⎟
⎜ 34.33 ⎟
⎜ 29.76 ⎟
⎜ ⎟
⎜ 23.89 ⎟
⎜ 23.76 ⎟
⎜ ⎟
⎝ 18.99 ⎠

Defining:

⎡ n ⎤ ⎡ n n ⎤ ⎡ n ⎤
⎢ n
⎢ ∑ xi ⎥


⎢ ∑ yi
∑ xi ⎥

⎢ n
⎢ ∑ yi ⎥

⎢ i = 1 ⎥ ⎢ i=1 i = 1 ⎥ ⎢ i = 1 ⎥
d := m := q :=
⎢ n n ⎥ ⎢ n n ⎥ ⎢ n n ⎥
⎢ 2⎥
( xi) ⎥ ⎢ ( x i ⋅ yi) 2⎥
( x i) ⎥ ⎢ (xi ⋅ yi) ⎥⎥
⎢ ∑ xi
∑ ⎢ ∑ ∑ ⎢∑ xi

⎣i = 1 i =1 ⎦ ⎣i = 1 i =1 ⎦ ⎣i = 1 i =1 ⎦

m q
C1 := C1 = 75.497 C2 := C2 = −99.618
d d

the fitted straight line function is then

f ( x ) := C1 + C2 ⋅ x

which is compared with the given data in Figure 6.2. At x= 0.20, 0.42 and 0.52, the fitted
.
function yields

f ( 0.20) = 55.574 f ( 0.42) = 33.658 f ( 0.52) = 23.696


188 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

i := 1 .. 11

Comparison- Fitted Function with Data


100
y and f(x) values

yi
50
f( x)
Given data
Fitted function
0
0.1 0.2 0.3 0.4 0.5 0.6
xi , x

x-values

Figure 6.2. Curve-fit with a linear function

6.4 CURVE- FITTING WITH A QUADRATIC FUNCTION

For some types of data, it may be appropriate to resort to a quadratic function of the
form:

f(x)= C1 + C2 x2 (6.8)

In this case, then , S will be

2 2 2
S = ⎡⎣y 1 − ⎡⎣C1 + C2 ⋅ x 1 ( )2⎤⎦⎤⎦ + ⎡⎣y 2 − ⎡⎣C1 + C2 ⋅ x 2
( )2⎤⎦⎤⎦ + .................. + ⎡⎣y n − ⎡⎣C1 + C2 ⋅ x n
( )2⎤⎦⎤⎦ (6.9)

Taking the partial derivatives of S with respect to C1 and C2 yields

⎡ n ⎤ ⎡ n ⎤
⎢ n
∑ ( xi) 2 ⎥ ⎢
∑ yi ⎥
⎢ ⎥ C ⎢ ⎥
⎢ i = 1 ⎥ ⋅ ⎛⎜ 1 ⎞⎟ = ⎢ i = 1 ⎥ (6.10)
⎢ n n ⎥ ⎝ C2 ⎠ ⎢ n ⎥
⎢ ( x i)
4⎥
( i) ⎥ ⎢ ⎡ 2 ⎤⎥
∑ ∑ ∑ ⎣( i) i⎦ ⎥
2
x x ⋅ y
⎢ ⎢
⎣i = 1 i = 1 ⎦ ⎣i = 1 ⎦
Chapter 6: Curve-Fitting 189

which can be solved for the C's using algebraic or matrix methods or by accessing the
Mathcad function lsolve. The above can be expressed as

[M]{C} = {A}, (6.11)

where

⎡ n ⎤ ⎡ n ⎤
⎢ n
∑ ( x i) 2⎥ ⎢
∑ yi ⎥
⎢ ⎥ ⎢ ⎥
⎢ i = 1 ⎥ ⎢ i = 1 ⎥ (6.12)
M= , A=
⎢ n n ⎥ ⎢ n ⎥
⎢ ( xi) 2 4⎥
( x i) ⎥ ⎢ ⎡( x i) 2 ⋅ y i⎤ ⎥
⎢ ∑ ∑ ⎢ ∑ ⎣ ⎦⎥
⎣i = 1 i = 1 ⎦ ⎣i = 1 ⎦

Example 6.2

A data set is given below .

x y

0.5 0.51
1.0 2.35
1.5 7.54
2.0 13.23
2.5 17.65
3.0 24.21
3.5 28.94
4.0 37.63
4.5 58.32
5.0 63.21

Determine the quadratic function that will best fit the data given

Put in the given data :

i := 1 , 2 .. 10 x 1 := 0.5 x i+1 := x i + 0.5 n := 10

y 1 := 0.51 y 2 := 2.35 y 3 := 7.54 y 4 := 13.23 y 5 := 17.65

y 6 := 24.21 y 7 := 28.94 y 8 := 37.63 y 9 := 58.32 y 10 := 63.21

In this problem, the [M} and {A} matrices are


190 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎡ n ⎤ ⎡ n ⎤
⎢ n
∑ ( )
xi
2⎥ ⎢
∑ yi ⎥
⎢ ⎥ ⎢ ⎥
⎢ i = 1 ⎥ ⎢ i = 1 ⎥
M := A :=
⎢ n n ⎥ ⎢ n ⎥
⎢ ( x i)
4⎥
( xi) ⎥ ⎢ ⎡( xi) 2 ⋅ yi⎤ ⎥
∑ ∑ ∑
2
⎢ ⎢ ⎣ ⎦⎥
⎣i = 1 i = 1 ⎦ ⎣i = 1 ⎦

Use lsolve to compute the C's:

C := lsolve ( M , A) ⎛ 0.779 ⎞
C= ⎜ ⎟ C1 = 0.779 C2 = 2.554
⎝ 2.554 ⎠

The quadratic function fitted to the given data is f(x),

2
f ( x ) := C1 + C2 ⋅ x

which is now plotted and compared with the given data in Figure 6.3

x := 0.5 , 0.51 .. 5.0 i := 1 , 2 .. 10 x 1 := 0.5 x i+1 := x i + 0.5

xi = yi = ( )
f xi =
0.5 0.51 1.417
1 2.35 3.333
1.5 7.54 6.525
2 13.23 10.994
2.5 17.65 16.74
3 24.21 23.763
<--- Given data compared with generated function
3.5 28.94 32.063
4 37.63 41.639
4.5 58.32 52.493
5 63.21 64.623
Chapter 6: Curve-Fitting 191

Comparison-Fitted Function with Data


100
Curve-fitting with quadratic function
Data (y) and f(x) Values

Given Data Points

( )
f xi
50
yi

0
0 1 2 3 4 5
xi

x-Values

Figure 6.3. Curve-fit with quadratic function

6.5 CURVE-FIT WITH A POWER FUNCTION

For some data types, it may be appropriate to do a curve-fit with a function of the form

f(x)= A x B (6.13)

where A and B are undetermined coefficients to be evaluated on a least squares basis.

Taking the logarithm of the above, we obtain

ln (f) = ln (A) +B ln (x) (6.14)

or, F=C+BX (6.15)

where F= ln f , C = ln (A) and X = ln (x) (6.16)

Thus, now the problem has been reduced to one of linear regression and can be handled as done
earlier for a linear fit.
192 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.3

For the data set given below, find a power function that will serve as a least squares fit.

x y

0.18 4.5921
0.32 5.2345
0.65 6.2321
1.12 6.5467
1.65 7.2976
2.34 7.7654
2.52 7.7237
2.85 8.2543
3.12 7.9765
3.61 8.4328
3.92 9.6322
4.51 8.8764
4.72 9.0276
5.25 9.2568
6.75 8.6432
7.85 10.2137

Using f(x)= y(x) = A x B , F=Y= C+B X, where F=Y = ln (y) , C = ln (A)


and X= ln (x), we have the following matrix equation to solve for C and B

⎡ n ⎤ ⎡ n ⎤
⎢ n
∑ Xi ⎥ ⎢
∑ ( i) ⎥
Y
⎢ ⎥ ⎢ ⎥
⎢ i = 1 ⎥ ⋅ ⎛⎜ C ⎞⎟ = ⎢ i = 1 ⎥
⎢ n n ⎥ ⎝B ⎠ ⎢ n ⎥ (n=16) (6.17)
⎢ 2⎥
( Xi) ⎥ ⎢ ⎡⎣( Yi) ⋅ Xi⎤⎦ ⎥
⎢ ∑ Xi
∑ ⎢∑ ⎥
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦

Put in the given data as shown below and calculate the Y's and X's using

( )
Yi := ln y i ( )
Xi := ln x i

n := 16 i := 1 , 2 .. 16
Chapter 6: Curve-Fitting 193

⎛ 0.18 ⎞ ⎛ 4.5921 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.32 ⎟ ⎜ 5.2345 ⎟
⎜ 0.65 ⎟ ⎜ 6.2321 ⎟
⎜ 1.12 ⎟ ⎜ 6.5467 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 1.65 ⎟ ⎜ 7.2976 ⎟
⎜ 2.34 ⎟ ⎜ 7.7654 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 2.52 ⎟ ⎜ 7.7237 ⎟
⎜ 2.85 ⎟ ⎜ 8.2543 ⎟
x := ⎜ ⎟ y := ⎜ ⎟
⎜ 3.12 ⎟ ⎜ 7.9765 ⎟
⎜ 3.61 ⎟ ⎜ 8.4328 ⎟
⎜ 3.92 ⎟ ⎜ 9.6322 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 4.51 ⎟ ⎜ 8.8764 ⎟
⎜ 4.72 ⎟ ⎜ 9.0276 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 5.25 ⎟ ⎜ 9.2568 ⎟
⎜ 6.75 ⎟ ⎜ 8.6432 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 7.85 ⎠ ⎝ 10.2137 ⎠

( )
Yi := ln y i ( )
Xi := ln x i

Equation 6.17 is: [D] { K} = {P}, where

⎡ n ⎤
⎡ ⎤
( Yi) ⎥
n
⎢ n
⎢ ∑ Xi ⎥


⎢ ∑ ⎥
⎢ i = 1 ⎥
⎢ i = 1 ⎥ P :=
D := ⎢ n ⎥
⎢ n ⎥ ⎢ ⎡⎣( Yi) ⋅ Xi⎤⎦ ⎥
n

⎢ ∑ Xi

2⎥
( Xi) ⎥ ⎢ ∑ ⎥
⎣i = 1 ⎦
⎣i = 1 i =1 ⎦

and {K} is the vector of unknown coefficients C and B

The constants C and B can be obtained using lsolve as shown below, and then the coefficients
A and B of the power function can be calculated

⎛ 1.883 ⎞
K := lsolve ( D , P) K= ⎜ ⎟ <------ This means: C= 1.883 and B = 0.2
⎝ 0.2 ⎠
C := K1 B := K2 C = 1.883 B = 0.2
194 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

C
A := e A = 6.571 B = 0.2

The fit generated is plotted and compared with the given data in Figur e 6.4

B
f ( x ) := A ⋅ x

Least Squares-Power Function Fit


10
Function Values

( )
f xi

yi 5

Least Squares/Power Function Fit


Given Data Points
0
1 2 3 4 5 6 7 8
xi

x-Values

Figure 6.4. Curve-fit with a power function

6.6. CURVE-FITTING WITH AN EXPONENTIAL FUNCTION

Function of Form A e Bx

Exponential functions appear frequently in the analysis of business, science as well as


engineering problems and are often employed to curve-fit experimental and statistical data. The
function, in this case, has the form .

B⋅ x (6.18)
f ( x) = A ⋅ e

where A and B are undetermined coefficients to be evaluated on a least squares basis. Taking the
logarithm of the above gives

ln ( f ) = ln ( A) + B ⋅ x (6.19)

or, F = C + B ⋅x (6.20)

where
Chapter 6: Curve-Fitting 195

F = ln ( F) and C = ln ( A) (6.21)
Thus, now the problem is again reduced to one of linear regression and can be handled as done
earlier for a linear fit.

Example 6.4

For the data set given below, find an exponential function that will serve as a least squares fit.

x y

0 16.45
1 4.47
2 1.06
3 0.45
4 0.15
5 0.03

Using f(x)= y(x) = A e Bx , F=Y= C+B x , where F=Y = ln (y) , and C = ln (A)
we have the following matrix equation to solve for C and B

⎡ n ⎤ ⎡ n ⎤
⎢ n
∑ xi ⎥ ⎢
∑ ( Yi) ⎥
⎢ ⎥ ⎢ ⎥
⎢ i = 1 ⎥ ⋅ ⎛⎜ C ⎞⎟ = ⎢ i = 1 ⎥ (n=6) (6.22)
⎢ n n ⎥ ⎝B ⎠ ⎢ n ⎥
⎢ 2⎥
( xi) ⎥ ⎢ ⎥
⎢ ∑ xi
∑ ⎢ ∑ ⎣⎡( Yi) ⋅ x i⎤⎦ ⎥
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦

which can be solved for C and B.

Put in the given data and calculate the natural logs of the y-values

i := 1 , 2 .. 6

⎛⎜ 16.45 ⎞⎟
⎛⎜ 0 ⎞⎟
⎜ 4.47 ⎟
⎜1 ⎟ ⎜ 1.06 ⎟
⎜2 ⎟ y := ⎜
i := 1 , 2 .. 6 n := 6
x := ⎜ ⎟

⎜ 0.45 ⎟
⎜3 ⎟ ⎜ 0.15 ⎟
⎜4 ⎟ ⎜ 0.03 ⎟
⎜5 ⎟ ⎝ ⎠
⎝ ⎠

Yi := ln y i ( )
196 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The matrix in the left hand side of Equation (6.22) and the column vector in the right hand side are:

⎡ n ⎤
⎡ ⎤
( Yi) ⎥
n
⎢ n
⎢ ∑ xi ⎥


⎢ ⎥ ∑
⎢ i = 1 ⎥
⎢ i = 1 ⎥ P :=
D := ⎢ n ⎥
⎢ n ⎥ ⎢ ⎥
⎣⎡( Yi) ⋅ xi⎤⎦ ⎥
n

⎢ ∑ xi

2⎥
( xi) ⎥ ⎢ ∑
⎣i = 1 ⎦
⎣i = 1 i =1 ⎦

The unknown constants C and B can be computed using lsolve and the coefficient A can then be
calculated as shown .

⎛ 2.733 ⎞
K := lsolve ( D , P) K= ⎜ ⎟ <------That is, C= 2.733 and B = -1.216
⎝ −1.216 ⎠

C := K1 B := K2 C = 2.733 B = −1.216

C B⋅ x
A := e A = 15.384 f ( x ) := A ⋅ e

The curve-fit and its natural log are generated below and compared with the given data in Figures
6.5 (a) and 6.5(b)

i := 1 .. 6
A = 15.384 B = −1.216 ( )
fi := f x i logfunci := ln f i ()
B⋅ x Natural Log of Curve- Fit
f ( x ) := A ⋅ e 4
Data points
Natural Log of Function Values

Curve-fit
Analysis-Generated Fit
2
Given Data Points
Yi
f( z) 10
0
yi logfunci

2
0
0 2 4
4
z , xi 0 2 4
xi

Figure 6.5 (a) Curve-fit with an exponential function Figure 6.5 (b) Natural log of Curve-Fit
of form A e Bx
Chapter 6: Curve-Fitting 197

Function of Form A x e Bx

There are times when exponential functions of the following form appear in the analysis of
practical problems

f(x)= y(x) = A x e Bx (6.23)

where A and B are undetermined coefficients to be evaluated on a least squares basis.

Taking the logarithm of the above, we obtain

ln (y ) = ln (f) = ln(A) + ln (x) + Bx

or, ln ( y/x) = ln ( A) + Bx

Z=C+Bx (6.24)
where,

Z= ln (y/x) and C = ln (A) (6.25)

Thus, now the problem is reduced again to one of linear regression and can be handled as done
earlier for a linear fit.

Example 6.5

For the stress versus strain data given below, find an exponential function of form
S = A e exp ( B e ) that will serve as a least squares fit.

Strain (e) 300 800 1300 1800 2300 2800 (micro-in/in)

Stress (S) 1656 3521 4079 4354 4300 3675 (psi)

Using f(e)= y(e) = A e exp(Be), Z= C + B e , where Z = ln(S/e) and C= ln (A), we have


the following matrix equation to solve for C and B Here, the x-coordinate is "e" and the y -
coordinate is "S"

⎡ n ⎤ ⎡ n ⎤
⎢ n
⎢ ∑ ei ⎥


⎢ ∑ Zi ⎥

⎢ i = 1 ⎥ ⋅ ⎛⎜ C ⎞⎟ = ⎢ i = 1 ⎥ (6.26)
⎢ n n ⎥ ⎝B ⎠ ⎢ n ⎥
⎢ 2⎥
(ei) ⎥ ⎢ (Zi ⋅ ei) ⎥⎥
⎢∑ ei
∑ ⎢∑
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦
198 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Put in the given data and calculate the Z's : i := 1 , 2 .. 6

e1 := 300 e2 := 800 e3 := 1300 e4 := 1800 e5 := 2300 e6 := 2800

S1 := 1656 S2 := 3521 S3 := 4079 S4 := 4354 S5 := 4300 S6 := 3675

Here,

⎛ Si ⎞
n := 6 Zi := ln ⎜ ⎟
⎝ ei ⎠
Letting

⎡ n ⎤
⎡ ⎤
( Zi ) ⎥
n
⎢ n
⎢ ∑ ei ⎥


⎢ ∑ ⎥
⎢ i = 1 ⎥
⎢ i = 1 ⎥ P :=
D := ⎢ n ⎥
⎢ n ⎥ ⎢ ⎥
⎣⎡( Zi) ⋅ ei⎤⎦ ⎥
n

⎢ ∑ ei

2⎥
(ei) ⎥ ⎢ ∑
⎣i = 1 ⎦
⎣i = 1 i =1 ⎦

the constants C and B of Equation (6.26) can be computed as shown below

⎛ 1.906 ⎞
K := lsolve ( D , P) K= ⎜ −4⎟
<------That is, C= 1.906 and B = -5.7215x 10 (-4)
⎝ −5.721 × 10 ⎠

−4
C := K1 B := K2 C = 1.906 B = −5.721 × 10

A := exp ( C) A = 6.725

The exponential fit, then, is

f ( e) := A ⋅ e ⋅ exp ( B ⋅ e)

which is plotted and compared with the given data in Figure 6.6

e := 300 , 301 .. 2800 i := 1 , 2 .. 6


Chapter 6: Curve-Fitting 199

e1 := 300 e2 := 800 e3 := 1300 e4 := 1800 e5 := 2300 e6 := 2800


<--Given
Data
S1 := 1656 S2 := 3521 S3 := 4079 S4 := 4354 S5 := 4300 S6 := 3675 Points

Curve-Fit with Exponential Function


5000

4000
Stress ( S) Values

f( z)
3000
Si

2000
Analysis-Generated Fit
Given Data Points
1000
500 1000 1500 2000 2500
z , ei

Strain ( e) Values

Figure 6.6. Curve -fit with exponential function A x e Bx

6.7 CURVE-FITTING WITH A LINEAR COMBINATION OF KNOWN


FUNCTIONS

In fitting a function to given data points, a linear combination of known functions may
have to be used at times.

Given a set of data points: (x1 , y1 ), (x2 ,y2 ) ......... ( xn , yn ), It is required to find
a function:

f(x) = C1 f1 (x) + C2 f2 (x) + ......... C m fm(x) (6.27)

where the f i (x) are prescribed functions and the C's are undetermined coefficients to be
evaluated. The total number of data points given is "n" while the total number of
prescribed functions to be utilized is "m" .

In general, then, the function value at x i will be


200 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

m
( )
f xi =
∑ ⎡⎣C j ⋅ f j( xi)⎤⎦ (6.28)
j = 1

In this case, the sum of the squares of the deviations from the true values y i will be:

(
s = y1 − f x1( ))2 + (y2 − f (x2))2 + ...................... + (yn − f (xn))2 (6.29)

For S to be a minimum , the partial derivatives of S with respect to C1 , C 2 ....... C m


must be zero and the second partials must be positive. These requirements lead to the following
matrix equation in the unknowns, C 1 , C2 ...... C m

[M] {C} = {L} (6.30)

where

⎡ n ⎤
⎢ ( ) ( ) ⎥
⎢ ∑ ⎣⎡⎡⎣ 1 i ⎤⎦ i ⎤⎦ ⎥
f x ⋅ y

⎢i = 1 ⎥
⎛⎜ C1 ⎞⎟ ⎢ n ⎥
⎢ ⎥
⎜ C2 ⎟ ⎢ ∑ ⎡⎣⎡⎣f2( xi)⎤⎦ ⋅ ( yi)⎤⎦ ⎥
⎜ ⎟ ⎢i = 1 ⎥
C := ⎜ ⎟ , L := ⎢ ⎥ , and
⎜ ⎟ ⎢ ⎥
⎜ ⎟ ⎢ ⎥
⎜ ⎟ ⎢ ⎥
⎜ Cm ⎟ ⎢ ⎥
⎝ ⎠
⎢ n ⎥

∑ ⎡⎣⎡⎣ m( i)⎦⎤ ( i)⎤⎦ ⎥
f x ⋅ y
⎢ ⎥
⎣i = 1 ⎦
Chapter 6: Curve-Fitting 201

⎡ n n n ⎤

∑ ( ) ∑ ( ) ( ) ∑ ⎡⎣⎡⎣f1 x i ⎤⎦ ⋅ ⎡⎣fm x i ⎤⎦⎤⎦ ⎥
( ) ( )
2
⎡⎣f1 xi ⎤⎦ ⎡⎣⎡⎣f1 x i ⎤⎦ ⋅ ⎡⎣f2 xi ⎤⎦⎤⎦
⎢ ⎥
⎢ i =1 i = 1 i = 1 ⎥
⎢ n n n ⎥
⎢ ⎥
∑ ⎡⎣⎡⎣f2( xi)⎤⎦ ⋅ ⎡⎣f1( x i)⎤⎦⎤⎦ ∑ ⎡⎣f2( x i)⎤⎦ ∑ ⎡⎣⎡⎣f2( x i)⎤⎦ ⋅ ⎡⎣fm( x i)⎤⎦⎤⎦ ⎥
2

⎢i = 1 i = 1 i = 1 ⎥
M := ⎢ ⎥
⎢ ⎥
⎢ ⎥
⎢ ⎥
⎢ ⎥
⎢ n n n ⎥

∑ ⎡⎣⎡⎣fm( xi)⎤⎦ ⋅ ⎡⎣f1( x i)⎤⎦⎤⎦ ∑ ⎡⎣⎡⎣fm( x i)⎤⎦ ⋅ ⎡⎣f2( xi)⎤⎦⎤⎦ ∑ ⎡⎣fm( x i)⎤⎦ ⎥
2
⎢ ⎥
⎣i = 1 i = 1 i =1 ⎦

The above equations can now be solved for the C 's.

Example 6.6.

Fit the data points:

x: 1.0 2.0 3.0 4.0 5.0

y: 5.75 10.75 12.65 29.95 49.35

3
with a function of the form y ( x ) = C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ), where f1 ( x ) = 1, and f2 ( x ) = x

In this case, n= 5 and m = 2. Equation (6.30), then, gives the following matrix equation
in C1 and C2

⎡ 5 5 ⎤ ⎡ 5 ⎤

∑ ⎡⎣f1( xi)⎤⎦ ∑ ⎡⎣⎡⎣f1( x i)⎤⎦ ⋅ ⎡⎣f2( xi)⎤⎦⎤⎦ ⎥

⎢ ⎡⎣⎡⎣f1( xi)⎤⎦ ⋅ y i⎤⎦ ⎥
2
⎢ ⎥ C ⎢ ⎥
⎢ i = 1 i = 1 ⎥ ⋅ ⎛⎜ 1 ⎞⎟ = ⎢i = 1 ⎥
⎢ 5 5 ⎥ ⎝ C2 ⎠ ⎢ 5 ⎥
⎢ ⎡⎣⎡⎣f1( x i)⎤⎦ ⋅ ⎡⎣f2( xi)⎤⎦⎤⎦ ⎡⎣f2( xi)⎤⎦ ⎥ ⎢ ⎥
∑ ∑ ∑ ⎣⎡⎣⎡f2( xi)⎦⎤ ⋅ y i⎦⎤ ⎥
2
⎢ ⎥ ⎢
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦

The given data is


202 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x 1 := 1 x 2 := 2.0 x 3 := 3.0 x 4 := 4.0 x 5 := 5.0

y 1 := 5.75 y 2 := 10.75 y 3 := 12.65 y 4 := 29.95 y 5 := 49.35

The subscipts 1 and 2 are literal


3 subscripts , and do not denote vector
i := 1 , 2 .. 5 f1 ( x ) := 1 f2 ( x ) := x <----
components. These are done by including a
period in the variable name . Whatever
follows the period then becomes the
subscript. For example , "vel init" is created
by typing a period after "vel " and then
typing "init"

Letting

⎡ 5 5 ⎤ ⎡ 5 ⎤
f1 ( xi) ( ( )) ( ) ( ( ))

∑ ∑ ⎡⎣ f1 x i ⋅ f2 xi ⎤⎦ ⎥ ⎢
∑ ⎡⎣ f1 xi ⋅ y i⎤⎦ ⎥
2
⎢ ⎥ ⎢ ⎥
M :=
⎢ i=1 i = 1 ⎥ L :=
⎢i = 1 ⎥
⎢ 5 5 ⎥ ⎢ 5 ⎥
⎢ ⎡⎣f2 ( x i) ( f1 ( x i) )⎤⎦ ( )2 ⎥ ⎢ ⎡⎣( f2 ( xi) ) ⋅ y i⎤⎦ ⎥
⎢ ∑ ∑ f2 xi
⎥ ⎢∑ ⎥
⎣i = 1 i = 1 ⎦ ⎣i = 1 ⎦

the constants C 1 and C2 can be computed as shown

C := lsolve ( M , L) ⎛ 5.931 ⎞ C1 := C1 C2 := C2
C= ⎜ ⎟
⎝ 0.35 ⎠

C1 = 5.931 C2 = 0.35

The fit yielded by this analysis is: y= f(x) = 5.931 + 0.350 x 3

which is plotted in Figure 6.7 and compared with the given data.

x 1 := 1 x 2 := 2.0 x 3 := 3.0 x 4 := 4.0 x 5 := 5.0


<--- Given data points

y 1 := 5.75 y 2 := 10.75 y 3 := 12.65 y 4 := 29.95 y 5 := 49.35


Chapter 6: Curve-Fitting 203

yanal ( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ) <--- Curve-fit generated by analysis

yanal ( 1 ) = 6.281 yanal ( 2 ) = 8.733

<---y-values generated by curve-fit


yanal ( 3 ) = 15.386

z := 1 , 1.1 .. 5 i := 1 .. 5

Analysis - Generated Fit/ Given Data


60
Results of Analysis
Given data points

40
Y- Values

yanal ( z)

yi
20

0
1 2 3 4 5
z , xi

X- Values

Figure 6.7. Curve-fit with a linear combination of known functions

6.8 CURVE-FITTING WITH POLYNOMIALS

If an mth order polynomial seems to be a good fit to the data points given, the procedure used
in fitting a linear combination of known functions can still be resorted to. In this case,

f 1 ( x) = 1, f 2 (x) = x , f 3 ( x ) = x 2, f 4 ( x ) = x 3 , ............. ,
(6.31)
f m+1 (x ) = x m

f(x) = C1 f 1 (x ) + C 2 f 2 ( x ) +....................... C m+1 f m+1 (x) (6.32)

in which the C 's must be solved for using

[M] {C} = {L} (6.33)


204 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

where ⎛ C1 ⎞
⎜ ⎟
⎜ C2 ⎟
C := ⎜ ⎟
⎜ ⎟
⎜ ⎟
⎜C ⎟
⎝ m+1 ⎠

⎡ n n ⎤
⎢ ( xi) m ⎥

n
∑ xi
∑ ⎥ ⎡ n ⎤

⎢ n
i = 1 i = 1 ⎥


⎢ ∑ yi ⎥

n n ⎢ i=1 ⎥
⎢ m+ 1 ⎥
⎢ ∑ xi
∑ ( xi) 2 ∑ ( xi) ⎥ ⎢ n ⎥
⎢ i=1 i = 1 i = 1 ⎥ ⎢
∑ ( x i ⋅ yi) ⎥
⎢ ⎥
⎢ n ⎥ L := ⎢ i = 1
M := n ⎥
⎢ m+ 2 ⎥ ,
⎢ ∑ ( xi) 2 ∑ ( xi) ⎥ ⎢ ⎥
⎢ ⎥
⎢i = 1 i = 1 ⎥ ⎢ ⎥
⎢ ⎥ ⎢ n ⎥
⎢ ⎥ ⎢ ⎡( xi) m ⋅ yi⎤ ⎥
⎢ n
( x i) m
n
(xi)2⋅ m ⎥

⎢ ∑ ⎣ ⎦⎥

⎢ ∑ ∑ ⎥ ⎣i = 1 ⎦
⎣i = 1 i = 1 ⎦

where the ( x i, y i ) are the "n" given data points.

Example 6.7.

Given the data points

x: 0 1 2 3 4 5

y: 0 8.47 17.48 19.57 14.69 11.23

2
Obtain a curve-fit of the form y ( x ) = f ( x ) = C1 + C2 ⋅ x + C3 ⋅ x

Here, n = 6 and m = 2 and the matrix equation to be solved is

[M]{C} = {L}
Chapter 6: Curve-Fitting 205

where [M] is a known 3x3 matrix and {L} is a known column vector as shown below. The

⎛ C1 ⎞
⎜ ⎟
The column vector {C} , which involves the undetermined coefficients, is ⎜ C2 ⎟
⎜C ⎟
⎝ 3⎠
The given data is

n := 6 x 1 := 0 x 2 := 1 x 3 := 2 x 4 := 3 x 5 := 4 x 6 := 5

y 1 := 0 y 2 := 8.47 y 3 := 17.48 y 4 := 19.57 y 5 := 14.69 y 6 := 11.23

The matrices M and L are

⎡ n ⎤
⎡ n n ⎤


n
∑ xi
∑ ( x i) 2 ⎥


⎢ ∑ yi ⎥

i = 1 i = 1 ⎢ i=1 ⎥
⎢ ⎥ ⎢ n ⎥
⎢ n ⎥ ⎢ ( i i) ⎥
n n
M :=

⎢ ∑ xi
∑ ( xi) 2
∑ ( x i) 3⎥

L :=
⎢ ∑ x ⋅ y

⎢ i=1 ⎥
⎢ i=1 i = 1 i = 1 ⎥ ⎢ n ⎥
⎢ n ⎥
n n ⎢ ⎡( xi) 2 ⋅ yi⎤ ⎥

⎢ ∑ ( x i) 2 ∑ ( )
xi
3

4⎥
( x i) ⎥ ⎢ ∑ ⎣ ⎦⎥
⎣i = 1 ⎦
⎣i = 1 i = 1 i = 1 ⎦

The unknown constants are determined below using lsolve

⎛ −0.444 ⎞
C := lsolve ( M , L) C = ⎜ 12.484 ⎟
⎜ ⎟
⎝ −2.057 ⎠

C1 := C1 C2 := C2 C3 := C3

C1 = −0.444 C2 = 12.484 C3 = −2.057

The polynomial fitting the given data , then, is


206 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

2
f ( x ) := C1 + C2 ⋅ x + C3 ⋅ x

which yields

f ( 1 ) = 9.982 f ( 2 ) = 16.294 f ( 4 ) = 16.574 f ( 3.5) = 18.047

f ( 4 ) = 16.574 f ( 5 ) = 10.542

Figure 6.8 shows a comparison of the curve-fit generated with the given data points

i := 1 .. 6 x 1 := 0 x 2 := 1 x 3 := 2 x 4 := 3

y 1 := 0 y 2 := 8.47 y 3 := 17.48 y 4 := 19.57


<---- Given data
x 5 := 4 x 6 := 5 y 5 := 14.69 y 6 := 11.23

z := 0 , 0.01 .. 5

Curve Fitted with Sec order Poly

20

15
Y-Values

f( z)
10
yi

Sec order Poly


Given data points
0
0 1 2 3 4 5
z , xi

X- Values

Figure 6.8. Curve-fit with polynomial


Chapter 6: Curve-Fitting 207

6.9 USE OF MATHCAD'S REGRESSION FUNCTIONS FOR


CURVE-FITTING

6.9.1 Linear Regression with Mathcad

In Mathcad, linear regression is done on data represented by vectors vx and vy by using the
" slope" and the " intercept " functions . The function slope(vx,vy) returns a scalar, which is
the slope of the least-squares fitted straight line . The function intercept(vx,vy) returns a
scalar which is the y- axis intercept of this line. Alternatively, the function line(vx,vy) can be
used, which returns a vector containing the y-intercept as well as the slope of the regression
line. Use of these functions is demonstrated below.
Example 6.8.

Determine a linear function that would fit the data of Example 6.1 using Mathcad's linear
regression

For this example, the given data is i := 1 , 2 .. 11 x 1 := 0.10 x i+1 := x i + 0.05

y 1 := 65.85 y 2 := 65.20 y 3 := 55.51 y 4 := 50.43 y 5 := 45.97 y 6 := 33.25 y 7 := 34.33

y 8 := 29.76 y 9 := 23.89 y 10 := 23.76 y 11 := 18.99 n := 11

The function generated in the example was f(x) with C1 and C2 shown below

C1 := 75.497 C2 := −99.618 f ( x ) := C1 + C2 ⋅ x

The data points are entered in as the vx and vy vectors in Mathcad

⎛ 0.1 ⎞ ⎛ 65.85 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.15 ⎟ ⎜ 65.20 ⎟
⎜ 0.2 ⎟ ⎜ 55.51 ⎟
⎜ 0.25 ⎟ ⎜ 50.43 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.30 ⎟ ⎜ 45.97 ⎟
vx := ⎜ 0.35 ⎟ vy := ⎜ 33.25 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.40 ⎟ ⎜ 34.33 ⎟
⎜ 0.45 ⎟ ⎜ 29.76 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.50 ⎟ ⎜ 23.89 ⎟
⎜ 0.55 ⎟ ⎜ 23.76 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 0.60 ⎠ ⎝ 18.99 ⎠
208 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The " slope " and the " intercept " functions are used as follows to generate the slope m and the
intercept c

m := slope ( vx , vy) m = −99.618 c := intercept ( vx , vy) c = 75.497

Alternatively, use line(vx,vy) to generate the above quantities.

⎛c ⎞
⎜ ⎟ := line ( vx , vy) m = −99.618 c = 75.497
⎝m ⎠

The straight line fit generated by Mathcad is then

ylinmathcad ( x ) := m ⋅ x + c ylinmathcad ( 0.55) = 20.707

Figure 6.9 shows a comparison of the data with the curve-fits generated

Comparison- Fitted Function with Data


80
Given data-
Fitted Function - f(x)
60 Mathcad Linear Regression
Y and f(x) values

yi

f( x) 40
ylinmathcad( x)

20

0
0.1 0.2 0.3 0.4 0.5 0.6
xi , x

X- Values

Figure 6.9. Curve-fit with Mathcad's linear regression functions


Chapter 6: Curve-Fitting 209

6.9.2 Nonlinear Regression with Mathcad

In Mathcad, the functions "regress" and "loess" are used along with "interp" to fit a
polynomial through given data points. The " regress " function fits a single polynomial of any
desired order to fit all given data points. In practice, however, there is no need to fit a polynomial
of order higher than 4 ( n = 4 ). This function will not work very well if your data does not fit into
a single polynomial.

Unlike "regress", the " loess " function generates different second-order polynomials for
different regions of the curve . It does this by examining data in small regions. The argument
"span" controls the size of this region. With larger "span " , "loess" becomes equivalent to n =
2. A good value to use is n = 0.75 . While smaller values of "span " will make the fitted curve
track the data fluctuations in a more precise manner, a larger value of " span " , in general ,
will generate a smoother fit.

The " interp" function is the same as the one we resorted to when doing interpolation. It returns
the interpolated y- values corresponding to the x values. In the examples that follow, the vector "
vs1" relates to "regress" while " vs2" is related to the use of the " loess " function.

The formats of these functions are given below.

regress(vx,vy,n): vx and vy are are vectors containing the given


data points and n is the polynomial order required.

loess(vx, vy, span): vx and vy are are vectors containing the


given data points and span controls the size of the
regions for which second-order polynomials are to be generated.

interp(vs,vx,vy,x): This returns the interpolated y value for x. Here, vs is the vector that is
generated through the use of loess or regress and vx and vy are as defined above

Example 6.9

Do a curve-fit on the data of Example 6.2, using Mathcad's nonlinear regression.

The given data is .

i := 1 , 2 .. 10 x 1 := 0.5 x i+1 := x i + 0.5

y 1 := 0.51 y 2 := 2.35 y 3 := 7.54 y 4 := 13.23 y 5 := 17.65

y 6 := 24.21 y 7 := 28.94 y 8 := 37.63 y 9 := 58.32 y 10 := 63.21


210 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The function generated in the example was a quadratic function of the form f(x)= C1 + C 2 x 2 with

2
C1 := 0.779 C2 := 2.554 f ( x ) := C1 + C2 ⋅ x

The data points given are put in as the vx and vy vectors shown below

i := 1 .. 10 vxi := x i vyi := y i

Define :

vs1 := regress ( vx , vy , 2) vs2 := loess ( vx , vy , 1.)

f1 ( x ) := interp ( vs1 , vx , vy , x ) f2 ( x ) := interp ( vs2 , vx , vy , x )

A comparison of curve-fits generated for the given data is presented in Figure 6.10 and
in Table 6.1

x 1 := 0.5 x i+1 := x i + 0.5 z := 0.5 , 0.6 .. 5 z := 0.5 , 0.51 .. 5.0 i := 1 .. 10

y 1 := 0.51 y 2 := 2.35 y 3 := 7.54 y 4 := 13.23 y 5 := 17.65 <-- Given data

y 6 := 24.21 y 7 := 28.94 y 8 := 37.63 y 9 := 58.32 y 10 := 63.21

Comparison-Fitted Function with Data


100
Given Data Points
Data (Y) and f(x) Values

yi Curve-Fit with Quadratic


Curve-Fit with Mathcad regress function
f( z) Curve-Fit with Mathcad loess function
50
f1( z)

f2( z)

0
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
xi , z

X-Values

Figure 6.10. Curve-fitting with Mathcad's nonlinear regression functions


Chapter 6: Curve-Fitting 211

TABLE 6.1

Comparison of given data with curve-fits generated

Given data Quadratic Fit Mathcad -regress Mathcad - loess

xi = yi = ( )
f xi = ( )
f1 x i = ( )
f2 x i =
0.5 0.51 1.417 7.709 7.654
1 2.35 3.333 7.375 7.437
1.5 7.54 6.525 8.732 8.881
2 13.23 10.995 11.782 11.946
2.5 17.65 16.741 16.523 16.818
3 24.21 23.765 22.956 23.167
3.5 28.94 32.066 31.081 31.243
4 37.63 41.643 40.898 40.985
4.5 58.32 52.498 52.407 52.4
5 63.21 64.629 65.608 65.52

6.9.3 Use of the Function linfit

The Mathcad function "linfit" evaluates the undetermined coefficients C1 , C2 ..... in a


regression function that is a linear combination of known functions f1 (x), f2 (x)..... .It has the
form

C1 f1 (x) + C2 f2 (x) + ....... .

The function linfit(vx,vy,F) returns a vector that contains the coefficients C 1 , C2 ..... in
the linear combination of known functions that would best fit the given data based on the
least squares principle. Here, "F" is a vector representing the functions that are linearly
combined to generate the best fit to the given data and vx and vy are vectors representing
the given data. Use of linfit requires that there be at least as many data points as there
are terms in F.

The use of " linfit" is illustrated in the following steps, as it is applied to the solution of
Example 6.6 .
212 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The given data points are:

x 1 := 1.0 x 2 := 2.0 x 3 := 3.0 x 4 := 4.0 x 5 := 5.0

y 1 := 5.75 y 2 := 10.75 y 3 := 12.65 y 4 := 29.95 y 5 := 49.35

The curve-fit of Example 6.6 was obtained as a linear combination of the form:

yanal(x)=C 1 f1 (x) + C2 f2 (x)

with C1 = 5.931, C2 = 0.35, f1 (x) =1 and f2 (x)= x3

3
C1 := 5.931 C2 := 0.35 f1 ( x ) := 1 f2 ( x ) := x

yanal ( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x )

The vx and vy vectors, in this case, are

vx := x vy := y

⎛1 ⎞ ⎛ 5.75 ⎞ Put in "F" as a


⎜2 ⎟ ⎜ 10.75 ⎟
⎜ ⎟ ⎜ ⎟ ⎛1⎞ vector of
F ( x ) := ⎜ ⎟ <--- functions
vx = ⎜ 3 ⎟ vy = ⎜ 12.65 ⎟ 3
⎝x ⎠ used in the
⎜4 ⎟ ⎜ 29.95 ⎟ linear
⎜ ⎟ ⎜ ⎟ combination
⎝5 ⎠ ⎝ 49.35 ⎠ resorted to

The vector "S" returned


⎛ 5.9309 ⎞
S := linfit ( vx , vy , F) S= ⎜ ⎟ <---- contains the coefficients C 1 ,
⎝ 0.3502 ⎠ C2 ........

C1 := S1 C1 = 5.9309 C2 := S2 C2 = 0.3502

The function " g(x) " is the linear combination of the known functions resorted to. Notice
that here both F(x) and S are vectors .
Chapter 6: Curve-Fitting 213

g ( x ) := F ( x ) ⋅ S

which means: g(x)= C1 f1 (x) + C2 f2 (x) x := 1.0 , 1.01 .. 5

The Mathcad ( linfit ) results and the linear combination analysis results are presented in
Figure 6.11 and compared with the given data points.

j := 1 , 2 .. 5

Comparison of results

Given Data Points


40 Analysis Results (Linear Combination )
vy j
y-values

Regression with linfit


yanal ( x)
20
g ( x)

0
1 1.5 2 2.5 3 3.5 4 4.5 5
vx j , x

x- values
Figure 6.11. Comparison of linfit results with given data

6.9.4 Use of the Function genfit

The function genfit(vx,vy,vg,F) returns a vector that contains the parameters that make a
nonlinear function f of x and n parameters u1 , u2 ... un best fit the data represented in vectors
vx and vy. Use of this function requires that there be at least as many data points as
parameters. Here, vg is an n -element vector of guess values for the parameters u1 , u2 ...
un , and F is a column vector containing the function f and its partial derivatives with respect
to the n parameters u1 , u2 ... un .

While it is often faster and less sensitive to poor guess values, this process may fail to
converge in situations where the derivative vectors are done incorrectly. The method also
permits a solution that employs numerical approximations for the parameter derivatives. To
change methods, right-click on the genfit function and select the desired method from the
menu. The use of this Mathcad function is best illustrated by an example as shown below.
214 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.11

Given the following data: x: 0.3, 0.4, 1, 1.4, 2, 4

y: 9.4 11.2, 5, 3 , 6, 0

Fit the data with a function of the form: exp ( u1 + u2 x + u3 x2 )

Assemble the column vector F as follows.

⎛⎜ u1+u2⋅ x+u3⋅ x2 ⎞⎟ This is a column vector with:


e
⎜ ⎟
⎜ u1+u2⋅ x+u3⋅ x2 ⎟ 1st row: Function fitting data
F ( x , u) :=
⎜ e ⎟ <--- 2nd row: Partial derivative of function with resp to u1
⎜ u +u ⋅ x+u ⋅ x2 ⎟ * 3rd row: Partial derivative of function with respect to u2
⎜ x ⋅e 1 2 3 ⎟ 4th row: Partial derivative of function with respect to u3
⎜ 2

⎜ x2 ⋅ eu1+u2⋅ x+u3⋅ x ⎟
⎝ ⎠

Put in the given data points as shown

⎛⎜ 0.3 ⎞⎟ ⎛⎜ 9.4 ⎞⎟
⎜ 0.4 ⎟ ⎜ 11.2 ⎟
⎜1 ⎟ ⎜ 5 ⎟
vx := ⎜ ⎟* vy := ⎜ ⎟*
⎜ 1.4 ⎟ ⎜ 3 ⎟
⎜2 ⎟ ⎜ 6 ⎟
⎜4 ⎟ ⎜ 0 ⎟
⎝ ⎠ ⎝ ⎠

Fill in the vector of guess values for u1 , u2 , u3 .....

⎛1 ⎞
vg := ⎜ 0 ⎟
⎜ ⎟*
⎝ −1 ⎠

The function genfit now computes the parameters u1 , u2 , u3 .....

P := genfit ( vx , vy , vg , F)
*
Chapter 6: Curve-Fitting 215

⎛ 2.5654 ⎞
P = ⎜ −0.7881 ⎟ <-----
That is, u1 = 2.5654, u2 = -0.7881 , u3 = 0.0364,
⎜ ⎟ * as calculated by genfit.
⎝ 0.0364 ⎠

u1 := P1 u2 := P2 u3 := P3
* * *

The function that best fits the given data is now

2 This form also can be used since it


u 1+ u2⋅ x+ u 3⋅ x g ( x ) := F ( x , P) 1 <--
g ( x ) := e or * represents the zeroth column of the
*
vector F(x,u )

It is plotted in Figure 6.12 and is compared with the given data points

i := 0 .. 6 x := 0.3 , 0.31 .. 4
* *

Comparison of curve-fit with given data


15
Given data points
Curve-fit with genfit
10
vy i

g ( x)
5

0
0 0.5 1 1.5 2 2.5 3 3.5 4
vxi , x

Figure 6.12. Curve-fitting with genfit

6.9.5 Use of the Mathcad functions logfit, lnfit, pwrfit and expfit

Mathcad's logfit

The function logfit(vx,vy,vg) returns a vector containing the coefficients a, b and c for a
216 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

logarithmic curve of the form a ln(x + b ) + c that best fits the real data values represented by
vectors vx and vy. The use of this function requires that there be at least three data points The
vector vg is a three-element vector of real guess values for the parameters a, b and c. If a
logarithmic fit is desired that is different from the above form, then use genfit or linfit.

As an example, for the data represented by vectors vx and vy below and with the use of the
guess vector vg as given

⎛⎜ 0 ⎞⎟ ⎛⎜ 4 ⎞⎟
⎜1 ⎟ ⎜5 ⎟
⎜2 ⎟ ⎜ 5.5 ⎟ ⎛2 ⎞
vx := ⎜ ⎟ vy := ⎜ ⎟ vg := ⎜ 1 ⎟
⎜3 ⎟ ⎜ 5.8 ⎟ ⎜ ⎟
⎝5 ⎠
⎜4 ⎟ ⎜ 6.2 ⎟
⎜5 ⎟ ⎜ 6.5 ⎟
⎝ ⎠ ⎝ ⎠

the curve-fit returned by Mathcad's logfit is

⎛⎜ a ⎞⎟
⎜ b ⎟ := logfit ( vx , vy , vg) a = 1.388 b = 1.0437 c = 3.9536
⎜c ⎟
⎝ ⎠

which is the function f(x )= 1.388 ln (x + 1.044 ) + 3.954, the plot of which is drawn below.

f ( x ) := a ⋅ ln ( x + b) + c x := 0 , 0.01 .. 5.

8
Given data points
Mathcad's logfit
vy 6
f( x)

0 1 2 3 4 5
vx , x
Chapter 6: Curve-Fitting 217

Mathcad's lnfit

The function lnfit(vx,vy) returns a vector containing the coefficients a and b for a logarithmic
curve of the form a ln(x ) + b that best fits the real data values represented by vectors vx and vy.
The use of this function requires that there be at least two data points. If a logarithmic fit is
desired that is different from the above form, then use genfit or linfit.

For the data represented by the following vx and vy vectors,

⎛⎜ 1 ⎞⎟ ⎛⎜ 5 ⎞⎟
⎜2 ⎟ ⎜ 6.5 ⎟
⎜3 ⎟ ⎜ 7.25 ⎟
vx := ⎜ ⎟ vy := ⎜ ⎟
⎜4 ⎟ ⎜ 7.80 ⎟
⎜5 ⎟ ⎜ 8.25 ⎟
⎜6 ⎟ ⎜ 8.63 ⎟
⎝ ⎠ ⎝ ⎠

the curve-fit returned by Mathcad's lnfit is

⎛a ⎞
⎜ ⎟ := lnfit ( vx , vy) a = 2.0048 b = 5.04
⎝b ⎠

which is the function f(x ) = 2.005 ln (x) + 5.04, the plot of which is shown below.

f ( x ) := a ⋅ ln ( x ) + b x := 1 , 1.01 .. 6.

10
Given data points
8 Mathcad's lnfit
vy

f( x) 6

1 2 3 4 5 6
vx , x
218 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Mathcad's pwrfit

The function pwrfit(vx,vy,vg) returns a vector containing the coefficients a, b and c for a power
curve of the form a x b + c that best fits the real data values represented by vectors vx and vy.
The use of this function requires that there be at least three data points and the vx values
must all be greater than or equal to zero. Negative x-values are not appropriate in this setting
because raising them to an arbitrary power can produce complex results that will not
correspond to the real y-values. If you need to fit a power function to data in the left-half plane,
shift it so that all x-values are positive, then adjust the fitted function accordingly to obtain
correct results. The vector vg is a three-element vector of real guess values for the parameters
a, b and c . If a logarithmic fit is desired that is different from the above form, then use genfit.

For the data points represented by the vectors vx and vy as given below, and with the use of
the vector vg of guess values shown for the coefficients a, b and c..

⎛⎜ 0 ⎞⎟ ⎛⎜ 5 ⎞⎟
⎜1 ⎟ ⎜ 9 ⎟
⎜2 ⎟ ⎜ 50 ⎟
⎜ ⎟ ⎜ ⎟ ⎛3⎞
vx :=
⎜3 ⎟ vy :=
⎜ 192 ⎟
vg := ⎜ 4 ⎟
⎜4 ⎟ ⎜ 517 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ ⎟ ⎝ 10 ⎠
⎜5 ⎟ ⎜ 1123 ⎟
⎜6 ⎟ ⎜ 2121 ⎟
⎜7 ⎟ ⎜ 3635 ⎟
⎝ ⎠ ⎝ ⎠

the curve-fit returned by Mathcad's pwrfit is

⎛⎜ a ⎞⎟
⎜ b ⎟ := pwrfit ( vx , vy , vg) giving a = 3.9977 b = 3.5003 c = 4.9612
⎜c ⎟
⎝ ⎠

which is the function f(x ) = 3.998 x 3.5 + 4.961 , the plot of which is sketched below.

b
f ( x ) := a ⋅ x + c x := 0 , 0.01 .. 7.
Chapter 6: Curve-Fitting 219

4000
Given data points
Mathcad's pwrfit

vy
2000
f( x)

0
0 1 2 3 4 5 6 7
vx , x

Mathcad's expfit

The function expfit(vx,vy, [vg]) returns a vector containing the coefficients a, b and c for an
exponential curve of the form A exp (b x ) + C that best fits the real data values represented by
vectors vx and vy. The use of this function requires that there be at least three data points and the
vx values must all be greater than or equal to zero. If a fit has be done on data that has negative
x-values, then the data must be shifted to the positive axis. . Otherwise, a curve-fit with erroneous
values may be generated. The vector vg is a three-element vector of real guess values for the
parameters A, b and C, but it is optional, and does not have to be used. However, if it is not
used, then expfit generates a guess that fits a line to the logs of vy. If an exponential fit is
desired that is different from the above form, then use genfit .

For the data points represented by the vectors vx and vy as given below, and with the use of the
vector vg of guess values shown for the coefficients a, b and c ,.

⎛0 ⎞ ⎛ 21 ⎞
⎜ ⎟ ⎜ ⎟
⎜1 ⎟ ⎜ 24.892 ⎟
⎜2 ⎟ ⎜ 31.31 ⎟
⎜3 ⎟ ⎜ 41.89 ⎟ ⎛3⎞
⎜ ⎟ ⎜ ⎟ vg := ⎜ 2 ⎟
vx := ⎜ 4 ⎟ vy := ⎜ 59.334 ⎟ ⎜ ⎟
⎜5 ⎟ ⎜ 88.095 ⎟ ⎝ 10 ⎠
⎜ ⎟ ⎜ ⎟
⎜6 ⎟ ⎜ 135.513 ⎟
⎜7 ⎟ ⎜ 213.693 ⎟
⎜ ⎟ ⎜ ⎟
⎝8 ⎠ ⎝ 342.589 ⎠

the curve-fit returned by Mathcad's expfit is defined by


220 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛⎜ A ⎞⎟
⎜ b ⎟ := expfit ( vx , vy , vg)
⎜C ⎟
⎝ ⎠

giving A=6 b = 0.5 C = 14.9999

Or, if vg is not used, resort to

⎛⎜ A ⎞⎟
⎜ b ⎟ := expfit ( vx , vy)
⎜C ⎟
⎝ ⎠

again, giving A=6 b = 0.5 C = 14.9999

which leads to the curve-fit f(x ) = 6 exp (0.5 x ) + 15 , the plot of which is sketched below.

b⋅ x
f ( x ) := A ⋅ e +C x := 0 , 0.01 .. 8

400
Given data points
Mathcad's expfit
vy 200

f( x)

0 1 2 3 4 5 6 7 8
vx , x

6.9.6 More Examples with Mathcad

Example 6.12

Using Mathcad's linear regression, generate a curve-fit in the form of a power function for the
data of Example 6.3 and compare with the given data and the analytical results of Example
6.3.
Chapter 6: Curve-Fitting 221

The function generated by analysis in Example 6.3 was:

B
f ( x) = A ⋅ x

with
A := 6.571 B := 0.2

B
f ( x ) := A ⋅ x

The data of Example 6.3 is shown below

n := 16 i := 1 , 2 .. 16

⎛ 4.5921 ⎞
⎛ 0.18 ⎞ ⎜ ⎟
⎜ ⎟ ⎜ 5.2345 ⎟
⎜ 0.32 ⎟ ⎜ 6.2321 ⎟
⎜ 0.65 ⎟ ⎜ 6.5467 ⎟
⎜ 1.12 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 7.2976 ⎟
⎜ 1.65 ⎟ ⎜ 7.7654 ⎟
⎜ 2.34 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 7.7237 ⎟
⎜ 2.52 ⎟ ⎜ 8.2543 ⎟
⎜ 2.85 ⎟ y := ⎜
x := ⎜
⎟ <-- Given data
⎟ ⎜ 7.9765 ⎟
⎜ 3.12 ⎟ ⎜ 8.4328 ⎟
⎜ 3.61 ⎟ ⎜ 9.6322 ⎟
⎜ 3.92 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 8.8764 ⎟
⎜ 4.51 ⎟ ⎜ 9.0276 ⎟
⎜ 4.72 ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 9.2568 ⎟
⎜ 5.25 ⎟ ⎜ 8.6432 ⎟
⎜ 6.75 ⎟ ⎜ ⎟
⎜ ⎟ ⎝ 10.2137 ⎠
⎝ 7.85 ⎠

In Mathcad, linear regression with the slope and intercept functions can be performed on
this problem or the Mathcad function pwrfit can be resorted to and curve-fits obtained as
shown in the following steps.

As was done in Section 6.5, we define:

i := 1 , 2 .. 16
222 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )
Yi := ln y i ( )
Xi := ln x i

vX i := Xi vY i := Yi <-- Define vectors vX and vY

The slope and the intercept of the new linear relationship Y= CSTAR+BSTAR*X can now
be computed as

BSTAR := slope ( vX , vY ) CSTAR := intercept ( vX , vY ) BSTAR = 0.1996 CSTAR = 1.8827

CSTAR
ASTAR := e A = 6.571

Using Mathcad's pwrfit function on the given data points with the guess vector

⎛6 ⎞
vg := ⎜ 0.1 ⎟
⎜ ⎟
⎝ 0.1 ⎠

⎛⎜ a ⎞⎟
yields ⎜ b ⎟ := pwrfit ( x , y , vg) That is, a = 9.4201 b = 0.142 c = −2.7907
⎜c ⎟
⎝ ⎠

b
powerfit ( x ) := a ⋅ x + c

The various curve-fits generated are now

BSTAR
fmcad ( x ) := ASTAR ⋅ x <--Fit generated with Mathcad's linear regression
(slope and intercept functions)

b
powerfit ( x ) := a ⋅ x + c <--Fit generated with Mathcad's pwrfit function

B
f ( x ) := A ⋅ x <--- Fit generated with power function (Example 6.3)
Chapter 6: Curve-Fitting 223

and these are compared and presented along with the given data in Figure 6.13.

i := 1 , 2 .. 16

Curve-Fitting with Power Function


11

10

9
yi

8
y- values

f( z)

fmcad ( z)
7
powerfit ( z)

6
Given Data Points
Power Function Fit
5 Mathcad's Linear Regression
Mathcad's pwrfit function
4
1 2 3 4 5 6 7 8
xi , z

x- values

Figure 6.13. Curve-fitting with a power function


224 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.13

Using Mathcad's linear regression, generate a curve-fit in the form of the exponential
function A exp ( Bx ) for the data of Example 6.4 . Compare this with the given data and
with the analytical results of Example 6.4

The data given is

i := 1 , 2 .. 6

⎛⎜ 0 ⎞⎟ ⎛⎜ 16.45 ⎞⎟
⎜1 ⎟ ⎜ 4.47 ⎟
⎜2 ⎟ ⎜ 1.06 ⎟
x := ⎜ ⎟ y := ⎜ ⎟
⎜3 ⎟ ⎜ 0.45 ⎟
⎜4 ⎟ ⎜ 0.15 ⎟
⎜5 ⎟ ⎜ 0.03 ⎟
⎝ ⎠ ⎝ ⎠

The function generated in Example 6.4 was :

n := 6
B⋅ x
f ( x) = A ⋅ e

with A := 15.384 B := −1.216

B⋅ x
f ( x ) := A ⋅ e <-- Analysis-generated fit

As was done in Section 6.6 , define

( )
fi := f x i ()
logfunci := ln f i

Yi := ln y i ( ) <-- log of the y-coordinates of given data

In Mathcad, linear regression with the slope and intercept functions or with the line function
can be performed on this problem and the curve-fit obtained as shown in the following steps

i := 1 .. 6

vx := x vY := Y
BSTAR := slope ( vx , vY ) CSTAR := intercept ( vx , vY )
Chapter 6: Curve-Fitting 225

⎛ CSTAR ⎞
⎜ ⎟ := line ( vx , vY ) BSTAR = −1.2164 CSTAR = 2.7334
⎝ BSTAR ⎠

CSTAR
ASTAR := e ASTAR = 15.3844

Then,

BSTAR⋅ x Mathcad -generated fit using slope and


fmcad ( x ) := ASTAR ⋅ e <---
intercept or the line function

Mathcad's expfit function can also be used to generate an exponential fit of the form
a exp(bx) + c as shown below

⎛⎜ a ⎞⎟
⎜ b ⎟ := expfit ( x , y) a = 16.4172 b = −1.3222 c = 0.0391
⎜c ⎟
⎝ ⎠
b⋅ x
exponfit ( x ) := a ⋅ e +c <-- Mathcad -generated fit using the expfit function

Figure 6.14 shows a comparison of the Mathcad fits with the results of Example 6.4 and
the given data .

B⋅ x
z := 0 , 0.1 .. 5 i := 1 .. 6 f ( x ) := A ⋅ e

Curve-Fitting with Exponential Function


20
Given Data Points
yi Analysis-Generated Fit
Mathcad's Linear Regression
f( z) Mathcad's expfit function
y- values

10
fmcad ( z)

exponfit ( z) − 0.05

0 1 2 3 4 5
xi , z

x- values

Figure 6.14. Curve-fit with an exponential function ( AeBx) using


Mathcad's linear regression
226 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.14

Using Mathcad's linear regression, generate a fit in the form of the exponential function
"Ax exp(Bx)" for the problem of Example 6.5 . Compare this with the results of Example
6.5 and with the given data .

The data given is

i := 1 , 2 .. 6

e1 := 300 e2 := 800 e3 := 1300 e4 := 1800 e5 := 2300 e6 := 2800

S1 := 1656 S2 := 3521 S3 := 4079 S4 := 4354 S5 := 4300 S6 := 3675

The function generated in Example 6.5 was

f ( e) = A ⋅ e ⋅ exp ( B ⋅ e)

−4
with A := 6.7248 B := −5.7205 ⋅ 10 f ( e) := A ⋅ e ⋅ exp ( B ⋅ e)

⎛ Si ⎞
n := 5 Zi := ln ⎜ ⎟ ( )
Si := ei ⋅ exp Zi
⎝ ei ⎠

e := 300 , 301 .. 3000

In Mathcad, linear regression can be performed on this problem and the curve-fit obtained as
shown in the following steps, :

e1 := 300 e2 := 800 e3 := 1300 e4 := 1800 e5 := 2300 e6 := 2800

S1 := 1656 S2 := 3521 S3 := 4079 S4 := 4354 S5 := 4300. S6 := 3675

i := 1 , 2 .. 6

As was done in Example 6.5, define

⎛ Si ⎞ vei := ei vZ i := Zi
Zi := ln ⎜ ⎟
⎝ ei ⎠
Chapter 6: Curve-Fitting 227

−4
BSTAR := slope ( ve , vZ ) CSTAR := intercept ( ve , vZ ) BSTAR = −5.7205 × 10

CSTAR = 1.9058

ASTAR := exp ( CSTAR ) ASTAR = 6.7248

fmcad ( e) := ASTAR ⋅ e ⋅ exp ( BSTAR ⋅ e) <-- Mathcad - generated fit

f ( e) := A ⋅ e ⋅ exp ( B ⋅ e) <-- Analysis-generated fit

Figure 6.15 shows a comparison of the Mathcad-generated fit using linear regression with the
analysis- generated fit of Example 6.5 and with the given data .

z := 300 , 301 .. 2800

Comparison of Curve-Fit Results


5000

4000
Stress (S) Values

Si

f( z) 3000
fmcad ( z)

2000
Given Data Points
Analysis -generated fit
Mathcad's Linear Regression
1000
500 1000 1500 2000 2500 3000
ei , z

Strain (e ) Values

Figure 6.15. Curve-fit with an exponential function (Axe Bx) using Mathcad's
linear regression
228 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.15

Using Mathcad, generate a nonlinear fit for the data of Example 6.6. Compare these
results with the fit generated in Example 6.6 by using a linear combination of
functions and the given data.

x := 1 x := 2.0 x := 3.0 x := 4.0 x := 5.0


1 2 3 4 5
<--- Given data
y := 5.75 y := 10.75 y := 12.65 y := 29.95 y := 49.35
1 2 3 4 5

The fit generated in Example 6.6 was yanal( x ) = C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x )

3
with f1 ( x ) := 1 f2 ( x ) := x C1 := 5.931 C2 := 0.35

Using Mathcad' s interp and loess functions , curve-fitting with a polynomial can be done
on this problem as shown in the following steps.

i := 1 , 2 .. 5 x := 1 , 1.1 .. 5.5 x := 1 x := 2.0 x := 3.0 x := 4.0 x := 5.0


1 2 3 4 5

y := 5.75 y := 10.75 y := 12.65 y := 29.95 y := 49.35


1 2 3 4 5

vx := x vy := y vs := loess( vx , vy , 1 ) floess( x ) := interp( vs , vx , vy , x )


i i i i

yanal( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ) floess := floess x


i ( i) yanal := yanal x
i ( i)
Figure 6.16 compares the Mathcad fit with the given data and the linear combination method

yanal( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ) floess( x ) := interp( vs , vx , vy , x ) z := 1 , 1.01 .. 5.0 i := 1 .. 5

Comparison- Mathcad loess / anal method


60
Given Data Points
yi
Y- Values

40 Linear Combination
yanal( z) Mathcad loess function
floess( z)20

0
1 1.5 2 2.5 3 3.5 4 4.5 5
xi , z

X- values

Figure 6.16. Comparison of Mathcad fit with linear combination method


Chapter 6: Curve-Fitting 229

Example 6.16

Using Mathcad, generate a nonlinear fit for the data of Example 6.7. Compare these results
with the fit of form y = f(x) = C1 + C2 x + C3 x 2 generated in Example 6.7 and with the given
data .

x := 0 x := 1 x := 2 x := 3 x := 4 x := 5
1 2 3 4 5 6
<--Given data
y := 0 y := 8.47 y := 17.48 y := 19.57 y := 14.69 y := 11.23 n := 6
1 2 3 4 5 6

2
The fit generated in Example 6.7 was f( x ) = C1 + C2 ⋅ x + C3 ⋅ x

with C1 := −0.444 C2 := 12.484 C3 := −2.057

Using Mathcad's interp and loess functions , curve-fitting the given data with a polynomial can
be done as shown below.

i := 1 , 2 .. 6 vx := x vy := y vs := loess( vx , vy , 1 ) floess( x ) := interp( vs , vx , vy , x )


i i i i

( i) ( i)
2
floess := floess x f( x ) := C1 + C2 ⋅ x + C3 ⋅ x f := f x
i i

The Mathcad results generated are compared with the data given and with the polynomial method
in Figure 6.17.
2
i := 1 .. 6 z := 0 , 0.01 .. 5 f( x ) := C1 + C2 ⋅ x + C3 ⋅ x floess( x ) := interp( vs , vx , vy , x )

Comparison- Polynomial Method/ Mathcad


20

yi
Y- Values

f( z) 10 Given Data Points


floess( z) Polynomial Method
Mathcad loess Function
0
0 1 2 3 4 5
xi , z

X- Values

Figure 6.17. Comparison of Mathcad fit with polynomial method


230 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.17

Fit the data points :

x: 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8


y: 0.59 0.83 0.90 0.95 1.02 1.43 1.75 3.15

with a function of the form : y = C1 f 1 (x ) + C2 f 2 ( x) +C 3 f 3 (x) + C4 f4 (x) where


f 1 (x ) = 1 , f 2 ( x ) = x2 , f 3 ( x) = sin (2x) , f 4 ( x) = exp( 0.95x )

In this case, we have the following matrix equation

[M] {C} = {L} ,

where {C} is the column vector of unknown coefficients C1 , C 2 , C3 and C4 , and [M] and {L} are
known matrices as generated below.

The given data is

N := 8

x 1 := 0.1 x 2 := 0.2 x 3 := 0.3 x 4 := 0.4 x 5 := 0.5 x 6 := 0.6 x 7 := 0.7 x 8 := 0.8

y 1 := 0.59 y 2 := 0.83 y 3 := 0.90 y 4 := 0.95 y 5 := 1.02 y 6 := 1.43 y 7 := 1.75 y 8 := 3.15

2
i := 1 , 2 .. 8 f1 ( x ) := 1 f2 ( x ) := x <--- The subscipts 1 and 2 are literal
subscripts , and do not denote
vector components. These are
f3 ( x ) := sin ( 2 ⋅ x ) done by including a period in the variable
name . Whatever follows the period then
becomes the subscript. For
example , "vel init" is created by typing
f4 ( x ) := exp ( 0.95 ⋅ x )
a period after "vel" and then typing "init"

In this case, the M and L matrices are


Chapter 6: Curve-Fitting 231

N := 8

⎡⎢ N N N N ⎤⎥
f1 ( xi) ⎡⎣( f1 ( x i) ) ⋅ f2 ( xi)⎤⎦ ⎡⎣( f1 ( x i) ) ⋅ f3 ( xi)⎤⎦ ⎡⎣( f1 ( x i) ) ⋅ f4 ( xi)⎤⎦ ⎥
∑ ∑ ∑ ∑
2

⎢ i=1 i = 1 i = 1 i = 1 ⎥
⎢ N ⎥
N N N
⎢ ⎥
∑ ⎡⎣f2 ( x i) ( f1 ( x i) )⎤⎦ ∑ f2 ( xi)
∑ ⎡⎣( f2 ( x i) ) ⋅ f3 ( xi)⎤⎦ ⎡⎣( f2 ( x i) ) ⋅ f4 ( xi)⎤⎦ ⎥

2

⎢i = 1 i = 1 i = 1 i = 1 ⎥
M := ⎢ ⎥
⎢ N N N N ⎥
∑ ⎡⎣f3 ( x i) ( f1 ( x i) )⎤⎦ ∑ ⎡⎣f3 ( xi) ( f2 ( xi) )⎤⎦ ∑ f3 ( xi)
∑⎡⎣( f3 ( x i) ) ⋅ f4 ( xi)⎤⎦ ⎥
2

⎢i = 1 i = 1 i = 1 i = 1 ⎥
⎢ ⎥
⎢ N N N N ⎥
∑ ⎡⎣f4 ( x i) ( f1 ( x i) )⎤⎦ ∑ ⎡⎣f4 ( xi) ( f2 ( xi) )⎤⎦ ∑ ⎡⎣f4 ( xi) ( f3 ( xi) )⎤⎦ ∑ f4 ( xi)
2
⎢ ⎥
⎢i = 1 ⎥
⎣ i = 1 i = 1 i = 1 ⎦

⎡⎢ N ⎤

⎢ ∑ ⎡⎣( f1 ( xi) ) ⋅ y i⎤⎦ ⎥
⎢i = 1 ⎥
⎢ N ⎥
⎢ ⎥
⎢ ∑ ⎡⎣( f2 ( xi) ) ⋅ y i⎤⎦ ⎥
⎢i = 1 ⎥
L := ⎢ ⎥
⎢ N ⎥
⎢ ∑ ⎡⎣( f3 ( xi) ) ⋅ y i⎤⎦ ⎥
⎢i = 1 ⎥
⎢ ⎥
⎢ N ⎥
⎢ ∑ ⎡⎣( f4 ( xi) ) ⋅ y i⎤⎦ ⎥
⎢i = 1 ⎥
⎣ ⎦

The unknown coefficients in {C} can be computed using the mathcad function lsolve as
follows

⎛ −45.612 ⎞
⎜ ⎟
C := lsolve ( M , L) yielding ⎜ −49.735 ⎟
C=
⎜ −16.7 ⎟
⎜ ⎟
⎝ 45.46 ⎠
232 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The constants C 1 through C4 are the components of the column vector {C} generated above. Thus,

C1 := C1 C2 := C2 C3 := C3 C4 := C4

C1 = −45.612 C2 = −49.735 C3 = −16.7 C4 = 45.46

The fit yielded by the analysis is, then,

yanal ( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ) + C3 ⋅ f3 ( x ) + C4 ⋅ f4 ( x )

and has the form

-45.612 -49.735 x 2 -16.7 sin (2x)+ 45.46 exp(0.95x)I

The use of " linfit" as applied to this problem is illustrated in the following steps.

The vx and vy vectors, in this are

vx := x vy := y

⎛⎜ 0.1 ⎞⎟ ⎛⎜ 0.59 ⎞⎟
⎜ 0.2 ⎟ ⎛ 1 ⎞
⎜ 0.83 ⎟
⎜ 0.3 ⎟ ⎜ ⎟
⎜ 0.9 ⎟ 2
⎜ ⎟ ⎜ x ⎟<--- Put in "F" as a
⎜ 0.4 ⎟ ⎜ ⎟ F ( x ) :=
⎜ sin ( 2 ⋅ x ) ⎟ vector of
vx = ⎜ 0.95 ⎟
⎜ 0.5 ⎟ vy = ⎜ ⎟ functions
⎜ 1.02 ⎟
⎜ ⎟ ⎝ exp ( 0.95 ⋅ x ) ⎠ used in the
⎜ ⎟
⎜ 0.6 ⎟ linear
⎜ 1.43 ⎟
⎜ 0.7 ⎟ combination
⎜ 1.75 ⎟
⎜ 0.8 ⎟ resorted to
⎝ ⎠ ⎜ 3.15 ⎟
⎝ ⎠

⎛ −45.612 ⎞
S := linfit ( vx , vy , F)
⎜ ⎟
⎜ −49.735 ⎟ <---- The vector "S" returned
S= contains the coefficients C 1 ,
⎜ −16.7 ⎟
⎜ ⎟ C2 ........
⎝ 45.46 ⎠
Chapter 6: Curve-Fitting 233

C1 := S1 C2 := S2 C3 := S3 C4 := S4

C1 = −45.612 C2 = −49.735 C3 = −16.7 C4 = 45.46

The function " g(x) " is then the linear combination of the known functions resorted to.
Notice that both F(x) and S are vectors here.

g ( x ) := F ( x ) ⋅ S

which means: g(x)= C 1 f1 (x) + C2 f2 (x) + C3 f3 (x) + C4 f4 (x)

The Mathcad ( linfit ) results and the linear combination analysis results are both presented in
Figure 6.18 and compared with the given data points.

j := 1 , 2 .. 8 x := 0.1 , 0.101 .. 0.80

Comparison of results
4
Given Data Points
Analysis Results (Linear Combination )
Regression with linfit
3

vy j
y-values

yanal ( x)2

g ( x)

0
0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8
vx j , x

x- values

Figure 6.18. Curve-fit with a linear combination of functions


234 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 6.18

Given the following data: x: 0,1,2,3,4,5,6,7

y: 3,6.2,9.75,20.13,24.25,33.21,37.54,41.95

Using Mathcad's genfit, fit the data with a function of the form: f(x )= u1 sin(2x)+u2 cos(4x)+u 3 x,
where the u's are constants to be determined using the least squares principle.

Put in the vector of the desired function and its partial derivatives, the given data points and the
guess vector as follows

A column vector with:


⎛ u1 ⋅ sin ( 2 ⋅ x ) + u2 ⋅ cos ( 4 ⋅ x ) + u3 ⋅ x ⎞
⎜ ⎟ 1st row: Function to fit data
⎜ sin ( 2 ⋅ x ) ⎟ <--- 2nd row: Partial derivative with respect to
F ( x , u) :=
⎜ cos ( 4 ⋅ x ) ⎟* u1
⎜ ⎟ 3rd row: Partial derivative with
⎝ x ⎠ respect to u2
4th row: Partial derivative with respect
to u3

⎛⎜ 0 ⎞⎟ ⎛⎜ 3 ⎞⎟
⎜1 ⎟ ⎜ 6.2 ⎟
⎜2 ⎟ ⎜ 9.75 ⎟
⎜ ⎟ ⎜ ⎟
⎜3 ⎟
vx := ⎜ 20.13 ⎟
⎜ 4 ⎟* vy := <----- Given data points
⎜ 24.25 ⎟*
⎜ ⎟ ⎜ ⎟
⎜5 ⎟ ⎜ 33.21 ⎟
⎜6 ⎟
⎜7 ⎟ ⎜ 37.54 ⎟
⎝ ⎠ ⎜ 41.95 ⎟
⎝ ⎠

⎛1 ⎞
vg := ⎜ 2 ⎟ <--- vector of guess values for u1 , u2 , u3 .....
⎜ ⎟*
⎝3 ⎠

The function genfit now returns a vector of the u- coefficients that will best fit the given data.

P := genfit ( vx , vy , vg , F)
*
Chapter 6: Curve-Fitting 235

⎛ 1.935 ⎞ That is , u1 = 1.935, u2 = 3.175, u3 = 6.251, as calculated by


P = ⎜ 3.175 ⎟ genfit.
⎜ ⎟ *
⎝ 6.251 ⎠

u1 := P1 u2 := P2 u3 := P3

The function that best fits the given data is now

f ( x ) := F ( x , P) 1 or, use, f ( x ) := u1 ⋅ sin ( 2 ⋅ x ) + u2 ⋅ cos ( 4 ⋅ x ) + u3 ⋅ x


*

It is plotted in Figure 6.19 and is compared with the given data points

i := 1 .. 8 x := 0 , 0.1 .. 7
* *

Comparison of curve-fit with given data


50
Given data points
Curve-fit obtained

40

30
vy i

f( x)
20

10

0
0 1 2 3 4 5 6 7
vxi , x

Figure 6.19. Curve-fitting with Mathcad's genfit


236 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

6.10 APPLICATIONS IN CURVE-FITTING

In the world of engineering, science, and business, it is very common to see a function in
the form of data prescribed at many points, and in addition, there may also be
considerable scatter in the available data . The methods discussed in the earlier sections
of this chapter will now be used to generate appropriate curve-fits to actual data from
several practical applications.
6.10.1 Fatigue Failure Curve for Loading in the Finite Life Range

The fatigue failure curve of a material provides useful information pertinent to the design of
machinery for finite life [ 19 ]. It is essentially a relationship between the completely
reversed applied stress, S, and fatigue life, L , which is measured in terms of the number
of stress reversals to failure.

The following data pertains to a steel with an endurance limit of 40 kpsi and an ultimate
strength of 90 kpsi. It is required to curve-fit the data with a suitable function.

L ( million cycles) Stress (kpsi)

⎛ 0.02 ⎞ ⎛ 80 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.03 ⎟ ⎜ 70 ⎟
⎜ 0.04 ⎟ ⎜ 65 ⎟
⎜ 0.05 ⎟ ⎜ 61 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.075 ⎟ ⎜ 57 ⎟
⎜ 0.1 ⎟ ⎜ 55 ⎟
⎜ ⎟ ⎜ ⎟
vL :=
⎜ 0.2 ⎟
vS :=
⎜ 50 ⎟
⎜ 0.4 ⎟ ⎜ 45 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 0.6 ⎟ ⎜ 42 ⎟
⎜ 0.8 ⎟ ⎜ 40.5 ⎟
⎜ 1.0 ⎟ ⎜ 40.1 ⎟
⎜ ⎟ ⎜ ⎟
⎜ 1.1 ⎟ ⎜ 40 ⎟
⎜ 1.2 ⎟ ⎜ 39.9 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 1.3 ⎠ ⎝ 39.85 ⎠

From the plot of the given data shown in Figure 6.20, it appears that a power function would
furnish a reasonable curve-fit. This has the form

S(L)= A*LB ,
Chapter 6: Curve-Fitting 237

where A and B are undetermined coefficients to be evaluated on a least squares basis.

i := 1 .. 14

Given Data Points

Given Data
80
Given S- values (kpsi)

vSi 60

40

0 0.2 0.4 0.6 0.8 1 1.2


vLi

Given L- values (million cycles )

Figure 6.20. Fatigue life data

Using S(L) = A L B , Sstar= C+ B . Lstar , where Lstar= ln (L) , C = ln (A) and


Sstar= ln (S), the matrix equation [D]{X} = {P} is obtained , where {X} is the column vector
containing the unknowns C and B.

n := 14 Sstar := ln ( vS) Lstar := ln ( vL) D⋅X = P

⎡ n ⎤ ⎡ n ⎤


n
∑ Lstar i⎥


⎢ ∑ Sstar i ⎥

⎢ i = 1 ⎥
D := ⎢ i =1 ⎥
⎢ n ⎥ P :=
n
⎢ n ⎥
⎢ 2⎥
(Lstar i) ⎥
⎢ ∑ Lstar i
∑ ⎢
⎢∑ (Sstar i ⋅ Lstar i) ⎥⎥
⎣i = 1 i =1 ⎦ ⎣i = 1 ⎦
The unknown constants C and B can be computed using lsolve as shown below

⎛ 3.684 ⎞
K := lsolve ( D , P) K= ⎜ ⎟ <------Thus, C= 3.684 and B = -0.155
⎝ −0.155 ⎠
C
C := K1 B := K2 C = 3.684 B = −0.155 A := e A = 39.808
238 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The curve-fit generated by the linear regression analysis is now

B
S ( L) := A ⋅ L with A = 39.808

Mathcad's linear regression can also be performed on this problem and the curve-fit obtained
as shown in the following steps.

vLstar := ln ( vL) vSstar := ln ( vS)

⎛ C1 ⎞
⎜ ⎟ := line ( vLstar , vSstar ) B1 = −0.155 C1 = 3.684
⎝ B1 ⎠

C1 B1
A1 := e A1 = 39.808 fmcad ( L) := A1 ⋅ L <--- Mathcad -generated fit

Figure 6.21 shows a comparison of the curve-fits generated with the given data points .

L := 0.02 , 0.03 .. 1.3 i := 1 .. 14

Curve-Fitting with Power Function

80 Given data points


Analysis-generated fit
Mathcad's linear regression
vSi
S- values (kpsi)

60
S( L )

fmcad( L )
40

20
0 0.2 0.4 0.6 0.8 1 1.2
vLi , L

L- values (million cycles)

Figure 6.21. Fatigue failure curve for loading in the finite life range
Chapter 6: Curve-Fitting 239

6.10.2. Temperature Response of an Object Placed in a Hot Stream of Air

The following data pertains to the temperatures of a solid steel sphere suspended in a hot stream
of air measured at several instants of time [ 17 ]. It is required to curve-fit the data with a suitable
function.

t= Time (secs) T=Temp (deg C) t=Time(Secs) T=Temp (deg C)

0.0 14 500 51
100 28 600 54
200 37.5 700 55
300 44 800 57.8
400 48 900 59.1

The given data is put in as follows and plotted in Figure 6.22.

n := 10 t1 := 0 t2 := 100 t3 := 200 t4 := 300 t5 := 400 t6 := 500 t7 := 600

T1 := 14 T2 := 28 T3 := 37.5 T4 := 44 T5 := 48 T6 := 51 T7 := 54

t8 := 700 t9 := 800 t10 := 900 T8 := 55 T9 := 57.8 T10 := 59.1

i := 1 .. 10
60
Given data points

40
Ti
20

0
0 200 400 600 800
ti

Figure 6.22. Given temperature versus time data

A glance at the plot of Figure 6.22 suggests that a function of the following form would provide a
reasonable fit to the given data
−t
τ
f ( t) = A + B ⋅ e

where f(t) is a linear combination of functions in which A and B are constants to be determined
on a least squares basis. From the data plot, it appears that the temperature profile attains
steady state in approximately 1200 seconds, which , when equated to four time constants, will
yield a time constant, τ, of 300 seconds. Thus, the coefficient of t in the exponential term can
be selected as (-1/300).
240 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

In this case, the matrix equation to be solved is

[M] {C} = {A}


−1
⋅t
300
in which {M] and {A} contain the functions f1 ( t) := 1 f2 ( t) := e

and

⎡ n n ⎤ ⎡ n ⎤
f 1 ( t i) ( f 1 ( t i) ⋅ f 2 ( t i) ) ⎥ ( f 1 ( t i ) ⋅ Ti ) ⎥

∑ ∑ ⎢

2
⎢ ⎥ ⎢ ⎥
⎢ i=1 =1 ⎛ C1 ⎞ ⎢i =
,⎥ ⎥
i 1
M := C=⎜ ⎟ , A :=
⎢ n ⎥ ⎢ n ⎥
n
⎝ C2 ⎠
⎢ ( f 1 ( t i) ⋅ f 2 ( t i) ) f 2 ( t i) ⎥ ⎢ (f2 (ti) ⋅ Ti) ⎥⎥
∑ ∑ ∑
2
⎢ ⎥ ⎢
⎣i = 1 i = 1 ⎦ ⎣i = 1 ⎦

In the above, note that n = 10 and m =2, and the unknown constants are C1 and C2 which are
determined below using lsolve.

C := lsolve ( M , A) ⎛ 60.477 ⎞ A := C1 B := C2
C= ⎜ ⎟
⎝ −45.967 ⎠

The function fitting the given data , then, is

−1
⋅t
300
f ( t) := A + B ⋅ e

which is shown in Figure 6.23 .

f ( 0 ) = 14.511 f ( 200 ) = 36.877 f ( 800 ) = 57.283 f ( 900 ) = 58.189

Mathcad's function linfit can also be used to evaluate the undetermined coefficients A and B in
the regression function employed which is a linear combination of the functions selected for the
curve-fit. This can be done as shown in the following steps.

⎛⎜ 1 ⎞⎟
vt := t vT := T F ( t) := ⎜ − 1 ⋅ t ⎟ S := linfit ( vt , vT , F)
⎜ e 300 ⎟
⎝ ⎠
Chapter 6: Curve-Fitting 241

⎛ 60.477 ⎞
S= ⎜ ⎟ <-- This vector returns the coefficients A and B which
⎝ −45.967 ⎠ are referred to here as Astar and Bstar

Astar := S1 Bstar := S2 Astar = 60.477 Bstar = −45.967

The function g(t) which is a scalar product of the vectors F(t) and S will then yield the linear
combination employed

g ( t) := F ( t) ⋅ S

That is,

g ( t) := Astar ⋅ f 1 ( t) + Bstar ⋅ f2 ( t)

The linfit results and those of the linear combination analysis are presented in Figure 6.23 and
compared with the given data .

−1
⋅t
300
f ( t) := A + B ⋅ e g ( t) := F ( t) ⋅ S i := 1 .. 10 t := 0.5 , 1.0 .. 900

Comparison-curve-fits with data


60
temperature, deg. C

vT i
40
f( t)
Given data points
g ( t) 20 Analysis fit
linfit results
0
0 100 200 300 400 500 600 700 800 900
vt i , t

time , seconds

Figure 6.23. Temperature response of an object placed in a hot stream of air


242 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

6.10.3. The Effect of Operating Temperature on the Strength of a Mechanical


Element

When a mechanical element is subjected to reversed stresses at temperatures below room


temperature, there is a strong possibility of the occurrence of brittle fracture . On the other
hand, when the operating temperatures are higher than room temperature, the yield strength
of the material drops off very rapidly with increase in temperature, and yielding can take place
. The temperature-corrected value of tensile strength, then, to be used in design calculations
is obtained by multiplying the tensile strength at room temperature by a factor K d which is
a function of the temperature of the operating environment.

The following data was collected from tests done on carbon and alloy steels and shows the
effect of operating temperature on tensile strength [ 18 ]. An appropriate curve-fit to the
given data is sought.

Temp., deg F. Kd Temp.,deg F. Kd

0 0.975 600 0.963

100 1.008 700 0.927

200 1.020 800 0.872

300 1.024 900 0.797

400 1.018 1000 0.698

500 0.995 1100 0.567

The given data points, which are plotted in Figure 6.24 below, are put in as follows.

n := 12 T1 := 0 T2 := 100 T3 := 200 T4 := 300 T5 := 400 T6 := 500

T7 := 600 T8 := 700 T9 := 800 T10 := 900 T11 := 1000 T12 := 1100

Kd 1 := 0.975 Kd 2 := 1.008 Kd 3 := 1.020 Kd 4 := 1.024 Kd 5 := 1.018 Kd 6 := 0.995

Kd 7 := 0.963 Kd 8 := 0.927 Kd 9 := 0.872 Kd 10 := 0.797 Kd 11 := 0.698 Kd 12 := 0.567


Chapter 6: Curve-Fitting 243

i := 1 .. 12 Given data points

Factor Kd
Kd i

Given data points


0.5
0 200 400 600 800 1000
Ti

Temperature, deg. F

Figure 6.24. Given Kd versus temperature data

We will fit the given data with a second-order polynomial of the following form.

Kd(T) = f(T) = C1 + C2 T + C3 T 2

Here, n = 12 and m = 2 and the matrix equation to be solved is:

[M]{C} = {A},

where [M] is a known 3x3 matrix and {A} is a known column vector as shown below. The
column vector {C} , which involves the undetermined coefficients, is

⎛ C1 ⎞
⎜ ⎟
C = ⎜ C2 ⎟
⎜C ⎟
⎝ 3⎠

The [M] and {A} matrices are


244 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎡ n n ⎤ ⎡ n ⎤
⎢ n
∑ Ti
∑ ( Ti ) 2⎥ ⎢
∑ Kd i ⎥
⎢ ⎥ ⎢ ⎥
i = 1 i = 1
⎢ ⎥ ⎢ i=1 ⎥
⎢ n n n ⎥ ⎢ n ⎥
⎢ ( Ti ) 2 ( Ti ) 3 ⎥ ⎢ ( ) ⎥
M :=
⎢ ∑ Ti
∑ ∑ ⎥
A :=
⎢ ∑ T i ⋅ Kd i

⎢ i=1 i = 1 i = 1 ⎥ ⎢ i=1 ⎥
⎢ n n n ⎥ ⎢ n ⎥
⎢ ( Ti ) ( Ti ) 4⎥
( Ti ) ⎥ ⎢ ⎡( Ti) 2 ⋅ Kd i⎤ ⎥
∑ ∑ ∑ ∑
2 3
⎢ ⎢ ⎣ ⎦⎥
⎣i = 1 i = 1 i = 1 ⎦ ⎣i = 1 ⎦

Using the Mathcad function lsolve gives

C := lsolve ( M , A) C1 := C1 C2 := C2 C3 := C3

−4 −7
C1 = 0.968 C2 = 4.199 × 10 C3 = −6.966 × 10

2
f ( T) := C1 + C2 ⋅ T + C3 ⋅ T f ( 200 ) = 1.024 f ( 700 ) = 0.921 f ( 900 ) = 0.782

The regress and loess functions in Mathcad can also be employed as shown below to
generate curve-fits to the given data .

i := 1 .. 12 vT i := Ti vKd i := Kd i

vs1 := regress ( vT , vKd , 2 ) vs2 := loess ( vT , vKd , 2 )

freg ( T) := interp ( vs1 , vT , vKd , T) floess ( T) := interp ( vs2 , vT , vKd , T)

Figure 6.25 shows a comparison of the various curve-fits generated with the given data points.

2
f ( T) := C1 + C2 ⋅ T + C3 ⋅ T T := 0 , 1 .. 1100 i := 1 .. 12
Chapter 6: Curve-Fitting 245

Curve-fits generated
1.2
Multiplying factor Kd

vKd i
1
f( T)
0.8
freg ( T) Given data points
Analysis-generated function
floess( T) 0.6
Mathcad regress
Mathcad loess
0.4
0 200 400 600 800 1000
vT i , T

Temperature, deg F

Figure 6.25. Effect of operating temperature on the strength of a mechanical element

6.10.4. Drop-Testing of Packaged Articles

An interesting parameter that often warrants investigation is the height from which a package can
be dropped before it suffers any accountable damage, which can be assessed from the maximum
acceleration imparted to the package at the end of the drop [ 20 ]. This is a question that arises
when packages must be properly cushioned before shipment to another location. The maximum
acceleration to gravity ratio, a , has been found to be a function of the ratio, h , of twice the
distance dropped to the static deflection of the package.

The following a versus h data is provided , using which a reasonable curve-fit is to be


generated .
h a h a
________________________
1 1.4 9 3.25
2 1.8 10 3.3
3 2 11 3.55
4 2.25 12 3.65
5 2.65 13 3.75
6 2.8 14 3.85
7 2.9 15 4.2
8 3.01 16 4.3
_______________________________
246 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

These data points are plotted in Figure 6.26.

n := 16 i := 1 , 2 .. 16 h1 := 1 hi+1 := hi + 1

a1 := 1.4 a2 := 1.8 a3 := 2. a4 := 2.25 a5 := 2.65 a6 := 2.8 a7 := 2.9 a8 := 3.01

a9 := 3.25 a10 := 3.3 a11 := 3.55 a12 := 3.65 a13 := 3.75 a14 := 3.85 a15 := 4.2 a16 := 4.3

We will generate a curve-fit of the form:

a = f(h) = C1 + C2 h + C3 h 2 + C 4 h 3

Here, n = 16 and m = 3 and the matrix equation to be solved is:

[M]{C} = {A},

Given data points


5
Given data points
(max acceleration/ gravity) ratio

ai 3

1
0 2 4 6 8 10 12 14 16
hi

(2h/ static deflection ) ratio

Figure 6.26. Given acceleration ratio versus height ratio data


Chapter 6: Curve-Fitting 247

where [M] is a known 4x4 matrix and {A} is a known column vector as shown below. The
column vector {C} , which involves the undetermined coefficients, is

⎛ C1 ⎞
⎜ ⎟
⎜ C2 ⎟
C=⎜ ⎟
⎜ C3 ⎟
⎜C ⎟
⎝ 4⎠
⎡⎢ n n n ⎤
(hi) (hi)3 ⎥⎥ ⎡⎢ ⎥⎤
n
∑ ∑ ∑
2


n hi
⎢ ai
⎢ ⎥
⎢ i = 1 i = 1 i = 1 ⎥ ⎢ i=1 ⎥
⎢ n ⎥
n n n ⎢ n ⎥
⎢ ⎥
∑ ∑ ( ) 2
∑ ( ) 3
∑ (hi)4 ⎥ ⎢
(hi ⋅ ai) ⎥



hi hi hi

⎢ i=1 i = 1 i = 1 i = 1 ⎥
M := ⎢
⎢ i=1 ⎥
⎥ A := ⎢ ⎥
⎢ n n n n

(hi)2 (hi) (hi) (hi)5 ⎥ ⎢ n ⎡ 2 ⎤⎥
∑ ∑ ∑ ∑
3 4

⎢i = 1 ⎥ ⎢ ∑ ⎣( hi) ⋅ ai⎦ ⎥
i = 1 i = 1 i = 1 ⎢i = 1 ⎥
⎢ ⎥ ⎢ ⎥
⎢ n n n n ⎥ ⎢ n ⎥
⎢∑ (hi)3 ∑ (hi)4 ∑ (hi)5 ∑ (hi)6 ⎥ ⎡( hi) 3 ⋅ ai⎤ ⎥
⎢i = 1
⎣ i = 1 i = 1 i = 1


⎢ ∑
⎢i = 1
⎣ ⎦

⎣ ⎦

⎛ 0.997 ⎞
⎜ 0.433 ⎟
C := lsolve ( M , A) C= ⎜ ⎟ C := C C2 := C2 C3 := C3 C4 := C4
⎜ −0.029 ⎟ 1 1
⎜ −4⎟
⎝ 9.472 × 10 ⎠

−4
C1 = 0.997 C2 = 0.433 C3 = −0.029 C4 = 9.472 × 10

2 3
f ( h) := C1 + C2 ⋅ h + C3 ⋅ h + C4 ⋅ h f ( 15) = 4.114 f ( 9 ) = 3.219

The Mathcad regress and loess functions also can be utilized as shown below to generate
curve-fits to the given data.
248 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

i := 1 , 2 .. 16

vhi := hi vai := ai vs1 := regress ( vh , va , 3 ) vs2 := loess ( vh , va , 2 )

freg ( h) := interp ( vs1 , vh , va , h) floess ( h) := interp ( vs2 , vh , va , h)

h := 1 , 1.1 .. 16 i := 1 , 2 .. 16

Figure 6.27 shows a comparison of the various curve-fits obtained with the given data points.

Curve-fits generated
5
Given data points
(max acceleration/gravity ) ratio

Analysis-generated fit
va i 4 Mathcad 's regress function
Mathcad's loess function
f( h )
3
freg ( h )

floess( h )
2

1
0 2 4 6 8 10 12 14 16
vhi , h

(2 h / static deflection ) ratio

Figure 6.27. Acceleration ratio versus height ratio curve-fit

PROBLEMS

In all problems, also obtain results with the Mathcad functions, slope, intercept, regress,
loess, interp, expfit, and pwrfit as appropriate and compare the results of your
regression analysis with the given data and results generated with Mathcad. Show this
comparison by means of Mathcad plots with proper labels, titles and traces.

6.1 Given the following data.

x: 1.3 3.2 4.6 6.1

y: 52 46 38 26

fit a straight line y= f(x)= C1 + C2 x to the above data. Determine y (5.1)


Chapter 6: Curve-Fitting 249

6.2. Given:

x: 0 1 2 3 4

y: 11.25 12.34 18.56 28.69 44.73

determine a least squares quadratic fit. Determine y ( 3.75 )

6.3. Generate a least squares fit of the form f(x) = AxB to the following data

x: 1 2 4 5

y: 2 10 60 110

Apply a weighting factor of 3 to the last data point and determine f(3.5)

6.4. The thermal conductivity, K, of a material varies with temperature, T, as follows

T ( deg Kelvin) 200 600 1000 1400

K (W/(cm-deg Kelvin) 1.0 0.40 0.30 0.25

Determine a least squares fit of the form K = ATB. Determine K for T = 1200 deg Kelvin. Obtain
an answer that is good to two decimal places.

6.5. For the data set given below, determine a power function that will serve as a least
squares fit.

x: 1 2 3 4 5 6 7 8
y: 0.43 0.71 0.94 1.07 1.19 1.27 1.32 1.47

Compute y( 4.5)

6.6. For the data set given below, find an exponential function of form :
y= f(x) = A exp(Bx) that will serve as a least squares fit.

x: 0 1 2 2.75 3.5

y: 2.5 6.75 15.42 28.86 52.03

Determine y( 3.0)
250 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

6.7. For the data set given below, find an exponential function of form :
y= f(x) = A exp(Bx) that will serve as a least squares fit.

x: 0 1 2 3 4

y: 2.01 1.37 0.83 0.55 0.33

Determine y(1.5)

6.8 For the experimental data given below

Strain, x, ( μ -in/in) 40 50 70 90

Displacement, y, (inches) 0.015 0.028 0.045 0.072

Determine a least squares fit of the form f(x)= Ax e (Bx). Determine f(45) and f(60)

6.9. For the strain ( e) / displacement (d) data shown below:

e: 43 57 68 84 99 115 (micro-in/in)

d: 0.009 0.017 0.029 0.043 0.067 0.097 (inches)

determine a least squares fit using an approximation of the form:

e= A x exp(-Bx)

Determine d(72)

6.10. The temperature of a a hot surface varies sinusoidally with time. The data which
is in terms of temperatures, T (deg C), measured at several time intervals , τ , where τ
is the number of hours from midnight, is given below.

τ (hours) 2 4 8

T (deg C) 80 120 105

Obtain a curve fit to the given data of the form

T = A sin ( πτ/12) + B cos ( πτ/12)

Determine T (6 hours).
Chapter 6: Curve-Fitting 251

6.11. Fit the data points given below with a function of the form y(x) = C1 f1 (x) + C2 f2 (x)
where f1 (x) = x and f2 (x) = x 3. Compute y(3.75).

x: 2.25 3.30 4.40

y: 9.75 15.60 28.27

6.12. The following data pertains to the temperatures of an object suspended in a hot
stream of air measured at several instants of time

t, Time (seconds) 0.0 400 1200

T, Temperature (deg C) 20 50 61.5

Curve-fit the data with a function of the form

( − 0.0033⋅ t)
f ( t) = A + B ⋅ e

and estimate the temperature attained by the object at time t = 600 seconds

6. 13. Fit a linear combination of known functions

f(x) = A + B x + C sin (πx) + D sin (3πx )

to
x = ( 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 )

y = ( 0.5541 2.1456 2.8021 2.5645 3.2458 2.9765 2.8521 2.4379 1.7872 0.1315 )

Determine y ( 0.85)

6.14. Given the 4 data points

x: 0 2.5 4.75 6.26

y: 0 12.45 22.67 36.89

obtain a curve-fit of the form: y = f(x) = C1 + C2 x + C3 x 2 + C 4 x 3 . Apply a


weighting factor of 2 to the last two data points. Determine y ( 3.5) .

6.15. Data defining the stress (S) versus strain (e) curve for an aluminum alloy is given below.
252 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Strain,e, ( %) Stress,S, (Kpsi)

10 63
20 63.6
40 62
60 60
80 58
100 56
120 52
140 48
150 47

Using an approximating polynomial of the form

2
S = C1 + C2 ⋅ e + C3 ⋅ e

obtain a least squares fit to the given data. Determine S(105%)

6. 16. The following data relating the distance traveled by an object to time was obtained from a
test in an experimental test track . The distance traveled, S, is in meters and the time , t, is in
seconds.

Time, t (secs) 0.1 0.5 1.5 3.0

Distance, S (m) 0.26 1.56 7.41 30.90

Generate a curve-fit of the form S(t) = A + B t + C t 2 . Compute S for t= 2 seconds.

6.17. The pressure drag , D P, on an object can be reduced in comparison with the total
drag , DT, by streamlining it , that is, making its length, L, in the direction of flow larger with
respect to its maximum thickness or diameter, D . [ 17 ] . Obtain an appropriate curve-fit to
the following data which is provided to demonstrate the effect of streamlining on the pressure
drag of a body of symmetrical airfoil cross section.

L
= ( 1 2 3 4 5 6 7 8 9 10 11 )
D

DP
= ( 1.25 0.65 0.42 0.25 0.20 0.10 0.08 0.06 0.04 0.02 0.01 )
DT

Determine D P / DT for L / D = 5.5

6.18. The following data , in which the time, t, is given in seconds provides the
Chapter 6: Curve-Fitting 253

response c ( t ) of a control system to a step input.

t := ( 0 0.75 1.50 2 3 4 5 6 )

c ( t) := ( 0 1.30 1.40 1.10 0.85 1.02 1.01 1.009 )

Obtain a least-squares fit of the form.

c(t)= A+ B e -t sin (2.828 t ) + C e -t cos ( 2.828 t )

and determine c at t = 3.5 seconds.

6.19. The Brinell hardness number, Bhn , for steel [ 18 ] is given as a function of the
tempering temperature, T, in the following table

T, deg F. 400 600 800 1000 1200

Bhn 540 500 445 375 310

Obtain an appropriate curve-fit to the given data and determine the Brinell hardness
number for T = 850 deg F.

6.20. The average strain rate used in obtaining stress/ strain data for a material
specimen is about 0.001 in / (in-sec ). However, when the strain rate, SR, is
increased, as it happens, under conditions of impact loading, the strength of the
material also increases. [ 18 ] The following data shows the influence of the
increase in strain rate on the yield strength, SYLD, of a steel specimen

(
SR = 10
−6
10
−5
10
−4
10
−2
1 10
2
10
3 ) in/(in-sec)

SYLD = ( 30 28 30 37 45 65 78 ) kpsi

Obtain an appropriate least-squares fit to the given data and determine SYLD
corresponding to an SR of 50.

6.21. The endurance limit of a steel specimen is, typically, a function of the condition
of its surface [ 19 ]. For an unnotched, ground specimen in reversed bending,
the endurance limit, S end , is related to its tensile strength, S ult , as given in the
following table of data.
Sult = ( 60 80 100 120 140 160 180 200 220 ) kpsi

Send = ( 25 35 45 55 65 72 80 85 90 ) kpsi
254 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Obtain a reasonable curve-fit to the given data. Determine S end corresponding to S ult = 135
kpsi .

6.22. Expected voice levels required for speech communication with various back-
ground noise levels and separations between the speaker and the listener [ 10 ]
are given in the following table of data .

Obtain appropriate curve-fits to the given data in the form of separate curves for
(a) Peak shouting (b) Shouting (c) Very loud voice (d) Raised voice and (e)
Normal voice.

Determine the voice levels required in the following situations.

(1) Speaker listener distance = 2.5 m , background noise level = 68 dBA

(2) Speaker listener distance = 4.5 m , background noise level = 86 dBA

(3) Speaker listener distance = 8.5 m , background noise level = 45 dBA

(4) Speaker listener distance = 6.5 m , background noise level = 65 dBA

BACKGROUND NOISE LEVELS (dBA)

Speaker Normal Raised Very Shouting Peak


/listener Voice Voice Loud Shouting
distance Voice
(m)

⎛ 0.25 ⎞ ⎛ 90 ⎞ ⎛⎜ ⎞⎟ ⎛ 95 ⎞ ⎛ 100 ⎞ ⎛ 118 ⎞


⎜ 0.5 ⎟ ⎜ 80 ⎟ ⎜ 88 ⎟ ⎜ 92 ⎟ ⎜ 112 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 1.0 ⎟ ⎜ 72 ⎟ ⎜ ⎟ ⎜ 78 ⎟ ⎜ 84 ⎟ ⎜ 103 ⎟
⎜ 2 ⎟ ⎜ 63 ⎟ ⎜ ⎟ ⎜ 72 ⎟ ⎜ 77 ⎟ ⎜ 96 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 3 ⎟ ⎜ 57 ⎟ ⎜ 62 ⎟ ⎜ 68 ⎟ ⎜ 74 ⎟ ⎜ 92.5 ⎟
⎜ 4 ⎟ ⎜ 54.5 ⎟ ⎜ ⎟ ⎜ 66 ⎟ ⎜ 72 ⎟ ⎜ 90 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ 60 ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 5 ⎟ ⎜ 52 ⎟ ⎜ 57 ⎟ ⎜ 64 ⎟ ⎜ 68 ⎟ ⎜ 88 ⎟
⎜ 6 ⎟ ⎜ 50 ⎟ ⎜ ⎟ ⎜ 62.5 ⎟ ⎜ 67.5 ⎟ ⎜ 86.5 ⎟
⎜ 7 ⎟ ⎜ 49.5 ⎟ ⎜ 56.5 ⎟ ⎜ 61.5 ⎟ ⎜ 66 ⎟ ⎜ 86 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ 55 ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 8 ⎟ ⎜ 49 ⎟ ⎜ ⎟ ⎜ 60 ⎟ ⎜ 65 ⎟ ⎜ 85 ⎟
⎜ 9 ⎟ ⎜ 48 ⎟ ⎜ 54 ⎟ ⎜ 58 ⎟ ⎜ 64.5 ⎟ ⎜ 84.5 ⎟
⎜ 53.5 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ 52.5 ⎟ ⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝ 9.5 ⎠ ⎝ 47.5 ⎠ ⎝ ⎠ ⎝ 57.5 ⎠ ⎝ 64 ⎠ ⎝ 84 ⎠
Chapter 7: Numerical Differentiation 255

CHAPTER 7

NUMERICAL DIFFERENTIATION
7.1 INTRODUCTION TO NUMERICAL DIFFERENTIATION AND THE
USE OF THE MATHCAD DERIVATIVE OPERATORS

In the process of generating a solution to a science or an engineering problem, the need to


come up with a numerical estimate of a derivative is often encountered. We will discuss two
methods of computing derivatives as follows

1. Method of finite differences


2. Interpolating polynomial method.

These methods will be discussed in detail in the paragraphs following.


To compute the derivative of a function using the Mathcad derivative operator, first define the
point x at which the derivative is to be computed . Then click on the derivative operator in the
calculus palette of Figure 7.1 and fill in the placeholders as required. Press the equals sign
to see the derivative.

7.2 METHOD OF FINITE DIFFERENCES

In traditional calculus , a derivative of a function f(x) is defined as

d f ( x + Δx ) − f ( x )
f ( x) = lim (7.1)
dx Δx → 0 Δx

In numerical methods, taking the above limit as Δx goes to zero is not possible , and, although, Δx
can be very small, it certainly cannot be zero. The process by which this increment Δx is made
small and utilized in calculations paves the way for the method of finite differences.

If the dependent variable x is incremented by Δx, as shown in Figure 7.2 , the difference between
the function value at (x+Δx ) and that at x is called a forward difference . In this case, the derivative
of the function f(x) at x is

d f ( x + Δx ) − f ( x )
f ( x) = (7.2)
dx Δx

However, if the dependent variable is incremented by - Δx , the difference between the function
values is a backward difference, and the derivative , in this case is

d f ( x ) − f ( x − Δx )
f ( x) = (7.3)
dx Δx
256 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure 7.1. Use of the Mathcad derivative operators


Chapter 7: Numerical Differentiation 257

Figure 7.2. Finite differences

If a two-step difference is employed, the method of computing derivatives is called the two-step
method. An estimate of the derivative, in this case, is

d f ( x + Δx ) − f ( x − Δx )
f ( x) = (7.4)
dx 2 ⋅ Δx

For obtaining an estimate of the second derivative of f(x), we will consider the following Taylor
series expansions.

2 2
d Δx d
f ( x + Δx ) = f ( x ) + ( Δx ) ⋅ f (x) + ⋅ ( f ( x ) + .................................................................
(7.5)
dx 2! 2
dx

2 2
d Δx d
f ( x − Δx ) = f ( x ) − ( Δx ) ⋅ f (x) + ⋅ ( f ( x ) + .............................................................
(7.6)
dx 2! 2
dx

Adding Equations (7.5) and (7.6) with terms beyond the second derivative terms ignored
will give an estimate of the second derivative as

2 1
d
(f (x) = ⋅ ( f ( x + Δx ) − 2 ⋅ f ( x ) + f ( x − Δx ) ) (7.7)
2 2
dx Δx

By following a similar procedure, the third and fourth derivatives can also be estimated as
258 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

3 1
d
f ( x) = ⋅ ( f ( x + 2 ⋅ Δx ) − 2 ⋅ f ( x + Δx ) + 2 ⋅ f ( x − Δx ) − f ( x − 2 ⋅ Δx ) ) (7.8)
3 3
dx Δx

4 1
d
f ( x) = ⋅ ( f ( x + 2 ⋅ Δx) − 4 ⋅ f ( x + Δx) + 6 ⋅ f ( x ) − 4 ⋅ f ( x − Δx) + f ( x − 2 ⋅ Δx)(7.9)
)
4 4
dx Δx

Example 7.1.

Using the data given, compute the first derivative at x= 4 using the forward, backward as well
as the two-step finite difference approximations. Also compute the second derivative at x= 4
.

x 2 3 4 5 6

f(x) 0.703 1.212 1.456 1.723 1.853

The first derivative at x=4 using Equation (7.2) , which is the forward difference approximation is

1.723 − 1.456
fprimefwd := fprimefwd = 0.267
5−4

The first derivative at x=4 using Equation (7.3) , which is the backward difference
approximation, is

1.456 − 1.212
fprimebckwd := fprimebckwd = 0.244
4−3

The first derivative at x=4 using Equation (7.4) , which is the two-step difference
approximation, is

1.723 − 1.212
fprimetwostep := fprimetwostep = 0.2555
5−3

The second, third and fourth derivatives at x =4 are given by Equations (7.7), (7.8) and (7.9),
and these are

1
Δx := 1 fdblprime := ⋅ ( 1.723 − 2 ⋅ 1.456 + 1.212 ) fdblprime = 0.023
2
Δx

1
ftripleprime := ⋅ ( 1.853 − 2 ⋅ 1.723 + 2 ⋅ 1.212 − 0.703 ) ftripleprime = 0.128
3
Δx
Chapter 7: Numerical Differentiation 259

1
ffourprime := ⋅ ( 1.853 − 4 ⋅ 1.723 + 6 ⋅ 1.456 − 4 ⋅ 1.212 + 0.703 ) ffourprime = −0.448
4
Δx

7.3. INTERPOLATING POLYNOMIAL METHOD

In the interpolating polynomial method, an interpolating polynomial passing through given


data points is determined by any of the methods discussed in Chapter 5. Then, the
polynomial generated can be differentiated as is done in traditional calculus. If the polynomial
derived , for instance, is

2 3 n
f ( x ) = a0 + a1 ⋅ x + a2 ⋅ x + a3 ⋅ x + ............ + an ⋅ x (7.10)

then, its derivative can be obtained as

d 2 n− 1
f ( x ) = a1 + 2 ⋅ a2 ⋅ x + 3 ⋅ a3 ⋅ x + .............. + n ⋅ an ⋅ x (7.11)
dx

Example 7.2

Given the following data :

x 2 3 4 5 6

y 0.80354 1.23591 1.62312 1.89765 1.96789

Use the Method of Undetermined Coefficients to fit an interpolating polynomial to the given
data and estimate: yprime(4.5), yprime(2.7), ydblprime(4.5) and ydblprime(2.7).

Since 5 data points are given, we can generate a 4 th order polynomial.

Let y(x) = f(x)= a + bx + cx2 + dx 3 + e x4

Substituting the given values of (x,y) in the assumed polynomial above yields the following
matrix equation:

[M]{A}= {V}

where
260 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛1 2 3 4 ⎞
⎜ 2 2 2 2 ⎟ ⎛⎜ 0.80354 ⎞⎟
⎜ 2 3 4⎟
⎜1 3 3 3 3 ⎟ ⎜ 1.23591 ⎟
M := ⎜ 1 2 3 4⎟ ⎜
V := 1.62312

⎜ 4 4 4 4 ⎟ ⎜ ⎟
⎜ 2 3 4⎟ ⎜ 1.89765 ⎟
⎜1 5 5 5 5
⎟ ⎜ 1.96789 ⎟
⎝ ⎠
⎜ 2 3 4⎟
⎝1 6 6 6 6 ⎠

Using the Mathcad function lsolve, the unknown coefficients in the column vector {A} can be
found

⎛ −0.04705 ⎞
⎜ ⎟
⎜ 0.40726 ⎟
⎜ −3 ⎟
A := lsolve ( M , V ) A = ⎜ 7.43375 × 10 ⎟
⎜ −3 ⎟
⎜ 2.79917 × 10 ⎟
⎜ −3⎟
⎝ −1.00375 × 10 ⎠

The coefficients a, b, c, and d of the assumed polynomial are

−3 −3
a := A0 a = −0.04705 c := A2 c = 7.43375 × 10 d := A3 d = 2.79917 × 10

−3
b := A1 b = 0.40726 e := A4 e = −1.00375 × 10

The polynomial derived is, then,

2 3 4
y ( x ) := a + b ⋅ x + c ⋅ x + d ⋅ x + e ⋅ x

where a, b, c,d and e are as given above and with first and second derivatives as follows

First derivative:

2 3
yprime ( x ) := b + 2 ⋅ c ⋅ x + 3 ⋅ d ⋅ x + 4 ⋅ e ⋅ x yprime ( 4.5 ) = 0.27835 yprime ( 2.7 ) = 0.42959

Second derivative:

2
ydblprime ( x ) := 2 ⋅ c + 6 ⋅ d ⋅ x + 12 ⋅ e ⋅ x ydblprime ( 4.5 ) = −0.15347 ydblprime ( 2.7 ) = −0.02759
Chapter 7: Numerical Differentiation 261

Use of the Mathcad derivative operators yields:

2
d d
x := 4.5 y ( x ) = 0.27835 ( y ( x ) ) = −0.15347
dx 2
dx

2
d d
x := 2.7 y ( x ) = 0.42959 ( y ( x ) ) = −0.02759
dx 2
dx

Notice that the Mathcad derivative operators have yielded the same results as those
obtained through the manual process of differentiation. These are presented in Figures 7.3
and 7.4.

x := 2.0 , 2.1 .. 6.0

A comparison of results
0.6
First derivative values

yprime( x) 0.4

d 0.2
y ( x)
dx
0 First deriv/ inter poly
First deriv/Mathcad
− 0.2
2 3 4 5 6
x
X-Values

Figure 7.3 Graph of first derivative

A comparison of results
0.1
Second derivative values

0
ydblprime( x)
− 0.1
2
d
( y ( x) ) − 0.2
2
dx
Second deriv/Inter poly
− 0.3
Second deriv/ Mathcad
− 0.4
2 3 4 5 6
x
X-Values

Figure 7.4. Graph of second derivative


262 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The interpolating polynomial can also be generated by using the Mathcad function interp with
lspline, pspline or cspline, and then numerically differentiated by using the Mathcad derivative
operator, as shown below.

j := 0 .. 4 Vyj := V j

⎛⎜ 0.80354 ⎞⎟
⎛⎜ 2 ⎞⎟
⎜ 1.23591 ⎟
⎜3 ⎟ ⎜ ⎟ <-- Generate the Vx and Vy vectors for given
⎜ ⎟ Vy = 1.62312
Vx := 4 ⎜ ⎟ data
⎜ ⎟ ⎜ 1.89765 ⎟
⎜5 ⎟ ⎜ 1.96789 ⎟
⎜6 ⎟ ⎝ ⎠
⎝ ⎠

Vs := cspline ( Vx , Vy) fspline ( x ) := interp ( Vs , Vx , Vy , x ) <--- interpolating


polynomial
generated

2
d d
x := 4.5 fspline ( x ) = 0.2786 ( fspline ( x ) ) = −0.15547
dx 2
dx

2
d d
x := 2.7 fspline ( x ) = 0.43082 ( fspline ( x ) ) = −0.02671
dx 2
dx

The Mathcad results appear to be in reasonable agreement with those of the Undetermined
Coefficients method.

7.4. APPLICATIONS IN NUMERICAL DIFFERENTIATION

The following examples illustrate situations where numerical differentiation can be applied
and used to advantage in generating realistic solutions to practical problems.

7.4.1. Determination of Velocities and Accelerations from Given Displacement


Data

The following position versus time data for a train has been experimentally obtained.

Time t (secs) 0 10 20 30 35 40
Position s ( m) 0 75 175 360 480 600
Chapter 7: Numerical Differentiation 263

Determine the velocity and acceleration of the train as functions of time. Estimate the
velocity and acceleration of the train at t = 32 seconds

We first use the method of undetermined coefficients to fit an interpolating polynomial to


the given data. Since six data points are given, a fifth order polynomial of the following
form will be generated.

s(t) = f(t)= a + bt + ct 2 + dt3 + e t4 + f t 5

Substituting the given values of (t,s) in the assumed polynomial above yields the following
matrix equation:

[M]{A} = {V}

where

⎛⎜ 1 0
2
0 0
3
0
4
0
5 ⎞⎟
⎛ 0 ⎞
⎜ 2 3 4 5⎟ ⎜ ⎟
⎜1 10 10 10 10 10 ⎟ ⎜ 75 ⎟
⎜ 2 3 4 5⎟ ⎜ 175 ⎟
M :=
⎜1 20 20 20 20 20 ⎟ V := ⎜ ⎟
⎜ 2 3 4 5⎟ ⎜ 360 ⎟
⎜1 30 30 30 30 30 ⎟ ⎜ 480 ⎟
⎜ 2 3 4 5⎟ ⎜ ⎟
⎜1 35 35 35 35 35
⎟ ⎝ 600 ⎠
⎜ 2 3 4 5⎟
⎝1 40 40 40 40 40 ⎠

Using the Mathcad function lsolve, the unknown coefficients in the column vector {A} can be found

⎛ 0 ⎞
⎜ ⎟
⎜ 9.41429 ⎟
⎜ −0.36131 ⎟
A := lsolve ( M , V )
A=
⎜ ⎟
⎜ 0.01667 ⎟
⎜ −5 ⎟
⎜ 7.7381 × 10 ⎟
⎜ −6⎟
⎝ −4.52381 × 10 ⎠
264 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The coefficients a, b, c, d. e and f of the assumed polynomial are

a := A0 a= 0 c := A2 c = −0.36131 d := A3 d = 0.01667

−5 −6
b := A1 b = 9.41429 e := A4 e = 7.7381 × 10 f := A5 f = −4.52381 × 10

The polynomial derived is, then,

2 3 4 5
s ( t) := a + b ⋅ t + c ⋅ t + d ⋅ t + e ⋅ t + f ⋅ t

with first and second derivatives as follows

First derivative:

2 3 4
velocity ( t ) := b + 2 ⋅ c ⋅ t + 3 ⋅ d ⋅ t + 4 ⋅ e ⋅ t + 5 ⋅ f ⋅ t velocity ( 32 ) = 23.91516 (m/sec)

Second derivative:

2 3
acceler ( t ) := 2 ⋅ c + 6 ⋅ d ⋅ t + 12 ⋅ e ⋅ t + 20 ⋅ f ⋅ t acceler ( 32 ) = 0.46351 (m/sec2 )

Use of the Mathcad derivative operators yields:

t := 32
2
d d
s ( t) = 23.91516 m/sec ( s ( t) ) = 0.46351 (m/sec2 )
dt 2
dt

As is expected, the results of the Mathcad derivative operators are the same as those
obtained through the process of manual differentiation of the interpolating polynomial .
They are presented in Figure 7.5.
Chapter 7: Numerical Differentiation 265

t := 0 , 1 .. 40

Position versus time


600
Position, meters

400
s( t )
200

0
0 10 20 30 40
t
Time , secs

Velocity versus time


25
Interpolating polynomial method
Velocity, m/sec

velocity ( t ) 20 Mathcad's derivative operator

d 15
s( t )
dt
10

5
0 10 20 30 40
t
Time, secs

Acceleration versus time


1
Acceleration, m/sec2

0.5
acceler ( t )
0
2
d
2
s( t ) − 0.5
dt Interpolating polynomial method
−1
Mathcad's derivative operator
− 1.5
0 10 20 30 40
t
Time, sec
Figure 7.5. Position, velocity and acceleration versus time
266 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

7.4.2. Determination of Shock Absorber Parameters , and Damper and Spring


Restoring Forces from Given Vehicle Displacement Data

The following displacement (x) versus time (t) data is provided for a 200 kg motorcycle with a
shock absorber .

t,sec 0 0.5 1.0 1.5 2.0 2.5


x,mm 0 0.23 0 -0.075 0.025 0

Generate an interpolating polynomial for the displacement data given and obtain velocity and
acceleration profiles using Mathcad. Determine (a) the stiffness and the damping constant
of the shock absorber and (b) the damper and spring restoring forces of the shock absorber
as functions of time .

The given data is plotted in Figure 7.6 below.

i := 1 , 2 .. 6

t1 := 0 ti+ 1 := ti + 0.5

x 1 := 0 x 2 := 0.23 x 3 := 0 x 4 := −0.075 x 5 := 0.025 x 6 := 0

Vxi := x i Vti := ti

Given data

0.2
Displacement, mm

Vxi 0.1

Given data
0.1
0 0.5 1 1.5 2 2.5
Vt i

Time, sec

Figure 7.6. Given data


Chapter 7: Numerical Differentiation 267

As done in the preceding problem, we first use the method of undetermined coefficients to fit an
interpolating polynomial to the given data. Since six data points are prescribed , a fifth order
polynomial of the following form can be generated.

x(t) = f(t)= a + bt + ct2 + dt3 + e t4 + f t 5

Substituting the given values of (t,x) in the assumed polynomial above yields the following matrix
equation:

[M]{A}= {V}

where

⎛⎜ 1 0 02 03 04 05 ⎞⎟
⎜ 5⎟
⎛ 0 ⎞
2 3 4 ⎜ ⎟
⎜ 1 0.5 0.5 0.5 0.5 0.5 ⎟ ⎜ 0.23 ⎟
⎜ 5 ⎟
2 3 4 ⎜ 0 ⎟
⎜ 1 1 1 1 1 1 ⎟
V := ⎜
M := ⎟
⎜ 5⎟
2 3 4 ⎜ −0.075 ⎟
⎜ 1 1.5 1.5 1.5 1.5 1.5 ⎟ ⎜ 0.025 ⎟
⎜ 5 ⎟
2 3 4
⎜ ⎟
⎜1 2 2 2 2 2
⎟ ⎝ 0 ⎠
⎜ 2 3 4 5⎟
⎝ 1 2.5 2.5 2.5 2.5 2.5 ⎠

⎛ 0 ⎞
⎜ ⎟
⎜ 1.7375 ⎟
⎜ −3.69917 ⎟
A := lsolve ( M , V) A=⎜ ⎟
⎜ 2.65167 ⎟
⎜ −0.76333 ⎟
⎜ ⎟
⎝ 0.07333 ⎠

The coefficients a, b, c, d. e and f of the assumed polynomial are

a := A1 a= 0 c := A3 c = −3.69917 d := A4 d = 2.65167

b := A2 b = 1.737 e := A5 e = −0.76333 f := A6 f = 0.073


268 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The interpolated polynomial which is

2 3 4 5
x ( t) := a + b ⋅ t + c ⋅ t + d ⋅ t + e ⋅ t + f ⋅ t

is plotted in Figure 7.7 below. As follows, Mathcad's cspline function can also be used
with interp to generate a cubic spline interpolation which is compared with the
undetermined coefficients method and with the given data .

i := 1 , 2 .. 6
⎛ 0 ⎞ ⎛ 0 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.5 ⎟ ⎜ 0.23 ⎟
Vs := cspline ( Vt , Vx)
⎜ 1 ⎟ ⎜ 0 ⎟
Vt = ⎜ ⎟ Vx = ⎜ ⎟
⎜ 1.5 ⎟ ⎜ −0.075 ⎟
⎜ 2 ⎟ ⎜ 0.025 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 2.5 ⎠ ⎝ 0 ⎠

fspline ( t ) := interp ( Vs , Vt , Vx , t )

t := 0 , 0.01 .. 2.5

Motorcycle displacement profile

Given data points


Undetermined coefficients method
0.2 Mathcad's cubic spline interpolation
Displacement, mm

Vxi

x( t) 0.1
fspline ( t)
0

0 0.5 1 1.5 2 2.5


Vt i , t

Time, seconds

Figure 7.7. Interpolating polynomial generated


Chapter 7: Numerical Differentiation 269

Using Mathcad's derivative operators, the velocity and the acceleration of the vehicle are also
plotted as functions of time in Figure 7.8.

0
x( t)

d 2
x( t)
dt

2
d 4
x( t)
2
dt
6 Displacement, mm
Velocity, mm/sec
Acceleration, mm/sec2
8
0 0.5 1 1.5 2 2.5
t

Figure 7.8. Displacement, velocity and acceleration profiles

The system settling time is the time taken for the free vibration of the motorcycle to
completely die down. This , in this case, is 2.5 seconds. Since the settling time is
approximately equal to four time constants for a 2 % error, the system time constant τ
can be computed.

2.5
τ := τ = 0.625 seconds
4

The displacement profile also indicates a natural period, τ d , of one oscillation to be


approximately two seconds

τ d := 2 seconds

yielding the natural frequency of the system as

π
ω d := 2 ⋅ ω d = 3.142 rad/sec
τd
270 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

As shown below , the damping, c , and stiffness, k , furnished by the shock absorber
can be computed using the pertinent formulas available in textbooks in Vibrations and
Controls [ 6, 9, 14, 16, 20, 21].

Damping factor x undamped natural frequency = 1/ system time constant :

1
ζωn := ζωn = 1.6
τ

Since ζωn and ω d are known , the damping factor ζ can be calculated.

2
ωd = ωn⋅ 1 − ζ

2
π ζωn
ω d := 2 ⋅ ω d = 3.142 rad/sec ζ := ζ = 0.454
τd 2 2
ω d + ζωn

The undamped natural frequency of the system along with the stiffness and damping coefficients
of the shock absorber can now be computed using

ζωn
ω n := ω n = 3.526 rad/sec
ζ

The stiffness and damping coefficients of the shock absorber can now be determined.

m := 200 kg

2 3
k := m ⋅ ω n k = 2.486 × 10 N/m

c := 2 ⋅ m ⋅ ω n ⋅ ζ c = 640 N-sec/ m

The shock absorber spring and damper restoring forces which can now be calculated as functions
of time using
Chapter 7: Numerical Differentiation 271

x ( t)
Fspring ( t ) := k ⋅
1000

d x ( t)
Fdamper ( t ) := c ⋅
dt 1000

are plotted in Figure 7.10.

t := 0 , 0.1 .. 2.5

Shock absorber spring/damp rest forces


1.5

1
Force, Newtons

Fdamper ( t)
0.5
Fspring( t)

0.5
0 0.5 1 1.5 2 2.5
t
Time, seconds

Figure 7.9. Shock absorber spring and damper restoring forces

PROBLEMS

7.1. Given the following function

θ = ( 4 , 5 , 6 , 7 , 8)

Cos ( θ ) = ( 0.99756 , 0.99619 , 0.99452 , 0.99252 , 0.99027 )

determine the first derivative of cos(θ ) at θ = 7 degrees, using forward, backward and two-step finite
difference approximations. Compare the results with the true value.
272 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Also obtain an answer using cspline/interp and the Mathcad derivative operator. Obtain the
Mathcad interpolated function and its derivative in the given range and graph them with proper
labels and titles.

7.2. Given the following

x 1.0 1.2 1.4 1.6 1.8 2.0

f(x) 1.1 0.9283 0.8972 0.9015 0.9422 1.1

compute first and second derivatives at x= 1.4, using finite differences . Also obtain
answers using cspline/interp and the Mathcad derivative operators. Obtain the Mathcad
interpolated function and its first and second derivatives in the given range and graph them
with proper labels and titles.

7.3. Given f(x) versus x as follows

x 1 2 3 4 5

f(x) 5.25 155.2 458.1 1682 6489

fit an interpolating polynomial using the method of undetermined coefficients and estimate
the first derivative at x=3.5 and x=4.5. Also obtain answers using cspline/interp and the
Mathcad derivative operator. Graph the polynomials obtained in the given range . Show a
comparison of the first derivative obtained by the undetermined coefficient method with that
generated using Mathcad.

7.4. Fit an interpolating polynomial to the data of Problem 7.2.


Estimate first and second derivatives at x= 1.4. Also obtain answers using cspline/interp
and the Mathcad derivative operators. Graph the polynomials obtained in the given range .
Show a comparison of the first derivative and second derivative obtained by the
undetermined coefficient method with those generated using Mathcad.

7.5. The horizontal and vertical positions of a weather balloon with respect to a station
are furnished below at various instants of time.
Chapter 7: Numerical Differentiation 273

t horiz distance x vertical distance, y


(seconds) (ft) (ft)

⎛0 ⎞ ⎛ 0 ⎞
⎛0 ⎞ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ ⎜8 ⎟ ⎜ 6.4 ⎟
⎜1 ⎟ ⎜ 16 ⎟ ⎜ 25.6 ⎟
⎜2 ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 24 ⎟ ⎜ 57.6 ⎟
⎜3 ⎟ ⎜ 32 ⎟ ⎜ 102.4 ⎟
⎜4 ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ ⎜ 40 ⎟ ⎜ 160 ⎟
⎜5 ⎟ ⎜ 48 ⎟ ⎜ 230.4 ⎟
⎜6 ⎟ ⎜ ⎟ ⎜ ⎟
⎜ ⎟ ⎝ 54 ⎠ ⎝ 291.6 ⎠
⎝7 ⎠

Using the method of undetermined coefficients, fit interpolating polynomials to the given data
and generate x versus time and y versus time curves. Estimate horizontal and vertical
velocities as well as horizontal and vertical accelerations at t = 4.5 seconds. Also obtain
answers using cspline/interp and the Mathcad derivative operators. Show a comparison of
all results generated with the help of appropriate plots.

7.6. The deflection curve of a cantilevered beam is given by the following data, in which x
is the distance in feet from the fixed end and y is the deflection in feet at x .

x = (0 1 2 3 4 5 6 7 8 )

y = ( 0 0.015 0.05 0.1 0.2 0.3 0.4 0.5 0.6 )

Using Mathcad's cspline/interp functions, generate an interpolating polynomial for the given
data , and obtain the beam slope at x = 5.5 ft and x= 6.5 ft. Also plot the deflection versus x
and slope versus x curves

7.7. In a slider-crank mechanism , the crank angle θ and the position x of the slider
are prescribed below at various instants of time. The position x is in feet while the angle θ is
in radians

θ := ( 0 0.393 0.785 1.178 1.571 1.963 2.356 2.749 3.142 )

x := ( 0.667 0.65 0.604 0.539 0.471 0.412 0.368 0.342 0.333 )


274 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure P 7.7

Using cspline/interp, generate an interpolating polynomial for the given data. If the
rotational speed of the crank is ω 2 = 1000 RPM, counterclockwise, and it is maintained
constant , estimate the velocity and acceleration of the slider at θ = π/2 and 3π/4 radians.
Plot the position x, velocity dx/dt and acceleration d2 x/ dt 2 as functions of the crank
angle θ.

7,8 The data in the following table are the distances (D) traveled by a motorcycle for
selected times (t)

t(seconds): 0 2 4 6 8 10

D (ft): 0 10 50 150 330 610

(a) Determine the velocity and acceleration of the motorcycle at t = 4 , 6 and 8


seconds, using finite differences.

(b) Using only three relevant data points, determine the velocity and acceleration at t = 6
seconds by the Interpolating Polynomial method. Compare these results with those of Part
(a).
.
7.9. The deflection curve of a cantilevered beam is given by the following data, in which x is
the distance in feet from the fixed end and y is the deflection at x.

x: 1 4 8

y: 0.015 0.20 0.60

Using the Interpolating Polynomial method, compute the first and second derivatives of y(x) at
x= 3 ft and x = 6 ft.

7.10. The displacements of an instrument subjected to a random vibration test are given below
.
Time, t (seconds): 0.05 0.10 0.15 0.20 0.25 0.30

Displacement, y (in): 0.144 0.172 0.213 0.296 0.070 0.085


Chapter 7: Numerical Differentiation 275

Using finite differences, determine the velocity and acceleration of the instrument at t= 0.15,
0.20 and 0.25 seconds.
276 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

NOTES:
Chapter 8: Numerical Integration 277

CHAPTER 8

NUMERICAL INTEGRATION
8.1 INTRODUCTION TO NUMERICAL INTEGRATION AND THE USE
OF THE MATHCAD INTEGRAL OPERATORS

In traditional calculus, an integral of a function f(x) between limits a and b is the shaded
area shown in Figure 8.1 and is defined as

b

I=⎮ f ( x ) dx (8.1)

a

Figure 8.1. Integral of a function

This analytical solution is possible only when f(x) is explicitly known. When the evaluation of
this integral becomes either impossible or extremely difficult , it is advantageous to resort to
numerical techniques.

In many science and engineering problems, data is often presented as a given bunch of
numbers for given values of the independent variable. In this case too, the evaluation of the
integral, is done by a numerical process.

There are several methods available for evaluating an integral numerically. However, only the
following will be discussed here.

1. The interpolating polynomial method.


278 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

2. Trapezoidal rule.
3. Simpson's rules.
4. Romberg integration.

To compute the definite integral of a function usiong the Mathcad integral operator, click on
the integral operator in the calculus palette shown in Figure 8.2 . Then, start filling in the
placeholders as required and press the equals sign to see the result. Mathcad resorts to
Romberg integration to approximate the integral of an expression over an interval of real
numbers.

Figure 8.2. Use of the Mathcad Integral Operator


Chapter 8: Numerical Integration 279

8.2 THE INTERPOLATING POLYNOMIAL METHOD

In this method, an interpolating polynomial passing through the data points is generated as
was done in the chapter on numerical differentiation and then the polynomial is integrated
anaytically over the prescribed limits of integration. If the polynomial generated is of the form

2 3
y ( x ) = a + bx + c ⋅ x + d ⋅ x + .......... , (8.2)

its integral is

2 3 4
x x x
I = a⋅x + b⋅ + c⋅ + d⋅ + ............ (8.3)
2 3 4

which can be evaluated analytically over any prescribed integration limits.

Example 8.1.

Given the data of Example 7.2 of Chapter 7, which is

x 2 3 4 5 6

y(x) 0.80354 1.23591 1.62312 1.89765 1.96789

estimate the integral of y(x) over the range x = 2 to x = 6.

The interpolating polynomial generated in Example 7.2 for the data given was

2 3 4
y (x) = a + b ⋅ x + c ⋅ x + d ⋅ x + e ⋅ x

−3 −3 −3
with a := −0.04705 b := 0.407 c := 7.43375 ⋅ 10 d := 2.79917 ⋅ 10 e := −1.00375 ⋅ 10

2 3 4
y ( x ) := a + b ⋅ x + c ⋅ x + d ⋅ x + e ⋅ x

Then, the required integral is

6

⎮ y ( x ) dx = 6.1803

2
280 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

8.3 TRAPEZOIDAL RULE

Figure 8.3. The trapezoidal rule

The trapezoidal rule estimates the integral of a function on the basis of linear interpolation
between the given data points . If the integral of a given function f(x) is to be evaluated
between limits a and b , the interval from a to b is divided into n equal subintervals each of
width H, where

b− a
H= (8.4)
n

The desired integral which is, in fact, the sum of the areas in the subintervals under the
curve f(x) is then ( See Figure 8.3. )

n
( f ( x i+ 1) + f ( x i) )
1
I=
2
⋅H ⋅
∑ (8.5)
i = 1

where the (x i , y i ) are the x and y- coordinates of the given data points at the beginning and
end of each subinterval. An alternative form of the above integral is

m⋅− 1
( f ( x i+ 1) + f ( x i)
1
I=
2
⋅h⋅
∑ (8.6)
i = 1
Chapter 8: Numerical Integration 281

where m , now, is the number of data points given on the curve and h is, accordingly,

b−a
h= (8.7)
m−1

For example, if five data points are prescribed as shown in Figure 8.4, there will be four
segments and the trapezoidal rule will estimate the integral of the function from the first to the
last data point as

( )
1
I= ⋅ h ⋅ y1 + 2 ⋅ y2 + 2 ⋅ y3 + 2 ⋅ y4 + y5 (8.8)
2

Figure 8.4. Trapezoidal rule with five data points

If the intervals are not of equal width, the following equation should be used in place of Equation
(8.6).

m− 1
⎡⎣( xi+1 − xi) ⋅ ( f ( xi+1) + f ( xi) )⎤⎦
1
I=
2

∑ (8.9)
i = 1
282 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 8.2.

Given:

θ (degrees): 0 15 30 45 60` 75 90

Sin(2θ): 0 0.5 0.86603 1 0.86603 0.5 0

Use the trapezoidal rule to determine the area under the curve from θ = 0 to 90 degrees.
Compare the result with the true value of the integral.

The subinterval width, in this case , is

π
h := 15 ⋅ h = 0.2618
180

The y-coordinates of the 7 given data points are

y1 := 0 y2 := 0.5 y3 := 0.86603 y4 := 1.0 y5 := 0.86603

y6 := 0.5 y7 := 0.0

The value of the desired integral can be estimated by the trapezoidal rule as

Int := 0.5 ⋅ h ⋅ ( y1 + 2 ⋅ y2 + 2 ⋅ y3 + 2 ⋅ y4 + 2 ⋅ y5 + 2 ⋅ y6 + y7 ) Int = 0.97705

The true value and the error between the true and estimated values of the integral are

Trueval − Int
Trueval := 1.0 %err := ⋅ 100 %err = 2.2949
Trueval

Using Mathcad, the following estimate of the integral is obtained:

x := 0 , 0.2618 .. 1.5708 y ( x ) := sin ( 2 ⋅ x ) y (x) =


0
0.5
1.5708 0.866

⎮ sin ( 2 ⋅ θ) dθ = 1 1
⌡ 0.866
0
0.5
-7.3464·10-6
Chapter 8: Numerical Integration 283

8.4 SIMPSON'S RULES

Simpson's rules are two in number. They are the Simpson's one-third rule and the Simpson's
three-eighth rule. The first is based on the area under a parabola passing through three equally
spaced points, while the second is based on the area under a third-order interpolating
polynomial passing through 4 points .
8.4.1 Simpson's One-Third Rule

It can be easily shown that the area under a parabola passing through the equally spaced
points ( x1 , y1 ), ( x2 , y2 ) and ( x3 , y3 ), shown in Figure 8.5 is

( )
h
A= ⋅ y1 + 4 ⋅ y2 + y3 (8.10)
3

Figure 8.5. Basis of Simpson's one-third rule

In general, if the area under a curve is divided into n segments, there are (n+1 ) points on the
curve

(x1 , y1) (x2 , y2) ................ ( x n+ 1 , y n+ 1)

If each segment is of width "h" and there are "n" segments, "n" being an even number, the area
under the parabola passing through the first three points will be
284 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )
h
A1 = ⋅ y1 + 4 ⋅ y2 + y3 (8.11)
3

Similarly, the areas under the parabolas passing through the third, fourth and fifth points, and
through the last three points on the curve will be

( )
h
A2 = ⋅ y3 + 4 ⋅ y4 + y5
3

........................................
(8.12)

( )
h
Alast = ⋅ yn−1 + 4 ⋅ yn + yn+1
3

The integral according to Simpson's one-third rule is the sum of the above areas and can be written
as

h
I=
3
⋅ y1 + 4 ⋅
∑ yi + 2 ⋅
∑ yi + yn+1 (8.13)
ieven i oddexceptlast

For example, if there are seven data points, this integral can be estimated as

( )
h
I= ⋅ y1 + 4 ⋅ y2 + 2 ⋅ y3 + 4 ⋅ y4 + 2 ⋅ y5 + 4 ⋅ y6 + y7
3 (8.14)

Figure 8.6 shows a comparison between the function approximations obtained with the trapezoidal
rule and Simpson's rule, and the consequent integrals which are the respective areas under these
functions.

Figure 8.6. Comparison between the trapezoidal and Simpson's one-third rules
Chapter 8: Numerical Integration 285

Example 8.3.

For the given data, compute the area under the curve by Simpson's one-third rule and
compare the estimated value with the true value

Theta: 0 15 30 45 60 75 90

Sin(2θ) 0 0.5 0.86603 1 0.86603 0.5 0.

The interval width in this case is

π
h := 15 ⋅ h = 0.2618
180.

The y-coordinates of the given data points are

y1 := 0 y2 := 0.5 y3 := 0.86603 y4 := 1.0 y5 := 0.86603

y6 := 0.5 y7 := 0.0

An estimate of the desired integral by Simpson's one-third rule is then

y1 + 4 ⋅ y2 + 2 ⋅ y3 + 4 ⋅ y4 + 2 ⋅ y5 + 4 ⋅ y6 + y7
Int1 := h ⋅ Int1 = 1.0004
3

The true value of the integral is

⎛ π ⎞
⎜⌠ 2 ⎟
⎜⎮ ⎟
Trueval := ⎮ sin ( 2 ⋅ θ) dθ
⎜⌡ ⎟
⎝ 0 ⎠

Trueval := 1.0

The percentage error between the true and estimated values is


286 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Trueval − Int1
%err1 := ⋅ 100 %err1 = −0.0433
Trueval

8.4.2 Simpson's three-eighth rule

Simpson's 3/8 th rule is based on the area under a third order interpolating polynomial passing
through four points . If these points are

(x1 , y1 ) , (x2 , y2 ), (x3 , y3 ) and (x4 , y4 )

as shown in Figure 8.7, then this area can be shown to be

( )
3
A= ⋅ h ⋅ y1 + 3 ⋅ y2 + 3 ⋅ y3 + y4 (8.15)
8

Figure 8.7. Basis of Simpson's three-eighth rule

If the area under a curve is divided into n segments of equal width h giving (n+1) points on
the curve, Equation (8.15)) will give the area under the third-order interpolating polynomial
passing through the first four points . Similarly, the area under the polynomial passing
through the fourth, fifth, sixth and seventh points will be
Chapter 8: Numerical Integration 287

( )
3
A2 = ⋅ y4 + 3 ⋅ y5 + 3 ⋅ y6 + y7 ⋅ h (8.16)
8

and that under the polynomial passing through the last four points will be

( )
3
Alast = ⋅ yn−2 + 3 ⋅ yn−1 + 3 ⋅ yn + yn+1 ⋅ h (8.17)
8

The integral according to Simpson's 3/8 th rule is then the sum of the above areas and can be
written as

( )
3
I= ⋅ h ⋅ y 1 + 3 ⋅ y 2 + 3 ⋅ y 3 + 2 ⋅ y 4 + 3 ⋅ y 5 + 3 ⋅ y 6 + 2 ⋅ y 7 + 3 ⋅ y 8 + 3 ⋅ y 9 + 2 ⋅ y 10 + ..... y n+ 1
8
(8.18)

8.4.3 Limitations of Simpson's Rules

While the one-third rule requires the number of segments under the curve to be even, the
mininum number being two, the three-eighth rule requires the number of segments to be a
number divisible by three , the minimum number being three. That is, for computation by the
one-third rule, the number of segments can be 2, 4, 6 and so on while the number of
segments required by the 3/8 th rule is 3, 6, 9, 12 etc.

Example 8.4.

For the data of Example 8.3, compute the area under the curve by Simpson's three-eighth
rule and compare the estimated value with the true value

Theta: 0 15 30 45 60 75 90

Sin(2θ): 0 0.5 0.86603 1 0.86603 0.5 0.

The interval width is , as before,

π
h := 15 ⋅ h = 0.2618
180.

and the y-coordinates of the given data points are

y1 := 0 y2 := 0.5 y3 := 0.86603 y4 := 1.0 y5 := 0.86603


288 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

y6 := 0.5 y7 := 0.0

The integral , according to Simpson's three-eighth rule, can be estimated as

⎛ 3 ⎞ ⋅ h ⋅ ( y1 + 3 ⋅ y2 + 3 ⋅ y3 + 2 ⋅ y4 + 3 ⋅ y5 + 3 ⋅ y6 + y7 )
Int2 := ⎜ ⎟ Int2 = 1.001
⎝ 8⎠

The true value, as computed in Example 8.3 , is

Trueval := 1.0

and the percentage error is

Trueval − Int2
%err2 := ⋅ 100 %err2 = −0.1008
Trueval

8.5 ROMBERG INTEGRATION

Romberg integration is a very powerful and efficient method of computing an integral. It uses a
combination of the trapezoidal rule with an extrapolation technique known as Richardson
extrapolation, and provides much better accuracy than the trapezoidal rule.

(a) (b)

Figure 8.8. Basis of Romberg integration


Chapter 8: Numerical Integration 289

The procedure for computing the integral of f(x) over a prescribed range of x= a to x= b by
the Romberg method is as follows.

1. For a range of integration from x =a to x= b, a shown in Figure 8.8 (a) ,obtain


the first trapezoidal estimate I 0,1 of the integral of the given function f(x) by the trapezoidal
rule as

1 (8.19)
I0 , 1 = ⋅ h ⋅ [ ( f ( a) + f ( b) ]
2

where h=b− a (8.20)

2. Divide the given range into two segments as shown in Figure 8.8 (b) and obtain
the second trapezoidal estimate I 1,1 as

( )
h
I1 , 1 = ⋅ y1 + 2 ⋅ y2 + y3 (8.21)
2

where

b−a
h=
2

y 1 = f ( a) <--Value of function at the beginning of the range of


integration
(8.22)

y 2 = f ( a + h) <--Value of function at the middle of the range of


integration

y 3 = f ( a + 2 ⋅ h) <--Value of function at the end of the range of


integration

3 Now divide the given range further into four segments and obtain the third
trapezoidal estimate I 2,1 with h = one-half of the previous h = (b-a)/4 .

4. Obtain a fourth trapezoidal estimate I 3,1with eight segments and h = (b-a)/8

5. Obtain a fifth trapezoidal estimate I 4,1 with sixteen segments and h = (b-a)/16

6. Using the Richardson extrapolation formula which is


290 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

( )
1
Ii , j = Ii , j −1 + ⋅ Ii , j −1 − Ii−1 , j −1 (8.23)
j −1
4 −1

compute the other unknown components of the matrix [I] , namely ,

I1 , 2 I2 , 2 I2 , 3 I3 , 2 I3 , 3 I3 , 4

I4 , 2 I4 , 3 I4 , 4 I4 , 5 ............

and fill in the Romberg table which is shown in Table 8.1. Keep filling in the table
until the integral values converge , i.e,

I3 , 3 = I3 , 2 or I4 , 3 = I4 , 2 or I4 , 4 = I4 , 3

whichever occurs earlier.

TABLE 8.1

The Romberg table

⎛⎜ I0 , 1 ⎞⎟
⎜ I1 , 1 I1 , 2 ⎟
⎜ ⎟
⎜ I2 , 1 I2 , 2 I2 , 3 ⎟

R := I3 , 1 I3 , 2 I3 , 3 I3 , 4

⎜ ⎟
⎜ I4 , 1 I4 , 2 I4 , 3 I4 , 4 I4 , 5 ⎟
⎜ ⎟
⎜ I5 , 1 I5 , 2 I5 , 3 I5 , 4 I5 , 5 I5 , 6 ⎟
⎜ ⎟
⎝ ⎠

The procedure of this method should be clear from the examples that follow.
Chapter 8: Numerical Integration 291

Example 8.5

Use Romberg integration to find the integral of exp (- x 3 ) between the limits of a= 0.1 and b=
1.75 Use an "h" equal to (b-a) to begin with.

The given function and the integration limits are

3
−x
f ( x ) := e a := 0.1 b := 1.75

Computation of the first trapezoidal estimate :

−3
f ( a) = 0.999 f ( b) = 4.70385 × 10

h := b − a h = 1.65 I0 , 1 := 0.5 ⋅ h ⋅ ( f ( a) + f ( b) ) I0 , 1 = 0.82806<--- First


trapezoidal
estimate
Computation of the second trapezoidal estimate:

In this case, the interval size must be halved, giving y 1 , y2 and y 3 as the function values at the
beginning, middle and end of the integration range.

b− a
h := h = 0.825 y1 := f ( a) y1 = 0.999 y2 := f ( a + h) y2 = 0.45319
2
−3
y3 := f ( a + 2 ⋅ h) y3 = 4.70385 × 10

Second trapezoidal
I1 , 1 := 0.5 ⋅ h ⋅ ( y1 + 2 ⋅ y2 + y3) I1 , 1 = 0.78791 <--
estimate

Computation of the third trapezoidal estimate:

The "h" must be halved again giving h=(b-a)/4

h
h := h = 0.4125
2

In this case, the computation of the number of subintervals to be used in the integration
process (n), the coordinates of the resulting data points and the third trapezoidal estimate is
done as shown below

b− a
n :=
h
n= 4 i := 1 .. ( n + 1) x i := a + ( i − 1) ⋅ h ( )
y i := f x i
292 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xi = yi =
0.1 0.999
0.5125 0.87406
0.925 0.45319
1.3375 0.09139
1.75 4.70385·10 -3

⎛ 4 ⎞
⎜ y j + y 5⎟
I2 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅
⎜ ∑ ⎟
I2 , 1 = 0.7922 <--Third trapezoidal estimate

⎝ j = 2 ⎠

Computations of the fourth and fifth trapezoidal estimates:

These are very similar to that of the third estimate and are presented below

h b− a
Fourth estimate: h := h = 0.20625 n :=
2 h n= 8

i := 1 , 2 .. ( n + 1)

x i := a + ( i − 1) ⋅ h ( )
y i := f x i

xi = yi =
0.1 0.999
0.30625 0.97169
0.5125 0.87406
0.71875 0.68983
0.925 0.45319
1.13125 0.23511
1.3375 0.09139
1.54375 0.02525
1.75 4.70385·10 -3

⎛ 8 ⎞
⎜ y j + y 9⎟
I3 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅
⎜ ∑ ⎟
I3 , 1 = 0.79249 <--Fourth trapezoidal estimate

⎝ j = 2 ⎠
Fifth estimate:
h b− a
h := h = 0.10313 n := n = 16
2 h

i := 1 , 2 .. ( n + 1) x i := a + ( i − 1) ⋅ h ( )
y i := f x i
Chapter 8: Numerical Integration 293

xi =
yi =
0.1
0.999
0.20313
0.99165
0.30625
0.97169
0.40937
0.93369
0.5125
0.87406
0.61563
0.7919
0.71875
0.68983
0.82188
0.57398
0.925
0.45319 y 16 = 0.01149
1.02812
0.3373
1.13125
0.23511 −3
1.23438
0.15247
y 17 = 4.70385 × 10
1.3375
0.09139
1.44063
0.05029
1.54375
0.02525
1.64688
0.01149

⎛ 16 ⎞
⎜ y j + y 17⎟
I4 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅
⎜ ∑ ⎟ I4 , 1 = 0.79253 <--Fifth trapezoidal estimate
⎝ j = 2 ⎠

Using the Richardson extrapolation formula for improved estimates, which is

j := 2 , 3 .. 4 i := 1 , 2 .. 4

( )
1
Ii , j := Ii , j− 1 + ⋅ Ii , j−1 − Ii− 1 , j− 1
j−1
4 −1

or,

j− 1
4 ⋅ Ii , j− 1 − Ii−1 , j−1
Ii , j := ,
j−1
4 −1

improved estimates of the integral can be computed as shown below.

I2 , 2 = 0.79363 I2 , 3 = 0.7949 I3 , 2 = 0.79258 I3 , 3 = 0.79251

I3 , 4 = 0.79247 I4 , 2 = 0.79254 I4 , 3 = 0.79254 I4 , 4 = 0.79254


294 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

However, the Romberg procedure requires the following quantities to be zeros.

I1 , 3 := 0 I1 , 4 := 0 I2 , 4 := 0

⎛⎜ 0 0.82806 0 0 ⎞⎟ 0
⎜0 0.78791 0.77452 0 0 ⎟ Shown to the left is the Romberg Table

I= 0 0.7922 0.79363 0.7949 0
⎟ <--
The zeros shown in the first column of
⎜ ⎟ matrix, however, are to be ignored.
⎜0 0.79249 0.79258 0.79251 0.79247 ⎟
⎜0 ⎟
⎝ 0.79253 0.79254 0.79254 0.79254 ⎠

The Romberg table of integrals is cleaned up and shown below:

TABLE 8.2
The Romberg table for the integration of exp( - x 3 )

____________________________________________________________________________

0.82806

0.78791 0.77452

0.7922 0.79363 0.79490

0.79249 0.79258 0.79251 0.79247

0.79253 0.79254 0.79254 0.79254

______________________________________________________________________________

Notice that convergence has taken place at an integral value of 0.79254 as shown in the
tabular presentation.

Use of the Mathcad integral operator gives

1.75

⎮ f ( x ) dx = 0.79254

0.1

where the function f(x) was already defined earlier.


Chapter 8: Numerical Integration 295

Example 8.6

Use Romberg integration to find the integral of ln (1+ x 3 ) between the limits of a=1.0 and
b=4.5. Use an "h" value equal to (1/2) (b-a) to begin with .

Computation of first trapezoidal estimate:

a := 1.0 b := 4.5 (
f ( x ) := ln 1 + x
3 )
b− a
h := h = 1.75 y1 := f ( a) y1 = 0.69315 y2 := f ( a + h) y2 = 3.08177
2

y3 := f ( a + 2 ⋅ h) y3 = 4.52315

I1 , 1 := 0.5 ⋅ h ⋅ ( y1 + 2 ⋅ y2 + y3) I1 , 1 = 9.95735 <--First trapezoidal


estimate
Computation of second trapezoidal estimate:

h b− a
h := h = 0.875 n := n= 4 i := 1 , 2 .. ( n + 1)
2 h

x i := a + ( i − 1) ⋅ h ( )
y i := f x i
xi = yi =
1 0.69315
1.875 2.02707
2.75 3.08177
3.625 3.88434
4.5 4.52315

⎛ 4 ⎞
⎜ y j + y 5⎟
I2 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅
⎜ ∑ ⎟
I2 , 1 = 10.15116 <---- Second trapezoidal
estimate
⎝ j = 2 ⎠
Computation of third trapezoidal estimate:

h b− a
h := h = 0.4375 n := n= 8
2 h

i := 1 , 2 .. ( n + 1) x i := a + ( i − 1) ⋅ h ( )
y i := f x i
296 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xi =
yi =
1
0.69315
1.4375
1.37888
1.875
2.02707
2.3125
2.59275
2.75
3.08177
3.1875
3.50812
3.625
3.88434
4.0625
4.2202
4.5
4.52315

⎛ 8 ⎞
⎜ y j + y 9⎟
I3 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅
⎜ ∑ ⎟
I3 , 1 = 10.19431 <--- Third trapezoidal estimate

⎝ j = 2 ⎠

Computation of fourth trapezoidal estimate:

h b− a
h := h = 0.21875 n := n = 16
2 h

i := 1 , 2 .. ( n + 1) x i := a + ( i − 1) ⋅ h ( )
y i := f x i

xi = yi =
1 0.69315
1.21875 1.03328
1.4375 1.37888
1.65625 1.7126
1.875 2.02707
2.09375 2.32028
2.3125 2.59275
2.53125 2.84597
y 16 = 4.3754
2.75 3.08177
2.96875 3.30193
3.1875 3.50812 y 17 = 4.52315
3.40625 3.70182
3.625 3.88434
3.84375 4.0568
4.0625 4.2202
4.28125 4.3754
Chapter 8: Numerical Integration 297

⎛ 16 ⎞
I4 , 1 := 0.5 ⋅ h ⋅ ⎜ y 1 + 2 ⋅ y j + y 17⎟
⎜ ∑ ⎟ I4 , 1 = 10.20455 <--- Fourth trapezoidal estimate
⎝ j = 2 ⎠

Use of the Richardson extrapolation formula for improved estimates:

j := 2 , 3 .. 4 i := 1 , 2 .. 4

( )
1
Ii , j := Ii , j− 1 + ⋅ Ii , j−1 − Ii− 1 , j− 1 <---- Richardson extrapolation formula for
j−1
4 −1 improved estimates

or

j− 1
4 ⋅ Ii , j− 1 − Ii−1 , j−1
Ii , j :=
j−1
4 −1

I2 , 2 = 10.21576 I3 , 2 = 10.20869 I3 , 3 = 10.20822 I4 , 2 = 10.20796

I4 , 3 = 10.20791 I4 , 4 = 10.20791

The Romberg procedure requires the following quantities to be zeros.

I0 , 1 := 0 I1 , 2 := 0 I1 , 3 := 0 I1 , 4 := 0 I2 , 3 := 0 I2 , 4 := 0 I3 , 4 := 0

Thus, the estimates computed can be assembled in the tabular form (Romberg Table ) shown
below. The zeros shown in the matrix are to be ignored.

⎛⎜ 0 0 0 0 ⎞⎟ 0
⎜0 9.957348 0 0 0 ⎟

I= 0 10.151155 10.215758 0 0

⎜ ⎟
⎜0 10.194307 10.20869 10.208219 0 ⎟
⎜0 ⎟
⎝ 10.204548 10.207962 10.207914 10.207909 ⎠
298 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The Romberg table of integrals is cleaned up and shown below


:
TABLE 8.3
The Romberg Table for the integral of ln (1+x 3 )
_____________________________________________________________________________

9.957348

10.151155 10.215758

10.194307 10.20869 10.208219

10.204548 10.207962 10.207914 10.207909

______________________________________________________________________________

The tabular presentation of integral values shows that convergence has taken place at an
integral value of 10.2079

Use of the Mathcad integral operator yields

4.5
⌠ Shown on the left is the integral obtained with
⎮ f ( x ) dx = 10.20791 <----
⌡ Mathcad where the function f(x) was already defined
1.0 earlier.

Example 8.7.

Use Romberg integration to find the integral of the given function f(x) between the limits of a=
0 and b= 8. Begin with h= (b-a) to compute the first trapezoidal estimate I 0,1 . Compute
four trapezoidal estimates, then complete the Romberg table.

x f(x)

0 3.2
0.50 2.6
1.0 1.7
1.50 1.0
2.0 0.62
2.50 0.45
3.0 0.40
3.50 0.27
4.0 0.20
Chapter 8: Numerical Integration 299

Computation of the first trapezoidal estimate:

a := 0 b := 4 y 1 := 3.2 y 2 := 0.20 h := b − a h= 4

(
I0 , 1 := 0.5 ⋅ h ⋅ y 1 + y 2 ) I0 , 1 = 6.8 <-- FIRST TRAPEZOIDAL ESTIMATE

Computation of the second trapezoidal estimate

h b− a
h := h= 2 n := n= 2
2 h

x 1 := 0 y 1 := 3.2 x 2 := 2 y 2 := 0.62 x 3 := 4 y 3 := 0.20

(
I1 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅ y 2 + y 3 ) I1 , 1 = 4.64 <-- SEC TRAPEZOIDAL ESTIMATE

Computation of the third trapezoidal estimate:

h b− a
h := h= 1 n := n= 4
2 h

x 1 := 0 y 1 := 3.2 x 2 := 1. y 2 := 1.7 x 3 := 2.0 y 3 := 0.62

x 4 := 3.0 y 4 := 0.40 x 5 := 4.0 y 5 := 0.20

⎛ 4 ⎞
I2 , 1 := 0.5 ⋅ h ⋅ ⎜ y 1 + 2 ⋅ y j + y 5⎟
⎜ ∑ ⎟
I2 , 1 = 4.42 <-- THIRD TRAPEZOIDAL
ESTIMATE
⎝ j = 2 ⎠
Computation of the fourth trapezoidal estimate:

b− a
h n := n= 4
h := h = 0.5 h
2

x 1 := 0 y 1 := 3.2 x 2 := 0.50 y 2 := 2.60 x 3 := 1. y 3 := 1.70

x 4 := 1.50 y 4 := 1.0 x 5 := 2.0 y 5 := 0.62 x 6 := 2.50 y 6 := 0.45

x 7 := 3. y 7 := 0.40 x 8 := 3.50 y 8 := 0.27 x 9 := 4. y 9 := 0.20

⎛ 8 ⎞
I3 , 1 := 0.5 ⋅ h ⋅ ⎜ y 1 + 2 ⋅ y j + y 9⎟
⎜ ∑ ⎟
I3 , 1 = 4.37 <-- FOURTH TRAPEZOIDAL
ESTIMATE
⎝ j = 2 ⎠
300 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Use of the Richardson formula to obtain improved estimates:

j− 1
j := 2 .. 4 i := 1 , 2 .. 3 4 ⋅ Ii , j− 1 − Ii−1 , j−1
Ii , j :=
j−1
4 −1

I1 , 2 = 3.92 I2 , 2 = 4.34667 I2 , 3 = 4.37511

I3 , 1 = 4.37 I3 , 2 = 4.35333 I3 , 3 = 4.35378 I3 , 4 = 4.35344

I1 , 3 := 0 I1 , 4 := 0 I2 , 4 := 0
The Romberg procedure, in
<--- this case, requires these
I0 , 2 := 0 I0 , 3 := 0 I0 , 4 := 0 I4 , 1 := 0
quantities that are shown on
the left to be zeros.
I4 , 2 := 0 I4 , 3 := 0 I4 , 3 := 0 I4 , 4 := 0

⎛⎜ 0 6.8 0 0⎞⎟ 0
⎜0 4.64 3.92 0 0 ⎟ Shown to the left is the Romberg Table

I= 0 4.42 4.34667 4.37511 0
⎟ The zeros shown in the matrix, however,
⎜ ⎟ are to be ignored.
⎜0 4.37 4.35333 4.35378 4.35344 ⎟
⎜0 ⎟ <--
⎝ 0 0 0 0 ⎠
The Romberg table of integrals is cleaned up and shown below
:
TABLE 8.4
The Romberg Table for the integral of given function of Example 8.7
_____________________________________________________________________________

6.8

4.64 3.92

4.42 4.34667 4.37511

4.37 4.35333 4.35378 4.35344

______________________________________________________________________________

The tabular presentation of integral values shows that convergence has taken place at an
integral value of approximately 4.353.
Chapter 8: Numerical Integration 301

8.6 APPLICATIONS IN NUMERICAL INTEGRATION

Applications of numerical integration can be encountered in situations dealing with location of


centroids, determination of moments of inertia of cross sectional shapes and computation of
periods of oscillations in nonlinear vibration problems. Some examples are dealt with below.

8.6.1 Centroid of a Rod Bent into the Shape of a Parabola

The length of an element dL of a rod bent into the shape of the parabola shown in Figure 8.8
can be expressed in terms of the differentials dx and dy as [ 8 ]

2
2 2 ⎛d ⎞
dL = dx + dy or 1+ ⎜ x ⎟ ⋅ dy (8.24)
⎝ dy ⎠

2 d 2
Since x = 2⋅y , x = 4× y and dL = 1 + 16 ⋅ y ⋅ dy
dy
x
y ( x ) := x := 0 , 0.001 .. 0.5
2

Rod of parabolic shape: x = 2 y .y

0.4
y, meters

y( x)
0.2

bent shape of rod


0
0 0.1 0.2 0.3 0.4
x
x , meters

Figure 8.8. Rod bent into a parabolic shape

The length of the rod , then, can be written as

0.5
⌠ ⌠
⎮ 2
L= 1 dL or L := ⎮ 1 + 16 ⋅ y dy L = 0.739 meters
⎮ ⌡
⌡ 0
302 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The location of the centroid ( x CG , y CG ) can then be calculated using

⌠ ⌠
⎮ ⎮
x dL y dL
⎮ ⎮
⌡ ⌡
x CG = y CG =
L L

or,

0.5 0.5
1 ⌠ 2 2 1 ⌠ 2
x CG := ⋅ ⎮ 2 ⋅ y ⋅ 1 + 16 ⋅ y dy y CG := ⋅ ⎮ y ⋅ 1 + 16 ⋅ y dy
L ⌡ 0
L ⌡ 0
giving

x CG = 0.205 m y CG = 0.287 m

8.6.2. Moment of Inertia of a Beam of Semi-Elliptic Cross Section

For the semi-elliptic beam cross section shown in Figure 8.9 , its moments of inertia about
the x and y axes can be calculated using [ 8 ]

⌠ ⌠
⎮ 2 ⎮ 2
Ixx = ⎮ y dA Iyy = ⎮ x dA (8.25)
⌡ ⌡
where
2
y
dA = 2 ⋅ x ⋅ dy with x (y) = 1− ⋅ a and a := 4 in b := 2 in
2
b

2
x
dA = y ⋅ dx with y ( x ) := 1− ⋅b x := −4 , −3.99 .. 4.
2
a

The moments of inertia , I xx and I yy, Semi-elliptic cross section


can now be computed as follows,using 2
Equations (8.25) y( x)

b
a ⌠
0
2 2 2 4 2 0 2 4
Ixx := 2 ⋅ ⋅ ⎮ y ⋅ b − y dy
b ⌡ 0
x

Figure 8.9. Beam cross section


a
b ⌠ 2 2 2
Iyy := ⋅ ⎮ x ⋅ a − x dx giving Ixx = 12.566 in 4 and Iyy = 50.266 in 4
a ⌡−a
Chapter 8: Numerical Integration 303

8.6.3. Launch of a Projectile

A projectile is launched with an initial vertical velocity vy init from a height y 0 above the ground.
Neglecting air resistance, the relationship between its vertical velocity v y at any time t and the
height y above the ground that it attains on the way up is given by [ 8 ]

vy =
2
(
vyinit + 2 ⋅ G ⋅ y 0 − y ) (8.26)

where G is the acceleration due to gravity ( 32.2 ft/ sec 2 or 9.81 m/ sec 2 ) . Equating the
above quantity to dy/dt , and then integrating with respect to time from 0 to t seconds, the
following expression can be derived for the time t taken by the projectile to attain the height
y above the ground

y
⌠ 1
t=⎮ dy
⎮ (8.27)

2
vyinit + 2 ⋅ G ⋅ y 0 − y ( )
⌡y
0

The time taken to reach the maximum height , y max , is then the above integral with
y max as the upper limit of integration , where

2
vyinit
y max = + y0
2⋅G (8.28)

For an initial velocity of 55 ft/ sec at an angle of 65 degrees from a height of 3.5 ft above ground, the
time taken to attain the maximum height is calculated below using Equations (8.27) and (8.28).

v init := 55 ft/sec θ := 65 ⋅ deg sin ( θ ) = 0.906 G := 32.2 ft/ sec 2

vyinit := v init ⋅ sin ( θ ) vyinit = 49.847 ft/sec y 0 := 3.5 ft

2
vyinit
y max := + y0 y max = 42.083 ft
2⋅G

y
⌠ max 1
tmaxheight := ⎮ dy tmaxheight = 1.548 sec


2
vyinit + 2 ⋅ G ⋅ y 0 − y( )
⌡y
0
304 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

8.6.4. Large Oscillations of a Simple Pendulum

A simple pendulum, which is a mass attached to one end of a light rigid rod, is shown in Figure
8.10.

Figure 8.10. Vibration of a simple pendulum

The large-amplitude motion of this pendulum is governed by the following nonlinear differential
equation [ 6,9,16,20 ].

2 G
d
θ ( t) + ⋅ sin ( θ ( t) ) = 0 (8.29)
2 L
dt

where L is the length of the rod and G is the acceleration due to gravity . However, if the
amplitudes of vibration are considered small, the above differential equation assumes
its linear form

2 G
d
θ ( t) + ⋅ θ ( t) = 0 (8.30)
2 L
dt

and the motion, then, has a natural period

L
τ linear := 2 ⋅ π ⋅ seconds (8.31)
G

For the nonlinear problem , it can be shown that the period of one oscillation, which is now a
function of the amplitude of vibration , is given by the integral
Chapter 8: Numerical Integration 305

π
A⋅
⌠ 180.
L ⎮ 1
τ nonlinear ( A) := 2 ⋅ ⋅⎮ dθ seconds (8.32)
G ⎮ 2 2
⎮ ⎛ π ⎞ ⎛θ⎞
sin ⎜ A ⋅ ⎟ − sin ⎜ ⎟

⌡ ⎝ 360 ⎠ ⎝2⎠
0
where A is the maximum vibration amplitude in degrees that the pendulum attains. The
corresponding linear and nonlinear natural frequencies in Hertz can be computed using

1 1
flinear := fnonlinear ( A) := (8.33)
τ linear τ nonlinear ( A)

In a nonlinear problem, the percentage variation between the linear and nonlinear natural frequencies
is typically expressed in the following form

fnonlinear ( A) − f linear
Percentdiff ( A) := ⋅ 100 (8.34)
flinear

This variation, which is plotted in Figure 8.11 below as a function of the maximum amplitude of
vibration of the pendulum, confirms that there is a sharp increase in the variation from the linear
natural frequency with increase in the amplitude of vibration.

A := 0.1 , 0.2 .. 30

Variation of nat freq with amplitude


2
% variation from linear nat frequency

1.5

Percentdiff ( A) 1

0.5

0
5 10 15 20 25 30
A
Amplitude A, degrees

Figure 8.11. Variation of nonlinear natural frequency wiith amplitude


306 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

PROBLEMS

8.1. Use the trapezoidal rule to estimate the integral of x (2/3) over the range a=0 to
b=4.0 for interval widths of h= 1.0, 0.5 and 0.25. Also obtain an answer using the Mathcad
integral operator.

8.2. Using Simpson's rules, with h= 0.25, 0.5 and 1.5, obtain estimates of the
following integral:
3

⎮ 4
⎮ 3
I=⎮ x dx

0

Also obtain an answer using the Mathcad integral operator

8.3. Using Simpson's rules, with h = 4, 8 and 10 subdivisions of the given range of x,
obtain estimates of the following integral:

4

⎮ 1
⎮ dx
2⋅ x
⎮ 6 + 5⋅e

1
Also obtain an answer using the Mathcad integral operator

8.4. Use Romberg integration to find the integral of x 3 exp ( 2x ) between the
limits of a= 0 and b= 2.0. Use an "h" equal to (b-a) to begin with to compute the first
trapezoidal estimate. Also verify your answer using the Mathcad integral operator.

8.5. Use Romberg integration to find the integral of (1/x) between the limits of a= 1.0
and b= 2.0. Use an "h" equal to (b-a) to begin with. The completion of three rows of the
Romberg table should be sufficient.

8.6. Use Romberg integration to find the integral of ln(x) between the limits of a= 1.0
and b= 2.0. Use an "h" equal to (b-a) to begin with. Compute 3 trapezpoidal estimates
before completing the Romberg table.

8.7. Use Romberg integration to find the integral of

3 / (1 + x 2 )

between the limits of a= 0 and b= 4.0. Use an "h" equal to (b-a) to begin with.
Compute 3 trapezpoidal estimates before completing the Romberg table.
Chapter 8: Numerical Integration 307

8.8. Use Romberg integration to find the integral of ( x e 2x ) between the limits of a= 0
and b= 0.5. Use an "h" equal to (b-a) to begin with. Compute 3 trapezpoidal estimates
before completing the Romberg table.

8.9. Use Romberg integration to find the integral of x sin (x) between the limits of a= 1.0
and b= 2.0. Use an "h" equal to (b-a) to begin with. Compute an answer that is good to at
least 3 decimal places.

8.10. Use Romberg integration to find the integral of the following function f(x) between the
limits of a= 1.8 and b= 3.4. Begin with h= (b-a)/2 = 0.8 , that is , use the values marked by
asterisks to compute the first trapezoidal estimate I 1, 1 . Also verify your result by resorting
to Mathcad cubic spline interpolation and the Mathcad integral operator.

______________________________________________________________________
x f(x) x f(x) x f(x) x f(x)
----------------------------------------------------------------------------------------------------------------------------
1.6 4.751 1.8 6.152* 2.0 7.412 2.2 9.038
2.4 11.151 2.6 13.475* 2.8 16.521 3.0 20.08
3.2 24.721 3.4 30.114* 3.6 36.617 3.8 44.701
______________________________________________________________________

8.11. Use Romberg integration to find the integral of the following function f(x) between the
limits of a= 0.3 and b= 1.1. Begin with h= (b-a) to compute the first trapezoidal estimate
I 0,1 . Compute four trapezoidal estimates, then complete the Romberg table.

x f(x)

0.3 55
0.4 42
0.5 37
0.6 35
0.7 39
0.8 48
0.9 53
1.0 51
1.1 39

8.12. Use Romberg integration to find the integral of the function f(x) given in the following
table between the limits of a= 0 and b= 4. Begin with h= (b-a) to compute the first
trapezoidal estimate I 0,1 Compute four trapezoidal estimates, then complete the Romberg
table. Obtain an answer that is good to at least two decimal places.
308 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x f(x)

0 3
0.50 2.4
1.0 1.5
1.50 0.92
2.0 0.60
2.50 0.41
3.0 0.30
3.50 0.23
4.0 0.18

8.13. Use Romberg integration to find the integral of the following function f(x) between the
limits of a= 0 and b= 0.80. Begin with h= (b-a) to compute the first trapezoidal estimate I
0,1 . Compute four trapezoidal estimates, then complete the Romberg table.

x f(x)

0 0
0.1 0.12
0.2 0.30
0.3 0.55
0.4 0.89
0.5 1.36
0.6 1.99
0.7 2.84
0.8 3.96

8.14. Use Romberg integration to determine the integral of the following function f(x)
between the limits of a= 1.0 and b= 2.0. Use an "h" equal to (b-a) to begin with. Compute
three trapezoidal estimates and then complete the Romberg table. Obtain an answer that is
good to at least three decimal places.

x f(x)

1 0
1.25 0.2231
1.50 0.4055
1.75 0.5596
2.00 0.6931

8.15. The time, T, required to raise or lower the water surface in a tank with inflow
as well as outflow through an orifice at the bottom is given by the following integral
Chapter 8: Numerical Integration 309

H
⌠ 2
⎮ A
T= dh
⎮ 0.5
⎮ Q − k ⋅h
⌡H
1

where H1 and H2 are the initial and final heights of the water surface above the base of the
tank , A is the cross sectional area of the tank , Q is the inflow rate, and k is a constant
that depends on the coefficient of discharge and the area of the orifice. Determine how long
it will take for the water level to fall 10 ft in a tank 50 ft long by 30 ft wide with vertical sides,
when water enters the tank at the rate of 6 cu. ft / sec and the initial level of the water
surface in the tank is 16 ft above the bottom. Take k to be 3.

8.16. The motion of bodies moving with moderate speed in fluids such as
water or air is resisted by a damping force that is proportional to the square of the speed. In
order to estimate the resonant amplitude of the motion, the equivalent linear viscous
damping, C eq , is needed which is found by equating the energy dissipated by the viscous
damping in one cycle to that of the nonviscous damping [ 20 ]. The equivalent viscous
damping, in this case , can be shown to be

π
2⋅a⋅ω ⋅X ⌠ 3
Ceq = ⋅⎮ sin ( θ ) dθ
π ⌡
0

where a is a proportionality constant that depends on the body geometry and the
properties of the fluid medium , ω is the frequency of excitation and X is the
amplitude of the resulting vibration. Determine C eq if a = 0.85 N-sec 2 / m , ω = 550
rads/ sec and X = 16 mm.

8.17. The moment of inertia of a sphere of radius R about a diametral axis is given by
the integral [ 8 ]

R

⎮ 2
1
Idia = ⋅ ρ ⋅ π ⋅ ⎮ ⎡⎣( R 2 − x 2⎤⎦ dx
2 ⌡
−R

where ρ is the mass density of the material of the sphere. Estimate I dia for a 1 m
diameter sphere made of steel with ρ = 7.8 Mg / m 3

8.18. The shear stress distribution in a beam cross section subjected to a shear force
V is given by [ 18 ]
310 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

V ⋅Q
τ=
I ⋅b

where V is the shear force acting on the cross section, I is the moment of inertia of the
entire cross section about the neutral axis , b is the cross sectional width at the level
of interest in the cross section, and Q is the first moment of the part of the area of the
cross section above the level of interest about the neutral axis. For a circular cross
section of radius R , the shear stress at a height y 1 above the neutral axis is given by
the integral

R

V ⎮ 2 2
τ= ⋅2⋅⎮ R − y ⋅ y dy
2 2 ⌡y
I ⋅ 2 ⋅ R − y1 1

Determine the shear stress distribution in the case of a beam with a circular cross section
of radius 1 inch and subjected to a shear force of magnitude 500 lbs

8.19. The time taken by an object to slide down under the action of gravity from a
point O (0,0) on a curved ramp described by the function y = y(x) to a point B is given by the
integral

xB
⌠ 2
⎮ ⎛d ⎞
⎮ 1+ ⎜ y ( x) ⎟
⎮ ⎝ dx ⎠ dx
T=
⎮ 2 ⋅ G ⋅ y ( x)

0

where G is the acceleration due to gravity ( 32.2 ft / sec 2 ). Estimate the time required for
a sack to slide from the top O ( 0,0) of a ramp described by

y(x) = A x 2 + B x ( with A= 0.02551, B= 0.01 )

to its bottom at x = 28 ft.


Chapter 9: Numerical Solution of Ordinary Differential Equations 311

CHAPTER 9

NUMERICAL SOLUTION OF ORDINARY


DIFFERENTIAL EQUATIONS

9.1 INTRODUCTION
A differential equation is an equation that expresses a relationship between a dependent
variable y , an independent variable x and derivatives of y . The order of the differential
equation is the order of the highest derivative in the equation. The following equation has
only the first derivative of y in it and is, therefore, a first order differential equation.

d
y = f ( x , y) (9.1)
dx
while one that has both first and second derivatives or just the second derivative is a
second order differential equation. Its general form is given below.

d
2 ⎛ d ⎞
y = f ⎜x , y , y⎟
(9.2)
dx
2
⎝ dx ⎠

Applications of differential equations can be seen in heat transfer, electrical circuit analysis,
vibration analysis , control systems analysis and a host of other areas in science as well
as engineering.

The two broad classes of problems involving differential equations are (1) initial value
problems and (2) boundary value problems. In an initial value problem, conditions are
specified at only one value of the independent variable. An example of an initial value
problem is given below.

2
d d d
m⋅ y + c ⋅ y + k ⋅ y = f ( t) y ( 0 ) = dispinit y ( 0 ) = velinit (9.3)
2 dt dt
dt

Although the independent variable in the above equation represents time, it can, just as well,
represent a spatial coordinate also such as x . In a boundary value problem, conditions are
prescribed at two values of the independent variable, as in the following.

2
d d
A⋅ y + B⋅ y + C⋅ y = g (x) y (0) = y0 y ( L) = y L (9.4)
2 dx
dx
There are many methods of solving differential equations numerically. However, our discussion is
going to be limited to the Taylor series method, the Euler and Modified Euler methods, and the
Runge-Kutta methods.
312 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

9.2 TAYLOR SERIES METHOD

Given y= y(x) and the value of the independent variable at x= x0 as y(x0 ), a Taylor series
will give the value of y at any value of the dependent variable x as

(x − x0) 2 2
( ) (
y ( x) = y x0 + x − x0 ⋅ ) d
y (0) +
2!

d
2
y ( 0 ) + ................... (9.5)
dx dx

If the problem to be solved is a first- order differential equation of the form

d
y = f (x) (9.6)
dx

with initial condition

y = y0 at x = x0 , (9.7)

the Taylor series expansion of Equation (9.5) can, clearly, be made use of in generating a
solution to the given differential equation. The following example should clarify this issue.

Example 9.1

Given the differential equation:

dy/dx = 7 x4 - 3 x 2 +9 ,

with initial condition

y=4 at x = 1 ,

estimate values of the function using a Taylor series expansion for x = 1 to x = 1.3 . Take
Δx to be 0.05. Compare the accuracy of the solution by comparing the computed values
with the true values computed from the true solution, which is:
Chapter 9: Numerical Solution of Ordinary Differential Equations 313

y = (7/5)x 5 - x3 +9x- (27/5)

The initial condition prescribed is

x0 := 1 y0 := 4.0

The first, second, third, fourth and fifth derivatives of f(x) are

4 2
yprime ( x ) := 7 ⋅ x − 3 ⋅ x + 9 <--First derivative (given)

3
ydblprime ( x ) := 28 ⋅ x − 6 ⋅ x <--Second derivative

2
ytrprime ( x ) := 84 ⋅ x − 6 <--Third derivative
(9.8)
y4prime ( x ) := 168 ⋅ x <--Fourth derivative

y5prime ( x ) := 168. <--Fifth derivative

y6prime ( x ) := 0 <--Sixth derivative

The Taylor series of Equation (9.5) expanded to seven terms up to the sixth derivative term is

⎛ 1 ⎞ ⋅ ( x − x0) 2 ⋅ ydblprime ( x0) + ⎛ 1 ⎞ ⋅ ( x − x0) 3 ⋅ ytrprime ( x0) .


⎜ 2! ⎟
y ( x ) := y0 + ( x − x0) ⋅ yprime ( x0) + ⎜ 3! ⎟
⎝ ⎠ ⎝ ⎠
⎛ 1 ⎞ 4 ⎛ 1 ⎞ 5 ⎛1⎞ 6
+ ⎜ ⎟ ⋅ ( x − x0) ⋅ y4prime ( x0) + ⎜ ⎟ ⋅ ( x − x0) ⋅ y5prime ( x0) + ⎜ ⎟ ⋅ ( x − x0) ⋅ y6prime ( x
⎝ 4! ⎠ ⎝ 5! ⎠ ⎝ 6! ⎠

The true solution to the differential equation is given as (9.9)

7 5 3 27
ytrue ( x ) := ⋅x − x + 9 ⋅x − (9.10)
5 5

Using Equations (9.8) and (9.9), the Taylor series solution to the given differential equation
can be generated and compared with the true solution of Equation (9.10) as shown below.
314 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x := 1.0 , 1.05 .. 1.3 <--Range of interest

h := 0.05 <--Increment for x

1.3 − 1.0
n := n= 6 <--Number of segments or subintervals in range of
h interest

ytrue ( x ) − y ( x )
%error ( x ) := ⋅ 100 <--Percent error between true and computed
ytrue ( x ) values , function of x.

i := 1 , 2 .. n + 1 x i := x0 + ( i − 1 ) ⋅ h <--x-values at which solution is to be


computed

%error i := %error x i ( ) <--Percent error between true and computed


values at x= x i

( )
y i := y x i <--Computed Taylor solution at x= x i

ytruei := ytrue x i ( ) <--True solution at x= x i

i= xi = yi = ytruei = %error i =
1 1 4 4 0
2 1.05 4.67917 4.67917 1.898·10-14
3 1.1 5.42371 5.42371 -1.638·10-14
4 1.15 6.24503 6.24503 -1.422·10-14
5 1.2 7.15565 7.15565 -2.482·10-14
6 1.25 8.16934 8.16934 0
7 1.3 9.3011 9.3011 0

In what follows, several Taylor series solutions with varying number of terms are developed
and compared in Figure 9.1 with the true solution. Notice that because the sixth derivative
is zero, the six-term solution which is the same as the one with seven terms produces no
error at all.
Chapter 9: Numerical Solution of Ordinary Differential Equations 315

⎛ 1 ⎞ ⋅ ( x − x0) 2 ⋅ ydblprime ( x0) ...


y6 ( x ) := y0 + ( x − x0) ⋅ yprime ( x0) + ⎜ 2! ⎟
⎝ ⎠
⎛ 1 ⎞ ⋅ ( x − x0) 3 ⋅ ytrprime ( x0) ...
+ ⎜ 3! ⎟ <--Six-term solution
⎝ ⎠
⎛1⎞ 4 ⎛1⎞ 5
+ ⎜ ⎟ ⋅ ( x − x0) ⋅ y4prime ( x0) + ⎜ ⎟ ⋅ ( x − x0) ⋅ y5prime ( x0)
⎝ ⎠
4! ⎝ ⎠
5!

y2 ( x ) := y0 + ( x − x0) ⋅ yprime ( x0) <--Two-term solution

y1 ( x ) := y0 <--One-term solution

7 5 3 27
ytrue ( x ) := ⋅x − x + 9 ⋅x − <--True solution
5 5

x := 1.0 , 1.05 .. 1.5

Comparison of computed y with true y


20
Six term/ seven term solution
Two-term solution
One-term solution
15
y6 ( x) True solution

y2 ( x)
Y- values

10
y1 ( x)

ytrue ( x)
5

0
1 1.05 1.1 1.15 1.2 1.25
x
X- values

Figure 9.1. Comparison of Taylor series solutions with true solution


316 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Percentage errors for the various approximations are generated below and compared in Figure 9.2.
Notice that the error between the six-term/ seven -term approximation and the true solution is zero.
In general, the error is sizeable in the range of interest if anything short of a five-term approximation
is employed.

ytrue ( x ) − y6 ( x ) ytrue ( x ) − y2 ( x )
%error6 ( x ) := ⋅ 100 %error2 ( x ) := ⋅ 100
ytrue ( x ) ytrue ( x )

ytrue ( x ) − y1 ( x )
%error1 ( x ) := ⋅ 100
ytrue ( x )

x := 1.0 , 1.01 .. 3.

Comparison of Errors
100
Six/Seven terms
Two terms
80 One term

60
Percentage Error

%error6 ( x)

%error2 ( x) 40

%error1 ( x)

20

20
1 1.05 1.1 1.15 1.2 1.25
x
X- Values

Figure 9.2. Comparison of errors generated with varying number of terms in


Taylor series
Chapter 9: Numerical Solution of Ordinary Differential Equations 317

9.3 EULER'S METHOD

Euler's method is a simple, programmable, one-step method which uses information from the
beginning of an interval to estimate the function value at the end of that interval . It is based
on the forward difference equation for the first derivative only , without requiring the inclusion
of higher order derivatives .

Using the forward difference representation , the derivative of a function f(x) is

d f ( x + Δx ) − f ( x )
f (x) = (9.11)
dx Δx

If the slope of a curve y(x) at x n is known, say y' n , the function value y n+1 at an
adjacent point x n+1 is given by

y n+1 = y n + h y' n with h= x n+1 - xn (9.12)

so long as h, which is the difference between x n+1 and x n is small. ( See Figure 9.3)

Figure 9.3. Basis of Euler's method

If the problem to be solved is the following first-order initial value problem


318 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

d
y ( x) = f ( x , y) (9.13)
dx

with initial condition y (0) = y0 (9.14)

a numerical solution can be generated using Equation (9.12) which gives

(
yn+1 = y n + h ⋅ f x n , y n ) (9.15)

Now, given the initial condition (9.14), Equation 9.15 can be used to do the computations one step
at a time until the solution at a desired x value is generated, which is why, this method is also
referred to as Euler's one-step method . It is to be noted that the smaller the h, the more accurate
will be the results, and , clearly, the method is not going to be very accurate for large values of h.
Example 9.2 will illustrate the procedure of Euler's method.

Because the relationship of Equation 9.12 is based on a straight line segment between
( xn , y n ) and ( x n+1 , y n+1 ), the error due to the use of Euler's method is

(y predicted - y true)

as shown in Figure 9.4. An approximate estimate of this error is given by the second order
term of the Taylor series expansion which is

( x − x0)2 d
2
ε= ⋅ y (9.16)
2! 2
dx

Figure 9.4. Error of Euler's method


Chapter 9: Numerical Solution of Ordinary Differential Equations 319

Example 9.2

Solve: dy/dx = y' =f(x,y)=-4x 3 +18x 2 -22x +9 by Euler's method. The prescribed initial
conditions are: x=0, y=2, and the range for the solution is to be x=0 to 4.0 with a step size of
h= 0.5.

The function f(x,y) which is the derivative of y(x) is given as

3 2
f ( x , y ) := −4 ⋅ x + 18 ⋅ x − 22 ⋅ x + 9

The initial condition for this initial-value problem is prescribed as

x0 := 0 y0 := 2

By integrating the given equation, its true solution can be easily seen to be

4 3 2
ytrue ( x ) := −x + 6 ⋅ x − 11 ⋅ x + 9 ⋅ x + 2

The procedure of Euler's method is utilized as shown below to generate solutions at x =


0.5 and 1.0.

f ( x0 , y0) = 9 h := 0.5

x1 := 0.5 y1 := y0 + h ⋅ f ( x0 , y0) y1 = 6.5 f ( x1 , y1) = 2

x2 := 1.0 y2 := y1 + h ⋅ f ( x1 , y1) y2 = 7.5 f ( x2 , y2) = 1

The percentage errors between the true values at x = 0.5 and 1.0 and those computed by
Euler's method are:

ytrue ( x1) − y1
ytrue ( x1) = 4.438 error := ⋅ 100 error = −46.479
ytrue ( x1)

ytrue ( x2) − y2
ytrue ( x2) = 5 error := ⋅ 100 error = −50
ytrue ( x2)

Similarly, solutions as well as errors at x = 1.5, 2.0,.....4 can also be generated easily
320 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Alternatively, Euler solutions as well as the associated percentage errors at x = 0.5, 1.0,
1.5,........4.0. can be generated by resorting to the following procedure .

x 0 := 0 y 0 := 2 <-- Prescribed initial condition

xlast := 4 <--End-point of prescribed range of solution

h := 0.5 <-- Step size

3 2
f ( x , y ) := −4 ⋅ x + 18 ⋅ x − 22 ⋅ x + 9 <--- Prescribed derivative of y(x) at (x,y)

f ( x0 , y0) = 9 <-- Derivative value at starting point

xlast − x 0 Number of points beyond the starting point at


N := N= 8 <--
h which solution is desired

i := 0 , 1 .. N

x i+1 := x i + h
<--Formulas to be used in Euler's method

(
y i+1 := y i + h ⋅ f x i , y i )

ytruei := ytrue x i ( ) <---True solution value at x i

ytruei − y i
error i := ⋅ 100 <-- Percentage Error between true value
ytruei and value computed by Euler's method

The computed and true solutions and the error due to Euler's method for the range of interest are
tabulated and shown in Table 9.1 below.
Chapter 9: Numerical Solution of Ordinary Differential Equations 321

TABLE 9.1

Comparison between true and computed solutions and error due to Euler's method

x-value Computed solution True solution Percentage error


xi = yi = ytruei = error i =
0 2 2 0
0.5 6.5 4.438 -46.479
1 7.5 5 -50
1.5 8 5.938 -34.737
2 9.5 8 -18.75
2.5 12 10.438 -14.97
3 14 11 -27.273
3.5 12.5 5.938 -110.526
4 3 -10 130

Mathcad's cubic spline interpolation can now be resorted to as shown below to generate a plot
of the computed solution . This solution is compared with the true solution in Figure 9.5. The
errors due to Euler's method for the range of interest are presented in Figure 9.6, which shows
that the Euler error for this problem can be as large as 98 % , thus making the method
extremely inaccurate.

4 3 2
Vxi := x i Vyi := y i ytrue ( x ) := −x + 6 ⋅ x − 11 ⋅ x + 9 ⋅ x + 2

⎛0 ⎞ ⎛ 2 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.5 ⎟ ⎜ 6.5 ⎟
⎜1 ⎟ ⎜ 7.5 ⎟
⎜ 1.5 ⎟ ⎜ 8 ⎟
⎜ ⎟ ⎜ ⎟
Vx = ⎜ 2 ⎟ Vy = ⎜ 9.5 ⎟ <-- Vx and Vy vectors for Euler solution generated
⎜ 2.5 ⎟ ⎜ 12 ⎟
⎜ ⎟ ⎜ ⎟
⎜3 ⎟ ⎜ 14 ⎟
⎜ 3.5 ⎟ ⎜ 12.5 ⎟
⎜ ⎟ ⎜ ⎟
⎝4 ⎠ ⎝ 3 ⎠

Vs := cspline ( Vx , Vy)

fspline ( x ) := interp ( Vs , Vx , Vy , x ) <--Cubic spline solution generated with interp/


cspline

4 3 2
ytrue ( x ) := −x + 6 ⋅ x − 11 ⋅ x + 9 ⋅ x + 2 <-- True solution
322 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

ytrue ( x ) − fspline ( x )
%error ( x ) := ⋅ 100 <--Percentage error between computed and true
ytrue ( x ) solutions, function of x

x := 0 , 0.01 .. 4

Comparison- Euler with True solution


15
true solution
Euler solution

10
Y-VALUES

ytrue ( x)

fspline ( x)

0
0 0.5 1 1.5 2 2.5 3 3.5 4
x
X-VALUES

Figure 9.5. Comparison of Euler solution with true solution

ERROR VERSUS x
0
PERCENTAGE ERROR

50
%error ( x)

100

0 0.5 1 1.5 2 2.5 3 3.5


x
X-VALUES

Figure 9.6. Error due to Euler's method


Chapter 9: Numerical Solution of Ordinary Differential Equations 323

9.4 MODIFIED EULER'S METHOD

Euler's method which was discussed in the previous section does not require the use of
higher-order derivatives but apparently yields very inaccurate answers unless the step
size used is very very small. The modified Euler's method , which is an attempt at
improving the accuracy of the Euler solution, is quite similar and is still a one-step
method but uses an average slope over the segment or step rather than that at the
beginning of the segment . Thus, in this "modified" method, Equation (9.15) is replaced
by

( )
f x n+ 1 + f x n( )
yn+1 = y n + h ⋅ (9.17)
2

Another way to look at this method is to note that it employs the trapezoidal rule as
shown in the sketch below to compute the integral

x
⌠ n+1
⎮ f ( x ) dx
⌡x
n

The procedural steps to be followed in this method are given below.

1. Determine slope dy/dx at the beginning of the interval , x 0

2. Estimate y at x 1 using Euler's formula (Equation 9.15)

(
yn+1 = y n + h ⋅ f x n , y n )
3. Evaluate slope dy/dx at the end of the interval x 1 , i.e., evaluate f(x1 , y1 ).

4. Determine average of the slopes at the beginning and end of the interval, i.e,

( ) (
f x0 , y0 + f x1 , y1 )
2
324 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

5. Compute a revised estimate of y 1 using

( )
f x0 , y0 + f x1 , y1 ( )
y1 = y0 + h ⋅
2

6. Keep improving the estimate " y 1 " until convergence to a solution takes place .

Example 9.3

Using the modified Euler method, solve: dy/dx= f(x,y) = -3x -y; y(0) = -1.5; h= 0.1;
Range of x: 0 to 0.5

The function, f(x,y) which is the derivative of y(x) is given as

f ( x , y ) := −3 ⋅ x − y h := 0.1

The initial condition prescribed is

x0 := 0. y0 := −1.5

The procedure of the modified Euler method is utilized as shown in the following steps to generate
numerical solutions at x= 0.1 and 0.2. Solutions for x= 0.3, 0.4 and 0.5 are obtained in a similar
manner. The quantity y1c refers to the revised estimate of the solution in each iteration .

x1 := 0.1
f ( x0 , y0) = 1.5

y1 := y0 + h ⋅ f ( x0 , y0) y1 = −1.35 f ( x1 , y1) = 1.05

f ( x1 , y1) + f ( x0 , y0)
= 1.275
2
f ( x1 , y1) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.373 y1 := y1c
2
f ( x1 , y1) = 1.073 f ( x1 , y1) + f ( x0 , y0)
= 1.286
2
f ( x1 , y1) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.371 y1 := y1c
2

f ( x1 , y1) = 1.071 f ( x1 , y1) + f ( x0 , y0)


= 1.286
2
Chapter 9: Numerical Solution of Ordinary Differential Equations 325

f ( x1 , y1) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.371 y1 := y1c
2

f ( x1 , y1) = 1.071 f ( x1 , y1) + f ( x0 , y0)


= 1.286
2

f ( x1 , y1) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.371 y1 := y1c
2

x2 := 0.2

f ( x1 , y1) = 1.071

y2 := y1 + h ⋅ f ( x1 , y1) y2 = −1.264 f ( x2 , y2) = 0.664

f ( x2 , y2) + f ( x1 , y1) f ( x2 , y2) + f ( x1 , y1)


= 0.868 y2c := y1 + ⋅h
2 2

y2c = −1.285 y2 := y2c f ( x2 , y2) = 0.685

f ( x2 , y2) + f ( x1 , y1)
= 0.878
2

f ( x2 , y2) + f ( x1 , y1)
y2c := y1 + ⋅ h y2c = −1.284 y2 := y2c
2

f ( x2 , y2) + f ( x1 , y1)
f ( x2 , y2) = 0.684 = 0.878
2

f ( x2 , y2) + f ( x1 , y1)
y2c := y1 + ⋅ h y2c = −1.284 y2 := y2c
2

f ( x2 , y2) + f ( x1 , y1)
f ( x2 , y2) = 0.684 = 0.878
2

f ( x2 , y2) + f ( x1 , y1)
y2c := y1 + ⋅h y2c = −1.284 y2 := y2c
2

At x3, x4, and x5 , the solutions can be similarly generated as :


326 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x3 := 0.3 y3 := −1.2328

x4 := 0.4 y4 := −1.2154

x5 := 0.5 y5 := −1.2283

The results obtained by this method are presented in Table 9.2 in a tabular format for x- values
ranging from 0 to 0.5 with a step size of 0.1. Converged values of the solution are pointed out in the
" corrected yn " column .

TABLE 9.2

Calculations by Modified Euler method

xn yn f(xn,yn) avg slope corrected


at beginning yn
and end

0 -1.5 1.5

0.1 -1.35 1.05 1.275 -1.3725


-1.3725 1.0725 1.2862 -1.3714
-1.3714 1.0714 1.2857 -1.3714
-1.3714 1.0714 1.2857 -1.3714 converged

0.2 -1.2643 0.6643 0.8679 -1.2846


-1.2846 0.6846 0.878 -1.2836
-1.2836 0.6836 0.8775 -1.2837
-1.2837 0.6837 0.8776 -1.2837 converged

0.3 -1.2153 0.3153 0.4995 -1.2337


-1.2337 0.3337 0.5087 -1.2338
-1.2328 0.3328 0.5082 -1.2338 converged

0.4 -1.1996 -0.0004 0.1662 -1.2162


-1.2162 0.0162 0.1745 -1.2154
-1.2154 0.0154 0.1741 -1.2154 converged

0.5 -1.2139 -0.2861 -0.1353 -1.229


-1.229 -0.271 -0.1278 -1.2282
-1.2282 -0.2718 -0.1282 -1.2283
-1.2283 -0.2717 -0.1282 -1.2283 converged

The x values and the computed "y" values are assembled in the form of vectors Vx and Vy in order
to generate a cubic spline interpolation as follows which is presented as a plot of the solution in
Figure 9.7.
Chapter 9: Numerical Solution of Ordinary Differential Equations 327

⎛ x0 ⎞ ⎛⎜ 0 ⎞⎟ ⎛ y0 ⎞ ⎛⎜ −1.5 ⎞⎟
⎜ ⎟ ⎜ ⎟
⎜ x1 ⎟ ⎜ 0.1 ⎟ ⎜ y1 ⎟ ⎜ −1.371 ⎟
⎜ x2 ⎟ ⎜ 0.2 ⎟ ⎜ y2 ⎟ ⎜ −1.284 ⎟ <--Vx and Vy vectors for
Vx := ⎜ ⎟ Vx = ⎜ ⎟ Vy := ⎜ ⎟ Vy = ⎜ ⎟ modified Euler solution
⎜ x3 ⎟ ⎜ 0.3 ⎟ ⎜ y3 ⎟ ⎜ −1.233 ⎟ generated
⎜ x4 ⎟ ⎜ 0.4 ⎟ ⎜ y4 ⎟ ⎜ −1.215 ⎟
⎜ ⎟ ⎜ 0.5 ⎟ ⎜ ⎟ ⎜ −1.228 ⎟
⎝ x5 ⎠ ⎝ ⎠ ⎝ y5 ⎠ ⎝ ⎠

Vs := cspline ( Vx , Vy)

fspline ( x ) := interp ( Vs , Vx , Vy , x ) <-- cubic spline interpolation for the generated


solution

A solution by the Euler method is generated below in order to obtain a comparison between the
Modified Euler solution and the not-so-accurate Euler solution

x 0 := 0 y 0 := −1.5 <-- Prescribed initial condition

xlast := 0.5 <--End-point of prescribed range of solution

h := 0.1 <-- Step size

f ( x , y ) := −3 ⋅ x − y <--- Prescribed derivative of y(x) at (x,y)

f ( x0 , y0) = 1.5 <-- Derivative value at starting point

xlast − x 0 Number of points beyond the starting point at


N := N= 5 <--
h which solution is desired

i := 0 , 1 .. N x i+1 := x i + h
<--Formulas of Euler's method

(
y i+1 := y i + h ⋅ f x i , y i )
328 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

x-value Computed solution


xi = yi =
0 -1.5
0.1 -1.35
0.2 -1.245 < --- Euler solution
0.3 -1.181
0.4 -1.152
0.5 -1.157

Mathcad's cubic spline interpolation is now used below to generate a plot of the computed Euler
solution, which is compared with the Modified Euler solution in Figure 9.7.

Vxeuler i := x i Vyeuler i := y i

⎛⎜ 0 ⎞⎟ ⎛⎜ −1.5 ⎞⎟
⎜ 0.1 ⎟ ⎜ −1.35 ⎟
⎜ 0.2 ⎟ ⎜ −1.245 ⎟
Vxeuler = ⎜ ⎟ Vyeuler = ⎜ ⎟ <-- Vx and Vy vectors for Euler solution generated
⎜ 0.3 ⎟ ⎜ −1.181 ⎟
⎜ 0.4 ⎟ ⎜ −1.152 ⎟
⎜ 0.5 ⎟ ⎜ −1.157 ⎟
⎝ ⎠ ⎝ ⎠
Vseuler := cspline ( Vxeuler , Vyeuler )
Cubic spline solution using the
fsplineeuler ( x ) := interp ( Vseuler , Vxeuler , Vyeuler , x ) <--- Euler method generated with
interp / cspline

x := 0 , 0.01 .. 0.5

Euler/ Mod Euler solutions compared


1

1.2
Y- Values

fsplineeuler ( x)

fspline ( x)
1.4
Euler solution
Modified Euler solution
1.6
0 0.1 0.2 0.3 0.4
x
X- Values
Figure 9.7. Comparison of the modified Euler solution with
the Euler solution .
Chapter 9: Numerical Solution of Ordinary Differential Equations 329

9.5 RUNGE- KUTTA METHODS

Runge-Kutta methods are among the most popular techniques employed in the numerical
solution of ordinary differential equations. These methods use the y -value at the beginning of
an interval along with some representative slope over the interval to determine the y -value at
the end of the interval. Special cases of the method are Euler's method , which uses the
slope at the beginning of the interval and the modified Euler method which uses the average
slope over the segment .

There are several orders of Runge-Kutta methods from the first order to the eighth
depending on the number of iterations involved in the solution -generating process.
However, only the fourth-order Runge-Kutta method will be disussed in detail in this
chapter. All of these still resort only to the use of the first derivative and do not require the
involvement of higher order derivatives. While the second-order Runge-Kutta method is
essentially the same as the modified Euler method, and offers the same order of accuracy,
the main difference is that it resorts only to two iterations. The third-order Runge-Kutta
method resorts to Simpson's one-third rule , which is a higher-order integration scheme
than the trapezoidal rule , and uses slopes at the beginning, middle and end of the
segment in order to estimate the y-value at the end of the segment . As its name clearly
suggests , the fourth-order method does require four iterations to determine the y-value at
the segment end. Because higher-order integration schemes are employed, the
Runge-Kutta fourth-order method gives very accurate results. Orders of the method that are
higher than fourth are not common .

Advantages of Runge-Kutta methods include ease of programming, good stability


characteristics, flexibility of step size, and self-starting capability.

9.5.1 Fourth-order Runge-Kutta Method

The fourth-order Runge-Kutta method comes in two popular versions . The first version is
based on Simpson's one-third rule while the second is based on Simpson's three-eighth
rule. The sequence of calculations to be used in these versions is given below.

Procedure for version 1- based on Simpson's one-third rule:

Figure 9.8. Basis of the fourth-order Runge-Kutta method utilizing


Simpson's one- third rule
330 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Given : dy/dx = f(x,y) = Slope , y= y 0 at x= x0 ( Initial condition ),


h = interval width.

Determine: y 1 , which is y ( x 1 ). ( See Figure 9.8)

1. Compute h times slope K1 at ( x0 , y0 ): K1 = h. f(x0 ,y0 )

2. Compute y at midpoint of interval x 0 to x 1 using the Euler formula

1
y MP = y 0 + ⋅ K1 (9.18)
2

3. Compute K2 at x = x MP, y= yMP

(
K2 = h ⋅ f x MP , y MP )
4. Obtain a revised estimate of y MP with y 0 , K2 and (1/2)h

1
y MP = y 0 + ⋅ K2 (9.19)
2
5. Compute revised estimate of K at mid-interval, using above value of y MP

(
K3 = h ⋅ f x MP , y MP ) (9.20)

6 Compute y 1 using: . y 1 = y 0 + K3 (9.21)

7. Compute K at end of interval: ( )


K4 = h ⋅ f x 1 , y 1 (9.22)

8. Compute revised estimate of y 1 using:

( )
1
y1 = y0 + ⋅ K 1 + 2 ⋅ K2 + 2 ⋅ K3 + K4 (9.23)
6

per Simpson's 1/3 rd rule. Notice the four estimates of slope being used

9. Repeat above steps for computing y 2 , y3 etc. For example, to compute y 2 , repeat the
procedure with (x1 ,y1 ) replacing (x0 ,y0 ) and (x2 ,y2 ) replacing (x1 ,y1 ).

Procedure for version 2- based on Simpson's three-eighth rule :

The procedure for version 2 is very similar to version 1, except that the range x 0 to x 1 is
Chapter 9: Numerical Solution of Ordinary Differential Equations 331

subdivided into three regions , the starting and ending points of which are x 0 , x13 , x23 and
x 1 . The slopes at these intermediate points within the range x 0 to x 1 are computed and
revised until y 1 is determined as shown in the following summary of calculations.

Summary of calculations for versions 1 and 2:

VERSION 1- based on Simpson's one-third rule:

(
K1 = h ⋅ f x 0 , y 0 )
1
y MP = y 0 + ⋅ K1
2

(
K2 = h ⋅ f x MP , y MP )
1
y MP = y 0 + ⋅ K2 Revised y MP (using h times (9.24)
2 <-----
the new slope)

(
K3 = h ⋅ f x MP , y MP )
y 1 = y 0 + K3

(
K4 = h ⋅ f x 1 , y 1 )

( )
1
y1 = y0 + ⋅ K 1 + 2 ⋅ K2 + 2 ⋅ K3 + K4
6

VERSION 2- based on Simpson's three-eighth rule:

( )
1 2
x 13 = x 0 + ⋅h x 23 = x 0 + ⋅h K1 = h ⋅ f x 0 , y 0
3 3

( )
1 1 1
y 13 = y 0 + ⋅ K1 K2 = h ⋅ f x 13 , y 13 y 23 = y 0 + ⋅ K1 + ⋅ K2
3 3 3

Note that y 0 +(1/3)K1 is, in fact, y 13


(9.25)

(
K3 = h ⋅ f x 23 , y 23 ) y 1adj = y 0 + K1 − K2 + K3 <---
" K1 - K 2 + K 3 "
represents h
multiplied by a
(
K4 = h ⋅ f x 1 , y 1adj ) representative average
slope over segment
x 0 -> x1
( )
1
y1 = y0 + ⋅ K 1 + 3 ⋅ K2 + 3 ⋅ K3 + K4
8
332 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 9.4

Solve: dy/dx = f(x,y) = -2x 2 - y ; Initial condition : y(0) = -1.75 ;


h = 0.1; Range of x : 0 to 0.5

Version 1 - based on Simpson's one-third rule:

The given information is.

2
f ( x , y ) := −2 ⋅ x − y h := 0.1 x0 := 0 y0 := −1.75

Calculations for x= x1 and x= x2 are shown below

x1 := 0.1

f ( x0 , y0) = 1.75

k1 := h ⋅ f ( x0 , y0) k1 = 0.175 ymp := y0 + 0.5 ⋅ k1 ymp = −1.6625

xmp := x0 + 0.5 ⋅ h xmp = 0.05 k2 := h ⋅ f ( xmp , ymp) k2 = 0.166


ymp := y0 + 0.5 ⋅ k2 ymp = −1.6671 k3 := h ⋅ f ( xmp , ymp) k3 = 0.166
y1 := y0 + k3 y1 = −1.584 k4 := h ⋅ f ( x1 , y1) k4 = 0.156

⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
This formula is based on <-- y1 := y0 + ⎜6⎟ y1 = −1.584
Simpson's one-third rule ⎝ ⎠

x2 := 0.2

f ( x1 , y1) = 1.564

k1 := h ⋅ f ( x1 , y1) k1 = 0.156 ymp := y1 + 0.5 ⋅ k1 ymp = −1.50591

xmp := x1 + 0.5 ⋅ h xmp = 0.15 k2 := h ⋅ f ( xmp , ymp) k2 = 0.146


ymp := y1 + 0.5 ⋅ k2 ymp = −1.5111 k3 := h ⋅ f ( xmp , ymp) k3 = 0.147
y2 := y1 + k3 y2 = −1.438 k4 := h ⋅ f ( x2 , y2) k4 = 0.136
⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
y2 := y1 + ⎜6⎟ y2 = −1.438
⎝ ⎠

Calculations for x = 0.3, 0.4 and 0.5 are similar to the above and lead to
Chapter 9: Numerical Solution of Ordinary Differential Equations 333

x3 := 0.3 y3 := −1.31316 x4 := 0.4 y4 := −1.21178 x5 := 0.5 y5 := −1.13531

In order to generate a cubic spline interpolation for the Runge-Kutta solution, the x and y values
are put into vectors Vx and Vy. The resulting y is plotted as a function of x in Figure 9.9.

⎛ x0 ⎞ ⎛ y0 ⎞ ⎛⎜ 0 ⎞⎟ ⎛⎜ −1.75 ⎞⎟
⎜ ⎟ ⎜ ⎟
⎜ x1 ⎟ ⎜ y1 ⎟ ⎜ 0.1 ⎟ ⎜ −1.584 ⎟
⎜ x2 ⎟ ⎜ y2 ⎟ ⎜ 0.2 ⎟ ⎜ −1.438 ⎟ <-- Vx and Vy vectors
Vx := ⎜ ⎟ Vy := ⎜ ⎟ Vx = ⎜ ⎟ Vy = ⎜ ⎟ for the Runge-Kutta
⎜ x3 ⎟ ⎜ y3 ⎟ ⎜ 0.3 ⎟ ⎜ −1.313 ⎟ results generated
⎜ x4 ⎟ ⎜ y4 ⎟ ⎜ 0.4 ⎟ ⎜ −1.212 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ 0.5 ⎟ ⎜ −1.135 ⎟
⎝ x5 ⎠ ⎝ y5 ⎠ ⎝ ⎠ ⎝ ⎠

Vs := cspline ( Vx , Vy)

cubic spline interpolation for the


fspline1 ( x ) := interp ( Vs , Vx , Vy , x ) <-----
solution generated

Version 2 - based on Simpson's three-eighth rule:

2
f ( x , y ) := −2 ⋅ x − y h := 0.1 x0 := 0 y0 := −1.75 f ( x0 , y0) = 1.75

x1 := 0.1

k1 := h ⋅ f ( x0 , y0) k1 = 0.175 y13 := y0 + 0.33333 ⋅ k1 y13 = −1.69167

⎛ 1 ⎞ ⋅ h x13 = 0.033
x13 := x0 + ⎜3⎟ k2 := h ⋅ f ( x13 , y13) k2 = 0.169
⎝ ⎠
⎛2⎞
x23 := x0 + ⎜ ⎟ ⋅ h x23 = 0.067 y23 := y0 +
k1
+
k2
y23 = −1.635
⎝3⎠ 3 3
k3 := h ⋅ f ( x23 , y23) k3 = 0.163

x1 := x0 + h y1adj := y0 + k1 − k2 + k3 y1adj = −1.581


k4 := h ⋅ f ( x1 , y1adj ) k4 = 0.156

⎛ 1 ⎞ ⋅ ( k1 + 3 ⋅ k2 + 3 ⋅ k3 + k4)
This formula is based on <-- y1 := y0 + ⎜8⎟ y1 = −1.584
Simpson's 3/8 th rule ⎝ ⎠

x2 := 0.2
334 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

k1 := h ⋅ f ( x1 , y1) k1 = 0.156 y13 := y1 + 0.33333 ⋅ k1 y13 = −1.53212

⎛ 1 ⎞ ⋅ h x13 = 0.133 k2 := h ⋅ f ( x13 , y13)


x13 := x1 + ⎜3⎟ k2 = 0.15
⎝ ⎠
⎛2⎞
x23 := x1 + ⎜ ⎟ ⋅ h x23 = 0.167 y23 := y1 +
k1
+
k2
y23 = −1.482
⎝3⎠ 3 3
k3 := h ⋅ f ( x23 , y23) k3 = 0.143

x2 := x1 + h y2adj := y1 + k1 − k2 + k3 y2adj = −1.435


k4 := h ⋅ f ( x2 , y2adj ) k4 = 0.135

⎛ 1 ⎞ ⋅ ( k1 + 3 ⋅ k2 + 3 ⋅ k3 + k4)
This formula is based on <--- y2 := y1 + ⎜8⎟ y2 = −1.438
Simpson's 3/8 th rule ⎝ ⎠

Calculations for x = 0.3 , 0.4 and 0.5 are again very similar to the above and lead to:
x3 := 0.3 y3 := −1.31361 x4 := 0.4 y4 := −1.21238 x5 := 0.5 y5 := −1.13605

Generation of the Vx and Vy vectors and fspline(x) as in version 1, leads to the graph in
Figure 9.9 which compares the two solutions of the fourth-order Runge-Kutta method.
These plots definitely indicate that the two solutions are very very close to each other.

⎛ x0 ⎞ ⎛ y0 ⎞
⎜ ⎟ ⎜ ⎟ ⎛⎜ 0 ⎞⎟ ⎛⎜ −1.75 ⎞⎟
⎜ x1 ⎟ ⎜ y1 ⎟ ⎜ 0.1 ⎟ ⎜ −1.584 ⎟
⎜ x2 ⎟ ⎜ y2 ⎟ Vs := cspline ( Vx , Vy)
Vx := ⎜
⎜ 0.2 ⎟ ⎜ −1.438 ⎟
⎟ Vy := ⎜ ⎟ Vx = ⎜ ⎟ Vy = ⎜ ⎟
⎜ ⎟
x3 ⎜ y3 ⎟ fspline2 ( x ) := interp ( Vs , Vx , Vy , x )
⎜ 0.3 ⎟ ⎜ −1.314 ⎟
⎜ x4 ⎟ ⎜ y4 ⎟ ⎜ 0.4 ⎟ ⎜ −1.212 ⎟
⎜ ⎟ ⎜ ⎟ fspline2 ( 0.5) = −1.136
⎝ x5 ⎠ ⎝ y5 ⎠ ⎜ 0.5 ⎟ ⎜ −1.136 ⎟
⎝ ⎠ ⎝ ⎠
x := 0 , 0.01 .. 0.5

Fourth Order R/K Method


1
Version 1-one-third rule
Version 2-three-eighth rule
Y-VALUES

fspline1( x)
1.5
fspline2( x)

2
0 0.1 0.2 0.3 0.4
x
X-VALUES
Figure 9.9 Solution by fourth-order Runge-Kutta method
Chapter 9: Numerical Solution of Ordinary Differential Equations 335

9.5.2 Mathcad Solutions to a First-Order Differential Equation

The Mathcad function " rkfixed" is a general-purpose Runge-Kutta solver which can handle
both nth order differential equations with initial conditions as well as systems of differential
equations.

The arguments to the function rkfixed are

rkfixed( y,x1, x2, npoints, D)

where y is a vector of n initial values, n being the order of the differential equation or the
size of the system of equations being solved. In this case, where we have a first-order
differential equation, the vector reduces to one point. which is the initial value of y . The
quantities x1 and x2 are the endpoints of the interval over which the solution is to be
generated, and npoints is the number of points beyond the initial point at which the
solution is to be approximated. This controls the number of rows ( 1+ npoints ) in the
matrix that rkfixed returns. Here, D(x,y) is an n -element vector function containing the
first derivatives of the unknown functions. and, for a first-order differential equation, this will
be just one function , namely the given derivative.

Another Mathcad function that can be used is "Rkadapt". While "rkfixed" evaluates the
solution at equally spaced intervals , " Rkadapt " examines how fast the solution is
changing and adjusts the step size accordingly. Although "Rkadapt " may use non-uniform
step sizes in generating the solution, it will still return a solution at equally spaced points.
The arguments used by " Rkadapt " are the same as those of " rkfixed " .

To illustrate the use of these Mathcad functions, the Mathcad solution to the problem of
Example 9.1 presented in the section on the Taylor series method is outlined below.

Define the initial y value: Use the left bracket " [ " to make the subscript:

y 0 := 4.0

Put in the first derivative that is given: yprime = 7x 4 -3 x 2 + 9

4 2
D ( x , y ) := 7 ⋅ x − 3 ⋅ x + 9

Evaluate the solution at 50 points between 1.0 and 1.3

Z := rkfixed ( y , 1.0 , 1.3 , 50 , D)


336 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

i := 0 .. rows ( Z) − 1

Plot the solution. Use Ctrl + 6 to get the superscript. ( See Figure 9.10).

To use Rkadapt , the procedure, which is presented below , is essentially the same as
with the use of rkfixed.

Define the initial y value: Use the left bracket " [ " to make the subscript:

y 0 := 4.0

Put in the first derivative that is given: yprime = 7x4 - 3 x 2 + 9

4 2
D ( x , y ) := 7 ⋅ x − 3 ⋅ x + 9

Evaluate the solution at 50 points between 1.0 and 1.3

Y := Rkadapt ( y , 1.0 , 1.3 , 50 , D)

i := 0 .. rows ( Y) − 1

Plot the solution. Use Ctrl + 6 to get the superscript. (See Figure 9.10 ).

The solutions to the differential equation obtained with rkfixed and Rkadapt are presented
in Figure 9.10 and compared with the Taylor series solution, which is redone here in order to
obtain its plot.

4 2 3
x0 := 1 y0 := 4.0 yprime ( x ) := 7 ⋅ x − 3 ⋅ x + 9 ydblprime ( x ) := 28 ⋅ x − 6 ⋅ x

2
ytrprime ( x ) := 84 ⋅ x − 6 y4prime ( x ) := 168 ⋅ x y5prime ( x ) := 168. y6prime ( x ) := 0

x := 1.0 , 1.05 .. 1.3


Chapter 9: Numerical Solution of Ordinary Differential Equations 337

⎛ 1 ⎞ ⋅ ( x − x0) 2 ⋅ ydblprime ( x0)


y ( x ) := y0 + ( x − x0) ⋅ yprime ( x0) + ⎜ ⎟ ...
⎝ 2! ⎠
⎛ 1 ⎞ ⋅ ( x − x0) 3 ⋅ ytrprime ( x0)
+ ⎜ ⎟ ...
⎝ 3! ⎠ <-- Taylor series
⎛1⎞ 4 ⎛1⎞ 5 solution
+ ⎜ ⎟ ⋅ ( x − x0) ⋅ y4prime ( x0) + ⎜ ⎟ ⋅ ( x − x0) ⋅ y5prime ( x0) ...
⎝ 4! ⎠ ⎝ 5! ⎠
⎛1⎞ 6
+ ⎜ ⎟ ⋅ ( x − x0) ⋅ y6prime ( x0)
⎝ ⎠
6!

〈0〉
x i := Y i ( ) ( )
y i := y x i

Comparison of solutions
10
Rkadapt solution
( Y〈1〉 ) i rkfixed solution
8 Taylor series solution
Y-Values

( )
〈1〉
Z i

6
yi

4
1 1.05 1.1 1.15 1.2 1.25

( )
〈0〉
Y i

X-Values

Figure 9.10. Comparison of Mathcad solutions with the Taylor series solution

As can be seen from the solution plots, the Mathcad solutions agree well with the Taylor series
solution.

Example 9.5

Using the Mathcad functions, solve the problem of Example 9.3 presented in the section on the
modified Euler method, namely,

dy/dx= f(x,y) = -3x -y; y(0) = -1.5 ; h= 0.1; Range of x: 0 to 0.5

The Mathcad statements necessary for the computation of the solution with rkfixed as well as
Rkadapt are
338 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

y 0 := −1.5 D ( x , y ) := −3 ⋅ x − y Z := rkfixed ( y , 0. , 0.5 , 50 , D) i := 0 .. rows ( Z) − 1

Y := Rkadapt ( y , 0. , 0.5 , 50 , D)

The modified Euler solution is partially redone here in the following steps in order to generate a
solution plot.

⎛ 0 ⎞ ⎛ −1.5 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.1 ⎟ ⎜ −1.3714 ⎟
⎜ 0.2 ⎟ ⎜ −1.2837 ⎟
Vx := ⎜ ⎟ Vy := ⎜ ⎟ <--- Vx and Vy vectors for the modified Euler
⎜ 0.3 ⎟ ⎜ −1.2328 ⎟ solution
⎜ 0.4 ⎟ ⎜ −1.2154 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 0.5 ⎠ ⎝ −1.2283 ⎠

Vs := cspline ( Vx , Vy) fspline ( x ) := interp ( Vs , Vx , Vy , x ) <---- modified Euler


solution

x := 0 , 0.01 .. 0.5 ( )
〈0〉
x i := Y i fspline i := fspline x i( )

Figure 9.11 shows a comparison of the Mathcad solutions with the modified Euler solution. The
agreement between the results is, clearly, very good.

Comparison of solutions

( Z〈1〉 ) i
Y-VALUES

1.3
( )
〈1〉
Y i

1.4 rkfixed solution


fspline i
Rkadapt solution
modified Euler solution
1.5
0 0.1 0.2 0.3 0.4

( )
〈0〉
Z i

X-VALUES

Figure 9.11. Comparison of Mathcad solutions with modified Euler method


Chapter 9: Numerical Solution of Ordinary Differential Equations 339

Example 9.6

Using the Mathcad functions , solve the problem of Example 9.4 presented in the sub-section
on the Runge-Kutta fourth order method, which is repeated below.

Solve: dy/dx = f(x,y) = - 2x 2 - y ; Initial condition : y(0) = -1.75 ;


h = 0.1; Range of x: 0 to 0.5

The Mathcad statements needed to solve the problem are

2
y 0 := −1.75 D ( x , y ) := −2 ⋅ x − y Z := rkfixed ( y , 0. , 0.5 , 50 , D)

Y := Rkadapt ( y , 0. , 0.5 , 50 , D) i := 0 .. rows ( Z) − 1

The Runge-Kutta solutions (Versions 1 and 2 ) are repeated here in the following steps in order to
generate solution plots in Figure 9.12 for comparison with the solutions generated with the
Mathcad functions rkfixed and Rkadapt.

⎛ 0.0 ⎞ ⎛ −1.75 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.1 ⎟ ⎜ −1.58412 ⎟
⎜ 0.2 ⎟ ⎜ −1.43786 ⎟ Vx and Vy vectors for the Runge-Kutta
Vx1 := ⎜ ⎟ Vy1 := ⎜ ⎟ <------ solution (Version 1 - one-third rule)
⎜ 0.3 ⎟ ⎜ −1.31316 ⎟
⎜ 0.4 ⎟ ⎜ −1.21178 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 0.5 ⎠ ⎝ −1.13531 ⎠
Vs1 := cspline ( Vx1 , Vy1 ) fspline1 ( x ) := interp ( Vs1 , Vx1 , Vy1 , x ) <-- R/K solution (Version 1)

( )
〈0〉
fspline1i := fspline1 ⎡ Z i⎤
⎣ ⎦
⎛ 0 ⎞ ⎛ −1.75 ⎞
⎜ ⎟ ⎜ ⎟
⎜ 0.1 ⎟ ⎜ −1.58426 ⎟
⎜ 0.2 ⎟ ⎜ −1.43815 ⎟
Vx2 := ⎜ ⎟ Vy2 := ⎜ ⎟ <---- Vx and Vy vectors for the Runge-Kutta
⎜ 0.3 ⎟ ⎜ −1.31361 ⎟ solution (Version 2- three-eighth rule )
⎜ 0.4 ⎟ ⎜ −1.21238 ⎟
⎜ ⎟ ⎜ ⎟
⎝ 0.5 ⎠ ⎝ −1.13605 ⎠
340 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Vs2 := cspline ( Vx2 , Vy2 ) fspline2 ( x ) := interp ( Vs2 , Vx2 , Vy2 , x ) <--R/K solution- version 2


〈0〉
fspline2i := fspline2 ⎡ Z i⎤

( ) x := 0 , 0.01 .. 0.5

Comparison of solutions
1

( Z〈1〉 ) i 1.2

( Y〈1〉 ) i
y-values

1.4
fspline1i

fspline2i rkfixed solution


1.6 Rkadapt solution
version 1- one-third rule
version 2- three-eighth rule
1.8
0 0.1 0.2 0.3 0.4 0.5

( )
〈0〉
Z i

x- values

Figure 9.12 Comparison of Mathcad solutions with Runge-Kutta fourth order


methods

The functions " rkfixed " and " Rkadapt " generate the solution to the given differential
equation for a number of uniformly spaced x values in the integration range bounded by x1
and x2 . If you are interested in seeing the solution only at a value of x, say x2 , it is
preferable to resort to the function " rkadapt " . Although " rkfixed " and " Rkadapt " will give
you the solution at x2 , they also go through a lot of unnecessary work returning intermediate
values of the solution at x- values in which you may not have any interest at all. The use of the
function " rkadapt " is illustrated below:

rkadapt ( y, x1, x2, acc, D, kmax, save)

where y is a vector of initial values, x1 and x2 are the endpoints of the interval of interest
over which the solution to the differential equation is to be generated. The initial values in " y "
are the values, then, at x1. The parameter "acc" controls the accuracy of the solution.
Chapter 9: Numerical Solution of Ordinary Differential Equations 341

Small values of " acc " yield accurate solutions. A value of " acc " around 0.001 is
recommended. The parameter " kmax " is the maximum number of intermediate points at
which the solution will be computed. This value places an upper limit on the number of rows of
the matrix returned by " rkadapt" . The parameter " save " is the smallest allowable spacing
between the x- values at which the solutions are to be computed .

This command computes y values for the x-


Y := rkadapt ( y , 0 , 0.5 , 0.001 , D , 50 , 0.001 ) <----
values given in the first column.

x y
This is the solution returned by "rkadapt" for
0 1 the interval x= 0 to 0.5, with acc= 0.001. The
0 0 -1.75 solution is generated at kmax = 50 points in
Y= 1 5·10-3 -1.741272 the interval where the maximum allowable
<---- spacing between the x-values at which the
2 0.03 -1.698298
solution is to be computed is save = 0.001.
3 0.155 -1.501116 The last row is the solution generated by
4 0.5 -1.135307 "Rkadapt" at the endpoint x= 0.5

9.6 SYSTEMS OF ORDINARY DIFFERENTIAL EQUATIONS

Sometimes, in engineering and the physical sciences, the need to solve a system of first-order
ordinary differential equations is encountered. Such a system is

dy1 /dx = f ( x, y1 , y2 , ....... yn )

dy2 /dx = g ( x, y1 , y2 , ....... yn )


(9.26)

..................................................

dyn /dx = z ( x, y1 , y2 , ....... yn )

with n initial conditions specified at the starting value of the independent variable x . This
system of equations can be easily solved using any of the methods discussed in sections 9.2-
9.5. However, here, the application of the the fourth order Runge-Kutta procedure to obtain a
solution to a problem involving a pair of first-order differential equations will be illustrated. Notice
that in a problem involving a pair of differential equations , where x and y are the dependent
variables and t is the independent variable , x and y calculations have to be alternately
performed as illustrated in the following example.
342 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Example 9.7.

Using the Runge-Kutta fourth-order method, obtain a solution to

dx/dt = f(x,y,t) = xy2 + t; dy/dt= g(x,y,t) = ty + x 2

for t = 0 to t= 0.5 seconds. The initial conditions are given as: x(0)= -1; y (0) = 1
Use a time increment of h = 0.1 second.

Note that both x and y are functions of the independent variable " t".

Thus, in this case,

dx/dt = f(x,y,t ) = xy2 + t ,

and dy/dt = g(x,y,t) = ty + x 2

Version 1 of the Runge-Kutta Method which utilizes Simpson's one-third rule, will then resort
to the following sequential equations to compute x and y at t1, where t1 = t0 + h., and h is
the increment or step size

k1 = h f(x0, y0, t0) ; m1= h g(x0,y0, t0) ;

tmp = t0 + 0.5 h ; xmp= x0+ 0.5 k1; ymp= y0+0.5 m1

k2= h f(xmp, ymp, tmp) ; m2 = h g( xmp, ymp, tmp )

xmp* = x0 + 0.5 k2 ; ymp* = y0+ 0.5 m2

k3 = h f ( xmp*, ymp*, tmp ) ; m3 = h g ( xmp*, ymp*, tmp )

x1 = x0 + k3 ; y1 = y0 + m3 ; t1 = t0 + h

k4 = h f ( x1, y1, t1 ) ; m4 = h g ( x1, y1, t1 )

x1= x0 + (1/6) [ k1 +2 k2 + 2 k3 + k4 ] ; y1 = y0 + (1/6 ) [ m1 + 2 m2 + 2 m3 + m4 ]

In order to compute the solution (x1,y1) at t1, we start with known values x0, y0 and t0 as
shown above. Similarly, to compute the solution (x2, y2) at t2, the above procedure needs to
be repeated with ( x1, y1 , t1) replacing (x0, y0,t0) and (x2,y2,t2) replacing (x1,y1,t1).

Define the functions f(x,y,t) and g(x,y,t), the step size and the initial conditions:.
Chapter 9: Numerical Solution of Ordinary Differential Equations 343

2 2
f ( x , y , t) := x ⋅ y + t g ( x , y , t) := t ⋅ y + x <--Define functions f(x,y,t) and g(x,y,t)

h := 0.1 <--Time increment or step size

t0 := 0 x0 := −1 y0 := 1 <-- Given initial conditions

Calculations for generating solution at t1:

t1 := t0 + h t1 = 0.1

k1 := h ⋅ f ( x0 , y0 , t0) k1 = −0.1

m1 := h ⋅ g ( x0 , y0 , t0) m1 = 0.1

tmp := t0 + 0.5 ⋅ h tmp = 0.05

xmp := x0 + 0.5 ⋅ k1 xmp = −1.05

ymp := y0 + 0.5 ⋅ m1 ymp = 1.05

k2 := h ⋅ f ( xmp , ymp , tmp) k2 = −0.110763

m2 := h ⋅ g ( xmp , ymp , tmp) m2 = 0.1155

xmpstar := x0 + 0.5 ⋅ k2 xmpstar = −1.055381

ympstar := y0 + 0.5 ⋅ m2 ympstar = 1.05775

k3 := h ⋅ f ( xmpstar , ympstar , tmp) k3 = −0.11308

m3 := h ⋅ g ( xmpstar , ympstar , tmp) m3 = 0.116672

x1 := x0 + k3 x1 = −1.11308

y1 := y0 + m3 y1 = 1.116672

t1 := t0 + h t1 = 0.1

k4 := h ⋅ f ( x1 , y1 , t1) k4 = −0.128796

m4 := h ⋅ g ( x1 , y1 , t1) m4 = 0.135061
344 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
x1 := x0 + ⎜ ⎟ x1 = −1.112747
⎝ 6⎠
1
y1 := y0 + ⋅ ( m1 + 2 ⋅ m2 + 2 ⋅ m3 + m4) y1 = 1.116567
6

Calculations for generating solution at t2:

t2 := t1 + h t2 = 0.2

k1 := h ⋅ f ( x1 , y1 , t1) k1 = −0.128729

m1 := h ⋅ g ( x1 , y1 , t1) m1 = 0.134986

tmp := t1 + 0.5 ⋅ h tmp = 0.15

xmp := x1 + 0.5 ⋅ k1 xmp = −1.177111

ymp := y1 + 0.5 ⋅ m1 ymp = 1.184061

k2 := h ⋅ f ( xmp , ymp , tmp) k2 = −0.150031

m2 := h ⋅ g ( xmp , ymp , tmp) m2 = 0.15632

xmpstar := x1 + 0.5 ⋅ k2 xmpstar = −1.187762

ympstar := y1 + 0.5 ⋅ m2 ympstar = 1.194727

k3 := h ⋅ f ( xmpstar , ympstar , tmp) k3 = −0.154538

m3 := h ⋅ g ( xmpstar , ympstar , tmp) m3 = 0.158999

x2 := x1 + k3 x2 = −1.267285

y2 := y1 + m3 y2 = 1.275566

k4 := h ⋅ f ( x2 , y2 , t2) k4 = −0.186196

m4 := h ⋅ g ( x2 , y2 , t2) m4 = 0.186112
Chapter 9: Numerical Solution of Ordinary Differential Equations 345

⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
x2 := x1 + ⎜ ⎟ x2 = −1.266757
⎝ 6⎠
1
y2 := y1 + ⋅ ( m1 + 2 ⋅ m2 + 2 ⋅ m3 + m4) y2 = 1.27519
6

Caculations for t= 0.3, 0.4 and 0.5 seconds can be performed in a similar way , giving the
following results.

t3 := 0.3 x3 := −1.506 y3 := 1.5

t4 := 0.4 x4 := −1.945 y4 := 1.848

t5 := 0.5 x5 := −3.025 y5 := 2.522

Plots of the results of the Runge-Kutta process can be obtained by an interpolation procedure
involving the generation of the Vt, Vx and Vy vectors and the spline functions xspline(t) and
yspline(t) as shown below.

⎛ t0 ⎞
⎜ ⎟ ⎛ x0 ⎞ ⎛ y0 ⎞
⎜ ⎟ ⎜ ⎟
⎜ t1 ⎟ ⎜ x1 ⎟ ⎜ y1 ⎟
⎜ t2 ⎟
Vt := ⎜ ⎟
⎜ x2 ⎟ ⎜ y2 ⎟
Vx := ⎜ ⎟ Vy := ⎜ ⎟
⎜ t3 ⎟ ⎜ x3 ⎟ ⎜ y3 ⎟
⎜ t4 ⎟ ⎜ x4 ⎟ ⎜ y4 ⎟
⎜ ⎟
⎝ t5 ⎠ ⎜ ⎟ ⎜ ⎟
⎝ x5 ⎠ ⎝ y5 ⎠

⎛ 0 ⎞ ⎛ −1 ⎞ ⎛ 1 ⎞
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 0.1 ⎟ ⎜ −1.112747 ⎟ ⎜ 1.116567 ⎟
⎜ 0.2 ⎟ ⎜ −1.266757 ⎟ ⎜ 1.27519 ⎟
Vt = ⎜ ⎟ Vx = ⎜ ⎟ Vy = ⎜ ⎟
⎜ 0.3 ⎟ ⎜ −1.506 ⎟ ⎜ 1.5 ⎟
⎜ 0.4 ⎟ ⎜ −1.945 ⎟ ⎜ 1.848 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎝ 0.5 ⎠ ⎝ −3.025 ⎠ ⎝ 2.522 ⎠

Vsx := cspline ( Vt , Vx) Vsy := cspline ( Vt , Vy)


346 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

xspline ( t ) := interp ( Vsx , Vt , Vx , t ) yspline ( t ) := interp ( Vsy , Vt , Vy , t )

The Mathcad solution process using " rkfixed " and " Rkadapt " is done below.

2 2
t := 0 , 0.1 .. 0.5 f ( x , y , t) := x ⋅ y + t g ( x , y , t) := t ⋅ y + x

Define initial conditions:

⎛ −1 ⎞
x := ⎜ ⎟ <-- Define initial conditions x(0) = x 0 = -1 and y(0) = y1 = 1.
⎝1 ⎠

Define derivatives:
Note that the vector
function "D" has 2
rows in this case. If
⎡x0 ⋅ ( x1) 2 + t ⎤ < First derivative the problem had
D ( t , x ) :=
⎢ ⎥ <---
involved 3 first order
⎢ 2⎥
⎣t ⋅ x 1 + ( x0) ⎦
< Second derivative eqs, there would be 3
rows.

Evaluate solution at 50 points between 0 and 0.5.

The following commands will generate the solution matrix which will contain 3 columns- the "t"
values, the corresponding "x" values and the "y " values.

Z := rkfixed ( x , 0 , 0.5 , 50 , D) Y := Rkadapt ( x , 0 , 0.5 , 50 , D) i := 0 .. rows ( Z) − 1


〈0〉
xspline i := xspline ⎡ Z i⎤ ( )

〈0〉
yspline i := yspline ⎡ Z i⎤

( )

The Mathcad solution generated through the above procedure is compared with the analytical
Runge-Kutta results in Figures 9.13 and 9.14.
Chapter 9: Numerical Solution of Ordinary Differential Equations 347

System of first order differential equations


1

( Z〈1〉 ) i
2
( )
x- values

〈1〉
Y i
3 rkfixed solution
xsplinei Rkadapt solution
R/K 4th order-
4
0 0.1 0.2 0.3 0.4

( ) 〈0〉
Z i

Time, t, seconds

Figure 9.13. Solution to differential equation pair- x (t)

System of first-order differential equations


2.5
rkfixed solution
Rkadapt solution

( Z〈2〉 ) i
R/K 4th order
2

( Y〈2〉 ) i
y- Values

ysplinei 1.5

1
0 0.1 0.2 0.3 0.4

( )
〈0〉
Z i

Time , t , seconds

Figure 9.14. Solution to differential equation pair - y (t)


348 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

t x(t) y(t) t x(t) y(t)

( obtained with rkfixed ) (obtained with Rkadapt )

0 1 2 0 1 2
0 0 -1 1 0 0 -1 1
1 0.01 -1.010102 1.010151 1 0.01 -1.010102 1.010151
2 0.02 -1.020419 1.020611 2 0.02 -1.020419 1.020611
3 0.03 -1.030966 1.031388 3 0.03 -1.030966 1.031388
4 0.04 -1.041759 1.042491 4 0.04 -1.041759 1.042491
5 0.05 -1.052815 1.05393 5 0.05 -1.052815 1.05393
6 0.06 -1.064154 1.065717 6 0.06 -1.064154 1.065717
Z= 7 0.07 -1.075794 1.077862 Y= 7 0.07 -1.075794 1.077862
8 0.08 -1.087758 1.090377 8 0.08 -1.087758 1.090377
9 0.09 -1.100067 1.103275 9 0.09 -1.100067 1.103275
10 0.1 -1.112747 1.11657 10 0.1 -1.112747 1.11657
11 0.11 -1.125822 1.130277 11 0.11 -1.125822 1.130277
12 0.12 -1.139322 1.144412 12 0.12 -1.139322 1.144412
13 0.13 -1.153276 1.158991 13 0.13 -1.153276 1.158991
14 0.14 -1.167716 1.174032 14 0.14 -1.167716 1.174032
15 0.15 -1.182678 1.189556 15 0.15 -1.182678 1.189556

Note that the first column of the above matrices is the time " t " column, the second is the
solution "x" while the third is the solution "y".

Using " rkadapt " , the solution can be found at any point in time as follows.

This command generates the


solution to the differential
Value := rkadapt ( x , 0 , 0.5 , 0.001 , D , 50 , 0.00001) equation at the endpoint t = 0.5
<--
secs with acc = 0.001, kmax =
50 ( maximum number of
intermediate points at which the
solution will be approximated)
and save = 0.00001 which is the
smallest spacing between
the t- values at which the
solutions are to be
computed.
Chapter 9: Numerical Solution of Ordinary Differential Equations 349

t x y

⎛⎜ 0 −1 1 ⎞⎟
⎜ 5 × 10 − 3 −1.005025 1.005038 ⎟
⎜ ⎟
Value = ⎜
0.03 −1.030966 1.031388 ⎟ Note: The first column contains the
⎜ 0.155 −1.190367 1.197505 ⎟
t-values, the second column contains
⎜ ⎟ <--- the x-values, and the third contains the
⎜ 0.374387 −1.800212 1.741152 ⎟ y- values
⎜⎝ 0.5 −3.026613 2.524711 ⎟⎠

It is noted that the Runge-Kutta- Version 1 solution agrees reasonably well with the
Mathcad solutions.

9.7 SOLUTION OF HIGHER-ORDER ORDINARY DIFFERENTIAL


EQUATIONS

In many areas of engineering and physics, the solution of higher-order differential equations is
often required. Examples can be encountered in vibration engineering, control systems
engineering , electrical circuit analysis etc.

The general form of a second-order differential equation is

d2 y/dx2 = f ( x, y, dy/dx ) (9.27)

A more special form often encountered in vibrations, control systems, and electrical circuit
analysis is

a d2 y/dx2 + b dy/dx + c y = q(x) (9.28)

with initial conditions y(0) and dy/dx(0) specified.

Although any of the methods discussed in sections 9.2-9.5 can certainly be used to obtain
solutions to Equation 9.28, it must first be converted to a system of first-order differential
equations as follows.

dy/dx = z = f ( x,y, z)

(9.29)
dz/dx = [q(x) - b z - c y] / a = g (x,y, z )
350 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

with the initial conditions now becoming y(0) and z(0) . Thus, if a solution to the second-order
differential equation, which is now represented by the two first-order equations of Equations
9.29 , is to be generated by Version 1 of the fourth-order Runge-Kutta method, the
calculations shown in the following procedure must be resorted to.

k1 = h f (y0, z0, x0) ; m1= h g (y0,z0, x0) ;

xmp = x0 + 0.5 h ; ymp= y0+ 0.5 k1; zmp= z0+0.5 m1

k2= h f (ymp, zmp, xmp) ; m2 = h g ( ymp, zmp, xmp )

ymp* = y0 + 0.5 k2 ; zmp* = z0+ 0.5 m2 (9.30)

k3 = h f ( ymp*, zmp*, xmp ) ; m3 = h g ( ymp*, zmp*, xmp )

y1 = y0 + k3 ; z1 = z0 + m3 ; x1 = x0 + h

k4 = h f ( y1, z1, x1 ) ; m4 = h g ( y1, z1, x1 )

y1= y0 + (1/6) [ k1 +2 k2 + 2 k3 + k4 ] ; z1 = z0 + (1/6 ) [ m1 + 2 m2 + 2 m3 + m4 ]

where h is the increment or step size. Note that in order to generate the solution
(y1, z1), we started with known values ( x0, y0, z0 ). Similarly, to obtain ( y2, z2) , the above
procedure must be repeated with (x1,y1,z1) replacing ( x0, y0,z0) and (x2, y2,z2) replacing
(x1,y1,z1).

Example 9.8

Using the Runge-Kutta fourth-order


method, obtain a solution to

m d2y /dt2 +c dy/dt + k y = F(t) ;

m= 1kg, c= 3 N-sec/m, k = 9 N/m , F(t)= 9 N.

for t = 0 to t= 3 seconds. The initial conditions


are given as: y(0)=0; dy/dt (0) = 0 .
Use a time increment of 0.5 seconds.
Chapter 9: Numerical Solution of Ordinary Differential Equations 351

The above differential equation represents the equation of motion of a linear


spring-mass-damper system subjected to a step forcing function as shown in the above figure.
As mentioned earlier , application of the fourth order Runge- Kutta Method to the solution of a
second-order differential equation is quite similar to that of a first order differential equation ,
except that the following two first-order equations must now be considered.

dy/dt = z ;

and m dz/dt + c z +k y = F(t)

Note that, now, both y and z are functions of the independent variable " t".

Thus, in this case,

dy/dt = f( y,z, t ) = z ,

and dz/dt = g(y,z,t) =[F(t) - ( k y + c z)] /m

Version 1 of the R-K procedure will then resort to the following equations to compute y and z at
t1:

k1 = h f(y0, z0, t0) ; m1= h g(y0,z0, t0) ;

tmp = t0 + 0.5 h ; ymp= y0+ 0.5 k1; zmp= z0+0.5 m1

k2= h f(ymp, zmp, tmp) ; m2 = h g( ymp, zmp, tmp )

ymp* = y0 + 0.5 k2 ; zmp* = z0+ 0.5 m2

k3 = h f ( ymp*, zmp*, tmp ) ; m3 = h g ( ymp*, zmp*, tmp )

y1 = y0 + k3 ; z1 = z0 + m3 ; t1 = t0 + h

k4 = h f ( y1, z1, t1 ) ; m4 = h g ( y1, z1, t1 )

y1= y0 + (1/6) [ k1 +2 k2 + 2 k3 + k4 ] ; z1 = z0 + (1/6 ) [ m1 + 2 m2 + 2 m3 + m4 ]


352 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

F ( t) := 9 m := 1 c := 3 k := 9 <--Given data

F ( t) − c ⋅ z − k ⋅ y
f ( y , z , t) := z g ( y , z , t) := <--Define functions f(y,z,t) and g(y,z,t)
m

h := 0.5 <--Time increment or step size

t0 := 0 y0 := 0 z0 := 0 <-- Given initial conditions

Calculations for generating solution at t1:

t1 := t0 + h t1 = 0.5

k1 := h ⋅ f ( y0 , z0 , t0) m1 := h ⋅ g ( y0 , z0 , t0) tmp := t0 + 0.5 ⋅ h

k1 = 0 m1 = 4.5

ymp := y0 + 0.5 ⋅ k1 zmp := z0 + 0.5 ⋅ m1 k2 := h ⋅ f ( ymp , zmp , tmp)

m2 := h ⋅ g ( ymp , zmp , tmp) k2 = 1.125 m2 = 1.125

ympstar := y0 + 0.5 ⋅ k2 zmpstar := z0 + 0.5 ⋅ m2

k3 := h ⋅ f ( ympstar , zmpstar , tmp) m3 := h ⋅ g ( ympstar , zmpstar , tmp)

k3 = 0.28125 m3 = 1.125

y1 := y0 + k3 z1 := z0 + m3 t1 := t0 + h

k4 := h ⋅ f ( y1 , z1 , t1) m4 := h ⋅ g ( y1 , z1 , t1)

k4 = 0.5625 m4 = 1.546875

⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
y1 := y0 + ⎜ ⎟ y1 = 0.5625
⎝ 6⎠
Chapter 9: Numerical Solution of Ordinary Differential Equations 353

1
z1 := z0 + ⋅ ( m1 + 2 ⋅ m2 + 2 ⋅ m3 + m4) z1 = 1.757813
6

Calculations for generating solution at t2:

t2 := t1 + h t2 = 1

k1 := h ⋅ f ( y1 , z1 , t1) m1 := h ⋅ g ( y1 , z1 , t1) tmp := t1 + 0.5 ⋅ h

k1 = 0.878906 m1 = −0.667969

ymp := y1 + 0.5 ⋅ k1 zmp := z1 + 0.5 ⋅ m1 k2 := h ⋅ f ( ymp , zmp , tmp)

m2 := h ⋅ g ( ymp , zmp , tmp) k2 = 0.711914 m2 = −2.144531

ympstar := y1 + 0.5 ⋅ k2 zmpstar := z1 + 0.5 ⋅ m2

k3 := h ⋅ f ( ympstar , zmpstar , tmp) m3 := h ⋅ g ( ympstar , zmpstar , tmp)

k3 = 0.342773 m3 = −0.661377

y2 := y1 + k3 z2 := z1 + m3 t2 := t1 + h

k4 := h ⋅ f ( y2 , z2 , t2) m4 := h ⋅ g ( y2 , z2 , t2)

k4 = 0.548218 m4 = −1.218384

⎛ 1 ⎞ ⋅ ( k1 + 2 ⋅ k2 + 2 ⋅ k3 + k4)
y2 := y1 + ⎜ ⎟ y2 = 1.151917
⎝ 6⎠

1
z2 := z1 + ⋅ ( m1 + 2 ⋅ m2 + 2 ⋅ m3 + m4) z2 = 0.508118
6

Caculations for t = 1.5,2.0,2.5 and 3.0 can be performed in a similar way , giving the
following results.

t3 := 1.5 y3 := 1.166 z3 := −0.342 t4 := 2 y4 := 1.006 z4 := −0.24


354 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

−3
t5 := 2.5 y5 := 0.938 z5 := 2.889 ⋅ 10 t6 := 3 y6 := 0.94 z6 := −0.019

Plots of the results of the Runge-Kutta process can be obtained by an interpolation


procedure involving the generation of the Vt, Vy and Vz vectors and the spline functions
yspline(t) and zspline(t) as shown below.

⎛⎜ t0 ⎞⎟ ⎛⎜ y0 ⎞⎟ ⎛⎜ z0 ⎞⎟
⎜ t1 ⎟ ⎜ y1 ⎟ ⎜ z1 ⎟
⎜ t2 ⎟ ⎜ y2 ⎟ ⎜ z2 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
Vt := ⎜ t3 ⎟ Vy := ⎜ y3 ⎟ Vz := ⎜ z3 ⎟
⎜ t4 ⎟ ⎜ y4 ⎟ ⎜ z4 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ t5 ⎟ ⎜ y5 ⎟ ⎜ z5 ⎟
⎜ t6 ⎟ ⎜ y6 ⎟ ⎜ z6 ⎟
⎝ ⎠ ⎝ ⎠ ⎝ ⎠

⎛⎜ 0 ⎞⎟ ⎛⎜ 0 ⎞⎟ ⎛ 0 ⎞
⎜ ⎟
⎜ 0.5 ⎟ ⎜ 0.5625 ⎟ ⎜ 1.757813 ⎟
⎜ 1 ⎟ ⎜ 1.151917 ⎟ ⎜ 0.508118 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
Vt = ⎜ 1.5 ⎟ Vy = ⎜ 1.166 ⎟ Vz = ⎜ −0.342 ⎟
⎜ 2 ⎟ ⎜ 1.006 ⎟ ⎜ −0.24 ⎟
⎜ ⎟ ⎜ ⎟ ⎜ ⎟
⎜ 2.5 ⎟ ⎜ 0.938 ⎟ ⎜ 2.889 × 10 − 3 ⎟
⎜ 3 ⎟ ⎜ 0.94 ⎟ ⎜ ⎟
⎝ ⎠ ⎝ ⎠ ⎝ −0.019 ⎠

Vsy := cspline ( Vt , Vy) Vsz := cspline ( Vt , Vz) yspline ( t ) := interp ( Vsy , Vt , Vy , t )

zspline ( t ) := interp ( Vsz , Vt , Vz , t )

The Mathcad solution process using " rkfixed " and " Rkadapt " is done below.

t := 0 , 0.1 .. 3 F ( t) := 9 m := 1 c := 3 k := 9

F ( t) − c ⋅ z − k ⋅ y
f ( y , z , t) := z g ( y , z , t) :=
m

Define initial conditions:


Chapter 9: Numerical Solution of Ordinary Differential Equations 355

⎛0 ⎞ Define initial conditions y(0) = y0 = 0 and z(0) = y1 = 0.


y := ⎜ ⎟ <--
⎝0 ⎠ y 0 is initial displacement and y 1 is initial velocity

Define first and second derivatives:

Note that the vector


function "D" has 2
⎛ y1 ⎞ < First derivative rows in this case. If the
⎜ ⎟ problem had involved 3
D ( t , y ) := ⎜ F ( t) − c ⋅ y 1 − k ⋅ y 0 ⎟ < Second derivative first order eqs, there
⎜ ⎟
⎝ m ⎠ would be 3 rows.

Evaluate solution at 100 points between 0 and 3:

The following commands will generate the solution matrix which will contain 3 columns- the
"t" values, the corresponding "y" values and the "dy/dt " values.

Z := rkfixed ( y , 0 , 3.0 , 100 , D) Y := Rkadapt ( y , 0 , 3.0 , 100 , D) i := 0 .. rows ( Z) − 1


( )
〈0〉
yspline i := yspline ⎡ Z i⎤

( )〈0〉
zspline i := zspline ⎡ Z i⎤
⎣ ⎦

The displacement and velocity profiles generated are compared in Figures 9.15 and 9.16.
_________________________________________________________________________________

N O T E S:
356 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Sec-order differential equation solution


1.5

( Z〈1〉 ) i 1
DISPLACEMENT

( Y〈1〉 ) i
ysplinei
0.5

rkfixed solution
Rkadapt solution
R/K 4th order-
0
0 0.5 1 1.5 2 2.5 3

( )
〈0〉
Z i

TIME

Figure 9.15. Solution to second- order differential equation - displacement as a


function of time

Second-order differential equation solution


2
rkfixed solution

( Z〈2〉 ) i
Velocity ( dy/dt) Values

Rkadapt solution
R/K 4th order

( Y〈2〉 ) i
1

zsplinei
0

0 0.5 1 1.5 2 2.5 3

( )
〈0〉
Z i

Time values

Figure 9.16. Solution to second-order differential equation- velocity as a function


of time
Chapter 9: Numerical Solution of Ordinary Differential Equations 357

t y(t) ydot(t)
( obtained with rkfixed )

0 1 2
0 0 0 0
1 0.03 3.9285·10 -3 0.257858
2 0.06 0.015229 0.491527
3 0.09 0.033181 0.701279
4 0.12 0.057071 0.887549
5 0.15 0.086205 1.050917
6 0.18 0.119904 1.192089
Z= 7 0.21 0.157516 1.311883
8 0.24 0.198412 1.41121
9 0.27 0.241994 1.491065
10 0.3 0.287692 1.552504
11 0.33 0.334971 1.596638
12 0.36 0.383329 1.624613
13 0.39 0.432298 1.637605
14 0.42 0.481447 1.636801
15 0.45 0.53038 1.623397

t y(t) ydot(t)
( obtained with Rkadapt )

0 1 2
0 0 0 0
1 0.03 3.928548·10 -3 0.257858
2 0.06 0.01523 0.491526
3 0.09 0.033181 0.701278
4 0.12 0.057071 0.887549
5 0.15 0.086205 1.050917
6 0.18 0.119904 1.192088
Y= 7 0.21 0.157516 1.311882
8 0.24 0.198413 1.411209
9 0.27 0.241994 1.491064
10 0.3 0.287692 1.552503
11 0.33 0.334971 1.596637
12 0.36 0.383329 1.624612
13 0.39 0.432298 1.637603
14 0.42 0.481447 1.6368
15 0.45 0.53038 1.623396

Note that the first column of the above matrices is the time " t " column, the second is the
displacement " y " and the last is velocity " dy/dt "
358 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Using " rkadapt " , the solution can be found at any point in time as follows. This command
generates the solution to the differential equation at the endpoint t = 0.1 secs with acc =
0.001, kmax = 100 ( maximum number of intermediate points at which the solution will be
approximated) and save = 0.00001 which is the smallest spacing between the t- values at
which the solutions are to be computed.

Value := rkadapt ( y , 0 , 0.1 , 0.001 , D , 100 , 0.00001)

Time Displacement Velocity

⎛ 0 0 0 ⎞
⎜ ⎟
⎜ 1.638135 × 10 − 5 −9 −4
1.207549 × 10 1.474285 × 10 ⎟
⎜ −5 −8 −4
⎟ First column is the
⎜ 8.192772 × 10 3.020221 × 10 7.372589 × 10 ⎟ "time" column.
⎜ −4 −7 −3⎟
Value = ⎜ ⎟ <--
4.096596 × 10 7.54885 × 10 3.684671 × 10
Second column is
⎜ −3 −5 ⎟ "Displacement" .
⎜ 2.048319 × 10 1.884157 × 10 0.018378 ⎟
⎜ −4 ⎟ Third is " Velocity ".
⎜ 0.010242 4.671742 × 10 0.090759 ⎟
⎜ 0.051208 0.011197 0.42554 ⎟
⎜ ⎟
⎝ 0.1 0.040519 0.765952 ⎠

It is noted that the Runge-Kutta- Version 1 solution agrees reasonably well with the Mathcad
solutions.

9.8 BOUNDARY-VALUE PROBLEMS AND THE SHOOTING METHOD

As mentioned in the introduction, a boundary value problem is one in which conditions are
prescribed at two different values of the independent variable. An example of a boundary-value
problem is

2
d
y + C ⋅ y = F ( x) , y ( 0) = 0 , y ( L) = 2 (9.31)
2
dx

Because the two conditions given in Equations 9.31 deal with different values of the independent
variable x , this problem cannot be solved by the methods discussed so far. Other methods,
therefore, have to be resorted to in order to obtain a solution. One such method is the Shooting
Method in which a boundary-value problem is converted to a pseudo- initial-value problem
involving a trial and error approach that can make use of any of the methods discussed in this
chapter. Since only one initial condition is prescribed at x= 0 , the second initial condition,
which is not given, must be assumed at x = 0, in order to start the
Chapter 9: Numerical Solution of Ordinary Differential Equations 359

solution process. Upon completion of the solution process, a check must be made to
determine if the condition prescribed at the point x = L is satisfied or not. If not, a second
guess must be made and the procedure redone. If the second guess still does not satisfy the
prescribed end condition, a third guess has to be estimated. This can be accomplished by
noting the difference between the given and the computed conditions at x = L and resorting to
a linear interpolation process that utilizes the results of the first two trials. This iteration
process must be repeated until the prescribed end condition is , in fact, satisfied. In this
method, it is important for the analyst to be skillful at estimating the unprescribed initial
condition properly at the starting point in order to maintain efficiency and avoid divergence.
Because the method is analogous to aiming properly and shooting at a given object, it has
been appropriately labelled as the shooting method. When higher-order differential equations
must be solved involving several boundary conditions, this method has proven to be quite
uncouth and laborious and, thus, inefficient.

Although any of the methods discussed in this chapter can be employed in the solution of a
boundary-value problem by the shooting method. the following example will illustrate its
application with Mathcad's Rkadapt and rkadapt functions.

Example 9.9

Solve: d2 y/ dx 2 = M/EI = (30 x - 1.5 x^2)/ EI , where EI = 121,500 kips-ft 2

The boundary conditions prescribed are:

x = 0, y = 0 and x= 20, y =0

This differential equation represents the relationship between the bending moment M and
the deflection "y" at a distance "x" along the span from the left hand end of a 20 foot long
simply supported beam loaded with a uniformly distributed load of 3 kips/ ft. as shown in
Figure 9.17. The slope and deflection at any point " x " can be obtained by solving the above
differential equation.

Consider the two equations:

dy/dx = z ; and
(9.32)
dz/dx = M/EI = (30 x - 1.5 x^2 )/ EI

Note that, now, both y and z are functions of the dependent variable " x". Thus, in this
case,
dy/dx = f ( x, y, z ) = z ,
(9.33)
and dz/dx = g( x, y, z ) =[30 x - 1.5 x ^2 ] / EI

As said earlier, the shooting method is a trial- and- error method used in the solution of
boundary-value problems. Although two boundary conditions are prescribed here, there is
only one initial condition ( x= 0, y = 0 ) provided to begin the solution process. The initial
360 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

slope ( derivative ) at x= 0 is not known. Thus, the second initial condition, namely, the value
of dy/dx at x = o has to be assumed so that the solution process can be initiated. With these
initial values of y and dy/dx , the solution " y" at x= 20 ft must then be computed by any
appropriate method such as Modified Euler or Runga- Kutta. If this solution does not agree
with the true value of y at x= 20 ft , which, in this case is zero , the initial condition dy/dx at
x= 0 used must be revised and a new solution y at x= 20 computed. This process has to be
repeated until the solution obtained at x= 20 ft agrees with the actual condition prescribed at
that point on the beam.

In the following paragraphs, an iteration process utilizing Mathcad's Rkadapt and rkadapt
functions is presented by which a solution to this boundary value problem is arrived at :

EI := 121500 <-- Given EI value in kips-ft 2

Figure 9.17. A simply supported beam with a uniformly distributed load


Define initial conditions:

⎛0 ⎞ <---
Define initial conditions y(0) = y0 = 0 and z(0) = y1
y := ⎜ ⎟ = -1.000 (assumed). y0 is y at x = 0, and y 1 is slope
⎝ y1 ⎠ (dy/dx) at x = 0.

Define first and second derivatives:

(Note that the vector


function "D" has 2
⎛ y1 ⎞
⎜ ⎟ < First derivative rows in this case. If
D ( x , y ) := ⎜ 2 ⎟ the problem had
30 ⋅ x − 1.5 ⋅ x
⎜ ⎟ < Second derivative involved 3 first order
⎝ EI ⎠ equations, there
would be 3 rows.)
Chapter 9: Numerical Solution of Ordinary Differential Equations 361

Using " rkadapt " :

Because y1 which is the slope at x = 0 is not


known, a reasonable starting value has
This command generates the
to be assumed.
solution to the differential equation
at the endpoint x= 20 Ft with acc
y 1 := −1.0
= 0.001, kmax = 1000
( max number of intermediate
points at which the solution will be
Value1 := rkadapt ( y , 0 , 20 , 0.001 , D , 1000 , 0.00001) <--
approximated) and save = 0.00001
which is the smallest spacing
between the x- values at which the
solutions are to be computed.

⎛⎜ 0 0 −1 ⎞⎟
⎜ 0.2 −0.2 −0.999995 ⎟

Value1 = 1.2 −1.199931 −0.999829
⎟ First column is the " x" column
⎜ ⎟ <-- Second column is "y "
⎜ 6.2 −6.191712 −0.996235 ⎟ Third is " dy/dx "
⎜ 20 −19.835391 ⎟
⎝ −0.983539 ⎠

The above computation indicates that the deflection at x = 20 Ft generated with the
assumed dy/dx value at x= 0 is non-zero. Therefore, try a new value of y1:

y 1 := −0.5 Value2 := rkadapt ( y , 0 , 20 , 0.001 , D , 1000 , 0.00001)

⎛⎜ 0 0 −0.5 ⎞⎟
⎜ 0.2 −0.1 −0.499995 ⎟

Value2 = 1.2 −0.599931 −0.499829

⎜ ⎟
⎜ 6.2 −3.091712 −0.496235 ⎟
⎜ 20 −9.835391 ⎟
⎝ −0.483539 ⎠

The above analysis has yielded a deflection of 9.835 ft at the end x= 20 Ft.

With the assumed value of y1, the deflection at the end is , clearly, still non-zero. Therefore, a
third value of y1 must be tried which can be arrived at by a process of linear interpolation
utilizing the results of the first two trials. The formula arrived at that must be used for
estimating the next guess is the following.
362 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

New value of y1 to be tried = [second value of the first derivative y1 assumed times the
first deflection value at the end computed - first value of the first derivative y1 assumed
times the second deflection value at the end computed ] / [ first deflection value at the end
computed - the second deflection value at the end computed ] . That is,

Value2 0 , 2 ⋅ Value1 4 , 1 − Value1 0 , 2 ⋅ Value2 4 , 1


y 1 :=
Value1 4 , 1 − Value2 4 , 1

−3
y 1 = −8.23045 × 10

Then, the solution with the new guess is

Value3 := rkadapt ( y , 0 , 20 , 0.001 , D , 1000 , 0.00001)

which yields

⎛ 0 −8.230453 × 10 ⎞−3
⎜ 0 ⎟ Note the negligible
⎜ −3 −3⎟ deflection
⎜ 0.2 −1.645763 × 10 −8.225547 × 10 ⎟ computed at the end with
Value3 = ⎜ 1.2 −9.807565 × 10 − 3 −8.059786 × 10
− 3 ⎟ <---- the current assumed value
⎜ ⎟ of the slope (derivative) at
⎜ −3⎟ x= 0
−0.042741 −4.465547 × 10
⎜ 6.2 ⎟
⎜ − 15 −3 ⎟
⎝ 20 −1.922074 × 10 8.230453 × 10 ⎠

Now , using Rkadapt , the solution can be evaluated at 100 points between 0 and 20 with the
above initial slope value put in which led to the computation of the true displacement (y= 0) at the
end x= 20 Ft. The following commands will then generate the solution matrix [ Z ] which will
contain 3 columns- the "x" values, the corresponding "y" values and the "dy/dx " values.

Z := Rkadapt ( y , 0 , 20 , 100 , D)

i := 0 .. rows ( Z) − 1

The results of the above Mathcad computation are plotted in Figures 9.18 and 9.19.
Chapter 9: Numerical Solution of Ordinary Differential Equations 363

Beam Slope ( dy/dx )


Beam Displacement (y)
0.01
0
Y- values ( Feet )

dy/ dx values
0.02
( Z〈1〉 ) i ( Z〈2〉 ) i 0
0.04

0.06 0.01
0 10 20 0 10 20
( )〈0〉
Z i ( )
〈0〉
Z i

x- Values (Feet) x- Values

Figure 9.18. Beam displacement along span Figure 9.19. Beam slope along span

9.9 APPLICATIONS IN NUMERICAL SOLUTION OF ORDINARY


DIFFERENTIAL EQUATIONS

9.9.1 Response of an Electric R-L Circuit to a Unit-Step Voltage Input

Consider the electric circuit shown in Figure 9.20 which is subjected to a unit step input
voltage .

Figure 9.20. R-L circuit with a unit-step voltage input

Summing voltages around the loop, the governing differential equation for the circuit can be
seen to be
d
R ⋅ I ( t) + L ⋅ I ( t) = u ( t) (9.34)
dt

where R is the resistance of the circuit, L its inductance and I(t) the current in it at any time
t . With no initial energy in the network, the following initial condition is applicable
I ( 0) = 0 (9.35)
364 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The Mathcad statements that are needed to generate the solution using Rkadapt are

1 − R⋅I
R := 1 L := 1 I0 := 0 D ( t , I ) := Z := Rkadapt ( I , 0. , 8. , 50 , D)i := 0 .. rows ( Z) − 1
L
Current in a an electric circuit
1
Current, amperes

( Z〈1〉 ) i 0.5

0
0 1 2 3 4 5 6

( )
〈0〉
Z i

Time, seconds

Figure 9.21. Response of R-L circuit to unit step input voltage

The Mathcad solution generated is shown in Figure 9.21. Notice how the current attains its
steady-state value in a time t equal to 4 τ, where τ is the system time constant, which, in this
case, is L/ R or 1 second.

9.9.2. Deflection Curve of a Cantilevered Beam with a Uniformly Distributed


Load

Consider a uniform beam fixed at one end at free at the other as shown with a uniformly
distributed load of w per unit length. The bending moment at a distance x from the left hand end
of the beam is given by

2 2
L x
M (x) = w⋅ L ⋅ x − w⋅ − w⋅
2 2 (9.36)

Figure 9.22. Cantilevered beam with uniformly distributed load


Chapter 9: Numerical Solution of Ordinary Differential Equations 365

The displacement at x is then given by

2 M ( x)
d
y ( x) = (9.37)
2 EI
dx

where E is Young's modulus of elasticity for the material of the beam, and I is the moment
of inertia of the beam cross section about its neutral axis. In order to determine the
deflection curve of the beam, Equation (9.37) must be integrated and the appropriate
boundary conditions applied , which are

d
y ( 0) = 0 , y ( 0) = 0
dx
For a beam of length L= 165 inches, w = 12 lb/in, EI = 75 x 10 6 lb- in 2 , Equation (9.37)
can be solved with Mathcad's Rkadapt function as follows.

6
L := 165 EI := 75 ⋅ 10 w := 12 y 0 := 0 y 1 := 0

⎛ y0 ⎞ Define initial conditions to start the solution process: y(0)= 0 , dy/dx (0)
y := ⎜ ⎟ <--- = 0 ( y 0 is the displacement at x=0 and y 1 is the slope at x= 0 )
⎝ y1 ⎠
2 2
L x <--Bending moment expression at x
M ( x ) := w ⋅ L ⋅ x − w ⋅ − w⋅
2 2

⎛⎜ y1 ⎞⎟
D ( x , y ) := ⎜ M ( x ) ⎟ <--Define first and second derivatives
⎜ EI ⎟
⎝ ⎠

Z := Rkadapt ( y , 0 , L , 100 , D) <------ Evaluates solution at 100 points between 0 and L with
the given initial conditions
i := 0 .. rows ( Z) − 1

The results of the Mathcad computation are shown in Figures 9.23 and 9.24, with the maximum
deflection and slope occurring at the free end as anticipated.
366 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Slope along beam span


Deflection along beam span
0
0
Deflection , inches

( Z〈2〉 ) i 0.1

Slope
( Z〈1〉 ) i 10

0.2
20 0 100

( )
0 50 100 150 〈0〉
( Z〈0〉 ) i Z i

Distance from left end


Distance from left end, inches

Figure 9.23. Beam deflection curve Figure 9.24. Beam slope along span

9.9.3. Temperature Response of a Solid Steel Ball Placed in a Hot Stream Of Air

When a body which is initially at a uniform temperature T 0 is suddenly placed in a hot


fluid, the rate at which the temperature T(t) of the body changes with time is given by the
differential equation [ 17 ]

ρ ⋅ c ⋅ Lc ⋅ T ( t) = h ⋅ ( Tinf − T ( t) )
d
(9.38)
dt

where ρ is the mass density of the body, c is its specific heat at constant pressure, T inf is
the temperature of the surrounding fluid, h is the average convection heat transfer coefficient,
and L c is the characteristic length. For a solid steel sphere of diameter 2 cm initially at 10 deg
C., and placed in a stream of hot air at temperature, T inf = 75 deg C. , with h =25 W/ m 2 . K,
we have

d := 0.02 m ρ := 7854 kg/ m 3


c := 434 J/ kg . K h := 25 W/ m 2 . K

3
d 2
Tinf := 75 deg. K T0 := 10 deg. K V := π ⋅ A := π ⋅ d
6

V −3
Lc := Lc = 3.333333 × 10 m
A

and the Mathcad statements needed to generate the temperature response of the steel
ball to the hot stream of air are
Chapter 9: Numerical Solution of Ordinary Differential Equations 367

( )
h
T0 := 10 D ( t , T) := ⋅ Tinf − T Z := Rkadapt ( T , 0. , 2000 , 100 , D)
ρ ⋅ c ⋅ Lc

i := 0 .. rows ( Z) − 1

Temp response of steel ball


100
Temperature, deg. C.

( Z〈1〉 ) i 50

0
0 500 1000 1500 2000

( )
〈0〉
Z i

Time, seconds

Figure 9.25. Temperature response of steel ball

The Mathcad solution generated, which is plotted in Figure 9.25 shows how the steel ball attains
its steady-state temperature of 75 deg C. in a time t equal to 4τ, where τ is the system time
Lc
constant which, in this case, is ρ ⋅ c ⋅ = 454.5 seconds.
h

9.9.4. Nonlinear Vibration of a Simple Pendulum

The natural frequency of large oscillation of a simple pendulum, which is a mass attached
to one end of a light rigid rod, has been generated in Section 6 of Chapter 8 as a function
of the maximum amplitude of the pendulum. The nonlinear behavior of the pendulum is
governed by the differential equation [ 6, 9, 16, 20 ]

2 G
d
θ ( t) + ⋅ sin ( θ ( t) ) = 0 (9.39)
2 L
dt

where L is the length of the pendulum and G is the acceleration due to gravity .
368 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

For a pendulum of of length L= 12 inches, with a maximum initial amplitude of A= 60


degrees and zero initial velocity, the initial conditions are defined by

d
θ ( 0) = 0 θ ( 0) = 0 (9.40)
dt

Equation (9.39) can then be solved with Mathcad's Rkadapt function as follows.

π
L := 12 in G := 386.4 in/ sec 2 A := 60 ⋅ θ 0 := A θ 1 := 0
180

⎛ θ0 ⎞ Define initial conditions to start the solution process: θ(0)= A ,


<----
θ := ⎜ ⎟ dθ/dt (0) = 0 ( θ 0 is the angular displacement at t=0 and θ 1 is the
⎝ θ1 ⎠ angular velocity at t= 0 )

⎛⎜ θ1 ⎞⎟
D ( t , θ ) := ⎜ −G ⎟ <---- Define first and second derivatives
⎜ L ⋅ sin ( θ 0) ⎟
⎝ ⎠

Z := Rkadapt ( θ , 0 , 5 , 100 , D) <---- Evaluates solution at 100 points between t=0


and t=5 seconds with the given initial conditions

Results are generated below with Mathcad for several initial amplitudes of the pendulum ranging
from 10 degrees to 120 degrees and these are plotted in Figure 9.26.

Amplitude = 10 degrees:

⎛⎜ θ1 ⎞⎟
π ⎛ θ0 ⎞
A := 10 ⋅ θ 0 := A θ 1 := 0 θ := ⎜ ⎟ D1 ( t , θ ) := ⎜ −G ⎟
180 ⎝ θ1 ⎠ ⎜ L ⋅ sin ( θ 0) ⎟
⎝ ⎠

Z1 := Rkadapt ( θ , 0 , 5 , 100 , D1 )

Amplitude = 50 degrees:

π ⎛ θ0 ⎞ ⎛⎜ θ1 ⎞⎟
A := 50 ⋅ θ 0 := A θ 1 := 0 θ := ⎜ ⎟ D2 ( t , θ ) := ⎜ −G ⎟
⎝ θ1 ⎠ ⎜ L ⋅ sin ( θ 0) ⎟
180
⎝ ⎠
Z2 := Rkadapt ( θ , 0 , 5 , 100 , D2 )
Chapter 9: Numerical Solution of Ordinary Differential Equations 369

Amplitude = 90 degrees:

⎛⎜ θ1 ⎞⎟
π ⎛ θ0 ⎞
A := 90 ⋅ θ 0 := A θ 1 := 0 θ := ⎜ ⎟ D3 ( t , θ ) := ⎜ −G ⎟
180 ⎝ θ1 ⎠ ⎜ L ⋅ sin ( θ 0) ⎟
⎝ ⎠
Z3 := Rkadapt ( θ , 0 , 5 , 100 , D3 )

Amplitude = 120 degrees:

⎛⎜ θ1 ⎞⎟
π ⎛ θ0 ⎞
A := 120 ⋅ θ 0 := A θ 1 := 0 θ := ⎜ ⎟ D4 ( t , θ ) := ⎜ −G ⎟
180 ⎝ θ1 ⎠ ⎜ L ⋅ sin ( θ 0) ⎟
⎝ ⎠
Z4 := Rkadapt ( θ , 0 , 5 , 100 , D4 ) i := 0 .. rows ( Z) − 1

Nonlinear pendulum response


200

( Z1〈1〉 ) i ⋅ 180
π
Angular position , degrees

100
( Z2〈1〉 ) i ⋅ 180
π

( Z3〈1〉 ) i ⋅ 180 0
π

( Z4〈1〉 ) i ⋅ 180
π
100

0 1 2 3 4 5
( )
〈0〉
Z i

Time, seconds
A= 10 degrees
A= 50 degrees
A= 90 degrees
A= 120 degrees

Figure 9.26. Dynamic response of nonlinear pendulum

The graphs plotted in Figure 9.26 clearly indicate that there is an increase in the magnitude of
the period of vibration of the nonlinear pendulum with increase in the oscillation amplitude .
Also, as can be seen from the response plots , because there is no damping present in the
system, the free vibration of the pendulum never decays out.
370 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

9.9.5. Transient Vibration of a Spring-Mass-Damper System Excited by a Pulse


Function

The differential equation of the spring-mass-damper system of Figure 9.27 excited by the
pulse function F(t) shown is
2
d d
M⋅ x + c ⋅ x + k ⋅ x = F ( t) (9.41)
2 d t
dt

Figure 9.27. Spring-mass-damper system excited by pulse function

For a system with m= 3 kg, c =12 N-sec/ m, k= 120 N/m, and initially at rest, the solution to
Equation (9.41) can be obtained using Mathcad as follows

M := 3 c := 12 k := 120 <--Specify the parameters of the given system

x init := 0 v init := 0 <-- Specify initial displacement and velocity

⎛ xinit ⎞ <----
Express initial conditions as a vector y , the first
y := ⎜ ⎟ component being the initial displacement and the
⎝ vinit ⎠ second being the initial velocity.
These steps create the forcing
function F(t) using linear
⎛ 0 ⎞ ⎛ 0 ⎞
⎜ ⎟ ⎜ ⎟ <----- interpolation and the " if "
vt :=
⎜ 0.5 ⎟ vf :=
⎜ 2. ⎟ function.
⎜ 1.0 ⎟ ⎜ 0.5 ⎟
⎜ ⎟ ⎜ ⎟ if(cond,tval,fval) returns
⎝ 2 ⎠ ⎝ 0 ⎠ tval if cond is true but
returns fval if cond is false .
G ( t) := linterp ( vt , vf , t) F ( t) := if ( t > 2.0 , 0 , G ( t) )

t := 0 , 0.001 .. 2
Chapter 9: Numerical Solution of Ordinary Differential Equations 371

Plot of exciting pulse


2

Force, Newtons
F( t) 1

0
0 0.5 1 1.5 2
t
Time, seconds

⎡⎢ y1 ⎤⎥ Set up the derivative vector function. In this case, it


<--
D ( t , y ) := ⎢ 1 ⎥ has two rows . If the problem had involved three
⎢ M ⋅ ( F ( t) − c ⋅ y1 − k ⋅ y0) ⎥ first-order equations, there would be three rows
⎣ ⎦

T := 4.0 N := 500 <--- Specify the time period in seconds and the
number of points beyond the initial point at
which the solution is to be approximated .

This command will generate the solution at


Z := Rkadapt ( y , 0 , T , N , D) <---- N points between 0 and T seconds using the
Rkadapt function. The solution matrix will
contain three columns- the "t " values, the
corresponding "y" and the "v" values .

The results can be plotted as shown below. The first column ( the zeroth one according to
Mathcad ) is a vector of time values, the second ( first one according to Mathcad ) is a
column of displacements while the last is a column of velocities.

i := 0 .. N ti := Zi , 0 x i := Zi , 1 <---- Note that the t's are in the


zeroth column of "Z" , while the
displacements "x" and the
v i := Zi , 2
velocities " v" are in the first
and second columns
respectively

The displacement and velocity time histories plotted in Figures 9.28 and 9.29 indicate that,
because of the presence of damping in the system, the response, which is in fact transient in
nature, dies out soon after the forcing function is withdrawn at t = 2 seconds.
372 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Disp Time Hist Obt with Rkadapt


0.02
Displacement , m

0.01
xi

0 0.5 1 1.5 2 2.5 3 3.5 4


ti

Time, seconds

Figure 9.28. Displacement response of spring-mass-damper system

Velocity Time Hist Obt with Rkadapt


0.05
Velocity , m/ sec

vi 0

0.05
0 0.5 1 1.5 2 2.5 3 3.5 4
ti

Time, seconds

Figure 9.29. Velocity response of spring-mass-damper system


Chapter 9: Numerical Solution of Ordinary Differential Equations 373

9.9.6. Nonlinear Vibration of a Damped System with a Hardening Spring.

Consider the following nonlinear equation known as Duffing's equation [ 6, 16, 20, 21 ],
which is the differential equation of motion of a spring-mass-damper system with a
hardening spring

2
d d 3
M⋅ x + c ⋅ x + k ⋅ x + μ ⋅ x = F ( t) (9.42)
2 dt
dt

where the last term on the right hand side of the equation is due to the effect of the hardening
spring in which the spring stiffness increases as the load on it increases.
The term F(t) on the right hand side of the equation is the exciting force that the system
is subjected to. We will utilize Mathcad to obtain solutions to Equation (9.42) for m
= 1.2 kg, c= 0.6 N-sec/m , k= 1.2 N/ m and F(t) = (a) a step function and then (b) a
ramp function for values of μ ranging from 0.01 to 1.0. The results will then be compared with
those for μ =0

(a) Step input with μ =0, 0.01, 0.1, 1.0:

M := 1.2 k := 1.2 c := 0.6

x init := 0 v init := 0 <-- Specify initial displacement and velocity

⎛⎜ xinit ⎞⎟ Express initial conditions as a vector y , the first


y := <---- component being the initial displacement and the
⎜v ⎟
⎝ init ⎠ second being the initial velocity.

F ( t) := Φ ( t) This creates a unit step function (Φ(t)= unit


<----
step or Heaviside function )

t := 0 , 0.1 .. 1.0 Unit step input


1.001
F(t), Newtons

1
F( t)
0.999

0.998
0 0.2 0.4 0.6 0.8
t

Time, seconds
374 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

μ = 0.01:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥
<-- Set up the derivative vector function.

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 0.01 ⋅ ( y 0) ⎦ ⎥
⎣ ⎦

T := 20.0 N := 500 <--- Specify the time period in seconds and the
number of points beyond the initial point at
which the solution is to be approximated .

Z1 := Rkadapt ( y , 0 , T , N , D) <--- Generate the solution at N points between 0


and T seconds. The solution matrix will
contain three columns- the "t " values, the
corresponding "y" and the "v" values as
shown below
μ = 0.1:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 0.1 ⋅ ( y0) ⎦ ⎥
⎣ ⎦

Z2 := Rkadapt ( y , 0 , T , N , D)

μ = 1.0:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 1.0 ⋅ ( y0) ⎦ ⎥
⎣ ⎦

Z3 := Rkadapt ( y , 0 , T , N , D)
Chapter 9: Numerical Solution of Ordinary Differential Equations 375

μ = 0.0:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 ⎥ T := 20.0 N := 500
⎢ M ⋅ ( F ( t) − c ⋅ y1 − k ⋅ y0) ⎥
⎣ ⎦

Z4 := Rkadapt ( y , 0 , T , N , D)

The results can be plotted as shown below. The first column ( the zeroth one according to
Mathcad ) is a vector of time values, the second ( first one according to Mathcad ) is a
column of displacements while the last is a column of velocities.

i := 0 .. N ti := Z1i , 0

xlowmui := Z1i , 1 xmidmui := Z2i , 1


Note that the t's are in the
zeroth column of the "Z1 " ,
xhighmui := Z3i , 1 "Z2", "Z3" and "Z4 " matrices ,
<-----
while the displacements "x"
and the velocities " v" are in the first and
xzeromui := Z4i , 1
second columns respectively

Step Response
1.5

xlowmu i
Displacement, m

1
xmidmui

xhighmui

xzeromui
0.5 mu=0.01
mu=0.1
mu=1.0
mu= 0
0
0 5 10 15 20
ti

Time, seconds

Figure 9.30. Response of system with hardening spring to unit step input
376 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

(b) Ramp input with μ = 0, 0.01, 0.1, 1.0:

M := 1.2 k := 1.2 c := 0.6 μ := 1.0

x init := 0 v init := 0 <-- Specify initial displacement and velocity

⎛⎜ xinit ⎞⎟ Express initial conditions as a vector y , the first


y := <--- component being the initial displacement and the
⎜v ⎟
⎝ init ⎠ second being the initial velocity.

F ( t) := Φ ( t) ⋅ t <--This creates the unit ramp function

t := 0 , 0.1 .. 1.0

The unit ramp input


1
F(t), Newtons

F( t) 0.5

0
0 0.2 0.4 0.6 0.8
t
mu = 0.01: Time, seconds

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 0.01 ⋅ ( y 0) ⎦ ⎥
⎣ ⎦

Z1 := Rkadapt ( y , 0 , T , N , D)
Chapter 9: Numerical Solution of Ordinary Differential Equations 377

mu= 0.1:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 0.1 ⋅ ( y0) ⎦ ⎥
⎣ ⎦

Z2 := Rkadapt ( y , 0 , T , N , D)

mu= 1.0:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 1.0 ⋅ ( y0) ⎦ ⎥
⎣ ⎦

Z3 := Rkadapt ( y , 0 , T , N , D)

mu= 0.0:

⎡⎢ y1 ⎤⎥
D ( t , y ) := ⎢ 1 3⎤ ⎥ T := 20.0 N := 500

⎢ M ⋅ ⎣F ( t) − c ⋅ y1 − k ⋅ y0 − 0.0 ⋅ ( y0) ⎦ ⎥
⎣ ⎦

Z4 := Rkadapt ( y , 0 , T , N , D)

The results are generated below and are plotted in Figure 9.31.

i := 0 .. N ti := Z1i , 0 xlowmui := Z1i , 1 xmidmui := Z2i , 1 xhighmui := Z3i , 1

xzeromui := Z4i , 1
378 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Reponse to ramp input


20
mu= 0.01
Displacement , m

xlowmu i
15 mu= 0.1
xmidmui mu= 1.0
xhighmui 10 mu = 0

xzeromui
5

0
0 5 10 15 20
ti

Time, seconds

Figure 9.31. Response of system with hardening spring to a unit ramp input

9.9.7. Temperature Distribution in the Wall of a Pipe Carrying a Hot Fluid

The differential equation relating temperature in the wall of a metal pipe to radial distance is
[ 17 ]

2
d d
r⋅ T ( r) + T ( r) = 0 (9.43)
2 dt
dr

where T(r) is the temperature in the pipe wall at a radial distance r from the pipe centerline. The
inner radius of the pipe is r i and the outer radius is r o . The temperature of the fluid inside the
pipe is T f while the temperature of the outer wall of the pipe is T o. Although two boundary
conditions are prescribed here, namely the temperatures at the inner and outer walls of the pipe,
there is only one initial condition available, that is , T(r i )= T f to begin the solution process.
The initial temperature gradient, namely, dT/dr , is not known. Thus, in order to resort to the
Shooting Method, this initial temperature gradient must be assumed so that the process of
generating a solution may be initiated. The solution T at ro must then be determined with these
initial values of T and dT/dr ( at r i ). If the solution obtained does not match up with the given
condition at r = r o , the assumed initial condition must be revised and a new solution T(r o )
determined. This process must be repeated until the solution obtained at r o agrees with the
prescribed condition . As discussed in Section 9.7, Equation (9.43) must first be converted to
the following two first- order differential equations before a solution can be obtained.

d d −1
T=0 , z = ⋅z (9.44)
dr dr r
Chapter 9: Numerical Solution of Ordinary Differential Equations 379

In the following paragraphs, an iteration process utilizing Mathcad's Rkadapt and rkadapt
functions is presented by which a solution to this boundary value problem is arrived at for r i =
1 cm, r o = 2 cm, T f = 650 deg C. and T o = 10 deg C.

ri := 1 ro := 2 Tf := 650 TO := 10 <-- Given data

Define initial conditions:

initgrad := −100 <--- Define initial conditions T 0 = T(ri) = Tf = 650 deg C.


and dT/dr (r i ) = z(ri) = initgrad.

⎛ 650 ⎞
T := ⎜ ⎟
⎝ initgrad ⎠

Define first and second derivatives:

Note that the vector


function "D" has 2 rows in
⎛ initgrad ⎞ < First derivative this case. If the problem
D ( r , T) := ⎜ −1 ⎟ had involved 3 first order
⎜ ⋅ initgrad ⎟ < Second derivative equations, there would be
⎝ r ⎠ 3 rows.

The use of the Mathcad function " rkadapt " is demonstrated below. Because initgrad which .
is the temperature gradient at r = r i is not known, a reasonable starting value has been
assumed. The following command generates the solution to the differential equation at the
endpoint r= r o with acc = 0.001, kmax = 1000 ( max number of intermediate points at which
the solution will be approximated) and save = 0.00001 which is the smallest spacing between
the r- values at which the solutions are to be computed.

(
Value1 := rkadapt T , ri , ro , 0.001 , D , 1000 , 0.00001 )
⎛⎜ 1 650 −100 ⎞⎟
⎜ 1.01 649 −99.005 ⎟ First column is the " r" column

Value1 = 1.06 644 −94.173
⎟ <--
Second column is "T "
⎜ ⎟ Third is " dT/dr "
⎜ 1.31 619 −72.997 ⎟
⎜ 2 ⎟
⎝ 550 −30.685 ⎠
380 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

The above computation indicates that, with the assumed dT/dr value at r= r i , a temperature of
550 deg. C. is generated at r = ro , which is not the prescribed temperature. Therefore, try a
new value of initgrad:

⎛ initgrad ⎞
initgrad := −200 ⎛ 650 ⎞ D ( r , T) := ⎜ −1 ⎟
T := ⎜ ⎟ ⎜ ⋅ initgrad ⎟
⎝ initgrad ⎠ ⎝ r ⎠

(
Value2 := rkadapt T , ri , ro , 0.001 , D , 1000 , 0.00001 )

⎛⎜ 1 650 −200 ⎞⎟
⎜ 1.01 648 −198.01 ⎟

Value2 = 1.06 638 −188.346

⎜ ⎟
⎜ 1.31 588 −145.995 ⎟
⎜ 2 ⎟
⎝ 450 −61.369 ⎠

The above analysis has yielded a temperature of 450 deg. C . at the outer wall of the pipe.
With the second assumed value of initgrad, the temperature of the outer pipe wall is ,
clearly, still not what it should be . Therefore, a third value of initgrad must be tried which
can be arrived at by a process of linear interpolation utilizing the results of the first two
trials. The formula generated that must be used for estimating the next guess is given
below.

Tdiff1 = firstTouter − Touterprescribed

Tdiff2 = secondTouter − Touterprescribed

secguess ⋅ Tdiff1 − firstguess ⋅ Tdiff2


guess =
firstTouter − secondTouter

That is,

( ) (
Value2 0 , 2 ⋅ Value1 4 , 1 − TO − Value1 0 , 2 ⋅ Value2 4 , 1 − TO )
initgrad :=
Value1 4 , 1 − Value2 4 , 1

initgrad = −640
Chapter 9: Numerical Solution of Ordinary Differential Equations 381

Then, the solution with the new initgrad is

⎛ 650 ⎞ ⎛ initgrad ⎞
initgrad = −640 T := ⎜ ⎟ D ( r , T) := ⎜ −1 ⎟
⎝ initgrad ⎠ ⎜ ⋅ initgrad ⎟
⎝ r ⎠
(
Value3 := rkadapt T , ri , ro , 0.001 , D , 1000 , 0.00001 )

which yields

⎛⎜ 1 650
⎟⎞
−640
Note that the temperature
⎜ 1.01 643.6 −633.632 ⎟
generated at the outer wall now

Value3 = 1.06 611.6 −602.708
⎟ <----
matches the prescribed
⎜ ⎟ temperature (10 deg. C. ).
⎜ 1.31 451.6 −467.183 ⎟
⎜ 2 ⎟
⎝ 10 −196.382 ⎠

Now , using Rkadapt , the solution can be evaluated at 100 points between r= 1 and r =
2 cm with the above initial dT/dr value put in which led to the computation of the true
temperature at the outer wall . The following commands will then generate the solution
matrix [ Z ] which will contain 3 columns- the "r" values, the corresponding "T" values
and the "dT/dr " values.

⎛ initgrad ⎞
initgrad = −640
D ( r , T) := ⎜ −1 ⎟ ( )
Z := Rkadapt T , ri , ro , 100 , D
⎜ ⋅ initgrad ⎟
⎝ r ⎠

i := 0 .. rows ( Z) − 1

The results of the above Mathcad computation are plotted in Figures 9.32 and 9.33.
382 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Temperature in pipe wall


600
Temperature, deg. C.

400

( )
〈1〉
Z i

200

0
1 1.2 1.4 1.6 1.8

( )i
Z
〈0〉

r, cm
Figure 9.32. Temperature in pipe outer wall

Temp gradient in pipe wall


0
dT/dr, (deg.C./ cm)

200

( Z〈2〉 ) i 400
600

800
1 1.2 1.4 1.6 1.8

( )
〈0〉
Z i

r, cm
Figure 9.33. Temperature gradient in pipe outer wall

9.9.8 Response of an R-L Circuit with a Nonlinear Resistor

Consider a series R-L circuit having a nonlinear resistor R with the voltage drop across it is
given by

(
VR = R ⋅ I ( t ) − I ( t )
3 ) (9.45)
Chapter 9: Numerical Solution of Ordinary Differential Equations 383

Summing voltages around the loop, the governing differential equation for the circuit is

(
R ⋅ I ( t) − I ( t)
3
) + L ⋅ d I ( t) = 0 (9.46)
dt

where R is the resistance of the circuit, L its inductance and I(t) the current in it at any
time t . If R= 2.25 Ω, L= 1.10 H and the initial current in the circuit is I(0) = 0.75
amperes, the following Mathcad statements will be needed to generate the solution
using Rkadapt and compare it with that for the corresponding linear problem .

R := 2.25 L := 1.10 I0 := 0.75

D1 ( t , I ) :=
0 − R⋅ I − I( 3 ) Z := Rkadapt ( I , 0. , 3 , 100 , D1) <-- Nonlinear resistor
L

0 − R⋅I
D2 ( t , I ) := Y := Rkadapt ( I , 0. , 3 , 100 , D2 ) <--Linear resistor
L

i := 0 .. rows ( Z) − 1

Current in R-L circuit


Current, amperes

Nonlinear resistor
( )
〈1〉
Z i 0.5 Linear resistor

( Y〈1〉 ) i

0
0 0.5 1 1.5 2 2.5 3

( )
〈0〉
Z i

Time, seconds
Figure 9.34. Response of R-L circuit with a nonlinear
resistor

The Mathcad solutions generated are shown in Figure 9.34. Notice how the current decays
down to a zero value in a time t approximately equal to 4 τ, where τ is the system time
constant, which, in this case, is L / R or 0.5 seconds .
384 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

9.9.9. The Effect of Damping on the Step Response of a Second-Order Control System

The general differential equation governing the behavior of a second-order control system
subjected to a unit step input is [ 14 ]

d
2 ⎛d ⎞ 2 2
y ( t) + 2 ⋅ ζ ⋅ ω n⎜ y ( t) ⎟ + ω n ⋅ y ( t) = ω n (9.47)
dt
2
⎝ dt ⎠

where y(t) is the system response to the unit step input, ζ is the system damping ratio,
and ω n is the natural frequency of the undamped system. We want to investigate the dynamic
response of the system to different levels of damping obtained by varying ζ. If ωn = 5 rad/sec
and ζ is varied from 0.1 to1.4, the following Mathcad statements will be needed to generate the
rseponses .

⎛0 ⎞
ω n := 5 y := ⎜ ⎟
⎝0 ⎠

⎛ y1 ⎞
ζ 1 := 0.1 D1 ( t , y ) := ⎜ ⎟ Z1 := Rkadapt ( y , 0 , 10 , 1000 , D1)
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟
⎝ n 1 n n ⎠

⎛ y1 ⎞
ζ 2 := 0.2 D2 ( t , y ) := ⎜ ⎟ Z2 := Rkadapt ( y , 0 , 10 , 1000 , D2)
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟
⎝ n 2 n n ⎠

⎛ y1 ⎞
ζ 3 := 0.6 D3 ( t , y ) := ⎜ ⎟
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟ Z3 := Rkadapt ( y , 0 , 10 , 1000 , D3 )
⎝ n 3 n n ⎠

⎛ y1 ⎞
ζ 4 := 0.8 D4 ( t , y ) := ⎜ ⎟ Z4 := Rkadapt ( y , 0 , 10 , 1000 , D4 )
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟
⎝ n 4 n n ⎠
Chapter 9: Numerical Solution of Ordinary Differential Equations 385

⎛ y1 ⎞
ζ 5 := 1.0 D5 ( t , y ) := ⎜ ⎟ Z5 := Rkadapt ( y , 0 , 10 , 1000 , D5 )
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟
⎝ n 5 n n ⎠

⎛ y1 ⎞
ζ 6 := 1.4 D6 ( t , y ) := ⎜ ⎟ Z6 := Rkadapt ( y , 0 , 10 , 1000 , D6 )
⎜ ω 2 − 2 ⋅ ζ ⋅ ω ⋅ y1 − ω 2 ⋅ y0 ⎟
⎝ n 6 n n ⎠

i := 0 .. rows ( Z1) − 1

Second-order system response

( Z1〈1〉 ) i
1.5
( )
〈1〉
Z2 i

( Z3〈1〉 ) i
Response

1
( )
〈1〉
Z4 i
Damp ratio=0.1
( )
〈1〉
Z5 i Damp ratio= 0.2
0.5 Damp ratio= 0.6
( Z6〈1〉 ) i Damp ratio=0.8
Damp ratio=1.0
Damp ratio= 1.4
0
0 2 4 6 8 10

( 〈0〉
Z1 i )
Time, seconds
Figure 9.35. Response of second-order system to unit step input with
various levels of damping

The response of the second-order system to a unit step input , which is plotted in Figure 9.35
for varying levels of damping, indicates that as ζ varies from 0.1 to 1.0, the peak overshoots as
well as the time to achieve steady-state diminish, with the transient oscillations dying down
faster as the system damping is increased. An overdamped system (ζ >1.0) , however, is the
most sluggish in attaining steady-state.
386 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

PROBLEMS

In all problems, also obtain numerical solutions using the Mathcad functions rkfixed and
Rkadapt and graph them with proper labels , traces and titles

9.1 Given the differential equation

dy/dx = ( 1 - 2 x) e -2x

with initial condition: y = 3 at x =0 , estimate values of the function using a Taylor series
expansion for x = 0 to x = 0.5 . Take Δx to be 0.10. Construct a Mathcad plot to
determine the accuracy of the Taylor series solution by comparing the computed values
with the true values computed from the true solution, which is y =3 + x e -2x . Also plot
the percentage error as a function of x .

9.2 Given the differential equation

dy/dx = e x + x e x+ 2 x

with boundary condition y = 0 at x = 0 , estimate values of the function using a Taylor


series expansion for x = 0 to x = 1.0 . Take Δx to be 0.20. Construct a Mathcad plot to
determine the accuracy of the Taylor series solution by comparing the computed values
with the true values computed from the true solution, which is y = x e x + x 2 . Also plot the
percentage error as a function of x .

9.3. Given the differential equation

dy/dx = x + y +xy ,

with boundary condition y = 1 at x = 0 , estimate values of the function using a 5-term Taylor
series expansion for the range x = 0.1 to x = 0.5 . Take Δx to be 0.10.

9.4. Solve Problem 9.1 using Euler's method. The step size is to be h= 0.1 and the
range for the solution is to be x= 0 to x= 1. Compare the Euler solution with the true
solution y =3 + x e-2x by constructing a Mathcad plot. Also plot the percentage error as a
function of x .

9.5. Using Euler's method, solve

dy/dx = y' = f(x,y) = 3 x 2 / 2y

The initial condition given is y=3 at x = 2 , and the step size, h is to be 0.1. The range for
the solution is to be : x= 2 to x = 3. By constructing a Mathcad plot, compare the Euler
3
solution with the true solution y = 1+x . Also plot the percentage error as a function of
x.
Chapter 9: Numerical Solution of Ordinary Differential Equations 387

9.6. Given the following information:

dy/dt = -y 2 + 2, y(t=0) = 10 , h = 0.1.

(a) Determine y(0.1), using the modified Euler method. The answer should be correct to 3
decimal places. (b) Continue the computation process until t= 0.5 is reached. (c) Use
Mathcad to generate a plot of y(t) in the range : t= 0 to 0.5. Compare with results of
"rkfixed" and "Rkadapt" .

9.7. Given the differential equation

dy/dt = y 2 + t2 , with initial condition y = 0 at t=1,

determine (a) y(1.1) and y(1.2) by Euler's Method using an interval size h = 0.1.
(b) y(1.1) and y(1.2) by Modified Euler's Method, using an interval size h= 0.1 again

9.8. Given the differential equation

dy/dt = y 2 + t3 , with initial condition y = 1 at t=2,

determine (a) y(2.1) and y(2.2) by Euler's Method using an interval size h = 0.1

(b) y(2.1) by Modified Euler's Method, using an interval size h= 0.1

9.9. Given the differential equation

dy/dt = y + t2 , with initial condition y = 2 at t=1,

determine (a) y(1.1) and y(1.2) by Euler's Method using an interval size h = 0.1

(b) y(1.1) by Modified Euler's Method, using an interval size h= 0.1

9.10. Given the differential equation

dy/dx = y + x5 , with initial condition y = 3 at x =2

determine (a) y(2.1) and y(2.2) by Euler's Method using an interval size h = 0.1 (b) y(2.1)
by Modified Euler's Method, using an interval size h= 0.1. Obtain answers that are good to at
least three decimal places

9.11. Given the differential equation

dy/dx = 4x/y - xy 3 , with initial condition y = 3 at x =0,

determine (a) y(0.1) and y(0.2) by Euler's Method using an interval size h = 0.1
388 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

(b) y(0.1) by Modified Euler's Method, using an interval size h= 0.1.Obtain answers that are
good to at least three decimal places.

9.12. Given the differential equation

dy/dx = y + x 2 ,

with initial condition y = 0.515 at x = 1, determine y at x = 1.2 by Modified Euler's Method,


using an interval size of h = 0.2 .

9.13. Given the differential equation

dy/dt + y = 1 , with initial condition y = 0 at t =0,

determine (a) y(0.10) and y(0.20) by Euler's Method using an interval size h =0.10

(b) y(0.10) by Modified Euler's Method, using an interval size h= 0.10 . You will
require about 4- 5 iterations to converge to a solution. Obtain answers that are good to at
least three decimal places.

9.14. Given the differential equation

dy/dx = y+2x5 , with initial condition y = 3.525 at x =2,

determine (a) y(2.1) and y(2.2) by Euler's Method using an interval size h = 0.1. (b) y(2.1)
by Modified Euler's Method, using an interval size h= 0.1.Obtain answers that are good to at
least three decimal places.

9.15. Given the differential equation

dy/dx = -xy 2 , with initial condition y = 2 at x =1,

determine (a) y(1.1) and y(1.2) by Euler's Method using an interval size h = 0.1.(b) y(1.1)
by Modified Euler's Method, using an interval size h= 0.1.Obtain answers that are good to at
least three decimal places.

9.16. Given the differential equation

dy/dx = -xy2 , with initial condition y = 0.3333 at x = 2,

determine (a) y(2.2) and y(2.4) by Euler's Method using an interval size h = 0.2.
(b) y(2.2) by Modified Euler's Method, using an interval size h= 0.2. Obtain answers that
are good to four decimal places.

9.17. Given the initial-value problem


Chapter 9: Numerical Solution of Ordinary Differential Equations 389

dy/dx = f(x,y) = x 3 - 2 y 2 ; Initial condition : y(x=0) = 1;


h = step size =0.3 ,

obtain the solution at x = 0.3, using Version 1 of the Runge-Kutta method, which is based on
Simpson's one-third rule.

9.18.. Given the initial-value problem

dy/dx = 4 x 3 - 9 x 2 + 4 x - 1 , y= 5 at x = 1 , h= 0.2 ,

obtain a solution by the Runge-Kutta fourth-order method for the range x =1 to 2. Compare
the solution generated solution generated with the true solution which is
y = x 4 - 3 x 3 + 2 x 2 - x + 6. Using Mathcad, plot the results of the Runge-Kutta
solution and compare with the results of rkfixed and Rkadapt .

9.19. Using the Runge-Kutta fourth order method (Version2- that employs Simpson's
three-eighth rule),.obtain a solution at x= 0.1 and x= 0.2 to the differential equation

dy/dx = 4x/y - xy

with initial condition y(0)= 3. Use a step size of h = 0.1

9.20. Given

dy/dx = ( 3 x 3 + 6 x y 2 - x) / ( 2 y ) , y = 0.707 at x = 0 , h= 0.1 ,

obtain a solution by the fourth-order Runge-Kutta method for the range x=0 to 0.5.
Compare the solution generated with the true solution, which is
ln (x 2 + 2 y 2 ) - 3 x 2 . Using Mathcad, plot the results of the Runge-Kutta solution and
compare with the results of rkfixed and Rkadapt .

9.21. The governing differential equation of a series R-L circuit with a nonlinear resistor is

L dy/dt = - R { y - y 3 }

where R and L are the resistance and inductance in the circuit, and y(t) is the current. The
prescribed initial condition is: at t = o, y = 0.75 (ampere). Using Version 1 (that employs
Simpson's 1/3rd rule) as well as Version 2 ( that employs Simpson's 3/8th rule ) of the
Runge-Kutta fourth order method, obtain solutions at t= 0.5 seconds, that is, determine
y( 0.5) and dy/dt (0.5). Use a step size of h = 0.5 seconds. Take R = 2.25 Ohms, and L =
1.10 Henries. Obtain answers that are good to at least two decimal places.

9.22. Given the initial value problem


390 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

c dy/dx + k y = F(x) with c= 0.4 , k= 1, F(x)=1 and initial condition: y(0)=0.

Using Version 1 ( that employs Simpson's 1/3 rd rule ) as well as Version 2 ( that employs
Simpson's 3/8 th rule ) of Runga-Kutta's fourth order method, obtain solutions at

t= 0.5 sec, i.e, find y(0.5) and dy/dx ( 0.5 ). Use a step size of h= 0.5

9.23. Using the Runge-Kutta fourth-order method, obtain a solution to

dx/dt = f(x,y,t) = xy3 + t2 ; dy/dt= g(x,y,t) = ty + x3

for t = 0 to t= 1 second. The initial conditions are given as: x(0)=0; y (0) = 1. Use a time
increment of 0.2 second.

9.24. Given the initial value problem

d2 y/dt2 = (y / (e t + 1) , y(0)=1, and dy/dt (0) =0 ,

obtain a solution at t= 0.1 , i.e., find y(0.1) and dy/dt (0.1), using Version 1 of Runge-Kutta's
fourth order method . Use a step size of 0.1

9.25. Given the initial value problem:

d2 y/ dt 2 + 2 dy/dt + 4 y =0, y(0)=2, and dy/dt (0) =0

obtain a solution at t= 0.1 , i.e., find y(0.1) and dy/dt (0.1), using Version 1 of
Runge-Kutta's fourth order method . Use a step size of 0.1 .

9.26. Given the initial value problem

m d2y /dt2 +c dy/dt + k y3 = F(t)

with m= 1, c= 0.5 , k= 9, and F(t)=9 t and initial condns: y(0)=0; dy/dt (0) = 1 ,obtain a
solution at t= 0.5 seconds, i.e, find y(0.5) and dy/dt ( 0.5 ) using Runga-Kutta's fourth order
method. Use a step size of h= 0.5

9.27. Given the initial value problem

d2 y/ dt2 + 3 dy/dt + 4 y = 3

and initial conditions y(t=0) = 0 , dy/dt(t=0) =1, obtain a solution at t = 0.5 seconds , i.e,
determine y ( 0.5 ) and dy/dt ( 0.5 ) using Runge-Kutta's fourth order method. Use a step
size of h = 0.5. Obtain answers that are good to at least three decimal places.
Chapter 9: Numerical Solution of Ordinary Differential Equations 391

9.28. Given the initial value problem

d2 y/ dt2 + 4 dy/dt + 6 y = 6

and initial conditions y(t=0) = 0 , dy/dt(t=0) =2, obtain a solution at t = 0.5 seconds , i.e,
determine y ( 0.5 ) and dy/dt ( 0.5 ) using Runge-Kutta's fourth order method. Use a step
size of h = 0.5. Obtain answers that are good to at least three decimal places.

9.29. Given the initial value problem

d2 y/ dt2 + 2 dy/dt + 3 y = 3

and initial conditions y(t=0) = 0 , dy/dt(t=0) =1, obtain a solution at t = 0.5 seconds , i.e,

determine y ( 0.5 ) and dy/dt ( 0.5 ) using Runge-Kutta's fourth order method. Use a step
size of h = 0.5. Obtain answers that are good to at least three decimal places.

9.30. Given the initial value problem

d2 y/ dt 2 + 4 dy/dt +6 y =0, y(0)=0, and dy/dt (0) =2 ,

obtain a solution at t= 0.1 , i.e., find y(0.1) and dy/dt (0.1), using Version 1 of
Runge-Kutta's fourth order method). Use a step size of 0.1

9.31. The differential equation for a series R-C circuit with an applied voltage v(t) is


⎮ 1
R ⋅ i ( t) + ⎮ ⋅ i ( t ) dt + v C ( 0) = v ( t)
⎮ C

where v C (0) is the initial voltage across the capacitor and i(t) is the resulting current in
the circuit.

Figure P 9.31

Obtain a numerical solution to this differential equation for R = 1 Ω , C = 1 F, and v(t) =


u(t), a unit step function . Assume that there is no initial energy in the network .
392 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

9.32. The differential equation governing the vertical fall of an object in air from a height is

d 2
M ⋅ v ( t) + C ⋅ v ( t) = M ⋅ G
dt

where M is the mass of the object, C is a proportionality constant that defines the aerodynamic
drag on the object which is proportional to the square of the velocity, v(t) is
the vertical velocity ( positive downward ) the object acquires at time t measured from the start
of the fall and G is the acceleration due to gravity . Obtain and plot the numerical solution of
this differential equation over 30 seconds for M = 100 kg, and C = 0.30 N- sec 2 /m2.
Assume that the initial vertical velocity of the object at t= 0 is zero .

9.33. In a tank with inflow as well as outflow, the relationship between the liquid
level or head h (t) and the rate of inflow q in (t) is given by the following
differential equation

d h ( t)
A ⋅ h ( t) + = qin ( t)
dt R

where A is the cross sectional area of the tank and R is the hydraulic resistance which is the
change in head required to cause a unit change in the rate of outflow .

Figure P 9.33

If the hydraulic resistance is given by R = C ⋅ h ( t) ,where C is a constant, obtain a numerical


solution to the differential equation for A = 0.04 m 2 , q in = 5 x 10 - 4 m 3 / sec and C = 3500 .
The initial level in the tank is h = 0 at t= 0. Plot h versus time and determine how long it will
take for the level in the tank to rise to 2.5 m . Also determine the steady state value of h and the
time taken to achieve it.

9.34. The large-angle deflection of a cantilevered beam of length L loaded with a vertical
Chapter 9: Numerical Solution of Ordinary Differential Equations 393

load P at the free end is given by

3
2
2 −P ⋅ ( L − x ) ⎡⎢ ⎛d ⎞
2⎤

d
y ( x) = ⋅ 1+
⎢⎣ ⎜ y ( x) ⎟ ⎥⎦
dx
2 E ⋅I ⎝ dx ⎠

where y(x) is the beam deflection at a distance x from the left hand end which is the
cantilevered end, L is its length, I is the moment of inertia of the cross- section about the
neutral axis , and E is the modulus of elasticity. Using Mathcad, obtain numerical
solutions to this differential equation and generate deflection curves for a beam with a load
P = 10 kips, length L = 15 feet , and moment of inertia I = 200 in 4 in the following cases.

Figure P 9.34

a) The beam is made of steel, with modulus of elasticity E = 30 x 10 6 psi. (b) The beam
is made of wood, with modulus of elasticity E = 1.8 x 10 6 psi.
Compare your results in the
two cases with those for small-angle bending (linear beam theory ) generated by ignoring
the effect of the nonlinearity in the right hand side of the equation.

9.35. The differential equation governing the behavior of a control system is

d
2
⎛d ⎞
y ( t) + 6 ⋅ ⎜ y ( t) ⎟ + 9 ⋅ y ( t) = u ( t)
dt
2
⎝ dt ⎠

Using Mathcad, obtain and plot the numerical solution to the differential equation for zero
initial conditions. What is the steady-state value of the response and how long does it
take to achieve it ?

9.36. A spring-mass-damper system is subjected to a force pulse F(t) which is one half
of a sine wave. The governing differential equation is, thus,
394 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

2
d d
M⋅ x ( t) + C ⋅ x ( t) + K ⋅ x ( t) = F ( t)
2 d t
dt

where M is the mass , C is the damping coefficient, K is the spring constant and x(t) is
the displacement of the mass at time t. The forcing function F(t) is

( )
F ( t) = F0 ⋅ sin ω f ⋅ t 0≤t≤1

F ( t) := 0 t≥1

10
Force F(t)

10⋅ sin( π ⋅ t)
0

10
0 0.2 0.4 0.6 0.8
t

Time, sec

Figure P 9.36

Using Mathcad, obtain and plot the numerical solution ( displacement and velocity )
to the above differential equation for M= 1 kg, C = 1.25 N-sec / m, K = 10 N / m , F0 =
10 N, ω f = π, and zero initial conditions. How long does it take for the transient
vibration to die out?

9.37. The system of Problem 9.36 is now excited by the trapezoidal pulse F(t)
shown below. Using Mathcad, obtain and plot the numerical solution (displacement and
velocity ). How long does it take for the transients to die out ?
Chapter 9: Numerical Solution of Ordinary Differential Equations 395

Figure P 9.37

9.38. The Van der Pol oscillator is a spring-mass system with a damping mechanism that
depends on position as well as velocity . The motion of this system is a sustained oscillation
that is independent of initial conditions and is governed by th e following differential equation

d
2 ⎛d ⎞ (2 )
x − μ ⋅ ⎜ x ⎟ ⋅ 1 − x + x = F ( t)
dt
2
⎝ dt ⎠

Using Mathcad, obtain numerical solutions to this equation for μ = 0.01, 0.1 , 1 , when F(t) is a
unit step function, and the initial conditions are zero.

9.39. Verify, using Mathcad, that the motion of the Van der Pol oscillator of Problem 9.38
subjected to a unit step forcing function is a sustained oscillation that is independent of
initial conditions. Consider four sets of initial conditions as follows : (1) t= 0, x= 0 , dx/dt
=0 (2) t = 0 , x=0 , dx/dt = 10 (3) t=0 , x=2, dx/dt = 0 (4) t=0 , x=2, dx/dt =10.
Obtain numerical solutions for μ = 1 only.

9.40. The differential equation for a series R-L-C circuit with an applied voltage v(t) is


⎛d ⎞ ⎮ 1
R ⋅ i ( t) + L ⋅ ⎜ i ( t) ⎟ + ⎮ ⋅ i ( t ) dt + v C ( 0) = v ( t)
⎝ dt ⎠ ⎮ ⌡
C

where v C (0) is the initial voltage across the capacitor and i(t) is the resulting current in
the circuit.
396 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

Figure P 9.40

Using Mathcad, obtain a numerical solution to this differential equation for R = 1 Ω , L = 1


H , C = 0.04 F, and v(t) = u(t), a unit step function . Assume that there is initially no
current in the circuit.

9.41. A simply supported steel beam of length L is loaded with a distributed load that
increases from zero at the left hand end to a maximum value of w 0 at the right hand end.
Its deflection curve is given by the following differential equation

2 M ( x)
d
y ( x) =
dx
2 E ⋅I

where EI is the bending stiffness of the beam and the bending moment M(x) at x is

M (x) =
w0 ⋅ x
(2
⋅ L −x
2 )
6⋅L

Figure P 9.41

Using the Shooting Method, obtain a numerical solution to this boundary value problem in the
form of deflection and slope curves for w 0 = 2.5 kips/ ft, L = 15 ft , EI= 2.5x 10 9 lbs- in 2

9.42. A cylindrical metal pipe of inner radius r i and outer radius r o transports cold air at T f
Chapter 9: Numerical Solution of Ordinary Differential Equations 397

degrees C in a building which has a room temperature of T R degrees C. The differential


equation that relates the temperature in the pipe wall to radial distance is known to be

2
d d
r⋅ T ( r) + T ( r) = 0
2 dr
dr

By employing the Shooting Method, obtain the numerical solution to the differential equation
in the form of temperature and temperature gradient profiles in the pipe wall for T f = 2 deg C ,
T R = 30 deg C , r i = 2 cm and r o = 4 cm.
398 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

NOTES:
399

BIBLIOGRAPHY

1. Ayyub, Bilal M. and McCuen, Richard H., Numerical Methods for Engineers, Prentice-
Hall , Inc., 1996.

2. Bell, Lewis H. and Bell, Douglas H., Industrial Noise Control Fundamentals and
Applications, Second Edition, Marcell Dekker, Inc., 1994.

3. Cernica, John J., Strength of Materials, Holt, Rinehart and Winston, Inc., 1966.

4. Chapra, Stephen C. and Canale, Raymond P., Numerical Methods for Engineers, Fourth
Edition, McGraw Hill Book Company, 2002.

5. Deutschman, Aaron D., Michels, Walter J. and Wilson, Charles E., Machine Design,
Macmillan Publishing Company, Inc., 1975.

6. Dimargonas, Andrew D., Vibration for Engineers, Second Edition, Prentice-Hall, Inc.,
1996.

7. Gerald, Curtis F. and Wheatley, Patrick O., Applied Numerical Analysis, Fifth Edition,
Addison-Wesley Publishing Company, 1994.

8. Hibbeler, Russel C., Engineering Mechanics, Tenth Edition, Pearson Prentice-Hall,


2004.

9. Inman, Daniel J., Engineering Vibration, Second Edition, Prentice- Hall Inc., 2001.

10. Irwin, J. D., and Graf, E. R. , Industrial Noise and Vibration Control, Prentice- Hall ,
Inc., 1979 .

11. James, M. L. , Smith, G.M. and Wolford , J. C., Applied Numerical Methods for Digital
Computation, Fourth Edition, Harper Collins College Publishers, 1993.

12. Juvinall, Robert C. and Marshek, Kurt M, Fundamentals of Machine Component Design,
Third Edition, John Wiley and Sons, 2000.

13. Nakamura, Shoichiro, Numerical Analysis and Graphic Visualization with MATLAB,
Prentice-Hall, Inc., 1996 .

14. Nise, Norman S. , Control Systems Engineering, Fourth Edition , John Wiley and Sons,
Inc., 2004

15. Norton, Robert L., Machine Design- An Integrated Approach, Third Edition, Pearson
Prentice-Hall, 2006
400 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

16. Rao, Singiresu S., Mechanical Vibrations, Fourth Edition, Pearson Prentice Hall, 2004

17. Schmidt, Frank W., Henderson, Robert E. and Wolgemuth, Carl H., Introduction to
Thermal Sciences, John Wiley and Sons, Inc., 1984.

18. Shigley, Joseph E., and Mischke, Charles R., Mechanical Engineering Design, Fifth
Edition, McGraw-Hill Book Company, 1989.

19. Spotts, M. F., Shoup, T. E. and Hornberger, L. E. , Design of Machine Elements, Eighth
Edition, Pearson Prentice- Hall, 2004

20. Thomson, William T. and Dahleh, Marie Dillon, Theory of Vibration with Applications,
Fifth Edition, Prentice-Hall, Inc., 1998.

21. Volterra, Enrico, and Zachmanoglou, E. C., Dynamics of Vibrations, Charles E. Merrill
Books, Inc., Columbus, Ohio, 1965.

22. Wilson, James F. , Ed., Dynamics of Offshore Structures, John Wiley and Sons, 1984.
Index 401

INDEX Mathcad’s lspline function, 160


Mathcad’s pspline function, 160
Mathcad’s cspline function, 160
Accuracy, 43 in interpolation, 159
Analytical methods, 43 Curve-fitting (or regression analysis), 183
Approximation, 44 applications, 236-248
Animation, 7 least squares method, 184
Applications, linear regression , 191,195,197
curve-fitting, 236-248 regression with Mathcad, 207
matrices, 117-128 expfit function, 219
numerical differentiation, 262-271 genfit function, 213
numerical integration, 301-305 intercept function , 207
numerical interpolation , 168-177 interp function , 209
numerical solution of ordinary line fuinction, 208
differential Equations , 363- linear regression, 207
385 linfit function , 211
roots of equations, 87-95 lnfit function , 217
loess function , 209
logfit function , 215
Backward differences, 255 nonlinear regression, 209
Bisection method, 53 pwrfit function. 218
advantages and disadvantages , 55 regress function , 209
convergence criterion, 54 slope function , 207
finding roots, 53 straight line regression, 185
Boundary conditions, differential equations, weighted data, 184
311,358,365,378 with exponential function, 194
Boundary value problems, with linear combination of functions,
differential equations, 311 199
shooting method, 358,378 with polynomials, 203
with power function, 191
with quadratic function , 188
Characteristic equation, 53,91
Cofactor, 103
Column vector, 108,114 Degrees of freedom, 117,121
Convergence, Dependent variable,
Bisection method, 54 differential equations, 311
Iteration method, 78,84 Determinant, 104
Modified Euler’s method, 324 Differential equations, ordinary, ( see
Newton-Raphson method , 67 numerical solution of ordinary
Romberg integration, 290 differential equations ), 311
Secant method, 74 Differentiation, (see Numerical
Successive substitution method, differentiation ), 255
78,86
Cramer’s rule, 110
cspline function. 160 Eigenvalue problem, 114,116, 117
Cubic spline, 159-160 in stress analysis, 125
402 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

in vibration engineering, 117 Initial-value problems, 311


solving with Mathcad , 116 Integration, ( see Numerical integration), 277
eigenvals function, 116 intercept function, 207
eigenvec function, 116 interp function , 160
eigenvecs function , 116 Interpolating polynomial, 141- 177, 259,279
Error, Interpolation, ( see Numerical
Euler’s method, 318 interpolation ), 141
relative error, 44 Inverse of matrix, 104,108
Taylor series, 44 Inversion, matrix, 104,108
Taylor series solution, Iteration method , 78,84
differential equation, 312
true error, 44
Euler’s method, 317 Lagrangian interpolation, 155
expfit function , 219 Least squares method, 184
Exponential function,curve-fitting, 194 Linear algebraic equations, 108-114
Linear combination of functions, 199
Linear interpolation, 141
False position or Linear interpolation with Mathcad, 159
Regula falsi method, 58 Linear spline, 158
find function , 86 line function , 207
Finite differences, linfit function ,211
differentiation, 255 linterp function , 159
interpolation , 149 lnfit function, 217
First-order differential equation, 311 loess function, 209
Euler’s method, 317 logfit function , 215
4th-order Runge-Kutta method, lsolve function , 112
Simpson’s 1/3 rule, 329 lspline function, 160
Simpson’s 3/8 rule, 330
Mathcad solution,
335, 346, 354 Mathcad,
Modified Euler’s method, 323 advantages, 7
Runge-Kutta methods, 329 building math expressions, 21
Taylor series method, 312 computation , 10
Forward differences, 255 defining functions, 19
defining range variables, 23
editing expressions, 21
given function , 85 entering math, 16
Gregory-Newton method, 144 entering text, 16
exact answers , 3
feedback, 4
Higher order differential equations, 349 formatting math, 35
formatting results and graphs, 36
formatting text, 35
Independent variable, differential equations, functions, 19
311-312 graphing of functions, 5
Initial conditions, differential equations, 311 graphics, 5
Index 403

graphs, 5, 28 Cramer’s rule, 110


live math, 3 determinant, 103
Mathcad screen, 1 eigenvalues, 114-131
matrices, 24, 103 identity matrix, 104,114
palettes, 2,13 inverse, 104
plotting of data, 5, 28 minor, 103
regions, 15 multiplication, 103
styles, 17 nonlinear simultaneous
templates, 17 equations, 131
toolbars, 13 operations, 103
tutorial, 7 stress analysis application, 125
using units, 39 transpose, 103
variables, 18 unit matrix , 104
vectors, 24 use of eigenvals, 116
window, 13 use of eigenvec, 116
Mathcad functions, use of eigenvecs, 116
cspline , 160 use of lsolve, 112
eigenvals, 116 vibration engineering
eigenvec, 116 application, 117-124
eigenvecs, 116 Modified Euler’s method, 323
expfit, 219 Mode shapes, 118-121,124
find , 86 Multiple roots, 80
genfit, 213
given , 86
intercept, 207 Natural frequencies, 118-119,124
interp, 209 Natural modes, 120, 124
linfit, 211 Newton-Raphson method, 65
linterp, 159 Advantages, 67
lnfit, 217 Disadvantages, 67
loess, 209 Newton interpolating polynomial, 151
logfit, 215 Newton interpolation, 151
lsolve, 112 Normal modes, see natural modes, 120,124
lspline , 160 Normalization , 120
polyroots, 71, 72 Numerical methods, 43
pspline, 160 Numerical differentiation, 255
pwrfit, 218 applications, 262- 271
regress, 209 calculus definition , 255
rkadapt, 340,348,360 finite differences, 255
Rkadapt, 335, 346, 354 first derivative, 255, 257
rkfixed , 335,346, 354 interpolating polynomial
root, 71 method, 259
slope, 207 Mathcad derivative operators,
Matrices, 103 255,256
addition, 103 second derivative, 257-259
adjoint, 104 Numerical integration , 277
cofactor, 103 applications, 301- 305
404 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD

interpolating polynomial shooting method, 358,378


method, 279 Taylor series method, 312
Mathcad’s integral use of rkfixed & Rkadapt,
operators, 277, 278 335,346, 354
Romberg integration, 288 use of rkadapt , 340,348,360
Simpson’s 1/3 rule, 283
Simpson’s 3/8 rule, 286
trapezoidal rule , 280 One-step method, 317,323
Numerical interpolation, 141 Order, differential equation, 311
applications, 168-177 Ordinary differential equations ( See
cubic spline interpolation, 158-160 numerical solution of ordinary
finite differences, 149 differential equations , 311
Gregory-Newton method, 144 Orthogonality, 128
interpolation with Mathcad, 159
Lagrangian interpolation, 155
linear interpolation, 141 Polynomial, 53,141,144,149,155,203
linear spline, 158 polyroots function , 72
Newton’s method , 151 Power function, curve-fitting, 191
quadratic spline, 158 Principal directions, 125
undetermined coefficients, 141 Principal stresses, 125
use of cspline , 160 pspline function , 160
use of lspsline, 160 pwrfit function, 218
use of pspline , 160
use of interp , 160
use of linterp , 159 Quadratic function, curve-fitting, 188
Numerical solution of ordinary Quadratic spline, interpolation, 158
differential equations, 311
applications , 363-385
boundary value problems, 311 Regula falsi or false position
Euler’s method, 317 method, 58
first -order equations, 311,335 regress function , 209
higher- order equations, 349 Regression analysis ( see
initial value problems, 317 curve-fitting), 183
Mathcad solution to first Relative error, 44
order equation, 335 rkadapt function , 340,348,360
Mathcad solution to second order Rkadapt function , 336,346, 354
equation, 346 rkfixed function , 335,346, 354
modified Euler’s method, 323 Romberg integration , 288
Runge-Kutta fourth order root , 71
method, 329 Root-finding, 53
Simpson’s 1/3 rule, 330 applications, 87-95
Simpson’s 3/8 rule, 331 bisection method, 53
Runge-Kutta methods, 329 False position or Regula falsi
second- order equations, 349- 363 method, 58
systems of ordinary differential iteration method, 78,84
equations, 349 multiple roots , 80
Index 405

Newton-Raphson method, 65 curve-fitting, 185-206


Regula falsi or false position interpolation, 141
method, 58
secant method, 72
successive substitution, 78 Weighting factors,
using Mathcad, 71,86 curve-fitting, 184
using root, 71
using polyroots , 72
Runge-Kutta methods, 329
first order differential equation, 329
first order method, 329
fourth order method, 329,341, 350
second-order differential equation,
349-363
second order method, 329
Simpson’s 1/3 rule, 330
Simpson’s 3/8 rule, 331

Secant method, 72
Second order differential equation, 311,
349- 363
Shooting method,
boundary value problems,
31, 358, 378
Simpson’s one-third rule,
differential equation, 329
numerical integration, 283
Simpson’s three-eighth rule,
differential equation, 330
numerical integration, 286
slope function , 207
Successive substitution method, 78
Systems of ordinary differential equations,
349

Taylor series,
basis of approximation in numerical
analysis, 44
differential equation solution, 312
Trapezoidal rule, integration, 280
True error, 44

Undetermined coefficients,

You might also like