Professional Documents
Culture Documents
An Introduction To Numerical Methods Usi PDF
An Introduction To Numerical Methods Usi PDF
Mathcad Release 14
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 .
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
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
6. Curve-Fitting. 183
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.
CHAPTER 1
BASICS OF MATHCAD
1.1 INTRODUCTION
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
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 ⎠
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
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
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.
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.
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
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
. . . 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.
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 .
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.
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
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.
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
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.
3 6 − 5i 3 3
[ 14.95 + ( 5.7i) ] + e = 1.999 × 10 + 4.024i × 10 <-- Complex numbers.
sin ( x )
f ( x ) := f ( 37 ) = −0.045 <-- Use of a defined function
x
a+
6
12 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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
0.2
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
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
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.
This is below the main menu. It opens up palettes of math operators as described below.
14 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
Calculus toolbar
Just click on one of these as appropriate and start inserting math symbols into the
worksheet.
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.
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.
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
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 .
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
Selected regions in dashed line boxes can be deleted by choosing Cut from the Edit menu.
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 .
Entering Math
Math can be entered by clicking the mouse in a blank space to see a red crosshair cursor.
Type
1+
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
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.
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.
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
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
Type
Totprice=
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.
Totprice = 30
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
7
f ( x ) := x
5
f ( 6) = 2.799 × 10
5
x := 6 f ( x ) = 2.799 × 10
x := 1 .. 6
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.
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⎠
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
7
x + 675
780
Type
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.
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
x := 0 , 3 .. 30
x:0;7 f(x):x^5[Spacebar]-6*x^3[Spacebar]+9
24 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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
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
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 .
V[0=
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 )
⎛⎜ 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
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
⎛⎜ 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
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.
x := 0 , 0.1 .. 6
600
400
f ( x)
200
0 2 4 6
x
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
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
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.
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 ⎟
⎝ ⎠
( )
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 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
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 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
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
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.
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)
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
R := 4 ⋅ km
40 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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
The following calculation, obviously, cannot be completed and Mathcad indicates the same.
7 ⋅ ft + 6 ⋅ sec =
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.
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
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.
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.
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.
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.
while the relative true error is the error relative to the true value
XComp − XTrue
er = ⋅ 100 (%) (2.3)
XTrue
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
( ) ( )
f x0 + h = f x0 + h ⋅
d
( )
f x0
(2.5)
dx
2
( ) ( )
f x0 + h = f x0 + h ⋅
d
( )
f x0 ⋅
d
2
( )
f x0 (2.6)
dx dx
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
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.
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
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
ftaylor3 := term1 + term2 + term3 + term4 <---third order approximation with 4 terms
ftaylor4 := term1 + term2 + term3 + term4 + term5 <---fourth order approximation with 5 terms
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
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
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
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
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
x := 0 , 0.01 .. 5
0
err0( x)
err3( x) 50
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
Table 2.2
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 ,
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.
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 .
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.
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.
f(x) = e x
about x = 0 , determine the fourth-order approximation of e 2.5 and compare it with the exact
solution.
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
f(x) = x3 e - 5x
about x = 1, determine the third-order approximation of f(1.2) and compare it with the exact
solution.
f(x) = e cos x
about x =0 , determine the fourth- order approximation of f (2π) and compare it with the exact
solution. .
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.
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.
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.
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
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.
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.
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.
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.
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
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.
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.
4. If the above product is negative, then the root lies between xstart and xend. If this
product is positive, reselect xstart and xend.
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
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.
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
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
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
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
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
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
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
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
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
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))
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.
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
xend − xstart
ystart = 2.8146 yend = −4.2539 xnew := xend − ⋅ yend xnew = 17.5927
yend − ystart
The root must lie beteen above xstart and xnew. Thus the new xstart is 16 and the new
xend is 17.5927
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
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
−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
−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
−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
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
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
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
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
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
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
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
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
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
xend − xstart
ystart = −2.2078 yend = 890 xnew := xend − ⋅ yend xnew = 5.3246
yend − ystart
The root must lie beteen above xnew and xend. Thus the new xstart is 5.325 and the new
xend is 10
xend − xstart
ystart = −1.0813 yend = 890 xnew := xend − ⋅ yend xnew = 5.3307
yend − ystart
The root must lie beteen above xnew and xend. Thus the new xstart is 5.331 and the new xend
is 10
xend − xstart
ystart = −0.5163 yend = 890 xnew := xend − ⋅ yend xnew = 5.3337
yend − ystart
The root must lie beteen above xnew and xend. Thus the new xstart is 5.334 and the new
xend is 10
xend − xstart
ystart = −0.2334 yend = 890 xnew := xend − ⋅ yend xnew = 5.3352
yend − ystart
The root must lie beteen above xnew and xend. Thus the new xstart is 5.335 and the new
xend is 10
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
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.
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)
( )
f xi
xi+1 = xi −
fprime ( x i)
( )
= g xi (3.7)
(
f X Old )
X New = X Old − (
= g XOld ) (3.8)
(
fprime X Old )
Chapter 3: Roots of Equations 67
4. Solution is done when a new improved value X New is almost equal to the
previous value X Old
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.
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
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.
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
−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
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.
3 2
f ( x ) := x + 7 ⋅ x + 19 ⋅ x + 13
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.
−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
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)
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 )
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.
3
f ( x ) := x − 4.2 ⋅ x − 8.5
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
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 ⎠
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
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.
( )(
f x i ⋅ xi−1 − xi )
xi+1 = xi −
f ( x i− 1) − f ( x i)
4. Solution is done when a new improved value x i+1 is almost equal to the previous
value x i
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
− 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)
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)
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
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 ⎠
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 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 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
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 ⎠
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)
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.
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:
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
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.
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.
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
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
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)
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.
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 ⎠
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
2
10.5 − 1.1 ⋅ x 2
x= y = 58.2 − 3.2 ⋅ x ⋅ y
1.2 ⋅ y
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
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 #
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
----------------------------------------------------------------------------------------------------------------------------------
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.
Example 3.11.
2 2
1.5 ⋅ x + 2 ⋅ y = 6.5. x + y = 2.75
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 ⎠
The following are examples of situations where roots of nonlinear equations need to be
evaluated.
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
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.
Here,
6
σyld := 40000. (psi) E := 30 ⋅ 10 (psi) FS := 2.75
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 ⎠
((
a := root f Pmax , Pmax ) )
5
a = 1.0221 × 10 (lbs) <-- Maximum allowable load column can carry
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
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.
ρ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
a
β := β = 0.0938
L
4
EI ⋅ β
ω := ω = 27.7106 (rad/sec) <-- First or fundamental
ρ natural frequency
a = 4.6941
a
β := β = 0.2347
L
4
EI ⋅ β
ω := ω = 173.6595 (rad/sec) <-- Second natural frequency
ρ
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.
⎛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
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.
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
The use of the Secant method in obtaining the required root is shown below.
( )(
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 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.
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
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 2
x − 6.1 ⋅ x + 11.26 ⋅ x − 6.336
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 2
x − 2⋅x − 5 ⋅x + 6
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
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
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
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:
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:
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:
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
where Det (A) is the determinant of the given matrix, which must be non-zero for the inverse to
exist.
Example 4.1
⎛⎜ 1 2 3 ⎞⎟
A := ⎜ 1 3 2 ⎟
⎜1 0 4 ⎟
⎝ ⎠
⎛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,
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 ⎟
⎝ ⎠⎝ ⎠ ⎝ ⎠
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.
⎛⎜ 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
⎛⎜ 5 0 10 ⎞⎟
A ⋅ 5 = ⎜ 5 15 10 ⎟
⎜ 35 40 45 ⎟
⎝ ⎠
Determinant of A : A = −15
⎛⎜ 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.
For a system of linear algebraic equations that can be written in matrix form as
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
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 ⎟
⎝ ⎠ ⎝ ⎠
⎛⎜ 1 0 0 ⎞⎟
−1
A ⋅A = ⎜0 1 0 ⎟
⎜0 0 1 ⎟
⎝ ⎠
−1
X := A ⋅B
yielding
⎛⎜ 2 ⎞⎟
X = ⎜ −4 ⎟
⎜1 ⎟
⎝ ⎠
Letting
⎛ A11 A12 B1 ⎞
⎜ ⎟
⎜ A21 A22 B2 ⎟
⎜A ⎟
X3 =
⎝ 31 A32 B3 ⎠
⎛ A11 A12 A13 ⎞
⎜ ⎟
⎜ A21 A22 A23 ⎟
⎜A ⎟
⎝ 31 A32 A33 ⎠
⎛⎜ 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
The lsolve function in Mathcad can be used to solve a system of linear algebraic equations .
Given:
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
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
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}
⎛⎜ 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
⎝ ⎠
⎛⎜ 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
⎛⎜ 0.676 ⎞⎟
X := lsolve ( A , B) X = ⎜ −0.254 ⎟ <--These are the currents i1 , i2 and i 3
⎜ 0.423 ⎟
⎝ ⎠
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.
{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
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 ⎠
⎛ 17 − λ −6 ⎞
A − λ ⋅I = ⎜ ⎟
⎝ 45 −16 − λ ⎠
⎛ 17 − λ −6 ⎞
⎜ ⎟ =0
⎝ 45 −16 − λ⎠
⎛ −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 ⎠
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 ⎟
⎝ ⎠
⎛⎜ 0.923 ⎞⎟
eigenvec ( A , 0.468 ) = ⎜ −0.127 ⎟
⎜ −0.364 ⎟
⎝ ⎠
⎛ 17 −6 ⎞
A := ⎜ ⎟
⎝ 45 −16 ⎠
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
2 m d2x1 /dt2 + 3 k x1 - k x2 = 0
(4.11)
m d 2x2 /dt2 +k ( x2 - x 1 ) = 0
where [M] is the mass or inertia matrix , and [K] is the stiffness matrix, and assuming that
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
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.
m := 1 k := 1
⎛ 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 − λ) 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]
⎛ 0.5 ⎞ ⎛1 ⎞
FirstMode := ⎜ ⎟ and SecondMode := ⎜ ⎟
⎝ 1 ⎠ ⎝1 ⎠
⎛ 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
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.
⎛ 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 ⎠
Example 4.5.
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
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 ⎦
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 ⎠
⎛ 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⋅π
⎛⎜ 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.
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
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:
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 ⎠
Example 4.7
σxx= 1000 psi, σyy = -1000 psi, σxy = 500 psi , σ yz = -200 psi, σ zx =100 psi , σ zz = 250
psi
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 ⎠
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
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
π
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
Example 4.8
⎛⎜ 0 −1 1 ⎞⎟
A := ⎜ −1 0 1 ⎟
⎜ 1 1 0⎟
⎝ ⎠
⎛⎜ 1 ⎞⎟
<-- double root at λ = 1
lambda := eigenvals ( A) lambda = ⎜ −2 ⎟
⎜1 ⎟
⎝ ⎠
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 ⎟
⎝ ⎠
⎛⎜ 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
⎛⎜ −14 1 0 ⎞⎟
A := ⎜ 0 2 0 ⎟
⎜ 1 0 2⎟
⎝ ⎠
⎛⎜ 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 .
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.
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
Iteration Number 1
2 2 2
f ( x , y ) := x + 3 ⋅ y − 54 g ( x , y ) := −4 ⋅ x + 3 ⋅ x ⋅ y − 2 ⋅ y + 15
where the above are partial derivatives of f(x,y) and g(x,y) with respect to x and y.
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
⎛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
Iteration Number 2
⎛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
Following the same procedure as above, new improved values for x and y can be generated in
iterations 3, 4 and 5 as
−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.
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 ⎟
⎝ ⎠
⎛⎜ 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.
(a) x + y = 4 ; 2x - y = 2
(b) 5x - 3 y = 37 ; -2x + 7 y = - 38
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.
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
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.
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.
⎛1 3 ⎞
⎜ ⎟
⎝2 1 ⎠
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 .
⎛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
⎛ 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
using the Taylor series approach. Check your answer using the Given and Find functions
of Mathcad
using the Taylor series approach. Check your answer using the Given and Find functions of
Mathcad .
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
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
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.
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.
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
( )
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
2 n
Pn( x ) = a0 + a1 ⋅ x + a2 ⋅ x + ............ + an ⋅ x , (5.3)
⎡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 :
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
2
f ( x ) := a + b ⋅ x + c ⋅ x with a= 2 b = −1.41 c = 1.66
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
( ) ( )( ) ( )( )(
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
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)
( ) ( )(
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 -
with a1 = Y1
a2 = (Y2-Y1) / (X2-X1)
⎛⎜ 100 ⎞⎟ ⎛⎜ 975 ⎞⎟
X := ⎜ 200 ⎟ Y := ⎜ 1575 ⎟
⎜ 400 ⎟ ⎜ 2054 ⎟
⎝ ⎠ ⎝ ⎠
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
( ) ( )(
f ( x ) := a1 + a2 ⋅ x − X1 + a3 ⋅ x − X1 ⋅ x − X2 )
3
f ( 251) = 1.788 × 10
2000
Function values
Yi
1500
f( x)
1000 Given data points
Interpolation
500
100 150 200 250 300 350 400
Xi , x
x- values
Example 5.3
Because six data points are given, the interpolating polynomial in this case will be of fifth
order, and will have the following form
y2 − y1 y3 − a1 − a2 ⋅ ( x3 − x1)
a1 := y1 a2 := a3 :=
x2 − x1 ( x3 − x1) ⋅ ( x3 − x2)
−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
yielding
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
100
Function values
Yi
f( x)
50
0
0 100 200 300 400 500
Xi , x
x-values
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
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
------------------------------------------------------------------------------------------------------------------------------------
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
------------------------------------------------------------------------------------------------------------------------------------
( ) ( )( ) ( )(
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 )
Δ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
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
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
________________________________________________
0 895
-37
20 858 -99
-136 2
40 722 -97 74
-233 76
60 489 -21
-254
80 235
____________________________________________________
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;
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
FLUX
600
R( L)
400
200
0 10 20 30 40 50 60 70 80
LAT , L
Latitude- degrees North
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
Example 5.5
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 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
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
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
Y
1000
f( x)
800
600
100 150 200 250 300 350 400 450
X, x
X- Values
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)
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)
(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.
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 .
lspline(vx,vy) : This generates a spline curve that approaches a straight line 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 .
⎛⎜ 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
The interp function along with lspline, pspline and cspline can be utilized as follows to
generate cubic splines
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
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
4
f2( x)
f3( x)
3
flin( x)
1
0 0.5 1 1.5 2
vx , x
x- Values
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 ⎟
⎝ ⎠ ⎝ ⎠
3 3
fcspline ( 251) = 1.737 × 10 flin ( 251) = 1.697 × 10
Chapter 5: Numerical Interpolation 163
2000
1800
function values
vy
1600
fcspline ( x)
flin( x) 1400
1200
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 )
x := 0 , 5 .. 500
150
Given data points
Interpolated values
linterp( vx , vy , x)
50
0
0 100 200 300 400 500
vx , x
x- values
The Gregory Newton polynomial for this problem has been derived earlier and is the function
f(x) shown below .
−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
<-- Mathcad
flin ( x ) := linterp ( vx , vy , x ) flspline ( x ) := interp ( vs1 , vx , vy , x ) splines
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
The Mathcad interpolated values at x = 150, 350 and 450 are shown below.
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 ⎞ ⎛ y1 ⎞
⎜ ⎟ ⎜ ⎟
vx :=
⎜ x2 ⎟ vy :=
⎜ y2 ⎟
⎜ x3 ⎟ ⎜ y3 ⎟
⎜ ⎟ ⎜ ⎟
⎝ x4 ⎠ ⎝ y4 ⎠
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
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
The various interpolations resorted to give the following interpolated function values f or x = 102,
322 and 415.
3
x=415: f ( 415 ) = 955.728 flin ( 415 ) = 1.017 × 10 flspline ( 415 ) = 985.191
168 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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.
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.
fspline ( ε ) := interp ( Vs , Vε , Vσ , ε ) <-- Generates a cubic spline function for given data
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
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.
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 ⎠
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 ⎟
⎝ ⎠ ⎝ ⎠
fspline ( 0.13) = 0.787 fspline ( 0.15 ) = 0.813 f ( 0.15 ) = 0.814 fspline ( 0.15 ) = 0.813
0.8
Notch sensitivity, q
Vq i
f( r) 0.6
fspline ( r)
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
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.
x 1 := 0 x 2 := 2 x 3 := 4 x 4 := 6 x 5 := 8 x 6 := 10 x 7 := 12
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
−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
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⎠
x := 0 , 1 .. 12 i := 1 , 2 .. 7
Chapter 5: Numerical Interpolation 175
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
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.
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,
( 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)
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 )
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.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).
x 0 20 40
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
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.
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.
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.
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
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
5.10. For the given data, generate both a finite-difference table and an interpolating
polynomial.
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.
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.
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
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.
5.15. The following table gives the pressure (P) versus temperature (T) relationship in the
liquid-vapor region for water [ 17 ]
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 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
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
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
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.
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)
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
⎡ 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
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
⎡ 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
⎛ 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
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
i := 1 .. 11
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
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)
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)
⎡ 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
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
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
⎡ 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 ⎦
C := lsolve ( M , A) ⎛ 0.779 ⎞
C= ⎜ ⎟ C1 = 0.779 C2 = 2.554
⎝ 2.554 ⎠
2
f ( x ) := C1 + C2 ⋅ x
which is now plotted and compared with the given data in Figure 6.3
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
( )
f xi
50
yi
0
0 1 2 3 4 5
xi
x-Values
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)
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
⎡ 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
⎡ 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 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
( )
f xi
yi 5
x-Values
Function of Form A e Bx
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 ⎦
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
or, ln ( y/x) = ln ( A) + Bx
Z=C+Bx (6.24)
where,
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.
⎡ 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
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 ⎦
⎛ 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
f ( e) := A ⋅ e ⋅ exp ( B ⋅ e)
which is plotted and compared with the given data in Figure 6.6
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
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:
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" .
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)
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 ⎦
Example 6.6.
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 ⎦
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 ⎦
C := lsolve ( M , L) ⎛ 5.931 ⎞ C1 := C1 C2 := C2
C= ⎜ ⎟
⎝ 0.35 ⎠
C1 = 5.931 C2 = 0.35
which is plotted in Figure 6.7 and compared with the given data.
z := 1 , 1.1 .. 5 i := 1 .. 5
40
Y- Values
yanal ( z)
yi
20
0
1 2 3 4 5
z , xi
X- Values
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
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 ⎦
Example 6.7.
x: 0 1 2 3 4 5
2
Obtain a curve-fit of the form y ( x ) = f ( x ) = C1 + C2 ⋅ x + C3 ⋅ x
[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
⎡ 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 ⎦
⎛ −0.444 ⎞
C := lsolve ( M , L) C = ⎜ 12.484 ⎟
⎜ ⎟
⎝ −2.057 ⎠
C1 := C1 C2 := C2 C3 := C3
2
f ( x ) := C1 + C2 ⋅ x + C3 ⋅ x
which yields
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
z := 0 , 0.01 .. 5
20
15
Y-Values
f( z)
10
yi
X- Values
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
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
⎛ 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
⎛c ⎞
⎜ ⎟ := line ( vx , vy) m = −99.618 c = 75.497
⎝m ⎠
Figure 6.9 shows a comparison of the data with the curve-fits generated
yi
f( x) 40
ylinmathcad( x)
20
0
0.1 0.2 0.3 0.4 0.5 0.6
xi , x
X- Values
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.
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
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 :
A comparison of curve-fits generated for the given data is presented in Figure 6.10 and
in Table 6.1
f2( z)
0
0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
xi , z
X-Values
TABLE 6.1
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
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 curve-fit of Example 6.6 was obtained as a linear combination of the form:
3
C1 := 5.931 C2 := 0.35 f1 ( x ) := 1 f2 ( x ) := x
yanal ( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x )
vx := x vy := y
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
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
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
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
y: 9.4 11.2, 5, 3 , 6, 0
⎛⎜ 0.3 ⎞⎟ ⎛⎜ 9.4 ⎞⎟
⎜ 0.4 ⎟ ⎜ 11.2 ⎟
⎜1 ⎟ ⎜ 5 ⎟
vx := ⎜ ⎟* vy := ⎜ ⎟*
⎜ 1.4 ⎟ ⎜ 3 ⎟
⎜2 ⎟ ⎜ 6 ⎟
⎜4 ⎟ ⎜ 0 ⎟
⎝ ⎠ ⎝ ⎠
⎛1 ⎞
vg := ⎜ 0 ⎟
⎜ ⎟*
⎝ −1 ⎠
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
* * *
It is plotted in Figure 6.12 and is compared with the given data points
i := 0 .. 6 x := 0.3 , 0.31 .. 4
* *
g ( x)
5
0
0 0.5 1 1.5 2 2.5 3 3.5 4
vxi , x
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 ⎟
⎝ ⎠ ⎝ ⎠
⎛⎜ 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.
⎛⎜ 1 ⎞⎟ ⎛⎜ 5 ⎞⎟
⎜2 ⎟ ⎜ 6.5 ⎟
⎜3 ⎟ ⎜ 7.25 ⎟
vx := ⎜ ⎟ vy := ⎜ ⎟
⎜4 ⎟ ⎜ 7.80 ⎟
⎜5 ⎟ ⎜ 8.25 ⎟
⎜6 ⎟ ⎜ 8.63 ⎟
⎝ ⎠ ⎝ ⎠
⎛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 ⎟
⎝ ⎠ ⎝ ⎠
⎛⎜ 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 ⎠
⎛⎜ A ⎞⎟
⎜ b ⎟ := expfit ( vx , vy , vg)
⎜C ⎟
⎝ ⎠
⎛⎜ A ⎞⎟
⎜ b ⎟ := expfit ( vx , vy)
⎜C ⎟
⎝ ⎠
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
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
B
f ( x) = A ⋅ x
with
A := 6.571 B := 0.2
B
f ( x ) := A ⋅ x
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.
i := 1 , 2 .. 16
222 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
( )
Yi := ln y i ( )
Xi := ln x i
The slope and the intercept of the new linear relationship Y= CSTAR+BSTAR*X can now
be computed as
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
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
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
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
i := 1 , 2 .. 6
⎛⎜ 0 ⎞⎟ ⎛⎜ 16.45 ⎞⎟
⎜1 ⎟ ⎜ 4.47 ⎟
⎜2 ⎟ ⎜ 1.06 ⎟
x := ⎜ ⎟ y := ⎜ ⎟
⎜3 ⎟ ⎜ 0.45 ⎟
⎜4 ⎟ ⎜ 0.15 ⎟
⎜5 ⎟ ⎜ 0.03 ⎟
⎝ ⎠ ⎝ ⎠
n := 6
B⋅ x
f ( x) = A ⋅ e
B⋅ x
f ( x ) := A ⋅ e <-- Analysis-generated fit
( )
fi := f x i ()
logfunci := ln f i
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,
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
10
fmcad ( z)
exponfit ( z) − 0.05
0 1 2 3 4 5
xi , z
x- values
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 .
i := 1 , 2 .. 6
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 ⎠
In Mathcad, linear regression can be performed on this problem and the curve-fit obtained as
shown in the following steps, :
i := 1 , 2 .. 6
⎛ 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
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 .
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.
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.
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
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
Using Mathcad's interp and loess functions , curve-fitting the given data with a polynomial can
be done as shown below.
( 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 )
yi
Y- Values
X- Values
Example 6.17
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.
N := 8
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"
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
yanal ( x ) := C1 ⋅ f1 ( x ) + C2 ⋅ f2 ( x ) + C3 ⋅ f3 ( x ) + C4 ⋅ f4 ( x )
The use of " linfit" as applied to this problem is illustrated in the following steps.
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
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
The Mathcad ( linfit ) results and the linear combination analysis results are both presented in
Figure 6.18 and compared with the given data points.
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
Example 6.18
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
⎛⎜ 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
u1 := P1 u2 := P2 u3 := P3
It is plotted in Figure 6.19 and is compared with the given data points
i := 1 .. 8 x := 0 , 0.1 .. 7
* *
40
30
vy i
f( x)
20
10
0
0 1 2 3 4 5 6 7
vxi , x
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.
⎛ 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
i := 1 .. 14
Given Data
80
Given S- values (kpsi)
vSi 60
40
⎡ 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
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.
⎛ 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 .
60
S( L )
fmcad( L )
40
20
0 0.2 0.4 0.6 0.8 1 1.2
vLi , L
Figure 6.21. Fatigue failure curve for loading in the finite life range
Chapter 6: Curve-Fitting 239
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.
0.0 14 500 51
100 28 600 54
200 37.5 700 55
300 44 800 57.8
400 48 900 59.1
T1 := 14 T2 := 28 T3 := 37.5 T4 := 44 T5 := 48 T6 := 51 T7 := 54
i := 1 .. 10
60
Given data points
40
Ti
20
0
0 200 400 600 800
ti
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
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 ⎠
−1
⋅t
300
f ( t) := A + B ⋅ e
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
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
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
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.
The given data points, which are plotted in Figure 6.24 below, are put in as follows.
Factor Kd
Kd i
Temperature, deg. F
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
[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⎠
⎡ 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 ⎦
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
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
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.
n := 16 i := 1 , 2 .. 16 h1 := 1 hi+1 := hi + 1
a9 := 3.25 a10 := 3.3 a11 := 3.55 a12 := 3.65 a13 := 3.75 a14 := 3.85 a15 := 4.2 a16 := 4.3
a = f(h) = C1 + C2 h + C3 h 2 + C 4 h 3
[M]{C} = {A},
ai 3
1
0 2 4 6 8 10 12 14 16
hi
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
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
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.
y: 52 46 38 26
6.2. Given:
x: 0 1 2 3 4
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)
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
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
Determine y(1.5)
Strain, x, ( μ -in/in) 40 50 70 90
Determine a least squares fit of the form f(x)= Ax e (Bx). Determine f(45) and f(60)
e: 43 57 68 84 99 115 (micro-in/in)
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
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).
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
( − 0.0033⋅ t)
f ( t) = A + B ⋅ e
and estimate the temperature attained by the object at time t = 600 seconds
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.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
10 63
20 63.6
40 62
60 60
80 58
100 56
120 52
140 48
150 47
2
S = C1 + C2 ⋅ e + C3 ⋅ e
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.
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
6.18. The following data , in which the time, t, is given in seconds provides the
Chapter 6: Curve-Fitting 253
t := ( 0 0.75 1.50 2 3 4 5 6 )
6.19. The Brinell hardness number, Bhn , for steel [ 18 ] is given as a function of the
tempering temperature, T, in the following table
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.
CHAPTER 7
NUMERICAL DIFFERENTIATION
7.1 INTRODUCTION TO NUMERICAL DIFFERENTIATION AND THE
USE OF THE MATHCAD DERIVATIVE OPERATORS
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
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
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
2 3 n
f ( x ) = a0 + a1 ⋅ x + a2 ⋅ x + a3 ⋅ x + ............ + an ⋅ x (7.10)
d 2 n− 1
f ( x ) = a1 + 2 ⋅ a2 ⋅ x + 3 ⋅ a3 ⋅ x + .............. + n ⋅ an ⋅ x (7.11)
dx
Example 7.2
x 2 3 4 5 6
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).
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 ⎠
−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
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
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.
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
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
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 ⎟ ⎝ ⎠
⎝ ⎠
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.
The following examples illustrate situations where numerical differentiation can be applied
and used to advantage in generating realistic solutions to practical problems.
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
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
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
2 3 4 5
s ( t) := a + b ⋅ t + c ⋅ t + d ⋅ t + e ⋅ t + f ⋅ t
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 )
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
400
s( t )
200
0
0 10 20 30 40
t
Time , secs
d 15
s( t )
dt
10
5
0 10 20 30 40
t
Time, secs
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
The following displacement (x) versus time (t) data is provided for a 200 kg motorcycle with a
shock absorber .
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 .
i := 1 , 2 .. 6
t1 := 0 ti+ 1 := ti + 0.5
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
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.
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 ⎠
a := A1 a= 0 c := A3 c = −3.69917 d := A4 d = 2.65167
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
Vxi
x( t) 0.1
fspline ( t)
0
Time, seconds
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
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
τ d := 2 seconds
π
ω 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].
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
t := 0 , 0.1 .. 2.5
1
Force, Newtons
Fdamper ( t)
0.5
Fspring( t)
0.5
0 0.5 1 1.5 2 2.5
t
Time, seconds
PROBLEMS
θ = ( 4 , 5 , 6 , 7 , 8)
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.
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.
x 1 2 3 4 5
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.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
⎛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 )
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
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
(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
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
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
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.
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.
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
Example 8.1.
x 2 3 4 5 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
6
⌠
⎮ y ( x ) dx = 6.1803
⌡
2
280 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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
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
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.
π
h := 15 ⋅ h = 0.2618
180
y6 := 0.5 y7 := 0.0
The value of the desired integral can be estimated by the trapezoidal rule as
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
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
In general, if the area under a curve is divided into n segments, there are (n+1 ) points on the
curve
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
π
h := 15 ⋅ h = 0.2618
180.
y6 := 0.5 y7 := 0.0
y1 + 4 ⋅ y2 + 2 ⋅ y3 + 4 ⋅ y4 + 2 ⋅ y5 + 4 ⋅ y6 + y7
Int1 := h ⋅ Int1 = 1.0004
3
⎛ π ⎞
⎜⌠ 2 ⎟
⎜⎮ ⎟
Trueval := ⎮ sin ( 2 ⋅ θ) dθ
⎜⌡ ⎟
⎝ 0 ⎠
Trueval := 1.0
Trueval − Int1
%err1 := ⋅ 100 %err1 = −0.0433
Trueval
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
( )
3
A= ⋅ h ⋅ y1 + 3 ⋅ y2 + 3 ⋅ y3 + y4 (8.15)
8
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)
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
π
h := 15 ⋅ h = 0.2618
180.
y6 := 0.5 y7 := 0.0
⎛ 3 ⎞ ⋅ h ⋅ ( y1 + 3 ⋅ y2 + 3 ⋅ y3 + 2 ⋅ y4 + 3 ⋅ y5 + 3 ⋅ y6 + y7 )
Int2 := ⎜ ⎟ Int2 = 1.001
⎝ 8⎠
Trueval := 1.0
Trueval − Int2
%err2 := ⋅ 100 %err2 = −0.1008
Trueval
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)
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 (8.19)
I0 , 1 = ⋅ h ⋅ [ ( f ( a) + f ( b) ]
2
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
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 .
5. Obtain a fifth trapezoidal estimate I 4,1 with sixteen segments and h = (b-a)/16
( )
1
Ii , j = Ii , j −1 + ⋅ Ii , j −1 − Ii−1 , j −1 (8.23)
j −1
4 −1
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
TABLE 8.1
⎛⎜ 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.
3
−x
f ( x ) := e a := 0.1 b := 1.75
−3
f ( a) = 0.999 f ( b) = 4.70385 × 10
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
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 ⎠
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 ⎠
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
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 ⎠
TABLE 8.2
The Romberg table for the integration of exp( - x 3 )
____________________________________________________________________________
0.82806
0.78791 0.77452
______________________________________________________________________________
Notice that convergence has taken place at an integral value of 0.79254 as shown in the
tabular presentation.
1.75
⌠
⎮ f ( x ) dx = 0.79254
⌡
0.1
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 .
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
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 ⎠
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 ⎠
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
I4 , 3 = 10.20791 I4 , 4 = 10.20791
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
9.957348
10.151155 10.215758
______________________________________________________________________________
The tabular presentation of integral values shows that convergence has taken place at an
integral value of 10.2079
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
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
h b− a
h := h= 2 n := n= 2
2 h
(
I1 , 1 := 0.5 ⋅ h ⋅ y 1 + 2 ⋅ y 2 + y 3 ) I1 , 1 = 4.64 <-- SEC TRAPEZOIDAL ESTIMATE
h b− a
h := h= 1 n := n= 4
2 h
⎛ 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
⎛ 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
j− 1
j := 2 .. 4 i := 1 , 2 .. 3 4 ⋅ Ii , j− 1 − Ii−1 , j−1
Ii , j :=
j−1
4 −1
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
______________________________________________________________________________
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
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
0.4
y, meters
y( x)
0.2
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
⌠ ⌠
⎮ ⎮
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
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
b
a ⌠
0
2 2 2 4 2 0 2 4
Ixx := 2 ⋅ ⋅ ⎮ y ⋅ b − y dy
b ⌡ 0
x
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).
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
A simple pendulum, which is a mass attached to one end of a light rigid rod, is shown in Figure
8.10.
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
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
1.5
Percentdiff ( A) 1
0.5
0
5 10 15 20 25 30
A
Amplitude A, degrees
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
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.
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
CHAPTER 9
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
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
d
y = f (x) (9.6)
dx
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
dy/dx = 7 x4 - 3 x 2 +9 ,
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
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
The Taylor series of Equation (9.5) expanded to seven terms up to the sixth derivative term is
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
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.
( )
y i := y x i <--Computed Taylor 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
y1 ( x ) := y0 <--One-term solution
7 5 3 27
ytrue ( x ) := ⋅x − x + 9 ⋅x − <--True solution
5 5
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
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
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 .
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
so long as h, which is the difference between x n+1 and x n is small. ( See Figure 9.3)
d
y ( x) = f ( x , y) (9.13)
dx
(
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
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.
3 2
f ( x , y ) := −4 ⋅ x + 18 ⋅ x − 22 ⋅ x + 9
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
f ( x0 , y0) = 9 h := 0.5
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 .
3 2
f ( x , y ) := −4 ⋅ x + 18 ⋅ x − 22 ⋅ x + 9 <--- Prescribed derivative of y(x) at (x,y)
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 − 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
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)
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
10
Y-VALUES
ytrue ( x)
fspline ( x)
0
0 0.5 1 1.5 2 2.5 3 3.5 4
x
X-VALUES
ERROR VERSUS x
0
PERCENTAGE ERROR
50
%error ( x)
100
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
(
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
( )
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
f ( x , y ) := −3 ⋅ x − y h := 0.1
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
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) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.371 y1 := y1c
2
f ( x1 , y1) + f ( x0 , y0)
y1c := y0 + ⋅h y1c = −1.371 y1 := y1c
2
x2 := 0.2
f ( x1 , y1) = 1.071
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
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
0 -1.5 1.5
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)
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
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
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
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
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 .
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.
1
y MP = y 0 + ⋅ K1 (9.18)
2
(
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)
( )
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 ).
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.
(
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
( )
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
(
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
2
f ( x , y ) := −2 ⋅ x − y h := 0.1 x0 := 0 y0 := −1.75
x1 := 0.1
f ( x0 , y0) = 1.75
⎛ 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
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
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)
2
f ( x , y ) := −2 ⋅ x − y h := 0.1 x0 := 0 y0 := −1.75 f ( x0 , y0) = 1.75
x1 := 0.1
⎛ 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
⎛ 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
⎛ 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
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
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.
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
4 2
D ( x , y ) := 7 ⋅ x − 3 ⋅ x + 9
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
4 2
D ( x , y ) := 7 ⋅ x − 3 ⋅ x + 9
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
〈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,
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 := 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 ⎠
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
( )
〈0〉
Z i
X-VALUES
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.
2
y 0 := −1.75 D ( x , y ) := −2 ⋅ x − y Z := rkfixed ( y , 0. , 0.5 , 50 , D)
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
( )
〈0〉
Z i
x- values
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:
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 .
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
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
..................................................
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.
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".
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
x1 = x0 + k3 ; y1 = y0 + m3 ; t1 = t0 + h
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)
t1 := t0 + h t1 = 0.1
k1 := h ⋅ f ( x0 , y0 , t0) k1 = −0.1
m1 := h ⋅ g ( x0 , y0 , t0) m1 = 0.1
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
t2 := t1 + h t2 = 0.2
k1 := h ⋅ f ( x1 , y1 , t1) k1 = −0.128729
m1 := h ⋅ g ( x1 , y1 , t1) m1 = 0.134986
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.
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 ⎠
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
⎛ −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.
The following commands will generate the solution matrix which will contain 3 columns- the "t"
values, the corresponding "x" values and the "y " values.
⎣
〈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
( 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
( 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
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.
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.
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.
A more special form often encountered in vibrations, control systems, and electrical circuit
analysis is
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.
y1 = y0 + k3 ; z1 = z0 + m3 ; x1 = x0 + h
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
dy/dt = z ;
Note that, now, both y and z are functions of the independent variable " t".
dy/dt = f( y,z, t ) = z ,
Version 1 of the R-K procedure will then resort to the following equations to compute y and z at
t1:
y1 = y0 + k3 ; z1 = z0 + m3 ; t1 = t0 + h
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
t1 := t0 + h t1 = 0.5
k1 = 0 m1 = 4.5
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
t2 := t1 + h t2 = 1
k1 = 0.878906 m1 = −0.667969
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.
−3
t5 := 2.5 y5 := 0.938 z5 := 2.889 ⋅ 10 t6 := 3 y6 := 0.94 z6 := −0.019
⎛⎜ 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 ⎠
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
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.
⎣
( )
〈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
( 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
( Z〈2〉 ) i
Velocity ( dy/dt) Values
Rkadapt solution
R/K 4th order
( Y〈2〉 ) i
1
zsplinei
0
( )
〈0〉
Z i
Time values
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.
⎛ 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.
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
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.
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 :
⎛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.
⎛⎜ 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:
⎛⎜ 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,
−3
y 1 = −8.23045 × 10
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
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
Figure 9.18. Beam displacement along span Figure 9.19. Beam slope along span
Consider the electric circuit shown in Figure 9.20 which is subjected to a unit step input
voltage .
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
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.
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)
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
( 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
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
ρ ⋅ 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
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
( Z〈1〉 ) i 50
0
0 500 1000 1500 2000
( )
〈0〉
Z i
Time, seconds
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
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
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
⎛⎜ θ1 ⎞⎟
D ( t , θ ) := ⎜ −G ⎟ <---- Define first and second derivatives
⎜ L ⋅ sin ( θ 0) ⎟
⎝ ⎠
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 )
⎛⎜ θ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
( 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
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
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
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
⎛ 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
Force, Newtons
F( t) 1
0
0 0.5 1 1.5 2
t
Time, seconds
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 .
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.
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
0.01
xi
Time, seconds
vi 0
0.05
0 0.5 1 1.5 2 2.5 3 3.5 4
ti
Time, seconds
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
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 .
⎡⎢ 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
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
t := 0 , 0.1 .. 1.0
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.
xzeromui := Z4i , 1
378 AN INTRODUCTION TO NUMERICAL METHODS USING MATHCAD
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
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.
⎛ 650 ⎞
T := ⎜ ⎟
⎝ initgrad ⎠
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.
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
⎛ 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
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
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
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 .
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
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
( 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
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 .
dy/dx = e x + x e x+ 2 x
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 .
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
(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" .
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
determine (a) y(2.1) and y(2.2) by Euler's Method using an interval size h = 0.1
determine (a) y(1.1) and y(1.2) by Euler's Method using an interval size h = 0.1
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
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.
dy/dx = y + x 2 ,
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.
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.
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.
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.
obtain the solution at x = 0.3, using Version 1 of the Runge-Kutta method, which is based on
Simpson's one-third rule.
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
9.20. Given
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.
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
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.
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
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 .
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
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
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.
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.
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
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
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
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.
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
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
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.
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
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,