انت هنا الان : شبكة جامعة بابل > موقع الكلية > نظام التعليم الالكتروني > مشاهدة المحاضرة

Numerical Analysis Using MATLAB and Spreadsheets

Share |
الكلية كلية تكنولوجيا المعلومات     القسم قسم البرامجيات     المرحلة 2
أستاذ المادة حيدر كاظم زغير الجبوري       01/11/2014 08:28:32
orchard publications
www.orchardpublications.com
numerical
analysis
using matlab® and spreadsheets
steven t. karris
second edition
numerical analysis using matlab and spreadsheets, second edition i
orchard publications
table of contents
chapter 1
introduction to matlab
command window....................................................................................................................... 1-1
roots of polynomials.................................................................................................................... 1-3
polynomial construction from known roots .............................................................................. 1-4
evaluation of a polynomial at specified values ........................................................................... 1-5
rational polynomials .................................................................................................................... 1-7
using matlab to make plots.................................................................................................... 1-9
subplots...................................................................................................................................... 1-18
multiplication, division and exponentiation............................................................................. 1-18
script and function files............................................................................................................ 1-25
display formats .......................................................................................................................... 1-29
summary .................................................................................................................................... 1-30
exercises..................................................................................................................................... 1-35
solutions to exercises ................................................................................................................. 1-36
chapter 2
root approximations
newton’s method for root approximation ................................................................................. 2-1
approximations with spreadsheets .............................................................................................. 2-7
the bisection method for root approximation........................................................................ 2-19
summary .................................................................................................................................... 2-27
exercises..................................................................................................................................... 2-28
solutions to exercises ................................................................................................................. 2-29
chapter 3
sinusoids and phasors
alternating voltages and currents .............................................................................................. 3-1
characteristics of sinusoids .......................................................................................................... 3-2
inverse trigonometric functions ............................................................................................... 3-10
phasors ....................................................................................................................................... 3-10
addition and subtraction of phasors ......................................................................................... 3-11
multiplication of phasors............................................................................................................ 3-12
division of phasors ..................................................................................................................... 3-12
ii numerical analysis using matlab and spreadsheets, second edition
orchard publications
exponential and polar forms of phasors ....................................................................................3-13
summary.....................................................................................................................................3-18
exercises .....................................................................................................................................3-21
solutions to exercises..................................................................................................................3-22
chapter 4
matrices and determinants
matrix definition ......................................................................................................................... 4-1
matrix operations....................................................................................................................... 4-2
special forms of matrices ............................................................................................................ 4-5
determinants............................................................................................................................... 4-9
minors and cofactors................................................................................................................. 4-12
cramer’s rule............................................................................................................................ 4-16
gaussian elimination method ................................................................................................... 4-18
the adjoint of a matrix............................................................................................................. 4-19
singular and non-singular matrices ......................................................................................... 4-20
the inverse of a matrix.............................................................................................................. 4-21
solution of simultaneous equations with matrices................................................................... 4-23
summary.................................................................................................................................... 4-29
exercises .................................................................................................................................... 4-33
solutions to exercises................................................................................................................. 4-35
chapter 5
differential equations, state variables, and state equations
simple differential equations .......................................................................................................5-1
classification ................................................................................................................................5-2
solutions of ordinary differential equations (ode)...................................................................5-5
solution of the homogeneous ode .............................................................................................5-8
using the method of undetermined coefficients for the forced response...............................5-10
using the method of variation of parameters for the forced response....................................5-19
expressing differential equations in state equation form ........................................................5-23
solution of single state equations..............................................................................................5-27
the state transition matrix.......................................................................................................5-28
computation of the state transition matrix..............................................................................5-30
eigenvectors ...............................................................................................................................5-37
summary.....................................................................................................................................5-41
exercises .....................................................................................................................................5-46
solutions to exercises..................................................................................................................5-47
numerical analysis using matlab and spreadsheets, second edition iii
orchard publications
chapter 6
fourier, taylor, and maclaurin series
wave analysis .............................................................................................................................. 6-1
evaluation of the coefficients ...................................................................................................... 6-2
symmetry ..................................................................................................................................... 6-7
waveforms in trigonometric form of fourier series................................................................. 6-12
alternate forms of the trigonometric fourier series ................................................................ 6-25
the exponential form of the fourier series .............................................................................. 6-28
line spectra ............................................................................................................................... 6-33
numerical evaluation of fourier coefficients............................................................................ 6-36
power series expansion of functions ......................................................................................... 6-37
taylor and maclaurin series....................................................................................................... 6-40
summary .................................................................................................................................... 6-47
exercises..................................................................................................................................... 6-50
solutions to exercises ................................................................................................................. 6-52
chapter 7
finite differences and interpolation
divided differences ...................................................................................................................... 7-1
factorial polynomials.................................................................................................................... 7-6
antidifferences........................................................................................................................... 7-11
newton’s divided difference interpolation method ................................................................. 7-15
lagrange’s interpolation method ............................................................................................... 7-18
gregory-newton forward interpolation method....................................................................... 7-19
gregory-newton backward interpolation method .................................................................... 7-20
interpolation with matlab..................................................................................................... 7-23
summary .................................................................................................................................... 7-37
exercises..................................................................................................................................... 7-42
solutions to exercises ................................................................................................................. 7-43
chapter 8
linear and parabolic regression
curve fitting................................................................................................................................ 8-1
linear regression......................................................................................................................... 8-2
parabolic regression ..................................................................................................................... 8-7
regression with power series approximations .......................................................................... 8-14
summary .................................................................................................................................... 8-24
iv numerical analysis using matlab and spreadsheets, second edition
orchard publications
exercises .................................................................................................................................... 8-26
solutions to exercises................................................................................................................. 8-28
chapter 9
solution of differential equations by numerical methods
taylor series method................................................................................................................... 9-1
runge-kutta method................................................................................................................... 9-5
adams’ method......................................................................................................................... 9-13
milne’s method .......................................................................................................................... 9-16
summary.................................................................................................................................... 9-17
exercises .................................................................................................................................... 9-20
solutions to exercises................................................................................................................. 9-21
chapter 10
integration by numerical methods
the trapezoidal rule................................................................................................................. 10-1
simpson’s rule ........................................................................................................................... 10-6
summary.................................................................................................................................. 10-13
exercises .................................................................................................................................. 10-15
solution to exercises ................................................................................................................ 10-16
chapter 11
difference equations
definition, solutions, and applications..................................................................................... 11-1
fibonacci numbers .................................................................................................................... 11-7
summary.................................................................................................................................. 11-10
exercises .................................................................................................................................. 11-13
solutions to exercises............................................................................................................... 11-14
chapter 12
partial fraction expansion
partial fraction expansion.........................................................................................................12-1
alternate method of partial fraction expansion ....................................................................12-13
summary..................................................................................................................................12-18
exercises ..................................................................................................................................12-21
solutions to exercises...............................................................................................................12-22
numerical analysis using matlab and spreadsheets, second edition v
orchard publications
chapter 13
the gamma and beta functions and distributions
the gamma function ................................................................................................................ 13-1
the gamma distribution ......................................................................................................... 13-15
the beta function.................................................................................................................... 13-17
the beta distribution............................................................................................................... 13-20
summary .................................................................................................................................. 13-21
exercises................................................................................................................................... 13-24
solutions to exercises ............................................................................................................... 13-25
chapter 14
orthogonal functions and matrix factorizations
orthogonal functions ................................................................................................................14-1
orthogonal trajectories .............................................................................................................14-2
orthogonal vectors....................................................................................................................14-4
the gram-schmidt orthogonalization procedure .....................................................................14-7
the lu factorization.................................................................................................................14-9
the cholesky factorization .....................................................................................................14-15
the qr factorization...............................................................................................................14-17
singular value decomposition ................................................................................................14-20
summary.................................................................................................................................14-21
exercises .................................................................................................................................14-23
solutions to exercises ..............................................................................................................14-25
chapter 15
bessel, legendre, and chebyshev functions
the bessel function ................................................................................................................... 15-1
legendre functions .................................................................................................................. 15-10
laguerre polynomials................................................................................................................ 15-20
chebyshev polynomials ............................................................................................................ 15-21
summary .................................................................................................................................. 15-26
exercises................................................................................................................................... 15-32
solutions to exercises ............................................................................................................... 15-33
vi numerical analysis using matlab and spreadsheets, second edition
orchard publications
chapter 16
optimization methods
linear programming................................................................................................................... 16-1
dynamic programming............................................................................................................... 16-4
network analysis ..................................................................................................................... 16-14
summary.................................................................................................................................. 16-19
exercises .................................................................................................................................. 16-20
solutions to exercises............................................................................................................... 16-22
numerical analysis using matlab and spreadsheets, second edition 1-1
orchard publications
chapter 1
introduction to matlab
his chapter is an introduction of the basic matlab commands and functions, procedures
for naming and saving the user generated files, comment lines, access to matlab’s editor/
debugger, finding the roots of a polynomial, and making plots. several examples are provided
with detailed explanations.
1.1 command window
to distinguish the screen displays from the user commands, important terms and matlab functions,
we will use the following conventions:
click: click the left button of the mouse
courier font: screen displays
helvetica font: user inputs at matlab’s command window prompt edu>>*
helvetica bold: matlab functions
times bold italic: important terms and facts, notes, and file names
when we first start matlab, we see the toolbar on top of the command screen and the prompt
edu>>. this prompt is displayed also after execution of a command matlab now waits for a
new command from the user. we can use the editor/debugger to write our program, save it, and
return to the command screen to execute the program as explained below.
to use the editor/debugger:
1. from the file menu on the toolbar, we choose new and click on m-file. this takes us to the
editor window where we can type our code (list of statements) for a new file, or open a previously
saved file. we must save our program with a file name which starts with a letter. important!
matlab is case sensitive, that is, it distinguishes between upper- and lower-case letters.
thus, t and t are two different characters in matlab language. the files that we create are
saved with the file name we use and the extension .m for example, myfile01.m. it is a good
practice to save the code in a file name that is descriptive of our code content. for instance, if
the code performs some matrix operations, we ought to name and save that file as
matrices01.m or any other similar name. we should also use a separate disk to backup our
files.
* edu>> is the matlab prompt in the student version.
t
chapter 1 introduction to matlab
1-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
2. once the code is written and saved as an m-file, we may exit the editor/debugger window by
clicking on exit editor/debugger of the file menu, and matlab returns to the command window.
3. to execute a program, we type the file name without the .m extension at the edu>> prompt
then, we press and observe the execution and the values obtained from it. if we have
saved our file in drive a or any other drive, we must make sure that it is added it to the desired
directory in matlab’s search path. the matlab user’s guide provides more information
on this topic.
henceforth, it will be understood that each input command is typed after the edu>> prompt and
followed by the key.
the command help matlab iofun will display input/output information. to get help with other
matlab topics, we can type help followed by any topic from the displayed menu. for example, to
get information on graphics, we type help matlab graphics. we can also get help from the help
pull-down menu. the matlab user’s guide contains numerous help topics.
to appreciate matlab’s capabilities, we type demo and we see the matlab demos menu. we
can do this periodically to become familiar with them. whenever we want to return to the command
window, we click on the close button.
when we are done and want to leave matlab, we type quit or exit. but if we want to clear all
previous values, variables, and equations without exiting, we should use the clear command. this
command erases everything it is like exiting matlab and starting it again. the clc command
clears the screen but matlab still remembers all values, variables and equations which we have
already used. in other words, if we want matlab to retain all previously entered commands, but
leave only the edu>> prompt on the upper left of the screen, we can use the clc command.
all text after the % (percent) symbol is interpreted by matlab as a comment line and thus it is
ignored during the execution of a program. a comment can be typed on the same line as the function
or command or as a separate line. for instance,
conv(p,q) % performs multiplication of polynomials p and q
% the next statement performs partial fraction expansion of p(x) / q(x)
are both correct.
one of the most powerful features of matlab is the ability to do computations involving complex
numbers. we can use either , or to denote the imaginary part of a complex number, such as
or . for example, the statement
z=3-4j
displays
z =
3.0000 - 4.0000i
i j
3 – 4i 3 – 4j
numerical analysis using matlab and spreadsheets, second edition 1-3
orchard publications
roots of polynomials
in the example above, a multiplication (*) sign between and was not necessary because the
complex number consists of numerical constants. however, if the imaginary part is a function or
variable such as , we must use the multiplication sign, that is, we must type cos(x)*j or
j*cos(x).
1.2 roots of polynomials
in matlab, a polynomial is expressed as a row vector of the form . the elements
of this vector are the coefficients of the polynomial in descending order. we must
include terms whose coefficients are zero.
we can find the roots of any polynomial with the roots(p) function where p is a row vector containing
the polynomial coefficients in descending order.
example 1.1
find the roots of the polynomial
(1.1)
solution:
the roots are found with the following two statements. we have denoted the polynomial as p1,
and the roots as roots_ p1.
p1=[1 ?10 35 ?50 24] % specify the coefficients of p1(x)
p1 =
1 -10 35 -50 24
roots_ p1=roots(p1) % find the roots of p1(x)
roots_p1 =
4.0000
3.0000
2.0000
1.0000
we observe that matlab displays the polynomial coefficients as a row vector, and the roots as a
column vector.
example 1.2
find the roots of the polynomial
(1.2)
4 j
cos(x)
[an an – 1 a2 a1 a0]
ai
p1(x) = x4 – 10x3 + 35x2 – 50x + 24
p2(x) = x5 – 7x4 + 16x2 + 25x + 52
chapter 1 introduction to matlab
1-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
there is no cube term therefore, we must enter zero as its coefficient. the roots are found with the
statements below where we have defined the polynomial as p2, and the roots of this polynomial as
roots_ p2. the result indicates that this polynomial has three real roots, and two complex roots. of
course, complex roots always occur in complex conjugate* pairs.
p2=[1 ?7 0 16 25 52]
p2 =
1 -7 0 16 25 52
roots_ p2=roots(p2)
roots_ p2 =
6.5014
2.7428
-1.5711
-0.3366 + 1.3202i
-0.3366 - 1.3202i
1.3 polynomial construction from known roots
we can compute the coefficients of a polynomial from a given set of roots with the poly(r) function
where r is a row vector containing the roots.
example 1.3
it is known that the roots of a polynomial are . compute the coefficients of this polynomial.
solution:
we first define a row vector, say , with the given roots as elements of this vector then, we find
the coefficients with the poly(r) function as shown below.
r3=[1 2 3 4] % specify the roots of the polynomial
r3 =
1 2 3 4
poly_r3=poly(r3) % find the polynomial coefficients
poly_r3 =
1 -10 35 -50 24
* by definition, the conjugate of a complex number a = a + jb is a? = a – jb
1, 2, 3, and 4
r3
numerical analysis using matlab and spreadsheets, second edition 1-5
orchard publications
evaluation of a polynomial at specified values
we observe that these are the coefficients of the polynomial of example 1.1.
example 1.4
it is known that the roots of a polynomial are . find the coefficients
of this polynomial.
solution:
we form a row vector, say , with the given roots, and we find the polynomial coefficients with
the poly(r) function as shown below.
r4=[ ?1 ?2 ?3 4+5j 4?5j ]
r4 =
columns 1 through 4
-1.0000 -2.0000 -3.0000 -4.0000 + 5.0000i
column 5
-4.0000 - 5.0000i
poly_r4=poly(r4)
poly_r4 =
1 14 100 340 499 246
therefore, the polynomial is
(1.3)
1.4 evaluation of a polynomial at specified values
the polyval(p,x) function evaluates a polynomial at some specified value of the independent
variable .
example 1.5
evaluate the polynomial
(1.4)
at .
solution:
p5=[1 ?3 0 5 ?4 3 2] % these are the coefficients
% the semicolon ( ) after the right bracket suppresses the display of the row vector
% that contains the coefficients of p5.
%
val_minus3=polyval(p5, ?3)% evaluate p5 at x=?3 no semicolon is used here
% because we want the answer to be displayed
p1(x)
–1, –2, –3, 4 + j5, and 4 – j5
r4
p4(x) = x5 + 14x4 + 100x3 + 340x2 + 499x + 246
p(x)
x
p5(x) = x6 – 3x5 + 5x3 – 4x2 + 3x + 2
x = –3
chapter 1 introduction to matlab
1-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
val_minus3 =
1280
other matlab functions used with polynomials are the following:
conv(a,b) ? multiplies two polynomials a and b
[q,r]=deconv(c,d) ?divides polynomial c by polynomial d and displays the quotient q and remainder
r.
polyder(p) ? produces the coefficients of the derivative of a polynomial p.
example 1.6
let
(1.5)
compute the product with the conv(a,b) function.
solution:
p1=[1 ?3 0 5 7 9]
p2=[2 0 ?8 0 4 10 12]
p1p2=conv(p1,p2)
p1p2 =
2 -6 -8 34 18 -24 -74 -88 78 166 174 108
therefore,
we can write matlab statements in one line if we separate them by commas or semicolons.
commas will display the results whereas semicolons will suppress the display.
example 1.7
let
(1.6)
compute the quotient using the deconv(p,q) function.
solution:
p3=[1 0 ?3 0 5 7 9] p4=[2 ?8 0 0 4 10 12] [q,r]=deconv(p3,p4)
p1 = x5 – 3x4 + 5x2 + 7x + 9
p2 = 2x6 – 8x4 + 4x2 + 10x + 12
p1 ? p2
p1 ? p2 = 2x11 – 6x10 – 8x9 + 34x8 + 18x7 – 24x6
–74x5–88x4 + 78x3 + 166x2 + 174x + 108
p3 = x7 – 3x5 + 5x3 + 7x + 9
p4 = 2x6 – 8x5 + 4x2 + 10x + 12
p3 ? p4
numerical analysis using matlab and spreadsheets, second edition 1-7
orchard publications
rational polynomials
q =
0.5000
r =
0 4 -3 0 3 2 3
therefore, the quotient and remainder are
example 1.8
let
(1.7)
compute the derivative using the polyder(p) function.
solution:
p5=[2 0 ?8 0 4 10 12]
der_p5=polyder(p5)
der_p5 =
12 0 -32 0 8 10
therefore,
1.5 rational polynomials
rational polynomials are those which can be expressed in ratio form, that is, as
(1.8)
where some of the terms in the numerator and/or denominator may be zero. we can find the roots
of the numerator and denominator with the roots(p) function as before.
example 1.9
let
(1.9)
express the numerator and denominator in factored form, using the roots(p) function.
solution:
num=[1 ?3 0 5 7 9] den=[2 0 ?8 0 4 10 12] % do not display num and den coefficients
q(x) r(x)
q(x) = 0.5 r(x) = 4x5 – 3x4 + 3x2 + 2x + 3
p5 = 2x6 – 8x4 + 4x2 + 10x + 12
dp5 ? dx
dp5 ? dx = 12x5 – 32x3 + 4x2 + 8x + 10
r(x) num(x)
den(x)
-
bnxn bn – 1xn – 1 bn – 2xn – 2 + + +… + b1x + b0
amxm am – 1xm – 1 am – 2xm – 2 + + +… + a1x + a0
= = -
r(x)
pnum
pden
- x5 – 3x4 5x+ 2 + 7x + 9
2x6 – 8x4 + 4x2 + 10x + 12
= = -
chapter 1 introduction to matlab
1-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
roots_num=roots(num), roots_den=roots(den) % display num and den roots
roots_num =
2.4186 + 1.0712i 2.4186 - 1.0712i -1.1633
-0.3370 + 0.9961i -0.3370 - 0.9961i
roots_den =
1.6760 + 0.4922i 1.6760 - 0.4922i -1.9304
-0.2108 + 0.9870i -0.2108 - 0.9870i -1.0000
as expected, the complex roots occur in complex conjugate pairs.
for the numerator, we have the factored form
and for the denominator, we have
we can also express the numerator and denominator of this rational function as a combination of
linear and quadratic factors. we recall that in a quadratic equation of the form
whose roots are and , the negative sum of the roots is equal to the coefficient of the term,
that is, , while the product of the roots is equal to the constant term , that is,
. accordingly, we form the coefficient by addition of the complex conjugate roots and
this is done by inspection then we multiply the complex conjugate roots to obtain the constant
term using matlab as indicated below.
(2.4186+1.0712i)*(2.4186 ?1.0712i) % form the product of the 1st set of complex conjugates
ans = 6.9971
(?0.3370+0.9961i)*(?0.3370?0.9961i) % form the product of the 2nd set of complex conjugates
ans = 1.1058
(1.6760+0.4922i)*(1.6760?0.4922i)
ans = 3.0512
(?0.2108+0.9870i)*(?0.2108?0.9870i)
ans = 1.0186
pnum = (x–2.4186 – j1.0712) ? (x–2.4186 + j1.0712) ? (x + 1.1633) ?
(x + 0.3370 – j0.9961) ? (x + 0.3370 + j0.9961)
pden = (x–1.6760 – j0.4922) ? (x–1.6760 + j0.4922) ? (x + 1.9304) ?
(x + 0.2108–j0.9870) ? (x + 0.2108 + j0.9870) ? (x + 1.0000)
x2 + bx + c = 0
x1 x2 b x
–(x1 + x2) = b c
x1 ? x2 = c b
c
numerical analysis using matlab and spreadsheets, second edition 1-9
orchard publications
using matlab to make plots
1.6 using matlab to make plots
quite often, we want to plot a set of ordered pairs. this is a very easy task with the matlab
plot(x,y) command which plots versus . here, is the horizontal axis (abscissa) and is the
vertical axis (ordinate).
example 1.10
consider the electric circuit of figure 1.1, where the radian frequency (radians/second) of the
applied voltage was varied from to in steps of radians/second, while the amplitude
was held constant. the ammeter readings were then recorded for each frequency. the magnitude
of the impedance was computed as and the data were tabulated in table 1.1.
figure 1.1. electric circuit for example 1.10
plot the magnitude of the impedance, that is, versus radian frequency .
table 1.1 table for example 1.10
(rads/s) ohms (rads/s) ohms
300 39.339 1700 90.603
400 52.589 1800 81.088
500 71.184 1900 73.588
600 97.665 2000 67.513
700 140.437 2100 62.481
800 222.182 2200 58.240
900 436.056 2300 54.611
1000 1014.938 2400 51.428
1100 469.83 2500 48.717
1200 266.032 2600 46.286
1300 187.052 2700 44.122
1400 145.751 2800 42.182
1500 120.353 2900 40.432
1600 103.111 3000 38.845
y x x y
?
300 3000 100
z z = v ? a
??
?? ??
??
a
??v
r
l
c
? z ? z
z ?
chapter 1 introduction to matlab
1-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
we cannot type (omega) in the matlab command window, so we will use the english letter w
instead.
if a statement, or a row vector is too long to fit in one line, it can be continued to the next line by
typing three or more periods, then pressing to start a new line, and continue to enter
data. this is illustrated below for the data of w and z. also, as mentioned before, we use the semicolon
( ) to suppress the display of numbers which we do not care to see on the screen.
the data are entered as follows:
w=[300 400 500 600 700 800 900 1000 1100 1200 1300 1400.... % use 4 periods to continue
1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500....
2600 2700 2800 2900 3000] % use semicolon to suppress display of these numbers
%
z=[39.339 52.789 71.104 97.665 140.437 222.182 436.056....
1014.938 469.830 266.032 187.052 145.751 120.353 103.111....
90.603 81.088 73.588 67.513 62.481 58.240 54.611 51.468....
48.717 46.286 44.122 42.182 40.432 38.845]
of course, if we want to see the values of w or z or both, we simply type w or z, and we press
.
to plot z ( ) versus w ( ), we use the plot(x,y) command. for this example, we use
plot(w,z). when this command is executed, matlab displays the plot on matlab’s graph
screen. this plot is shown in figure 1.2.
figure 1.2. plot of impedance versus frequency for example 1.10
this plot is referred to as the amplitude frequency response of the circuit.
?
y – axis x – axis
z ?
numerical analysis using matlab and spreadsheets, second edition 1-11
orchard publications
using matlab to make plots
to return to the command window, we press any key, or from the window pull-down menu, we
select matlab command window. to see the graph again, we click on the window pull-down
menu, and we select figure.
we can make the above, or any plot, more presentable with the following commands:
grid on: this command adds grid lines to the plot. the grid off command removes the grid. the
command grid toggles them, that is, changes from off to on or vice versa. the default* is off.
box off: this command removes the box (the solid lines which enclose the plot), and box on
restores the box. the command box toggles them. the default is on.
title(‘string’): this command adds a line of the text string (label) at the top of the plot.
xlabel(‘string’) and ylabel(‘string’) are used to label the - and -axis respectively.
the amplitude frequency response is usually represented with the -axis in a logarithmic scale.
we can use the semilogx(x,y) command that is similar to the plot(x,y) command, except that
the -axis is represented as a log scale, and the -axis as a linear scale. likewise, the semilogy(
x,y) command is similar to the plot(x,y) command, except that the -axis is represented as a
log scale, and the -axis as a linear scale. the loglog(x,y) command uses logarithmic scales for
both axes.
throughout this text, it will be understood that log is the common (base 10) logarithm, and ln is
the natural (base e) logarithm. we must remember, however, the function log(x) in matlab is
the natural logarithm, whereas the common logarithm is expressed as log10(x). likewise, the logarithm
to the base 2 is expressed as log2(x).
let us now redraw the plot with the above options, by adding the following statements:
semilogx(w,z) grid % replaces the plot(w,z) command
title( magnitude of impedance vs. radian frequency )
xlabel( w in rads/sec ) ylabel( |z| in ohms )
after execution of these commands, our plot is as shown in figure 1.3.
if the -axis represents power, voltage, or current, the -axis of the frequency response is more
often shown in a logarithmic scale, and the -axis in db (decibels) scale. a review of the decibel
unit follows.
the ratio of any two values of the same quantity (power, voltage, or current) can be expressed in
decibels (db). thus, we say that an amplifier has power gain, or a transmission line has a
power loss of (or gain ). if the gain (or loss) is the output is equal to the input.
* default is a particular value for a variable or condition that is assigned automatically by an operating system, and remains
in effect unless canceled or overridden by the operator.
x y
x
x y
y
x
y x
y
10 db
7 db –7 db 0 db
chapter 1 introduction to matlab
1-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 1.3. modified frequency response plot of figure 1.2.
by definition,
(1.10)
therefore,
represents a power ratio of
represents a power ratio of
it is very useful to remember that:
represents a power ratio of
represents a power ratio of
represents a power ratio of
also,
represents a power ratio of approximately
represents a power ratio of approximately
represents a power ratio of approximately
from these, we can estimate other values. for instance,
and since and then,
db 10
pout
pin
= log
10 db 10
10n db 10n
20 db 100
30 db 1, 000
60 db 1, 000, 000
1 db 1.25
3 db 2
7 db 5
4 db = 3 db + 1 db 3 db ? power ratio of 2 1 db ? power ratio of 1.25
4 db ? ratio of (2 × 1.25) = ratio of 2.5
numerical analysis using matlab and spreadsheets, second edition 1-13
orchard publications
using matlab to make plots
likewise, and this is equivalent to a power ratio of approximately
using the relations
and
if we let , the db values for voltage and current ratios become
(1.11)
and
(1.12)
to display the voltage in a db scale on the , we add the relation db=20*log10(v), and we
replace the semilogx(w,z) command with semilogx(w,db).
the command gtext(‘string’) switches to the current figure window, and displays a cross-hair
which can be moved around with the mouse. for instance, we can use the command
gtext(‘impedance |z| versus frequency’), and this will place a cross-hair in the figure window.
then, using the mouse, we can move the cross-hair to the position where we want our label to
begin, and we press .
the command text(x,y,’string’) is similar to gtext(‘string’). it places a label on a plot in some
specific location specified by x and y, and string is the label which we want to place at that location.
we will illustrate its use with the following example which plots a 3-phase sinusoidal waveform.
the first line of the code below has the form
linspace(first_value, last_value, number_of_values)
this command specifies the number of data points but not the increments between data points.
an alternate command uses the colon notation and has the format
x=first: increment: last
this format specifies the increments between points but not the number of data points.
the code for the 3-phase plot is as follows:
x=linspace(0, 2*pi, 60) % pi is a built-in function in matlab
% we could have used x=0:0.02*pi:2*pi or x = (0: 0.02: 2)*pi instead
y=sin(x) u=sin(x+2*pi/3) v=sin(x+4*pi/3)
27 db = 20 db + 7 db
100 × 5 = 500
y = logx2 = 2logx
p v2
z
= = i 2z
z = 1
dbv 10 vout
vin
-
2
log 20 vout
vin
= = log -
dbi 10 iout
iin

2
log 20 iout
iin
= = log
v y – axis
chapter 1 introduction to matlab
1-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
plot(x,y,x,u,x,v) % the x-axis must be specified for each function
grid on, box on, % turn grid and axes box on
text(0.75, 0.65, sin(x) ) text(2.85, 0.65, sin(x+2*pi/3) ) text(4.95, 0.65, sin(x+4*pi/3) )
these three waveforms are shown on the same plot of figure 1.4.
figure 1.4. three-phase waveforms
in our previous examples, we did not specify line styles, markers, and colors for our plots. however,
matlab allows us to specify various line types, plot symbols, and colors. these, or a combination
of these, can be added with the plot(x,y,s) command, where s is a character string containing one
or more characters shown on the three columns of table 1.2.
matlab has no default color it starts with blue and cycles through the first seven colors listed in
table 1.2 for each additional line in the plot. also, there is no default marker no markers are
drawn unless they are selected. the default line is the solid line.
for example, plot(x,y, m*: ) plots a magenta dotted line with a star at each data point, and
plot(x,y, rs ) plots a red square at each data point, but does not draw any line because no line was
selected. if we want to connect the data points with a solid line, we must type plot(x,y, rs? ). for
additional information we can type help plot in matlab’s command screen.
the plots which we have discussed thus far are two-dimensional, that is, they are drawn on two
axes. matlab has also a three-dimensional (three-axes) capability and this is discussed next.
0 1 2 3 4 5 6 7
-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1
sin(x) sin(x+2*pi/3) sin(x+4*pi/3)
numerical analysis using matlab and spreadsheets, second edition 1-15
orchard publications
using matlab to make plots
the plot3(x,y,z) command plots a line in 3-space through the points whose coordinates are the
elements of , , and , where , , and are three vectors of the same length.
the general format is plot3(x1,y1,z1,s1,x2,y2,z2,s2,x3,y3,z3,s3,...) where xn, yn and zn are vectors
or matrices, and sn are strings specifying color, marker symbol, or line style. these strings are the
same as those of the two-dimensional plots.
example 1.11
plot the function
(1.13)
solution:
we arbitrarily choose the interval (length) shown on the code below.
x= ?10: 0.5: 10 % length of vector x
y= x % length of vector y must be same as x
z= ?2.*x.^3+x+3.*y.^2?1 % vector z is function of both x and y*
plot3(x,y,z) grid
the three-dimensional plot is shown in figure 1.5.
table 1.2 styles, colors, and markets used in matlab
symbol color symbol marker symbol line style
b blue . point ? solid line
g green o circle : dotted line
r red x x-mark ?. dash-dot line
c cyan + plus ?? dashed line
m magenta * star
y yellow s square
k black d diamond
w white ? triangle down
? triangle up
< triangle left
> triangle right
p pentagram
h hexagram
* this statement uses the so called dot multiplication, dot division, and dot exponentiation where these operations are preceded
by a dot (period). these operations will be explained in section 1.8.
x y z x y z
z 2x3 – x 3y2
= + + – 1
chapter 1 introduction to matlab
1-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 1.5. three dimensional plot for example 1.11
the command plot3(x,y,z, bd? ) will display the plot in blue diamonds, connected with a solid
line.
in a three-dimensional plot, we can use the zlabel(‘string’) command in addition to the xlabel(‘
string’) and ylabel(‘string’).
in a two-dimensional plot, we can set the limits of the - and -axes with the axis([xmin xmax
ymin ymax]) command. likewise, in a three-dimensional plot we can set the limits of all three
axes with the axis([xmin xmax ymin ymax zmin zmax]) command. it must be placed after the
plot(x,y) or plot3(x,y,z) commands, or on the same line without first executing the plot command.
this must be done for each plot. the three-dimensional text(x,y,z,’string’) command will
place string beginning at the co-ordinate ( ) on the plot.
for three-dimensional plots, grid on and box off are the default states.
the mesh(x,y,z) command displays a three-dimensional plot. another command, contour(z,n),
draws contour lines for n levels. we can also use the mesh(x,y,z) command with two vector arguments.
these must be defined as and where . in
this case, the vertices of the mesh lines are the triples . we observe that x corresponds
to the columns of , and y corresponds to the rows of .
to produce a mesh plot of a function of two variables, say , we must first generate the
and matrices which consist of repeated rows and columns over the range of the variables and
. we can generate the matrices and with the [x,y]=meshgrid(x,y) function which creates
the matrix whose rows are copies of the vector x, and the matrix whose columns are copies of
the vector y.
x y
x, y, z
length(x) = n length(y) = m [m, n] = size(z)
{x(j), y(i), z(i, j)}
z z
z = f(x, y) x
y x
y x y
x y
numerical analysis using matlab and spreadsheets, second edition 1-17
orchard publications
using matlab to make plots
example 1.12
the volume of a right circular cone of radius and height is given by
(1.14)
plot the volume of the cone as and vary on the intervals and meters.
solution:
the volume of the cone is a function of both the radius and the height , that is,
the three-dimensional plot is created with the following matlab code where, as in the previous
example, in the second line we have used the dot multiplication, division, and exponentiation. as
mentioned in the footnote of the previous page, this topic will be explained in section 1.8.
[r,h]=meshgrid(0: 4, 0: 6) % creates r and h matrices from vectors r and h
v=(pi .* r .^ 2 .* h) ./ 3 mesh(r, h, v)
xlabel( x-axis, radius r (meters) ) ylabel( y-axis, altitude h (meters) )
zlabel( z-axis, volume (cubic meters) ) title( volume of right circular cone ) box on
the three-dimensional plot of figure 1.6, shows how the volume of the cone increases as the
radius and height are increased.
figure 1.6. volume of a right circular cone.
this, and the plot of figure 1.5, are rudimentary matlab can generate very sophisticated and
impressive three-dimensional plots. the matlab user’s manual contains more examples.
v r h
v 13
= ?r2h
r h 0 ? r ? 4 0 ? h ? 6
r h v = f (r, h)
chapter 1 introduction to matlab
1-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
1.7 subplots
matlab can display up to four windows of different plots on the figure window using the command
subplot(m,n,p). this command divides the window into an matrix of plotting areas
and chooses the area to be active. no spaces or commas are required between the three integers
, , and . the possible combinations are shown in figure 1.7.
we will illustrate the use of the subplot(m,n,p) command following the discussion on multiplication,
division and exponentiation that follows.
figure 1.7. possible subpot arrangements in matlab
1.8 multiplication, division and exponentiation
matlab recognizes two types of multiplication, division, and exponentiation. these are the
matrix multiplication, division, and exponentiation, and the element-by-element multiplication,
division, and exponentiation. they are explained in the following paragraphs.
in section 1.2, the arrays , such a those that contained the coefficients of polynomials,
consisted of one row and multiple columns, and thus are called row vectors. if an array has one column
and multiple rows, it is called a column vector. we recall that the elements of a row vector are
separated by spaces. to distinguish between row and column vectors, the elements of a column
vector must be separated by semicolons. an easier way to construct a column vector, is to write it
first as a row vector, and then transpose it into a column vector. matlab uses the single quotation
character (?) to transpose a vector. thus, a column vector can be written either as
b=[?1 3 6 11]
or as
b=[?1 3 6 11]
matlab produces the same display with either format as shown below.
b=[?1 3 6 11]
m × n
pth
m n p
111
full screen default
211
212
221 222
223 224
121 122
221 222
212
211
223 224
221
223
122 121
222
224
[a b c …]
numerical analysis using matlab and spreadsheets, second edition 1-19
orchard publications
multiplication, division and exponentiation
b =
-1
3
6
11
b=[?1 3 6 11]
b =
-1
3
6
11
we will now define matrix multiplication and element-by-element multiplication.
1. matrix multiplication (multiplication of row by column vectors)
let and be two vectors. we observe that
is defined as a row vector whereas is defined as a column vector, as indicated by the transpose
operator (?). here, multiplication of the row vector by the column vector , is performed
with the matrix multiplication operator (*). then,
(1.15)
for example, if and , the matrix multiplication
produces the single value 68, that is,
this can be verified with matlab as follows.
a=[1 2 3 4 5] b=[ ?2 6 ?3 8 7] a*b
ans =
68
now, let us suppose that both and are row vectors, and we attempt to perform a row-byrow
multiplication with the following matlab statements.
a=[ 1 2 3 4 5] b=[?2 6 ?3 8 7] a*b
when these statements are executed, matlab displays the following message:
??? error using ==> *
inner matrix dimensions must agree.
a = [a1 a2 a3 … an] b = [b1 b2 b3 … bn]
a b
a b
a*b = [a1b1 + a2b2 + a3b3 + … + anbn] = single value
a = [1 2 3 4 5] b = [–2 6 –3 8 7]
a*b
a?b = 1 × (–2) + 2 × 6 + 3 × (–3) + 4 × 8 + 5 × 7 = 68
a b
chapter 1 introduction to matlab
1-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
here, because we have used the matrix multiplication operator (*) in a*b, matlab expects
vector to be a column vector, not a row vector as it was specified. matlab recognizes that
is a row vector, and warns us that we cannot perform this multiplication using the matrix
multiplication operator (*). accordingly, we must perform this type of multiplication with a different
operator. this operator is defined below.
2. element-by-element multiplication (multiplication of a row vector by another row vector)
let and be two row vectors. here, multiplication
of the row vector by the row vector is performed with the dot multiplication
operator (.*). there is no space between the dot and the multiplication symbol. thus,
(1.16)
and this product is another row vector with the same number of elements, as the elements of
and .
as an example, let and . dot multiplication of
these two row vectors produce the following result.
check with matlab:
c=[ 1 2 3 4 5] % vectors c and d must have
d=[?2 6 ?3 8 7] % same number of elements
c.*d % we observe that this is a dot multiplication
ans =
-2 12 -9 32 35
similarly, the division (/) and exponentiation (^) operators, are used for matrix division and
exponentiation, whereas dot division (./) and dot exponentiation (.^) are used for element-byelement
division and exponentiation.
we must remember that no space is allowed between the dot (.) and the multiplication, division,
and exponentiation operators.
note: a dot (.) is never required with the plus (+) and minus (?) operators.
example 1.13
write the matlab code that produces a simple plot for the waveform defined as
(1.17)
in the seconds interval.
b
b
c = [c1 c2 c3 … cn] d = [d1 d2 d3 … dn]
c d
c.?d = [c1d1 c2d2 c3d3 … cndn]
c
d
c = [1 2 3 4 5] d = [–2 6 –3 8 7]
c.?d = 1 × (–2) 2 × 6 3 × (–3) 4 × 8 5 × 7 = –2 12 –9 32 35
y f(t) 3e–4t cos5t 2e–3t – sin2t t2
t + 1
= = +
0 ? t ? 5
numerical analysis using matlab and spreadsheets, second edition 1-21
orchard publications
multiplication, division and exponentiation
solution:
the matlab code for this example is as follows:
t=0: 0.01: 5 % define t-axis in 0.01 increments
y=3 .* exp(?4 .* t) .* cos(5 .* t)?2 .* exp(?3 .* t) .* sin(2 .* t) + t .^2 ./ (t+1)
plot(t,y) grid xlabel( t ) ylabel( y=f(t) ) title( plot for example 1.13 )
figure 1.8 shows the plot for this example.
figure 1.8. plot for example 1.13
had we, in this example, defined the time interval starting with a negative value equal to or less
than , say as , matlab would have displayed the following message:
warning: divide by zero.
this warning is displayed because the last term (the rational fraction) of the given expression, is
divided by zero when . to avoid division by zero, we use the special matlab function
eps, which is a number approximately equal to . it will be used with the next example.
the command axis([xmin xmax ymin ymax]) scales the current plot to the values specified by
the arguments xmin, xmax, ymin and ymax. there are no commas between these four arguments.
this command must be placed after the plot command and must be repeated for each plot.
the following example illustrates the use of the dot multiplication, division, and exponentiation,
the eps number, the axis([xmin xmax ymin ymax]) command, and also matlab’s capability
of displaying up to four windows of different plots.
example 1.14
plot the functions
(1.18)
–1 –3 ? t ? 3
t = –1
2.2 10–16 ×
y = sin2x, z = cos2x, w = sin2x ? cos2x, v = sin2x ? cos2x
chapter 1 introduction to matlab
1-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
in the interval using 100 data points. use the subplot command to display these functions
on four windows on the same graph.
solution:
the matlab code to produce the four subplots is as follows:
x=linspace(0, 2*pi,100) % interval with 100 data points
y=(sin(x) .^ 2) z=(cos(x) .^ 2)
w=y .* z
v=y ./ (z+eps) % add eps to avoid division by zero
subplot(221) % upper left of four subplots
plot(x,y) axis([0 2*pi 0 1])
title( y=(sinx)^2 )
subplot(222) % upper right of four subplots
plot(x,z) axis([0 2*pi 0 1])
title( z=(cosx)^2 )
subplot(223) % lower left of four subplots
plot(x,w) axis([0 2*pi 0 0.3])
title( w=(sinx)^2*(cosx)^2 )
subplot(224) % lower right of four subplots
plot(x,v) axis([0 2*pi 0 400])
title( v=(sinx)^2/(cosx)^2 )
these subplots are shown in figure 1.9.
figure 1.9. subplots for the functions of example 1.14
the next example illustrates matlab’s capabilities with imaginary numbers. we will introduce
the real(z) and imag(z) functions which display the real and imaginary parts of the complex quantity
z = x + iy, the abs(z), and the angle(z) functions that compute the absolute value (magni-
0 ? x ? 2?
numerical analysis using matlab and spreadsheets, second edition 1-23
orchard publications
multiplication, division and exponentiation
tude) and phase angle of the complex quantity . we will also use the
polar(theta,r) function that produces a plot in polar coordinates, where r is the magnitude, theta
is the angle in radians, and the round(n) function that rounds a number to its nearest integer.
example 1.15
consider the electric circuit of figure 1.10.
figure 1.10. electric circuit for example 1.15
with the given values of resistance, inductance, and capacitance, the impedance as a function
of the radian frequency can be computed from the following expression.
(1.19)
a. plot (the real part of the impedance ) versus frequency .
b. plot (the imaginary part of the impedance ) versus frequency .
c. plot the impedance versus frequency in polar coordinates.
solution:
the matlab code below computes the real and imaginary parts of that is, for simplicity,
denoted as , and plots these as two separate graphs (parts a & b). it also produces a polar plot
(part c).
w=0: 1: 2000 % define interval with one radian interval
z=(10+(10 .^ 4 ?j .* 10 .^ 6 ./ (w+eps)) ./ (10 + j .* (0.1 .* w ?10.^5./ (w+eps))))
%
% the first five statements (next two lines) compute and plot re{z}
real_part=real(z) plot(w,real_part) grid
xlabel( radian frequency w ) ylabel( real part of z )
%
% the next five statements (next two lines) compute and plot im{z}
imag_part=imag(z) plot(w,imag_part) grid
xlabel( radian frequency w ) ylabel( imaginary part of z )
% the last six statements (next six lines) below produce the polar plot of z
z = x + iy = r??
??
?? ??
??
a
b
10 ?
10 ?
0.1 h
zab 10 ?f
zab
?
zab z 10 104 j 106 – ( ? ?)
10 + j (0.1? – 105 ? ? )
= = +
re{z} z ?
im{z} z ?
z ?
zab
z
chapter 1 introduction to matlab
1-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
mag=abs(z) % computes |z|
rndz=round(abs(z)) % rounds |z| to read polar plot easier
theta=angle(z) % computes the phase angle of impedance z
polar(theta,rndz) % angle is the first argument
grid
ylabel( polar plot of z )
the real, imaginary, and polar plots are shown in figures 1.11, 1.12, and 1.13 respectively.
figure 1.11. plot for the real part of z in example 1.15
figure 1.12. plot for the imaginary part of z in example 1.15
numerical analysis using matlab and spreadsheets, second edition 1-25
orchard publications
script and function files
figure 1.13. polar plot of z in example 1.15
example 1.15 clearly illustrates how powerful, fast, accurate, and flexible matlab is.
1.9 script and function files
matlab recognizes two types of files: script files and function files. both types are referred to as
m-files since both require the .m extension.
a script file consists of two or more built-in functions such as those we have discussed thus far.
thus, the code for each of the examples we discussed earlier, make up a script file. generally, a
script file is one which was generated and saved as an m-file with an editor such as the matlab’s
editor/debugger.
a function file is a user-defined function using matlab. we use function files for repetitive
tasks. the first line of a function file must contain the word function, followed by the output argument,
the equal sign ( = ), and the input argument enclosed in parentheses. the function name
and file name must be the same, but the file name must have the extension .m. for example, the
function file consisting of the two lines below
function y = myfunction(x)
y=x .^ 3 + cos(3 .* x)
is a function file and must be saved. to save it, from the file menu of the command window, we
choose new and click on m-file. this takes us to the editor window where we type these two
lines and we save it as myfunction.m.
we will use the following matlab functions with the next example.
the function fzero(f,x) tries to find a zero of a function of one variable, where f is a string containing
the name of a real-valued function of a single real variable. matlab searches for a value
near a point where the function f changes sign, and returns that value, or returns nan if the
search fails.
chapter 1 introduction to matlab
1-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
important: we must remember that we use roots(p) to find the roots of polynomials only, such as
those in examples 1.1 and 1.2.
fmin(f,x1,x2) minimizes a function of one variable. it attempts to return a value of where is
minimum in the interval . the string f contains the name of the function to be minimized.
note: matlab does not have a function to maximize a function of one variable, that is, there is
no such function as fmax(f,x1,x2) matlab but since a maximum of is equal to a minimum
of , we can use fmin(f,x1,x2) to find both minimum and maximum values of a function.
fplot(fcn,lims) plots the function specified by the string fcn between the -axis limits specified by
lims = [xmin xmax]. using lims = [xmin xmax ymin ymax] also controls the -axis limits. the
string fcn must be the name of an m-file function or a string with variable .
note: nan (not-a-number) is not a function it is matlab’s response to an undefined expression
such as , , or inability to produce a result as described on the next paragraph. we
can avoid division by zero using the eps number, that we mentioned earlier.
example 1.16
find the zeros, maxima and minima of the function
(1.20)
solution:
we first plot this function to observe the approximate zeros, maxima, and minima using the following
code.
x=?1.5: 0.01: 1.5
y=1 ./ ((x?0.1) .^ 2 + 0.01) ?1 ./ ((x?1.2) .^ 2 + 0.04) ?10
plot(x,y) grid
the plot is shown in figure 1.14.
the roots (zeros) of this function appear to be in the neighborhood of and . the
maximum occurs at approximately where, approximately, , and the minimum
occurs at approximately where, approximately, .
next, we define and save as the funczero01.m function m-file with the following code:
function y=funczero01
% finding the zeros of the function below
y=1/((x?0.1)^2+0.01)?1/((x?1.2)^2+0.04)?10
x f(x)
x1 < x < x2
f (x)
–f (x)
x
y
x
0 ? 0 ? ? ?
f (x) 1
(x – 0.1)2 + 0.01
1
(x – 1.2)2 + 0.04
= + – 10
x = –0.2 x = 0.3
x = 0.1 ymax = 90
x = 1.2 ymin = –34
f(x)
numerical analysis using matlab and spreadsheets, second edition 1-27
orchard publications
script and function files
figure 1.14. plot for example 1.16 using the plot command
now, we can use the fplot(fcn,lims) command to plot as follows.
fplot( funczero01 , [ ?1.5 1.5] ) grid
this plot is shown in figure 1.15. as expected, this plot is identical to the plot of figure 1.14 that
was obtained with the plot(x,y) command.
figure 1.15. plot for example 1.16 using the fplot command
we will use the fzero(f,x) function to compute the roots of in (1.20) more precisely. the
code below must be saved with a file name, and then invoked with that file name.
x1= fzero( funczero01 , ?0.2)
x2= fzero( funczero01 , 0.3)
f(x)
f(x)
chapter 1 introduction to matlab
1-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
fprintf( the roots (zeros) of this function are r1= %3.4f ,x1) % this command is explained below
fprintf( and r2= %3.4f \n , x2)
matlab displays the following:
the roots (zeros) of this function are r1= -0.1919 and r2= 0.3788
the fprintf(format,array) command used above displays and prints both text and arrays. it uses
specifiers to indicate where and in which format the values would be displayed and printed. thus,
if %f is used, the values will be displayed and printed in fixed decimal format, and if %e is used, the
values will be displayed and printed in scientific notation format. with these commands only the
real part of each parameter is processed.
whenever we use the fmin(f,x1,x2) function, we must remember that this function searches for a
minimum and it may display the values of local minima* , if any, before displaying the function minimum.
it is, therefore, advisable to plot the function with either the plot(x,y) or the fplot(fcn,lims)
command to find the smallest possible interval within which the function minimum lies. for this
example, we specify the range rather than the interval .
the minimum of is found with the fmin(f,x1,x2) function as follows:
min_val=fmin( funczero01 , 0, 1.5)
min_val =
1.2012
this is the value of x at which is minimum. to find the value of corresponding to this
value of , we substitute it into , that is,
x=1.2012
y=1 / ((x?0.1) ^ 2 + 0.01) ?1 / ((x?1.2) ^ 2 + 0.04) ?10
y =
-34.1812
to find the maximum value, we must first define a new function m-file that will produce . we
define it as follows:
function y=minusfunczero01(x)
% it is used to find maximum value from ?f(x)
y=?(1/((x?0.1)^2+0.01)?1/((x?1.2)^2+0.04)?10)
we have placed the minus (?) sign in front of the right side of the last expression above, so that
* local maxima or local minima, are the maximum or minimum values of a function within a restricted range of values in the
idependent variable. when the entire range is considered, the maxima and minima are considered be to the maximum and
minimum values in the entire range in which the function is defined.
0 ? x ? 1.5 –1.5 ? x ? 1.5
f(x)
y = f(x) y
y f(x)
–f(x)
numerical analysis using matlab and spreadsheets, second edition 1-29
orchard publications
display formats
the maximum value will be displayed. of course, this is equivalent to the negative of the
funczero01 function.
now, we execute the following code to get the value of x where the maximum occurs.
max_val=fmin( minusfunczero01 , 0,1)
max_val =
0.0999
x=0.0999
y=1 / ((x?0.1) ^ 2 + 0.01) -1 / ((x?1.2) ^ 2 + 0.04) ?10
y =
89.2000
1.10 display formats
matlab displays the results on the screen in integer format without decimals if the result is an
integer number, or in short floating point format with four decimals if it a fractional number. the
format displayed has nothing to do with the accuracy in the computations. matlab performs all
computations with accuracy up to 16 decimal places.
the output format can changed with the format command. the available formats can be displayed
with the help format command as follows:
help format
format set output format.
all computations in matlab are done in double precision.
format may be used to switch between different output display
formats as follows:
format default. same as short.
format short scaled fixed point format with 5 digits.
format long scaled fixed point format with 15 digits.
format short e floating point format with 5 digits.
format long e floating point format with 15 digits.
format short g best of fixed or floating point format
with 5 digits.
format long g best of fixed or floating point format
with 15 digits.
format hex hexadecimal format.
format + the symbols +, - and blank are printed
for positive, negative and zero elements.
imaginary parts are ignored.
format bank fixed format for dollars and cents.
format rat approximation by ratio of small integers.
y = f(x)
chapter 1 introduction to matlab
1-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
spacing:
format compact suppress extra line-feeds.
format loose puts the extra line-feeds back in.
some examples with different format displays age given below.
format short 33.3335 four decimal digits (default)
format long 33.33333333333334 16 digits
format short e 3.3333e+01 four decimal digits plus exponent
format short g 33.333 better of format short or format short e
format bank 33.33 two decimal digits
format + only + or ? or zero are printed
format rat 100/3 rational approximation
1.11 summary
• we can get help with matlab topics by typing help followed by any topic available. for
example, the command help matlab\iofun will display input/output information, and help matlab
graphics will display help on graphics.
• the matlab demos menu displays matlab’s capabilities. to access it we type demo and
we see the different topics. whenever we want to return to the command window, we click on
the close button.
• we type quit or exit when we are done and want to leave matlab.
• we use the clear command if we want to clear all previous values, variables, and equations
without exiting.
• the clc command clears the screen but matlab still remembers all values, variables and
equations which we have already used.
• all text after the % (percent) symbol is interpreted by matlab as a comment line and thus it
is ignored during the execution of a program. a comment can be typed on the same line as the
function or command or as a separate line.
• for computations involving complex numbers we can use either , or to denote the imaginary
part of the complex number.
• in matlab, a polynomial is expressed as a row vector of the form . the
elements of this vector are the coefficients of the polynomial in descending order. we must
include terms whose coefficients are zero.
i j
[an an – 1 a2 a1 a0]
ai
numerical analysis using matlab and spreadsheets, second edition 1-31
orchard publications
summary
• we can find the roots of any polynomial with the roots(p) function where p is a row vector
containing the polynomial coefficients in descending order.
• we can compute the coefficients of a polynomial from a given set of roots with the poly(r)
function where r is a row vector containing the roots.
• the polyval(p,x) function evaluates a polynomial at some specified value of the independent
variable .
• the conv(a,b) function multiplies the polynomials a and b.
• the [q,r]=deconv(c,d) function divides polynomial c by polynomial d and displays the quotient
q and remainder r.
• the polyder(p) function produces the coefficients of the derivative of a polynomial p.
• we can write matlab statements in one line if we separate them by commas or semicolons.
commas will display the results whereas semicolons will suppress the display.
• rational polynomials are those which can be expressed in ratio form, that is, as
where some of the terms in the numerator and/or denominator may be zero. normally, we
express the numerator and denominator of a rational function as a combination of linear and
quadratic factors.
• we use the matlab command plot(x,y) to make two-dimensional plots. this command plots
versus where is the horizontal axis (abscissa) and is the vertical axis (ordinate).
• if a statement, or a row vector is too long to fit in one line, it can be continued to the next line
by typing three or more periods, then pressing to start a new line, and continue to
enter data.
• we can make a plot more presentable with the commands grid, box, title(‘string’), xlabel(‘
string’), and ylabel(‘string’).
• the semilogx(x,y) command that is similar to the plot(x,y) command, except that the -axis is
represented as a log scale, and the -axis as a linear scale. likewise, the semilogy(x,y) command
is similar to the plot(x,y) command, except that the -axis is represented as a log scale,
and the -axis as a linear scale. the loglog(x,y) command uses logarithmic scales for both
axes.
• the function log(x) in matlab is the natural logarithm, whereas the common logarithm is
expressed as log10(x). likewise, the logarithm to the base 2 is expressed as log2(x).
p(x)
x
r(x) num(x)
den(x)
-
bnxn bn – 1xn – 1 bn – 2xn – 2 + + +… + b1x + b0
amxm am – 1xm – 1 am – 2xm – 2 + + +… + a1x + a0
= = -
y x x y
x
y
y
x
chapter 1 introduction to matlab
1-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the ratio of any two values of the same quantity, typically power, is normally expressed in decibels
(db) and by definition,
• the command gtext(‘string’) switches to the current figure window, and displays a cross-hair
which can be moved around with the mouse. the command text(x,y,’string’) is similar to
gtext(‘string’) it places a label on a plot in some specific location specified by x and y, and string
is the label which we want to place at that location.
• the command linspace(first_value, last_value, number_of_values) specifies the number of data
points but not the increments between data points. an alternate command uses the colon notation
and has the format x=first: increment: last. this format specifies the increments between
points but not the number of data points.
• matlab has no default color it starts with blue and cycles through seven colors. also, there
is no default marker no markers are drawn unless they are selected. the default line is the solid
line.
• the plot3(x,y,z) command plots a line in 3-space through the points whose coordinates are the
elements of , , and , where , , and are three vectors of the same length.
• in a three-dimensional plot, we can use the zlabel(‘string’) command in addition to the xlabel(‘
string’) and ylabel(‘string’).
• in a two-dimensional plot, we can set the limits of the - and -axes with the axis([xmin xmax
ymin ymax]) command. likewise, in a three-dimensional plot we can set the limits of all three
axes with the axis([xmin xmax ymin ymax zmin zmax]) command. it must be placed after the
plot(x,y) or plot3(x,y,z) commands, or on the same line without first executing the plot command.
this must be done for each plot. the three-dimensional text(x,y,z,’string’) command will
place string beginning at the co-ordinate ( ) on the plot.
• the mesh(x,y,z) command displays a three-dimensional plot. another command, contour(z,n),
draws contour lines for n levels. we can also use the mesh(x,y,z) command with two vector
arguments. these must be defined a s and where
. in this case, the vertices of the mesh lines are the triples .
we observe that x corresponds to the columns of , and y corresponds to the rows of . to
produce a mesh plot of a function of two variables, say , we must first generate the
and matrices which consist of repeated rows and columns over the range of the variables
and . we can generate the matrices and with the [x,y]=meshgrid(x,y) function which
creates the matrix whose rows are copies of the vector x, and the matrix whose columns
are copies of the vector y.
db 10
pout
pin
= log
x y z x y z
x y
x, y, z
length(x) = n length(y) = m
[m, n] = size(z) {x(j), y(i), z(i, j)}
z z
z = f(x, y) x
y x
y x y
x y
numerical analysis using matlab and spreadsheets, second edition 1-33
orchard publications
summary
• matlab can display up to four windows of different plots on the figure window using the
command subplot(m,n,p). this command divides the window into an matrix of plotting
areas and chooses the area to be active.
• with matlab, matrix multiplication (multiplication of row by column vectors) is performed
with the matrix multiplication operator (*), whereas element-by-element multiplication is performed
with the dot multiplication operator (.*). similarly, the division (/) and exponentiation
(^) operators, are used for matrix division and exponentiation, whereas dot division (./) and
dot exponentiation (.^) are used for element-by-element division and exponentiation.
• to avoid division by zero, we use the special matlab function eps, which is a number
approximately equal to .
• the command axis([xmin xmax ymin ymax]) scales the current plot to the values specified by
the arguments xmin, xmax, ymin and ymax. there are no commas between these four arguments.
this command must be placed after the plot command and must be repeated for each
plot.
• the real(z) and imag(z) functions display the real and imaginary parts of the complex quantity
z = x + iy, and the abs(z), and the angle(z) functions compute the absolute value (magnitude)
and phase angle of the complex quantity . the polar(theta,r) function
produces a plot in polar coordinates, where r is the magnitude, and theta is the angle in radians.
• matlab recognizes two types of files: script files and function files. both types are referred to
as m-files. a script file consists of two or more built-in functions. generally, a script file is one
which was generated and saved as an m-file with an editor such as the matlab’s editor/
debugger. a function file is a user-defined function using matlab. we use function files for
repetitive tasks. the first line of a function file must contain the word function, followed by the
output argument, the equal sign ( = ), and the input argument enclosed in parentheses. the
function name and file name must be the same, but the file name must have the extension .m.
• the matlab fmin(f,x1,x2) function minimizes a function of one variable. it attempts to
return a value of where is minimum in the interval . the string f contains the
name of the function to be minimized.
• the matlab fplot(fcn,lims) command plots the function specified by the string fcn between
the -axis limits specified by lims = [xmin xmax]. using lims = [xmin xmax ymin ymax] also
controls the -axis limits. the string fcn must be the name of an m-file function or a string
with variable .
• the matlab fprintf(format,array) command used above displays and prints both text and
arrays. it uses specifiers to indicate where and in which format the values would be displayed
and printed. thus, if %f is used, the values will be displayed and printed in fixed decimal form
× n
pth
2.2 10–16 ×
z = x + iy = r??
x f(x) x1 < x < x2
x
y
x
chapter 1 introduction to matlab
1-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
mat, and if %e is used, the values will be displayed and printed in scientific notation format.
with these commands only the real part of each parameter is processed.
• matlab displays the results on the screen in integer format without decimals if the result is
an integer number, or in short floating point format with four decimals if it a fractional number.
the format displayed has nothing to do with the accuracy in the computations. matlab performs
all computations with accuracy up to 16 decimal places.
numerical analysis using matlab and spreadsheets, second edition 1-35
orchard publications
exercises
1.12 exercises
1. use matlab to compute the roots of the following polynomials:
a.
b.
2. use matlab to derive the polynomials having the following roots:
a.
b. two roots at and three roots at
3. use matlab to evaluate the polynomials below at the specified values.
a. at
b. at
4. in the circuit below, the applied voltage was kept constant and the voltage across the
capacitor was measured and recorded at several frequencies as shown on the table below.
plot (in db scale) versus (in common log scale) and label the axes appropriately.
capacitor voltage versus radian frequency
500 600 700 800 900 1000
88.9 98.5 103.0 104.9 105.3 104.8
1100 1200 1300 1400 1500 1600
103.8 102.4 100.7 98.9 96.5 94.9
p(x) = x3 + 8x2 + 10x + 4
p(y) = y5 + 7y4 + 19y3 + 25y2 + 16y + 4
–6.5708 – 0.7146 + j0.3132 –0.7146–j0.3132
x = –2.000 x = –3.000
p(x) = x3 + 8x2 + 10x + 4 x = 1.25
p(y) = y5 + 7y4 + 19y3 + 25y2 + 16y + 4 y = –3.75
vs vc
??
?? ??
??
??vs vc
?
vc
?
vc
vc ?
chapter 1 introduction to matlab
1-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
1.13 solutions to exercises
dear reader:
the remaining pages on this chapter contain the solutions to the exercises.
you must, for your benefit, make an honest effort to find the solutions to the exercises without first
looking at the solutions that follow. it is recommended that first you go through and work out
those you feel that you know. for the exercises that you are uncertain, review this chapter and try
again. refer to the solutions as a last resort and rework those exercises at a later date.
you should follow this practice with the rest of the exercises of this book.
numerical analysis using matlab and spreadsheets, second edition 1-37
orchard publications
solutions to exercises
1.
a.
px=[1 8 10 4] roots(px)
ans =
-6.5708
-0.7146 + 0.3132i
-0.7146 - 0.3132i
b.
py=[1 7 19 25 16 4] roots(py)
ans =
-2.0000
-2.0000
-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i
2.
a.
r1=[?6.5708 ?0.7146+0.3132j ?0.7146-0.3132j] poly_r1=poly(r1)
poly_r1 = 1.0000 8.0000 9.9997 4.0000
b.
r2=[?2 ?2 ?3 ?3 ?3] poly_r2=poly(r2)
poly_r2 =
1 13 67 171 216 108
3.
a.
pv=[1 8 10 4] value=polyval(pv, 1.25)
value = 30.9531
b.
pw=[1 7 19 25 16 4] value=polyval(pw, ?3.75)
value = -63.6904
p(x) = x3 + 8x2 + 10x + 4
p(z) = z5 + 13z4 + 67z3 + 171z2 + 216z + 108
chapter 1 introduction to matlab
1-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
4.
w=[5 6 7 8 9 10 11 12 13 14 15 16]*100
vc=[88.9 98.5 103 104.9 105.3 104.8 103.8 102.4 100.7 98.9 96.5 94.9]
db=20*log10(vc) semilogx(w,db) grid title( magnitude of vc vs. w ) ...
xlabel( w in rads/sec ) ylabel( |vc| in volts )
numerical analysis using matlab and spreadsheets, second edition 2-1
orchard publications
chapter 2
root approximations
his chapter is an introduction to newton’s and bisection methods for approximating roots
of linear and non-linear equations. several examples are presented to illustrate practical
solutions using matlab and spreadsheets.
2.1 newton’s method for root approximation
newton’s (or newton-raphson) method can be used to approximate the roots of any linear or nonlinear
equation of any degree. this is an iterative (repetitive procedure) method and it is derived
with the aid of figure 2.1.
figure 2.1. newton’s method for approximating real roots of a function
we assume that the slope is neither zero nor infinite. then, the slope (first derivative) at
is
(2.1)
the slope crosses the at and . since this point lies on
the slope line, it satisfies (2.1). by substitution,
(2.2)
and in general,
(2.3)
t


tangent line (slope) to the curve
at point
y = f(x)
x
y
{x1, f(x1)}
(x2, 0)
{x1, f(x1)}
y = f(x)
x = x1
f (x1)
y f x1
– ( )
x – x1
=
y f x1
– ( ) = f (x1)(x – x1)
x – axis x = x2 y = 0 [x2, f(x2)] = (x2, 0)
0 f x1
– ( ) = f (x1)(x2 – x1)
x2 x1
f(x1)
f (x1)
= –
xn + 1 xn
f(xn)
f (xn)
= –
chapter 2 root approximations
2-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 2.1
use newton’s method to approximate the positive root of
(2.4)
to four decimal places.
solution:
as a first step, we plot the curve of (2.4) to find out where it crosses the . this can be
done easily with a simple plot using matlab or a spreadsheet. we start with matlab and will
discuss the steps for using a spreadsheet afterwards.
we will now introduce some new matlab functions and review some which we discussed in
chapter 1.
input(‘string’): it displays the text string, and waits for an input from the user. we must enclose
the text in single quotation marks.
we recall that the polyder(p) function displays the row vector whose values are the coefficients
of the first derivative of the polynomial p. the polyval(p,x) function evaluates the polynomial p
at some value x. therefore, we can compute the next iteration for approximating a root with
newton’s method using these functions. knowing the polynomial p and the first approximation
, we can use the following code for the next approximation .
q=polyder(p)
x1=x0?polyval(p,x0)/polyval(q,x0)
we used the fprintf command in chapter 1 we will use it many more times. therefore, let us
review it again.
the following description was extracted from the help fprintf function.
it formats the data in the real part of matrix a (and in any additional matrix arguments), under control
of the specified format string, and writes it to the file associated with file identifier fid and contains c language
conversion specifications. these specifications involve the character %, optional flags, optional
width and precision fields, optional subtype specifier, and conversion characters d, i, o, u, x, x, f, e, e,
g, g, c, and s. see the language reference guide or a c manual for complete details. the special formats
\n,\r,\t,\b,\f can be used to produce linefeed, carriage return, tab, backspace, and formfeed characters
respectively. use \\ to produce a backslash character and %% to produce the percent character.
to apply newton’s method, we must start with a reasonable approximation of the root value. in
all cases, this can best be done by plotting versus with the familiar statements below. the
following two lines of code will display the graph of the given equation in the interval .
x=linspace(?4, 4, 100) % specifies 100 values between ?4 and 4
y=x .^ 2 ? 5 plot(x,y) grid % the dot exponentiation is a must
f(x) = x2 – 5
x – axis
x0 x1
f(x) x
–4 ? x ? 4
numerical analysis using matlab and spreadsheets, second edition 2-3
orchard publications
newton’s method for root approximation
we chose this interval because the given equation asks for the square root of we expect this
value to be a value between and . for other functions, where the interval may not be so obvious,
we can choose a larger interval, observe the crossings, and then redefine the interval.
figure 2.2. plot for the curve of example 2.1
as expected, the curve shows one crossing between and , so we take as our
first approximation, and we compute the next value as
(2.5)
the second approximation yields
(2.6)
we will use the following matlab code to verify (2.5) and (2.6).
% approximation of a root of a polynomial function p(x)
% do not forget to enclose the coefficients in brackets [ ]
p=input( enter coefficients of p(x) in descending order: )
x0=input( enter starting value: )
q=polyder(p) % calculates the derivative of p(x)
x1=x0-polyval(p,x0)/polyval(q,x0)
fprintf( \n ) % inserts a blank line
%
% the next function displays the value of x1 in decimal format as indicated
5
2 3
x – axis
x = 2 x = 3 x0 = 2
x1
x1 x0
f(x0)
f (x0)
– 2 (2)2 – 5
2(2)
– 2 (–1)
4
= = = – = 2.25
x2 x1
f(x1)
f (x1)
– 2.25 (2.25)2 – 5
2(2.25)
– 2.25 0.0625
4.5
= = = – = 2.2361
chapter 2 root approximations
2-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
% by the specifier %9.6f, i.e., with 9 digits where 6 of these digits
% are to the right of the decimal point such as xxx.xxxxxx, and
% \n prints a blank line before printing x1
fprintf( the next approximation is: %9.6f \n , x1)
fprintf( \n ) % inserts another blank line
%
fprintf( rerun the program using this value as your next....
approximation \n )
the following lines show matlab’s inquiries and our responses (inputs) for the first two
approximations.
enter coefficients of p(x) in descending order:
[1 0 ?5]
enter starting value: 2
the next approximation is: 2.250000
rerun the program using this value as your
next approximation
enter polynomial coefficients in
descending order: [1 0 ?5]
enter starting value: 2.25
the next approximation is: 2.236111
we observe that this approximation is in close agreement with (2.6).
in chapter 1 we discussed script files and function files. we recall that a function file is a userdefined
function using matlab. we use function files for repetitive tasks. the first line of a
function file must contain the word function followed by the output argument, the equal sign (=),
and the input argument enclosed in parentheses. the function name and file name must be the
same but the file name must have the extension .m. for example, the function file consisting of
the two lines below
function y = myfunction(x)
y=x .^ 3 + cos(3 .* x)
is a function file and must be saved as myfunction.m
we will use the while end loop, whose general form is
while expression
commands ...
end
where the commands ... in the second line are executed as long as all elements in expression of the
first line are true.
we will also be using the following commands:
numerical analysis using matlab and spreadsheets, second edition 2-5
orchard publications
newton’s method for root approximation
disp(x): displays the array x without printing the array name. if x is a string, the text is displayed.
for example, if , disp(v) displays 12, and disp(‘volts’) displays volts.
sprintf(format,a): formats the data in the real part of matrix a under control of the specified
format string. for example,
sprintf( %d ,round(pi))
ans =
3
where the format code %d specifies an integer. likewise,
sprintf( %4.3f ,pi)
ans =
3.142
where the format code %4.3f specifies a fixed format of 4 digits where 3 of these digits are allocated
to the fractional part.
example 2.2
approximate one real root of the non-linear equation
(2.7)
to four decimal places using newton’s method.
solution:
as a first step, we sketch the curve to find out where the curve crosses the . we generate
the plot with the code below.
x=linspace(?pi, pi, 100) y=x .^ 2 + 4 .* x + 3 + sin(x) ? x .* cos(x) plot(x,y) grid
the plot is shown in figure 2.3.
the plot shows that one real root is approximately at , so we will use this value as our first
approximation.
next, we generate the function funcnewt01 and we save it as an m-file. to save it, from the file
menu of the command window, we choose new and click on m-file. this takes us to the editor
window where we type the following three lines and we save it as funcnewt01.m.
function y=funcnewt01(x)
% approximating roots with newton s method
y=x .^ 2 + 4 .* x + 3 + sin(x) ? x .* cos(x)
we also need the first derivative of y this is
v = 12
f(x) = x2 + 4x + 3 + sinx – x cosx
x – axis
x = –1
y = 2x + 4 + x sinx
chapter 2 root approximations
2-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 2.3. plot for the equation of example 2.2
the computation of the derivative for this example was a simple task however, we can let matlab
do the differentiation, just as a check, and to introduce the diff(s) function. this function
performs differentiation of symbolic expressions. the syms function is used to define one or
more symbolic expressions.
syms x
y = x^2+4*x+3+sin(x)?x*cos(x) % dot operations are not necessary with
% symbolic expressions, but correct
% answer will be displayed if they are used.
y1=diff(y) % find the derivative of y
y1 =
2*x+4+x*sin(x)
now, we generate the function funcnewt02, and we save it as m-file. to save it, from the file
menu of the command window, we choose new and click on m-file. this takes us to the editor
window where we type these two lines and we save it as funcnewt02.m.
function y=funcnewt02(x)
% finding roots by newton s method
% the following is the first derivative of the function defined as funcnewt02
y=2 .* x + 4 + x .* sin(x)
our code for finding the next approximation with newton’s method follows.
x = input( enter starting value: )
fx = funcnewt01(x)
fprimex = funcnewt02(x)
numerical analysis using matlab and spreadsheets, second edition 2-7
orchard publications
approximations with spreadsheets
xnext = x-fx/fprimex
x = xnext
fx = funcnewt01(x)
fprimex = funcnewt02(x)
disp(sprintf( first approximation is x = %9.6f \n , x))
while input( next approximation? (=no,1=yes) )
xnext=x-fx/fprimex
x=xnext
fx=funcnewt01(x)
fprimex=funcnewt02(x)
disp(sprintf( next approximation is x = %9.6f \n , x))
end
disp(sprintf( %9.6f \n , x))
matlab produces the following result with as a starting value.
enter starting value: ?1
first approximation is: -0.894010
next approximation? (=no,1=yes)1
-0.895225
next approximation? (=no,1=yes)
we can also use the fzero(f,x) function. it was introduced in chapter 1. this function tries to
find a zero of a function of one variable. the string f contains the name of a real-valued function
of a single real variable. as we recall, matlab searches for a value near a point where the function
f changes sign and returns that value, or returns nan if the search fails.
2.2 approximations with spreadsheets
in this section, we will go through several examples to illustrate the procedure of using a spreadsheet
such as excel* to approximate the real roots of linear and non-linear equations.
we recall that there is a standard procedure for finding the roots of a cubic equation it is
included here for convenience.
a cubic equation of the form
(2.8)
can be reduced to the simpler form
(2.9)
where
* we will illustrate our examples with excel, although others such as lotus 1-2-3, and quattro can also be used. henceforth,
all spreadsheet commands and formulas that we will be using, will be those of excel.
–1
y3 + py2 + qy + r = 0
x3 + ax + b = 0
chapter 2 root approximations
2-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(2.10)
for the solution it is convenient to let
(2.11)
then, the values of for which the cubic equation of (2.11) is equal to zero are
(2.12)
if the coefficients , , and are real, then (2.13)
while matlab handles complex numbers very well, spreadsheets do not. therefore, unless we
know that the roots are all real, we should not use a spreadsheet to find the roots of a cubic equation
by substitution in the above formulas. however, we can use a spreadsheet to find the real
root since in any cubic equation there is at least one real root. for real roots, we can use a spreadsheet
to define a range of values with small increments and compute the corresponding values
of . then, we can plot versus to observe the values of that make . this
procedure is illustrated with the examples that follow.
note: in our subsequent discussion we will omit the word cell and the key . thus b3,
c11, and so on will be understood to be cell b3, cell c11, and so on. also, after an entry has been
made, it will be understood that the key was pressed.
example 2.3
compute the roots of the polynomial
(2.14)
using excel.
x y p3
+ a 13
(3q – p2) b 1
27
= = = -(2p3 – 9pq + 27r)
a –b
2
- b2
4
- a3
27
+ + -
3
= b –b
2
- b2
4
- a3
27
+ + -
3
= –
x
x1 = a + b x2
a b +
2
– - a b –
2 = + - –3 x3
a b +
2
– - a b –
2 = – - –3
p q r
if b2
4
- a3
27
+ - > 0 one root will be real and the other two complex conjugates
if b2
4
- a3
27
+ - < 0 the roots will be real and unequal
if b2
4
- a3
27
+ - = 0 there will be three real roots with at least two equal
x
y = f(x) y x x f(x) = 0
y = f(x) = x3 – 7x2 + 16x – 12
numerical analysis using matlab and spreadsheets, second edition 2-9
orchard publications
approximations with spreadsheets
solution:
we start with a blank worksheet. in an excel worksheet, a selected cell is surrounded by a heavy
border. we select a cell by moving the thick hollow white cross pointer to the desired cell and we
click. for this example, we first select a1 and we type x. we observe that after pressing the
key, the next cell moves downwards to a2 this becomes the next selected cell. we type
0.00 in a2. we observe that this value is displayed just as 0, that is, without decimals. next, we
type 0.05 in a3. we observe that this number is displayed exactly as it was typed.
we will enter more values in column a, and to make all values look uniform, we click on letter a
on top of column a. we observe that the entire column is now highlighted, that is, the background
on the monitor has changed from white to black. next, from the tools droping menu of the
menu bar, we choose options and we click on the edit tab. we click on the fixed decimal check
box to place a check mark and we choose 2 as the number of decimal places. we repeat these
steps for column b and we choose 3 decimal places. then, all numbers that we will type in column
a will be fixed numbers with two decimal places, and the numbers in column b will be fixed
with three decimal places.
to continue, we select a2, we click and holding the mouse left button down, we drag the mouse
down to a3 so that both these two cells are highlighted then we release the mouse button.
when properly done, a2 will have a white background but a3 will have a black background. we
will now use the autofill* feature to fill-in the other values of in column a. we will use values
in 0.05 increments up to 5.00. column a now contains 100 values of from 0.00 to 5.00 in increments
of 0.05.
next, we select b1, and we type f(x). in b2, we type the equation formula with the = sign in front
of it, that is, we type
= a2^3?7*a2^2 + 16*a2?2
where a2 represents the first value of . we observe that b2 displays the value .
this is the value of when next, we want to copy this formula to the range b3:b102
(the colon : means b3 through b102). with b2 still selected, we click on edit on the main taskbar,
and we click on copy. we select the range b3:b102 with the mouse, we release the mouse button,
and we observe that this range is now highlighted. we click on edit, then on paste and we observe
that this range is now filled with the values of . alternately, we can use the copy and paste
icons of the taskbar.
* to use this feature, we highlight cells a2 and a3. we observe that on the lower right corner of a3, there is a small black
square this is called the fill handle. if it does not appear on the spreadsheet, we can make it visible by performing the
sequential steps tools>options, select the edit tab, and place a check mark on the drag and droping setting. next, we point
the mouse to the fill handle and we observe that the mouse pointer appears as a small cross. we click, hold down the mouse
button, we drag it down to a102, and we release the mouse button. we observe that, as we drag the fill handle, a pop-up
note shows the cell entry for the last value in the range.
x
x
x = 0.00 –12.000
f (x) x = 0.00
f (x)
chapter 2 root approximations
2-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
to plot versus , we click on the chart wizard icon of the standard toolbar, and on the
chart type column we click on xy (scatter). from the displayed charts, we choose the one on top
of the right side (the smooth curves without connection points). then, we click on next, next,
next, and finish. a chart similar to the one on figure 2.4 appears.
figure 2.4. plot of the equation of example 2.3.
we will modify this plot to make it more presentable, and to see more precisely the crossing(
s), that is, the roots of . this is done with the following steps:
1. we click on the series 1 box to select it, and we deleting it by pressing the deleting key.
2. we click anywhere inside the graph box. then, we see it enclosed in six black square handles.
from the view menu, we click on toolbars, and we place a check mark on chart. the chart
menu appears in two places, on the main taskbar and below it in a box where next to it is
another small box with the hand icon. note: the chart menu appears on the main taskbar and
on the box below it, only when the graph box is selected, that is, when it is enclosed in black
square handles. from the chart menu box (below the main taskbar), we select value (x) axis,
and we click on the small box next to it (the box with the hand icon). then, on the format axis
menu, we click on the scale tab and we make the following entries:
minimum: 0.0
maximum: 5.0
major unit: 1.0
minor unit: 0.5
we click on the number tab, we select number from the category column, and we type 0 in the
decimal places box. we click on the font tab, we select any font, regular style, size 9. we click
on the patterns tab to select it, and we click on low on the tick mark labels (lower right box).
f (x) x
123456789
10
11
12
a b c d e f g h i
x f(x)
0.00 -12.000
0.05 -11.217
0.10 -10.469
0.15 -9.754
0.20 -9.072
0.25 -8.422
0.30 -7.803
0.35 -7.215
0.40 -6.656
0.45 -6.126
0.50 -5.625
f(x)
-15.000
-10.000
-5.000
0.000
5.000
10.000
15.000
20.000
0.00 1.00 2.00 3.00 4.00 5.00 6.00
f(x)
x – axis
f (x)
numerical analysis using matlab and spreadsheets, second edition 2-11
orchard publications
approximations with spreadsheets
we click on ok to return to the graph.
3. from the chart menu box we select value (y) axis and we click on the small box next to it (the
box with the hand icon). on the format axis menu, we click on the scale tab, and we make the
following entries:
minimum: ?1.0
maximum: 1.0
major unit: 0.25
minor unit: 0.05
we click on the number tab, we select number from the category column, and we select 2 in
the decimal places box. we click on the font tab, select any font, regular style, size 9. we click
on the patterns tab, and we click on outside on the major tick mark type (upper right box). we
click on ok to return to the graph.
4. we click on chart on the main taskbar, and on the chart options. we click on gridlines, we
place check marks on major gridlines of both value (x) axis and value (y) axis. then, we click
on the titles tab and we make the following entries:
chart title: f(x) = the given equation (or whatever we wish)
value (x) axis: x (or whatever we wish)
value (y) axis: y=f(x) (or whatever we wish)
5. now, we will change the background of the plot area from gray to white. from the chart
menu box below the main task bar, we select plot area and we observe that the gray background
of the plot area is surrounded by black square handles. we click on the box next to it
(the box with the hand icon), and on the area side of the patterns tab, we click on the white
square which is immediately below the gray box. the plot area on the chart now appears on
white background.
6. to make the line of the curve thicker, we click at any point near it and we observe that
several black square handles appear along the curve. series 1 appears on the chart menu box.
we click on the small box next to it, and on the patterns tab. from the weight selections we
select the first of the thick lines.
7. finally, to change chart area square corners to round, we select chart area from the chart
menu, and on the patterns tab we place a check mark on the round corners box.
the plot now resembles the one shown in figure 2.5 where we have shown partial lists of and
. the given polynomial has two roots at , and the third root is .
we will follow the same procedure for generating the graphs of the other examples which follow
therefore, it is highly recommended that this file is saved with any name, say poly01.xls where.xls
is the default extension for file names saved in excel.
f(x)
x
f(x) x = 2 x = 3
chapter 2 root approximations
2-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 2.5. modified plot of the equation of example 2.3.
example 2.4
find a real root of the polynomial
(2.15)
using excel.
solution:
to save lots of unnecessary work, we invoke (open) the spreadsheet of the previous example, that
is, poly01.xls (or any other file name that was assigned to it), and save it with another name such
as poly02.xls. this is done by first opening the file poly01.xls, and from the file droping down menu,
we choose the save as option then, we save it as poly02.xls, or any other name. when this is
done, the spreadsheet of the previous example still exists as poly01.xls. next, we perform the following
steps:
1. for this example, the highest power of the polynomial is 5 (odd number), and since we know
that complex roots occur in conjugate pairs, we expect that this polynomial will have at least
one real root. since we do not know where a real root is in the x-axis interval, we arbitrarily
choose the interval . then, we enter ?10 and ?9 in a2 and a3 respectively. using
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
a b c d e f g
x f(x)
0.00 -12.000
0.05 -11.217
0.10 -10.469
0.15 -9.754
0.20 -9.072
0.25 -8.422
0.30 -7.803
0.35 -7.215
0.40 -6.656
0.45 -6.126
0.50 -5.625
0.55 -5.151
0.60 -4.704
0.65 -4.283
0.70 -3.887 x f(x) x f(x)
0.75 -3.516 1.90 -0.011 2.90 -0.081
0.80 -3.168 1.95 -0.003 2.95 -0.045
0.85 -2.843 roots 2.00 0.000 3.00 0.000
0.90 -2.541 2.05 -0.002 3.05 0.055
0.95 -2.260 2.10 -0.009 3.10 0.121
1.00 -2.000 f(x) =0 at x=2 (double root) and at x=3
f(x) = x 3 - 7x 2 + 16x - 12
-1.00
-0.75
-0.50
-0.25
0.00
0.25
0.50
0.75
1.00
0 1 2 3 4 5
x
f(x)
y = f(x) = 3x5 – 2x3 + 6x – 8
–10 ? x ? 10
numerical analysis using matlab and spreadsheets, second edition 2-13
orchard publications
approximations with spreadsheets
the autofill feature, we fill-in the range a4:a22, and we have the interval from ?10 to 10 in
increments of 1. we must now deleting all rows starting with 23 and downward. we do this by
highlighting the range a23:b102, and we press the deleting key. we observe that the chart has
changed shape to conform to the new data.
now we select b2 where we enter the formula for the given equation, i.e.,
=3*a2^5?2*a2^3+6*a2?8
we copy this formula to b3:b22. columns a and b now contain values of x and respectively,
and the plot shows that the curve crosses the x-axis somewhere between and
.
a part of the table is shown in figure 2.6. columns a (values of x), and b (values of ),
reveal some useful information.
figure 2.6. partial table for example 2.4
this table shows that changes sign somewhere in the interval from and . let
us then redefine our interval of the x values as in increments of 0.05, to get better
approximations. when this is done a1 contains 1.00, a2 contains 1.05, and so on. our
spreadsheet now shows that there is a sign change from b3 to b4, and thus we expect that a
real root exists between and . to obtain a good approximation of the real
root in that interval, we perform steps 2 through 4 below.
2. on the view menu, we click on toolbars and place a check mark on chart. we select the graph
box by clicking inside it, and we observe the square handles surrounding it. the chart menu
on the main taskbar and the chart menu box below it, are now displayed. from the chart
menu box (below the main taskbar) we select value (x) axis, and we click on the small box
next to it (the box with the hand). next, on the format axis menu, we click on the scale tab
and make the following entries:
minimum: 1.0
maximum: 1.1
major unit: 0.02
minor unit: 0.01
f(x)
x = 1
x = 2
f(x)
x f(x)
-10.00 -298068.000
-9.00 -175751.000
0.00 -8.000
1.00 -1.000
2.00 84.000
9.00 175735.000
10.00 298052.000
sign change
f(x) x = 1 x = 2
1 ? x ? 2
x = 1.05 x = 1.10
chapter 2 root approximations
2-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3. from the chart menu we select value (y) axis, and we click on the small box next to it. then,
on the format axis menu, we click on the scale tab and make the following entries:
minimum: -1.0
maximum: 1.0
major unit: 0.5
minor unit: 0.1
4. we click on the titles tab and make the following entries:
chart title: f(x) = the given equation (or whatever we wish)
value (x) axis: x (or whatever we wish)
value (y) axis: y=f(x) (or whatever we wish)
our spreadsheet now should look like the one in figure 2.7 and we see that one real root is
approximately 1.06.
since no other roots are indicated on the plot, we suspect that the others are complex conjugates.
we confirm this with matlab as follows:
p = [ 3 0 ?2 0 6 ?8] roots_p=roots(p)
roots_p =
-1.1415 + 0.8212i
-1.1415 - 0.8212i
0.6113 + 0.9476i
0.6113 - 0.9476i
1.0604
example 2.5
compute the real roots of the trigonometric function
(2.16)
using a spreadsheet.
solution:
we invoke (open) the spreadsheet of one of the last two examples, that is, poly01.xls or poly02.xls,
and save it with another name, such as poly03.xls.
since we do not know where real roots (if any) are in the x-axis interval, we arbitrarily choose the
interval . then, we enter ?1.00 and ?0.90 in a2 and a3 respectively, using the auto-
fill feature, we fill-in the range a4:a72 and thus we have the interval from ?1 to 6 in increments
of 0.10. next, we select b2 and we enter the formula for the given equation, i.e.,
=cos(2*a2)+sin(2*a2)+a2?1
y = f(x) = cos2x + sin2x + x – 1
–1 ? x ? 6
numerical analysis using matlab and spreadsheets, second edition 2-15
orchard publications
approximations with spreadsheets
figure 2.7. graph for example 2.4
and we copy this formula to b3:b62.
there is a root at this is found by substitution of zero into the given equation. we observe
that columns a and b contain the following sign changes (only a part of the table is shown):
we observe two sign changes. therefore, we expect two more real roots, one in the
interval and the other in the interval. if we redefine the
range as 1 to 2.5, we will find that the other two roots are approximately and .
approximate values of these roots can also be observed on the plot of figure 2.8 where the curve
crosses the .
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
a b c d e f g
x f(x)
1.00 -1.000
1.05 -0.186
1.10 0.770
1.15 1.892
1.20 3.209
1.25 4.749
1.30 6.545
1.35 8.631
1.40 11.047
1.45 13.832
1.50 17.031
1.55 20.692
1.60 24.865
1.65 29.605
1.70 34.970 x f(x)
1.75 41.021 1.00 -1.000
1.80 47.823 1.05 -0.186
1.85 55.447 1.10 0.770
1.90 63.965 1.15 1.892
1.95 73.455 1.20 3.209
2.00 84.000 f(x) = ? 0.007 at x = 1.06
f(x) = 3x 5 - 2x 3 + 6x - 8
-1.00
-0.50
0.00
0.50
1.00
1.00 1.02 1.04 1.06 1.08 1.10
x
f(x)
real root between
x = 0
x f(x)
1.20 0.138
1.30 -0.041
2.20 -0.059
2.30 0.194
sign change
sign change
1.20 ? x ? 1.30 2.20 ? x ? 2.30 x – axis
x = 1.30 x = 2.24
x – axis
chapter 2 root approximations
2-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 2.8. graph for example 2.5
we can obtain more accurate approximations using excel’s goal seek feature. we use goal seek
when we know the desired result of a single formula, but we do not know the input value which
satisfies that result. thus, if we have the function , we can use goal seek to set the
dependent variable to the desired value (goal) and from it, find the value of the independent
variable which satisfies that goal. in the last three examples our goal was to find the values of
for which .
to illustrate the goal seek feature, we will use it to find better approximations for the non-zero
roots of example 2.5. we do this with the following steps:
1. we copy range a24:b24 (or a25:b25) to two blank cells, say j1 and k1, so that j1 contains
1.20 and k1 contains 0.138 (or 1.30 and ?0.041 if range a25:b25 was copied). we increase the
accuracy of columns j and k to 5 decimal places by clicking on format, cells, numbers tab.
2. from the tools droping menu, we click on goal seek, and when the goal seek dialog box appears,
we make the following entries:
set cell: k1
to value: 0
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e f g
x f(x)
-1.00 -3.325
-0.90 -3.101
-0.80 -2.829
-0.70 -2.515
-0.60 -2.170
-0.50 -1.801
-0.40 -1.421
-0.30 -1.039
-0.20 -0.668
-0.10 -0.319
0.00 0.000
0.10 0.279
0.20 0.510
0.30 0.690 x f(x)
0.40 0.814 0.00 0.000
0.50 0.882 1.20 0.138
0.60 0.894 1.30 -0.041
0.70 0.855 2.20 -0.059
0.80 0.770 2.30 0.194
f(x) = cos2x + sin2x + x - 1
-4
-2
0
2
4
6
-1 0 1 2 3 4 5 6
x
f(x)
real root between
real root between
real root at
y = f(x)
y
x x
y = f(x) = 0
numerical analysis using matlab and spreadsheets, second edition 2-17
orchard publications
approximations with spreadsheets
by changing cell: j1
3. when this is done properly, we will observe the changes in j1 and k1. these indicate that for
, .
4. we repeat the above steps for the next root near , and we verify that for
, .
another method of using the goal seek feature, is with a chart such as those we’ve created for the
last three examples. we will illustrate the procedure with the chart of example 2.5.
1. we point the mouse at the curve where it intersects the x-axis, near the point. a
square box appears and displays series 1, (1.30, ?0.041). we observe that other points are also
displayed as the mouse is moved at different points near the curve.
2. we click anywhere near the curve, and we observe that five handles (black square boxes) are
displayed along different points on the curve. next, we click on the handle near the
point, and when the cross symbol appears, we drag it towards the x-axis to change its value.
the goal seek dialog box then appears where the set cell shows b24. then, in the to value box
we enter 0, in the by changing cell we enter a24 and we click on ok. we observe now that a24
displays 1.28 and b24 displays 0.000.
for repetitive tasks, such as finding the roots of polynomials, it is prudent to construct a template
(model spreadsheet) with the appropriate formulas and then enter the coefficients of the polynomial
to find its real roots*. this is illustrated with the next example.
example 2.6
construct a template (model spreadsheet), with excel, which uses newton’s method to approximate
a real root of any polynomial with real coefficients up to the seventh power then, use it to
compute a root of the polynomial
(2.17)
given that one real root lies in the interval.
solution:
1. we start with a blank spreadsheet and we make the entries shown in figure 2.9.
* there exists a numerical procedure, known as bairstow’s method, that we can use to find the complex roots of a polynomial
with real coefficients. we will not discuss this method here it can be found in advanced numerical analysis textbooks.
x = 1.27647 y = f(x) = 0.00002
x = 2.20
x = 2.22515 y = f(x) = 0.00020
x = 1.30
x = 1.30
y = f(x) = x7 – 6x6 + 5x5 – 4x4 + 3x3 – 2x2 + x – 15
4 ? x ? 6
chapter 2 root approximations
2-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 2.9. model spreadsheet for finding real roots of polynomials.
we save the spreadsheet of figure 2.9 with a name, say template.xls. then, we save it with a different
name, say example_2_6.xls, and in b16 we type the formula
=a16-($a$7*a16^7+$b$7*a16^6+$c$7*a16^5+$d$7*a16^4
+$e$7*a16^3+$f$7*a16^2+$g$7*a16^1+$h$7)/
($b$12*a16^6+$c$12*a16^5+$d$12*a16^4+$e$12*a16^3
+$f$12*a16^2+$g$12*a16^1+$h$12)
the use of the dollar sign ($) is explained in paragraph 4 below.
the formula in b16 of figure 2.10, is the familiar newton’s formula which also appears in row
14. we observe that b16 now displays #div/0! (this is a warning that some value is being
divided by zero), but this will change once we enter the polynomial coefficients, and the coefficients
of the first derivative.
2. since we are told that one real root is between 4 and 6, we take the average 5 and we enter it in
a16. this value is our first (initial) approximation. we also enter the polynomial coefficients,
and the coefficients of the first derivative in rows 7 and 12 respectively.
3. next, we copy b16 to c16:f16 and the spreadsheet now appears as shown in the spreadsheet
of figure 2.10. we observe that there is no change in the values of e16 and f16 therefore, we
terminate the approximation steps there.
123456789
10
11
12
13
14
15
16
a b c d e f g h
spreadsheet for finding approximations of the real roots of polynomials
up the 7th power by newton s method.
powers of x and corresponding coefficients of given polynomial p(x)
enter coefficients of p(x) in row 7
x7 x6 x5 x4 x3 x2 x constant
coefficients of the derivative p (x)
enter coefficients of p (x) in row 12
x6 x5 x4 x3 x2 x constant
approximations: xn+1 = xn ? p(xn)/p (xn)
initial (x0) 1st (x1) 2nd (x2) 3rd (x3
) 4th (x4) 5th (x5
) 6th (x6) 7th (x7)
numerical analysis using matlab and spreadsheets, second edition 2-19
orchard publications
the bisection method for root approximation
figure 2.10. spreadsheet for example 2.6.
4. all cells in the formula of b16, except a16, have dollar signs ($) in front of the column letter,
and in front of the row number. these cells are said to be absolute. the value of an absolute
cell does not change when it is copied from one position to another. a cell that is not absolute
is said to be relative cell. thus, b16 is a relative cell, and $b$16 is an absolute cell. the contents
of a relative cell changes when it is copied from one location to another. we can easily
convert a relative cell to absolute or vice versa, by first placing the cursor in front, at the end,
or between the letters and numbers of the cell, then, we press the function key f4. in this
example, we made all cells, except a16, absolute so that the formula of b16 can be copied to
c16, d16 and so on, without changing its value. the relative cell a16, when copied to the
next column, changes to b16, when copied to the next column to the right, changes to c16,
and so on.
we can now use this template with any other polynomial by just entering the coefficients of the
new polynomial in row 7 and the coefficients of its derivative in row 12 then, we observe the
successive approximations in row 16.
2.3 the bisection method for root approximation
the bisection (or interval halving) method is an algorithm* for locating the real roots of a function.
* this is a step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a
problem in a finite number of steps.
123456789
10
11
12
13
14
15
16
a b c d e f g h
spreadsheet for finding approximations of the real roots of polynomials
up the 7th power by newton s method.
powers of x and corresponding coefficients of given polynomial p(x)
enter coefficients of p(x) in row 7
x7 x6 x5 x4 x3 x2 x constant
1 -6 5 -4 3 -2 1 -15
coefficients of the derivative p (x)
enter coefficients of p (x) in row 12
x6 x5 x4 x3 x2 x constant
7 -36 25 -16 9 -4 1
approximations: xn+1 = xn ? p(xn)/p (xn)
initial (x0) 1st (x1) 2nd (x2) 3rd (x3
) 4th (x4) 5th (x5) 6th (x6) 7th (x7)
5.0 5.20409 5.16507 5.163194 5.163190 5.163190
chapter 2 root approximations
2-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the objective is to find two values of x, say and , so that and have opposite signs,
that is, either and , or and . if any of these two conditions is
satisfied, we can compute the midpoint xm of the interval with
(2.18)
knowing , we can find . then, the following decisions are made:
1. if and have the same sign, their product will be positive, that is, .
this indicates that and are on the left side of the x-axis crossing as shown in figure 2.11.
in this case, we replace with .
figure 2.11. sketches to illustrate the bisection method when and have same sign
2. if and have opposite signs, their product will be negative, that is, .
this indicates that and are on the right side of the x-axis crossing as in figure 2.12. in
this case, we replace with .
figure 2.12. sketches to illustrate the bisection method when and have opposite signs
after making the appropriate substitution, the above process is repeated until the root we are
seeking has a specified tolerance. to terminate the iterations, we either:
a. specify a number of iterations
b. specify a tolerance on the error of
x1 x2 f(x1) f(x2)
f(x1) > 0 f(x2) < 0 f(x1) < 0 f(x2) > 0
x1 ? x ? x2
xm
x1 + x2
2
=
xm f(xm)
f(xm) f(x1) f(xm) ? f(x1) > 0
xm x1
x1 xm
• • •
are
both positive and thus
• • •
their product is positive
both negative and thus
their product is positive
f(x1) and f(xm) f x are f(x1) and ( m)
x1 xm x2 x1 xm x2
f(x1) f(xm)
f(xm) f(x1) f(xm) ? f(x1) < 0
xm x2
x2 xm
• • •
opposite signs and thus
• • •
their product is negative
opposite signs and thus
their product is negative
f(x1) and f(xm) have f(x1) and f(xm) have
x1 xm x2 x1 xm x2
f(x1) f(xm)
f(x)
numerical analysis using matlab and spreadsheets, second edition 2-21
orchard publications
the bisection method for root approximation
we will illustrate the bisection method with examples using both matlab and excel.
example 2.7
use the bisection method with matlab to approximate one of the roots of
(2.19)
by
a. by specifying 16 iterations, and using a for end loop matlab program
b. by specifying 0.00001 tolerance for , and using a while end loop matlab program
solution:
this is the same polynomial as in example 2.4.
a. the for end loop allows a group of functions to be repeated a fixed and predetermined number
of times. the syntax is:
for x = array
commands...
end
before we write the program code, we must define a function assigned to the given polynomial
and save it as a function m-file. we will define this function as funcbisect01 and will save it as
funcbisect01.m.
function y= funcbisect01(x)
y = 3 .* x .^ 5 ? 2 .* x .^ 3 + 6 .* x ? 8
% we must not forget to type the semicolon at the end of the line above
% otherwise our code will fill the screen with values of y
on the code below, the statement for k = 1:16 says for , evaluate all
commands down to the end command. after the iteration, the loop ends and any
commands after the end are computed and displayed as commanded.
let us also review the meaning of the fprintf( %9.6f %13.6f \n , xm,fm) line. here, %9.6f and
%13.6f are referred to as format specifiers or format codes the first specifies that the value of xm
must be expressed in decimal format also called fixed point format, with a total of 9 digits, 6 of
which will be to the right of the decimal point. likewise, fm must be expressed in decimal format
with a total of 13 digits, 6 of which will be to the right of the decimal point. some other
specifiers are %e for scientific format, %s for string format, and %d for integer format. for
more information, we can type help fprintf. the special format \n specifies a linefeed, that is, it
prints everything specified up to that point and starts a new line. we will discuss other special
formats as they appear in subsequent examples.
the code for the first part of example 2.7 is given below.
y = f(x) = 3x5 – 2x3 + 6x – 8
f(x)
k = 1, k = 2, …, k = 16
k = 16
chapter 2 root approximations
2-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
x1=1 x2=2 % we know this interval from example 2.4, figure 2.6
disp( xm fm ) % xm is the average of x1 and x2, fm is f(xm)
disp( - ) % insert line under xm and fm
for k=1:16
f1=funcbisect01(x1) f2=funcbisect01(x2)
xm=(x1+x2) / 2 fm=funcbisect01(xm)
fprintf( %9.6f %13.6f \n , xm,fm) % prints xm and fm on same line
if (f1*fm<0)
x2=xm
else
x1=xm
end
end
when this program is executed, matlab displays the following:
xm fm
-
1.500000 17.031250
1.250000 4.749023
1.125000 1.308441
1.062500 0.038318
1.031250 -0.506944
1.046875 -0.241184
1.054688 -0.103195
1.058594 -0.032885
1.060547 0.002604
1.059570 -0.015168
1.060059 -0.006289
1.060303 -0.001844
1.060425 0.000380
1.060364 -0.000732
1.060394 -0.000176
1.060410 0.000102
we observe that the values are displayed with 6 decimal places as we specified, but for the
integer part unnecessary leading zeros are not displayed.
b. the while end loop evaluates a group of commands an indefinite number of times. the syntax
is:
while expression
commands...
end
numerical analysis using matlab and spreadsheets, second edition 2-23
orchard publications
the bisection method for root approximation
the commands between while and end are executed as long as all elements in expression are
true. the code should be written so that eventually a false condition is reached and the loop
then terminates.
there is no need to create another function m-file we will use the same as in part a. now we
type and execute the following while end loop program.
x1=1 x2=2 tol=0.00001
disp( xm fm ) disp( - )
while (abs(x1-x2)>2*tol)
f1=funcbisect01(x1) f2=funcbisect01(x2) xm=(x1+x2)/2
fm=funcbisect01(xm)
fprintf( %9.6f %13.6f \n , xm,fm)
if (f1*fm<0)
x2=xm
else
x1=xm
end
end
when this program is executed, matlab displays the following:
xm fm
-
1.500000 17.031250
1.250000 4.749023
1.125000 1.308441
1.062500 0.038318
1.031250 -0.506944
1.046875 -0.241184
1.054688 -0.103195
1.058594 -0.032885
1.060547 0.002604
1.059570 -0.015168
1.060059 -0.006289
1.060303 -0.001844
1.060425 0.000380
1.060364 -0.000732
1.060394 -0.000176
1.060410 0.000102
1.060402 -0.000037
1.060406 0.000032
1.060404 -0.000003
chapter 2 root approximations
2-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
next, we will use an excel spreadsheet to construct a template that approximates a real root of a
function with the bisection method. this requires repeated use of the if function which has the
following syntax.
=if(logical_test,value_if_true,value_if_false)
where
logical_test: any value or expression that can be evaluated to true or false.
value_if_true: the value that is returned if logical_test is true.
if logical_test is true and value_if_true is omitted, true is returned. value_if_true can be another
formula.
value_if_false is the value that is returned if logical_test is false. if logical_test is false and
value_if_false is omitted, false is returned. value_if_false can be another formula.
these statements may be clarified with the following examples.
=if(c11>=1500,a15, b15):if the value in c11 is greater than or equal to 1500, use the value in
a15 otherwise use the value in b15.
=if(d22800 otherwise assign the number 1200.
=if(m8<>n17, k7*12, l8/24):if the value in m8 is not equal to the value in n17, use the value in
k7 multiplied by 12 otherwise use the value in l8 divided by 24.
example 2.8
use the bisection method with an excel spreadsheet to approximate the value of within
0.00001 accuracy.
solution:
finding the square root of 5 is equivalent to finding the roots of . we expect the positive
root to be in the interval so we assign and . the average of these values
is . we will create a template as we did in example 2.6 so we can use it with any
polynomial equation. we start with a blank spreadsheet and we make the entries in rows 1
through 12 as shown in figure 2.13.
now, we make the following entries in rows 13 and 14.
a13: 2
b13: 3
c13: =(a13+b13)/2
5
x2 – 5 = 0
2 < x < 3 x1 = 2 x2 = 3
xm = 2.5
numerical analysis using matlab and spreadsheets, second edition 2-25
orchard publications
the bisection method for root approximation
figure 2.13. partial spreadsheet for example 2.8
d13: =$a$9*a13^7+$b$9*a13^6+$c$9*a13^5+$d$9*a13^4
+$e$9*a13^3+$f$9*a13^2+$g$9*a13^1+$h$9*a13^0
e13: =$a$9*c13^7+$b$9*c13^6+$c$9*c13^5+$d$9*c13^4
+$e$9*c13^3+$f$9*c13^2+$g$9*c13^1+$h$9*c13^0
f13: =d13*e13
a14: =if(a14=a13, c13, b13)
b14: =if(a14=a13, c13, b13)
we copy c13 into c14 and we verify that c14: =(a14+b14)/2
next, we highlight d13:f13 and on the edit menu we click on copy. we place the cursor on d14
and from the edit menu we click on paste. we verify that the numbers on d14:f14 are as shown
on the spreadsheet of figure 2.14. finally, we highlight a14:f14, from the edit menu we click on
copy, we place the cursor on a15, and holding the mouse left button, we highlight the range
a15:a30. then, from the edit menu, we click on paste and we observe the values in a15:f30.
the square root of 5 accurate to six decimal places is shown on c30 in the spreadsheet of figure
2.14.
123456789
10
11
12
a b c d e f g h
spreadsheet for finding approximations of the real roots
of polynomials using the bisection method
equation: y = f(x) = x2 ? 5 = 0
powers of x and corresponding coefficients of given polynomial f(x)
enter coefficients of f(x) in row 9
x7 x6 x5 x4 x3 x2 x constant
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 0 -5
x1 x2 xm f(x1) f(xm) f(x1)f(xm)
(x1+x2)/2
chapter 2 root approximations
2-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 2.14. entire spreadsheet for example 2.8
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
a b c d e f g h
spreadsheet for finding approximations of the real roots
of polynomials using the bisection method
equation: y = f(x) = x2 ? 5 = 0
powers of x and corresponding coefficients of given polynomial f(x)
enter coefficients of f(x) in row 9
x7 x6 x5 x4 x3 x2 x constant
0.00000 0.00000 0.00000 0.00000 0.00000 1.00000 0 -5
x1 x2 xm f(x1) f(xm) f(x1)f(xm)
(x1+x2)/2
2.00000 3.00000 2.50000 -1.00000 1.25000 -1.25000
2.00000 2.50000 2.25000 -1.00000 0.06250 -0.06250
2.00000 2.25000 2.12500 -1.00000 -0.48438 0.48438
2.12500 2.25000 2.18750 -0.48438 -0.21484 0.10406
2.18750 2.25000 2.21875 -0.21484 -0.07715 0.01657
2.21875 2.25000 2.23438 -0.07715 -0.00757 0.00058
2.23438 2.25000 2.24219 -0.00757 0.02740 -0.00021
2.23438 2.24219 2.23828 -0.00757 0.00990 -0.00007
2.23438 2.23828 2.23633 -0.00757 0.00116 -0.00001
2.23438 2.23633 2.23535 -0.00757 -0.00320 0.00002
2.23535 2.23633 2.23584 -0.00320 -0.00102 0.00000
2.23584 2.23633 2.23608 -0.00102 0.00007 0.00000
2.23584 2.23608 2.23596 -0.00102 -0.00047 0.00000
2.23596 2.23608 2.23602 -0.00047 -0.00020 0.00000
2.23602 2.23608 2.23605 -0.00020 -0.00006 0.00000
2.23605 2.23608 2.23607 -0.00006 0.00000 0.00000
2.23605 2.23607 2.23606 -0.00006 -0.00003 0.00000
2.23606 2.23607 2.23606 -0.00003 -0.00001 0.00000
numerical analysis using matlab and spreadsheets, second edition 2-27
orchard publications
summary
2.4 summary
• newton’s (or newton-raphson) method can be used to approximate the roots of any linear or
non-linear equation of any degree. it uses the formula
to apply newton’s method, we must start with a reasonable approximation of the root value.
in all cases, this can best be done by plotting versus .
• we can use a spreadsheet to approximate the real roots of linear and non-linear equations but
to approximate all roots (real and complex conjugates) it is advisable to use matlab.
• the matlab the while end loop evaluates a group of statements an indefinite number of
times and thus can be effectively used for root approximation.
• for approximating real roots we can use excel’s goal seek feature. we use goal seek when
we know the desired result of a single formula, but we do not know the input value which satisfies
that result. thus, if we have the function , we can use goal seek to set the
dependent variable to the desired value (goal) and from it, find the value of the independent
variable which satisfies that goal.
• for repetitive tasks, such as finding the roots of polynomials, it is prudent to construct a template
(model spreadsheet) with the appropriate formulas and then enter the coefficients of the
polynomial to find its real roots.
• the bisection (or interval halving) method is an algorithm for locating the real roots of a
function. the objective is to find two values of x, say and , so that and have
opposite signs, that is, either and , or and . if any of these
two conditions is satisfied, we can compute the midpoint xm of the interval with
• we can use the bisection method with matlab to approximate one of the roots by specifying
a number of iterations using a for end or by specifying a tolerance using a while end loop
program.
• we can use an excel spreadsheet to construct a template that approximates a real root of a
function with the bisection method. this requires repeated use of the if function which has
the =if(logical_test,value_if_true,value_if_false)
xn + 1 xn
f(xn)
f (xn)
= –
f(x) x
y = f(x)
y
x
x1 x2 f(x1) f(x2)
f(x1) > 0 f(x2) < 0 f(x1) < 0 f(x2) > 0
x1 ? x ? x2
xm
x1 + x2
2
=
chapter 2 root approximations
2-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
2.5 exercises
1. use matlab to sketch the graph for each of the following functions, and verify
from the graph that and , where and defined below, have opposite signs. then,
use newton’s method to estimate the root of that lies between and .
a.
b.
hint: start with
2. repeat exercise 1 above using the bisection method.
3. repeat example 2.5 using matlab.
hint: use the procedure of example 2.2
y = f(x)
f(a) f(b) a b
f(x) = 0 a b
f1(x) = x4 + x – 3 a = 1 b = 2
f2(x) = 2x + 1 – x + 4 a = 2 b = 4
x0 = (a + b) ? 2
numerical analysis using matlab and spreadsheets, second edition 2-29
orchard publications
solutions to exercises
2.6 solutions to exercises
1.
a.
x=?2:0.05:2 f1x=x.^4+x?3 plot(x,f1x) grid
from the plot above we see that the positive root lies between and so we
choose and so we take as our first approximation. we compute
the next value as
the second approximation yields
check with matlab:
pa=[1 0 0 1 ?3] roots(pa)
ans =
-1.4526
0.1443 + 1.3241i
0.1443 - 1.3241i
1.1640
x = 1 x = 1.25
a = 1 b = 1.25 x0 = 1.1
x1
x1 x0
f(x0)
f (x0)
– 1.1 (1.1)4 + 1.1 – 3
4(1.1)3 + 1
– - 1.1 (–0.436)
6.324
= = = – - = 1.169
x2 x1
f(x1)
f (x1)
– 1.169 (1.169)4 + 1.169 – 3
4(1.169)3 + 1
– - 1.169 0.0365
7.39
= = = – = 1.164
chapter 2 root approximations
2-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
b.
x=?5:0.05:5 f2x=sqrt(2.*x+1)-sqrt(x+4) plot(x,f2x) grid
warning: imaginary parts of complex x and/or y arguments ignored.
from the plot above we see that the positive root is very close to and so we take
as our first approximation. to compute the next value we first need to find the
first derivative of . we rewrite it as
then,
and
thus, the real root is exactly . we also observe that since ,
there was no need to find the first derivative .
check with matlab:
syms x f2x=sqrt(2.*x+1)-sqrt(x+4) solve(f2x)
ans =
3
x = 3
x0 = 3 x1
f2(x)
f2(x) 2x + 1 – x + 4 (2x + 1)1 ? 2 (x + 4)1 ? 2 = = –
d
dx
- f2 x ( ) ? 12
(2x + 1)–1 ? 2 2 ? ? 12
(x + 4)–1 ? 2 – ? ? 1 1
2x + 1
- 1
2 x + 4
= = – -
x1 x0
f(x0)
f (x0)
– 3 2 × 3 + 1 – 3 + 4
1 ? 7 – 1 ? (2 7)
– 3 0
1 ? (2 7)
= = = – = 3
x = 3 f(x0) = 7 – 7 = 0
f (x0)
numerical analysis using matlab and spreadsheets, second edition 2-31
orchard publications
solutions to exercises
2.
a. we will use the for end loop matlab program and specify 12 iterations. before we write
the program code, we must define a function assigned to the given polynomial and save it
as a function m-file. we will define this function as exercise2 and will save it as
exercise2.m
function y= exercise2(x)
y = x .^ 4 +x ? 3
after saving this file as exercise2.m, we execute the following program:
x1=1 x2=2 % x1=a and x2=b
disp( xm fm ) % xm is the average of x1 and x2, fm is f(xm)
disp( - ) % insert line under xm and fm
for k=1:12
f1=exercise2(x1) f2=exercise2(x2)
xm=(x1+x2) / 2 fm=exercise2(xm)
fprintf( %9.6f %13.6f \n , xm,fm)% prints xm and fm on same line
if (f1*fm<0)
x2=xm
else
x1=xm
end
end
matlab displays the following:
xm fm
-
1.500000 3.562500
1.250000 0.691406
1.125000 -0.273193
1.187500 0.176041
1.156250 -0.056411
1.171875 0.057803
1.164063 0.000200
1.160156 -0.028229
1.162109 -0.014045
1.163086 -0.006930
1.163574 -0.003367
1.163818 -0.001584
b. we will use the while end loop matlab program and specify a tolerance of 0.00001.
we need to redefine the function m-file because the function in part (b) is not the same as
in part a.
chapter 2 root approximations
2-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
function y= exercise2(x)
y = sqrt(2.*x+1)?sqrt(x+4)
after saving this file as exercise2.m, we execute the following program:
x1=2.1 x2=4.3 tol=0.00001 % if we specify x1=a=2 and x2=b=4, the program
% will not display any values because xm=(x1+x2)/2 = 3 = answer
disp( xm fm ) disp( - )
while (abs(x1-x2)>2*tol)
f1=exercise2(x1) f2=exercise2(x2) xm=(x1+x2)/2
fm=exercise2(xm)
fprintf( %9.6f %13.6f \n , xm,fm)
if (f1*fm<0)
x2=xm
else
x1=xm
end
end
when this program is executed, matlab displays the following:
xm fm
-
3.200000 0.037013
2.650000 -0.068779
2.925000 -0.014289
3.062500 0.011733
2.993750 -0.001182
3.028125 0.005299
3.010938 0.002065
3.002344 0.000443
2.998047 -0.000369
3.000195 0.000037
2.999121 -0.000166
2.999658 -0.000065
2.999927 -0.000014
3.000061 0.000012
2.999994 -0.000001
3.000027 0.000005
3.000011 0.000002
3.
from example 2.5,
y = f(x) = cos2x + sin2x + x – 1
numerical analysis using matlab and spreadsheets, second edition 2-33
orchard publications
solutions to exercises
we use the following code to plot this function.
x=?5:0.02:5 y=cos(2.*x)+sin(2.*x)+x?1 plot(x,y) grid
let us find out what a symbolic solution gives.
syms x y=cos(2*x)+sin(2*x)+x?1 solve(y)
ans =
[0]
[2]
the first value (0) is correct as it can be seen from the plot above and also verified by substitution
of into the given function. the second value (2) is not exactly correct as we can
see from the plot. this is because when solving equations of periodic functions, there are an
infinite number of solutions and matlab restricts its search for solutions to a limited range
near zero and returns a non-unique subset of solutions.
to find a good approximation of the second root that lies between and , we write
and save the function files exercise3 and exercise3der as defined below.
function y=exercise3(x)
% finding roots by newton s method using matlab
y=cos(2.*x)+sin(2.*x)+x?1
function y=exercise3der(x)
% finding roots by newton s method
% the following is the first derivative of
% the function defined as exercise3
y=?2.*sin(2.*x)+2.*cos(2.*x)+1
x = 0
x = 2 x = 3
chapter 2 root approximations
2-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
now, we write and execute the following program and we find that the second root is
and this is consistent with the value shown on the plot.
x = input( enter starting value: )
fx = exercise3(x)
fprimex = exercise3der(x)
xnext = x-fx/fprimex
x = xnext
fx = exercise3(x)
fprimex = exercise3der(x)
disp(sprintf( first approximation is x = %9.6f \n , x))
while input( next approximation? (=no,1=yes) )
xnext=x-fx/fprimex
x=xnext
fx=exercise3(x)
fprimex=exercise3der(x)
disp(sprintf( next approximation is x = %9.6f \n , x))
end
disp(sprintf( %9.6f \n , x))
enter starting value: 3
first approximation is x = 2.229485
x = 2.2295
numerical analysis using matlab and spreadsheets, second edition 3-1
orchard publications
chapter 3
sinusoids and phasors
his chapter is an introduction to alternating current waveforms. the characteristics of sinusoids
are discussed and the frequency, phase angle, and period are defined. voltage and current
relationships are expressed in sinusoidal terms. phasors which are rotating vectors in
terms of complex numbers are also introduced and their relationships to sinusoids are derived.
3.1 alternating voltages and currents
the waveforms shown in figure 3.1 may represent alternating currents or voltages.
figure 3.1. examples of alternating voltages and currents
t
fundamental and harmonic
sine waveforms
time
voltage or current
t
sine waveform
time
voltage or current
t
t
square waveform
time
voltage or current
t
saw-tooth waveform
time
voltage or current
chapter 3 sinusoids and phasors
3-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
thus an alternating current (ac) is defined as a periodic current whose average value over a period
is zero. stated differently, an alternating current alternates between positive and negative values
at regularly recurring intervals of time. also, the average of the positive and negative values over a
period is zero.
as shown in figure 3.1, the period t of an alternating current or voltage is the smallest value of
time which separates recurring values of the alternating waveform.
unless otherwise stated, our subsequent discussion will be restricted to sine or cosine waveforms
and these are referred to as sinusoids. two main reasons for studying sinusoids are: (1) many physical
phenomena such as electric machinery produce (nearly) sinusoidal voltages and currents and
(2) by fourier analysis, any periodic waveform which is not a sinusoid, such as the square and sawtooth
waveforms on the previous page, can be represented by a sum of sinusoids.
3.2 characteristics of sinusoids
consider the sine waveform shown in figure 3.2, where may represent either a voltage or a
current function, and let where is the amplitude of this function. a sinusoid (sine
or cosine function) can be constructed graphically from the unit circle, which is a circle with radius
of one unit, that is, as shown, or any other unit. thus, if we let the phasor (rotating vector)
travel around the unit circle with an angular velocity , the and functions are generated
from the projections of the phasor on the horizontal and vertical axis respectively. we
observe that when the phasor has completed a cycle (one revolution), it has traveled radians or
degrees, and then repeats itself to form another cycle.
figure 3.2. generation of a sinusoid by rotation of a phasor
at the completion of one cycle, (one period), and since is the angular velocity, commonly
known as angular or radian frequency, then
f (t)
f(t) = asint a
a = 1
? cos?t sin?t
2?
360°
sine waveform
time
voltage or current
0
a
?a
?/2
? 3?/2 2?
phasor
direction
of rotation
?(180°) 0 (0°)
? ? 2 (90°)
3? ? 2 (270°)
a = 1 2?(360°)
?
f (t)
t = t ?
numerical analysis using matlab and spreadsheets, second edition 3-3
orchard publications
characteristics of sinusoids
(3.1)
the term frequency in hertz, denoted as , is used to express the number of cycles per second.
thus, if it takes one second to complete one cycle (one revolution around the unit circle), we say
that the frequency is or one cycle per second.
the frequency is denoted by the letter and in terms of the period and (3.1) we have
(3.2)
the frequency is often referred to as the cyclic frequency to distinguish it from the radian frequency
.
since the cosine and sine functions are usually known in terms of degrees or radians, it is convenient
to plot sinusoids versus (radians) rather that time . for example, ,
and are plotted as shown in figure 3.3.
figure 3.3. plot of the cosine and sine functions
by comparing the sinusoidal waveforms of figure 3.3, we see that the cosine function will be the
same as the sine function if the latter is shifted to the left by radians, or . thus, we say
that the cosine function leads (is ahead of) the sine function by radians or . likewise, if we
shift the cosine function to the right by radians or , we obtain the sine waveform in this
case, we say that the sine function lags (is behind) the cosine function by radians or .
another common expression is that the cosine and sine functions are out-of-phase by , or there is
a phase angle of between the cosine and sine functions. it is possible, of course, that two sinusoids
are out-of-phase by a phase angle other than . figure 3.4 shows three sinusoids which are outof-
phase. if the phase angle between them is degrees, the two sinusoids are said to be in-phase.
?t = 2? or t 2?
?
=
hz
1 hz
f t
f 1t
= - or ? = 2?f
f
?
?t t v(t) = vmax cos?t
i(t) = imax sin?t
0
imax
?/2
? 3?/2
2?
?imax
imaxsin?t
?t (r)
i(t)
t
0
vmaxx
?/2 ?
3?/2
2?
?vmax
vmaxcos?t
?t (r)
v(t)
t
? ? 2 90°
? ? 2 90°
? ? 2 90°
? ? 2 90°
90°
90°
90°

chapter 3 sinusoids and phasors
3-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 3.4. out-of-phase sinusoids
we must remember that when we say that one sinusoid leads or lags another sinusoid, these are of
the same frequency. obviously, two sinusoids of different frequencies can never be in phase.
it is convenient to express the phase angle in degrees rather than in radians in a sinusoidal function.
for example, it is acceptable to express
as
since the subtraction inside the parentheses needs not to be performed.
when two sinusoids are to be compared in terms of their phase difference, these must first be written
either both as cosine functions, or both as sine functions, and should also be written with positive
amplitudes. we should remember also that a negative amplitude implies phase shift.
example 3.1
find the phase difference between the sinusoids
and
solution:
we recall that the minus (?) sign indicates a phase shift, and that the sine function lags the
cosine by . then,
0
v(t)
out-of-phase sinusoids
v(t)
v1maxsin?t
? ?
v2maxsin(?t+?) leads v1maxsin?t by ? deg.
v3maxsin(?t??) lags v1maxsin?t by ? deg.
?t
v(t) = 100sin(2000?t – ? ? 6)
v(t) = 100sin(2000?t – 30°)
180°
i1 120 100?t 30°
= cos( – )
i2 6 – 100?t 30°
= sin( – )
±180°
90°
–sinx = sin(x ± 180°) and sinx = cos(x – 90°)
numerical analysis using matlab and spreadsheets, second edition 3-5
orchard publications
characteristics of sinusoids
and
and comparing with , we see that leads by , or lags by .
in our subsequent discussion, we will be using several trigonometric identities, derivatives and
integrals involving trigonometric functions. we, therefore, provide the following relations and
formulas for quick reference. let us also review the definition of a radian and its relationship to
degrees with the aid of figure 3.5.
figure 3.5. definition of radian
as shown in figure 3.5, the radian is a circular angle subtended by an arc equal in length to the
radius of the circle, whose radius is units in length. the circumference of a circle is units
therefore, there are or radians in degrees. then,
(3.3)
the angular velocity is expressed in radians per second, and it is denoted by the symbol . then,
a rotating vector that completes revolutions per second, has an angular velocity radians
per second.
some useful trigonometric relations are given below for quick reference.
(3.4)
(3.5)
(3.6)
(3.7)
i2 6 100?t 210°
– ( ) sin 6 100?t 150°
= = sin( + )
6 100?t 150° 90° – + ( ) cos 6 100?t 60°
= = cos( + )
i2 i1 i2 i1 90° i1 i2 90°
r
r
? radians
1 radian = 57.3 deg
r 2?r
2? 6.283… 360°
1 radian 360°
2?
= - ? 57.3°
?
n ? = 2?n
cos0° = cos360° = cos2? = 1
30° cos ?6
cos 3
2
= = = 0.866
45° cos ?4
cos 2
2
= = = 0.707
60° cos ?3
cos 12
= = = 0.5
chapter 3 sinusoids and phasors
3-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(3.8)
(3.9)
(3.10)
(3.11)
(3.12)
(3.13)
(3.14)
(3.15)
(3.16)
(3.17)
(3.18)
(3.19)
(3.20)
(3.21)
(3.22)
(3.23)
90° cos ?2
= cos = 0
cos120° 2?
3
cos –1
2
= = - = –0.5
cos150° 5?
6
cos – 3
2
= = = –0.866
cos180° = cos? = –1
cos210° 7?
6
cos – 3
2
= = = –0.866
cos225° 5?
4
cos – 2
2
= = = –0.707
cos240° 4?
3
cos –1
2
= = - = –0.5
cos270° 3?
2
= cos = 0
cos300° 5?
3
= cos = 0.5
cos330° 11?
6
= cos - = 0.866
sin0° = sin360° = sin2? = 0
30 sin ° ?6 sin 12
= = = 0.5
45 sin ° ?4
sin 2
2
= = = 0.707
60 sin ° ?3
sin 12
= = = 0.866
90 sin ° ?2
= sin = 1
sin120° 2?
3
sin 3
2
= = = 0.866
numerical analysis using matlab and spreadsheets, second edition 3-7
orchard publications
characteristics of sinusoids
(3.24)
(3.25)
(3.26)
(3.27)
(3.28)
(3.29)
(3.30)
(3.31)
(3.32)
(3.33)
(3.34)
(3.35)
(3.36)
(3.37)
(3.38)
(3.39)
(3.40)
(3.41)
(3.42)
sin150° 5?
6
sin 12
= = = 0.5
sin180° = sin? = 0
sin210° 7?
6
sin –1
2
= = - = –0.5
sin225° 5?
4
sin – 2
2
= = = –0.707
sin240° 4?
3
sin – 3
2
= = = –0.866
sin270° 3?
2
= sin = –1
sin300° 5?
3
sin – 3
2
= = = –0.866
sin330° 11?
6
sin - –1
2
= = - = –0.5
cos(–?) = cos?
cos(90° + ?) = –sin?
cos(180° – ?) = –cos?
sin(–?) = –sin?
sin(90° + ?) = cos?
sin(180° – ?) = sin?
tan? sin?
cos?
= -
cot? cos?
sin?
- 1
tan?
= = -
sec? 1
cos?
= -
csc? 1
sin?
=
tan(90° + ?) = –cot?
chapter 3 sinusoids and phasors
3-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(3.43)
(3.44)
(3.45)
(3.46)
(3.47)
(3.48)
(3.49)
(3.50)
(3.51)
(3.52)
(3.53)
(3.54)
(3.55)
(3.56)
(3.57)
(3.58)
(3.59)
tan(180° – ?) = –tan?
cos(? + ?) = cos?cos? – sin?sin?
cos(? – ?) = cos?cos? + sin?sin?
sin(? + ?) = sin?cos? + cos?sin?
sin(? – ?) = sin?cos?–cos?sin?
tan(? + ?) tan? + tan?
1 – tan?tan?
=
tan(? – ?) tan? – tan?
1 + tan?tan?
=
cos2? + sin2? = 1
cos2? = cos2? – sin2?
sin2? = 2sin?cos?
tan2? 2tan?
1 – tan2?
=
? 2 cos 12
= (1 + cos2?)
? 2 sin 12
= (1 – cos2?)
? ? cos cos 12
? ? + ( ) 12
= cos + cos(? – ?)
? ? sin cos 12 ? ? + ( ) 12
= sin – sin(? – ?)
? sin ? cos 12
? ? + ( ) 12
= sin + sin(? – ?)
? sin ? sin 12
? ? – ( ) cos 12
= – cos(? + ?)
numerical analysis using matlab and spreadsheets, second edition 3-9
orchard publications
characteristics of sinusoids
let figure 3.6 be any triangle.
figure 3.6. general triangle
then,
by the law of sines,
(3.60)
by the law of cosines,
(3.61)
(3.62)
(3.63)
and by the law of tangents,
(3.64)
the following differential and integral trigonometric and exponential functions, are used extensively
in engineering.
(3.65)
(3.66)
(3.67)
(3.68)
(3.69)
(3.70)
? ?
a ? b
c
a
sin?
- b
sin?
c
sin?
= =
a2 = b2 + c2 – 2bc cos?
b2 = a2 + c2 – 2ac cos?
c2 = a2 + b2 – 2abcos?
a – b
a + b

12
tan (? – ?)
12
tan (? + ?)
= b – c
b + c
-
12
tan (? – ?)
12
tan (? + ?)
= c – a
c + a
-
12tan (?

?)
12
tan (? + ?)
= -
dx
d ( sinv) cosvdv
dx
= -
dx
d ( cosv) –sinvdv
dx
= -
dx
d (ev) ev dv
dx
= -
? sinaxdx 1a
= – cosax + c
? cosaxdx 1a
= sinax + c
?eaxdx 1a
= eax + c
chapter 3 sinusoids and phasors
3-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.3 inverse trigonometric functions
the notation or is used to denote an angle whose cosine is . thus, if ,
then . similarly, if , then , and if , then .
these are called inverse trigonometric functions.
example 3.2
find the angle if
solution:
here, we want to find the angle ? given that its cosine is 0.5. from (3.7), . therefore,
3.4 phasors
in the language of mathematics, the square root of minus one is denoted as , that is, . in
the electrical engineering field, we denote as to avoid confusion with current . essentially, is
an operator that produces a counterclockwise rotation to any vector to which it is applied as a
multiplying factor. thus, if it is given that a vector has the direction along the right side of the
-axis as shown in figure 3.7, multiplication of this vector by the operator will result in a new
vector whose magnitude remains the same, but it has been rotated counterclockwise by .
also, another multiplication of the new vector by will produce another counterclockwise
direction. in this case, the vector has rotated and its new value now is . when this
vector is rotated by another for a total of , its value becomes . a fourth
rotation returns the vector to its original position, and thus its value is again . therefore, we
conclude that , , , and the rotating vector is referred to as a phasor.
note: in our subsequent discussion, we will designate the -axis (abscissa) as the real axis, and the
-axis (ordinate) as the imaginary axis with the understanding that the “imaginary” axis is just as
“real” as the real axis. in other words, the imaginary axis is just as important as the real axis.*
an imaginary number is the product of a real number, say , by the operator . thus, is a real
number and is an imaginary number.
* a more appropriate nomenclature for the real and imaginary axes would be the axis of the cosines and the axis of the sines
respectively.
cos–1y arc cosy y y = cosx
x = cos–1y w = sinv v = sin–1w z = tanu u = tan–1z
? cos–10.5 = ?
cos60° = 0.5
? = 60°
i i = –1
i j i j
90°
a
x j
ja 90°
ja j 90°
a 180° –a
90° 270° j(–a) = –ja 90°
a
j 2 = –1 j 3 = –j j 4 = 1 a
x
y
r j r
jr
numerical analysis using matlab and spreadsheets, second edition 3-11
orchard publications
addition and subtraction of phasors
figure 3.7. the j operator
a complex number is the sum (or difference) of a real number and an imaginary number. for
example, the number where and are both real numbers, is a complex number.
then, and where denotes real part of , and the
imaginary part of . when written as , it is said to be expressed in rectangular form.
since in engineering we use complex quantities as phasors, henceforth any complex number will
be referred to as a phasor.
by definition, two phasors and where and , are equal if and only if
their real parts are equal and also their imaginary parts are equal. thus, if and only if
and .
3.5 addition and subtraction of phasors
the sum of two phasors has a real component equal to the sum of the real components, and an
imaginary component equal to the sum of the imaginary components. for subtraction, we change
the signs of the components of the subtrahend and we perform addition. thus, if and
, then
and
example 3.3
it is given that , and . find and
solution:
x
y
ja
j(ja) = j2a = –a
j(–a) = j 3a = –ja
j(–ja) = –j2a = a
a
a = a + jb a b
a = re{a} b = im{a} re{a} a b = im{a}
a a = a + jb
a b a = a + jb b = c + jd
a = b
a = c b = d
a = a + jb
b = c + jd
a + b = (a + c) + j(b + d)
a – b = (a – c) + j(b – d)
a = 3 + j4 b = 4 – j2 a + b a – b
a + b = (3 + j4) + (4 – j2) = (3 + 4) + j(4 – 2) = 7 + j2
a – b = (3 + j4) – (4 – j2) = (3 – 4) + j(4 + 2) = – 1 + j6
chapter 3 sinusoids and phasors
3-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.6 multiplication of phasors
phasors are multiplied using the rules of elementary algebra, and making use of the fact that
. thus, if and , then
and since , it follows that
(3.71)
example 3.4
it is given that and . find
solution:
the conjugate of a phasor, denoted as , is another phasor with the same real component, and
with an imaginary component of opposite sign. thus, if , then .
example 3.5
it is given that . find
solution:
the conjugate of the phasor has the same real component, but the imaginary component has
opposite sign. then,
if a phasor is multiplied by its conjugate, the result is a real number. thus, if , then
example 3.6
it is given that . find
solution:
3.7 division of phasors
when performing division of phasors, it is desirable to obtain the quotient separated into a real
part and an imaginary part. this procedure is called rationalization of the quotient, and it is done by
multiplying the denominator by its conjugate. thus, if and , then,
j 2 = –1 a = a + jb b = c + jd
a ? b = (a + jb) ? (c + jd) = ac + jad + jbc + j2bd
j 2 = –1
a ? b = ac + jad + jbc–bd = (ac – bd) + j(ad + bc)
a = 3 + j4 b = 4 – j2 a ? b
a ? b = (3 + j4) ? (4 – j2) = 12 – j6 + j16 – j 28 = 20 + j10
a?
a = a + jb a? = a–jb
a = 3 + j5 a?
a
a? = 3–j5
a a = a + jb
a ? a? = (a + jb)(a – jb) = a2 – jab + jab – j 2b2 = a2 + b2
a = 3 + j5 a ? a?
a ? a? = (3 + j5)(3 – j5) = 32 + 52 = 9 + 25 = 34
a = a + jb b = c + jd
numerical analysis using matlab and spreadsheets, second edition 3-13
orchard publications
exponential and polar forms of phasors
(3.72)
in (3.72), we multiplied both the numerator and denominator by the conjugate of the denominator
to eliminate the operator from the denominator of the quotient. using this procedure, we
see that the quotient is easily separated into a real and an imaginary part.
example 3.7
it is given that , and . find
solution:
using the procedure of (3.72), we get
3.8 exponential and polar forms of phasors
the relations
(3.73)
and
(3.74)
are known as the euler’s identities.
multiplying (3.73) by the real positive constant c we get:
(3.75)
this expression represents a phasor, say , and thus
(3.76)
equating real and imaginary parts in (3.75) and (3.76), we get
(3.77)
squaring and adding the expressions in (3.77), we get
ab
- a + jb
c + jd
(a + jb)(c – jd)
(c + jd)(c – jd)
- ab
- b?
b?
? (ac + bd) + j(bc – ad)
c2 d 2 +
= = = =
(ac + bd)
c2 d 2 +
- j(bc – ad)
c2 d 2 +
= +
j
a = 3 + j4 b = 4 + j3 a ? b
ab
- 3 + j4
4 + j3
(3 + j4)(4 – j3)
(4 + j3)(4 – j3)
12 – j9 + j16 + 12
42 32 +
- 24 + j7
25
- 24
25
- j 7
25
= = = = = + - = 0.96 + j0.28
e j? = cos? + j sin?
e– j? = cos?–j sin?
ce j? = ccos? + jcsin?
a + jb
ce j? = a + jb
a = ccos? and b = csin?
chapter 3 sinusoids and phasors
3-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
then,
or
(3.78)
also, from (3.77)
or
(3.79)
therefore, to convert a phasor from rectangular to exponential form, we use the expression
(3.80)
to convert a phasor from exponential to rectangular form, we use the expressions
(3.81)
the polar form is essentially the same as the exponential form but the notation is different, that is,
(3.82)
where the left side of (3.82) is the exponential form, and the right side is the polar form.
we must remember that the phase angle is always measured with respect to the positive real axis, and
rotates in the counterclockwise direction.
example 3.8
convert the following phasors to exponential and polar forms:
a. b. c. d.
solution:
a. the real and imaginary components of this phasor are shown in figure 3.8.
a2 + b2 (ccos?)2 (csin?)2 = + = c2( cos2? + sin2?) = c2
c2 a2 = + b2
c a2 = + b2
ba
csin?
ccos?
= - = tan?
? ba
? ?
–1? ? = tan
a + jb a2 + b2e j tan 1 – ba
? ?
? ?
=
ce j? = ccos? + jcsin?
ce– j? = ccos?–jcsin?
ce j? = c??
?
3 + j4 – 1 + j2 – 2 – j 4 – j3
numerical analysis using matlab and spreadsheets, second edition 3-15
orchard publications
exponential and polar forms of phasors
figure 3.8. the components of
then,
check with matlab:
x=3+j*4 magx=abs(x) thetax=angle(x)*180/pi disp(magx) disp(thetax)
5
53.1301
b. the real and imaginary components of this phasor are shown in figure 3.9.
figure 3.9. the components of
then,
check with matlab:
y=?1+j*2 magy=abs(y) thetay=angle(y)*180/pi disp(magy) disp(thetay)
2.2361
116.5651
c. the real and imaginary components of this phasor are shown in figure 3.10.
then,
re
im
4
3
5
53.1°
3 + j4
3 + j4 32 + 42 e j (4 ? 3) – ? tan 5e j53.1° = = = 5?53.1°
re
im
2
?1
116.6°
63.4°
5
– 1 + j2
– 1 + j2 12 + 22e j (2 ? –1) – tan 5e j116.6° = = = 5?116.6°
–2–j1 22 + 12 e j (–1 ? –2) – ? tan 5e j206.6° 5?206.6° 5e j(–153.4)° = = = = = 5?–153.4°
chapter 3 sinusoids and phasors
3-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 3.10. the components of
check with matlab:
v=?2?j*1 magv=abs(v) thetav=angle(v)*180/pi disp(magv) disp(thetav)
2.2361
-153.4349
d. the real and imaginary components of this phasor are shown in figure 3.11.
figure 3.11. the components of
then,
check with matlab:
w=4?j*3 magw=abs(w) thetaw=angle(w)*180/pi disp(magw) disp(thetaw)
5
-36.8699
example 3.9
express the phasor in exponential and in rectangular forms.
solution:
we recall that . since each rotates a vector by counterclockwise, then is
the same as rotated counterclockwise by . therefore,
re
im
?2
?1
206.6°
?153.4°(measured
26.6°
5 clockwise)
– 2 – j
re
im
4
?3
5
323.1°
?36.9°
4 – j3
4–j3 42 + 32 e j (–3 ? 4) – ? tan 5e j323.1° 5?323.1° 5e –j36.9° = = = = = 5?–36.9°
–2?30°
–1 = j2 j 90° –2?30°
2?30° 180°
–2?30° = 2?(30° + 180°) = 2?210° = 2?–150°
numerical analysis using matlab and spreadsheets, second edition 3-17
orchard publications
exponential and polar forms of phasors
the components of this phasor are shown in figure 3.12.
figure 3.12. the components of
then,
note: the rectangular form is most useful when we add or subtract phasors however, the exponential
and polar forms are most convenient when we multiply or divide phasors.
to multiply two phasors in exponential (or polar) form, we multiply the magnitudes and we add
the phase angles, that is, if
then,
(3.83)
example 3.10
multiply by
solution:
multiplication in polar form yields
and multiplication in exponential form yields
to divide one phasor by another when both are expressed in exponential or polar form, we divide
the magnitude of the dividend by the magnitude of the divisor, and we subtract the phase angle of
the divisor from the phase angle of the dividend, that is, if
then,
(3.84)
re
im
?1.73
?1
210°
2
?150°(measured
30°
clockwise)
2?–150°
2?–150° 2e –j150°
= = 2( cos150° – j sin150°) = 2(– 0.866 – j0.5) = – 1.73 – j
a = m?? and b = n??
ab mn?(? + ?) me j?ne j? mne j(? + ?)
= = =
a = 10?53.1° b = 5?–36.9°
ab = (10 × 5)?[53.1° + (–36.9°)] = 50?16.2°
ab 10e j53.1° ( ) 5e –j36.9° ( ) 50e j (53.1° – 36.9°) 50e j16.2° = = =
a = m?? and b = n??
ab
- mn
?(? – ?) me j?
ne j? - mn
e j(? – ?)
= = =
chapter 3 sinusoids and phasors
3-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 3.11
divide by
solution:
division in polar form yields
division in exponential form yields
3.9 summary
• an alternating current (or voltage) alternates between positive and negative values at regularly
recurring intervals of time.
• the period t of an alternating current or voltage is the smallest value of time which separates
recurring values of the alternating waveform.
• sine and cosine waveforms and these are referred to as sinusoids.
• the angular velocity is commonly known as angular or radian frequency and
• the term frequency in hertz, denoted as , is used to express the number of cycles per second.
the frequency is denoted by the letter and in terms of the period , . the frequency
is often referred to as the cyclic frequency to distinguish it from the radian frequency
.
• the cosine function leads (is ahead of) the sine function by radians or , and the sine
function lags (is behind) the cosine function by radians or . alternately, we say that
the cosine and sine functions are out-of-phase by , or there is a phase angle of between
the cosine and sine functions.
• two (or more) sinusoids can be out-of-phase by a phase angle other than .
• it is important to remember that when we say that one sinusoid leads or lags another sinusoid,
these are of the same frequency since two sinusoids of different frequencies can never be in
phase.
• it is customary to express the phase angle in degrees rather than in radians in a sinusoidal function.
for example, we write as
a = 10?53.1° b = 5?–36.9°
ab
- 10?53.1°
5?–36.9°
= = 2?[53.1° – (–36.9°)] = 2?90°
ab
- 10e j53.1°
5e–j36.9° - 2e j53.1°e j36.9° 2e j90° = = =
? ?t = 2?
hz
f t f = 1 ? t
f
?
? ? 2 90°
? ? 2 90°
90° 90°
90°
v(t) = 100sin(2000?t – ? ? 6) v(t) = 100sin(2000?t – 30°)
numerical analysis using matlab and spreadsheets, second edition 3-19
orchard publications
summary
• when two sinusoids are to be compared in terms of their phase difference, these must first be
written either both as cosine functions, or both as sine functions, and should also be written
with positive amplitudes.
• a negative amplitude implies phase shift.
• the radian is a circular angle subtended by an arc equal in length to the radius of the circle,
whose radius is units in length. the circumference of a circle is .
• the notation or is used to denote an angle whose cosine is . thus, if
, then . these are called inverse trigonometric functions.
• a phasor is a rotating vector expressed as a complex number where is an operator that
rotates a vector by in a counterclockwise direction.
• two phasors and where and , are equal if and only if their real
parts are equal and also their imaginary parts are equal. thus, if and only if and
.
• the sum of two phasors has a real component equal to the sum of the real components, and an
imaginary component equal to the sum of the imaginary components. for subtraction, we
change the signs of the components of the subtrahend and we perform addition. thus, if
and , then and
• phasors are multiplied using the rules of elementary algebra. if and ,
then
• the conjugate of a phasor, denoted as , is another phasor with the same real component,
and with an imaginary component of opposite sign. thus, if , then .
• when performing division of phasors, it is desirable to obtain the quotient separated into a
real part and an imaginary part. this is achieved by multiplying the denominator by its conjugate.
thus, if and , then,
• the relations and are known as the euler’s identities.
• to convert a phasor from rectangular to exponential form, we use the expression
180°
r 2?r
cos–1y arc cosy y
y = cosx x = cos–1y
j
90°
a b a = a + jb b = c + jd
a = b a = c
b = d
a = a + jb b = c + jd a + b = (a + c) + j(b + d) a – b = (a – c) + j(b – d)
a = a + jb b = c + jd
a ? b = ac + jad + jbc–bd = (ac – bd) + j(ad + bc)
a?
a = a + jb a? = a–jb
a = a + jb b = c + jd
ab
- a + jb
c + jd
(a + jb)(c – jd)
(c + jd)(c – jd)
- (ac + bd) + j(bc – ad)
c2 d 2 +
(ac + bd)
c2 d 2 +
- j(bc – ad)
c2 d 2 +
= = = = +
e j? = cos? + j sin? e– j? = cos?–j sin?
a + jb a2 + b2e j tan 1 – ba
? ?
? ?
=
chapter 3 sinusoids and phasors
3-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• to convert a phasor from exponential to rectangular form, we use the expressions
• the polar form is essentially the same as the exponential form but the notation is different,
that is,
and it is important to remember that the phase angle is always measured with respect to the
positive real axis, and rotates in the counterclockwise direction.
• the rectangular form is most useful when we add or subtract phasors however, the exponential
and polar forms are most convenient when we multiply or divide phasors.
• to multiply two phasors in exponential (or polar) form, we multiply the magnitudes and we
add the phase angles, that is, if
then,
• to divide one phasor by another when both are expressed in exponential or polar form, we
divide the magnitude of the dividend by the magnitude of the divisor, and we subtract the
phase angle of the divisor from the phase angle of the dividend, that is, if
then,
ce j? = ccos? + jcsin?
ce– j? = ccos?–jcsin?
ce j? = c??
?
a = m?? and b = n??
ab mn?(? + ?) me j?ne j? mne j(? + ?)
= = =
a = m?? and b = n??
ab
- mn
?(? – ?) me j?
ne j? - mn
e j(? – ?)
= = =
numerical analysis using matlab and spreadsheets, second edition 3-21
orchard publications
exercises
3.10 exercises
1. perform the following operations, and check your answers with matlab.
a. b. c. d.
e. f. g.
2. perform the following operations, and check your answers with matlab.
a. b. c. d.
3. any phasor can be expressed as
using the identities or , compute:
a. b.
check your answers with matlab
4. compute the exponential and polar forms of
a. b.
check your answers with matlab.
5. compute the rectangular form of
a. b.
check your answers with matlab
6. find the real and imaginary components of
(2 – j4) + (3 + j4) (– 3 + j5) – (1 + j6) (2 – j3) – (2 – j3)? (3 – j2) ? (3 – j2)?
(2 – j4) ? (3 + j5) (3 – j2) ? (– 2 – j3) (2 – j4) ? (3 + j5) ? (3 – j2) ? (– 2 – j3)
22 + j6
3 + j2
- 8 + j6
– 3 – j
120
4 – j10
(3 – j2)
(3 – j2)?
-
a
a a + jb r( cos? + j sin?) re j? = = =
re j? ( ) rne jn? = n re j? n re j? ? n =
6 12 + j5 4 100 2(1 – j)
9 + j5
– 4 – j2
– 8 + j3
– 2 + j4

4?30°
5?–150°
e j60°
2e–j30° –
-
9 – j4
– 5 + jx

chapter 3 sinusoids and phasors
3-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.11 solutions to exercises
1.
a.
b.
c.
d.
e.
f.
g.
check with matlab:
(2?4j)+(3+4j), (?3+5j)?(1+6j), (2?3j)?(2+3j), (3?2j)*(3+2j),...
(2?4j)*(3+5j), (3?2j)*(?2?3j), (2?4j)*(3+5j)*(3?2j)*(?2?3j)
ans =
5
ans =
-4.0000 - 1.0000i
ans =
0 - 6.0000i
ans =
13
ans =
26.0000 - 2.0000i
ans =
-12.0000 - 5.0000i
ans =
-3.2200e+002 - 1.0600e+002i
(2 – j4) + (3 + j4) = 5 + 0 = 5
(– 3 + j5) – (1 + j6) = – 4 – j
(2 – j3) – (2 – j3)? = (2 – j3) – (2 + j3) = 0 – j6
(3 – j2) ? (3 – j2)? = (3 – j2) ? (3 + j2) = 9 + j6 – j6 + 4 = 13
(2 – j4) ? (3 + j5) = 6 + j10 – j12 + 20 = 26 – j2
(3 – j2) ? (– 2 – j3) = – 6 – j9 + j4 – 6 = – 12 – j5
(2 – j4) ? (3 + j5) ? (3 – j2) ? (– 2 – j3) = (6 + j10 – j12 + 20) ? (– 6 – j9 + j4 – 6)
= (26 – j2) ? (– 12 – j5)
= – 312 – j130 + j24 – 10 = – 322 – j106
numerical analysis using matlab and spreadsheets, second edition 3-23
orchard publications
solutions to exercises
2.
a.
b.
c.
d.
check with matlab:
22+6j)/(3+2j), (8+6j)/(?3?j), 120/(4?10j), (3?2j)/(3+2j)
ans =
6 - 2i
ans =
-3 - 1i
ans =
120/29 + 300/29i
ans =
5/13 - 12/13i
3.
a.
b.
check with matlab:
(12+5j)^(1/6), (100*sqrt(2)*(1?j))^(1/4)
22 + j6
3 + j2
- 22 + j6
3 + j2
- 3 – j2
3 – j2
? - 66 – j44 + j18 + 12
32 + 22
78 – j26
13
= = = - = 6 – j2
8 + j6
– 3 – j
8 + j6
– 3 – j
– 3 + j
– 3 + j
? - – 24 + j8 – j18 – 6
32 + 12
– 30 – j10
10
= = = = – 3 – j
120
4 – j10
120
4 – j10
4 + j10
4 + j10
? - 480 + j1200
42 + 102
- 480
116
j 1200
116
+ ? - 120
29
j 300
29
= = = = + ?
(3 – j2)
(3 – j2)?
- (3 – j2)
(3 + j2)
(3 – j2)
(3 – j2)
? 9 – j6 – j6 – 4
32 + 22
- 5 – j12
13
5
13
- j 12
13
= = = = – ? -
6 12 + j5 6 13e j0.395 6 13 e j0.3948 ? 6 ? 131 ? 6 = = = ? e j0.0658
= 1.5334( cos0.0658 + j sin0.0658) = 1.53 + j0.10
4 100 2(1 – j) 100 2 2e–j? ? 4 4 ? 100 2 2e–j? ? 4 ( ? )1 ? 4 (100 2)1 ? 4 21 ? 4e
–j? ? 16
= = = ?
= (3.4485 × 1.0905)( cos(? ? 16) – j sin(? ? 16)) = 3.6883 – j0.7337
chapter 3 sinusoids and phasors
3-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
ans =
1.5301 + 0.1008i
ans =
3.6883 - 0.7337i
4.
a.
b.
check with matlab:
x=(9+5j)/(?4?2j) abs(x), angle(x)*180/pi,...
y=(?8+3j)/(?2+4j) abs(y), angle(y)*180/pi
ans =
2.3022
ans =
-177.5104
ans =
1.9105
ans =
42.8789
5.
a.
b.
9 + j5
– 4 – j2
92 + 52 ej –1(5 ? 9) ? tan
42 + 22 e j (–2 ? –4) –1 ? tan
106 ? e j0.5071
20 ? e j3.6052
= = - = 2.3022e –j3.0981
2.3022e –j177.5081° = = 2.3022?–177.5081°
– 8 + j3
– 2 + j4
82 + 32 ej –1(3 ? –8) ? tan
22 + 42 e j (4 ? –2) –1 ? tan
73
20
- e j0.3588 –
e–j1.1071 = = ? = 1.9105e j0.7483
1.9105e j42.8744° = = 1.9105?42.8744°
4?30°
5?–150°
= (4 ? 5)?180° = –0.8
e j60°
2e–j30° –
- 0.5e j90° = – = –0.5( cos90° + j sin90°) = –0.5(0 + j) = –j0.5
numerical analysis using matlab and spreadsheets, second edition 3-25
orchard publications
solutions to exercises
check with matlab:
4*(cos(pi/6)+sin(pi/6)*j)/(5*(cos(?5*pi/6)+sin(?5*pi/6)*j)),...
exp(pi*j/3)/(?2*exp(?pi*j/6))
ans =
-0.8000 - 0.0000i
ans =
-0.0000 - 0.5000i
6.
9 – j4
– 5 + jx
9 – j4
– 5 + jx
–5–jx
–5–jx
? – 45 – j9x + j20 – 4x
52 + x2
– 4x – 45
x2 + 25
j– 9x + 20
x2 + 25
= = = +
chapter 3 sinusoids and phasors
3-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 4-1
orchard publications
chapter 4
matrices and determinants
his chapter is an introduction to matrices and matrix operations. determinants, cramer’s
rule, and gauss’s elimination method are introduced. some definitions and examples are
not applicable to subsequent material presented in this text, but are included for subject
continuity, and reference to more advance topics in matrix theory. these are denoted with a dagger
( † ) and may be skipped.
4.1 matrix definition
a matrix is a rectangular array of numbers such as those shown below.
in general form, a matrix is denoted as
(4.1)
the numbers are the elements of the matrix where the index indicates the row, and indicates
the column in which each element is positioned. thus, indicates the element positioned
in the fourth row and third column.
a matrix of rows and columns is said to be of order matrix.
if , the matrix is said to be a square matrix of order (or ). thus, if a matrix has five rows
and five columns, it is said to be a square matrix of order 5.
in a square matrix, the elements are called the main diagonal elements.
alternately, we say that the matrix elements , are located on the main
diagonal.
t
2 3 7
1 –1 5
or
1 3 1
–2 1 –5
4 –7 6
a
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
am1 am2 am3 … amn
=
aij i j
a43
m n m × n
m = n m n
a11, a22, a33, …, ann
a11, a22, a33, …, ann
chapter 4 matrices and determinants
4-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
† the sum of the diagonal elements of a square matrix is called the trace* of .
† a matrix in which every element is zero, is called a zero matrix.
4.2 matrix operations
two matrices and are equal, that is, , if and only if
(4.2)
two matrices are said to be conformable for addition (subtraction), if they are of the same order
.
if and are conformable for addition (subtraction), their sum (difference) will
be another matrix with the same order as and , where each element of is the sum (difference)
of the corresponding elements of and , that is,
(4.3)
example 4.1
compute and given that
and
solution:
and
check with matlab:
a=[1 2 3 0 1 4] b=[2 3 0 ?1 2 5] % define matrices a and b
a+b % add a and b
* henceforth, all paragraphs and topics preceded by a dagger ( † ) may be skipped. these are discussed in matrix theory textbooks.
a a
a = aij b = bij a = b
aij = bij i = 1, 2, 3, …, m j = 1, 2, 3,…, n
m × n
a aij = b bij =
c a b c
a b
c = a ± b = [aij ± bij]
a + b a – b
a 1 2 3
0 1 4
= b 2 3 0
–1 2 5
=
a + b 1 + 2 2 + 3 3 + 0
0 – 1 1 + 2 4 + 5
3 5 3
–1 3 9
= =
a – b 1 – 2 2 – 3 3 – 0
0 + 1 1 – 2 4 – 5
–1 –1 3
1 –1 –1
= =
numerical analysis using matlab and spreadsheets, second edition 4-3
orchard publications
matrix operations
ans =
3 5 3
-1 3 9
a?b % subtract b from a
ans =
-1 -1 3
1 -1 -1
if is any scalar (a positive or negative number), and not [ ] which is a matrix, then multiplication
of a matrix by the scalar , is the multiplication of every element of by .
example 4.2
multiply the matrix
by (a) and (b)
solution:
a.
b.
check with matlab:
k1=5 k2=(?3 + 2*j) % define scalars k1 and k2
a=[1 ?2 2 3] % define matrix a
k1*a % multiply matrix a by constant k1
ans =
5 -10
10 15
k2*a %multiply matrix a by constant k2
ans =
-3.0000+ 2.0000i 6.0000- 4.0000i
k k 1 × 1
a k a k
a 1 –2
2 3
=
k1 = 5 k2 = – 3 + j2
k1 ? a 5 1 –2
2 3
× 5 × 1 5 × (–2)
5 × 2 5 × 3
5 –10
10 15
= = =
k2 ? a (– 3 + j2) 1 –2
2 3
× (– 3 + j2) × 1 (– 3 + j2) × (–2)
(– 3 + j2) × 2 (– 3 + j2) × 3
– 3 + j2 6 – j4
– 6 + j4 – 9 + j6
= = =
chapter 4 matrices and determinants
4-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
-6.0000+ 4.0000i -9.0000+ 6.0000i
two matrices and are said to be conformable for multiplication in that order, only when
the number of columns of matrix is equal to the number of rows of matrix . that is, the product
(but not ) is conformable for multiplication only if is an and matrix is
an matrix. the product will then be an matrix. a convenient way to determine
if two matrices are conformable for multiplication is to write the dimensions of the two matrices
side-by-side as shown below.
for the product we have:
for matrix multiplication, the operation is row by column. thus, to obtain the product , we
multiply each element of a row of by the corresponding element of a column of then, we add
these products.
example 4.3
given that
and
compute the products and
solution:
the dimensions of matrices and are respectively therefore the product is
feasible, and will result in a , that is,
a b a ? b
a b
a ? b b ? a a m × p b
p × n a ? b m × n
m × p p × n
a b
shows that a and b are conformable for multiplication
indicates the dimension of the product a ? b
b ? a
here, b and a are not conformable for multiplication
b a
p × n m × p
a ? b
a b
c = 2 3 4 d
1
–1
2
=
c ? d d ? c
c d 1 × 3 3 × 1 c ? d
1 × 1
numerical analysis using matlab and spreadsheets, second edition 4-5
orchard publications
special forms of matrices
the dimensions for and are respectively and therefore, the product is also
feasible. multiplication of these will produce a 3 × 3 matrix as follows.
check with matlab:
c=[2 3 4] d=[1 ?1 2] % define matrices c and d
c*d % multiply c by d
ans =
7
d*c % multiply d by c
ans =
2 3 4
-2 -3 -4
4 6 8
division of one matrix by another, is not defined. however, an equivalent operation exists, and it
will become apparent later in this chapter, when we discuss the inverse of a matrix.
4.3 special forms of matrices
† a square matrix is said to be upper triangular when all the elements below the diagonal are
zero. the matrix below is an upper triangular matrix.
(4.4)
in an upper triangular matrix, not all elements above the diagonal need to be non-zero. for
applications, refer to chapter 14.
c ? d 2 3 4
1
–1
2
= = (2) ? (1) + (3) ? (–1) + (4) ? (2) = 7
d c 3 × 1 1 × 3 d ? c
d ? c
1
–1
2
2 3 4
(1) ? (2) (1) ? (3) (1) ? (4)
(–1) ? (2) (–1) ? (3) (–1) ? (4)
(2) ? (2) (2) ? (3) (2) ? (4)
2 3 4
–2 –3 –4
4 6 8
= = =
a
a
a11 a12 a13 … a1n
0 a22 a23 … a2n
0 0 … … …
… … 0 … …
0 0 0 … amn
=
chapter 4 matrices and determinants
4-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
† a square matrix is said to be lower triangular, when all the elements above the diagonal are
zero. the matrix below is a lower triangular matrix. for applications, refer to chapter 14.
(4.5)
in a lower triangular matrix, not all elements below the diagonal need to be non-zero.
† a square matrix is said to be diagonal, if all elements are zero, except those in the diagonal. the
matrix below is a diagonal matrix.
(4.6)
† a diagonal matrix is called a scalar matrix, if where is a
scalar. the matrix below is a scalar matrix with .
(4.7)
a scalar matrix with , is called an identity matrix . shown below are , , and
identity matrices.
(4.8)
the matlab eye(n) function displays an identity matrix. for example,
eye(4)% display a 4 by 4 identity matrix
b
b
a11 0 0 … 0
a21 a22 0 … 0
… … … 0 0
… … … … 0
am1 am2 am3 … amn
=
c
c
a11 0 0 … 0
0 a22 0 … 0
0 0 … 0 0
0 0 0 … 0
0 0 0 … amn
=
a11 = a22 = a33 = … = ann = k k
d k = 4
d
4 0 0 0
0 4 0 0
0 0 4 0
0 0 0 4
=
k = 1 i 2 × 2 3 × 3
4 × 4
1 0
0 1
1 0 0
0 1 0
0 0 1
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
n × n
numerical analysis using matlab and spreadsheets, second edition 4-7
orchard publications
special forms of matrices
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
likewise, the eye(size(a)) function, produces an identity matrix whose size is the same as
matrix . for example, let be defined as
a=[1 3 1 ?2 1 ?5 4 ?7 6] % define matrix a
a =
1 3 1
-2 1 -5
4 -7 6
then,
eye(size(a))
displays
ans =
1 0 0
0 1 0
0 0 1
† the transpose of a matrix , denoted as , is the matrix that is obtained when the rows and
columns of matrix are interchanged. for example, if
(4.9)
in matlab we use the apostrophe (?) symbol to denote and obtain the transpose of a matrix.
thus, for the above example,
a=[1 2 3 4 5 6] % define matrix a
a =
1 2 3
4 5 6
a a
a at
a
a 1 2 3
4 5 6
= then at
1 4
2 5
3 6
=
chapter 4 matrices and determinants
4-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
a % display the transpose of a
ans =
1 4
2 5
3 6
† a symmetric matrix , is one such that = , that is, the transpose of a matrix is the same
as . an example of a symmetric matrix is shown below.
(4.10)
† if a matrix has complex numbers as elements, the matrix obtained from by replacing each
element by its conjugate, is called the conjugate of , and it is denoted as .
an example is shown below.
† matlab has two built-in functions which compute the complex conjugate of a number. the
first, conj(x), computes the complex conjugate of any complex number, and the second,
conj(a), computes the conjugate of a matrix . using matlab with the matrix defined as
above, we get
a = [1+2j j 3 2-3j] % define and display matrix a
a =
1.0000 + 2.0000i 0 + 1.0000i
3.0000 2.0000 - 3.0000i
conj_a=conj(a) % compute and display the conjugate of a
conj_a =
1.0000 - 2.0000i 0 - 1.0000i
3.0000 2.0000 + 3.0000i
† a square matrix a such that , is called skew-symmetric. for example,
a ata a
a
a
1 2 3
2 4 –5
3 –5 6
= at
1 2 3
2 4 –5
3 –5 6
= = a
a a
a a?
a 1 + j2 j
3 2 – j3
= a? 1 – j2 –j
3 2 + j3
=
a a
at = –a
a
0 2 –3
–2 0 –4
3 4 0
= at
0 –2 3
2 0 4
–3 –4 0
= = –a
numerical analysis using matlab and spreadsheets, second edition 4-9
orchard publications
determinants
therefore, matrix above is skew symmetric.
† a square matrix such that , is called hermitian. for example,
therefore, matrix above is hermitian.
† a square matrix such that , is called skew?hermitian. for example,
therefore, matrix above is skew-hermitian.
4.4 determinants
let matrix be defined as the square matrix
(4.11)
then, the determinant of , denoted as , is defined as
(4.12)
the determinant of a square matrix of order is referred to as determinant of order .
let be a determinant of order , that is,
(4.13)
a
a at? = a
a
1 1 – j 2
1 + j 3 j
2 –j 0
at
1 1 + j 2
1 – j 3 –j
2 j 0
at*
1 1 + j 2
1 – j 3 –j
2 j 0
= = = = a
a
a at? = –a
a
j 1 – j 2
– 1 – j 3j j
–2 j 0
at
j – 1 – j –2
1 – j 3j j
2 j 0
at*
–j – 1 + j –2
1 + j –3j –j
2 –j 0
= = = = –a
a
a
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
an1 an2 an3 … ann
=
a deta
deta a11a22a33…ann a12a23a34…an1 a13a24a35…an2 …
an1…a22a13… an2 – …a23a14 an3– – …a24a15 – …
= + + +
n n
a 2
a
a11 a12
a21 a22
=
chapter 4 matrices and determinants
4-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
then,
(4.14)
example 4.4
given that
and
compute and .
solution:
check with matlab:
a=[1 2 3 4] b=[2 ?1 2 0] % define matrices a and b
det(a) % compute the determinant of a
ans =
-2
det(b) % compute the determinant of b
ans =
2
let be a matrix of order 3, that is,
(4.15)
then, is found from
(4.16)
a convenient method to evaluate the determinant of order 3, is to write the first two columns to
the right of the matrix, and add the products formed by the diagonals from upper left to
lower right then subtract the products formed by the diagonals from lower left to upper right as
shown on the diagram of the next page. when this is done properly, we obtain (4.16) above.
deta = a11 a22 – a21a12
a 1 2
3 4
= b 2 –1
2 0
=
deta detb
deta = 1 ? 4 – 3 ? 2 = 4 – 6 = –2
deta = 2 ? 0 – 2 ? (–1) = 0 – (–2) = 2
a
a
a11 a12 a13
a21 a22 a23
a31 a32 a33
=
deta
deta = a11 a22 a33 + a12 a23 a31 + a11 a22 a33
–a11 a22 a33 – a11 a22 a33 – a11 a22 a33
3 × 3
numerical analysis using matlab and spreadsheets, second edition 4-11
orchard publications
determinants
this method works only with second and third order determinants. to evaluate higher order
determinants, we must first compute the cofactors these will be defined shortly.
example 4.5
compute and given that
and
solution:
or
likewise,
or
check with matlab:
a=[2 3 5 1 0 1 2 1 0] det(a) % define matrix a and compute deta
ans =
9
b=[2 ?3 ?4 1 0 ?2 0 ?5 ?6] det(b) % define matrix b and compute detb
ans =
-18
a11 a12 a13
a21 a22 a23
a31 a32 a33
a11 a12
a21 a22
a31 a32 +
?
deta detb
a
2 3 5
1 0 1
2 1 0
= b
2 –3 –4
1 0 –2
0 –5 –6
=
deta
2 3 5 2 3
1 0 1 1 0
2 1 0 2 1
=
deta (2 × 0 × 0) (3 × 1 × 1) (5 × 1 × 1)
– (2 × 0 × 5) – (1 × 1 × 2) – (0 × 1 × 3)
+ +
= 11 – 2 = 9
=
detb
2 –3 –4 2 –3
1 0 –2 1 –2
0 –5 –6 2 –6
=
detb [2 × 0 × (–6)] [(–3) × (–2) × 0] [(–4) × 1 × (–5)]
– [0 × 0 × (–4)] – [(–5) × (–2) × 2] – [(–6) × 1 × (–3)]
+ +
= 20 – 38 = –18
=
chapter 4 matrices and determinants
4-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
4.5 minors and cofactors
let matrix be defined as the square matrix of order as shown below.
(4.17)
if we remove the elements of its row, and column, the determinant of the remaining
square matrix is called the minor of determinant , and it is denoted as .
the signed minor is called the cofactor of and it is denoted as .
example 4.6
given that
(4.18)
compute the minors , , and the cofactors , and .
solution:
and
the remaining minors
and cofactors
a n
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
an1 an2 an3 … ann
=
ith jth n – 1
a mij
(–1)i + j mij aij ?ij
a
a11 a12 a13
a21 a22 a23
a31 a32 a33
=
m11 m12 m13 ?11 ?12 ?13
m11
a22 a23
a32 a33
= m12
a21 a23
a31 a33
= m11
a21 a22
a31 a32
=
?11 (–1)1 + 1 m11 m11 ?12 (–1)1 + 2 m12 m12 ?13 m13 (–1)1 + 3 m13 = = = = – = =
m21 m22 m23 m31 m32 m33 , , , , ,
numerical analysis using matlab and spreadsheets, second edition 4-13
orchard publications
minors and cofactors
are defined similarly.
example 4.7
given that
(4.19)
compute its cofactors.
solution:
(4.20)
(4.21)
(4.22)
(4.23)
(4.24)
it is useful to remember that the signs of the cofactors follow the pattern
that is, the cofactors on the diagonals have the same sign as their minors.
let be a square matrix of any size the value of the determinant of is the sum of the products
obtained by multiplying each element of any row or any column by its cofactor.
?21, ?22, ?23, ?31, ?32, and ?33
a
1 2 –3
2 –4 2
–1 2 –6
=
?11 (–1)1 + 1 –4 2
2 –6
= = 20 ?12 (–1)1 + 2 2 2
–1 –6
= = 10
?13 (–1)1 + 3 2 –4
–1 2
0 ?21 (–1)2 + 1 2 –3
2 –6
= = = = 6
?22 (–1)2 + 2 1 –3
–1 –6
= = –9 ?23 (–1)2 + 3 1 2
–1 2
= = –4
?31 (–1)3 + 1 2 –3
–4 2
= = –8 ?32 (–1)3 + 2 1 –3
2 2
, = = –8
?33 (–1)3 + 3 1 2
2 –4
= = –8
+ ? + ? +
? + ? + ?
+ ? + ? +
? + ? + ?
+ ? + ? +
a a
chapter 4 matrices and determinants
4-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 4.8
compute the determinant of using the elements of the first row.
(4.25)
solution:
check with matlab:
a=[1 2 ?3 2 ?4 2 ?1 2 ?6] det(a) % define matrix a and compute deta
ans =
40
we must use the above procedure to find the determinant of a matrix of order 4 or higher.
thus, a fourth-order determinant can first be expressed as the sum of the products of the elements
of its first row by its cofactor as shown below.
(4.26)
determinants of order five or higher can be evaluated similarly.
example 4.9
compute the value of the determinant
(4.27)
a
a
1 2 –3
2 –4 2
–1 2 –6
=
deta 1 –4 2
2 –6
= 2 2 2
–1 –6
3 2 –4
–1 2
– – = 1 × 20 – 2 × (–10) – 3 × 0 = 40
a
a
a11 a12 a13 a14
a21 a22 a23 a24
a31 a32 a33 a34
a41 a42 a43 a44
a11
a22 a23 a24
a32 a33 a34
a42 a43 a44
a21
a12 a13 a14
a32 a33 a34
a42 a43 a44

+a31
a12 a13 a14
a22 a23 a24
a42 a43 a44
a41
a12 a13 a14
a22 a23 a24
a32 a33 a34

= =
a
2 –1 0 –3
–1 1 0 –1
4 0 3 –2
–3 0 0 1
=
numerical analysis using matlab and spreadsheets, second edition 4-15
orchard publications
minors and cofactors
solution:
using the above procedure, we will multiply each element of the first column by its cofactor.
then,
next, using the procedure of example 4.5 or example 4.8, we find
, , ,
and thus
we can verify our answer with matlab as follows:
a=[ 2 ?1 0 ?3 ?1 1 0 ?1 4 0 3 ?2 ?3 0 0 1] delta = det(a)
delta =
-33
some useful properties of determinants are given below.
property 1:
if all elements of one row or one column are zero, the determinant is zero. an example of this is the
determinant of the cofactor above.
property 2:
if all the elements of one row or column are m times the corresponding elements of another row or column,
the determinant is zero. for example, if
(4.28)
then,
(4.29)
here, is zero because the second column in is 2 times the first column.
a=2
1 0 –1
0 3 –2
0 0 1
[a]
(–1)
–1 0 –3
0 3 –2
0 0 1

[b]
+4
–1 0 –3
1 0 –1
0 0 1
[c]
(–3)
–1 0 –3
1 0 –1
0 3 –2

[d]
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
[a] = 6 [b] = –3 [c] = 0 [d] = –36
deta = [a] + [b] + [c] + [d] = 6 – 3 + 0 – 36 = –33
[c]
a
2 4 1
3 6 1
1 2 1
=
deta
2 4 1
3 6 1
1 2 1
2 4
3 6
1 2
= = 12 + 4 + 6 – 6 – 4 – 12 = 0
deta a
chapter 4 matrices and determinants
4-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
check with matlab:
a=[2 4 1 3 6 1 1 2 1] det(a)
ans =
0
property 3:
if two rows or two columns of a matrix are identical, the determinant is zero. this follows from property
2 with .
4.6 cramer’s rule
let us consider the systems of the three equations below
(4.30)
and let
cramer’s rule states that the unknowns , , and can be found from the relations
(4.31)
provided that the determinant ? (delta) is not zero.
we observe that the numerators of (4.31) are determinants that are formed from ? by the substitution
of the known values a, b, and c, for the coefficients of the desired unknown.
cramer’s rule applies to systems of two or more equations.
if (4.30) is a homogeneous set of equations, that is, if , then, are
all zero as we found in property 1 above. then, also.
example 4.10
use cramer’s rule to find if
m = 1
a11x + a12y + a13z = a
a21x + a22y + a23z = b
a31x + a32y + a33z = c
?
a11 a12 a13
a21 a22 a23
a31 a32 a33
d1
a a11 a13
b a21 a23
c a31 a33
d2
a11 a a13
a21 b a23
a31 c a33
d3
a11 a12 a
a21 a22 b
a31 a32 c
= = = =
x y z
x
d1
?
= y
d2
?
= z
d3
?
=
a = b = c = 0 d1, d2, and d3
x = y = z = 0
v1, v2, and v3
numerical analysis using matlab and spreadsheets, second edition 4-17
orchard publications
cramer’s rule
(4.32)
and verify your answers with matlab.
solution:
rearranging the unknowns , and transferring known values to the right side, we get
(4.33)
now, by cramer’s rule,
therefore, using (4.31) we get
(4.34)
we will verify with matlab as follows.
% the following code will compute and display the values of v1, v2 and v3.
format rat % express answers in ratio form
b=[2 ?1 3 ?4 ?3 ?2 3 1 ?1] % the elements of the determinant d
delta=det(b) % compute the determinant d of b
2v1 – 5 – v2 + 3v3 = 0
–2v3 – 3v2 – 4v1 = 8
v2 + 3v1 – 4 – v3 = 0
v
2v1 – v2 + 3v3 = 5
–4v1 – 3v2 – 2v3 = 8
3v1 + v2 – v3 = 4
?
2 –1 3
–4 –3 –2
3 1 –1
2 –1
–4 –3
3 1
= = 6 + 6 – 12 + 27 + 4 + 4 = 35
d1
5 –1 3
8 –3 –2
4 1 –1
5 –1
8 –3
4 1
= = 15 + 8 + 24 + 36 + 10 – 8 = 85
d2
2 5 3
–4 8 –2
3 4 –1
2 5
–4 8
3 4
= = – 16 – 30 – 48 – 72 + 16 – 20 = –170
d3
2 –1 5
–4 –3 8
3 1 4
2 –1
–4 –3
3 1
= = – 24 – 24 – 20 + 45 – 16 – 16 = –55
x1
d1
? 85
35
- 17
7
= = = - x2
d2
? 170
35
– 34
7
= = = – - x3
d3
? 55
35
– - 11
7
= = = – -
chapter 4 matrices and determinants
4-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
d1=[5 ?1 3 8 ?3 ?2 4 1 ?1] % the elements of d1
detd1=det(d1) % compute the determinant of d1
d2=[2 5 3 ?4 8 ?2 3 4 ?1] % the elements of d2
detd2=det(d2) % compute the determinant of d2
d3=[2 ?1 5 ?4 ?3 8 3 1 4] % the elements of d3
detd3=det(d3) % compute he determinant of d3
v1=detd1/delta % compute the value of v1
v2=detd2/delta % compute the value of v2
v3=detd3/delta % compute the value of v3
%
disp( v1= ) disp(v1) % display the value of v1
disp( v2= ) disp(v2) % display the value of v2
disp( v3= ) disp(v3) % display the value of v3
v1=
17/7
v2=
-34/7
v3=
-11/7
these are the same values as in (4.34)
4.7 gaussian elimination method
we can find the unknowns in a system of two or more equations also by the gaussian elimination
method. with this method, the objective is to eliminate one unknown at a time. this can be done
by multiplying the terms of any of the equations of the system by a number such that we can add
(or subtract) this equation to another equation in the system so that one of the unknowns will be
eliminated. then, by substitution to another equation with two unknowns, we can find the second
unknown. subsequently, substitution of the two values found can be made into an equation
with three unknowns from which we can find the value of the third unknown. this procedure is
repeated until all unknowns are found. this method is best illustrated with the following example
which consists of the same equations as the previous example.
example 4.11
use the gaussian elimination method to find of
(4.35)
v1, v2, and v3
2v1 – v2 + 3v3 = 5
–4v1 – 3v2 – 2v3 = 8
3v1 + v2 – v3 = 4
numerical analysis using matlab and spreadsheets, second edition 4-19
orchard publications
the adjoint of a matrix
solution:
as a first step, we add the first equation of (4.35) with the third to eliminate the unknown and
we obtain the following equation.
(4.36)
next, we multiply the third equation of (4.35) by 3, and we add it with the second to eliminate
. then, we obtain the following equation.
(4.37)
subtraction of (4.37) from (4.36) yields
(4.38)
now, we can find the unknown from either (4.36) or (4.37). by substitution of (4.38) into
(4.36) we get
(4.39)
finally, we can find the last unknown from any of the three equations of (4.35). by substitution
into the first equation we get
(4.40)
these are the same values as those we found in example 4.10.
the gaussian elimination method works well if the coefficients of the unknowns are small integers,
as in example 4.11. however, it becomes impractical if the coefficients are large or fractional
numbers.
the gaussian elimination is further discussed in chapter 14 in conjunction with the factorization
method.
4.8 the adjoint of a matrix
let us assume that is an square matrix and is the cofactor of . then the adjoint of ,
denoted as , is defined as the square matrix shown on the next page.
v2
5v1 + 2v3 = 9
v2
5v1 – 5v3 = 20
7v3 11 or v3
11
7
= – = – -
v1
5v1 2 11
7
?– - ?
+ ? ? ? 9 or v1
17
7
= = -
v2
v2 2v1 + 3v3 – 5 34
7
- 33
7
– - 35
7
– - 34
7
= = = – -
lu
a n ?ij aij a
adja n
chapter 4 matrices and determinants
4-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(4.41)
we observe that the cofactors of the elements of the row (column) of , are the elements of
the column (row) of .
example 4.12
compute given that
(4.42)
solution:
4.9 singular and non-singular matrices
an square matrix is called singular if if , a is called non-singular.
example 4.13
given that
(4.43)
determine whether this matrix is singular or non-singular.
adja
?11 ?21 ?31 … ?n1
?12 ?22 ?32 … ?n2
?13 ?23 ?33 … ?n3
… … … … …
?1n ?2n ?3n … ?nn
=
ith a
ith adja
adja
a
1 2 3
1 3 4
1 4 3
=
adja
3 4
4 3
2 3
4 3
– 2 3
3 4
1 4
1 3
– 1 3
1 3
2 3
3 4

1 3
1 4
1 2
1 4
– 1 2
1 3
–7 6 –1
1 0 –1
1 –2 1
= =
n a deta = 0 deta ? 0
a
1 2 3
2 3 4
3 5 7
=
numerical analysis using matlab and spreadsheets, second edition 4-21
orchard publications
the inverse of a matrix
solution:
therefore, matrix is singular.
4.10 the inverse of a matrix
if and b are square matrices such that , where is the identity matrix, is
called the inverse of , denoted as , and likewise, is called the inverse of , that is,
if a matrix is non-singular, we can compute its inverse from the relation
(4.44)
example 4.14
given that
(4.45)
compute its inverse, that is, find
solution:
here, , and since this is a non-zero value, it is possible to compute
the inverse of using (4.44).
from example 4.12,
then,
(4.46)
deta
1 2 3
2 3 4
3 5 7
1 2
2 3
3 5
= = 21 + 24 + 30 – 27 – 20 – 28 = 0
a
a n ab = ba = i i b
a b a–1 = a b
a b–1 =
a
a–1 1
deta
= -adja
a
1 2 3
1 3 4
1 4 3
=
a–1
deta = 9 + 8 + 12 – 9 – 16 – 6 = –2
a
adja
–7 6 –1
1 0 –1
1 –2 1
=
a–1 1
deta
-adja 1
–2
-
–7 6 –1
1 0 –1
1 –2 1
3.5 –3 0.5
–0.5 0 0.5
–0.5 1 –0.5
= = =
chapter 4 matrices and determinants
4-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
check with matlab:
a=[1 2 3 1 3 4 1 4 3], inva=inv(a) % define matrix a and compute its inverse
a =
1 2 3
1 3 4
1 4 3
inva =
3.5000 -3.0000 0.5000
-0.5000 0 0.5000
-0.5000 1.0000 -0.5000
multiplication of a matrix by its inverse produces the identity matrix , that is,
(4.47)
example 4.15
prove the validity of (4.47) for
proof:
then,
and
4.11 solution of simultaneous equations with matrices
consider the relation
(4.48)
where and are matrices whose elements are known, and is a matrix (a column vector)
whose elements are the unknowns. we assume that and are conformable for multiplication.
a a–1 i
aa–1 i or a–1= a = i
a 4 3
2 2
=
deta 8 – 6 2 and adja 2 –3
–2 4
= = =
a–1 1
deta
-adja 12 2 3 –
–2 4
1 –3 ? 2
–1 2
= = =
aa–1 4 3
2 2
1 –3 ? 2
–1 2
4 – 3 – 6 + 6
2 – 2 – 3 + 4
1 0
0 1
= = = = i
ax = b
a b x
a x
numerical analysis using matlab and spreadsheets, second edition 4-23
orchard publications
solution of simultaneous equations with matrices
multiplication of both sides of (4.48) by yields:
(4.49)
or
(4.50)
therefore, we can use (4.50) to solve any set of simultaneous equations that have solutions. we
will refer to this method as the inverse matrix method of solution of simultaneous equations.
example 4.16
given the system of equations
(4.51)
compute the unknowns using the inverse matrix method.
solution:
in matrix form, the given set of equations is where
(4.52)
then,
(4.53)
or
(4.54)
next, we find the determinant , and the adjoint .
a–1
a–1ax a–1b ix a–1= = = b
x=a–1b
2x1 + 3x2 + x3 = 9
x1 + 2x2 + 3x3 = 6
?3x1 + x2 + 2x3 = 8?
? ?
? ?
? ?
? ?
x1, x2, and x3
ax = b
a
2 3 1
1 2 3
3 1 2
= x
x1
x2
x3
= b
9
6
8
, , =
x a–1= b
x1
x2
x3
2 3 1
1 2 3
3 1 2
–1
9
6
8
=
deta adja
deta = 18 and adja
1 –5 7
7 1 –5
–5 7 1
=
chapter 4 matrices and determinants
4-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
therefore,
and by (4.53) we obtain the solution as follows.
(4.55)
to verify our results, we could use the matlab inv(a) function, and multiply by . however,
it is easier to use the matrix left division operation this is matlab’s solution of
for the matrix equation , where matrix is the same size as matrix . for this
example,
a=[2 3 1 1 2 3 3 1 2] b=[9 6 8] x=a \ b % observe that b is a column vector
x =
1.9444
1.6111
0.2778
example 4.17
for the electric circuit of figure 4.1, the loop equations are
figure 4.1. circuit for example 4.17
(4.56)
use the inverse matrix method to compute the values of the currents .
a–1 1
deta
- adja 1
18
-
1 –5 7
7 1 –5
–5 7 1
= =
x
x1
x2
x3
1
18
-
1 –5 7
7 1 –5
–5 7 1
9
6
8
1
18
-
35
29
5
35 ? 18
29 ? 18
5 ? 18
1.94
1.61
0.28
= = = = =
a–1 b
x = a \ b
a–1b a ? x = b x b
+
?
v = 100 v
9 ? 9 ? 4 ?
1 ? 2 ? 2 ?
i1 i i3 2
10i1 – 9i2 = 100
–9i1 + 20i2 – 9i3 = 0
–9i2 + 15i3 = 0
i1, i2, and i3
numerical analysis using matlab and spreadsheets, second edition 4-25
orchard publications
solution of simultaneous equations with matrices
solution:
for this example, the matrix equation is or , where
the next step is to find . this is found from the relation
(4.57)
therefore, we find the determinant and the adjoint of . for this example, we find that
(4.58)
then,
and
check with matlab:
r=[10 ?9 0 ?9 20 ?9 0 ?9 15] v=[100 0 0] i=r\v
i =
22.4615
13.8462
8.3077
we can also use subscripts to address the individual elements of the matrix. accordingly, the
above code could also have been written as:
r(1,1)=10 r(1,2)=?9 % no need to make entry for a(1,3) since it is zero.
ri = v i r–1= v
r
10 –9 0
–9 20 –9
0 –9 15
= v
100
0
0
and i
i1
i2
i3
, = =
r–1
r–1 1
detr
= - adjr
r
detr = 975 adjr
219 135 81
135 150 90
81 90 119
, =
r–1 1
detr
-adjr 1
975

219 135 81
135 150 90
81 90 119
= =
i
i1
i2
i3
1
975

219 135 81
135 150 90
81 90 119
100
0
0
100
975

219
135
81
22.46
13.85
8.31
= = = =
chapter 4 matrices and determinants
4-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
r(2,1)=?9 r(2,2)=20 r(2,3)=?9 r(3,2)=?9 r(3,3)=15 v=[100 0 0] i=r\v
i =
22.4615
13.8462
8.3077
spreadsheets also have the capability of solving simultaneous equations using the inverse matrix
method. for instance, we can use microsoft excel’s minverse (matrix inversion) and mmult
(matrix multiplication) functions, to obtain the values of the three currents in example 4.17.
the procedure is as follows:
1. we start with a blank spreadsheet and in a block of cells, say b3:d5, we enter the elements of
matrix as shown in figure 4.2. then, we enter the elements of matrix in g3:g5.
figure 4.2. solution of example 4.17 with a spreadsheet
2. next, we compute and display the inverse of r, that is, . we choose b7:d9 for the elements
of this inverted matrix. we format this block for number display with three decimal
places. with this range highlighted and making sure that the cell marker is in b7, we type the
formula
=mininverse(b3:d5)
and we press the crtl-shift-enter keys simultaneously. we observe that appears in these
cells.
3. now, we choose the block of cells g7:g9 for the values of the current . as before, we highlight
them, and with the cell marker positioned in g7, we type the formula
=mmult(b7:d9,g3:g5)
and we press the crtl-shift-enter keys simultaneously. the values of i then appear in g7:g9.
r v
1234567
89
10
a b c d e f g h
spreadsheet for matrix inversion and matrix multiplication
10 -9 0 100
r= -9 20 -9 v= 0
0 -9 15 0
0.225 0.138 0.083 22.462
r-1= 0.138 0.154 0.092 i= 13.846
0.083 0.092 0.122 8.3077
r–1
r–1
i
numerical analysis using matlab and spreadsheets, second edition 4-27
orchard publications
solution of simultaneous equations with matrices
example 4.18
for the phasor circuit of figure 4.3, the current can be found from the relation
figure 4.3. circuit for example 4.18
(4.59)
and the voltages and can be computed from the nodal equations
(4.60)
(4.61)
compute, and express the current in both rectangular and polar forms by first simplifying like
terms, collecting, and then writing the above relations in matrix form as , where
, , and .
solution:
the elements of the matrix are the coefficients of and . simplifying and rearranging the
nodal equations of (4.60) and (4.61), we get
(4.62)
next, we write (4.62) in matrix form as
(4.63)
ix
+
?
r1
85 ?
50 ?
r2
c
l
r3 = 100 ?
ix
vs
?j100 ?
j200 ?
170?0°
v1 v2
ix
v1 – v2
r3
= -
v1 v2
v1 – 170?0°
85
-
v1 – v2
100
-
v1 – 0
j200
+ + = 0
v2 – 170?0°
–j100
-
v2 – v1
100
-
v2 – 0
50
+ + = 0
ix
yv = i
y = admit tance v = voltage i = current
y v1 v2
(0.0218 – j0.005)v1 – 0.01v2 = 2
–0.01v1 + (0.03 + j0.01)v2 = j1.7
0.0218 – j0.005 –0.01
–0.01 0.03 + j0.01
y
v1
v2
v
2
j1.7
i
=
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
chapter 4 matrices and determinants
4-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
where the matrices , , and are as indicatedin (4.63).
we will use matlab to compute the voltages and , and to do all other computations.
the code is shown below.
y=[0.0218?0.005j ?0.01 ?0.01 0.03+0.01j] i=[2 1.7j] v=y\i % define y, i, and find v
fprintf( \n ) % insert a line
disp( v1 v2 ) disp( ) % display v1 and v2 with dash line underneath
fprintf( %9.3f %9.3f\n ,v(1),v(2)) % display values of v1 and v2 in tabular form
fprintf( \n )% insert another line
v1 v2

104.905 53.416
next, we find from
r3=100 ix=(v(1)?v(2))/r3 % compute the value of ix
ix =
0.5149 - 0.0590i
and this is the rectangular form of . for the polar form we use
magix=abs(ix) % compute the magnitude of ix
magix =
0.5183
thetaix=angle(ix)*180/pi % compute angle theta in degrees
thetaix =
-6.5326
therefore, in polar form
spreadsheets have limited capabilities with complex numbers, and thus we cannot use them to
compute matrices that include complex numbers in their elements.
y v i
v1 v2
ix
ix
ix = 0.518?–6.53°
numerical analysis using matlab and spreadsheets, second edition 4-29
orchard publications
summary
4.12 summary
• a matrix is a rectangular array of numbers whose general form is
the numbers are the elements of the matrix where the index indicates the row, and
indicates the column in which each element is positioned. a matrix of rows and columns
is said to be of order matrix. if , the matrix is said to be a square matrix of order
.
• two matrices and are equal, that is, , if and only if
• two matrices are said to be conformable for addition (subtraction), if they are of the same order
. if and are conformable for addition (subtraction), their sum (difference)
will be another matrix with the same order as and , where each element of
is the sum (difference) of the corresponding elements of and , i.e.,
• if is any scalar (a positive or negative number), and not [ ] which is a matrix, then
multiplication of a matrix by the scalar , is the multiplication of every element of by .
• two matrices and are said to be conformable for multiplication in that order, only
when the number of columns of matrix is equal to the number of rows of matrix . that is,
the product (but not ) is conformable for multiplication only if is an and
matrix is an matrix. the product will then be an matrix.
• for matrix multiplication, the operation is row by column. thus, to obtain the product ,
we multiply each element of a row of by the corresponding element of a column of then,
we add these products.
• division of one matrix by another, is not defined.
• a scalar matrix is a square matrix where and is a scalar.
a scalar matrix with , is called an identity matrix .
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
am1 am2 am3 … amn
=
aij i j
m n
m × n m = n
m
a aij = b bij = a = b
aij = bij i = 1, 2, 3, …, m j = 1, 2, 3,…, n
m × n a aij = b bij =
c a b c
a b c = a ± b = [aij ± bij]
k k 1 × 1
a k a k
a b a ? b
a b
a ? b b ? a a m × p
b p × n a ? b m × n
a ? b
a b
a11 = a22 = a33 = … = ann = k k
k = 1 i
chapter 4 matrices and determinants
4-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the matlab eye(n) function displays an identity matrix and the eye(size(a)) function
displays an identity matrix whose size is the same as matrix .
• the transpose of a matrix , denoted as , is the matrix that is obtained when the rows and
columns of matrix are interchanged.
• the determinant of a square matrix where
is denoted as and it is defined as
• if from a matrix be defined as
we remove the elements of its row, and column, the determinant of the remaining
square matrix is called the minor of determinant , and it is denoted as .
• the signed minor is called the cofactor of and it is denoted as .
• let be a square matrix of any size the value of the determinant of is the sum of the products
obtained by multiplying each element of any row or any column by its cofactor. we must
use this procedure to find the determinant of a matrix of order 4 or higher.
• some useful properties of determinants are:
a. if all elements of one row or one column are zero, the determinant is zero.
n × n
a
a at
a
a
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
an1 an2 an3 … ann
=
deta
deta a11a22a33…ann a12a23a34…an1 a13a24a35…an2 …
an1…a22a13… an2 – …a23a14 an3– – …a24a15 – …
= + + +
a
a
a11 a12 a13 … a1n
a21 a22 a23 … a2n
a31 a32 a33 … a3n
… … … … …
an1 an2 an3 … ann
=
ith jth
n – 1 a mij
(–1)i + j mij aij ?ij
a a
a
numerical analysis using matlab and spreadsheets, second edition 4-31
orchard publications
summary
b. if all the elements of one row or column are m times the corresponding elements of
another row or column, the determinant is zero.
c. if two rows or two columns of a matrix are identical, the determinant is zero.
• cramer’s rule states that if a system of equations is defined as
and we let
the unknowns , , and can be found from the relations
provided that the determinant ? (delta) is not zero.
• we can find the unknowns in a system of two or more equations also by the gaussian elimination
method. with this method, the objective is to eliminate one unknown at a time. this can
be done by multiplying the terms of any of the equations of the system by a number such that
we can add (or subtract) this equation to another equation in the system so that one of the
unknowns will be eliminated. then, by substitution to another equation with two unknowns,
we can find the second unknown. subsequently, substitution of the two values found can be
made into an equation with three unknowns from which we can find the value of the third
unknown. this procedure is repeated until all unknowns are found.
• if is an square matrix and is the cofactor of , the adjoint of , denoted as , is
defined as the square matrix below.
a11x + a12y + a13z = a
a21x + a22y + a23z = b
a31x + a32y + a33z = c
?
a11 a12 a13
a21 a22 a23
a31 a32 a33
d1
a a11 a13
b a21 a23
c a31 a33
d2
a11 a a13
a21 b a23
a31 c a33
d3
a11 a12 a
a21 a22 b
a31 a32 c
= = = =
x y z
x
d1
? = y
d2
? = z
d3
? =
a n ?ij aij a adja
n
adja
?11 ?21 ?31 … ?n1
?12 ?22 ?32 … ?n2
?13 ?23 ?33 … ?n3
… … … … …
?1n ?2n ?3n … ?nn
=
chapter 4 matrices and determinants
4-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• an square matrix is called singular if if , a is called non-singular.
• if and b are square matrices such that , where is the identity matrix, is
called the inverse of , denoted as , and likewise, is called the inverse of , that is,
• if a matrix is non-singular, we can compute its inverse from the relation
• multiplication of a matrix by its inverse produces the identity matrix , that is,
• if and are matrices whose elements are known, is a matrix (a column vector) whose elements
are the unknowns and and are conformable for multiplication, we can use the relation
to solve any set of simultaneous equations that have solutions. we refer to this
method as the inverse matrix method of solution of simultaneous equations.
• the matrix left division operation is defined as this is matlab’s solution of
for the matrix equation , where matrix is the same size as matrix .
• we can use microsoft excel’s minverse (matrix inversion) and mmult (matrix multiplication)
functions, to solve any set of simultaneous equations that have solutions. however, we
cannot use them to compute matrices that include complex numbers in their elements.
n a deta = 0 deta ? 0
a n ab = ba = i i b
a b a–1 = a b
a b–1 =
a
a–1 1
deta
= -adja
a a–1 i
aa–1 i or a–1= a = i
a b x
a x
x=a–1b
x = a \ b a–1b
a ? x = b x b
numerical analysis using matlab and spreadsheets, second edition 4-33
orchard publications
exercises
4.13 exercises
for exercises 1 through 3 below, the matrices , , and are defined as:
1. perform the following computations, if possible. verify your answers with excel or matlab.
a. b. c. d. e. f. g. h.
2. perform the following computations, if possible. verify your answers with excel or matlab.
a. b. c. d. e. f. g. h.
3. perform the following computations, if possible. verify your answers with excel or matlab.
a. b. c. d. e. f.
4. solve the following system of equations using cramer’s rule. verify your answers with excel or
matlab.
5. repeat exercise 4 using the gaussian elimination method.
6. use the matlab det(a) function to find the unknowns of the system of equations below.
7. solve the following system of equations using the inverse matrix method. verify your answers
with excel or matlab.
a b c d
a
1 –1 –4
5 7 –2
3 –5 6
= b
5 9 –3
–2 8 2
7 –4 6
= c=
4 6
–3 8
5 –2
d 1 –2 3
–3 6 –4
=
a + b a + c b + d c + d a – b a – c b – d c – d
a b ? a c ? b d ? c d ? b a ? c a ? d a ? d · ? c
deta detb detc detd det(a ? b) det(a ? c)
x1 – 2x2 + x3 = –4
–2x1 + 3x2 + x3 = 9
3x1 + 4x2 – 5x3 = 0
– x1 + 2x2 – 3x3 + 5x4 = 14
x1 + 3x2 + 2x3 – x4 = 9
3x1–3x2 + 2x3 + 4x4 = 19
4x1 + 2x2 + 5x3 + x4 = 27
1 3 4
3 1 –2
2 3 5
x1
x2
x3
?
–3
–2
0
=
chapter 4 matrices and determinants
4-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8. use excel to find the unknowns for the system
verify your answers with the matlab left division operation.
2 4 3 –2
2 –4 1 3
–1 3 –4 2
2 –2 2 1
x1
x2
x3
x4
?
1
10
–14
7
=
numerical analysis using matlab and spreadsheets, second edition 4-35
orchard publications
solutions to exercises
4.14 solutions to exercises
1.
a. b. not conformable for addition
c. not conformable for addition d. not conformable for addition
e. f. not conformable for subtraction
g. not conformable for subtraction h. not conformable for subtraction
2.
a.
check with matlab:
a=[1 ?1 ?4 5 7 ?2 3 ?5 6] b=[5 9 ?3 ?2 8 2 7 ?4 6] a*b
ans =
-21 17 -29
-3 109 -13
67 -37 17
b.
c. not conformable for multiplication
d.
a + b
1 + 5 – 1 + 9 – 4 – 3
5 – 2 7 + 8 – 2 + 2
3 + 7 – 5 – 4 6 + 6
6 8 –7
3 15 0
10 –9 12
= = a + c
b + d c + d
a – b
1 – 5 – 1 – 9 – 4 + 3
5 + 2 7 – 8 – 2 – 2
3 – 7 – 5 + 4 6 – 6
–4 –10 –1
7 –1 –4
–4 –1 0
= = a – c
b – d c – d
a ? b
1 × 5 + (–1) × (–2) + (–4) × 7 1 × 9 + (–1) × 8 + (–4) × (–4) 1 × (–3) + (–1) × 2 + (–4) × 6
5 × 5 + 7 × (–2) + (–2) × 7 5 × 9 + 7 × 8 + (–2) × (–4) 5 × (–3) + 7 × 2 + (–2) × 6
3 × 5 + (–5) × (–2) + 6 × 7 3 × 9 + (–5) × 8 + 6 × (–4) 3 × (–3) + (–5) × 2 + 6 × 6
=
–21 17 –29
–3 109 –13
67 –37 17
=
a ? c
1 × 4 + (–1) × (–3) + (–4) × 5 1 × 6 + (–1) × 8 + (–4) × (–2)
5 × 4 + 7 × (–3) + (–2) × 5 5 × 6 + 7 × 8 + (–2) × (–2)
3 × 4 + (–5) × (–3) + 6 × 5 3 × 6 + (–5) × 8 + 6 × (–2)
–13 6
–11 90
57 –34
= =
b ? d
? d
4 × 1 + 6 × (–3) 4 × (–2) + 6 × 6 4 × 3 + 6 × (–4)
(–3) × 1 + 8 × (–3) (–3) × (–2) + 8 × 6 (–3) × 3 + 8 × (–4)
5 × 1 + (–2) × (–3) 5 × (–2) + (–2) × 6 5 × 3 + (–2) × (–4)
–14 28 –12
–27 54 –41
11 –22 23
= =
chapter 4 matrices and determinants
4-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
e.
f. not conformable for multiplication
g.
h.
3.
a.
b.
c. does not exist matrix must be square
d. does not exist matrix must be square
e. and from parts (a) and (b),
f. does not exist because does not exist
b ? a
5 × 1 + 9 × 5 + (–3) × 3 (–2) × 1 + 8 × 5 + 2 × 3 7 × 1 + (–4) × 5 + 6 × 3
5 × (–1) + 9 × 7 + (–3) × (–5) (–2) × (–1) + 8 × 7 + 2 × (–5) 7 × (–1) + (–4) × 7 + 6 × (–5)
5 × (–4) + 9 × (–2) + (–3) × 6 (–2) × (–4) + 8 × (–2) + 2 × 6 7 × (–4) + (–4) × (–2) + 6 × 6
=
41 73 –56
44 48 4
5 –65 16
=
c ? a
d ? a 1 × 1 + (–2) × 5 + 3 × 3 1 × (–1) + (–2) × 7 + 3 × (–5) 1 × (–4) + (–2) × (–2) + 3 × 6
(–3) × 1 + 6 × 5 + (–4) × 3 (–3) × (–1) + 6 × 7 + (–4) × (–5) (–3) × (–4) + 6 × (–2) + (–4) × 6
=
0 –30 18
15 65 –24
=
d ? c 1 × 4 + (–2) × (–3) + 3 × 5 1 × 6 + (–2) × 8 + 3 × (–2)
(–3) × 4 + 6 × (–3) + (–4) × 5 (–3) × 6 + 6 × 8 + (–4) × (–2)
25 –16
–50 38
= =
deta
1 –1 –4 1 –1
5 7 –2 5 7
3 –5 6 3 –5
=
= 1 × 7 × 6 + (–1) × (–2) × 3 + (–4) × 5 × (–5) – [3 × 7 × (–4) + (–5) × (–2) × 1 + 6 × 5 × (–1)]
= 42 + 6 + 100 – (–84) – 10 – (–30) = 252
detb
5 9 –3 5 9
–2 8 2 –2 8
7 –4 6 7 –4
=
= 5 × 8 × 6 + 9 × 2 × 7 + (–3) × (–2) × (–4) – [7 × 8 × (–3) + (–4) × 2 × 5 + 6 × (–2) × 9]
= 240 + 126 – 24 – (–168) + 40 – (–108) = 658
detc
detd
det(a ? b) = deta ? detb det(a ? b) = 252 × 658 = 165816
det(a ? c) detc
numerical analysis using matlab and spreadsheets, second edition 4-37
orchard publications
solutions to exercises
4.
5.
(1)
(2)
(3)
multiplication of (1) by yields
(4)
addition of (2) and (4) yields
(5)
?
1 –2 1 1 –2
–2 3 1 –2 3
3 4 –5 3 4
=
= 1 × 3 × (–5) + (–2) × 1 × 3 + 1 × (–2) × 4 – [3 × 3 × 1 + 4 × 1 × 1 + (–5) × (–2) × (–2)]
= – 15 – 6 – 8 – 9 – 4 + 20 = –22
d1
–4 –2 1 4 –2
9 3 1 9 3
0 4 –5 0 4
=
= –4 × 3 × (–5) + (–2) × 1 × 0 + 1 × 9 × 4 – [0 × 3 × 1 + 4 × 1 × 4 + (–5) × 9 × (–2)]
= 60 + 0 + 36 – 0 + 16 – 90 = 22
d2
1 –4 1 1 –4
–2 9 1 –2 9
3 0 –5 3 0
=
= 1 × 9 × (–5) + (–4) × 1 × 3 + 1 × (–2) × 0 – [3 × 9 × 1 + 0 × 1 × 1 + (–5) × (–2) × (–4)]
= – 45 – 12 – 0 – 27 – 0 + 40 = –44
d3
1 –2 –4 1 –2
–2 3 9 –2 3
3 4 0 3 4
=
= 1 × 3 × 0 + (–2) × 9 × 3 + (–4) × (–2) × 4 – [3 × 3 × (–4) + 4 × 9 × 1 + 0 × (–2) × (–2)]
= 0 – 54 + 32 + 36 – 36 – 0 = –22
x1
d1
?
- 22
–22
= = = –1 x2
d2
?
- –44
22 –
= = = 2 x3
d3
?
- –22
–22
= = = 1
x1 – 2x2 + x3 = –4
–2x1 + 3x2 + x3 = 9
3x1 + 4x2 – 5x3 = 0
2
2x1 – 4x2 + 2x3 = –8
–x2 + 3x3 = 1
chapter 4 matrices and determinants
4-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
multiplication of (1) by yields
(6)
addition of (3) and (6) yields
(7)
multiplication of (5) by 10 yields
(8)
addition of (7) and (8) yields
(9)
or
(10)
substitution of (10) into (7) yields
(11)
or
(12)
and substitution of (10) and (12) into (1) yields
(13)
or
(14)
6.
delta=[?1 2 ?3 5 1 3 2 ?1 3 ?3 2 4 4 2 5 1]
d1=[14 2 ?3 5 9 3 2 ?1 19 ?3 2 4 27 2 5 1]
d2=[?1 14 ?3 5 1 9 2 ?1 3 19 2 4 4 27 5 1]
d3=[?1 2 14 5 1 3 9 ?1 3 ?3 19 4 4 2 27 1]
d4=[?1 2 ?3 14 1 3 2 9 3 ?3 2 19 4 2 5 27]
x1=det(d1)/det(delta), x2=det(d2)/det(delta),...
x3=det(d3)/det(delta), x4=det(d4)/det(delta)
x1=1 x2=2 x3=3 x4=4
–3
–3x1 + 6x2 – 3x3 = 12
10x2 – 8x3 = 12
–10x2 + 30x3 = 10
22x3 = 22
x3 = 1
10x2 – 8 = 12
x2 = 2
x1 – 4 + 1 = –4
x1 = –1
numerical analysis using matlab and spreadsheets, second edition 4-39
orchard publications
solutions to exercises
7.
deta
1 3 4
3 1 –2
2 3 5
1 3
3 1
2 3
= 1 × 1 × 5 + 3 × (–2) × 2 + 4 × 3 × 3 – [2 × 1 × 4 + 3 × (–2) × 1 + 5 × 3 × 3]
=
= 5 – 12 + 36 – 8 + 6 – 45 = –18
adja
11 –3 –10
–19 –3 14
7 3 –8
=
a–1 1
deta
- ? adja 1
–18

11 –3 –10
–19 –3 14
7 3 –8
?
–11 ? 18 3 ? 18 10 ? 18
19 ? 18 3 ? 18 –14 ? 18
–7 ? 18 –3 ? 18 8 ? 18
= = =
x
x1
x2
x3
–11 ? 18 3 ? 18 10 ? 18
19 ? 18 3 ? 18 –14 ? 18
–7 ? 18 –3 ? 18 8 ? 18
–3
–2
0
33 ? 18 – 6 ? 18 + 0
–57 ? 18 – 6 ? 18 + 0
21 ? 18 + 6 ? 18 + 0
27 ? 18
–63 ? 18
27 ? 18
1.50
–3.50
1.50
= = = = =
chapter 4 matrices and determinants
4-40 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8.
a=[2 4 3 ?2 2 ?4 1 3 ?1 3 ?4 2 2 ?2 2 1]
b=[1 10 ?14 7] a\b
ans =
-11.5000
1.5000
12.0000
9.0000
numerical analysis using matlab and spreadsheets, second edition 5-1
orchard publications
chapter 5
differential equations, state variables, and state equations
his chapter is a review of ordinary differential equations and an introduction to state variables
and state equations. solutions of differential equations with numerical methods will be
discussed in chapter 9.
5.1 simple differential equations
in this section we present two simple examples to show the importance of differential equations in
engineering applications.
example 5.1
the current and voltage in a capacitor are related by
(5.1)
where is the current through the capacitor, is the voltage across the capacitor, and the
constant is the capacitance in farads (f). for this example and the capacitor is being
charged by a constant current . find the voltage across this capacitor as a function of time
given that the voltage at some reference time is .
solution:
it is given that the current, as a function of time, is constant, that is,
(5.2)
by substitution of (5.2) into (5.1) we get
and by separation of the variables,
(5.3)
integrating both sides of (5.3) we get
(5.4)
where represents the constants of integration of both sides.
t
ic(t) c
dvc
dt
=
ic(t) vc(t)
c c = 1 f
i vc
t = 0 v0
ic(t) = i = cons tant
dvc
dt
= i
dvc = idt
vc(t) = it + k
k
chapter 5 differential equations, state variables, and state equations
5-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we can find the value of the constant by making use of the initial condition, i.e., at ,
and (5.4) then becomes
(5.5)
or , and by substitution into (5.4),
(5.6)
this example shows that when a capacitor is charged with a constant current, a linear voltage is produced
across the terminals of the capacitor.
example 5.2
find the current through an inductor whose slope at the coordinate is and the
current passes through the point .
solution:
we are given that
(5.7)
by separating the variables we get
(5.8)
and integrating both sides we get
(5.9)
where represents the constants of integration of both sides.
we find the value of the constant by making use of the initial condition. for this example,
and thus at , . with these values (5.9) becomes
(5.10)
or , and by substitution into (5.9),
(5.11)
5.2 classification
differential equations are classified by:
1. type - ordinary or partial
2. order - the highest order derivative which is included in the differential equation
k t = 0
vc = v0
v0 = 0 + k
k = v0
vc(t) = it + v0
il(t) (t, il) cost
il (? ? 2,1)
dil
dt
- = cost
dil = costdt
il(t) = sint + k
k
k
? = 1 ?t = t = ? ? 2 il = 1
1 ?2
= sin + k
k = 0
il(t) = sint
numerical analysis using matlab and spreadsheets, second edition 5-3
orchard publications
classification
3. degree - the exponent of the highest power of the highest order derivative after the differential
equation has been cleared of any fractions or radicals in the dependent variable and its derivatives
for example, the differential equation
is an ordinary differential equation of order and degree .
if the dependent variable is a function of only a single variable , that is, if , the differential
equation which relates and is said to be an ordinary differential equation and it is abbreviated
as ode.
the differential equation
is an ode with constant coefficients.
the differential equation
is an ode with variable coefficients.
if the dependent variable is a function of two or more variables such as , where and
are independent variables, the differential equation that relates , , and is said to be a partial
differential equation and it is abbreviated as pde.
an example of a partial differential equation is the well-known one-dimensional wave equation
shown below.
most engineering problems are solved with ordinary differential equations with constant coefficients
however, partial differential equations provide often quick solutions to some practical
applications as illustrated with the following three examples.
example 5.3
the equivalent resistance of three resistors , , and in parallel is obtained from
d4y
dx4
? ?
? ?
? ?2
5 d3y
dx3
? ?
? ?
? ?4
6 d2y
dx2
? ?
? ?
? ?6
3 dy
dx
? -?
? ?8 y2
x3 + 1
+ + + + ye–2x =
4 2
y x y = f (x)
y x
d2y
dt2 3dy
dt
+ - + 2 = 5cos4t
x2 d2y
dt2 xdy
dt
+ - + (x2 – n2) = 0
y y = f (x, t) x
t yx t
?2y
?t2 a2?2y
?x2 =
rt r1 r2 r3
chapter 5 differential equations, state variables, and state equations
5-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
given that initially , , and , compute the change in if is
increased by and is decreased by while does not change.
solution:
the initial value of the equivalent resistance is .
now, we treat and as constants and differentiating with respect to we get
similarly,
and the total differential is
by substitution of the given numerical values we get
therefore, the eequivalent resistance decreases by .
example 5.4
in a series electric circuit that is excited by a sinusoidal voltage, the magnitude of the impedance
is computed from . initially, and . find the change in
the impedance if the resistance is increased by ( ) and the capacitive reactance
is decreased by ( ).
solution:
we will first find the partial derivatives and then we compute the change in impedance
from the total differential . thus,
1
rt
1
r1
- 1
r2
- 1
r3
= + + -
r1 = 5 ? r2 = 20 ? r3 = 4 ? rt r2
10% r3 5% r1
rt = 5 || 20 || 4 = 2 ?
r2 r3 rt r1
1
rt 2–
?rt
?r1
- 1
r1 2
= – - or
?rt
?r1
-
rt
r1
? ?
? ?2
=
?rt
?r2
-
rt
r2
? ?
? ?2
= and
?rt
?r3
-
rt
r3
? ?
? ?2
=
drt
drt
?rt
?r1
-dr1
?rt
?r2
-dr2
?rt
?r3
+ + -dr3
rt
r1
? ?
? ?2
dr1
rt
r2
? ?
? ?2
dr2
rt
r3
? ?
? ?2
= = + + dr3
drt
25
? ?
? ?2
(0) 2
20
? - ?
? ?2
2 ( ) 24
? ?
? ?2
= + + (–0.2) = 0.02 – 0.05 = –0.03
3%
rc
z z r 2 xc
= + 2 r = 4 ? xc = 3 ?
z r 0.25 ? 6.25%
xc 0.125 ? –4.167%
?z
?r
?z
?xc
-
dz
numerical analysis using matlab and spreadsheets, second edition 5-5
orchard publications
solutions of ordinary differential equations (ode)
and
and by substitution of the given values
therefore, if increases by and decreases by , the impedance increases by
.
example 5.5
a light bulb is rated at volts and watts. if the voltage decreases by volts and the resistance
of the bulb is increased by , by how much will the power change?
solution:
at volts and watts, the bulb resistance is
and since
and the total differential is
that is, the power will decrease by watts.
5.3 solutions of ordinary differential equations (ode)
a function is a solution of a differential equation if the latter is satisfied when and its
derivatives are replaced throughout by and its corresponding derivatives. also, the initial
conditions must be satisfied.
?z
?r
r
r 2 xc
+ 2
= and ?z
?xc
-
xc
r 2 xc
+ 2
=
dz ?z
?r
dr ?z
?xc
+ - dxc
r dr + xc dxc
r 2 xc
+ 2
= =
dz 4 (0.25) + 3 (–0.125)
4 2 + 32
- 1 – 0.375
5
= = - = 0.125
r 6.25% xc 4.167% z
4.167%
120 75 5
8 ?
v = 120 p = 75
r v2
p
- 120 2
75
= = = 192 ?
p v2
r
= - then ?p
?v
2v
r
= and ?p
?r
v2
r2 = – -
dp ?p
?v
dv ?p
?r
+ dr 2v
r
dv v2
r2 – -dr 2(120)
192
(–5) 1202
1922 = = = – -(8) = –9.375
9.375
y = f (x) y
f (x)
chapter 5 differential equations, state variables, and state equations
5-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
for example a solution of the differential equation
is
since and its second derivative satisfy the given differential equation.
any linear, time-invariant system can be described by an ode which has the form
(5.12)
if the excitation in (b12) is not zero, that is, if , the ode is called a non-homogeneous ode.
if , it reduces to:
(5.13)
the differential equation of (5.13) above is called a homogeneous ode and has different linearly
independent solutions denoted as .
we will now prove that the most general solution of (5.13) is:
(5.14)
where the subscript on the left side is used to emphasize that this is the form of the solution of
the homogeneous ode and are arbitrary constants.
proof:
let us assume that is a solution of (5.13) then by substitution,
(5.15)
d2y
dx2 + y = 0
y = k1 sinx + k2 cosx
y
an
d ny
dtn an – 1
d n – 1y
dt n – 1 - … a1
dy
dt
+ + + - + a0 y
bm
d mx
dt m - bm – 1
d m – 1x
dt n – 1 … b1
dx
dt
+ + + - + b0x
excitation (forcing) function x(t)
=
non – homogeneous differential equation
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
x(t) ? 0
x(t) = 0
an
d ny
dtn an – 1
d n – 1y
dt n – 1 - … a1
dy
dt
+ + + - + a0 y = 0
homogeneous differential equation
n
y1(t), y2(t), y3(t), …, yn(t)
yh(t) = k1 y1(t) + k2 y2(t) + k3 y3(t) +… + kn yn(t)
h
k1, k2, k3,…, kn
y1(t)
an
d ny1
dtn - an – 1
d n – 1y1
dt n – 1 - … a1
dy1
dt
+ + + + a0 y1 = 0
numerical analysis using matlab and spreadsheets, second edition 5-7
orchard publications
solutions of ordinary differential equations (ode)
a solution of the form will also satisfy (5.13) since
(5.16)
if and are any two solutions, then will also be a solution
since
and
therefore,
(5.17)
in general, if
are the solutions of the homogeneous ode of (5.13), the linear combination
is also a solution.
in our subsequent discussion, the solution of the homogeneous ode, i.e., the complementary
solution, will be referred to as the natural response, and will be denoted as or simply . the
particular solution of a non-homogeneous ode will be referred to as the forced response, and will
be denoted as or simply . accordingly, we express the total solution of the non-homogeneous
ode of (5.12) as:
(5.18)
k1y1(t)
an
d n
dtn -(k1 y1) an – 1
d n – 1
dtn – 1 -(k1 y1) … a1
d
dt
+ + + (k1 y1) + a0(k1 y1)
k1 an
d ny1
dtn - an – 1
d n – 1y1
dt n – 1 - … a1
dy1
dt
+ + + + a0 y1
? ?
? ?
? ?
= = 0
y = y1(t) y = y2(t) y = y1(t) + y2(t)
an
d ny1
dtn - an – 1
d n – 1y1
dt n – 1 - … a1
dy1
dt
+ + + + a0 y1 = 0
an
d ny2
dtn - an – 1
d n – 1y2
dt n – 1 - … a1
d y2
dt
+ + + - + a0 y2 = 0
an
d n
dtn -(y1 + y2) an – 1
d n – 1
dtn – 1 -(y1 + y2) … a1
d
dt
+ + + (y1 + y2) + a0(y1 + y2)
an
d n
dtn - y1 an – 1
d n – 1
dtn – 1 - y1 … a1
d
dt
y1 a0 y1
an
d n
dtn - y2 an – 1
d n – 1
dtn – 1 - y2 … a1
d
dt
y2 a0 y2
+ + + +
+ + + + + 0
=
=
y= k1y1(t), k2y1(t), k3y3(t), …, knyn(t)
n
y= k1y1(t) + k2y1(t) + k3y3(t) + … + knyn(t)
yn (t) yn
yf (t) yf
y(t) y natural
response
y forced
response
= + = yn + yf
chapter 5 differential equations, state variables, and state equations
5-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the natural response contains arbitrary constants and these can be evaluated from the given
initial conditions. the forced response , however, contains no arbitrary constants. it is imperative
to remember that the arbitrary constants of the natural response must be evaluated from the
total response.
5.4 solution of the homogeneous ode
let the solutions of the homogeneous ode
(5.19)
be of the form
(5.20)
then, by substitution of (5.20) into (5.19) we get
or
(5.21)
we observe that (5.21) can be satisfied when
(5.22)
but the only meaningful solution is the quantity enclosed in parentheses since the latter two yield
trivial (meaningless) solutions. we, therefore, accept the expression inside the parentheses as the
only meaningful solution and this is referred to as the characteristic (auxiliary) equation, that is,
(5.23)
since the characteristic equation is an algebraic equation of an nth-power polynomial, its solutions
are , and thus the solutions of the homogeneous ode are:
(5.24)
case i ? distinct roots
if the roots of the characteristic equation are distinct (different from each another), the solutions
of (5.23) are independent and the most general solution is:
yn
yf
an
d ny
-d t n- an – 1
d n – 1y
dt n – 1 - … a1
dy
dt
+ + + - + a0 y = 0
y = kest
an ksnest an – 1 ksn – 1+ est + … + a1 ksest + a0 kest = 0
an sn an – 1 sn – 1 ( + + … + a1 s + a0 ) kest = 0
an sn an – 1 sn – 1 ( + + … + a1 s + a0 ) = 0 or k = 0 or s = –?
an sn an – 1 sn – 1 + + …+ a1 s + a0 = 0
characteristic equation
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
s1, s2, s3, …, sn
y1 k1es1t
= y2 k2es2t
= y3 k3es3t
= … yn knesnt
, , , , =
n
numerical analysis using matlab and spreadsheets, second edition 5-9
orchard publications
solution of the homogeneous ode
(5.25)
case ii ? repeated roots
if two or more roots of the characteristic equation are repeated (same roots), then some of the
terms of (5.24) are not independent and therefore (5.25) does not represent the most general solution.
if, for example, , then,
and we see that one term of (5.25) is lost. in this case, we express one of the terms of (5.25), say
as . these two represent two independent solutions and therefore the most general
solution has the form:
(5.26)
if there are equal roots the most general solution has the form:
(5.27)
case iii ? complex roots
if the characteristic equation contains complex roots, these occur as complex conjugate pairs.
thus, if one root is where and are real numbers, then another root is
. then,
(5.28)
if (5.28) is to be a real function of time, the constants and must be complex conjugates. the
other constants , , , and the phase angle are real constants.
the forced response can be found by
a. the method of undetermined coefficients or
yn k1es1t
= k2es2t … knesnt
+ + +
for distinct roots
s1 = s2
k1e
s1t
k2e
s2t
+ k1e
s1t
k2e
s1t
+ (k1 + k2)e
s1t
k3e
s1t
= = =
k2es1t k2tes1t
yn (k1 + k2t)es1t
= k3es3t … knesnt
+ + +
m
yn k1 k2t … kmtm – 1 ( + + + ) e
s1t
= kn – ies2t … knesnt
+ + +
for m equal roots
s1 = – ? + j? ? ?
s1 = –?–j?
k1es1t k2es2t
+ k1e – ?t + j?t k2e + –?t–j?t e –?t = = (k1ej?t + k2e–j?t)
e –?t = (k1 cos?t + jk1 sin?t + k2 cos?t–jk2 sin?t)
e –?t = [(k1 + k2) cos?t + j(k1 – k2) sin?t]
e –?t (k3 cos?t + k4 sin?t) e –?t= = k5 cos(?t + ?)
for two complex conjugate roots
k1 k2
k3 k4 k5 ?
chapter 5 differential equations, state variables, and state equations
5-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
b. the method of variation of parameters
we will study the method of undetermined coefficients first.
5.5 using the method of undetermined coefficients for the forced response
for simplicity, we will only consider odes of . higher order odes are discussed in differential
equations textbooks.
consider the non-homogeneous ode
(5.29)
where , , and are real constants.
we have learned that the total (complete) solution consists of the summation of the natural and
forced responses.
for the natural response, if and are any two solutions of (5.29), the linear combination
, where and are arbitrary constants, is also a solution, that is, if we know
the two solutions, we can obtain the most general solution by forming the linear combination of
and . to be certain that there exist no other solutions, we examine the wronskian determinant
defined below.
(5.30)
if (5.30) is true, we can be assured that all solutions of (5.29) are indeed the linear combination of
and .
the forced response is obtained by observation of the right side of the given ode as it is illustrated
by the examples that follow.
example 5.6
find the total solution of the ode
(5.31)
order 2
a
t2
2
d
d y b d
dt
+ y + cy = f(x)
a b c
y1 y2
y3 = k1 y1 + k2 y2 k1 k2
y1 y2
w(y1, y2)
y1 y2
d
dx
- y1
d
dx
- y2
? y1
d
dx
- y2 y2
d
dx
= – - y1 ? 0
wronskian determinant
y1 y2
t2
2
d
d y 4dy
dt
+ - + 3y = 0
numerical analysis using matlab and spreadsheets, second edition 5-11
orchard publications
using the method of undetermined coefficients for the forced response
subject to the initial conditions and where
solution:
this is a homogeneous ode and its total solution is just the natural response found from the characteristic
equation whose roots are and . the total response is:
(5.32)
the constants and are evaluated from the given initial conditions. for this example,
or
(5.33)
also,
or
(5.34)
simultaneous solution of (5.33) and (5.34) yields and . by substitution into
(5.32), we get
(5.35)
check with matlab:
y=dsolve( d2y+4*dy+3*y=0 , y(0)=3 , dy(0)=4 )
y =
(-7/2*exp(-3*t)*exp(t)+13/2)/exp(t)
pretty(y)
- 7/2 exp(-3 t) exp(t) + 13/2
-
exp(t)
the function , shown in figure 5.1 was plotted with the use of the matlab command
ezplot(y,[0 10]).
y(0) = 3 y (0) = 4 y = dy ? dt
s2 + 4s + 3 = 0 s1 = –1 s2 = –3
y(t) yn(t) k1e–t k2e–3t = = +
k1 k2
y(0) = 3 = k1e0 + k2e0
k1 + k2 = 3
y (0) 4 dy
dt
-
t = 0
k1e–t – 3k2e–3t –
t = 0
= = =
– k1 – 3k2 = 4
k1 = 6.5 k2 = –3.5
y(t) yn(t) 6.5e–t 3.5e–3t = = –
y = f (t)
chapter 5 differential equations, state variables, and state equations
5-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 5.1. plot for the function of example 5.6.
example 5.7
find the total solution of the ode
(5.36)
subject to the initial conditions and
solution:
the left side of (5.36) is the same as that of example 5.6.therefore,
(5.37)
(we must remember that the constants and must be evaluated from the total response).
to find the forced response, we assume a solution of the form
(5.38)
we can find out whether our assumption is correct by substituting (5.38) into the given ode of
(5.36). then,
(5.39)
from which and the total solution is
y = f (t)
t2
2
d
d y 4dy
dt
+ - + 3y 3e–2t =
y(0) = 1 y (0) = –1
yn(t) k1e–t k2e–3t = +
k1 k2
yf ae–2t =
4ae–2t 8ae–2t – 3ae–2t + 3e–2t =
a = –3
numerical analysis using matlab and spreadsheets, second edition 5-13
orchard publications
using the method of undetermined coefficients for the forced response
(5.40)
the constants and are evaluated from the given initial conditions. for this example,
or
(5.41)
also,
or
(5.42)
simultaneous solution of (5.41) and (5.42) yields and . by substitution into
(5.40), we get
(5.43)
check with matlab:
y=dsolve( d2y+4*dy+3*y=3*exp(?2*t) , y(0)=1 , dy(0)=?1 )
y =
(-3*exp(-2*t)*exp(t)+3/2*exp(-3*t)*exp(t)+5/2)/exp(t)
pretty(y)
-3 exp(-2 t) exp(t) + 3/2 exp(-3 t) exp(t) + 5/2

exp(t)
ezplot(y,[0 8])
the plot is shown in figure 5.2
example 5.8
find the total solution of the ode
(5.44)
subject to the initial conditions and
y(t) yn + yf k1e–t k2e–3t –3e–2t = = +
k1 k2
y(0) = 1 = k1e0 + k2e0 – 3e0
k1 + k2 = 4
y (0) –1 dy
dt
-
t = 0
k1e–t – 3k2e–3t – 6e–2t +
t = 0
= = =
– k1 – 3k2 = –7
k1 = 2.5 k2 = 1.5
y(t) yn + yf 2.5e–t 1.5e–3t –3e–2t = = +
t2
2
d
d y 6dy
dt
+ - + 9y = 0
y(0) = –1 y (0) = 1
chapter 5 differential equations, state variables, and state equations
5-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 5.2. plot for the function of example 5.7.
solution:
this is a homogeneous ode and therefore its total solution is just the natural response found
from the characteristic equation whose roots are (repeated roots).
thus, the total response is
(5.45)
next, we evaluate the constants and from the given initial conditions. for this example,
or
(5.46)
also,
or
(5.47)
from (5.46) and (5.47) we get yields and . by substitution into (5.45),
(5.48)
check with matlab:
y=dsolve( d2y+6*dy+9*y=0 , y(0)=?1 , dy(0)=1 )
y = f (t)
s2 + 6s + 9 = 0 s1 = s2 = –3
y(t) yn k1e–3t k2 te–3t = = +
k1 k2
y(0) = –1 = k1e0 + k2(0)e0
k1 = –1
y (0) 1 dy
dt
-
t = 0
3k1e–3t – k2e–3t 3k2te–3t + –
t = 0
= = =
– 3k1 + k2 = 1
k1 = –1 k2 = –2
y(t) –e–3t 2te–3t = –
numerical analysis using matlab and spreadsheets, second edition 5-15
orchard publications
using the method of undetermined coefficients for the forced response
y =
-exp(-3*t)-2*exp(-3*t)*t
ezplot(y,[0 4])
the plot is shown in figure 5.3.
figure 5.3. plot for the function of example 5.8.
example 5.9
find the total solution of the ode
(5.49)
solution:
no initial conditions are given therefore, we will express the solution in terms of the constants
and . by inspection, the roots of the characteristic equation of (5.48) are and
and thus the natural response has the form
(5.50)
next, we find the forced response by assuming a solution of the form
(5.51)
we can find out whether our assumption is correct by substitution of (5.51) into the given ode of
(5.49). then,
(5.52)
y = f (t)
t2
2
d
d y 5dy
dt
+ - + 6y 3e–2t =
k1
k2 s1 = –2 s2 = –3
yn k1e–2t k2 e–3t = +
yf ae–2t =
4ae–2t 10ae–2t – 6ae–2t + 3e–2t =
chapter 5 differential equations, state variables, and state equations
5-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
but the sum of the three terms on the left side of (5.52) is zero whereas the right side can never be
zero unless we let and this produces a meaningless result.
the problem here is that the right side of the given ode of (5.49) has the same form as one of the
terms of the natural response of (5.50), namely the term .
to work around this problem, we assume that the forced response has the form
(5.53)
that is, we multiply (5.51) by in order to eliminate the duplication of terms in the total response.
then, by substitution of (5.53) into (5.49) and equating like terms, we find that . therefore,
the total response is
(5.54)
check with matlab:
y=dsolve( d2y+5*dy+6*y=3*exp(?2*t) )
y =
-3*exp(-2*t)+3*t*exp(-2*t)+c1*exp(-3*t)+c2*exp(-2*t)
we observe that the first and last terms of the displayed expression above have the same form and
thus they can be combined to form a single term c3*exp(-2*t).
example 5.10
find the total solution of the ode
(5.55)
solution:
no initial conditions are given therefore, we will express solution in terms of the constants
and . we observe that the left side of (5.55) is the same of that of example 5.9. therefore, the
natural response is the same, that is, it has the form
(5.56)
next, to find the forced response and we assume a solution of the form
(5.57)
we can find out whether our assumption is correct by substitution of the assumed solution of
t ? ?
k1 e–2t
yf ate–2t =
t
a = 3
y(t) yn + yf k1e–2t k2e–3t 3te–2t = = + +
t2
2
d
d y 5dy
dt
+ - + 6y = 4cos5t
k1
k2
yn k1e–2t k2e–3t = +
yf = acos5t
numerical analysis using matlab and spreadsheets, second edition 5-17
orchard publications
using the method of undetermined coefficients for the forced response
(5.57) into the given ode of (5.56). then,
but this relation is invalid since by equating cosine and sine terms, we find that and
also . this inconsistency is a result of our failure to recognize that the derivatives of
produce new terms of the form and these terms must be included in the forced response.
accordingly, we let
(5.58)
and by substitution into (5.54) we get
collecting like terms and equating sine and cosine terms, we obtain the following set of equations
(5.59)
we use matlab to solve (5.59)
format rat [k3 k4]=solve(19*x+25*y, 25*x?19*y?4)
k3 =
50/493
k4 =
-38/493
therefore, the total solution is
(5.60)
check with matlab:
y=dsolve( d2y+5*dy+6*y=4*cos(5*t) ) y=simple(y)
y =
-38/493*cos(5*t)+50/493*sin(5*t)+c1*exp(-3*t)+c2*exp(-2*t)
in most engineering problems the right side of the non-homogeneous ode consists of elementary
functions such as (constant), where is a positive integer, , , , and linear
combinations of these. table 5.1 summarizes the forms of the forced response for a second order
ode with constant coefficients.
we must remember that if is the sum of several terms, the most general form of the forced
response is the linear combination of these terms. also, if a term in is a duplicate of a
– 25acos5t – 25asin5t + 6acos5t = – 19acos5t – 25asin5t = 4cos5t
a = –4 ? 19
a = 0 acos5t
bsin5t
yf = k3 sin5t + k4 cos5t
–25k3 sin5t – 25k4 cos5t + 25k3 cos5t – 25k4 sin5t + 6k3 sin5t + 6k4 cos5t = 4cos5t
19k3 + 25k4 = 0
25k3–19k4 = 4
y(t) yn + yf(t) k1e–2t k2e–3t 50
493
sin5t –38
493
= = + + + cos5t
k xn n ekx coskx sinkx
f (t)
yf (t) yf (t)
chapter 5 differential equations, state variables, and state equations
5-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
term in the natural response , we must multiply by the lowest power of that will
eliminate the duplication.
example 5.11
find the total solution of the ode
(5.61)
solution:
no initial conditions are given therefore we will express solution in terms of the constants and
. the roots of the characteristic equation are equal, that is, , and thus the natural
response has the form
(5.62)
to find the forced response (particular solution), we refer to the table of the previous page and
from the last row we choose the term . this term with , , and ,
reduces to . therefore the forced response will have the form
(5.63)
but the terms and are also present in (5.61) therefore, we multiply (5.62) by to
table 5.1 form of the forced response for 2nd order differential equations
forced response of the ode
form of forced response
(constant) (constant)
( = positive integer)
( =real or complex)
or ( =constant)
or
+
yn (t) yf (t) t
ad2y
dt2 bdy
dt
+ - + cy = f (t)
f (t) yf (t)
k k
k t n n k0 t n k1 tn – 1 + + … + kn – 1t + kn
ker tr ker t
k cos?t k sin?t ? k1cos?t + k2 sin?t
k t ner t cos?t k t ner t sin?t k0 t n k1 tn – 1 ( + + … + kn – 1t + kn)er t cos?t
k0 t n k1 tn – 1 ( + + … + kn – 1t + kn)er t sin?t
t2
2
d
d y 4dy
dt
+ - + 4y te–2t e–2t = –
k1
k2 s1 = s2 = –2
yn = k1e–2t + k2 te–2t
k t ner t cos?t n = 1 r = –2 ? = 0
kte–2t
yf = (k3 t + k4)e–2t
e–2t te–2t t2
numerical analysis using matlab and spreadsheets, second edition 5-19
orchard publications
using the method of variation of parameters for the forced response
obtain a suitable form for the forced response which now is
(5.64)
now, we need to evaluate the constants and . this is done by substituting (5.64) into the
given ode of (5.61) and equating with the right side. we use matlab do the computations as
shown below.
syms t k3 k4 % define symbolic variables
f0=(k3*t^3+k4*t^2)*exp(?2*t) % forced response (5.64)
f1=diff(f0) f1=simple(f1) % compute and simplify first derivative
f1 =
-t*exp(-2*t)*(-3*k3*t-2*k4+2*k3*t^2+2*k4*t)
f2=diff(f0,2) f2=simple(f2) % compute and simplify second derivative
f2 =
2*exp(-2*t)*(3*k3*t+k4-6*k3*t^2-4*k4*t+2*k3*t^3+2*k4*t^2)
f=f2+4*f1+4*f0 f=simple(f) % form and simplify the left side of the given ode
f = 2*(3*k3*t+k4)*exp(-2*t)
finally, we equate f above with the right side of the given ode, that is
(5.65)
and we find and . by substitution of these values into (5.64) and combining
the forced response with the natural response, we get the total solution
(5.66)
we verify this solution with matlab as follows:
z=dsolve( d2y+4*dy+4*y=t*exp(?2*t)?exp(?2*t) )
z =
1/6*exp(-2*t)*t^3-1/2*exp(-2*t)*t^2
+c1*exp(-2*t)+c2*t*exp(-2*t)
5.6 using the method of variation of parameters for the forced response
in certain non-homogeneous odes, the right side cannot be determined by the method of
undetermined coefficients. for these odes we must use the method of variation of parameters.
this method will work with all linear equations including those with variable coefficients such as
yf = (k3 t3 + k4 t2)e–2t
k3 k4
2 3k3 ( t + k4)e–2t te–2t e–2t = –
k3 = 1 ? 6 k4 = –1 ? 2
y t ( ) k1e 2 – t k2te 2 – t 16 t3e 2 – t 12
t2e –2t
= + + –
f (t)
chapter 5 differential equations, state variables, and state equations
5-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.67)
provided that the general form of the natural response is known.
our discussion will be restricted to second order odes with constant coefficients.
the method of variation of parameters replaces the constants and by two variables and
that satisfy the following three relations:
(5.68)
(5.69)
(5.70)
simultaneous solution of (5.68) and (5.69) will yield the values of and then, integration
of these will produce and , which when substituted into (5.67) will yield the total
solution.
example 5.12
find the total solution of
(5.71)
in terms of the constants and by the
a. method of undetermined coefficients
b. method of variation of parameters
solution:
with either method, we must first find the natural response. the characteristic equation yields
the roots and . therefore, the natural response is
(5.72)
a. using the method of undetermined coefficients we let (a constant). then, by substitution
into (5.70) we get and thus the total solution is
d2y
dt2 ?(t)dy
dt
+ - + ?(t)y = f (t)
k1 k2 u1
u2
y = u1 y1 + u2 y2
du1
dt
- y1
du2
dt
+ - y2 = 0
du1
dt
-
dy1
dt
? -
du2
dt

dy2
dt
+ ? = f (t)
du1 ? dt du2 ? dt
u1 u2
d2y
dt2 4dy
dt
+ - + 3y = 12
k1 k2
s1 = –1 s2 = –3
yn k1e–t = + k2 e–3t
yf = k3
k3 = 4
numerical analysis using matlab and spreadsheets, second edition 5-21
orchard publications
using the method of variation of parameters for the forced response
(5.73)
b. with the method of variation of parameters we start with the natural response found above as
(5.71) and we let the solutions and be represented as
(5.74)
then by (5.68), the total solution is
or
(5.75)
also, from (5.69),
or
(5.76)
and from (5.70),
or
(5.77)
next, we find and by cramer’s rule as follows:
(5.78)
and
(5.79)
now, integration of (5.78) and (5.79) and substitution into (5.75) yields
y(t) yn + yf k1e–t = = + k2e–3t + 4
y1 y2
y1 e–t = and y2 e–3t =
y = u1y1 + u2y2
y u1e–t u2e–3t = +
du1
dt
y1
du2
dt
+ y2 = 0
du1
dt
e–t du2
dt
e–3t + = 0
du1
dt
-
dy1
dt
? -
du2
dt

dy2
dt
+ ? = f(t)
du1
dt
e–t (– )
du2
dt
3e–3t + (– ) = 12
du1 ? dt du2 ? dt
du1
dt

0 e–3t
12 3e–3t –
e–t e–3t
e–t – 3e–3t –
12e 3t – –
3e–4t – e–4t +
- 12e 3t – –
2e–4t –
= = = = 6et
du2
dt

e–t 0
e–t – 12
2e–4t –
- 12e t –
2e–4t –
= = - = –6e3t
chapter 5 differential equations, state variables, and state equations
5-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.80)
(5.81)
we observe that the last expression in (5.81) is the same as (5.73) of part (a).
check with matlab:
y=dsolve( d2y+4*dy+3*y=12 )
y =
(4*exp(t)+c1*exp(-3*t)*exp(t)+c2)/exp(t)
example 5.13
find the total solution of
(5.82)
in terms of the constants and by any method.
solution:
this ode cannot be solved by the method of undetermined coefficients therefore, we will use
the method of variation of parameters.
the characteristic equation is from which and thus the natural response is
(5.83)
we let
(5.84)
then, by (5.68) the solution is
(5.85)
also, from (5.69),
or
(5.86)
u1 = 6?etdt = 6et + k1 u2 = –6?e3tdt = –2e3t + k2
y u1e–t u2e–3t + (6et + k1)e–t (–2e3t + k2)e–3t = = +
6 k1e–t 2 k2e–3t + – + k1e–t k2e–3t = = + + 4
d2y
dt2 + 4y = tan2t
k1 k2
s2 + 4 = 0 s = ±j2
yn = k1ej2t + k2e–j2t
y1 = cos2t and y2 = sin2t
y = u1y1 + u2y2 = u1 cos2t + u2 sin2t
du1
dt
-y1
du2
dt
+ y2 = 0
du1
dt
- cos2t
du2
dt
+ sin2t = 0
numerical analysis using matlab and spreadsheets, second edition 5-23
orchard publications
expressing differential equations in state equation form
and from (5.70),
(5.87)
next, we find and by cramer’s rule as follows:
(5.88)
and
(5.89)
now, integration of (5.88) and (5.89) and substitution into (5.85) yields
(5.90)
(5.91)
(5.92)
check with matlab:
y=dsolve( d2y+4*y=tan(2*t) )
y =
-1/4*cos(2*t)*log((1+sin(2*t))/cos(2*t))+c1*cos(2*t)+c2*sin(2*t)
5.7 expressing differential equations in state equation form
a first order differential equation with constant coefficients has the form
(5.93)
in a second order differential equation the highest order is a second derivative.
an nth-order differential equation can be resolved to first-order simultaneous differential equadu1
dt
-
dy1
dt
? -
du2
dt

dy2
dt
+ ? f(t)
du1
dt
= = -(–2sin2t)
du2
dt
+ (2cos2t) = tan2t
du1 ? dt du2 ? dt
du1
dt
-
0 sin2t
tan2t 2cos2t
cos2t sin2t
–2sin2t 2cos2t

sin22t
cos2t

2cos22t + 2sin22t
2t –sin2
2cos2t
= = =
du2
dt

cos2t 0
–2sin2t tan2t
2
sin2t
2
= =
u1
12
– sin22t
cos2t
? dt sin2t
4
14
= = – ln( sec2t + tan2t) + k1
u2
12
?sin2tdt cos2t
4
= = – - + k2
y u1y1 + u2y2 sin2t cos2t
4
- 14
– cos2t ln( sec2t + tan2t) + k1 cos2t sin2t cos2t
4
= = – - + k2 sin2t
14
= – cos2t ln( sec2t + tan2t) + k1 cos2t + k2 sin2t
a1
dy
dt
- + a0 y(t) = x(t)
n
chapter 5 differential equations, state variables, and state equations
5-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
tions with a set of auxiliary variables called state variables. the resulting first-order differential
equations are called state space equations, or simply state equations. the state variable method offers
the advantage that it can also be used with non-linear and time-varying systems. however, our
discussion will be limited to linear, time-invariant systems.
state equations can also be solved with numerical methods such as taylor series and runge-kutta
methods these will be discussed in chapter 9. the state variable method is best illustrated
through several examples presented in this chapter.
example 5.14
a system is described by the integro-differential equation
(5.94)
differentiating both sides and dividing by we get
(5.95)
or
(5.96)
next, we define two state variables and such that
(5.97)
and
(5.98)
then,
(5.99)
where denotes the derivative of the state variable .
from (5.96) through (5.99), we obtain the state equations
(5.100)
it is convenient and customary to express the state equations in matrix form. thus, we write the
state equations of (5.100) as
ri ldi
dt
1c
- idt
–?
t
+ + ? = e j?t
l
d2t
dt2 - rl
- di
dt
1
lc
+ + - i 1l
= j?e j?t
d2t
dt2 - rl
- di
dt
1
lc
- i 1l
= – – + j?e j?t
x1 x2
x1 = i
x2
di
dt
dx1
dt
- x· = = = 1

2 d2i dt2
= ?

k xk

1 = x2

2
rl
– x2 1
lc
– x1 1
l
= + j?e j?t
numerical analysis using matlab and spreadsheets, second edition 5-25
orchard publications
expressing differential equations in state equation form
(5.101)
we usually write (5.101) in a compact form as
(5.102)
where
(5.103)
the output is expressed by the state equation
(5.104)
where is another matrix, and is a column vector. therefore, the state representation of a system
can be described by the pair of the of the state space equations
(5.105)
the state space equations of (5.105) can be realized with the block diagram of figure 5.1.
figure 5.4. block diagram for the realization of the state equations of (5.105)
we will learn how to solve the matrix equations of (5.105) in the subsequent sections.
example 5.15
a fourth-order system is described by the differential equation

1

2
0 1
1
lc
– rl

x1
x2
0
1l
j?e j?t = + u

= ax + bu


1

2
a
0 1
1
lc
– rl

, = , x x1
x2
b
0
1l
j?e j?t = = , = , and u = any input
y(t)
y = cx + du
c d

= ax + bu
y = cx + du
u b
a
c
d
x x·
+ +
+ ? ?dt + ? y
chapter 5 differential equations, state variables, and state equations
5-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.106)
where is the output and is any input. express (5.15) as a set of state equations.
solution:
the differential equation of (5.106) is of fourth-order therefore, we must define four state variables
that will be used with the resulting four first-order state equations.
we denote the state variables as , and , and we relate them to the terms of the given
differential equation as
(5.107)
we observe that
(5.108)
and in matrix form
(5.109)
in compact form, (5.109) is written as
(5.110)
where
d 4y
dt4 - a3
d 3y
dt3 - a2
d2y
dt2 a1
dy
dt
+ + + - + a0 y(t) = u(t)
y(t) u(t)
x1, x2, x3 x4
x1 = y(t) x2
dy
dt
= - x3
d 2y
dt2 = - x4
d 3y
dt3 = -

1 = x2

2 = x3

3 = x4
d 4y
dt4 - x· = 4 = –a0x1–a1x2 – a2x3–a3x4 + u(t)

1

2

3

4
0 1 0 0
0 0 1 0
0 0 0 1
–a0 –a1 –a2 –a3
x1
x2
x3
x4
0
0
0
1
= + u(t)

= ax + bu


1

2

3

4
= a
0 1 0 0
0 0 1 0
0 0 0 1
–a0 –a1 –a2 –a3
= x
x1
x2
x3
x4
= b
0
0
0
1
, , , = , and u = u(t)
numerical analysis using matlab and spreadsheets, second edition 5-27
orchard publications
solution of single state equations
5.8 solution of single state equations
let us consider the state equations
(5.111)
where , , , and are scalar constants, and the initial condition, if non-zero, is denoted as
(5.112)
we will now prove that the solution of the first state equation in (5.111) is
(5.113)
proof:
first, we must show that (5.113) satisfies the initial condition of (5.112). this is done by substitution
of in (5.113). then,
(5.114)
the first term in the right side of (114) reduces to since
(5.115)
the second term of (5.114) is zero since the upper and lower limits of integration are the same.
therefore, (5.114) reduces to and thus the initial condition is satisfied.
next, we must prove that (5.113) satisfies also the first equation in (5.111). to prove this, we differentiate
(5.113) with respect to and we get
(5.116)

= ?x + ?u
y = k1x + k2u
? ? k1 k2
x0 = x(t0)
x(t) e?(t – t0)x0 e?t e –???u(?)d?
t0
t
= + ?
t = t0
x(t0) e?(t0 – t0)x0 e?t e –???u(?)d?
t0
t0 = + ?
x0
e?(t0 – t0)x0 = e0x0 = x0
x(t0) = x0
t

(t) d
dt
e?(t – t0)( x0) d
dt
e?t e –???u(?)d?
t0
t
? ? ?
? ?
? ?
= +

(t) ?e?(t – t0)x0 ?e?t e –???u(?) ? e?t e –??[ ?u(?)] ? = t d +
t0
t
= + ?
? e?(t – t0)x0 e?t e –???u(?)d?
t0
t
+ ? e?te –?t= + ?u(t)

(t) ? e?(t – t0)x0 e?(t – ?)?u(?)d?
t0
t
= + ? + ?u(t)
chapter 5 differential equations, state variables, and state equations
5-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we observe that the bracketed terms of (5.116) are the same as the right side of the assumed solution
of (5.113). therefore,
and this is the same as the first equation of (5.111). the second equation of (5.111) is an algebraic
equation whose coefficients are scalar constants.
in summary, if and are scalar constants, the solution of
(5.117)
with initial condition
(5.118)
is obtained from the relation
(5.119)
5.9 the state transition matrix
let us again consider the state equations pair
(5.120)
where for two or more simultaneous differential equations and are or higher order
matrices, and and are column vectors with two or more rows. in this section we will introduce
the state transition matrix , and we will prove that the solution of the matrix differential
equation
(5.121)
with initial conditions
(5.122)
is obtained from the relation
(5.123)
proof:
let be any matrix whose elements are constants. then, another matrix denoted as
, is said to be the state transition matrix of (5.34), if it is related to the matrix as the matrix
power series
(5.124)

= ?x + ?u
? ?

= ?x + ?u
x0 = x(t0)
x(t) e?(t – t0)x0 e?t e –???u(?)d?
t0
t
= + ?

= ax + bu
y = cx + du
a c 2 × 2
b d
eat

= ax + bu
x(t0) = x0
x(t) ea t t0
( – )x0 eat e–a?bu(?)d?
t0
t
= + ?
a n × n n × n
?(t) a
?(t) eat i at 1
2!
a2t2 1
3!
a3t3 … 1
n!
? = + + + + + antn
numerical analysis using matlab and spreadsheets, second edition 5-29
orchard publications
the state transition matrix
where is the identity matrix.
from (5.124), we find that
(5.125)
differentiation of (5.124) with respect to yields
(5.126)
and by comparison with (5.124) we get
(5.127)
to prove that (5.123) is the solution of the first equation of (5.120), we must prove that it satisfies
both the initial condition and the matrix differential equation. the initial condition is satisfied
from the relation
(5.128)
where we have used (5.125) for the initial condition. the integral is zero since the upper and lower
limits of integration are the same.
to prove that the first equation of (5.120) is also satisfied, we differentiate the assumed solution
with respect to and we use (5.127), that is,
then,
or
(5.129)
we recognize the bracketed terms in (5.129) as , and the last term as . thus, the expression
(5.129) reduces to
i n × n
?(0) = ea0 = i + a0 + … = i
t
? (t) d
dt
eat 0 a 1 a2
= = + ? + t + … = a + a2t + …
d
dt
eat = aeat
x(t0) ea(t0 – t0)x0 eat0 e–a?bu(?)d?
t0
t0 = + ? = ea0x0 + 0 = ix0 = x0
x(t) ea t t0
( – )x0 eat e–a?bu(?)d?
t0
t
= + ?
t
d
dt
eat = aeat

(t) aea t t0
( – )x0 aeat e–a?bu(?)d?
t0
t
= + ? + eate–atbu(t)

(t) a ea t t0
( – )x0 eat e–a?bu(?)d?
t0
t
= + ? + eate–atbu(t)
x(t) bu(t)

(t) = ax + bu
chapter 5 differential equations, state variables, and state equations
5-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
in summary, if is an matrix whose elements are constants, , and is a column vector
with n elements, the solution of
(5.130)
with initial condition
(5.131)
is
(5.132)
therefore, the solution of second or higher order systems using the state variable method, entails
the computation of the state transition matrix , and integration of (5.132).
5.10 computation of the state transition matrix
let be an matrix, and be the identity matrix. by definition, the eigenvalues ,
of are the roots of the nth order polynomial
(5.133)
we recall that expansion of a determinant produces a polynomial. the roots of the polynomial of
(5.133) can be real (unequal or equal), or complex numbers.
evaluation of the state transition matrix is based on the cayley-hamilton theorem. this theorem
states that a matrix can be expressed as an degree polynomial in terms of the matrix
as
(5.134)
where the coefficients are functions of the eigenvalues . we accept (5.134) without proving
it. the proof can be found in linear algebra and matrix theory textbooks.
since the coefficients are functions of the eigenvalues , we must consider the following cases:
case i: distinct eigenvalues (real or complex)
if , that is, if all eigenvalues of a given matrix are distinct, the
coefficients are found from the simultaneous solution of the following system of equations:
a n × n n ? 2 b

(t) = ax + bu
x0 = x(t0)
x(t) ea t t0
( – )x0 eat e–a?bu(?)d?
t0
t
= + ?
eat
eat
a n × n i n × n ?i
i = 1, 2, …, n a
det[a – ?i] = 0
eat
(n – 1)th
a
eat a0i a1a a2a2 … an – 1an – 1 = + + + +
ai ?
ai ?
?1 ? ?2 ? ?3 ? …? ?n a
ai
numerical analysis using matlab and spreadsheets, second edition 5-31
orchard publications
computation of the state transition matrix
(5.135)
example 5.16
compute the state transition matrix given that
solution:
we must first find the eigenvalues of the given matrix . these are found from the expansion
of
for this example,
or
therefore,
(5.136)
next, we must find the coefficients of (5.134). since is a matrix, we only need to
consider the first two terms of that relation, that is,
(5.137)
the coefficients and are found from (5.137). for this example,
or
(5.138)
a0 a1?1 a2?1
2 … an – 1?1
n – 1 + + + + e?1t
=
a0 a1?2 a2?2
2 … an 1 – ?2
n – 1 + + + + e?2t
=

a0 a1?n a2?n 2 … an – 1?n
n – 1 + + + + e?nt
=
eat a –2 1
0 –1
=
? a
det[a – ?i] = 0
det[a – ?i] det –2 1
0 –1
? 1 0
0 1

? ?
? ?
? ?
det – 2 – ? 1
0 – 1 – ?
= = = 0
= (– 2 – ?)(– 1 – ?) = 0
(? + 1)(? + 2) = 0
?1 = –1 and ?2 = –2
ai a 2 × 2
eat = a0i + a1a
a0 a1
a0 + a1?1 e?1t
=
a0 + a1?2 e?2t
=
a0 + a1(–1) e–t =
a0 + a1(–2) e–2t =
chapter 5 differential equations, state variables, and state equations
5-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
simultaneous solution of (5.138) yields
(5.139)
and by substitution into (5.137),
or
(5.140)
in summary, we compute the state transition matrix for a given matrix using the following
procedure:
1. we find the eigenvalues from . we can write at once by subtracting
from each of the main diagonal elements of . if the dimension of is a
matrix, it will yield two eigenvalues if it is a matrix, it will yield three eigenvalues,
and so on. if the eigenvalues are distinct, we perform steps 2 through 4 otherwise we refer
to case ii below.
2. if the dimension of is a matrix, we use only the first 2 terms of the right side of the
state transition matrix
(5.141)
if matrix is a matrix, we use the first 3 terms, and so on.
3. we obtain the coefficients from
we use as many equations as the number of the eigenvalues, and we solve for the coefficients
.
4. we substitute the coefficients into the state transition matrix of (5.141), and we simplify.
a0 2e–t e–2t = –
a1 e–t e–2t = –
eat 2e–t e–2t ( – ) 1 0
0 1
e–t e–2t ( – ) –2 1
0 –1
= +
eat e 2t – e–t e–2t –
0 e–t
=
eat a
? det[a – ?i] = 0 [a – ?i]
? a a 2 × 2
3 × 3
a 2 × 2
eat a0i a1a a2a2 … an – 1an – 1 = + + + +
a 3 × 3
ai
a0 a1?1 a2?1
2 … an 1 – ?1
n – 1 + + + + e?1t
=
a0 a1?2 a2?2
2 … an 1 – ?2
n – 1 + + + + e?2t
=

a0 a1?n a2?n 2 … an – 1?n
n – 1 + + + + e?nt
=
ai
ai
numerical analysis using matlab and spreadsheets, second edition 5-33
orchard publications
computation of the state transition matrix
example 5.17
compute the state transition matrix given that
(5.142)
solution:
1. we first compute the eigenvalues from . we obtain at once, by
subtracting from each of the main diagonal elements of . then,
(5.143)
and expansion of this determinant yields the polynomial
(5.144)
we will use matlab roots(p) function to obtain the roots of (5.57).
p=[1 ?6 11 ?6] r=roots(p) fprintf( \n ) fprintf( lambda1 = %5.2f \t , r(1)) ...
fprintf( lambda2 = %5.2f \t , r(2)) fprintf( lambda3 = %5.2f , r(3))
lambda1 = 3.00 lambda2 = 2.00 lambda3 = 1.00
and thus the eigenvalues are
(5.145)
2. since is a matrix, we need to use the first terms of (5.134), that is,
(5.146)
3. we obtain the coefficients from
or
eat
a
5 7 –5
0 4 –1
2 8 –3
=
det[a – ?i] = 0 [a – ?i]
? a
det[a – ?i] det
5 – ? 7 –5
0 4 – ? –1
2 8 – 3 – ?
= = 0
?3 – 6?2 + 11? – 6 = 0
?1 = 1 ?2 = 2 ?3 = 3
a 3 × 3 3
eat = a0i + a1a + a2a2
a0, a1, and a2
a0 a1?1 a2?1
+ + 2 e?1t
=
a0 a1?2 a2?2
+ + 2 e?2t
=
a0 a1?3 a2?3
+ + 2 e?3t
=
chapter 5 differential equations, state variables, and state equations
5-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.147)
we will use the following matlab code for the solution of (5.147).
b=sym( [1 1 1 1 2 4 1 3 9] ) b=sym( [exp(t) exp(2*t) exp(3*t)] ) a=b\b fprintf( \n ) ...
disp( a0 = ) disp(a(1)) disp( a1 = ) disp(a(2)) disp( a2 = ) disp(a(3))
a0 =
3*exp(t)-3*exp(2*t)+exp(3*t)
a1 =
-5/2*exp(t)+4*exp(2*t)-3/2*exp(3*t)
a2 =
1/2*exp(t)-exp(2*t)+1/2*exp(3*t)
thus,
(5.148)
4. we also use matlab to perform the substitution into the state transition matrix, and to
perform the matrix multiplications. the code is shown below.
syms t a0 = 3*exp(t)+exp(3*t)?3*exp(2*t) a1 = ?5/2*exp(t)?3/2*exp(3*t)+4*exp(2*t) ...
a2 = 1/2*exp(t)+1/2*exp(3*t)?exp(2*t) ...
a = [5 7 ?5 0 4 ?1 2 8 ?3] eat=a0*eye(3)+a1*a+a2*a^2
eat =
[-2*exp(t)+2*exp(2*t)+exp(3*t),-6*exp(t)+5*exp(2*t)+exp(3*t),
4*exp(t)-3*exp(2*t)-exp(3*t)]
[-exp(t)+2*exp(2*t)-exp(3*t),-3*exp(t)+5*exp(2*t)-exp(3*t),
2*exp(t)-3*exp(2*t)+exp(3*t)]
[-3*exp(t)+4*exp(2*t)-exp(3*t),-9*exp(t)+10*exp(2*t)-exp(3*t),
6*exp(t)-6*exp(2*t)+exp(3*t)]
thus,
a0 + a1 + a2 = et
a0 + 2a1 + 4a2 = e2t
a0 + 3a1 + 9a2 = e3t
a0 = 3et – 3e2t + e3t
a1
52
et – 4e2t 32
= + – e3t
a2
12
et – e2t 12
= + e3t
eat
– 2et + 2e2t + e3t –6et + 5e2t + e3t 4et – 3e2t – e3t
– et + 2e2t – e3t – 3et + 5e2t – e3t 2et – 3e2t + e3t
– 3et + 4e2t – e3t – 9et + 10e2t – e3t 6et – 6e2t + e3t
=
numerical analysis using matlab and spreadsheets, second edition 5-35
orchard publications
computation of the state transition matrix
case ii: multiple eigenvalues
in this case, we will assume that the polynomial of
(5.149)
has roots, and of these roots are equal. in other words, the roots are
(5.150)
the coefficients of the state transition matrix
(5.151)
are found from the simultaneous solution of the system of equations of (5.65) below.
(5.152)
example 5.18
compute the state transition matrix given that
solution:
1. we first find the eigenvalues of the matrix and these are found from the polynomial of
. for this example,
det[a – ?i] = 0
n m
?1 = ?2= ?3… = ?m, ?m + 1 , ?n
ai
eat a0i a1a a2a2 … an – 1an – 1 = + + + +
a0 a1?1 a2?1 2 … an – 1?1
n – 1 + + + + e?1t
=
d
d?1
a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) d
d?1
e?1t
=
d 2
d?1 2 a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) d 2
d?1 2 e?1t
=

d m – 1
d?1
m – 1 - a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) dm – 1
d?1
m – 1 -e?1t
=
a0 a1?m + 1 a2?m + 1
2 … an 1 – ?m + 1
n – 1 + + + + e? m + 1t
=

a0 a1?n a2?n 2 … an – 1?n
n – 1 + + + + e?nt
=
eat
a –1 0
2 –1
=
? a
det[a – ?i] = 0
chapter 5 differential equations, state variables, and state equations
5-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and thus,
2. since is a matrix, we only need the first two terms of the state transition matrix,
that is,
(5.153)
3. we find and from (5.65). for this example,
or
and by substitution with , we get
simultaneous solution of the last two equations yields
(5.154)
4. by substitution of (5.67) into (5.66), we get
(5.155)
we can use the matlab eig(x) function to find the eigenvalues of an matrix. to find out
how it is used, we invoke the help eig command.
det[a – ?i] det – 1 – ? 0
2 – 1 – ?
= = 0
= (– 1 – ?)(– 1 – ?) = 0
= (? + 1)2 = 0
?1 = ?2 = –1
a 2 × 2
eat = a0i + a1a
a0 a1
a0 + a1?1 e?1t
=
d
d?1
(a0 + a1?1) d
d?1
e?1t
=
a0 + a1?1 e?1t
=
a1 te?1t
=
?1 = ?2 = –1
a0 – a1 e–t =
a1 te–t =
a0 e–t te–t = +
a1 te–t =
eat e–t te–t ( + ) 1 0
0 1
te–t –1 0
2 –1
+ eat e t – 0
2te–t e–t
= = =
n × n
numerical analysis using matlab and spreadsheets, second edition 5-37
orchard publications
eigenvectors
we will first use matlab to verify the values of the eigenvalues found in examples 5.6 through
5.8, and we will briefly discuss eigenvectors on the next section.
for example 5.6
a= [?2 1 0 ?1] lambda=eig(a)
lambda =
-2
-1
for example 5.7
b = [5 7 ?5 0 4 ?1 2 8 ?3] lambda=eig(b)
lambda =
1.0000
3.0000
2.0000
for example 5.8
c = [?1 0 2 ?1] lambda=eig(c)
lambda =
-1
-1
5.11 eigenvectors
consider the relation
(5.156)
where is an matrix, is a column vector, and is a scalar number. we can express this
relation in matrix form as
(5.157)
we write (5.157) as
(5.158)
or
ax = ?x
a n × n x ?
a11 a12 … a1n
a21 a22 … a2n
… … … …
an1 an2 … ann
x1
x2

xn
?
x1
x2

xn
=
(a–?i)x = 0
chapter 5 differential equations, state variables, and state equations
5-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.159)
the equations of (5.159) will have non-trivial solutions if and only if its determinant is zero*, that
is, if
(5.160)
expansion of the determinant of (5.160) results in a polynomial equation of degree in , and it
is called the characteristic equation.
we can express (5.73) in a compact form as
(5.161)
as we know, the roots of the characteristic equation are the eigenvalues of the matrix , and
corresponding to each eigenvalue , there is a non-trivial solution of the column vector , i.e.,
. this vector is called eigenvector. obviously, there is a different eigenvector for each
eigenvalue. eigenvectors are generally expressed as unit eigenvectors, that is, they are normalized to
unit length. this is done by dividing each component of the eigenvector by the square root of the
sum of the squares of their components, so that the sum of the squares of their components is
equal to unity.
in many engineering applications the unit eigenvectors are chosen such that where
is the transpose of the eigenvector , and is the identity matrix.
two vectors and are said to be orthogonal if their inner (dot) product is zero. a set of eigenvectors
constitutes an orthonormal basis if the set is normalized (expressed as unit eigenvectors)
and these vector are mutually orthogonal. an orthonormal basis can be formed with the gram-
schmidt orthogonalization procedure we will discuss it in chapter 14.
the example that follows, illustrates the relationships between a matrix , its eigenvalues, and
eigenvectors.
*. this is because we want the vector x in (5.158) to be a non-zero vector and the product to be zero.
(a11 – ?)x1 a12x2 … a1nxn
a21x1 (a22–?)x2 … a2nxn
… … … …
an1x1 an2x2 … (ann – ?)xn
= 0
(a–?i)x
det
(a11 – ?) a12 … a1n
a21 (a22–?) … a2n
… … … …
an1 an2 … (ann – ?)
= 0
n ?
det(a–?i) = 0
? a
? x
x ? 0 x
x ? xt = i xt
x i
x y
a
numerical analysis using matlab and spreadsheets, second edition 5-39
orchard publications
eigenvectors
example 5.19
given the matrix
a. find the eigenvalues of
b. find eigenvectors corresponding to each eigenvalue of
c. form a set of unit eigenvectors using the eigenvectors of part (b).
solution:
a. this is the same matrix as in example 5.17, where we found the eigenvalues to be
b. we start with
and we let
then,
(5.162)
or
(5.163)
equating corresponding rows and rearranging, we get
(5.164)
for , (5.164) reduces to
a
5 7 –5
0 4 –1
2 8 –3
=
a
a
?1 = 1 ?2 = 2 ?3 = 3
ax = ?x
x
x1
x2
x3
=
5 7 –5
0 4 –1
2 8 –3
x1
x2
x3
?
x1
x2
x3
=
5x1 7x2 –5x3
0 4x2 –x3
2x1 8x2 –3x3
?x1
?x2
?x3
=
(5 – ?)x1 7x2 –5x3
0 (4 – ?)x2 –x3
2x1 8x2 –(3 – ?)x3
0
0
0
=
? = 1
chapter 5 differential equations, state variables, and state equations
5-40 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(5.165)
by crame’s rule, or matlab, we get the indeterminate values
(5.166)
since the unknowns are scalars, we can assume that one of these, say , is known,
and solve and in terms of . then, we get , and .
therefore, an eigenvector for is
(5.167)
since any eigenvector is a scalar multiple of the last vector in (5.167).
similarly, for , we get , and . then, an eigenvector for is
(5.168)
finally, for , we get , and . then, an eigenvector for is
(5.169)
c. we find the unit eigenvectors by dividing the components of each vector by the square root of
the sum of the squares of the components. these are:
4x1 + 7x2 – 5x3 = 0
3x2 – x3 = 0
2x1 + 8x2 – 4x3 = 0
x1 = 0 ? 0 x2 = 0 ? 0 x3 = 0 ? 0
x1, x2, and x3 x2
x1 x3 x2 x1 = 2x2 x3 = 3x2
? = 1
x? = 1
x1
x2
x3
=
2x2
x2
3x2
x2
2
1
3
2
1
3
= = =
? = 2 x1 = x2 x3 = 2x2 ? = 2
x? = 2
x1
x2
x3
=
x2
x2
2x2
x2
1
1
2
1
1
2
= = =
? = 3 x1 = –x2 x3 = x2 ? = 3
x? = 3
x1
x2
x3
=
–x2
x2
x2
x2
–1
1
1
–1
1
1
= = =
22 + 12 + 32 = 14
12 + 12 + 22 = 6
(–1)2 + 12 + 12 = 3
numerical analysis using matlab and spreadsheets, second edition 5-41
orchard publications
summary
the unit eigenvectors are
(5.170)
we observe that for the first unit eigenvector the sum of the squares is unity, that is,
(5.171)
and the same is true for the other two unit eigenvectors in (5.170).
5.12 summary
• differential equations are classified by:
type - ordinary or partial
order - the highest order derivative which is included in the differential equation
degree - the exponent of the highest power of the highest order derivative after the differential
equation has been cleared of any fractions or radicals in the dependent variable and its
derivatives
• if the dependent variable is a function of only a single variable , that is, if , the differential
equation which relates and is said to be an ordinary differential equation and it is
abbreviated as ode.
• if the dependent variable is a function of two or more variables such as , where
and are independent variables, the differential equation that relates , , and is said to be
a partial differential equation and it is abbreviated as pde.
• a function is a solution of a differential equation if the latter is satisfied when and
its derivatives are replaced throughout by and its corresponding derivatives. also, the initial
conditions must be satisfied.
• the ode
unit x? = 1
2
14
-
1
14
-
3
14
-
= unit x? = 2
1
6

1
6

2
6

= unit x? = 3
–1
3

1
3

1
3

=
2
14
? - ?
? ?2 1
14
? - ?
? ?2 3
14
? - ?
? ?2
+ + 4
14
- 1
14
- 9
14
= + + - = 1
y x y = f (x)
y x
y y = f (x, t) x
t yx t
y = f (x) y
f (x)
an
d ny
dtn an – 1
d n – 1y
dt n – 1 - … a1
dy
dt
+ + + - + a0 y bm
d mx
dt m - bm – 1
d m – 1x
dt n – 1 … b1
dx
dt
= + + + - + b0x
chapter 5 differential equations, state variables, and state equations
5-42 numerical analysis using matlab and spreadsheets, second edition
orchard publications
is a non-homogeneous differential equation if the right side, known as forcing function, is not
zero. if the forcing function is zero, the differential equation is referred to as homogeneous differential
equation.
• the most general solution of an homogeneous ode is the linear combination
where the subscript is used to denote homogeneous and are arbitrary constants.
• generally, in engineering the solution of the homogeneous ode, also known as the complementary
solution, is referred to as the natural response, and is denoted as or simply .
the particular solution of a non-homogeneous ode is be referred to as the forced response,
and is denoted as or simply . the total solution of the non-homogeneous ode is the
summation of the natural and forces responses, that is,
the natural response contains arbitrary constants and these can be evaluated from the
given initial conditions. the forced response , however, contains no arbitrary constants. it is
imperative to remember that the arbitrary constants of the natural response must be evaluated
from the total response.
• for an order homogeneous differential equation the solutions are
where are the solutions of the characteristic equation
and are the constant coefficients of the ode
• if the roots of the characteristic equation are distinct, the solutions of the natural response
are independent and the most general solution is:
• if the solution of the characteristic equation contains equal roots, the most general solution
has the form:
yh(t) = k1 y1(t) + k2 y2(t) + k3 y3(t) +… + kn yn(t)
h k1, k2, k3,…, kn
yn (t) yn
yf (t) yf
y(t) y natural
response
y forced
response
= + = yn + yf
yn
yf
nth
y1 k1es1t
= y2 k2es2t
= y3 k3es3t
= … yn knesnt
, , , , =
s1, s2,…, sn
an sn an – 1 sn – 1 + + … + a1 s + a0 = 0
an an – 1 , , …, a1, a0
n
yn k1es1t
= k2es2t … knesnt
+ + +
m
yn k1 k2t … kmtm – 1 ( + + + ) e
s1t
= kn – ies2t … knesnt
+ + +
numerical analysis using matlab and spreadsheets, second edition 5-43
orchard publications
summary
• if the characteristic equation contains complex roots, these occur as complex conjugate pairs.
thus, if one root is where and are real numbers, then another root is
. then, for two complex conjugate roots we evaluate the constants from the
expressions
• the forced response of a non-homogeneous ode can be found by the method of undetermined
coefficients or the method of variation of parameters.
• with the method of undetermined coefficients, the forced response is a function similar to the
right side of the non-homogeneous ode. the form of the forced response for second order
non-homogeneous odes is given in table 5.1.
• in certain non-homogeneous odes, the right side cannot be determined by the method of
undetermined coefficients. for these odes we must use the method of variation of parameters.
this method will work with all linear equations including those with variable coefficients provided
that the general form of the natural response is known.
• for second order odes with constant coefficients, the method of variation of parameters
replaces the constants and by two variables and that satisfy the following three
relations:
simultaneous solution of last two expressions above will yield the values of and
then, integration of these will produce and , which when substituted into the
first will yield the total solution.
• an nth-order differential equation can be resolved to first-order simultaneous differential
equations with a set of auxiliary variables called state variables. the resulting first-order differential
equations are called state space equations, or simply state equations.
• the state representation of a system can be described by the pair of the of the state space equations
s1 = – ? + j? ? ?
s2 = –?–j?
k1es1t k2es2t
+ e –?t (k3 cos?t + k4 sin?t) e –?t= = k5 cos(?t + ?)
f (t)
k1 k2 u1 u2
y = u1 y1 + u2 y2
du1
dt
- y1
du2
dt
+ - y2 = 0
du1
dt
-
dy1
dt
? -
du2
dt

dy2
dt
+ ? = f (t)
du1 ? dt
du2 ? dt u1 u2
n

= ax + bu
y = cx + du
chapter 5 differential equations, state variables, and state equations
5-44 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• in a system of state equations of the form
where , , , and are scalar constants, and the initial condition, if non-zero is denoted as
, the solution of the first state equation above is
• in a system of state equations of the form
where for two or more simultaneous differential equations and are or higher order
matrices, and and are column vectors with two or more rows, the solution of the matrix
differential equation with initial conditions is obtained from the
relation
where the state transition matrix is defined as the matrix power series
and is the identity matrix.
• if is an matrix, and be the identity matrix, the eigenvalues ,
of are the roots of the nth order polynomial
• evaluation of the state transition matrix is based on the cayley-hamilton theorem. this
theorem states that a matrix can be expressed as an degree polynomial in terms of
the matrix as
where the coefficients are functions of the eigenvalues .

= ?x + ?u
y = k1x + k2u
? ? k1 k2
x0 = x(t0)
x(t) e?(t – t0)x0 e?t e –???u(?)d?
t0
t
= + ?

= ax + bu
y = cx + du
a c 2 × 2
b d

= ax + bu x(t0) = x0
x(t) ea t t0
( – )x0 eat e–a?bu(?)d?
t0
t
= + ?
eat
?(t) eat i at 1
2!
a2t2 1
3!
a3t3 … 1
n!
? = + + + + + antn
i n × n
a n × n i n × n ?i i = 1, 2, …, n
a
det[a – ?i] = 0
eat
(n – 1)th
a
eat a0i a1a a2a2 … an – 1an – 1 = + + + +
ai ?
numerical analysis using matlab and spreadsheets, second edition 5-45
orchard publications
summary
• if , that is, if all eigenvalues of a given matrix are distinct, the coefficients
are found from the simultaneous solution of the following system of equations:
• if the polynomial of has roots, and of these roots are equal, that is, if
, the coefficients of the state transition matrix
are found from the simultaneous solution of the system of equations below.
• we can use the matlab eig(x) function to find the eigenvalues of an matrix.
• if is an matrix, is a non-zero column vector, and is a scalar number, the vector
is called eigenvector. obviously, there is a different eigenvector for each eigenvalue. eigenvectors
are generally expressed as unit eigenvectors, that is, they are normalized to unit length. this
is done by dividing each component of the eigenvector by the square root of the sum of the
squares of their components, so that the sum of the squares of their components is equal to
unity.
?1 ? ?2 ? ?3 ?… ? ?n a
ai
a0 a1?1 a2?1
2 … an 1 – ?1
n – 1 + + + + e?1t
=
a0 a1?2 a2?2
2 … an 1 – ?2
n – 1 + + + + e?2t
=

a0 a1?n a2?n 2 … an – 1?n
n – 1 + + + + e?nt
=
det[a – ?i] = 0 n m
?1 = ?2= ?3… = ?m, ?m + 1 , ?n ai
eat a0i a1a a2a2 … an – 1an – 1 = + + + +
a0 a1?1 a2?1 2 … an – 1?1
n – 1 + + + + e?1t
=
d
d?1
a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) d
d?1
e?1t
=
d 2
d?1 2 a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) d 2
d?1 2 e?1t
=

d m – 1
d?1
m – 1 - a0 a1?1 a2?1 2 … an – 1?1
n – 1 ( + + + + ) dm – 1
d?1
m – 1 -e?1t
=
a0 a1?m + 1 a2?m + 1
2 … an 1 – ?m + 1
n – 1 + + + + e? m + 1t
=

a0 a1?n a2?n 2 … an – 1?n
n – 1 + + + + e?nt
=
n × n
a n × n x ? x
chapter 5 differential equations, state variables, and state equations
5-46 numerical analysis using matlab and spreadsheets, second edition
orchard publications
5.13 exercises
solve the following odes by any method and verify your answers with matlab.
1.
2.
3. hint: use
4.
5 express the integro-differential equation below as a matrix of state equations where
are constants.
6. express the matrix of the state equations below as a single differential equation, and let
.
7. compute the eigenvalues of the matrices , , and below.
hint: one of the eigenvalues of matrix c is .
8. compute given that
d2y
dt2 4dy
dt
+ - + 3y = t – 1
d2y
dt2 4dy
dt
+ - + 3y 4e–t =
d2y
dt2 2dy
dt
- y + + t 2 cos = t 2 cos 12
= ( cos2t + 1)
d2y
dt2 + y = sect
k1, k2, and k3
dv2
dt2 - k3
dv
dt
- k2v k1 vdt
0
t
+ + + ? = sin3t + cos3t
x(y) = y(t)

1

2

3

4
0 1 0 0
0 0 1 0
0 0 0 1
–1 –2 –3 –4
x1
x2
x3
x4
?
0
0
0
1
= + u(t)
a b c
a 1 2
3 –1
= b a 0
–a b
= c
0 1 0
0 0 1
–6 –11 –6
=
–1
eat
a
0 1 0
0 0 1
–6 –11 –6
=
numerical analysis using matlab and spreadsheets, second edition 5-47
orchard publications
solutions to exercises
5.14 solutions to exercises
1. the characteristic equation of the homogeneous part is from which
and . thus . for the forced response, we refer to table 5.1 and we
assume a solution of the form and the total solution is
the first and second derivatives of are
and by substitution into the given ode
equating like terms we get
and simultaneous solution of the last two yields and . therefore,
check with matlab:
y=dsolve( d2y+4*dy+3*y=t?1’) y=simple(y)
y =
-7/9+1/3*t+c1/exp(t)+c2/exp(t)^3
2. the characteristic equation of the homogeneous part is the same as for exercise 1 and thus
. for the forced response, we refer to table 5.1 and we assume a solution of
the form where we multiplied by to avoid the duplication with . by substitution
of this assumed solution into the given ode and using matlab to find the first and
second derivatives we get:
s2 + 4s + 3 = 0 s1 = –1
s2 = –3 yn k1e–t k2e–3t = +
yf = k3t + k4
y k1e–t k2e–3t = + + k3t + k4
y
dy ? dt k1e–t – 3k2e–3t = – + k3
d 2y dt2
? k1e–t 9k2e–3t = +
k1e–t 9k2e–3t 4 k1e–t – 3k2e–3t ( – + k3) 3 k1e–t k2e–3t + + + ( + + k3t + k4) = t – 1
4k3 3k3t 3k4
+ + = t – 1
3k3t = t
4k3 + 3k4 = –1
k3 = 1 ? 3 k4 = –7 ? 9
y k1e–t k2e 3t – 13
t 79
= + + –
yn k1e–t k2e–3t = +
yf k3te–t = e–t t k1e–t
y k1e–t k2e–3t k3te–t = + +
chapter 5 differential equations, state variables, and state equations
5-48 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we will use matlab to find the first and second derivatives of this expression.
syms t k3 % define symbolic variables
y0=k3*t*exp(?t) % assumed form of total solution
y1=diff(y0) f1=simple(y1) % compute and simplify first derivative
f1 =
-k3*exp(-t)*(-1+t)
thus, the first derivative of is
y2=diff(y0,2) f2=simple(y2) % compute and simplify second derivative
f2 =
k3*exp(-t)*(-2+t)
and the second derivative of is
f=y2+4*y1+3*y0 f=simple(f) % form and simplify the left side of the given ode
f =
2*k3/exp(t)
and by substitution into the given ode
or . therefore,
check with matlab:
y=dsolve( d2y+4*dy+3*y=4*exp(?t) ) y=simple(y)
2*t/exp(t)-1/exp(t)+c1/exp(t)+c2/exp(t)^3
we observe that the second and third terms of the displayed expression above have the same
form and thus they can be combined to form a single term c3/exp(t).
3. the characteristic equation yields two equal roots and thus the natural response
has the form
for the forced response we assume a solution of the form
yf
dyf ? dt k3e–t k3te–t = –
y
d 2yf ? dt2 2k3e–t – k3te–t = +
2k3e–t 4e–t =
k3 = 2
y k1e–t k2e–3t 2te–t = + +
s1 = s2 = –1
yn k1e–t k2te–t = +
yf = k3 cos2t + k4 sin2t + k5
numerical analysis using matlab and spreadsheets, second edition 5-49
orchard publications
solutions to exercises
we will use matlab to find the first and second derivatives of this expression.
syms t k1 k2 k3 k4 k5 % define symbolic variables
y0=k3*cos(2*t)+k4*sin(2*t)+k5 % assumed form of total solution
y1=diff(y0) f1=simple(y1) % compute and simplify first derivative
f1 =
-2*k3*sin(2*t)+2*k4*cos(2*t)
thus, the first derivative of is
y2=diff(y0,2) f2=simple(y2) % compute and simplify second derivative
f2 =
-4*k3*cos(2*t)-4*k4*sin(2*t)
and the second derivative of is
f=y2+2*y1+y0 f=simple(f) % form and simplify the left side of the given ode
f =
-3*k3*cos(2*t)-3*k4*sin(2*t)-4*k3*sin(2*t)+4*k4*cos(2*t)+k5
simplifying this expression and equating with the right side of the given ode we get:
equating like terms and solving for the terms we get
simultaneous solution of the first two equations above yields and .
therefore, the forced response is
and the total response is
check with matlab:
yf
dyf ? dt = –2k3 sin2t + 2k4 cos2t
y
d 2yf ? dt2 = –4k3 cos2t–4k4 sin2t
(– 3k3 + 4k4) cos2t – (4k3 + 3k4) sin2t + k5 cos2t
2
- 12
= +
k
– 3k3 + 4k4 = 1 ? 2
– 4k3 – 3k4 = 0
k5 = 1 ? 2
k3 = –3 ? 50 k4 = 4 ? 50
yf = (–3 ? 50) cos2t + (4 ? 50) sin2t + 1 ? 2
y k1e–t k2te t – 12
3cos2t – 4sin2t
50
= + + – -
chapter 5 differential equations, state variables, and state equations
5-50 numerical analysis using matlab and spreadsheets, second edition
orchard publications
y=dsolve( d2y+2*dy+y=cos(2*t)/2+1/2 ) f=simple(y)
f =
-3/50*cos(2*t)+2/25*sin(2*t)+1/2+c1*exp(-t)+c2*exp(-t)*t
4. it is very difficult, if not impossible, to assume a solution for the forced response of this ode.
therefore, we will use the method of variation of parameters.
the characteristic equation is from which and thus the natural response is
we let
then, by (5.68) the solution is
(1)
also, from (5.69),
or
and from (5.70),
next, we find and by cramer’s rule as follows:
(2)
and
(3)
integration of (2) and (3) above and substitution into (1) yields
s2 + 1 = 0 s = ±j
yn = k1e jt + k2e–jt
y1 = cost and y2 = sint
y = u1y1 + u2y2 = u1 cost + u2 sint
du1
dt
-y1
du2
dt
+ y2 = 0
du1
dt
- cost
du2
dt
+ sint = 0
du1
dt
-
dy1
dt
? -
du2
dt

dy2
dt
+ ? f(t)
du1
dt
= = -(–sint)
du2
dt
+ ( cost) = sect
du1 ? dt du2 ? dt
du1
dt
-
0 sint
sect cost
cost sint
–sint cost
-
sint
cost

cos2t + sin2t
–tant
1
= = = - = –tant
du2
dt

cost 0
–sint sect
1
11
= = = 1
u1 = ?(–tant)dt = – (–ln cost) + k1 = ln cost + k1
u2 = ?dt = t + k2
numerical analysis using matlab and spreadsheets, second edition 5-51
orchard publications
solutions to exercises
check with matlab:
y=dsolve( d2y+y=sec(t) ) f=simple(y)
f =
sin(t)*t+log(cos(t))*cos(t)+c1*sin(t)+c2*cos(t)
5. differentiating the given integro-differential equation with respect to we get
or
(1)
we let
then,
and by substitution into (1)
thus, the state equations are
and in matrix form
y = u1y1 + u2y2 = ( ln cost + k1) cost + (t + k2) sint
= k1 cost + k2 sint + t sint + cost( ln cost)
t
dv3
dt3 k3
dv2
dt2 k2
dv
dt
+ + - + k1v = 3cos3t – 3sin3t = 3( cos3t – sin3t)
dv3
dt3 k3
dv2
dt2 = – –k2dv
dt
-–k1v + 3( cos3t – sin3t)
v = x1 dv
dt
- x2 x1
· = = dv2
dt2 x3 x2
· = =
dv3
dt3 x3
· =
x3
· – k1x1 k= – 2x2 – k3x3 + 3( cos3t – sin3t)
x1
· = x2
x2
· = x3
x3
· – k1x1 k= – 2x2 – k3x3 + 3( cos3t – sin3t)
x1
·
x2
·
x3
·
0 1 0
0 0 1
–k1 –k2 –k3
x1
x2
x3
?
0
0
1
= + ? 3( cos3t – sin3t)
chapter 5 differential equations, state variables, and state equations
5-52 numerical analysis using matlab and spreadsheets, second edition
orchard publications
6. expansion of the given matrix yields
letting we get
7.
a.
, , , and thus
b.
, and thus
c.
and it is given that . then,
and thus
8.
a. matrix is the same as matrix c in exercise 7. then,
x1
· = x2 x2
· = x3 x3
· = x2 x4
· –x= 1 – 2x2 – 3x3 – 4x4 + u(t)
x = y
dy4
dt4 4 dy3
dt3 3dy2
dt2 2dy
dt
+ + + - + y = u(t)
a 1 2
3 –1
= det(a – ?i) det 1 2
3 –1
? 1 0
0 1

? ?
? ?
? ?
det 1 – ? 2
3 – 1 – ?
= = = 0
(1 – ?)(– 1 – ?) – 6 = 0 – 1 – ? + ? + ?2 – 6 = 0 ?2 = 7 ?1 = 7 ?2 = – 7
b a 0
–a b
= det(b – ?i) det a 0
–a b
? 1 0
0 1

? ?
? ?
? ?
det a – ? 0
–a b – ?
= = = 0
(a – ?)(b – ?) = 0 ?1 = a ?2 = b
c
0 1 0
0 0 1
–6 –11 –6
= det(c – ?i) det
0 1 0
0 0 1
–6 –11 –6
?
1 0 0
0 1 0
0 0 1

? ?
? ?
? ?
? ?
? ?
=
det
–? 1 0
0 –? 1
–6 –11 –6–?
= = 0
?2(–6–?) – 6 – (–11)(–?) = ?3 + 6?2 + 11? + 6 = 0 ?1 = –1
?3 + 6?2 + 11? + 6
(? + 1)
= ?2 + 5? + 6 ? (? + 1)(? + 2)(? + 3) = 0
?1 = –1 ?2 = –2 ?1 = –3
a
?1 = –1 ?2 = –2 ?1 = –3
numerical analysis using matlab and spreadsheets, second edition 5-53
orchard publications
solutions to exercises
and since is a matrix the state transition matrix is
(1)
then,
syms t a=[1 ?1 1 1 ?2 4 1 ?3 9] ...
a=sym( [exp(?t) exp(?2*t) exp(?3*t)] ) x=a\a fprintf( \n ) ...
disp( a0 = ) disp(x(1)) disp( a1 = ) disp(x(2)) disp( a2 = ) disp(x(3))
a0 =
3*exp(-t)-3*exp(-2*t)+exp(-3*t)
a1 =
5/2*exp(-t)-4*exp(-2*t)+3/2*exp(-3*t)
a2 =
1/2*exp(-t)-exp(-2*t)+1/2*exp(-3*t)
thus,
now, we compute of (1) with the following matlab code:
syms t a0=3*exp(?t)?3*exp(?2*t)+exp(?3*t) a1=5/2*exp(?t)?4*exp(?2*t)+3/2*exp(?3*t) ...
a2=1/2*exp(?t)?exp(?2*t)+1/2*exp(?3*t) a=[0 1 0 0 0 1 ?6 ?11 ?6] fprintf( \n ) ...
eat=a0*eye(3)+a1*a+a2*a^2
eat =
[3*exp(-t)-3*exp(-2*t)+exp(-3*t), 5/2*exp(-t)-4*exp(-2*t)+3/
2*exp(-3*t), 1/2*exp(-t)-exp(-2*t)+1/2*exp(-3*t)]
[-3*exp(-t)+6*exp(-2*t)-3*exp(-3*t), -5/2*exp(-t)+8*exp(-
2*t)-9/2*exp(-3*t), -1/2*exp(-t)+2*exp(-2*t)-3/2*exp(-3*t)]
[3*exp(-t)-12*exp(-2*t)+9*exp(-3*t), 5/2*exp(-t)-16*exp(-
2*t)+27/2*exp(-3*t), 1/2*exp(-t)-4*exp(-2*t)+9/2*exp(-
3*t)]
then,
a 3 × 3
eat = a0i + a1a + a2a2
a0 a1?1 a2?1
+ + 2 e?1t
= a0 – a1 + a2 e–t ? =
a0 a1?2 a2?2
+ + 2 e?2t
= a0 – 2a1 + 4a2 e–2t ? =
a0 a1?3 a2?3 2 + + e?3t
= a0 – 3a1 + 9a2 e–3t ? =
a0 3e–t 3e–2t – 3e–3t = +
a1 2.5e–t 4e–2t – 1.5e–3t = +
a2 0.5e–t e–2t – 0.5e–3t = +
eat
chapter 5 differential equations, state variables, and state equations
5-54 numerical analysis using matlab and spreadsheets, second edition
orchard publications
eat
3e–t 3e–2t – e–3t + 2.5e–t 4e–2t – 1.5e–3t + 0.5e–t e–2t – 0.5e–3t +
–3e–t 6e–2t 3e–3t + – –2.5e–t 8e–2t 4.5e–3t + – –0.5e–t 2e–2t 1.5e–3t + –
3e–t 12e–2t – 9e–3t + 2.5e–t 16e–2t – 13.5e–3t + 0.5e–t 4e–2t – 4.5e–3t +
=
numerical analysis using matlab and spreadsheets, second edition 6-1
orchard publications
chapter 6
fourier, taylor, and maclaurin series
his chapter is an introduction to fourier and power series. we begin with the definition of
sinusoids that are harmonically related and the procedure for determining the coefficients of
the trigonometric form of the series. then, we discuss the different types of symmetry and
how they can be used to predict the terms that may be present. several examples are presented to
illustrate the approach. the alternate trigonometric and the exponential forms are also presented.
we conclude with a discussion on power series expansion with the taylor and maclaurin series.
6.1 wave analysis
the french mathematician fourier found that any periodic waveform, that is, a waveform that
repeats itself after some time, can be expressed as a series of harmonically related sinusoids, i.e.,
sinusoids whose frequencies are multiples of a fundamental frequency (or first harmonic). for
example, a series of sinusoids with frequencies , , , and so on, contains the
fundamental frequency of , a second harmonic of , a third harmonic of , and
so on. in general, any periodic waveform can be expressed as
(6.1)
or
(6.2)
where the first term is a constant, and represents the (average) component of .
thus, if represents some voltage , or current , the term is the average value of
or .
the terms with the coefficients and together, represent the fundamental frequency component
*. likewise, the terms with the coefficients and together, represent the second harmonic
component , and so on.
* we recall that where is a constant.
t
1 mhz 2 mhz 3 mhz
1 mhz 2 mhz 3 mhz
f(t)
f t ( ) 12
= a0 + a1 cos?t + a2 cos2?t + a3 cos3?t + a4 cos4?t + …
+ b1 sin?t + b2 sin2?t + b3 sin3?t + b4 sin4?t +…
f t ( ) 12
a0 (an cosn?t + bn sinn?t)
n = 1
??
= +
a0 ? 2 dc f(t)
f(t) v(t) i(t) a0 ? 2
v(t) i(t)
a1 b1
? a2 b2
k1 cos?t + k2 sin?t = k cos(?t + ?) ?
2?
chapter 6 fourier, taylor, and maclaurin series
6-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
since any periodic waveform can be expressed as a fourier series, it follows that the sum of
the , the fundamental, the second harmonic, and so on, must produce the waveform .
generally, the sum of two or more sinusoids of different frequencies produce a waveform that is
not a sinusoid as shown in figure 6.1.
figure 6.1. summation of a fundamental, second and third harmonic
6.2 evaluation of the coefficients
evaluations of and coefficients of (6.1) is not a difficult task because the sine and cosine are
orthogonal functions, that is, the product of the sine and cosine functions under the integral evaluated
from to is zero. this will be shown shortly.
let us consider the functions and where and are any integers, and for convenience,
we have assumed that . then,
(6.3)
(6.4)
(6.5)
the integrals of (6.3) and (6.4) are zero since the net area over the 0 to area is zero. the integral
of (6.5) is also is zero since
f(t)
dc f(t)
total
fundamental
2nd harmonic
3rd harmonic
ai bi
0 2?
sinmt cosmt m n
? = 1
sinmtdt
0
2?
? = 0
cosmtdt
0
2?
? = 0
( sinmt)( cosnt)
0
2?
? dt = 0
2?
x y cos sin 12
= [ sin(x + y) + sin(x – y)]
numerical analysis using matlab and spreadsheets, second edition 6-3
orchard publications
evaluation of the coefficients
this is also obvious from the plot of figure 6.2, where we observe that the net shaded area above
and below the time axis is zero.
figure 6.2. graphical proof of
moreover, if and are different integers, then,
(6.6)
since
the integral of (6.6) can also be confirmed graphically as shown in figure 6.3, where and
. we observe that the net shaded area above and below the time axis is zero.
figure 6.3. graphical proof of for and
sinx cosx
sinx ? cosx
( sinmt)( cosnt)
0
2?
? dt = 0
m n
( sinmt)( sinnt)dt
0
2?
? = 0
x sin ( ) y sin ( ) 12
= [ cos(x – y) – cos(x – y)]
m = 2
n = 3
sin2x sin3x sin2x ? sin3x
( sinmt)( sinnt)dt
0
2?
? = 0 m = 2 n = 3
chapter 6 fourier, taylor, and maclaurin series
6-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
also, if and are different integers, then,
(6.7)
since
the integral of (6.7) can also be confirmed graphically as shown in figure 6.4, where and
. we observe that the net shaded area above and below the time axis is zero.
figure 6.4. graphical proof of for and
however, if in (6.6) and (6.7), , then,
(6.8)
and
(6.9)
the integrals of (6.8) and (6.9) can also be seen to be true graphically with the plots of figures 6.5
and 6.6.
it was stated earlier that the sine and cosine functions are orthogonal* to each other. the simplification
obtained by application of the orthogonality properties of the sine and cosine functions,
becomes apparent in the discussion that follows.
* we will discuss orthogonal functions in chapter 14
m n
( cosmt)( cosnt)dt
0
2?
? = 0
x cos ( ) y cos ( ) 12
= [ cos(x + y) + cos(x – y)]
m = 2
n = 3
cos3x cos2x cos2x ? cos3x
( cosmt)( cosnt)dt
0
2?
? = 0 m = 2 n = 3
m = n
( sinmt)2dt
0
2?
? = ?
( cosmt)2dt
0
2?
? = ?
numerical analysis using matlab and spreadsheets, second edition 6-5
orchard publications
evaluation of the coefficients
figure 6.5. graphical proof of
figure 6.6. graphical proof of
in (6.1), for simplicity, we let . then,
(6.10)
to evaluate any coefficient, say , we multiply both sides of (6.10) by . then,
next, we multiply both sides of the above expression by , and we integrate over the period to
. then,
sinx
sin2x
( sinmt)2dt
0
2?
? = ?
cosx
cos2x
( cosmt)2dt
0
2?
? = ?
? = 1
f t ( ) 12
= a0 + a1 cost + a2 cos2t + a3 cos3t + a4 cos4t + …
+ b1 sint + b2 sin2t + b3 sin3t + b4 sin4t +…
b2 sin2t
f t ( ) 2t sin 12
= a0 sin2t + a1 cost sin2t + a2 cos2t sin2t + a3 cos3t sin2t + a4 cos4t sin2t +…
b1 sint sin2t + b2( sin2t)2 + b3 sin3t sin2t + b4 sin4t sin2t +…
dt 0
2?
chapter 6 fourier, taylor, and maclaurin series
6-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(6.11)
we observe that every term on the right side of (6.11) except the term
is zero as we found in (6.6) and (6.7). therefore, (6.11) reduces to
or
and thus we can evaluate this integral for any given function . the remaining coefficients can
be evaluated similarly.
the coefficients , , and are found from the following relations.
(6.12)
(6.13)
(6.14)
the integral of (6.12) yields the average ( ) value of .
f(t) sin2tdt
0
2?
? 12
a0 sin2tdt
0
2?
? a1 cost sin2tdt
0
2?
? a2 cos2t sin2tdt
0
2?
= + + ?
+ a3 cos3t sin2tdt
0
2?
? a4 cos4t sin2tdt
0
2?
+ ? + …
+ b1 sint sin2tdt
0
2?
? b2 ( sin2t)2dt
0
2?
? b3 sin3t sin2tdt
0
2?
+ + ?
+ b4 sin4t sin2tdt
0
2?
? + …
b2 ( sin2t)2dt
0
2?
?
f(t) sin2tdt
0
2?
? b2 ( sin2t)2dt
0
2?
= ? = b2?
b2
1?
f(t) sin2tdt
0
2?
= ?
f(t)
a0 an bn
12
a0
1
2?
f(t)dt
0
2?
= ?
an
1?
f(t) cosntdt
0
2?
= ?
bn
1?
f(t) sinntdt
0
2?
= ?
dc f(t)
numerical analysis using matlab and spreadsheets, second edition 6-7
orchard publications
symmetry
6.3 symmetry
with a few exceptions such as the waveform of example 6.6, the most common waveforms used
in science and engineering, do not have the average, cosine, and sine terms all present. some
waveforms have cosine terms only, while others have sine terms only. still other waveforms have
or have not components. fortunately, it is possible to predict which terms will be present in
the trigonometric fourier series, by observing whether or not the given waveform possesses some
kind of symmetry.
we will discuss three types of symmetry that can be used to facilitate the computation of the trigonometric
fourier series form. these are:
1. odd symmetry ? if a waveform has odd symmetry, that is, if it is an odd function, the series will
consist of sine terms only. in other words, if is an odd function, all the coefficients
including , will be zero.
2. even symmetry ? if a waveform has even symmetry, that is, if it is an even function, the series
will consist of cosine terms only, and may or may not be zero. in other words, if is an
even function, all the coefficients will be zero.
3. half-wave symmetry ? if a waveform has half-wave symmetry (to be defined shortly), only odd
(odd cosine and odd sine) harmonics will be present. in other words, all even (even cosine and
even sine) harmonics will be zero.
we will now define even and odd functions and we should remember that even functions have
nothing to do with even harmonics, and odd functions have nothing to do with odd harmonics.
a function is an even function of time if the following relation holds.
(6.15)
that is, if in an even function we replace with , the function does not change. thus,
polynomials with even exponents only, and with or without constants, are even functions. for
instance, the cosine function is an even function because it can be written as the power series*
other examples of even functions are shown in figure 6.7.
* we will discuss power series later in this chapter.
dc
f(t) ai
a0
a0 f(t)
bi
f(t)
f(–t) = f(t)
t –t f(t)
cost 1 t2
2!
t4
4!
– + t6
6!
= – + …
chapter 6 fourier, taylor, and maclaurin series
6-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 6.7. examples of even functions
a function is an odd function of time if the following relation holds.
(6.16)
that is, if in an odd function we replace with , we obtain the negative of the function . thus,
polynomials with odd exponents only, and no constants are odd functions. for instance, the sine
function is an odd function because it can be written as the power series
other examples of odd functions are shown in figure 6.8.
figure 6.8. examples of odd functions
we observe that for odd functions, . however, the reverse is not always true that is, if
, we should not conclude that is an odd function. an example of this is the function
in figure 6.7.
the product of two even or two odd functions is an even function, and the product of an even function
times an odd function, is an odd function.
henceforth, we will denote an even function with the subscript , and an odd function with the subscript
. thus, and will be used to represent even and odd functions of time respectively.
also,
(6.17)
and
t
f(t)
t
f(t)
t
f(t)
k
0 0 0
t2
t2 + k
f(t)
–f(–t) = f(t)
t –t f(t)
sint t t3
3!
t5
5!
– + t7
7!
= – + …
t
f(t)
mt
t
f(t)
t
f(t)
0 0 0
t3
f(0) = 0
f(0) = 0 f(t)
f(t) t2 =
e
o fe(t) fo(t)
fe(t)dt
–t
t
? 2 fe(t)dt
0
t
= ?
numerical analysis using matlab and spreadsheets, second edition 6-9
orchard publications
symmetry
(6.18)
a function that is neither even nor odd can be expressed as
(6.19)
or as
(6.20)
by addition of (6.16) with (6.17), we get
(6.21)
that is, any function of time can be expressed as the sum of an even and an odd function.
to understand half-wave symmetry, we recall that any periodic function with period , is
expressed as
(6.22)
that is, the function with value at any time , will have the same value again at a later time
.
a periodic waveform with period , has half-wave symmetry if
(6.23)
that is, the shape of the negative half-cycle of the waveform is the same as that of the positive
half-cycle, but inverted.
we will test the waveforms of figures 6.9 through 6.13 for any of the three types of symmetry.
1. square waveform
for the waveform of figure 6.9, the average value over one period is zero, and therefore,
. it is also an odd function and has half-wave symmetry since and
.
fo(t)dt
–t
t
? = 0
f(t)
fe t ( ) 12
= [f(t) + f(–t)]
fo t ( ) 12
= [f(t)–f(–t)]
f(t) = fe(t) + fo(t)
t
f(t) = f(t + t)
f(t) t
t + t
t
–f (t + t ? 2) = f(t)
t
a0 = 0 –f(–t) = f(t)
–f (t + t ? 2) = f(t)
chapter 6 fourier, taylor, and maclaurin series
6-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 6.9. square waveform test for symmetry
an easy method to test for half-wave symmetry is to choose any half-period length on the
time axis as shown in figure 6.9, and observe the values of at the left and right points on the
time axis, such as and . if there is half-wave symmetry, these will always be equal but
will have opposite signs as we slide the half-period length to the left or to the right on the
time axis at non-zero values of .
2. square waveform with ordinate axis shifted
if we shift the ordinate axis radians to the right, as shown in figure 6.10, we see that the
square waveform now becomes an even function and has half-wave symmetry since
and . also, .
figure 6.10. square waveform with ordinate shifted by
obviously, if the ordinate axis is shifted by any other value other than an odd multiple of ,
the waveform will have neither odd nor even symmetry.
3. sawtooth waveform
for the sawtooth waveform of figure 6.11, the average value over one period is zero and therefore,
. it is also an odd function because , but has no half-wave symmetry
0
? 2?
t
?t
t/2
t/2
a
?a
f(a)
f(b)
t ? 2
f(t)
f (a) f (b)
t ? 2
f(t)
? ? 2
f(–t) = f(t)
–f (t + t ? 2) = f(t) a0 = 0
0
?/2 2?
t
?t
t/2 t/2
a
?a
??/2
?2? ?? ?
? ? 2
? ? 2
t
a0 = 0 –f(–t) = f(t)
numerical analysis using matlab and spreadsheets, second edition 6-11
orchard publications
symmetry
since
figure 6.11. sawtooth waveform test for symmetry
4. triangular waveform
for this triangular waveform of figure 6.12, the average value over one period is zero and
therefore, . it is also an odd function since . moreover, it has half-wave symmetry
because
figure 6.12. triangular waveform test for symmetry
5. fundamental, second and third harmonics of a sinusoid
figure 6.13 shows a fundamental, second, and third harmonic of a typical sinewave where the
half period , is chosen as the half period of the period of the fundamental frequency. this is
necessary in order to test the fundamental, second, and third harmonics for half-wave symmetry.
the fundamental has half-wave symmetry since the and values, when separated by ,
are equal and opposite. the second harmonic has no half-wave symmetry because the ordinates
on the left and on the right, although are equal, there are not opposite in sign. the third
harmonic has half-wave symmetry since the and values, when separated by are equal
and opposite. these waveforms can be either odd or even depending on the position of the ordinate.
also, all three waveforms have zero average value unless the abscissa axis is shifted up or
down.
–f (t + t ? 2) ? f(t)
0
2?
t
?t
t/2
a
?a
?2? ?? ?
t/2
t
a0 = 0 –f(–t) = f(t)
–f (t + t ? 2) = f(t)
0 2?
t
?t
a
?a
?? ?
?2?
t/2 t/2
t ? 2
a –a t ? 2
b b
c –c t ? 2
chapter 6 fourier, taylor, and maclaurin series
6-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 6.13. fundamental, second, and third harmonic test for symmetry
in the expressions of the integrals in (6.12) through (6.14), the limits of integration for the coefficients
and are given as to , that is, one period . of course, we can choose the limits
of integration as to . also, if the given waveform is an odd function, or an even function,
or has half-wave symmetry, we can compute the non-zero coefficients and by integrating
from to only, and multiply the integral by . moreover, if the waveform has half-wave symmetry
and is also an odd or an even function, we can choose the limits of integration from to
and multiply the integral by . the proof is based on the fact that, the product of two even
functions is another even function, and also that the product of two odd functions results also in
an even function. however, it is important to remember that when using these shortcuts, we must
evaluate the coefficients and for the integer values of that will result in non-zero coefficients.
this point will be illustrated in example 6.2.
we will now derive the trigonometric fourier series of the most common periodic waveforms.
6.4 waveforms in trigonometric form of fourier series
example 6.1
compute the trigonometric fourier series of the square waveform of figure 6.14.
figure 6.14. square waveform for example 6.1
a
?a
?/2
b b
?/2
?c
c
?/2
an bn 0 2? t
–? +?
an bn
0 ? 2
0
? ? 2 4
an bn n
0
? 2?
t
?t
a
?a
numerical analysis using matlab and spreadsheets, second edition 6-13
orchard publications
waveforms in trigonometric form of fourier series
solution:
the trigonometric series will consist of sine terms only because, as we already know, this waveform
is an odd function. moreover, only odd harmonics will be present since this waveform has
half-wave symmetry. however, we will compute all coefficients to verify this. also, for brevity, we
will assume that .
the coefficients are found from
(6.24)
and since is an integer (positive or negative) or zero, the terms inside the parentheses on the
second line of (6.24) are zero and therefore, all coefficients are zero, as expected, since the
square waveform has odd symmetry. also, by inspection, the average ( ) value is zero, but if
we attempt to verify this using (6.24), we will get the indeterminate form . to work around
this problem, we will evaluate directly from (6.12). then,
(6.25)
the coefficients are found from (6.14), that is,
(6.26)
for , (6.26) yields
as expected, since the square waveform has half-wave symmetry.
for , (6.21) reduces to
and thus
? = 1
ai
an
1?
f(t) cosntdt
0
2?
? 1?
acosntdt
0
?
? (–a) cosntdt
?
2?
+ ? a
n?
sinnt 0
? sinnt ?
2? = = = ( – )
a
n?
( sinn? – 0 – sinn2? + sinn?) a
n?
= = (2sinn? – sinn2?)
n
ai
dc
0 ? 0
a0
a0
1?
a t d
0
?
? (–a)dt
?
2?
+ ? a?
= = -(? – 0 – 2? + ?) = 0
bi
bn
1?
f(t) sinntdt
0
2?
? 1?
asinntdt
0
?
? (–a) sinntdt
?
2?
+ ? a
n?
–cosnt 0
? cosnt ?
2? = = = ( + )
a
n?
(– cosn? + 1 + cos2n? – cosn?) a
n?
= = (1–2cosn? + cos2n?)
n = even
bn
a
n?
= (1–2 + 1) = 0
n = odd
bn
a
n?
(1 + 2 + 1) 4a
n?
= =
b1
4a
?
=
chapter 6 fourier, taylor, and maclaurin series
6-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and so on.
therefore, the trigonometric fourier series for the square waveform with odd symmetry is
(6.27)
it was stated above that, if the given waveform has half-wave symmetry, and it is also an odd or an
even function, we can integrate from to , and multiply the integral by . we will apply this
property to the following example.
example 6.2
compute the trigonometric fourier series of the square waveform of example 1 by integrating
from to , and multiplying the result by .
solution:
since the waveform is an odd function and has half-wave symmetry, we are only concerned with
the odd coefficients. then,
(6.28)
for , (6.28) becomes
(6.29)
as before, and thus the series is the same as in example 1.
example 6.3
compute the trigonometric fourier series of the square waveform of figure 6.15.
solution:
this is the same waveform as in example 6.1, except that the ordinate has been shifted to the
right by radians, and has become an even function. however, it still has half-wave symmetry.
therefore, the trigonometric fourier series will consist of odd cosine terms only.
b3
4a
3?
=
b5
4a
5?
=
f(t) 4a
?
?t 13
3?t 15
? sin + sin + sin5?t + …?
? ? 4a
?
1n
sinn?t
n = odd
= = ?
0 ? ? 2 4
0 ? ? 2 4
bn
bn 41?
f(t) sinntdt
0
? ? 2
? 4a
n?
–cosnt 0
? ? 2 ( ) 4a
n?
n?2
?– cos + 1?
= = = ? ?
n = odd
bn
4a
n?
(– 0 + 1) 4a
n?
= =
? ? 2
numerical analysis using matlab and spreadsheets, second edition 6-15
orchard publications
waveforms in trigonometric form of fourier series
figure 6.15. waveform for example 6.3
since the waveform has half-wave symmetry and is an even function, it will suffice to integrate
from to , and multiply the integral by . the coefficients are found from
(6.30)
we observe that for , all coefficients are zero, and thus all even harmonics are zero
as expected. also, by inspection, the average ( ) value is zero.
for , we observe from (6.30) that , will alternate between and depending
on the odd integer assigned to . thus,
(6.31)
for , and so on, (6.30) becomes
and for , and so on, it becomes
then, the trigonometric fourier series for the square waveform with even symmetry is
(6.32)
0
? / 2
2?
t
?t
a
?a
?
3? / 2
0 ? ? 2 4 an
an 41?
f(t) cosntdt
0
? ? 2
? 4?
acosntdt
0
? ? 2
? 4a
n?
sinnt 0
? ? 2 ( ) 4a
n?
n?2
? sin ?
= = = = ? ?
n = even an
dc
n odd = n?2
sin +1 –1
n
an
4a
n?
= ±
n = 1, 5, 9, 13
an
4a
n?
=
n = 3, 7, 11, 15
an
–4a
n?
= -
f(t) 4a
?
? cos t 13
– 3?t 15
? cos + cos5?t –…?
? ? 4a
?
(–1)
(n – 1)
2
1n
cosn?t
n = odd
= = ?
chapter 6 fourier, taylor, and maclaurin series
6-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
alternate solution:
since the waveform of example 6.3 is the same as of example 6.1, but shifted to the right by
radians, we can use the result of example 6.1, i.e.,
(6.33)
and substitute with , that is, we let . with this substitution, relation
(6.33) becomes
(6.34)
and using the identities , , and so on, we rewrite
(6.34) as
(6.35)
and this is the same as (6.27).
therefore, if we compute the trigonometric fourier series with reference to one ordinate, and
afterwards we want to recompute the series with reference to a different ordinate, we can use the
above procedure to save time.
example 6.4
compute the trigonometric fourier series of the sawtooth waveform of figure 6.14.
figure 6.16. sawtooth waveform
? ? 2
f(t) 4a
?
?t 13
3?t 15
? sin + sin + sin5?t + …?
= ? ?
?t ?t + ? ? 2 ?t = ?? + ? ? 2
f(?) 4a
?
?? ?2
? + ?
? ? 13
3 ?? ?2
? + ?
? ? 15
5 ?? ?2
? + ?
= sin + sin + sin ? ? + …
4a
?
?? ?2
? + ?
? ? 13
3?? 3?
2
? + ?
? ? 15
5?? 5?
2
? + ?
= sin + sin + sin? ? +…
sin(x + ? ? 2) = cosx sin(x + 3? ? 2) = –cosx
f(?) 4a
?
?? 13
3?? 15
= cos – cos + cos5?? – …
0 2?
t
?t
a
?a
?? ?
?2?
numerical analysis using matlab and spreadsheets, second edition 6-17
orchard publications
waveforms in trigonometric form of fourier series
solution:
this waveform is an odd function but has no half-wave symmetry therefore, it contains sine
terms only with both odd and even harmonics. accordingly, we only need to evaluate the
coefficients. by inspection, the component is zero. as before, we will assume that .
if we choose the limits of integration from to we will need to perform two integrations since
however, we can choose the limits from to , and thus we will only need one integration
since
better yet, since the waveform is an odd function, we can integrate from to , and multiply the
integral by this is what we will do.
from tables of integrals,
(6.36)
then,
(6.37)
we observe that:
1.if , and . then, (6.37) reduces to
that is, the even harmonics have negative coefficients.
2.if , , . then,
that is, the odd harmonics have positive coefficients.
bn
dc ? = 1
0 2?
f(t)
a?
-t 0 < t < ?
a?
-t 2a – ? t 2? < < ?
?
?
?
?
=
–? +?
f t ( ) a?
= -t –? < t < ?
0 ?
2
?x sinaxdx 1
a2 - a sin x xa
= – cosax
bn
2?
a?
-t sinntdt
0
?
? 2a
?2 t sinntdt
0
?
? 2a
?2 1
n2 - nt sin tn
? – cosnt?
? ?
0
?
= = =
2a
n2?2 -( sinnt – nt cosnt) 0
? 2a
n2?2 = = -( sinn? – n?cosn?)
n = even sinn? = 0 cosn? = 1
bn
2a
n2?2 -(–n?) 2a
n?
= = –
n = odd sinn? = 0 cosn? = –1
bn
2a
n2?2 -(n?) 2a
n?
= =
chapter 6 fourier, taylor, and maclaurin series
6-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
thus, the trigonometric fourier series for the sawtooth waveform with odd symmetry is
(6.38)
example 6.5
find the trigonometric fourier series of the triangular waveform of figure 6.17. assume .
figure 6.17. triangular waveform for example 6.5
solution:
this waveform is an odd function and has half-wave symmetry then, the trigonometric fourier
series will contain sine terms only with odd harmonics. accordingly, we only need to evaluate the
coefficients. we will choose the limits of integration from to , and will multiply the
integral by .
by inspection, the component is zero. from tables of integrals,
(6.39)
then,
(6.40)
we are only interested in the odd integers of , and we observe that:
for odd integers of , the sine term yields
f(t) 2a
?
?t 12
2?t 13
3?t sin 14
? sin – sin + – sin4?t + …?
? ? 2a
?
(–1)n – 1 ? 1n
= = sinn?t
? = 1
0 2?
t
?t
a
?a
?? ?
?2?
?/2
bn 0 ? ? 2
4
dc
?x sinaxdx 1
a2 a sin x xa
= – cosax
bn
4?
2a
?
t sinntdt
0
? ? 2
? 8a
?2 t sinntdt
0
? ? 2
? 8a
?2 1
n2 nt sin tn
? – cosnt?
? ?
0
? ? 2
= = =
8a
n2?2 ( sinnt – nt cosnt) 0
? ? 2 8a
n2?2 n?2
sin n?2
n?2
? – cos ?
= = ? ?
n
n?2
cos = 0
n
numerical analysis using matlab and spreadsheets, second edition 6-19
orchard publications
waveforms in trigonometric form of fourier series
thus, the trigonometric fourier series for the triangular waveform with odd symmetry is
(6.41)
example 6.6
a half-wave rectification waveform is defined as
(6.42)
express as a trigonometric fourier series. assume .
solution:
the waveform for this example is shown in figure 6.18.
figure 6.18. for example 6.6
by inspection, the average is a non-zero value, and the waveform has neither odd nor even symmetry.
therefore, we expect all terms to be present. the coefficients are found from
for this example,
n?2
sin
1 for n 1 5 9 … then bn
8a
n2?2 = , , , , = -
–1 for n 3 7 11 … then bn
8a
n2?2 = , , , , = – -
? ? ? ? ? ? ?
=
f(t) 8a
?2 ? sin t 19
– 3?t 1
25
sin5?t 1
49
? sin + – - sin7?t + …?
? ? 8a
?2 (–1)
(n – 1)
2
1
n2 - sinn?t
n = odd
= = ?
f (t)
sin?t 0 < ?t < ?
0 ? ?t 2? < < ?
?
?
=
f (t) ? = 1
?2? ?? 0 ? 2?
f (t)
an
an
1?
f(t) cosntdt
0
2?
= ?
an
a?
- sint cosntdt
0
?
? a?
- 0cosntdt
?
2?
= + ?
chapter 6 fourier, taylor, and maclaurin series
6-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and from tables of integrals
then,
(6.43)
using the trigonometric identities
and
we get
and
then, by substitution into (6.43),
(6.44)
next, we can evaluate all the coefficients, except , from (6.44).
first, we will evaluate to obtain the value. by substitution of , we get
therefore, the value is
(6.45)
we cannot use (6.44) to obtain the value of therefore, we will evaluate the integral
from tables of integrals,
?( sinmx)( cosnx)dx cos(m – n)x
2(m – n)
– - cos(m + n)x
2(m + n)
= – (m2 ? n2)
an
a?
- 12
cos(1 – n)t
1 – n
- cos(1 + n)t
1 + n
+ -
0
?

? ?
? ?
? ?
=
a
2?
– cos(? – n?)
1 – n
- cos(? + n?)
1 + n
+ - 1
1 – n
- 1
n + 1
– +
? ?
? ?
? ?
=
cos(x – y) = cosx cosy + sinxsiny
cos(x + y) = cosx cosy – sinx siny
cos(? – n?) = cos?cosn? + sin?sinn? = –cosn?
cos(? + n?) = cos?cosn? – sin?sinn? = –cosn?
an
a
2?
– –cosn?
1 – n
–cosn?
1 + n
+ 2
1 – n2
– -
? ?
? ?
? ? a
2?
cosn?
1 – n
- cosn?
1 + n
+ - 2
1 – n2
+ -
? ?
? ?
? ?
= =
a
2?
cosn? + ncosn? + cosn? – ncosn?
1 n2 –
- 2
1 n2 –
? + -?
? ? a?
- cosn? + 1
1 n2 ( – )
? ?
= = ? ? n ? 1
an a1
a0 dc n = 0 a0 = 2a ? ?
dc
12
a0
a?
= -
a1
a1
a?
- sint costdt
0
?
= ?
?( sinax)( cosax)dx 1
2a
= ( sinax)2
numerical analysis using matlab and spreadsheets, second edition 6-21
orchard publications
waveforms in trigonometric form of fourier series
and thus,
(6.46)
from (6.44) with , we get
(6.47)
(6.48)
we see that for odd integers of , . however, for , we get
(6.49)
(6.50)
(6.51)
and so on.
now, we need to evaluate the coefficients. for this example,
and from tables of integrals,
therefore,
that is, all the coefficients, except , are zero.
a1
a
2?
( sint)2
0
?
= = 0
n = 2, 3, 4, 5, …
a2
a?
- cos2? + 1
(1 – 22)
? ?
? ? 2a
3?
= = –
a3
a( cos3? + 1)
?(1 – 32)
= - = 0
n an = 0 n = even
a4
a( cos4? + 1)
?(1 – 42)
- 2a
15?
= = – -
a6
a( cos6? + 1)
?(1 – 62)
- 2a
35?
= = – -
a8
a( cos8? + 1)
?(1 – 82)
- 2a
63?
= = – -
bn
bn a1?
f(t) sinntdt
0
2?
? a?
- sint sinntdt
0
?
? a?
- 0sinntdt
?
2?
= = + ?
?( sinmx)( sinnx)dx sin(m – n)x
2(m – n)
sin(m + n)x
2(m + n)
= – - (m2 ? n2)
bn
a? - 12
? sin(1 – n)t
1 – n
sin(1 + n)t
1 + n

0
?
? ?
? ?
? ?
=
a
2?
sin(1 – n)?
1 – n
sin(1 + n)?
1 + n
= – – 0 + 0 = 0 (n ? 1)
bn b1
chapter 6 fourier, taylor, and maclaurin series
6-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we will find by direct substitution into (6.14) for . thus,
(6.52)
combining (6.45) and (6.47) through (6.52), we find that the trigonometric fourier series for the
half-wave rectification waveform with no symmetry is
(6.53)
example 6.7
a full-wave rectification waveform is defined as
(6.54)
express as a trigonometric fourier series. assume .
solution:
the waveform is shown in figure 6.19 where the ordinate was arbitrarily chosen as shown.
figure 6.19. full-wave rectified waveform with even symmetry
by inspection, the average is a non-zero value. we choose the period of the input sinusoid so that
the output will be expressed in terms of the fundamental frequency. we also choose the limits of
integration as and , we observe that the waveform has even symmetry.
therefore, we expect only cosine terms to be present. the coefficients are found from
where for this example,
b1 n = 1
b1
a?
- ( sint)2dt
0
?
? a?
- t2
sin2t
4

0
? a?
- ?2
sin2?
4
– a2
= = = = -
f t ( ) a?
- a2
- t sin a?
- 2t cos3
- cos4t
15
- cos6t
35
- cos8t
63
= + – + + + - + …
f (t) = asin?t
f (t) ? = 1
a
?2? ?? 0 ? 2?
–? +?
an
an
1?
f(t) cosntdt
0
2?
= ?
numerical analysis using matlab and spreadsheets, second edition 6-23
orchard publications
waveforms in trigonometric form of fourier series
(6.55)
and from tables of integrals,
since
we express (6.55) as
(6.56)
to simplify the last expression in (6.56), we make use of the trigonometric identities
and
then, (6.56) simplifies to
(6.57)
now, we can evaluate all the coefficients, except , from (6.57). first, we will evaluate to
obtain the value. by substitution of , we get
therefore, the value is
(6.58)
from (6.57) we observe that for all , other than , .
an
1?
asint cosntdt
–?
?
? 2a
?
sint cosntdt
0
?
= = ?
?( sinmx)( cosnx)dx cos(m – n)x
2(n – m)
- cos(m + n)x
2(m + n)
= – (m2 ? n2)
cos(x – y) = cos(y – x) = cosx cosy + sinxsiny
an
2a
?
12
? cos(n – 1)t
n – 1
- cos(n + 1)t
n + 1
– -
0
?
? ?
? ?
? ?
=
a?
- cos(n – 1)?
n – 1
- cos(n + 1)?
n + 1
– - 1
n – 1
- 1
n + 1
– –
? ?
? ?
? ?
=
a?
- 1 – cos(n? + ?)
n + 1
cos(n? – ?) – 1
n – 1
= +
cos(n? + ?) = cosn?cos? – sinn?sin? = –cosn?
cos(n? – ?) = cosn?cos? + sinn?sin? = –cosn?
an
a?
- 1 + cosn?
n + 1
1 + cosn?
n – 1
– a?
- – 2 + (n – 1) cosn? – (n + 1) cosn?
n2 – 1
= = -
–2a( cosn? + 1)
?(n2 – 1)
= n ? 1
an a1 a0
dc n = 0
a0
4a
?
=
dc
12
a0
2a
?
=
n = odd n = 1 an = 0
chapter 6 fourier, taylor, and maclaurin series
6-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
to obtain the value of , we must evaluate the integral
from tables of integrals,
and thus,
(6.59)
for , from (6.57) we get
(6.60)
(6.61)
(6.62)
(6.63)
and so on. then, combining the terms of (6.58) and (6.60) through (6.63) we get
(6.64)
therefore, the trigonometric form of the fourier series for the full-wave rectification waveform with
even symmetry is
(6.65)
this series of (6.65) shows that there is no component of the fundamental frequency. this is
because we chose the period to be from and . generally, the period is defined as the shortest
period of repetition. in any waveform where the period is chosen appropriately, it is very
unlikely that a fourier series will consist of even harmonic terms only.
a1
a1
1?
sint costdt
0
?
= ?
?( sinax)( cosax)dx 1
2a
= ( sinax)2
a1
1
2?
( sint)2
0
?
= = 0
n = even
a2
–2a( cos2? + 1)
?(22 – 1)
4a
3?
= = –
a4
–2a( cos4? + 1)
?(42 – 1)
4a
15?
= = – -
a6
–2a( cos6? + 1)
?(62 – 1)
4a
35?
= = – -
a8
–2a( cos8? + 1)
?(82 – 1)
4a
63?
= = – -
f(t) 2a
?
4a
?
cos2?t
3
cos4?t
15
cos6?t
35
cos8?t
63
+ + + + …
? ?
? ?
? ?
= –
f(t) 2a
?
4a
?
1
(n2 – 1)
cosn?t
n = 2, 4, 6,…
??
= –
–? +?
numerical analysis using matlab and spreadsheets, second edition 6-25
orchard publications
alternate forms of the trigonometric fourier series
6.5 alternate forms of the trigonometric fourier series
we recall that the trigonometric fourier series is expressed as
(6.66)
if a given waveform does not have any kind of symmetry, it may be advantageous of using the
alternate form of the trigonometric fourier series where the cosine and sine terms of the same frequency
are grouped together, and the sum is combined to a single term, either cosine or sine.
however, we still need to compute the and coefficients separately.
we use the triangle shown in figure 6.20 for the derivation of the alternate forms.
figure 6.20. derivation of the alternate form of the trigonometric fourier series
we assume , and for , we rewrite (6.66) as
and, in general, for , we get
(6.67)
f t ( ) 12
= a0 + a1 cos?t + a2 cos2?t + a3 cos3?t + a4 cos4?t + …
+ b1 sin?t + b2 sin2?t + b3 sin3?t + b4 sin4?t +…
an bn
an
bn
cn
?n
?n
sin?n
bn
an + bn
-
bn
cn
= = -
cn = an + bn
cos?n
an
an + bn
-
an
cn
= = -
?n
bn
an
= atan - ?n
an
bn
cos?n = sin?n = atan -
? = 1 n = 1, 2, 3,…
f t ( ) 12
a0 c1
a1
c1
t
b1
c1
? cos + sint?
? ? c2
a2
c2
2t
b2
c2
? cos + sin2t?
= + + ? ? + …
+ cn
an
cn
- nt
bn
cn
? cos + - sinnt?
? ?
12
a0 c1
cos?1 cost + sin?1 sint
? cos(t – ?1) ?
? ? c2
cos?2 cos2t + sin?2 sin2t
? cos(2t – ?2) ?
= + + ? ? + …
+ cn
cos?n cosnt + sin?n sinnt
? cos(nt – ?n) ?
? ?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
? ? 1
f t ( ) 12
a0 cn cos(n?t – ?n)
n = 1
??
+ 12
a0 cn n?t
bn
an
? – atan -?
cos? ?
n = 1
??
= = +
chapter 6 fourier, taylor, and maclaurin series
6-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
similarly,
and, in general, where , we get
(6.68)
the series of (6.67) and (6.68) can be expressed as phasors. since it is customary to use the cosine
function in the time domain to phasor transformation, we choose to use the transformation of
(6.63) below.
(6.69)
example 6.8
find the first 5 terms of the alternate form of the trigonometric fourier series for the waveform of
figure 6.21.
figure 6.21. waveform for example 6.8
solution:
the given waveform has no symmetry thus, we expect both cosine and sine functions with odd
and even terms present. also, by inspection the value is not zero.
we will compute the and coefficients, the value, and we will combine them to get an
expression in the form of (6.63). then,
f t ( ) 12
a0 c1
sin?1 cost + cos?1 sint
? sin(t + ?1) ?
= + ? ?
c2
sin?2 cos2t + cos?2 sin2t
? sin(2t + ?2) ?
? ? … cn
sin?n cosnt + cos?n sinnt
? sin(nt + ?n) ?
+ + ? ?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
?
? ? 1
f t ( ) 12
a0 cn sin(n?t + ?n)
n = 1
??
+ 12
a0 cn n?t
an
bn
? + atan -?
sin? ?
n = 1
??
= = +
12
a0 cn n?t
bn
an
? – atan -?
cos? ?
n = 1
??
+ 12
a0 cn
bn
an
?–atan -
n = 1
??
? +
1
2
3
?/2 ? 3?/2 2?
t
f(t)
? = 1
dc
an bn dc
numerical analysis using matlab and spreadsheets, second edition 6-27
orchard publications
alternate forms of the trigonometric fourier series
(6.70)
we observe that for , .
for ,
(6.71)
and
(6.72)
the value is
(6.73)
the coefficients are
(6.74)
then,
(6.75)
(6.76)
(6.77)
(6.78)
from (6.69),
an
1?
3 ( ) nt cos t 1?
(1) cosntdt
? ? 2
2?
d + ?
0
? ? 2
? 3
n?
sinnt
0
? ? 2 1
n?
sinnt
? ? 2
2?
= = +
3
n?
n?2
sin 1
n?
sinn2? 1
n?
n?2
+ – sin 2
n?
n?2
= = sin
n = even an = 0
n = odd
a1
2?
=
a3
2
3?
= –
dc
12
a0
1
2?
(3)dt
0
? ? 2
? 1
2?
(1)dt
? ? 2
2?
+ ? 1
2?
3t 0
? ? 2 t ? ? 2
2? = = ( + )
1
2?
3?
2
2? ?2
? + – ?
? ? 1
2?
? 2? + ( ) 32
= = =
bn
bn
1?
3 ( ) nt sin t 1?
(1) sinntdt
? ? 2
2?
d + ?
0
? ? 2
? –3
n?
cosnt
0
? ? 2 –1
n?
cosnt
? ? 2
2?
= = +
–3
n?
n?2
cos 3
n?
–1
n?
cosn2? 1
n?
n?2
+ + + cos 1
n?
(3 – cosn2?) 2
n?
= = =
b1 = 2 ? ?
b2 = 1 ? ?
b3 = 2 ? 3?
b4 = 1 ? 2?
12
a0 cn n?t
bn
an
? – atan -?
cos? ?
n = 1
??
+ 12
a0 cn
bn
an
?–atan -
n = 1
??
? +
chapter 6 fourier, taylor, and maclaurin series
6-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
where
(6.79)
thus, for , we get:
(6.80)
similarly,
(6.81)
(6.82)
and
(6.83)
combining the terms of (6.73) and (6.80) through (6.83), we find that the alternate form of the
trigonometric fourier series representing the waveform of this example is
(6.84)
6.6 the exponential form of the fourier series
the fourier series are often expressed in exponential form. the advantage of the exponential
form is that we only need to perform one integration rather than two, one for the , and another
for the coefficients in the trigonometric form of the series. moreover, in most cases the integration
is simpler.
the exponential form is derived from the trigonometric form by substitution of
(6.85)
and
cn
bn
an
- atan – ? an 2 bn 2 +
bn
an
- atan – ? an 2 bn 2 = = + ?–?n = an – jbn
n = 1, 2, 3, and 4
a1 jb1 – 2?
j2?
– 2?
? ?
? ? 2 2?
? ?
? ? 2
= = + ?–45°
8
?2
?–45° 2 2
?
?–45° 2 2
?
= = ? cos(?t – 45°)
a2 jb2 – 0 j1?
– 1?
= = ?–90° 1?
? cos(2?t – 90°)
a3 – jb3 2
3?
– j 2
3?
– 2 2
3?
= = ?–135° 2 2
3?
? cos(3?t – 135°)
a4 – jb4 0 j 1
2?
– 1
2?
= = ?–90° 1
2?
? cos(4?t – 90°)
f t ( ) 32
1?
= + [2 2cos(?t – 45°) + cos(2?t – 90°)
+ 2 2
3
3?t 135° – ( ) cos 12+ cos(4?t – 90°) + … ]
an
bn
cos?t e j?t e–j?t +
2
= -
numerical analysis using matlab and spreadsheets, second edition 6-29
orchard publications
the exponential form of the fourier series
(6.86)
into . thus,
(6.87)
and grouping terms with same exponents, we get
(6.88)
the terms of (6.88) in parentheses are usually denoted as
(6.89)
(6.90)
(6.91)
then, (6.88) is written as
(6.92)
we must remember that the coefficients, except , are complex and occur in complex conjugate
pairs, that is,
(6.93)
we can derive a general expression for the complex coefficients , by multiplying both sides of
(6.92) by and integrating over one period, as we did in the derivation of the and
coefficients of the trigonometric form. then, with ,
sin?t e j?t e–j?t –
j2
= -
f(t)
f t ( ) 12
a0 a1
e j?t e–j?t +
2
? - ?
? ? a2
e j2?t e–j2?t +
2
? ?
? ?
… b1
e j?t e–j?t –
j2
? -?
? ? b2
e j2?t e–j2?t –
j2
? ?
? ? …
+ + +
+ + +
=
f(t) … a2
2
b2
j2
? – ?
? ? e–j2?t a1
2
b1
j2
? – ?
? ? e j?t – 12
a0
a1
2
b1
j2
? + ?
? ?ej?t a2
2
b2
j2
? + ?
= + + + + + ? ? ej2?t
c–n
12
an
bn
j
? – -?
? ? 12
= = (an + jbn)
cn
12
an
bn
j
? + -?
? ? 12
= = (an–jbn)
c0
12
= a0
f(t) … c–2e–j2?t c–1e–j?t = + + + c0 + c1e j?t + c2ej2?t + …
ci c0
c–n cn= ?
cn
e–jn?t an bn
? = 1
chapter 6 fourier, taylor, and maclaurin series
6-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(6.94)
we observe that all the integrals on the right side of (6.97) are zero except the last one. therefore,
or
and, in general, for ,
(6.95)
or
(6.96)
we can derive the trigonometric fourier series from the exponential series by addition and subtraction
of the exponential form coefficients and . thus, from (6.89) and (6.90),
or
(6.97)
similarly,
(6.98)
or
(6.99)
symmetry in exponential series
1. for even functions, all coefficients are real
f(t)e–jnt dt
0
2?
? … c–2 e–j2te–jnt dt
0
2?
? c–1 e–jte–jnt dt
0
2?
?
+ c0 e–jnt dt
0
2?
? c1 e jte–jnt dt
0
2?
?
+ c2 e j2te–jnt dt
0
2?
? … cne jnte–jnt dt
0
2?
?
+ +
+
+ +
=
f(t)e–jnt dt
0
2?
? cne jnte–jnt dt
0
2?
? cndt
0
2?
= = ? = 2?cn
cn
1
2?
f(t)e–jnt dt
0
2?
= ?
? ? 1
cn
1
2?
f(t)e–jn?t d(?t)
0
2?
= ?
cn
1t
- f(t)e–jn?t d(?t)
0
t
= ?
cn c–n
cn + c–n 12
= (an – jbn + an + jbn)
an = cn + c–n
cn – c–n 12
= (an–jbn–an–jbn)
bn = j(cn – c–n)
ci
numerical analysis using matlab and spreadsheets, second edition 6-31
orchard publications
the exponential form of the fourier series
we recall from (6.89) and (6.90) that
(6.100)
and
(6.101)
since even functions have no sine terms, the coefficients in (6.100) and (6.101) are zero.
therefore, both and are real.
2. for odd functions, all coefficients are imaginary
since odd functions have no cosine terms, the coefficients in (6.100) and (6.101) are zero.
therefore, both and are imaginary.
3. if there is half-wave symmetry, for
we recall from the trigonometric fourier series that if there is half-wave symmetry, all even
harmonics are zero. therefore, in (6.100) and (6.101) the coefficients and are both zero
for , and thus, both and are also zero for .
4. if there is no symmetry, is complex.
5. always
this can be seen in (6.100) and (6.101)
example 6.9
compute the exponential fourier series for the square waveform of figure 6.22 below. assume
that .
figure 6.22. waveform for example 6.9
c–n
12
an
bn
j
? – -?
? ? 12
= = (an + jbn)
cn
12
an
bn
j
? + -?
? ? 12
= = (an–jbn)
bn
c–n cn
ci
an
c–n cn
cn = 0 n = even
an bn
n = even c–n cn n = even
f(t)
c–n cn= ?
? = 1
0
? 2?
t
a
?a
?t
chapter 6 fourier, taylor, and maclaurin series
6-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
this is the same waveform as in example 6.1, and as we know, it is an odd function, has half-wave
symmetry, and its component is zero. therefore, the coefficients will be imaginary,
for , and . using (6.95) with , we get
and for ,
as expected.
for ,
(6.102)
for , then,
(6.103)
as expected.
for , . therefore,
(6.104)
using (6.92), that is,
we obtain the exponential fourier series for the square waveform with odd symmetry as
dc cn
cn = 0 n = even c0 = 0 ? = 1
cn
1
2?
f(t)e–jnt dt
0
2?
? 1
2?
ae–jnt dt
0
?
? 1
2?
–ae–jnt dt
?
2?
= = + ?
n = 0
c0
1
2?
ae–0 dt
0
?
? (–a)e–0 dt
?
2?
+ ? a
2?
= = (? – 2? + ?) = 0
n ? 0
cn
1
2?
ae–jnt dt
0
?
? –ae–jnt dt
?
2?
+ ? 1
2?
a
–jn
-e–jnt
0
? –a
–jn
-e–jnt
?
2?
= = +
1
2?
a
–jn
- e–jn? ( – 1) a
jn
e–jn2? e–jn? + ( – ) a
2j?n
- 1 e–jn? – e–jn2? e–jn? = = ( + – )
a
2j?n
- 1 e–jn2? 2e–jn? ( + – ) a
2j?n
- e–jn? ( – 1)2
= =
n = even e–jn? = 1
cn
n = even
a
2j?n
- e–jn? ( – 1)2 a
2j?n
= = -(1 – 1)2 = 0
n = odd e–jn? = –1
cn n = odd
a
2j?n
- e–jn? ( – 1)2 a
2j?n
-(– 1 – 1)2 a
2j?n
-(–2)2 2a
j?n
= = = =
f(t) … c–2e–j2?t c–1e–j?t = + + + c0 + c1e j?t + c2e j2?t + …
numerical analysis using matlab and spreadsheets, second edition 6-33
orchard publications
line spectra
(6.105)
the minus (?) sign of the first two terms within the parentheses results from the fact that
. for instance, since , it follows that . we
observe that is purely imaginary, as expected, since the waveform is an odd function.
to prove that (6.105) and (6.22) are the same, we group the two terms inside the parentheses of
(6.105) for which this will produce the fundamental frequency . then, we group
the two terms for which , and this will produce the third harmonic , and so on.
6.7 line spectra
when the fourier series are known, it is useful to plot the amplitudes of the harmonics on a frequency
scale that shows the first (fundamental frequency) harmonic, and the higher harmonics
times the amplitude of the fundamental. such a plot is known as line spectrum and shows the
spectral lines that would be displayed by a spectrum analyzer*.
figure 6.23 shows the line spectrum of the square waveform of example 6.1.
figure 6.23. line spectrum for square waveform of example 6.1
figure 6.24 shows the line spectrum for the half-wave rectification waveform of example 6.6.
figure 6.24. line spectrum for half-wave rectifier of example 6.6
* an instrument that displays the spectral lines of a waveform.
f(t) 2a
j?
… 13
e–j3?t – e–j?t – e j?t 13
? + + e j3?t ?
? ? 2a
j?
1n
e jn?t
n = odd
= = ?
c–n cn= ? c3 = 2a ? j3? c–3 c3= ? = –2a ? j3?
f(t)
n = 1 sin?t
n = 3 sin3?t
bn
n?t
0 1 2 3 4 5 6 7 8 9
4/?
n?t 0 1
2 4 6 8
a/?
a/2
dc
chapter 6 fourier, taylor, and maclaurin series
6-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the line spectra of other waveforms can be easily constructed from the fourier series.
example 6.10
compute the exponential fourier series for the waveform of figure 6.25, and plot its line spectra.
assume .
figure 6.25. waveform for example 6.11
solution:
this recurrent rectangular pulse is used extensively in digital communications systems. to determine
how faithfully such pulses will be transmitted, it is necessary to know the frequency components.
as shown in figure 6.25, the pulse duration is . thus, the recurrence interval (period) , is
times the pulse duration. in other words, is the ratio of the pulse repetition time to the duration
of each pulse.
for this example, the components of the exponential fourier series are found from
(6.106)
the value of the average ( component) is found by letting . then, from (6.106) we get
(6.107)
for the values for , integration of (6.106) yields
(6.108)
and thus,
? = 1
0
??/? 2?
t
?t
a
?/? ?
t/?
?2? ??
t ? k t k
k
cn
1
2?
ae–jnt dt
–?
?
? a
2?
e–jnt dt
–? ? k
? ? k
= = ?
dc n = 0
c0
a
2?
t
–? ? k
? ? k a
2?
?k
?k
? + ?
? ? ak
= = = -
n ? 0
cn
a
–jn2?
e–jnt
–? ? k
? ? k a
n?
e jn? k ? e –jn? ? k –
j2
? a
n?
n?
k
? ?
= = = ? sin? ?
asin(n? ? k)
n?
- ak
- sin(n? ? k)
n? ? k
= = ? -
numerical analysis using matlab and spreadsheets, second edition 6-35
orchard publications
line spectra
(6.109)
the relation of (6.109) has the form, and the line spectrum is shown in figures 6.26
through 6.28, for , and .
figure 6.26. line spectrum of (6.109) for
figure 6.27. line spectrum of (6.109) for
figure 6.28. line spectrum of (6.112) for
f t ( ) ak
- sin(n? ? k)
n? ? k
? -
n = –?
??
=
sinx ? x
k = 2 k = 5 k = 10
s in(n? /k )/(n? /k )
-10 -8 -6 -4 -2 0 2 4 6 8 10
k = 2
k = 2
sin(n?/k)/(n?/k)
-10 -5 0 5 10
k = 5
k = 5
sin(n?/k)/(n?/k)
-10 -5 0 5 10
k = 10
k = 10
chapter 6 fourier, taylor, and maclaurin series
6-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the spectral lines are separated by the distance and thus, as gets larger, the lines get closer
together while the lines are further apart as gets smaller. although the space between lines
seems to be the same in each case, we should observe that the number of lines between line crossings,
are different.
6.8 numerical evaluation of fourier coefficients
quite often, it is necessary to construct the fourier expansion of a function based on observed
values instead of an analytic expression. examples are meteorological or economic quantities
whose period may be a day, a week, a month or even a year. in these situations, we need to evaluate
the integral(s) using numerical integration.
the procedure presented here, will work for both the waveforms that have an analytical solution
and those that do not. even though we may already know the fourier series from analytical methods,
we can use this procedure to check our results.
consider the waveform of shown in figure 6.29, were we have divided it into small pulses of
width . obviously, the more pulses we use, the better the approximation.
if the time axis is in degrees, we can choose to be and it is convenient to start at the zero
point of the waveform. then, using a spreadsheet, such as microsoft excel, we can divide the
period to in intervals, and enter these values in column of the spreadsheet.
figure 6.29. waveform whose analytical expression is unknown
since the arguments of the sine and the cosine are in radians, we multiply degrees by
(3.1459...) and divide by to perform the conversion. we enter these in column and we
denote them as . in column we enter the corresponding values of as measured from
the waveform. in columns and we enter the values of and the product respectively.
similarly, we enter the values of and in columns and respectively.
1 ? k k
k
f(x)
?x
?x 2.5°
0° 360° 2.5° a
x
f(x)
?
180 b
x c y = f(x)
d e cosx y cosx
sinx y sinx f g
numerical analysis using matlab and spreadsheets, second edition 6-37
orchard publications
power series expansion of functions
next, we form the sums of and , we multiply these by , and we divide by to
obtain the coefficients and . to compute the coefficients of the higher order harmonics, we
form the products , , , , and so on, and we enter these in subsequent
columns of the spreadsheet.
figure 6.30 is a partial table showing the computation of the coefficients of the square waveform,
and figure 6.31 is a partial table showing the computation of the coefficients of a clipped sine
waveform. the complete tables extend to the seventh harmonic to the right and to down.
6.9 power series expansion of functions
a power series has the form
(6.110)
some familiar power series expansions for real values of are
(6.111)
(6.112)
(6.113)
the following example illustrates the fact that a power series expansion can lead us to a fourier
series.
example 6.11
if the applied voltage is small (no greater than 5 volts), the current in a semiconductor diode
can be approximated by the relation
(6.114)
where and are arbitrary constants, and the input voltage is a sinusoid, that is,
(6.115)
express the current in (6.114) as a power series.
y cosx y sinx ?x ?
a1 b1
y cos2x y sin2x y cos3x y sin3x
360°
akxk
k = 0
??
= a0 + a1x + a2x2 + …
x
ex 1 x x2
2!
x3
3!
x4
4!
= + + + + + …
sinx x x3
3!
– x5
5!
x7
7!
= + – + …
cosx 1 x2
2!
– x4
4!
x6
6!
= + – +…
v i
i = a(ekv – 1)
a k
v = vmax cos?t
i
chapter 6 fourier, taylor, and maclaurin series
6-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 6.30. numerical computation of the coefficients of the square waveform (partial listing)
analytical:
f(t)=4(sinwt/p+sin3wt/3p+sin5wt/5p+ ….)
numerical:
average= dc= 0.000
a1= 0.000 b1= 1.273
a2= 0.000 b2= 0.000
a3= 0.000 b3= 0.424
a4= 0.000 b4= 0.000
a5= 0.000 b5= 0.254
a6= 0.000 b6= 0.000
a7= 0.000 b7= 0.180
x(deg) x(rad) y=f(x) 0.5*a0 cosx ycosx sinx ysinx cos2x ycox2x sin2x ysin2x cos3x ycos3x sin3x ysin3x
0.0 0.000 0.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000 0.000 0.000
2.5 0.044 1.000 0.044 0.999 0.999 0.044 0.044 0.996 0.996 0.087 0.087 0.991 0.991 0.131 0.131
5.0 0.087 1.000 0.044 0.996 0.996 0.087 0.087 0.985 0.985 0.174 0.174 0.966 0.966 0.259 0.259
7.5 0.131 1.000 0.044 0.991 0.991 0.131 0.131 0.966 0.966 0.259 0.259 0.924 0.924 0.383 0.383
10.0 0.175 1.000 0.044 0.985 0.985 0.174 0.174 0.940 0.940 0.342 0.342 0.866 0.866 0.500 0.500
12.5 0.218 1.000 0.044 0.976 0.976 0.216 0.216 0.906 0.906 0.423 0.423 0.793 0.793 0.609 0.609
15.0 0.262 1.000 0.044 0.966 0.966 0.259 0.259 0.866 0.866 0.500 0.500 0.707 0.707 0.707 0.707
17.5 0.305 1.000 0.044 0.954 0.954 0.301 0.301 0.819 0.819 0.574 0.574 0.609 0.609 0.793 0.793
20.0 0.349 1.000 0.044 0.940 0.940 0.342 0.342 0.766 0.766 0.643 0.643 0.500 0.500 0.866 0.866
22.5 0.393 1.000 0.044 0.924 0.924 0.383 0.383 0.707 0.707 0.707 0.707 0.383 0.383 0.924 0.924
25.0 0.436 1.000 0.044 0.906 0.906 0.423 0.423 0.643 0.643 0.766 0.766 0.259 0.259 0.966 0.966
27.5 0.480 1.000 0.044 0.887 0.887 0.462 0.462 0.574 0.574 0.819 0.819 0.131 0.131 0.991 0.991
30.0 0.524 1.000 0.044 0.866 0.866 0.500 0.500 0.500 0.500 0.866 0.866 0.000 0.000 1.000 1.000
32.5 0.567 1.000 0.044 0.843 0.843 0.537 0.537 0.423 0.423 0.906 0.906 -0.131 -0.131 0.991 0.991
35.0 0.611 1.000 0.044 0.819 0.819 0.574 0.574 0.342 0.342 0.940 0.940 -0.259 -0.259 0.966 0.966
37.5 0.654 1.000 0.044 0.793 0.793 0.609 0.609 0.259 0.259 0.966 0.966 -0.383 -0.383 0.924 0.924
40.0 0.698 1.000 0.044 0.766 0.766 0.643 0.643 0.174 0.174 0.985 0.985 -0.500 -0.500 0.866 0.866
42.5 0.742 1.000 0.044 0.737 0.737 0.676 0.676 0.087 0.087 0.996 0.996 -0.609 -0.609 0.793 0.793
45.0 0.785 1.000 0.044 0.707 0.707 0.707 0.707 0.000 0.000 1.000 1.000 -0.707 -0.707 0.707 0.707
47.5 0.829 1.000 0.044 0.676 0.676 0.737 0.737 -0.087 -0.087 0.996 0.996 -0.793 -0.793 0.609 0.609
50.0 0.873 1.000 0.044 0.643 0.643 0.766 0.766 -0.174 -0.174 0.985 0.985 -0.866 -0.866 0.500 0.500
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
0.0 2.0 4.0 6.0 8.0
square waveform
numerical analysis using matlab and spreadsheets, second edition 6-39
orchard publications
power series expansion of functions
figure 6.31. numerical computation of the coefficients of a clipped sine waveform (partial listing)
analytical:
f(t)=unknown
numerical:
dc= 0.000
a1= 0.000 b1= 0.609
a2= 0.000 b2= 0.000
a3= 0.000 b3= 0.138
a4= 0.000 b4= 0.000
a5= 0.000 b5= 0.028
a6= 0.000 b6= 0.000
a7= 0.000 b7= -0.010
x(deg) x(rad) y=f(x) 0.5*a0 cosx ycosx sinx ysinx cos2x ycox2x sin2x ysin2x cos3x ycos3x sin3x ysin3x
0.0 0.000 0.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000 0.000 0.000 1.000 0.000 0.000 0.000
2.5 0.044 0.044 0.002 0.999 0.044 0.044 0.002 0.996 0.043 0.087 0.004 0.991 0.043 0.131 0.006
5.0 0.087 0.087 0.004 0.996 0.087 0.087 0.008 0.985 0.086 0.174 0.015 0.966 0.084 0.259 0.023
7.5 0.131 0.131 0.006 0.991 0.129 0.131 0.017 0.966 0.126 0.259 0.034 0.924 0.121 0.383 0.050
10.0 0.175 0.174 0.008 0.985 0.171 0.174 0.030 0.940 0.163 0.342 0.059 0.866 0.150 0.500 0.087
12.5 0.218 0.216 0.009 0.976 0.211 0.216 0.047 0.906 0.196 0.423 0.091 0.793 0.172 0.609 0.132
15.0 0.262 0.259 0.011 0.966 0.250 0.259 0.067 0.866 0.224 0.500 0.129 0.707 0.183 0.707 0.183
17.5 0.305 0.301 0.013 0.954 0.287 0.301 0.090 0.819 0.246 0.574 0.172 0.609 0.183 0.793 0.239
20.0 0.349 0.342 0.015 0.940 0.321 0.342 0.117 0.766 0.262 0.643 0.220 0.500 0.171 0.866 0.296
22.5 0.393 0.383 0.017 0.924 0.354 0.383 0.146 0.707 0.271 0.707 0.271 0.383 0.146 0.924 0.354
25.0 0.436 0.423 0.018 0.906 0.383 0.423 0.179 0.643 0.272 0.766 0.324 0.259 0.109 0.966 0.408
27.5 0.480 0.462 0.020 0.887 0.410 0.462 0.213 0.574 0.265 0.819 0.378 0.131 0.060 0.991 0.458
30.0 0.524 0.500 0.022 0.866 0.433 0.500 0.250 0.500 0.250 0.866 0.433 0.000 0.000 1.000 0.500
32.5 0.567 0.500 0.022 0.843 0.422 0.537 0.269 0.423 0.211 0.906 0.453 -0.131 -0.065 0.991 0.496
35.0 0.611 0.500 0.022 0.819 0.410 0.574 0.287 0.342 0.171 0.940 0.470 -0.259 -0.129 0.966 0.483
37.5 0.654 0.500 0.022 0.793 0.397 0.609 0.304 0.259 0.129 0.966 0.483 -0.383 -0.191 0.924 0.462
40.0 0.698 0.500 0.022 0.766 0.383 0.643 0.321 0.174 0.087 0.985 0.492 -0.500 -0.250 0.866 0.433
42.5 0.742 0.500 0.022 0.737 0.369 0.676 0.338 0.087 0.044 0.996 0.498 -0.609 -0.304 0.793 0.397
45.0 0.785 0.500 0.022 0.707 0.354 0.707 0.354 0.000 0.000 1.000 0.500 -0.707 -0.354 0.707 0.354
47.5 0.829 0.500 0.022 0.676 0.338 0.737 0.369 -0.087 -0.044 0.996 0.498 -0.793 -0.397 0.609 0.304
50.0 0.873 0.500 0.022 0.643 0.321 0.766 0.383 -0.174 -0.087 0.985 0.492 -0.866 -0.433 0.500 0.250
-1.5
-1.0
-0.5
0.0
0.5
1.0
1.5
0.0 2.0 4.0 6.0 8.0
sine wave clipped at ?/6, 5?/6 etc.
chapter 6 fourier, taylor, and maclaurin series
6-40 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
the term inside the parentheses of (6.114) suggests the power series expansion of (6.111).
accordingly, we rewrite (6.114) as
(6.116)
substitution of (6.115) into (6.116) yields,
(6.117)
this expression can be simplified with the use of the following trigonometric identities:
(6.118)
then, substitution of (6.118) into (6.117) and after simplification, we obtain a series of the following
form:
(6.119)
we recall that the series of (6.119) is the trigonometric series form of the fourier series. we
observe that it consists of a constant term, a term of the fundamental frequency, and terms of all
harmonic frequencies, that is, higher frequencies which are multiples of the fundamental frequency.
6.10 taylor and maclaurin series
a function which possesses all derivatives up to order at a point can be expanded
in a taylor series as
(6.120)
if , (6.120) reduces to
ekv
i a 1 kv (kv)2
2!
(kv)3
3!
(kv)4
4!
? + + + + + … – 1?
? ? a kv (kv)2
2!
(kv)3
3!
(kv)4
4!
? + + + +…?
= = ? ?
i a kvmax cos?t
(kvp cos?t)2
2!
-
(kvp cos?t)3
3!
-
(kvp cos?t)4
4!
? + + + - + …?
= ? ?
x 2 cos 12
12
= + cos2x
x 3 cos 34
x cos 14
= + cos3x
x 4 cos 38
12
2x cos 18
= + + cos4x
i = a0 + a1 cos?t + a2 cos2?t + a3 cos3?t + a4 cos4?t + …
f (x) n x = x0
f (x) f(x0) f ?(x0)(x – x0)
f ??(x0)
2!
(x – x0)2 …
f (n) (x0)
n!
= + + + + (x – x0)n
x0 = 0
numerical analysis using matlab and spreadsheets, second edition 6-41
orchard publications
taylor and maclaurin series
(6.121)
relation (6.121) is known as maclaurin series, and has the form of power series of (6.110) with
.
to appreciate the usefulness and application of the taylor series, we will consider the plot of figure
6.32, where represents some experimental data for the current-voltage ( ) characteristics
of a semiconductor diode operating at the volts region.
figure 6.32. current-voltage (i-v) characteristics for a typical semiconductor diode
now, suppose that we want to approximate the function by a power series, in the neighborhood
of some arbitrary point shown in figure 6.33. we assume that the first derivatives
of the function exist at this point.
we begin by referring to the power series of (6.110), where we observe that the first term on the
right side is a constant. therefore, we are seeking a constant that it will be the best approximation
to the given curve in the vicinity of point . obviously, the horizontal line passes through
point , and we denote this first approximation as shown in figure 6.34.
f (x) f (0) f ?(0)x f ??(0)
2!
-x2 …
f (n) (0)
n!
= + + + + xn
an f (n) = (0) ? n!
i(v) i – v
0 ? v ? 5
v
i
0
i(v)
i(v)
p(v0, i0) n
i(v)
p i0
p a0
chapter 6 fourier, taylor, and maclaurin series
6-42 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 6.33. approximation of the function by a power series
figure 6.34. first approximation of
the next term in the power series is the linear term . thus, we seek a linear term of the form
. but since we want the power series to be a good approximation to the given function for
some distance on either side of point , we are interested in the difference . accordingly,
we express the desired power series as
(6.122)
now, we want the linear term to be the best approximation to the function in
the vicinity of point . this will be accomplished if the linear term has the same slope as the
given function as shown in figure 6.35.
v
i
0
i(v)
p(v0, i0)
v0
i0
i(v)
v
i
0
i(v)
p(v0, i0)
i0
v0
a0
i(v)
a1x
a0 + a1x
p v – v0
f (v) = a0 + a1(v – v0) + a2(v – v0)2 + a3(v – v0)3 + a4(v – v0)4 + …
a0 + a1(v – v0) i(v)
p
numerical analysis using matlab and spreadsheets, second edition 6-43
orchard publications
taylor and maclaurin series
figure 6.35. second approximation of
it is evident that the slope of at is and therefore, the linear term
can be expressed as .
the third term in (6.122), that is, is a quadratic and therefore, we choose such that
it matches the second derivative of the function in the vicinity of point as shown in figure
6.36.
figure 6.36. third approximation of
then, or . the remaining coefficients , and so on of
(6.122) are found by matching the third, fourth, fifth, and higher order derivatives of the given
function with these coefficients. when this is done, we obtain the following taylor series.
v
i
0 v0
i0 a0
i(v)
p(v0, i0)
a1(v – v0)
a0 + a1(v – v0)
i(v)
i(v) v0 i?(v0) = a1
a0 + a1(v – v0) i(v0) + i?(v0)(v – v0)
a2(v – v0)2 a2
i(v) p
v
i
0
i(v)
p(v0, i0) a0 + a1(v – v0)
a0
a1(v – v0)
i0
v0
a0 + a1(v – v0) + a2(v – v0)2
a2(v – v0)2
i(v)
2a2 = i??(v0) a2 = i??(v0) ? 2 a3, a4, a5
chapter 6 fourier, taylor, and maclaurin series
6-44 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(6.123)
we can also describe any function that has an analytical expression, by a taylor series as illustrated
by the following example.
example 6.12
compute the first three terms of the taylor series expansion for the function
(6.124)
at .
solution:
the taylor series expansion about point is given by
(6.125)
and since we are asked to compute the first three terms, we must find the first and second derivatives
of .
from math tables, , so . to find we need to find the first
derivative of , so we let . then, using , we get
(6.126)
next, using the trigonometric identity
(6.127)
and by substitution of (6.127) into (6.126), we get,
(6.128)
now, at point we have:
(6.129)
and by substitution into (6.125),
i(v) i(v0) i?(v0)(v – v0)
i??(v0)
2!
(v – v0)2 i???(v0)
3!
= + + + (v – v0)3 +…
y = f (x) = tanx
a = ? ? 4
a
fn(x) f (a) f ?(a)(x – a) f ??(a)
2!
(x – a)2 f ???(a)
3!
= + + + -(x – a)3 + …
f(x) = tanx
dx
d tanx = sec2x f ?(x) = sec2x f ??(x)
sec2x z = sec2x d
dx
- secx = secx ? tanx
dz
dx
- 2secx d
dx
= - secx = 2secx( secx ? tanx) = 2sec2x ? tanx
sec2x = tan2x + 1
dz
dx
- = f ??(x) = 2( tan2x + 1) tanx
a = ? ? 4
f a ( ) f ?4
? ?
= ? ?
?4
? ?
? ? tan 1 f ? a ( ) f ? ?4
? ?
? ? = 1 1 + 2 f ?? a ( ) f ?? ?4
? ?
= = = = = ? ? = 2(12 + 1)1 = 4
numerical analysis using matlab and spreadsheets, second edition 6-45
orchard publications
taylor and maclaurin series
(6.130)
we can also obtain a taylor series expansion with the matlab taylor(f,n,a) function where f
is a symbolic expression, n produces the first terms in the series, and a defines the taylor
approximation about point . a detailed description can be displayed with the help taylor command.
for example, the following code will compute the first 8 terms of the taylor series expansion
of about .
x=sym( x ) y=tan(x) z=taylor(y,8,pi/4) pretty(z)
2 3 4
1 + 2x - 1/2 pi + 2(x - 1/4 pi) + 8/3(x - 1/4 pi) + 10/3(x - 1/4 pi)
64 5 244 6 2176 7
+ (x - 1/4 pi) + - (x - 1/4 pi) + (x - 1/4 pi)
15 45 315
example 6.13
express the function
(6.131)
in a maclaurin’s series.
solution:
a maclaurin’s series has the form of (6.132), that is,
(6.132)
for this function, we have and thus . since all derivatives are , then,
and therefore,
(6.133)
matlab displays the same result.
t=sym( t ) fn=taylor(exp(t)) pretty(fn)
2 3 4 5
1 + t + 1/2 t + 1/6 t + 1/24 t + 1/120 t
fn(x) 1 2 x ?
4
? – ?
? ? 2 x ?
4
? – ?
? ?2
= + + + …
n
a
y = f(x) = tanx a = ? ? 4
y = f (t) = et
f (x) f (0) f ?(0)x f ??(0)
2!
x2 … f n ( ) (0)
n!
= + + + + xn
f(t) = et f (0) = 1 et
f ?(0) = f ??(0) = f???(0) = … = 1
fn(t) 1 t t2
2!
t3
3!
= + + + +…
chapter 6 fourier, taylor, and maclaurin series
6-46 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 6.14
in a semiconductor diode , the instantaneous current and voltage are related as
(6.134)
where is the dc (average) component of the current, the constant has a value between
and depending on the material and physical structure of the diode, and is the thermal voltage
which depends on the temperature, and its value at room temperature is approximately .
expand this relation into a power series that can be used to compute the current when the voltage
is small and varies about .
solution:
since the voltage is small and varies about , we can use the following maclaurin’s series.
(6.135)
the first term on the right side of (6.135) is found by letting in (6.134). then,
(6.136)
to compute the second and third terms of (6.135), we must find the first and second derivatives
of (6.134). these are:
(6.137)
(6.138)
then, by substitution of (6.136), (6.137), and (6.138) into (6.135) we get
(6.139)
d id vd
id(vd) ide
vd ? nvt
=
id n 1
2 vt
25 mv
vd = 0
vd = 0
id(vd) id(0) id
? (0)vd
id ??(0)
2!
-vd 2 id
???(0)
3!
-vd 3 = + + + + …
id(0) vd = 0
id(0) = id
i d? (vd) d
dvd
-id
1
nvt
- ide
vd ? nvt
and i d? (0) 1
nvt
= = ? = - ? id
id ??(vd) d 2
d 2vd
id
1
n2vt
2 ide
vd ? nvt
and id ??(0) 1
n2vt
2 = = ? = ? id
id(vd) id 1 1
nvt
-vd
1
n2vt
2 vd 2 + + + …
? ?
? ?
? ?
=
numerical analysis using matlab and spreadsheets, second edition 6-47
orchard publications
summary
6.11 summary
• any periodic waveform can be expressed as
where the first term is a constant, and represents the (average) component of .
the terms with the coefficients and together, represent the fundamental frequency
component . likewise, the terms with the coefficients and together, represent the second
harmonic component , and so on. the coefficients , , and are found from the
following relations:
• if a waveform has odd symmetry, that is, if it is an odd function, the series will consist of sine
terms only. odd functions are those for which .
• if a waveform has even symmetry, that is, if it is an even function, the series will consist of
cosine terms only, and may or may not be zero. even functions are those for which
• a periodic waveform with period , has half-wave symmetry if
that is, the shape of the negative half-cycle of the waveform is the same as that of the positive
half-cycle, but inverted. if a waveform has half-wave symmetry only odd (odd cosine and odd
sine) harmonics will be present. in other words, all even (even cosine and even sine) harmonics
will be zero.
• the trigonometric fourier series for the square waveform with odd symmetry is
• the trigonometric fourier series for the square waveform with even symmetry is
f(t)
f t ( ) 12
a0 (an cosn?t + bn sinn?t)
n = 1
??
= +
a0 ? 2 dc f(t)
a1 b1
? a2 b2
2? a0 an bn
12
a0
1
2?
f(t)dt
0
2?
= ?
an
1?
f(t) cosntdt
0
2?
= ?
bn
1?
f(t) sinntdt
0
2?
= ?
–f(–t) = f(t)
a0
f(–t) = f(t)
t
–f (t + t ? 2) = f(t)
f(t) 4a
?
?t 13
3?t 15
? sin + sin + sin5?t + …?
? ? 4a
?
1n
sinn?t
n = odd
= = ?
chapter 6 fourier, taylor, and maclaurin series
6-48 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the trigonometric fourier series for the sawtooth waveform with odd symmetry is
• the trigonometric fourier series for the triangular waveform with odd symmetry is
• the trigonometric fourier series for the half-wave rectification waveform with no symmetry is
• the trigonometric fourier series for the full-wave rectification waveform with even symmetry is
• the fourier series are often expressed in exponential form as
where the coefficients are related to the trigonometric form coefficients as
• the coefficients, except , are complex, and appear as complex conjugate pairs, that is,
• in general, for ,
f(t) 4a
?
? cos t 13
– 3?t 15
? cos + cos5?t –…?
? ? 4a
?
(–1)
(n – 1)
2
1n
cosn?t
n = odd
= = ?
f(t) 2a
?
?t 12
2?t 13
3?t sin 14
? sin – sin + – sin4?t + …?
? ? 2a
?
(–1)n – 1 ? 1n
= = sinn?t
f(t) 8a
?2 ? sin t 19
– 3?t 1
25
sin5?t 1
49
? sin + – - sin7?t + …?
? ? 8a
?2 (–1)
(n – 1)
2
1
n2 - sinn?t
n = odd
= = ?
f t ( ) a?
- a2
- t sin a?
- cos2t
3
- cos4t
15
- cos6t
35
- cos8t
63
= + – + + + - +…
f(t) 2a
?
4a
?
1
(n2 – 1)
cosn?t
n = 2, 4, 6, …
??
= –
f(t) … c–2e–j2?t c–1e–j?t = + + + c0 + c1e j?t + c2ej2?t + …
ci
c–n
12
an
bn
j
? – -?
? ? 12
= = (an + jbn)
cn
12
an
bn
j
? + -?
? ? 12
= = (an–jbn)
c0
12
= a0
ci c0
c–n cn= ?
? ? 1
numerical analysis using matlab and spreadsheets, second edition 6-49
orchard publications
summary
• we can derive the trigonometric fourier series from the exponential series from the relations
and
• for even functions, all coefficients are real
• for odd functions, all coefficients are imaginary
• if there is half-wave symmetry, for
• always
• a line spectrum is a plot that shows the amplitudes of the harmonics on a frequency scale.
• the frequency components of a recurrent rectangular pulse follow a form.
• we can evaluate the fourier coefficients of a function based on observed values instead of an
analytic expression using numerical evaluations with the aid of a spreadsheet.
• a power series has the form
• a function that possesses all derivatives up to order at a point can be expanded
in a taylor series as
if , the series above reduces to
and this relation is known as maclaurin series
• we can also obtain a taylor series expansion with the matlab taylor(f,n,a) function where
f is a symbolic expression, n produces the first terms in the series, and a defines the taylor
approximation about point .
cn
1t
f(t)e–jn?t d(?t)
0
t
? 1
2?
f(t)e–jn?t d(?t)
0
2?
= = ?
an = cn + c–n
bn = j(cn – c–n)
ci
ci
cn = 0 n = even
c–n cn= ?
sinx ? x
akxk
k = 0
??
= a0 + a1x + a2x2 + …
f (x) n x = x0
f (x) f(x0) f ?(x0)(x – x0)
f ??(x0)
2!
(x – x0)2 …
f (n) (x0)
n!
= + + + + (x – x0)n
x0 = 0
f (x) f (0) f ?(0)x f ??(0)
2!
-x2 …
f (n) (0)
n!
= + + + + xn
n
a
chapter 6 fourier, taylor, and maclaurin series
6-50 numerical analysis using matlab and spreadsheets, second edition
orchard publications
6.12 exercises
1. compute the first 5 components of the trigonometric fourier series for the waveform of figure
6.37. assume .
figure 6.37. waveform for exercise 1
2. compute the first 5 components of the trigonometric fourier series for the waveform of figure
6.38. assume .
figure 6.38. waveform for exercise 2
3. compute the first 5 components of the exponential fourier series for the waveform of figure
6.39. assume .
figure 6.39. waveform for exercise 3
4. compute the first 5 components of the exponential fourier series for the waveform of figure
6.40. assume .
figure 6.40. waveform for exercise 4
? = 1
0 ?t
a
f(t)
? = 1
0
?t
a
f(t)
? = 1
0
?t
a
f(t)
? = 1
0 ?t
f(t)
a ? 2
–a ? 2
numerical analysis using matlab and spreadsheets, second edition 6-51
orchard publications
exercises
5. compute the first 5 components of the exponential fourier series for the waveform of figure
6.41. assume .
figure 6.41. waveform for exercise 5
6. compute the first 5 components of the exponential fourier series for the waveform of figure
6.42. assume .
figure 6.42. waveform for exercise 6
7. compute the first 4 terms of the maclaurin series for each of the following functions.
a. b. c.
confirm your answers with matlab.
8. compute the first 4 terms of the taylor series for each of the following functions.
a. b.
confirm your answers with matlab.
9. in a non-linear device, the voltage and current are related as
where is a constant and is the dc component of the instantaneous voltage . expand
this function into a power series that can be used to compute the current , when the voltage
is small, and varies about .
? = 1
0
?t
a
f(t)
? = 1
0 ?t
a
?a
f(t)
f (x) e–x = f (x) = sinx f (x) = sinhx
f x ( ) 1x
about a 1 – = = f x ( ) x sin about a ?4
= = –
i v ( ) k 1 vv
? + - ?
? ?1.5
=
k v v
i
v v = 0
chapter 6 fourier, taylor, and maclaurin series
6-52 numerical analysis using matlab and spreadsheets, second edition
orchard publications
6.13 solutions to exercises
1.
this is an even function therefore, the series consists of cosine terms only. there is no halfwave
symmetry and the average ( component) is not zero. we will integrate from to
and multiply by . then,
(1)
from tables of integrals,
and thus (1) becomes
and since for all integer ,
(2)
we cannot evaluate the average from (2) we must use (1). then, for ,
or
we observe from (2) that for , . then,
and so on.
0 ?t
a f t ( ) a?
-t
–2? –? ? 2?
dc 0 ?
2
an
2?
a?
-t cosntdt
0
?
? 2a
?2 t cosntdt
0
?
= = ?
?x cosaxdx 1
a2 - ax cos xa
= + sinax
an
2a
?2 1
n2 - nt cos tn
? + sinnt ?
? ?
0
? 2a
?2 1
n2 - n? cos tn
nt? 1
n2 ? + sin – - – 0 ?
= = ? ?
sinnt? = 0 n
an
2a
?2 1
n2 - cosn? 1
n2 ? – - ?
? ? 2a
n2?2 = = -( cosn? – 1)
(1 ? 2) ? a0 n = 0
12
a0
2a
2?2 tdt
0
?
? a
?2 - t2
2
? -
0
?
a
?2 - ?2
2
= = = ? -
(1 ? 2) ? a0 = a ? 2
n = even an = even = 0
forn 1 a1
4a
?2 , = – , for n 3 a3
–4a
32?2 - forn 5 a5
4a
52?2 , = – -, for n 7 a3
–4a
72?2 = = , = , = = , = -
numerical analysis using matlab and spreadsheets, second edition 6-53
orchard publications
solutions to exercises
therefore,
2.
this is an even function therefore, the series consists of cosine terms only. there is no halfwave
symmetry and the average ( component) is not zero.
(1)
and with
(1) simplifies to
and since for all integer ,
we observe that the fourth harmonic and all its multiples are zero. therefore,
f t ( ) 12
a0
4a
?2 t cos 19
cos3t 1
25
- cos5t 1
49
? + + + - cos7t + …?
? ? – a2
- 4a
?
1
n2 - cosnt
n = odd
??
= = –
0
?t
a
f(t) 2a
?
t
? ? 2 ? 3? ? 2 ?
dc
average 12
a0
area
period
- 2 × [(a ? 2) ? (? ? 2)] + a?
2?
3a ? (? ? 2)
2?
3a
4
= = = = =
an
2?
2a
?
t cosntdt
0
? ? 2
? 2?
acosntdt
? ? 2
?
= + ?
?x cosaxdx 1
a2 - ax cos xa
+ sinax 1
a2 = = -( cosax + ax sinax)
an
4a
?2 1
n2 -( cosnt + nt sinnt)
0
? ? 2 2a
n?
nt ? ? 2
? = + sin
4a
n2?2 - n?
2
cos n?
2
n?
2
? + sin – 1 – 0?
? ? 2a
n?
sinn? n?
2
? – sin ?
= + ? ?
sinnt? = 0 n
an
4a
n2?2 - n?
2
cos 2a
n?
n?
2
sin 4a
n2?2 – - 2a
n?
n?
2
+ – sin 4a
n2?2 - n?
2
? cos – 1?
= = ? ?
forn 1 a1
4a
?2 (0 – 1) 4a
?2 , = = – , for n 2 a2
4a
4?2 (– 1 – 1) 2a
?2 = = , = = –
forn 3 a3
4a
9?2 (0 – 1) 4a
9?2 , = = – , for n 4 a4
–4a
72?2 = = , = -(1 – 1) = 0
chapter 6 fourier, taylor, and maclaurin series
6-54 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.
this is neither an even nor an odd function and has no half-wave symmetry therefore, the
series consists of both cosine and sine terms. the average ( component) is not zero. then,
and with
the value is
for
recalling that
for , and for , . then,
and
by substitution into the expression
we find that
f(t) 3a
4
4a
?2 t cos 12
2t cos 19
? + + cos3t + …?
= – ? ?
0
?t
a
f(t)
? 2?
dc
cn
1
2?
f(t)e–jn?t d(?t)
0
2?
= ?
? = 1
cn
1
2?
f(t)e–jnt dt
0
2?
? 1
2?
ae–jnt dt
0
?
? 0e–jnt dt
?
2?
+ ? a
2?
e–jnt dt
0
?
= = = ?
dc
c0
a
2?
e0dt
0
?
? a
2?
t
0
? a2
= = = -
n ? 0
cn
a
2?
e–jnt dt
0
?
? a
–j2n?
e–jnt
0
?
= a
j2n?
- 1 e–jn? = = ( – )
e–jn? = cosn? – j sinn?
n = even e–jn? = 1 n = odd e–jn? = –1
cn = even
a
j2n?
= -(1 – 1) = 0
cn = odd
a
j2n?
-[1 – (–1)] a
jn?
= =
f(t) … c–2e–j2?t c–1e–j?t = + + + c0 + c1e j?t + c2ej2?t + …
numerical analysis using matlab and spreadsheets, second edition 6-55
orchard publications
solutions to exercises
the minus (?) sign of the first two terms within the parentheses results from the fact that
. for instance, since , it follows that . we
observe that is complex, as expected, since there is no symmetry.
4.
this is the same waveform as in exercise 3 where the component has been removed.
then,
it is also the same waveform as in example 6.9 except that the amplitude is halved. this waveform
is an odd function and thus the expression for is imaginary.
5.
this is the same waveform as in exercise 3 where the vertical axis has been shifted to make
the waveform an even function. therefore, for this waveform is real. then,
the value is
for
f t ( ) a2
- a
j?
… 13
e–j3?t e–j?t – – ej?t 13
? + + ej3?t + …?
= + ? ?
c–n cn= ? c1 = 2a ? j? c–1 c1= ? = –2a ? j?
f(t)
0 ?t
f(t)
a ? 2
–a ? 2
dc
f(t) a
j?
… 13
e–j3?t e–j?t – – ej?t 13
? + + ej3?t + …?
= ? ?
f(t)
0 ?t
a
f(t)
?
–? ? 2 ? ? 2
–?
cn
cn
1
2?
f(t)e–jnt dt
–?
?
? a
2?
e–jnt dt
–? ? 2
? ? 2
= = ?
dc
c0
a
2?
t
–? ? 2
? ? 2 a
2?
?2
?2
? + ?
? ? a2
= = = -
n ? 0
chapter 6 fourier, taylor, and maclaurin series
6-56 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and we observe that for ,
for , alternates in plus (+) and minus (?) signs, that is,
thus,
where the plus (+) sign is used with and the minus (?) sign is used with
. we can express in a more compact form as
6.
we will find the exponential form coefficients from
from tables of integrals
then,
cn
a
2?
e–jnt dt
–? ? 2
? ? 2
? a
–j2n?
e–jnt
–? ? 2
? ? 2
= a
–j2n?
e–jn? ? 2 e jn? ? 2 = = ( – )
a
j2n?
- e jn? ? 2 e–jn? ? 2 ( – ) a
n?
e jn? 2 ? e–jn? ? 2 –
j2
? ?
? ? a
n?
n?
2
= = = sin
n = even cn = 0
n = odd cn
cn
a
n?
= if n = 1, 5, 9, …
cn
a
n?
= – if n = 3, 7, 11, …
f t ( ) a2
- a
n?
?± e jn?t?
? ?
n = odd
= + ?
n = 1, 5, 9, …
n = 3, 7, 11, … f(t)
f t ( ) a2
- (–1)(n – 1) ? 2 a
n?
e jn?t
n = odd
= + ?
0 ?t
a
?a
f(t) 2a
?
t – 1
–? ?
–? ? 2 ? ? 2
cn
cn
1
2?
f(t)e–jnt dt
–?
?
= ?
?xeaxdx eax
a2 = -(ax – 1)
numerical analysis using matlab and spreadsheets, second edition 6-57
orchard publications
solutions to exercises
integrating and rearranging terms we get
and since for all integer ,
for , and for , , and
also, by inspection, the component . then,
the coefficients of the terms and are positive because all coefficients of are
real. this is to be expected since is an even function. it also has half-wave symmetry and
thus for as we’ve found.
7.
a. , , , , , ,
, , and so on. therefore,
matlab displays the same result.
x=sym( x ) fn=taylor(exp(?x)) pretty(fn)
2 3 4 5
1 - x + 1/2 x - 1/6 x + 1/24 x - 1/120 x
cn
1
2?
2a
?
?– t – 1?
? ? e–jnt dt
–?
0
? 2a
?
? t – 1?
? ? e–jnt dt
0
?
= + ?
cn
1
2?
4a
n2?
– 4a
n2?
n? e jn? e–jn? –
j2
? - e jn? e–jn? +
2
? + ?
? ? 2a
n
e jn? e–jn? –
j2
= + – ? -
4a
2n2?2 – 1 n?sinn? cosn? n?
2
? + + – sinn??
= ? ?
sinn? = 0 n
cn
2a
n2?2 = -( cosn? – 1)
n = even cn = 0 n = odd cosn? = –1 cn
–4a
n2?2 = -
dc c0 = 0
f(t) 4a
?2 – … 19
e–j3?t e–j?t ej?t 19
? + + + + ej3?t +…?
= ? ?
e–j3?t e–j?t cn
f(t)
cn = 0 n = even
f (x) f (0) f ?(0)x f ??(0)
2!
-x2 …
f (n) (0)
n!
= + + + + xn
f (x) e–x = f (0) = 1 f ?(x) e–x = – f ?(0) = –1 f ??(x) e–x = f ??(0) = 1
f ???(x) e–x = – f ???(0) = –1
fn(x) 1 – x x2
2!
x3
3!
= + – + …
chapter 6 fourier, taylor, and maclaurin series
6-58 numerical analysis using matlab and spreadsheets, second edition
orchard publications
b. , , , , , ,
, , and so on. therefore,
matlab displays the same result.
x=sym( x ) fn=taylor(sin(x)) pretty(fn)
3 5
x - 1/6 x + 1/120 x
c. , , , , , ,
, , and so on. therefore,
matlab displays the same result.
x=sym( x ) fn=taylor(sinh(x)) pretty(fn)
3 5
x + 1/6 x + 1/120 x
8.
a. , , , , ,
, , , and so on. therefore,
or
matlab displays the same result.
x=sym( x ) y=1/x z=taylor(y,4,?1) pretty(z)
2 3
-2 - x - (x + 1) - (x + 1)
f (x) = sinx f (0) = 0 f ?(x) = cosx f ?(0) = 1 f ??(x) = –sinx f ??(0) = 0
f ???(x) = –cosx f ???(0) = –1
fn(x) x x3
3!
x5
5!
x7
7!
= – + – + …
f (x) = sinhx f (0) = 0 f ?(x) = coshx f ?(0) = 1 f ??(x) = sinhx f ??(0) = 0
f ???(x) = coshx f ???(0) = 1
fn(x) x x3
3!
x5
5!
x7
7!
= + + + +…
fn(x) f (a) f ?(a)(x – a) f ??(a)
2!
(x – a)2 f ???(a)
3!
= + + + -(x – a)3 + …
f (x) = 1 ? x f (a) = f(–1) = –1 f ?(x) = –1 ? x2 f ?(a) = f ?(–1) = –1 f ??(x) = 2 ? x3
f ??(a) = f ??(–1) = –2 f ???(x) = –6 ? x4 f ???(a) = f ???(–1) = –6
fn(x) = –1–(x + 1)– (x + 1)2 – (x + 1)3 + …
fn(x) = –2–x– (x + 1)2 – (x + 1)3 + …
numerical analysis using matlab and spreadsheets, second edition 6-59
orchard publications
solutions to exercises
b. , , , ,
, , ,
, and so on. therefore,
matlab displays the same result.
x=sym( x ) y=sin(x) z=taylor(y,4,?pi/4) pretty(z)
1/2 1/2 1/2 2
- 1/2 2 + 1/2 2 (x + 1/4 pi) + 1/4 2 (x + 1/4 pi)
1/2 3
- 1/12 2 (x + 1/4 pi)
9.
the taylor series for this relation is
since the voltage is small, and varies about , we expand this relation about and
the series reduces to the maclaurin series below.
(1)
by substitution of into the given relation we get
the first and second derivatives of are
and by substitution into (1)
f (x) = sinx f (a) = f(–? ? 4) = – 2 ? 2 f ?(x) = cosx f ?(a) = f ?(–? ? 4) = 2 ? 2
f ??(x) = –sinx f ??(a) = f ??(–? ? 4) = 2 ? 2 f ???(x) = –cosx
f ???(a) = f ???(–? ? 4) = – 2 ? 2
fn(x) – 2 ? 2 + ( 2 ? 2)(x + ? ? 4) + ( 2 ? 4)(x + ? ? 4)2 ( 2 ? 12)(x + ? ? 4)3
= – + …
i v ( ) k 1 vv
? + - ?
? ?1.5
=
i(v) i(v0) i ?(v0)(v – v0)
i ??(v0)
2!
-(v – v0)2 i ???(v0)
3!
= + + + -(v – v0)3 + …
v v = 0 v = 0
i(v) i(0) i ?(0)v i ??(0)
2!
= + + -v2 + …
v = 0
i(0) = k
i
i ?(v) 3k
2v
1 vv
? + -?
? ? 1 ? 2
= i ?(0) 3k
2v
=
i ??(v) 3k
4v2 - 1 vv
? + -?
? ? –1 ? 2
= i ??(0) 3k
4v2 = -
i(v) k 3k
2v
v 3k
8v2 + + -v2 +… k 1 3
2v
v 3
8v2 ? + + -v2 +…?
= = ? ?
chapter 6 fourier, taylor, and maclaurin series
6-60 numerical analysis using matlab and spreadsheets, second edition
orchard publications
matlab displays the same result.
x=sym( x ) i=sym(‘i’) v=sym(‘v’) k=sym(‘k’) v=sym(‘v’) ...
i=k*(1+v/v)^1.5 z=taylor(i,4,0) pretty(z)
2 3
k v k v k v
k + 3/2 - + 3/8 - 1/16
v 2 3
v v
numerical analysis using matlab and spreadsheets, second edition 7-1
orchard publications
chapter 7
finite differences and interpolation
his chapter begins with finite differences and interpolation which is one of its most important
applications. finite differences form the basis of numerical analysis as applied to other
numerical methods such as curve fitting, data smoothing, numerical differentiation, and
numerical integration. we will discuss these applications in this and the next three chapters.
7.1 divided differences
consider the continuous function and let be some values of
in the interval . it is customary to show the independent variable , and its corresponding
values of in tabular form as in table 7.1.
let and be any two, not necessarily consecutive values of , within this interval. then, the
first divided difference is defined as:
(7.1)
likewise, the second divided difference is defined as:
(7.2)
table 7.1 the variable x and in tabular form
x
… …
t
y = f (x) x0 x1 x2 … xn – 1 , , , , , xn x
x0 ? x ? xn x
y = f(x)
y = f (x)
f (x)
x0 f (x0)
x1 f (x1)
x2 f (x2)
xn – 1 f xn – 1 ( )
xn f (xn)
xi xj x
f(xi, xj)
f(xi) – f(xj)
xi – xj
=
f(xi, xj, xk)
f(xi, xj) – f(xj, xk)
xi – xk
= -
chapter 7 finite differences and interpolation
7-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the third, fourth, and so on divided differences, are defined similarly.
the divided differences are indicated in a difference table where each difference is placed
between the values of the column immediately to the left of it as shown in table 7.2.
example 7.1
form a difference table showing the values of given as , the values of
corresponding to , and the first through the fourth divided differences.
solution:
we construct table 7.3 with six columns. the first column contains the given values of , the
second the values of , and the third through the sixth contain the values of the first through
the fourth divided differences. these differences are computed from (7.1), (7.2), and other relations
for higher order divided differences. for instance, the second value on the first divided difference
is found from (7.1) as
and third value on the second divided difference is found from (7.2) as
likewise, for the third divided difference we have
table 7.2 conventional presentation of divided differences
x f (x)
x0 f (x0)
f (x0, x1)
x1 f (x1) f (x0, x1, x2)
f (x1, x2) f (x0, x1, x2, x3)
x2 f (x2) f (x1, x2, x3)
f (x2, x3)
x3 f (x3)
x 0, 1, 2, 3, 4, 7, and 9
f (x) y = f (x) = x3
x
f (x)
1 – 27
1 – 3
= 13
37 – 93
3 – 7
- = 14
numerical analysis using matlab and spreadsheets, second edition 7-3
orchard publications
divided differences
and for the fourth
we observe that, if the values of the divided difference are the same, as in the fifth column
(third divided differences for this example), all subsequent differences will be equal to zero.
in most cases, the values of in a table are equally spaced. in this case, the differences are sets of
consecutive values. moreover, the denominators are all the same therefore, they can be omitted.
these values are referred to as just the differences of the function.
if the constant difference between successive values of is , the typical value of is
(7.3)
we can now express the first differences in terms of the difference operator as
(7.4)
likewise, the second differences are
(7.5)
and, in general, for positive integer values of
table 7.3 divided differences for example 7.1
function divided differences
x first second third fourth
0 0
1
1 1 4
13 1
3 27 8 0
37 1
4 64 14 0
93 1
7 343 20
193
9 729
f(x) = x3
4 – 8
0 – 4
- = 1
1 – 1
0 – 4
- = 0
nth
x
x h xk
xk = x0 + kh for k = …,–2, –1, 0, 1, 2,…
?
?fk = fk + 1 – fk
?2fk ?(?fk) ?fk + 1 = = – ?fk
n
chapter 7 finite differences and interpolation
7-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.6)
the difference operator obeys the law of exponents, that is,
(7.7)
we construct the difference table in terms of the difference operator as shown in table 7.4.
example 7.2
construct a difference table showing the values of x given as and , the values of
corresponding to , and the first through the fourth differences.
solution:
following the same procedure as in the previous example, we construct table 7.5.
table 7.4 divided differences table in terms of the difference operator
function differences
x f first second third fourth
?nfk ? ?n – 1( fk) ?n – 1fk + 1 ?n – 1= = – fk
?
?m(?nfk) ?m + n= fk
?
?

x0 f0
?f0
x1 f1 ?2f0
?f1 ?3f0
x2 f2 ?2f1 ?4f0
?f2 ?3f1
x3 f3 ?2f2
?f3
x4 f4

xn fn
1, 2, 3, 4, 5, 6, 7 8
f (x) y = f(x) = x3
numerical analysis using matlab and spreadsheets, second edition 7-5
orchard publications
divided differences
we observe that the fourth differences are zero, as expected.
using the binomial expansion
(7.8)
we can show that
(7.9)
for , and , relation (7.9) reduces to
(7.10)
it is interesting to observe that the first difference in (7.10), is the difference quotient whose limit
defines the derivative of a continuous function that is defined as
table 7.5 difference table for example 7.2
function differences
k xk fk
1 1 1
7
2 2 8 12
19 6
3 3 27 18 0
37 6
4 4 64 24 0
61 6
5 5 125 30 0
91 6
6 6 216 36 0
127 6
7 7 343 42
169
8 8 512
?fk ?2fk ?3fk ?4fk …
?4fk
nj
? ?
? ? n!
j!(n – j)!
= -
?nfk fk + n – nfk + n – 1 n(n – 1)
2!
-fk + n – 2 … (–1)n – 1nfk + 1 = + + + + (–1)nfk
k = 0 n = 1, 2, 3 4
?f0 = f2 – f1
?2f0 = f2 – 2f1 + f0
?3f0 = f3 – 3f2 + 3f1 – f0
?4f0 = f4 – 4f3 + 6f2–4f1 + f0
chapter 7 finite differences and interpolation
7-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.11)
as with derivatives, the differences of a polynomial of degree are constant.
7.2 factorial polynomials
the factorial polynomials are defined as
(7.12)
and
(7.13)
these expressions resemble the power functions and in elementary algebra.
using the difference operator with (7.12) and (7.13) we get
(7.14)
and
(7.15)
we observe that (7.14) and (7.15) are very similar to differentiation of and .
occasionally, it is desirable to express a polynomial as a factorial polynomial. then, in analogy
with maclaurin power series, we can express that polynomial as
(7.16)
and now our task is to compute the coefficients .
for , relation (7.16) reduces to
(7.17)
to compute the coefficient , we take the first difference of in (7.16). using (7.14) we get
(7.18)
and letting , we find that
(7.19)
differencing again we get
?y
?x

?x ? 0
lim
f(x1 + ?x) – f(x1)
?x

?x ? 0
= lim
nth n
(x)(n) = x(x – 1)(x – 2)…(x – n + 1)
(x)–(n) 1
(x – 1)(x – 2)…(x + n)
=
xn x–n
?
?(x)(n) n(x)(n – 1) =
?(x)–(n) –n(x)–(n – 1) =
xn x–n
pn(x)
pn(x) a0 a1(x)(1) a2(x)(2) … an(x)(n)
= + + + +
ak
x = 0
a0 = pn(0)
a1 pn(x)
?pn(x) 1x0a1 2a2(x)(1) 3a3(x)(2) … nan(x)(n – 1) = + + + +
x = 0
a1 = ?pn(0)
numerical analysis using matlab and spreadsheets, second edition 7-7
orchard publications
factorial polynomials
(7.20)
and for ,
(7.21)
in general,
(7.22)
factorial polynomials provide an easier method of constructing a difference table. with this
method we perform the following steps:
1. we divide in (7.16) by to get a quotient and a remainder which turns out to
be the constant term . then, we express (7.16) as
(7.23)
2. we divide in (7.23) by to get a quotient and a remainder which turns
out to be the constant term . then,
(7.24)
by substitution of (7.24) into (7.23), and using the form of relation (7.16), we get
(7.25)
3. we divide in (7.25) by to get a quotient and a remainder which turns
out to be the constant term , and thus
(7.26)
by substitution of (7.26) into (7.25), we get
(7.27)
continuing with the above procedure, we get a new quotient whose degree is one less than preceding
quotient and therefore, the process of finding new quotients and remainders terminates
after steps.
the general form of a factorial polynomial is
?2pn x ( ) 2 1a2
? 3 ? 2a3(x)(1) … n(n – 1)an(x)(n – 2) = + + +
x = 0
a2
?2pn(0)
2 ? 1

?2pn(0)
2!
= =
aj
?jpn(0)
j!
= for j = 0, 1, 2, …, n
pn(x) x q0(x) r0
a0
pn(x) = r0 + xq0(x)
q0(x) (x – 1) q1(x) r1
a1
q0(x) = r1 + (x – 1)q1(x)
pn(x) r0 + x[r1 + (x – 1)q1(x)] r0 r1(x)(1) = = + + x(x – 1)q1(x)
q1(x) (x – 2) q2(x) r2
a2
q1(x) = r2 + (x – 2)q2(x)
pn(x) r0 r1(x)(1) = + + x(x – 1)[r2 + (x – 2)q2(x)]
r0 r1(x)(1) + r2(x)(2) = + + x(x – 1)(x – 2)q2(x)
(n + 1)
chapter 7 finite differences and interpolation
7-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.28)
and from (7.16) and (7.22),
(7.29)
or
(7.30)
example 7.3
express the algebraic polynomial
(7.31)
as a factorial polynomial. then, construct the difference table with .
solution:
since the highest power of the given polynomial is , we must evaluate the remainders
and then, we will use (7.28) to determine . we can compute the remainders
by long division, but for convenience, we will use the matlab deconv(p,q) function which
divides the polynomial p by q.
the matlab code is as follows:
px=[1 ?5 0 3 4] % coefficients of given polynomial
d0=[1 0] % coefficients of first divisor, i.e, x
[q0,r0]=deconv(px,d0) % computation of first quotient and remainder
d1=[1 ?1] % coefficients of second divisor, i.e, x?1
[q1,r1]=deconv(q0,d1) % computation of second quotient and remainder
d2=[1 ?2] % coefficients of third divisor, i.e, x?2
[q2,r2]=deconv(q1,d2) % computation of third quotient and remainder
d3=[1 ?3] % coefficients of fourth divisor, i.e, x?3
[q3,r3]=deconv(q2,d3) % computation of fourth quotient and remainder
d4=[1 ?4] % coefficients of fifth (last) divisor, i.e, x?4
[q4,r4]=deconv(q3,d4) % computation of fifth (last) quotient and remainder
q0 =
1 -5 0 3
r0 =
0 0 0 0 4
q1 =
1 -4 -4
r1 =
pn(x) r0 r1(x)(1) + r2(x)(2) … rn – 1 (x)(n – 1) rn(x)(n)
= + + + +
rj aj
?jpn(0)
j!
= =
?jpn(0) = j!rj
p(x) = x4 – 5x3 + 3x + 4
h = 1
p(x) 4
r0, r1, r2, r3 r4 pn(x)
numerical analysis using matlab and spreadsheets, second edition 7-9
orchard publications
factorial polynomials
0 0 0 -1
q2 =
1 -2
r2 =
0 0 -8
q3 =
1
r3 =
0 1
q4 =
0
r4 =
1
therefore, with reference to (7.28), the factorial polynomial is
(7.32)
we can verify that (7.32) is the same polynomial as (7.31), by expansion of the factorials using
(7.12). this can be easily done with the matlab collect(‘s_expr’) function, where ‘s_expr’ is
a symbolic expression. for this example, the matlab code is
syms x px=collect((x*(x?1)*(x?2)*(x?3))+(x*(x?1)*(x?2))?(8*x*(x?1))?x+4)
px =
x^4-5*x^3+3*x+4
we observe that this is the same algebraic polynomial as in (7.31).
we will now compute the leading entries for the difference table using (7.30) and (7.32). then,
(7.33)
1. we enter the values of (7.33) in the appropriate spaces as shown in table 7.6.
2. we obtain the next set of values by crisscross addition as shown in table 7.7.
3. the second crisscross addition extends the difference table as shown in table 7.8.
pn(x) 4 (x)(1) – 8(x)(2) – (x)(3) (x)(4) = + +
?0p(0) = 0! ? 4 = 4
?1p(0) = 1! ? (–1) = –1
?2p(0) = 2! ? (–8) = –16
?3p(0) = 3! ? 1 = 6
?4p(0) = 4! ? 1 = 24
?5p(0) = 5! ? 0 = 0
chapter 7 finite differences and interpolation
7-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
table 7.6 leading entries of (7.33) in table form
4
?1
?16
6
24
0
table 7.7 crisscross addition to find second set of values
4
?1
3 ?16
?17 6
?10 24
30 0
24
table 7.8 second crisscross addition to find third set of values
x p(x)
4
?1
3 ?16
?17 6
?14 ?10 24
?27 30 0
20 24
54
x p x ( ) ? ?2 ?3 ?4 ?5
x p x ( ) ? ?2 ?3 ?4 ?5
? ?2 ?3 ?4 ?5
numerical analysis using matlab and spreadsheets, second edition 7-11
orchard publications
antidifferences
4. continuation of this procedure produces the complete difference table. this is shown in table
7.9.
7.3 antidifferences
we recall from elementary calculus that when we know the first derivative of a function, we can
integrate or antidifferentiate to find the function. by a similar method, we can find the antidifference
of a factorial polynomial. we denote the antidifference as . it is computed from
(7.34)
example 7.4
compute the antidifference of the algebraic polynomial
(7.35)
solution:
this is the same algebraic polynomial as that of the previous example, where we found that the
corresponding factorial polynomial is
table 7.9 complete difference table for example 7.3
x p(x)
4
?1
3 ?16
?17 6
?14 ?10 24
?27 30 0
?41 20 24
?7 54
?48 74
67
19
? ?2 ?3 ?4 ?5
?–1pn(x)
?–1 (x)(n) (x) (n + 1)
(n + 1)
=
p(x) = x4 – 5x3 + 3x + 4
chapter 7 finite differences and interpolation
7-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.36)
then, by (7.34), its antidifference is
(7.37)
where c is an arbitrary constant.
antidifferences are very useful in finding sums of series. before we present an example, we need
to review the definite sum and the fundamental theorem of sum calculus. these are discussed
below.
in analogy with definite integrals for continuous functions, in finite differences we have the definite
sum of which for the interval is denoted as
(7.38)
also, in analogy with the fundamental theorem of integral calculus which states that
(7.39)
we have the fundamental theorem of sum calculus which states that
(7.40)
example 7.5
derive a simple expression, in closed form, that computes the sum of the cubes of the first odd
integers.
solution:
an odd number can be expressed as , and thus its cube is . to use (7.40), we
must express this term as a factorial polynomial. recalling from (7.12) that
(7.41)
and using the matlab expand(f) function where f is a symbolic expression, we execute
syms m f = (2*m?1)^3 expand(f)
and we get
pn(x) 4 (x)(1) – 8(x)(2) – (x)(3) (x)(4) = + +
?–1pn(x) (x) (5)
5
- (x) (4)
4
- 8(x) (3)
3
– - (x) (2)
2
– - 4(x)(1) = + + + c
pn(x) a ? x ? a + (n – 1)h
pn(x)
x = ?
? + (n – 1)h
? = pn(?) + pn(? + h) + pn(? + 2h) + … + pn[? + (n – 1)h]
f(x)dx
a
b
? = f(b) – f(a)
pn(x)
x = ?
? + (n – 1)h
? ?–1pn(x)
?
? + nh =
n
2m – 1 (2m – 1)3
(x)(n) = x(x – 1)(x – 2)…(x – n + 1)
numerical analysis using matlab and spreadsheets, second edition 7-13
orchard publications
antidifferences
ans =
8*m^3-12*m^2+6*m-1
thus
(7.42)
following the procedure of example 7.3, we find with matlab as
pm=[8 ?12 6 ?1]
d0=[1 0]
[q0,r0]=deconv(pm,d0)
d1=[1 ?1]
[q1,r1]=deconv(q0,d1)
d2=[1 ?2]
[q2,r2]=deconv(q1,d2)
d3=[1 ?3]
[q3,r3]=deconv(q2,d3)
q0 =
8 -12 6
r0 =
0 0 0 -1
q1 =
8 -4
r1 =
0 0 2
q2 =
8
r2 =
0 12
q3 =
0
r3 =
8
therefore,
(7.43)
taking the antidifference of (7.43) we get
(7.44)
p(m)= (2m – 1)3 = 8m3 – 12m2 + 6m – 1
pn(m)
pn(m) 8(m)(3) 12(m)(2) 2(m)(1) = + + – 1
?–1pn(m) 8(m) (4)
4
12(m) (3)
3
- 2(m) (2)
2
(m)(1) = + + –
2(m)(4) 4(m)(3) (m)(2) (m)(1) = + + –
chapter 7 finite differences and interpolation
7-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and with (7.40)
(7.45)
since
(7.46)
relation (7.45) reduces to
(7.47)
and this can be simplified with the matlab collect(f) function as follows.
syms n sum=collect(2*(n+1)*n*(n?1)*(n-2)+4*(n+1)*n*(n?1)+(n+1)*n?(n+1)+1)
sum =
2*n^4-n^2
that is,
(7.48)
we can verify that this is the correct expression by considering the first odd integers
. the sum of their cubes is
this is verified with (7.48) since
one important application of finite differences is interpolation. we will discuss the newton’s
divided-difference interpolation method, lagrange’s interpolation method, gregory-newton forward,
and gregory-newton backward interpolation methods. we will use spreadsheets to facilitate
the computations. we will also discuss interpolation using matlab.
?cubes 2(m)(4) 4(m)(3) (m)(2) (m)(1)
+ + – m = 1
n + 1
=
= 2(n + 1)n(n – 1)(n – 2) + 4(n + 1)n(n – 1) + (n + 1)n – (n + 1)
2(1)(4)
– –4(1) 3 ( ) (1)(2)
– (1) 1 ( )
+
(1)(4) = 1(1 – 1)(1 – 2)(1 – 3) = 0
(1)(3) = 1(1 – 1)(1 – 2) = 0
(1)(2) = 1(1 – 1) = 0
(1)(1) = 1
?cubes = 2(n + 1)n(n – 1)(n – 2) + 4(n + 1)n(n – 1) + (n + 1)n – (n + 1) + 1
?cubes = 2n4 – n2 = n2(2n2 – 1)
4
1, 3, 5, and 7
1 + 27 + 125 + 343 = 496
n2(2n2 – 1) = 42(2 ? 42 – 1) = 16 ? 31 = 496
numerical analysis using matlab and spreadsheets, second edition 7-15
orchard publications
newton’s divided difference interpolation method
7.4 newton’s divided difference interpolation method
this method, has the advantage that the values need not be equally spaced, or
taken in consecutive order. it uses the formula
(7.49)
where , , and are the first, second, and third divided differences
respectively.
example 7.6
use newton’s divided-difference method to compute from the experimental data shown in
table 7.10.
solution:
we must compute the first, second, and third divided differences as required by (7.49).
the first divided differences are:
(7.50)
the second divided differences are:
table 7.10 data for example 7.6
x ?1.0 0.0 0.5 1.0 2.5 3.0
3.0 ?2.0 -0.375 3.0 16.125 19.0
x0, x1, x2,…, xn
f(x) = f(x0) + (x – x0) f (x0, x1) + (x – x0)(x – x1) f (x0, x1, x2)
+ (x – x0)(x – x1)(x – x2) f (x0, x1, x2, x3)
f(x0, x1) f(x0, x1, x2) f(x0, x1, x2, x3)
f (2)
y = f (x)
– 2.000 – 3.000
0 – (–1.0)
- = –5.000
– 0.375 – (–2.000)
0.5 – 0.0
- = 3.250
3.000 – (–0.375)
1.0 – 0.5
= 6.750
16.125 – 3.000
2.5 – 1.0
- = 8.750
19.000 – 16.125
3.0 – 2.5
= 5.750
chapter 7 finite differences and interpolation
7-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.51)
and the third divided differences are:
(7.52)
with these values, we construct the difference table 7.11.
now, we have all the data that we need to find . we start with ,* and for in
(7.49), we use . then,
table 7.11 difference table for example 7.6
1st divided difference 2nd divided difference 3rd divided difference
x
?1.0 3.000
?5.000
0.0 ?2.000 5.500
3.250 ?1.000
0.5 ?0.375 3.500
6.750 ?1.000
1.0 3.000 1.000
8.750 ?1.000
2.5 16.125 ?1.500
5.750
3.0 19.000
* we chose this as our starting value so that will be between and
3.250 – (–5.000)
0.5 – (–1.0)
= 5.500
6.750 – 3.250
1.0 – 0.0
= 3.500
8.750 – 6.750
2.5 – 0.5
= 1.000
5.750 – 8.750
3.0 – 1.0
= –1.500
3.500 – 5.500
1.0 – (–1.0)
= –1.000
1.000 – 3.500
2.5 – 0.0
= –1.000
– 1.500 – 1.000
3.0 – 0.5
- = –1.000
f (x) f (x0, x1) f (x0, x1, x2) f (x0, x1, x2, x3)
f (2) x0 = 0.00 x
f (2) f (1) f (2.5)
x = 2
numerical analysis using matlab and spreadsheets, second edition 7-17
orchard publications
newton’s divided difference interpolation method
this, and other interpolation problems, can also be solved with a spreadsheet. the excel spreadsheet
for this example is shown in figure 7.1.
figure 7.1. spreadsheet for example 7.6
f(2) = – 2.0 + (2 – 0)(3.250) + (2 – 0)(2 – 0.5)(3.500) + (2 – 0)(2 – 0.5)(2 – 1)(–1.000)
= – 2.0 + 6.5 + 10.5 – 3
= 12
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
a b c d e f
interpolation with newton s divided difference formula
f(x) = f(x0)+(x-x0)f(x0,x1)+(x-x0)(x-x1)f(x0,x1,x2)+(x-x0)(x-x1)(x-x2)f(x0,x1,x2,x3)
in this example, we want to evaluate f(x) at x= 2
1st divided 2nd divided 3rd divided
difference difference difference
x f(x) f(x0, x1) f(x0, x1, x2) f(x0 ,x1,x2,x3)
-1.00 3.000
-5.000
0.00 -2.000 5.500
3.250 -1.000
0.50 -0.375 3.500
6.750 -1.000
1.00 3.000 1.000
8.750 -1.000
2.50 16.125 -1.500
5.750
3.00 19.000
we use the above formula with starting value x0 = 0.00
f(2)=b12+(e3-e18)*c13+(e3-e18)*(e3-a14)*d14+(e3-e18)*(e3-a14)*(e3-a16)*e15
or f(2)= 12.00
the plot below verifies that our answer is correct
-1.000 3.000
0.000 -2.000
0.500 -0.375
1.000 3.000
2.500 16.125
3.000 19.000
-5
0
5
10
15
20
-1.0 0.0 1.0 2.0 3.0
x
f(x)
chapter 7 finite differences and interpolation
7-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
7.5 lagrange’s interpolation method
this method uses the formula
(7.53)
and, like newton’s divided difference method, has the advantage that the values
need not be equally spaced or taken in consecutive order.
example 7.7
repeat example 7.6 using lagrange’s interpolation formula.
solution:
all computations appear in the spreadsheet of figure 7.2 where we have used relation (7.53).
figure 7.2. spreadsheet for example 7.7
f(x)
(x – x1)(x – x2)…(x – xn)
(x0 – x1)(x0 – x2)…(x0 – xn)
-f(x0)
(x – x0)(x – x2)…(x – xn)
(x1 – x0)(x1 – x2)…(x1 – xn)
= + -f(x1)
(x – x0)(x – x1)…(x – xn – 1)
(xn – x0)(xn – x2)…(xn – xn – 1)
+ f(xn)
x0, x1, x2, …, xn
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
a b c d e f g h i j k l
lagrange s interpolation method
numer. denom. division
interpol. at x= 2 partial partial of partial
prods prods prods
x f(x) x-x1 x-x2 x-x3 x-x4 x-x5 f(x0)
x0 -1.00 3.000 2.000 1.500 1.000 -0.500 -1.000 3.000 4.500
x1 0.00 -2.000 x0-x1 x0-x2 x0-x3 x0-x4 x0-x5 -0.107
x2 0.50 -0.375 -1.000 -1.500 -2.000 -3.500 -4.000 -42.000
x3 1.00 3.000 x-x0 x-x2 x-x3 x-x4 x-x5 f(x1)
x4 2.50 16.125 3.000 1.500 1.000 -0.500 -1.000 -2.000 -4.500
x5 3.00 19.000 x1-x0 x1-x2 x1-x3 x1-x4 x1-x5 -1.200
1.000 -0.500 -1.000 -2.500 -3.000 3.750
x-x0 x-x1 x-x3 x-x4 x-x5 f(x2)
3.000 2.000 1.000 -0.500 -1.000 -0.375 -1.125
x2-x0 x2-x1 x2-x3 x2-x4 x2-x5 0.600
1.500 0.500 -0.500 -2.000 -2.500 -1.875
x-x0 x-x1 x-x2 x-x4 x-x5 f(x3)
3.000 2.000 1.500 -0.500 -1.000 3.000 13.500
x3-x0 x3-x1 x3-x2 x3-x4 x3-x5 4.500
2.000 1.000 0.500 -1.500 -2.000 3.000
x-x0 x-x1 x-x2 x-x3 x-x5 f(x4)
3.000 2.000 1.500 1.000 -1.000 16.125 -145.125
x4-x0 x4-x1 x4-x2 x4-x3 x4-x5 11.057
3.500 2.500 2.000 1.500 -0.500 -13.125
x-x0 x-x1 x-x2 x-x3 x-x4 f(x5)
3.000 2.000 1.500 1.000 -0.500 19.000 -85.500
x5-x0 x5-x1 x5-x2 x5-x3 x5-x4 -2.850
4.000 3.000 2.500 2.000 0.500 30.000
f(2)= sum= 12
numerical analysis using matlab and spreadsheets, second edition 7-19
orchard publications
gregory-newton forward interpolation method
7.6 gregory-newton forward interpolation method
this method uses the formula
(7.54)
where is the first value of the data set, , , and are the first, second, and third forward*
differences respectively.
the variable is the difference between an unknown point and a known point divided by
the interval , that is,
(7.55)
the formula of (7.54) is valid only when the values are equally spaced with interval
. it is used to interpolate values near the smaller values of , that is, the values near the
beginning of the given data set. the formula that we will study on the next section, is used to
interpolate values near the larger values of , that is, the values near the end of the given data
set.
example 7.8
use the gregory-newton forward interpolation formula to compute from the following
data.
solution:
we enter the given and values in a difference table then, we compute the first, second,
and third differences. these are not divided differences and therefore, we simply subtract the second
value of from the first, the third from the second, and so on, as shown in table 7.13.
for this example,
* this is an expression to indicate that we use the differences in a forward sequence, that is, the first entries on the columns
where the differences appear.
table 7.12 table for example 7.8
x 1.00 1.05 1.10 1.15 1.20 1.25
1.000000 1.257625 1.531000 1.820875 2.128000 2.453125
f (x) f0 r?f0
r(r – 1)
2!
?2f0
r(r – 1)(r – 2)
3!
= + + + -?3f0 + …
f0 ?f0 ?2f0 ?3f0
r x x1
h
r
(x – x1)
h
=
x0, x1, x2,…, xn
h x
x
f (1.03)
y = f (x)
x f (x)
f (x)
chapter 7 finite differences and interpolation
7-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(7.56)
and with these values, using (7.54), we get
(7.57)
the spreadsheet of figure 7.3 shows the layout and computations for this example.
7.7 gregory-newton backward interpolation method
this method uses the formula
(7.58)
where is the first value of the data set, , , and are the first, second and third
backward differences, and
table 7.13 difference table for example 7.8
1st difference 2nd difference 3rd difference
x
1.00 1.000000
0.257625
1.05 1.257625 0.015750
0.273375 0.000750
1.10 1.531000 0.016500
0.289875 0.000750
1.15 1.820875 0.017250
0.307125 0.000750
1.20 2.128000 0.018000
0.325125
1.25 2.453125
f (x) f (x0, x1) f (x0, x1, x2) f (x0, x1, x2, x3)
f0 = f(1.00) = 1.000000
h = x1 – x0 = 1.05 – 1.00 = 0.05
r
x – x1
h
- 1.03 – 1.00
0.05
= = = 0.60
f(1.03) 1.000000 (0.60) ? (0.257625) (0.60) ? (0.60 – 1)
2!
= + + -
(0.60) ? (0.60 – 1)(0.60 – 2)
3!
+ ? (0.000750) = 1.152727
f (x) f0 r?f–1
r(r + 1)
2!
?2f–2
r(r + 1)(r + 2)
3!
-?3f–3 = + + + +…
f0 ?f–1 ?2f–2 ?3f–3
r
(x – x1)
h
=
numerical analysis using matlab and spreadsheets, second edition 7-21
orchard publications
gregory-newton backward interpolation method
figure 7.3. spreadsheet for example 7.8
expression (7.58) is valid only when the values are equally spaced with interval
. it is used to interpolate values near the end of the data set, that is, the larger values of .
backward interpolation is an expression to indicate that we use the differences in a backward
sequence, that is, the last entries on the columns where the differences appear.
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
a b c d e f g
gregory-newton forward interpolation method
see expressions (7.54) and (7.55)
interpolate f(x) at x= 1.03
x f(x) ?f ?2f ?3f
1.00 1.000000
0.257625
1.05 1.257625 0.015750
0.273375 0.000750
1.10 1.531000 0.016500
0.289875 0.000750
1.15 1.820875 0.017250
0.307125 0.000750
1.20 2.128000 0.018000
0.325125
1.25 2.453125
h= a10-a8= 0.05 r= (d5-a8)/c20= 0.6
f(1.12)= b8+f20*c9+(f20*(f20-1)*d10)/fact(2)+(f20*(f20-1)*(f20-2)*e11)/fact(3)
= 1.152727
1.00 1.000000
1.05 1.257625
1.10 1.531000
1.15 1.820875
1.20 2.128000
1.25 2.453125
0.00
0.50
1.00
1.50
2.00
2.50
3.00
1.00 1.05 1.10 1.15 1.20 1.25
x0, x1, x2, …, xn
h x
chapter 7 finite differences and interpolation
7-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 7.9
use the gregory-newton backward interpolation formula to compute from the data set of
table 7.14.
solution:
we arbitrarily choose as our starting point since lies between and
. then,
and
now, by (7.58) we have:
the computations were made with the spreadsheet of figure 7.4.
if the increments in values are small, we can use the excel vlookup function to perform
interpolation. the syntax of this function is as follows.
vlookup(lookup_value, table_array, col_index_num, range lookup)
where:
lookup_value is the value being searched in the first column of the lookup table
table_array are the columns forming a rectangular range or array
col_index_num is the column where the answer will be found
range lookup is a logical value (true or false) that specifies whether we require vlookup to
find an exact or an approximate match. if true is omitted, an approximate match is returned. in
other words, if an exact match is not found, the next largest value that is less than the
lookup_value is returned. if false is specified, vlookup will attempt to find an exact match,
and if one is not found, the error value #n/a will be returned.
a sample spreadsheet is shown in figure 7.5 where the values of x extend from ?5 to +5 volts.
only a partial table is shown
table 7.14 data for example 7.9
x 1.00 1.05 1.10 1.15 1.20 1.25
1.000000 1.257625 1.531000 1.820875 2.128000 2.453125
f (1.18)
y = f (x)
f0 = 2.128000 f(1.18) f(1.15)
f(1.20)
h = 1.20 – 1.15 = 0.05
r x x1 = ( – ) ? h = (1.18 – 1.20) ? 0.05 = –0.4
f(1.18) 2.128 (–0.4)(0.307125) (–0.4)(– 0.4 + 1)
2!
-(0.01725)
(–0.4)(– 0.4 + 1)(– 0.4 + 2)
3!
(0.00075)= 2.003032
+ +
+
=
x
numerical analysis using matlab and spreadsheets, second edition 7-23
orchard publications
interpolation with matlab
figure 7.4. spreadsheet for example 7.9
7.8 interpolation with matlab
matlab has several functions that perform interpolation of data. we will study the following:
1. interp1(x,y,xi) performs one dimensional interpolation where x and y are related as y = f(x)
and xi is some value for which we want to find y(xi) by linear interpolation, i.e., “table lookup”.
this command will search the x vector to find two consecutive entries between which the
desired value falls. it then performs linear interpolation to find the corresponding value of y.
to obtain a correct result, the components of the x vector must be monotonic, that is, either in
ascending or descending order.
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
a b c d e f g
gregory-newton backward interpolation method
see formula 7.58
interpolate f(x) at x= 1.18
x f(x) ?f ?2f ?3f
1.00 1.000000
0.257625
1.05 1.257625 0.015750
0.273375 0.000750
1.10 1.531000 0.016500
0.289875 0.000750
1.15 1.820875 0.017250
0.307125
1.20 2.128000
h= a16-a14= 0.05 r= (c5-a16)/c18= -0.4
f(1.18)= b16+f18*c15+(f18*(f18+1)*d14)/fact(2)+(f18*(f18+1)*(f18+2)*e13)/fact(3)
= 2.003032
1.00 1.000000
1.05 1.257625
1.10 1.531000
1.15 1.820875
1.20 2.128000
0.000
0.500
1.000
1.500
2.000
2.500
1.00 1.05 1.10 1.15 1.20
chapter 7 finite differences and interpolation
7-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 7.5. using the excel vlookup function for interpolation
2. interp1(x,y,xi,’method’) performs the same operation as interp1(x,y,xi) where the string
method allows us to specify one of the methods listed below.
nearest ? nearest neighbor interpolation
linear ? linear interpolation this is the default interpolation
spline ? cubic spline interpolation this does also extrapolation
cubic ? cubic interpolation this requires equidistant values of x
3. interp2(x,y,z,xi,yi) is similar to interp1(x,y,xi) but performs two dimensional interpolation
4. interp2(x,y,z,xi,yi,’method’) is similar to interp1(x,y,xi,’method’) but performs two dimensional
interpolation. the default is linear. the spline method does not apply to two dimensional
interpolation.
we will illustrate the applications of these functions with the examples that follow.
example 7.10
the (current-voltage) relation of a non-linear electrical device is given by
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e f g h
v i
-2.000 -0.0330
-1.975 -0.0326
-1.950 -0.0323
-1.925 -0.0320
-1.900 -0.0316
-1.875 -0.0313
-1.850 -0.0309 a b
-1.825 -0.0306 8 -1.8500 -0.03092657
-1.800 -0.0302 9 -1.8250 -0.03058033
-1.775 -0.0299
-1.750 -0.0295 =vlookup(-1.8375,a2:b282,2) = -0.0309266
-1.725 -0.0292 =vlookup(-1.8375,a2:b282,2,true) = -0.0309266
-1.700 -0.0288 =vlookup(-1.8375,a2:b282,2,false) = #n/a
-1.675 -0.0285
-1.650 -0.0281 a b
-1.625 -0.0277 264 4.5500 0.14843225
-1.600 -0.0274 265 4.5750 0.14967753
-1.575 -0.0270
-1.550 -0.0267 =vlookup(4.5535,a2:b282,2) = 0.14843225
-0.05
0.00
0.05
0.10
0.15
0.20
-2 -1 0 1 2 3 4 5
i – v
numerical analysis using matlab and spreadsheets, second edition 7-25
orchard publications
interpolation with matlab
(7.59)
where is in volts and in milliamperes. compute for 30 data points of within the interval
, plot versus in this range, and using linear interpolation compute when
volts.
solution:
we are required to use 30 data points within the given range accordingly, we will use the matlab
linspace(first_value, last_value, number_of_values) command. the code below produces
30 values in volts, the corresponding values in milliamperes, and plots the data for this
range. then, we use the interp1(x,y,xi) command to interpolate at the desired value.
% this code is for example_7_10.m
% it computes the values of current (in milliamps) vs. voltage (volts)
% for a diode whose v-i characteristics are i=0.1(exp(0.2v)-1).
% we can use the matlab function interp1 to linearly interpolate
% the value of milliamps for any value of v within the specified interval.
%
v=linspace(-2, 5, 30) % specify 30 intervals in the -2<=v<=5 interval
a=0.1 .* (exp(0.2 .* v)?1) % we use "a" for current instead of "i" to avoid conflict
% with imaginary numbers
v_a=[v a] % define "v_a" as a two-column matrix to display volts
% and amperes side-by-side.
plot(v,a) grid
title( volt-ampere characteristics for a junction diode )
xlabel( voltage (volts) )
ylabel( current (milliamps) )
fprintf( volts milliamps \n ) % heading of the two-column matrix
fprintf( \n )
disp(v_a) % display values of volts and amps below the heading
ma=interp1(v,a,1.265) % linear (default) interpolation
fprintf( current (in milliamps) @ v=1.265 is %2.4f \n , ma)
the data and the value obtained by interpolation are shown below.
volts milliamps
-2.0000 -0.0330
-1.7586 -0.0297
-1.5172 -0.0262
-1.2759 -0.0225
-1.0345 -0.0187
-0.7931 -0.0147
-0.5517 -0.0104
i(t) 0.1 e0.2v(t) = ( – 1)
v i i v
–(2 ? v ? 5) i v i
v = 1.265
chapter 7 finite differences and interpolation
7-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
-0.3103 -0.0060
-0.0690 -0.0014
0.1724 0.0035
0.4138 0.0086
0.6552 0.0140
0.8966 0.0196
1.1379 0.0256
1.3793 0.0318
1.6207 0.0383
1.8621 0.0451
2.1034 0.0523
2.3448 0.0598
2.5862 0.0677
2.8276 0.0760
3.0690 0.0847
3.3103 0.0939
3.5517 0.1035
3.7931 0.1135
4.0345 0.1241
4.2759 0.1352
4.5172 0.1468
4.7586 0.1590
5.0000 0.1718
current (in milliamps) @ v=1.265 is 0.0288
the plot for this example is shown in figure 7.6.
figure 7.6. plot for example 7.10
numerical analysis using matlab and spreadsheets, second edition 7-27
orchard publications
interpolation with matlab
example 7.11
plot the function
(7.60)
in the interval with 120 intermediate values. then, use the matlab
interp1(x,y,xi,’method’) function to interpolate at , , , and . compare the
values obtained with the linear, cubic, and spline methods, with the analytical values.
solution:
the code below plots (7.60) and produces the values of analytical values, for comparison with
the linear, cubic, and spline interpolation methods.
% this is the code for example_7_11
%
x=linspace(0,2*pi,120) % we need these two
y=(cos(x)) .^ 5 % statements for the plot
%
analytic=(cos([pi/8 pi/4 3*pi/5 3*pi/7] ) .^ 5)
%
plot(x,y) grid title( y=cos^5(x) ) xlabel( x ) ylabel( y )
%
linear_int=interp1(x,y,[pi/8 pi/4 3*pi/5 3*pi/7] , linear )
% the label linear on the right side of the above statement
% could be have been omitted since the default is linear
%
cubic_int=interp1(x,y,[pi/8 pi/4 3*pi/5 3*pi/7] , cubic )
%
spline_int=interp1(x,y,[pi/8 pi/4 3*pi/5 3*pi/7] , spline )
%
y=zeros(4,4) % construct a 4 x 4 matrix of zeros
y(:,1)=analytic % 1st column of matrix
y(:,2)=linear_int % 2nd column of matrix
y(:,3)=cubic_int % 3rd column of matrix
y(:,4)=spline_int % 4th column of matrix
fprintf( \n ) % insert line
fprintf( analytic \t linear int \t cubic int \t spline int \n )
fprintf( \n )
fprintf( %8.5f\t %8.5f\t %8.5f\t %8.5f\n ,y )
fprintf( \n )
%
% the statements below compute the percent error for the three
% interpolation methods as compared with the exact (analytic) values
%
y = f(x) = cos5x
0 ? x ? 2?
? ? 8 ? ? 4 3? ? 5 3? ? 7
chapter 7 finite differences and interpolation
7-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
error1=(linear_int?analytic).*100 ./ analytic
error2=(cubic_int?analytic).*100 ./ analytic
error3=(spline_int?analytic).*100 ./ analytic
%
z=zeros(4,3) % construct a 4 x 3 matrix of zeros
z(:,1)=error1 % 1st column of matrix
z(:,2)=error2 % 2nd column of matrix
z(:,3)=error3 % 3rd column of matrix
% fprintf( \n ) % insert line
disp( the percent errors for each interpolation method are: )
fprintf( \n )
fprintf( linear int \t cubic int \t spline int \n )
fprintf( \n )
fprintf( %8.5f\t %8.5f\t %8.5f\n ,z )
fprintf( \n )
the plot for the function of this example is shown in figure 7.7.
figure 7.7. plot the function of example 7.11
the analytical and interpolated values are shown below for comparison.
analytic linear int cubic int spline int
0.67310 0.67274 0.67311 0.67310
0.17678 0.17718 0.17678 0.17678
-0.00282 -0.00296 -0.00281 -0.00282
0.00055 0.00062 0.00054 0.00055
the percent errors for each interpolation method are:
numerical analysis using matlab and spreadsheets, second edition 7-29
orchard publications
interpolation with matlab
linear int cubic int spline int
-0.05211 0.00184 0.00002
0.22707 -0.00012 0.00011
5.09681 -0.40465 -0.01027
13.27678 -0.64706 -0.07445
example 7.12
for the impedance example of section 1.7 in chapter 1 whose code and plot are shown below,
use the spline method of interpolation to find the magnitude of the impedance at
.
solution:
% the file is example_7_12.m
% it calculates and plots the impedance z(w) versus radian frequency w.
%
% use the following five statements to obtain |z| versus radian frequency w
w=300:100:3000
z=zeros(28,2)
z(:,1)=w
z(:,2)=(10+(10.^4?j.*10.^6./w)./(10+j.*(0.1.*w?10.^5./w)))
fprintf( %2.0f\t %10.3f\n ,abs(z) )
%
w=[300 400 500 600 700 800 900 1000 1100 1200 1300 1400 1500....
1600 1700 1800 1900 2000 2100 2200 2300....
2400 2500 2600 2700 2800 2900 3000]
z=[39.339 52.789 71.104 97.665 140.437 222.182 436.056 1014.938...
469.830 266.032 187.052 145.751 120.353...
103.111 90.603 81.088 73.588 67.513 62.481...
58.240 54.611 51.468 48.717 46.286 44.122...
42.182 40.432 38.845]
semilogx(w,z) grid
title( magnitude of impedance vs. radian frequency )
xlabel( w in rads/sec ) ylabel( |z| in ohms )
%
zi=interp1(w,z,792, spline )
fprintf( \n )
fprintf( magnitude of z at w=792 rad/s is %6.3f ohms \n , zi)
fprintf( \n )
the plot for the function of this example is shown in figure 7.8.
? = 792 rad ? s
chapter 7 finite differences and interpolation
7-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 7.8. plot for the function of example 7.12
matlab interpolates the impedance at and displays the following message:
magnitude of z at w=792 rad/s is 217.034 ohms
two-dimensional plots were briefly discussed in chapter 1. for convenience, we will review the
following commands which can be used for two-dimensional interpolation.
1. mesh(z) ? plots the values in the matrix z as height values above a rectangular grid, and connects
adjacent points to form a mesh surface.
2. [x,y]=meshgrid(x,y) ? generates interpolation arrays which contain all combinations of the
x and y points which we specify. x and y comprise a pair of matrices representing a rectangular
grid of points in the plane. using these points, we can form a function where
is a matrix.
example 7.13
generate the plot of the function
(7.61)
in three dimensions , , and . this function is the equivalent of the function in
two dimensions. here, is a matrix that contains the distances from the origin to each point in
the pair of matrices that form a rectangular grid of points in the plane.
? = 792 rad ? s
x – y z = f (x, y)
z
z sinr
r
= -
x y z y = sinx ? x
r
[x, y] x – y
numerical analysis using matlab and spreadsheets, second edition 7-31
orchard publications
interpolation with matlab
solution:
the matrix that contains the distances from the origin to each point in the pair of
matrices, is
(7.62)
we let the origin be at , and the plot in the intervals and
. then, we write and execute the following matlab code.
% this is the code for example_7_13
x=-2*pi: pi/24: 2*pi % define interval in increments of pi/24
y=x % y must have same number of points as x
[x,y]=meshgrid(x,y) % create x and y matrices
r=sqrt(x .^ 2 + y .^ 2) % compute distances from origin (0,0) to x-y points
z=sin(r) ./ (r+eps) % eps prevents division by zero
mesh(x,y,z) % generate mesh plot for z=sin(r)/r
xlabel( x ) ylabel( y ) zlabel( z )
title( plot for the three-dimensional sin(r) / r function )
the plot for the function of this example is shown in figure 7.9.
figure 7.9. plot for example 7.13
example 7.14
generate the plot of the function
(7.63)
in three dimensions , , and . use the cubic method to interpolate the value of at
and .
r [x, y]
r = x 2 + y 2
(x0, y0) = (0, 0) –2? ? x ? 2?
–2? ? y ? 2?
z = x3 + y3 – 3xy
x y z z x = –1
y = 2
chapter 7 finite differences and interpolation
7-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
we let the origin be at , and the plot in the intervals and
. then, we write and execute the following code.
% this is the code for example_7_14
x=?10: 0.25: 10 % define interval in increments of 0.25
y=x % y must have same number of points as x
[x,y]=meshgrid(x,y) % create x and y matrices
z=x.^3+y.^3?3.*x.*y
mesh(x,y,z) % generate mesh plot
xlabel( x ) ylabel( y ) zlabel( z )
title( plot for the function of example 7.14 )
z_int=interp2(x,y,z, -1,2, cubic )
fprintf( \n )
fprintf( interpolated value of z at x = ?1 and y = 2 is z = %4.2f \n ,z_int)
fprintf( \n )
the plot for the function of this example is shown in figure 7.10.
figure 7.10. plot for example 7.14
interpolated value of z at x = -1 and y = 2 is z = 13.00
example 7.15
a land surveyor measured and recorded the data below for a rectangular undeveloped land which
lies approximately 500 meters above sea level.
500.08 500.15 500.05 500.08 500.14 500.13 500.09 500.15
500.12 500.01 500.11 500.18 500.15 500.12 500.05 500.15
(x0, y0) = (0, 0) –10 ? x ? 10
–10 ? y ? 10
numerical analysis using matlab and spreadsheets, second edition 7-33
orchard publications
interpolation with matlab
500.13 500.12 500.09 500.11 500.11 500.05 500.15 500.02
500.09 500.17 500.17 500.14 500.16 500.09 500.02 500.11
500.08 500.09 500.13 500.18 500.14 500.14 500.14 500.15
500.15 500.10 500.11 500.11 500.12 500.13 500.14 500.12
500.17 500.12 500.13 500.18 500.13 500.15 500.17 500.11
500.13 500.14 500.13 500.09 500.14 500.16 500.17 500.14
500.15 500.09 500.14 500.18 500.17 500.08 500.13 500.09
500.12 500.15 500.14 500.01 500.16 500.12 500.11 500.10
500.02 500.19 500.01 500.08 500.12 500.02 500.16 500.12
500.19 500.21 500.17 500.03 500.17 500.09 500.14 500.17
this rectangular land parcel is 175 meters wide and 275 meters deep. the measurements shown
above were made at points 25 meters apart.
a. denoting the width as the , the depth as the and the height as the ,
plot the given data to form a rectangular grid.
b. interpolate the value of at m, and m.
c. compute the maximum height and its location on the plane.
solution:
the matlab code and plot are shown below and explanations are provided with comment
statements.
% this code is for example_7_15
%
x=0: 25: 175 % x-axis varies across the rows of z
y=0: 25: 275 % y-axis varies down the columns of z
z=[500.08 500.15 500.05 500.08 500.14 500.13 500.09 500.15
500.12 500.01 500.11 500.18 500.15 500.12 500.05 500.15
500.13 500.12 500.09 500.11 500.11 500.05 500.15 500.02
500.09 500.17 500.17 500.14 500.16 500.09 500.02 500.11
500.08 500.09 500.13 500.18 500.14 500.14 500.14 500.15
500.15 500.10 500.11 500.11 500.12 500.13 500.14 500.12
500.17 500.12 500.13 500.18 500.13 500.15 500.17 500.11
500.13 500.14 500.13 500.09 500.14 500.16 500.17 500.14
500.15 500.09 500.14 500.18 500.17 500.08 500.13 500.09
500.12 500.15 500.14 500.01 500.16 500.12 500.11 500.10
500.02 500.19 500.01 500.08 500.12 500.02 500.16 500.12
500.19 500.21 500.17 500.03 500.17 500.09 500.14 500.17]
%
mesh(x,y,z) axis([0 175 0 275 500 502]) grid off box off
xlabel( x-axis, m ) ylabel( y-axis, m ) zlabel( height, meters above sea level ) title( parcel map )
% the pause command below stops execution of the program for 10 seconds
% so that we can see the mesh plot
x – axis y – axis z – axis
z x = 108 y = 177
x – y
chapter 7 finite differences and interpolation
7-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
pause(10)
z_int=interp2(x,y,z,108,177, cubic )
disp( interpolated z is: ) z_int
[xx,yy]=meshgrid(x,y)
xi=0: 2.5: 175 % make x-axis finer
% size(xi) % returns a row vector containing the size of xi where the
% first element denotes the number of rows and the second is the number
% of columns. here, size(xi) = 1 71
disp( size(xi) ) size(xi)
yi=0: 2.5: 275 % make y-axis finer
disp( size(yi) ) size(yi)
[xxi,yyi]=meshgrid(xi,yi) % forms grid of all combinations of xi and yi
% size(xxi) = size(yyi) = size(zzi) = 111 71
disp( size(xxi) ) size(xxi) disp( size(yyi) ) size(yyi) disp( size(zzi) ) size(zzi)
size(xxi), size(yyi), size(zzi)
zzi=interp2(x,y,z,xxi,yyi, cubic ) % cubic interpolation - interpolates
% all combinations of xxi and yyi and constructs the matrix zzi
mesh(xxi,yyi,zzi) % plot smoothed data
hold on
[xx,yy]=meshgrid(x,y) % grid with original data
plot3(xx,yy,z, *k ) axis([0 175 0 275 500 503]) grid off box off
xlabel( x-axis, m ) ylabel( y-axis, m ) zlabel( height, meters above sea level )
title( map of rectangular land parcel )
hold off
% max(x) returns the largest element of vector x
% max(a) returns a row vector which contains the maxima of the columns
% in matrix a. likewise max(zzi) returns a row vector which contains the
% maxima of the columns in zzi. observe that size(max(zzi)) = 1 71
% and size(max(max(zzi))) = 1 1
zmax=max(max(zzi)) % estimates the peak of the terrain
% the find function returns the subscripts where a relational expression
% is true. for example,
% a=[a11 a12 a13 a21 a22 a23 a31 a32 a33] or
% a=[?1 0 3 2 3 -4 -2 5 6]
% [i,j]=find(a>2)
% returns
% i =
%
% 2
% 3
% 1
% 3
%
%
% j =
numerical analysis using matlab and spreadsheets, second edition 7-35
orchard publications
interpolation with matlab
%
% 2
% 2
% 3
% 3
% that is, the elements a22=3, a32=5, a13=3 and a33=6
% satisfy the condition a>2
% the == operator compares two variables and returns ones when they
% are equal, and zeros when they are not equal
%
[m,n]=find(zmax==zzi)
% m =
%
% 65
%
% n =
%
% 36
%
% that is, zmax is located at zzi = z(65)(36)
%
% the x-cordinate is found from
xmax=xi(n)
% xmax =
%
% 1.7500 % column 36 size(xi) = 1 71
% and the y-coordinate is found from
ymax=yi(m)
% ymax =
%
% 3.2000 % row 65 size(yi) = 1 111
% remember that i is the row index, j is the column index, and x-axis
% varies across the rows of z and y-axis varies down the columns of z
interpolated z is:
z_int =
500.1492
size(xi)
ans =
1 71
size(yi)
ans =
chapter 7 finite differences and interpolation
7-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
1 111
size(xxi)
ans =
111 71
size(yyi)
ans =
111 71
zzi=interp2(x,y,z,xxi,yyi, cubic ) % cubic interpolation - interpolates
% all combinations of xxi and yyi and constructs the matrix zzi
size(zzi)
ans =
111 71
zmax=max(max(zzi)) % estimates the peak of the terrain
zmax =
500.2108
m =
111
n=
9
xmax =
20
ymax =
275
these values indicate that where the and coordinates are and
. the interpolated value of at m and m is . the plot is
shown in figure 7.11.
zmax = 500.21 x y x = 20
y = 275 z x = 108 y = 177 z = 500.192
numerical analysis using matlab and spreadsheets, second edition 7-37
orchard publications
summary
figure 7.11. plot for example 7.15
7.9 summary
• the first divided difference is defined as:
where and are any two, not necessarily consecutive values of , within an interval.
• likewise, the second divided difference is defined as:
and the third, fourth, and so on divided differences are defined similarly.
• if the values of are equally spaced and the denominators are all the same, these values are
referred to as the differences of the function.
• if the constant difference between successive values of is , the typical value of is
• we can now express the first differences are usually expressed in terms of the difference operator
as
f(xi, xj)
f(xi) – f(xj)
xi – xj
=
xi xj x
f(xi, xj, xk)
f(xi, xj) – f(xj, xk)
xi – xk
= -
x
x h xk
xk = x0 + kh for k = …,–2, –1, 0, 1, 2, …
?
?fk = fk + 1 – fk
chapter 7 finite differences and interpolation
7-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• likewise, the second differences are expressed as
and, in general, for positive integer values of
• the difference operator obeys the law of exponents which states that
• the differences are found from the relation
for , and , the above relation reduces to
• as with derivatives, the differences of a polynomial of degree are constant.
• the factorial polynomials are defined as
and
using the difference operator with the above relations we get
and
these are very similar to differentiation of and .
?2fk ?(?fk) ?fk + 1 = = – ?fk
n
?nfk ? ?n – 1( fk) ?n – 1fk + 1 ?n – 1= = – fk
?
?m(?nfk) ?m + n= fk
nth ?nfk
?nfk fk + n – nfk + n – 1 n(n – 1)
2!
-fk + n – 2 … (–1)n – 1nfk + 1 = + + + + (–1)nfk
k = 0 n = 1, 2, 3 4
?f0 = f2 – f1
?2f0 = f2 – 2f1 + f0
?3f0 = f3 – 3f2 + 3f1 – f0
?4f0 = f4 – 4f3 + 6f2–4f1 + f0
nth n
(x)(n) = x(x – 1)(x – 2)…(x – n + 1)
(x)–(n) 1
(x – 1)(x – 2)…(x + n)
=
?
?(x)(n) n(x)(n – 1) =
?(x)–(n) –n(x)–(n – 1) =
xn x–n
numerical analysis using matlab and spreadsheets, second edition 7-39
orchard publications
summary
• we can express any algebraic polynomial as a factorial polynomial . then, in analogy
with maclaurin power series, we can express that polynomial as
where
• factorial polynomials provide an easier method of constructing a difference table. the procedure
is as follows:
1. we divide by to get a quotient and a remainder which turns out to be the
constant term . then, the factorial polynomial reduces to
2. we divide by , to get a quotient and a remainder which turns out to
be the constant term . then,
and by substitution we get
3. we divide by , to get a quotient and a remainder which turns out to
be the constant term , and thus
and by substitution we get
and in general,
where
fn(x) pn(x)
pn(x) a0 a1(x)(1) a2(x)(2) … an(x)(n)
= + + + +
aj
?jpn(0)
j!
= for j = 0, 1, 2, …, n
pn(x) x q0(x) r0
a0
pn(x) = r0 + xq0(x)
q0(x) (x – 1) q1(x) r1
a1
q0(x) = r1 + (x – 1)q1(x)
pn(x) r0 + x[r1 + (x – 1)q1(x)] r0 r1(x)(1) = = + + x(x – 1)q1(x)
q1(x) (x – 2) q2(x) r2
a2
q1(x) = r2 + (x – 2)q2(x)
pn(x) r0 r1(x)(1) = + + x(x – 1)[r2 + (x – 2)q2(x)]
r0 r1(x)(1) + r2(x)(2) = + + x(x – 1)(x – 2)q2(x)
pn(x) r0 r1(x)(1) + r2(x)(2) … rn – 1 (x)(n – 1) rn(x)(n)
= + + + +
rj aj
?jpn(0)
j!
= =
chapter 7 finite differences and interpolation
7-40 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the antidifference of a factorial polynomial is analogous to integration in elementary calculus.
it is denoted as , and it is computed from
• antidifferences are very useful in finding sums of series.
• the definite sum of for the interval is
• in analogy with the fundamental theorem of integral calculus which states that
we have the fundamental theorem of sum calculus which states that
• one important application of finite differences is interpolation.
• newton’s divided difference interpolation method uses the formula
where , , and are the first, second, and third divided differences
respectively. this method has the advantage that the values need not be
equally spaced, or taken in consecutive order.
• lagrange’s interpolation method uses the formula
and, like newton’s divided difference method, has the advantage that the values
need not be equally spaced or taken in consecutive order.
?–1pn(x)
?–1 (x)(n) (x) (n + 1)
(n + 1)
=
pn(x) a ? x ? a + (n – 1)h
pn(x)
x = ?
? + (n – 1)h
? = pn(?) + pn(? + h) + pn(? + 2h) + …+ pn[? + (n – 1)h]
f(x)dx
a
b
? = f(b) – f(a)
pn(x)
x = ?
? + (n – 1)h
? ?–1pn(x)
?
? + nh =
f(x) = f(x0) + (x – x0) f (x0, x1) + (x – x0)(x – x1) f (x0, x1, x2)
+ (x – x0)(x – x1)(x – x2) f (x0, x1, x2, x3)
f(x0, x1) f(x0, x1, x2) f(x0, x1, x2, x3)
x0, x1, x2,…, xn
f(x)
(x – x1)(x – x2)…(x – xn)
(x0 – x1)(x0 – x2)…(x0 – xn)
-f(x0)
(x – x0)(x – x2)…(x – xn)
(x1 – x0)(x1 – x2)…(x1 – xn)
= + -f(x1)
(x – x0)(x – x1)…(x – xn – 1)
(xn – x0)(xn – x2)…(xn – xn – 1)
+ f(xn)
x0, x1, x2, …, xn
numerical analysis using matlab and spreadsheets, second edition 7-41
orchard publications
summary
• the gregory-newton forward interpolation method uses the formula
where is the first value of the data set, , , and are the first, second, and third
forward differences respectively. the variable is the difference between an unknown point
and a known point divided by the interval , that is,
this formula is valid only when the values are equally spaced with interval .
it is used to interpolate values near the smaller values of , that is, the values near the beginning
of the given data set, hence the name forward interpolation.
• the gregory-newton backward interpolation method uses the formula
where is the first value of the data set, , , and are the first, second and third
backward differences, and
this formula is valid only when the values are equally spaced with interval .
it is used to interpolate values near the end of the data set, that is, the larger values of . backward
interpolation is an expression to indicate that we use the differences in a backward
sequence, that is, the last entries on the columns where the differences appear.
• if the increments in values are small, we can use the excel vlookup function to perform
interpolation.
• we can perform interpolation to verify our results with the matlab functions
interp1(x,y,xi), interp1(x,y,xi,’method’) where method allows us to specify nearest (nearest
neighbor interpolation), linear (linear interpolation, the default interpolation), spline (cubic
spline interpolation which does also extrapolation), cubic (cubic interpolation which requires
equidistant values of ), and interp2(x,y,z,xi,yi) which is similar to interp1(x,y,xi) but performs
two dimensional interpolation
f (x) f0 r?f0
r(r – 1)
2!
?2f0
r(r – 1)(r – 2)
3!
= + + + -?3f0 + …
f0 ?f0 ?2f0 ?3f0
r x
x1 h
r
(x – x1)
h
=
x0, x1, x2, …, xn h
x
f (x) f0 r?f–1
r(r + 1)
2!
?2f–2
r(r + 1)(r + 2)
3!
-?3f–3 = + + + + …
f0 ?f–1 ?2f–2 ?3f–3
r
(x – x1)
h
=
x0, x1, x2, …, xn h
x
x
x
chapter 7 finite differences and interpolation
7-42 numerical analysis using matlab and spreadsheets, second edition
orchard publications
7.10 exercises
1. express the given polynomial below as a factorial polynomial , calculate the leading
differences, and then construct the difference table with .
2. use the data of the table below and the appropriate (forward or backward) gregory-newton
formula, to compute:
a.
b.
3. use the data of the table below and newton’s divided difference formula to compute:
a.
b.
x 50 51 52 53 54 55 56
7.071 7.141 7.211 7.280 7.348 7.416 7.483
x 1.1 1.2 1.5 1.7 1.8 2.0
y=f(x) 1.112 1.219 1.636 2.054 2.323 3.011
f (x) p(x)
h = 1
f (x) = x5 – 2x4 + 4x3 – x + 6
50.2
55.9
x
f(1.3)
f(1.95)
numerical analysis using matlab and spreadsheets, second edition 7-43
orchard publications
solutions to exercises
7.11 solutions to exercises
1.
the highest power of the given polynomial is , we must evaluate the remainders
and then, we will use (7.28), given below, to determine .
we can compute the remainders by long division but, for convenience, we will use the matlab
deconv(p,q) function which divides the polynomial p by q.
the matlab code is as follows:
px=[1 ?2 4 0 ?1 6] % coefficients of given polynomial
d0=[1 0] % coefficients of first divisor, i.e, x
[q0,r0]=deconv(px,d0) % computation of first quotient and remainder
d1=[1 ?1] % coefficients of second divisor, i.e, x?1
[q1,r1]=deconv(q0,d1) % computation of second quotient and remainder
d2=[1 ?2] % coefficients of third divisor, i.e, x?2
[q2,r2]=deconv(q1,d2) % computation of third quotient and remainder
d3=[1 ?3] % coefficients of fourth divisor, i.e, x?3
[q3,r3]=deconv(q2,d3) % computation of fourth quotient and remainder
d4=[1 ?4] % coefficients of fifth divisor, i.e, x?4
[q4,r4]=deconv(q3,d4) % computation of fifth quotient and remainder
d5=[1 ?5] % coefficients of sixth (last) divisor, i.e, x?5
[q5,r5]=deconv(q4,d5) % computation of sixth (last) quotient and remainder
q0 =
1 -2 4 0 -1
r0 =
0 0 0 0 0 6
q1 =
1 -1 3 3
r1 =
0 0 0 0 2
q2 =
1 1 5
r2 =
0 0 0 13
f (x) = x5 – 2x4 + 4x3 – x + 6
f(x) 5
r0, r1, r2, r3, r4 r5 pn(x)
pn(x) r0 r1(x)(1) + r2(x)(2) … rn – 1 (x)(n – 1) rn(x)(n)
= + + + +
chapter 7 finite differences and interpolation
7-44 numerical analysis using matlab and spreadsheets, second edition
orchard publications
q3 =
1 4
r3 =
0 0 17
q4 =
1
r4 =
0 8
q5 =
0
r5 =
1
therefore, with reference to (7.28), the factorial polynomial is
we will verify that above is the same polynomial as the given by expansion of the
factorials using (7.12), i.e.,
with the matlab collect(‘s_expr’) function.
syms x px=collect((x*(x?1)*(x?2)*(x?3)*(x-4)+(8*x*(x?1)*(x?2)*(x-3))+(17*x*(x?1)*(x?2))+...
(13*x*(x?1))+2*x+6))
px =
x^5-2*x^4+4*x^3-x+6
we observe that this is the same algebraic polynomial as .
we will now compute the leading entries for the difference table using (7.30), i.e,
and above
we enter these values in the appropriate spaces as shown on the table below.
pn(x) 6 2(x)(1) 13(x)(2) 17(x)(3) 8(x)(4) (x)(5) = + + + + +
pn(x) fn(x)
(x)(n) = x(x – 1)(x – 2)…(x – n + 1)
f (x)
?jpn(0) = j!rj pn(x)
?0p(0) = 0! ? 6 = 6 ?1p(0) = 1! ? 2 = 2 ?2p(0) = 2! ? 13 = 26
?3p(0) = 3! ? 17 = 102 ?4p(0) = 4! ? 8 = 192 ?5p(0) = 5! ? 1 = 120
?6p(0) = 6! ? 0 = 0
numerical analysis using matlab and spreadsheets, second edition 7-45
orchard publications
solutions to exercises
we obtain the remaining set of values by crisscross addition as shown on the table below.
2.
a. we will use the differences in a forward sequence, that is, the first entries on the columns
where the differences appear. this is because the value of should be in the interval
. we enter the given and values in a difference table then, we compute
the first, second, and third differences. these are not divided differences and therefore, we
simply subtract the second value of from the first, the third from the second, and so
on, as shown below.
6
2
26
102
192
120
0
6
2
8 26
28 102
36 128 192
156 294 120
192 422 312 0
578 606 120
770 1028 432
1606 1038
2376 2066
3672
6048
x 50 51 52 53 54 55 56
7.071 7.141 7.211 7.280 7.348 7.416 7.483
x p x ( ) ? ?2 ?3 ?4 ?5 ?6
x p x ( ) ? ?2 ?3 ?4 ?5 ?6
x
50.2
50 ? x ? 51 x f (x)
f (x)
chapter 7 finite differences and interpolation
7-46 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and with these values, using (7.54), we get
the spreadsheet below shows the layout and computations for part (a).
check with matlab:
x =[50 51 52 53 54 55 56]
fx=[7.071 7.141 7.211 7.280 7.348 7.416 7.483]
spline_interp=interp1(x,fx,[50.2] , spline ) fprintf( \n ) ...
fprintf( spline interpolation yields f(50.2) = \n ) disp(spline_interp)
spline interpolation yields f(50.2) =
7.0849
1st difference 2nd difference 3rd difference
x
50 7.071
0.070
51 7.141 0.000
0.070 -0.001
52 7.211 -0.001
0.069 0.000
53 7.280 -0.001
0.068 0.001
54 7.348 0.000
0.068 -0.001
55 7.416 -0.001
0.067
56 7.483
f (x) f (x0, x1) f (x0, x1, x2) f (x0, x1, x2, x3)
f0 = f(50) = 7.071
h = x1 – x0 = 51 – 50 = 1
r
x – x1
h
- 50.2 50.0 –
1
= = = 0.20
f(50.2) 7.071 (0.20) ? (7.071) (0.20) ? (0.20 – 1)
2!
= + + -
(0.20) ? (0.20 – 1)(0.20 – 2)
3!
+ ? (0.001) = 7.085
numerical analysis using matlab and spreadsheets, second edition 7-47
orchard publications
solutions to exercises
b. since the value of is very close to the last value in the given range, we will use the
backward interpolation formula
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
a b c d e f g h
gregory-newton forward interpolation method for exercise 7.2(a)
see expressions (7.54) and (7.55)
interpolate f(x) at x= 50.2
x f(x) ?f ?2f ?3f ?4f ?5f ?6f
50.0 7.071
0.070
51.0 7.141 0.000
0.070 -0.001
52.0 7.211 -0.001 0.001
0.069 0.000 0.000
53.0 7.280 -0.001 0.001 -0.003
0.068 0.001 -0.003
54.0 7.348 0.000 -0.002
0.068 -0.001
55.0 7.416 -0.001
0.067
56.0 7.483
h= a10-a8= 1.00 r= (d5-a8)/c22= 0.2
f(50.2)= round(b8+f22*c9+(f20*(f20-1)*d10)/fact(2)+(f20*(f20-1)*(f20-2)*e11)/fact(3),3)
= 7.085
50 7.071
51 7.141
52 7.211
53 7.280
54 7.348
55 7.416
56 7.483
7.00
7.10
7.20
7.30
7.40
7.50
50 51 52 53 54 55 56
55.9
f (x) f0 r?f–1
r(r + 1)
2!
?2f–2
r(r + 1)(r + 2)
3!
-?3f–3 = + + + + …
chapter 7 finite differences and interpolation
7-48 numerical analysis using matlab and spreadsheets, second edition
orchard publications
where is the first value of the data set, , , and are the first, second and
third backward differences, and
we arbitrarily choose as our starting point since lies between and
. then,
and
now, by (7.58) we have:
check with matlab:
x =[50 51 52 53 54 55 56]
fx=[7.071 7.141 7.211 7.280 7.348 7.416 7.483]
spline_interp=interp1(x,fx,[55.9] , spline ) fprintf( \n ) ...
fprintf( spline interpolation yields f(55.9) = \n ) disp(spline_interp)
spline interpolation yields f(55.9) =
7.4764
3.
a. the first divided differences are:
the second divided differences are:
x 1.1 1.2 1.5 1.7 1.8 2.0
y=f(x) 1.112 1.219 1.636 2.054 2.323 3.011
f0 ?f–1 ?2f–2 ?3f–3
r
(x – x1)
h
=
f0 = 7.483 f(55.9) f(55)
f(56)
h = 56 – 55 = 1
r x x1 = ( – ) ? h = (55.9 – 56.0) ? 1 = –0.1
f(55.9) 7.483 (–0.1)(0.070) (–0.1)(– 0.1 + 1)
2!
-(0.000)
(–0.1)(– 0.1 + 1)(– 0.1 + 2)
3!
(–0.001)= 7.476
+ +
+
=
1.219 – 1.112
1.2 – 1.1
= 1.070 1.636 – 1.219
1.5 – 1.2
= 1.390 2.054 – 1.636
1.7 – 1.5
= 2.090
2.323 – 2.054
1.8 – 1.7
= 2.690 3.011 – 2.323
2.0 – 1.8
= 3.440
1.390 – 1.070
1.5 – 1.1
= 0.800 2.090 – 1.390
1.7 – 1.2
= 1.400
2.690 – 2.090
1.8 – 1.5
= 2.000 3.440 – 2.690
2.0 – 1.7
= 2.500
numerical analysis using matlab and spreadsheets, second edition 7-49
orchard publications
solutions to exercises
and the third divided differences are:
with these values, we construct the difference table below.
to find . we start with and for in (7.49), we use . then,
b.to find we start with and for in (7.49), we use . then,
the spreadsheet below verifies our calculated values.
1st divided
difference
2nd divided
difference
3rd divided
difference
x
1.1 1.112
1.070
1.2 1.219 0.800
1.390 1.000
1.5 1.636 1.400
2.090 1.000
1.7 2.054 2.000
2.690 1.000
1.8 2.323 2.500
3.440
2.0 3.011
1.400 – 0.800
1.7 – 1.1
= 1.000 2.000 – 1.400
1.8 – 1.2
= 1.000 2.500 – 2.000
2.0 – 1.5
= 1.000
f (x) f (x0, x1) f (x0, x1, x2) f (x0, x1, x2, x3)
f(1.3) x0 = 1.1 x x = 1.3
f(x) = f(x0) + (x – x0) f (x0, x1) + (x – x0)(x – x1) f (x0, x1, x2)
+ (x – x0)(x – x1)(x – x2) f (x0, x1, x2, x3)
f(1.3) = 1.112 + (1.3 – 1.1)(1.07) + (1.3 – 1.1)(1.3 – 1.2)(1.4) + (1.3 – 1.1)(1.3 – 1.2)(1.3 – 1.5)(1)
= 1.112 + 0.214 + 0.028 – 0.004
= 1.350
f(1.95) x0 = 2.0 x x = 1.95
f(x) = f(x0) + (x – x0) f (x0, x1) + (x – x0)(x – x1) f (x0, x1, x2)
+ (x – x0)(x – x1)(x – x2) f (x0, x1, x2, x3)
f(1.95) = 3.011 + (1.95 – 2)(3.44) + (1.95 – 2)(1.95 – 1.8)(2.5) + (1.95 – 2)(1.95 – 1.8)(1.95 – 1.7)(1)
= 3.011 – 0.172 – 0.019 – 0.002
= 2.818
chapter 7 finite differences and interpolation
7-50 numerical analysis using matlab and spreadsheets, second edition
orchard publications
check with matlab:
x=[ 1.1 1.2 1.5 1.7 1.8 2.0]
fx=[ 1.112 1.219 1.636 2.054 2.323 3.011]
spline_interp=interp1(x,fx,[1.3] , spline ) fprintf( \n ) ...
fprintf( spline interpolation value of f(1.3): \n\n ) disp(spline_interp)
spline interpolation value of f(1.3):
1.3380
spline_interp=interp1(x,fx,[1.95] , spline ) fprintf( \n ) ...
fprintf( spline interpolation value of f(1.95): \n\n ) disp(spline_interp)
spline interpolation value of f(1.95):
2.8184
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
a b c d e f
1st divided 2nd divided 3rd divided
difference difference difference
x f(x) f(x0, x1) f(x0, x1, x2) f(x0,x1,x2,x3)
1.1 1.112
1.070
1.2 1.219 0.800
1.390 1.000
1.5 1.636 1.400
2.090 1.000
1.7 2.054 2.000
2.690 1.000
1.8 2.323 2.500
3.440
2.0 3.011
1.1 1.112
1.2 1.219
1.5 1.636
1.7 2.054
1.8 2.323
2 3.011
1.00
1.50
2.00
2.50
3.00
3.50
1.0 1.3 1.5 1.8 2.0
x
f(x)
numerical analysis using matlab and spreadsheets, second edition 8-1
orchard publications
chapter 8
linear and parabolic regression
his chapter is an introduction to regression and procedures for finding the best curve to fit
a set of data. we will discuss linear and parabolic regression, and regression with power
series approximations. we will illustrate their application with several examples.
8.1 curve fitting
curve fitting is the process of finding equations to approximate straight lines and curves that best
fit given sets of data. for example, for the data of figure 8.1, we can use the equation of a straight
line, that is,
(8.1)
figure 8.1. straight line approximation.
for figure 8.2, we can use the equation for the quadratic or parabolic curve of the form
(8.2)
figure 8.2. parabolic line approximation
in finding the best line, we normally assume that the data, shown by the small circles in figures
8.1 and 8.2, represent the independent variable , and our task is to find the dependent variable
t
y = mx + b
y
x
y ax2 = + bx + c
y
x
x
chapter 8 linear and parabolic regression
8-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
. this process is called regression.
regression can be linear (straight line) or curved (quadratic, cubic, etc.) and it is not restricted to
engineering applications. investment corporations use regression analysis to compare a portfolio’s
past performance versus index figures. financial analysts in large corporations use regression to
forecast future costs, and the census bureau use it for population forecasting.
obviously, we can find more than one straight line or curve to fit a set of given data, but we interested
in finding the most suitable.
let the distance of data point from the line be denoted as , the distance of data point
from the same line as , and so on. the best fitting straight line or curve has the property that
(8.3)
and it is referred to as the least-squares curve. thus, a straight line that satisfies (8.3) is called a least
squares line. if it is a parabola, we call it a least-squares parabola.
8.2 linear regression
we perform linear regression with the method of least squares. with this method, we compute the
coefficients (slope) and (y-intercept) of the straight line equation
(8.4)
such that the sum of the squares of the errors will be minimum. we derive the values of and ,
that will make the equation of the straight line to best fit the observed data, as follows:
let and be two related variables, and assume that corresponding to the values
, we have observed the values . now, let us suppose that we have
plotted the values of versus the corresponding values of , and we have observed that the points
approximate a straight line. we denote the straight line equations
passing through these points as
(8.5)
in (8.5), the slope and y-intercept are the same in all equations since we have assumed that
all points lie close to one straight line. however, we need to determine the values of the unknowns
y
x1 d1 x2
d2
d1
2 d2
2 … d3
+ + + 2 = minimum
m b
y = mx + b
m b
x y
x1, x2, x3,…, xn y1, y2, y3, …, yn
y x
(x1, y1), (x2, y2), (x3, y3), …, (xn, yn)
y1 = mx1 + b
y2 = mx2 + b
y3 = mx3 + b

yn = mxn + b
m b
numerical analysis using matlab and spreadsheets, second edition 8-3
orchard publications
linear regression
and from all equations we will not obtain valid values for all points if we solve just two
equations with two unknowns. *
the error (difference) between the observed value , and the value that lies on the straight line,
is . this difference could be positive or negative, depending on the position of the
observed value, and the value at the point on the straight line. likewise, the error between the
observed value and the value that lies on the straight line is and so on. the
straight line that we choose must be a straight line such that the distances between the observed
values, and the corresponding values on the straight line, will be minimum. this will be achieved
if we use the magnitudes (absolute values) of the distances if we were to combine positive and
negative values, some may cancel each other and give us an erroneous sum of the distances.
accordingly, we find the sum of the squared distances between observed points and the points on
the straight line. for this reason, this method is referred to as the method of least squares.
let the sum of the squares of the errors be
(8.6)
since is a function of two variables and , to minimize (8.6) we must equate to
zero its two partial derivatives with respect to and . then,
(8.7)
and
(8.8)
the second derivatives of (8.7) and (8.8) are positive and thus will have its minimum
value.
collecting like terms, and simplifying (8.7) and (8.8) we get
* a linear system of independent equations that has more equations than unknowns is said to be overdetermined and no
exact solution exists. on the contrary, a system that has more unknowns than equations is said to be underdetermined and
these systems have infinite solutions.
m b n
y1
y1 – (mx1 + b)
y2 y2 – (mx2 + b)
?squares [y1 – (mx1 + b)]2 [y2 – (mx2 + b)]2 …
[yn – (mxn + b)]2
+ +
+
=
?squares m b
m b
?m
? ?squares 2x= – 1[y1 – (mx1 + b)] – 2x2[y2 – (mx2 + b)] – …
–2xn[yn – (mxn + b)] = 0
?b
? ?squares 2 y= – [ 1 – (mx1 + b)] – 2[y2 – (mx2 + b)] – …
–2[yn – (mxn + b)] = 0
?squares
chapter 8 linear and parabolic regression
8-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(8.9)
where
we can solve the equations of (8.9) simultaneously by cramer’s rule, or with excel, or with matlab
using matrices.
with cramer’s rule, and are computed from
(8.10)
where
(8.11)
example 8.1
in a typical resistor, the resistance in (denoted as in the equations above) increases with an
increase in temperature in (denoted as ). the temperature increments and the observed
resistance values are shown in table 8-1. compute the straight line equation that best fits the
observed data.
solution:
there are sets of data and thus . for convenience, we use the spreadsheet of figure 8.3
where we enter the given values and we perform the computations using spreadsheet formulas.
table 8.1 data for example 8.1 ? resistance versus temperature
t
0 10 20 30 40 50 60 70 80 90 100
r 27.6 31.0 34.0 37 40 42.6 45.5 48.3 51.1 54 56.7
(?x2)m + (?x)b = ?xy
(?x)m + nb = ?y
?x = sum of the numbers x
?y = sum of the numbers y
?xy = sum of the numbers of the product xy
?x2 = sum of the numbers x squared
n = number of data x
m b
m
d1
?
= b
d2
?
=
? ?x2 ?x
?x n
= d1
?xy ?x
?y n
= d2
?x2 ?xy
?x ?y
=
r ? y
t °c x
c ° ( ) x
(?) y
11 n = 11
numerical analysis using matlab and spreadsheets, second edition 8-5
orchard publications
linear regression
figure 8.3. spreadsheet for example 8.1
accordingly, we enter the (temperature) values in column a, and (the measured resistance
corresponding to each temperature value) in column b. columns c and d show the and
products. then, we compute the sums so they can be used with (8.10) and (8.11). all work is
shown on the spreadsheet of figure 8.3. the values of and are shown in cells i20 and i24
respectively. thus, the straight line equation that best fits the given data is
(8.12)
we can use excel’s add trendline feature to produce quick answers to regression problems. we
will illustrate the procedure with the following example.
example 8.2
repeat example 8.1 using excel’s add trendline feature.
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
a b c d e f g h i
spreadsheet for example 8.1
x (0c) y(?) x2 xy
0 27.6 0 0
10 31.0 100 310
20 34.0 400 680
30 37.0 900 1110
40 40.0 1600 1600
50 42.6 2500 2130
60 45.5 3600 2730
70 48.3 4900 3381
80 51.1 6400 4088
90 54.0 8100 4860
100 56.7 10000 5670
550 467.8 38500 26559
?x2 ?x 38500 550
= = 121000
?x n 550 11
m=d1/?= 0.288
?xy ?x 26559 550
= = 34859
?y n 467.8 11
b=d2/?= 28.1227
?x2 ?xy 38500 26559
= = 3402850
?x ?y 550 467.8
resistance versus temperature
20.0
30.0
40.0
50.0
60.0
0 20 40 60 80 100
temperature
resistance
x y
x 2 xy
m b
y = mx + b = 0.288x + 28.123
chapter 8 linear and parabolic regression
8-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
we first enter the given data in columns a and b as shown on the spreadsheet of figure 8.4.
figure 8.4. plot of the straight line for example 10.2
to produce the plot of figure 8.4, we perform the following steps:
1. we click on the chart wizard icon. the displayed chart types appear on the standard types
tab. we click on xy (scatter) type. on the chart sub-types options, we click on the top (scatter)
sub-type. then, we click on next>next> next>finish, and we observe that the plot
appears next to the data. we click on the series 1 block inside the chart box, and we press the
deleting key to deleting it.
2. to change the plot area from gray to white, we choose plot area from the taskbar below the
main taskbar, we click on the small (with the hand) box, on the patterns tab we click on the
white box (below the selected gray box), and we click on ok. we observe now that the plot
area is white. next, we click anywhere on the perimeter of the chart area, and observe six
square handles (small black squares) around it. we click on chart on the main taskbar, and on
the gridlines tab. under the value (y) axis, we click on the major gridlines box to deselect it.
3. we click on the titles tab, and on the chart title box, we type straight line for example 8.2, on
the value x-axis, we type temperature (degrees celsius), and on the value y-axis, we type resistance
(ohms). we click anywhere on the x-axis to select it, and we click on the small (with the
hand) box. we click on the scale tab, we change the maximum value from 150 to 100, and we
click ok. we click anywhere on the y-axis to select it, and we click on the small (with the
hand) box. we click on the scale tab, we change the minimum value from 0 to 20, we change
the major unit to 10, and we click on ok.
4. to make the plot more presentable, we click anywhere on the perimeter of the chart area, and
we observe the six handles around it. we place the cursor near the center handle of the upper
123456789
10
11
12
13
14
a b c d
x (0c) y(?)
0 27.6
10 31.0
20 34.0
30 37.0
40 40.0
50 42.6
60 45.5
70 48.3
80 51.1
90 54.0
100 56.7
straight line for example 8.2
20
30
40
50
60
0 20 40 60 80 100
temperature (degrees celsius)
resistance (ohms)
numerical analysis using matlab and spreadsheets, second edition 8-7
orchard publications
parabolic regression
side of the graph, and when the two-directional arrow appears, we move it upwards by moving
the mouse in that direction. we can also stretch (or shrink) the height of the chart area by
placing the cursor near the center handle of the lower side of the graph, and move it downwards
with the mouse. similarly, we can stretch or shrink the width of the plot to the left or to
the right, by placing the cursor near the center handle of the left or right side of the chart
area.
5. we click anywhere on the perimeter of the chart area to select it, and we click on chart above
the main taskbar. on the pull-down menu, we click on add trendline. on the type tab, we
click on the first (linear), and we click on ok. we now observe that the points on the plot
have been connected by a straight line.
we can also use excel to compute and display the equation of the straight line. this feature will
be illustrated in example 8.4. the data analysis toolpack in excel includes the regression analysis
tool which performs linear regression using the least squares method. it provides a wealth of information
for statisticians, and contains several terms used in probability and statistics.
8.3 parabolic regression
we find the least-squares parabola that fits a set of sample points with
(8.13)
where the coefficients are found from
(8.14)
where = number of data points.
example 8.3
find the least-squares parabola for the data shown in table 8.2.
table 8.2 data for example 8.3
x 1.2 1.5 1.8 2.6 3.1 4.3 4.9 5.3
y 4.5 5.1 5.8 6.7 7.0 7.3 7.6 7.4
x 5.7 6.4 7.1 7.6 8.6 9.2 9.8
y 7.2 6.9 6.6 5.1 4.5 3.4 2.7
y ax2 = + b + c
a, b, and c
(?x2)a + (?x)b + nc= ?y
(?x3)a + (?x2)b + (?x)c= ?xy
(?x4)a + (?x3)b + (?x2)c= ?x2y
n
chapter 8 linear and parabolic regression
8-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
we construct the spreadsheet of figure 8.5, and from the data of columns a and b, we compute
the values shown in columns c through g. the sum values are shown in row 18, and from these
we form the coefficients of the unknown .
figure 8.5. spreadsheet for example 8.3
by substitution into (8.14),
(8.15)
we solve the equations of (8.15) with matrix inversion and multiplication, as shown in figure 8.6.
the procedure was presented in chapter 4.
figure 8.6. spreadsheet for the solution of the equations of (8.15)
a, b, and c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
a b c d e f g
x y x2 x3 x4 xy x2y
1.2 4.5 1.44 1.73 2.07 5.40 6.48
1.5 5.1 2.25 3.38 5.06 7.65 11.48
1.8 5.8 3.24 5.83 10.50 10.44 18.79
2.6 6.7 6.76 17.58 45.70 17.42 45.29
3.1 7.0 9.61 29.79 92.35 21.70 67.27
4.3 7.3 18.49 79.51 341.88 31.39 134.98
4.9 7.6 24.01 117.65 576.48 37.24 182.48
5.3 7.4 28.09 148.88 789.05 39.22 207.87
5.7 7.2 32.49 185.19 1055.60 41.04 233.93
6.4 6.9 40.96 262.14 1677.72 44.16 282.62
7.1 6.6 50.41 357.91 2541.17 46.86 332.71
7.6 5.1 57.76 438.98 3336.22 38.76 294.58
8.6 4.5 73.96 636.06 5470.08 38.70 332.82
9.2 3.4 84.64 778.69 7163.93 31.28 287.78
9.8 2.7 96.04 941.19 9223.68 26.46 259.31
?x= ?y= ?x2= ?x3= ?x4= ?xy= ?x2y=
79.1 87.8 530.15 4004.50 32331.49 437.72 2698.37
530.15a + 79.1b + 15c= 87.8
4004.50a + 530.15b + 79.1c= 437.72
32331.49a + 4004.50b + 530.15c= 2698.37
1234
567
89
a b c d e f g
matrix inversion and matrix multiplication for example 8.3
530.15 79.10 15.00 ?y= 87.80
a= 4004.50 530.15 79.10 ?xy= 437.72
32331.49 4004.50 530.15 ?x2y= 2698.37
0.032 -0.016 0.002 a= -0.20
a-1= -0.385 0.181 -0.016 b= 1.94
0.979 -0.385 0.032 c= 2.78
numerical analysis using matlab and spreadsheets, second edition 8-9
orchard publications
parabolic regression
therefore, the least-squares parabola is
the plot for this parabola is shown in figure 8.7.
figure 8.7. parabola for example 8.3
example 8.4
the voltages (volts) shown on table 8.3 were applied across the terminal of a non-linear device
and the current ma (milliamps) values were observed and recorded. use excel’s add trendline
feature to derive a polynomial that best approximates the given data.
solution:
we enter the given data on the spreadsheet of figure 8.8 where, for brevity, only a partial list of
the given data is shown. however, to obtain the plot, we need to enter all data in columns a and
b.
table 8.3 data for example 8.4
experimental data
volts 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50
ma 0.00 0.01 0.03 0.05 0.08 0.11 0.14 0.18 0.23 0.28 0.34
volts 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00
ma 0.42 0.50 0.60 0.72 0.85 1.00 1.18 1.39 1.63 1.91
y 0.20x2 = – + 1.94x + 2.78
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
c d e f g h i
y = ?0.20x2+1.94x+2.78
0
1
2
3
4
5
6
7
8
0 1 2 3 4 5 6 7 8 9 10
x
y
chapter 8 linear and parabolic regression
8-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 8.8. plot for the data of example 8.4
following the steps of example 8.2, we create the plot shown next to the data. here, the smooth
curve was chosen from the add trendline feature, but we clicked on the polynomial order 3 on the
add trendline type tab. on the options tab, we clicked on display equation on chart, we clicked on
display r squared value on chart, and on ok. the quantity is a measure of the goodness of fit
for a straight line or, as in this example, for parabolic regression. this is the pearson correlation coefficient
it is discussed in probability and statistics textbooks*. the correlation coefficient can
vary from 0 to 1. when , there is no relationship between the dependent and independent
variables. when , there is a nearly perfect relationship between these variables.
thus, the result of example 8.4 indicates that there is a strong relationship between the variables
and , that is, there is a nearly perfect fit between the cubic polynomial and the experimental
data.
with matlab, regression is performed with the polyfit(x,y,n) command, where x and y are the
coordinates of the data points, and n is the degree of the polynomial. thus, if , matlab
computes the best straight line approximation, that is, linear regression, and returns the coefficients
and . if , it computes the best quadratic polynomial approximation and returns
the coefficients of this polynomial. likewise, if , it computes the best cubic polynomial
approximation, and so on.
let denote the polynomial (linear, quadratic, cubic, or higher order) approximation that is computed
with the matlab polyfit(x,y,n) function. suppose we want to evaluate the polynomial
* it is also discussed in mathematics for business, science, and technology, orchard publications
123456789
10
11
12
13
14
a b c d
volts amps
0.00 0.00
0.25 0.01
0.50 0.03
0.75 0.05
1.00 0.08
1.25 0.11
1.50 0.14
1.75 0.18
2.00 0.23
2.25 0.28
2.50 0.34
y = 0.0182x3 - 0.0403x2 + 0.1275x - 0.0177
r2 = 0.9997
0.00
0.25
0.50
0.75
1.00
1.25
1.50
1.75
2.00
0 1 2 3 4 5
r2
r
r2 ? 0 y
x r2 ? 1
x y
n = 1
m b n = 2
n = 3
p
p
numerical analysis using matlab and spreadsheets, second edition 8-11
orchard publications
parabolic regression
at one or more points. we can use the polyval(p,x) function to evaluate the polynomial. if x is a
scalar, matlab returns the value of the polynomial at point x. if x is a row vector, the polynomial
is evaluated for all values of the vector x.
example 8.5
repeat example 8.1 using the matlab’s polyfit(x,y,n) function. use to compute the
best straight line approximation. plot resistance versus temperature in the range
. use also the polyval(p,x) command to evaluate the best line approximation p
in the range in ten degrees increments, and compute the percent error (difference
between the given values and the polynomial values).
solution:
the following matlab code will do the computations and plot the data.
% this is the code for example 8.5
%
t= [ 0 10 20 30 40 50 60 70 80 90 100] % x-axis data
r=[27.6 31 34 37 40 42.6 45.5 48.3 51.1 54 56.7] % y-axis data
axis([?10 110 20 60]) % establishes desired x and y axes limits
plot(t,r, *b ) % display experimental (given) points with asterisk
% and smoothed data with blue line
grid title( r (ohms) vs t (deg celsius, n=1 ) xlabel( t ) ylabel( r )
hold % hold current plot so we can add other data
p=polyfit(t,r,1) % fits a first degree polynomial (straight line since n =1) and returns
% the coefficients m and b of the straight line equation y = mx + b
a=0: 10: 100 % define range to plot the polynomial
q=polyval(p,a) % compute p for each value of a
plot(a,q) % plot the polynomial
% display the coefficients m and b
fprintf( \n ) % insert line
disp( coefficients m and b are: ) fprintf( \n ) disp(p)
format bank % two decimal place display will be sufficient
disp( smoothed r values evaluated from straight line are: )
r_smoothed=polyval(p,t) % compute and display the values of the fitted
% polynomial at same points as given
% (experimental) values of r
r_exper = r % display the experimental values of r for comparison
% the statement below computes the percent error between
% the fitted polynomial and the experimental data
disp( % error at points of given values is: )
% the percent error is computed with the following statement
error=(r_smoothed-r_exper).*100./r_exper
format short % return to default format
n = 1
r t
–10 ? t ? 110 °c
0 ? t ? 100
chapter 8 linear and parabolic regression
8-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the plot for the data of this example is shown in figure 8.9.
figure 8.9. plot for example 8.5
matlab also displays the following data:
coefficients m and b are:
0.2881 28.1227
smoothed r values evaluated from straight line are:
r_smoothed =
columns 1 through 5
28.12 31.00 33.88 36.77 39.65
columns 6 through 10
42.53 45.41 48.29 51.17 54.05
column 11
56.93
r_exper =
columns 1 through 5
27.60 31.00 34.00 37.00 40.00
columns 6 through 10
42.60 45.50 48.30 51.10 54.00
column 11
56.70
% error at points of given values is:
0 20 40 60 80 100
25
30
35
40
45
50
55
60
numerical analysis using matlab and spreadsheets, second edition 8-13
orchard publications
parabolic regression
error =
columns 1 through 5
1.89 0.01 -0.34 -0.63 -0.88
columns 6 through 10
-0.17 -0.20 -0.02 0.14 0.09
column 11
0.41
we can make the displayed data more presentable by displaying the values in four columns. the
following matlab code will do that and will display the error in absolute values.
t= [0 10 20 30 40 50 60 70 80 90 100] % x-axis data
r=[27.6 31.0 34.0 37.0 40.0 42.6 45.5 48.3 51.1 54.0 56.7] % y-axis data
p=polyfit(t,r,1) r_smoothed=polyval(p,t) r_exper = r
error=(r_smoothed-r_exper).*100./r_exper
y=zeros(11,4) % construct an 11 x 4 matrix of zeros
y(:,1)=t % 1st column of matrix
y(:,2)=r_exper % 2nd column of matrix
y(:,3)=r_smoothed % 3rd column of matrix
y(:,4)=abs(error) % 4th column of matrix
fprintf( \n ) % insert line
fprintf( temp \t exper r\t smoothed r \t |error| \n )
fprintf( \n ) % insert line
fprintf( %3.0f\t %5.4f\t %5.4f\t %5.4f\n ,y )
fprintf( \n ) % insert line
when this code is executed, matlab displays the following where the error is in percent.
temp exper r smoothed r |error|
0 27.6000 28.1227 1.8939
10 31.0000 31.0036 0.0117
20 34.0000 33.8845 0.3396
30 37.0000 36.7655 0.6339
40 40.0000 39.6464 0.8841
50 42.6000 42.5273 0.1707
60 45.5000 45.4082 0.2018
70 48.3000 48.2891 0.0226
80 51.1000 51.1700 0.1370
90 54.0000 54.0509 0.0943
100 56.7000 56.9318 0.4089
chapter 8 linear and parabolic regression
8-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8.4 regression with power series approximations
in cases where the observed data deviate significantly from the points of a straight line, we can
draw a smooth curve and compute the coefficients of a power series by approximating the derivatives
with finite differences . the following example illustrates the procedure.
example 8.6
the voltages (volts) shown in table 8.4, were applied across the terminal of a non-linear device,
and the current ma (milliamps) values were observed and recorded. use the power series method
to derive a polynomial that best approximates the given data.
solution:
we start by plotting the given data and we draw a smooth curve as shown in figure 8.10.
using the plot of figure 8.10 we read the voltmeter reading and the corresponding smoothed ma
readings and enter the values in table 8.5.
next, we compute for
to facilitate the computations, we enter these values in the spreadsheet of figure 8.11. in cell e4
we enter the formula =(b5-b4)/(a5-a4) and we copy it down to e5:e23.
next, we plot the computed values of versus and again we smooth the data as shown in
the spreadsheet of figure 8.12.
the smoothed values of the plot of figure 8.12 are shown in figure 8.13, and from these we compute
.
table 8.4 data for example 8.6
experimental data
volts 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50
ma 0.00 0.01 0.03 0.05 0.08 0.11 0.14 0.18 0.23 0.28 0.34
volts 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00
ma 0.42 0.50 0.60 0.72 0.85 1.00 1.18 1.39 1.63 1.91
di ? dv ?i ? ?v
?i ? ?v i = 1, 2,…20
?i ? ?v v
?2i ? ?2v
numerical analysis using matlab and spreadsheets, second edition 8-15
orchard publications
regression with power series approximations
figure 8.10. spreadsheet for example 8.6
finally, we plot versus volts and again we smooth the data as shown in figure 8.14.
table 8.5
smoothed data for computation of ?i / ?v
volts 0.00 0.25 0.50 0.75 1.00 1.25 1.50 1.75 2.00 2.25 2.50
ma ?0.02 0.01 0.04 0.06 0.09 0.11 0.14 0.18 0.22 0.27 0.33
volts 2.75 3.00 3.25 3.50 3.75 4.00 4.25 4.50 4.75 5.00
ma 0.41 0.49 0.60 0.72 0.85 1.01 1.20 1.40 1.63 1.89
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
a b c d e
experimental
volts ma
0.00 0.00
0.25 0.01
0.50 0.03
0.75 0.05
1.00 0.08
1.25 0.11
1.50 0.14
1.75 0.18
2.00 0.23
2.25 0.28
2.50 0.34
2.75 0.42
3.00 0.50
3.25 0.60
3.50 0.72
3.75 0.85 from this plot, i | v=0 = i(0) = ?0.02
4.00 1.00
4.25 1.18
4.50 1.39
4.75 1.63
5.00 1.91
smoothed experimental data
-0.20
0.00
0.20
0.40
0.60
0.80
1.00
1.20
1.40
1.60
1.80
2.00
0 1 2 3 4 5
v
ma
?2i ? ?2v
chapter 8 linear and parabolic regression
8-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 8.11. spreadsheet for computation of in example 8.6
following the same procedure we can find higher order derivatives. however, for this example we
will consider only the first three terms of the polynomial whose coefficients i, and ,
all three evaluated at and are read from the plots. therefore, the polynomial that best fits
the given data is
(8.16)
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
a b c d e
smoothed computed
volts ma ?i / ?v
0.00 -0.02 ?i1 / ?v1= (0.01-(-0.02))/(0.25-0.00)= 0.12
0.25 0.01 ?i2 / ?v2= (0.04-(0.01))/(0.25-0.00)= 0.12
0.50 0.04 … … 0.08
0.75 0.06 … … 0.12
1.00 0.09 … … 0.08
1.25 0.11 … … 0.12
1.50 0.14 … … 0.16
1.75 0.18 … … 0.16
2.00 0.22 … … 0.20
2.25 0.27 … … 0.24
2.50 0.33 … … 0.32
2.75 0.41 … … 0.32
3.00 0.49 … … 0.44
3.25 0.60 … … 0.48
3.50 0.72 … … 0.52
3.75 0.85 … … 0.64
4.00 1.01 … … 0.76
4.25 1.20 … … 0.80
4.50 1.40 … … 0.92
4.75 1.63 ?i20 / ?v20= (1.89-(1.63))/(0.25-0.00)= 1.04
5.00 1.89
?i ? ?v
?i ? ?v ?i2 ? ?v2
v = 0
i(v) i(0) i ?(0) 1
2!
= + + i ??(0) + … = – 0.02 + 0.12v + 0.5(–0.08)v2
= – 0.04v2 + 0.12v – 0.02
numerical analysis using matlab and spreadsheets, second edition 8-17
orchard publications
regression with power series approximations
figure 8.12. plot to obtain smoothed data for in example 8.6
example 8.7
repeat example 8.4 using the matlab polyfit(x,y,n) function. use to compute the best
cubic polynomial approximation.
solution:
with matlab, higher degree polynomial regression is also performed with the polyfit(x,y,n)
function, where . in this example we will use as we did with excel. the matlab
code below computes the smoothed line and produces the plot shown on figure 8.15.
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a b c
computed
volts ?i / ?v
0.00 0.12
0.25 0.12
0.50 0.08
0.75 0.12
1.00 0.08
1.25 0.12
1.50 0.16
1.75 0.16
2.00 0.20
2.25 0.24
2.50 0.32
2.75 0.32
3.00 0.44
3.25 0.48 from this plot, (?i / ?v) | v=0 = i (0) = 0.12
3.50 0.52
3.75 0.64
4.00 0.76
4.25 0.80
4.50 0.92
4.75 1.04
smoothed ?i / ?v
0.0
0.2
0.4
0.6
0.8
1.0
1.2
0 1 2 3 4 5
volts
?? / ?v
?i ? ?v
n = 3
n ? 2 n = 3
chapter 8 linear and parabolic regression
8-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 8.13. spreadsheet for computation of in example 8.6
v=[0 0.25 0.5 0.75 1 1.25 1.5 1.75 2 2.25 2.5 2.75 3....
3.25 3.5 3.75 4 4.25 4.5 4.75 5] % x-axis data
ma=[0 0.01 0.03 0.05 0.08 0.11 0.14 0.18 0.23 0.28....
0.34 0.42 0.50 0.60 0.72 0.85 1.00 1.18 1.39 1.63 1.91] % y-axis data
axis([?1 6 ?1 2]) % establishes desired x and y axes limits
plot(v,ma, +r ) grid % indicate data points with + and straight line in red
%
hold % hold current plot so we can add other data
disp( polynomial coefficients in descending order are: )
%
p=polyfit(v,ma,3) % fits a third degree polynomial to
% the data and returns the coefficients
% of the polynomial (cubic equation for
% this example since n=3)
a=0:0.25:5 % define range to plot the polynomial
q=polyval(p,a) % calculate p at each value of a
% continued on the next page
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
a b c d e
smoothed computed
volts ?i/?v ?i2 / ?v2
0.00 0.12 ?i2
1 / ?v2
1= (0.11-0.12)/(0.25-0.00)= -0.04
0.25 0.11 ?i2
2 / ?v2
2= (0.10-011)/(0.25-0.00)= -0.04
0.50 0.10 … … 0.00
0.75 0.10 … … 0.04
1.00 0.11 … … 0.04
1.25 0.12 … … 0.08
1.50 0.14 … … 0.12
1.75 0.17 … … 0.12
2.00 0.20 … … 0.16
2.25 0.24 … … 0.20
2.50 0.29 … … 0.24
2.75 0.35 … … 0.24
3.00 0.41 … … 0.28
3.25 0.48 … … 0.32
3.50 0.56 … … 0.32
3.75 0.64 … … 0.36
4.00 0.73 … … 0.36
4.25 0.82 … … 0.40
4.50 0.92 … … 0.44
4.75 1.03 ?i2
20 / ?v2
20= (1.03-0.92)/(0.25-0.00)= 0.44
5.00
?i2 ? ?v2
numerical analysis using matlab and spreadsheets, second edition 8-19
orchard publications
regression with power series approximations
figure 8.14. plot to obtain smoothed data of in example 8.6
%
plot(a,q) title( milliamps vs volts, n=3 ) ...
xlabel( v ) ylabel( ma ) % plot the polynomial
% display actual, smoothed and % error values
ma_smooth=polyval(p,v) % calculate the values of the fitted polynomial
ma_exper = ma
% the following statement computes the percent error between the
% smoothed polynomial and the experimental (given) data
error=(ma_smooth-ma_exper).*100./(ma_exper+eps)
%
y=zeros(21,4) % construct a 21 x 4 matrix of zeros
y(:,1)=v % 1st column of matrix
y(:,2)=ma_exper % 2nd column of matrix
y(:,3)=ma_smooth % 3rd column of matrix
y(:,4)=abs(error) % 4th column of matrix
fprintf( \n ) % insert line
% continued on the next page
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
a b c
computed
volts ?i2 / ?v2
0.00 -0.04
0.25 -0.04
0.50 0.00
0.75 0.04
1.00 0.04
1.25 0.08
1.50 0.12
1.75 0.12
2.00 0.16
2.25 0.20
2.50 0.24
2.75 0.24
3.00 0.28
3.25 0.32
3.50 0.32
3.75 0.36
4.00 0.36 from this plot, (?i2 / ?v2) | v=0 = i (0) = ?0.08
4.25 0.40
4.50 0.44
smoothed ?i2 / ?v2
-0.1
0.0
0.1
0.2
0.3
0.4
0.5
0 1 2 3 4 5
v
??2 / ?v2
?i2 ? ?v2
chapter 8 linear and parabolic regression
8-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 8.15. plot for example 8.7
fprintf( volts \t exper ma\t smoothed ma \t |%%error| \n )
fprintf( \n )
fprintf( %3.2f\t %7.5f\t %7.5f\t %7.5f\n ,y )
fprintf( \n )
matlab computes and displays the following data.
polynomial coefficients in descending order are:
p =
0.0182 -0.0403 0.1275 -0.0177
volts exper ma smoothed ma |%error|
0.00 0.00000 -0.01766 7955257388080461.00000
0.25 0.01000 0.01197 19.74402
0.50 0.03000 0.03828 27.61614
0.75 0.05000 0.06298 25.95052
1.00 0.08000 0.08775 9.69226
1.25 0.11000 0.11433 3.93513
1.50 0.14000 0.14441 3.14852
1.75 0.18000 0.17970 0.16677
2.00 0.23000 0.22191 3.51632
2.25 0.28000 0.27275 2.58785
2.50 0.34000 0.33393 1.78451
2.75 0.42000 0.40716 3.05797
3.00 0.50000 0.49413 1.17324
3.25 0.60000 0.59657 0.57123
numerical analysis using matlab and spreadsheets, second edition 8-21
orchard publications
regression with power series approximations
3.50 0.72000 0.71618 0.53040
3.75 0.85000 0.85467 0.54911
4.00 1.00000 1.01374 1.37399
4.25 1.18000 1.19511 1.28020
4.50 1.39000 1.40048 0.75362
4.75 1.63000 1.63155 0.09538
5.00 1.91000 1.89005 1.04436
we will conclude this chapter with one more example to illustrate the uses of the matlab
polyfit(x,y,n) and polyval(p,x) functions.
example 8.8
use matlab to
a. plot the function
(8.17)
in the interval radians.
b. compute
c. plot versus for these values and use the matlab polyfit(x,y,n) and polyval(p,x) functions
to find a suitable polynomial that best fits the and data.
solution:
a. the fplot function below plots . we added eps to avoid division by zero.
fplot( sin(x)./(x+eps) ,[0 16 ?0.5 2]) grid ...
title( (sinx)/x curve for x > 0 )
the plot for the function of (8.17) is shown in figure 8.16.
b. we use the code below to evaluate at the specified points.
x=0:2:16 y=sin(x)./(x+eps)
y =
0 0.4546 -0.1892 -0.0466 0.1237 -0.0544 -0.0447 0.0708 -0.0180
c. the code for finding a suitable polynomial is listed below.
x=[0 2 4 6 8 10 12 14 16]
y=[1 0.4546 ?0.1892 ?0.0466 0.1237 ?0.0544 ?0.0447 0.0708 ?0.0180]
p5=polyfit(x,y,5) % fits the polynomial to the data
p7=polyfit(x,y,7) % of x and y with fifth, seventh,
p9=polyfit(x,y,9) % and ninth degree polynomials
% continued on the next page
y = sinx ? x
0 ? x ? 16
y(0), y(2), y(4), y(6), y(8), y(10), y(12), y(14), y(16)
y x
x y
y = sinx ? x
y
chapter 8 linear and parabolic regression
8-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 8.16. plot for example 8.8
x_span=0: 0.1: 16 % specifies values for x-axis
p5_pol=polyval(p5, x_span) % compute the polynomials for this range of x values.
p7_pol=polyval(p7, x_span) p9_pol=polyval(p9, x_span)
plot(x_span,p5_pol, , x_span,p7_pol, -. , x_span,p9_pol, - ,x,y, * )
% the following two statements establish coordinates for three legends
% in x and y directions to indicate degree of polynomials
x_ref=[2 5.3] y_ref=[1.3,1.3]
hold on
% the following are line legends for each curve
plot(x_ref,y_ref, ,x_ref,y_ref-0.2, -. ,x_ref,y_ref-0.4, - )
% the following are text legends for each curve
text(5.5,1.3, 5th degree polynomial )
text(5.5,1.1, 7th degree polynomial )
text(5.5,0.9, 9th degree polynomial ) grid
hold off
format short e % exponential short format
disp( the coefficients of 5th order polynomial in descending order are: )
p5_coef=polyfit(x,y,5)
disp( the coefficients of 7th order polynomial in descending order are: )
p7_coef=polyfit(x,y,7)
disp( the coefficients of 9th order polynomial in descending order are: )
p9_coef=polyfit(x,y,9)
format short % we could just type format only since it is the default
the 5th, 7th, and 9th order polynomials are shown in figure 8.17.
numerical analysis using matlab and spreadsheets, second edition 8-23
orchard publications
regression with power series approximations
figure 8.17. polynomials for example 8.8
the coefficients of the 5th, 7th, and 9th order polynomials are shown below.
the coefficients of 5th order polynomial in descending order are:
p5_coef =
6.5865e-006 -1.4318e-004 -1.5825e-003
6.0067e-002 -4.6529e-001 1.0293e+000
the coefficients of 7th order polynomial in descending order are:
p7_coef =
columns 1 through 6
2.6483e-006 -1.6672e-004 4.1644e-003
-5.2092e-002 3.3560e-001 -9.9165e-001
columns 7 through 8
7.2508e-001 9.9965e-001
the coefficients of 9th order polynomial in descending order are:
p9_coef =
columns 1 through 6
-1.0444e-008 1.1923e-006 -4.8340e-005
9.5032e-004 -9.7650e-003 4.9437e-002
columns 7 through 10
-8.4572e-002 -1.0057e-001 0 1.0000e+000
chapter 8 linear and parabolic regression
8-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8.5 summary
• curve fitting is the process of finding equations to approximate straight lines and curves that
best fit given sets of data.
• regression is the process of finding the dependent variable from some data of the independent
variable . regression can be linear (straight line) or curved (quadratic, cubic, etc.)
• the best fitting straight line or curve has the property that and it
is referred to as the least-squares curve. a straight line that satisfies this property is called a least
squares line. if it is a parabola, we call it a least-squares parabola.
• we perform linear regression with the method of least squares. with this method, we compute
the coefficients (slope) and (y-intercept) of the straight line equation such
that the sum of the squares of the errors will be minimum. the values of and can be found
from the relations
where
,
,
the values of and are computed from
where
• we find the least-squares parabola that fits a set of sample points with where the
coefficients are found from
where = number of data points.
y
x
d1
2 d2
2 … d3
+ + + 2 = minimum
m b y = mx + b
m b
(?x2)m + (?x)b = ?xy
(?x)m + nb = ?y
?x = sum of the numbers x ?y = sum of the numbers y
?xy = sum of the numbers of the product xy ?x2 = sum of the numbers x squared
n = number of data x
m b
m
d1
?
= b
d2
?
=
? ?x2 ?x
?x n
= d1
?xy ?x
?y n
= d2
?x2 ?xy
?x ?y
=
y ax2 = + b + c
a, b, and c
(?x2)a + (?x)b + nc= ?y
(?x3)a + (?x2)b + (?x)c= ?xy
(?x4)a + (?x3)b + (?x2)c= ?x2y
n
numerical analysis using matlab and spreadsheets, second edition 8-25
orchard publications
summary
• with matlab, regression is performed with the polyfit(x,y,n) command, where x and y are
the coordinates of the data points, and n is the degree of the polynomial. thus, if ,
matlab computes the best straight line approximation, that is, linear regression, and returns
the coefficients and . if , it computes the best quadratic polynomial approximation
and returns the coefficients of this polynomial. likewise, if , it computes the best cubic
polynomial approximation, and so on.
• in cases where the observed data deviate significantly from the points of a straight line, we can
draw a smooth curve and compute the coefficients of a power series by approximating the
derivatives with finite differences .
n = 1
m b n = 2
n = 3
dy ? dx ?y ? ?x
chapter 8 linear and parabolic regression
8-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8.6 exercises
1. consider the system of equations below derived from some experimental data.
using the relations (8.10) and (8.11), find the values of and that best fit this system of
equations.
2. in a non-linear device, measurements yielded the following sets of values:
use the procedure of example 8.1 to compute the straight line equation that best fits the given
data.
3. repeat exercise 8.2 using excel’s trendline feature.
4. repeat exercise 8.2 using the matlab’s polyfit(x,y,n) and polyval(p,x) functions.
5. a sales manager wishes to forecast sales for the next three years for a company that has been in
business for the past 15 years. the sales during these years are shown on the next page.
millivolts 100 120 140 160 180 200
milliamps 0.45 0.55 0.60 0.70 0.80 0.85
2x + y = –1
x – 3y = –4
x + 4y = 3
3x – 2y = –6
– x + 2y = 3
x + 3y = 2
x y
numerical analysis using matlab and spreadsheets, second edition 8-27
orchard publications
exercises
using excel’s trendline feature, choose an appropriate polynomial to smooth the given data
and using the polynomial found, compute the sales for the next three years. you may round
the sales to the nearest thousand.
6. repeat exercise 8.5 using the matlab polyfit(x,y,n) and polyval(p,x) functions.
year sales
1 $9,149,548
2 13,048,745
3 19,147,687
4 28,873,127
5 39,163,784
6 54,545,369
7 72,456,782
8 89,547,216
9 112,642,574
10 130,456,321
11 148,678,983
12 176,453,837
13 207,547,632
14 206,147,352
15 204,456,987
chapter 8 linear and parabolic regression
8-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8.7 solutions to exercises
1. we construct the spreadsheet below by entering the given values and computing the values
from the formulas given.
thus, and
12
3456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
a b c d e f g h i j
spreadsheet for exercise 8.1
a b c a2 ab b2 ac bc
2 1 -1 4 2 1 -2 -1
1 -3 -4 1 -3 9 -4 12
1 4 3 1 4 16 3 12
3 -2 -6 9 -6 4 -18 12
-1 2 3 1 -2 4 -3 6
1 3 2 1 3 9 2 6
? 7 5 -3 17 -2 43 -22 47
?a2 ?ab 17 -2
? = = 727
?ab ?b2 -2 43
x=d1/?= -1.172
?ac ?ab -22 -2
d1 = = -852
?bc ?b2 47 43
y=d2/?= 1.039
?a2 ?ac 17 -22
d2 = = 755
?ab ?bc -2 47
x = –1.172 y = 1.039
numerical analysis using matlab and spreadsheets, second edition 8-29
orchard publications
solutions to exercises
2. we construct the spreadsheet below by entering the given values and computing the values
from the formulas given.
thus,
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
a b c d e f g h i
spreadsheet for exercise 8.2
x (mv) y(ma) x2 xy
100 0.45 10000 45
120 0.55 14400 66
140 0.60 19600 84
160 0.70 25600 112
180 0.80 32400 144
200 0.85 40000 170
900 3.95 142000 621
?x2 ?x 142000 900
= = 42000
?x n 900 6
m=d1/?= 0.004
?xy ?x 621 900
= = 171
?y n 4.0 6
b=d2/?= 0.04762
?x2 ?xy 142000 621
= = 2000
?x ?y 900 4.0
milliamps versus millivolts
0.40
0.60
0.80
1.00
100 120 140 160 180 200
millivolts
milliamps
y = mx + b = 0.004x + 0.0476
chapter 8 linear and parabolic regression
8-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3. following the procedure of example 12.2, we get the trendline shown below.
4.
mv= [100 120 140 160 180 200] % x-axis data
ma=[0.45 0.55 0.60 0.70 0.80 0.85] % y-axis data
axis([100 200 0 1]) % establishes desired x and y axes limits
plot(mv,ma, *b ) % display experimental (given) points with
% asterisk and smoothed data with blue line
grid title( ma (milliamps) vs mv (millivolts, n=1 ) xlabel( mv ) ylabel( ma )
hold % hold current plot so we can add other data
p=polyfit(mv,ma,1) % fits a first degree polynomial (straight line since n =1) and returns
% the coefficients m and b of the straight line equation y = mx + b
a=0: 10: 200 % define range to plot the polynomial
q=polyval(p,a) % compute p for each value of a
plot(a,q) % plot the polynomial
% display the coefficients m and b
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
a b c d e f g h i
trendline for exercise 8.3
x (mv) y(ma) x2 xy
100 0.45 10000 45
120 0.55 14400 66
140 0.60 19600 84
160 0.70 25600 112
180 0.80 32400 144
200 0.85 40000 170
900 3.95 142000 621
?x2 ?x 142000 900
= = 42000
?x n 900 6
m=d1/?= 0.004
?xy ?x 621 900
= = 171
?y n 4.0 6
b=d2/?= 0.04762
?x2 ?xy 142000 621
= = 2000
?x ?y 900 4.0
milliamps versus millivolts
0.40
0.60
0.80
1.00
100 120 140 160 180 200
millivolts
milliamps
numerical analysis using matlab and spreadsheets, second edition 8-31
orchard publications
solutions to exercises
fprintf( \n ) % insert line
disp( coefficients m and b are: ) fprintf( \n ) disp(p)
format bank % two decimal place display will be sufficient
ma_smoothed=polyval(p,mv) % compute the values of the fitted polynomial at
% same points as given (experimental) values of ma
ma_exper = ma % display the experimental values of ma for comparison
% the statement below computes the percent error between
% the fitted polynomial and the experimental data
% disp( % error at points of given values is: )
% the percent error is computed with the following statement
error=(ma_smoothed-ma_exper).*100./ma_exper
format short % return to default format
y=zeros(6,4) % construct an 6 x 4 matrix of zeros
y(:,1)=mv % 1st column of matrix
y(:,2)=ma_exper % 2nd column of matrix
y(:,3)=ma_smoothed % 3rd column of matrix
y(:,4)=abs(error) % 4th column of matrix
fprintf( \n ) % insert line
fprintf( mv \t exper ma\t smoothed ma \t |error| percent \n )
fprintf( \n ) % insert line
fprintf( %3.0f\t %5.4f\t %5.4f\t %5.4f\n ,y )
fprintf( \n ) % insert line
coefficients m and b are:
0.0041 0.0476
mv exper ma smoothed ma |error| percent
100 0.4500 0.4548 1.0582
120 0.5500 0.5362 2.5108
140 0.6000 0.6176 2.9365
160 0.7000 0.6990 0.1361
180 0.8000 0.7805 2.4405
200 0.8500 0.8619 1.4006
chapter 8 linear and parabolic regression
8-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
5. following the procedure of example 8.4, we choose polynomial 4 and we get the trendline
shown below.
the sales for the next 3 years are from the equation above produced by excel.
these results indicate that non-linear interpolation is, in most cases, unreliable. we will
compare these values with the results of exercise 6.
1 9149548
2 13048745
3 19147687
4 28873127
5 39163784
6 54545369
7 72456782
8 89547216
9 112642574
10 130456321
11 148678983
12 176453837
13 207547632
14 206147352
15 204456987
y = -17797x4 + 436354x3 - 2e+06x2 + 1e+07x -
2e+06
r2 = 0.9966
8000000
58000000
108000000
158000000
208000000
258000000
0 5 10 15 20
y16 17797x4 – 436354x3 2 106
x2 × – 107x 2 106
+ + – × x = 16 = = 266961792
y17 17797x4 – 436354x3 2 106
x2 × – 107x 2 106
+ + – × x = 17 = = 247383965
y18 17797x4 – 436354x3 2 106
x2 × – 107x 2 106
+ + – × x = 18 = = 206558656
numerical analysis using matlab and spreadsheets, second edition 8-33
orchard publications
solutions to exercises
6.
year= [1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] % x-axis data
sales=[9149548 13048745 19147687 28873127 39163784 ...
54545369 72456782 89547216 112642574 130456321 ...
148678983 176453837 207547632 206147352 204456987] % y-axis data
plot(year,sales, *b ) % display experimental (given) points with
% asterisk and smoothed data with blue line
hold % hold current plot so we can add other data
grid title( yearly sales vs years, n=4 ) xlabel( years ) ylabel( yearly sales )
p=polyfit(year,sales,4) % fits a first degree polynomial (n=4) and returns
% the coefficients of the polynomial
a=linspace(0, 15, 15) % define range to plot the polynomial
q=polyval(p,a) % compute p for each value of a
plot(a,q) % plot the polynomial
% display coefficients ofpolynomial
fprintf( \n ) % insert line
disp( coefficients are: ) fprintf( \n ) disp(p)
sales_smoothed=polyval(p,year) % compute the values of the fitted polynomial at
% same points as given (experimental) values of ma
sales_exper = sales % display the experimental values of ma for comparison
% the statement below computes the percent error between
% the fitted polynomial and the experimental data
% the percent error is computed with the following statement
error=(sales_smoothed-sales_exper).*100./sales_exper
y=zeros(15,4) % construct an 15 x 4 matrix of zeros
y(:,1)=year % 1st column of matrix
y(:,2)=sales_exper % 2nd column of matrix
y(:,3)=sales_smoothed % 3rd column of matrix
y(:,4)=abs(error) % 4th column of matrix
fprintf( \n )
fprintf( year\t exper sales\t smoothed sales \t |error| percent \n )
fprintf( \n )
fprintf( %2.0f\t %9.0f\t %9.0f\t %5.2f\n ,y )
fprintf( \n )
coefficients are:
1.0e+007 *
-0.0018 0.0436 -0.2386 1.1641 -0.2415
chapter 8 linear and parabolic regression
8-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
year exper sales smoothed sales |error| percent
1 9149548 7258461 20.67
2 13048745 14529217 11.35
3 19147687 21374599 11.63
4 28873127 29344934 1.63
5 39163784 39563426 1.02
6 54545369 52726163 3.34
7 72456782 69102111 4.63
8 89547216 88533118 1.13
9 112642574 110433913 1.96
10 130456321 133792104 2.56
11 148678983 157168183 5.71
12 176453837 178695519 1.27
13 207547632 196080363 5.53
14 206147352 206601848 0.22
15 204456987 207111986 1.30
from the coefficients produced by matlab, shown on the previous page, we form the polynomial
and from it we find the values of (the yearly sales) as follows:
x=16 y16=?1.8*10^4*x^4+4.36*10^5*x^3?2.386*10^6*x^2+1.1641*10^7*x?2.415*10^6
x=17 y17=?1.8*10^4*x^4+4.36*10^5*x^3?2.386*10^6*x^2+1.1641*10^7*x?2.415*10^6
x=18 y18=?1.8*10^4*x^4+4.36*10^5*x^3?2.386*10^6*x^2+1.1641*10^7*x?2.415*10^6
y 1.8 104 x4 × – 4.36 105x3 × 2.386 106 × x2 – 1.1641 107x 2.415 106
= + + × – ×
y
numerical analysis using matlab and spreadsheets, second edition 8-35
orchard publications
solutions to exercises
y16, y17, y18
y16 =
1.7923e+008
y17 =
1.4462e+008
y18 =
8.7243e+007
these values vary significantly from those of exercise 5. as stated above, non-linear interpolation
especially for polynomials of fourth degree and higher give inaccurate results. we should
remember that the equations produced by both excel and matlab represent the equations
that best fit the experimental values. for extrapolation, linear regression gives the best
approximations.
chapter 8 linear and parabolic regression
8-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 9-1
orchard publications
chapter 9
solution of differential equations by numerical methods
his chapter is an introduction to several methods that can be used to obtain approximate
solutions of differential equations. such approximations are necessary when no exact solution
can be found. the taylor, runge-kutta, adams’, and milne’s methods are discussed.
9.1 taylor series method
we recall from chapter 6 that the taylor series expansion about point is
(9.1)
now, if is a value close to , we can find the approximate value of by using the
first terms in the taylor expansion of about . letting in (9.1), we
get:
(9.2)
obviously, to minimize the error we need to keep sufficiently small.
for another value , close to , we repeat the procedure with then,
(9.3)
in general,
(9.4)
example 9.1
use the taylor series method to obtain a solution of
(9.5)
correct to four decimal places for values , , , , , and
with the initial condition .
t
a
yn= f(x) f(a) f ?(a)(x – a) f ??(a)
2!
-(x – a)2 … f (n) (a)
n!
= + + + + -(x – a)n
x1 > a a y1 f (x1)
k + 1 f (x1) x = a h1 = x – a
y1 y0 y 0?h1
1
2!
y0 ??h1 2 1
3!
y0 ???h1 3 1
4!
y0
(4)h1 4 = + + + + +…
f (x1) – y1 h1
x2 > x1 x1 h2 = x2 – x1
y2 y1 y 1?h2
1
2!
y1 ??h2 2 1
3!
y1 ???h2 3 1
4!
y1
(4)h2 4 = + + + + +…
yi + 1 yi y i?hi + 1
1
2!
y ?i?hi + 1
2 1
3!
y i???hi + 1
3 1
4!
yi
(4)hi + 1
= + + + + 4 + …
y ? = –xy
x0 = 0.0 x1 = 0.1 x2 = 0.2 x3 = 0.3 x4 = 0.4
x5 = 0.5 y(0) = 1
chapter 9 solution of differential equations by numerical methods
9-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
for this example,
and by substitution into (9.4),
(9.6)
for and .
the first through the fourth derivatives of (9.5) are:
(9.7)
we use the subscript to express them as
(9.8)
where represents , , , , and .
using the values of the coefficients of in (9.8), we construct the spreadsheet of figure 9.1.
figure 9.1. spreadsheet for example 9.1
h = x1 – x0 = 0.1 – 0.0 = 0.1
yi + 1 yi 0.1y ?i 0.005yi ?? 0.000167yi ??? 0.000004yi
(4) = + + + +
i = 0, 1, 2, 3, 4
y ? = –xy
y ?? = – xy ? – y = – x(–xy) – y = (x2 – 1)y
y ??? = (x2 – 1)y ? + 2xy = (x2 – 1)(–xy)2xy = (– x3 + 3x)y
y(4) = (– x3 + 3x)(–xy) + (– 3x2 + 3)y = (x4 – 6x2 + 3)y
i
y i? = –xiyi
y ?i? xi
( 2 – 1)y= i
y i??? xi
– 3 3x= ( + i)yi
yi
(4) xi
4 6xi
( – 2 + 3)y= i
xi x0 = 0.0 x1 = 0.1 x2 = 0.2 x3 = 0.3 x4 = 0.4
yi
123
4
56789 10
a b c d e f g h
differential equation is y = ?xy
numerical solution by taylor method follows
xi xi
2 xi
3 xi
4 -xi xi
2-1 -xi
3+3xi xi
4-6xi
2+3
0.0 0.00 0.0000 0.0000 0.0 -1.00 0.000 3.0000
0.1 0.01 0.0010 0.0001 -0.1 -0.99 0.299 2.9401
0.2 0.04 0.0080 0.0016 -0.2 -0.96 0.592 2.7616
0.3 0.09 0.0270 0.0081 -0.3 -0.91 0.873 2.4681
0.4 0.16 0.0640 0.0256 -0.4 -0.84 1.136 2.0656
numerical analysis using matlab and spreadsheets, second edition 9-3
orchard publications
taylor series method
the values in e6:e10, f6:f10, g6:g10, and h6:h10 of the spreadsheet of figure 9.1, are now
substituted into (9.8), and we obtain the following relations:
(9.9)
(9.10)
(9.11)
(9.12)
by substitution of (9.9) through (9.12) into (9.6), and using the given initial condition ,
we get:
y 0? = –x0 y0 = –0y0 = 0
y 1? = –x1 y1 = –0.1y1
y 2? = –x2 y2 = –0.2y1
y 3? = –x3 y3 = –0.3y1
y 4? = –x4 y4 = –0.4y1
y0
?? x0 2 = ( – 1) y0 = –y0
y1
?? x1 2 = ( – 1) y1 = –0.99y1
y2
?? x2 2 = ( – 1) y2 = –0.96y2
y3
?? x3 2 = ( – 1) y3 = –0.91y3
y4
?? x4 2 = ( – 1) y4 = –0.84y1
y0 ??? x0 3 = (– + 3x0) y0 = 0
y1 ??? x1 3 = (– + 3x1) y1 = 0.299y1
y2 ??? x2 3 = (– + 3x2) y2 = 0.592y2
y3 ??? x3 3 = (– + 3x3) y3 = 0.873y3
y4 ??? x4 3 = (– + 3x4) y4 = 1.136y4
y0
(4) x0 4 6x0 2 = ( – + 3) y0 = 3y0
y1
(4) x1 4 6x1 2 – 3 + ( ) y1 2.9401y1 = =
y2
(4) x2 4 6x2 2 = ( – + 3) y2 = 2.7616y2
y3
(4) x3 4 6x3 2 = ( – + 3) y3 = 2.4681y3
y4
(4) x4 4 6x4 2 = ( – + 3) y4 = 2.0656y4
y0 = 1
chapter 9 solution of differential equations by numerical methods
9-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(9.13)
similarly,
(9.14)
(9.15)
(9.16)
(9.17)
the differential equation of this example can be solved analytically as follows:
and with the initial condition when ,
or
(9.18)
y1 y0 0.1y 0? 0.005y ?0? 0.000167y 0??? 0.000004y0
(4) = + + + +
= 1 + 0.1(0) + 0.005(–1) + 0.00167(0) + 0.000004(3)
= 1 – 0.005 + 0.000012
= 0.99501
y2 y1 0.1y 1? 0.005y ?1? 0.000167y 1??? 0.000004y1
(4) = + + + +
= (1 – 0.01 – 0.00495 + 0.00005 + 0.00001)y1
= 0.98511(0.99501)
= 0.980194
y3 y2 0.1y 2? 0.005y ?2? 0.000167y 2??? 0.000004y2
(4) = + + + +
= (1 – 0.02 – 0.0048 + 0.0001 + 0.00001)y2
= 0.97531(0.980194)
= 0.955993
y4 y3 0.1y 3? 0.005y ?3? 0.000167y 3??? 0.000004y3
(4) = + + + +
= (1 – 0.03 – 0.00455 + 0.00015 + 0.00001)y3
= 0.9656(0.955993)
= 0.923107
y5 y4 0.1y 4? 0.005y ?4? 0.000167y 4??? 0.000004y4
(4)
= + + + +
= (1 – 0.04 – 0.0042 + 0.00019 + 0.00001)y4
= 0.95600(0.923107)
= 0.88249
dy
dx
- = –xy
dy
y
- = –xdx dy
y
- ? x x d ? – = y ln 12
= – x2 + c
y = 1 x = 0
1 ln 12
0 ( ) – c + = c 0 = y ln 12
= – x2
y e–x ? 2 =
numerical analysis using matlab and spreadsheets, second edition 9-5
orchard publications
runge-kutta method
for (9.18) yields
and we observe that this value is in close agreement with the value of (9.17).
we can verify the analytical solution of example 9.1 with matlab’s dsolve(s) function using
the following code:
syms x y z
z=dsolve( dy=?x*y , y(0)=1 , x )
z =
exp(-1/2*x^2)
the procedure used in this example, can be extended to apply to a second order differential
equation
(9.19)
in this case, we need to apply the additional formula
(9.20)
9.2 runge-kutta method
the runge-kutta method is the most widely used method of solving differential equations with
numerical methods. it differs from the taylor series method in that we use values of the first
derivative of at several points instead of the values of successive derivatives at a single
point.
for a runge-kutta method of order 2, the following formulas are applicable.
(9.21)
when higher accuracy is desired, we can use order 3 or order 4. the applicable formulas are as
follows:
x5 = 0.5
y e–0.125 = = 0.8825
y ?? = f (x, y, y ?)
y ?i + 1 y ?i y ?i?h 1
2!
y ?i??h2 1
3!
yi
(4)= + + + h3 +…
f(x, y)
k1 = hf (xn , yn)
k2 = hf (xn + h, yn + h)
yn + 1 yn
12
= + (k1 + k2)
for runge-kutta method of order 2
chapter 9 solution of differential equations by numerical methods
9-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(9.22)
(9.23)
example 9.2
compute the approximate value of at from the solution of the differential equation
(9.24)
given the initial condition . use order 2, 3, and 4 runge-kutta methods with .
solution:
a. for order 2, we use (9.21). since we are given that , we start with , and .
then,
and
(9.25)
l1 = hf (xn , yn) = k1
l2 hf xn
h2
+ yn
l1
2
? , + -?
= ? ?
l3 = hf (xn + h, yn + 2l2 – l1)
yn + 1 yn
16
= + (l1 + 4l2 + l3)
for runge-kutta method of order 3
m1 = hf (xn , yn) = l1 = k1
m2 hf xn
h2
+ yn
m1
2
? , + ?
= ? ? = l2
m3 hf xn
h2
+ yn
m2
2
? , + ?
= ? ?
m4 = hf (xn + h, yn + m3)
yn + 1 yn
16
= + (m1 + 2m2 + 2m3 + m4)
for runge-kutta method of order 4
y x = 0.2 y(x)
y ? = x + y2
y(0) = 1 h = 0.2
y(0) = 1 x = 0 y = 1
k1 = hf (xn , yn) = 0.2(0 + 12) = 0.2
k2 = hf (xn + h, yn + h) = 0.2[0 + 0.2 + (1 + 0.22)] = 0.328
y1 y0
12
k1 k2 + ( ) + 1 12
= = + (0.2 + 0.328) = 1.264
numerical analysis using matlab and spreadsheets, second edition 9-7
orchard publications
runge-kutta method
b. for order 3, we use (9.22). then,
(9.26)
and
(9.27)
c. for order 4, we use (9.23). then,
(9.28)
and
(9.29)
the runge-kutta method can also be used for second order differential equations of the form
(9.30)
for second order differential equations, the pair of 3rd-order formulas* are:
* second and fourth order formulas can also be used, but these will not be discussed in this text. they can be found in differential
equations and advanced mathematics texts.
l1 = hf (xn , yn) = k1 = 0.2
l2 hf xn
h2
+ yn
l1
2
? , + -?
? ? 0.2 0 12
? + ? 0.2?
? ? 1 12
? + ? 0.2?
? ?2
= = + = 0.262
l3 = hf (xn + h, yn + 2l2 – l1) = 0.2[(0 + 0.2) + (1 + 2 × 0.262 – 0.2)2] = 0.391
y1 y0
16
l1 4l2 l3 + + ( ) + 1 16
= = + (0.2 + 4 × 0.262 + 0.391) = 1.273
m1 = hf (xn , yn) = l1 = k1 = 0.2
m2 hf xn
h2
+ yn
m1
2
? , + ?
= ? ? = l2 = 0.262
m3 hf xn
h2
+ yn
m2
2
? , + ?
? ? 0.2 0 0.2
2
- 1 0.262
2
? + -?
? ?2
= = + + = 0.276
m4 = hf (xn + h, yn + m3) = 0.2[0 + 0.2 + (1 + 0.276)2] = 0.366
y1 y0
16
= + (m1 + 2m2 + 2m3 + m4)
1 16
= + (0.2 + 2 × 0.262 + 2 × 0.276 + 0.366) = 1.274
y ?? = f (x, y, y ?)
chapter 9 solution of differential equations by numerical methods
9-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(9.31)
example 9.3
given the 2nd order non-linear differential equation
(9.32)
with the initial conditions , , compute the approximate values of and at
. use .
solution:
we are given the values of , , and we are seeking the values of and
at . we will use .
we rewrite the given equation as
(9.33)
and using (9.31) we get:
l1 = hy ?n
l1
? = hf (xn , yn , y ?n)
l2 h y ?n
l1
?
2
? + -?
= ? ?
l2
? hf xn
h2
yn
l1
2
, + -, y ?n
l1
?
2
? + + -?
= ? ?
l3 h y ?n 2l2
? l1
= ( + – ? )
l3
? hf xn h yn 2l2 – l1 y, + , ?n 2l2
? l1
= ( + + – ? )
yn + 1 yn
16
= + (l1 + 4l2 + l3)
yn + 1 ? yn ? 16
l1
? 4l2
? l3
= + ( + + ? )
for runge-kutta method of order 3
2nd order differential equation
y?? – 2y3 = 0
y(0) = 1 y ?(0) = –1 y y ?
x = 0.2 h = 0.2
x0 = 0 y0 = 0 y0
? = –1 y1 y1
?
x1 = 0.2 h = x1 – x0 = 0.2
y?? = 2y3 = 0 ? x + 2y3
numerical analysis using matlab and spreadsheets, second edition 9-9
orchard publications
runge-kutta method
(9.34)
by substitution into the last two formulas of (9.31), we get:
(9.35)
matlab has two functions for computing numerical solutions of ordinary differential equations
(ode). the first, ode23, uses second and third-order runge-kutta methods. the second,
ode45, uses fourth and fifth-order runge-kutta methods. both have the same syntax therefore,
we will use the ode23 function in our subsequent discussion.
the syntax for ode23 is ode23(‘f’,tspan,y0). the first argument, f, in single quotation marks, is
the name of the user defined matlab function. the second, tspan, defines the desired time
span of the interval over which we want to evaluate the function . the third argument,
y0, represents the initial condition or boundary point that is needed to determine a unique solution.
this function produces two outputs, a set of values and the corresponding set of values
that represent points of the function .
example 9.4
use the matlab ode23 function to find the analytical solution of the second order nonlinear
equation
(9.36)
l1 hy0
= ? = 0.2(–1) = –0.2
l1
? hf x0 y0 y0
= ( , , ? ) = 0.2(0 + 2 × 13 + 0) = 0.4
l2 h y0
? l1
?
2
? + - ?
? ? 0.2 – 1 0.4
2
? + -?
= = ? ? = –0.16
l2
? hf x0
h2
y0
l1
2
- y0
, + , ?
l1
?
2
? + + - ?
? ? 0.2 0 2 1 –0.2
2
? + ?
? ?3
= = + + 0
= 0.2 [2(1 – 0.1)3] = 0.2(1.458) = 0.2916
l3 h y0
? 2l2
? l1
= ( + – ? ) = 0.2 (– 1 + 2 × 0.2916 – 0.4) = –0.1634
l3
? hf x0 h y0 2l2 – l1 y0
, + , ? 2l2
? l1
= ( + + – ? )
= 0.2 {0 + 2[1 + 2(–0.16) – (–0.2)]3 + 0}
= 0.2[2(1 – 0.32 + 0.2)3] = 0.2[2(0.88)3] = 0.2726
y1 y0
16
l1 4l2 l3 + + ( ) + 1 16
= = + (– 0.2 + 4(0.16) – 0.1634) = 0.8328
y1
? y0
? 16
l1
? 4l2
? l3
+ ( + + ? ) – 1 16
= = + (0.4 + 4(0.2916) + 0.2726) = –0.6935
y = f (x)
x y
y = f (x)
y?? – 2y3 = 0
chapter 9 solution of differential equations by numerical methods
9-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
with the initial conditions and . then, plot the numerical solution using the
function ode23 for the tspan interval . compare values with those of example 9.3, at
points and .
solution:
if we attempt to find the analytical solution with the following matlab code
syms x y
y=dsolve( d2y=2*y^3,y(0)=1,dy(0)=?1 , x )
matlab displays the following message:
warning: explicit solution could not be found.
this warning indicates that matlab could not find a closed-form solution for this non-linear
differential equation. this is because, in general, non-linear differential equations cannot be
solved analytically, although few methods are available for special cases. these can be found in
differential equations textbooks.
the numerical solution for this non-linear differential equation is obtained and plotted with the
following code, by first writing a user defined m-file which we denote as fex9_4. the code is
shown below.
function d2y=fex9_4(x,y)
d2y=[y(2) 2*y(1)^3] % output must be a column
this file is saved as fex9_4. next, we write and execute the code below to obtain the plots for
and .
tspan=[0 1] % interval over which we want to evaluate y=f(x)
y0=[1 -1] % given initial conditions
[x,y]=ode23( fex9_4 , tspan, y0) % use 2nd and 3rd order runge-kutta
% plot numeric values with the statements below
plot(x, y(:,1), +r- , x, y(:,2), ob )
title( numerical solution for differential equation of example 9.4 ),...
xlabel( x ), ylabel( y (upper curve), yprime (lower curve) ), grid
the plots for and are shown in figure 9.2. we observe that the values at points and
, compare favorably with those that we found in example 9.3.
y(0) = 1 y ?(0) = –1
0 ? x ? 1
y(0.2) y ?(0.2)
y
y ?
y y ? y(0.2)
y ?(0.2)
numerical analysis using matlab and spreadsheets, second edition 9-11
orchard publications
runge-kutta method
figure 9.2. plot for example 9.4
example 9.5
use matlab to find the analytical solution of
(9.37)
with the initial conditions and . then, compute and plot the numerical
solution using the command ode23 along with points of the analytical solution, to verify the
accuracy of the numerical solution.
solution:
the analytical solution of (9.37) with the given initial conditions is found with matlab as follows:
syms x y
y=dsolve( x^2*d2y?x*dy-3*y=x^2*log(x), dy(1)=0, y(1)=?1 , x )
y =
1/9*(-3*x^3*log(x)-2*x^3-7)/x
y=simple(y)
y =
(-1/3*log(x)-2/9)*x^2-7/9/x
pretty(y)
2
(- 1/3 log(x) - 2/9) x - 7/9 1/x
and therefore, the analytical solution of (9.37) is
x2y??–xy ? – 3y = x2 lnx
y(1) = –1 y ?(1) = 0
chapter 9 solution of differential equations by numerical methods
9-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(9.38)
next, we create and save a user defined m-file, fex9_5.
function d2y=fex9_5(x,y) % produces the derivatives of example 9.5
% x^2*y ?x*y -3*y=x^2*log(x) where y =2nd der, y =1st der, logx=lnx
%
% we let y(1) = y and y(2)=y , then y(1) =y(2)
%
% and y(2) =y(2)/x^2+3*y(1)/x^2+log(x)
%
d2y=[y(2) y(2)/x+3*y(1)/x^2+log(x)] % output must be a column
the following matlab code computes and plots the numerical solution values for the interval
and compares these with the actual values obtained from the analytical solutions.
tspan=[1 4] % interval over which we want to evaluate y=f(x)
y0=[?1 0] % given initial conditions
[x,y]=ode23( fex9_5 , tspan, y0) % use 2nd and 3rd order runge-kutta
anal_y=((?1./3).*log(x)-2./9).*x.^2-7./(9.*x) % this is the...
% analytic solution of the 2nd order differential equation of (9.38)
anal_yprime=((-2./3).*log(x)-7./9).*x+7./(9.*x.^2) % this is the first derivative of (9.38)
% plot numeric and analytic values with the statements below
plot(x, y(:,1), + , x, anal_y, - , x, y(:,2), o , x, anal_yprime, - ),...
title( numeric and analytic solutions of differential equation of example 9.5 ),...
xlabel( x ), ylabel( y (line with +), yprime (line with o) ), grid
the numeric and analytical solutions are shown in figure 9.3.
figure 9.3. plot for example 9.5
y 13
x ln – 29
? – ?
? ?x2 7
9x
= – -
1 ? x ? 4
numerical analysis using matlab and spreadsheets, second edition 9-13
orchard publications
adams’ method
9.3 adams’ method
in this method, the step from to is performed by a formula expressed in terms of differences
of .
adams’ method uses the formula
(9.39)
where
and so on.
obviously, to form a table of differences, it is necessary to have several (4 or more) approximate
values of in addition to the given initial condition . these values can be found by
other methods such as the taylor series or runge-kutta methods.
example 9.6
given the differential equation
(9.40)
with the initial condition ,
compute the approximate values of y for by the third-order
runge-kutta method. then, find the value of corresponding to correct to three decimal
places using adams’ method.
solution:
the spreadsheet of figure 9.4 shows the results of the computations of
using the third-order runge-kutta method as in example 9.2.
next, we compute the following values to be used in adams’ formula of (9.39). these are shown
below.
yn yn + 1
f (x, y)
yn + 1 yn h fn
12
?fn
5
12
-?2fn
38
= + + + + ?3fn + …
h = xn + 1 – xn
fn = (xn, yn)
?fn fn fn – 1 = –
?2fn ?fn ?fn – 1 = –
y(x) y(0)
y ? = 2y + x
y(0) = 1
x = 0.1, 0.2, 0.3, 0.4 and 0.5
y x = 0.6
y1, y2, y3, y4, and y5
chapter 9 solution of differential equations by numerical methods
9-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and by substitution into (7.39)
(9.41)
as with the other methods, adams’ method can also be applied to second order differential equations
of the form with initial conditions and .
xn yn fn=xn+2yn ?fn ?2fn ?3fn
0.0 1.0000 2.0000
0.5534
0.1 1.2267 2.5534 0.1224
0.6758 0.0272
0.2 1.5146 3.2292 0.1496
0.8254 0.0330
0.3 1.8773 4.0546 0.1826
1.0080 0.0406
0.4 2.3313 5.0626 0.2232
1.2312
0.5 2.8969 6.2938
y6 2.8969 0.1 6.2638 12
(1.2312) 5
12
- 0.2232 ( ) 38
= + + + + (0.0406) = 3.599
y ?? = f(x, y, y ?) y(x0) = y0 y ?(x0) y ?= 0
numerical analysis using matlab and spreadsheets, second edition 9-15
orchard publications
adams’ method
figure 9.4. spreadsheet for example 9.6
123456789
10
1112
1134
15
16
1178
19
20
2212
2234
25
26
2278
29
30
3312
3334
35
36
3378
39
40
4421
4434
45
46
4478
49
50
5521
53
a b c d e f
differential equation y = x + 2y
numerical solution by runga-kutte method follows
h= x(1)-x(0)= 0.1000 x0= 0.0
x(0)= (given) 0.0000 x1= 0.1
y(0)= initial condition (given) 1.0000 x2= 0.2
x3= 0.3
x4= 0.4
l(1)= h*f(xn,yn) h*(0+2*1)= 0.2000 x5= 0.5
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.2250
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.2600
y(1)= y(0) +(l(1) + 4*l(2) + l(3))/6 1.2267
h= x(2)-x(1)= 0.1000
x(0.1)= next value x(0) + h 0.1000
y(1)= from previous computation 1.2267
l(1)= h*f(xn,yn) h*(0.1+2*1.2267)= 0.2553
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.2859
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.3286
y(2)= y(1) +(l(1) + 4*l(2) + l(3))/6 1.5146
h= x(2)-x(1)= 0.1000
x(0.2)= next value x(0) + 2*h 0.2000
y(2)= from previous computation 1.5146
l(1)= h*f(xn,yn) h*(0.2+2*1.5146)= 0.3229
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.3602
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.4124
y(3)= y(2) +(l(1) + 4*l(2) + l(3))/6 1.8773
h= x(3)-x(2)= 0.1000
x(0.3)= next value x(0) + 3*h 0.3000
y(3)= from previous computation 1.8773
l(1)= h*f(xn,yn) h*(0.3+2*1.8773)= 0.4055
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.4510
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.5148
y(4)= y(2) +(l(1) + 4*l(2) + l(3))/6 2.3313
h= x(4)-x(3)= 0.1000
x(0.4)= next value x(0) + 4*h 0.4000
y(4)= from previous computation 2.3313
l(1)= h*f(xn,yn) h*(0.3+2*1.8773)= 0.5063
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.5619
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.6398
y(5)= y(2) +(l(1) + 4*l(2) + l(3))/6 2.8969
chapter 9 solution of differential equations by numerical methods
9-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
9.4 milne’s method
milne’s method also requires prior knowledge of several values of . it uses the predictor-corrector
pair
(9.42)
and
(9.43)
the corrector formula of (9.43) serves as a check for the value
(9.44)
if and in (9.42) and (9.43) respectively, do not differ considerably, we accept
as the best approximation. if they differ significantly, we must reduce the interval .
example 9.7
use milne’s method to find the value of y corresponding to for the differential equation
(9.45)
with the initial condition .
solution:
this is the same differential equation as in example 9.6 where we found the following values:
and using the predictor formula we find
(9.46)
table 9.1 table for example 9.7
n xn yn fn=xn+2yn
2 0.2 1.5146 3.2292
3 0.3 1.8773 4.0546
4 0.4 2.3313 5.0626
5 0.5 2.8969 6.2938
y
yn + 1 yn – 3
43
h 2fn
fn – 1 – 2fn – 2 = + [ + ]
yn + 1 yn – 1
13
h fn + 1 4fn fn – 1 = + [ + + ]
yn + 1 f xn + 1 yn + 1 = ( , )
yn + 1 yn + 1 yn + 1
h
x = 0.6
y ? = 2y + x
y(0) = 1
y6 y2
43
= + (0.1)[2f5 – f4 + 2f3]
1.5146 43
= + × 0.1(2 × 6.2938 – 5.0626 + 2 × 4.0546) = 3.599
numerical analysis using matlab and spreadsheets, second edition 9-17
orchard publications
summary
before we use the corrector formula of (9.43), we must find the value of this is found from
where , and from example 9.5 . then,
and
(9.47)
we see from (9.46) and (9.47) that the predictor-corrector pair is in very close agreement.
milne’s method can also be extended to second order differential equations of the form
with initial conditions and .
9.5 summary
• the taylor series method uses values of successive derivatives at a single point. we can use
this series method to obtain approximate solutions of differential equations with the relation
provided that is sufficiently small such as .
• the taylor series method can also be extended to apply to a second order differential equation
using the relation
• the runge-kutta method uses values of the first derivative of at several points. it is
the most widely used method of solving differential equations using numerical methods.
• for a runge-kutta method of order 2 we use the relations
provided that is sufficiently small such as .
f6
f6 = x6 + 2y6
x6 = 0.6 y6 = 3.599
f6 = x6 + 2y6 = 0.6 + 2 × 3.599 = 7.7984
y6 y4
13
= + 0.1(f6 + 4f5 + f4)
2.3313 13
= + × 0.1(7.7984 + 4 × 6.2938 + 5.0626) = 3.599
y ?? = f(x, y, y ?) y(x0) = y0 y ?(x0) y ?= 0
yi + 1 yi y i?hi + 1
1
2!
y ?i?hi + 1
2 1
3!
y i???hi + 1
3 1
4!
yi
(4)hi + 1
= + + + + 4 + …
h h = 0.1
y ?? = f (x, y, y ?)
y ?i + 1 y ?i y ?i?h 1
2!
y ?i??h2 1
3!
yi
(4)= + + + h3 +…
f(x, y)
k1 = hf (xn , yn) k2 = hf (xn + h, yn + h) yn + 1 yn
12
= + (k1 + k2)
h h = 0.1
chapter 9 solution of differential equations by numerical methods
9-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• for a runge-kutta method of order 3 we use the relations
• for a runge-kutta method of order 4 we use the relations
• the runge-kutta method can also be used for second order differential equations of the form
• for second order differential equations, the pair of 3rd-order relations are:
second and fourth order formulas can also be used but they were not be discussed in this text.
they can be found in differential equations texts.
• matlab has two functions for computing numerical solutions of ordinary differential equations
(ode). the first, ode23, uses second and third-order runge-kutta methods. the second,
ode45, uses fourth and fifth-order runge-kutta methods. both have the same syntax
therefore, we will use the ode23 function in our subsequent discussion.
• the syntax for ode23 is ode23(‘f’,tspan,y0). the first argument, f, in single quotation marks,
is the name of the user defined matlab function. the second, tspan, defines the desired
time span of the interval over which we want to evaluate the function . the third
argument, y0, represents the initial condition or boundary point that is needed to determine a
unique solution. this function produces two outputs, a set of values and the corresponding
set of values that represent points of the function .
l1 = hf (xn , yn) = k1 l2 hf xn
h2
+ yn
l1
2
? , + -?
= ? ? l3 = hf (xn + h, yn + 2l2 – l1)
yn + 1 yn
16
= + (l1 + 4l2 + l3)
m1 = hf (xn , yn) = l1 = k1 m2 hf xn
h2
+ yn
m1
2
? , + ?
= ? ? = l2
m3 hf xn
h2
+ yn
m2
2
? , + ?
= ? ? m4 = hf (xn + h, yn + m3)
yn + 1 yn
16
= + (m1 + 2m2 + 2m3 + m4)
y ?? = f (x, y, y ?)
l1 = hy ?n l1
? = hf (xn , yn , y ?n)
l2 h y ?n
l1
?
2
? + -?
= ? ? l2
? hf xn
h2
yn
l1
2
, + -, y ?n
l1
?
2
? + + -?
= ? ?
l3 h y ?n 2l2
? l1
= ( + – ? ) l3
? hf xn h yn 2l2 – l1 y, + , ?n 2l2
? l1
= ( + + – ? )
yn + 1 yn
16
= + (l1 + 4l2 + l3) yn + 1 ? yn ? 16
l1
? 4l2
? l3
= + ( + + ? )
y = f (x)
x
y y = f (x)
numerical analysis using matlab and spreadsheets, second edition 9-19
orchard publications
summary
• adams’ method provides the transition from to is performed by a formula expressed
in terms of differences of . this method uses the formula
where
and so on. to use this method, it is necessary to have several (4 or more) approximate values
of in addition to the given initial condition . these values can be found by other
methods such as the taylor series or runge-kutta methods.
• milne’s method also requires prior knowledge of several values of . it uses the predictor-corrector
pair
and
where is the predictor formula and is the corrector formula. the corrector formula
serves as a check for the value
if and do not differ considerably, we accept as the best approximation. if
they differ significantly, we must reduce the interval .
milne’s method can also be extended to second order differential equations of the form
with initial conditions and . the procedure for this
method was not discussed. it can be found in differential equations texts.
yn yn + 1
f (x, y)
yn + 1 yn h fn
12
?fn
5
12
-?2fn
38
= + + + + ?3fn + …
h = xn + 1 – xn
fn = (xn, yn)
?fn fn fn – 1 = –
?2fn ?fn ?fn – 1 = –
y(x) y(0)
y
yn + 1 yn – 3
43
h 2fn
fn – 1 – 2fn – 2 = + [ + ]
yn + 1 yn – 1
13
h fn + 1 4fn fn – 1 = + [ + + ]
yn + 1 yn + 1
yn + 1 f xn + 1 yn + 1 = ( , )
yn + 1 yn + 1 yn + 1
h
y ?? = f(x, y, y ?) y(x0) = y0 y ?(x0) y ?= 0
chapter 9 solution of differential equations by numerical methods
9-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
9.6 exercises
1. use the matlab ode23 function to verify the analytical solution of example 9.1.
2. construct a spreadsheet for the numerical solutions of example 9.2.
3. use matlab to find the analytical solution of
with the initial condition . then, compute and plot the numerical solution using
the matlab function ode23 along with points of the analytical solution to verify the accuracy
of the numerical solution for the interval .
4. use matlab to plot the numerical solution of the non-linear differential equation
with the initial condition using the command ode23 for the interval .
5. given the differential equation
with the initial condition and find the values of corresponding to the
values of and correct to four decimal places using the third-order runge-
kutta method. it is suggested that a spreadsheet is used to do all computations.
6. given the differential equation
compute the approximate values of and at and given that ,
, and correct to four decimal places, using the third-order runge-kutta
method. it is suggested that a spreadsheet is used to do all computations.
y ? = f(x) = 3x2
y(2) = 0.5
2 ? x ? 4
y ? = – y3 + 0.2sinx
y(0) = 0.707 0 ? x ? 10
y ? = x2–y
y(0) = 1 x0 = 0.0 y
x0 + 0.1 x0 + 0.2
y?? + y ? = xy
y y ? x0 + 0.1 x0 + 0.2 y(0) = 1
y ?(0) = –1 x0 = 0.0
numerical analysis using matlab and spreadsheets, second edition 9-21
orchard publications
solutions to exercises
9.7 solutions to exercises
1.
we write and save the following function file:
function dy = func_exer9_1(x,y)
dy = ?x*y
next, we write and execute the matlab code below.
tspan=[0 3] % interval over which we want to evaluate y=f(x)
y0=[1 ?1] % given initial conditions
[x,y]=ode23( func_exer9_1 , tspan, y0) % use 2nd and 3rd order runge-kutta
% plot numeric values with the statements below
plot(x, y(:,1), +r- , x, y(:,2), ob )
title( numeric solution of differential equation of exercise 9.1 ),...
xlabel( x ), ylabel( y (upper curve), yprime (lower curve) ), grid
the plot below shows the function y = f(x) and its derivative dy ? dx .
chapter 9 solution of differential equations by numerical methods
9-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
2.
3.
the analytical solution is found with
syms x y
y=dsolve( dy=3*x^2,y(2)=0.5 , x )
and matlab displays
y =
x^3-15/2
next we write and save the following statements as function file fexer9_3
function dy=fexer9_3(x,y)
dy=3*x^2
the matlab code for the numerical solution is as follows:
tspan=[2 4] % interval over which we want to evaluate y=f(x)
y0=7.5 % initial condition: since y=x^3-15/2 and y(2) = 0.5, it follows that y(0) = 7.5
[x,y]=ode23( fexer9_3 , tspan, y0) % use 2nd and 3rd order runge-kutta
% plot numeric values with the statements below
plot(x, y, +r- )
title( numeric solution of differential equation of exercise 9.3 ),...
xlabel( x ), ylabel( y ), grid
123456789
10
11
12
13
14
15
16
17
18
19
20
21
a b c d
differential equation y = x + y2
numerical solution by runga-kutte method follows
h= (given) 0.2000
x(0)= (given) 0.0000
y(0)= initial condition (given) 1.0000
k(1)= h*f(xn,yn) h*(0+1^2)= 0.2000
k(2)= h*f(xn+h,yn+h) h*(0+0.2+(1+0.2)^2)= 0.3280
y(1)= y(0)+0.5(k(1)+k(2)) y(0)+0.5*(d11+d12) 1.2640
l(1)= h*f(xn,yn) h*(0+1^2)= 0.2000
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+(1+0.5*l(1)^2)]= 0.2620
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+(1+2*l(2)-l(1))^2]= 0.3906
y(1)= y(0) +(l(1) + 4*l(2) + l(3))/6 1.2731
m(1)= h*f(xn,yn) h*(0+1^2)= 0.2000
m(2)= h*f(xn+0.5*h,yn+0.5*m(1)) h*[(0+0.5*h)+(1+0.5*m(1))^2]= 0.2620
m(3)= h*f(xn+0.5*h,yn+0.5*m(2)) h*[(0+0.5*h)+(1+0.5*m(2))^2]= 0.2758
m(4)= h*f(xn+h,yn+m(3)) h*[(0+h)+(1+m(3))^2]= 0.3655
y(1)= y(0) +(1/6)*(m(1) + 2*m(2)+2*m(3) + m(4)) 1.2735
numerical analysis using matlab and spreadsheets, second edition 9-23
orchard publications
solutions to exercises
4.
we write and save the following statements as function file fexer4
function dy=fexer4(x,y)
dy=?y^3+0.2*sin(x)
the matlab code and the plot for the numerical solution are as follows:
tspan=[0 10] x0=[0.707]
[x,num_x]=ode23( fexer4 ,tspan,x0) plot(x,num_x, + , x,num_x, - ),...
title( numeric solution of non-linear differential equation dy/dx=-x^3+0.2sinx ),...
xlabel( x ), ylabel( y=f(x) ), grid
5. the spreadsheet is shown on the following two pages.
chapter 9 solution of differential equations by numerical methods
9-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
continued on next page
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
a b c d e f
differential equation is y = x2 - y
numerical solution by runga-kutte method follows
h= x(1)-x(0)= 0.1000 x0= 0.0
x(0)= (given) 0.0000 x1= 0.1
y(0)= initial condition (given) 1.0000 x2= 0.2
l(1)= h*f(xn,yn) h*(0-1)= -0.1000
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)^2-(1+0.5*l(1))]= -0.0948
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)^2-(1+2*l(2)-l(1))]= -0.0901
y(1)= y(0) +(l(1) + 4*l(2) + l(3))/6 0.9052
h= x(2)-x(1)= 0.1000
x(0.1)= next value x(0) + h 0.1000
y(1)= from previous computation 0.9052
l(1)= h*f(xn,yn) h*(0.1^2-0.9052)= -0.0895
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)^2-(0.9052+0.5*l(1))]= -0.0838
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0.1+h)^2-(0.9052+2*l(2)-l(1))]= -0.0787
y(2)= y(1) +(l(1) + 4*l(2) + l(3))/6 0.8213
h= x(2)-x(1)= 0.1000
x(0.2)= next value x(0) + 2*h 0.2000
y(2)= from previous computation 0.8213
l(1)= h*f(xn,yn) h*(0.2+2*1.5146)= 0.1843
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.2077
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.2405
y(3)= y(2) +(l(1) + 4*l(2) + l(3))/6 1.0305
h= x(3)-x(2)= -0.2000
x(0.3)= next value x(0) + 3*h -0.6000
y(3)= from previous computation 1.0305
l(1)= h*f(xn,yn) h*(0.3+2*1.8773)= -0.2922
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= -0.2138
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= -0.1981
y(4)= y(2) +(l(1) + 4*l(2) + l(3))/6 0.8063
numerical analysis using matlab and spreadsheets, second edition 9-25
orchard publications
solutions to exercises
6.
continued on next page
44
45
46
47
48
49
50
51
52
53
a b c d e f
h= x(4)-x(3)= 0.0000
x(0.4)= next value x(0) + 4*h 0.0000
y(3)= from previous computation 0.8063
l(1)= h*f(xn,yn) h*(0.3+2*1.8773)= 0.0000
l(2)= h*f(xn+0.5*h,yn+0.5*l(1)) h*[(0+0.5*h)+2*(1+0.5*l(1))]= 0.0000
l(3)= h*f(xn+h,yn+2*l(2)-l(1)) h*[(0+h)+2*(1+2*l(2)-l(1))]= 0.0000
y(4)= y(2) +(l(1) + 4*l(2) + l(3))/6 0.8063
123456789
10
11
12
13
14
15
16
17
18
19
20
21
22
23
a b c
differential equation is y +y =xy or y =xy-y
numerical solution by runga-kutte method follows
h= x(1)-x(0)= 0.1000
x(0)= initial condition (given) 0.0000
y(0)= initial condition (given) 1.0000
y (0)= initial condition (given) -1.0000
l(1)= h*y (0)= -0.1000
l (1)= h*f(x(0), y(0), y (0))= 0.1000
l(2)= h*(y (0) + 0.5*l (1))= -0.0950
l (2)= h*f(x(0) + 0.5*h, y(0) + 0.5*l(1), y (0) + 0.5*l(1))= 0.0998
l(3)= h*(y (0) + 2*l (2) - l (1))= -0.0901
l (3)= h*f(x(0) + h, y(0) + 2*l(2) - l(1), y (0) + 2*l (2) - l (1))= 0.0992
y(1)= y(0) +(l(1) + 4*l(2) + l(3))/6 0.9050
y (1)= y (0) + (l (1) + 4*l (2) + l (3))/6= -0.9003
h= x(1)-x(0)= 0.1000
x(0.1)= next value x(0) + h 0.1000
y(1)= from previous computation 0.9050
y (1)= from previous computation -0.9003
chapter 9 solution of differential equations by numerical methods
9-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
a b c
l(1)= h*y (1)= -0.0900
l (1)= h*f(x(1), y(1), y (1))= 0.0991
l(2)= h*(y (1) + 0.5*l (1))= -0.0851
l (2)= h*f(x(1) + 0.5*h, y(1) + 0.5*l(1), y (1) + 0.5*l (1))= 0.0980
l(3)= h*(y (1) + 2*l (2) - l (1))= -0.0803
l (3)= h*f(x(1) + h, y(1) + 2*l(2) - l(1), y (1) + 2*l (2) - l (1))= 0.0968
y(2)= y(0) +(l(1) + 4*l(2) + l(3))/6 0.8199
y (2)= y (0) + (l (1) + 4*l (2) + l (3))/6= -0.8023
h= x(2)-x(1)= 0.1000
x(0.2)= next value x(0) + 2*h 0.2000
y(2)= from previous computation 0.8199
y (2)= from previous computation -0.8023
l(1)= h*y (0)= -0.0802
l (1)= h*f(x(0), y(0), y (0))= 0.0966
l(2)= h*(y (0) + 0.5*l (1))= -0.0754
l (2)= h*f(x(0) + 0.5*h, y(0) + 0.5*l(1), y (0) + 0.5*l(1))= 0.0949
l(3)= h*(y (0) + 2*l (2) - l (1))= -0.0709
l (3)= h*f(x(0) + h, y(0) + 2*l(2) - l(1), y 0 + 2*l (2) - l (1))= 0.0934
y(3)= y(0) +(l(1) + 4*l(2) + l(3))/6 0.7444
y (3)= y (0) + (l (1) + 4*l (2) + l (3))/6= -0.7074
numerical analysis using matlab and spreadsheets, second edition 10-1
orchard publications
chapter 10
integration by numerical methods
his chapter is an introduction to numerical methods for integrating functions which are very
difficult or impossible to integrate using analytical means. we will discuss the trapezoidal
rule that computes a function with a set of linear functions, and simpson’s rule that
computes a function with a set of quadratic functions.
10.1 the trapezoidal rule
consider the function for the interval , shown in figure 10.1.
figure 10.1. integration by the trapezoidal rule
to evaluate the definite integral , we divide the interval into subintervals
each of length . then, the number of points between and is
. therefore, the integral from a to b is the sum
of the integrals from to , from to , and so on, and finally from to . the total area
is
the integral over the first subinterval, can now be approximated by the area of the trapezoid
t f (x)
f (x)
y = f(x) a ? x ? b
x
a b
...........
0
f (x)
x1 x2 xn – 1
y0 y1 y2 yn – 1 yn
p0
p1
p2
pn – 1
pn
f(x)dx
a
b
? a ? x ? b n
?x b a –
n
= - x0 = a xn = b
x1 = a + ?x, x2 = a + 2?x,…, xn – 1 = a + (n – 1)?x
a x1 x1 x2 xn – 1 b
f(x)dx
a
b
? f(x)dx
a
x1 ? f(x)dx
x1
x2 ? … f(x)dx
xn – 1
b
+ + + ? f(x)dx
xk – 1
xk ?
k = 1
n
= = ?
chapter 10 integration by numerical methods
10-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
that is equal to plus the area of the trapezoid that is equal to
, and so on. then, the trapezoidal approximation becomes
or
(10.1)
example 10.1
using the trapezoidal rule with , estimate the value of the definite integral
(10.2)
compare with the exact value, and compute the percent error.
solution:
the exact value of this integral is
(10.3)
for the trapezoidal rule approximation we have
then,
ap0p1x1
12
(y0 + y1)?x x1p1p2x2
12
(y1 + y2)?x
t 12
y0 y1 + ( )?x 12
y1 y2 + ( )?x … 12
= + + + (yn – 1 + yn)?x
t 12
y0 y1 y2 … yn – 1
12
? + + + + + yn?
= ? ? ?x
trapezoidal rule
n = 4
x2dx
1
2
?
x2dx
1
2
? x3
3

1
2
83
13
– 73
= = = = 2.33333
x0 = a = 1
xn = b = 2
n = 4
?x b a –
n
- 2 1 –
4
- 14
= = =
y = f (x) = x2
numerical analysis using matlab and spreadsheets, second edition 10-3
orchard publications
the trapezoidal rule
and by substitution into (10.1),
(10.4)
from (10.3) and (10.4), we find that the percent error is
(10.5)
the matlab function trapz(x,y,n) where y is the integral with respect to x, approximates the
integral of a function using the trapezoidal rule, and n (optional) performs integration
along dimension .
example 10.2
use the matlab function trapz(x,y) to approximate the value of the integral
(10.6)
and by comparison with the exact value, compute the percent error when and
solution:
the exact value is found from
for the approximation using the trapezoidal rule, we let represent the row vector with ,
and the vector with , that is, and respectively. the corresponding
values of are denoted as and , and the areas under the curve as and
x0 = a = 1 y0 f(x0) 12 16
16
= = = -
x1 a ?x + 54
= = y1 f x1 ( ) 54
? ?
? ? 2 25
16
= = = -
x2 a 2?x + 64
= = y2 f x2 ( ) 64
? ?
? ?2 36
16
= = = -
x3 a 3?x + 74
= = y3 f x3 ( ) 74
? ?
? ?2 49
16
= = = -
x4 b 2 = = y4 f x4 ( ) 84
? ?
? ?2 64
16
= = = -
t 12
16
16
× - 25
16
- 36
16
- 49
16
- 12
64
16
? + + + + × - ?
? ? 14
× 150
16
14
× 75
32
= = = - = 2.34375
% error 2.34375 – 2.33333
2.33333
= × 100 = 0.45 %
y = f (x)
n
1x dx
1
2
?
n = 5 n = 10
1x x
d
1
2
? lnx 1
= 2 = ln2 – ln1 = 0.6931 – 0.0000 = 0.6931
x5 n = 5
x10 n = 10 ?x = 1 ? 5 ?x = 1 ? 10
y y5 y10 area5
chapter 10 integration by numerical methods
10-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
respectively. we use the following matlab code.
x5=linspace(1,2,5) x10=linspace(1,2,10)
y5=1./x5 y10=1./x10
area5=trapz(x5,y5), area10=trapz(x10,y10)
area5 =
0.6970
area10 =
0.6939
the percent error when is used is
and the percent error when is used is
example 10.3
the integral
(10.7)
where is a dummy variable of integration, is called the error function* and it is used extensively
in communications theory. use the matlab trapz(x,y) function to find the area under this
integral with when the upper limit of integration is .
solution:
we use the same procedure as in the previous example. the matlab code for this example is
t=linspace(0,2,10) y=exp(?t.^2) area=trapz(t,y)
matlab displays the following result.
area =
0.8818
* the formal definition of the error function is
area10
?x = 1 ? 5
% error 0.6970 – 0.6931
0.6931
= × 100 = 0.56 %
?x = 1 ? 10
% error 0.6939 – 0.6931
0.6931
= × 100 = 0.12 %
f(t) e –?2 d?
0
t
= ?
?
erf(u) 2
?
- e ?2 – d?
0
u
= ?
n = 10 t = 2
numerical analysis using matlab and spreadsheets, second edition 10-5
orchard publications
the trapezoidal rule
example 10.4
the (current-voltage) relation of a non-linear electrical device is given by
(10.8)
where .
by any means, find
a. the instantaneous power
b. the energy dissipated in this device from to
solution:
a. the instantaneous power is
(10.9)
b. the energy is the integral of the instantaneous power, that is,
(10.10)
an analytical solution of the last integral is possible using integration by parts, but it is not
easy. we can try the matlab int(f,a,b) function where f is a symbolic expression, and a and
b are the lower and upper limits of integration respectively.
when matlab cannot find a solution, it returns a warning. for this example, matlab
returns the following message when integration is attempted with the symbolic expression of
(10.10).
t=sym( t )
s=int(0.1*sin(3*t)*(exp(0.2*sin(3*t))?1),0,10)
when this code is executed, matlab displays the following message.
warning: explicit integral could not be found.
next, we will find and sketch the power and energy by the trapezoidal rule using the matlab
trapz(x,y) function. for this example, we choose , so that . the
matlab code below will compute and plot the power.
t=linspace(0,10,100)
v=sin(3.*t) i=0.1.*(exp(0.2.*v)?1) p=v.*i
plot(t,p) grid title( power vs time ) xlabel( seconds ) ylabel( watts )
the power varies in a uniform fashion as shown by the plot of figure 10.2.
i – v
i(t) 0.1 e0.2v(t) = ( – 1)
v(t) = sin3t
p(t)
w(t0, t1) t0 = 0 t1 = 10 s.
p(t) = v(t)i(t) = 0.1sin3t(e0.2 sin3t – 1)
w(t0, t1) p(t)dt
t0
t1 ? 0.1 sin3t(e0.2 sin3t – 1)dt
0
10 s
= = ?
n = 100 ?x = 1 ? 100
chapter 10 integration by numerical methods
10-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 10.2. plot for the power variation in example 10.4
the plot of figure 10.2 shows that the power is uniform for all time, and thus we expect the
energy to be constant.
the matlab code below computes and plots the energy.
energy=trapz(t,p), plot(t,energy, + ) grid title( energy vs time ) ...
xlabel( seconds ) ylabel( joules )
energy =
0.1013
thus, the value of the energy is 0.1013 joule.
the energy is shown in figure 10.3.
10.2 simpson’s rule
the trapezoidal and simpson’s rules are special cases of the newton-cote rules which use higher
degree functions for numerical integration.
let the curve of figure 10.4 be represented by the parabola
y = ?x2 + ?x + ? (10.11)
numerical analysis using matlab and spreadsheets, second edition 10-7
orchard publications
simpson’s rule
figure 10.3. plot for the energy of example 10.4
figure 10.4. simpson’s rule of integration
the area under this curve for the interval is
(10.12)
the curve passes through the three points , , and . then, by (10.11) we
have:
0
x
y
?h h
y0 y1 y2
–h ? x ? h
area –h
h (?x2 + ?x + ?)dx
–h
h
? ?x3
3
?x2
2
+ + ?x
–h
h
= =
?h3
3
?h2
2
?h ?h3
3
– ?h2
2
? + – ?h?
+ + – ? ? 2?h3
3
= = + 2?h
13=
h(2?h3 +
6?)
(–h, y0) (0, y1) (h, y2)
chapter 10 integration by numerical methods
10-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(10.13)
we can now evaluate the coefficients and express (10.12) in terms of , , and .
this is done with the following procedure.
by substitution of (b) of (10.13) into (a) and (c) and rearranging we get
(10.14)
(10.15)
addition of (10.14) with (10.15) yields
(10.16)
and by substitution into (10.12) we get
(10.17)
or
(10.18)
now, we can apply (10.18) to successive segments of any curve in the interval
as shown on the curve of figure 10.5.
figure 10.5. simpson’s rule of integration by successive segments
from figure 10.5, we see that each segment of width of the curve can be approximated by a
y0 = ?h2 – ?h + ? (a)
y1 = ? (b)
y2 = ?h2 + ?h + ? (c)
?, ?, ? h y0 y1 y2
?h2 – ?h = y0 – y1
?h2 + ?h = y2 – y1
2?h2 = y0 – 2y1 + y2
area –h
h 13
h 2?h3 6? + ( ) 13
= = h[(y0 – 2y1 + y2) + 6y1]
area –h
h 13
= h(y0 + 4y1 + y2)
y = f(x) a ? x ? b
a b
h
a b
c
yn – 1 y0 y1 y2 y3 y4 yn
2h
numerical analysis using matlab and spreadsheets, second edition 10-9
orchard publications
simpson’s rule
parabola through its ends and its midpoint. thus, the area under segment is
(10.19)
likewise, the area under segment is
(10.20)
and so on. when the areas under each segment are added, we get
(10.21)
since each segment has width , to apply simpson’s rule of numerical integration, the number of
subdivisions must be even. this restriction does not apply to the trapezoidal rule of numerical integration.
the value of for (10.21) is found from
(10.22)
example 10.5
using simpson’s rule with 4 subdivisions , compute the approximate value of
(10.23)
solution:
this is the same integral as that of example 10.2 where we found that the analytical value of this
definite integral is . we can also find the analytical value with matlab’s int(f,a,b)
function where f is a symbolic expression, and a and b are the lower and upper limits of integration
respectively. for this example,
syms x
area=int(1/x,1,2)
area =
log(2)
we recall that log(x) in matlab is the natural logarithm.
to use simpson’s rule, for convenience, we construct the following table using the spreadsheet of
figure 10.6.
ab
area ab
13
= h(y0 + 4y1 + y2)
bc
area bc
13
= h(y2 + 4y3 + y4)
area 13
= h(y0 + 4y1 + 2y2 + 4y3 + 2y4 + … + 2yn – 2 + 4yn – 1 + yn)
simpson? s rule of numerical integration
2h n
h
h b a –
n = - n = even
(n = 4)
1x
dx
1
2
?
ln= 0.6931
chapter 10 integration by numerical methods
10-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 10.6. spreadsheet for numerical integration of (10.23)
by comparison of the numerical with the exact value, we observe that the error is very small when
simpson’s method is applied.
matlab has two quadrature functions for performing numerical integration, the quad and
quad8. the description of these can be seen by typing help quad or help quad8. both of these
functions use adaptive quadrature methods this means that these methods can handle irregularities
such as singularities. when such irregularities occur, matlab displays a warning message but
still provides an answer.
the quad function uses an adaptive form of simpson’s rule, while the quad8 function uses the
so-called newton-cotes 8-panel rule. the quad8 function detects and handles irregularities more
efficiently.
both functions have the same syntax, that is, q=quad(‘f’,a,b,tol), and integrate to a relative error
tol which we must specify. if tol is omitted, it is understood to be the standard tolerance of .
the string ‘f’ is the name of a user defined function, and a and b are the lower and upper limits of
integration respectively.
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e
example 10.5
? (1/x)dx evaluated from a = 1 to b = 2 with n = 4
numerical integration by simpson s method follows
given a= 1
b= 2
n= 4
then, h = (b-a)/n = 0.2500
multiplier products
x0=a= 1.00000
y0=1/x0= 1.00000 1 1.00000
x1=a+h= 1.25000
y1=1/x1= 0.80000 4 3.20000
x2=a+2h= 1.50000
y2=1/x2= 0.66667 2 1.33333
x3=a+3h= 1.75000
y3=1/x3= 0.57143 4 2.28571
x4=b= 2.00000
y4=1/x4= 0.50000 1 0.50000
sum of products = 8.31905
area = (h/3)*(sum of products) = (1/12)*8.31905 = 0.69325
10–3
numerical analysis using matlab and spreadsheets, second edition 10-11
orchard publications
simpson’s rule
example 10.6
given the definite integral
(10.24)
a. use matlab’s symbolic int function to obtain the value of this integral
b. obtain the value of this integral with the q=quad(‘f’,a,b) function
c. obtain the value of this integral with the q=quad(‘f’,a,b,tol) function where
d. obtain the value of this integral with the q=quad8(‘f’,a,b) function
e. obtain the value of this integral with the q=quad8(‘f’,a,b,tol) function where
solution:
a.
syms x y=int(exp(?x^2),0,2) % define symbolic variable x and integrate
y =
1/2*erf(2)*pi^(1/2)
pretty(y)
1/2
1/2 erf(2) pi
erf is an acronym for the error function and we can obtain its definition with help erf
b. first, we need to create and save a function m-file. we name it errorfcn1.m as shown below.
we will use format long to display the values with 15 digits.
function y = errorfcn1(x)
y = exp(?x.^2)
with this file saved as errorfcn1.m, we write and execute the following matlab code.
format long
y_std=quad( errorfcn1 ,0,2)
we obtain the answer in standard tolerance form as
y_std =
0.88211275610253
c. with the specified tolerance, the code and the answer are as follows:
y_tol=quad( errorfcn1 ,0,2,10^?10)
y_tol =
0.88208139076242
y= f(x) e x– 2 dx
0
2
= ?
tol 10–10 =
tol 10–10 =
chapter 10 integration by numerical methods
10-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
d. with the standard tolerance,
y_std8=quad8( errorfcn1 ,0,2)
y_std8 =
0.88208139076194
e. with the specified tolerance,
y_tol8=quad8( errorfcn1 ,0,2,10^?10)
y_tol8 =
0.88208139076242
we observe that with the tolerance, both quad and quad8 produce the same result.
example 10.7
using the quad and quad8 functions with standard tolerance, evaluate the integral
(10.25)
at and . use the fprintf function to display first the analytical
values, then, the numerical values produced by the quad and quad8 functions for each set of
data.
solution:
evaluating the given integral, we get
(10.26)
where and are non-negative values. substitution of the values of the given values of and
will be included in the matlab code below.
the sqrt function in a built-in function and therefore, we need not write a user defined m-file.
we will include the input function in the code. the code is then saved as example_10_7.
% this code displays the approximations obtained with the quad and quad8 functions
% with the analytical results for the integration of the square root of x over the
% interval (a,b) where a and b are non-negative.
%
fprintf( \n ) % insert line
a=input( enter first point "a" (non-negative): )
b=input( enter second point "b" (non-negative): )
k=2/3.*(b.^(1.5)-a.^(1.5))
10–10
y f(x) xdx
a
b
= = ?
((a, b) = (0.2, 0.8), (1.4, 2.3)), (3,8)
y x1 ? 2 dx
a
b
? x3 2 ?
3 ? 2
-
a
b
23 b3 ?
2
a3 ?
2
=
=
=
(

)
a b a b
numerical analysis using matlab and spreadsheets, second edition 10-13
orchard publications
summary
kq=quad( sqrt ,a,b)
kq8=quad8( sqrt ,a,b)
fprintf( \n ) ... % insert line
fprintf( analytical: %f \n numerical quad, quad8: %f %f \n ,k,kq,kq8) ...
fprintf( \n ) fprintf( \n ) % insert two lines
now, we execute this saved file by typing its name, that is,
example_10_7
enter first point "a" (non-negative): 0.2
enter second point "b" (non-negative): 0.8
analytical: 0.417399
numerical quad, quad8: 0.417396 0.417399
example_10_7
enter first point "a" (non-negative): 1.4
enter second point "b" (non-negative): 2.3
analytical: 1.221080
numerical quad, quad8: 1.221080 1.221080
example_10_7
enter first point "a" (non-negative): 3
enter second point "b" (non-negative): 8
analytical: 11.620843
numerical quad, quad8: 11.620825 11.620843
10.3 summary
• we can evaluate a definite integral with the trapezoidal approximation
by dividing interval into subintervals each of length . the number of
subdivisions can be even or odd.
• the matlab function trapz(x,y,n) where y is the integral with respect to x, approximates
the integral of a function using the trapezoidal rule, and n (optional) performs integration
along dimension .
f(x)dx
a
b
?
t 12
y0 y1 y2 … yn – 1
12
? + + + + + yn ?
= ? ? ?x
a x b ? ? n ?x b a –
n
= - n
y = f (x)
n
chapter 10 integration by numerical methods
10-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• we can perform numerical integration with the matlab function int(f,a,b) function where f
is a symbolic expression, and a and b are the lower and upper limits of integration respectively.
• we can evaluate a definite integral with simpson’s rule of numerical integration
using the expression
where the number of subdivisions must be even.
• the trapezoidal and simpson’s rules are special cases of the newton-cote rules which use
higher degree functions for numerical integration.
• matlab has two quadrature functions for performing numerical integration, the quad and
quad8. both of these functions use adaptive quadrature methods. the quad function uses an
adaptive form of simpson’s rule, while the quad8 function uses the so-called newton-cotes 8-
panel rule. the quad8 function detects and handles irregularities more efficiently. both functions
have the same syntax, that is, q=quad(‘f’,a,b,tol), and integrate to a relative error tol
which we must specify. if tol is omitted, it is understood to be the standard tolerance of .
the string ‘f’ is the name of a user defined function, and a and b are the lower and upper limits
of integration respectively.
f(x)dx
a
b
?
area 13
= h(y0 + 4y1 + 2y2 + 4y3 + 2y4 + … + 2yn – 2 + 4yn – 1 + yn)
n
10–3
numerical analysis using matlab and spreadsheets, second edition 10-15
orchard publications
exercises
10.4 exercises
1. use the trapezoidal approximation to compute the values the following definite integrals and
compare your results with the analytical values. verify your answers with the matlab
trapz(x,y,n) function.
a.
b.
c.
d.
2. use simpson’s rule to approximate the following definite integrals and compare your results
with the analytical values. verify your answers with the matlab quad(‘f’,a,b) function.
a.
b.
c.
x x d
0
2
? n = 4
x3dx
0
2
? n = 4
x4dx
0
2
? n = 4
1
x2 dx
1
2
? n = 4
x2dx
0
2
? n = 4
sinxdx
0
?
? n = 4
1
x2 + 1
dx
0
1
? n = 4
chapter 10 integration by numerical methods
10-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
10.5 solution to exercises
1.
a. the exact value is
for the trapezoidal rule approximation we have
x=linspace(0,2,4) y=x area=trapz(x,y)
area =
2
b. the exact value is
for the trapezoidal rule approximation we have
t 12
y0 y1 y2 … yn – 1
12
? + + + + + yn ?
= ? ? ?x
x x d
0
2
? x2
2

0
2
= = 2
x0 = a = 0
xn = b = 2
n = 4
?x b a –
n
- 2 0 –
4
- 12
= = =
y = f (x) = x
x0 = a = 0 y0 = f(x0) = 0
x1 a ?x + 12
= = y1 f x1 ( ) 12= =
x2 = a + 2?x = 1 y2 = f(x2) = 1
x3 a 3?x + 32
= = y3 f x3 ( ) 32
= =
x4 = b = 2 y4 = f(x4) = 2
t 12
0 × 12
1 32 12
? + + + + × 2 ?
? ? 12
× 4 12
= = × = 2
x3dx
0
2
? x4
4

0
2
= = 4
numerical analysis using matlab and spreadsheets, second edition 10-17
orchard publications
solution to exercises
x=linspace(0,2,4) y=x.^3 area=trapz(x,y)
area =
4.4444
the deviations from the exact value are due to the small number of divisions we chose.
c. the exact value is
for the trapezoidal rule approximation we have
x0 = a = 0
xn = b = 2
n = 4
?x b a –
n
- 2 0 –
4
- 12
= = =
y = f (x) = x3
x0 = a = 0 y0 = f(x0) = 0
x1 a ?x + 12
= = y1 f x1 ( ) 18
= =
x2 = a + 2?x = 1 y2 = f(x2) = 1
x3 a 3?x + 32
= = y3 f(x3) 27
8
= = -
x4 = b = 2 y4 = f(x4) = 8
t 12
0 × 18
1 27
8
- 12
? + + + + × 8 ?
? ? 12
× 5 72
? + ?
? ? 12
= = × = 4.25
n
x4dx
0
2
? x5
5

0
2
32
5
= = - = 6.4
x0 = a = 0
xn = b = 2
n = 4
?x b a –
n
- 2 0 –
4
- 12
= = =
y = f (x) = x4
chapter 10 integration by numerical methods
10-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
x=linspace(0,2,4) y=x.^4 area=trapz(x,y)
area =
7.5720
d. the exact value is
for the trapezoidal rule approximation we have
x0 = a = 0 y0 = f(x0) = 0
x1 a ?x + 12
= = y1 f(x1) 1
16
= = -
x2 = a + 2?x = 1 y2 = f(x2) = 1
x3 a 3?x + 32
= = y3 f(x3) 81
8
= = -
x4 = b = 2 y4 = f(x4) = 16
t 12
× 0 1
16
- 1 81
16
- 12
? + + + + × 16 ?
? ? 12
× 9 41
8
? + - ?
? ? 12
= = × = 7.0625
1
x2 dx
1
2
? 1x

1
2 12
= =
x0 = a = 1
xn = b = 2
n = 4
?x b a –
n
- 2 1 –
4
- 14
= = =
y = f (x) = 1 ? x2
x0 = a = 1 y0 = f(x0) = 1
x1 a ?x + 54
= = y1 f(x1) 16
25
= = -
x2 a 2?x + 32
= = y2 f x2 ( ) 49
= =
x3 a 3?x + 74
= = y3 f(x3) 16
49
= = -
x4 b 2 = = y4 f x4 ( ) 14
= =
t 12
× 1 16
25
- 49
16
49
- 12
14
? + + + + × ?
? ? 14
× 3905
1918
? - ?
? ? 14
= = × = 0.5090
numerical analysis using matlab and spreadsheets, second edition 10-19
orchard publications
solution to exercises
x=linspace(1,2,4) y=1./x.^2 area=trapz(x,y)
area =
0.5158
2.
a. the exact value is
to use simpson’s rule we construct the following table using a spreadsheet.
we create and save a function m-file. we name it exer_10_2_a.m as shown below.
function y = exer_10_2_a(x)
y = x.^2
we write and execute the following matlab code:
y_std=quad( exer_10_2_a ,0,2)
area 13
= h(y0 + 4y1 + 2y2 + 4y3 + 2y4 + …+ 2yn – 2 + 4yn – 1 + yn)
x2dx
0
2
? x3
3

0
2
83
= = = 2.6667
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e
exercise 10.2.a
? x 2 dx evaluated from a = 0 to b = 2 with n = 4
numerical integration by simpson s method follows
given a= 0
b= 2
n= 4
then, h = (b-a)/n = 0.5000
multiplier products
x0=a= 0.00000
y0=x0
2= 0.00000 1 0.0000
x1=a+h= 0.50000
y1=x1
2= 0.25000 4 1.0000
x2=a+2h= 1.00000
y2=x2
2= 1.00000 2 2.0000
x3=a+3h= 1.50000
y3=x3
2= 2.25000 4 9.0000
x4=b= 2.00000
y4=x4
2= 4.00000 1 4.0000
sum of products = 16.0000
area = (h/3)*(sum of products) = (1/12)*8.31905 = 2.6667
chapter 10 integration by numerical methods
10-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
y_std =
2.6667
b. the exact value is
to use simpson’s rule we construct the following table using a spreadsheet.
we create and save a function m-file. we name it exer_10_2_b.m as shown below.
function y = exer_10_2_b(x)
y = sin(x)
we write and execute the following matlab code:
y_std=quad( exer_10_2_b ,0,pi)
y_std =
2.0000
c. the exact value is
sinxdx
0
?
? –cosx 0
? = = –(– 1 – 1) = 2
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e
exercise 10.2.b
? sinxdx evaluated from a = 0 to b = ? with n = 4
numerical integration by simpson s method follows
given a= 0
b= 3.14159
n= 4
then, h = (b-a)/n = 0.7854
multiplier products
x0=a= 0.00000
y0=sinx0= 0.00000 1 0.0000
x1=a+h= 0.78540
y1=sinx1= 0.70711 4 2.8284
x2=a+2h= 1.57080
y2=sinx2= 1.00000 2 2.0000
x3=a+3h= 2.35619
y3=sinx3= 0.70711 4 2.8284
x4=b= 3.14159
y4=sinx4= 0.00000 1 0.0000
sum of products = 7.6569
area = (h/3)*(sum of products) = (1/12)*8.31905 = 2.0046
1
x2 + 1
dx
0
1
? –1x tan 0
1 ?4
= = = 0.7854
numerical analysis using matlab and spreadsheets, second edition 10-21
orchard publications
solution to exercises
to use simpson’s rule we construct the following table using a spreadsheet.
we create and save a function m-file. we name it exer_10_2_c.m as shown below.
function y = exer_10_2_c(x)
y = 1./(x.^2+1)
we write and execute the following matlab code:
y_std=quad( exer_10_2_c ,0,1)
y_std =
0.7854
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e
exercise 10.2.c
? (1/(x 2 +1))dx evaluated from a = 0 to b = 1 with n = 4
numerical integration by simpson s method follows
given a= 0
b= 1
n= 4
then, h = (b-a)/n = 0.2500
multiplier products
x0=a= 0.00000
y0=1/(x0
2+1)= 1.00000 1 1.0000
x1=a+h= 0.25000
y1=1/(x1
2+1)= 0.94118 4 3.7647
x2=a+2h= 0.50000
y2=1/(x2
2+1)= 0.80000 2 1.6000
x3=a+3h= 0.75000
y3=1/(x3
2+1)= 0.64000 4 2.5600
x4=b= 1.00000
y4=1/(x4
2+1)= 0.50000 1 0.5000
sum of products = 9.4247
area = (h/3)*(sum of products) = (1/12)*8.31905 = 0.7854
chapter 10 integration by numerical methods
10-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 11-1
orchard publications
chapter 11
difference equations
his chapter is an introduction to difference equations. the discussion is limited to linear
difference equations with constant coefficients. the fibonacci numbers are defined, and a
practical example in electric circuit theory is given at the end of this chapter.
11.1 definition, solutions, and applications
difference equations are used in numerous applications such as engineering, mathematics, physics,
and other sciences. a difference equation defines the relationship between the values of a
function, and the discrete set of the independent variables . for example, the relation
(11.1)
is a linear difference equation with constant coefficients, and describes the relationship of a discrete
input and the corresponding discrete output in a linear and time invariant* system.
with constant coefficients and .
in (11.1), the difference order was chosen to be the same on both sides. however, in most
cases certain coefficients and are zero and thus, the order for the left and right sides will
not always be the same.
the general form of a linear, constant coefficient difference equation has the form
(11.2)
where represents a constant coefficient and is an operator similar to the operator in
ordinary differential equations. the operator increases the argument of a function by one
interval , and is a positive integer that denotes the order of the difference equation.
in terms of the interval , the difference operator is
(11.3)
* a time invariant system is defined as one in which the input-output relationship does not change with time. that is, if an
input x produces an output at some time , the same input x will produce the same output y at any other time.
all systems in this text are assumed to be time invariant.
t
yk
xk
y(n) + b1 y(n – 1) + b1 y(n – 1) + b2 y(n – 2) + …+ bk y(n – k)
= a0x(n) + a1 x(n – 1) + a2 x(n – 2) +… + ak x(n – k)
x(n) y(n)
y = f (x) t0
ai bi
k
ai bi k
a0er a1e r – 1 ar – 1( + + e + ar)y = ?(x)
ak e d
e
h r
h e
ef(xk) f(xk + h) f xk + h = = ( )
chapter 11 difference equations
11-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the interval is usually unity, i.e., , and the subscript is normally omitted. thus, (11.3)
is written as
(11.4)
if, in (11.4), we increase the argument of by another unit, we get the second order operator ,
that is,
(11.5)
and in general,
(11.6)
as with ordinary differential equations, the right side of (11.2) is a linear combination of terms
such as , , and , where is a non-zero constant and is a non-negative integer.
moreover, if, in (11.2), , the equation is referred to as a homogeneous difference equation,
and if , it is a non-homogeneous difference equation.
if, in (11.2), we let r = 2, we obtain the second order difference equation
(11.7)
and if the right side is zero, it reduces to
(11.8)
if and are any two solutions of (11.8), the linear combination is also
a solution. also, if the casorati determinant, analogous to the wronskian determinant in ordinary
differential equations, is non-zero, that is, if
(11.9)
then, any other solution of (11.8) can be expressed as
(11.10)
where and are constants.
for the non-homogeneous difference equation
(11.11)
where , if is any solution of (11.11), then the complete solution is
h h = 1 k
ef (x) f (x + 1) fx + 1 = =
f e2
e2f (x) e[ef (x)] ef (x + 1) f (x + 2) fx + 2 = = = =
erf (x) = f(x + r) = fx + r
kx coskx xn k n
? (x) = 0
?(x) ? 0
(a2e 2 + a1e + a0) y = ? (x)
(a2e2 + a1e + a0) y = 0
y1(x) y2(x) k1y1(x) + k2y1(x)
c[y1(x), y2(x)] y1(x) y2(x)
ey1(x) ey2(x)
= ? 0
y3(x) = k1y1(x) + k2y2(x)
k1 k2
(a2e2 + a1e + a0)y = ?(x)
? (x) ? 0 y(x)
numerical analysis using matlab and spreadsheets, second edition 11-3
orchard publications
definition, solutions, and applications
(11.12)
as with ordinary differential equations, we first find the solution of the homogeneous difference
equation then, we add the particular solution to it to obtain the total solution. we find
by the method of undetermined coefficients.
we have assumed that the coefficients in (11.11) are constants then, in analogy with the
solution of the differential equation of the form , for the homogeneous difference equation
we assume a solution of the form
(11.13)
by substitution into (11.8), and recalling that , we get
(11.14)
and this is the characteristic equation of a second order difference equation.
as with algebraic quadratic equations, the roots of (11.14) can be real and unequal, real and
equal, or complex conjugates depending on whether the discriminant is positive, zero,
or negative. these cases are summarized in table 11.1.
example 11.1
find the solution of the difference equation
(11.15)
table 11.1 roots of the characteristic equation in difference equations
characteristic equation of
roots and discriminant general solution
real and unequal
real and equal
complex conjugates
y = k1y1(x) + k2y2(x) + y(x)
y(x)
y(x)
ai
y = keax
y = mx
ef (x) = f (x + 1)
a2mx + 2 a1mx + 1 + + a0mx = 0
a1 2 – 4a2a0
a2m2 + a1m + a0 = 0 (a2e2 + a1e + a0)y = 0
m1 m2
m1 ? m2
a1 2 – 4a2a0 > 0 y k1m1
x k2m2
x = +
k1 and k2 cons tants
m1 = m2
a1 2 – 4a2a0 = 0 y k1m1
x k2xm2
x = +
k1 and k2 cons tants
m1 = ? + j?
m2 = ?–j?
a1 2 – 4a2a0 < 0 y = rx(c1 cos?x + c2 sin?x)
r ?2 ?2 + = ? ??
-
–1
= tan
(e 2–6e + 8)y = 0
chapter 11 difference equations
11-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
with initial conditions and . then, compute .
solution:
the characteristic equation of (11.15) is
(11.16)
and its roots are and . therefore, with reference to table 11.1, we get the solution
(11.17)
to make use of the first initial condition, we let . then, (11.17) becomes
or
(11.18)
for the second initial condition, we let . then, (11.17) becomes
or
(11.19)
simultaneous solution of (11.18) and (11.19) yields and . thus, the solution is
(11.20)
for , we get
example 11.2
find the solution of the difference equation
(11.21)
solution:
the characteristic equation of (11.21) is
(11.22)
y0 = y(0) = 3 y1 = y(1) = 2 y5 = y(5)
m 2–6m + 8 = 0
m1 = 2 m2 = 4
yx = y(x) = k12x + k24 x
x = 0
y0 = 3 = k120 + k240
k1 + k2 = 3
x = 1
y1 = 2 = k121 + k241
2k1 + 4k2 = 2
k1 = 5 k2 = –2
yx = 5 ? 2x – 2 ? 4 x
x = 5
y5 = 5 ? 25 – 2 ? 45 = 5 × 32 – 2 × 1024 = –1888
(e 2 + 2e + 4) y = 0
m2 + 2m + 4 = 0
numerical analysis using matlab and spreadsheets, second edition 11-5
orchard publications
definition, solutions, and applications
and its roots are and . from table 11.1,
and . therefore, the solution is
(11.23)
the constants and can be evaluated from the initial conditions.
for non-homogeneous difference equations of the form of (11.11), we combine the particular
solution with the solution of the homogeneous equation shown in (11.12). for the particular
solution, we start with a linear combination of all the terms of the right side, that is, , and we
apply the operator . if any of the terms in the initial choice duplicates a term in the solution of
the homogeneous equation, this choice must be multiplied by until there is no duplication of
terms.
table 11.2 shows the form of the particular solution for different terms of .
example 11.3
find the solution of the difference equation
(11.24)
solution:
the characteristic equation of (11.24) is
table 11.2 form of the particular solution for a non-homogeneous difference equation
non-homogeneous difference equation
form of particular solution
? (constant) a (constant)
(k = positive integer)
or
or
m1 = – 1 + j 3 m2 = –1–j 3 r (–1)2 ( 3)2
= + = 2
? ( 3 ? (–1)) –1 = tan = 2? ? 3
y 2x c1
2?
3
x c2
2?
3
? cos + sin x?
= ? ?
c1 c2
?(x)
e
x
?(x)
(a2e 2 + a1e + a0)y = ?(x)
? (x) y(x)
?xk akxk ak – 1xk – 1 + + … + a1x + a0
?kx akx
?cosmx ?sinmx a1 cosmx + a2 sinmx
?xklx cosmx ?xklx sinmx akxk ak – 1xk – 1 ( + + … + a1x + a0) lx cosmx
+ bkxk bk – 1xk – 1 ( + +… + b1x + b0) lx sinmx
e 2 5 – e 6 + ( ) y x 2 x = +
chapter 11 difference equations
11-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(11.25)
and its roots are and
from table 11.1, the solution of the homogeneous difference equation is
(11.26)
for the particular solution we refer table 11.2. for the first term of the right side of (11.24), we
use the term , or . for the second term , we get or , and thus, the particular
solution has the form
(11.27)
but the term in (11.27), is also a term in (11.26). therefore, to eliminate the duplication, we
multiply the term by . thus, the correct form of the particular solution is
(11.28)
to evaluate the constants , , and , we substitute (11.28) into (11.24). then, using (11.5)
we get
(11.29)
using the law of exponents , simplifying, and equating like terms, we get
(11.30)
relation (11.30) will be true if
or
by substitution into (11.28), we obtain the particular solution
(11.31)
therefore, the total solution is the sum of (11.26) and (11.31), that is,
(11.32)
m 2–5m + 6 = 0
m1 = 2 m2 = 3
yh
yh = k12 x + k23x
x
a1x + a0 ax + b 2x a2 x c2 x
yp = ax + b + c2x
c2x
c2x x
yp = ax + b + cx2x
ab c
a(x + 2) b c(x + 2) 2 x + 2 [ + + ? ] 5 a(x + 1) b c(x + 1) 2 x + 1 – [ + + ? ]
+ 6[ax + b + cx2 x] = x + 2 x
w m + n = w m × w n
2ax + (– 3a + 2b) – 2c2 x = x + 2 x
2a = 1 – 3a + 2b = 0 –2c = 1
a = 0.5 b = 0.75 c = –0.5
yp = 0.5x + 0.75–0.5x2 x
ytotal = yh + yp = k12 x + k23 x + 0.5x + 0.75–0.5x2 x
numerical analysis using matlab and spreadsheets, second edition 11-7
orchard publications
fibonacci numbers
11.2 fibonacci numbers
the fibonacci numbers are solutions of the difference equation
(11.33)
that is, in a series of numbers, each number after the second, is the sum of the two preceding
numbers.
example 11.4
given that and , compute the first 12 fibonacci numbers.
solution:
for and so on, we obtain the fibonacci numbers
we will conclude this chapter with an application to electric circuit analysis.
example 11.5
for the electric network of figure 11.1, derive an expression for the voltage at each point
where , given that the voltage at point is known.
.
figure 11.1. electric network for example 11.5
solution:
we need to derive a difference equation that relates the unknown voltage to the known voltage
. we start by drawing part of the circuit as shown in figure 11.2, and we denote the voltages
and currents as indicated.
yx + 2 yx + 1 = + yx
y0 = 0 y1 = 1
x = 0, 1, 2, 3
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, …
vx px
x = 0, 1, 2,…, n v0 p0
+?
r r r r r r
2r 2r 2r 2r 2r 2r
+
?
+
?
+
?
+
?
+
?
+
?
+
?
p0
p pn – 3 1 p2 p3 pn
pn – 2 pn – 1
v0
v1 v2 vn – 3 v3 vn vn – 2 vn – 1
vx
v0
chapter 11 difference equations
11-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 11.2. part of the circuit of figure 11.1
by application of kirchoff’s current law (kcl) at node of figure 11.2, we get
(11.34)
and after simplification,
of course, the term cannot be zero. therefore, we must have
(11.35)
relation (11.35) is valid for all points except and * as shown in figure 11.1 therefore,
we must find the current relations at these two points.
also, by application of kirchoff’s current law (kcl) at node of figure 11.1, we get
and after simplification,
(11.36)
likewise, at node of figure 11.1, we get
observing that , and simplifying, we get
(11.37)
* the voltages at nodes and are and respectively.
r r
2r 2r 2r
+
?
+
?
+
?
vx + 1 vx + 2 vx
px px + 2 px + 1
px + 1
vx + 1 – vx
r

vx + 1
2r

vx + 1 vx + 2 –
r
+ + - = 0
2r
- vx + 2 –2.5vx + 1 ( + vx) = 0
2 ? r
vx + 2 –2.5vx + 1 + vx = 0
p1 pn – 1
p0 pn v0 vn
p1
v1 – v0
r
-
v1
2r

v1 – v2
r
+ + - = 0
v2 – 2.5v1 + v0 = 0
pn – 1
vn – 1 vn – 2 –
r
-
vn – 1
2r

vn – 1 – vn
r
+ + = 0
vn = 0
2.5vn – 1 vn – 2 – = 0
numerical analysis using matlab and spreadsheets, second edition 11-9
orchard publications
fibonacci numbers
relation (11.36) is a difference equation of the form
where . its characteristic equation is
(11.38)
the roots of the characteristic equation of (11.38) are and . thus, the solution
is
(11.39)
the constant coefficients and in (11.39), are found by substitution of this relation into
(11.36) and (11.37). thus, from (11.39) and (11.36), we get
or
or
(11.40)
likewise, from (11.39) and (11.37) we get
or
or
or
(11.41)
simultaneous solution of (11.40) and (11.41) yields
(11.42)
(e 2–2.5e + 1) y = 0
y = vx
m 2–2.5m + 1 = 0
m1 = 0.5 m2 = 2
y = vx = k1(0.5)x + k2(2)x
k1 k2
k1(0.5)2 + k2(2)2 – 2.5(k1(0.5)1 + k2(2)1) + v0 = 0
0.25k1 + 4k2 – 1.25k1 – 5k2 + v0 = 0
k1 + k2 = v0
2.5 k1
12
? ?
? ? n – 1
k2(2)n – 1 ? + ?
? ? k1
12
? ?
? ? n – 2
k2(2)n – 2 – – = 0
2.5k1
2n – 1
2.5k2(2)n – 1 k1
2n – 2 – - k2(2)n – 2 + – = 0
2(2.5)k1
2 n
2.5k2(2)n
2
- 4k1
2 n –
k2(2)n
4
+ – - = 0
k1
2 n + k2(2) n = 0
k1
2 2n
2 2n – 1
= -v0 k2
–1
2 2n – 1
= -v0
chapter 11 difference equations
11-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
finally, substitution of (11.42) into (11.39) yields a solution of the difference equation in terms of
, that is,
or
(11.43)
we observe that when ,
and when ,
11.3 summary
• the general form of a linear, constant coefficient difference equation has the form
where represents a constant coefficient and is an operator similar to the operator in
ordinary differential equations. as with ordinary differential equations, the right side is a linear
combination of terms such as , , and , where is a non-zero constant and is a
non-negative integer. if , the equation is referred to as a homogeneous difference
equation, and if , it is a non-homogeneous difference equation.
• the difference operator is
the interval is usually unity, i.e., , and the subscript is normally omitted. thus,
(11.3) is written as
and in general,
v0
y vx
2 2n
22n – 1
v0
12
? ?
? ? x –1
2 2n – 1
= = + -v0(2)x
y vx
2 2n
2x – 2x
? ?
? ?
? ? v0
22n – 1
= =
x = 0
y vx
22n
1
? - – 1?
? ? v0
2 2n – 1
= = - = v0
x = n
y vx
22n
2n - – 2n
? ?
? ?
? ? v0
22n – 1
(2n – 2n)
v0
22n – 1
= = = = 0
a0er a1e r – 1 ar – 1( + + e + ar)y = ?(x)
ak e d
kx coskx xn k n
? (x) = 0
?(x) ? 0
e
ef(xk) f(xk + h) f xk + h = = ( )
h h = 1 k
ef (x) f (x + 1) fx + 1 = =
erf (x) = f(x + r) = fx + r
numerical analysis using matlab and spreadsheets, second edition 11-11
orchard publications
summary
• if and are any two solutions of a homogeneous difference equation, the linear
combination , where and are constants, is also a solution.
• if the casorati determinant, analogous to the wronskian determinant in ordinary differential
equations, is non-zero, that is, if
then, any other solution of the homogeneous difference equation can be expressed as
• for the non-homogeneous difference equation
where , if is any solution of (11.11), then the complete solution is
as with ordinary differential equations, we first find the solution of the homogeneous difference
equation then, we add the particular solution to it to obtain the total solution. we
find by the method of undetermined coefficients.
• in analogy with the solution of the differential equation of the form , for the homogeneous
difference equation, we assume a solution of the form
• since , the characteristic equation of a second order difference equation is
and as with algebraic quadratic equations, the roots can be real and unequal, real and equal, or
complex conjugates depending on whether the discriminant is positive, zero, or
negative. these cases are summarized in table 11.1.
• for non-homogeneous difference equations we combine the particular solution with the solution
of the homogeneous equation. for the particular solution, we start with a linear combination
of all the terms of the right side, that is, , and we apply the operator . if any of the
terms in the initial choice duplicates a term in the solution of the homogeneous equation, this
choice must be multiplied by until there is no duplication of terms. the form of the particuy1(
x) y2(x)
k1y1(x) + k2y1(x) k1 k2
c[y1(x), y2(x)] y1(x) y2(x)
ey1(x) ey2(x)
= ? 0
y3(x) = k1y1(x) + k2y2(x)
(a2e2 + a1e + a0)y = ?(x)
? (x) ? 0 y(x)
y = k1y1(x) + k2y2(x) + y(x)
y(x)
y(x)
y = keax
y = mx
ef (x) = f (x + 1)
a2mx + 2 a1mx + 1 + + a0mx = 0
a1 2 – 4a2a0
?(x) e
x
chapter 11 difference equations
11-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
lar solution for different terms of is shown in table 11.2.
• the fibonacci numbers are solutions of the difference equation
that is, in a series of numbers, each number after the second, is the sum of the two preceding
numbers.
?(x)
yx + 2 yx + 1 = + yx
numerical analysis using matlab and spreadsheets, second edition 11-13
orchard publications
exercises
11.4 exercises
find the total solution of the following difference equations.
1.
2.
3.
4.
(e2 + 7e + 12) y = 0
(e2 + 2e + 2) y = 0
e2 e – 6 – ( ) y x 3 x = +
(e2 + 1) y = sinx
chapter 11 difference equations
11-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
11.5 solutions to exercises
1.
the characteristic equation is
and its roots are and . therefore, with reference to table 11.1, we get the
solution
(1)
the constants and can be evaluated from the initial conditions. since they were not
given, let us assume that and .
to make use of the first initial condition, we let . then, (1) becomes
or
(2)
for the second initial condition, we let . then, (1) becomes
or
(3)
simultaneous solution of (2) and (3) yields and . thus, the solution is
2.
the characteristic equation is
and its roots are and . from table 11.1, and
. therefore, the solution is
(e2 + 7e + 12) y = 0
m 2 + 7m + 12 = 0
m1 = –3 m2 = –4
yx = y(x) = k1(–3) x + k2(–4) x
k1 k2
y0 = y(0) = 1 y1 = y(1) = 2
x = 0
y0 = 1 = k1(–3)0 + k2(–4) 0
k1 + k2 = 1
x = 1
y1 = 2 = k1(–3) 1 + k2(–4) 1
–3k1 – 4k2 = 2
k1 = 6 k2 = –5
yx = y(x) = 6 × (–3) x–5 × (–4) x
(e2 + 2e + 2) y = 0
m2 + 2m + 2 = 0
m1 = – 1 + j m2 = –1–j r = (–1)2 + (1)2 = 2
? –11 ? (–1) = tan = –? ? 4
y 2x = (c1 cos(–? ? 4)x + c2 sin(–? ? 4)x)
numerical analysis using matlab and spreadsheets, second edition 11-15
orchard publications
solutions to exercises
the constants and can be evaluated from the initial conditions. for this exercise, they
were not given.
3.
the characteristic equation is
and its roots are and
from table 11.1, the solution of the homogeneous difference equation is
(1)
for the particular solution we refer table 11.2. for the first term of the right side of the
given equation we use the term , or . for the second term , we get or
, and thus, the particular solution has the form
but the term is also a term in the given equation. therefore, to eliminate the duplication,
we multiply the term by . thus, the correct form of the particular solution is
(2)
to evaluate the constants , , and , we substitute the last expression above into the given
equation. then,
using the law of exponents , simplifying, and equating like terms, we get
this relation will be true if
or
c1 c2
e2 e – 6 – ( ) y x 3 x = +
m 2–m – 6 = 0
m1 = –2 m2 = 3
yh
yh = k1(–2) x + k23x
x
a1x + a0 ax + b 3x a3x
c3x
yp = ax + b + c3x
c3x
c3x x
yp = ax + b + cx3x
ab c
a(x + 2) b c(x + 2) 3 x + 2 [ + + ? ] a(x + 1) b c(x + 1) 3 x + 1 –[ + + ? ]
–6[ax + b + cx3 x] = x + 3 x
w m + n = w m × w n
–6ax + (a–6b) + 15c3 x = x + 3 x
–6a = 1 a–6b = 0 15c = 1
a = –1 ? 6 b = 1 ? 36 c = 1 ? 15
chapter 11 difference equations
11-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
by substitution into (2), we obtain the particular solution
(3)
therefore, the total solution is the sum of (1) and (3), that is,
4.
(1)
the characteristic equation is
and its roots are and
from table 11.1, and . therefore, the homogeneous
part of the solution is
(2)
for the particular solution we refer table 11.2 where we find that the solution has the form
, and for this exercise
since the cosine and sine terms appear in the complimentary solution, we multiply the terms of
the particular solution by and we get
(3)
to evaluate the constants , , and , we substitute the last expression above into (1) and
we get
using the trig identities
expanding, rearranging, equating like terms, and combining the complimentary and particular
solutions we get
yp = (–1 ? 6)x + 1 ? 36 + (1 ? 15)x2 x
ytotal = yh + yp = k1(–2) x + k23 x + (–1 ? 6)x + 1 ? 36 + (1 ? 15)x2 x
(e2 + 1) y = sinx
m 2 + 1 = 0
m1 = j m2 = –j
r = (1)2 = 1 ? –11 ? 0 = tan = ? ? 2
yh = c1 cos(? ? 2)x + c2 sin(? ? 2)x
a1 cosmx + a2 sinmx
yp = acosx + bsinx
x
yp = ax cosx + bx sinx
ab c
a(x + 2) cos(x + 2) + b(x + 2) sin(x + 2) + ax cosx + bx sinx = sinx
cos(a + b) = cos acosb – sin asinb
sin(a + b) = sin acosb – sinbcosa
y c1
?2
x c2
?2
x sinx + sin(x – 2)
2(1 + cos2)
= cos + sin + -
numerical analysis using matlab and spreadsheets, second edition 12-1
orchard publications
chapter 12
partial fraction expansion
his chapter is an introduction to partial fraction expansion methods. in elementary algebra
we learned how to combine fractions over a common denominator. partial fraction expansion
is the reverse process and splits a rational expression into a sum of fractions having
simpler denominators.
12.1 partial fraction expansion
the partial fraction expansion method is used extensively in integration and in finding the
inverses of the laplace, fourier, and z transforms. this method allows us to decompose a rational
polynomial into smaller rational polynomials with simpler denominators, from which we can easily
recognize their integrals or inverse transformations. in the subsequent discussion we will discuss
the partial fraction expansion method and we will illustrate with several examples. we will
also use the matlab residue(r,p,k) function which returns the residues (coefficients) r of a
partial fraction expansion, the poles p and the direct terms k. there are no direct terms if the
highest power of the numerator is less than that of the denominator.
let
(12.1)
where and are polynomials and thus (12.1) can be expressed as
(12.2)
the coefficients and for are real numbers and, for the present discussion,
we have assumed that the highest power of is less than the highest power of , i.e.,
. in this case, is a proper rational function. if , is an improper rational function.
it is very convenient to make the coefficient of in (12.2) unity to do this, we rewrite it as
(12.3)
t
f(s) n(s)
d(s)
= -
n(s) d(s)
f(s) n(s)
d(s)
-
bmsm bm – 1sm – 1 bm – 2sm – 2 + + + …+ b1s + b0
ansn an – 1sn – 1 an – 2sn – 2 + + + … + a1s + a0
= =
ak bk k = 0, 1, 2, …, n
n(s) d(s)
m < n f(s) m ? n f(s)
an sn
f(s) n(s)
d(s)
-
1
an
- bmsm bm – 1sm – 1 bm – 2sm – 2 ( + + + …+ b1s + b0)
sn an – 1
an
-sn – 1 an – 2
an
-sn – 2 …
a1
an
-s
a0
an
+ + + + + -
= = -
chapter 12 partial fraction expansion
12-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the roots of the numerator are called the zeros of , and are found by letting in
(12.3). the roots of the denominator are called the poles of and are found by letting
.
the zeros and poles of (12.3) can be real and distinct, or repeated, or complex conjugates, or
combinations of real and complex conjugates. however, in most engineering applications we are
interested in the nature of the poles. we will consider the nature of the poles for each case.
case i: distinct poles
if all the poles of are distinct (different from each another), we can factor
the denominator of in the form
(12.4)
where is distinct from all other poles. then, the partial fraction expansion method allows us to
express (12.4) as
(12.5)
where are the residues of .
to evaluate the residue , we multiply both sides of (12.5) by then, we let , that
is,
(12.6)
example 12.1
use partial fraction expansion to simplify of (12.7) below.
(12.7)
solution:
(12.8)
f(s) n(s) = 0
f(s)
d(s) = 0
p1, p2, p3, …, pn f(s)
f(s)
f(s) n(s)
(s – p1) ? (s – p2) ? (s – p3) ? …? (s – pn)
=
pk
f(s)
r1
(s – p1)

r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
= + + + +
r1, r2, r3, …, rn f(s)
rk (s – pk) s ? pk
rk (s – pk)f(s)
s ? pk
lim (s – pk)f(s)
s = pk
= =
f1(s)
f1(s) 3s + 2
s2 + 3s + 2
= -
f1(s) 3s + 2
s2 + 3s + 2
- 3s + 2
(s + 1)(s + 2)
-
r1
(s + 1)

r2
(s + 2)
= = = +
numerical analysis using matlab and spreadsheets, second edition 12-3
orchard publications
partial fraction expansion
therefore, by substitution into (12.8), we get
(12.9)
we can us the matlab residue(r,p,k) function to verify our answers with the following code:
ns = [3, 2] ds = [1, 3, 2] [r, p, k] = residue(ns, ds)
r =
4
-1
p =
-2
-1
k =
[]
where we have denoted ns and ds as two vectors that contain the numerator and denominator
coefficients of . matlab displays the r, p, and k vectors these represent the residues,
poles, and direct terms respectively. the first value of the vector r is associated with the first value
of the vector p, the second value of r is associated with the second value of p, and so on. the vector
k is referred to as the direct term, and it is always empty (has no value) whenever is a
proper rational function. for this example, we observe that the highest power of the denominator
is whereas the highest power of the numerator is s and therefore, the direct term k is empty.
example 12.2
use partial fraction expansion to simplify of (12.10) below.
(12.10)
r1 (s + 1)f(s)
s ? –1
lim 3s + 2
(s + 2)

s = –1
= = = –1
r2 (s + 2)f(s)
s ? –2
lim 3s + 2
(s + 1)

s = –2
= = = 4
f1(s) 3s + 2
s2 + 3s + 2
- –1
(s + 1)
4
(s + 2)
= = +
f1(s)
f(s)
s2
f2(s)
f2(s) 3s2 + 2s + 5
s3 + 12s2 + 44s + 48
= -
chapter 12 partial fraction expansion
12-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
first, we will use the matlab function factor(s) to express the denominator polynomial of
in factored form.* this function returns an expression that contains the prime factors of a
polynomial. however, this function is used with symbolic expressions. these expressions are
explained below.
the functions, like roots(p), which we have used before, are display numeric expressions, that is,
they produce numerical results. symbolic expressions, on the other hand, can manipulate mathematical
expressions without using actual numbers. some examples of symbolic expressions are
given below.
matlab contains the so-called symbolic math toolbox. this is a collection of tools (functions)
which are used in solving symbolic expressions they are discussed in detail in matlab user’s
manual. for the present, our interest is in using the factor(s) to express the denominator of
(12.10) as a product of simple factors.
before using symbolic expressions, we must create a symbolic variable , , , etc. this is done
with the sym function. for example, s = sym (‘s’) creates the symbolic variable . alternately,
we can use the syms function to define one or more symbolic variables with a single statement.
for example,
syms x y z a1 k2
defines the symbolic variables , , , and .
returning to example 12.2 and using matlab we have:
syms s den=s^3+12*s^2+44*s+48 factor(den)
ans =
(s+4)*(s+2)*(s+6)
and thus,
next, we find the residues , , and . these are
* of course, we can use the roots(p) function. the factor(s) function is a good alternative.
f2(s)
sin2x e –?t y d 2
dt2 - 3t3 4t 2 – 5t 8 + + ( ) = u 1x
= ? dx
x y s t
s
x y z a1 k2
f2(s) 3s2 + 2s + 5
s3 + 12s
2 + 44s + 48
- 3s2 + 2s + 5
(s + 2)(s + 4)(s + 6)

r1
(s + 2)

r2
(s + 4)

r3
(s + 6)
= = = + +
r1 r2 r3
numerical analysis using matlab and spreadsheets, second edition 12-5
orchard publications
partial fraction expansion
therefore,
case ii: complex poles
quite often, the poles of a proper rational function are complex, and since complex poles
occur in complex conjugate pairs, the number of complex poles is even. thus if is a complex
pole, then its complex conjugate is also a pole. the partial fraction expansion method can
also be used in this case, as illustrated by the following example.
example 12.3
use partial fraction expansion to simplify of (12.11) below.
(12.11)
solution:
as a first step, we express the denominator in factored form to identify the poles of . using
the matlab code
syms s factor(s^3 + 5*s^2 + 12*s + 8)
we get
ans =
(s+1)*(s^2+4*s+8)
since the factor(s) function did not factor the quadratic term*, we will use the roots(p) function
to find its roots by treating it as a polynomial.
p=[1 4 8] roots_p=roots(p)
roots_p =
-2.0000+2.0000i
-2.0000-2.0000i
* for some undocumented reason, the factor(s) function does not seem to work with complex numbers.
r1
3s2 + 2s + 5
(s + 4)(s + 6)
-
s = –2
98
= = r2
3s2 + 2s + 5
(s + 2)(s + 6)
-
s = –4
= 37
4
=– - r3
3s2 + 2s + 5
(s + 2)(s + 4)
-
s = –6
= 89
8
= -
f2(s) 3s2 + 2s + 5
s3 + 12s
2 + 44s + 48
- 9 ? 8
(s + 2)
–37 ? 4
(s + 4)
89 ? 8
(s + 6)
= = + +
f(s)
pk
pk?
f3(s)
f3(s) s + 3
s3 + 5s2 + 12s + 8
= -
f3(s)
chapter 12 partial fraction expansion
12-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
then,
(12.12)
and the residues are
of course, the last evaluation was not necessary since or
and this is always true since complex roots occur in conjugate pairs. then, by substitution into
(12.12), we get
(12.13)
we can express (12.13) in a different form if we want to eliminate the complex presentation.
this is done by combining the last two terms on the right side of (12.13) to form a single term and
now is written as
(12.14)
case iii: multiple (repeated) poles
in this case, has simple poles but one of the poles, say , has a multiplicity . then,
(12.15)
f3(s) s + 3
s3 + 5s
2 + 12s + 8
- s + 3
(s + 1)(s + 2 + j2)(s + 2 – j2)
= =
s + 3
s3 + 5s
2 + 12s + 8
-
r1
(s + 1)

r2
(s + 2 + j2)
-
r3
(s + 2–j2)
= = + +
r1
s + 3
s2 + 4s + 8
-
s = –1
25
= =
r2
s + 3
(s + 1)(s + 2–j2)
-
s = – 2 – j2
1 – j2
(– 1 – j2)(–j4)
1 – j2
– 8 + j4
= = =
(1 – j2)
(– 8 + j4)
-(– 8 – j4)
(– 8 – j4)
- – 16 + j12
80
15
– j 3
20
= = = + -
r3
s + 3
(s + 1)(s + 2 + j2)

s = – 2 + j2
1 – j2
(– 1 + j2)(j4)
- 1 – j2
– 8 – j4
= = =
(1 – j2)
(– 8 – j4)
-(– 8 + j4)
(– 8 + j4)
- (–16)–j12
80
- 15
– j 3
20
= = = – -
r3 r2= ?
r3
15
– j 3
20
? + -?
? ?* 15
– j 3
20
= = – -
f3(s) 2 ? 5
(s + 2)
– 1 ? 5 + j3 ? 20
(s + 2 + j2)
- – 1 ? 5 – j3 ? 20
(s + 2–j2)
= + + -
f3(s) 2 ? 5
(s + 2)
15
(2s + 1)
(s2 + 4s + 8)
= – ?
f(s) p1 m
f(s) n(s)
(s – p1)m(s – p2)…(s – pn – 1)(s – pn)
= -
numerical analysis using matlab and spreadsheets, second edition 12-7
orchard publications
partial fraction expansion
and denoting the residues corresponding to multiple pole as , the partial
fraction expansion of (12.15) can be expressed as
(12.16)
for the simple poles we proceed as before, that is,
to find the first residue of the repeated pole, we multiply both sides of (12.16) by .
then,
(12.17)
next, taking the limit as on both sides of (12.17), we get
(12.18)
or
(12.19)
and thus (12.19) yields the residue of the first repeated pole.
to find the second residue of the second repeated pole , we first differentiate the relation
of (12.18) with respect to then, we let , that is,
(12.20)
to find the third residue of the repeated pole , we differentiate (12.18) twice with respect
m p1 r11, r12, … r1m
f(s)
r11
(s – p1)m -
r12
(s – p1)m – 1
r13
(s – p1)m – 2 …
r1m
(s – p1)
= + + + +
r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
+ + +
p1, p2, … pn
rk (s – pk)f(s)
s ? pk
lim (s – pk)f(s)
s = pk
= =
r11 (s – p1)m
(s – p1)mf(s) r11 (s – p1)r12 (s – p1)2r13 … (s – p1)m – 1= + + + + r1m
(s – p1)m r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
? + + + ?
+ ? ?
s ? p1
(s – p1)mf(s)
s ? p1
lim
r11 (s – p1)r12 (s – p1)2r13 … (s – p1)m – 1[ + + + r1m]
s ? p1
= + lim
(s – p1)m r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
? + + + ?
? ?
s ? p1
+ lim
r11 (s – p1)mf(s)
s ? p1
= lim
r12 p1
s s ? p1
r12
d
ds
-
s ? p1
= lim [(s – p1)mf(s)]
r13 p1
chapter 12 partial fraction expansion
12-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
to then, we let , that is,
(12.21)
this process is continued until all residues of the repeated poles have been found.
in general, for repeated poles the residue can be derived from the relation
(12.22)
whose derivative of both sides is
(12.23)
or
(12.24)
example 12.4
use partial fraction expansion to simplify of (12.25) below.
(12.25)
solution:
we observe that there is a pole of multiplicity at and thus, (12.25) in partial fraction
expansion form is
(12.26)
the residues are
s s ? p1
r13
d2
ds2 -
s ? p1
= lim [(s – p1)mf(s)]
r1k
(s – p1)mf(s) = r11 + r12(s – p1) + r13(s – p1)2 + …
(m – 1)th
(k – 1)!r1k
d k – 1
dsk – 1 [(s – p1)mf(s)]
s ? p1
= lim
r1k
1
(k – 1)!

s ? p1
lim d k 1 –
dsk – 1 = [(s – p1)mf(s)]
f4(s)
f4(s) s + 3
(s + 2)(s + 1)2 = -
2 s = –1
f4(s) s + 3
(s + 2)(s + 1)2 -
r1
(s + 2)

r21
(s + 1)2
r22
(s + 1)
= = + +
r1
s + 3
(s + 1)2
s = –2
= = 1
r21
s + 3
(s + 2)

s = –1
= = 2
r22
d
ds
- s + 3
s + 2
? - ?
? ?
s = –1
(s + 2) – (s + 3)
(s + 2)2
s = –1
= = = –1
numerical analysis using matlab and spreadsheets, second edition 12-9
orchard publications
partial fraction expansion
then, by substitution into (12.26),
instead of differentiation, the residue could be found by substitution of the already known
values of and into (12.26), and letting *, that is,
or from which as before.
to check our answers with matlab, we will use the expand(s) function. like the factor(s)
function, expand(s) is used with symbolic expressions. its description can be displayed with the
help expand command.
check with matlab:
syms s % create symbolic variable s
expand((s + 1)^2) % express it as a polynomial
ans =
s^2+2*s+1
ns = [1 3] % coefficients of the numerator n(s)
d1 = [1 2 1] % coefficients of (s + 1)^2 = s^2 + 2*s + 1 term in d(s)
d2 = [0 1 2] % coefficients of (s + 2) term in d(s)
ds=conv(d1,d2) % multiplies polynomials d1 and d2 to express denominator d(s) as polynomial
[r,p,k]=residue(ns,ds)
r =
1.0000
-1.0000
2.0000
p =
-2.0000
-1.0000
-1.0000
k =
[]
* we must remember that (2.45) is an identity, and as such, it is true for any value of s.
f4(s) s + 3
(s + 2)(s + 1)2 - 1
(s + 2)
2
(s + 1)2 –1
(s + 1)
= = + +
r22
r1 r21 s = 0
s + 3
(s + 1)2(s + 2)
-
s = 0
1
(s + 2)

s = 0
2
(s + 1)2
s = 0
r22
(s + 1)

s = 0
= + +
3 ? 2 = 1 ? 2 + 2 + r22 r22 = –1
chapter 12 partial fraction expansion
12-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 12.5
use partial fraction expansion to simplify of (12.27) below.
(12.27)
solution:
we observe that there is a pole of multiplicity 3 at , and a pole of multiplicity at .
then, in partial fraction expansion form
(12.28)
we find the residue by evaluating at as
(12.29)
the residue is found by first taking the first derivative of , and evaluating it at .
thus,
(12.30)
the residue is found by taking the second derivative of and evaluating it at .
then,
(12.31)
f5(s)
f5(s) s2 + 3s + 1
(s + 1)3(s + 2)2 =
s = –1 2 s = –2
f5(s)
r11
(s + 1)3
r12
(s + 1)2
r13
(s + 1)

r21
(s + 2)2
r22
(s + 2)
= + + + +
r11 f5(s) s = –1
r11
s2 + 3s + 1
(s + 2)2 -
s = –1
= = –1
r12 f5(s) s = –1
r12
d
ds
- s2 + 3s + 1
(s + 2)2 -
? ?
? ?
? ?
s = –1
=
(s + 2)2(2s + 3)–2(s + 2)(s2 + 3s + 1)
(s + 2)4
s = –1
s + 4
(s + 2)3 = = = 3
r13 f5(s) s = –1
r13
1
2!
d 2
ds2 - s2 + 3s + 1
(s + 2)2 -
? ?
? ?
? ?
s = –1
12
d
ds
- d
ds
- s2 + 3s + 1
(s + 2)2 -
? ?
? ?
? ?
s = –1
= =
12
d
ds
- s + 4
(s + 2)3 ? ?
? ?
s = –1
12
(s + 2)3 – 3(s + 2)2(s + 4)
(s + 2)6 = =
12
s + 2 – 3s – 12
(s + 2)4 ? - ?
? ?
s = –1
– s – 5
(s + 2)4
s = –1
= = = –4
numerical analysis using matlab and spreadsheets, second edition 12-11
orchard publications
partial fraction expansion
similarly, the residue if found by evaluating at , and the residue is found by
first taking the first derivative of and evaluating it at . therefore,
by substitution of these residues into (12.28), we get in partial fraction expansion as
(12.32)
we will now verify the values of these residues with matlab. before we do this, we introduce
the collect(s) function that we can use to multiply two or more symbolic expressions to obtain
the result in a polynomial form. its description can be displayed with the help collect command.
we must remember that the conv(p,q) function is used with numeric expressions, i.e., polynomial
coefficients only.
the matlab code for this example is as follows.
syms s % we must first define the variable s in symbolic form
% the function "collect" below multiplies (s+1)^3 by (s+2)^2
ds=collect(((s+1)^3)*((s+2)^2))
ds =
s^5+7*s^4+19*s^3+25*s^2+16*s+4
% we now use this result to express the denominator d(s) as a
% polynomial so we can use its coefficients with the "residue" function
%
ns=[1 3 1] ds=[1 7 19 25 16 4] [r,p,k]=residue(ns,ds)
r =
4.0000
1.0000
-4.0000
r21 f5(s) s = –2 r22
f5(s) s = –2
r21
s2 + 3s + 1
(s + 1)3 -
s = –2
= = 1
r22
d
ds
- s2 + 3s + 1
(s + 1)3 -
? ?
? ?
? ?
s = –2
=
(s + 1)3(2s + 3) – 3(s + 1)2(s2 + 3s + 1)
(s + 1)6
s = –2
=
r22
(s + 1)(2s + 3) – 3(s2 + 3s + 1)
(s + 1)4 -
s = –2
– s2 – 4s
(s + 1)4
s = –2
= = = 4
f5(s)
f5(s) –1
(s + 1)3 3
(s + 1)2 –4
(s + 1)
1
(s + 2)2 4
(s + 2)
= + + + +
chapter 12 partial fraction expansion
12-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.0000
-1.0000
p =
-2.0000
-2.0000
-1.0000
-1.0000
-1.0000
k =
[]
case for m ? n
our discussion thus far, was based on the condition that is a proper rational function, that
is, the highest power of the numerator is less than the highest power of the denominator,
i.e., . if , is an improper rational function, and before we apply the partial fraction
expansion, we must divide the numeraror by the denominator to obtain an
expression of the form
(12.33)
so that .
example 12.6
express of (12.34) below in partial expansion form.
(12.34)
solution:
in (12.34), and thus we need to express in the form of (12.33). by long division,
(12.35)
check with matlab:
ns = [1 2 2] ds = [1 1] [r, p, k] = residue(ns, ds)
r =
1
p =
-1
k =
f(s)
m n
m < n m ? n f(s)
n(s) d(s)
f(s) k0 k1s k2s2 … km – nsm – n n(s)
d(s)
= + + + + + -
m < n
f6(s)
f6(s) s2 + 2s + 2
s + 1
= -
m > n f6(s)
f6(s) s2 + 2s + 2
s + 1
- 1
s + 1
= = - + s + 1
numerical analysis using matlab and spreadsheets, second edition 12-13
orchard publications
alternate method of partial fraction expansion
1 1
the direct terms are the coefficients of the term and the constant in (2.54).
12.2 alternate method of partial fraction expansion
the partial fraction expansion method can also be performed by the equating the numerators procedure
thereby making the denominators of both sides the same, and then equating the numerators.
we assume that the degree on the numerator is less than the degree of the denominator. if
not, we first perform a long division and then work with the quotient and the remainder as
before.
we also assume that the denominator can be expressed as a product of real linear and quadratic
factors. if these assumptions prevail, we let be a linear factor of and we suppose
that is the highest power of that divides . then, we can express as
(12.36)
next, let be a quadratic factor of and suppose that is the highest
power of this factor that divides . now, we perform the following steps:
1. to this factor, we assign the sum of n partial fractions as shown below.
(12.37)
2. we repeat step 1 for each of the distinct linear and quadratic factors of .
3. we set the given equal to the sum of these partial fractions.
4. we multiply each term of the right side by the appropriate factor to make the denominators of
both sides equal.
5. we arrange the terms of both sides in decreasing powers of .
6. we equate the coefficients of corresponding powers of s.
7. we solve the resulting equations for the residues.
example 12.7
express of (12.38) below as a sum of partial fractions using the equating the numerators
procedure.
k = [1 1] s
n(s)
d(s)
s – a d(s)
(s – a)m s – a d(s) f(s)
f(s) n(s)
d(s)
-
r1
s – a
-
r2
(s – a)2 …
rm
(s – a)m = = + + -
s2 + ?s + ? d(s) (s2 + ?s + ?)n
f(s)
r1s + k1
s2 + ?s + ?
-
r2s + k2
(s2 + ?s + ?)2 …
rns + kn
(s2 + ?s + ?)n + + +
d(s)
f(s)
s
f7 (s)
chapter 12 partial fraction expansion
12-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(12.38)
solution:
by steps 1 through 3 above,
(12.39)
by step 4,
(12.40)
and by steps 5, 6, and 7,
(12.41)
relation (12.41) is an identity in therefore, the coefficients of each power of on the left and
right sides are equal. accordingly, by equating like powers of , we get
(12.42)
subtracting the second equation from the fourth in (12.42), we get
(12.43)
and by substitution into the first equation of (12.42), we get
(12.44)
next, substitution of (12.43) and (12.44) into the third equation of (12.42), yields
(12.45)
and using the fourth equation of (12.42, we get:
(12.46)
therefore in partial fraction expansion form becomes
f7 (s) – 2s + 4
(s2 + 1)(s – 1)2 = -
f7 (s) – 2s + 4
(s2 + 1)(s – 1)2 -
r1s + a
(s2 + 1)

r21
(s – 1)2
r22
(s – 1)
= = + + -
– 2s + 4 = (r1s + a)(s – 1)2 + r21(s2 + 1) + r22(s – 1)(s2 + 1)
– 2s + 4 = (r1 + r22)s3 + (– 2r1 + a – r22 + r21)s2
+ (r1 – 2a + r22)s + (a – r22 + r21)
s s
s
0 = r1 + r22
0 2r1 = – + a – r22 + r21
–2 = r1 – 2a + r22
4 = a – r22 + r21
4 2r1 = or r1 = 2
0 = 2 + r22 or r22 = –2
–2 = 2 – 2a – 2 or a = 1
4 = 1 + 2 + r21 or r21 = 1
f7 (s)
numerical analysis using matlab and spreadsheets, second edition 12-15
orchard publications
alternate method of partial fraction expansion
example 12.8
use the equating the numerators procedure to obtain the partial fraction expansion of in
(12.47) below.
(12.47)
solution:
this is the same rational function as that of example 12.3, where we found that the denominator
can be expressed in factored form of a linear and a quadratic factor, that is,
(12.48)
and in partial fraction expansion form,
(12.49)
as in example 12.3, we first find the residue of the linear factor as
(12.50)
to compute and , we use the equating the numerators procedure and we get
(12.51)
since is already known, we only need two equations in and . equating the coefficient of
on the left side, which is zero, with the coefficients of on the right side of (12.51), we get
(12.52)
with , (12.52) yields . to find the third residue , we equate the constant
terms of (12.51), that is, , and with , we get . then, by substitution
into (12.49), we get
f7 (s) – 2s + 4
(s2 + 1)(s – 1)2 - 2s + 1
(s2 + 1)
1
(s – 1)2 2
(s – 1)
= = + – -
f8 (s)
f8 (s) s + 3
s3 + 5s
2 + 12s + 8
= -
f7 (s) s + 3
(s + 1)(s2 + 4s + 8)
= -
f7 (s) s + 3
(s + 1)(s2 + 4s + 8)
-
r1
s + 1
-
r2s + r3
s2 + 4s + 8
= = + -
r1
s + 3
s2 + 4s + 8
-
s = –1
25=
=

r2 r3
(s + 3) = r1(s2 + 4s + 8) + (r2s + r3)(s + 1)
r1 r2 r3
s2 s2
0 = r1 + r2
r1 = 2 ? 5 r2 = –2 ? 5 r3
3 8r1 = + r3 r1 = 2 ? 5 r3 = –1 ? 5
f7 (s) 2 ? 5
(s + 2)
15
– (2s + 1)
(s2 + 4s + 8)
=
chapter 12 partial fraction expansion
12-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
as before. the remaining steps are the same as in example 12.3.
we will conclude the partial fraction expansion topic with a few more examples, using the residue(
r,p,k) function.
example 12.9
use the residue(r,p,k) function to compute the poles and residues of the function
(12.53)
solution:
let and be the poles (the denominator roots) and and be the residues. then,
can be written as
(12.54)
the matlab code for this example is as follows:
num=[0 8 2] % the semicolon suppress the display of the row vector typed
% and zero is typed to make the numerator have same number
% of elements as the denominator not necessary, but recommended
den=[1 3 2] [r,p,k]=residue(num,den)
r =
14
-6
p =
-2
-1
k =
[]
therefore, in partial fraction expansion form is written as
(12.55)
example 12.10
use the residue(r,p,k) function to compute the poles and residues of in (12.56) below.
(12.56)
f9(s) 8s + 2
s2 + 3s + 2
= -
p1 p2 r1 r2 f9 (s)
f9 (s)
r1
s + p1
-
r2
s + p2
= + -
f9(s)
f9 (s)
r1
s + p1
-
r2
s + p2
+ - 14
s + 2
- –6
s + 1
= = + -
f10 (s)
f10 (s) s + 3
(s + 1) s2 ( + 4s + 8)
= -
numerical analysis using matlab and spreadsheets, second edition 12-17
orchard publications
alternate method of partial fraction expansion
solution:
let , , and be the poles (the denominator roots) and , ,and be the residues of
. then, it can be written as
(12.57)
the poles and the residues can be found with the statement [r,p,k]=residue(num, den). before
we use this statement, we need to express the denominator as a polynomial. we will use the function
conv(a,b) to multiply the two factors of the denominator of (12.56).
we recall that we can write two or more statements on one line if we separate them by commas
or semicolons. we also recall that commas will display the results, whereas semicolons will suppress
the display. then,
a=[1 1] b=[1 4 8] c=conv(a,b) c, num=[1,3] den=c
[r,p,k]=residue(num,den)
c =
1 5 12 8
r =
-0.2000- 0.1500i
-0.2000+ 0.1500i
0.4000
p =
-2.0000+ 2.0000i
-2.0000- 2.0000i
-1.0000
k =
[]
therefore, in partial fraction expansion form is
(12.58)
by repeated use of the deconv(num,den) function, we can reduce a rational polynomial to simple
terms of a polynomial, where the last term is a rational polynomial whose order of the numerator
is less than that of the denominator as illustrated by the following example.
example 12.11
use the deconv(num,den) function to express the following rational polynomial as a polynomial
with four terms.
p1 p2 p3 r1 r2 r3
f10(s)
f10 (s)
r1
s + p1
-
r2
s + p2
-
r3
s + p3
= + + -
f10 (s)
f10 (s)
r1
s + p1
-
r2
s + p2
-
r3
s + p3
+ + - – 0.2 – 0.15j
s + 2 – 2j
– 0.2 + 0.15j
s + 2 + 2j
0.4
s + 1
= = + + -
chapter 12 partial fraction expansion
12-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(12.59)
solution:
num=[1 2 0 1] den=[0 0 0.5 ?1] [q,r]=deconv(num,den)
q =
2 8 16
r =
0 0 0 17
therefore, can now be written as
(12.60)
it is important to remember that the function roots(p) is used with polynomials only. if we want
to find the zeros of any function, such as the function defined as
(12.61)
we must use the function fzero(‘function’,x0), where function is a pre-defined string, and x0 is a
required initial value. we can approximate this value by first plotting to find out where it
crosses the -axis. this was discussed in chapter 1.
12.3 summary
• the function
where the coefficients and for are real numbers, is a proper rational
function if the highest power of the numerator is less than the highest power of of the
denominator , i.e., . if , is an improper rational function.
• partial fraction expansion applies only to proper rational functions. if is an improper
rational function we divide the numeraror by the denominator to obtain an expression
of the form
so that .
f1(x) x3 2x2 + + 1
0.5x – 1
= -
f1(x)
f1(x) 2x2 8x 16 17
0.5x – 1
= + + + -
f2(x)
f2(x) 3x3 7x2 + + 9
12x6 2x4 13x2 ( + + + 25)
- 0.5x5 6.3x2 + + 4.35
23x6 16x3 ( + + 7.5x)
1
4.11x + 2.75
= + +
f2(x)
x
f(s) n(s)
d(s)
-
bmsm bm – 1sm – 1 bm – 2sm – 2 + + + … + b1s + b0
ansn an – 1sn – 1 an – 2sn – 2 + + + … + a1s + a0
= =
ak bk k = 0, 1, 2, …, n
n(s)
d(s) m < n m ? n f(s)
f(s)
n(s) d(s)
f(s) k0 k1s k2s2 … km – nsm – n n(s)
d(s)
= + + + + + -
m < n
numerical analysis using matlab and spreadsheets, second edition 12-19
orchard publications
summary
• if the function
is a proper rational function where is a non-zero integer other than unity, we rewrite this
function as
to make unity.
• the roots of the numerator are called the zeros of , and are found by letting ,
and the roots of the denominator are called the poles of and are found by letting
.
• the zeros and poles can be real and distinct, or repeated, or complex conjugates, or combinations
of real and complex conjugates. in most engineering applications we are interested in the
nature of the poles.
• if all the poles of are distinct we can factor the denominator of
in the form
where is distinct from all other poles. then, the partial fraction expansion method allows us
to write the above expression as
where are the residues of . to evaluate the residue , we multiply both
sides of (12.5) by then, we let , that is,
• we can use the matlab residue(r,p,k) function to verify our answers. this function returns
the residues, their associated poles, and a direct term. for proper rational functions there is no
direct term.
f(s) n(s)
d(s)
-
bmsm bm – 1sm – 1 bm – 2sm – 2 + + + … + b1s + b0
ansn an – 1sn – 1 an – 2sn – 2 + + + … + a1s + a0
= =
an
f(s) n(s)
d(s)
-
1
an
- bmsm bm – 1sm – 1 bm – 2sm – 2 ( + + + … + b1s + b0)
sn an – 1
an
-sn – 1 an – 2
an
-sn – 2 …
a1
an
-s
a0
an
+ + + + + -
= = -
an
f(s) n(s) = 0
f(s)
d(s) = 0
p1, p2, p3, …, pn f(s) f(s)
f(s) n(s)
(s – p1) ? (s – p2) ? (s – p3) ? … ? (s – pn)
=
pk
f(s)
r1
(s – p1)

r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
= + + + +
r1, r2, r3, …, rn f(s) rk
(s – pk) s ? pk
rk (s – pk)f(s)
s ? pk
lim (s – pk)f(s)
s = pk
= =
chapter 12 partial fraction expansion
12-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the partial fraction expansion can also be used if the poles are complex. since complex poles
occur in conjugate pairs, if is a complex pole, then its complex conjugate is also a pole.
• if a rational function has simple poles but one of the poles, say , has a multiplicity ,
the function is expressed as
and denoting the residues corresponding to multiple pole as , the partial
fraction expansion can be expressed as
• if a rational function has simple poles but one of the poles, say , has a multiplicity ,
for the simple poles we use the same procedure as for distinct poles. the first residue of a
repeated pole is found from
the second repeated pole is found from
the third from
and this process is continued until all residues of the repeated poles have been found.
• with the alternate method of partial fraction expansion we use the equating the numerators
procedure thereby making the denominators of both sides the same, and then equating the
numerators. we assume that the denominator can be expressed as a product of real linear
and quadratic factors.
pk pk?
f(s) p1 m
f(s) n(s)
(s – p1)m(s – p2)…(s – pn – 1)(s – pn)
= -
m p1 r11, r12, … r1m
f(s)
r11
(s – p1)m -
r12
(s – p1)m – 1
r13
(s – p1)m – 2 …
r1m
(s – p1)
= + + + +
r2
(s – p2)

r3
(s – p3)

rn
(s – pn)
+ + +
f(s) p1 m
r11 (s – p1)mf(s)
s ? p1
= lim
r12
d
ds
-
s ? p1
= lim [(s – p1)mf(s)]
r13
d2
ds2 -
s ? p1
= lim [(s – p1)mf(s)]
d(s)
numerical analysis using matlab and spreadsheets, second edition 12-21
orchard publications
exercises
12.4 exercises
perform partial fraction expansion for the following. use matlab to simplify and to verify your
results.
1.
2.
3.
4.
5.
6.
7.
8.
1
1–s2
-
1
s2 + 4s – 5
-
s
s2–2s – 3

5s – 3
s2–2s – 3

s2
s2 + 2s + 1
-
1
s(s + 1)2 -
1
(s + 1)(s2 + 1)
-
1
s(s2 + s + 1)

chapter 12 partial fraction expansion
12-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
12.5 solutions to exercises
1.
then,
ns = [0, 0, ?1] ds = [1, 0, ?1] [r, p, k] = residue(ns, ds)
r =
0.5000
-0.5000
p =
-1
1
k =
[]
2.
then,
format rat ns = [0, 0, 1] ds = [1, 4, ?5] [r, p, k] = residue(ns, ds)
r =
-1/6
1/6
p =
-5
1
k =
[]
1
1–s2
- –1
s2 – 1
- –1
(s + 1)(s – 1)

r1
s + 1
-
r2
s – 1
= = = + -
r1
–1
s + 1
-
s = 1
= = –1 ? 2 r2
–1
s – 1
-
s = –1
= = 1 ? 2
–1
s2 – 1
- 1 ? 2
s + 1
– - 1 ? 2
s – 1
= + -
1
s2 + 4s – 5
- 1
(s – 1)(s + 5)

r1
s – 1
-
r2
s + 5
= = + -
r1
1
s + 5
-
s = 1
= = 1 ? 6 r2
1
s – 1
-
s = –5
= = –1 ? 6
1
s2 + 4s – 5
- 1 ? 6
s – 1
- 1 ? 6
s + 5
= – -
numerical analysis using matlab and spreadsheets, second edition 12-23
orchard publications
solutions to exercises
3.
then,
format rat ns = [0, 1, 0] ds = [1, ?2, ?3] [r, p, k] = residue(ns, ds)
r =
3/4
1/4
p =
3
-1
k =
[]
4.
then,
ns = [0, 5, ?3] ds = [1, ?2, ?3] [r, p, k] = residue(ns, ds)
r =
3
2
p =
3
-1
k =
[]
s
s2–2s – 3
s
(s + 1)(s – 3)

r1
s + 1
-
r2
s – 3
= = + -
r1
s
s – 3
-
s = –1
= = 1 ? 4 r2
s
s + 1
-
s = 3
= = 3 ? 4
s
s2–2s – 3
1 ? 4
s + 1
- 3 ? 4
s – 3
= + -
5s – 3
s2–2s – 3
5s – 3
(s + 1)(s – 3)

r1
s + 1
-
r2
s – 3
= = + -
r1
5s – 3
s – 3

s = –1
= = 2 r2
5s – 3
s + 1

s = 3
= = 3
5s – 3
s2–2s – 3
2
s + 1
- 3
s – 3
= + -
chapter 12 partial fraction expansion
12-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
5.
this is an improper rational function, and before we apply the partial fraction expansion, we
must divide the numeraror by the denominator to obtain an expression of the form
we could perform long division but we will use the matlab deconv(num,den) function to
express the following rational polynomial as a polynomial with four terms.
num=[1 0 0 ] den=[1 2 1] [q,r]=deconv(num,den)
q =
1
r =
0 -2 -1
and thus
then,
ns = [1, 0, 0] ds = [1, 2, 1] [r, p, k] = residue(ns, ds)
r =
-2
1
p =
-1
-1
k =
1
6.
n(s) d(s)
f(s) k0 k1s k2s2 … km – nsm – n n(s)
d(s)
= + + + + + -
s2
s2 + 2s + 1
- 1 –2s – 1
s2 + 2s + 1
+ - 1 2s + 1
(s + 1)2 – 1
r1
(s + 1)2
r2
(s + 1)
= = = – +
–2s – 1
(s + 1)2
r1
(s + 1)2
r2
(s + 1)
= + –2s – 1 = r1 + r2(s + 1)
r2 = –2 r1 + r2 = –1 r1 = 1
s2
s2 + 2s + 1
- 1 –2s – 1
s2 + 2s + 1
+ - 1 1
(s + 1)2 –2
(s + 1)
= = + +
1
s(s + 1)2 -
r1
s

r21
(s + 1)2
r22
(s + 1)
= + +
numerical analysis using matlab and spreadsheets, second edition 12-25
orchard publications
solutions to exercises
then,
syms s expand(s*(s+1)^2)
ans =
s^3+2*s^2+s
ns = [0, 0, 0, 1] ds = [1, 2, 1, 0] [r, p, k] = residue(ns, ds)
r =
-1
-1
1
p =
-1
-1
0
k =
[]
7.
equating numerators and like terms we get
syms r1 r2 r3
eq1=r1+r2?0
eq2=r2+r3?0
eq3=r1+r3?1
s=solve(eq1, eq2, eq3)
eq1 =
r1+r2
eq2 =
r1
1
s + 1
-
s = 0
= = 1 r21
1s

s = –1
= = –1 r22
d
ds
- 1s
? ?
? ?
s = –1
1
s2 –
s = –1
= = = –1
1
s(s + 1)2 - 1s
–1
(s + 1)2 –1
(s + 1)
= + +
1
(s + 1)(s2 + 1)
-
r1
s + 1
-
r2s + r3
s2 + 1
+
r1(s2 + 1)
(s + 1)(s2 + 1)
-
(r2s + r3)(s + 1)
(s + 1)(s2 + 1)
= = +
1 = r1s2 + r1 + r2s2 + r2s + r3s + r3
r1 + r2 = 0 r2 + r3 = 0 r1 + r3 = 1
chapter 12 partial fraction expansion
12-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
r2+r3
eq3 =
r1+r3-1
s =
r1: [1x1 sym]
r2: [1x1 sym]
r3: [1x1 sym]
s.r1
ans =
1/2
s.r2
ans =
-1/2
s.r3
ans =
1/2
the statement s=solve(eq1, eq2, eq3, ...eqn) returns the solutions in the structure s whose
named fields hold hold the solution for each variable. thus, , , and
. then,
syms s expand((s+1)*(s^2+1))
ans =
s^3+s+s^2+1
ns = [0, 0, 0, 1] ds = [1, 1, 1, 1] [r, p, k] = residue(ns, ds)
r =
1/2
-1/4 - 1/4i
-1/4 + 1/4i
p =
-1
-1/6004799503160662 + 1i
-1/6004799503160662 - 1i
k =
[]
r1 = 1 ? 2 r2 = –1 ? 2
r3 = 1 ? 2
1
(s + 1)(s2 + 1)
- 1 ? 2
s + 1
- (–1 ? 2)s + 1 ? 2
s2 + 1
= +
numerical analysis using matlab and spreadsheets, second edition 12-27
orchard publications
solutions to exercises
these values are inconsistent with those we’ve found. the matlab help residue command
displays the following:
warning: numerically, the partial fraction expansion of a ratio of
polynomials represents an ill-posed problem. if the denominator
polynomial, a(s), is near a polynomial with multiple roots, then
small changes in the data, including roundoff errors, can make arbitrarily
large changes in the resulting poles and residues. problem
formulations making use of state-space or zero-pole representations
are preferable.
8.
equating numerators and like terms we get
by inspection, , , and . then,
syms s expand(s*(s^2+s+1))
ans =
s^3+s^2+s
ns = [0, 0, 0, 1] ds = [1, 1, 1, 0] [r, p, k] = residue(ns, ds)
r =
-1/2 + 390/1351i
-1/2 - 390/1351i
1
p =
-1/2 + 1170/1351i
-1/2 - 1170/1351i
0
k =
[]
as in exercise 7, these values are inconsistent with those we’ve found.
1
s(s2 + s + 1)

r1
s

r2s + r3
s2 + s + 1
+
r1(s2 + s + 1)
s(s2 + s + 1)

(r2s + r3)s
s(s2 + s + 1)
= = +
1 = r1s2 + r1s + r1 + r2s2 + r3s
r1 + r2 = 0 r2 + r3 = 0 r1 = 1
r1 = 1 r2 = –1 r3 = 1
1
(s + 1)(s2 + 1)
- 1s
– s + 3
s2 + s + 1
= +
chapter 12 partial fraction expansion
12-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 13-1
orchard publications
chapter 13
the gamma and beta functions and distributions
his chapter is an introduction to the gamma and beta functions and their distributions used
with many applications in science and engineering. they are also used in probability, and in
the computation of certain integrals.
13.1 the gamma function
the gamma function, denoted as , is also known as generalized factorial function. it is defined as
(13.1)
and this improper* integral converges (approaches a limit) for all .
we will derive the basic properties of the gamma function and its relation to the well known factorial
function
(13.2)
we will evaluate the integral of (13.1) by performing integration by parts using the relation
(13.3)
letting
(13.4)
we get
(13.5)
then, with (13.3), we write (13.1) as
* improper integrals are two types and these are:
a. where the limits of integration or or both are infinite
b. where becomes infinite at a value between the lower and upper limits of integration inclusive.
t
?(n)
?(n) xn – 1e–x dx
0
?
= ?
n > 0
f(x)dx
a
b
? a b
f(x)dx
a
b
? f(x) x
n! = n(n – 1)(n – 2)…3 ? 2 ? 1
?udv = uv – ?vdu
u e–x and dv xn – 1 = =
du e–x – dx and v xn
n
= =
chapter 13 the gamma and beta functions and distributions
13-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(13.6)
with the condition that , the first term on the right side of (13.6) vanishes at the lower limit,
that is, for . it also vanishes at the upper limit as . this can be proved with l’ hôpital’s
rule* by differentiating both numerator and denominator times, where .then,
(13.7)
therefore, (13.6) reduces to
(13.8)
and with (13.1) we have
(13.9)
by comparing the two integrals of (13.9), we see that
(13.10)
or
* quite often, the ratio of two functions, such as , for some value of , say , results in the indeterminate form
. to work around this problem, we consider the limit , and we wish to find this limit, if it exists. l’hôpital’s
rule states that if , and if the limit as approaches exists, then,
?(n) xne–x
n

x = 0
?
1n
xne–x dx
0
?
= + ?
n > 0
x = 0 x ? ?
m m ? n
f(x)
g(x)
x a
f(a)
g(a)
00
= f(x)
g(x)

x ? a
lim
f(a) = g(a) = 0 d
dx
-f(x) d
dx
? -g(x) x a
f(x)
g(x)

x? a
lim d
dx
-f(x) d
dx
? ? -g(x)?
? ?
x ? a
= lim
xne–x
n

x ? ?
lim xn
nex -
x ? ?
lim xm
m
d
d xn
xm
m
d
d nex

x ? ?
lim xm 1 –
m – 1
d
d nxn – 1
xm – 1
m – 1
d
d ne
x -
x ? ?
= = = lim = …
n(n – 1)(n – 2)…(n – m + 1)xn – m
nex
x ? ?
= lim
(n – 1)(n – 2)…(n – m + 1)
xm – nex
x ? ?
= lim = 0
? n ( ) 1n
xne–x dx
0
?
= ?
?(n) xn – 1e–x dx
0
?
? 1n
xne–x dx
0
?
= = ?
?(n) ?(n + 1)
n
=
numerical analysis using matlab and spreadsheets, second edition 13-3
orchard publications
the gamma function
(13.11)
it is convenient to use (13.10) for , and (13.11) for .
from (13.10), we see that becomes infinite as .
for , (13.1) yields
(13.12)
thus, we have derived the important relation,
(13.13)
from the recurring relation of (13.11), we obtain
(13.14)
and in general
(13.15)
the formula of (13.15) is a very useful relation it establishes the relationship between the
function and the factorial .
we must remember that, whereas the factorial is defined only for zero (recall that )
and positive integer values, the gamma function exists (is continuous) everywhere except at
and negative integer numbers, that is, , and so on. for instance, when , we can
find in terms of , but if we substitute the numbers and so on in
(13.11), we get values which are not consistent with the definition of the function, as
defined in that relation.
stated in other words, the function is defined for all positive integers and positive fractional values,
and for all negative fractional, but not negative integer values.
we can use matlab’s gamma(n) function to plot versus . this is done with the code
below which produces the plot shown in figure 13.1.
n=?4: 0.05: 4 g=gamma(n) plot(n,g) axis([?4 4 ?6 6]) grid
title( the gamma function ) xlabel( n ) ylabel( gamma(n) )
figure 13.1 shows the plot of the function versus .
n?(n) = ?(n + 1)
n < 0 n > 0
?(n) n ? 0
n = 1
?(1) e–x dx
0
? ? e–x
0
?
= = – = 1
?(1) = 1
?(2) = 1 ? ?(1) = 1
?(3) = 2 ? ?(2) = 2 ? 1 = 2!
?(4) = 3 ? ?(3) = 3 ? 2 = 3!
?(n + 1) = n! for n = 1, 2, 3, …
?(n)
n!
n! 0! = 1
0
–1, –2, –3 n = –0.5
?(–0.5) ?(0.5) 0, –1, –2, –3
?(n)
?(n)
?(n) n
?(n) n
chapter 13 the gamma and beta functions and distributions
13-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 13.1. plot of the gamma function
numerical values of for , can be found in math tables, but we can use (13.10) or
(13.11) to compute values outside this range. of course, we can use matlab to find any valid
values of .
example 13.1
compute:
a. b. c.
solution:
a. from (13.11)
then,
and from math tables
therefore,
b. from (13.10)
then,
?(n) 1 ? n ? 2
n
?(3.6) ?(0.5) ?(–0.5)
?(n + 1) = n?(n)
?(3.6) = 2.6?(2.6) = (2.6)(1.6)?(1.6)
?(1.6) = 0.8953
?(3.6) = (2.6)(1.6)(0.8953) = 3.717
?(n) ?(n + 1)
n
=
?(0.5) ?(0.5 + 1)
0.5
- ?(1.5)
0.5
= =
numerical analysis using matlab and spreadsheets, second edition 13-5
orchard publications
the gamma function
and from math tables
therefore,
c. from (13.10)
then,
and using the result of (b),
we can verify these answers with matlab as follows:
a=gamma(3.6), b=gamma(0.5), c=gamma(?0.5)
a =
3.7170
b =
1.7725
c =
-3.5449
excel does not have a function which evaluates directly. it does, however, have the gammaln(
x) function. therefore, we can use the exp(gammaln(n)) function to evaluate at
some positive value of . but because it first computes the natural log, it does not produce an
answer if is negative as shown in figure 13.2.
figure 13.2. using excel to find
?(1.5) = 0.8862
?(0.5) = (2)(0.8862) = 1.772
?(n) ?(n + 1)
n
=
?(–0.5) ?(– 0.5 + 1)
–0.5
- ?(0.5)
–0.5
= = = –2?(0.5)
?(–0.5)= –2?(0.5) = (–2)(1.772) = –3.544
?(n)
?(n)
n
n
exp(gammaln(x))=
x gammaln(x) gamma(x)
3.6 1.3129 3.7170
0.5 0.5724 1.7725
-0.5 #num! #num!
?(n)
chapter 13 the gamma and beta functions and distributions
13-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 13.2
prove that when is a positive integer, the relation
(13.16)
is true.
proof:
from (13.11),
(13.17)
then,
(13.18)
next, replacing n with on the left side of (13.18), we get
(13.19)
substitution of (13.19) into (13.18) yields
(13.20)
by repeated substitutions, we get
(13.21)
and since , we have
(13.22)
or
(13.23)
example 13.3
use the definition of the function to compute the exact value of
solution:
from (13.1),
(13.24)
then,
(13.25)
n
?(n) = (n – 1)!
?(n + 1) = n?(n)
?(n) = (n – 1)?(n – 1)
n – 1
?(n – 1) = (n – 2)?(n – 2)
?(n) = (n – 1)(n – 2)?(n – 2)
n
?(n) = (n – 1)(n – 2)(n – 3)…1?(1)
?(1) = 1
?(n) = (n – 1)(n – 2)(n – 3)…1
?(n) = (n – 1)!
?(n) ? ( 1 ? 2 )
?(n) xn – 1e–x dx
0
?
= ?
? 12
? ?
? ? x0.5 – 1e–x dx
0
?
? x–0.5e–x dx
0
?
= = ?
numerical analysis using matlab and spreadsheets, second edition 13-7
orchard publications
the gamma function
letting
we get
or
by substitution of the last three relations into (13.25), we get
(13.26)
next, we define as a function of both and , that is, we let
(13.27)
(13.28)
multiplication of (13.27) by (13.28) yields
(13.29)
now, we convert (13.29) to polar coordinates by making the substitution
(13.30)
and by recalling that:
1. the total area of a region is found by either one of the double integrals
(13.31)
2. from differential calculus
(13.32)
then,
(13.33)
we observe that as ,
x = y2
dx
dy
- = 2y
dx = 2ydy
? 12
? ?
? ? y2(–0.5)e–y22ydy
0
?
? 2 y–1ye–y2dy
0
?
? 2 e–y2dy
0
?
= = = ?
? ( 1 ? 2 ) x y
? 12
? ?
? ? 2 e–x2dx
0
?
= ?
? 12
? ?
? ? 2 e–y2dy
0
?
= ?
? 12
? ?
? ? 2
4 e–x2dx e–y2dy
0
?
? 0
?
? 4 e–(x2 + y2) dxdy
0
?
? 0
?
= = ?
?2 = x2 + y2
a = ??dxdy = ??rdrd?
d
du
eu2
eu2 d
du
u2 2ueu2
= =
?e –?2 d?
?1
?2 ? 12
– e –?2 =
x ? ? and y ? ?
chapter 13 the gamma and beta functions and distributions
13-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(13.34)
substitution of (13.30), (13.33) and (13.34) into (13.29) yields
and thus, we have obtained the exact value
(13.35)
example 13.4
compute:
a. b . c.
solution:
using the relations
we get:
a. for ,
b. for ,
c. for ,
other interesting relations involving the function are:
(13.36)
(13.37)
? ? ? and ? ? ? ? 2
? 12
? ?
? ? 2
–2 e –?2
? = 0
?
? ?
? ? d?
0
? ? 2
? –2 (0 – 1)d?
0
? ? 2
? 2 d?
0
? ? 2
? 2? 0
? ? 2 = = = = = ?
? 12
? ?
? ? = ?
? (–0.5) ? (–1.5) ? (–2.5)
?(n) ? (n + 1)
n
= and ? (0.5) = ?
n = –0.5
? (–0.5) ? (0.5)
–0.5
- ?
–0.5
= = = –2 ?
n = –1.5
? (–1.5) ? (– 1.5 + 1)
–1.5
- ? (–0.5)
–1.5
–2 ?
–1.5
- 43
= = = = ?
n = –2.5
? (–2.5) ? (– 2.5 + 1)
–2.5
- ? (–1.5)
–2.5

43
?
–2.5
8
15
= = = = – - ?
?(n)
?(n)?(1 – n) ?
sinn?
=
for 0 < n < 1
22n – 1? n ( )? n 12
? + ?
? ? = ??(2n)
for any n ? negative integer
numerical analysis using matlab and spreadsheets, second edition 13-9
orchard publications
the gamma function
(13.38)
relation (13.38) is referred to as stirling’s asymptotic series for the function. if is a positive
integer, the factorial can be approximated as
(13.39)
example 13.5
use (13.36) to prove that
proof:
or
therefore,
example 13.6
compute the product
solution:
using (13.36), we get
or
?(n + 1) = n!
2?nnne–n 1 1
12n
- 1
288n2 139
51840n3 571
2488320n4 + + – – +…
? ?
? ?
? ?
=
?(n) n
n!
n! 2?nnne–n ?
? 12
? ?
? ? = ?
? 12
? ?
? ?? 1 12
? – ?
? ? ? 12
? ?
? ?? 12
? ?
? ? ?
?2
sin
= = -
? 12
? ?
? ? 2
= ?
? 12
? ?
? ? = ?
? 13
? ?
? ?? 23
? ?
? ?
? 13
? ?
? ?? 1 13
? – ?
? ? ?
?
3
sin
=
? 13
? ?
? ?? 23
? ?
? ? ?
3 ? 2
- 2?
3
2 3?
3
= = = -
chapter 13 the gamma and beta functions and distributions
13-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 13.7
use (13.37) to find
solution:
example 13.8
use (13.39) to compute
solution:
we use matlab as a calculator, that is, we type and execute the expression
sqrt(2*pi*50)*50^50*exp(?50)
ans =
3.0363e+064
this is an approximation. to find the exact value, we use the relation and the
matlab gamma(n) function. then,
gamma(50+1)
ans =
3.0414e+064
we can check this answer with the excel fact(n) function, that is,
=fact(50) and excel displays 3.04141e+64
the function is very useful in integrating some improper integrals. some examples follow.
? 32
? ?
? ?
23 – 1? 32
? ?
? ?? 32
12
? + ?
? ? ?? 2 32
? ? ?
= ? ?
22? 32
? ?
? ? ?(2) = ??(3)
? 32
? ?
? ? ??(3)
4?(2)
2! ?
4 ? 1
- ?
2
= = =
50!
50! 2? × 50 × 5050 e–50 ? ×
?(n + 1) = n!
?(n)
numerical analysis using matlab and spreadsheets, second edition 13-11
orchard publications
the gamma function
example 13.9
using the definition of the function, evaluate the integrals
a. b.
solution:
by definition,
then,
a.
b.
let then, , and by substitution,
example 13.10
a negatively charged particle is meters apart from the positively charged side of an electric
field. it is initially at rest, and then moves towards the positively charged side with a force
inversely proportional to its distance from it. assuming that the particle moves towards the center
of the positively charged side, considered to be the center of attraction , derive an expression
for the time required the negatively charged particle to reach in terms of the distance
and its mass .
solution:
let the center of attraction be the point zero on the -axis, as indicated in figure 13.3.
figure 13.3. sketch for example 13.10
by newton’s law,
?(n)
x4e–x dx
0
?
? x5e–2x dx
0
?
?
xn – 1e–x dx
0
?
? = ?(n)
x4e–x dx
0
?
? = ?(5) = 4! = 24
2x = y dx = dy ? 2
x5e–2x dx
0
?
? y2
? ?
? ?5
e–ydy
2
-
0
?
? 1
26 - y5e–y dy
0
?
= = ?
?(6)
64
- 5!
64
- 120
64
15
8
= = = = -
?
0
0 ?
m
0 x
?
0
movement of particle
x
chapter 13 the gamma and beta functions and distributions
13-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(13.40)
where
= mass of particle
= distance (varies with time)
= positive constant of proportionality and the minus (?) sign indicates that the distance
decreases as time increases.
at , the particle is assumed to be located on the -axis at point , and moves towards
the origin at . let the velocity of the particle be . then,
(13.41)
and
(13.42)
substitution of (13.42) into (13.40) yields
(13.43)
or
(13.44)
integrating both sides of (13.44), we get
(13.45)
where represents the constants of integration of both sides, and it is evaluated from the initial
condition that when . then,
(13.46)
and by substitution into (13.45),
(13.47)
solving for and taking the square root of both sides we get
mdx2
dt2 kx
= –
m
x
k x
t
t = 0 x x = ?
x = 0 v
dx
dt
- = v
dx2
dt2 dv
dt
- dv
dx
-dx
dt
- vdv
dx
= = = -
mvdv
dx
- kx
= –
mvdv kx
= – (dx)
mv2
2
- = –k lnx + c
c
v = 0 x = ?
c = k ln?
mv2
2
- k ? ln k x ln – k ?x
= = ln -
v2
numerical analysis using matlab and spreadsheets, second edition 13-13
orchard publications
the gamma function
(13.48)
since decreases as increases, we choose the negative sign, that is,
(13.49)
solving (13.49) for we get
(13.50)
we are interested in the time required for the particle to reach the origin . we denote this
time as it is found from the relation below, noting that the integration on the right side is
with respect to the distance x where at , , and at , . then,
(13.51)
to simplify (13.51), we let
(13.52)
or
(13.53)
also, since
the lower and upper limits of integration in (13.51), are being replaced with and respectively.
therefore, we express (13.51) as
finally, using the definition of the function, we have
(13.54)
v dx
dt
- 2k
m
- ?x
= = ± ln -
x t
dx
dt
- 2k
m
- ?x
= – ln -
dt
dt m
2k
– - dx
ln(? ? x)
=
0
t
t = 0 x = ? ? = t x = 0
t d?
0
t
? m
2k
– - dx
ln(? ? x)

?
0
= = ?
y ?x
? - ?
? ? then e y , ln ?x
= = -
x ?e–y , and dx ?e–y= = – dy
?x
? -?
ln? ?
x ? ?
lim 0 and ?x
? -?
ln? ?
x ? 0
= lim = ?
0 ?
t m
2k
– - ?e –y– dy
y

0
?
? ? m
2k
- y–1 ? 2e–ydy
0
?
= = ?
?(n)
t ?? 12
? ?
? ? m
2k
- ? ? m
2k
- ? ?m
2k
= = = -
chapter 13 the gamma and beta functions and distributions
13-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
example 13.11
evaluate the integrals
(13.55)
solution:
from the definition of the function,
(13.56)
also,
(13.57)
for and , multiplication of (13.56) by (13.57) yields
(13.58)
where and are dummy variables of integration. next, letting and , we get
and . then, with these substitutions, relation (13.58) it written as
(13.59)
next, we convert (13.59) to polar coordinates by letting and then,
(13.60)
to simplify (13.60), we let then, and thus relation (13.60) is written as
(13.61)
cosn?d?
0
? ? 2
? and sinn?d?
0
? ? 2
?
?(n)
?(n) xn – 1e–x dx
0
?
= ?
?(m) xm – 1e–x dx
0
?
= ?
m > 0 n > 0
?(m)?(n) um – 1e–u du
0
?
? vn – 1e–v dv
0
?
= ?
u v u x2 = v = y2
du = 2xdx dv = 2ydy
?(m)?(n) x2 (m – 1)
2xe–x2 dx
0
?
? y2 n – 1 ( )
2ye–y2 dy
0
?
? 4 x2m – 2xe–x2 dx
0
?
? y2n – 2ye–y2 dy
0
?
= = ?
4 x2m – 1y2n – 1e–(x2 + y2) dxdy
0
?
? 0
?
= ?
x = ?cos? y = ?sin?
?(m)?(n) 4 (?cos?)2m – 1 (?sin?)2n – 1e –?2?d?d?
0
?
? 0
? ? 2
= ?
2 2m – 1? cos 2n – 1? ? sin d? ?2m + 2n – 2e –?22?d?
0
?
? 0
? ? 2
= ?
?2 = w dw= 2?d?
?(m)?(n) 2 2m – 1? cos 2n – 1? ? sin d? wm + n – 1e –w dw
0
?
? 0
? ? 2
= ?
2 2m – 1? cos 2n – 1? ? sin d? ? ?(m + n)
0
? ? 2
= ?
numerical analysis using matlab and spreadsheets, second edition 13-15
orchard publications
the gamma distribution
rearranging (13.61) we get
(13.62)
and this expression can be simplified by replacing with , that is, , and
with , that is, . then, we get the special case of (13.62) as
(13.63)
if, in (13.62), we replace with and with , we get the integral of the
function as
(13.64)
we observe that (13.63) and (13.64) are equal since and can be interchanged. therefore,
(13.65)
the relations of (13.65) are known as wallis’s formulas.
13.2 the gamma distribution
one of the most common probability distributions* is the gamma distribution which is defined as
(13.66)
* several probability distributions are presented in mathematics for business, science, and technology, isbn 0-9709511-
0-8 and the ebook version isbn 0-9744239-0-4, orchard publications
2m – 1? cos 2n – 1? ? sin d?
0
? ? 2
? ?(m)?(n)
2?(m + n)
= -
2m – 1 n m (n + 1)
2
=
2n 1 – 0 n 12
=
cosn? d?
0
? ? 2
?
? n 1 +
2 ? ?
? ?? 12
? ?
? ?
2? n 1 +
2
12
? + ?
? ?

? n 1 +
2 ? ?
? ?
? n2
? + 1 ?
? ?
- ?
2
= = -
2m – 1 0 2n – 1 m sinn?
sinm? d?
0
? ? 2
?
? 12
? ?
? ? ? m + 1
2
? - ?
? ?
2? 12
m + 1
2
? + - ?
? ?
-
? m + 1
2
? - ?
? ?
? m2
? + 1 ?
? ?
- ?
2
= = -
m n
cosn? d?
0
? ? 2
? sinn? d?
0
? ? 2
?
? n 1 +
2 ? ?
? ?
? n2
? + 1?
? ?
- ?
2
= = - n > –1
f(x, n, ?) xn – 1e –x ? ?
?n?(n)
= - x > 0, n, ? > 0
chapter 13 the gamma and beta functions and distributions
13-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
a detailed discussion of this probability distribution is beyond the scope of this book it will suffice
to say that it is used in reliability and queuing theory. when is a positive integer, it is referred to
as erlang distribution. figure 13.4 shows the probability density function (pdf) of the gamma distribution
for and .
figure 13.4. the pdf for the gamma distribution.
we can evaluate the gamma distribution with the excel gammadist function whose syntax is
gammadist(x,alpha,beta,cumulative)
where:
x = value at which the distribution is to be evaluated
alpha = the parameter in (13.66)
beta = the parameter in (13.66)
cumulative = a true / false logical value if true, gammadist returns the cumulative distribution
function (cdf), and if false, it returns the probability density function* (pdf).
example 13.12
use excel’s gammadist function to evaluate , that is, the pdf of the gamma distribution if:
a. , , and
b. , , and
* several probability density functions are also presented on the text mentioned on the footnote of the previous page.
n
n = 3 ? = 2
x n ? ?(n) ?^n f(x)
0.0 3.0 2.0 2.0 8.0 0.0000
0.2 0.0023
0.4 0.0082
0.6 0.0167
0.8 0.0268
1.0 0.0379
1.2 0.0494
1.4 0.0608
1.6 0.0719
1.8 0.0823
2.0 0.0920
2.2 0.1007
2.4 0.1084
probability density function
of the gamma distribution
for n = 3 and ? = 2
0.00
0.05
0.10
0.15
0.20
0 2 4 6 8 10 12
x
f(x)
n
?
f (x)
x = 4 n = 3 ? = 2
x = 7 n = 3 ? = 2
numerical analysis using matlab and spreadsheets, second edition 13-17
orchard publications
the beta function
solution:
since we are interested in the probability density function (pdf) values, we specify the false
condition. then,
a.
=gammadist(4,3,2,false) returns 0.1353
b.
=gammadist(7,3,2,false) returns 0.0925
we observe that these values are consistent with the plot of figure 13.4.
13.3 the beta function
the beta function, denoted as , is defined as
(13.67)
where and .
example 13.13
prove that
(13.68)
proof:
let then, . we observe that as , and as , . therefore,
and thus (13.68) is proved.
example 13.14
prove that
(13.69)
b(m, n)
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
= ?
m > 0 n > 0
b(m, n) = b(n, m)
x = 1 – y dx = –dy x ? 0 y ? 1 x ? 1 y ? 0
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
? (1 – y)m – 1[1 – (1 – y)]n – 1 dy
1
0
= = –?
(1 – y)m – 1yn – 1 dy
0
1
? yn – 1 (1 – y)m – 1 dy
0
1
= = ? = b(n, m)
b(m, n) 2 2m – 1? cos 2n – 1? ? sin d?
0
? ? 2
= ?
chapter 13 the gamma and beta functions and distributions
13-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
proof:
we let then, . we observe that as , and as ,
. then,
(13.70)
example 13.15
prove that
(13.71)
proof:
the proof is evident from (13.62) and (13.70).
the function is also useful in evaluating certain integrals as illustrated by the following
examples.
example 13.16
evaluate the integral
(13.72)
solution:
by definition
and thus for this example,
using (13.71) we get
(13.73)
x = sin2? dx = 2sin? cos?d? x ? 0 ? ? 0 x ? 1
? ? ? ? 2
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
= ?
2 ( sin2?) ( cos2?) m – 1
n – 1sin? cos? d?
0
? ? 2
= ?
2 2m – 1? ( sin ) 2m – 1? ( cos ) d?
0
? ? 2
= ?
b(m, n) ?(m)?(n)
?(m + n)
= -
b(m, n)
x4(1 – x)3dx
0
1
?
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
= ?
x4(1 – x)3dx
0
1
? = b(5, 4)
b(5, 4) ?(5)?(4)
?(9)
- 4!3!
8!
24 × 6
40320
144
40320
1
280
= = = = =
numerical analysis using matlab and spreadsheets, second edition 13-19
orchard publications
the beta function
we can also use matlab’s beta(m,n) function. for this example,
format rat % display answer in rational format
z=beta(5,4)
z =
1/280
excel does not have a function that computes the function directly. however, we can
use (13.71) for its computation as shown in figure 13.5.
figure 13.5. computation of the beta function with excel.
example 13.17
evaluate the integral
(13.74)
solution:
let then , and . we observe that as , , and as ,
. then, (13.74) becomes
(13.75)
where
(13.76)
then, from (13.74), (13.75) and (13.76) we get
(13.77)
b(m, n)
p g
?(m) ?(n) ?(m+n) beta(m,n)=
exp(gammaln(m)) exp(gammaln(n)) exp(gammaln(m+n)) ?(m) x ?(n) / ?(m+n)
m= 5
24.00 6.00 40320.00 1/280
n= 4
x2
2 – x
-dx
0
2
?
x = 2v x2 = 4v2 dx = 2dv x ? 0 v ? 0 x ? 2
v ? 1
4v2
2 – 2v
2dv
0
1
? 8
2
v2
1 – v
-dv
0
1
? 4 2 v2
(1 – v) –1 ? 2 dv
0
1
= = ?
4 2 b 3 12 , ? ?
? ? ? 4 2 ?(3)?(1 ? 2)
?(7 ? 2)
= =
?(3) = 2!
? ( 1 ? 2 ) = ?
? ( 7 ? 2 ) = (7 ? 2 – 1)? ( 7 ? 2 – 1) = (5 ? 2) ?( 5 ? 2 ) = (5 ? 2)(5 ? 2 – 1)? ( 5 ? 2 – 1)
= 5 ? 2 ? 3 ? 2 ? (3 ? 2 – 1)? ( 3 ? 2 – 1) = (15 ? 8)? ( 1 ? 2 ) = 15 ? ? 8
x2
2 – x
-dx
0
2
? 4 2 ? 2! ? ?
15 ? ? 8
- 64 2
15
= = -
chapter 13 the gamma and beta functions and distributions
13-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
13.4 the beta distribution
the beta distribution is defined as
(13.78)
a plot of the beta probability density function (pdf) for and , is shown in figure
13.6.
figure 13.6. the pdf of the beta distribution
as with the gamma probability distribution, a detailed discussion of the beta probability distribution
is beyond the scope of this book it will suffice to say that it is used in computing variations in
percentages of samples such as the percentage of the time in a day people spent at work, driving
habits, eating times and places, etc.
using (13.71) we can express the beta distribution as
(13.79)
we can evaluate the beta cumulative distribution function (cdf) with excels’s betadist function
whose syntax is
betadist(x,alpha,beta,a,b)
f(x, m, n) xm – 1 (1 – x)n – 1
b(m, n)
= x < 0 < 1, m, n > 0
m = 3 n = 2
x m n ?(m) ?(n) ?(m+n) x(m-1) (1-x)(n-1) f(x,m,n)
0.00 3.0 2.0 2.0 1.0 24.0 0.0000 1.0000 0.0000
0.02 0.0004 0.9800 0.0047
0.04 0.0016 0.9600 0.0184
0.06 0.0036 0.9400 0.0406
0.08 0.0064 0.9200 0.0707
0.10 0.0100 0.9000 0.1080
0.12 0.0144 0.8800 0.1521
0.14 0.0196 0.8600 0.2023
0.16 0.0256 0.8400 0.2580
0.18 0.0324 0.8200 0.3188
0.20 0.0400 0.8000 0.3840
0.22 0.0484 0.7800 0.4530
0.24 0.0576 0.7600 0.5253
0.26 0.0676 0.7400 0.6003
0.28 0.0784 0.7200 0.6774
0.30 0.0900 0.7000 0.7560
0.32 0.1024 0.6800 0.8356
0.34 0.1156 0.6600 0.9156
probability density function
of the beta distribution
for m = 3 and n = 2
0.0
0.4
0.8
1.2
1.6
2.0
0.0 0.2 0.4 0.6 0.8 1.0
x
f(x,m,n)
f(x, m, n) ?(m + n)
?(m)?(n)
- xm – 1 (1 – x)n – 1 = ? x < 0 < 1, m, n > 0
numerical analysis using matlab and spreadsheets, second edition 13-21
orchard publications
summary
where:
x = value between a and b at which the distribution is to be evaluated
alpha = the parameter in (13.79)
beta = the parameter in (13.79)
a = the lower bound to the interval of x
b = the upper bound to the interval of x
from the plot of figure 13.6, we see that when , which represents the probability
density function, is zero. however, the cumulative distribution (the area under the curve) at
this point is or unity since this is the upper limit of the -range. this value can be verified
by
=betadist(1,3,2,0,1) which returns 1.0000
13.5 summary
• the gamma function, denoted as , is also known as generalized factorial function. it is
defined as
• it is convenient to use the relation
for and the relation
for .
• the function is defined for all positive integers and positive fractional values, and for all
negative fractional, but not negative integer values.
• the function and the factorial are related as
• we can use matlab’s gamma(n) function to obtain values of .
• we can use the exp(gammaln(n)) function to evaluate at some positive value of .
• to evaluate when is a positive integer, we can use the relation
m
n
x = 1 f(x, m, n)
100% x
?(n)
?(n) xn – 1e–x dx
0
?
= ?
?(n) ?(n + 1)
n
=
n < 0
n?(n) = ?(n + 1)
n > 0
?(n)
?(n) n!
?(n + 1) = n! for n = 1, 2, 3, …
?(n)
?(n) n
?(n) n
?(n) = (n – 1)!
chapter 13 the gamma and beta functions and distributions
13-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• other useful relations are shown below.
• the relation
is referred to as stirling’s asymptotic series for the function. if is a positive integer, the
factorial can be approximated as
• the function is very useful in integrating some improper integrals.
• the relations
are known as wallis’s formulas.
• the gamma distribution which is defined as
• the beta function, where and is defined as
• the beta function and gamma function are related by
.
? ( 1 ? 2 ) = ?
?(n)?(1 – n) ?
sinn?
=
for 0 < n < 1
22n – 1?(n) ? = ??(2n)
for any n ? negative integer
?(n + 1) n! 2?nnne–n 1 1
12n
- 1
288n2 139
51840n3 571
2488320n4 + + – – + …
? ?
? ?
? ?
= =
?(n) n
n!
n! 2?nnne–n ?
?(n)
cosn? d?
0
? ? 2
? sinn? d?
0
? ? 2
?
? n 1 +
2 ? ?
? ?
? n2 1 + ? ?
? ?
- ?
2
= = - n > –1
f(x, n, ?) xn – 1e –x ? ?
?n?(n)
= - x > 0, n, ? > 0
b(m, n) m > 0 n > 0
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
= ?
b(m, n) ?(n)
b(m, n) ?(m)?(n)
?(m + n)
= -
numerical analysis using matlab and spreadsheets, second edition 13-23
orchard publications
summary
• the beta function is also useful in evaluating certain integrals.
• we can use matlab’s beta(m,n) function to evaluate the beta function.
• the beta distribution is defined as
b(m, n)
b(m, n)
f(x, m, n) xm – 1 (1 – x)n – 1
b(m, n)
= x < 0 < 1, m, n > 0
chapter 13 the gamma and beta functions and distributions
13-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
13.6 exercises
1. given that and , compute
verify your answer with matlab and excel
2. given that and , compute
verify your answer with matlab and excel
3.evaluate the following integrals
a.
b.
c.
d.
e.
m = 2.5 n = –1.25
?(m + n)
?(m)?(n)
-
m = 10 n = 8 b(m, n)
e–x3 dx
0
?
?
xe–x3 dx
0
?
?
dx
1 – x4
-
0
1
?
tan?d?
0
? ? 2
?
dx
3x – x2
-
0
3
?
numerical analysis using matlab and spreadsheets, second edition 13-25
orchard publications
solutions to exercises
13.7 solutions to exercises
1.
by repeated use of the relations for and for ,
we get
(1)
there are no exact values for and therefore, we obtain their approximate
values from tables, where we find that and . then, by
substitution into (1) we get:
check with matlab:
m=2.5 n=?1.25 gamma(m+n)/(gamma(m)*gamma(n))
ans =
0.1739
we cannot check the answer with excel because it cannot compute negative values.
2.
check with matlab:
beta(10,8)
ans =
5.1419e-006
n?(n) = ?(n + 1) n > 1 ?(n) ?(n + 1)
n
= n < 1
?(m + n)
?(m)?(n)
- ?(2.5 + (–1.25))
?(2.5)?(–1.25)
- ?(1.25)
?(2.5)?(–1.25)
?(5 ? 4)
?(5 ? 2) ? ?(–5 ? 4)
= = = -
?(1 ? 4 + 1)
?(3 ? 2 + 1) ?(–5 ? 4 + 1)
(–5 ? 4)
? -
1 ? 4 ? ?(1 ? 4)
3 ? 2 ? ?(3 ? 2) ? (–4 ? 5) ? ?(–1 ? 4)
= =
1 ? 4 ? ?(1 ? 4)
3 ? 2 ? 1 ? 2 ? ?(1 ? 2) ? (–4 ? 5) ?(3 ? 4)
–1 ? 4
?
- 5
48
- ?(1 ? 4)
? ? ?(3 ? 4)
= = ?
?(1 ? 4) ?(3 ? 4)
?(1 ? 4) = 3.6256 ?(3 ? 4) = 1.2254
?(m + n)
?(m)?(n)
- 5
48
- 3.6256
? ? 1.2254
= ? = 0.1739
b(m, n) ?(m)?(n)
?(m + n)
- ?(10) ? ?(8)
?(18)
(9!) × (7!)
17!
= = = -
9 ? 8 ? 7 ? 6 ? 5 ? 4 ? 3 ? 2 ? 7 ? 6 ? 5 ? 4 ? 3 ? 2
17 ? 16 ? 15 ? 14 ? 13 ? 12 ? 11 ? 10 ? 9 ? 8 ? 7 ? 6 ? 5 ? 4 ? 3 ? 2
=
7 ? 6 ? 5 ? 4 ? 3 ? 2
17 ? 16 ? 15 ? 14 ? 13 ? 12 ? 11 ? 10
- 5040
980179200
5.1419 10–6 = = = ×
chapter 13 the gamma and beta functions and distributions
13-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
3.
a.
let , then , , so
b.
c.
we let or . then, and thus
(1)
also,
or
,
and by substitution into (1)
d.
from (13.62),
x3 = y x y1 ? 3 = dx 13
y(–2 ? 3)= ? dy
e–x3 dx
0
?
? e y – 13
y1 ? 3 – 1 ? ? dy
0
?
? 13
? 13
? ?
= = ? ?
xe–x3 dx
0
?
? x2 – 1e –x3 dx
0
?
= ? = ?(2) = 1! = 1
x4 = y x y1 ? 4 = dx (1 ? 4)y(–3 ? 4)= dy
dx
1 – x4
-
0
1
? 14
1
1 – y
- y(–3 ? 4) ? dy
0
1
? 14
(1 – y)(–1 ? 2) y(–3 ? 4) ? dy
0
1
= = ?
14
(1 – y)1 ? 2 – 1 y1 ? 4 – 1 ? dy
0
1
? 14
?(1 ? 4) ? ?(1 ? 2)
?(3 ? 4)
= = ? -
?(3 ? 4) ? ?(1 – 3 ? 4) ?
sin(3? ? 4)
?
2 ? 2
= = -
?(3 ? 4) ? ?(1 ? 4) = 2? ?(3 ? 4) 2?
?(1 ? 4)
=
dx
1 – x4
-
0
1
? 14
?(1 ? 4) ? ?(1 ? 2)
?(3 ? 4)
- ? 14
?(1 ? 4) ? ?
2? ? ?(1 ? 4)
= = ? -
?
4 2?
- ? {?(1 ? 4)}2 {?(1 ? 4)}2
4 2?
= = -
tan?d?
0
? ? 2
? sin?
cos?
? - ?
? ? 1 ? 2
d?
0
? ? 2
? ( sin?)1 ? 2 ( cos?)–1 ? 2 (d?)
0
? ? 2
= = ?
2m – 1? cos 2n – 1? ? sin d?
0
? ? 2
? ?(m)?(n)
2?(m + n)
= -
numerical analysis using matlab and spreadsheets, second edition 13-27
orchard publications
solutions to exercises
letting and we get and .then,
e.
(1)
let , then , . when , and
when , , and the integral of (1) becomes
(2)
recalling that
it follows that , , , and thus
2m – 1 = 1 ? 2 2n – 1 = –1 ? 2 m = 3 ? 4 n = 1 ? 4
tan?d?
0
? ? 2
? ?(3 ? 4)?(1 ? 4)
2?(3 ? 4 + 1 ? 4)
2?
2?(1)
2?
2
= = =
dx
3x – x2
-
0
3
? dx
x(3 – x)
-
0
3
? x–1 ? 2 (3 – x)–1 ? 2 ? dx
0
3
= = ?
x = 3y dx = 3dy x–1 ? 2 (3y)–1 ? 2 ( 3 ? 3)y–1 ? 2 = = x = 0 y = 0
x = 3 y = 1
3
3
3 y–1 ? 2 (3 – 3y)–1 ? 2 ? dy
0
1
? ? 3 y–1 ? 2 1
3
(1 – y)
–1 ? 2
? dy
0
1
= ?
y–1 ? 2 (1 – y)–1 ? 2 dy
0
1
= ?
b(m, n) xm – 1 (1 – x)n – 1 dx
0
1
? ?(m) ? ?(n)
?(m + n)
= = -
m – 1 = –1 ? 2 m = 1 ? 2 n – 1 = –1 ? 2 m = 1 ? 2
dx
3x – x2
-
0
3
? b 12
12
? , ?
? ? ?(1 ? 2) ? ?(1 ? 2)
?(1 ? 2 + 1 ? 2)
- {?(1 ? 2)}2
?(1)
= = = - = ?
chapter 13 the gamma and beta functions and distributions
13-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 14-1
orchard publications
chapter 14
orthogonal functions and matrix factorizations
his chapter is an introduction to orthogonal functions. we begin with orthogonal lines and
functions, orthogonal trajectories, orthogonal vectors, and we conclude with the factorization
methods lu, cholesky, qr, and singular value decomposition.
14.1 orthogonal functions
orthogonal functions are those which are perpendicular to each other. mutually orthogonal systems
of curves and vectors are of particular importance in physical problems. from analytic geometry
and elementary calculus we know that two lines are orthogonal if the product of their slopes is
equal to minus one. this is shown in figure 14.1.
figure 14.1. orthogonal lines
orthogonality applies also to curves. figure 14.2 shows the angle between two curves and .
figure 14.2. orthogonal curves
t
slope = m1
slope = m2
x
y m1 ? m2 = –1
c1 c2
chapter 14 orthogonal functions and matrix factorizations
14-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
by definition, in figure 14.2, the angle between the curves and is the angle between
their tangent lines and . if and are the slopes of these two lines, then, and are
orthogonal if .
example 14.1
prove that every curve of the family
(14.1)
is orthogonal to every curve of the family
(14.2)
proof:
at a point on any curve of (14.1), the slope is
or
(14.3)
on any curve of (14,2) the slope is
or
(14.4)
from (14.3) and (14.4) we see that these two curves are orthogonal since their slopes are negative
reciprocals of each other. the cases where or cannot occur because we defined
and .
other orthogonal functions are the and functions as we’ve learned in chapter 6.
14.2 orthogonal trajectories
two families of curves with the property that each member of either family cuts every member of
the other family at right angles are said to be orthogonal trajectories of each other. thus, the curves
of (14.2) are orthogonal trajectories of the curves of (14.1). the two families of these curves are
shown in figure 14.3.
c1 c2 ?
l1 l2 m1 m2 l1 l2
m2 = –1 ? m1
xy = a a ? 0
x2 – y2 = b b ? 0
p(x, y)
xdy + ydx = 0
dy
dx
- yx
= –
2xdx – 2ydy = 0
dy
dx
- xy
=
x = 0 y = 0
a ? 0 b ? 0
cosx sinx
numerical analysis using matlab and spreadsheets, second edition 14-3
orchard publications
orthogonal trajectories
figure 14.3. the families of the curves in example 14.1
example 14.2
find the orthogonal trajectories of the family of parabolas
(14.5)
solution:
the slope of (14.5) is
(14.6)
from (14.5), and thus we rewrite (14.6) as
(14.7)
therefore, the slope of the orthogonal family we are seeking must be
(14.8)
or
(14.9)
y cx2 = c ? 0
dy
dx
- = 2cx
c y x2 = ?
dy
dx
- 2 y
x2 x 2y
x
= = -
dy
dx
- x
2y
= – -
2ydy + xdx = 0
2?ydy + ?xdx = 0
2 y2
2
x2
2
+ = k (cons tant)
x2 + 2y2 = c (cons tant)
chapter 14 orthogonal functions and matrix factorizations
14-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
relation (14.9) represents a family of ellipses and the trajectories are shown in figure14.4.
figure 14.4. orthogonal trajectories for families of parabolas and ellipses.
14.3 orthogonal vectors
let and be two vectors of the same length. their
inner (dot) product is defined as
(14.10)
example 14.3
given that
and
find the dot product
solution:
definition:two vectors and are said to be orthogonal if their dot product is zero.
example 14.4
test the vectors
and
for orthogonality.
solution:
therefore the vectors and are orthogonal to each other.
x = [x1 x2 x3 … xn] y = [y1 y2 y3 … yn]
x ? y = x1 y1 + x2 y2 + x3 y3 + … + xn yn (a scalar )
x = [1 1 1] y = [2 1 2]
x ? y
x ? y = (1) ? (2) + (1) ? (1) + (1) ? (2) = 5
x1 x2
x1 = [1 1 1] x2 = [1 -2 1]
x1 ? x2 = (1) ? (1) + (1) ? (–2) + (1) ? (1) = 0
x1 x2
numerical analysis using matlab and spreadsheets, second edition 14-5
orchard publications
orthogonal vectors
with any vector we may associate a unique unit vector which is obtained by dividing
each component of by each magnitude defined as
where represents an element of the vector . this process is called normalization.
example 14.5
given that
compute the unit vector .
solution:
first, we compute the magnitude . for this example,
to compute the unit vector we divide each element of by the magnitude . thus,
a basis that consists of mutually orthogonal vectors is referred to as an orthogonal basis. if these
vectors are also unit vectors, the basis is called orthonormal basis.
if the column (or row) vectors of a square matrix are mutually orthogonal unit vectors, the
matrix is orthogonal and
(14.11)
where is the transpose of and is the identity matrix.
example 14.6
given that
find an orthonormal set of eigenvectors* and verify that the result satisfies (14.11).
* it is strongly suggested that the reader reviews the definitions of eigenvalues and eigenvectors in chapter 5 at this time.
x ? 0 u
x x
x x1 2 x2 2 … xn 2 = + + +
xi x
x = [2 4 4]
ux
x
x = 22 + 42 + 42 = 6
ux x x
ux
26
46
46
12
23
23
= =
a
a
a ? at = i
at a i
a 1 ? 2 1 ? 4
1 ? 2 1 ? 2
=
chapter 14 orthogonal functions and matrix factorizations
14-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
first, we find the eigenvalues of the matrix from the relation where for this
example
from which and and as we’ve learned in chapter 5, with these eigenvalues
we can obtain an infinite number of eigenvectors. to find a square matrix such that
we start with
(14.12)
or
(14.13)
equating like terms we get
from the second equation we get and by substitution into the first we get or
this result indicates that we can choose either or for the values of and .we
choose the value and then the first (left most) matrix in (14.12) is
a det(a – ?i) = 0
det 1 ? 2 1 ? 4
1 ? 2 1 ? 2
? 1 0
0 1

? ?
? ?
? ?
= 0
det 1 ? 2 – ? 1 ? 4
1 ? 2 1 ? 2 – ?
= 0
?2 – ? + 3 ? 16 = 0
?1 = 1 ? 4 ?2 = 3 ? 4
2 × 2 z
z ? zt = i
z1 z2
–z1 z2
z1 –z1
z2 z2
? 1 0
0 1
=
z1 2 z2 2 + z – 1
2 z2 2 +
z – 1
2 z2 2 + z1 2 z2 2 +
1 0
0 1
=
z1 2 z2 2 + 1 = z – 1
2 z2 2 + = 0
z1 2 z2 2 = 2z1 2 = 1
z1 = z2 = 1 ? (± 2)
1 ? 2 1 ? (– 2) z1 z2
1 ? 2
z 1 ? 2 1 ? 2
1 ? (– 2) 1 ? 2
=
numerical analysis using matlab and spreadsheets, second edition 14-7
orchard publications
the gram-schmidt orthogonalization procedure
and as a check,
the computations for finding orthonormal sets of eigenvectors for larger size ( or higher)
matrices using the above procedure becomes quite involved. a simpler procedure is the gram-
schmidt orthogonalization procedure which we will discuss on the next section.
14.4 the gram-schmidt orthogonalization procedure
let be some column vectors. we can find an orthogonal basis using
the following relations. we must remember that the products in (14.14) below are the inner
(dot) products and if and are two vectors of the
same length their dot product is defined as .
thus in the second equation in (14.14) the dot products on the numerator and denominator must
be found first and the result must be from the dot product of it and
(14.14)
also, the unit vectors
(14.15)
are mutually orthogonal and form an orthonormal basis.
in our subsequent discussion the column vectors will be denoted as row vectors transposed.
example 14.7
given that , , and , find an orthonormal basis.
1 ? 2 1 ? 2
1 ? (– 2) 1 ? 2
1 ? 2 1 ? (– 2)
1 ? 2 1 ? 2
? 1 0
0 1
=
3 × 3
x1, x2,…xm y1, y2, …ym
x = [x1 x2 x3 … xn] y = [y1 y2 y3 … yn]
x ? y = x1 y1 + x2 y2 + x3 y3 + …+ xn yn (a scalar )
y1
y1 = x1
y2 x2
y1 ? x2
y1 ? y1
= – ? y1
y3 x3
y2 ? x3
y2 ? y2
– ? y2
y1 ? x3
y1 ? y1
= – ? y1

ym xm
ym – 1 ? xm
ym – 1 ym – 1 ?
ym – 1 – ? –…
y1 ? xm
y1 ? y1
= – ? y1
ui
yi
yi
= - i = 1, 2,…, m
x1 = [1 1 1]t x2 = [1 -2 1]t x3 = [1 2 3]t
chapter 14 orthogonal functions and matrix factorizations
14-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
from (14.14)
and from (14.15)
and denoting the matrix whose elements are the unit vectors as , we have:
we can verify that with the matlab code below.
a=[1/sqrt(3) 1/sqrt(6) ?1/sqrt(2) 1/sqrt(3) ?2/sqrt(6) 0 1/sqrt(3) 1/sqrt(6) 1/sqrt(2)]
i=a*a
i =
1.0000 0 0.0000
0 1.0000 0
0.0000 0 1.0000
we can also use the matlab function orth(a) to produce an orthonormal basis as shown
below.
b=[1 1 1 1 ?2 1 1 2 3] c=orth(b)
y1 = x1 = [1 1 1]t
y2 x2
y1 ? x2
y1 ? y1
y1 ? – 1 -2 1 [ ]t 03
= = – ? y1 = [1 -2 1]t
y3 x3
y2 ? x3
y2 ? y2
– ? y2
y1 ? x3
y1 ? y1
y1 ? – 1 2 3 [ ]t 06
– ? y2 63
= = – [1 1 1]t
= [1 2 3]t – [2 2 2]t = [–1 0 1]t
u1
y1
y1
[1 ? 3 1 ? 3 1 ? 3]t
= =
u2
y2
y2
[1 ? 6 –2 ? 6 1 ? 6]t
= =
u3
y3
y3
[–1 ? 2 0 1 ? 2]t
= =
a
a
1 ? 3 1 ? 6 –1 ? 2
1 ? 3 –2 ? 6 0
1 ? 3 1 ? 6 1 ? 2
=
a ? at = i
numerical analysis using matlab and spreadsheets, second edition 14-9
orchard publications
the lu factorization
c =
-0.4027 0.0000 0.9153
0.0000 1.0000 0.0000
-0.9153 0.0000 -0.4027
we observe that the vectors of the matrix produced by matlab are different from those we
derived with the gram-schmidt orthogonalization procedure. the reason for this difference is
that the orthogonalization process is not unique, that is, we may find different values depending
on the process being used. as shown below, the vectors produced by matlab also satisfy the
condition .
i=c*c
i =
1.0000 -0.0000 0.0000
-0.0000 1.0000 -0.0000
0.0000 -0.0000 1.0000
14.5 the lu factorization
in matrix computations, computers use the so-called matrix factorization methods to decompose
a matrix into a product of other smaller matrices. the lu factorization method decomposes a
matrix into a lower triangular matrix and an upper triangular matrix so that .
in chapter 4 we saw how the method of gaussian elimination proceeds by systematically removing
the unknowns from a system of linear equations.
consider the following lower triangular case.
the unknowns are found from
(14.16)
provided that .
for the upper triangular case, the unknowns are written in reverse order. thus, to solve
c
c ? ct = i
a
a l u a = l ? u
3 × 3
l11 0 0
l21 l22 0
l31 l32 l33
x1
x2
x3
?
b1
b2
b3
=
x1 = b1 ? l11
x2 = (b2 – l21 x1) ? l22
x3 = (b3 – l31 x1 – l31 x2) ? l33
l11 ? l22 ? l33 ? 0
chapter 14 orthogonal functions and matrix factorizations
14-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(14.17)
we start from the bottom to the top as shown below.
(14.18)
provided that .
example 14.8
let us review the example given in chapter 4 which consists of the following equations.
(14.19)
to find the three unknowns, we start by multiplying the first equation by and subtracting it
from the second equation. this removes from the second equation. likewise, we multiply the
first equation by and we subtract it from the third equation. with these two reductions we
obtain
(14.20)
next we multiply the second equation of (14.20) by and we subtract it from the third equation
of (14.20) and we get the system of equations below.
(14.21)
we see that the eliminations have transformed the given square system into an equivalent upper
triangular system that gives the same solution which is obtained as follows:
u11 u12 u13
0 u22 u23
0 0 u33
x1
x2
x3
?
b1
b2
b3
=
x3 = b3 ? u33
x2 = (b2 – u23 x3) ? u22
x1 = (b1 – u12 x2 – u13 x3) ? u11
u11 ? u22 ? u33 ? 0
2v1 – v2 + 3v3 = 5
–4v1 – 3v2 – 2v3 = 8
3v1 + v2 – v3 = 4
–2
v1
3 ? 2
2v1 – v2 + 3v3 = 5
–5v2 + 4v3 = 18
2.5v2–5.5v3 = –3.5
–1 ? 2
2v1 – v2 + 3v3 = 5
–5v2 + 4v3 = 18
–3.5v3 = 5.5
numerical analysis using matlab and spreadsheets, second edition 14-11
orchard publications
the lu factorization
the elements of the upper triangular matrix are the coefficients of the unknowns in (14.21).
thus,
now, let us use the relations of (14.16) and (14.18) to find the lower and upper triangular matrices
of our example where
we want to find and such that
(14.22)
where the first matrix on the left side is the lower triangular matrix and the second is the
upper triangular matrix . the elements of matrix are the coefficients of , , and in
(14.20). to find the elements of matrix we use matlab to multiply matrix by the inverse
of matrix . thus,
u=[2 ?1 3 0 ?5 4 0 0 ?3.5] a=[2 ?1 3 ?4 ?3 ?2 3 1 ?1] l=a*inv(u)
l =
1 0 0
-2 1 0
3/2 -1/2 1
therefore, the matrix has been decomposed to a lower triangular matrix and an upper
matrix as shown below.
v3 = –11 ? 7
v2 = (18 – 4v3) ? (–5) = –34 ? 7
v1 = (5 + v2 – 3v3) ? 2 = 17 ? 7
u
u11 u12 u13
0 u22 u23
0 0 u33
2 –1 3
0 –5 4
0 0 –3.5
=
a
2 –1 3
–4 –3 –2
3 1 –1
=
lij uij
l11 0 0
l21 l22 0
l31 l32 l33
u11 u12 u13
0 u22 u23
0 0 u33
? a
2 –1 3
–4 –3 –2
3 1 –1
= =
l
u u v1 v2 v3
l a
u
a l
u
chapter 14 orthogonal functions and matrix factorizations
14-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
check with matlab:
l=[1 0 0 ?2 1 0 3/2 ?1/2 1] u=[2 ?1 3 0 ?5 4 0 0 ?3.5] a=l*u
a =
2 -1 3
-4 -3 -2
3 1 -1
we found the elements of the lower triangular matrix by first computing the inverse of the
upper triangular matrix and performing the matrix multiplication but not
. was this necessary? the answer is no. for a square matrix where none of the diagonal
elements are zero, the lower triangular matrix has the form
and in our example we found that the values of the subdiagonal elements are ,
, and . these values are the multipliers that we’ve used in the elimination
process in succession.
example 14.9
use the matlab function [l,u]=lu(a) to decompose the matrix
into a lower and an upper triangular.
solution:
format rat a=[2 ?3 1 ?1 5 ?2 3 ?8 4] [l,u]=lu(a)
l =
2/3 1 1
-1/3 1 0
1 0 0
2 –1 3
–4 –3 –2
3 1 –1
1 0 0
–2 1 0
3 ? 2 –1 ? 2 1
2 –1 3
0 –5 4
0 0 –3.5
= ?
l
u l a u–1 = ?
l u–1 = ? a
l
1 0 0
l21 1 0
l31 l32 1
=
l21 = –2
l31 = 3 ? 5 l32 = –1 ? 2
a
2 –3 1
–1 5 –2
3 –8 4
=
numerical analysis using matlab and spreadsheets, second edition 14-13
orchard publications
the lu factorization
u =
3 -8 4
0 7/3 -2/3
0 0 -1
we observe that while the upper triangular matrix has the proper structure, the lower triangular
matrix lacks structure. when a matrix lacks structure we say that it is permuted. to put
in the proper structure, let us interchange the first and third rows. then,
(14.23)
the new matrix has now the proper structure. let us now use matlab to see if .
l1=[1 0 0 ?1/3 1 0 2/3 1 1] u=[3 ?8 4 0 7/3 ?2/3 0 0 ?1] a1=l1*u
a1 =
3 -8 4
-1 5 -2
2 -3 1
we see that matrix is now permuted. to put it in the given form we need to make the same
interchanges in rows as with the lower triangular matrix, that is, we must interchange the first
and third rows.
to find out how matlab performs lu factorization, let us invoke the help lu command. only
part of the display is shown below.
help lu
lu lu factorization.
[l,u] = lu(x) stores an upper triangular matrix in u and a
"psychologically lower triangular matrix" (i.e. a product
of lower triangular and permutation matrices) in l, so
that x = l*u. x can be rectangular.
[l,u,p] = lu(x) returns unit lower triangular matrix l, upper
triangular matrix u, and permutation matrix p so that
p*x = l*u.
the permutation matrix is an identity matrix that is permuted so that the rows of this matrix
indicate the interchanges. consider, for example, the identity matrix
u
l
l
l
1 0 0
–1 ? 3 1 0
2 ? 3 1 1
=
l l ? u = a
a
p
chapter 14 orthogonal functions and matrix factorizations
14-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
if we interchange the first and third rows of the identity matrix above, we get the permutation
matrix below.
(14.24)
and matrix indicates the same interchanges as with the lower triangular matrix in example
14.9.
example 14.10
use the matlab function [l,u,p]=lu(a) to decompose the matrix
into a lower and an upper triangular and show that .
solution:
this is the same matrix as in example 14.9. thus,
a=[2 ?3 1 ?1 5 ?2 3 ?8 4] [l,u,p]=lu(a)
l =
1 0 0
-1/3 1 0
2/3 1 1
u =
3 -8 4
0 7/3 -2/3
0 0 -1
p =
0 0 1
0 1 0
1 0 0
i
1 0 0
0 1 0
0 0 1
=
i
p
p
0 0 1
0 1 0
1 0 0
=
p
a
2 –3 1
–1 5 –2
3 –8 4
=
p ? a = l ? u
numerical analysis using matlab and spreadsheets, second edition 14-15
orchard publications
the cholesky factorization
we observe that the lower triangular matrix has now the proper structure and the matrix displayed
by matlab is the same as in (14.24). also,
pa=p*a, lu=l*u
pa =
3 -8 4
-1 5 -2
2 -3 1
lu =
3 -8 4
-1 5 -2
2 -3 1
we observe that with the first and second rows interchanged when compare with
the given matrix .
the matlab matrix left division operator uses the factorization approach.
14.6 the cholesky factorization
a matrix is said to be positive definite if
(14.25)
for every and is symmetric, that is, . under those conditions, there exists an
upper triangular matrix with positive diagonal elements such that
(14.26)
relation (14.26) is referred to as cholesky factorization. it is a special case of lu factorization
and requires fewer computations than the lu factorization method of the previous section. let
us invoke the matlab help chol command to see how matlab performs this factorization.
chol cholesky factorization.
chol(x) uses only the diagonal and upper triangle of x.
the lower triangular is assumed to be the (complex conjugate)
transpose of the upper. if x is positive definite, then
r = chol(x) produces an upper triangular r so that r *r = x.
if x is not positive definite, an error message is printed.
[r,p] = chol(x), with two output arguments, never produces an
error message. if x is positive definite, then p is 0 and r
is the same as above. but if x is not positive definite,
p
p ? a = l ? u
a
x = a\b l ? u
xt ? a ? x > 0
x ? 0 a at = a
g
gt ? g = a
chapter 14 orthogonal functions and matrix factorizations
14-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
then p is a positive integer.
we will consider an example using the cholesky factorization after we review the matlab
functions eye(n) and diag(v,k) as defined by matlab.
help eye
eye identity matrix.
eye(n) is the n-by-n identity matrix.
eye(m,n) or eye([m,n]) is an m-by-n matrix with 1 s on
the diagonal and zeros elsewhere.
eye(size(a)) is the same size as a.
see also ones, zeros, rand, randn.
help diag
diag diagonal matrices and diagonals of a matrix.
diag(v,k) when v is a vector with n components is a square
matrix of order n+abs(k) with the elements of v on the k-th
diagonal. k = 0 is the main diagonal, k > 0 is above the main
diagonal and k < 0 is below the main diagonal.
diag(v) is the same as diag(v,0) and puts v on the main diagonal.
diag(x,k) when x is a matrix is a column vector formed from
the elements of the k-th diagonal of x.
diag(x) is the main diagonal of x. diag(diag(x)) is a diagonal
matrix.
example
m = 5
diag(-m:m) + diag(ones(2*m,1),1) + diag(ones(2*m,1),-1)
produces a tridiagonal matrix of order 2*m+1.
see also spdiags, triu, tril.
example 14.11
use matlab to compute the cholesky factorization of matrix as defined below.
format bank b=[?0.25 ?0.50 ?0.75 ?1.00]
a=5*eye(5)+diag(b, ?1)+diag(b, 1), g=chol(a), a1=g *g
solution:
execution of the matlab code above displays the following:
a
numerical analysis using matlab and spreadsheets, second edition 14-17
orchard publications
the qr factorization
a =
5.00 -0.25 0 0 0
-0.25 5.00 -0.50 0 0
0 -0.50 5.00 -0.75 0
0 0 -0.75 5.00 -1.00
0 0 0 -1.00 5.00
g =
2.24 -0.11 0 0 0
0 2.23 -0.22 0 0
0 0 2.22 -0.34 0
0 0 0 2.21 -0.45
0 0 0 0 2.19
a1 =
5.00 -0.25 0 0 0
-0.25 5.00 -0.50 0 0
0 -0.50 5.00 -0.75 0
0 0 -0.75 5.00 -1.00
0 0 0 -1.00 5.00
we observe that , that is, the matrix product is satisfied.
14.7 the qr factorization
the qr factorization decomposes a matrix into the product of an orthonormal matrix and an
upper triangular matrix. the matlab function [q,r]=qr(a) produces an matrix whose
columns form an orthonormal or unitary* matrix and an upper triangular matrix of the same
size as matrix . in other words, can be factored as
(14.27)
then, a system described by becomes
(14.28)
and multiplying both sides of (14.28) by we get
(14.29)
the matlab [q,r]=qr(a) is described as follows:
* an matrix is called unitary if where is the complex conjugate matrix of .
a1 = a gt ? g = a
a
n × n
q r
n × n a (a?)t a–1 = a? a
a a
a = qr
ax = b
qrx = b
q ? qt = i
rx = qtb
chapter 14 orthogonal functions and matrix factorizations
14-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
help qr
qr orthogonal-triangular decomposition.
[q,r] = qr(a) produces an upper triangular matrix r of the same
dimension as a and a unitary matrix q so that a = q*r.
[q,r,e] = qr(a) produces a permutation matrix e, an upper
triangular r and a unitary q so that a*e = q*r. the column
permutation e is chosen so that abs(diag(r)) is decreasing.
[q,r] = qr(a,0) produces the "economy size" decomposition.
if a is m-by-n with m > n, then only the first n columns of q
are computed.
q,r,e] = qr(a,0) produces an "economy size" decomposition in
which e is a permutation vector, so that q*r = a(:,e). the column
permutation e is chosen so that abs(diag(r)) is decreasing.
by itself, qr(a) is the output of lapack s dgeqrf or zgeqrf routine.
triu(qr(a)) is r.
r = qr(a) returns only r. note that r = chol(a *a).
[q,r] = qr(a) returns both q and r, but q is often nearly full.
c,r] = qr(a,b), where b has as many rows as a, returns c = q *b.
r = qr(a,0) and [c,r] = qr(a,b,0) produce economy size results.
the full version of qr does not return c.
the least squares approximate solution to a*x = b can be found
with the q-less qr decomposition and one step of iterative
refinement:
x = r\(r \(a *b))
r = b - a*x
e = r\(r \(a *r))
x = x + e
example 14.12
given that
and
solve using the matlab function [q,r]=qr(a) and .
a
2 –3 1
–1 5 –2
3 –8 4
= b
2
4
5
=
ax = b x = r\qtb
numerical analysis using matlab and spreadsheets, second edition 14-19
orchard publications
the qr factorization
solution:
a=[2 ?3 1 ?1 5 ?2 3 ?8 4] b=[2 4 5] [q,r]=qr(a), x=r\q *b
q =
-0.53 -0.62 -0.58
0.27 -0.77 0.58
-0.80 0.15 0.58
r =
-3.74 9.35 -4.28
0 -3.24 1.54
0 0 0.58
x =
4.14
4.43
7.00
check=a\b
check =
4.14
4.43
7.00
let us verify that the matrix is unitary. of course, since the elements are real numbers, the
complex conjugate of is also and thus we only need to show that or .
q*q
ans =
1.00 0.00 -0.00
0.00 1.00 -0.00
-0.00 -0.00 1.00
qr factorization is normally used to solve overdetermined systems,* that is, systems with more
equations than unknowns as in applications where we need to find the least square distance in
linear regression. in an overdetermined system, there is no vector which can satisfy the entire
system of equations, so we select the vector which produces the minimum error. matlab
does this with either the left division operator ( \ ) or with the non-negative least-squares function
lsqnonneg(a,b). this function returns the vector that minimizes norm(a*x?b) subject
to provided that the elements of and are real numbers.
* we defined overdetermined and underdetermined systems in chapter 8
q
q q qt q–1 = q ? qt = i
x
x
x
x ? 0 a b
chapter 14 orthogonal functions and matrix factorizations
14-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
a=[2 ?3 1 ?1 5 ?2 3 ?8 4] b=[2 4 5] x=lsqnonneg(a,b)
x =
4.1429
4.4286
7.0000
underdetermined systems have infinite solutions and matlab selects one but no warning message
is displayed.
as we’ve learned in chapter 4, the matlab function inv(a) produces the inverse of the square
matrix and an error message is displayed if . the function pinv(a) displays the pseudo-inverse
of a (non-square) matrix . of course, if is square, then pinv(a)=inv(a).
14.8 singular value decomposition
the singular value decomposition (svd) method decomposes a matrix into a diagonal matrix
, of the same dimension as and with nonnegative diagonal elements in decreasing order, and
unitary matrices and so that
(14.30)
the matrices , , and , decomposed from a given matrix , can be found with the matlab
function [u, s, v]=svd(a).
example 14.13
decompose the matrix
into two unitary matrices and a diagonal matrix with non-negative elements.
solution:
we will use the matlab [u, s, v]=svd(a) function.
a=[2 ?3 1 ?1 5 ?2 3 ?8 4] [u,s,v]=svd(a)
u =
-0.3150 -0.8050 -0.5028
0.4731 -0.5924 0.6521
-0.8228 -0.0325 0.5675
a a
m × n a a
a
s a
u v
a u s vt = ? ?
u s v a
a
2 –3 1
–1 5 –2
3 –8 4
=
numerical analysis using matlab and spreadsheets, second edition 14-21
orchard publications
summary
s =
11.4605 0 0
0 1.1782 0
0 0 0.5184
v =
-0.3116 -0.9463 0.0863
0.8632 -0.2440 0.4420
-0.3972 0.2122 0.8929
as expected, the diagonal elements of the triangular matrix are non-negative and in decreasing
values. we also verify that the matrices and are unitary as shown below.
u*u
ans =
1.0000 -0.0000 -0.0000
-0.0000 1.0000 0.0000
-0.0000 0.0000 1.0000
v*v
ans =
1.0000 -0.0000 0.0000
-0.0000 1.0000 -0.0000
0.0000 -0.0000 1.0000
14.9 summary
• orthogonal functions are those which are perpendicular to each other.
• two families of curves with the property that each member of either family cuts every member
of the other family at right angles are said to be orthogonal trajectories of each other.
• the inner (dot) product of two vectors and is
a scalar defined as
• if the dot product of two vectors and is zero, these vector are said to be orthogonal to
each other.
• the magnitude of a vector , denoted as , is defined as . a unique
unit vector is obtained by dividing each component of by the magnitude and this
process is referred to as normalization.
s
u v
x = [x1 x2 x3 … xn] y = [y1 y2 y3 … yn]
x ? y = x1 y1 + x2 y2 + x3 y3 + … + xn yn
x1 x2
x x x x1 2 x2 2 … xn 2 = + + +
u x x
chapter 14 orthogonal functions and matrix factorizations
14-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• a basis that consists of mutually orthogonal vectors is referred to as an orthogonal basis. if
these vectors are also unit vectors, the basis is called orthonormal basis.
• if the column (or row) vectors of a square matrix are mutually orthogonal unit vectors, the
matrix is said to be orthogonal and where is the transpose of and is the
identity matrix.
• we can find an orthonormal set of eigenvectors in a matrix easily from the eigenvalues
but the computations for finding orthonormal sets of eigenvectors for larger size ( or
higher) matrices using the above procedure becomes quite involved. a simpler procedure is
the gram-schmidt orthogonalization procedure which we will discuss on the next section.
• the lu factorization method decomposes a matrix into a lower triangular matrix and an
upper triangular matrix so that . the matlab function [l,u]=lu(a) decomposes
the matrix into a lower triangular matrix and an upper triangular matrix .
• a matrix is said to be positive definite if for every and is symmetric, that
is, . under those conditions, there exists an upper triangular matrix with positive
diagonal elements such that . this process is referred to as the cholesky factorization.
• the qr factorization decomposes a matrix into the product of an orthonormal matrix and
an upper triangular matrix. the matlab function [q,r]=qr(a) produces an matrix
whose columns form an orthonormal or unitary matrix and an upper triangular matrix of
the same size as matrix .
• the singular value decomposition (svd) method decomposes a matrix into a diagonal
matrix , of the same dimension as and with nonnegative diagonal elements in decreasing
order, and unitary matrices and so that . the matrices , , and ,
decomposed from a given matrix , can be found with the matlab function [u, s,
v]=svd(a).
a
a a ? at = i at a i
2 × 2
3 × 3
a l
u a = l ? u
a l u
xt ? a ? x > 0 x ? 0 a
at = a g
gt ? g = a
a
n × n
q r
a
a
s a
u v a usvt = ? ? u s v
a
numerical analysis using matlab and spreadsheets, second edition 14-23
orchard publications
exercises
14.10 exercises
1. show that the curve and the curve where and are constants,
are orthogonal to each other.
2. find the orthogonal trajectories of the curves of the family
3. given the vectors and , use the gram-schmidt orthogonalization
procedure to find two vectors and to form an orthonormal basis.
4. use matlab to find another set of an orthonormal basis with the vectors given in exercise
3.
5. use the gaussian elimination method as in example 14.8 to decompose the system of equations
into an upper triangular matrix and a lower triangular matrix . verify your answers with
matlab.
6. using the matlab functions eye(n) and diag(v,k) to define and display the matrix
shown below.
then, use the matlab cholesky factorization function to obtain the matrix and verify
that .
7. use the appropriate matlab function to decompose the system of equations
and
into an upper triangular matrix of the same dimension as and a unitary matrix so that
. use a suitable function to verify your results.
x2 + 3y2 = k1 3y = k2x3 k1 k2
2x2 + y2 = kx
x1 = [2 – 1]t x2 = [1 – 3]t
y1 y2
x1 + 2x2 + 3x3 = 14
–2x1 + 3x2 + 2x3 = 10
5x1–8x2 + 6x3 = 7
u l
a
a
4.00 –0.80 0 0
–0.80 4.00 –1.00 0
0 –1.00 4.00 –1.20
0 0 –1.20 4.00
=
g
gt ? g = a
a
1 0 –1
0 1 2
–2 –3 4
= b
3
5
9
=
r a q
q ? r = a
chapter 14 orthogonal functions and matrix factorizations
14-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
8. use the appropriate matlab function to decompose the matrix given as
into a diagonal matrix of the same dimension as and with non-negative diagonal elements
in decreasing order and unitary matrices and so that .
a
a
1 0 –1
0 1 2
–2 –3 4
=
s a
u v us vt ? ? = a
numerical analysis using matlab and spreadsheets, second edition 14-25
orchard publications
solutions to exercises
14.11 solutions to exercises
1.
(1)
(2)
implicit differentiation of (1) yields
or
(3)
differentiation of (2) yields
(4)
from (2),
and by substitution into (4) we get
(5)
we see that (5) is the negative reciprocal of (3) and thus the given curves are orthogonal to
each other.
2.
(1)
implicit differentiation of (1) yields
and solving for ,
(2)
from (1),
(3)
x2 + 3y2 = k1
3y = k2x3
2x 6ydy
dx
+ - = 0
dy
dx
- 13
xy
= – ?
dy
dx
-
3k2x2
3
= - = k2x2
k2
3y
x3 = -
dy
dx
- 3y
x3 - x2 ? 3yx
= =
2x2 + y2 = kx
d
dx
-(2x2) d
dx
+ - y2 d
dx
= -(kx)
4x 2ydy
dx
+ - = k
dy ? dx
dy
dx
- k – 4x
2y
=
k 2x2 + y2
x
= -
chapter 14 orthogonal functions and matrix factorizations
14-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and by substitution into (2)
(4)
now, we need to find the curves whose slopes are given by the negative reciprocal of (4), that
is, we need to find the family of the curves of
(5)
we rewrite (5) as
(6)
and we let . then, and by substitution into (6)
division of both sides of the above by yields
collecting like terms and simplifying we get
separating the variables we get
or
and by integrating these terms we find
by substitution of we get
dy
dx
-
2x2 + y2
x
- – 4x
2y
- – 2x2 y+ 2
2xy
= =
dy
dx
- 2xy
2x2 – y2
= -
(2x2 – y2)dy = 2xydx
y = ux dy = udx + xdu
(2x2 – u2x2)(udx + xdu) = 2x2udx
x2
(2 – u2)(udx + xdu) = 2udx
x 2 u2 ( – )du = u3dx
dx
x
- (2 – u2)
u3 du 2
u3 -du du
u
= = –
dx
x
- du
u
+ 2
u3 = -du
ln x + ln u –1
u2 = - + c
u = y ? x
x ln yx
+ ln + ln c x yx
? ? ? c?
ln ? ? ln cy –x2
y2 = = =
numerical analysis using matlab and spreadsheets, second edition 14-27
orchard publications
solutions to exercises
and thus the family of curves orthogonal to the given family is
3.
from (14.14)
and from (14.15)
and denoting the matrix whose elements are the unit vectors as , we have:
we verify that as shown below.
4.
a=[2 ?1 1 ?3] b=orth(a)
b =
-0.5257 -0.8507
-0.8507 0.5257
i=b*b
i =
1.0000 -0.0000
-0.0000 1.0000
x2 = –y2 ln cy
y1 = x1 = [2 –1]t
y2 x2
y1 ? x2
y1 ? y1
– ? y1 [1 –3]t [2 –1]t ? [1 –3]t
[2 –1]t ? [2 –1]t
= = – ? [2 –1]t
[1 –3]t [2 + 3]
[4 + 1]
2 1 – [ ]t ? – 1 3 – [ ]t 55
= = – ? [2 –1]t =
= [1 –3]t – [2 –1]t = [–1 2]t
u1
y1
y1
[2 ? 5 – 1 ? 5]t
= =
u2
y2
y2
[–1 ? 5 –2 ? 5]t
= =
a
a 2 ? 5 –1 ? 5
–1 ? 5 –2 ? 5
=
a ? at = i
2 ? 5 –1 ? 5
–1 ? 5 –2 ? 5
2 ? 5 –1 ? 5
–1 ? 5 –2 ? 5
? 4 ? 5 + 1 ? 5 –2 ? 5 + 2 ? 5
–2 ? 5 + 2 ? 5 1 ? 5 + 4 ? 5
1 0
0 1
= =
chapter 14 orthogonal functions and matrix factorizations
14-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
5.
(1)
multiplying the first equation of (1) by and subtracting it from the second in (1) we get the
second equation in (2) and thus is eliminated. likewise, we multiply the first equation by
and we subtract it from the third in (1). then,
(2)
next, we multiply the second equation in (2) by and we subtract it from the third in
(2). then, after simplification
(3)
thus,
the multipliers that we’ve used are , , and . these are the elements , , and
respectively. therefore, the lower triangular matrix is
the elements of the upper triangular matrix are the coefficients of the unknowns in (3) and
thus
now, we use matlab to verify that
l=[1 0 0 ?2 1 0 5 ?18/7 1] u=[1 2 3 0 7 8 0 0 81/7] a=l*u
x1 + 2x2 + 3x3 = 14
–2x1 + 3x2 + 2x3 = 10
5x1–8x2 + 6x3 = 7
–2
x1 5
x1 + 2x2 + 3x3 = 14
7x2 + 8x3 = 38
–18x2–9x3 = –63
–18 ? 7
x1 + 2x2 + 3x3 = 14
7x2 + 8x3 = 38
(81 ? 7)x3 = 243 ? 7
x3 = 243 ? 81 = 3
x2 = (38 – 8x3) ? 7 = 2
x1 = (14 – 3x3 – 2x2) = 1
–2 5 –18 ? 7 l21 l31
l32
l
1 0 0
–2 1 0
5 –18 ? 7 1
=
u
1 2 3
0 7 8
0 0 81 ? 7
=
l ? u = a
numerical analysis using matlab and spreadsheets, second edition 14-29
orchard publications
solutions to exercises
a =
1 2 3
-2 3 2
5 -8 6
6.
format bank b=[?0.8 ?1.0 ?1.2]
a=4*eye(4)+diag(b, ?1)+diag(b, 1), g=chol(a), a1=g *g
a =
4.00 -0.80 0 0
-0.80 4.00 -1.00 0
0 -1.00 4.00 -1.20
0 0 -1.20 4.00
g =
2.00 -0.40 0 0
0 1.96 -0.51 0
0 0 1.93 -0.62
0 0 0 1.90
a1 =
4.00 -0.80 0 0
-0.80 4.00 -1.00 0
0 -1.00 4.00 -1.20
0 0 -1.20 4.00
7.
a=[1 0 ?1 0 1 2 ?2 -3 4] b=[3 5 9]
[q,r]=qr(a), qqt=q*q , x=r\q *b, check=a\b
q =
-0.4472 0.7171 0.5345
0 -0.5976 0.8018
0.8944 0.3586 0.2673
r =
-2.2361 -2.6833 4.0249
0 -1.6733 -0.4781
0 0 2.1381
qqt =
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
x =
chapter 14 orthogonal functions and matrix factorizations
14-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
6.7500
-2.5000
3.7500
check =
6.7500
-2.5000
3.7500
8.
a=[1 0 ?1 0 1 2 ?2 ?3 4]
[u,s,v]=svd(a), uut=u*u , vvt=v*v
u =
-0.2093 -0.2076 -0.9556
0.1977 0.9480 -0.2493
0.9577 -0.2410 -0.1574
s =
5.5985 0 0
0 2.0413 0
0 0 0.7000
v =
-0.3795 0.1345 -0.9154
-0.4779 0.8187 0.3184
0.7922 0.5583 -0.2464
uut =
1.0000 0.0000 0.0000
0.0000 1.0000 0.0000
0.0000 0.0000 1.0000
vvt =
1.0000 0.0000 -0.0000
0.0000 1.0000 -0.0000
-0.0000 -0.0000 1.0000
numerical analysis using matlab and spreadsheets, second edition 15-1
orchard publications
chapter 15
bessel, legendre, and chebyshev functions
his chapter is an introduction to some very interesting functions. these are special functions
that find wide applications in science and engineering. they are solutions of differential
equations with variable coefficients and, under certain conditions, satisfy the orthogonality
principle.
15.1 the bessel function
the bessel functions, denoted as , are used in engineering, acoustics, aeronautics, thermodynamics,
theory of elasticity and others. for instance, in the electrical engineering field, they are
used in frequency modulation, transmission lines, and telephone equations.
bessel functions are solutions of the differential equation
(15.1)
where can be any number, positive or negative integer, fractional, or even a complex number.
then, the form of the general solution of (15.1) depends on the value of .
differential equations with variable coefficients, such as (15.1), cannot be solved in terms of
familiar functions as those which we encountered in ordinary differential equations with constant
coefficients. the usual procedure is to derive solutions in the form of infinite series, and the most
common are the method of frobenius and the method of picard. it is beyond the scope of this book
to derive the infinite series which are approximations to the solutions of these differential equations
these are discussed in advanced mathematics textbooks. therefore, we will accept the solutions
without proof.
applying the method of frobenius to (15.1), we obtain the infinite power series
(15.2)
this series is referred to as bessel function of order where is any positive real number or zero. if
in (15.2), we replace with , we get the relation
(15.3)
t
jn(x)
x2
x2
2
d
d y x x d
dy x2 n2 + + ( – )y = 0
n
n
jn(x) (–1)k
k = 0
??
x2
? ?
? ? n + 2k 1
k! ? ?(n + k + 1)
= ? ? - n ? 0
n n
n –n
j–n(x) (–1)k
k = 0
??
x2
? ?
? ? – n + 2k 1
k! ? ?(– n + k + 1)
= ? ? -
chapter 15 bessel, legendre, and chebyshev functions
15-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and the function is referred to as the bessel function of negative order .
for the special case where is a positive integer or zero,
(15.4)
and (15.2) reduces to
(15.5)
or
(15.6)
for and , (15.6) reduces to the following series:
(15.7)
(15.8)
(15.9)
we observe from (15.7) through (15.9), that when is zero or even, is an even function of
, and odd when is odd.
if we differentiate the series of in (15.7), and compare with the series of in (15.8), we
see that
(15.10)
also, if we multiply the series for by and differentiate it, we will find that
(15.11)
j–n(x) n
n
?(n + k + 1) = (n + k)!
jn(x) (–1)k
k = 0
??
x2
? ?
? ? n + 2k 1
k! ? (n + k)!
= ? ? - n = 0, 1, 2, …
jn(x) xn
2n ? n!
1 x2
22 ? 1! ? (n + 1)
– x4
24 ? 2! ? (n + 1)(n + 2)

x6
26 ? 3! ? (n + 1)(n + 2)(n + 3)
– - …
+
+
?
?
?
?
?
?
=
n = 0, 1 2
j0(x) 1 x2
22 ? (1!)2
– - x4
24 ? (2!)2
- x6
26 ? (3!)2
- x8
28 ? (4!)2
= + – + - – …
j1 x ( ) x2
x3
23 ? 1! ? 2!
– x5
25 ? 2! ? 3!
x7
27 ? 3! ? 4!
x9
29 ? 4! ? 5!
= + – + –…
j2(x) x2
22 ? 2!
x4
24 ? 1! ? 3!
– x6
26 ? 2! ? 4!
x8
28 ? 3! ? 5!
x10
210 ? 4! ? 6!
= + – + – …
n jn(x)
x n
j0(x) j1(x)
dx
d j0(x) j= – 1(x)
j1(x) x
dx
d {xj1(x)} xj= 0(x)
numerical analysis using matlab and spreadsheets, second edition 15-3
orchard publications
the bessel function
example 15.1
compute, correct to four decimal places, the values of
a. b. c.
solution:
a.
from (15.7),
or from math tables,
b.
from (15.8),
or from math tables,
c.
from (15.9),
or from math tables, .
we can use the matlab besselj(n,x) function or the excel besselj(x,n) function for the
above computations. with matlab, we get
besselj(0,2), besselj(1,3), besselj(2,1)
ans =
0.2239
ans =
0.3391
ans =
0.1149
and with excel,
the matlab code below plots , , and .
j0(2) j1(3) j2(1)
j0 2 ( ) 1 44
– 16
64
- 64
64 × 36
– 256
256 × 576
+ + 14
1
36
– - 1
576
+ 43
192
= = = = 0.2240
j0(2) = 0.2239
j1 3 ( ) 32
27
16
– - 243
384
2187
18432
– 19683
1474560
= + + - = 0.3400
j1(3) = 0.3391
j2 1 ( ) 18
1
96
– - 1
1536
- 1
184320
– - 1
17694720
= + + - = 0.1152
j2(1) = 0.1149
besselj(2,0)= 0.2239 besselj(3,1)= 0.3391 besselj(1,2)= 0.1149
j0(x) j1(x) j2(x)
chapter 15 bessel, legendre, and chebyshev functions
15-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
x = 0.00: 0.05: 10.00 v = besselj(0,x) w = besselj(1,x) z = besselj(2,x)
plot(x,v,x,w,x,z) grid title( bessel functions of the first kind ) xlabel( x ) ylabel( jn(x) )
text(0.95, 0.85, j0(x) ) text(2.20, 0.60, j1(x) ) text(4.25, 0.35, j2(x) )
the plots for , and are shown in figure 15.1.*
figure 15.1. plots of , and using matlab
we can also use excel to plot these series as shown in figure 15.2.
the definition of a bessel function of the first kind will be explained shortly.
the -axis crossings in the plot of figures 15.1 and 15.2 show the first few roots of the ,
, and series. however, all are infinite series and thus, it is a very difficult and
tedious task to compute all roots of these series. fortunately, tables of some of the roots of
and are shown in math tables.
the equations and exhibit some interesting characteristics. the most noteworthy
are:
1. they have no complex roots
2. each has an infinite number of distinct real roots
* in frequency modulation (fm), is denoted as and it is called modulation index. the functions , ,
and so on, represent the carrier, first sideband, second sideband etc. respectively.
j0(x) j1(x) j2(x)
x ? j0(?) j1(?)
j2(?)
j0(x) j1(x) j2(x)
x j0(x)
j1(x) j2(x) jn(x)
j0(x)
j1(x)
j0(x) = 0 j1(x) = 0
numerical analysis using matlab and spreadsheets, second edition 15-5
orchard publications
the bessel function
figure 15.2. plots of , and using excel
3. between two consecutive roots of one of these equations lies one and only one root of the
other equation, that is, the roots of these equations separate each other. this is observed on
table 15.1 which shows the first positive roots of these equations, and the differences
between consecutive roots. for instance, we observe that the first root of lies
between the roots and of .
table 15.1 the first few roots of and
roots differences roots differences
2.4048 3.8317
3.1153 3.1839
5.5201 7.0156
3.1336 3.1579
8.6537 10.1735
3.1378 3.1502
11.7915 13.3237
3.1394 3.1469
14.9309 16.4706
plot of bessel function jn(x) for n = 0, 1 and 2
x j0(x) j1(x) j2(x)
0.00 1.0000 0.0000 0.0000
0.05 0.9994 0.0250 0.0003
0.10 0.9975 0.0499 0.0012
0.15 0.9944 0.0748 0.0028
0.20 0.9900 0.0995 0.0050
0.25 0.9844 0.1240 0.0078
0.30 0.9776 0.1483 0.0112
0.35 0.9696 0.1723 0.0152
0.40 0.9604 0.1960 0.0197
0.45 0.9500 0.2194 0.0249
0.50 0.9385 0.2423 0.0306
0.55 0.9258 0.2647 0.0369
0.60 0.9120 0.2867 0.0437
0.65 0.8971 0.3081 0.0510
0.70 0.8812 0.3290 0.0588
0.75 0.8642 0.3492 0.0671
bessel functions of the first kind
-0.5
0.0
0.5
1.0
0 2 4 6 8 10
x
jn(x)
j0(x)
j1(x)
j2(x)
j0(x) j1(x) j2(x)
5
3.8317 j1(x)
2.4048 5.5201 j0(x)
j0(x) j1(x)
j0(x) = 0 j1(x) = 0
… …
… …
chapter 15 bessel, legendre, and chebyshev functions
15-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
4. as the roots become larger and larger, the difference between consecutive roots approaches
the value of ?, that is, and , are almost periodic with period almost . in other
words, these series behave like the and functions.
if is half of an odd integer, such as , , , and so on, then can be expressed in a
finite form of sines and cosines. consider, for example, the so-called half-order bessel functions
and . if we let in (15.2), we get
(15.12)
likewise, if we let in (15.3), we get
(15.13)
example 15.2
compute, correct to four decimal places, the values of
a. b.
solution:
a. using (15.12),
b. using (15.13).
check with matlab:
besselj(0.5,pi/4), besselj(?0.5,pi/4)
ans =
0.6366
ans =
0.6366
the bessel functions which we have discussed thus far, are referred to as bessel functions of the first
kind. other bessel functions, denoted as and referred to as bessel functions of the second kind,
j0(x) j1(x) 2?
cosx sinx
n 1 ? 2 3 ? 2 5 ? 2 jn(x)
j1 ? 2(x) j(–1 ? 2)(x) n = 1 ? 2
j1 ? 2(x) 2
?x
= - sinx
n = 1 ? 2
j(–1 ? 2)(x) 2
?x
= - cosx
j1 ? 2
?4
? ?
? ? j 1 2 (– ? )
?4
? ?
? ?
j1 ? 2
?4
? ?
? ? 2
?(? ? 4)
? 4 ? ( ) sin 1?
8 2
2
? 4
2?
2?
= = = = = 0.6366
j(–1 ? 2)
?4
? ?
? ? 2
?(? ? 4)
? 4 ? ( ) cos 1?
8 2
2
? 2?
= = = = 0.6366
yn(x)
numerical analysis using matlab and spreadsheets, second edition 15-7
orchard publications
the bessel function
or weber functions, or neumann functions. these are additional solutions of the bessel’s equation,
and will be explained in the next paragraph. also, certain differential equations resemble the
bessel equation, and thus their solutions are called modified bessel functions, or hankel functions.
as mentioned earlier, a bessel function for , can be obtained by replacing with
in (15.2). if is an integer, we will prove that
(15.14)
proof:
from (15.3),
(15.15)
now, we recall from chapter 13, that the numbers yield infinite values in
then, the first summation in the above relation is zero for . also, if we let
in the second summation, after simplification and comparison with (15.5), we see that
and thus (15.14) has been proved.
it is shown in advanced mathematics textbooks that, if is not an integer, and are
linearly independent for this case, the general solution of the bessel equation is
(15.16)
for and so on, the functions and are not linearly independent as we
have seen in (15.14) therefore, (15.16) is not the general solution, that is, for this case, these two
series produce only one solution, and for this reason, the bessel functions of the second kind are
introduced to obtain the general solution.
j–n(x) n > 0 n –n
n
j–n(x) = (–1)njn(x)
for n = 1, 2, 3, …
j–n(x) (–1)k ( x ? 2 )– n + 2k ?
k! ? ?(– n + k + 1)

k = 0
??
=
(–1)k ( x ? 2 )– n + 2k ?
k! ? ?(– n + k + 1)

k = 0
n 1 –
? = (–1)k ( x ? 2 )– n + 2k ?
k! ? ?(– n + k + 1)

k = n
??
+
n = 0, –1, –2, … ?(n)
k = 0, 1, 2,…, n – 1
k = n + m
(–1)n + m ( x ? 2 )– n + 2n + 2m ?
(n + m)! ? ?(m + 1)
-
m = 0
??
1 – ( )n x2
? ?
? ? n + 2m (–1)m
?(n + m + 1)! ? m!

m = 0
??
= = (–1)njn(x)
n jn(x) j–n(x)
y ajn = (x) + bj–n(x)
n ? 0, 1, 2, 3, …
n = 1, 2, 3, … jn(x) j–n(x)
chapter 15 bessel, legendre, and chebyshev functions
15-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
the following example illustrates the fact that when is not an integer or zero, relation (15.16) is
the general solution.
example 15.3
find the general solution of bessel’s equation of order .
solution:
by the substitution in (15.1), we get
(15.17)
we will show that the general solution of (15.17) is
(15.18)
by substitution of (15.12) and (15.13) into (15.18), we get
(15.19)
and letting and , (15.19) can be written as
(15.20)
since the two terms on the right side of (15.20) are linearly independent, represents the general
solution of (15.17).
the bessel functions of the second kind, third kind, and others, can be evaluated at specified values
either with matlab or excel. the descriptions, syntax, and examples for each can be found
by invoking help bessel for matlab, and help for excel.
one very important property of the bessel’s functions is that within certain limits, they constitute
an orthogonal system.* for instance, if and are distinct roots of , and
, then,
(15.21)
* two functions constitute an orthogonal system, when the average of their cross product is zero within some specified limits.
n
1 ? 2
n = 1 ? 2
x2
x2
2
d
d y x x d
dy x2 14
? – ?
+ + ? ? y = 0
y = aj1 ? 2(x) + bj–1 ? 2(x)
y a 2
?x
- sinx b 2
?x
= + cosx
c1 = a 2 ? ? c2 = b 2 ? ?
y c1
sinx
x
c2
cosx
x
= + -
y
a b j0 (x) = 0 j0(a) = 0
j0(b) = 0
xj0(ax)j0(bx)dx
0
1
? = 0
numerical analysis using matlab and spreadsheets, second edition 15-9
orchard publications
the bessel function
and we say that and are orthogonal in the interval . they are also orthogonal
with the variable .
the function
(15.22)
is referred to as the generating function for bessel functions of the first kind of integer order. using this
function, we can obtain several interesting properties for integer values of . some of these are
given below without proof. more detailed discussion and proofs can be found in advanced mathematics
textbooks.
(15.23)
(15.24)
where the subscript denotes that the first relation is valid for even values of , whereas
in the second, indicates that the second relation is valid for odd values of . also,
(15.25)
and
(15.26)
relations (15.23) through (15.26) appear in frequency modulation. for example, the average
power is shown to be
and with (15.23), it reduces to
j0(ax) j0(bx) 0 ? x ? 1
x
e
x2
t 1t
? – ?
? ?
jn(x)tn
n = –?
??
=
n
jn
2(x)
n = –?
??
= 1
cos(x sin?) j0(x) 2 j2k(x) cos2k?
k = 1
??
= +
sin(x sin?) 2 j2k – 1(x) sin(2k – 1)?
k = 1
??
=
2k k 2k – 1
k
cosn? ? cos(x sin?)d?
0
?
? ?jn(x) n = even
0 n odd = ?
?
?
=
sinn? ? sin(x sin?)d?
0
?
? 0 n = even
?jn x ( ) n odd = ?
?
?
=
jn x ( ) 1?
cos(n? – x sin?)d?
0
?
= ?
pave
12
ac 2 jn
2(?)
n = –?
??
=
pave
12
ac 2 =
chapter 15 bessel, legendre, and chebyshev functions
15-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
15.2 legendre functions
another second-order differential equation with variable coefficients is the equation
(15.27)
known as legendre’s equation. here, is a constant, and if it is zero or a positive integer, then
(15.27) has polynomial solutions of special interest.
applying the method of frobenius, as in the bessel equation, we obtain two independent solutions
and as follows.
(15.28)
(15.29)
where and are constants. we observe that is an even function of , while is an odd
function. then, the general solution of (15.27) is or
(15.30)
and this series is absolutely convergent* for .
the parameter is usually a positive integer. if is zero, or an even positive integer, the first
term on the right side of (15.30) contains only a finite number of terms if it is odd, the second
term contains only a finite number of terms. therefore, whenever is zero or a positive integer
the general solution of legendre’s equation contains a polynomial solution which is denoted as
, and an infinite series solution which is denoted as .
the legendre polynomials are defined as
* assume that the infinite series converges, i.e., reaches a limit. if, when we replace the
terms of this series by their absolute value, we find that the resulting series also converges,
this series is said to be absolutely convergent.
(1 – x2)d 2 y
dx2 - 2xdy
dx
– - + n(n + 1)y = 0
n
y1 y2
y1 a0 1 n(n + 1)
2!
– x2 (n – 2)n(n + 1)(n + 3)
4!
= + -x4 – …
y2 a1 x (n – 1)(n + 2)
3!
– x3 (n – 3)(n – 1)(n + 2)(n + 4)
5!
= + -x5 –…
a0 a1 y1 x y2
y = y1 + y2
y a0 1 n(n + 1)
2!
– x2 (n – 2)n(n + 1)(n + 3)
4!
+ -x4 –…
a1 x (n – 1)(n + 2)
3!
– x3 (n – 3)(n – 1)(n + 2)(n + 4)
5!
+ + -x5 –…
=
x > 1
un(x0)
n = 1
??
= u1(x0) + u2(x0) +…
un(x0)
n = 1
??
= u1(x0) + u2(x0) +…
n n
n
pn(x) qn(x)
numerical analysis using matlab and spreadsheets, second edition 15-11
orchard publications
legendre functions
(15.31)
(15.32)
and these are also referred to as surface zonal harmonics. the infinite series solution is
referred to as legendre functions of the second kind. these become infinite as and their
applications to science and engineering problems are very limited. accordingly, they will not be
discussed in this text.
the even and odd functions of (15.31) and (15.32) can be combined to a single relation as
(15.33)
from (15.33), or (15.31) and (15.32), we obtain the following first 6 legendre polynomials.
(15.34)
the relation
(15.35)
is known as rodrigues’ formula, and offers another method of expressing the legendre polynomials.
we prove (15.35) as follows.
from the binomial theorem,
(15.36)
pn(x) (–1)n ? 2 1 ? 3 ? 5 ?… ? (n – 1)
2 ? 4 ? 6 ?… ? n
? - 1 n(n + 1)
2!
= – x2 + …
for n = 0 or n = even integer
pn(x) (–1)(n – 1) ? 2 1 ? 3 ? 5 ? …? n
2 ? 4 ? 6 ? …? (n – 1)
? - x (n – 1)(n + 2)
3!
= – x3 + …
for n = odd integer
qn(x)
x ? ±1
pn(x) (–1)k ? (2n – 2k)!
2nk!(n – k)!(n – 2k)!
-xn – 2k
k = 0
n?
=
where n n2
for n even and n (n – 1)
2
= = = for n = odd
p0(x) = 1 p1(x) = x
p2 x ( ) 12
3x2 1 – ( ) = p3 x ( ) 12= (5x3 – 3x)
p4 x ( ) 18
35x4 30x2 – 3 + ( ) = p5 x ( ) 18
= (63x5 – 70x3 + 15x)
pn(x) 1
2n ? n!
- d n
dxn
(x2 – 1)
n
= ?
(x2 – 1)n (–1)k ? n!
k! ? (n – k)!
-x2n – 2k
k = 0
n?
=
chapter 15 bessel, legendre, and chebyshev functions
15-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and differentiation of (15.36) with respect to times yields
(15.37)
now, by comparison with (15.33), we recognize (15.37) as and thus (15.35) is
proved.
another important identity involving legendre polynomials, is the generating function for legendre
polynomials which is defined as
(15.38)
we will illustrate the use of the legendre polynomials with the following example.
example 15.4
find the potential difference (voltage) at a point developed by a nearby dipole* in terms of
the distance between the point and the dipole, and the angle which point makes with the
center of the dipole.
solution:
let the charges and of the dipole be a distance 2d apart with the origin 0 as the midpoint as
shown in figure 15.3.
figure 15.3. figure for example 15.4
let the potential at point be . from electromagnetic field textbooks we find that
* a dipole is a pair of electric charges or magnetic poles, of equal magnitude but of opposite sign or polarity, separated by a
small distance. alternately, a dipole is an antenna, usually fed from the center, consisting of two equal rods extending outward
in a straight line.
x n
d n
dx -n-(x2 – 1)n (–1)k ? n!
k!(n – k)!
- (2n – 2k)!
(n – 2k)!
? xn – 2k
k = 0
n?
=
2n ? n! ? pn(x)
1
1 – 2xt + t2
- p0(x) + p1(x)t + p2(x)t2 + …+ pn(x)tn pn(x)tn
n = 0
??
= =
v p
p p
q –q
?q d 0 d q
r1 r2
r
?
p
p vp
numerical analysis using matlab and spreadsheets, second edition 15-13
orchard publications
legendre functions
(15.39)
where is the permittivity* of the vacuum. for simplicity, we will denote the quantity
as k and thus we rewrite (15.39) as
(15.40)
next, we need to express and in terms or d and r. by the law of cosines,
(15.41)
and
(15.42)
dividing both sides of (15.42) by , we get
(15.43)
or
(15.44)
in all practical applications, the point is sufficiently far from the origin thus, we assume that
. now, we want to relate the terms inside the parentheses of (15.44), to a legendre polynomial.
we do this by expressing these terms in the form of the generating function of (15.38).
we let , and then, by substitution into (15.44) we get
(15.45)
we recall that (15.45) holds only if and . this requirement is satisfied since x and y,
as defined, are both less than unity.
to find a similar expression for , we simply replace with in (15.45), and thus
* permittivity is a measure of the ability of a material to resist the formation of an electric field within it.
vp
q
4??0
- 1
r2
1
r1
? – ?
= ? ?
?0 1 ? (4??0)
vp kq 1
r2
1
r1
? – ?
? ? kq r2
–1 r1
–1 = = ( – )
r1 r2
r1 = d 2 + r2–2dr cos(180° – ?) = d 2 + r2 + 2dr cos?
r2 = d 2 + r2–2dr cos?
r
r2
r
d 2
r2 1 2dcos?
r
= + –
r2
1 – 1r
d 2
r2 1 2dcos?
r
+ –
? ?
? ?
? ? –1 ? 2
=
p
r > d
x = cos? y = d ? r
d 2
r2 - 1 2dcos?
r
+ –
? ?
? ?
? ? –1 ? 2
( 1 – 2xy + y2) –1 ? 2 pn(x)yn
n = 0
??
= =
x < 1 y < 1
r1
–1 x –x
chapter 15 bessel, legendre, and chebyshev functions
15-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
(15.46)
by substitution of (15.45) and (15.46) into (15.40), we get
(15.47)
since , and , we can express (15.47) as
(15.48)
however, if is even in (15.48), , and therefore, all even powers vanish.
but when is odd, and the odd powers in (15.48) are duplicated. then,
(15.49)
and for , (15.49) can be approximated as
* (15.50)
the term is the magnitude of the so-called dipole moment. it is a vector directed from the negative
charge towards the positive charge. it is denoted with the letter , that is,
(15.51)
the relation of (15.50) can, of course, be derived without the use of legendre polynomials as follows:
for , the distances , r, and can be approximated by parallel lines as shown in figure
15.4. then, the negative and positive charges look like a single point charge, and using (15.40) we
get
(15.52)
we observe that (15.52) is the same as (15.50).
* here, we have used the identity . this will be seen shortly in (15.57) when we discuss the trigonometric
form of the legendre polynomials.
1 2xy y2
( + + ) –1 ? 2 pn(–x)yn
n = 0
??
=
vp
kq
r
- [pn(x) yn – pn(–x)yn]
n = 0
??
=
x = cos? y = d ? r
vp
kq
r
- [pn( cos?) – pn(–cos?)]
n = 0
??
= dr
? ?
? ?n
n pn(–cos?) = pn( cos?)
n pn(–cos?) = –pn( cos?)
vp
kq
r
- 2p2n + 1( cos?)
n = 0
??
= dr
? ?
? ? 2n + 1
r » d
vp
2kq
r
-p1 ? cos ( ) dr
? ?
? ? ? 2kdq
r2 = cos?
p1( cos?) = cos?
2dq
p
p = 2qd
r » d r1 r2
vp kq 1
r2
1
r1
? – ?
? ? kq
r1 ? r2
-(r2 – r1) 2kqd
r2 = = = cos?
numerical analysis using matlab and spreadsheets, second edition 15-15
orchard publications
legendre functions
figure 15.4. derivation of the voltage developed by a dipole
another interesting relation that can be used to find the legendre polynomial series of a function
for , is
(15.53)
then, a function can be expanded as
(15.54)
the example below illustrates how this relation is being used.
example 15.5
compute the legendre polynomial series representing the waveform of figure 15.5.
figure 15.5. waveform for example 15.5
?q d 0 d q
r
?
p
?
r1
r2
r1 × r2 = r2
r2 – r1 = 2dcos?
f(x) x < 1
bn
2n + 1
2
- f (x)pn(x)dx
–1
1
= ?
f (x)
f (x) b0p0 (x) + b1p1(x) + b2p2(x) + … + bnpn(x) bnpn(x)
n = 0
??
= =
?1
0 1
x
1
?1
f (x)
chapter 15 bessel, legendre, and chebyshev functions
15-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
we will first compute the coefficients from (15.53) then, we will substitute these into (15.54).
we will also use (15.34) for the polynomials of .
for this example,
and so on. therefore, using (15.54) and (15.34) we get
(15.55)
we observe that the waveform of is an odd function and, as we found above, its expansion
contains only odd legendre polynomials.
in many applications, the algebraic form of the legendre polynomials is usually the most useful.
however, there are times when we want to express the polynomials in terms trigonometric functions,
as we did in example 15.4. also, the trigonometric forms are most convenient with the
cylindrical and spherical coordinate systems. it is shown in advanced mathematics textbooks that
bn
pn(x)
b0
12
(–1) ? 1 ? dx
–1
0
? 12
1 ? 1 ? dx
0
1
+ ? 12
x –1
0 12
x 0
1 + – 12
– 12
= = = + = 0
b1
32
(–1) ? x ? dx
–1
0
? 32
1 ? x ? dx
0
1
+ ? 34
x2
–1
0 34
x2
0
1
+ – 34
34
+ 32
= = = =
b2
52
1 – ( ) 12
? (3x2 – 1)dx
–1
0
? 52
1 12
? (3x2 – 1)dx
0
1
= + ? = 0
b3
72
1 – ( ) 12
? (5x3 – 3x)dx
–1
0
? 72
1 12
? (5x3 – 3x)dx
0
1
? + 78
= = –
b4
92
1 – ( ) 18
? (35x4 – 30x2 + 3)dx
–1
0
? 92
1 18
? (35x4 – 30x2 + 3)
0
1
= + ? = 0
b5
–11
2
18
(63x5 – 70x3 + 15x)dx
–1
0
? 11
2
- 18
(63x5 – 70x3 + 15x)
0
1
+ ? 11
16
= = -
f x ( ) 32
p1 x ( ) 78
– p3(x) 11
16
= + -p5(x)
32
x 78
12
(5x3 – 3x) 11
16
- 18
= – ? + ? (63x5 – 70x3 + 15x)
32
x 7
16
-(5x3 – 3x) 11
128
= – + ? (63x5 – 70x3 + 15x)
525
128
x 525
64
– x3 693
128
= + x5
f(x)
numerical analysis using matlab and spreadsheets, second edition 15-17
orchard publications
legendre functions
(15.56)
from (15.56) we obtain the first 6 legendre polynomials in trigonometric form listed below.
(15.57)
the legendre polynomials in algebraic form, satisfy the orthogonality principle when as
indicated by the following integral.
(15.58)
similarly, the legendre polynomials in trigonometric form satisfy the orthogonality principle
when as indicated by the following integral.
(15.59)
we must remember that all the legendre polynomials we have discussed thus far are referred to
as surface zonal harmonics, and math tables include values of these as computed from rodrigues’
formula of (15.35).
there is another class of legendre functions which are solutions of the differential equation
(15.60)
pn cos? 1 ? 3 ?… ? (2n – 1)
2 ? 4 ? … ? 2n
-2 n? cos 12
1 ? 3 ? …? (2n – 3)
2 ? 4 ? …? (2n – 2)
= + ? -2cos(n – 2)?
+ 1 ? 3
2 ? 4
1 ? 3 ? … ? (2n – 5)
2 ? 4 ? … ? (2n – 4)
? -2cos(n – 4)? + …
p0 cos? = 1
p1 cos? = cos?
p2 cos? 3cos2? + 1
4
= -
p3 cos? 5cos3? + 3cos?
8
= -
p4 cos? 35cos4? + 20cos2? + 9
64
=
p5 cos? 63cos5? + 35cos3? + 30cos?
128
= -
m ? n
pm(x)pn(x)dx
–1
1
?
0 m ? n
2
2n + 1
- m = n
? ? ? ? ?
=
m ? n
pm( cos?)pn( cos?) sin?d?
0
?
?
0 m ? n
2
2n + 1
- m = n
? ? ? ? ?
=
(1 – x2)d 2 y
dx2 - 2xdy
dx
– - n(n + 1) m2
1 – x2
+ – - y = 0
chapter 15 bessel, legendre, and chebyshev functions
15-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
and this is referred to as the associated legendre differential equation. we observe that if ,
(15.60) reduces to (15.27).
the general solution of (15.60) is
(15.61)
where and are arbitrary constants. the functions and are referred to as associated
legendre functions of the first and second kind respectively. these are evaluated from
(15.62)
and
(15.63)
relations (15.62) and (15.63) are also known as spherical harmonics.
we will restrict our subsequent discussion to the associated legendre functions of the first kind,
that is, the polynomials .
at present, excel does not have any functions related to legendre polynomials. matlab provides
the legendre(n,x) function that computes the associated legendre functions of the first
kind of degree , and order evaluated for each element of .
example 15.6
find the following associated legendre functions and evaluate as indicated.
a. b. c.
solution:
for this example, we use the relation (15.62), that is,
and the appropriate relations of (15.34). for this example,
a.
m = 0
y c1pn
m(x) c2qn
= + m(x)
c1 c2 pn
m(x) qn
m(x)
pn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -pn(x)
qn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -qn(x)
pn
m(x)
n m = 0, 1, 2,…, n x
p2
1(x)
x = 0.5
p3
2(x)
x = –0.5
p2
3(x)
x = 0.25
pn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -pn(x)
p2
1(x)
x = 0.5
(–1)1(1 – x2)1 ? 2 d
dx
-p2(x)
x = 0.5
–(1 – x2)
1 ? 2 d
dx
- 3x2 – 1
2
? -?
? ?
x = 0.5
= =
–(1 – x2)
1 ? 2 = (3x) = –1.2990
numerical analysis using matlab and spreadsheets, second edition 15-19
orchard publications
legendre functions
for in (15.62), we get
as stated above, the matlab legendre(n,x) function computes the associated legendre
functions of the first kind of degree and order evaluated for each element
of . here, and thus matlab will return a matrix whose rows correspond to the values
of , , and , for the first, second, and third rows respectively.
check with matlab:
disp( the values for m = 0, m = 1 and m = 2 are: ) legendre(2,0.5)
the values for m = 0, m = 1 and m = 2 are:
ans =
-0.1250
-1.2990
2.2500
or more elegantly,
m=0:2 y=zeros(3,2) y(:,1)=m y(:,2)=legendre(2,0.5)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 -0.1250
1 -1.2990
2 2.2500
b.
here, , and thus matlab will display a matrix whose rows correspond to the values of
, , , and , for the first, second, third and fourth rows respectively.
check with matlab:
m=0:3 y=zeros(4,2) y(:,1)=m y(:,2)=legendre(3,?0.5)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m = 0
p2 1 (x)
x = 0.5
3x2 – 1
2
? -?
? ?
x = 0.5
= = –0.125
n m = 0, 1, 2, …, n
x n = 2
m = 0 m = 1 m = 2
p3
2(x)
x = –0.5
(–1)2(1 – x2)2 ? 2 d 2
dx2 p3(x)
x = –0.5
(1 – x2) d 2
dx2 5x3 – 3x
2
? ?
? ?
x = –0.5
= =
(1 – x2) d
dx
- 15x2 – 3
2
? ?
? ?
x = –0.5
(1 – x2)(15x)x = –0.5 = = = –5.6250
n = 3
m = 0 m = 1 m = 2 m = 3
chapter 15 bessel, legendre, and chebyshev functions
15-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
m legendre
0 0.4375
1 -0.3248
2 -5.6250
3 -9.7428
c.
and since the third derivative of is zero, it follows that .
in general, if , then .
15.3 laguerre polynomials
another class of polynomials that satisfy the orthogonality principle, are the laguerre polynomials
these are solutions of the differential equation
(15.64)
these polynomials are computed with the rodrigues’ formula
(15.65)
the orthogonality principle for these polynomials states that
(15.66)
example 15.7
compute the laguerre polynomials
a. b. c. d.
solution:
using rodrigues’s formula of (15.65), we get
p2
3(x)
x = 0.25
(–1)3(1 – x2)3 ? 2 d 3
dx3 p2(x)
x = 0.25
–(1 – x2)
3 ? 2 d 3
dx3 (3x2 – 1) x = 0.25 = =
3x2 – 1 p2
3(x) = 0
m > n pn
m(x) = 0
ln(x)
x
x2
2
d
d y 1 x ( – )
dx
dy ny + + = 0
ln(x) ex
xn
n
d
d xne–x = ( )
e–xlm(x)ln(x)dx
0
?
? = 0
l0(x) l1(x) l2(x) l3(x)
numerical analysis using matlab and spreadsheets, second edition 15-21
orchard publications
chebyshev polynomials
(15.67)
the differentiation of the last two polynomials in (15.67) was performed with matlab as follows:
syms x y z
y=x^2*exp(?x) z=diff(y,2) % differentiate y twice with respect to x
l2x=exp(x)*z simplify(l2x)
ans =
2-4*x+x^2
syms x y z w y=x^3*exp(?x) z=diff(y,2) % differentiate y twice
% we cannot differentiate three times at once
w=diff(z) % differentiate one more time
l3x=exp(x)*w simplify(l3x)
ans =
6-18*x+9*x^2-x^3
15.4 chebyshev polynomials
the chebyshev polynomials are solutions of the differential equations
(15.68)
and
(15.69)
the solutions of (15.68) are referred to as chebyshev polynomials of the first kind and are denoted
as .* the solutions of (15.69) are the chebyshev polynomials of the second kind these are
denoted as . both kinds comprise a set of orthogonal functions.
l0(x) ex
x0
0
d
d x0e–x ( ) exe–x = = = e0 = 1
l1(x) ex
dx
d xe–x ( ) ex e–x xe–x = = ( – ) = 1 – x
l2(x) ex
x2
2
d
d x2e–x ( ) exe–x = = (2 – 4x + x2) = 2 – 4x + x2
l3(x) ex
x3
3
d
d x3e–x = ( ) = 6 – 18x + 9x2 – x3
(1 – x2)d 2 y
dx2 - xdy
dx
– - + n2y = 0
(1 – x2)d 2 y
dx2 - 3xdy
dx
– - + n(n + 2)y = 0
y = tn(x)
y = un(x)
chapter 15 bessel, legendre, and chebyshev functions
15-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we will restrict our discussion to the polynomials. we will plot some of these later in this
section.
two interesting properties of the polynomials are:
1. they exhibit equiripple amplitute characteristics over the range , that is, within this
range, they oscillate with the same ripple. this property is the basis for the chebyshev approximation
in the design of chebyshev type electric filters.
2. for they increase or decrease more rapidly than any other polynomial of order .
these polynomials are tabulated in reference books which contain mathematical functions. a
good reference is the handbook of mathematical functions, dover publications. they can also be
derived from the following relations.
(15.70)
(15.71)
using (15.70) or (15.71), we can express as polynomials in powers of . some are shown in
table 15.2.
* some books use the notation for these polynomials. however, another class of orthogonal functions known as genenbauer
or ultraspherical functions use the notation and for this reason, we will avoid notation for the chebyshev
polynomials.
table 15.2 chebyshev polynomials expressed in powers of x
n
0 1
1 x
2
3
4
5
6
ck(x)
cn
(a) (x) ck(x)
tn(x)
tn(x)
–1 ? x ? 1
x > 1 n
tn(x) n –1x = cos( cos ) for x ? 1
tn(x) h n h–1x = cos ( cos ) for x > 1
tn(x) x
tn(x)
2x2 – 1
4x3 – 3x
8x4 – 8x2 + 1
16x5 – 20x3 + 5x
32x6 – 48x4 + 18x2 – 1
numerical analysis using matlab and spreadsheets, second edition 15-23
orchard publications
chebyshev polynomials
to show that the relation of (15.70) can be expressed as a polynomial, we let
(15.72)
and
(15.73)
next, in (15.73), we replace with and we get
(15.74)
similarly, replacing with , we get
(15.75)
now, we add (15.74) with (15.75), and making use of (15.73) and (15.72), we get
(15.76)
or
then, we can replace with to get
(15.77)
the polynomials in table 15.2, can now be verified by using a combination of the above relations.
thus, for , (15.73) yields
(15.78)
for , from (15.73) and (15.72), we get
(15.79)
to derive the algebraic expressions corresponding to and so on, we use the recurrence
formula of (15.77). for instance, when ,
(15.80)
and when ,
(15.81)
alternately, we can prove the first 3 entries of table 15.2 with (15.70) by letting .
x = cosy
tn(y) = cosny
n n + 1
tn + 1 (y) = cos(n + 1)y = cosny cosy – sinny siny
n n – 1
tn – 1 (y) = cos(n – 1)y = cosny cosy + sinny siny
tn 1 + y ( ) tn 1 – y ( ) + 2 ny y cos cos 2tn y ( )x 2xtn = = = (y)
tn + 1 (y) = 2xtn (y) – tn – 1(y)
y x
tn + 1 (x) = 2xtn (x) – tn – 1 (x)
recurrence relation
n = 0
t0 (y) = t0 (x) = 1
n = 1
t1 (y) = t1(x) = x
n = 2, 3, 4
n = 2
t2(x) = 2xt1 (x) – t0 (x) = 2x2 – 1
n = 3
t3(x) = 2xt2(x) – t1(x) = 4x3 – 2x – x = 4x3 – 3x
y –1x = cos
chapter 15 bessel, legendre, and chebyshev functions
15-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
thus, for ,
for ,
and for ,
or
relation (15.71) can be derived from (15.70) as follows:
we recall that
(15.82)
and
(15.83)
then,
(15.84)
and when ,
* (15.85)
by substitution into (15.70), making use of (15.85), and that , we get
and this is the same as (15.71).
we can also use matlab to convert (15.70) and (15.71) to polynomials. for example, if ,
syms x
expand(cos(3*acos(x))), expand(cosh(3*acosh(x)))
* let then , , and (15.85) follows.
n = 0
t0(x) 0 –1x = cos( ? cos ) = cos(0 ? y) = 1
n = 1
t1(x) 1 –1x = cos( ? cos ) = cos(1 ? y) = cosy = x
n = 2
t2(x) 2 –1x = cos( ? cos ) = cos(2 ? y) = cos2y = 2cos2y – 1
2 –1x ( cos ) 2 cos – 1 2 x –1 cos( cos )
x
–1x cos( cos )
x
= = ? – 1
?
?
?
?
?
?
?
?
?
?
t2(x) = 2x2 – 1
cos? e j? e–j? +
2
= -
cosh? e? e –? +
2
= -
cos? = coshj?
x > 1
–1x cos j –1x = – cosh
cos? = coshj? = v ? –1v = cos j? –1v = cosh j –1v cos –1v = cosh
cosh(–t) = cosht
tn(x) n j –1x cos[ (– cosh )] jn –1x cos(– cosh ) jnj –1x = = = cosh( cosh )
j jn –1x = cosh[ (– cosh )] n –1x = cosh( cosh )
n = 3
numerical analysis using matlab and spreadsheets, second edition 15-25
orchard publications
chebyshev polynomials
ans =
4*x^3-3*x
ans =
4*x^3-3*x
the matlab code below plots the for through .
% chebyshev polynomials
%
x=?1.2:0.01:1.2 tnx0=cos(0*acos(x))
tnx1=cos(1*acos(x)) tnx2=cos(2*acos(x)) tnx3=cos(3*acos(x)) tnx4=cos(4*acos(x))
tnx5=cos(5*acos(x)) tnx6=cos(6*acos(x))
plot(x, tnx0, x, tnx1, x, tnx2, x, tnx3, x, tnx4, x, tnx5, x, tnx6) ....
axis([?1.2 1.2 ?1.5 1.5]) grid title( chebyshev polynomials of the first kind )
xlabel( x ) ylabel( tn(x) )
% we could have used the gtext function to label the curves but it is easier with the figure text
% tool
figure 15.6 shows the plot of the chebyshev polynomials of the first kind for
through .
as mentioned earlier, chebyshev polynomials, among other applications, are used in the design
of electric filters.* the filters are described in terms of rational polynomials that approximate the
behavior of ideal filters. the basic chebyshev low-pass filter approximation is defined as
(15.86)
where is the operating radian frequency, is the cutoff frequency, and and are other
parameters that are used to specify the order and type of the electric filter.
for example, if we want to design a second order chebyshev low-pass filter, we use the chebyshev
polynomial
and (15.86) becomes
(15.87)
* for a thorough discussion on the design of analog and digital filters refer to signals and systems with matlab applications,
orchard publications, isbn 0-9709511-6-7.
tn(x) n = 0 n = 6
tn(x) n = 0
n = 6
a2(?) ?
1 ?2t2
+ n(? ? ?c)
= -
? ?c ? ?
t2(x) = 2x2 – 1
a2(?) ?
1 + ?2[2(? ? ?c)2 – 1]
=
chapter 15 bessel, legendre, and chebyshev functions
15-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 15.6. plot of chebyshev polynomials with matlab
15.5 summary
• differential equations with variable coefficients cannot be solved in terms of familiar functions
as those which we encountered in ordinary differential equations with constant coefficients.
the usual procedure is to derive solutions in the form of infinite series, and the most common
are the method of frobenius and the method of picard.
• bessel functions are solutions of the differential equation
where can be any number, positive or negative integer, fractional, or even a complex number.
the general solution depends on the value of .
x2
x2
2
d
d y x dx
dy x2 n2 + + ( – )y = 0
n
n
numerical analysis using matlab and spreadsheets, second edition 15-27
orchard publications
summary
• the series
where is any positive real number or zero is referred to as bessel function of order .
• the series
is referred to as the bessel function of negative order .
• for and the series reduce to
• two more useful relations are
• values of can be calculated using the appropriate series given above. they also can be
found in math table books, and can also be found with the matlab besselj(n,x) function or
the excel besselj(x,n) function.
• the bessel functions
and
are known as half-order bessel functions.
• besides the above functions known as bessel functions of the first kind, other bessel functions,
denoted as and referred to as bessel functions of the second kind, or weber functions,
jn(x) (–1)k
k = 0
??
x2
? ?
? ? n + 2k 1
k! ? ?(n + k + 1)
= ? ? - n ? 0
n n
j–n(x) (–1)k
k = 0
??
x2
? ?
? ? – n + 2k 1
k! ? ?(– n + k + 1)
= ? ? -
n
n = 0, 1 2
j0(x) 1 x2
22 ? (1!)2
– - x4
24 ? (2!)2
- x6
26 ? (3!)2
- x8
28 ? (4!)2
= + – + - – …
j1 x ( ) x2
x3
23 ? 1! ? 2!
– x5
25 ? 2! ? 3!
x7
27 ? 3! ? 4!
x9
29 ? 4! ? 5!
= + – + –…
j2(x) x2
22 ? 2!
x4
24 ? 1! ? 3!
– x6
26 ? 2! ? 4!
x8
28 ? 3! ? 5!
x10
210 ? 4! ? 6!
= + – + –…
dx
d j0(x) j= – 1(x)
dx
d {xj1(x)} xj= 0(x)
jn(x)
j1 ? 2(x) 2
?x
= sinx j(–1 ? 2)(x) 2
?x
= cosx
yn(x)
chapter 15 bessel, legendre, and chebyshev functions
15-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
or neumann functions exist. also, certain differential equations resemble the bessel equation,
and thus their solutions are called modified bessel functions, or hankel functions.
• if is not an integer, and are linearly independent for this case, the general solution
of the bessel equation is
• if and are distinct roots of , and , then,
and thus we say that and are orthogonal in the interval .
• the differential equation
where is a constant, is known as legendre’s equation.
• the infinite series solution of the legendre functions, denoted as , is referred to as legendre
functions of the second kind.
• the legendre polynomials are defined as
and the first 6 legendre polynomials are
• the relation
n jn(x) j–n(x)
y ajn = (x) + bj–n(x)
n ? 0, 1, 2, 3, …
a b j0(x) = 0 j0(a) = 0 j0(b) = 0
xj0(ax)j0(bx)dx
0
1
? = 0
j0(ax) j0(bx) 0 ? x ? 1
(1 – x2)d 2 y
dx2 - 2xdy
dx
– - + n(n + 1)y = 0
n
qn(x)
pn(x) (–1)k ? (2n – 2k)!
2nk!(n – k)!(n – 2k)!
-xn – 2k
k = 0
n?
=
where n n2
for n even and n (n – 1)
2
= = = for n = odd
p0(x) = 1 p1(x) = x
p2 x ( ) 12
3x2 1 – ( ) = p3 x ( ) 12
= (5x3 – 3x)
p4 x ( ) 18
35x4 30x2 – 3 + ( ) = p5 x ( ) 18
= (63x5 – 70x3 + 15x)
pn(x) 1
2n ? n!
- d n
dxn
(x2 – 1)
n
= ?
numerical analysis using matlab and spreadsheets, second edition 15-29
orchard publications
summary
is known as rodrigues’ formula, and offers another method of expressing the legendre polynomials.
• the legendre polynomial series of a function for , is
and with this relation we can find a polynomial defined as
• the trigonometric form of the legendre polynomials is
and the first 6 legendre polynomials in trigonometric form listed below.
• the legendre polynomials in algebraic form, satisfy the orthogonality principle when as
indicated by the integral
• the legendre polynomials in trigonometric form satisfy the orthogonality principle when
as indicated by the integral
f(x) x < 1
bn
2n + 1
2
- f (x)pn(x)dx
–1
1
= ?
f (x)
f (x) b0p0 (x) + b1p1(x) + b2p2(x) +… + bnpn(x) bnpn(x)
n = 0
??
= =
pn cos? 1 ? 3 ? …? (2n – 1)
2 ? 4 ?… ? 2n
-2 n? cos 12
1 ? 3 ?… ? (2n – 3)
2 ? 4 ?… ? (2n – 2)
= + ? -2cos(n – 2)?
+ 1 ? 3
2 ? 4
1 ? 3 ?… ? (2n – 5)
2 ? 4 ?… ? (2n – 4)
? -2cos(n – 4)? + …
p0 cos? = 1
p1 cos? = cos?
p2 cos? 3cos2? + 1
4
= -
p3 cos? 5cos3? + 3cos?
8
= -
p4 cos? 35cos4? + 20cos2? + 9
64
=
p5 cos? 63cos5? + 35cos3? + 30cos?
128
= -
m ? n
pm(x)pn(x)dx
–1
1
?
0 m ? n
2
2n + 1
- m = n
? ? ? ? ?
=
m ? n
chapter 15 bessel, legendre, and chebyshev functions
15-30 numerical analysis using matlab and spreadsheets, second edition
orchard publications
• the differential equation
is referred to as the associated legendre differential equation. the general solution of this
equation is
where and are arbitrary constants. the functions and are referred to as
associated legendre functions of the first and second kind respectively. these functions, also
known as spherical harmonics, are evaluated from the relations
• the matlab legendre(n,x) function computes the associated legendre functions of the first
kind of degree , and order evaluated for each element of .
• the solutions of the differential equation
are known as laguerre polynomials and are denoted as . these polynomials are satisfy
the orthogonality principle. they are computed with the rodrigues’ formula
• the chebyshev polynomials are solutions of the differential equations
pm( cos?)pn( cos?) sin?d?
0
?
?
0 m ? n
2
2n + 1
- m = n
? ? ? ? ?
=
(1 – x2)d 2 y
dx2 - 2xdy
dx
– - n(n + 1) m2
1 – x2
+ – - y = 0
y c1pn
m(x) c2qn
= + m(x)
c1 c2 pn
m(x) qn
m(x)
pn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -pn(x)
qn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -qn(x)
n m = 0, 1, 2,…, n x
x
x2
2
d
d y 1 x ( – )
dx
dy ny + + = 0
ln(x)
ln(x) ex
xn
n
d
d xne–x = ( )
(1 – x2)d 2 y
dx2 - xdy
dx
– - + n2y = 0
numerical analysis using matlab and spreadsheets, second edition 15-31
orchard publications
summary
and
the solutions of the first differential equation are referred to as chebyshev polynomials of the
first kind and are denoted as . the solutions of the second are the chebyshev polynomials
of the second kind these are denoted as . both kinds comprise a set of
orthogonal functions.
• the polynomials are derived from the relations
these polynomials exhibit equiripple amplitute characteristics over the range , that
is, within this range, they oscillate with the same ripple as shown in figure 15.6. this property
is the basis for the chebyshev approximation in the design of chebyshev type electric filters.
(1 – x2)d 2 y
dx2 - 3xdy
dx
– - + n(n + 2)y = 0
y = tn(x)
y = un(x)
tn(x)
tn(x) n –1x = cos( cos ) for x ? 1
tn(x) h n h–1x = cos ( cos ) for x > 1
–1 ? x ? 1
chapter 15 bessel, legendre, and chebyshev functions
15-32 numerical analysis using matlab and spreadsheets, second edition
orchard publications
15.6 exercises
1. use the appropriate series of the bessel functions to compute the following values using
the first 4 terms of the series and check your answers with matlab or excel.
a. b. c. d.
2. use the appropriate legendre polynomials or and rodrigues’s formulas to compute
the following, and check your answers with matlab.
a. b. c.
d. e. f.
3. compute the legendre polynomial representing the waveform of the figure below.
the first 5 terms of , i.e., through will be sufficient. then, use matlab or
excel to plot and compare with .
jn(x)
j0(3) j1(2) j1 ? 2(? ? 6) j–1 ? 2(? ? 3)
pn(x) pn
m(x)
p1(x) x = 0.5 p2(x) x = 0.75 p3(x) x = 0.25
p1
2(x) x = 0.5 p2
3(x)x = –0.5 p3
2(x) x = 0.25
g(x) f (x)
pn(x) p0(x) p4(x)
g(x) f (x)
?1 1 x
1
f (x)
numerical analysis using matlab and spreadsheets, second edition 15-33
orchard publications
solutions to exercises
15.7 solutions to exercises
1.
a.
b.
c.
d.
check with matlab:
besselj(0,3), besselj(1,2), besselj(0.5,pi/6), besselj(?0.5,pi/3)
ans =
-0.2601
ans =
0.5767
ans =
0.5513
ans =
0.3898
we observe that the first value returned by matlab above is significantly different from
that we obtained from the series. this is because our computation was based on the first 4
terms of the series. had we taken also the fifth term our answer would have been and
this is much closer to the value obtained with matlab.
2.
we will use the relations of (15.34) and (15.62). they are repeated below for convenience.
j0(x) 1 x2
22 ? (1!)2
– - x4
24 ? (2!)2
- x6
26 ? (3!)2
= + – -
x = 3
1 94
– 81
64
- 729
2304
= + – - = –0.3008
j1 x ( ) x2
x3
22 ? 4
– x5
22 ? 42 ? 6
x7
22 ? 42 ? 62 ? 8
= + –
x = 2
1 8
16
– - 32
384
128
18432
= + – = 0.5764
j1 ? 2 (x) 2
?x
sinx
x = ? ? 6
2
?(? ? 6)
sin(? ? 6) 12
?
- 12
? 2 3
2?
3
?
= = = = = = 0.5513
j–1 ? 2 (x) 2
?x
cosx
x = ? ? 3
2
?(? ? 3)
cos(? ? 3) 6
?
12
? 6
2?
= = = = = 0.3898
–0.2563
chapter 15 bessel, legendre, and chebyshev functions
15-34 numerical analysis using matlab and spreadsheets, second edition
orchard publications
a.
b.
c.
d.
we recall that if , then .
e.
this is because . in other words,
f.
check with matlab:
p0(x) = 1 p1(x) = x
p2 x ( ) 12
3x2 1 – ( ) = p3 x ( ) 12
= (5x3 – 3x)
p4 x ( ) 18
35x4 30x2 – 3 + ( ) = p5 x ( ) 18
= (63x5 – 70x3 + 15x)
pn
m(x) (–1)m(1 – x2)m ? 2 d m
dxm = ? -pn(x)
p1(x) x = 0.5 = x = 0.5
p2(x) x = 0.75
12
(3x2 – 1)
x = 0.75
= = 0.5(3 × (0.75)2 – 1) = 0.3438
p3(x) x = 0.25
12
(5x3 – 3x)
x = 0.25
= = 0.5(5 × (0.25)3 – 3 × 0.25) = –0.3359
p1
2(x) x = 0.5 (–1)2(1 – x2)2 ? 2 d2
dx2 p1(x)
x = 0.5
(1 – x2) d2
dx2 ? (x)
x = 0.5
= = = 0
m > n pn
m(x) = 0
p2
3(x)x = –0.5 = 0
m > n
d3
dx3 p2(x) d3
dx3 12
(3x2 – 1)
? ?
? ?
? ?
= = 0
p3
2(x) x = 0.25 (–1)2(1 – x2)2 ? 2 d2
dx2 p3(x)
x = 0.25
(1 – x2) d2
dx2 12
(5x3 – 3x)
? ?
? ?
? ?
x = 0.25
= =
1 0.252
– ( ) 12
d
dx
? ? -(15x2 – 3)
x = 0.25
0.4688(30x) x = 0.25 = = = 3.5160
numerical analysis using matlab and spreadsheets, second edition 15-35
orchard publications
solutions to exercises
a.
m=0:1 y=zeros(2,2) y(:,1)=m y(:,2)=legendre(1,0.5)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 0.5000
1 -0.8660
b.
m=0:2 y=zeros(3,2) y(:,1)=m y(:,2)=legendre(2,0.75)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 0.3438
1 -1.4882
2 1.3125
c.
m=0:3 y=zeros(4,2) y(:,1)=m y(:,2)=legendre(3,0.25)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 -0.3359
1 0.9985
2 3.5156
3 -13.6160
d.
m=0:1 y=zeros(2,2) y(:,1)=m y(:,2)=legendre(1,0.5)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 0.5000
1 -0.8660
here, the legendre(n,x) function computes the associated legendre functions of degree n
and order m = 0, 1, ..., n, evaluated for each element of x. for this example, , that is,
and and the statement m=0:2 is not accepted. for this reason we’ve used
m=0:1.
e.
m=0:2 y=zeros(3,2) y(:,1)=m y(:,2)=legendre(2,?0.5)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
as in (d) above and matlab returns
m > n
m = 2 n = 1
m > n
chapter 15 bessel, legendre, and chebyshev functions
15-36 numerical analysis using matlab and spreadsheets, second edition
orchard publications
m legendre
0 -0.1250
1 1.2990
2 2.2500
f.
m=0:3 y=zeros(4,2) y(:,1)=m y(:,2)=legendre(3,0.25)
fprintf( \n ) fprintf( m\t legendre \n ) fprintf( %2.0f\t %7.4f \n ,y )
m legendre
0 -0.3359
1 0.9985
2 3.5156
3 -13.6160
3.
for this exercise for and thus
then,
bn
2n + 1
2
- f (x)pn(x)dx
–1
1
= ?
f(x) = 0 –1 ? x ? 0
bn
2n + 1
2
- f (x)pn(x)dx
0
1
= ?
b0
12
x ? p0(x) ? dx
0
1
? 12
x2
2
?
0
1
14
= = =
b1
32
x ? p1(x) ? dx
0
1
? 32
x3
3
?
0
1
12
= = =
b2
52
x 12
? (3x2 – 1)dx
0
1
? 54
(3x3 – x)dx
0
1
? 54
3x4
4
x2
2
? – ?
? ?
0
1
5
16
= = = = -
b3
72
x 12
? (5x3 – 3x)dx
0
1
? 74
(5x4 – 3x2)dx
0
1
? 74
5x5
5
3x3
3
? – ?
? ?
0
1
= = = = 0
b4
92
x 18
? (35x4 – 30x2 + 3)dx
0
1
? 9
16
- (35x5 – 30x3 + 3x)dx
0
1
= = ?
9
16
- 35x6
6
- 30x4
4
– - 3x2
2
? + ?
? ?
0
1
3
32
= = – -
numerical analysis using matlab and spreadsheets, second edition 15-37
orchard publications
solutions to exercises
and by substitution into
we get
or
we note that and . these values are close to zero. also,
and this value is close to unity.
we plot with the matlab code below.
x=0: 0.01: 1 fx=(15+128.*x+210.*x.^2?105.*x.^4)./256 plot(x,fx) xlabel(‘x’) ylabel(‘f(x)’)
grid
f (x) b0p0 (x) + b1p1(x) + b2p2(x) +… + bnpn(x) bnpn(x)
n = 0
??
= =
f x ( ) 14
p0 x ( ) 12
p1(x) 5
16
- p2(x) 0 ? p3(x) 3
32
= + + + – -p4(x)
14
1 ? 12
? x 5
16
- 12
? (3x2 – 1) 3
32
- 18
= + + – ? (35x4 – 30x2 + 3)
14
x2
15x2
32
- 5
32
– - 105x4
256
– 90x2
256
- 9
256
= + + + –
f(x) 1
256
= (15 + 128x + 210x2 – 105x4)
f(–1) = –8 ? 256 f(0) = 15 ? 256
f(1) = 248 ? 256
f(x)
chapter 15 bessel, legendre, and chebyshev functions
15-38 numerical analysis using matlab and spreadsheets, second edition
orchard publications
notes
numerical analysis using matlab and spreadsheets, second edition 16-1
orchard publications
chapter 16
optimization methods
his chapter introduces three methods for maximizing or minimizing some function in order
to achieve the optimum solution. these methods are topics discussed in detail in a branch of
mathematics called operations research that is concerned with financial and engineering economic
problems. our intent here is to introduce these methods with the basic ideas. we will discuss
linear programming, dynamic programming, and network analysis and we will illustrate these
with some simple but practical examples.
16.1 linear programming
in linear* programming we seek to maximize or minimize a particular quantity, referred to as the
objective, which is dependent on a finite number of variables. these variables may or may not be
independent of each another, and in most cases are subject to certain conditions or limitations
referred to as constraints.
example 16.1
the abc semiconductor corporation produces microprocessors ( ) and memory ( )
chips. the material types, and , required to manufacture the and and the profits
for each are shown in table 16.1.
due to limited supplies of silicon, phosphorus and boron, its product mix at times of high consumer
demand, is subject to limited supplies. thus, abc semiconductor can only buy parts of
material , and parts of material . this corporation needs to know what combination of
and will maximize the overall profit.
* a linear program is one in which the variables form a linear combination,i.e., are linearly related. all other programs are
considered non-linear.
table 16.1 data for example 16.1
parts of material types
(1000s)
semiconductor material 3 2
semiconductor material 5 10
profit $25.00 per unit $20.00 per 1000
t
?ps ram
a b ?ps rams
?ps rams
a
b
450
a 1000 b
?ps rams
chapter 16 optimization methods
16-2 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
since with material we can produce and , and with material and
, the corporation is confronted with the following constraints:
we now can state the problem as
(16.1)
subject to the constraints
(16.2)
two additional constraints are , , and and must be integers.
for this example, there are only two variables, and therefore, a graphical solution is possible.
we will solve this example graphically.
the and intercept corresponding to the above equations is shown in the plot of figure 16.1
where the cross-hatched area indicates the feasible region.*
figure 16.1. plot of constraint lines for example 16.1
the equation of the straight line of the maximum profit is referred to as isoprofit line. this line will
pass through one of the three corners denoted as , , and .
* the feasible region is the area which includes all points ( ) satisfying all constrains.
a 3?ps 2 rams b 5 ?ps 10
rams
3x + 2y ? 450
5x + 10y ? 1000
maximize z = 25 × ?p + 20 × rams
3x + 2y ? 450
5x + 10y ? 1000
x ? 0 y ? 0 x y
x y
x y
x, y
50
100
50 100
150
200
250
150 200 250
3x + 2y = 450
5x + 10y = 1000
isoprofit line
x
y
a
b
c
a b c
numerical analysis using matlab and spreadsheets, second edition 16-3
orchard publications
linear programming
the isoprofit line that we are interested is described by the equation
(16.3)
we can express this equation in form, that is,
(16.4)
where is the -intercept. therefore, all possible isoprofit lines have the same slope, that is, are
parallel to each another, and the highest isoprofit line passes through point .
the coordinates of point in figure 16.1 are found by simultaneous solution of
(16.5)
using matlab for the solution of (16.5) we get
syms x y
[x y]=solve(3*x+2*y?450, 5*x+10*y?1000)
x =
125
y =
75/2
of course, these values must be integers, so we accept the values , and . then, by
substitution into (16.1),
(16.6)
and the isoprofit line can be drawn from the equation
(16.7)
by first letting , then, . then, we obtain the points
and
this is shown as a dotted line on the plot of figure 16.1.
it was possible to solve this problem graphically because it is relatively simple. in most cases, howz
= 25 × ?p + 20 × ram = cons tant
= 25x + 20y = c
y = mx + b
y 25
20
– -x c
20
= + - = – 1.25x + k
k y
b
b
3x + 2y = 450
5x + 10y = 1000
x = 125 y = 37
zmax = 25 × 125 + 20 × 37 = $3865
25x + 20y = 3865
x = 0 y = 0
x 3865
25
= - = 154.6
y 3865
20
= - = 193.25
chapter 16 optimization methods
16-4 numerical analysis using matlab and spreadsheets, second edition
orchard publications
ever, we cannot obtain the solution by graphical methods and therefore, we must resort to algebraic
methods such as the simplex method. this and other methods are described in operations
research textbooks.
we can find the optimum solution to this type of problems with excel’s solver feature. the procedure
is included in the spreadsheet of figure 16.2.
figure 16.2. spreadsheet for solution of example 16.1 with excel’s solver
16.2 dynamic programming
dynamic programming is based on r. bellman’s principle of optimality which states that:
an optimum policy has the property that whatever the initial state and the initial decisions are,
the remaining decisions must constitute an optimum policy with regard to the state resulting from
the first decision.
figure 16.3 represents a line graph, where the nodes through represent the states, and the
choice of alternative paths when leaving a given state, is called a decision. the alternative paths
are represented by the line segments , , , and so on.
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e f
optimization - maximum profit for example 16.1
1. enter zeros in b12 and b13 2. in b15 enter =25*b12+20*b13
3. in b17 enter =3*b12+2*b13 and in b18 =5*b12+10*b13
4. from the tools droping menu select solver . use add-ins if necessary to add it.
5. on the solver parameters screen enter the following:
set target cell: b15
equal to: max
by changing cells: b12:b13
click on add and enter constraints:
b12=integer, add b13=integer, add b12>=0, add b13>=0,
add b17<=450, add b18<=1000, ok, solve
x(?ps)= 124
y(rams)= 38
maximum profit= $3,860
semiconductor material a= 448
semiconductor material b= 1000
note: contents of a12:a18 are typed-in for information only
a h
ab ac bd
numerical analysis using matlab and spreadsheets, second edition 16-5
orchard publications
dynamic programming
figure 16.3. line graph for a typical dynamic programming example
we assume that all segments are directed from left to right, and each has a value assigned to it
which we will refer to as the cost. thus, there is a cost associated with each segment, and it is usually
denoted with the letter . for example, for the path , , , and , the cost is
(16.8)
the costs for the other possible paths are defined similarly.
for the line graph of figure 16.3, the objective is to go from state to state with minimum cost.
accordingly, we say that the optimum path policy for this line graph is
(16.9)
now, let us suppose that the initial state is , and the initial decision has been made to go to state
. then, the path from to must be selected optimally, if the entire path from to is to be
optimum (minimum in this case).
let the minimum cost from state to be denoted as . then,
(16.10)
likewise, if the initial decision is to go from state to , the path from state to must be optimum,
that is,
(16.11)
the optimum path policy of (16.9) can now be expressed in terms of (16.10) and (16.11) as
(16.12)
this relation indicates that to obtain the minimum cost we must minimize:
a
b
c
d
f
g
h
e
j ac f h
jah = jac + jcf + jfh
a h
jmin min (jac + jcf + jfh) (jac + jce + jeh)
(jab + jbd + jdh) (jab + jbg + jgh)
, ,
,
{
}
=
a
b b h a h
b h gb
gb = min{(jbd + jdh), (jbg + jgh)}
a c c h
gc = min{(jcf + jfh), (jce + jeh)}
ga= jmin = min{(jab + gb), (jac + gc)}
chapter 16 optimization methods
16-6 numerical analysis using matlab and spreadsheets, second edition
orchard publications
1. the part which is related to the present decision, in this case, costs and .
2. the part which represents the minimum value of all future costs starting with the state which
results from the first decision.
example 16.2
find the minimum cost route from state to state for the line graph of figure 16.4. the line
segments are directed from left to right and the costs are indicated beside each line segment.
figure 16.4. line graph for example 16.2
solution:
we observe that at states , , , and have no alternative paths since the lines are directed
from left to right. therefore, we make the first decision at state . then,
(16.13)
next, we make decisions at states and .
(16.14)
(16.15)
the final decision is at state and thus
(16.16)
jab jac
a m
k
h
f
d
e
c
b
a m
5
6
3
4
6
3
9 6
8
4
5
5
h k d f
e
ge = min{(3 + gh), (5 + gk)} = min{(3 + 5), (5 + 4)} = 8
(e ? h ? m)
b c
gb = min{(9 + gd), (6 + ge)} = min{(9 + 6), (6 + 8)} = 14
(b ?e)
gc = min{(4 + ge), (6 + gf)} = min{(4 + 8), (6 + 8)} = 12
(c ? e)
a
ga = min{(5 + gb), (3 + gc)} = min{(5 + 14), (3 + 12)} = 15
(a ? c)
numerical analysis using matlab and spreadsheets, second edition 16-7
orchard publications
dynamic programming
therefore, the minimum cost is and it is achieved through path , as shown
in figure 16.5
figure 16.5. line graph showing the minimum cost for example 16.2
example 16.3
on the line graph of figure 16.6, node represents an airport in new york city and nodes
through several airports throughout europe and asia. all flights originate at and fly eastward.
a salesman must leave new york city and be in one of the airports , , , or at the
shortest possible time. the encircled numbers represent waiting times in hours at each airport.
the numbers in squares show the hours he must travel by an automobile to reach his destination,
and the numbers beside the line segments indicated the flight times, also in hours. which airport
should he choose ( , , , or ) to minimize his total travel time, and in how many hours after
departure from will he reach his destination?
figure 16.6. line graph for example 16.3
15 a ? c? e? h? m
k
h
f
d
e
c
b
a m
5
6
3
4
6
3
9 6
8
4
5
5
a b
l a
h j k l
h j k l
a
a
b
d
h
c
f
l
e
j
k 4
2
3
3
4
2
3 4
3
8
6
5
7
4
5
4
3
4
6
2
7
chapter 16 optimization methods
16-8 numerical analysis using matlab and spreadsheets, second edition
orchard publications
solution:
the hours that the salesman must travel by automobile to reach his destination are
, , , and (16.17)
the first decisions are made at , , and . then,
(16.18)
(16.19)
(16.20)
the next decisions are made at and where we find that
(16.21)
(16.22)
the final decision is made at , where we find
(16.23)
therefore, the minimum cost (minimum time from departure to arrival at destination) is
hours and it is achieved through path , as shown in figure 16.7.
figure 16.7. line graph showing the minimum cost for example 16.3
gh = 3 gj = 2 gk = 4 gl = 3
d e f
gd = 2 + min{(5 + gh), (4 + gj)} = 2 + min{(5 + 3), (4 + 2)} = 2 + 6 = 8
d ? j
ge = 4 + min{(3 + gj), (4 + gk)} = 4 + min{(3 + 2), (4 + 4)} = 4 + 5 = 9
e ? j
gf = 3 + min{(6 + gk), (7 + gl)} = 3 + min{(6 + 4), (7 + 3)} = 3 + 10 = 13
f ? k or f ? l
b c
gb = 4 + min{(4 + gd), (2 + ge)} = 4 + min{(4 + 8), (2 + 9)} = 4 + 11 = 15
b ? e
gc 3 min 7 ge
= + {( + ), (5 + gf)} = 3 + min{(7 + 9), (5 + 13)} = 3 + 16 = 19
c ? e
a
ga = min{(8 + gb), (6 + gc)} = min{(8 + 15), (6 + 19)} = 23
a ? b
23
a ? b? e? j
a
b
d
h
c
f
l
e
j
k 4
2
3
3
4
2
3 4
3
8
6
5
7
4
5
4
3
4
6
2
7
numerical analysis using matlab and spreadsheets, second edition 16-9
orchard publications
dynamic programming
example 16.4
a start-up, high-technology company, has $4,000,000 to invest in three different products a, b
and c. investments in each product are assumed to be multiples of $1,000,000 and the company
may allocate all the money to just one product or split it between these three products. the
expected profits are shown in table 16.2.
how should the money be allocated so that company will realize the maximum profit?
solution:
this problem can also be solved with linear programming methods but we will use the so called
tabular form of solution. let
(16.24)
denote the profits in millions from product , when units of dollars are invested in it. for simplicity,
we express the profits in millions, and we enter these in table 16.3.
our objective is to maximize the total profit that represents the sum of the profits from each
product, subject to the constraint that the amount invested does not exceed four million dollars.
in other words is, we want to
(16.25)
table 16.2 amounts invested and return on investment for each product
investments amount invested
0 $1,000,000 $2,000,000 $3,000,000 $4,000,000
return on investment
product a 0 $2,000,000 $5,000,000 $6,000,000 $7,000,000
product b 0 $1,000,000 $3,000,000 $6,000,000 $7,000,000
product c 0 $1,000,000 $4,000,000 $5,000,000 $8,000,000
table 16.3 modified table 16.2
x 0 1 2 3 4
0 2 5 6 7
0 1 3 6 7
0 1 4 5 8
pi(x) i = a, b, c
i x
p(x)
pa(x)
pb(x)
pc(x)
z
maximize z = pa(x) + pb(x) + pc(x)
chapter 16 optimization methods
16-10 numerical analysis using matlab and spreadsheets, second edition
orchard publications
subject to the constraint
(16.26)
where , , and , are the amounts to be invested in products a, b and c respectively.
the computations are done in three stages, one per product. we start by allocating units (millions)
to product c (stage c), but since we do not know what units were allocated to the previous
products a and b, we must consider all possibilities.
we let denote the value of the optimum profit that can be achieved, where the subscript j
indicates the number or stage assigned to the product, i.e., a for product a, b for product b, and
c for product c, and represents the number of money units. also, we let be the decision
that is being made to achieve the optimum value from .
at stage c, , and , i.e., millions assumed to be allocated to product c.
the possibilities that we allocate or or or or units (millions) to product c, and the
corresponding returns are, from table 16.3,
(16.27)
with decision
(16.28)
that is, the maximum appears in the fourth position since the left most is the zero position.
the next possibility is that one unit was invested in either product a or product b, by a previous
decision. in this case, do not have 4 units to invest in product c we have three or less.
if we invest the remaining three units in product c, the optimum value is found from
(16.29)
with decision
(16.30)
if we have only two units left, and we invest them in product c, we obtain the maximum from
(16.31)
with decision
(16.32)
with only one unit left to invest, we have
xa + xb + xc ? 4
xa xb xc
vj (u)
u dj (u)
vj (u)
j = c u = 4 4
0 1 2 3 4
vc (4) = max{pc(0), pc(1), pc(2), pc(3), pc(4)} = max{0, 1, 4, 5, 8} = 8
dc(4) = 8
vc (3)
vc (3) = max{pc(0), pc(1), pc(2), pc(3)} = max{0, 1, 4, 5} = 5
dc (3) = 5
vc (2) = max{pc(0), pc(1), pc(2)} = max{0, 1, 4} = 4
dc (2) = 4
numerical analysis using matlab and spreadsheets, second edition 16-11
orchard publications
dynamic programming
(16.33)
with decision
(16.34)
finally, with no units left to invest in product c,
(16.35)
with decision
(16.36)
with these values, we construct table 16.4.
next, we consider stage b, and since we do not know what units were allocated to product a
(stage a), again we must consider all possibilities.
with and and , we have
(16.37)
this expression says that if zero units were invested in product b, it is possible that all four units
were invested in product c, or if one unit was invested in product b, it is possible that 3 units were
invested in product c, and so on. inserting the appropriate values, we get
(16.38)
with decision
(16.39)
table 16.4 optimum profit and decisions made for stage c
u
0 1 2 3 4
stage
c
0 1 4 5 8
0 1 2 3 4
stage
b
stage
a
vc(1) = max{pc(0), pc(1)} = max{0, 1} = 1
dc (1) = 1
vc (0) = max{pc(0)} = max{0} = 0
dc (0) = 0
vc(u)
dc(u)
vb(u) … … … … …
db(u) … … … … …
va(u) … … … … …
da(u) … … … … …
j = b u = 4, 3, 2, 1 0
vb(4) max pb(0) + vc(4 – 0) pb(1) vc(4 – 1) pb(2)
+vc(4 – 2), pb(3) + vc(4 – 3), pb(4) + vc(4 – 4)
{ , + ,
}
=
vb(4) = max{0 + 8, 1 + 5, 3 + 4, 6 + 1, 7 + 0} = 8
db (4) = 0
chapter 16 optimization methods
16-12 numerical analysis using matlab and spreadsheets, second edition
orchard publications
since the maximum value is the zero position term.
using a similar reasoning, we have
(16.40)
or
(16.41)
with decision
(16.42)
also,
(16.43)
or
(16.44)
with decision
(16.45)
(16.46)
or
(16.47)
with decision
(16.48)
if we consider the zero position term, or
(16.49)
if we consider the first position term.
also,
(16.50)
or
(16.51)
with decision
(16.52)
next, we updating the previous table to include the stage b values. these are shown in table 16.5.
vb (3) max pb(0) + vc(3 – 0) pb(1) vc(3 – 1) pb(2)
+vc(3 – 2), pb(3) + vc(3 – 3)
{ , + ,
}
=
vb (3) = max{0 + 5, 1 + 4, 3 + 1, 6 + 0} = 6
db (3) = 3
vb (2) = max{pb(0) + vc(2 – 0), pb(1) + vc(2 – 1), pb(2)+vc(2 – 2)}
vb (2)= max{0 + 4, 1 + 1, 3 + 0} = 4
db (2) = 0
vb (1) = max{pb(0) + vc(1 – 0), pb(1) + vc(1 – 1)}
vb (1) = max{0 + 1, 1 + 0} = 1
db (1) = 0
db (1) = 1
vb (0) = max{pb(0) + vc(0 – 0)}
vb (0) = max{0 + 0} = 0
db (0) = 0
numerical analysis using matlab and spreadsheets, second edition 16-13
orchard publications
dynamic programming
finally, with and *
(16.53)
or
(16.54)
with decision
(16.55)
we complete the table by entering the values of stage a in the last two rows as shown in table
16.6. the only entries are in the last column, and this is always the case since in deriving
and , all possibilities have been considered.
table 16.6 indicates that the maximum profit is realized with , that is, units, and thus
the maximum profit is $9,000,000.
to determine the investment allocations to achieve this profit, we start with this tells
us that we should allocate units to product a, and the given table shows that units
($2,000,000) invested in this product will return $5,000,000.
table 16.5 updatingd table to include stage b values
u
0 1 2 3 4
stage
c
0 1 4 5 8
0 1 2 3 4
stage
b
0 1 4 6 8
0 1 0 3 0
stage
a
* since this is the first stage, all 4 units can be allocated to the product a or some of these can be allocated to products b and
c. therefore, considers all possibilities.
vc(u)
dc(u)
vb(u)
db(u)
va(u) … … … … …
da(u) … … … … …
j = a u = 4
va(4)
va(4) max pa(0) + vb(4 – 0) pa(1) vb(4 – 1) pa(2)
+vb(4 – 2), pa(3) + vb(4 – 3), pa(4) + vb(4 – 4)
{ , + ,
}
=
va(4) = max{0 + 8, 2 + 6, 5 + 4, 6 + 1, 7 + 0} = 9
da(4) = 2
va(4)
da(4)
va (4) = 9 9
da (4) = 2
2 2
chapter 16 optimization methods
16-14 numerical analysis using matlab and spreadsheets, second edition
orchard publications
we now have two units left to invest in products b and c. to find out where we should invest
these units, we consider the decision at stage b. since two out of the four units have already been
invested, we have , and by reference to the table 16.6, we see that .
this tells us that we should not invest any units in product b if only two units are left. the decision
at stage c yields , and from table 16.6, . this indicates that
we should invest the remaining two units to product c where we can get a return of $4,000,000.
in summary, to obtain the maximum profit of $9,000,000, we should allocate:
1. two units to product a to earn $5,000,000
2. zero units to product b to earn $0
3. two units to product c to earn $4,000,000
16.3 network analysis
a network, as defined here, is a set of points referred to as nodes and a set of lines referred to as
branches. thus, figure 16.8 is a network with nodes , , , and , and branches ,
, , , , and .
figure 16.8. a typical network
table 16.6 updatingd table to include values for all stages
u
0 1 2 3 4
stage
c
0 1 4 5 8
0 1 2 3 4
stage
b
0 1 4 6 8
0 1 0 3 0
stage
a
9
2
vc(u)
dc(u)
vb(u)
db(u)
va(u) … … … …
da(u) … … … …
db(4 – 2) = db(2) d2(2) = 0
dc(4 – 0 – 2) = dc(2) dc(2) = 2
5 abcd e 6 ab
ac ad bd be cd
a
c
b
d
e
numerical analysis using matlab and spreadsheets, second edition 16-15
orchard publications
network analysis
branches can be either directed (or oriented), if they have a direction assigned to them, that is, oneway,
or two-way. if no direction is assigned, they are considered to be two-way. thus, the branches
and in figure 16.8, are directed but the others are not.
a network is said to be connected, if there is a path (branch) connecting each pair of nodes. thus,
the network shown in figure 16.8 is connected.
figure 16.9. a network which is connected
the network of figure 16.9 is also connected. however, the network of figure 16.10 is not connected
since the branch is removed.
figure 16.10. a network which is not connected
a tree is a connected network which has branches and nodes. for example, the network of
figure 16.11 is a tree network.
figure 16.11. a tree network
network analysis is a method that is used to solve minimum span problems. in such problems, we
seek to find a tree which contains all nodes, and the sum of the costs (shortest total distance) is a
minimum.
example 16.5
figure 16.12 represents a network for a project that requires telephone cable be installed to link
towns. the towns are the nodes, the branches indicate possible paths, and the numbers beside the
bd be
a
c
b
d
e
cd
a
c
b
d
e
n n + 1
a
b
d
e
7
chapter 16 optimization methods
16-16 numerical analysis using matlab and spreadsheets, second edition
orchard publications
branches, show the distance (not to scale) between towns in kilometers. find the minimal spanning
tree, that is, the least amount of telephone cable required to link each town.
figure 16.12. network for example 16.5
solution:
for convenience, we redraw the given network with dotted lines as shown in figure 16.13, and
we arbitrarily choose as the starting node.
figure 16.13. network of example 16.5 with no connections
we observe that there are branches associated with node , i.e., , , and . by inspection,
or from the expression
(16.56)
we find that branch is the shortest. we accept this branch as the first branch of the minimum
span tree and we draw a solid line from node to node as shown in figure 16.14.
figure 16.14. network of example 16.5 with first connection
a
c
b
d
e
g
f
5
3
4
5
8
7
5
4 5 2
10
6
a
a
c
b
d
e
g
f
5
3
4
5
8
7
5
4 5 2
10
6
3 a ab ad ac
min{ab = 3, ad = 5, ac = 4} = ab = 3
ab
a b
a
c
b
d
e
g
f
5
3
4
5
8
7
5
4 5 2
10
6
numerical analysis using matlab and spreadsheets, second edition 16-17
orchard publications
network analysis
next, we consider all branches associated with nodes and . we find that the minimum of
these is
(16.57)
and thus, is connected to the network as shown in figure 16.15.
figure 16.15. network of example 16.5 with the second connection
we continue by considering all branches associated with nodes , and , and we find that the
shortest is
(16.58)
and we add branch to the network shown in figure 16.16. the dotted lines and have
been removed since we no longer need to consider branch and , because nodes and
are already connected otherwise, we will not have a tree network.
figure 16.16. network of example 16.5 with the third connection
next, considering all branches associated with nodes , , and and we find that the shortest is
(16.59)
and the network now is connected as shown in figure 16.17.
a b
min{ad = 5, ac = 4, bd = 5, be = 8} = ac = 4
ac
a
c
b
d
e
g
f
5
3
4
5
8
7
5
4 5 2
10
6
a b c
min{ad = 5, be = 8, bd = 5, cd = 4, cf = 10} = cd = 4
cd ad bd
ad bd b d
a
c
b
d
e
g
f
3
4
8
7
5
4 5 2
10
6
b c d
min{be = 8, de = 6, dg = 7, df = 5, cf = 10} = df = 5
chapter 16 optimization methods
16-18 numerical analysis using matlab and spreadsheets, second edition
orchard publications
figure 16.17. network of example 16.5 with the fourth connection
continuing, we get
(16.60)
and the network is connected as shown in figure 16.18
figure 16.18. network of example 16.5 with the fifth connection
the last step is to determine the shortest branch to node . we find that
(16.61)
and the complete minimum span tree is shown in figure 16.19.
figure 16.19. network of example 16.5 with all connections
figure 16.19 shows that the minimum distance is kilometers.
a
c
b
d
e
g
f
3
4
8
7
5
4 5 2
6
min{be = 8, de = 6, dg = 7} = de = 6
a
c
b
d
e
g
f
3
4
7
5
4 5 2
6
g
min{eg = 5, dg = 7, fg = 2} = fg = 2
a
c
b
d
e
g
f
3
4 5 2 4
6
3 + 4 + 4 + 6 + 5 + 2 = 24
numerical analysis using matlab and spreadsheets, second edition 16-19
orchard publications
summary
16.4 summary
• linear programming is a procedure we follow to maximize or minimize a particular quantity,
referred to as the objective, which is dependent on a finite number of variables. these variables
may or may not be independent of each another, and in most cases are subject to certain conditions
or limitations referred to as constraints.
• dynamic programming is based on r. bellman’s principle of optimality which states that an
optimum policy has the property that whatever the initial state and the initial decisions are,
the remaining decisions must constitute an optimum policy with regard to the state resulting
from the first decision.
• a network, as defined in this chapter, is a set of points referred to as nodes and a set of lines
referred to as branches.
• a tree is a connected network which has branches and nodes.
• network analysis is a method that is used to solve minimum span problems. in such problems, we
seek to find a tree which contains all nodes, and the sum of the costs (shortest total distance) is
a minimum.
n n + 1
chapter 16 optimization methods
16-20 numerical analysis using matlab and spreadsheets, second edition
orchard publications
16.5 exercises
1. a large oil distributor can buy grade a oil which contains lead for per barrel from
one oil refinery company. he can also buy grade b oil which contains lead for
per barrel from another oil refinery company. the environmental protection agency (epa)
requires that all oil sold must not contain more than lead. how many barrels of each
grade of oil should he buy so that after mixing the two grades can minimize his cost while at
the same time meeting epa’s requirement? solve this problem graphically and check your
answers with excel’s solver.
2. use dynamic programming to find the minimum cost route from state to state for the line
graph shown below. the line segments are directed from left to right and the costs are indicated
beside each line segment.
3. repeat example 16.3 for the line graph shown below.
4. a salesman has hours available to visit of his customers. he will earn the commissions
shown on the table below for various visiting times. compute the optimal allocation of time
that he should spent with his customers so that he will maximize the sum of his commissions.
consider only integer number of visiting hours, and ignore travel time from customer to cus-
7% $25.00
15% $20.00
10%
a m
k
h
f
d
e
c
b
a m
3
6
3
4
5
5
8 5
7
5
4
4
a
b
d
h
c
f
l
e
j
k
3
2
2
4
5
1
2 3
4
3
5
1
6
2
6
4
4
5
7
4
7
4 4
numerical analysis using matlab and spreadsheets, second edition 16-21
orchard publications
exercises
tomer. the third row (zero hours) indicates the commission that he will receive if he just calls
instead of visiting them.
5. repeat example 16.5 for the network shown below.
visit time
(hours)
customer
1 2 3 4
0 $20 $40 $40 $80
1 $45 $45 $52 $91
2 $65 $57 $62 $95
3 $75 $61 $71 $97
4 $83 $69 $78 $98
a
c
b
d
e
f
30
20
40
50
70
40
90
50
50
chapter 16 optimization methods
16-22 numerical analysis using matlab and spreadsheets, second edition
orchard publications
16.6 solutions to exercises
1.
let be the number of barrels of grade a oil and be the number of barrels of grade b oil.
the objective is to minimize or, for simplicity,
(1)
we want to minimize (1) because it represents a cost, not a profit.
each barrel to be sold must not contain more than lead and since grade a contains
and grade b , we must have
(2)
the oil of grade a and grade b used in each barrel to be sold must be equal to unity. thus,
(3)
moreover, and cannot be negative numbers, therefore
(4)
the problem then can be stated as:
minimize (1) subject to the constraints of (2), (3), and (4). to determine the feasible region
we plot (2) and (3) where the and crossings are found by first setting and then
. thus from (2), if , and if , .
likewise, from (3), if , , and if , .
the isoprofit line passes through point and its coordinates are found by simultaneous solux
y
z = $25.00x + $20.00y
z = 25x + 20y
10% 7%
15%
0.07x + 0.15y ? 0.10
x + y = 1
x y
x ? 0 y ? 0
x y x = 0
y = 0 x = 0 y = 0.10 ? 0.15 = 2 ? 3 y = 0 x = 0.10 ? 0.07 = 10 ? 7
x = 0 y = 1 y = 0 x = 1
x
y
1
1
2 ? 3 a
1 ? 3
1 ? 7 2 ? 7 3 ? 7 4 ? 7 5 ? 7 6 ? 7 8 ? 7 9 ? 7 10 ? 7
b
c
x + y = 1
0.07x + 0.15y = 0.10
isoprofit line
b
numerical analysis using matlab and spreadsheets, second edition 16-23
orchard publications
solutions to exercises
tion of (2) and (3). for convenience, we use the following matlab code:
syms x y [x,y]=solve(0.07*x+0.15*y?0.10, x+y?1)
x =
5/8
y =
3/8
therefore, the distributor should buy grade a oil at the ratio and grade b at the
ratio and by substitution into (1)
and this represents his cost per barrel. the isoprofit line is
and the -intercept is found by setting in the equation above to zero and we find that
check with excel’s solver:
x = 5 ? 8
y = 3 ? 8
z 25 58
× 20 38
+ × 185
8
= = = $23.125
z = 25x + 20y = 23.125
y x
y – intercept = 23.125 ? 20 = 1.1563
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e f
optimization - minimum cost for exercise 16.1
1. enter zeros in b12 and b13 2. in b15 enter =25*b12+20*b13
3. in b17 enter =0.07*b12+0.15*b13 and in b18 =b12+b13
4. from the tools droping menu select solver . use add-ins if necessary to add it.
5. on the solver parameters screen enter the following:
set target cell: b15
equal to: min
by changing cells: b12:b13
click on add and enter constraints:
b12>=0, add b13>=0,
add b17<=0.10, add b18=1, ok, solve
grade a= 0.625002
grade b= 0.374999
minimum cost= $23.125
lead content= 0.10
grade a + grade b= 1.000001
note: contents of a12:a18 are typed-in for information only
chapter 16 optimization methods
16-24 numerical analysis using matlab and spreadsheets, second edition
orchard publications
2.
since the segments are directed from left to right, state is the first node where a decision
must be made and thus
therefore, the best route from state to state passes through state . next,
and
finally,
thus, the best (shortest) path is
3.
the numbers in circles represent the waiting time at these nodes.
last stage: , , , and
k
h
f
d
e
c
b
a m
3
6
3
4
5
5
8 5
7
5
4
4
e
ge = min{3 + gh, 4 + gk} = min{3 + 4, 4 + 5} = 7
e m h
gb = min{8 + gd, 5 + ge} = min{8 + 5, 5 + 7} = 12
gc = min{4 + ge, 6 + gf} = min{4 + 7, 6 + 7} = 11
ga = min{3 + gb, 5 + gc} = min{3 + 12, 5 + 11} = 15
a ? b ? e ? h ? m
a
b
d
h
c
f
l
e
j
k
3
2
2
4
5
1
2 3
4
3
5
1
6
2
6
4
4
5
7
4
7
gh = 2 gj = 2 gk = 3 gl = 4
numerical analysis using matlab and spreadsheets, second edition 16-25
orchard publications
solutions to exercises
next stage to the left:
next to initial stage:
initial stage:
therefore, minimum path is from and the numerical minimum cost is .
4.
continued on the next page
gd 1 min 6 gh
= + { + , 4 + gj} = 1 + 6 = 7 from d to j
ge 3 min 4 gj
= + { + , 5 + gk} = 3 + 6 = 9 from e to j
gf 4 min 7 gk
= + { + , 6 + gl} = 4 + 10 = 14 from f to k or f to l
gb 5 min 2 gd
= + { + , 4 + ge} = 5 + 9 = 14 from b to d
gc 2 min 7 ge
= + { + , 1 + gf} = 2 + 15 = 17 from c to f
ga 0 min 3 gb
= + { + , 5 + gc} = 17 from a to b
a ? b? d? j 17
123456789
10
11
12
13
14
15
16
17
18
19
20
a b c d e f g h
exercise 16.4 - solution
a salesman has 4 hours available to visit of his customers. he will earn the commissions shown on
the table below for various visiting times. compute the optimal allocation of time that he should spend
with his customers so that he will maximize the sum of his commissions. consider only integer number
of visiting hours, and ignore travel time from customer to customer. the third row (zero hours)
indicates the commission that he will receive if he just calls instead of visiting them.
visit time customer
(hours) 1 2 3 4
0 $20 $40 $40 $80
1 $45 $45 $52 $91
2 $65 $57 $62 $95
3 $75 $61 $71 $97
4 $83 $69 $78 $98
solution
we will follow the same method as in example 16.4
it is convenient to rearrange the table as shown below.
chapter 16 optimization methods
16-26 numerical analysis using matlab and spreadsheets, second edition
orchard publications
continued on the next page.
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
a b c d e f g h i
u
f(x) \ x 0 1 2 3 4
f1(x) 20 45 65 75 83
f2(x) 40 45 57 61 69
f3(x) 40 52 62 71 78
f4(x) 80 91 95 97 98
m4(4)= max(b26,c26,d26,e26,f26) 98 with d4(4)= 4
m4(3)= max(b26,c26,d26,e26) 97 with d4(3)= 3
m4(2)= max(b26,c26,d26) 95 with d4(2)= 2
m4(1)= max(b26,c26) 91 with d4(1)= 1
m4(1)= max(b26) 80 with d4(0)= 0
the values of m4(u) and d4(u) are entered in the table below.
u
0 1 2 3 4
m4(u) 80 91 95 97 98
d4(u) 0 1 2 3 4
m3(u) 120 132 143 153 162
d3(u) 0 1 1 2 3
m2(u) 160 172 183 193 202
d2(u) 0 0 0 0 0
m1(u) 248
d1(u) 2
next, we compute the values of m3(u) and d3(u)
m3(4)=max[f3(0)+m4(4-0), f3(1)+m4(4-1), f3(2)+m4(4-2), f3(3)+m4(4-3), f3(4)+m4(4-4)]
max(b25+f38,c25+e38,d25+d38,e25+c38,f25+b38) 162 with d3(4)= 3
m3(3)=max[f3(0)+m4(3-0), f3(1)+m4(3-1), f3(2)+m4(3-2), f3(3)+m4(3-3)]
max(b25+e38,c25+d38,d25+c38,e25+b38) 153 with d3(3)= 2
m3(2)=max[f3(0)+m4(2-0), f3(1)+m4(2-1), f3(2)+m4(2-2)]
max(b25+d38,c25+c38,d25+b38) 143 with d3(2)= 1
m3(1)=max[f3(0)+m4(1-0), f3(1)+m4(1-1)]
max(b25+c38,c25+b38) 132 with d3(1)= 1
m3(0)=max[f3(0)+m4(0-0)]
max(b25+b38) 120 with d3(0)= 0
numerical analysis using matlab and spreadsheets, second edition 16-27
orchard publications
solutions to exercises
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
a b c d e f g h i
these values are now added to the table above, rows 40 and 41
similarly, we compute the values of m2(u) and d2(u)
m2(4)=max[f2(0)+m3(4-0), f2(1)+m3(4-1), f2(2)+m3(4-2), f2(3)+m3(4-3), f2(4)+m3(4-4)]
max(b24+f40,c24+e40,d24+d40,e24+c40,f25+b40) 202 with d2(4)= 0
m2(3)=max[f2(0)+m3(3-0), f2(1)+m3(3-1), f2(2)+m3(3-2), f2(3)+m3(3-3)]
max(b24+e40,c24+d40,d24+c40,e24+b40) 193 with d2(3)= 0
m2(2)=max[f2(0)+m3(2-0), f2(1)+m3(2-1), f2(2)+m3(2-2)]
max(b24+d40,c24+c40,d24+b40) 183 with d2(2)= 0
m2(1)=max[f2(0)+m3(1-0), f2(1)+m3(1-1)]
max(b24+c40,c24+b40) 172 with d2(1)= 0
m2(0)=max[f2(0)+m3(0-0)]
max(b24+b40) 160 with d2(0)= 0
these values are added to the table above, rows 42 and 43
stage 1 is the last stage and there is only one state associated with it, u=4, and thus
m1(4)=max[f1(0)+m2(4-0), f1(1)+m2(4-1), f1(2)+m2(4-2), f1(3)+m2(4-3), f1(4)+m2(4-4)]
max(b23+f42,c23+e42,d23+d42,e23+c42,f23+b42) 248 with d1(4)= 2
these two values are the last entries into the table in cells f44 and f45
the table (rows 36 through 45) indicates that, to achieve the maximum sum of commissions,
the salesman should spend 2 hours with customer #1 (d1(4)=2), 0 hours with customer #2
(d2(4)=0), and for the remaining 2 hours he should spend 1 hour with customer #3 and 1
hour with customer #4.
check:
customer #1, 2 hours = $65
customer #2, 0 hours = $40
customer #3, 1 hour = $52
customer #4, 1 hour = $91
total = $248
chapter 16 optimization methods
16-28 numerical analysis using matlab and spreadsheets, second edition
orchard publications
5.
and thus the minimum distance is
a
c
b
d
e
f
30
20
40
50
70
40
90
50
50
min[a ? b, a ? c] = min[30, 20] = 20 a ? c
min[a ? b, c ? d, c ? f] = min[30, 50, 40] = 30 a ? b
min[b ? d, b ? e, c ? d, c? f] = min[40, 50, 50, 40] = 40 choose b ? d
min[b ? e, d ? e, d ? f, c ? f] = min[50, 90, 50, 40] = 40 c ? f
min[b ? e, d ? e, f ? e] = min[50, 90, 70] = 50 b ? e
a
c
b
d
e
f
30
20
50
40
40
20 + 30 + 40 + 40 + 50 = 180 kilometers
numerical analysis
using matlab and spreadsheets
second edition
steven t. karris
orchard publications
www.orchardpublications.com
numerical analysis using matlab and spreadsheets, second edition
copyright ” 2004 orchard publications. all rights reserved. printed in the united states of america. no part of this
publication may be reproduced or distributed in any form or by any means, or stored in a data base or retrieval system,
without the prior written permission of the publisher.
direct all inquiries to orchard publications, info@orchardpublications.com
product and corporate names are trademarks or registered trademarks of the microsoft corporation and the
mathworks, inc. they are used only for identification and explanation, without intent to infringe.
library of congress cataloging-in-publication data
library of congress control number (lccn) 2003099336
copyright tx 5-589-152
isbn 0-9744239-1-2
disclaimer
the author has made every effort to make this text as complete and accurate as possible, but no warranty is implied.
the author and publisher shall have neither liability nor responsibility to any person or entity with respect to any loss
or damages arising from the information contained in this text.
preface
numerical analysis is the branch of mathematics that is used to find approximations to difficult
problems such as finding the roots of non-linear equations, integration involving complex
expressions and solving differential equations for which analytical solutions do not exist. it is
applied to a wide variety of disciplines such as business, all fields of engineering, computer science,
education, geology, meteorology, and others.
years ago, high-speed computers did not exist, and if they did, the largest corporations could only
afford them consequently, the manual computation required lots of time and hard work. but now
that computers have become indispensable for research work in science, engineering and other
fields, numerical analysis has become a much easier and more pleasant task.
this book is written primarily for students/readers who have a good background of high-school
algebra, geometry, trigonometry, and the fundamentals of differential and integral calculus.* a
prior knowledge of differential equations is desirable but not necessary this topic is reviewed in
chapter 5.
one can use fortran, pascal, c, or visual basic or even a spreadsheet to solve a difficult problem.
it is the opinion of this author that the best applications programs for solving engineering
problems are 1) matlab which is capable of performing advanced mathematical and
engineering computations, and 2) the microsoft excel spreadsheet since the versatility offered by
spreadsheets have revolutionized the personal computer industry. we will assume that the reader
has no prior knowledge of matlab and limited familiarity with excel.
we intend to teach the student/reader how to use matlab via practical examples and for
detailed explanations he/she will be referred to an excel reference book or the matlab user’s
guide. the matlab commands, functions, and statements used in this text can be executed
with either matlab student version 12 or later. our discussions are based on a pc with
windows xp platforms but if you have another platform such as macintosh, please refer to the
appropriate sections of the matlab’s user guide that also contains instructions for installation.
matlab is an acronym for matrix laboratory and it is a very large computer application
which is divided to several special application fields referred to as toolboxes. in this book we will
be using the toolboxes furnished with the student edition of matlab. as of this writing, the
latest release is matlab student version release 13 and includes simulink which is a
* these topics are discussed in mathematics for business, science, and technology by this author, orchard publications,
isbn 0-9709511-0-8. this text includes probability and other advanced topics which are supplemented by many practical
applications using microsoft excel and matlab.
software package used for modeling, simulating, and analyzing dynamic systems. simulink is
not discussed in this text the interested reader may refer to the documentation which also
includes demo models with detailed explanations. additional information including purchasing
may be obtained from the mathworks, inc., 3 apple hill drive, natick, ma 01760-2098.
phone: 508 647-7000, fax: 508 647-7001, e-mail: info@mathwork.com and web site http://
www.mathworks.com.
the author makes no claim to originality of content or of treatment, but has taken care to present
definitions, statements of physical laws, theorems, and problems.
chapter 1 is an introduction to matlab. the discussion is based on matlab student version
5 and it is also applicable to version 6. chapter 2 discusses root approximations by numerical
methods. chapter 3 is a review of sinusoids and complex numbers. chapter 4 is an introduction to
matrices and methods of solving simultaneous algebraic equations using excel and matlab.
chapter 5 is an abbreviated, yet practical introduction to differential equations, state variables,
state equations, eigenvalues and eigenvectors. chapter 6 discusses the taylor and maclaurin
series. chapter 7 begins with finite differences and interpolation methods. it concludes with
applications using matlab. chapter 8 is an introduction to linear and parabolic regression.
chapters 9 and 10 discuss numerical methods for differentiation and integration respectively.
chapter 11 is a brief introduction to difference equations with a few practical applications.
chapters 12 is devoted to partial fraction expansion. chapters 13, 14, and 15 discuss certain
interesting functions that find wide application in science, engineering, and probability. this text
concludes with chapter 16 which discusses three popular optimization methods.
new to the second edition
this is an extensive revision of the first edition. the most notable changes are the inclusion of
fourier series, orthogonal functions and factorization methods, and the solutions to all end-ofchapter
exercises. it is in response to many readers who expressed a desire to obtain the solutions
in order to check their solutions to those of the author and thereby enhancing their knowledge.
another reason is that this text is written also for self-study by practicing engineers who need a
review before taking more advanced courses such as digital image processing. the author has
prepared more exercises and they are available with their solutions to those instructors who adopt
this text for their class.
another change is the addition of a rather comprehensive summary at the end of each chapter.
hopefully, this will be a valuable aid to instructors for preparation of view foils for presenting the
material to their class.
the last major change is the improvement of the plots generated by the latest revisions of the
matlab® student version, release 13.
orchard publications
fremont, california
www.orchardpublications.com
info@orchardpublications.com
index
symbols chart wizard in excel 2-10, 8-6 differential equation
chebyshev polynomials classification of 5-2
% (percent) symbol in matlab 1-2 of the first kind 15-21 degree of 5-3
%d in matlab 2-21 of the second kind 15-21 most general solution 5-6
%e in matlab 2-21 cholesky factorization 14-15 order of 5-2
%s in matlab 2-21 clc matlab command 1-2 numerical solution by the
%u in matlab 2-21 clear matlab coommand 1-2 runge-kutta method 9-5
=if in excel 2-24 cofactor - see matrix type of 5-2
collect(s) matlab function 12-11 dipole 15-12
a column vector 1-18 direct term in matlab 12-1
command screen in matlab 1-1 disp(x) matlab command 2-5
abs(z) matlab function 1-22 command window in matlab 1-1 display formats in matlab 1-29
absolute cell in excel 2-19 commas in matlab 1-6 divided difference 7-1
absolutely convergent function 15-10 comment line in matlab 1-2 division in matlab
add trendline feature in excel 8-5 complex element-by-element 1-20
adjoint of a matrix - see matrix conjugate of a matrix - see matrix division of phasors 3-12
alternating currents 3-1 conjugate of a number 1-4, 3-12 dot multiplication in matlab 1-20
angle(z) matlab function 1-22 number 1-2, 3-11 dsolve(s) matlab function 9-5
angular velocity 3-2 roots of characteristic equation 5-9 dynamic programming 16-4
antidifference 7-11 conformable optimum path policy 16-5
approximations with excel 2-7 for addition - see matrix
autofill feature in excel feature 2-9 for multiplication - see matrix e
axis([xmin xmax ymin ymax]) conj(x) matlab function 4-8
matlab command 1-21 conjugate of a complex number 3-12 editor window in matlab 1-1
constraints - see linear programming editor/debugger in matlab 1-1
b contour matlab function 1-16 eigenvalues 5-30
conv(a,b) matlab function 1-6 eigenvector 5-38
bairstow’s method 2-17 cramer’s rule 4-16, 8-4 elements of a matrix - see matrix
bessel functions cubic interpolarion - see interpolation eps matlab function 1-21
defined 15-1 cubic spline interpolarion - see interpolation erlang distribution 13-16
modified 15-7 curve fitting 8-1 error function 10-4
of order n 15-1 curved regression - see regression euler’s identities 3-13
of negative order n 15-2 cycle 3-2 even functions 6-7
of the first kind 15-6 cyclic frequency - see frequency even symmetry - see fourier series
of the second kind 15-6 exit matlab command 1-2
besselj(n,x) matlab function 15-3 d exp(gammaln(n)) excel function 13-5
besselj(x,n) excel function 15-3 expand(s) matlab function 7-12, 12-9
beta distribution 13-20 decibel 1-11 exponential form of phasors - see phasor
beta function 13-17 decimal format in matlab 2-21 exponential form of the fourier series
beta(m,n) matlab function 13-19 deconv(p,q) matlab function 1-6, 7-8 see fourier series
betadist excel function 13-20 default in matlab exponentiation in matlab
bisection method color 1-14 element-by-element 1-20
for root approximation 2-19 line 1-14 eye(n) matlab command 4-6, 14-16
box matlab command 1-11 marker 1-14 eye(size(a)) matlab command 4-7
demo in matlab 1-2
c determinant - see matrix f
diag(v,k) matlab function 14-16
casorati’s determinant 11-2 diagonal elements - see matrix factor(p) matlab function 12-4
cayley-hamilton theorem 5-30 diagonal of a matrix - see matrix factorial polynomials 7-6
characteristic equation diff(s) matlab function 2-6 fibonacci numbers 11-7
of a differential equation 5-8 difference equation 11-1 figure window in matlab 1-13
of a difference equation 11-3 difference operator 7-4, 11-1 finite differences 7-1
chart type in excel 2-10 differences 7-3 fixed point format in matlab 2-21
fmin(f,x1,x2) matlab function 1-26 h of the second kind 15-11
forced response 5-7 associated 15-18
format matlab command 1-29 half-wave rectification 6-19 legendre polynomials
format specifiers in matlab 2-21 half-wave symmetry 6-7 algebraic form of 15-10, 15-16
fourier series harmonics 6-1 trigonometric form of 15-17
defined 6-1 help matlab command 1-2 legendre(n,x) matlab function 15-18
exponential form 6-28 hermitian matrix - see matrix lims = matlab function 1-26
numerical evaluation 6-36 hertz 3-3 line graph 16-4
symmetry line spectrum 6-33
even 6-7 i linear factors 1-8
in exponential series 6-30 linear interpolarion - see interpolation
in trigonometric series 6-7 identity matrix - see matrix linear programming 16-1
odd 6-7 imag(z) matlab function 1-22 constraints 16-1
trigonometric form 6-7 imaginary axis 3-10 objective 16-1
alternate 6-25 imaginary number 3-10 simplex method 16-4
fplot(fcn,lims) matlab command 1-26 improper integral 13-1 linear regression - see regression
fprintf matlab command 7-27 improper rational function 12-1 linspace(f,l,n) matlab command 1-13
frequency in-phase - see sinusoids ln (natural log) 1-11
cyclic 3-3 input(‘string’) matlab command 2-2 log 1-11
defined 3-3 int(f,a,b) matlab function 10-5 log(x) matlab function 1-11
fundamental 6-1 interp1(x,y,xi,’method’) log10(x) matlab function 1-11
radian 3-2 matlab function 7-24 log2(x) matlab function 1-11
response 1-10 interp2(x,y,z,xi,yi,’method’) loglog(x,y) matlab function 1-11
see also harmonics matlab function 7-24 lower triangular matrix - see matrix
frequency modulation (fm) 15-4 interpolation lsqnonneg(a,b) matlab function 14-19
frobenius - see method of cubic 7-24 lu factorization method 14-9
full-wave rectifier with even symmetry 6-24 cubic spline 7-24 lu(a) matlab frunction 14-12
function files in matlab 1-25, 2-4 linear 7-24
fundamental frequency - see frequency nearest neighbor 7-24 m
fundamental theorem of sum calculus 7-12 newton’s divided difference 7-15
fzero(‘function’,x0) in matlab 12-18 with matlab 7-23 maclaurin series 6-41, 7-6
fzero(f,x) in matlab 1-25 see also gregory-newton matlab demos 1-2
interval halving 2-19 matrix
g inverse of a matrix - see matrix adjoint of 4-19
isoprofit line 16-2 cofactor 4-12
gamma distribution 13-15 conformable for addition 4-2
gamma function 13-1 j conformable for multiplication 4-4
gamma(n) matlab function 13-3 conjugate of 4-8
gammadist excel function 13-16 j operator 3-11 defined 4-1
gammaln excel function 13-5 determinant 4-9
gammaln(x) 13-5 l diagonal elements of 4-1
gaussian elimination method 4-18, 14-9 diagonal of 4-1, 4-6
genenbauer 15-22 l’ hôpital’s rule 13-2 elements of 4-1
generalized factorial function 13-1 lagrange’s interpolation method 7-18 hermitian 4-9
generating function lagging waveform 3-3 identity 4-6
for bessel functions of the first kind laguerre polynomials 15-20 inverse of 4-21
of integer order 15-9 leading waveform 3-3 left division in matlab 4-24
for legendre polynomials 15-12 least squares lower triangular 4-6
goal seek excel feature 2-16 curve 8-2 main diagonal of 4-1
gram-schmidt orthogonalization line 8-2 main diagonal elements of 4-1
procedure 14-7 method 8-2 minor of determinant 4-12
gregory-newton parabola 8-2 multiplication 1-18
backward interpolation method 7-20 legendre equation 15-10 non-singular 4-20
forward interpolation method 7-19 associated 15-18 scalar 4-6
see also interpolation legendre functions singular 4-20
grid matlab function 1-11 of the first kind 15-10 size of 4-7
gtext(‘string’) matlab function 1-13, 32 associated 15-18 skew-hermitian 4-9
skew-symmetric 4-8 ode45 matlab function 9-9 r
square 4-1 optimum path policy
state transition 5-28 see dynamic programming radian frequency - see frequency
symmetric 4-8 ordinary differential equation 5-3 rational polynomials
trace of 4-2 orthogonal improper 12-1
transpose of 4-7 basis 14-5 proper 12-1
unitary 14-17 functions 6-2, 14-1 rationalization of the quotient 3-12
upper triangular 4-5 system 15-8 real axis 3-10
zero 4-2 trajectories 14-2 real number 3-11
mesh(x,y,z) matlab function 1-16 unit vectors 14-5 real(z) matlab function 1-22
mesh(z) matlab function 7-30 vectors 5-38, 14-4 rectangular form of phasor - see phasor
meshgrid(x,y) matlab function 1-16 orthonormal basis 14-5 regression
method of out-of-phase - see sinusoids linear 8-2
frobenius 15-1 overdetermined system 8-3 curved 8-7
least squares 8-2 relative cell in excel 2-19
picard 15-1 p residue(r,p,k) matlab function 12-1
undetermined coefficients 5-9, 11-3 rodrigues’ formula 15-11, 15-17
variation of parameters 5-19 parabolic curve 8-1 roots(p) matlab function 1-3
m-file in matlab 1-1, 1-25, 2-6 partial differential equation 5-3 rotating vector - see phasor
milne’s method 9-16 partial fraction expansion 12-1 round(n) matlab function 1-23
minimum span problems 16-15 pde 5-3 runge-kutta - see differential equation
minor of determinant - see matrix pearson correlation coefficient 8-10
minverse excel function 4-26 period - see periodic waveform s
mmult excel function 4-26 periodic waveform 3-2
modified bessel functions period of 3-2 sawtooth waveform 6-10
see bessel functions phasor scalar matrix - see matrix
modulation index 15-4 defined 3-2 script files in matlab 1-25
multiple poles - see poles exponential form of 3-14 semicolons in matlab 1-6
multiplication in matlab polar form of 3-14 semilogx(x,y) matlab command 1-11
element-by-element 1-20 rectangular form 3-2, 3-14 semilogy(x,y) matlab command 1-11
matrix 1-19 picard - see method of simplex method - see linear programming
multiplication of phasors 3-12 plot(x,y) matlab function 1-9 simpson’s rule 10-6
plot3(x,y,z) matlab function 1-15 singular matrix - see matrix
n polar plot 1-23 singular value decomposition 14-20
polar(theta,r) matlab function 1-23 sinusoids 3-2
nan in matlab 1-25 poles in-phase 3-3
natural response 5-7 complex 12-5 out-of-phase 3-3
nearest neighbor interpolarion defined 12-2 size of a matrix - see matrix
see interpolation distinct 12-2 skew-hermitian matrix - see matrix
network analysis 16-14 repeated (multiple) 12-6 skew-symmetric matrix - see matrix
neumann functions 15-7 poly(r) matlab function 1-4 space equations - see state equations
newton’s divided difference interpolation polyder(p) matlab function 1-6 spectrum analyzer 6-33
see interpolation polyfit(x,y,n) matlab function 8-10 spherical harmonics 15-18
non-homogeneous polyval(p,x) matlab function 1-5, 8-11 sprintf(format,a) matlab command 2-5
difference equation 11-2 power series 6-37 square matrix - see matrix
ordinary differential equation 5-6 proper rational function 12-1 square waveform 6-12
non-singular matrix -see matrix state equations 5-24
numerical evaluation of fourier coefficients q state transition matrix - see matrix
see fourier series state variables 5-24
qr factorization 14-17 stirling’s asymptotic series 13-9
o qr(a) matlab function 14-17 subplot(m,n,p) matlab command 1-18
quad(‘f’,a,b,tol) matlab function 10-10 surface zonal harmonics 15-11
objective - see linear programming quad8 matlab function 10-10 svd(a) matlab function 14-20
odd functions 6-8 quadratic curve 8-1 sym matlab command 12-4
odd symmetry - see fourier series quadratic factor 1-8 symbolic expressions in matlab 12-4
ode 5-3 quit matlab command 1-2 symbolic math toolbox in matlab 12-4
ode23 matlab function 9-9 symmetric matrix - see matrix
symmetry - see fourier series
syms matlab function 12-4
t
taylor series 5-24, 6-40, 9-1
text(x,y,’string’) matlab command 1-13
text(x,y,z,’string’) matlab command 1-16
title(‘string’) matlab command 1-11
trace of a matrix - see matrix
transpose of a matrix - see matrix
trapezoidal rule 10-1
trendline feature in excel 8-9
triangular waveform 6-19
trigonometric relations 3-5
two-dimensional plots 7-30
u
ultraspherical functions 15-22
undetermined coefficients - see method of
underdetermined system 8-3
unitary matrix - see matrix
upper triangular matrix - see matrix
v
variation of parameters - see method of
vlookup excel function 7-22
w
wallis’s formulas 13-15
wave equation 5-3
weber functions 15-7
while end in matlab 2-4
wronskian determinant 5-10, 11-2
x
xlabel(‘string’) matlab command 1-11
y
ylabel(‘string’) matlab command 1-11
z
zero matrix - see matrix
zeros of a rational function 12-2
zlabel(‘string’) matlab command 1-16
$39.95 u.s.a.
isbn 0-9744239-1-2
orchard publications
visit us on the internet
www.orchardpublications.com
or email us: info@orchardpublications.com
steven t. karris is the president and founder of orchard publications. he earned a bachelors
degree in electrical engineering at christian brothers university, memphis, tennessee, a masters
degree in electrical engineering at florida institute of technology, melbourne, florida, and has
done post-master work at the latter. he is a registered professional engineer in california and
florida. he has over 30 years of professional engineering experience in industry. in addition, he
has over 25 years of teaching experience that he acquired at several educational institutions as
an adjunct professor. he is currently with uc berkeley extension.
this text includes the following chapters:
• introduction to matlab • root approximations • sinusoids and complex numbers • matrices
and determinants • review of differential equations • fourier, taylor, and maclaurin series
• finite differences and interpolation • linear and parabolic regression • solution of differential
equations by numerical methods • integration by numerical methods • difference equations
• partial fraction expansion • the gamma and beta functions • orthogonal functions and matrix
factorizations • bessel, legendre, and chebyshev polynomials • optimization methods
each chapter contains numerous practical applications supplemented with detailed instructions
for using matlab and/or microsoft excel® to obtain quick solutions.
students and working professionals will
find numerical analysis using matlab®
and spreadsheets, second edition, to be a
concise and easy-to-learn text. it provides
complete, clear, and detailed explanations
of the principal numerical analysis methods
and well known functions used in science
and engineering. these are illustrated
with many real-world examples.
numerical analysis
using matlab® and spreadsheets
second edition

المادة المعروضة اعلاه هي مدخل الى المحاضرة المرفوعة بواسطة استاذ(ة) المادة . وقد تبدو لك غير متكاملة . حيث يضع استاذ المادة في بعض الاحيان فقط الجزء الاول من المحاضرة من اجل الاطلاع على ما ستقوم بتحميله لاحقا . في نظام التعليم الالكتروني نوفر هذه الخدمة لكي نبقيك على اطلاع حول محتوى الملف الذي ستقوم بتحميله .
الرجوع الى لوحة التحكم