Sei sulla pagina 1di 1195

Introduction to SCILAB

By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
INTRODUCTION TO SCILAB 3
Getting started with SCILAB 4
Other menus in the SCILAB worksheet 5
Simple operations with SCILAB 6
Simple SCILAB Input and Output 8
SCILAB command history 9
Selective worksheet output 9
The PFE software 12
Current directory / creating a work directory 12
A preview of SCILAB functions 13
Exercises 13
REFERENCES (FOR ALL SCILAB DOCUMENTS AT INFOCLEARINGHOUSE.COM) 15
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
I In nt tr ro od du uc ct ti io on n t to o S SC CI IL LA AB B
This chapter is intended to get the user started using SCILAB through simple exercises in numerical
calculations. The chapter starts by describing how to download and install SCILAB in a Windows
environment. Installation of the software in other operating systems is very similar and is explained in
detail in the SCILAB website.
What is SCILAB?
SCILAB is a numerical, programming and graphics environment available for free from the French
Government's "Institut Nationale de Recherche en Informatique et en Automatique - INRIA (National
Institute for Informatics and Automation Research)." It is similar in operation to MATLAB and other
existing numerical/graphic environments, and it can be run using a variety of operating systems including
UNIX, Windows, Linux, etc.
SCILAB is a self-contained package including a large number of intrinsic numeric, programming and
graphics functions. Once unpacked and installed in your computer it will consume about 50 MB of your
hard disk. Make sure you have at least that much memory in your hard disk before downloading and
installing SCILAB.
Where to find SCILAB
SCILAB is available for free from the SCILAB web page: http://www-rocq.inria.fr/SCILAB/
Once, you have accessed this web page, follow the procedure described below to download and install
SCILAB in your computer.
How to download the software
Click on the link labeled Download Scilab 2.6 . This will take you to an information page. Next, click on
the link labeled ftp.inria.fr in directory. This will send you to the ftp download page
ftp://ftp.inria.fr/INRIA/Projects/Meta2/Scilab/distributions/. From that directory you need to click on the
link Scilab26.exe,or on the link Scilab26.zip if you prefer a zip file containing the installation program.
Save the program it in the directory of your choice.
How to install SCILAB
Unpack SCILAB26.zip, if you downloaded that version of the installation program. Run the program
SCILAB26.exe and follow the instructions in your screen. In a Windows system, SCILAB 2.6 is typically
installed in the sub-directory c:\Program Files\Scilab-2.6
How to get documentation for SCILAB
To get documentation on SCILAB scroll down the SCILAB main web page and click on the documentation
link. This will take you to their documentation page, showing the following options:
Introduction to Scilab
Communication Toolbox Documentation
Signal Processing
Lmitool: Linear Matrix Inequalities Optimization Toolbox Documentation
Metanet User's Guide and Tutorial
Scicos
Scilab's Internals Documentation
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
HOWTO's Scilab
Scilab's demonstrations
The documentation is available in html format, which you can access by clicking in any of the options
shown above. If you want to have your own copy of the documentation in PostScript or PDF format, press
the appropriate option in the web page.
Things that SCILAB lets you do
SCILAB can be used for simple arithmetic operations as well as for some algebraic operations, to generate
graphics, to program functions, and to solve linear algebra problems and ordinary differential equations,
among other things. You can also program SCILAB and produce simple or fancy graphic user interface
components for your program.
Getting started with SCILAB
To get started, launch the SCILAB application.
To load a value into a variable use an assignment statement (one that includes the equal sign),
e.g., a = 3.2. Try the following exercises for simple arithmetic operations:
a = 3.2 <return>
b = 6.4 <return>
a+b <return>
a-b <return>
a*b <return>
a/b <return>
a^b <return>
who <return>
The last command will return a list of the active variables in your worksheet. Many of them are
system variables that cannot be modified by the user.
SCILAB has a number of special constants, such as %i, %pi, and %e, corresponding to the unit
imaginary number, = ratio of circumference to diameter, and e = the base of the natural logarithms,
respectively. The value %eps is another special constant corresponding to the maximum value for
which 1 + %eps = 1. Other important constants are %inf = infinity, and %nan = not-a-number.
Boolean (i.e., logical ) constants are %t = true and %f = false.
Try the following exercises to see what values are returned by SCILAB:
%e <return>
%i <return>
%pi <return>
%eps <return>
%inf <return>
%nan <return>
%t <return>
%f <return>
Comments in SCILAB are represented by the double forward slash (//). Anything in front of // is
taken as a comment. For example, enter:
a = 4.5 // redefining a <return>
SCILAB will return the value of 4.5 for a and ignore the comment after the //
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
Scalars: real, logical, string, polynomial, rational, and basically any object that is not between
brackets in SCILAB is referred to as a scalar. Examples are:
a = 1 // real constant <return>
2>1 // Boolean constant (i.e., logical) <return>
'my name' // character string or constant <return>
r = poly(1.,'x') // polynomial with variable 'x' and root at 1.0 <return>
q = y/r // rational expression <return>
Using the "Introduction to SCILAB" demo: SCILAB is provided with a number of demos to show
the software abilities. To access the demos click on the Demos menu. This will provide a dialogue
window (SCILAB Choose) with a list of subjects, such as Introduction to SCILAB, Graphics, etc.
Select the Introduction to SCILAB, and press OK. A file with SCILAB commands is loaded in
memory. To see each line press <return>. The lines are shown and executed (if executable).
Comments have been added to each line to explain the operation shown. (The entire demo session is
listed in pages 118 to 137 of the documentation file Introduction to SCILAB.) Keep pressing the
<return> key to observe a good summary of SCILAB abilities. In particular, pay special attention to
item 2 (MATRICES) since SCILAB is a matrix-based system. Also of interest are the items labeled
OPERATIONS and SOME NUMERICAL PRIMITIVES. (The term primitives is used to refer to
simple numerical operations pre-programmed in SCILAB or any other numerical environment).
Using other demos: To see any of the other demos provided with SCILAB select the option
Demos from the File menu, then select the Demo subject you want (e.g. , Graphics: Introduction), and
select a particular demo (e.g., plot2d3 in Graphics: Introduction). Notice that, when using graphics in
SCILAB, the program generates a window called SCILABGraphic0.
Getting help: In the Help menu there are three options that you can use to get help from SCILAB:
Help Dialogue: provides a list of help items classified by chapters. There will be two windows
available using this option, the lower window shows the chapter titles while the upper window
shows the subjects in the chapter currently selected. Select the item you want to display and press
the Show button located to the left side of the help window.
Topic: enter a topic you want help with in the window shown when using this option and press
the OK button. If no help is available you will get a message indicating so in the main SCILAB
window. If help is available, SCILAB will show you the corresponding instructions. For
example, use this option and request information on the keyword inv. It will give you information
on the inverse operation for matrices.
Apropos: enter a keyword related to the topic that you are looking for (e.g., inverse) and press the
OK button. You will see a window with topics including the keyword you selected. Scroll
through the list and find that topic closer to your interest (e.g., inv - matrix inverse), and press OK
to get information on that particular topic.
Other menus in the SCILAB worksheet
The "File" menu: The following options are available in the File menu:
Getf: use it to load a function. (Type help getf for additional information).
Exec: to execute a script file. (Type help exec for additional information).
Save: to save variables (Type help save for additional information).
Load: to load a saved variable (Type help load for additional information).
Change Directory: self-explanatory.
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
Get Current Directory: self-explanatory.
The "Functions" menu:
Copy to Clipboard: lets you copy highligted text to the Windows clipboard
Paste: lets you paste text in the clipboard to current SCILAB prompt location
Choose Font : lets you choose the font in the main SCILAB window
History: access to a menu of commands for editing SCILAB command lines. The character ^ in
this menu stands for the Ctrl key.
The "Control" menu
Restart: clears all user-defined variables and initializes environmental variables.
Pause: switch control to pause mode. Used mainly for debugging SCILAB functions.
Resume: resume execution after a pause.
Abort: end execution of current program.
Interupt : interrupt execution of current program.
Pause mode: when entering Pause mode, SCILAB creates a new (numbered) prompt. In this mode you
can enter SCILAB commands without affecting the main line of calculation (represented by the main
SCILAB prompt !). To get into the pause mode you can type pause at the main prompt or click on
the Pause option in the upper menu bar of the main SCILAB window. (The option Interupt [sic] in the
upper menu bar also allows you to enter into pause mode, however, it will interrupt any operation
currently being executed by SCILAB).. You can have several levels of pause, one for every time you
enter a pause command. To leave the current pause level and move the upper one type return. To
move up two levels use the command quit. (Note, when used in the main SCILAB prompt, quit will
terminate your SCILAB session). For more information on the pause mode use the help feature as
described above, or simple type help pause.
Simple operations with SCILAB
Simple scalar operations: the following exercises will get you acquainted with a few of SCILAB's
abilities for operating with scalar values.
a = 2 <return>
b = 3 <return>
Save a <return>
clear a <return>
a <return>
b <return>
load a <return>
a <return>
exp(a) + exp(b) <return>
sin(a*%pi/b) <return>
(Note: the clear command is used to eliminate variables, as in clear a, as shown above. By itself, clear
deletes all variables recently defined. Therefore, be very careful when using this command).
Vectors:
To enter vectors use the square brackets and separate the elements with commas or blanks for a
row vector, e.g.: v = [-1. , 2., %pi].
The transpose of a vector (or matrix) is expressed using the apostrophe, for example, type: v'
To enter a column vector, use any of the following procedures: w = [3; -2; 5] <return> or
r = [6 <return>
-2 <return>
10 ] <return>
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
You can create a row vector by indicating a starting value, an increment (or decrement), and an
ending value, all separated by the colon (:) operator as follows:
vector_name = starting_value : increment : ending value
for example: x = -10.0 : 0.5 : 10.0 <return>
If you prefer to store it as a column vector, try the following: xt = x' <return>
Let's apply a function to the vector x, try: y = sin(x*%pi/10) <return>
We can plot the vectors x,y using: plot(x,y,'x','y','first plot') <return>
[Type help plot <return> for more information]
Let's restart SCILAB and operate with other vectors: Press the Restart option in the upper bar
menu.
Enter the row vectors, u = [-1. 2. 3.] and v = [6. -10. 0.]
Perform the following operations:
u + v <return>
u - v <return>
u*v <return>
u*v' <return>
u'*v <return>
To extract elements of the vectors, try:
u(3) <return>
u(2) + v(1) <return>
Try the following exercise: a = 1; b = 2; c = 3; r = [a, b, c] <return>
To suppress SCILAB responses use the semi-colon after entering a command. For example, try: s
= [-1., 2.]; <return>
Vectors can also contain characters as their elements, e.g., letters = ['a', 'b', 'c',
'd'] <return>
Note: Expressions such as a, b, etc., are referred to as strings. Therefore, only those string
operations such as concatenation, part, etc. are allowed for vectors with character elements.
SCILAB strings and string operations are presented in a subsequent chapter.
Matrices:
Here are several ways to enter matrices: (press Restart)
A = [1. 2. 3.; 4. 5. 6.; 1. -1. 0.] <return>
B = [ 1. 1. 1. <return>
2. 3. -1. <return>
5. 7. -2. ] <return>
u = [1. 3. -5. ]; v = [4. 2. 3. ]; w = [-1. 0. 1.]; <return>
C = [u; v; w] <return>
r = [u, v, w] <return>
D = [u' v' w'] <return>
Matrix operations: try the following operations:
A + B <return>
C - D <return>
A*B <return>
B*A <return>
C*u <return>
D*v' <return>
rank (A) <return>
inv(A) <return>
cond(B) <return>
det(C) <return>
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
A*inv(A) <return>
inv(B)*B <return>
spec(A) <return> (calculates eigenvalues)
trace(C ) <return>
(Note: to find out more about any of the commands listed here type help followed by the
command name, e.g., help spec).
Solution of linear systems: two possibilities are:
(Press Restart)
A = [1. 3. 2.; 2. 1. -1.; 5. 2. 1.]; b = [2; 3; 4]; <return>
xa = inv(A)*b <return>
xb = linsolve(A,b)<return>
(Note: type help linsolve to learn more about this command).
Simple SCILAB Input and Output
Output: To get a list of your current session use the function diary. The format is as follows: diary
(output_filename), where the filename is written within quotes. To end collecting output in the current
diary output file use diary(0). For example, try the following session:
(Press Restart)
diary ('session1.txt') <return>
A = [1. 2. 3.; 2. 3. 1.; 3. 2. 1.];b=[5; 4; -1.];<return>
A <return>
b <return>
x = linsolve(A,b) <return>
diary(0) <return>
Next, use NOTEPAD, or other text editor, to open the file session1.txt to see the contents of the file.
The default directory for storing a diary file is the bin subdirectory within the SCILAB directory.
Note: The SCILAB worksheet does not allow cutting and pasting. Therefore, the use of the diary
function with a filename is the only way to copy SCILAB output into a text file.
Command Input: you can read SCILAB commands from a script file, which is basically a text file
listing all the commands you want to use. As an example, create the following text file in the bin
subdirectory of the SCILAB directory using NOTEPAD, and call it session2.txt:
//--------------------------------------------------------------------------
clear
A = [1. 2. -3. // entering
3. 4. 5. // elements of
7. 8. 9.] // matrix A
b = [1.; 2.; 3.] // enter vector b
xa = inv(A)*b // calculate x using matrix inverse
xb = linsolve(A,b) // calculate x using SCILAB's own linsolve function
//--------------------------------------------------------------------------
Then, press Restart in SCILAB, and type: exec('session2.txt')
You will see SCILAB execute all the commands in your file, stopping at the end of the file.
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
SCILAB command history
All commands entered in a given SCILAB session get stored into a SCILAB command history buffer.
The commands are thus accessible for re-use or edition. All the command history functions are
available through the option History under the File menu in the SCILAB worksheet. The most useful
commands are cntl-P and cntl-N, which lets you access the previous command or the next command,
respectively, in the command history buffer. Once a command is recalled from the command history
buffer it can be edited by using the backspace or delete keys, or by inserting new characters by simply
typing at the proper location.
For example, try the following SCILAB session:
1 - Press the Restart option in the menu bar to clear the SCILAB environment.
2 - Enter the following commands (you dont need to enter the comments):
-->x = [0:%pi/20:2*%pi];
-->y = sin(x) + sin(2*x);
3 - Use cntl-P and edit the previous command (y = sin(x) + sin(2*x);) to read:
-->z = sin(x) + cos(x);
4 - Use cntl-P once more to edit the previous command (z = sin(x) + cos(x); ) to read:
-->p = cos(x) + cos(2*x);
5 - So far you have created vectors x, y, z, and p. Next, we use the functions xset with the option
window, and the function plot to produce a plot of y-vs.-x:
-->xset('window',0); plot(x,y)
6 - Use cntl-P to edit the previous command to read:
-->xset('window',1); plot(x,z)
7 - Continue using cntl-P to edit the last commands and produce the following plots:
-->xset('window',2); plot(x,p)
-->xset('window',3); plot(y,z)
-->xset('window',4); plot(y,p)
-->xset('window',5); plot(z,p)
Selective worksheet output
Suppose you have been working on a lengthy SCILAB session whose command history buffer
contains some results that produced errors as well as some intermediary results that are not of interest
to you for output. For your output, you can select only those commands relevant to your final results
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
by using Cntl-P and Cntl-N. Try the following SCILAB session that explores some basic vector
operations using vectors x and y:
1 - Press the Restart option in the menu bar.
2 - Enter the following SCILAB commands:
-->x = [1, 2, 5, -4]
x =
! 1. 2. 5. - 4. !
-->y = [0, 2, 3,-5]
y =
! 0. 2. 3. - 5. !
-->x*y
!--error 10
inconsistent multiplication
-->x.*y
ans =
! 0. 4. 15. 20. !
-->sum(ans)
ans =
39.
-->sum(x.*y)
ans =
39.
-->x*y'
ans =
39.
-->x'*y
ans =
! 0. 2. 3. - 5. !
! 0. 4. 6. - 10. !
! 0. 10. 15. - 25. !
! 0. - 8. - 12. 20. !
-->y'*x
ans =
! 0. 0. 0. 0. !
! 2. 4. 10. - 8. !
! 3. 6. 15. - 12. !
! - 5. - 10. - 25. 20. !
-->x*y' + y'*x
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
ans =
! 39. 39. 39. 39. !
! 41. 43. 49. 31. !
! 42. 45. 54. 27. !
! 34. 29. 14. 59. !
-->b = x*y' + y'*x
b =
! 39. 39. 39. 39. !
! 41. 43. 49. 31. !
! 42. 45. 54. 27. !
! 34. 29. 14. 59. !
Note: These commands and their corresponding results represent an exploratory session for vector
operations.
3 - Suppose that you are only interested in the commands defining vectors x and y, in the operations
that produce the dot product of the vectors (i.e., sum(x.*y) and x*y'), and in the very last command
(b = x*y' + y'*x). Using the diary command create the file c:\myVectors.txt and collect only the
commands of interest out of the command history buffer by using cntl-P and cntl-N as needed. The
resulting SCILAB session should look like this:
--> diary(c:\myVectors)
-->x = [1, 2, 5, -4]
x =
! 1. 2. 5. - 4. !
-->y = [0, 2, 3,-5]
y =
! 0. 2. 3. - 5. !
-->sum(x.*y)
ans =
39.
-->x*y'
ans =
39.
-->b = x*y' + y'*x
b =
! 39. 39. 39. 39. !
! 41. 43. 49. 31. !
! 42. 45. 54. 27. !
! 34. 29. 14. 59. !
-->diary(0)
The session, except for the very first command () is stored in file c:\myVectors.txt. This file can be
edited or printed from a text editor such as NOTEPAD, or my favorite, PFE (Programmers File
Editor) available for free from:
http://www.lancs.ac.uk/people/cpaap/pfe
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
The PFE software
PFE is a text editor designed for text-based programming in languages such as FORTRAN, JAVA,
C++, etc. PFE allows the user direct access to DOS windows for compilation and testing of programs,
runs DOS commands from the PFE interface, allows recording and playing of Macro commands, and
permits easy navigation of files by allowing line numbering for easy debugging of programs.
PFE can be used in combination with SCILAB to type scripts and function files or to edit diary files
produced from within SCILAB.
Current directory / creating a work directory
SCILAB uses a current directory where files are saved by default, for example when using the function
diary. To see the current directory use:
-->pwd
Under a Windows operating system, the default current directory is typically c:\
The command pwd stands for print working directory.
I recommend that you create a sub-directory, or folder, called work and locate it under the SCILAB
main directory. For example, under a Windows operating system, the SCILAB main directory will
typically be
c:\Program Files\SCILAB2.5
Thus, your work directory would correspond to:
c:\Program Files\SCILAB2.5\work
At the beginning of a SCILAB session, you can change the current directory to the work directory by
using the function chdir:
--> chdir(c:\Program Files\SCILAB2.5\work)
You can use your work directory to store scripts and functions that you create. For example, if you
create a script called script1 in the work directory, once you change the current directory to the work
directory, you can simply use:
-->exec(script1)
to run your script.
If you type a function called function1 in the work directory, with the work directory selected as the
current directory, you can load the function by simply using:
-->getf(function1)
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
Scripts and function files can be created using NOTEPAD, PDE, or any other text editor.
A preview of SCILAB functions
Here are some useful functions in SCILAB that we will explore in more details in subsequent
chapters:
Elementary functions: sum, prod, sqrt, diag, cos, max, round, sign, fft
Sorting: sort, sortup, gsort, find
Specific matrices: zeros, eye, ones, matrix, empty
Linear algebra: det, inv, qr, svd, bdiag, spec, schur, trace
Polynomials: poly, roots, coeff, horner, clean, freq
Random numbers: rand
Programming: function, deff, argn, for, if, end, while, select, warning,
error, break, return
Comparison symbols: ==, >=, >, <=, <, =, & (and), | (or)
Execution of a file: exec
Debugging: pause, return, abort
Spline functions, interpolation: splin, interp, interpln
Character strings: string, part, evstr, execstr
Graphics: plot, xset, driver, plot2d, xgrid, locate, plot3d, Graphics
Ordinary differential equation solvers: ode, dassl, dassrt, odedc
To find out more about these functions use the help command. For example, try:
-->help roots
-->help eye
-->help trace
Exercises
Determine the result of the following calculations using SCILAB if a = 2.3, b = -2.3, c= /2, x = 2/, and y
= 3:
[1]. (a
2
+ bc + x)
[2]. sin(c) + y/c
[3]. (a+c)/(x+y)
[4]. 1/(cos(c) + ln(x))
[5]. (a+c)
3
/b
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
Check if the following Boolean statements are true or false based on the values of a, b, c, x, and y given
above.
[6]. a > c
[7]. a = b
[8]. (2a+b)/x
2
< 1
[9]. x + 2ab + b
2
23
[10]. 2ac = 2cb
[11]. Use SCILABs help facility to find out information about function deff and use it to define a function
y = f(x) = x
2
+ 1.
Using the vectors u = [3, 2, -1], and v = [4, -6, 2], calculate the following operations:
[12]. w = u+v
[13]. r = u./v
[14]. z = v*u
[15]. t = v.*u
Using the matrices A, B, and C, shown below, perform the following operations:

=
2 4
3 1
5 2
,
2 4 0
10 1 1
4 5
,
2 1 4
0 2 1
2 5 5
C B A

[16]. A + B
[17]. AB
[18]. BA
[19]. BC
[20]. A BC
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at INFOCLEARINGHOUSE.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical
Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John Wiley &
Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company, Reading,
Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's outlines, McGraw-
Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition," Brooks/Cole
- Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace Jovanovich,
Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International Geological
Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley Publishing Company,
Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press, Cambridge CB2
2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications, Inc., New
York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers," The Mc-Graw
Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to MATLAB", Prentice
Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman Scientific and
Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific Computing
(FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper Saddle River, N.J.,
U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace Jovanovich,
Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data," Cambridge
University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for Engineers and
Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline Series, Harcourt
Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD., Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in Applied
Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com, Charleston,
S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate Engineering
Students," John Wiley & Sons, Inc., New York.
Programming with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
SCILAB PROGRAMMING, IO, AND STRINGS 2
SCILAB programming constructs 2
Comparison and Logical Operators 2
Loops in SCILAB 3
Conditional constructs in SCILAB 3
Functions in SCILAB 5
Global and local variables 6
Special function commands 6
Debugging 7
An example of a function - Calculation of Frobenius norm of a matrix. 8
Input/Output in SCILAB 9
Saving and loading variables. 9
Unformatted output to the screen 9
Unformatted output to a file 9
Working with files. 10
Writing to files. 10
Reading from the keyboard 11
Reading from files 12
Manipulating strings in SCILAB 12
String concatenation 13
String functions 13
Converting numerical values to strings 14
String catenation for a vector of strings 15
Converting strings to numbers 15
Executing SCILAB statements represented by strings 16
Producing labeled output in SCILAB 17
Using the function disp 18
The variable ans 18
Exercises 19
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
S SC CI IL LA AB B P Pr ro og gr ra am mm mi in ng g, , I IO O, , a an nd d s st tr ri in ng gs s
Programming is the basic skill for implementing numerical methods. In this chapter we
describe the fundamental programming constructs used in SCILAB and present examples of
their applications to some elementary numerical methods. The second part of this chapter is
dedicated at exploring input/output functions provided by SCILAB including operations with
files. Finally, manipulation of strings in SCILAB is presented.
SCILAB programming constructs
SCILAB provides the user with a number of programming constructs very similar to those
available in FORTRAN and other high-level languages. We present some of the constructs
below:
Comparison and Logical Operators
SCILAB comparison operators are
== equal to
< less than
> greater than
<= less than or equal to
>= greater than or equal to
<> or ~= not equal to
SCILAB logical operators are
& and
| or
~ not
As an example, try the following commands in SCILAB:
3 <> 2 <enter>
3 == 3 <enter>
(2>1)&(3>1) <enter>
(2>1)&(3>5) <enter>
(2<1)&(3>1) <enter>
(2<1)&(3>5) <enter>
(2>1) | (3>1) <enter>
(2>1) | (3>5) <enter>
(2<1) | (3>1) <enter>
(2<1) | (3>5) <enter>
~(2<1) <enter>
~(2>1) <enter>
~(2>1) | (3>5) <enter>
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Loops in SCILAB
SCILAB includes For and While loops. The For loop is similar to the DO loop in FORTRAN or the
FOR..NEXT loop in Visual Basic. The basic construct for the For loop is:
for index = starting_value : increment : end_value, statements, end
for index = starting_value : end_value, statements, end
If no increment is included it is supposed to be equal to 1.
For example, enter the following For loops in SCILAB:
r = 1; for k = 1:0.5:4, r = r+k, end <enter>
xs = -1.0; dx = 0.25; n = 10; for j = 1:n, x = xs + (j-1)*dx, end <enter>
for m = 1:10, a(m) = m^2, end <enter>
a <enter>
The basic construct for the While loop is:
while condition, statements, end
For example, try the following while loop:
s = 100; while s>50, disp(s^2), s = s - 5, end <enter>
For and while loops can be terminated with the command break, for example, try the
following:
for j = 1:10, disp(j), if j>5 then break, end, end <enter>
Conditional constructs in SCILAB
In the example above we used an if thenend construct. There are two type of conditional
constructs in SCILAB, one is the if-then-else-end construct (as in the example above) and the
second one is the select-case conditional construct. Different forms of the if-then-else
construct are:
if condition then statement, end
if condition then statement, else statement, end
if condition then statement, elseif condition then statement, else statement, end
Try the following examples:
x = 10; y = 5; if x> 5 then disp(y), end <enter>
x = 3 ; y = 5; if x>5 then disp(y), else disp(x), end <enter>
x = 3; y = 5; z = 4; if x>5 then disp(x), elseif x>6 then disp(y), else disp(z), end <enter>
The general form of the select-case construct is:
select variable, case n1, statement, case n2, statement, , end
Try the following examples:
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
x = -1; select x, case 1, y = x+5, case -1, y = sqrt(x), end <enter>
r = 7; select r, case 1, disp( r), case 2, disp(r^2), case 7, disp(r^3), end <enter>
All these constructs can be programmed in files following a structure similar to FORTRAN or
Visual Basic programs, and then executed from within SCILAB. Such files are referred to as
scripts. For example, type the following SCILAB script into a file called program1.txt:
clear //erase all variables
x = [10. -1. 3. 5. -7. 4. 2.];
suma = 0;
[n,m] = size(x);
for j = 1:m
suma = suma + x(j);
end
xbar = suma/m;
xbar
Save it into the bin sub-directory. Within SCILAB type:
exec('program1.txt') <enter>
Note that since x is a row vector (actually a matrix with n = 1 row and m = 7 columns), the size
function provides you with an array of two values in the statement [n,m] = size(x). Then, m
is used in the for loop and in the calculation of xbar.
As an alternative to using a row (or column) vector is the use of lists. A list is a collection of
data objects not necessarily of the same type. In the following example we limit ourselves to
using lists of numbers. To define a list we use the list command, for example, try:
y = list(0., 1., 2., 3., 4., 6.) <enter>
size(y) <enter>
In this case, the size of the list, unlike that of a vector or matrix, is provided as a single
number. A modified version of the script in program1.txt is shown below. Type this file into
program2.txt and save it in the bin sub-directory under the SCILAB directory:
//Same as program1.txt, but using lists
clear //erase all variables
x = list(10., -1., 3., 5., -7., 4., 2.);
suma = 0;
n = size(x);
for j = 1:n
suma = suma + x(j);
end
xbar = suma/n;
n
xbar
To run the script, from within SCILAB type:
exec('program2.txt')<enter>
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
Functions in SCILAB
Functions are procedures that may take input arguments and return zero, one or more values.
Functions are defined either on line, using the deff command, or as a separate file that needs
to be loaded using the getf command. Following some examples of on-line functions are
presented:
deff('[z]=Euler(r,theta)','z=r*exp(%i*theta)') <enter>
Euler(1.0,-%pi/2) <enter>
deff('[r,theta]=cartpol(x,y)',['r=sqrt(x^2+y^2)'; 'theta=atan(y,x)']) <enter>
[radius,angle] = cartpol(3., 4.) <enter>
These functions could have been defined by using the Define User Function option in SCILAB's
Functions menu. For example, select this option and enter the following (your reply is shown
in italics):
(1) Name of output variable? x,y [OK];
(2) Name for function? polcart[OK];
(3) Variable/s? r,theta [OK];
(4) Code? ['x=r*cos(theta)','y=r*sin(theta)'][OK].
SCILAB's response is:
! deff('[x,y]=polcart(r,theta)',['x=r*cos(theta)';'y=r*sin(theta)]).
Try the following application:
[h,v] = polcart(10.0,%pi/6) <enter>
polcart(100.0,%pi/3) <enter>
The last command will give you only the result for y since the function call was not assigned to
an array as in the first case.
Functions defined in files must start with the command
Function [y1,,yn] = fname(x1,,xm)
Where fname is the function name, [y1,,yn] is an array of output values, and x1,,xm are the
input values. Type in the following function into a file called sphecart.txt using a text editor
(e.g., NOTEPAD, or PFE):
function [x,y,z] = sphecart(r,theta,rho)
//conversion from spherical to Cartesian coordinates
x = r*cos(rho)*cos(theta)
y = r*cos(rho)*sin(theta)
z = r*sin(rho)
In SCILAB load the function using:
getf('sphercart.txt') <enter>
[x1,y1,z1]=sphercart(10.0, %pi/3, %pi/6) <enter>
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
Notice that SCILAB on-line functions are similar to FORTRAN function declarations, while
SCILAB functions defined in files are similar to FORTRAN or Visual Basic function sub-programs
or subroutines. The main difference is that FORTRAN and Visual Basic functions can only return
one value, while SCILAB functions can return zero, one or more values.
Global and local variables
A global variable is one define in the main SCILAB environment, while a local variable is one
defined within a function. If a variable in a function is not defined, or is not among the input
parameters, then it takes the value of a variable of the same name that exist in the calling
environment. This variable remains local in the sense that modifying it within the function
does not alter its value in the calling environment unless the command resume is used.
For example, using the function sphercart, try the following:
clear
getf('sphercart.txt') <enter>
theta = %pi/3 <enter>
rho = %pi/6 <enter>
[x,y,z] = sphercart(10.0,theta)<enter>
Since rho is defined in the calling environment, even though that value is missing in the calling
sequence to the function sphercart, it takes the value of rho in the calling environment.
Note that it is not possible to call a function if one of the parameters in the calling sequence is
not defined. Try the following:
clear
getf('sphercart.txt') <enter>
theta = %pi/3 <enter>
[x,y,z]=sphercart(10.0,%pi/3,rho) <enter>
Because rho is not defined in this case, the function can not be evaluated.
Special function commands
These are SCILAB command used almost exclusively in functions:
argn: returns the number of input and output arguments of the function
error: suspends a function's operation, prints an error message, and returns to previous
environment level if an error is detected
warning: prints a warning message
pause: temporarily suspends the operation of a function
break: forces the end of a loop
return or resume: use to return to the calling environment and to pass local variables from
the function environment to the calling environment.
For additional information use the help feature in SCILAB with these functions. The following
example illustrate the use of some of these special function commands. Enter the function in a
file called func1.txt, and save it in the bin sub-directory of SCILAB:
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
function [z] = func1(x,y)
[out,in]=argn(0)
if x == 0 then
error('division by zero');
end,
slope = y/x;
pause,
z = sqrt(slope);
s = resume(slope);
Then, within SCILAB enter the following:
clear <enter>
getf('func1.txt') <enter>
z = func1(0,1) <enter>
z = func1(2,1) <enter>
In the second call to func1, the -1-> prompt indicates a pause mode. The function operation
is temporarily suspended. The user can, at this point, examine values calculated inside the
function, plot data, or perform any SCILAB operation. Control is returned to the function by
typing the command return <enter> (resume can also be used here). Operation of the function
can be stopped by using quit or abort. When return (or resume) is used, the function
calculates and reports the value of z. Also available in the environment is the local variable s
which is passed to the global environment by the resume command within the function. Type s
<enter> to see the value of s.
Debugging
The simplest way to debug a SCILAB function is to use a pause command in the function. When
this command is encountered the function stops and the prompt -1-> is shown. This indicates
a different "level" of calculation that can be used to recall variable values including global
variables from the calling environment, experiment with operations, produce a graph if
needed, etc. Using a second pause will produce a new level characterized by the prompt -2-
>, and so on. The function resumes execution by typing the command return or resume, at
which point the variables used at the higher level prompts are cleared. Execution of the
function can be interrupted with the command abort.
An additional feature for debugging that is available in SCILAB is the insertion of breakpoints in
the function. These are pre-identified points in the function to which you can access during
the function execution to check the values of the variables or perform other operations. Check
the commands setbpt, delbpt, and disbpt.
You can also trap errors during the function execution by using the commands errclear and
errcatch. Check these commands using SCILAB help. At a higher level of expertise in SCILAB
debugging the user can try the function debug(i) where i = 0, 1, 2, 3, 4, denotes a debugging
level. Check out the debug function using help.
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
An example of a function - Calculation of Frobenius norm of a
matrix.
This function is to be stored in file AbsM.txt within subdirectory bin in the SCILAB directory.
(Note: While the name of the file containing a function does not have to be the same as the
name of the function, it is recommended that they be the same to facilitate loading and
operation of the function).
The Frobenius norm of a matrix A = [a
ij
] with n rows and m columns is defined as the square
root of the sum of the squares of each of the elements of the matrix, i.e.,
. || ||
1 1

= =
=
n
i
m
j
ij F
a A
The function AbsM(A), listed below, calculates the Frobenius norm of a matrix:
function [v]=AbsM(A)
// This function calculates the Frobenius norm of a matrix
// First obtain the matrix size
[n m] = size(A);
// Then initialize suma and add terms a(i,j)^2
suma = 0;
for i = 1:n
for j = 1:m
suma = suma + A(i,j)^2;
end
end;
// take square root and show result
v = sqrt(suma);
// end of the function
Within SCILAB try the following commands to load and run the function for a particular case:
clear <enter>
getf('AbsM.txt') <enter>
R = [1. 3. 4. 2. <enter>
3. -2. 5. -7. <enter>
1. 3. 4. 5. ] <enter>
AbsM(R) <enter>
Functions are defined throughout the book in relation to different mathematical subjects, i.e.,
vectors, matrices, integrals, differential equations, etc. The following sections of this chapter
deal with the subjects of input/output and string manipulation in SCILAB.
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Input/Output in SCILAB
Saving and loading variables.
To save variables in a file use the command save. Let's try some examples:
A = [1. 2. 3.; -3. 4. 5.; 2. 4. 5.; 1. 3. 2.]; b = 1:10; <enter>
A <enter>
b <enter>
save('DataAb.dat', A,b)<enter>
Next, using NOTEPAD or PDE, open the file DataAB.dat in sub-directory bin of SCILAB.
You will notice that you cannot see the numbers in the file. That is because they have
been saved in a binary format. Let's clear the variables in SCILAB and re-load the
values of A and b using the command load:
clear <enter>
load('DataAb.dat') <enter>
A <enter>
b <enter>
Unformatted output to the screen
To print strings and variables without a format you can use the print function. The
general form of the function is: print (unit or filename, x1, x2, (y1, .., )). The unit
value for the screen is either 6 or %io(2). Try the following examples:
x = 5; y = sin(%pi*x/10); r = 1:2:25; A = rand(5,3); <enter>
%io(2) <enter>
print(6,x,y) <enter>
print (6,A,r)<enter>
print(%io(2),x,y,r)<enter>
print(%io(2),A) <enter>
Notice that the function print, as with the function disp used earlier, prints the last
variable in the list first. Try some more examples:
Print(6,x,'x value =') <enter>
Notice that, in this case, the string 'x value =' is printed together with the string 'x = ',
which is a default from the print command. Therefore, it is not a good idea to include
an identifying string when using the print function to print to the screen.
Unformatted output to a file
You can use the print function to print to a filename, for example, try:
print('data1.txt',A,r)<enter>
print ('data2.txt',x,y)<enter>
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
Next, using NOTEPAD open the files data1.txt and data2.txt. Notice that the output
includes all the identifiers and brackets (!) provided by SCILAB.
Working with files.
The following command allows you to open a file:
[unit [,err]]=file('open', file-name [,status] [,access [,recl]] [,format])
file-name: string, file name of the file to be opened
status: string, The status of the file to be opened
"new" : file must not exist new file (default)
"old" : file must already exists.
"unknown" : unknown status
"scratch" : file is to be deleted at end of session
access: string, The type of access to the file
"sequential" : sequential access (default)
"direct" : direct access.
format: string,
"formatted" : for a formatted file (default)
"unformatted" : binary record.
recl: integer, is the size of records in bytes when access="direct"
unit: integer, logical unit descriptor of the opened file
err: integer, error message number (see error), if open fails. If err is
omitted an error message is issued.
You can also use the command: file(action,unit)
where action is one of the following strings:
"close": closes the file.
"rewind": puts the pointer at beginning of file.
"backspace": puts the pointer at beginning of last record.
"last": puts the pointer after last record.
Once a file is open it can be used for input (read function) or output (write function).
Some examples of file opening, input and output are shown below.
Writing to files.
The following programs use the values of x, y, A, and r defined above. In these
examples we open and write to files, and close them. Notice that this command is
oriented towards printing matrices -- one at a time -- therefore, as shown in Example
2, it is better if you put together your data into a matrix before printing it. Notice also
that the format part, which is enclosed between quotes, is basically a FORTRAN
format.
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Example 1.
u = file('open','data3.txt','new')<enter>
write(u,A,'(3f10.6)') <enter>
file('close',u)<enter>
Example 2.
x1 = 0:0.5:10 <enter>
x2 = x1^2 <enter>
B = [x1',x2'] <enter>
m = file('open','data4.txt','new')<enter>
write(m,B,'(2(f10.6,2x))') <enter>
file('close',m)<enter>
Example 3. Including labels. Note: labels are written separated from the variables
A = rand(2,3); B = rand(2,3); C = A + B <enter>
u = file('open','data5.txt','new) <enter>
write(u,'this is matrix A','(a)') <enter>
write(u,A,'(3(f10.6,2x)') <enter>
write(u,'this is matrix B','(a)') <enter>
write(u,B,'(3(f10.6,2x)') <enter>
write(u,'this is matrix C = A + B','(a)') <enter>
write(u,C,'(3(f10.6,2x)') <enter>
file('close',u) <enter>
Reading from the keyboard
Reading from the keyboard can be accomplished by using the read function with unit
%io(1) or 5. The general form of the read function is:
[x]=read(file-description,n,m,[format]),
i.e., a variable must be assigned a value (could be a matrix of size n,m) during the
operation of read. The file description can be a unit or number assigned to a file or to
the keyboard. The format is not necessary. Also, to read a single value use m = 1, n=
1, as shown below.
For example, type the following function into a file called inout.txt:
function inout()
//this script illustrates using read and write
write(%io(2),'Enter a real variable x:','(a)');
x = read (%io(1),1,1);
write(%io(2),'Enter a real variable y:','(a)');
y = read (%io(1),1,1);
z = x+y;
write(%io(2),'the sum of x and y is:','(a)')
write(%io(2),z,'(10x,e13.7)')
//end of function
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
Within SCILAB, type the following commands, and responses to prompts:
Getf(inout.txt) <enter>
inout( ) <enter>
1.2 <enter>
2.4 <enter>
Notice that the function inout has no arguments. Still, in the function definition as
well as in the function call it has to have a pair of parentheses.
Reading from files
Use the same read command as used while reading from the keyboard, but using an
open file unit to read. For example, suppose that you have a file called signal.txt,
containing the following values:
1.0 2.0 4.0
2.0 3.0 9.0
3.0 4.0 16.0
4.0 5.0 25.0
5.0 6.0 36.0
6.0 7.0 49.0
If you know the number of rows (n=6, in this case). To read the matrix of values, use:
u=file('open','signal.txt','old') <enter>
A=read(u,6,3); <enter>
A<enter>
If the number of rows is unknown using n=-1 will ensure that the entire file is read. It
is assumed that the file contains only the matrix of interest. For example,
file('rewind',u) <enter>
B = read(u,-1,3); <enter>
B <enter>
file('close',u) <enter>
Manipulating strings in SCILAB
A string is basically text that can be manipulated through SCILAB commands. Strings in SCILAB
are written between single or double quotes. The following are examples of strings:
myFile The result is: a b c abc a b c
Text to be included Please enter the graphic window number 1 3 5
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
String concatenation
The joining of two or more strings is called concatenation. The plus symbol (+), when placed
between two strings concatenates the strings into a single one. In the next example variables
s1, s2, and s3 are defined and concatenated:
-->s1 = 'The result from '
s1 =
The result from
-->s2 = 'multiplication '
s2 =
multiplication
-->s3 = 'is given below.'
s3 =
is given below.
-->sOut = s1 + s2 + s3
sOut =
The result from multiplication is given below.
String functions
The function length determines the number of characters in a given string, for example:
-->length(sOut)
ans =
46.
The function part allows the extraction of characters from a given string. For example, to
extract the first character of a string use:
-->part('abcd',1)
ans =
a
The next command extracts the first and second character of a string:
-->part('abcd',[1,2])
ans =
ab
In the next example, characters 1 and 3 of the string are extracted:
-->part('abcd',[1,3])
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
ans =
ac
To extract a series of character, the characters positions in the string are indicated as a
sequence of values in the vector representing the second argument to function part:
-->part(sOut,[4:1:15])
ans =
result from
The function strindex (string index), with a typical call of the form strindex(string1,string2)
determines the position of the first occurrence of sub-string string2 within string1. For
example,
-->strindex(sOut,'mult')
ans =
17.
Once the position of a sub-string has been determined you can use the function part to extract
that sub-string or other sub-string starting at that position. For example, this function call
extracts characters 17 to 24 of string sOut:
-->part(sOut,[17:24])
ans =
multipli
The function strsubst (string substitution), with a typical call of the form
strsubst(string1,string2,string3)
replaces sub-string string2 with sub-string string3 within string string1. For example, the next
call to function strsubst replaces the sub-string multiplication with division within
string sOut:
-->strsubst(sOut,'multiplication','division')
ans =
The result from division is given below.
Converting numerical values to strings
The function string is used to convert a numerical result into a string. This operation is useful
when showing output from numerical calculations. For example, the next SCILAB input line
performs a numerical calculation, whose immediate output is suppressed by the semi-colon,
and then produces an output string showing the result. The output string produced consists of
the sub-string The sum is concatenated to the numerical result that has been converted to
a string with string(s).
-->s = 5+2; "The sum is " + string(s)
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
ans =
The sum is 7
The following command produces an array or vector of strings. The strings in the vector
represent the numbers from 1 to 5.
-->sNum = string(1:5)
sNum =
!1 2 3 4 5 !
An attempt to add the first two elements of vector sNum produces instead their concatenation,
verifying that the elements are indeed strings, and not numbers:
-->sNum(1)+sNum(2)
ans =
12
String catenation for a vector of strings
To generate a string consisting in inserting a particular sub-string between the characters of a
vector or array of strings use the function strcat (string catenation). The next example
produces a string resulting from inserting the character - between the elements of sNum:
-->strcat(sNum,' - ')
ans =
1 - 2 - 3 - 4 - 5
Converting strings to numbers
To convert a string representing numbers into their numerical equivalent you can use function
evstr (evaluate string). The next command, for example, converts the string elements of
vector sNum, defined earlier, into their numerical equivalents:
-->nNum = evstr(sNum)
nNum =
! 1. 2. 3. 4. 5. !
The plus sign (+) applied to the two first elements of nNum would add, rather than
concatenate, those elements:
-->nNum(1) + nNum(2)
ans =
3.
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
The function evstr can be used to evaluate numerically any string representing number
operations. Some examples are shown below:
-->evstr('2+2')
ans =
4.
-->evstr('sin(%pi/6) + 1/3')
ans =
.8333333
The following example uses function evstr to evaluate the numerical values defined in the
elements of a vector. This particular example uses the values of a couple of variables, s and
m, which must be defined before attempting the evaluation of the strings.
-->s = 2, m = 3
s =
2.
m =
3.
-->evstr(['2' 'sqrt(s)' 'm + s'])
ans =
! 2. 1.4142136 5. !
Executing SCILAB statements represented by strings
To evaluate assignment statements or SCILAB commands defined by strings we use function
execstr (execute string). For example,
-->execstr('a=1')
Although the statement a=1 is executed through the use of execstr, no output is produced. To
check that the statement was indeed executed, request that SCILAB show the value of a:
-->a
a =
1.
You can use execstr to evaluate a series of commands by placing the commands in an array or
vector:
-->execstr(['a=1','b=2','a+b'])
Once again, no output is shown, so the result from the last element in the vector is lost, but
variable b (from the second element in the vector) was indeed stored:
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
-->b
b =
2.
A second example of multiple statements executed through execstr follows:
-->execstr(['s=2' 'm=3' 'r=sqrt(s)' 'q=m+s'])
Check the results of the statements by using:
-->[s m r q]
ans =
! 2. 3. 1.4142136 5. !
The following example shows the execution of a small program whose lines are presented as
string elements of a vector:
-->execstr(['a=2' 'x=[]' 'for j = 1:4' 'x = [x a^j]' 'end'])
The result from the last command can be seen by entering:
-->x
x =
! 2. 4. 8. 16. !
Producing labeled output in SCILAB
The following example shows a way to produce labeled output in SCILAB. The data for the
output is contained in vector d of dimensions 1xm:
-->d = [0.5:0.25:1.5];
-->[n m] = size(d); // m is the list of the
-->for j = 1:m, 'distance no. ' + string(j) + ' is ' + string(d(j)) + '.', end
ans =
distance no. 1 is .5.
ans =
distance no. 2 is .75.
ans =
distance no. 3 is 1.
ans =
distance no. 4 is 1.25.
ans =
distance no. 5 is 1.5.
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
Using the function disp
The previous result uses the variable ans to show each line of output. This is the standard way
that SCILAB uses to show the current output. The result shown above can be simplified even
further by using the function disp (display), as follows:
-->for j=1:m, disp('distance no. '+ string(j) + ' is ' + string(d(j)) + '.'),
end
distance no. 1 is .5.
distance no. 2 is .75.
distance no. 3 is 1.
distance no. 4 is 1.25.
distance no. 5 is 1.5.
The function disp can be used to display any result, not only strings. The following example
shows the function disp used with string as well as numerical output:
-->a = 2; A = [2,3;-1,4]; B = a*A;
-->disp('Matrix B is:'), disp(B)
Matrix B is:
! 4. 6. !
! - 2. 8. !
__________________________________________________________________________________
The variable ans
The variable ans (answer) contains SCILABs current output. You can refer to the last SCILAB
output by using the variable name ans. For example, the following commands uses the
contents of ans to operate on the most recent SCILAB output:
-->3+2
ans =
5.
-->exp(ans)
ans =
148.41316
To verify that the result obtained is correct use:
-->exp(5)
ans =
148.41316
__________________________________________________________________________________
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
Exercises
[1]. Write a SCILAB function to calculate the factorial of an integer number:
n! = n(n-1)(n-2)321
[2]. Write a SCILAB function to calculate the standard deviation of the data contained in a
vector x = [x
1
x
2
x
3
].
( ) ,
1
1
1
2

=
n
k
k
x x
n
s
wherex is the mean value of the data,
.
1
1

=
=
n
k
k
x
n
s
[3]. Write a SCILAB function to calculate the function defined by

< +
< +
=
elsewhere
h
, 0
4 1 ),
2
exp( ) 2 ln(
1 0 ), 1 ln(
) (

[4]. Plot the function h() in the interval -1<<10.


[5]. Save the data used in exercise [4] into a text file, then, retrieve the data into vectors x
and y and calculate the mean and standard deviation of x and y using the function developed in
exercise [2].
[6]. Write a SCILAB function that finds the median of a data sample. The median is defined as
that value located exactly in the middle of the data sample once it has been sorted in
increasing order. The algorithm to find such value is given by:
x
m
= x
(n+1)/2
, if n is even
x
m
= (x
n/2
+x
(n+2)/2
), if n is odd
where n is the sample size. To order the data sample you can use the SCILAB function sortup
(use -->help sort to find more about this function).
[7]. The coefficients of the binomial expansion
(a+b)
n
= C(n,0)a
n
+ C(n,1)a
n-1
b + C(n,2)a
n-2
b
2
+ + C(n,n-1)ab
n-1
+ C(n,n)b
n
,
are given by
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
.
)! ( !
!
) , (
k n k
n
k n C

=
Write a SCILAB function that produces a table of binomial coefficients for n = 1, 2, , 5. Use
the function developed in exercise [1] to calculate factorials of integer numbers.
[8]. Write a SCILAB program to define a function given by

< +
< +
<
=
3 2 , 1
2 1 ), 1 /( 1
1 0 ), sin(
) (
2
2
2
x x
x x
x x x
x f
Plot the function for 0 < x < 3.
[9]. Write a SCILAB function that request from the user the values of the bottom width (b) and
water depth (y) for a rectangular cross-section open channel (see figure below) and prints the
area (A = bh), wetted perimeter (P = b+2h), and hydraulic radius (R = A/P) properly labeled.
Try the function for values of b = 3.5 and y = 1.2.
[10]. Write a SCILAB function that request from the user the values of the initial position
(x
0
,y
0
) of a projectile, the initial velocity given as a magnitude v
0
, and an angle
0
, and the
acceleration of gravity g (see figure below). The function also requests from the user an
initial time t
0
, a time increment t, and an ending time t
f
. The function produces a table of
values of the velocity components v
x
= v
0
cos(
0
), v
y
= v
0
cos(
0
), the magnitude of the velocity,
v = (v
x
2
+v
y
2
)
1/2
, the position of the projectile, x = x
o
+ v
0
cos(
0
)t, y = y
o
+ v
0
sin(
0
)t - gt
2
/2, and
the distance of the projectile from the launching point, r
0
= ((x-x
0
)
2
+(y-y
0
)
2
)
1/2
. The function
also produces plots of x - vs. - t, y - vs. - t, r
0
- vs. - t, and y - vs. -x in different graphic
windows. [Note: to generate a new graphics window use the SCILAB command --
>xset(window,j) where j is the window number.]
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
[11]. Suppose you want to plot the function r() = 3.5(1 - cos(2 )). Write a SCILAB function
that generates values of from 0 to 2, calculates the values of r, and the Cartesian
coordinates x = r cos(), y = r sin(), and prints a table showing those values, i.e., , r, x, and
y. The function also produces a plot of y-vs.-x.
R RE EF FE ER RE EN NC CE ES S ( (f fo or r a al ll l S SC CI IL LA AB B d do oc cu um me en nt ts s a at t I IN NF FO OC CL LE EA AR RI IN NG GH HO OU US SE E. .c co om m) )
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Graphics with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
GRAPHICS WITH SCILAB 3
TWO-DIMENSIONAL GRAPHICS 3
The plot command: simple x-y plots 3
The SCILAB Graphic window 4
Creating a plot with error bars 5
Adding a simple grid to the plot 5
Other on-line commands for managing graphics 5
Changing global parameters of a plot 6
The plot2d command 12
Defining the grid 16
Other two-dimensional plot commands 18
Histograms 19
Creating sub windows with the command xsetech 19
Modifying plot properties 20
Storing a plot as a pixmap an example of animation 21
The colormap 22
Density plots 26
Color map of a matrix 27
Plotting a function of the form y = f(x) 27
Plotting a function of two variables using grayscale 28
Plotting a vector field in the plane 29
Direction field vector plot for an ordinary differential equation 29
THREE DIMENSIONAL GRAPHICS 30
Plotting data in 3D 30
Three-dimensional surface plot with color or gray scales 34
Plotting functions in 3D 35
Grayscale or colorscale three-dimensional plot for functions 35
Parametric curves in space 35
Contour plots in the plane and in space 36
Three dimensional histograms 38
Animation of three-dimensional graphs 39
Combination of two-dimensional and three-dimensional plots 39
OVERVIEW OF SCILAB GRAPHICS FUNCTIONS 40
Exercises 43
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Graphics with SCILAB
SCILAB provides a number of functions for producing a variety of two- and three-dimensional graphics.
Some examples of graphics and their applications using SCILAB are provided in this chapter. The chapter
includes description of two- and three-dimensional graphics, as well as some examples of graphics
animation.
TWO-DIMENSIONAL GRAPHICS
The plot command: simple x-y plots
In the solution of non-linear equations of the form f(x) = 0, it is often convenient to be able to plot the
function y = f(x) to visualize the location of the roots of the function. SCILAB offers the command plot to
obtain such graphics. The general form of the command is:
plot(x,y,[xcap,ycap,caption])
plot(y,[xcap,ycap,caption])
where x and y are column vectors (of the same length), xcap is the x-axis label, ycap is the y-axis label, and
caption is the plot title.
In the following example, we first generate a column vector t with the values (0.00, 0.25,,10.0) using the
command
t = (0:0.25:10)';
Next, we generate a vector s = 2t
2
- 2t + 1, by using:
s = 2*t^2-2*t+1;
Finally, we use the plot command to plot s vs. t, as follows:
plot(t,s,'time(s)','position(ft)','linear motion')
SCILAB will generate a graphics window called SCILABGraphic0 showing the plot (see figure below).
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
The SCILAB Graphic window
The following menus are available in the SCILAB Graphic window:
File
2D Zoom
UnZoom
3D Rot
Within the File menu you will find the options:
Clear: erases current graph
Select: (operation not clear)
Print(SCILAB): prints current graph.
Export: allows you to save the plot in formats such as Postscript,GIF, etc.
Save: allows you to save the plot in SCILAB format, the default filename has the suffix scg.
(e.g., plot1.scg)
Load: allows you to read in a plot saved in SCILAB format.
Close: to close the current SCILAB graphics window.
Print(Windows): prints graph to Windows default printer
Copy to clipboard(EnhMetafile): copies graph to clipboard as an enhanced metafile that can be pasted
into other documents, such as a MSWord file.
Copy to clipboard(Metafile+DIB): similar to the previous option. (Note: when pasting to MSWord,
for example, the previous option seems to produce a better quality graph.)
Redraw: to redraw graph if manipulation of the same has diminished its quality.
Update Ini: (operation not clear)
Clicking on the 2D Zoom menu option allows you to select an area for zooming by dragging the cursor
through the area of interest. When you release the mouse's left button the selected area will be zoomed in
the graphics window. Click on the UnZoom menu option to recover the original graph.
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
The 3D Rot menu option is used with three-dimensional graphics to rotate the point of view. It will not
work with two-dimensional graphics such as the present one.
Creating a plot with error bars
The command errbar lets you add error bars to plots of functions. The call to the command has the form
errbar(x,y,em,ep)
where x, y, em, and ep are four matrices of the same size. These matrices contain the values of the plot
coordinates (x,y) and the values that determine the error bars according to the formula:
[y(i,j)-em(i,j),y(i,j)+ep(i,j)].
As an example, try the following commands:
x = (-%pi:%pi/50:%pi); y = sin(x); // Calculate vectors x and y
em = 0.05.*rand(x); ep = 0.1*rand(x); // Generate random errors
plot(x,y) // Plot y vs. x
errbar(x,y,em,ep) // Add error bars
Adding a simple grid to the plot
To add a grid to the plot use the command:
xgrid()
With this call, grid properties are chosen by SCILAB. The function xgrid( ) can use an integer value as
argument that determine the color of the gridlines. For example, try the following SCILAB commands to
see the different grid types:
x = (-%pi:%pi/50:%pi); y = sin(x);
plot(x,y,"x","y","a plot")
xgrid()
xgrid(1)
xgrid(2)
xgrid(3)
xgrid(4)
xgrid(15)
xgrid(21)
Other on-line commands for managing graphics
These are generic graphic commands that can be used from the SCILAB command window to manipulate
the current graph window.
xbasc(): clears the current graphics window
xclear(i): clears one or more graphic windows without affecting the contents of the graphs.
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
xdel(i): deletes a graphic window
xselect(): brings up the current graphic window
xsave('filename',i): saves the graph of window i in the file designated by the string 'filename'
xload('filename',[i]): loads the graph the file designated by the string 'filename'. The window number, i, is
optional.
winsid(): returns the number of the current graphics window.
Changing global parameters of a plot
Global parameters of a plot refers to settings to be used in the plot such as the type and size of font used in
labels, the type and size of the marks for plotting points, the colormap (a matrix of color definitions for
filling areas in the plot), the window number to be open or created, the position of the graphic window, etc.
Many of these properties can be changed by using the function xset:
xset(choice-name,x1,x2,x3,x4,x5)
xset( )
The parameter of this function are a string, choice-name, and five numbers, x1,x2,x3,x4,x5, which depend
on the value of the string choice-name.
Some calls for the function xset are shown below:
xset("alufunction",number): Used to set the logical function for drawing. It works only under X11
NOTE: X11 is the driver for X-Windows environments in UNIX and LINUX operating systems. Most
graphical functions that start with x act on the X11 driver, however, some can also be used to control the
SCILAB graphics window in Windows or other operating systems
The logical function used (alufunction) is set by x1. src is the source, i.e., what we want to draw. dst is the
destination, i.e. , what is already drawn where we want to draw.
Usual values of x1 are: 3 for copying (default), 6 for animation and 0 for clearing.
0: clear, i.e., "0"
1: and , i.e., "src AND dst"
2: and reverse, i.e., "src AND NOT dst"
3: copy, i.e., "src"
4: and inverted , i.e., "(NOT src) AND dst"
5: noop , i.e., "dst"
6: xor, i.e., "src XOR dst"
7: or, i.e., "src OR dst"
8: nor, i.e., "(NOT src) AND (NOT dst)"
9: equiv, i.e., "(NOT src) XOR dst"
10: invert, i.e., "NOT dst"
11: or reverse, i.e., "src OR (NOT dst)"
12: copy inverted , i.e., "NOT src"
13: or inverted , i.e., "(NOT src) OR dst"
14: nand , i.e., "(NOT src) OR (NOT dst)"
15: set , i.e., "1"
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
xset("auto clear","on"|"off"): Switch "on" or "off" the auto clear mode for graphics. When the auto
clear mode is "on", successive plots are not super-posed, i.e., an xbasc() operation (the graphics window is
cleared and the associated recorded graphics is erased) is performed before each high level graphics
function. Default value is "off".
xset("background",color) : Set the background color of the current graphics window.
xset("clipping",x,y,w,h) : Set the clipping zone (the zone of the graphics window where plots can be
drawn) to the rectangle (x,y,w,h) (Upper-Left point Width Height). This function uses the current
coordinates of the plot.
xset("colormap",cmap): Set the colormap as a m x 3 matrix. m is the number of colors. Color number i
is given as a 3-uple cmap(i,1), cmap(i,2), cmap(i,3) corresponding respectively to red, green and blue
intensity between 0 and 1.
xset("dashes",i): Set the dash style to style i (0 for solid line). This is used only when in black and white
mode: xset("use color",0). Use xset() to see the styles.
xset("default"): Reset the graphics context to default values.
xset("font",fontid,fontsize): Set the current font and its current size.
xset("foreground",color): Set the foreground color of the current graphics window.
xset("fpf",string): Set the floating point format for number display in contour functions. string is a string
giving the format in C format syntax (for example string="%.3f"). Use string="" to switch back to default
format.
xset("hidden3d",colorid): Set the color number for hidden faces in plot3d.
xset("line mode",type): This function is used to set the line drawing mode. Absolute mode is set with
type=1 and relative mode with type=0. (Warning: the mode type=0 has bugs)
xset("mark",markid,marksize): Set the current mark and the current mark size. Use xset() to see the
marks.
xset("pattern",value): Set the current pattern for filling functions. value is an integer projected in the
interval [0,whiteid]. 0 is used for black filling and whiteid for white. The value of whiteid can be obtained
with xget("white").
xset("pixmap",flag)
If flag=0 the graphics are directly displayed on the screen.
If flag=1 the graphics are done on a pixmap and are sent to
the graphics window with the command xset("wshow"). The pixmap
is cleared with the command xset("wwpc"). Note that the usual
command xbasc() also clears the pixmap.
xset("thickness",value): Set the thickness of lines in pixel (0 and 1 represent 1 pixel thick).
xset("use color",flag)
If flag=1 then xset("pattern",.) or xset("dashes",.) will be
used so as to change the default color for drawing or for
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
filling patterns.
If flag=0 then we switch back to the gray and dashes mode.
xset("viewport",x,y): Set the position of the panner.
xset("wdim",width,height): Set the width and the height of the current graphics window. This option is
not used by the postscript driver.
xset("window",window-number): Set the current window to the window window-number and creates the
window if it does not exist.
xset("wpos",x,y): Set the position of the upper left point of the graphics window.
xset("wresize",flag)
If flag=1 then the graphic is automatically resized to fill
the graphics window.
xdel();xset("wresize",1);plot2d();xset("wdim",1000,500)
If flag=0 the scale of the graphic is left unchanged when the
graphics window is resized. Top left panner or keyboard arrows
may be used to scroll over the graphic.
xdel();plot2d();xset("wresize",0);xset("wdim",1000,500)
xset("wshow"): See xset("pixmap",1) above.
xset("wwpc") : See xset("pixmap",1) above.
Changing parameters with xset() or xsetm():
The command xset(), or the command xsetm(), both without arguments, can be used to set some of the
graph properties through a series of input boxes provided by SCILAB. For example, if you use:
xset()
You can change the following properties (see Figure below):
fontID gets changed to times italic
fontsize gets changed to 14
markID gets changed to "+"
Press the "Next>" button so that you can change the next set of properties (see second Figure below):
marksize gets changed to 10
Thickness gets changed to 4
pixmap/flag will be kept as off
Press the "Next>" button so that you can verify the third set of properties (see third Figure below):
use colors will be kept as "Yes"
colors will be kept as 33
alufunction will be kept as scr (see options for alufunction above)
Press the "Finished" button when done modifying properties.
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
Create the following plot:
t = (-10:0.1:10); s = 3.2.*(1 - exp(-0.2*t));
plot(t,s,"time(hrs)","position of asteroid(10000 km)","Astronomy Plot")
To reset the graphical properties use:
xset('default')
This will clear out the current graphics window. Try the plot again, using:
plot(t,s,"time(hrs)","position of asteroid(10000 km)","Astronomy Plot")
This time you will get the default settings for the graph.
Changing the font style and size with xset('font', fontId, fontSize)
You can use the function xset to change the font size. The fontSize corresponds to typical font sizes used
in texts, while fontId refers to the following font styles:
0 Courier
1 Symbol
2 Times
3 Times Italic
4 Times Bold
5 Times Bold Italic
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Try the following exercise to see the different font options:
x = (0.1:0.1:20); y = sqrt(1+x^2);
for j = 0:5, xset('window',j), xset('font',j,4), plot(x,y,"x","y","title"), end
Generating new graphics windows
The command
xset('window',window_number)
generates a new graphics window. The window_number must be a nonnegative integer. The default
value is SCILABGraphic0, corresponding to window_number = 0.
This exercise shows how to produce four different graphs in four different windows:
x = (-10:0.1:10); y = sin(2.*x);
z = cos(2.*x) - 1; w = 1./(x+1); t = abs(x);
xset('window',1); plot(x,y,'x','y','First Plot')
xset('window',2); plot(x,z,'x','z','Second Plot')
xset('window',3); plot(x,w,'x','w','Third Plot')
xset('window',4); plot(x,t,'x','t','Fourth Plot')
Changing the background color
The background color of the graphics window is by default white. You can change that background color
using the call
xset('background',color)
where the parameter color must be a nonnegative number. The available colors, for color values from zero
to 20, in a Windows 98 environment, are:
0 black 1 black 2 dark blue
3 bright green 4 sky blue 5 bright red
6 purple 7 bright red 8 white
9 light blue 10 blue 11 dark blue
12 sky blue 13 dark green 14 dark green
15 bright green 16 dark green 17 dark greenish blue
18 greenish blue 19 dark red 20 dark red
21 red 22 dark purple 23 bright purple
24 dark reddish brown 25 dark reddish brown 26 reddish brown
27 dark orange 28 pink 29 pink
30 pink 31 pink 32 dark yellow
There are slight differences between colors 4 and 12, and between colors 13, 14, and 17.
The user is encouraged to try the following exercises to check the different background colors:
for j = 1:20, xset('window',j), xset('background',j), end
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
Changing the foreground color
The foreground of a graph corresponds to those lines such as the frame of a plot, the labels, the grid marks,
etc. The foreground color can be changed by using
xset('foreground',color)
where the parameter color was defined above for background color changes.
To see changing of foreground and background colors simultaneously, try the following exercise:
x = (-%pi:%pi/100:%pi); y = sin(x);
xset('background',32); // background = yellow,
xset('foreground', 13); // foreground = green
plot(x,y,"x","y","sine")
To find current settings use
xget( )
This is similar to the function xsetm( ).
The plot2d command
This command can be used if you want to plot more than one two-dimensional plot. The plot2d command
has the general form:
plot2d(x,y,[style,strf,leg,rect,nax])
plot2d(y);
where
x,y are two matrices of the same size [nl,nc], nc = number of curves, and nl = number of points for each
curve (if nl=1, it is assumed that there is only one curve of nc points);
style is a real vector of size (1,nc). The style to use for curve j is defined by style(j). For the plot2d
function the value of style represents the color of the curve used for the plot.
strf is a string of length 3, written as "xyz", where if x=1 captions are displayed (Captions are given by the
string leg1@leg2@...., corresponding to legends for the different curves being plotted);
y controls the computation of the frame,
if y=0 the current boundaries, set by a previous call to an other high level plotting function, are
used;
if y=1, the argument rect is used to specify the boundaries of the plot.
rect=[xmin,ymin,xmax,ymax];
if y=2 the boundaries of the plot are computed using max and min values of x and y.,
if y=3, it is similar to y=1 but produces isoview scaling;
if y=4, it is like y=2 but produces isoview scaling;
if y=5, it is like y=1 but the boundaries and nax can be changed to produce nice-looking
graduations (this mode is used when the zoom button is activated);
if y=6, it is like y=2 but the boundaries and nax can be changed to produce nice-looking
graduations (this mode is used when the zoom button is activated);
z controls the display of information on the frame around the plot;
if z=1, an axis is drawn the number of tics can be specified by the nax argument. nax is a vector
with four entries [nx,Nx,ny,Ny] where nx (ny) is the number of sub-graduations on the x (y) axis
and Nx (Ny) is the number of graduations on the x (y) axis;
if z=2, the plot is framed but no grid is shown;
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
if z takes any other values no frame is produced.
Simple examples of plot2d( )
Some examples of simple applications of the command plot2d follows:
First, we create the following (row) vectors:
x = (-2*%pi:%pi/100:2*%pi);
y = sin(2.*x);
z = cos(x);
w = exp(-abs(0.1*x)).*sin(x);
Next, we will use a simple call to the function plot2d to see what results:
plot2d(x,y)
plot2d(x,z)
plot2d(x,w)
The default graphic window is SCILABGraphic0. The function plot2d places one plot on top of the other in
the default graphic window. This graphic output is not very useful because the vertical scales get
recalculated with every new graph added. We will show later how to place more than one plot in a single
graph with a common vertical scale.
The next exercises show calls to the function plot2d using the transpose of the (row) vectors created earlier.
Close the graphic window before running these exercises:
plot2d(x',y')
plot2d(x',w')
plot2d(x',z')
As with the three previous examples, the graphic output is messy as the vertical scales for each individual
plot are different.
Creating multiple plots with plot2d( )
One can create individual plots in different graphic windows by using, for example, the following
commands:
xset('window',1); plot2d(x',y'); xtitle('Plot 1')
xset('window',2); plot2d(x',z'); xtitle('Plot 2')
xset('window',3); plot2d(x',w'); xtitle('Plot 3')
You can combine the three graphs into a single set of axes by using the following form of the plot2d
command:
xset('window',4); plot2d([x',x',x'],[y',z',w']); xtitle('Combined plot 1')
The command plot2d([x',x',x'],[y',z',w']) indicates to SCILAB that three graphs will be
created together with the abscissa values (x-values) of the plots given by the set [x', x', x'], and the
ordinate values (y-values) given by [y', z', w']. When producing a multiple-plot with this form of the
command, the vectors that describe the plots must be passed as column vectors. Because we defined the
vectors, x, y, z, and w, as row vectors, we need to add the apostrophe (i.e., x', y', z', w') in the
function call. Alternatively, you can create the vectors as column vectors from the very beginning, e.g.,
x = (-2*%pi:%pi/100:2*%pi);
y = sin(2.*x); z = cos(x); w = exp(-abs(0.1*x)).*sin(x);
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
and produce the combined plot as:
xset('window',5); plot2d([x,x,x],[y,z,w]); xtitle('Combined plot 2')
Notice that each of the three plots is shown with a different color in the two examples shown above. Also,
we have used the command xtitle to add a title to the plot. This command can be used with any two or
three-dimensional plots in SCILAB.
The following example shows another way of plotting more than one function of the same independent
variable:
x = (0:0.1:10)';
xset('window',0);
plot2d([x,x,x], [ sin(x) cos(x) abs(sin(x) cos(x))])
In this case, rather than creating vectors (such as vectors y, z, and w, used earlier), we define the functions
to be plotted ([ sin(x) cos(x) abs(sin(x) cos(x))]) within the call of the function plot2d.
Changing the line styles in multiple plots
So far we have used the plot2d command with only two arguments, namely the vectors of abscissa and
ordinate values of the plots. We can use additional arguments in the call of this function that lets us have
more control on the output. For example, the following call to plot2d is used to plot three curves
simultaneously.
plot2d([x,x,x],[sin(x) cos(x) sin(x)+cos(x)],[10 20 30])
This call to plot2d includes a third argument, besides those describing the vectors that produce the plots.
The vector, namely [10 20 30], represents a style or color vector. We are assigning the colors identified as
10, 20, and 30, respectively, to the three plots in question. (See the section before that describes changing
the foreground and background colors in a SCILAB plot). Try the command described in your SCILAB
command window to see the result.
Adding a caption to the plots
The following call to plot2d plots the same functions as before, but it adds captions to the plot that identify
the three different curves:
plot2d([x,x,x],[sin(x) cos(x) sin(x)+cos(x)],[10 20 30],'100',...
'position@velocity@acceleration')
The command includes the vector [10 20 30] representing styles for the three curves, and the string '100'
which is to be interpreted as follows:
1 Captions for the curves, given by the string 'position@velocity@acceleration', are to be displayed
0 Do not change the current graph boundary setting (i.e. use current setting)
0 Do not produce a frame in the plot
The command also includes the particle ... which is used in SCILAB to indicate continuation of a command
in the next line. Try this command to see the results.
NOTE: if you experience difficulties using the last command, try this command first:
plot(x,sin(x)+cos(x))
This command sets up the proper scale for the plot before you try the more complicated plot2d.
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
Changing the command window limits
If you want to determine the plot window dimensions in the call to the function plot2d, you need to change
the 0 in the middle position of the control string '100' to 1. For example, if you want to set your plot
window to cover the ranges 0 < x < , and 0 < y < 1, try the following command call:
plot2d([x,x,x],[sin(x) cos(x) sin(x)+cos(x)],[10 20 30],'110',...
'position@velocity@acceleration', [0 0 %pi 1])
The last argument in the call to plot2d shown above includes the window dimensions as [xmin ymin xmax
ymax] = [ 0 0 %pi 1], i.e., the plot window extends from point (0,0) to point (,1). Try this command to
see the result. Because the control string '110' includes a zero in the last position, the command, as shown,
will not produce a frame for the plot.
Showing the frame in the plot
If you want to show the frame in the plot, you need to change the string '110' to '111' in the command
above. The 1 in the third position of this control string allows the user to define the grid in the plot by
adding a vector [nx,Nx,ny,Ny] to the function call. In this vector, nx and ny are the number of sub-
graduations on the x and y axes, respectively, and Nx and Ny are the number of graduations on the x and y
axes, respectively. Try the following command:
plot2d([x,x,x],[sin(x) cos(x) sin(x)+cos(x)],[5,10,15],'101',...
'position@velocity@acceleration', [0 0 %pi 1], [10 5 10 5])
Additional examples of plot2d( )
Additional examples of use of the plot2d command follow:
xset('window',1);
x=-2*%pi:%pi/100:2*%pi; plot2d(sin(x)); //simple plot
xset('window',2);
plot2d([x;x;x]',[sin(x);sin(2*x);sin(3*x)]');//multiple plot
xset('window',3); //multiple plot with captions
plot2d([x;x;x]',[sin(x);sin(2*x);sin(3*x)]',...
[1,2,3],"111","s(m)@v(m/s)@a(m/s^2)",[0,-2,2*%pi,2],[2,10,2,10]);
xset('window',4); plot2d(x',sin(x)',1,'041') //isoview plot
xset('window',5); plot2d(x',sin(x)',1,'061') //auto scale
The command xbasc () is used to clear the existing graph before creating a new graph in the same window.
It is similar to using the option clear in the File menu of the SCILAB Graphic window. Alternatively, you
can use the command xset('default') to clear up a graphics window.
Plotting with symbols
To plot with symbols, use a negative integer in the style value. For example, the following statement will
create a plots with symbols rather than a continuous line:
x = (0:1:20); y = 2.*sin(x) + rand();
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
xset('window',1);plot2d(x',y',-5)
The symbols available in a Windows-based version of SCILAB are:
-1 + (dot) -2 X (cross) -3 * -4 diamond fill
-5 diamond -6 triangle up -7 triangle down -8 trifle
-9 circle
You can change the size of the symbol or mark by using the command xset("mark",markid,marksize),
e.g.,
xset("mark",-4,10)
Repeat the plot in a new window to see the change in symbol size:
xset('window',0);plot2d(x',y',-5)
Plotting lines and symbols
The following commands plot a continuous line for the same function used above:
xc = (0:0.1:20);yc = 2.*sin(xc) + rand();
xset('window',2);plot2d(xc',yc',5)
You can plot the two graphs (line and symbols) together by keeping the window dimensions the same in
two consecutive calls of the function plot2d:
xset('window',3)
plot2d(xc',yc',-5,'011','x',[0 -2 20 2],[5 20 10 10])
plot2d(xc',yc',5,'010','x',[0 -2 20 2])
Defining the grid
If you want to have more control not only on the grid characteristics, but other properties of the plot, use
the command plotframe, whose general form is:
plotframe(rect,tics [,arg_opt1,arg_opt2,arg_opt3])
where, rect is the vector [xmin,ymin,xmax,ymax] representing the minimum and maximum values for x
and y in the plot; tics is a vector [nx,mx,ny,my] where mx, nx are the number of x-axis intervals and
subintervals, and my, ny intervals and subintervals for the y-axis. The parameters arg_opt1, arg_opt2,
and arg_opt3 are optional arguments that can be chosen from:
flags, given as a Boolean vector [wantgrids,findbounds] where wantgrids can take values of true(%t) or
false (%f) depending on whether or not you want to have grids or not. The value findbounds can also take
values of true(%t) or false (%f) depending on whether or not you want to use redefine the boundaries of the
plot. If findbounds is %t, the bounds given in rect are slightly increase in order to have simpler graduations
(in such a case, tics(2),tics(4) are ignored).
captions, given as a vector of 3 character strings [title, x-leg, y-leg]
subwin, a vector of size 4 specifying a sub-window definition. The sub-window is specified with the
parameters [x,y,w,h], where (x,y) are the coordinates of the upper corner of the plot (with (0,0) being the
upper left corner of the window), w is the plot's width, and h is the plot's height. The values of x,y,w,h are
specified using proportion of the width or height of the current graphic window.
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
For example, to place a plot occupying the upper left quadrant of the SCILAB Graphic window, you
would use the following subwin vector: [0.,0.,0.5,0.5]. This is interpreted as saying use a sub-window
equal to half the width and the height of the SCILAB Graphic window, with its upper left corner
starting at point (0,0) of the same window. Some examples are shown below.
The frameplot command is to be used in conjunction with the plot2d command, rather than with the plot
command. The following commands illustrate the use of frameplot:
x=[-0.3:0.8:27.3]'; y=rand(x); // random number generator
rect=[min(x),min(y),max(x),max(y)];
tics=[4,10,2,5]; //4 x-intervals and 2 y-intervals
plotframe(rect,tics,[%f,%f],['My plot','x','y'],[0,0,0.5,0.5]);
plot2d(x,y,2,'000')
plotframe(rect,tics,[%t,%f],['My plot with grids','x','y'],[0.5,0,0.5,0.5]);
plot2d(x,y,3,'000')
plotframe(rect,tics,[%t,%t],...
['My plot with grids and automatic bounds','x','y'],[0,0.5,0.5,0.5]);
plot2d(x,y,4,'000')
plotframe(rect,tics,[%f,%t],...
['My plot without grids but with automatic bounds','x','y'],...
[0.5,0.5,0.5,0.5]);
plot2d(x,y,5,'000');
The calls to the function plot2d in this example show as third argument a single value representing the style
(i.e., the color) to be used for the single plot created in each call. Also, notice that, by using the string '000'
in the call to plot2d we let frameplot take over in terms of defining the range and grids of the plots. This
example also shows how to use the function plotframe to place four plots in the same window.
Calculating the grid
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
You can use the SCILAB command graduate to obtain a regular grid in your graphics. The call to the
function graduate can take two forms:
[xi,xa,np]=graduate( xmi, xma, n1, n2)
[xi,xa,np]=graduate( xmi, xma)
where xmi,xma are real scalars; n1, n2 are integers with default values 3 and 10, respectively. The function
returns two real scalars, xi, xa, and an integer value, np, representing a minimum interval [xi,xa] and a
number of ticks, np, such that xi xmi xma xa , xa - xi / np = k(10
n
), k in [1 3 5] for an integer n, and
n1 < np < n2.
Try the following example:
y=(0:0.33:145.78)';
xbasc();plot2d1('enn',0,y)
[ymn,ymx,np]=graduate(mini(y),maxi(y))
rect=[1,ymn,prod(size(y)),ymx];
xbasc();plot2d1('enn',0,y,1,'011',' ',rect,[10,3,10,np])
The example shown above uses the function plot2d1, which is described below. Functions mini, maxi, and
prod are also used. Use the help command with any of these functions to learn about their operation.
Other two-dimensional plot commands
The commands plot and plot2d produce continuous lines (or piecewise linear curves) for the curves being
plotted. If you want to use other type of curves you want to consider using the commands plot2d1, plot2d2,
plot2d3, and plot2d4. The type of curves produced by the different commands are as follows:
plot2d1: piecewise linear curves but with possible logarithmic scales
plot2d2: piecewise constant (stepped) curves
plot2d3: vertical bars
plot2d4: arrows style (used with ordinary differential equations in a phase space)
The general form for these commands is the following:
plot2di(str,x,y,[style,strf,leg,rect,nax]) (where i = 1, 2, 3, 4)
In this command, str is the string "abc", where if a=e the values of x are not used; if a=o, the x-values are
the same for each curve; and a=g means general. If b=l a logarithmic scale is used on the x-axis. If c=l a
logarithmic scale is used on the y-axis. The other parameters in the plot2di command are the same as in the
plot2d command shown earlier.
Examples of plot2d1, plot2d2, and plot2d4 are shown below:
x = (1:0.1:10)'; z1 = 1.5+0.2*sin(x); z2 = 2+cos(x);
xset("font",2,3); // Set font to times, size 3
plot2d1('oll',x,[z1 z2]); // Example of plot2d1 log scale
xbasc() // Clear window
plot2d2('onn',x,[z1 z2]); // Example of plot2d2 piecewise constant
xbasc() // Clear window
plot2d3('onn',x,[z1 z2]); // Example of plot2d3 vertical bar plot
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
The commands below repeat the same plots as above, but selecting plot colors and adding captions to the
curves. The first control string 'oll' indicates that logarithmic scales are to be used in the plot. The second
control string '111' indicates that the graph will have captions, graph window limits ([1 10 1 10]), and a
frame ([1 1 1 1]). The specifications following the second control string are the same that you would use in
a call to plot2d.
xbasc()
plot2d1('oll',x,[z1 z2],[10 20], '111', 'z1@z2', [1 10 1 10], [1 1 1 1] );
xbasc()
plot2d2('onn',x,[z1 z2],[10 20], '111', 'z1@z2', [0 10 0 4], [10 10 5 8] );
xbasc()
plot2d3('onn',x,[z1 z2],[10 20], '111', 'z1@z2', [0 10 0 4], [10 10 5 8] );
Try also combining a piecewise constant plot with a vertical bar plot by using:
xbasc()
plot2d2('onn',x,[z1 z2],[10 20], '111', 'z1@z2', [0 10 0 4], [10 10 5 8] );
plot2d3('onn',x,[z1 z2],[10 20], '111', 'z1@z2', [0 10 0 4], [10 10 5 8] );
Histograms
Histograms are bar plots that show the frequency distribution of a data set. The data gets accumulated into
classes, and the height of the histogram bars represents the count of data points in a particular class.
SCILAB provides the function histplot, whose call is:
histplot(npoint,data,[style,strf,leg,rect,nax])
In this function call the parameter npoint is either an integer value, representing the number of classes to be
included in the histogram, or a row vector of increasing values representing the class boundaries to be
included. The parameter data is a real vector containing the data whose frequency distribution is to be
obtained. The other optional parameters: style,strf,leg,rect, and nax follow the definition of function
plot2d.
For example, try the following exercise:
x = rand(1:500);
classes = [0., 0.1, 0.2, 0.3, 0.4, 0.5, 1.0];
histplot(classes,x)
In this first exercise, there are 6 classes defined, the first 5 with the same width of 0.1, and a last class with
a width of 0.5. The next exercise uses 10 classes of equal width (determined by SCILAB) by replacing the
classes vector with the integer number 10:
xbasc(); histplot(10,x)
Because the data was generated using the uniform random number generator function rand(), the histogram
shows a pretty uniform set of bars.
Creating sub windows with the command xsetech
The command xsetech is used to set the sub window of a graphic window for a specific graph. The general
form of this command is:
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
xsetech(wrect [,frect,logflag])
where wrect = a vector of size 4 (sub window definition); frect = a vector of size 4 (just like the rect
argument of plot2d ); and, logflag = a string of size 2 "xy", where x and y can be "n" or "l". In this case,
"n" stands for normal and "l" for log scale. x is for the x-axis, and y for the y-axis.
The following example will place four different versions of the same plot in a single window:
//Script to plot four different versions of a graph in the same window
x = (1:0.1:10)'; z1 = 2.5 + 0.2*sin(x); z2 = 3 + cos(x);
xset("font",2,3);
xbasc()
xsetech([0.,0.,0.5,0.5],[-1,1,-1,1]); //Upper-left quadrant
plot2d([x,x],[z1,z2],[4 10]);
xtitle('Plot2d - piecewise linear');
xsetech([0.5,0.,0.5,0.5],[-1,1,-1,1]); //Upper-right quadrant
plot2d1('oll',x,[z1,z2],[3 11]);
xtitle('Plot2d1 - with logarithmic scales');
xsetech([0.,0.5,0.5,0.5],[-1,1,-1,1]); //Lower-left quadrant
plot2d2('onn',x,[z1,z2],[2,17]);
xtitle('Plot2d2 - piecewise constant');
xsetech([0.5,0.5,0.5,0.5],[-1,1,-1,1]); //Lower-right quadrant
plot2d3('onn',x,[z1,z2],[12 22]);
xtitle('Plot2d3 - vertical bar plot');
Copy these commands into a text file to run it as a script.
A second example of using xsetech() to split a graph is shown next:
xbasc();
t = (0:0.05:1)'; st=sin(2*%pi*t);
xsetech([0,0,1,0.5]); //Upper half of window
plot2d2("onn",t,st);
xsetech([0.,0.5,1,0.5]); //Lower half of window
plot2d3("onn",t,st);
Modifying plot properties
The following commands can be used to modify plot properties:
xgrid(style): adds a grid to a 2D graph; the calling parameter is the color number (see examples above).
xtitle(title,xlabel,ylabel,frame): adds title and axes labels, as well as a frame if required.
titlepage(string): places title page in the middle of the plot
xclea(x,y,w,h): clears the area in the plot defined by upper left corner = (x,y), width = w, height = h.
xstring(x,y,str,[angle,flag]): draws the string str starting at point (x,y).
File plot3.txt includes these commands to produce a plot. A listing of the file follows:
x=-2*%pi:%pi/100:2*%pi; //Define x
y1=sin(x);y2=cos(x);y3=x; //Calculate signals y1, y2, y3
X=[x;x;x]; Y = [y1;y2;y3]; //Create plot vectors
xbasc(); //Clean graphs windows
//Plot the three signals using plot2d1
plot2d1("gnn",X',Y',[1 2 3]',"111","position@velocity@acceleration",...
[-3,-3,3,2],[2,20,5,5]);
xtitle(["Motion";"signals"],"time(s)","x(m), u(m/s), a(m/s^2)"); //Title
xgrid(15); //Grid
xclea(-2.7,1.5,1.5,1.5); //Clear a small window
xstring(-2.4,0.8,"Verify your results"); //Write to cleared window
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
titlepage("Non-Linear Motion"); //Place title in mid page
Use the help feature in SCILAB to find more information about these commands. To produce the plot use
the command:
exec('plot3.txt')
Notes:
xtitle lets you place a title for the plot, but also label the axes a feature that has been missing in our
previous examples.
xstring lets you write notes in your plots at any location within the graph that you desire.
titlepage places a string in the middle of the plot window. This may not be desirable for many plots for it
may muddle the plots.
Storing a plot as a pixmap an example of animation
The contents of a plot can be temporarily stored as a pixmap by using the command
xset('pixmap',1)
For example, try this exercise:
x = (0:0.1:10)'; y = sin(x); z = cos(x);
xset('pixmap',1) //Activates pixmap option
plot(x,y) //Plot is not shown
xset('wshow') //Recalls pixmap to screen
plot(x,z) //New plot to pixmap, screen is cleared
xset('wshow') //New plot is shown
The plot command is designed to clear out the current graph before plotting a new plot, therefore, there is
no need to clear the pixmap when creating a new graph with plot. The following exercise illustrates the use
of pixmaps with the command plot2d:
xbasc() //Clears the current graphics window
plot2d(x,y) //No plot is shown
xset('wshow') //Recent plot is shown
plot2d(x,z) //No new plot is shown in graphics window
xset('wshow') //Two plots shown in graphics window
Clearing the pixmap
The command plot2d does not clear the most recent screen (or pixmap) before creating a new plot. If you
want to clear the pixmap for a new plot use the command:
xset('wwpc')
For example:
xbasc() //Clears the current graphics window
plot2d(x,y) //No plot is shown
xset('wshow') //Recent plot is shown
xset('wwpc') //Clear out pixmap, but not graphics window
plot2d(x,z) //No plot is shown because pixmap is empty
xset('wshow') //Two plots shown in graphics window
Animation of a plot
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
The following lines lets you produce an animation of the sine wave by using pixmaps:
xbasc()
x = (0:0.1:10)';
for j = 1:1000, y =sin(x+j./10); plot(x,y), xset('wshow'), end
This example takes advantage of the ability of the plot command to clear the graphics window after every
pass. If we were to use the command plot2d to produce the animation, we need to include the command
xset('wwpc') to clear the graphics window after every plot. Try the following exercise:
xbasc()
x = (0:0.1:10)';
for j = 1:1000, y =sin(x+j./10); plot2d(x,y), xset('wshow'), xset('wwpc'), end
The following example shows a second animation:
xbasc()
x = (0:0.1:10)';
for j = 1:1000, y =exp(-0.01.*j).*sin(x); plot(x,y), xset('wshow'), end
While this animation run you will see the shape of the graph changing slightly, however, if you observe the
vertical scale carefully, you will notice that it covers smaller and smaller ranges of y as time increases.
Because we are using the plot command to produce the graph, we have no control over the vertical scale
and we cannot see very clearly the changes in the graph as the value of j increases. The following
commands, using the plot2d function with grid control, lets you see the graph changes in more detail:
xbasc()
x = (0:0.1:10)';
for j = 1:1000, y =exp(-0.01.*j).*sin(x); ...
plot2d(x,y,5,'010',' ',[0 -1 10 1]), ...
xset('wshow'), xset('wwpc'), end
The command plot2d in this exercise was set such that no frame or grid is shown (the control string is
'010'). To include a frame in the plots use:
xbasc()
x = (0:0.1:10)';
for j = 1:1000, y =exp(-0.01.*j).*sin(x); ...
plot2d(x,y,5,'011',' ',[0 -1 10 1],[5 10 5 10]), ...
xset('wshow'), xset('wwpc'), end
The resulting animation will show a sinusoidal wave flattening out. The following command will produce
a moving and flattening sinusoidal wave:
xbasc()
x = (0:0.1:10)';
for j = 1:50, y = exp(-0.01.*j).*sin(x+j./10); ...
plot2d(x,y,5,'011',' ',[0 -1 10 1],[5 10 5 10]), ...
xset('wshow'), xset('wwpc'), end
The colormap
The color map is a m 3 matrix, where m is the number of colors available. When SCILAB starts, a total
of 32 colors are available. These are referred to by its color number. (A list of the basic 32 color numbers
was presented in an earlier section.) A color number i can be defined as the set of three values: cmap(i,1),
cmap(i,2), and cmap(i,3), corresponding, respectively, to red, green, and blue intensities expressed as
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
numbers between 0 and 1. The colormap matrix would be, therefore, referred to as cmap. You can change
the colormap by using the command
xset("colormap",cmap).
An example of colormap modification
The following commands will let you create your own colormap with 100 colors. Before running these
commands, click on the Restart menu to reset SCILAB. The first step after resetting is to create vectors R
(red intensity), G (green intensity), and B (blue intensity), as follows (remember that lines preceded by //
are comment lines):
// Define m as 100, red intensities from 1/m to 1
m = 100; R = [(1:m)/m];
// Green intensities go from 2/m to 1 and back to 2/m
n2=m/2; G1 = [(1:n2)/n2]; G2 = [(n2+1:m)]; G3 = (m-G2)/(m-n2); G = [G1, G3];
// Blue intensities are zero for the first and last 25 values,
// and vary from 2/m to 1 for values 25 to 74.
B = [zeros(1:m/4),G1,zeros(1:m/4)];
// Create color map matrix with R, G, and B, and replace existing color map:
myColorMap1 = [R',G',B']; xset('colormap',myColorMap1);
Then, plot a series of lines corresponding to different colors in your colormap:
// Produce a graph illustrating the 100 different colors
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:100, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 200]), end
A script with many colormap examples
The following lines can be placed in a script, e.g., plot6.txt, and executed from the SCILAB command
window. It illustrates other colormaps by plotting 100 or 120 lines of different colors. The comments in
the script explain the way that the colormaps were created:
//----------------------------------------------------------------------------
// Case 2 (Different tones of red)
// Green and blue all set to zero
m=100; R=[(1:m)/m]; G = [zeros(1:m)]; B = [zeros(1:m)];
myColorMap2 = [R',G',B']; xset('colormap',myColorMap2);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:100, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 200]), end
xtitle('Case 2')
pause
//Case 3 (different tones of light blue)
//Green and blue all set to one
m=100; R=[(1:m)/m]; G = [ones(1:m)]; B = [ones(1:m)];
myColorMap3 = [R',G',B']; xset('colormap',myColorMap3);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
for j = 1:100, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 200]), end
xtitle('Case 3')
pause
//Case 4 (red to purple to blue)
//Green set to zero, blue set to one
m=100; R=[(1:m)/m]; G = [zeros(1:m)]; B = [ones(1:m)];
myColorMap4 = [R',G',B']; xset('colormap',myColorMap4);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:100, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 200]), end
xtitle('Case 4')
pause
//Case 5 (different tones of green)
//Green set to one, blue set to zero
m=100; R=[(1:m)/m]; G = [ones(1:m)]; B = [zeros(1:m)];
myColorMap5 = [R',G',B']; xset('colormap',myColorMap5);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:100, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 200]), end
xtitle('Case 5')
pause
//Case 6 (black, blue, green, red, black)
//Intensities in triangular shaped functions
m = 120; n1 = m/4; n2 = m/2; n3 = 3*m/4;
//Red intensities peak at n1
R1 = [(1:n1)/n1]; R2 = [(n1+1:m)]; R3 = (m-R2)/(m-n1); R = [R1, R3];
// Green intensities peak at n2
G1 = [(1:n2)/n2]; G2 = [(n2+1:m)]; G3 = (m-G2)/(m-n2); G = [G1, G3];
//Blue intensities peak at n3
B1 = [(1:n3)/n3]; B2 = [(n3+1:m)]; B3 = (m-B2)/(m-n3); B = [B1, B3];
myColorMap6 = [R',G',B']; xset('colormap',myColorMap6);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:120, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 240]), end
xtitle('Case 6')
pause
//Case 7 (black, red, green, blue, black)
//Intensities in triangular shaped functions but peaks for red and blue
//are reversed
m = 120; n1 = m/4; n2 = m/2; n3 = 3*m/4;
//Red intensities peak at n3
R1 = [(1:n3)/n3]; R2 = [(n3+1:m)]; R3 = (m-R2)/(m-n3); R = [R1, R3];
// Green intensities peak at n2
G1 = [(1:n2)/n2]; G2 = [(n2+1:m)]; G3 = (m-G2)/(m-n2); G = [G1, G3];
//Blue intensities peak at n1
B1 = [(1:n1)/n1]; B2 = [(n1+1:m)]; B3 = (m-B2)/(m-n1); B = [B1, B3];
myColorMap7 = [R',G',B']; xset('colormap',myColorMap7);
xbasc();
x = (0:0.1:1)'; y = zeros(x);
for j = 1:120, y = y+2; plot2d(x,y,j,'010',' ',[0 0 1 240]), end
xtitle('Case 7')
pause
//----------------------------------------------------------------------------
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
When running the script, SCILAB will produce numbered prompts (-1->) at each pause. Type the
command return at the prompt to continue the script execution. The graph below shows the intensity of
the three basic colors red, green, and blue for case 7 in the script.
Obtaining a color map with the function hotcolormap( )
If you want to obtain a colormap with n colors, with a nice gradation of colors, use the function
hotcolormap(n)
where n is the number of colors requested. For example,
cMap = hotcolormap(40)
xset('colormap',cMap)
plot3d() // This is a demo of the use of function plot3d (see below)
Obtaining a color map with the function graycolormap( )
The function graycolormap(), whose call is
cmap=graycolormap(n)
produces a colormap with n gradations of gray varying linearly from black to white. The exercise shown
above for hotcolormap() is repeated below with the graycolormap() function:
cMap = graycolormap(40)
xset('colormap',cMap)
plot3d() // This is a demo of the use of function plot3d (see below)
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Density plots
A density plot is a two-dimensional representation of a three-dimensional function. The three-dimensional
function z =f(x,y) is projected onto the x-y plane with different gradations of gray (or color) showing the
function values. In SCILAB, a density plot is also referred to as a grayplot. The grayplot command is
described by the call:
grayplot(x,y,z,[strf,rect,nax])
where x and y are real row vectors of size n1 and n2, and z is a real matrix of size (n1,n2). The value z(i,j)
is the value of the function z = f(x,y) at point (x(i),y(j)). The definition of the other optional parameters,
strf, rect, and nax is the same as in the command plot2d.
Examples of grayplot applications
The grayplot command uses the current colormap to produce the gradation of the plot. Check the results
of using the default colormap in SCILAB:
As an example, try the following commands:
xset('default'); xbasc(); // Set defaults, clear graphic
x=-10:10; y=-10:10;m1 =rand(21,21); // Generate data for plot
grayplot(x,y,m1,"111",[-20,-20,20,20]) // Call grayplot function
The default colormap is a color scale. Try a second example:
t=-%pi:0.1:%pi; m2=sin(t)'*cos(t); // Generate a second set of data
xbasc();grayplot(t,t,m2) // Clear graph, call grayplot
Density plots in grayscale
The following exercise changes the colormap of the first example shown to different tones of gray:
R = [1:256]/256; cMap = [R', R', R']; xset('colormap',cMap);
grayplot(x,y,m1,"111",[-20,-20,20,20])
For the second example, we would get:
xbasc();grayplot(t,t,m2)
Density plots in a reddish scale
If you want to create a grayplot with different tones of red, use, for example:
R = [1:256]/256; G = 0.1*ones(R);
cMap = [R', G', G']; xset('colormap',cMap);
grayplot(x,y,m1,"111",[-20,-20,20,20])
For the second example under study:
xbasc();grayplot(t,t,m2)
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
Smooth density plots
The function Sgrayplot is an alternative to grayplot producing smoothed-out contours in the plane. The call
to the function is the same as that of grayplot, thus, in the following script we repeat the exercises above
using Sgrayplot. When running this script, type return to continue script execution after the pauses.
xset('default'); xbasc();
x=-10:10; y=-10:10;m1 =rand(21,21);
Sgrayplot(x,y,m1,"111",[-20,-20,20,20])
pause
t=-%pi:0.1:%pi; m2=sin(t)'*cos(t);
xbasc(); Sgrayplot(t,t,m2)
pause
R = [1:256]/256; cMap = [R', R', R']; xset('colormap',cMap);
xbasc(); Sgrayplot(x,y,m1,"111",[-20,-20,20,20])
pause
R = [1:256]/256; G = 0.1*ones(R);
cMap = [R', G', G']; xset('colormap',cMap);
xbasc(); Sgrayplot(x,y,m1,"111",[-20,-20,20,20])
pause
xbasc(); Sgrayplot(t,t,m2)
Color map of a matrix
The function Maplot (must use upper case), with call
Matplot(a,[strf,rect,nax])
is used to produce a color plot of the matrix a, of size (n1,n2). The color associated with element a(i,j) is
placed in the square of side = 1 with center at location x=j, y=(n2-i+1). The optional parameters, strf, rect,
and nax are the same as in function plot2d.
As an example, try the following commands:
x = (0:0.1:10)'; y = (0:0.1:10)';
m = x*y';
xbasc();
Matplot(a);
A second example uses the control string and provides graph frame and tick information:
Matplot(a,'011',[-10,-10,110,110],[5 12 5 12])
Plotting a function of the form y = f(x)
The command fplot2d can be used to plot a function in a manner similar to that produced by the plot
command, except that in fplot2d the function is defined as an external function of the type y = f(x). For
example, the following commands, stored in the file plot4.txt, will plot the function y = sin(x) + sin(2*x):
x = (0:0.025:10);
deff("[y]=f(x)","y = sin(x) + sin(2*x)");
fplot2d(x,f)
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
To generate the plot use the command:
exec('plot4.txt')
To add a grid and labels use, for example:
xgrid( )
xtitle("Wave signal","time(s)","signal(J)");
Plotting a function of two variables using grayscale
To produce a grayscale or density plot of a function z = f(x,y) use the command fgrayplot. The general
call to this function is:
fgrayplot(x,y,f,[strf,rect,nax])
where x, and y are real row vectors, and f is an external function of type y=f(x,y). This function can be
defined as a function file or with the on-line function definition command deff. The optional arguments
strf, rect, and nax, in fgrayplot, are the same as in the plot2d function.
As an example, try the following exercise:
R = [1:256]/256; cMap = [R',R',R']; xset('colormap',cMap); xbasc();
deff('[z] = f(x,y)','z = x^2+y^2');
x = -2:0.1:2; y = x;
fgrayplot(x,y,f,'111',[-2.2, -2.2, 2.2, 2.2]);
To embellish the graph, you could add a title such as:
xtitle(['Grayplot produced'; 'with fgrayplot command']);
Try the plot again with a green-type of scale:
G = [1:256]/256; R = 0.1*ones(G);
cMap = [R',G',R']; xset('colormap',cMap); xbasc();
fgrayplot(x,y,f,'111',[-2.2, -2.2, 2.2, 2.2]);
Smooth grayscale function plot
To produce a smoothed-out grayscale or colorscale contour plot use the function Sfgrayplot. The following
script shows the exercises presented above for fgrayplot repeated using Sfgrayplot. Type return after each
pause to continue script execution.
//Green colorscale
G = [1:256]/256; R = 0.1*ones(G);
cMap = [R',G',R']; xset('colormap',cMap); xbasc();
Sfgrayplot(x,y,f,'111',[-2.2, -2.2, 2.2, 2.2]);
pause
//Gray scale
R = [1:256]/256; cMap = [R',R',R']; xset('colormap',cMap); xbasc();
deff('[z] = f(x,y)','z = x^2+y^2');
x = -2:0.1:2; y = x;
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
Sfgrayplot(x,y,f,'111',[-2.2, -2.2, 2.2, 2.2]);
xtitle(['Grayplot produced'; 'with fgrayplot command']);
Plotting a vector field in the plane
SCILAB provides the function fchamp to plot a vector field in the plane defined by a function v(x,y) =
[v
1
(x,y) v
2
(x,y)]. The general call to the function is
champ(x,y,fx,fy,[arfact,rect,strf])
where x and y are vectors that define a grid in the x-y plane, fx and fy are matrices that contain the x- and y-
components of the vector field at the gridpoints defined by x and y, e.g., fx(i,j) = x-component of vector
field at (x(i),y(j)). The optional argument arfact is a real number representing a scale factor for the display
of the arrow heads on the plot (default value is 1.0). The optional argument rect is a vector of four
components [xmin,ymin,xmax,ymax] that provides the frame of the plot. Finally, the optional argument
strf is a string of length 3 "xyz" similar to that used in command plot2d. However, the first character x of
this string has no effect when used in the command champ.
As an example, try the following exercise:
xbasc(); x = (0:0.1:10); y = (0:0.1:10);
fx = sin(x)'*cos(y); fy = cos(x)'*sin(y);
champ(x,y,fx,fy);
A variant of the command champ is the command champ1, which uses color, besides arrow length, to
indicate the intensity of the field. Command champ1 uses the same arguments as champ. Try this
example:
champ1(x,y,fx,fy);
Direction field vector plot for an ordinary differential equation
The function fchamp, whose call is given by
fchamp(f,t,x,y,[arfact,rect,strf])
can be used to plot the vector direction field corresponding to a given differential equation.
The argument f in the function call is an external (function or character string) or a list that describes an
ordinary differential equation. The argument f can be a function name f, corresponding to a function of
the form y=f(t,x,[u]), where f returns a column vector of size 2, y, which gives the value of the direction
field f at point x and at time t. The parameter f can also be an object of type list, i.e., list(f,u1) where f is a
function of type y=f(t,x,u) and u1 gives the value of the parameter u. The parameter t is the selected time
for the plot. The parameters x and y are two row vectors of size n1 and n2 defining the grid on which the
direction field is computed. The optional arguments, arfact, rect, and strf, follow the same definition as in
function champ.
For example, consider a system of differential equations defined by
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz

+
+
=

=
) 1 ( /
/
1 2 1
2 1
2
1
x x x
x x
dt dx
dt dx
dt
dx
where x = [x
1
(t), x
2
(t)], dx
1
/dt = x
1
+x
2
[=xx1], dx
2
/dt = x
1
+x
2
(1-x
1
) [=xx2]. To plot a field vector plot in the
domain -1 < xs < 1, -1 < ys < 1, at t = 0, we use:
-->deff('[dxdt]=der(t,x)',['xx1=x(1)+x(2)';'xx2=x(1)+x(2)*(1-x(1))';'dxdt= [xx1;xx2]'])
-->xs = -1:0.1:1; ys = -1:0.1:1;
-->fchamp(der,0,xs,ys)
THREE DIMENSIONAL GRAPHICS
Plotting data in 3D
SCILAB provides the function plot3d for plotting surfaces representing functions of the form z = f(x,y).
The call to the function plot3d can take any of the following three forms:
plot3d(x,y,z,[theta,alpha,leg,flag,ebox])
plot3d(xf,yf,zf,[theta,alpha,leg,flag,ebox])
plot3d(xf,yf,list(zf,colors),[theta,alpha,leg,flag,ebox])
First form of the function call
In the first form of the call, namely,
plot3d(x,y,z,[theta,alpha,leg,flag,ebox])
the arguments x and y are row vectors of sizes n1 and n2, representing the x-axis and y-axis coordinates or
the rectangular grid in x-y where the surface will be projected. These coordinates must be monotone, i.e.,
they must be in either increasing or decreasing order. The argument z is a matrix of size (n1,n2), so that
z(i,j) represents the value of the surface at point (x(i),y(j)).
Simple example of plot3d and rotation of the three-dimensional graph
A simple example is:
x = (0:0.1:10)';y=x;z=x*y';
plot3d(x,y,z)
This example produces a three-dimensional plot with a point of view at 45
o
from the positive x- and z-axes,
i.e., in the main diagonal of the positive octant in which space is divided by the Cartesian coordinate
system. You can move the point of view by clicking on the menu item "3D Rot", then pointing and
dragging the figure with your mouse. This will show the new location of the graphs borders. When you
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
find one location that you like, press the mouse button and SCILAB will redraw the surface in its new
orientation. Two positions for this example are shown below. First, the default position:
Next, after rotation:
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
Additional arguments in the function call
The optional arguments, theta, alpha, leg, flag, and ebox, apply to the first form of the function call, as well
as to the other two forms of the call listed above. These optional arguments are described as follows:
theta, and alpha are angles in degrees representing the spherical coordinates of the observation point. The
angle theta is measured counterclockwise from the positive x-axis in the x-y plane, while the angle alpha is
measured from the positive z-axis.
leg represents strings that define the captions for each axis with the character @ as field separator. The
simplest implementation of this string is "X@Y@Z" or "x@y@z".
flag is a real vector of size three, i.e., flag=[mode,type,box], where
mode is a string that controls the treatment of hidden parts of the surface as follows:
If mode>0, the hidden parts of the surface are removed and the surface is painted solidly with the
color whose number is represented by the value of mode.
If mode=0, the hidden parts of the surface are drawn.
If mode<0, then, only the shadow of the surface is painted with color or pattern id = mode.
type is an integer representing the scaling of the surface according to:
If type=0 the plot is made using the current 3D scaling, which has been set by a previous call to
param3d, plot3d, contour or plot3d1, or by SCILAB's default graphic parameters.
If type=1 the boundaries are specified by the value of the optional argument ebox (see below).
If type takes other values, the boundaries are computed using the given data.
box is an integer that controls the frame around the plot as follows:
If box=0 or 1, no frame is drawn.
If box=2, only the axes behind the surface are drawn.
If box=3, a box surrounding the surface is drawn and captions are added.
If box=4, a box surrounding the surface is drawn, captions and axes are added.
ebox is used when type in flag is equal to 1. It specifies the boundaries of the plot as the vector
[xmin,xmax,ymin,ymax,zmin,zmax].
Examples using the additional parameters
This first example changes the angle for the point of view and adds captions to the axes:
plot3d(x,y,z,125,-45,'s(m)@r(m)@q(m)')
The next example does the same as the first but adds the flag option with ebox:
plot3d(x,y,z,75,-30,'s(m)@r(m)@q(m)',[1 1 4], [-1,11,-1,11,-1,105])
resulting in the following plot:
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
Call to plot3d using facets
The following call to the function plot3d, namely,
plot3d(xf,yf,zf,[theta,alpha,leg,flag,ebox])
requires as arguments the matrices xf,yf,zf of size (nf,n) that define the n facets used to draw the required
surface. Each facet i is defined by a polygon with nf points. The x-, y-, and z-coordinates of the points of
the i-th facet are given respectively by xf(:,i), yf(:,i) and zf(:,i).
A third call to the function plot3d is
plot3d(xf,yf,list(zf,colors),[theta,alpha,leg,flag,ebox])
which is used to assign a specific color to each facet by using the argument list(zf,colors) instead of zf. In
this version of the call, colors is a vector of size n, with colors(i) being the color of facet i. If colors(i) is
negative, color id = colors(i) is used and the boundary of the facet is drawn.
Using the last two versions of the plot3d function call, you can draw multiple plots by replacing xf, yf and
zf by multiple matrices assembled by rows as [xf1 xf2 ...], [yf1 yf2 ...] and [zf1 zf2 ...].
Obtaining facet information
Facet information can be obtained from measurements, as would be the case of plotting the surface of a
solid object, or by using the function genfac3d to compute four sided facets from the surface z=f(x,y). This
function uses the call
[xx,yy,zz]=genfac3d(x,y,z,[mask])
where xx,yy, and zz are matrices of size (4,n-1*m-1), and their elements xx(:,i) ,yy(:,i) and zz(:,i) are the x-
, y-, and z-coordinates of the 4 points of the i-th four sided facet from the surface. The vectors x, and y are
vectors of size m and n, respectively, representing a grid in the x-y plane, while the matrix z, of size (m,n),
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
contains elements z(i,j) representing the function z = f(x(i),y(j)). The optional argument mask is a
Boolean matrix with the same size as z, and is used to select the entries of z to be represented by facets.
For example, for the surface z = f(x,y) described by:
x = (0:0.1:10)';y=x;z=x*y';
the following call to genfac3d will provide the matrices representing the facets:
[xf,yf,zf]=genfac3d(x,y,z)
Type xf in the SCILAB command window to see the contents of matrix xf, for example.
Obtaining facet information given a parametric surface
If the surface is described by the parametric representation, x = x(u,v), y = y(u,v), and z = z(u,v), where u
and v are real parameters, the command eval3dp can be used to provide facet information. The call for this
command is:
[xx,yy,zz]=eval3dp(fun,u,v)
where xx, yy, and zz are described as in the call to genfac3d, fun is the name of the SCILAB function that
describes the surface, and u and are vectors of size n, and m, respectively, representing a grid of the
parameters u and v that will generate the surface.
For example, try this exercise to obtain facet information and plot the resulting surface:
x = (0:%pi/20:2*%pi)';y=x;
deff('[x,y,z] = spar(u,v)', ...
["x=u.*sin(u).*cos(v)";"y=u.*cos(u).*cos(v)";"z=u.*sin(u)"])
[xf, yf, zf] = eval3dp(spar,x,y)
plot3d(xf,yf,zf);
Three-dimensional surface plot with color or gray scales
An alternative to the use of plot3d is the function plot3d1, which produces a three-dimensional gray or
color level plot of a surface z = f(x,y). The call to this function takes two forms, namely,
plot3d1(x,y,z,[theta,alpha,leg,flag,ebox])
plot3d1(xf,yf,zf,[theta,alpha,leg,flag,ebox])
The arguments used are exactly the same as those used in plot3d, with the caveat that only the sign of the
flag(1), i.e., the mode, parameter is used. If this value is negative, a grid is not drawn.
An example follows:
x = (0:0.1:10)';y=x;z=x*y';
plot3d(x,y,z)
While this result does not show a color scale, by rotating the graph by about 180 degrees about the z-axis
will show the color scale in the front face of the surface. You can accomplish that by dragging the curve
around with the mouse, or by using the command:
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
plot3d1(x,y,z,225,45)
The following example will produce a parametric surface with color scale. The function plo3d1 is called
with facet information as arguments.
x = (0:%pi/20:2*%pi)';y=x;
deff('[x,y,z] = spar(u,v)', ...
["x=u.*sin(u).*cos(v)";"y=u.*cos(u).*cos(v)";"z=u.*sin(u)"])
[xf, yf, zf] = eval3dp(spar,x,y)
plot3d1(xf,yf,zf);
Plotting functions in 3D
The command fplot3d produces the plot of a surface defined by a function f. The call to the command is:
fplot3d(x,y,f,[theta,alpha,leg,flag,ebox])
where x is a row vector of size n, y is a row vector of size m, and f is an external function of the type
z=f(x,y). The vectors x and y define a grid where the function is to be plotted. The remaining, optional,
arguments are described above under function plot3d.
Try the following example:
x = (-10:0.5:10); y = (0:0.5,10);
deff('[z] = fs(x,y)','z = x^2+y^2');
fplot3d(x,y,fs);
Grayscale or colorscale three-dimensional plot for functions
The function fplot3d1 produces a colorscale or grayscale plot of the surface defined by the SCILAB
function z = f(x,y). The call and arguments are very similar to that of function fplot3d:
fplot3d1(x,y,f,[theta,alpha,leg,flag,ebox])
As an example, we repeat the exercise above using fplot3d1:
x = (-10:0.5:10); y = (0:0.5,10);
deff('[z] = fs(x,y)','z = x^2+y^2');
fplot3d(x,y,fs);
Parametric curves in space
Earlier we saw how to plot a parametric curve in space by combining the functions eval3dp and
plot3d. SCILAB provides a function dedicated to the plotting of parametric curves:
param3d(x,y,z,[theta,alpha,leg,flag,ebox])
The arguments x,y, and z must be vectors of the same size representing the coordinates of points of the
parametric curve. The remaining optional arguments (theta, alpha, leg, flag, and ebox) were described in
the call to function plot3d.
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
Try the following examples:
x = (0:%pi/20:2*%pi)';y=x; //parametric curve x,y,z, = f(u,v)
deff('[x,y,z] = spar(x,y)', ...
["x=u.*sin(u).*cos(v)";"y=u.*cos(u).*cos(v)";"z=u.*sin(u)"])
[xs, ys, zs] = spar(u,v);
param3d(xs,ys,zs);
t = (0:%pi/100:2*%pi)'; //parametric curve x,y,z, = f(t)
deff('[x,y,z] = helix(t)', ...
["x = 2.*sin(5.*t)";"y = 2.*cos(5.*t)";"z = 2.5.*t"]);
[xs, ys, zs] = helix(t);
param3d(xs,ys,zs);
Plotting multiple parametric curves in space
A companion function, param3d1, can be used to produce multiple plots. This function can be called in
any of two ways:
param3d1(x,y,z,[theta,alpha,leg,flag,ebox])
param3d1(x,y,list(z,colors),[theta,alpha,leg,flag,ebox])
In these calls, x,y,z are matrices of the matrices of the same size (nl,nc). Each column i of the matrices
corresponds to the coordinates of the i- th curve. You can assign a specific color for each curve by using
the argument list(z,colors) instead of z, where colors is a vector of size nc. If color(i) is negative the curve
is plotted using the mark with id = abs(style(i))+1. If style(i) is strictly positive, a plain line with color id
= style(i) or a dashed line with dash id = style(i) is used. The remaining optional arguments, theta, alpha,
leg, flag, and ebox, are described in function plot3d.
An example follows:
x = (0:%pi/20:2*%pi)';y=x;
deff('[x,y,z] = spar(u,v)', ...
["x=u.*sin(u).*cos(v)";"y=u.*cos(u).*cos(v)";"z=u.*sin(u)"])
[xs, ys, zs] = spar(x,y);
param3d1(xs,ys,zs);
Contour plots in the plane and in space
Although contour plots are two-dimensional plots, they actually represent values of a three-dimensional
function (or curve) projected onto the x-y plane. SCILAB provides the function contour, whose call is
contour(x,y,z,nz,[theta,alpha,leg,flag,ebox,zlev])
to produce contour plots. In the function call, x,y are two real row vectors of sizes n1 and n2,and z is real
matrix of size (n1,n2) representing the values of the function, z = f(x,y). The number of level curves, nz, is
the fourth required argument. If nz is an integer, its value gives the number of equally-spaced level curves
selected by SCILAB in the range of values of z. The argument nz can also be a vector, so that the element
nz(i) gives the value of the i-th level curve.
The remaining optional arguments in the function call, theta, alpha, leg, and flag, and ebox, are the same as
in the function plot3d. In the call to function contour, however, only flag(1)=mode has a special meaning:
If mode=0, the level curves are drawn on the surface defined by (x,y,z). If mode=1, the level curves are
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
drawn on a 3D plot and on the plane defined by the equation z=zlev. Finally, if mode=2, the level curves
are drawn on a 2D plot.
You can change the format of the floating point number printed on the levels by using xset("fpf",string)
where string gives the format in C format syntax (for example string="%.3f"). Use string="" to switch back
to default format.
Contour plots in the plane (only)
The function contour2d is typically used to draw levels curves on a two-dimensional plot. The call to the
function is
contour2d(x,y,z,nz,[style,strf,leg,rect,nax])
The description of this function call is similar to that of the function contour. The optional arguments,
style, strf, leg, rect, and nax, are explained in the description of function plot2d.
Contour plots given a function z = f(x,y)
To produce contours if a curve is defined by the function z = f(x,y) , use the function
fcontour(x,y,f,nz,[theta,alpha,leg,flag,ebox,zlev])
where x and y are two real row vectors of size n1 and n2, and f is an external function which defines the
surface z=f(x,y). This function first calculates the matrix of values z = f(x,y) and then calls function
contour to plot contours in space or in the plane.
Contour plots in a plane given a function z = f(x,y)
To produce contour plots in the plane given a function z = f(x,y), use the function
fcontour2d(x,y,f,nz,[style,strf,leg,rect,nax])
The argument description is the same as for function contour2d.
Contour examples
The following script contains examples of all the contour functions presented above:
//Contour plots
x = (-10:0.5:10); y = x; //Generate x,y vectors
deff('[z] = fc(x,y)','z = x+y^2'); //Define function z = fc(x,y)
[nx mx] = size(x); [ny my] = size(y); //Get sizes of vectors x and y
//Generate z matrix
for i = 1:mx, for j = 1:my, z(i,j) = fc(x(i),y(j)); end, end;
styles = [2:2:20]; //Define styles for some plots
xbasc(); //Case 1
contour(x,y,z,10);
xtitle('contour - 2D')
pause
xbasc(); //Case 2
contour(x,y,z,10,45,45,'x1@x2@x3',[1 1 4],[-12.,12.,-12.,12.,-10.,100.], 40)
xtitle('contour - 3D')
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
pause
xbasc(); //Case 3
contour2d(x,y,z,10)
xtitle('contour2d - simple')
pause
xbasc(); //Case 4
contour2d(x,y,z,10,styles,'111','x1@x2@xx3',[-10 -10 10 10],[5 10 5 10])
xtitle('contour2d - complex')
pause
xbasc(); //Case 5
fcontour(x,y,fc,10)
xtitle('fcontour - 2D')
pause
xbasc(); //Case 6
fcontour(x,y,fc,10,45,45,'x1@x2@x3',[1 1 4],[-12., 12., -12., 12., -10., 100.],
40)
xtitle('fcontour - 3D')
pause
xbasc(); //Case 7
fcontour2d(x,y,fc,10)
xtitle('fcontour2d - simple')
pause
xbasc(); //Case 8
fcontour2d(x,y,fc,10,styles,'111','x1@x2@xx3',[-10 -10 10 10],[5 10 5 10])
xtitle('fcontour2d - complex')
Three dimensional histograms
A three-dimensional histogram will be a matricial representation of frequency counts where the classes are
defined in a grid. There are two different calls available for this function, namely,
hist3d(f,[theta,alpha,leg,flag,ebox])
hist3d(list(f,x,y),[theta,alpha,leg,flag,ebox])
The argument f is a matrix of size (m,n) that defines the frequency count for the histogram, i.e.,
f(i,j)=F(x(i),y(j)), where x and y are taken as 0:m and 0:n. If you use the second call to the function, you
need to provide as first argument a list: list(f,x,y), where f is a matrix of size (m,n) defining the frequency
count as above, and x and y vectors of size (1,n+1) and (1,m+1), respectively. The remaining optional
arguments are the same as in function plot3d.
Unlike the two-dimensional histogram function, histplot, in hist3d you need to provide the frequency count
before the histogram is plotted.
An example is shown next in the form of a script:
xset('default');
x = (-10:5:10); y = x;
[nx mx] = size(x); [ny my] = size(y);
f = rand(mx,my)*100;
xbasc();
hist3d(f)
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
pause
xbasc();
hist3d(f,45,60,'x@y@freq')
pause
Animation of three-dimensional graphs
As with a two-dimensional plot, the animation of a three-dimensional surface requires us to set the pixmap
option with the command xset('pixmap',1). As the plots are generated, with plot3d(xx,yy,zz,...), they are
posted in the graphics window with xset('wshow') and removed with xset('wwpc'). The combined effect of
posting and removing plots is to produce the animation of the surface. The values of the surface
elevations, z = f(x,y), are produced in the line zz = feval(xx+k./10,yy+k./10) where the index k acts as a
time value. Type the following script and run it from the SCILAB command window to see the animation
of the curve, which, in this case, resembles a wave moving in a water surface.
deff('[z] = fs(x,y)',"z = sin(x).*cos(y)"); // Define function f(x,y)
xx = (-%pi:%pi/10:%pi); yy = xx; // Generate xx,yy vectors
[nx mx] = size(xx); [ny my] = size(yy); // Vectors size
xset('pixmap',1); // Activate pixmap
// The following loop produces the animation
for k = 1:50,
zz = feval(xx+k./10,yy+k./10,fs);
//plot3d(xx,yy,zz);
plot3d(xx,yy,zz,45,45,'x@y@z',[3 1 4], [-%pi,%pi,-%pi,%pi,-1,1])
xset('wshow');
xset('wwpc');
end
Combination of two-dimensional and three-dimensional plots
The following examples show how to combine two-dimensional and three-dimensional plots in the same
three-dimensional frame. The first example shows the use of the function geom3d() to project a curve onto
a surface in space. You can type the following commands line by line, or create a scrip that is run from the
SCILAB command window:
//Projecting 3D onto 2D
xbasc();
r =(%pi):-0.01:0; x = r.*cos(10.*r); y = r.*sin(10*r);
deff('[z] = surf(x,y)','z=sin(x).*cos(y)');
t=%pi*(-10:10)/10;
fplot3d(t,t,surf,35,45,'x@y@z',[-3,2,3]); //Plots the surface
z = sin(x).*cos(y); //Generates curve
[x1 y1] = geom3d(x,y,z); //Projects curve onto surface
xpoly(x1,y1,"lines"); //Creates polyline
[x1,y1] = geom3d([0,0],[0,0],[5,0]); //Projects points on the surface
xsegs(x1,y1); //Draws segments
xstring(x1(1),y1(1),'the point (0,0,0)'); //Draws message in plot
The second example shows how to plot a surface and its contours in the same three-dimensional frame:
t = %pi*(-10:10)/10;
deff('[z] = surf(x,y)','z=sin(x).*cos(y)'); //Define function
rect = [-%pi,%pi,-%pi,%pi,-5,1]; //3-D frame dimensions
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
z = feval(t,t,surf); //Surface elevations
xbasc() //Clear window
contour(t,t,z,10,35,45,'x@y@z',[1,1,0],rect,-5); //Plot contours on z = -5
plot3d(t,t,z,35,45,'x@y@z',[2,1,3],rect); //Plot surface
title = ['Plot3d and contour']; //Add title
These examples made use of functions xpoly and xsegs, which have not been presented in this chapter. To
learn more about them use:
help xpoly
help xsegs
OVERVIEW OF SCILAB GRAPHICS FUNCTIONS
This is an overview of SCILAB graphic functions as presented in the SCILAB Help facility. We have
presented the operation of many of these functions in relation to two-dimensional and three-dimensional
graphics of functions. We also covered many of the graphic manipulation functions such as xbasc() and
xset(). Use the help command with any of the functions listed below to obtain on-line information on the
function.
Two-dimensional Plotting
plot : simple plot of a curve
plot2d : plot a curve
plot2d1 : plot a curve, allows logarithmic axes
plot2d2 : plot a curve as step function
plot2d3 : plot a curve with vertical bars
plot2d4 : plot a curve with arrows
fplot2d : plot a curve defined by a function
champ : 2D vector field
champ1 : 2D vector field with colored arrows
fchamp : direction field of a 2D first order ODE
contour2d : level curves of a surface on a 2D plot
fcontour2d : level curves of a surface defined by a function on a 2D plot
grayplot : 2D plot of a surface using colors
fgrayplot : 2D plot of a surface defined by a function using colors
Sgrayplot : smooth 2D plot of a surface using colors
Sfgrayplot : smooth 2D plot of a surface defined by a function using colors
xgrid : add a grid on a 2D plot
errbar : add vertical error bars on a 2D plot
histplot : plot a histogram
Matplot : 2D plot of a matrix using colors
Three-dimensional plotting
plot3d : plot a surface
plot3d1 : plot a surface with gray or color level
fplot3d : plot a surface defined by a function
fplot3d1 : plot a surface defined by a function with gray or color level
param3d : plot one curve
param3d1 : plots curves
contour : level curves on a 3D surface
fcontour : level curves on a 3D surface defined by a function
hist3d : 3D representation of a histogram
genfac3d : compute facets of a 3D surface
eval3dp : compute facets of a 3D surface
geom3d : projection from 3D on 2D after a 3D plot
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
Line and polygon plotting
xpoly : draw a polyline or a polygon
xpolys : draw a set of polylines or polygons
xrpoly : draw a regular polygon
xsegs : draw unconnected segments
xfpoly : fill a polygon
xfpolys : fill a set of polygons
Rectangle plotting
xrect : draw a rectangle
xfrect : fill a rectangle
xrects : draw or fill a set of rectangles
Arc plotting
xarc : draw a part of an ellipse
xarcs : draw parts of a set of ellipses
xfarc : fill a part of an ellipse
xfarcs : fill parts of a set of ellipses
Arrow plotting
xarrows : draw a set of arrows
Strings
xstring : draw strings
xstringl : compute a box, which surrounds strings
xstringb : draw strings into a box
xtitle : add titles on a graphics window
titlepage : add a title in the middle of a graphics window
xinfo : draw an info string in the message subwindow
Frames and axes
xaxis : draw an axis
graduate : pretty axis graduations
plotframe : plot a frame with scaling and grids
Coordinate transformations
isoview : set scales for isometric plot (do not change the size of the window)
square : set scales for isometric plot (change the size of the window)
scaling : affine transformation of a set of points
rotate : rotation of a set of points
xsetech : set the sub window of a graphics window for plotting
xgetech : get the current graphics scale
xchange : transform real to pixel coordinates
Colors
colormap : using colormaps
getcolor : dialog to select colors in the current colormap
addcolor : add new colors to the current colormap
graycolormap : linear gray colormap
hotcolormap : red to yellow colormap
Graphics context
xset : set values of the graphics context
xget : get current values of the graphics context
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
xlfont : load a font in the graphics context or query loaded font
getsymbol : dialog to select a symbol and its size
Save and load graphics
xsave : save graphics into a file
xload : load a saved graphics
xbasimp : send graphics to a Postscript printer or in a file
xs2fig : send graphics to a file in Xfig syntax
Graphics primitives
xbasc : clear a graphics window and erase the associated recorded graphics
xclear : clear a graphics window
driver : select a graphics driver
xinit : initialization of a graphics driver
xend : close a graphics session
xbasr : redraw a graphics window
replot : redraw the current graphics window with new boundaries
xpause : suspend SCILAB
xselect : raise the current graphics window
xclea : erase a rectangle
xclip : set a clipping zone
xdel : delete a graphics window
winsid : return the list of graphics windows
xname : change the name of the current graphics window
Mouse position
xclick : wait for a mouse click
locate : mouse selection of a set of points
xgetmouse : get the current position of the mouse
Interactive editor
edit_curv : interactive graphics curve editor
gr_menu : simple interactive graphic editor
sd2sci : gr_menu structure to SCILAB instruction conversion
Graphics functions for automatic control applications
bode : Bode plot
gainplot : magnitude plot
nyquist : Nyquist plot
m_circle : M-circle plot
chart : Nichols chart
black : Black's diagram
evans : Evans root locus
sgrid : s-plane grid lines
plzr : pole-zero plot
zgrid : zgrid plot
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
Exercises
[1]. Plot the following functions in the ranges indicated below:
(a) f(x) = (x+1)/(x+2), x = 0..10 (b) g(t) = t+t/(1+sin(t)), t = -2..2
[2]. Plot the following functions using polar coordinates. First, generate the values of (r,)
and convert to Cartesian coordinates to produce the plot [e.g., see exercise [11] in Chapter 2].
(a) r() = sin()/(1+cos()), = 0.. /2 (b) r() = sin()(1-sin()), = -/2.. /2
[3]. Plot the functions indicated by the following parametric equations using Cartesian coordinates:
(a) x(t) = sin(t+t
2
/2), y(t) = exp(-t/10) cos(2t), t = 0..10
(b) x(s) = (1+s
2
)
1/3
, y(s) = 1-s
3
, s = 0..10
[4]. Plot the functions indicated by the following parametric equations that use polar coordinates (see
exercise [2], above):
(a) r(t) = sin(t+t
2
/2), (t) = exp(-t/10) cos(2t), t = 0..10
(b) r(s) = (1+s
2
)
1/3
, (s) = 1-s
3
, s = 0..10
[5]. Plot the following functions using the type of plot indicated:
(a) f(x) = exp(5t), t = 1..20, semi-logarithmic plot with f(x) using a logarithmic scale
(b) f(x) = exp(5t), t = 1..1000, double-logarithmic plot
[6]. Plot the three-dimensional function f(x,y) as required. Note: to generate values of z = f(x,y), define the
function f(x,y) using SCILAB function deff, e.g., for case (a), use
-->deff([z]=f(x,y),z=x^2+y^2)
Write vectors containing the ranges of values of x and y, e.g.,
-->x=[-10:0.5:10]; y = [-10:0.5:10];
Then, produce a matrix of values of z=f(x,y) using either function feval or function eval3d, i.e.,
--> z = feval(x,y,f);
(a) f(x,y) = x
2
+y
2
, x = -10..10, y = -10..10
(b) (b) f(x,y) = x sin y + y sin x, x = -.., y = -..
[7]. Plot the space curves described by the following parametric equations in Cartesian coordinates:
(a) x(t) = sin(t), y(t) = 1+t
2
, z(t) = t, t = 0..10 (b) x(t) = t, y(t) = 1/(1+t
2
), z(t) = t
3
, t = 0..10
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
[8]. Plot the three-dimensional surfaces described by the following parametric equations in Cartesian
coordinates:
(a) x(u,v) = u
2
+v
2
, y(u,v) = 2uv, z(u,v) = u+v, u = -10..10, v = -10..10
(b) x(u,v) = (u-v)/(u+v), y(u,v) = uv, z(u,v) = sin(u) cos(v), v = -10..10
[9]. Produce a contour plot of the following functions:
(a) f(x,y) = x
2
+y
2
-2x
2
y+25, x = -2..2, y = -2..2
(b) f(x,y) = x siny + y sin x, x = -2..2, y = -2..2
[10]. Produce density plots of the functions of problem [9].
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and Mathematical
Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John Wiley &
Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company, Reading,
Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's outlines, McGraw-
Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition," Brooks/Cole
- Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace Jovanovich,
Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International Geological
Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley Publishing Company,
Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press, Cambridge CB2
2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications, Inc., New
York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers," The Mc-Graw
Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to MATLAB", Prentice
Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman Scientific and
Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific Computing
(FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper Saddle River, N.J.,
U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace Jovanovich,
Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data," Cambridge
University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for Engineers and
Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline Series, Harcourt
Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD., Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in Applied
Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com, Charleston,
S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate Engineering
Students," John Wiley & Sons, Inc., New York.
Vector Operations with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 - Gilberto E. Urroz
VECTORS WITH SCILAB 2
Operations with vectors 2
Vectors in Cartesian coordinates 5
Vector operations in SCILAB 6
Calculating 2 2 and 3 3 determinants 9
Cross product as a determinant 12
Polynomials as vector components 13
Applications of vector algebra using SCILAB 16
Example 1 - Position vector 16
Example 2 - Center of mass of a system of discrete particles 17
Example 3 - Resultant of forces 18
Example 4 Equation of a plane in space 23
Example 5 Moment of a force 24
Example 6 Cartesian and polar representations of vectors in the x-y plane 25
Example 7 Planar motion of a rigid body 26
Solution of equations one at a time 30
Solution of a system of linear equations using matrices 31
Exercises 32
Download at InfoClearinghouse.com 2 2001 - Gilberto E. Urroz
Vectors with SCILAB
A vector in two- or three-dimensions represents a directed segment, i.e., a mathematical
object characterized by a magnitude and a direction in the plane or in space. Vectors in the
plane or in space can be used to represent physical quantities such as the position,
displacement, velocity, and acceleration of a particle, angular velocity and acceleration or a
rotating body, forces and moments.
O Op pe er ra at ti io on ns s w wi it th h v ve ec ct to or rs s
Vectors in two- or three-dimensions are represented by arrows. The length of the arrow
represents the magnitude of the vector, and the arrowhead indicates the direction of the
vector. The figure below shows a vector A, and its negative A. As you can see, the negative
of a vector is a vector of the same magnitude, but with opposite sense.
Vectors can be added and subtracted. To illustrate vector addition and subtraction refer to the
figure below. Consider two vectors in the plane or in space, A and B, as shown in the figure
below, item (a). There are two ways that you can construct the vector sum, A+B:
(1) By attaching the origin of the vector B to the tip of vector A, as shown in the figure,
item (b). In this case, the vector sum is the vector extending from the origin of vector
A to the tip of vector B.
(2) By showing the vectors A and B with a common origin, and completing the
parallelogram resulting from drawing lines parallel to vectors A and B at the tips of
vectors B and A, respectively, as shown in the figure below, item (c).
The vector sum, also known as the resultant, is the diagonal of the parallelogram that starts at
the common origin of vectors A and B.
Subtraction is accomplished by using the definition:
A B = A + (B).
In other words, subtracting vector B from vector A is equivalent to adding vectors A and (B),.
This operation is illustrated in the figure below, item (d).
Download at InfoClearinghouse.com 3 2001 - Gilberto E. Urroz
A vector A can be multiplied by a scalar c, resulting in a vector cA, parallel and oriented in
the same direction as A, and whose magnitude is c times that of A. The figure below illustrates
the case in which c = 2. If the scalar c is negative, then the orientation of the vector cA will
be opposite that of A.
The magnitude of a vector A is represented by |A|. A unit vector in the direction of A is a
vector of magnitude 1 parallel to A. The unit vector corresponding to a vector A is shown in
the figure below, item (a). The unit vector along the direction of A is defined by
By definition, |e
A
| = 1.0.
The projection of vector B onto vector A, P
B/A
, is shown in the figure below, item (b). If
represents the angle between the two vectors, we see from the figure that P
B/A
= |B|cos =
|e
A
||B|cos , or
.
| | A
A
e
A
.
| |
cos | | | |
P
B/A
A
B A

Download at InfoClearinghouse.com 4 2001 - Gilberto E. Urroz


We will define the dot product, or internal product, of two vectors A and B as the scalar
quantity
AB = | A||B|cos ,
where is the angle between the vectors when they have a common origin. Notice that AB =
BA.
From the definition of the dot product it follows that the angle between two vectors can be
found from
From the same definition it follows that if the angle between the vectors A and B is = 90
o
=
/2
rad
, i.e., if A is perpendicular (or normal) to B (AB), cos = 0, and AB = 0. The reverse
statement is also true, i.e., if AB = 0, then AB.
The cross product, or vector product, of two vectors in space is defined as the vector C = AB,
such that |C| = |A| |B| sin , where is the angle between the vectors, and AC = 0 and BC
= 0 (i.e., C is perpendicular to both A and B). The cross product is illustrated in the figure
below. Since there could be two orientations for a vector C perpendicular to both A and B, we
need to refine the definition of C = AB by indicating its orientation. The so-called right-hand
rule indicates that if we were to curl the fingers of the right hand in the direction shown by the
curved arrow in the figure (i.e., from A to B), the right hand thumb will point towards the
orientation of C. Obviously, the order of the factors in a cross product affects the sign of the
result, for the right-hand rule indicates that BA = - C.
Three vectors A, B, and C, in space, having a common origin, determine a solid figure called a
parallelepiped, as shown in the figure below. It can be proven that the volume of the
parallelepiped is obtained through the expression A(BC). This expression is known as the
vector triple product.
.
| | | |
arccos

,
_


B A
B A

Download at InfoClearinghouse.com 5 2001 - Gilberto E. Urroz


V Ve ec ct to or rs s i in n C Ca ar rt te es si ia an n c co oo or rd di in na at te es s
The mathematical representation of a vector typically requires it to be referred to a specific
coordinate system. Using a Cartesian coordinate system, we introduce the unit vectors i, j,
and k, corresponding to the x-, y-, and z-directions, respectively. The unit vectors i, j, and k,
are shown in the figure below. These unit vectors are such that
ij = ik = jk =0,
and
ij = k, jk = i, ki = j, ik = -j, kj = -i, ji = -k.
n terms of these unit vectors, any vector A can be written as
A = A
x
i + A
y
j + A
z
k,
where the values A
x
, A
y
, and A
z
, are called the Cartesian components of the vector A.
Download at InfoClearinghouse.com 6 2001 - Gilberto E. Urroz
Using Cartesian components, therefore, we can also write B = B
x
i + B
y
j + B
z
k, and define the
following vector operations:
negative of vector: -A = - (A
x
i + A
y
j + A
z
k ) = -A
x
i - A
y
j - A
z
k.
addition: A+B = (A
x
+ B
x
)i + (A
y
+ B
y
)j + (A
z
+ B
z
)k.
subtraction: A-B = (A
x
- B
x
)i + (A
y
- B
y
)j + (A
z
- B
z
)k.
multiplication by a scalar, c: cA = c(A
x
i + A
y
j + A
z
k ) = cA
x
i + cA
y
j + cA
z
k.
dot product: AB = (A
x
i + A
y
j + A
z
k)(B
x
i + B
y
j + B
z
k) = A
x
B
x
+ A
y
B
y
+ A
z
B
z
.
magnitude: |A|
2
= AA = A
x
2
+ A
y
2
+ A
z
2
; |A| = (AA) = (A
x
2
+ A
y
2
+ A
z
2
)
1/2
.
unit vector: e
A
= A/|A| = (A
x
i + A
y
j + A
z
k)/(A
x
2
+ A
y
2
+ A
z
2
)
1/2
.
cross product: AB = (A
x
i + A
y
j + A
z
k )(B
x
i + B
y
j + B
z
k) =
(B
z
A
y
B
y
A
z
)i + (A
z
B
x
B
z
A
x
)j + (B
y
A
x
A
y
B
x
)k.
V Ve ec ct to or r o op pe er ra at ti io on ns s i in n S SC CI IL LA AB B
Consider the vectors A = 3i+5j-k and B = 2i+2j+3k. You can enter these three-dimensional
vectors as row vectors in SCILAB, i.e.,
-->A = [3, 5, -1], B = [2, 2, 3]
A =
! 3. 5. - 1. !
B =
! 2. 2. 3. !
Addition and subtraction of vectors is straightforward:
-->A+B
ans =
! 5. 7. 2. !
-->A-B
ans =
! 1. 3. - 4. !
The negative of these vectors are calculated as:
--> -A
A =
! -3. -5. 1. !
--> -B
Download at InfoClearinghouse.com 7 2001 - Gilberto E. Urroz
B =
! -2. -2. -3. !
The product of the vectors by a constant is also straightforward:
-->2*A, -5*B
ans =
! 6. 10. - 2. !
ans =
! - 10. - 10. - 15. !
The following is a linear combination of the two vectors:
-->5*A-3*B
ans =
! 9. 19. - 14. !
The magnitude of a vector is obtained by using the function norm. For example, the
magnitudes of vectors A and B are calculated as:
-->norm(A)
ans =
5.9160798
-->norm(B)
ans =
4.1231056
Unit vectors are calculated by dividing a vector by its magnitude. For example, unit vectors
parallel to A and B (i.e., e
A
and e
B
) are given by:
-->eA = A/norm(A)
eA =
! .5070926 .8451543 - .1690309 !
-->eB = B/norm(B)
eB =
! .4850713 .4850713 .7276069 !
You can check that these are indeed unit vectors by using norm:
-->norm(eA)
ans =
1.
-->norm(eB)
ans =
1.
Download at InfoClearinghouse.com 8 2001 - Gilberto E. Urroz
The matrix multiplication of a row vector times the transpose of a second row vector (i.e., a
column vector) which will produce a dot product, i.e.,
-->A*B'
ans =
13.
-->B*A'
ans =
13.
Following the rules of matrix multiplication (see Chapter ), the product of a column vector
times a row vector produces a matrix, e.g.,
-->A'*B
ans =
! 6. 6. 9. !
! 10. 10. 15. !
! - 2. - 2. - 3. !
-->B'*A
ans =
! 6. 10. - 2. !
! 6. 10. - 2. !
! 9. 15. - 3. !
When the multiplication symbol between two row (or column) vectors is preceded by a dot, the
result is a vector whose components are the product of the corresponding components of the
factors. This is referred to as a term-by-term multiplication. For example, try:
-->A.*B
ans =
! 6. 10. - 3. !
-->B.*A
ans =
! 6. 10. - 3. !
Term-by-term multiplication requires that the factors have the same dimensions. Thus, the
term-by-term multiplication of a row vector and a column vector is not defined. The following
term-by-term multiplications, for example, will produce error messages:
-->A.*B'
!--error 9999
inconsistent element-wise operation
-->A'.*B
!--error 9999
inconsistent element-wise operation
The dot product of two row or column vectors can be obtained by combining a term-by-term
multiplication with the function sum. The function sum, when applied to a vector, produces
the sum of the vector components, for example:
Download at InfoClearinghouse.com 9 2001 - Gilberto E. Urroz
-->sum(A)
ans =
7.
In the following examples, the function sum is used to calculate the dot product of two
vectors:
-->sum(A.*B)
ans =
13.
The cosine of the angle between two vectors is obtained from the dot product of the vectors
divided by the product of their magnitudes. For example, the cosine of the angle between
vectors A and B is given by
-->costheta = A*B'/(norm(A)*norm(B))
costheta =
.5329480
The corresponding angle is obtained by using the function acos.
-->acos(costheta)
ans =
1.0087155
Of course, the result is in radians, the natural angular units. To convert to degrees use:
-->theta = 180/%pi*acos(costheta)
theta =
57.795141
The projection of vector B over vector A is calculated by dividing the dot product of the two
vectors by the magnitude of A, i.e.,
-->PB_A = A*B'/norm(A)
PB_A =
2.1974011
The calculation of cross products requires the use of determinants, as described in the
following section.
__________________________________________________________________________________
C Ca al lc cu ul la at ti in ng g 2 2 2 2 a an nd d 3 3 3 3 d de et te er rm mi in na an nt ts s
The calculation of a cross product can be simplified if the cross product is written as a
determinant of a matrix of 3 rows and 3 columns (also referred to as a 33 matrix). A
determinant is a number associated with a square matrix, i.e., a matrix with the same number
of rows and columns. While there is a general rule to obtain the determinant of any square
Download at InfoClearinghouse.com 10 2001 - Gilberto E. Urroz
matrix, we concentrate our attention on 22 and 33 determinants. Next, we present a simple
way to calculate the determinant for 22 and 33 matrices.
The elements of a matrix are identified with two sub-indices, the first representing the row
and the second the column. Therefore, a 22 and a 33 matrix will be represented as:
The determinants corresponding to these matrices are represented by the same arrangement of
elements enclosed between vertical lines, i.e.,
A 22 determinant is calculated by multiplying the elements in its diagonal and adding those
products accompanied by the positive or negative sign as indicated in the diagram shown
below.
The 22 determinant is, therefore,
A 33 determinant is calculated by augmenting the determinant, an operation that consists on
copying the first two columns of the determinant, and placing them to the right of column 3, as
shown in the diagram below. The diagram also shows the elements to be multiplied with the
corresponding sign to attach to their product, in a similar fashion as done earlier for a 22
determinant. After multiplication the results are added together to obtain the determinant.
. ,
33 32 31
23 22 21
13 12 11
22 21
12 11
1
1
1
]
1

1
]
1

a a a
a a a
a a a
a a
a a
. ,
33 32 31
23 22 21
13 12 11
22 21
12 11
a a a
a a a
a a a
a a
a a
.
21 12 22 11
22 21
12 11
a a a a
a a
a a

Download at InfoClearinghouse.com 11 2001 - Gilberto E. Urroz
Therefore, a 33 determinant produces the following result:
Determinants can be calculated with SCILAB by using the function det. The following
commands shown examples of 2x2 and 3x3 determinant calculations in SCILAB.
-->A2x2 = [3, -1; 2, 5]
A2x2 =
! 3. - 1. !
! 2. 5. !
-->det(A2x2)
ans =
17.
-->A3x3 = [2,3, -1
--> 5,5, -2
--> 4, -3, 1]
A3x3 =
! 2. 3. - 1. !
! 5. 5. - 2. !
! 4. - 3. 1. !
-->det(A3x3)
ans =
- 6.
-->det([2,-2; 4, 5])
ans =
18.
__________________________________________________________________________________
). (
33 21 12 31 23 11 31 22 13
32 21 13 31 23 12 33 22 11
33 32 31
23 22 21
13 12 11
a a a a a a a a a
a a a a a a a a a
a a a
a a a
a a a
+ +
+ +
Download at InfoClearinghouse.com 12 2001 - Gilberto E. Urroz
C Cr ro os ss s p pr ro od du uc ct t a as s a a d de et te er rm mi in na an nt t
The cross product AB in Cartesian coordinates can be expressed as a determinant if the first
row of the determinant consists of the unit vectors i, j, and k. The components of vector A
and B constitute the second and third rows of the determinant, i.e.,
Evaluation of this determinant, as indicated above, will produce the result
AB = (B
z
A
y
B
y
A
z
)i + (A
z
B
x
B
z
A
x
)j + (B
y
A
x
A
y
B
x
)k.
SCILAB does not provide a cross-product function of its own. The following function,
CrossProd, calculates the cross product of two three-dimensional vectors:
function [p] = CrossProd(u,v)
//Calculates the cross-product of two vectors u and v.
//Vectors u and v can be columns or row vectors, but
//they must have only three elements each.
[nu,mu] = size(u);
[nv,mv] = size(v);
if nu*mu <> 3 | nv*mv <> 3 then
error('Vectors must be three-dimensional only')
abort;
end
A1 = [ u(2), u(3); v(2), v(3)];
A2 = [ u(3), u(1); v(3), v(1)];
A3 = [ u(1), u(2); v(1), v(2)];
px = det(A1); py = det(A2); pz = det(A3);
p = [px, py, pz]
//end function
An application of the function CrossProd follows:
-->getf('CrossProd')
-->u = [2, 3, -1], v = [-3, 1, 4]
u =
! 2. 3. - 1. !
v =
! - 3. 1. 4. !
-->CrossProd(u,v)
ans =
.
z y x
z y x
B B B
A A A
k j i
B A
Download at InfoClearinghouse.com 13 2001 - Gilberto E. Urroz
! 13. - 5. 11. !
These operations are interpreted as u = 2i+3j-k, v = -3i+j+4k, and uv = 13i+j+4k.
Earlier in this chapter we indicated that the volume of the parallelepiped defined by vectors A,
B, and C, is given by the magnitude of the vector triple product A(BC). As an example in
SCILAB, try the following calculation:
-->A = [2, 2, -1], B = [5, 3, 1], C = [1, 2, 3]
A =
! 2. 2. - 1. !
B =
! 5. 3. 1. !
C =
! 1. 2. 3. !
-->abs(A*CrossProd(B,C)')
ans =
21.
__________________________________________________________________________________
P Po ol ly yn no om mi ia al ls s a as s v ve ec ct to or r c co om mp po on ne en nt ts s
Although SCILAB is not a symbolic environment, some pseudo-symbolic calculations are possible
using polynomials. Polynomials are a special type of SCILAB objects useful in linear system
analysis. They are presented in more detail elsewhere in this book. In this section we present
simple applications of polynomials as vector components.
Simple symbolic variables can be defined using the following call to the SCILAB function poly.
For example, the following statement creates the symbolic variable c:
-->c=poly(0,'c')
c =
c
With the symbolic variable c defined above, you can produce the following vector operations:
-->c*A, c*B, c*(A+B), c*(A-B)
ans =
! 3c 5c - c !
ans =
! 2c 2c 3c !
ans =
! 5c 7c 2c !
ans =
Download at InfoClearinghouse.com 14 2001 - Gilberto E. Urroz
! c 3c - 4c !
It should be kept in mind that these are not symbolic results in the most general sense of the
word (i.e., as those obtained in a symbolic mathematical environment such as Maple or
Mathematica). The results obtained above, in terms of the polynomial c, are three-
dimensional vectors whose components are polynomials. Other examples of vectors with
polynomial components would be:
-->u = [ 1+c, (c+1)^2, c^3+2*c^2 ]
u =
! 2 2 3 !
! 1 + c 1 + 2c + c 2c + c !
-->u+c*A+3*c*B
ans =
! 2 2 3 !
! 1 + 10c 1 + 13c + c 8c + 2c + c !
Operations such as term-by-term multiplication, or linear combinations, are permitted with
vectors whose components are polynomials. For example,
-->A.*u
ans =
! 2 2 3 !
! 3 + 3c 5 + 10c + 5c - 2c - c !
-->u.*B
ans =
! 2 2 3 !
! 2 + 2c 2 + 4c + 2c 6c + 3c !
-->A-B.*u
ans =
! 2 2 3 !
! 1 - 2c 3 - 4c - 2c - 1 - 6c - 3c !
-->u +3*A.*u
ans =
! 2 2 3 !
! 10 + 10c 16 + 32c + 16c - 4c - 2c !
Some operations, such as norm, are not defined when a vector has polynomial components:
-->norm(u)
!--error 4
undefined variable : %p_norm
-->norm(c*A)
!--error 4
undefined variable : %p_norm
Download at InfoClearinghouse.com 15 2001 - Gilberto E. Urroz
Also, in keeping with SCILABs numerical nature, operations that attempt to combine two
polynomial variables (i.e., symbolic operations) are not allowed. For example, the following
command defines a new polynomial symbolic variable r:
-->r=poly(0,'r')
r =
r
The following linear combination of the polynomial variables c and r fails to produce a result:
-->c*A+r*B
!--error 4
undefined variable : %p_a_p
Assignment statements can replace the values of polynomial variables. In the next example,
the value of c, which so far has been used as a polynomial variable, gets redefined as a
constant:
-->c=2
c =
2.
With the new value of c the following operation produces a constant vector:
-->c*A
ans =
! 6. 10. - 2. !
As an application of polynomials as components of vectors, suppose that you want to determine
the value of c such that vector A = 2i+cj-k is normal to vector B = -5i+3j-2k. We will use the
fact that if vectors A and B are perpendicular then their dot product is zero, i.e., AB = 0.
Using SCILAB:
-->c = poly(0,'c')
c =
c
-->A = [2, c, -1], B = [-5, 3, -2]
A =
! 2 c - 1 !
B =
! - 5. 3. - 2. !
-->A*B'
ans =
- 8 + 3c
This translates into the equation -8 + 3c = 0. From which we can find c = 8/3.
__________________________________________________________________________________
Download at InfoClearinghouse.com 16 2001 - Gilberto E. Urroz
A Ap pp pl li ic ca at ti io on ns s o of f v ve ec ct to or r a al lg ge eb br ra a u us si in ng g S SC CI IL LA AB B
In this section we present examples of operations with 2- and 3-dimensional vectors using
SCILAB functions. The examples are taken from applications in different physical sciences.
Example 1 - Position vector
The position vector of a particle is a vector that starts at the origin of a system of coordinates
and ends at the particles position. If the current position of the particle is P(x,y,z), then the
position vector is
r = xi+yj+zk,
as illustrated in the figure below.
If a particle is located at point P(3, -2, 5), the position vector for this particle is r = 3i -2j+5k.
In SCILAB this position vector is written as [3 2 5].
-->r = [3,-2,5]
r =
! 3. - 2. 5. !
To determine the magnitude of the position vector the function norm.
-->abs_r = norm(r)
abs_r =
6.164414
In paper, you can write |r| = 6.164414. To determine the unit vector, use:
Download at InfoClearinghouse.com 17 2001 - Gilberto E. Urroz
-->e_r = r/abs_r
e_r = ! .4866643 - .3244428 .8111071 !
In paper, we can write: e
r
= r/|r| = [0.4866 -0.3244 0.81111].
Example 2 - Center of mass of a system of discrete particles
Consider a system of discrete particles of mass m
i
, located at position P
i
(x
i
, y
i
, z
i
), with i = 1,
2, 3, , n. We can write position vectors for each particle as r
i
= x
i
i+y
i
j+z
i
k. The center of
mass of the system of particles will be located at a position r
cm
defined by
A system of four particles is illustrated in the figure below.
The following table shows the coordinates and masses of 5 particles. Determine their center of
mass.
i x
i
y
i
z
i
m
i
1 2 3 5 12
2 -1 6 4 15
3 3 -1 2 25
4 5 4 -7 10
5 5 3 2 30
.
1
1

n
i
i
n
i
i i
cm
m
m r
r
Download at InfoClearinghouse.com 18 2001 - Gilberto E. Urroz
First, we enter vectors containing the coordinates x
i
, y
i
, and z
i
, as well as the masses m
i
:
-->x = [2,-1,3,5,5]
x =
! 2. - 1. 3. 5. 5. !
-->y = [3,6,-1,4,3]
y =
! 3. 6. - 1. 4. 3. !
-->z = [5,4,2,-7,2]
z =
! 5. 4. 2. - 7. 2. !
-->m = [12,15,25,10,30]
m =
! 12. 15. 25. 10. 30. !
Then, we proceed to calculate the coordinates of the center of mass, (x
cm
, y
cm
, z
cm
) using the
formulas:
. , ,
1
1
1
1
1
1

n
i
i
n
i
i i
cm
n
i
i
n
i
i i
cm
n
i
i
n
i
i i
cm
m
m z
x
m
m y
y
m
m x
x
Using SCILAB, these formulas are calculated using the function sum:
-->x_cm = sum(x.*m)/sum(m)
x_cm =
3.0869565
-->y_cm = sum(y.*m)/sum(m)
y_cm =
2.5108696
-->z_cm = sum(z.*m)/sum(m)
z_cm =
1.7391304
Example 3 - Resultant of forces
Download at InfoClearinghouse.com 19 2001 - Gilberto E. Urroz
The figure below shows a vertical pole buried in the ground and supported by four cables EA,
EB, EC, and ED. The magnitude of the tensions in each of those cables, as shown in the figure,
are T
1
= 150 N, T
2
= 300 N, T
3
= 200 N, and T
4
= 150 N.
To find the vector resultant of all those forces you need to add the four tensions written out as
vectors. The tension in cable i will be given by T
i
= T
i
e
i
, where e
i
is a unit vector in the
direction of the cable where the tension acts. (The tension vectors act so that the cables pull
away from point E, where the cables are attached to the pole.)
Writing out tension T
1
Tension T
1
acts along cable EA. To determine a unit vector along cable EA, you need to write
the vector r
EA
= r
A
r
E
, where r
A
is the position vector of point A (the tip of the vector r
EA
), and
r
A
is the position vector of point A (the origin of the vector r
EA
). The coordinates of these
points, obtained from the figure, are A(-1.0 m, -1.1 m, 0) and E(0, 0, 2.5 m). Therefore, we
can write r
E
= (2.5k) m, and r
A
= (-i 1.1j) m.
Using SCILAB we would calculate the vector r
EA
as follows. First, enter r
E
and r
A
:
--> rE = [ 0 0 2.5]; rA = [-1 -1.1 0];
To obtain, r
EA
= r
A
r
E
, we will use
--> rEA = rA - rE;
Download at InfoClearinghouse.com 20 2001 - Gilberto E. Urroz
rEA = ! -1. -1.1 0. !
i.e., r
EA
= -i 1.1j-2.5k.
To find the unit vector along which T
1
, first we find the magnitude,
--> rEA_abs = norm(rEA)
i.e., (|r
EA
| = 2.9086079).
Finally, the unit vector, is calculated as
--> eEA = rEA/rEA_abs
eEA = ! -0.344 - 0.378 -0.860 !
i.e., e
EA
= r
EA
/|r
EA
| = [-0.344 - 0.378 -0.860] = -0.344i - 0.378j -0.860k. (To verify that this is
indeed a unit vector, use the command --> norm(eEA) . You should get as a result 1.000).
The tension vector is calculated by multiplying the magnitude of the tension T
1
= 150, times
the unit vector e
EA
, i.e.,
--> T1 = 150*eEA
T1 = ! -51.57 -56.73 -128.93 !
The result is the vector [-51.57 -56.73 -128.93], or T
1
= (-51.57i -56.73j -128.93k) N.
Writing the vector that joins two points in space
The vector that joints points A and B, where A(x
A
, y
A
, z
A
) is the origin and point B(x
B
, y
B
, z
B
) the
tip of the vector, is written as
r
AB
= r
B
- r
A
.= (x
B
- x
A
)i+(y
B
- y
A
)j+(z
B
- z
A
)k,
where r
A
= x
A
i+y
A
j+z
A
k is the position vector of point A, and r
B
= x
B
i+y
B
j+z
B
k is the position
vector of point B. This operation is illustrated in the figure below.
Download at InfoClearinghouse.com 21 2001 - Gilberto E. Urroz
Relative position vector
The previous operation can also be interpreted as obtaining the relative position vector of
point B(x
B
, y
B
, z
B
) with respect to point A(x
A
, y
A
, z
A
), and written as
r
B/A
= r
B
- r
A
.= (x
B
- x
A
)i+(y
B
- y
A
)j+(z
B
- z
A
)k.
Writing out tensions T
2
, T
3
, and T
4
Having developed a procedure to determine the unit vector along any of the cables, we can
proceed to write out the vectors representing the tensions T
2
, T
3
, and T
4
. First, we determine
the coordinates of relevant points from the figure describing the problem. These points are:
A(-1.0 m, -1.1m, 0) B(0.8 m, -1.0 m, 0.0), C(-1.5 m, 0.0, 0.5 m), D(1.2 m, 1.3m, 0.0), and
E(0, 0, 2.5 m). The tensions, as vectors, will be written as T
2
= T
2
e
EB
, T
3
= T
3
e
EC
, and T
4
=
T
4
e
ED
. (Recall that T
2
= 300 N, T
3
= 200 N, and T
4
= 150 N.). Thus, we need to write out the
vectors r
EB
, r
EC
, and r
ED
, find their magnitudes, |r
EB
|, |r
EC
|, and |r
ED
|, and calculate the unit
vectors e
EB
= r
EB
/|r
EB
|, e
EC
= r
EC
/|r
EC
|, and e
ED
= r
ED
/|r
ED
|, in order to obtain T
2
, T
3
, and T
4
.
The following SCILAB commands perform the required calculations. First, we enter the position
vectors for points A, B, C, D, and E:
-->rA =[-1, -1.1, 0], rB = [0.8, -1, 0], rC = [-1.5, 0,0.5]
rA =
! - 1. - 1.1 0. !
rB =
! .8 - 1. 0. !
rC =
! - 1.5 0. .5 !
-->rD = [1.2, 1.3, 0], rE = [0, 0, 2.5]
rD =
Download at InfoClearinghouse.com 22 2001 - Gilberto E. Urroz
! 1.2 1.3 0. !
rE =
! 0. 0. 2.5 !
Next, we calculate the relative position vectors, r
EB
, r
EC
, and r
ED
:
-->rEB = rB - rE, rEC = rC - rE, rED = rD - rE
rEB =
! .8 - 1. - 2.5 !
rEC =
! - 1.5 0. - 2. !
rED =
! 1.2 1.3 - 2.5 !
The magnitudes of the vectors, , |r
EB
|, |r
EC
|, and |r
ED
|, are calculated next:
-->abs_rEB = norm(rEB), abs_rEC = norm(rEC), abs_rED= norm(rED)
abs_rEB =
2.8089144
abs_rEC =
2.5
abs_rED =
3.0626786
The unit vectors along the cables, e
EB
= r
EB
/|r
EB
|, e
EC
= r
EC
/|r
EC
|, and e
ED
= r
ED
/|r
ED
|, are
calculated as:
-->eEB = rEB/abs_rEB, eEC = rEC/abs_rEC, eED = rED/abs_rED
eEB =
! .2848075 - .3560094 - .8900236 !
eEC =
! - .6 0. - .8 !
eED =
! .3918139 .4244650 - .8162789 !
Next, we enter the magnitudes of the tensions, T
2
= 300 N, T
3
= 200 N, and T
4
= 150 N. These
are identified in SCILAB as T1m, T2m, and T3m, respectively:
-->T2m = 300, T3m = 200, T4m = 150
T2m =
300.
T3m =
200.
T4m =
150.
Download at InfoClearinghouse.com 23 2001 - Gilberto E. Urroz
The tension vectors, themselves, are calculated by using T
2
= T
2
e
EB
, T
3
= T
3
e
EC
, and T
4
= T
4
e
ED
.
In SCILAB we use:
-->T2 = T2m*eEB, T3 = T3m*eEC, T4 = T4m*eED
T2 =
! 85.442263 - 106.80283 - 267.00707 !
T3 =
! - 120. 0. - 160. !
T4 =
! 58.772083 63.669757 - 122.44184 !
The results are: T
2
= (85.44i -106.80j 267.01k) N, T
3
= (-120i 160k) N, and for cable ED, T
4
=
(58.77i +63.67j 122.44k) N.
The resultant of these four forces, namely, R = T
1
+ T
2
+ T
3
+ T
4
, turns out to be
R = (-27.36i 99.86j 678.38k) N.
Example 4 Equation of a plane in space
The equation of a plane in space, in Cartesian coordinates, can be obtained given a point on
the plane A(x
A
,y
A
,z
A
), and a vector normal to the plane n = n
x
i+n
y
j+n
z
k. Let P(x,y,z) be a
generic point on the plane of interest. We can form a relative position vector r
P/A
= r
P
r
A
=
(x-x
A
)i+(y-y
A
)j+(z-z
A
)k, which is contained in the plane, as shown in the figure below.
Because the vectors n and r
P/A
are perpendicular to each other, then, we can write nr
P/A
= 0,
or
(n
x
i+n
y
j+n
z
k)((x-x
A
)i+(y-y
A
)j+(z-z
A
)k) = 0,
which results in the equation
n
x
(x-x
A
) + n
y
(y-y
A
) + n
z
(z-z
A
) = 0.
To implement a SCILAB function to produce the equation of a plane in space we use the fact
that the equation can be re-written as n
x
x + n
y
y + n
z
z = nr
A
. The function PlaneEquation,
shown below, calculates the right-hand side of the equation, nr
A
, and uses the function string
Download at InfoClearinghouse.com 24 2001 - Gilberto E. Urroz
to convert numerical results to strings. The final result of the function is a string representing
the equation.
function [eq] = PlaneEquation(n,p)
//This function produces the equation of a plane
//in space given the normal vector n and the point p.
[nn,mn] = size(n);
[np,mp] = size(p);
if nn*mn <> 3 | np*mp <> 3 then
error('Vector n or point p in PlaneEquation must have 3 elements')
abort;
end
c = n*p';
eq = string(n(1))+'*x+'+string(n(2))+'*y+'+string(n(3))+'*z='+string(c)
//end function
The function requires as input the normal vector n and the point A (referred to as p in the
function), both entered as SCILAB vectors. For example, to find the equation of the plane with
normal vector n = 3i-5j+6k passing through point A(3, 6, -2) use:
-->getf('PlaneEquation')
-->n = [3, -5, 6], A = [3, 6, -2]
-->myEquation = PlaneEquation(n,A)
myEquation =
3*x+-5*y+6*z=-33
To verify that the equation is satisfied by point A use:
-->x = 3, y = 6, z = -2
x =
3.
y =
6.
z =
- 2.
-->3*x-5*y+6*z
ans =
- 33.
Example 5 Moment of a force
The figure below shows a force F acting on a point P in a rigid body. Suppose that the body is
allowed to rotate about point O. Let r be the position vector of point P with respect to the
Download at InfoClearinghouse.com 25 2001 - Gilberto E. Urroz
point of rotation O, which we make coincide with the origin of our Cartesian coordinate
system. The moment of the force F about point O is defined as M = rF.
Referring to the figure of Example 3, if we want to calculate the moment of tension T
1
= (-
51.57i -56.73j -128.93k) N, about point O, we will use as the position vector r of the forces
line of action through the pole, the vector r
OE
= (2.5k) m. The moment M
1
= r
OE
T
1
, can be
calculated as follows:
-->rOE = [0 0 2.5] Enter r
OE
-->T1 = [-57.51 56.73 128.93] Enter T
1
-->M1 = CrossProd(r0E,T1) Calculate M
1
= r
OE
T
1
The result is [141.825 143.775 0], or M
1
= (141.825 i 143.775 j ) mN.
To find the magnitude of the moment, use [!][ABS], thus, |M
1
| = M
1
= 201.954 mN.
Note: Moments are vector quantities and obey all rules of vectors, i.e., they can be added,
subtracted, multiplied by a scalar, undergo internal and external vector products. As an
exercise, the reader may want to calculate the moments corresponding to the other tensions in
Example 3, as well as the resultant moment from all four tensions.
Example 6 Cartesian and polar representations of vectors in the x-
y plane
A position vector in the x-y plane can be written simply as r = xi+yj. Let its magnitude be r =
|r|. A unit vector along the direction of r is given by e
r
= r/r = (x/r)i+(y/r)j. If we use polar
coordinates, we recognize
x/r = cos , and y/r = sin ,
thus, we can write the unit vector as
e
r
= r/r = cos i+ sin j.
Download at InfoClearinghouse.com 26 2001 - Gilberto E. Urroz
Thus, if we are given the magnitude, r, and the direction, , of a vector (i.e., its polar
coordinates (r,)), we can easily put together the vector as
r = re
r
= r (cos i+ sin j).
This result is illustrated in the figure below.
To enter a vector in SCILAB, given the magnitude r and the angle , you would use, for
example:
-->r = 2.5*[cos(0.75), sin(0.75)]
r =
! 1.8292222 1.7040969 !
In this case the angle represents radians. If you want to use an angle in degrees, you need to
transform it to radians in the trigonometric functions, for example:
-->r = 5.2*[cos(32*%pi/180), sin(32*%pi/180)]
r =
! 4.4098501 2.7555802 !
Example 7 Planar motion of a rigid body
In the study of the planar motion of a rigid body the following equations are used to determine
the velocity and acceleration of a point B (v
B
, a
B
) given the velocity and acceleration of a
reference point A (v
A
, a
A
):
v
B
= v
A
+
AB
r
B/A
,
a
B
= a
A
+
AB
r
B/A
-
AB
2
r
B/A.
Download at InfoClearinghouse.com 27 2001 - Gilberto E. Urroz
The equations also use the angular velocity of the body connecting A and B,
AB
, whose
magnitude is
AB
; the angular acceleration of the body connecting A and B,
AB
, and the
relative position vector of point B with respect to point A, r
B/A
. The following figure illustrates
the calculation of relative velocity and acceleration in planar motion of a rigid body.
To present applications of this equation using the HP 49 G calculator, we use the data from the
mechanism shown in the figure below.
In this mechanism there are two bars AB and AC pin-connected at B. Bar AB is pin supported at
A, and bar BC is attached through a pin to piston C. Piston C is allowed to move in the vertical
direction only. At the instant shown the angular velocity and acceleration of bar AB are 10
rad/s and 20 rad/s
2
clockwise. You are asked to determine the angular velocity and
acceleration of bar BC and the linear velocity and acceleration of piston C.
Download at InfoClearinghouse.com 28 2001 - Gilberto E. Urroz
Angular velocity and acceleration
Angular velocities and accelerations in the x-y plane are represented as vectors in the z-
direction, i.e., normal to the x-y plane. These vectors are positive if the angular velocity or
acceleration is counterclockwise. In general, thus we can write,
AB
= t
AB
k, and
AB
=
t
AB
k. For the data in this problem we can write, therefore,

AB
= (-10k) rad/s,
AB
= (-20k)rad/s
2
,
BC
= (
BC
k) rad/s, and
BC
= (
BC
k).
Using SCILAB, we will write:
-->wAB = [0, 0, -10], alphaAB = [0, 0, -20]
wAB =
! 0. 0. - 10. !
alphaAB =
! 0. 0. - 20. !
The next statements define the polynomial variables wBCm (standing for
BC
) and alBCm
(standing for
BC
). The m in the polynomial variable names stands for magnitude:
-->wBCm = poly(0,'wBCm'), alBCm = poly(0,'alBC')
wBCm =
wBCm
alBCm =
alBC
With the variables wBCm and alBCm we can define the vectors
BC
and
BC
as:
-->wBC = [0, 0, wBCm], alBC = [0, 0, alBCm]
wBC =
! 0 0 wBCm !
alBC =
! 0 0 alBC !
Relative position vector
The relative position vectors of interest in this problem are r
B/A
and r
C/B
, which are obtained as
follows:
To obtain r
B/A
, we use the fact that for the xy coordinate system shown, the angle
corresponding to vector r
B/A
is
B/A
= 90
o
+ 45
o
= 135
o
. Thus, we can write
-->rB_A = 0.25*[cos(135*%pi/180), sin(135*%pi/180), 0]
rB_A =
! - .1767767 .1767767 0. !
In paper, we would write, therefore, r
B/A
= (-0.177i + 0.177j)ft.
For r
C/B
, the angle
C/B
= 90
o
13.6
o
= 76.4
o
. Thus, in SCILAB,
Download at InfoClearinghouse.com 29 2001 - Gilberto E. Urroz
-->rC_B = 0.75*[ cos(76.4*%pi/180), sin(76.4*%pi/180), 0]
rC_B =
! .1763566 .7289708 0. !
In paper, we write, r
C/B
= (0.175i + 0.729j)ft.
Velocity
Since point A is fixed point, v
A
= 0, and we can write v
B
=
AB
r
B/A
= (-10k) rad/s (-0.177i +
0.177j)ft. Using SCILAB, we would write:
-->getf('CrossProd')
-->vA = 0
vA =
0.
-->vB = vA + CrossProd(wAB,rB_A)
vB =
! 1.767767 1.767767 0. !
Thus,
v
B
= (1.77i+1.77j) ft/s.
Note: Radians are basically dimensionless units, thus radft = ft, radm = m.
To find the velocity of point C, we use
v
C
= v
B
+
BC
r
C/B
= (-1.77i-1.77j) ft/s + (
BC
k) rad/s (0.175i + 0.729j)ft.
With SCILAB, this operation is written as:
-->vC = vB + CrossProd(wBC,rC_B)
vC =
! 1.767767 - .7289708wBCm 1.767767 + .1763566wBCm 0 !
The result can be written in paper as:
v
C
= ((1.77-0.729
BC
)i + (1.77+0.176
BC
)j) ft/s.
The figure above shows that the piston C is forced to move in the vertical direction, thus, the
velocity of point C can be written as
v
C
= (v
C
j) ft/s.
Equating the two results presented immediately above for v
C
we get:
(1.77-0.729
BC
)i + (1.77+0.176
BC
) j = 0 i + v
C
j.
Since the x- and y-components of the two vectors in each side of the equal sign must be the
same, we can write the system of equations:
Download at InfoClearinghouse.com 30 2001 - Gilberto E. Urroz
1.77-0.729
BC
= 0
1.77+ 0.176
BC
= vC
S So ol lu ut ti io on n o of f e eq qu ua at ti io on ns s o on ne e a at t a a t ti im me e
Using SCILAB, we can solve for the two unknowns (
BC
and v
C
) as follows:
1. From the first equation,
BC
= 1.77/0.729, i.e.,
-->wBCm = 1.77/0.729
wBCm = 2.4279835
2. From the second equation, vC = 1.77+(0.176)(2.43), i.e.,
-->vCm = 1.77+0.176*2.43
vCm = 2.19768
Thus, the solution of the system of equations is:

BC
= 2.43 rad/s, and v
C
= 2.20 ft/s.
The positive sign in
BC
means that the angular velocity is counterclockwise. The positive sign
in v
C
means that point C is moving upwards in the vertical direction.
Acceleration
Again, because A is a fixed point, a
A
= 0. Thus, the acceleration of point B is given by
a
B
=
AB
r
B/A
-
AB
2
r
B/A
= (-20k)rad/s
2

(-0.177i + 0.177j)ft (10 rad/s)
2
(-0.177i + 0.177j)ft
Using SCILAB we can obtain a
B
as follows:
-->aA = 0
aA =
0.
-->wABm = -10
wABm =
- 10.
-->aB = aA + CrossProd(alphaAB,rB_A)-wABm^2*rB_A
aB =
! 21.213203 - 14.142136 0. !
The result is [21.240 -14.142 0.000], or
a
B
= (21.24i -14.142j)ft/s
2
.
To calculate the acceleration of point C we use:
Download at InfoClearinghouse.com 31 2001 - Gilberto E. Urroz
a
C
= a
B
+
BC
r
C/B
-
BC
2
r
C/B
=
(21.24i -14.16j)ft/s
2

+(
BC
k) (0.175i + 0.729j)ft

-

(-2.42 rad/s)
2
(0.175i + 0.729j)ft
In Scilab:
-->aC = aB + CrossProd(alphaBC,rC_B) - wBCm^2*rC_B
aC =
! 20.173563 - .7289708alBC - 18.439494 + .1763566alBC 0 !
The result is:
a
C
= ((20.17-0.729
BC
)i+(-18.44+0.176
BC
))j) ft/s
2
.
Also, because the motion of point C is in the vertical direction, we can write
a
C
= a
C
j.
Equating the two expressions for the vector aC shown above, we get the following equations:
20.17-0.729
BC
= 0,
-18.44+0.176
BC
= a
C
.
S So ol lu ut ti io on n o of f a a s sy ys st te em m o of f l li in ne ea ar r e eq qu ua at ti io on ns s u us si in ng g m ma at tr ri ic ce es s
The system of linear equations in two unknowns (
BC
and a
C
) obtained above can be re-written
in matricial form as follows:
1. The equations are first re-written as
a
c
- 0.176
BC
= -18.44
0.729
BC
= 20.17
or, as a matrix equation:
2. This matrix equation can be solved by using the backward-slash operator (\), i.e., x = A\b.
This result follows from the original matrix equation, Ax = b, by dividing both sides of the
equation by A. However, since A is a matrix, this division is not a regular arithmetic
division. Typically, this operation would be represented as x = A
-1
b, where A
-1
is the inverse of
matrix A. Modern matrix-based numerical environments, such as SCILAB, provide the user with
the backward-slash operator instead of using the inverse matrix. The result, however, is
exactly the same.
Using SCILAB, we would enter:
-->A = [1, -0.176; 0, 0.729], b = [-18.44;20.17]
A =
! 1. - .176 !
! 0. .729 !
1
]
1

1
]
1

1
]
1


17 . 20
44 . 18
729 . 0 0
176 . 0 1
BC
C
a

Download at InfoClearinghouse.com 32 2001 - Gilberto E. Urroz


b =
! - 18.44 !
! 20.17 !
-->A\b
ans =
! - 13.570425 !
! 27.668038 !
The results are interpreted as
a
C
= -13.57 ft/s
2
,
BC
= 27.67 rad/s
2
.
The negative sign in a
c
indicates that point C is decelerating. The positive sign in
BC
indicates
that bar BC is accelerating angularly in the counterclockwise direction.
We have presented here two methods for solution of systems of linear equations. These
methods, and others, are presented in more detail in a different chapter.
Exercises
For the following exercises use the Cartesian vectors:
u = 3i+2j-5k, v = -i-3k, w = 5i-10j-3k, r = -8i+10j-2.5k, s = -3i-2j-5k, t = 6i-2j+15k,
[1]. Determine the result of the following operations:
(a) |u| (b) |w| (c) |v||r|
(d) |s|/|w| (e) a = u + v (f) b = r-t
(g) c = 3r-2v (h) d = -t + 2s (i) unit vectors: e
u
, e
v
, e
w
(j) angles between vectors u,v; r,s; and v,w. (k) u v
(l) w r (m) st (n) uv
(o) r w (p) u (vw) (q) (st) r
(r) w(ru) (s) (uv)t (t) (uv)(st)
[2]. Four different cables are attached to point E(0,0,0) on a structure. The four cables are
anchored to points A(-1,-1,-1), B(2,3,-5), C(-2,2,4), and D(2,3,-1). The tensions in the four
cables are: AE = 150 lb, BE = 250 lb, CE = 100 lb, DE = 50 lb. Determine the resultant force
from the four cables.
[3]. Determine the torque of the following forces F given the arm r, i.e., M = rF:
(a) F = (3i+2j-5k) N, r = (-2i+5j-3k) m (b) F = (i-4j-3k) lb, r = (i+8j-13k) ft
(a) F = 100(i+j-k) lb, r = (-2i+5j-3k)/2 ft (b) F = 200(i-4j-k) N, r = 3(i+j-10k) m
[4]. Determine the equation of the plane through point A with normal vector n. Sketch the
plane:
(a) A(-2,3,5), n = [2,-2,3] (b) A(0, -1, 2), n = [5,5,-1]
(c) A(2,5,-1), n = [1, 1, 1] (d) A(-1,5,-2), n = [3,3,-1]
Download at InfoClearinghouse.com 33 2001 - Gilberto E. Urroz
[5]. Two vectors n and m are said to be orthogonal if n m = 0. Determine the missing
components in the following vectors m and n so that they are orthogonal:
(a) n = [2, y,-2], m = [5,5,-1] (b) n = [x,5,4], m = [-1,0,2]
(c) n = [4,2,y], m = [3,3,2] (d) n = [5,5,2], m = [x,-2,3]
Note: define the unknown variable as a SCILAB polynomial variable, e.g., for (a):
-->y = poly(0,y)
Then, define the vectors as follows:
--> n = [2,y,-2]; m = [5,5,1];
Calculate the dot product as a polynomial:
--> p = n*m
and solve for y using function roots:
--> roots(p)
[6]. For the mechanism presented in Example 7, if the velocity of point C is v
c
= 1.2 ft/s and
its acceleration is a
c
= -0.2 ft/s
2
, determine the angular velocity and acceleration of bars AB
and BC.
Download at InfoClearinghouse.com 34 2001 - Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 35 2001 - Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Matrices and Linear Algebra with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 - Gilberto E. Urroz
MATRICES AND LINEAR ALGEBRA 3
Definitions 4
Matrices as tensors and the Kroneckers delta function 5
Matrix operations 5
Einsteins summation convention for tensor algebra 7
Addition and subtraction 9
Multiplication by a scalar 13
Matrix multiplication 14
Inverse matrices 16
Verifying properties of inverse matrices 17
Creating identity matrices in SCILAB 19
The Vandermonde matrix 20
The Hilbert matrix 21
Magic squares 22
Symmetric and anti-symmetric matrices 23
Manipulating elements of vectors and matrices 24
Determining the size of vectors and matrices 24
Extracting elements of vectors and matrices 25
Generating vectors and matrices containing random numbers 26
Extracting rows and columns with the colon operator 26
Programming constructs with matrix elements 27
Composing matrices by adding vectors 27
Composing a matrix by adding vectors one at a time 28
Replacing elements of vectors or matrices 30
Sum and product of matrix elements 31
Matrices and solution of linear equation systems 33
Solution to a system of linear equations using linsolve 33
Case 1. A system with the same number of equations and unknowns - unique solution
exists: 33
Case 2. A system with the same number of equations and unknowns - no unique solution
exists: 35
Case 3 - A system with more unknowns than equations 35
Case 4 A system with more equations than unknowns 37
Solution to an under-determined system of linear equations using the least-square method 37
A user-defined function for least-square solution for systems of linear equations 39
Solution of a system of linear equations using the left-division operator 40
Solution using the inverse matrix 41
Characterizing a matrix 42
Matrix decomposition - a brief introduction 42
Solution to a system of linear equations using function lu 43
Singular value decomposition and rank of a matrix 45
The function rank in SCILAB 47
The rank of a square matrix 48
Norms of a matrix or vector 49
The function norm 50
Determinants, singular matrices, and conditions numbers 52
Download at InfoClearinghouse.com 2 2001 - Gilberto E. Urroz
The determinant of a matrix 53
Properties of determinants 54
Cramers rule for solving systems of linear equations 56
The function TRACE 58
Download at InfoClearinghouse.com 3 2001 - Gilberto E. Urroz
Matrices and Linear Algebra
A matrix is a rectangular arrangement of numbers in rows and columns enclosed in brackets.
Examples of matrices follow:
A matrix with one row and three columns (a 13 matrix) is basically the same as a
three-dimensional row vector, e.g.,
[5. 6. 2.]
A matrix with one column and five rows (a 51 matrix) is the same as a column vector
of five elements, e.g.,
The following is a matrix of two rows and four columns (a 24 matrix):
A matrix can have variables and algebraic expressions as their elements, for example:
A matrix can have complex numbers as elements, for example:
1
]
1

1 0 7 11
0 3 2 5
1
1
1
1
1
1
]
1

4
0
5 . 2
3
5
1
]
1

2
2
22
2
11
a x
b a
1
1
1
]
1

+
2 /
2 5
5 2
3 5 1

i
e i
i i
i
Download at InfoClearinghouse.com 4 2001 - Gilberto E. Urroz
D De ef fi in ni it ti io on ns s
The elements of a matrix are referred to by using two sub-indices: the first one
representing the row, and the second one the column where the element is located. A
matrix A with n rows and m columns can be represented by
Thus, a generic element of matrix A belonging in row i and column j will be written as a
i,j
or a
ij
. The matrix A, itself, can be written in a simplified form as
A
nm
= [a
ij
].
A matrix having the same number of rows and columns is called a square matrix. The
following is a 33 square matrix:
The elements of a square matrix with equal sub-indices, i.e., a
11
, a
22
, , a
nn
, belong to
the matrixs main diagonal.
A diagonal matrix is a square matrix having non-zero elements only in the main
diagonal. An example of a 33 diagonal matrix is:
A diagonal matrix whose main diagonal elements are all equal to 1.0 is known as an
identity matrix, because multiplying I by any matrix results in the same matrix, i.e.,
1
1
1
1
1
1
]
1

m n m n n n
m n m n n n
m m
m m
a a a a
a a a a
a a a a
a a a a
, 1 , 2 , 1 ,
, 1 1 , 1 2 , 1 1 , 1
, 2 1 , 2 22 21
, 1 1 , 1 12 11
!
!
" " # " "
!
!
A
1
1
1
]
1

5 . 0 1 . 0 2
8 . 2 9 . 1 3 . 0
0 . 2 2 . 4 5 . 2
1
1
1
]
1

75 . 0 0 0
0 2 . 9 0
0 0 5 . 12
1
1
1
]
1

1 0 0
0 1 0
0 0 1
3 3
I
Download at InfoClearinghouse.com 5 2001 - Gilberto E. Urroz
IA = AI = A.
The identity matrix is typically given the symbol I. Matrix I
3x3
, above, is an identity matrix.
Matrices as tensors and the Kroneckers delta function
A sub-indexed variable, such as those used to identify a matrix, is also referred to as a tensor.
The number of sub-indices determines the order of the tensor. Thus, a vector is a first-order
tensor, and a matrix is a second order tensor. A scalar value is referred to as a zero-th order
tensor.
The Kroneckers delta function,
ij
, is a tensor function defined as
ij
= 1.0, if i = j, and
ij
, = 0,
if i j.
Using the Kroneckers delta function, therefore, an nn identity matrix can be written as
I
nn
= [
ij
].
A tridiagonal matrix is a matrix having non-zero elements in the main diagonal and the
upper and lower diagonals adjacent to the main diagonal. Tridiagonal matrices typically
arise from numerical solution of partial differential equations, and, more often than not,
the terms in the diagonals off the main diagonal are the same. An example of a 55
tridiagonal matrix follows:
M Ma at tr ri ix x o op pe er ra at ti io on ns s
The transpose of a matrix results from exchanging rows for columns and columns for
rows. Therefore, given the matrix A
nm
= [a
ij
], of n rows and m columns, its transpose
matrix is A
T
nm
= [a
T
ij
], of m rows and n columns, such that a
T
ij
= a
ji
, (i = 1,2, , n; j = 1,2,
, m).
Consider the matrices A
nm
= [a
ij
], and B
nm
= [b
ij
], and C
nm
= [c
ij
]. The operations of
addition, subtraction, and multiplication by as scalar, are defined as:
Addition: C
nm
= A
nm
+ B
nm
, implies c
ij
= a
ij
+ b
ij
.
Subtraction: C
nm
= A
nm
+ B
nm
, implies c
ij
= a
ij
+ b
ij
.
1
1
1
1
1
1
]
1

5 0 0 0 0
3 4 3 0 0
0 2 5 . 6 2 0
0 0 2 5 . 3 2
0 0 0 4 5 . 2
Download at InfoClearinghouse.com 6 2001 - Gilberto E. Urroz
Multiplication by a scalar, k: C
nm
= kA
nm
, implies c
ij
= ka
ij
.
Matrix multiplication requires that the number of rows of the first matrix be equal to
the number of columns of the second matrix. In other words, the only matrix
multiplication allowed is such that,
A
nm
B
mp
= C
np
,
with the elements of the matrix product given by
Schematically, the calculation of element cij of a matrix product, C
np
= A
nm
B
mp
, is shown
below:
Thus, element c
ij
of the product results from the summation:
c
ij
= a
i1
b
1j
+ a
i2
b
2j
++ a
ik
b
kj
+ + a
i,m-1
b
m-1,j
+ a
i,m
b
m,j
.
which is the term-by-term multiplication of the elements of row i from A and column j
from B which then are added together.
Note: Matrix multiplication is, in general, non-commutative, i.e., AB BA. In fact, if
one of these products exist, the other may not even be defined. The only case in which
both AB and BA are defined is when both A and B are square matrices of the same order.
__________________________________________________________________________________
). ,..., 2 , 1 ; ,..., 2 , 1 ( ,
1
p j n i b a c
m
k
kj ik ij

Download at InfoClearinghouse.com 7 2001 - Gilberto E. Urroz


Einsteins summation convention for tensor algebra
When developing his general theory of relativity, Albert Einstein was faced with the daunting
task of writing huge amounts of tensor summations. He figured out that he did not need to
write the summation symbol, , with its associated indices, if he used the convention that,
whenever two indices were repeated in an expression, the summation over all possible values
of the repeating index was implicitly expressed.
Thus, the equation for the generic term of a matrix multiplication, expressed above as a
summation, can be simplified to read
c
ij
= a
ik
b
kj
, (i = 1, 2, , n; j = 1, 2, , p).
Because the index k is repeated in the expression, the summation of all the products indicated
by the expression is implicit over the repeating index, k = 1, 2, , m.
The dot or internal product of two vectors of the same dimension (see Chapter 9), a = [a
1
a
2

a
n
] and b = [b
1
b
2
b
n
], can be expressed, using Einsteins summation convention, as
ab = a
i
b
i
, or ab = a
k
b
k
, or even ab = a
r
b
r
,
The repeating index in this, or in the previous, expression is referred to as a dummy index and
can be replaced by any letter, as long as we are aware of the range of values over which the
summation is implicit.
__________________________________________________________________________________
The inverse of a square matrix A
nn
, referred to as A
-1
, is defined in terms of matrix
multiplication and the nn identity matrix, I, as
AA
-1
= A
-1
A = I.
Enter the following matrices and store them in the names suggested. Notice that the names of
the variables correspond to an upper case letter of the alphabet followed by two numbers.
The numbers represent the number of rows and columns that the matrix has. This way we can
purposely select some particular matrices to illustrate matrix operations that are and that are
not allowed. Thus, proceed to store the following variables:
]] 5 [[ 11 ]], 2 [[ 11 ]], 3 [[ 11 C B A
]]. 20 10 [[ 12 ]], 2 3 [[ 12 ]], 6 5 [[ 12 C B A
.
2
2
21 ,
5
3
21 ,
3
7
21
1
]
1

1
]
1

1
]
1

C B A
.
2 8
4 1
22 ,
4 5
2 5
22 ,
6 4
0 3
22
1
]
1

1
]
1

1
]
1

C B A
]]. 10 3 5 [[ 13 ]], 4 3 0 [[ 13 ]], 6 2 1 [[ 13 C B A
Download at InfoClearinghouse.com 8 2001 - Gilberto E. Urroz
To enter these matrices in SCILAB use:
-->A11 = [3]; B11 = [-2]; C11 = [5];
-->A12 = [-5, 6]; B12 = [3, -2]; C12 = [-10, 20];
-->A13 = [1, -2, 6]; B13 = [0, 3, -4]; C13 = [5, 3, -10];
-->A21 = [-7; 3]; B21 = [3; 5]; C21 = [-2; 2];
-->A22 = [-3, 0; 4, -6]; B22 = [5, -2; 5, 4]; C22 = [-1, 4; 8, 2];
-->A23 = [8, 0, -1; 5, -2, 3]; B23 = [1, 0, 1; 0, 1, -1];
-->C23 = [2, -3, -5; 6, 4, -2];
-->A31 = [-10; 2; 5]; B31 = [3; -7; -2]; C31 = [0; 2; 6];
-->A32 = [1, 0; 1, 2; 5, 2]; B32 = [9, 2; 3, 0; 6, -5];
-->C32 = [5, 8; 6, -7; 3, -2];
-->A33 = [2, -1, 5; 0, 2, 1;-7, 2, -5];
-->B33 = [3, 1, 2; 0, 5, 2; -4, 2, 1];
-->C33 = [2, 1, 2; 3, -7, 0; 2, 1, 4];
Note: the use of a semi-colon instead of a comma or blank space suppresses the output in
SCILAB.
.
2 4 6
5 3 2
23 ,
1 1 0
1 0 1
23 ,
3 2 5
1 0 8
23
1
]
1

1
]
1

1
]
1

C B A
.
6
2
0
31 ,
2
7
3
31 ,
5
2
10
31
1
1
1
]
1

1
1
1
]
1


1
1
1
]
1

C B A
.
2 3
7 6
8 5
32 ,
5 6
0 3
2 9
32 ,
2 5
2 1
0 1
32
1
1
1
]
1



1
1
1
]
1

1
1
1
]
1

C B A
.
4 1 2
0 7 3
2 1 2
33 ,
1 2 4
2 5 0
2 1 3
33 ,
5 2 7
1 2 0
5 1 2
33
1
1
1
]
1


1
1
1
]
1

1
1
1
]
1

C B A
Download at InfoClearinghouse.com 9 2001 - Gilberto E. Urroz
Addition and subtraction
Once these variables have been entered, try the following exercises
A Ad dd di it ti io on n a an nd d s su ub bt tr ra ac ct ti io on n o of f 1 1 1 1 m ma at tr ri ic ce es s ( (i i. .e e. ., , s sc ca al la ar rs s) )
-->A11 + B11
ans = 1.
-->A11 + C11
ans = 8.
-->A11 + B11 + C11
ans = 6.
-->A11 - B11
ans = 5.
-->A11 - C11
ans = - 2.
-->B11 - C11
ans = - 7.
-->A11 - (B11 - C11)
ans = 10.
Addition and subtraction of 12 matrices (i.e., two-dimensional row vectors)
-->A12 + B12
ans = ! - 2. 4. !
-->A12 + C12
ans = ! - 15. 26. !
-->A12 + B12 + C12
ans = ! - 12. 24. !
-->A12 - B12
ans = ! - 8. 8. !
-->A12 - C12
ans = ! 5. - 14. !
-->B12 - C12
ans = ! 13. - 22. !
-->A12 - (B12 - C12)
ans = ! - 18. 28. !
Addition and subtraction of 21 matrices (i.e., two dimensional column vectors)
-->A21 + B21 + C21
ans = ! - 6. !
! 10. !
-->A21 - B21
Download at InfoClearinghouse.com 10 2001 - Gilberto E. Urroz
ans = ! - 10. !
! - 2. !
-->A21 - C21
ans = ! - 5. !
! 1. !
-->B21 - C21
ans = ! 5. !
! 3. !
-->A21 - (B21 - C21)
ans = ! - 12. !
! 0. !
Addition and subtraction of 22 matrices
-->A22 + B22
ans = ! 2. - 2. !
! 9. - 2. !
-->A22 + C22
ans = ! - 4. 4. !
! 12. - 4. !
-->A22 + B22 + C22
ans = ! 1. 2. !
! 17. 0. !
-->A22 - B22
ans = ! - 8. 2. !
! - 1. - 10. !
-->A22 - C22
ans = ! - 2. - 4. !
! - 4. - 8. !
-->B22 - C22
ans = ! 6. - 6. !
! - 3. 2. !
-->A22 - (B22 - C22)
ans = ! - 9. 6. !
! 7. - 8. !
Addition and subtraction of 13 matrices
-->A13 + B13
ans = ! 1. 1. 2. !
-->A13 + C13
ans = ! 6. 1. - 4. !
-->A13 + B13 + C13
ans = ! 6. 4. - 8. !
-->A13 - B13
ans = ! 1. - 5. 10. !
Download at InfoClearinghouse.com 11 2001 - Gilberto E. Urroz
-->A13 - C13
ans = ! - 4. - 5. 16. !
-->B13 - C13
ans = ! - 5. 0. 6. !
-->A13 - (B13 - C13)
ans = ! 6. - 2. 0. !
Addition and subtraction of 23 matrices
-->A23 + B23
ans = ! 9. 0. 0. !
! 5. - 1. 2. !
-->A23 + C23
ans = ! 10. - 3. - 6. !
! 11. 2. 1. !
-->A23 + B23 + C23
ans = ! 11. - 3. - 5. !
! 11. 3. 0. !
-->A23 - B23
ans = ! 7. 0. - 2. !
! 5. - 3. 4. !
-->A23 - C23
ans = ! 6. 3. 4. !
! - 1. - 6. 5. !
-->B23 - C23
ans = ! - 1. 3. 6. !
! - 6. - 3. 1. !
-->A23 - (B23 - C23)
ans = ! 9. - 3. - 7. !
! 11. 1. 2. !
Addition and subtraction of 31 matrices
-->A31 + B31
ans = ! - 7. !
! - 5. !
! 3. !
-->A31 + C31
ans = ! - 10. !
! 4. !
! 11. !
-->A31 + B31 + C31
ans = ! - 7. !
! - 3. !
! 9. !
-->A31 - B31
Download at InfoClearinghouse.com 12 2001 - Gilberto E. Urroz
ans = ! - 13. !
! 9. !
! 7. !
-->A31 - C31
ans = ! - 10. !
! 0. !
! - 1. !
-->B31 - C31
ans = ! 3. !
! - 9. !
! - 8. !
-->A31 - (B31 - C31)
ans = ! - 13. !
! 11. !
! 13. !
Addition and subtraction of 32 matrices
-->A32 + B32
ans = ! 10. 2. !
! 4. 2. !
! 11. - 3. !
-->A32 + C32
ans = ! 6. 8. !
! 7. - 5. !
! 8. 0. !
-->A32 + B32 + C32
ans = ! 15. 10. !
! 10. - 5. !
! 14. - 5. !
-->A32 - B32
ans = ! - 8. - 2. !
! - 2. 2. !
! - 1. 7. !
-->A32 - C32
ans = ! - 4. - 8. !
! 2. 4. !
-->B32 - C32
ans = ! 4. - 6. !
! - 3. 7. !
! 3. - 3. !
-->A32 - (B32 - C32)
ans = ! - 3. 6. !
! 4. - 5. !
! 2. 5. !
Addition and subtraction of 33 matrices
-->A33 + B33
ans = ! 5. 0. 7. !
Download at InfoClearinghouse.com 13 2001 - Gilberto E. Urroz
! 0. 7. 3. !
! - 11. 4. - 4. !
-->A33 + C33
ans = ! 4. 0. 7. !
! 3. - 5. 1. !
! - 5. 3. - 1. !
-->A33 + B33 + C33
ans = ! 7. 1. 9. !
! 3. 0. 3. !
! - 9. 5. 0. !
-->A33 - B33
ans = ! - 1. - 2. 3. !
! 0. - 3. - 1. !
! - 3. 0. - 6. !
-->A33 - C33
ans = ! 0. - 2. 3. !
! - 3. 9. 1. !
! - 9. 1. - 9. !
-->B33 - C33
ans = ! 1. 0. 0. !
! - 3. 12. 2. !
! - 6. 1. - 3. !
-->A33 - (B33 - C33)
ans = ! 1. - 1. 5. !
! 3. - 10. - 1. !
! - 1. 1. - 2. !
Notes:
The subtraction A B can be interpreted as A + (-B).
Addition is commutative, i.e., A + B = B + A, and associative, i.e., A+B+C = A+(B+C) = (A+B)+C.
Addition and subtraction can only be performed between matrices of the same dimensions.
Verify this by trying [A23][A21][+]. You will get an error message: <!> + Error: Invalid
Dimension
Multiplication by a scalar
-->2*A11
ans = 6.
-->-3*A12
ans = ! 15. - 18. !
-->-1*A21
ans = ! 7. !
Download at InfoClearinghouse.com 14 2001 - Gilberto E. Urroz
! - 3. !
-->5*A22
ans = ! - 15. 0. !
! 20. - 30. !
-->-2*A13
ans = ! - 2. 4. - 12. !
-->10*A31
ans = ! - 100. !
! 20. !
! 50. !
-->-5*A23
ans = ! - 40. 0. 5. !
! - 25. 10. - 15. !
-->2*A32
ans = ! 2. 0. !
! 2. 4. !
! 10. 4. !
-->1.5*A33
ans = ! 3. - 1.5 7.5 !
! 0. 3. 1.5 !
! - 10.5 3. - 7.5 !
Matrix multiplication
-->A11*B11
ans = - 6.
-->B11*A11
ans = - 6.
-->A12*B21
ans = 15.
-->B21*A12
ans = ! - 15. 18. !
! - 25. 30. !
-->A12*B22
ans = ! 5. 34. !
-->A21*B12
ans = ! - 21. 14. !
! 9. - 6. !
-->B12*A21
ans = - 27.
-->A22*B21
ans = ! - 9. !
! - 18. !
Download at InfoClearinghouse.com 15 2001 - Gilberto E. Urroz
-->A22*B22
ans = ! - 15. 6. !
! - 10. - 32. !
-->B22*A22
ans = ! - 23. 12. !
! 1. - 24. !
-->A13*B31
ans = 5.
-->B31*A13
ans = ! 3. - 6. 18. !
! - 7. 14. - 42. !
! - 2. 4. - 12. !
-->A13*B32
ans = ! 39. - 28. !
-->A11*B11
ans = - 6.
-->A23*B31
ans = ! 26. !
! 23. !
-->A23*B32
ans = ! 66. 21. !
! 57. - 5. !
-->A32*B23
ans = ! 1. 0. 1. !
! 1. 2. - 1. !
! 5. 2. 3. !
-->A23*B33
ans = ! 28. 6. 15. !
! 3. 1. 9. !
-->A33*B31
ans = ! 3. !
! - 16. !
! - 25. !
-->A23*B31
ans = ! 26. !
! 23. !
-->A23*B31
ans = ! 26. !
! 23. !
-->B33*A33
ans = ! - 8. 3. 6. !
! - 14. 14. - 5. !
! - 15. 10. - 23. !
Download at InfoClearinghouse.com 16 2001 - Gilberto E. Urroz
Note: Some of the examples above illustrate the fact that A
mn
B
nm
B
nm
A
mn
. That is,
multiplication, in general, is not commutative.
-->A12* (B21*C12)
ans = ! - 150. 300. !
-->(A12* B21)*C12
ans = ! - 150. 300. !
-->A22* (B21*C12)
ans = ! 90. - 180. !
! 180. - 360. !
-->(A22* B21)*C12
ans = ! 90. - 180. !
! 180. - 360. !
-->A32* (B23*C32)
ans = ! 8. 6. !
! 14. - 4. !
! 46. 20. !
-->(A32* B23)*C32
ans = ! 8. 6. !
! 14. - 4. !
! 46. 20. !
Note: The examples above illustrate the fact that multiplication is associative:
A
mn
B
nm
C
mp
= A
mn
(B
nm
C
mp
) = (A
mn
B
nm
)C
mp
.
Inverse matrices
Inverse matrices exists only for square matrices. In SCILAB, inverse matrices are calculated by
using the function inv. The standard notation for the inverse matrix of A is A
-1
. Try the
following exercises:
-->inv(A11)
ans = .3333333
-->inv(B11)
ans = - .5
-->inv(C11)
ans = .2
-->inv(A22)
ans = ! - .3333333 0. !
! - .2222222 - .1666667 !
-->inv(B22)
ans = ! .1333333 .0666667 !
! - .1666667 .1666667 !
-->inv(C22)
ans = ! - .0588235 .1176471 !
! .2352941 .0294118 !
Download at InfoClearinghouse.com 17 2001 - Gilberto E. Urroz
-->inv(A33)
ans = ! - .2264151 .0943396 - .2075472 !
! - .1320755 .4716981 - .0377358 !
! .2641509 .0566038 .0754717 !
-->inv(B33)
ans = ! .0285714 .0857143 - .2285714 !
! - .2285714 .3142857 - .1714286 !
! .5714286 - .2857143 .4285714 !
-->inv(C33)
ans = ! .8235294 .0588235 - .4117647 !
! .3529412 - .1176471 - .1764706 !
! - .5 0. .5 !
Note: Some matrices has no inverse, for example, matrices with one row or column whose
elements are all zeroes, or a matrix with a row or column being proportional to another row or
column, respectively. These are called singular matrices.
Try the following examples:
-->A = [ 1 2 3; 0 0 0; -2 5 2], inv(A)
A = ! 1. 2. 3. !
! 0. 0. 0. !
! - 2. 5. 2. !
A = [ 1 2 3; 0 0 0; -2 5 2], inv(A)
!--error 19
singular matrix
-->B = [ 1 2 3; 2 4 6; 5 2 -1], inv(B)
A = ! 1. 2. 3. !
! 2. 4. 6. !
! - 2. 5. 2. !
B = [ 1 2 3; 2 4 6; 5 2 -1], inv(B)
!--error 19
singular matrix
Verifying properties of inverse matrices
-->inv(A22)*A22
ans =
! 1. 0. !
! 0. 1. !
-->A22*inv(A22)
ans =
! 1. 0. !
! 0. 1. !
-->inv(B22)*B22
ans =
! 1. 0. !
! 0. 1. !
-->B22*inv(B22)
ans =
Download at InfoClearinghouse.com 18 2001 - Gilberto E. Urroz
! 1. 0. !
! 0. 1. !
-->inv(C22)*C22
ans =
! 1. 0. !
! 0. 1. !
-->C22*inv(C22)
ans =
! 1. 0. !
! 0. 1. !
-->inv(A33)*A33
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
-->A33*inv(A33)
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! - 2.220E-16 0. 1. !
-->inv(B33)*B33
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
-->B33*inv(B33)
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
-->inv(C33)*C33
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
-->C33*inv(C33)
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
Download at InfoClearinghouse.com 19 2001 - Gilberto E. Urroz
The last set of exercises verify the properties of inverse matrices, i.e., A A
-1
= A
-1
A = I, where I
is the identity matrix.
Creating identity matrices in SCILAB
To obtain a nxn identity matrix in SCILAB use the function eye, e.g.,
-->I22 = eye(2,2)
I22 =
! 1. 0. !
! 0. 1. !
-->I33 = eye(3,3)
I33 =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
If a square matrix A is already defined, you can obtain an identity matrix of the same
dimensions by using eye(A), for example:
-->A = [2, 3, 4, 2; -1, 2, 3, 1; 5, 4, 2, -2; 1, 1, 0, 1]
A =
! 2. 3. 4. 2. !
! - 1. 2. 3. 1. !
! 5. 4. 2. - 2. !
! 1. 1. 0. 1. !
-->eye(A)
ans =
! 1. 0. 0. 0. !
! 0. 1. 0. 0. !
! 0. 0. 1. 0. !
! 0. 0. 0. 1. !
The eye function call A = eye(n,m) produces a matrix whose terms a
ii
= 1, all other terms being
zero. For example,
-->A = eye(3,2)
A =
! 1. 0. !
! 0. 1. !
! 0. 0. !
-->A = eye(2,3)
A =
! 1. 0. 0. !
! 0. 1. 0. !
Download at InfoClearinghouse.com 20 2001 - Gilberto E. Urroz
Note: the name of the function eye comes from the name of the letter I typically used to refer
to the identity matrix.
__________________________________________________________________________________
The Vandermonde matrix
A Vandermonde matrix of dimension n is based on a given vector of size n. The Vandermonde
matrix corresponding to the vector [x
1
x
2
x
n
], is given by:
_ _
1 x
1
x
1
2
x
1
3
x
1
n-1
1 x
2
x
2
2
x
2
3
x
2
n-1
1 x
3
x
3
2
x
3
3
x
3
n-1
. . . . .
. . . . . .
1 x
n
x
n
2
x
n
3
x
n
n-1
_ _
To produce a Vandermonde matrix given an input vector x, we can use the following function
vandermonde:
function [v] = vandermonde(x)
//Produces the Vandermonde matrix based on vector x.
//If vector x has the components [x(1) x(2) ... x(n)],
//the corresponding Vandermonde matrix is written as:
// [ 1 x(1) x(1)^2 x(1)^3 ... x(1)^(n-1) ]
// [ 1 x(2) x(2)^2 x(2)^3 ... x(2)^(n-1) ]
// [ . . . . ... . ]
// [ . . . . ... . ]
// [ 1 x(n) x(n)^2 x(n)^3 ... x(n)^(n-1) ]
//
[nx,mx] = size(x);
if nx<>1 & mx<>1 then
error('Function vandermonde - input must be a vector');
abort;
end;
if nx == 1 then
u = x';
n = mx;
else
u = x;
n = nx;
end;
// u = column vector = [x(1) x(2) ... x(n)]'
// n = number of elements in u
// v = first column of Vandermonde matrix (all ones)
v = ones(n,1);
Download at InfoClearinghouse.com 21 2001 - Gilberto E. Urroz
for k = 1:(n-1)
v = [v,u^k];
end;
//end function
For example, the Vandermonde matrix corresponding to the vector x = [2, -3, 5, -2], is
calculated as follows:
-->x = [2, -3, 5, -2]
x =
! 2. - 3. 5. - 2. !
-->Vx = vandermonde(x)
Vx =
! 1. 2. 4. 8. !
! 1. - 3. 9. - 27. !
! 1. 5. 25. 125. !
! 1. - 2. 4. - 8. !
So, you wonder, what is the utility of the Vandermonde matrix? One practical application,
which we will be utilizing in a future chapter, is the fact that the Vandermonde matrix
corresponding to a vector [x
1
x
2
x
n
], which is in turn associated to a vector [y
1
y
2
y
n
], can
be used to determine a polynomial fitting of the form
y = b
0
+ b
1
x + b
2
x
2
++ b
n-1
x
n-1
,
to the data sets (x,y).
The Hilbert matrix
The nn Hilbert matrix is defined as H
n
= [h
jk
], so that
The Hilbert matrix has application in numerical curve fitting by the method of linear squares.
In SCILAB, a Hilbert matrix can be generated by first using the function testmatrix with the
option hilb and the size n. For example, for n=5, we have:
-->H_inv = testmatrix('hilb',5)
H_inv =
! 25. - 300. 1050. - 1400. 630. !
! - 300. 4800. - 18900. 26880. - 12600. !
! 1050. - 18900. 79380. - 117600. 56700. !
! - 1400. 26880. - 117600. 179200. - 88200. !
! 630. - 12600. 56700. - 88200. 44100. !
This function call produces the inverse of the Hilbert matrix. Use the function inv to obtain
the actual Hilbert matrix, i.e.,
.
1
1
+

k j
h
jk
Download at InfoClearinghouse.com 22 2001 - Gilberto E. Urroz
-->H = inv(H_inv)
H =
! 1. .5 .3333333 .25 .2 !
! .5 .3333333 .25 .2 .1666667 !
! .3333333 .25 .2 .1666667 .1428571 !
! .25 .2 .1666667 .1428571 .125 !
! .2 .1666667 .1428571 .125 .1111111 !
Magic squares
The function testmatrix can also produce matrices corresponding to magic squares, i.e.,
matrices such that the sums of its rows and columns, as well as its diagonals, produce the same
result. For example, a 3x3 magic square is obtained from:
-->A = testmatrix('magic',3)
A =
! 8. 1. 6. !
! 3. 5. 7. !
! 4. 9. 2. !
The sum of elements in each column of the magic square is calculated as follows:
-->sum(A(:,1))
ans =
15.
-->sum(A(:,2))
ans =
15.
-->sum(A(:,3))
ans =
15.
The sums corresponding to the rows of the magic square are:
-->sum(A(1,:))
ans =
15.
-->sum(A(2,:))
ans =
15.
-->sum(A(3,:))
ans =
15.
Download at InfoClearinghouse.com 23 2001 - Gilberto E. Urroz
The sums of the elements in the main diagonal is calculated using the function trace:
-->trace(A)
ans =
15.
Finally, the sum of the elements in the second main diagonal of the matrix is:
-->A(3,1)+A(2,2)+A(1,3)
ans =
15.
Thus, we have verified that matrix A is indeed a magic square.
__________________________________________________________________________________
S Sy ym mm me et tr ri ic c a an nd d a an nt ti i- -s sy ym mm me et tr ri ic c m ma at tr ri ic ce es s
A square matrix, A
nn
= [a
ij
], is said to be symmetric if a
ij
= a
ji
, for i j, i.e., A
T
= A. Also, a
square matrix, A
nn
= [a
ij
], is said to be anti-symmetric if a
ij
= -a
ji
, for i j, i.e., A
T
= -A.
The matrix A below is symmetric, while the matrix C below is anti-symmetric:
Any square matrix, B
nn
= [b
ij
] can be written as the sum of a symmetric B
nn
= [b
ij
] and an
anti-symmetric B
nn
= [b
ij
] matrices. Because
b
ij
= (b
ij
+ b
ji
) + (b
ij
- b
ji
),
we can write
b
ij
= (b
ij
+ b
ji
), and b
ij
= (b
ij
- b
ji
).
Therefore,
B
nn
= (B
nn
+ B
T
nn
), and B
nn
= (B
nn
- B
T
nn
),
where B
T
nn
= [b
T
ij
] = [b
ji
] is the transpose of matrix B.
For example, take the matrix
1
1
1
]
1

10 3 6
2 4 5
3 1 2
B
1
1
1
]
1


1
1
1
]
1

5 4 . 2 15
4 . 2 2 3 . 1
15 3 . 1 12
,
2 4 5
4 6 3
5 3 2
C A
Download at InfoClearinghouse.com 24 2001 - Gilberto E. Urroz
defined earlier, and use the following SCILAB commands to find its symmetric and anti-
symmetric components:
-->B = [2, -1, 3; 5, 4, -2; 6, 3, -10];
The symmetric component:
-->B_sym = (B+B')/2
B_sym =
! 2. 2. 4.5 !
! 2. 4. .5 !
! 4.5 .5 - 10. !
The anti-symmetric component is:
-->B_anti = (B-B')/2
B_anti =
! 0. - 3. - 1.5 !
! 3. 0. - 2.5 !
! 1.5 2.5 0. !
To verify that they add back to matrix B use:
-->B_sym + B_anti
ans =
! 2. - 1. 3. !
! 5. 4. - 2. !
! 6. 3. - 10. !
This decomposition of a square matrix into its symmetric and anti-symmetric parts is commonly
used in continuous mechanics to determine such effects as normal and shear strains or strain
rates and rotational components.
M Ma an ni ip pu ul la at ti in ng g e el le em me en nt ts s o of f v ve ec ct to or rs s a an nd d m ma at tr ri ic ce es s
Vectors and matrices are the basic data structures used in SCILAB. A vector or matrix is
defined by simply assigning an array of values to a variable name. Many example defining
vectors or matrices have been presented previously.
Determining the size of vectors and matrices
The function size is used to determine the number of rows and columns of a vector or matrix.
Some examples of the use of the function size are presented next:
-->rv = [1, 3, 5, 7, 9, 11] //A row vector
rv =
Download at InfoClearinghouse.com 25 2001 - Gilberto E. Urroz
! 1. 3. 5. 7. 9. 11. !
-->cv = [2; 4; 6; 8; 10; 12] //A column vector
cv =
! 2. !
! 4. !
! 6. !
! 8. !
! 10. !
! 12. !
-->size(rv)
ans =
! 1. 6. ! // 1 row x 6 columns (1x6)
-->size(cv)
ans =
! 6. 1. // 6 rows x 1 column (6x1)
-->M = [2,3;-2,1;5,4;1,3] // A matrix
M =
! 2. 3. !
! - 2. 1. !
! 5. 4. !
! 1. 3. !
-->size(M)
ans =
! 4. 2. ! //4 rows x 2 columns (4x2)
Extracting elements of vectors and matrices
Elements of vectors and matrices can be extracted and manipulated numerically by referring to
them with the vector or matrix name with an appropriate set of indices (or sub-indices). For
example,
-->u = [1:1:6], v = [10:-2:0]
u =
! 1. 2. 3. 4. 5. 6. !
v =
! 10. 8. 6. 4. 2. 0. !
-->u(1) + v(2), v(5)/u(2)
ans = 9.
ans = 1.
Download at InfoClearinghouse.com 26 2001 - Gilberto E. Urroz
Generating vectors and matrices containing random numbers
The function rand can be used to generate a matrix whose elements are random numbers
uniformly distributed in the interval (0,1). For example, to generate a matrix of random
numbers with 3 rows and 5 columns, use:
-->m = rand(3,5)
m =
! .2113249 .3303271 .8497452 .0683740 .7263507 !
! .7560439 .6653811 .6857310 .5608486 .1985144 !
! .0002211 .6283918 .8782165 .6623569 .5442573 !
If you want to generate a matrix of random integer numbers between 0 and 10, combine
function rand with function int as follows:
-->A = int(10*rand(4,2))
A =
! 2. 6. !
! 2. 3. !
! 2. 9. !
! 8. 2. !
Numerical operations involving elements of matrix A and of vector b are shown next:
-->A(2,1) + v(2), A(3,2)
ans =
10.
ans =
9.
Extracting rows and columns with the colon operator
The colon operator (:) can be used to extract rows or columns of a matrix. For example, the
next two SCILAB expressions, with the colon operator as the first sub-index, extract the first
and second columns from matrix A:
-->A(:,1), A(:,2)
ans =
! 2. !
! 2. !
Download at InfoClearinghouse.com 27 2001 - Gilberto E. Urroz
! 2. !
! 8. !
ans =
! 6. !
! 3. !
! 9. !
! 2. !
The next two statements, with the colon operator as the second sub-index, extract the first
and second rows from matrix A:
-->A(1,:), A(2,:)
ans =
! 2. 6. !
ans =
! 2. 3. !
Programming constructs with matrix elements
Programming constructs can be used to manipulate elements of a vector or matrix by using
appropriate values of the sub-indices. As an example, the following commands create a 4x3
matrix A such that its elements are defined by a
ij
= i+j:
-->for i = 1:4, for j = 1:3, A(i,j) = i+j; end, end
-->A
A =
! 2. 3. 4. !
! 3. 4. 5. !
! 4. 5. 6. !
! 5. 6. 7. !
Composing matrices by adding vectors
The next examples show how to create matrices by adding vectors to it as new columns or
rows. We start by defining the 1x3 row vectors u1, u2, and u3:
-->u1 = [2,3,-5]; u2 = [7, -2, 1]; u3 = [2, 2, -1];
A 1x9 row vector composed of the elements of u1, u2, and u3 is put together by using:
-->[u1, u2, u3]
ans =
! 2. 3. - 5. 7. - 2. 1. 2. 2. - 1. !
If the vector names are separated by semi-colons, the vectors are added as rows:
-->[u1; u2; u3]
Download at InfoClearinghouse.com 28 2001 - Gilberto E. Urroz
ans =
! 2. 3. - 5. !
! 7. - 2. 1. !
! 2. 2. - 1. !
The transpose vectors u1, u2, and u3 are column vectors. Therefore, the matrix that
results from listing the vector names u1, u2, u3 is a 3x3 matrix whose columns are the
vectors:
-->[u1' u2' u3']
ans =
! 2. 7. 2. !
! 3. - 2. 2. !
! - 5. 1. - 1. !
If semi-colons are placed between the column vectors the result is a 9x1 column vector:
-->[u1';u2';u3']
ans =
! 2. !
! 3. !
! - 5. !
! 7. !
! - 2. !
! 1. !
! 2. !
! 2. !
! - 1. !
Composing a matrix by adding vectors one at a time
The following exercises show how to build matrices by adding vectors one at a time. This
procedure may be useful in programming when one needs to build a matrix out of a number of
vectors. First, we define an empty matrix, by using B = [], and then the row vectors are added
one at a time:
-->B = []; B = [B u1]
B =
! 2. 3. - 5. !
-->B = [B;u2]
B =
! 2. 3. - 5. !
! 7. - 2. 1. !
-->B = [B; u3]
B =
! 2. 3. - 5. !
! 7. - 2. 1. !
! 2. 2. - 1. !
Download at InfoClearinghouse.com 29 2001 - Gilberto E. Urroz
In the next example, the row vectors are added one at a time to form a single row vector:
-->C = []; C = [C u1]
C =
! 2. 3. - 5. !
-->C = [C u2]
C =
! 2. 3. - 5. 7. - 2. 1. !
-->C = [C u3]
C =
! 2. 3. - 5. 7. - 2. 1. 2. 2. - 1. !
In the following example, column vectors are added one at a time:
-->D = []; D = [D u1']
D =
! 2. !
! 3. !
! - 5. !
-->D = [D u2']
D =
! 2. 7. !
! 3. - 2. !
! - 5. 1. !
-->D = [D u3']
D =
! 2. 7. 2. !
! 3. - 2. 2. !
! - 5. 1. - 1. !
Finally, to construct a single column vector out of three column vectors, the composing column
vectors are added one at a time:
-->E = []; E = [E; u1']
E =
! 2. !
! 3. !
! - 5. !
-->E = [E; u2']
E =
! 2. !
! 3. !
Download at InfoClearinghouse.com 30 2001 - Gilberto E. Urroz
! - 5. !
! 7. !
! - 2. !
! 1. !
-->E = [E; u3']
E =
! 2. !
! 3. !
! - 5. !
! 7. !
! - 2. !
! 1. !
! 2. !
! 2. !
! - 1. !
Replacing elements of vectors or matrices
Individual elements in a vector or matrix can be replaced through assignment statements, for
example:
-->B
B =
! 2. 3. - 5. !
! 7. - 2. 1. !
! 2. 2. - 1. !
-->B(2,1) = 0
B =
! 2. 3. - 5. !
! 0. - 2. 1. !
! 2. 2. - 1. !
-->B(3,2) = -10
B =
! 2. 3. - 5. !
! 0. - 2. 1. !
! 2. - 10. - 1. !
Notice that after each assignment statement the entire matrix is printed.
If need be to replace entire rows or columns in a matrix by a vector of the proper length, you
can build the new matrix as illustrated in the examples below. In the first example, vector u1
replaces the second row of matrix B:
Download at InfoClearinghouse.com 31 2001 - Gilberto E. Urroz
-->u1
u1 =
! 2. 3. - 5. !
-->[B(1,:);u1;B(3,:)]
ans =
! 2. 3. - 5. !
! 2. 3. - 5. !
! 2. - 10. - 1. !
In this second example we reconstruct the original matrix B and then replace its first row with
vector u2:
-->B = [u1;u2;u3]
B =
! 2. 3. - 5. !
! 7. - 2. 1. !
! 2. 2. - 1. !
--> [u2; B(2,:); B(3,:)]
ans =
! 7. - 2. 1. !
! 7. - 2. 1. !
! 2. 2. - 1. !
Sum and product of matrix elements
SCILAB provides the functions sum and prod to calculate the sum and products of all elements
of a vector or matrix. In the following examples we use matrix B, defined above:
-->sum(B)
ans =
9.
-->prod(B)
ans =
-1680.
To calculate the sum or product of rows of a matrix use the qualifier r in functions sum and
prod (i.e., adding or multiplying by rows):
-->sum(B,'r')
ans =
! 11. 3. - 5. !
Download at InfoClearinghouse.com 32 2001 - Gilberto E. Urroz
-->prod(B,'r')
ans =
! 28. - 12. 5. !
To obtain the sum or product of columns of a matrix use the qualifier c in sum and prod (i.e.,
adding or multiplying by columns):
-->sum(B,'c')
ans =
! 0. !
! 6. !
! 3. !
-->prod(B,'c')
ans =
! - 30. !
! - 14. !
! - 4. !
Download at InfoClearinghouse.com 33 2001 - Gilberto E. Urroz
M Ma at tr ri ic ce es s a an nd d s so ol lu ut ti io on n o of f l li in ne ea ar r e eq qu ua at ti io on n s sy ys st te em ms s
A system of n linear equations in m variables can be written as
a
11
x
1
+ a
12
x
2
+ a
13
x
3
+ + a
1,m-1
x
m-1
+ a
1,m
x
m
= b
1
,
a
21
x
1
+ a
22
x
2
+ a
23
x
3
+ + a
2,m-1
x
m-1
+ a
2,m
x
m
= b
2
,
a
31
x
1
+ a
32
x
2
+ a
33
x
3
+ + a
3,m-1
x
m-1
+ a
3,m
x
m
= b
3
,
. . . . . .
. . . . . .
a
n-1,1
x
1
+ a
n-1,2
x
2
+ a
n-1,3
x
3
+ + a
n-1,m-1
x
m-1
+ a
n-1,m
x
m
= b
n-1
,
a
n1
x
1
+ a
n2
x
2
+ a
n3
x
3
+ + a
n,m-1
x
m-1
+ a
n,m
x
m
= b
n
.
This system of linear equations can be written as a matrix equation,
A
nm
x
m1
= b
n1
,
if we define the following matrices:
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

n m nm n n
m
m
b
b
b
b
x
x
x
x
a a a
a a a
a a a
A
" "
!
" # " "
!
!
2
1
2
1
2 1
2 22 21
1 12 11
, ,
Using the matricial equation, Ax = b, we can solve for x by using the function linsolve. The
function requires as arguments the matrices A and b. Some examples are shown below:
Solution to a system of linear equations using linsolve
The function linsolve, rather than solving the matricial equation described above, namely, Ax
= b , solves the equation Ax + c = 0, where c = -b. The general form of the function call is
[x0,nsA]=linsolve(A,c [,x0])
where A is a matrix with n rows and m columns, c is a n1 vector, x0 is a vector (a particular
solution of the system), and nsA is an mk matrix known as the null space of matrix A.
Depending on the number of unknowns and equations (i.e., n and m), linsolve produces
different results. Some examples are shown following:
C Ca as se e 1 1. . A A s sy ys st te em m w wi it th h t th he e s sa am me e n nu um mb be er r o of f e eq qu ua at ti io on ns s a an nd d u un nk kn no ow wn ns s - - u un ni iq qu ue e s so ol lu ut ti io on n e ex xi is st ts s: :
Download at InfoClearinghouse.com 34 2001 - Gilberto E. Urroz
If the number of unknowns is the same as the number of equations, the solution may be
unique, in which case the null space returned by linsolve is empty. Consider the following
case involving three equations with three unknowns:
2x
1
+ 3x
2
5x
3
- 13 = 0,
x
1
3x
2
+ 8x
3
+ 13 = 0,
2x
1
2x
2
+ 4x
3
+ 6 = 0.
can be written as the matrix equation Ax + c = 0, if
If a unique solution exists it represents the point of intersection of the three planes in the
coordinate system (x
1
, x
2
, x
3
). Using function linsolve to obtain a solution we write:
-->A = [2, 3, -5; 1, -3, 8; 2, -2, 4], c = [-13; 13; 6]
A =
! 2. 3. - 5. !
! 1. - 3. 8. !
! 2. - 2. 4. !
c =
! - 13. !
! 13. !
! 6. !
-->[x0,nsA] = linsolve(A,c)
nsA =
[]
x0 =
! 1. !
! 2. !
! - 1. !
The null space returned for A is empty, indicating a unique solution. To verify that the solution
satisfies the system of equations we can use:
--> A*x0+c
ans =
1.0E-14 *
! .1776357 !
! 0. !
! 0. !
Although the resulting vector, A*x0+c, is not exactly zero, it is small enough (in the order of
10
-14
) to indicate that the solution satisfies the linear system.
.
6
13
13
, ,
4 2 2
8 3 1
5 3 2
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

c x A and
x
x
x
Download at InfoClearinghouse.com 35 2001 - Gilberto E. Urroz
C Ca as se e 2 2. . A A s sy ys st te em m w wi it th h t th he e s sa am me e n nu um mb be er r o of f e eq qu ua at ti io on ns s a an nd d u un nk kn no ow wn ns s - - n no o u un ni iq qu ue e s so ol lu ut ti io on n e ex xi is st ts s: :
A system of linear equations with the same number of equations and unknowns not always has
a unique solution, as illustrated by the following system:
2x
1
+ 3x
2
5x
3
- 13 = 0,
2x
1
+ 3x
2
-5x
3
+ 13 = 0,
2x
1
2x
2
+ 4x
3
+ 6 = 0.
The system of linear equations can be written as the matrix equation Ax + c = 0, if
Attempting a solution with linsolve in SCILAB produces the following:
-->A = [2, 3, -5; 2, 3, -5; 2, -2, 4], c = [-13, 13, 6]
A =
! 2. 3. - 5. !
! 2. 3. - 5. !
! 2. - 2. 4. !
c =
! - 13. 13. 6. !
-->[x0,nsA] = linsolve(A,c)
WARNING:Conflicting linear constraints!
nsA =
[ ]
x0 =
[ ]
The function linsolve fails to produce a solution because the first two equations represent
parallel planes in Cartesian coordinates. This failure is reported as a warning indicating
conflicting linear constraints.
C Ca as se e 3 3 - - A A s sy ys st te em m w wi it th h m mo or re e u un nk kn no ow wn ns s t th ha an n e eq qu ua at ti io on ns s
The system of linear equations
2x
1
+ 3x
2
5x
3
+ 10 = 0,
x
1
3x
2
+ 8x
3
- 85 = 0,
can be written as the matrix equation Ax + c = 0, if
.
6
13
13
, ,
4 2 2
5 3 2
5 3 2
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

c x A and
x
x
x
.
85
10
, ,
8 3 1
5 3 2
3
2
1
1
]
1

1
1
1
]
1

1
]
1

c x A and
x
x
x
Download at InfoClearinghouse.com 36 2001 - Gilberto E. Urroz
This system has more unknowns than equations, therefore, it is not uniquely determined. Such
a system is referred to as an over-determined system.
We can visualize the linear system by realizing that each of the linear equations represents a
plane in the three-dimensional Cartesian coordinate system (x
1
, x
2
, x
3
). The solution to the
system of equations shown above will be the intersection of two planes in space. We know,
however, that the intersection of two (non-parallel) planes is a straight line, and not a single
point. Therefore, there is more than one point that satisfies the system. In that sense, the
system is not uniquely determined.
Applying the function linsolve to this system results in:
-->A = [2, 3, -5; 1, -3, 8], c = [10; -85]
A =
! 2. 3. - 5. !
! 1. - 3. 8. !
c =
! 10. !
! - 85. !
-->[x0,nsA] = linsolve(A,c)
nsA =
! - .3665083 !
! .8551861 !
! .3665083 !
x0 =
! 15.373134 !
! 2.4626866 !
! 9.6268657 !
In this case, linsolve returns values for both x0, the particular solution, and for nsA, the null
space of A. There is no unique solution to the equation system, instead, a multiplicity of
solutions exist given by x = x0 + nsA*t, where t can take any real value. For example, for t =
10, we calculate a solution that we call x1:
-->x1 = x + nsA*10
x1 =
! 11.708051 !
! 11.014548 !
! 13.291949 !
To check whether this solution satisfies the system of equation we calculate:
-->A*x1+c
ans =
1.0E-12 *
! - .0426326 !
! - .1989520 !
Download at InfoClearinghouse.com 37 2001 - Gilberto E. Urroz
The result, containing values in the order of 10
-12
, is practically zero.
C Ca as se e 4 4 A A s sy ys st te em m w wi it th h m mo or re e e eq qu ua at ti io on ns s t th ha an n u un nk kn no ow wn ns s
The system of linear equations
x
1
+ 3x
2
- 15 = 0,
2x
1
5x
2
- 5 = 0,
-x
1
+ x
2
- 22 = 0,
can be written as the matrix equation Ax + c = 0, if
This system has more equations than unknowns, and is referred to as an under-determined
system. The system does not have a single solution. Each of the linear equations in the
system presented above represents a straight line in a two-dimensional Cartesian coordinate
system (x
1
, x
2
). Unless two of the three equations in the system represent the same equation,
the three lines will have three different intersection points. For that reason, the solution is
not unique.
An attempt to solve this system of linear equations with function linsolve produces no result:
-->A = [1, 3; 2, -5; -1, 1], c = [-15; -5; -22]
A =
! 1. 3. !
! 2. - 5. !
! - 1. 1. !
c =
! - 15. !
! - 5. !
! - 22. !
-->[x0,nsA] = linsolve(A,c)
WARNING:Conflicting linear constraints!
nsA = []
x0 = []
Solution to an under-determined system of linear equations using
the least-square method
The method of least squares can be used to force a solution to a system of linear equations
by minimizing the sum of the squares of the distances from the presumptive solution point to
each of the lines represented by the linear equations. For the case of a system of linear
equations involving two unknowns only, the equations represent actual geometrical lines in the
.
22
5
15
, ,
1 1
5 2
3 1
2
1
1
1
1
]
1

1
]
1

1
1
1
]
1

c x A and
x
x
Download at InfoClearinghouse.com 38 2001 - Gilberto E. Urroz
plane. Thus, the solution point can be visualized as that point such that the sum of the
squares of the distances from the point perpendicular to each line is a minimum.
SCILAB provides the function leastsqr to obtain the parameters that minimize the sum of
squares of the residuals of a function given a set of reference data. The residuals of a least-
square problem are the differences between the reference data and the data reproduced by
the function under consideration.
For the case of a system of linear equations the function to be minimized by the least-square
method is
f(x) = Ax + c,
where A and c describe the system Ax + c = 0. The parameters of the function sought in
this case are the elements of the least-square solution x. As it will be explained in more
detail in a subsequent chapter, the method of least squares requires taking the derivatives of
the function to be solved for with respect to the parameters sought. For this case, the
derivative of interest is
g(x) = f(x) = A.
This derivative is referred to as the gradient of the function.
A general call to function leastsqr in SCILAB is:
[SSE,xsol]=leastsq([imp,] fun [,Dfun],x0)
where SSE is the minimum value of the sum of the squared residuals (SSE stands for the Sum
of Squared Errors. Error is equivalent to residual in this context. ), xsol is the point that
minimizes the sum of squared residuals, imp is an optional value that determines the type of
output provided by the function, fun is the name of the function under consideration, Dfun is
the gradient of the function under consideration, and x0 is an initial guess to the least-square
solution.
We will apply function leastsq to the under-determined system of linear equations presented
earlier, namely,
Using SCILAB, we first enter the matrix A and vector c, as well as an initial guess for the
solution, vector x0, as follows:
--> A = [1,3;2,-5;-1,1],c=[-15;-5;-22]
A =
! 1. 3. !
! 2. - 5. !
! - 1. 1. !
c =
! - 15. !
! - 5. !
.
22
5
15
, ,
1 1
5 2
3 1
2
1
1
1
1
]
1

1
]
1

1
1
1
]
1

c x A and
x
x
Download at InfoClearinghouse.com 39 2001 - Gilberto E. Urroz
! - 22. !
-->x0 = [1;1]
x0 =
! 1. !
! 1. !
In order to use leastsq we need to define the functions f(x) = Ax + c, and g(x) = A, as follows:
-->deff('y = f(x,A,c)','y = A*x+c')
-->deff('yp = g(x,A,c)','yp = A')
Now, we invoke function leastsq with the appropriate arguments to obtain our least-square
solution as:
-->[SSE,xsol] = leastsq(f,g,x0)
xsol =
! 3.0205479 !
! 1.890411 !
SSE =
645.5411
The least-square solution to the system of linear equations is, therefore, the vector xsol =
[3.02, 1.89]
T
, and the sum of squared errors is SSE = 645.5411. You can check that, by
definition, SSE = |Axsol+c|
2
, by using:
--> norm(A*xsol+c)^2
ans =
645.5411
A A u us se er r- -d de ef fi in ne ed d f fu un nc ct ti io on n f fo or r l le ea as st t- -s sq qu ua ar re e s so ol lu ut ti io on n f fo or r s sy ys st te em ms s o of f l li in ne ea ar r e eq qu ua at ti io on ns s
As indicated in the example just solved, in order to obtain the least-square solution to a system
of linear equations you need to define the function to be minimized and its derivative. You
can incorporate such definitions and the call to function leastsq into a user-defined function,
lets call it LSlinsolve (for Least-Square linsolve), which will perform the least-square solution
for a system of linear equations. A listing of function LSlinsolve follows:
function [SSE,xsol] = LSlinsolve(A,c,x0)
//Uses the least-square method to obtain the point
//that minimizes the sum of squared residuals for
//the system of linear equations A*x+c = 0.
//
// A is a nxm matrix,
// x0 is a mx1 vector, and
// c is a nx1 vector.
[nA,mA] = size(A);
[nx,mx] = size(x0);
Download at InfoClearinghouse.com 40 2001 - Gilberto E. Urroz
[nc,mc] = size(c);
if nA <> nc | mA <> nx then
error('LSlinsolve - incompatible dimensions');
abort;
end;
deff('y00 = ff(xx,A,c)', 'y00 = A*xx+c');
deff('yp0 = gg(xx,A,c)', 'yp0 = A');
[SSE,xsol] = leastsq(ff,gg,x0);
//end of function
Using the same matrix A and vectors c and x0 from the previous example, we can use function
LSlinsolve to obtain the least-square solution to the linear system Ax + c as follows:
-->getf('LSlinsolve')
-->[SSE,xsol] = LSlinsolve(A,c,x0)
xsol =
! 3.0205479 !
! 1.890411 !
SSE =
645.5411
Solution of a system of linear equations using the left-division
operator
If you have a linear system of equations, Ax=b, such that its coefficient matrix, A, is square,
you can solve the system directly by using the backward slash operator, i.e., x = A\b. The
backward slash operator represents a division of vectors and matrices in a similar fashion as the
forward slash is used to represent a division of algebraic terms.
As an example, consider the linear system given by
.
51
23
18
, ,
2 5 0
3 2 2
1 5 2
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

b and
x
x
x
x A
The solution using the backward slash operator with SCILAB is obtained as follows:
-->A = [2, -5, 1; 2, 2, -3; 0, 5, -2], b = [18; 23; -51]
A =
! 2. - 5. 1. !
! 2. 2. - 3. !
! 0. 5. - 2. !
b =
Download at InfoClearinghouse.com 41 2001 - Gilberto E. Urroz
! 18. !
! 23. !
! - 51. !
-->x = A\b
x =
! - 48.333333 !
! - 35.666667 !
! - 63.666667 !
To check the solution use:
-->A*x
ans =
! 18. !
! 23. !
! - 51. !
Please notice that A\b is different from b\A:
-->b\A
ans =
! .0237406 - .0865663 .0147655 !
Solution using the inverse matrix
The traditional way of writing the solution to the system Ax = b is x = A
-1
b. This results from
multiplying the first equation by the inverse matrix A
-1
, i.e., A
-1
Ax = A
-1
b. By definition, A
-1
A
= I, therefore, Ix = A
-1
b. Also, Ix = x, thus, the final result is,
x = A
-1
b.
For the example used earlier we could first calculate the inverse of matrix A as:
-->Ainv = inv(A)
Ainv =
! .9166667 - .4166667 1.0833333 !
! .3333333 - .3333333 .6666667 !
! .8333333 - .8333333 1.1666667 !
The solution is, therefore:
-->x = Ainv*b
x =
! - 48.333333 !
! - 35.666667 !
! - 63.666667 !
The solution can be calculated without first calculating the inverse by using:
-->x = inv(A)*b
Download at InfoClearinghouse.com 42 2001 - Gilberto E. Urroz
x =
! - 48.333333 !
! - 35.666667 !
! - 63.666667 !
C Ch ha ar ra ac ct te er ri iz zi in ng g a a m ma at tr ri ix x
In the previous section we mentioned that, under certain conditions, a system of n linear
equations with n unknowns may not have a unique solution. In order to determine when such
situations occur, we can use certain measures or norms that characterize a matrix. Some of
those measures are the rank, norm, and determinant of the matrix. These and other ways of
characterizing a matrix are presented in this section.
Matrix decomposition - a brief introduction
To understand some of the numbers used in characterizing a matrix we will first introduce the
ideas of matrix decomposition. Basically, matrix decomposition involves the determination of
two or more matrices that, when multiplied in a certain order (and, perhaps, with some matrix
inversion or transposition thrown in), produce the original matrix. For example, the so-called
LU decomposition, of a square matrix A is accomplished by writing A = L U, where L is a lower-
triangular matrix, and U is an upper-triangular matrix. (A lower-triangular matrix is such that
elements above and to the right of the main diagonal are zero, while an upper-triangular
matrix is such that elements below and to the left of the main diagonal are zero.) An example
of LU decomposition is shown below:
Such a decomposition can be obtained with SCILAB by using the function lu. A general call to
function lu is
[L,U]= lu(A)
where A is as square matrix, and L, U are as defined above. The function can also be called by
using
[L,U,P] = lu(A)
where P is a permutation matrix. When the permutation matrix P is included in the analysis,
the relationship between the various matrices is given by PA = LU.
To illustrate the use of the function lu we use the matrix A defined above:
-->A = [2,4,-2;4,9,-3;-2,-3,7]
A =
U L A
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

4 0 0
1 1 0
2 4 2
1 1 1
0 1 2
0 0 1
7 3 2
3 9 4
2 4 2
Download at InfoClearinghouse.com 43 2001 - Gilberto E. Urroz
! 2. 4. - 2. !
! 4. 9. - 3. !
! - 2. - 3. 7. !
-->[L,U] = lu(A)
U =
! 4. 9. - 3. !
! 0. 1.5 5.5 !
! 0. 0. 1.3333333 !
L =
! .5 - .3333333 1. !
! 1. 0. 0. !
! - .5 1. 0. !
While matrix L is a proper upper-triangular matrix, L is not a proper lower-triangular matrix.
This results from the numerical algorithm used for the decomposition, which introduces a
permutation in the rows or columns of the matrix. The following call of the function provides
the required permutation matrix, as well as returning the L and U matrices of the
decomposition:
-->[L1,U1,P1] = lu(A)
P1 =
! 0. 1. 0. !
! 0. 0. 1. !
! 1. 0. 0. !
U1 =
! 4. 9. - 3. !
! 0. 1.5 5.5 !
! 0. 0. 1.3333333 !
L1 =
! 1. 0. 0. !
! - .5 1. 0. !
! .5 - .3333333 1. !
S So ol lu ut ti io on n t to o a a s sy ys st te em m o of f l li in ne ea ar r e eq qu ua at ti io on ns s u us si in ng g f fu un nc ct ti io on n l lu u
We indicated above that a call to the lu function of the form [L,U,P] = lu(A), where A is the
nxn matrix of coefficients of a system of linear equations, returns the matrices L, U, and P,
such that PA = LU. If the original system is Ax = b, then we can write, PAx = Pb, or LUx =
c, where c = Pb. This system can be solved in two steps, the first one consisting in solving Ly
= c, and the second one consisting in solving Ux = y.
Consider the system of linear equations:
Download at InfoClearinghouse.com 44 2001 - Gilberto E. Urroz
To solve the system of equations under consideration using function lu and the procedure
outlined above, use:
-->A= [2,4,6;3,-2,1;4,2,-1], b = [14;-3;-4]
A =
! 2. 4. 6. !
! 3. - 2. 1. !
! 4. 2. - 1. !
b =
! 14. !
! - 3. !
! - 4. !
-->[L,U,P] = lu(A)
P =
! 0. 0. 1. !
! 0. 1. 0. !
! 1. 0. 0. !
U =
! 4. 2. - 1. !
! 0. - 3.5 1.75 !
! 0. 0. 8. !
L =
! 1. 0. 0. !
! .75 1. 0. !
! .5 - .8571429 1. !
-->c = P*b
c =
! - 4. !
! - 3. !
! 14. !
The first system to be solved is:
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1


14
3
4
1 8571429 . 0 5 . 0
0 1 75 . 0
0 0 1
., . ,
3
2
1
y
y
y
e i c y L
This system is equivalent to the three equations:
y
1
= -4
0.75y
1
+ y
2
= -3
.
4
3
14
, ,
1 2 4
1 2 3
6 4 2
3
2
1
1
1
1
]
1


1
1
1
]
1

,
_

b x A
x
x
x
Download at InfoClearinghouse.com 45 2001 - Gilberto E. Urroz
0.5y
1
- 0.8571429y
2
+ y
3
= 14
This system of equations can be solved one equation at a time starting from the first one, y
1
= -
4. The second equation produces y
2
= -3 - 0.75 (-4) = 0. The third equation is used to solve for
y
3
= 14 - 0.5 (-4) + 0.8571429 (0) = 16. Thus, the solution vector, y, is written as:
.
16
0
4
1
1
1
]
1

y
Next, we proceed to solve the equation:
.
16
0
4
8 0 0
75 . 1 5 . 3 0
1 2 4
,
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1


x
x
x
or y x U
This produces the system of linear equations:
4x
1
+ 2x
2
- x
3
= -4
-3.5x
2
+1.75x
3
= -3
8x
3
= 16
These equations can be solved one at a time, starting with the third equation, x
3
= 16/8 = 2.
The second equation produces x
2
= (-1.75)(2)/(-3.5) = 1. Finally, the first equation produces x
1
= (-4-2(1)+2)/4 = -1. Thus, the solution is
.
2
1
1
1
1
1
]
1

x
In summary, the LU decomposition of a square matrix A helps in the solution of the linear
system Ax = b, by generating two systems of equations that can be easily solved one at a time.
With SCILAB, however, we need not be concerned with solving linear systems through LU
decomposition. Instead we can use functions linsolve or the backward slash operator as
illustrated earlier.
Singular value decomposition and rank of a matrix
The Singular Value Decomposition (SVD) of a rectangular matrix A
mn
is accomplished by
obtaining the matrices U, S, and V, such that
A
mn
= U
mm
S
mn
V
T

nn
,
Where U and V are orthogonal matrices, and S is a diagonal matrix. The diagonal elements of S
are called the singular values of A and are usually ordered so that s
i
s
i+1
, for i = 1, 2, , n-1.
The columns [u
j
] of U and [v
j
] of V are the corresponding singular vectors.
Download at InfoClearinghouse.com 46 2001 - Gilberto E. Urroz
Orthogonal matrices are such that U U
T
= I.
The rank of a matrix can be determined from its SVD by counting the number of non-singular
values.
SCILAB provides function svd to obtain the singular value decomposition of a matrix A. A call of
the form
[U,S,V] = svd(A)
will return matrices U, S, and V, as described above. A simpler call,
[s] = svd(A)
returns a vector s containing the singular values. If the rank of the matrix is also required, the
appropriate function call is
[U,S,V,r] = svd(A,tol)
where tol is a tolerance value. The rank is defined as the number of singular values larger than
tol.
As an example, we will attempt the singular value decomposition of the following rectangular
matrix,
'
4 5
2 3
1 2
1
1
1
]
1

A
Using SCILAB:
-->A = [2,-1;-3,2;5,4]
A =
! 2. - 1. !
! - 3. 2. !
! 5. 4. !
A vector of singular values is obtained with:
--> s = svd(A)
s =
! 6.6487173 !
! 3.8463696 !
The full singular value decomposition results from:
-->[U,S,V] = svd(A)
V =
! - .8882616 .4593379 !
Download at InfoClearinghouse.com 47 2001 - Gilberto E. Urroz
! - .4593379 - .8882616 !
S =
! 6.6487173 0. !
! 0. 3.8463696 !
! 0. 0. !
U =
! - .1981112 .4697774 - .8602681 !
! .2626234 - .8201336 - .5083402 !
! - .9443415 - .3266344 .0391031 !
If the rank is included in the SVD we have:
-->tol= 1e-10
tol =
1.000E-10
-->[U,S,V,rank] = svd(A,tol)
Warning :redefining function: rank
rank = 2.
V =
! - .8882616 .4593379 !
! - .4593379 - .8882616 !
S =
! 6.6487173 0. !
! 0. 3.8463696 !
! 0. 0. !
U =
! - .1981112 .4697774 - .8602681 !
! .2626234 - .8201336 - .5083402 !
! - .9443415 - .3266344 .0391031 !
To verify that U is an orthogonal matrix we use:
-->U*U'
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
To verify that V is also an orthogonal matrix use:
-->V*V'
ans =
! 1. 0. !
! 0. 1. !
T Th he e f fu un nc ct ti io on n r ra an nk k i in n S SC CI IL LA AB B
Download at InfoClearinghouse.com 48 2001 - Gilberto E. Urroz
In the previous example, when the rank is included in the call to the function svd, you get a
message indicating that the function rank is being redefined (Warning :redefining
function: rank.) This occurs because SCILAB already provides a function rank and a new
version of the function gets loaded through svd. If we attempt to use the function rank with a
matrix or vector after using svd with the option rank included, we get an error message, i.e.,
--> rank(A)
!--error 21
invalid index
To clear the name of the function rank use:
--> clear(rank)
Warning :redefining function: rank
To calculate the rank of matrix A we use:
--> rank(A)
ans =
2.
T Th he e r ra an nk k o of f a a s sq qu ua ar re e m ma at tr ri ix x
Consider the square matrix,
.
1 5 3
2 1 1
1 2 3
1
1
1
]
1

A
The rank of this matrix calculated with SCILAB is:
-->A = [3,-2,1;1,1,-2;3,5,-1]
A =
! 3. - 2. 1. !
! 1. 1. - 2. !
! 3. 5. - 1. !
--> rank(A)
ans =
3.
The rank of the matrix is 3, i.e., the same as the number of rows or columns of matrix A.
When the rank of a square matrix is the same as the number of rows or columns of the matrix,
the corresponding system of linear equations, Ax = b or Ax+c = 0, has a unique solution. If
the rank of the matrix is smaller than the number of rows or columns of the square matrix, the
matrix is singular (i.e., it does not have an inverse), and the corresponding system does not
have a unique solution.
Download at InfoClearinghouse.com 49 2001 - Gilberto E. Urroz
Consider, for example, the system given by
.
20
5
10
, ,
5 2 2
10 4 2
5 2 1
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

b x A and
x
x
x
We obtain the rank of the matrix A with function rank in SCILAB:
-->A = [1,2,-5;2,4,-10;2,2,5], b = [10;5;20]
A =
! 1. 2. - 5. !
! 2. 4. - 10. !
! 2. 2. 5. !
b =
! 10. !
! 5. !
! 20. !
--> rank(A)
ans =
2.
The rank of matrix A is 2. Therefore, the matrix is singular. We can check that by using:
-->A\b
!--error 19
singular matrix
Norms of a matrix or vector
The norm of a matrix is a number that characterizes the matrix and helps identify some matrix
properties in relation to linear algebra applications. There are many different types of matrix
norms, some of which are defined next:
The L
1
norm or column norm for a matrix A
mn
= [a
ij
] is defined as the largest column
sum of the matrix. The column sum is understood as the sum of the absolute values of
the elements in a given column. Thus,
). ,..., 2 , 1 |, | ( max || ||
1
1
m j a A
n
i
ij
j

The infinity norm or row norm of a matrix A


mn
= [a
ij
] is defined as the maximum row
sum of the matrix. Once more, this sum involves the absolute values of the elements.
Thus,
Download at InfoClearinghouse.com 50 2001 - Gilberto E. Urroz
). ,..., 2 , 1 |, | ( max || ||
1
n i a A
m
j
ij
i

The Frobenius norm of a matrix A


mn
= [a
ij
] is defined as
The Euclidean norm of a matrix A
mn
= [a
ij
] is defined as the largest singular value of
the matrix.
Norms for vectors are defined in a similar manner:
The L
p
-norm of a vector v
nx1
= [v
i
], or v
1xn
= [v
i
], is defined as
. || ||
/ 1
1
p
n
i
p
i P
v v
,
_

The Euclidean norm is the same as the L


2
-norm:
. || || | |
1
2
2

,
_

n
i
i
v v v
The infinite norm of v
nx1
= [v
i
], or v
1xn
= [v
i
], is the maximum absolute value of the
elements of the vector, i.e.,
. | | max || ||
i
i
v v

All these norms can be calculated using SCILABs function norm.


T Th he e f fu un nc ct ti io on n n no or rm m
We have used the function norm previously to determine the Euclidean magnitude of vectors.
The function norm can also be used to produce a number of matrix norms. The general form
of the function call is:
[y]=norm(x [,flag])
where x is a real or complex vector or matrix, and flag is a string or number that determines
the type of norm sought. The default value for norm is 2, indicating the Euclidean norm of the
matrix or vector.
If A is a matrix, according to the values of the flag argument, the function norm produces any
of the following norms:
norm(A), or norm(A,2): Euclidean norm of the matrix
. | | || ||
1 1
2

n
i
m
j
ij F
a A
Download at InfoClearinghouse.com 51 2001 - Gilberto E. Urroz
norm(A,1): The L
1
-norm or column norm of the matrix.
norm(A,'inf'),norm(A,%inf): The infinity norm of the matrix.
norm(A,'fro'): Frobenius norm the matrix.
If v is a vector, the function norm produces any of the following norms:
norm(v,p): The L
p
-norm of the vector.
norm(v) or norm(v,2): The L
2
- or Euclidean norm (magnitude) of the vector.
norm(v,'inf'): The infinite norm of the vector.
In this description of the function norm we use the concept of the singular values of a matrix.
Some applications of the function norm for matrices and vectors are presented next:
-->A = [2,3;-2,1;0,5]
A =
! 2. 3. !
! - 2. 1. !
! 0. 5. !
-->norm(A) //Default - Euclidean norm
ans =
5.964908
-->norm(A,1) //L1 norm
ans =
9.
-->norm(A,'inf') //Infinite norm
ans =
5.
-->norm(A,'fro') //Frobenius norm
ans =
6.5574385
-->v = [3, 3, -5, 2]
v =
! 3. 3. - 5. 2. !
-->norm(v,3) //L3 norm
ans =
5.7184791
-->norm(v,4) //L4 norm
ans =
5.3232748
-->norm(v) //Default: Euclidean norm
Download at InfoClearinghouse.com 52 2001 - Gilberto E. Urroz
ans =
6.8556546
-->norm(v,'inf') //Infinite norm
ans =
5.
Determinants, singular matrices, and conditions numbers
In the chapter on vectors we introduce the concept of determinants for square matrices of
dimensions 22 and 33. Calculation of determinants for matrices of higher order is described
later in this section. It will be enough at this point to indicate that it is possible to calculate
the determinant of any square matrix. If the determinant of matrix A, written det A, is zero,
then the matrix A is said to be singular. Otherwise, it is non-singular. Singular matrices do not
have an inverse.
The condition number of a square non-singular matrix is defined as the product of the matrix
norm times the norm of its inverse, i.e.,
cond(A) = ||A||||A
-1
||.
It is also defined as the ratio of the largest to the smallest singular values of the matrix.
The condition number of a non-singular matrix is a measure of how close the matrix is to being
singular. The larger the value of the condition number, the closer it is to singularity.
The condition number of a matrix is calculated in SCILAB using the function cond. The
following example illustrates the calculation of the condition number for a non-singular and a
singular matrix:
Non-singular matrix:
-->A = [2,3,-1;5,5,2;1,1,-1]
A =
! 2. 3. - 1. !
! 5. 5. 2. !
! 1. 1. - 1. !
-->cond(A)
ans =
20.765455
Singular matrix:
-->B = [2,3,-1;1,1,1;4,6,-2]
B =
! 2. 3. - 1. !
! 1. 1. 1. !
! 4. 6. - 2. !
Download at InfoClearinghouse.com 53 2001 - Gilberto E. Urroz
-->cond(B)
ans =
1.391E+16
A matrix very near singularity:
-->C = [2,2,-1;2.01,2.01,-0.99;1,5,-2]
C =
! 2. 2. - 1. !
! 2.01 2.01 - .99 !
! 1. 5. - 2. !
-->cond(C)
ans =
679.64701
The condition number for the singular matrix is practically infinity, while that for matrix C,
which is near singularity, is a relatively large number compared to that of the non-singular
matrix A. The ranks of these matrices are rank(A) = rank(C) = 3, and rank(B) = 2.
Both rank and condition numbers of a square matrix can be used to determine whether the
matrix is singular or near singularity. This information will be useful in the analysis of the
solution of a linear system.
The determinant of a matrix
For square matrices of order larger than 3, the determinant can be calculated by using smaller
order determinants called cofactors. The general idea is to expand a determinant of an nn
matrix (also referred to as a nn determinant) into a sum of the cofactors, which are (n-1)(n-
1) determinants, multiplied by the elements of a single row or column, with alternating
positive and negative signs. This expansion is then carried to the next (lower) level, with
cofactors of order (n-2)(n-2), and so on, until we are left only with a long sum of 22
determinants. The 22 determinants are then calculated through the method presented in
earlier.
The method of calculating a determinant by cofactor expansion is very inefficient in the sense
that it involves a number of operations that grows very fast as the size of the determinant
increases. A more efficient method, and the one preferred in numerical applications, is to use
a result from Gaussian elimination. The method of Gaussian elimination is used to solve
systems of linear equations. Details of this method are presented in a later part of this
chapter.
To refer to the determinant of a matrix A, we write det(A). As mentioned in chapter 9, the
determinant of a matrix can also be written as the elements of the matrix enclosed between
vertical bars, for example, given
Download at InfoClearinghouse.com 54 2001 - Gilberto E. Urroz
Determinants can be calculated in SCILAB by using the function det. For example, for the
matrices used earlier to illustrate the use of cond we find:
-->det(A)
ans =
7.
-->det(B)
ans =
0.
-->det(C)
ans =
- .12
Notice that the determinant for a singular matrix, such as B in this example, is zero. The
determinant of matrix C, which in nearly-singular, is very close to zero.
The determinant of a matrix, therefore, can also be used as a way to characterize a matrix.
Properties of determinants
The calculation of determinants by hand typically requires simplifying the determinant to
reduce the number of operations in its calculation. Manipulation of the determinant includes
operations such as multiplying or dividing a row or column by a constant, exchanging rows or
columns, or replacing a row or column by a linear combination of other rows or columns.
Whenever one of these operations take place, it is necessary to modify the expression for the
determinant to ensure that its value does not change. Some of the rules of determinant
manipulation are the following:
(1) Multiplying or dividing a row or column in a determinant by a constant is equivalent to
multiplying or dividing the determinant by that constant. For example, consider the
determinant used in the previous example
If we multiply any row by 2, the determinant gets multiplied by 2. For example (check
these with your
calculator):
. 19
7 5
1 2
) det(

A
. 76 19 2 2
14 10
2 4
, 38
14 5
2 2
, 38
7 10
1 4
, 38
14 10
1 2
, 38 19 2
7 5
2 4

. 19 5 ) 1 ( 7 2
7 5
1 2
) det( then ,
7 5
1 2

1
]
1


A A
Download at InfoClearinghouse.com 55 2001 - Gilberto E. Urroz
(2) Switching any two rows or columns produces a change of sign in the determinant. For
example, for the same case presented earlier we have:
(3) A row (or column) in a determinant can be replaced by a linear combination of rows (or
columns) without changing the value of the determinant. For example, referring to det(A)
above, we will replace the second row by the linear combination resulting from multiplying
the first row by 2 and adding the second row multiplied by 1, i.e., 2[5 7] +(-1) [2 1] = [8
15], i.e.,
Of course, with the HP 49 G calculator you no longer need to calculate determinants by hand.
Still, to understand their calculation and some of the matrix elimination methods to be
presented later, you need to keep in mind these rules.
Example: In the following example, we use determinant operations to simplify its calculation.
First, we divide the first row by 5, the second by 3, and the third by 2.
Next, we replace row 2 with ( row 1 row 2), and row 3 with (4row1-row3):
The next step in the simplification is to divide the second row by -6 and the third row by 5, to
get
. 19
5 7
2 1
, 19
1 2
7 5

. 19 8 7 15 5
15 8
7 5

1 3 4
3 4 1
4 2 1
2 3 5
2 6 8
9 12 3
20 10 5

15 5 0
7 6 0
4 2 1
2 3 5
1 3 4
3 4 1
4 2 1
2 3 5


Download at InfoClearinghouse.com 56 2001 - Gilberto E. Urroz
Finally, we replace the third row with (row 2 row 3), i.e.,
Now we use the method for calculating 33 determinants presented in chapter 9, i.e.,
You can check, using SCILABs function det, that indeed, det(A) = -1650.
Cramers rule for solving systems of linear equations
Cramers rule for solving systems of n linear equations with n unknowns, consists in forming the
matrix equation, Ax=b, as presented earlier in the chapter, and calculating the determinant
= det(A). After that, for each unknown x
i
, the matrix A
i
is formed consisting of the matrix A
with column i replaced by the components of vector b. The determinant corresponding to A
i
is
called
i
= det(A
i
). The unknown x
i
is then calculated as x
i
=
i
/.
For example, we will use Cramers rule to determine the solution to the following system of
linear equations:
X + 2Y+3Z+4R = 4,
-X+5Y+2Z+7R = 11,
4X+2Y-Z+6R=2,
2X+Y-4Z+7R = 9.
6 / 11 0 0
6 / 7 1 0
4 2 1
5 ) 6 ( 2 3 5
3 1 0
6 / 7 1 0
4 2 1
5 ) 6 ( 2 3 5


3 1 0
6 / 7 1 0
4 2 1
5 ) 6 ( 2 3 5
15 5 0
7 6 0
4 2 1
2 3 5


. 1650 )) 0 0 0 ( 0 0 6 / 11 1 1 ( 5 ) 6 ( 2 3 5
0 0
1 0
2 1
6 / 11 0 0
6 / 7 1 0
4 2 1
5 ) 6 ( 2 3 5
6 / 11 0 0
6 / 7 1 0
4 2 1
5 ) 6 ( 2 3 5
+ + + +


Download at InfoClearinghouse.com 57 2001 - Gilberto E. Urroz
To write the system as a matrix equation we use:
Application of the Cramers rule using SCILAB can be implemented as follows:
-->A = [1,2,3,4;-1,5,2,7;4,2,-1,6;2,1,-4,7]
A =
! 1. 2. 3. 4. !
! - 1. 5. 2. 7. !
! 4. 2. - 1. 6. !
! 2. 1. - 4. 7. !
-->b=[4;11;2;9]
b =
! 4. !
! 11. !
! 2. !
! 9. !
-->D = det(A)
D =
- 258.
-->DX = det([b,A(:,2),A(:,3),A(:,4)])
DX =
516.
-->DY = det([A(:,1),b,A(:,3),A(:,4)])
DY =
258.
-->DZ = det([A(:,1),A(:,2),b,A(:,4)])
DZ =
3.908E-14
-->DR = det([A(:,1),A(:,2),A(:,3),b])
DR =
- 516.
-->X = DX/D, Y = DY/D, Z = DZ/D, R = DR/D
X = - 2.
Y = - 1.
Z = - 1.515E-16
R = 2.
.
9
2
11
4
and , ,
7 4 1 2
6 1 2 4
7 2 5 1
4 3 2 1
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

b x A
R
Z
Y
X
Download at InfoClearinghouse.com 58 2001 - Gilberto E. Urroz
The direct solution using matrices is:
-->A\b
ans =
! - 2. !
! - 1. !
! 0. !
! 2. !
Notice that Cramers rule gives a value for Z of 1.51510
-16
, indicating that a negligible
numerical error was introduced.
The function TRACE
The function TRACE calculates the trace of square matrix, defined as the sum of the elements
in its main diagonal, or
Using Einsteins repeated index convention we can write simply, tr(A
nn
) = a
ii
.
SCILAB provides the function trace to calculate the trace of a matrix. For example, for the
matrix A used in the previous example:
--> trace(A)
ans =
12.


n
i
ii n n
a tr
1
. ) (A
Download at InfoClearinghouse.com 59 2001 - Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
Download at InfoClearinghouse.com 60 2001 - Gilberto E. Urroz
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Matrix Operations with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 - Gilberto E. Urroz
Gaussian and Gauss-Jordan elimination 2
Gaussian elimination using a system of equations 2
Forward elimination 2
Backward substitution 2
Gaussian elimination using matrices 3
Gaussian elimination algorithm 4
Augmenting a matrix in SCILAB 4
Algorithm for first step in forward elimination 5
SCILAB example for first step in forward elimination 5
Algorithm for second step in forward elimination 6
SCILAB example for second step in forward elimination 6
Algorithm for third and subsequent steps in the forward elimination 7
SCILAB example implementing the full forward elimination algorithm 8
Algorithm for backward substitution 8
SCILAB example for calculating the unknown xs 9
SCILAB function for Gaussian elimination 9
Pivoting 11
Gaussian elimination with partial pivoting 13
Solving multiple set of equations with the same coefficient matrix 15
Gaussian elimination for multiple sets of linear equations 16
Calculating an inverse matrix using Gaussian elimination 17
Calculating the determinant with Gaussian elimination 17
Gauss-Jordan elimination 20
Calculating the inverse through Gauss-Jordan elimination 21
Eigenvalues and eigenvectors 22
Calculating the first eigenvector 24
Calculating eigenvectors with a user-defined function 25
Generating the characteristic equation for a matrix 27
Generalized eigenvalue problem 29
Sparse matrices 31
Creating sparse matrices 31
Getting information about a sparse matrix 33
Sparse matrix with unit entries 33
Sparse identity matrices 34
Sparse matrix with random entries 34
Sparse matrices with zero entries 35
Visualizing sparse matrices 35
Factorization of sparse matrices 36
Solution to system of linear equations involving sparse matrices 39
Solution to system of linear equations using the inverse of a sparse matrix 40
Solution to a system of linear equations with a tri-diagonal matrix of coefficients 41
Solution to tri-diagonal systems of linear equations using sparse matrices 43
Iterative solutions to systems of linear equations 45
Jacobi iterative method 46
Download at InfoClearinghouse.com 2 2001 - Gilberto E. Urroz
G Ga au us ss si ia an n a an nd d G Ga au us ss s- -J Jo or rd da an n e el li im mi in na at ti io on n
Gaussian elimination is a procedure by which the square matrix of coefficients belonging to a
system of n linear equations in n unknowns is reduced to an upper-triangular matrix (echelon
form) through a series of row operations. This procedure is known as forward elimination.
The reduction of the coefficient matrix to an upper-triangular form allows for the solution of
all n unknowns, utilizing only one equation at a time, in a procedure known as backward
substitution.
Gaussian elimination using a system of equations
To illustrate the Gaussian elimination procedure we will use the following system of 3
equations in 3 unknowns:
2X +4Y+6Z = 14,
3X -2Y+ Z = -3,
4X +2Y -Z = -4.
Forward elimination
First, we replace the second equation by (equation 2 (3/2)* equation 1), and the third by
(equation 1 (4/2)*equation 1), to get
2X +4Y+6Z = 14,
-8Y-8Z = -24,
-6Y-13Z = -32.
Next, replace the third equation with (equation 3 - (6/8)*equation 2), to get
2X +4Y+6Z = 14,
-8Y-8Z = -24,
-7Z = -14.
Backward substitution
The system of equations is now in an upper-triangular form, allowing us to solve for Z first, as
Z = -14/-7 = 2.
We then replace this value into equation 2 to solve for Y, as
Y = (-24 +8Z)/(-8) = (-24 +82)/(-8) = 1.
Finally, we replace the values of Y and Z into equation 1 to solve for X as
X = (14-4Y-6Z)/2 = (14-4(1)-6(2))/2 =-1.
Download at InfoClearinghouse.com 3 2001 - Gilberto E. Urroz
The solution is, therefore, X = -1, Y = 1, Z = 2.
Gaussian elimination using matrices
The system of equations used in the example above, i.e.,
2X +4Y+6Z = 14,
3X -2Y+ Z = -3,
4X +2Y -Z = -4.
Can be written as a matrix equation Ax = b, if we use:
To obtain a solution to the system matrix equation using Gaussian elimination, we first create
what is known as the augmented matrix corresponding to A, i.e.,
The matrix A
aug
is nothing more than the original matrix A with a new row, corresponding to the
elements of the vector b, added (i.e., augmented) to the right of the rightmost column of A.
Once the augmented matrix is put together, we can proceed to perform row operations on it
that will reduce the original A matrix into an upper-triangular matrix similar to what we did
with the system of equations shown earlier. If you perform the forward elimination by hand,
you would write the following:
The symbol ( is congruent to) indicates that what follows is equivalent to the previous
matrix with some row (or column) operations involved.
After the augmented matrix is reduced as shown above, we can proceed to perform the
backward substitution by converting the augmented matrix into a system of equations and
solving for x
3
, x
2
, and x
1
in that order, as performed earlier.
.
4
3
14
, ,
1 2 4
1 2 3
6 4 2
1
1
1
]
1


1
1
1
]
1

,
_

b x A
Z
Y
X
.
4
3
14
1 2 4
1 2 3
6 4 2

,
_


aug
A

,
_

,
_

,
_


14
24
14
7 0 0
8 8 0
6 4 2
.
32
24
14
13 6 0
8 8 0
6 4 2
4
3
14
1 2 4
1 2 3
6 4 2
aug
A
Download at InfoClearinghouse.com 4 2001 - Gilberto E. Urroz
Gaussian elimination algorithm
In this section we present the algorithm for performing Gaussian elimination on an augmented
matrix. The augmented matrix results from the system of linear equations represented by the
matricial equation Ax = b. The matrix A and vectors x and b are given by
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

n m nm n n
m
m
b
b
b
b
x
x
x
x
a a a
a a a
a a a
A
! !
"
! # ! !
"
"
2
1
2
1
2 1
2 22 21
1 12 11
, ,
The augmented matrix is:
.
2
1
2 1
2 22 21
1 12 11
1
1
1
1
1
]
1

n nm n n
m
m
aug
b
b
b
a a a
a a a
a a a
A
!
"
! # ! !
"
"
Thus, the augmented matrix can be referred to as (A
aug
)
nx(n+1)
= [a
ij
], with a
i,n+1
= b
i
, for i = 1,2,
, n.
__________________________________________________________________________________
Augmenting a matrix in SCILAB
In SCILAB augmenting a matrix A by a column vector b is straightforward. For example, for the
linear system introduced earlier, namely,
an augmented matrix, A
aug
, is obtained as follows:
-->A = [2,4,6;3,-2,1;4,2,-1], b = [1;-3;-4]
A =
! 2. 4. 6. !
! 3. - 2. 1. !
! 4. 2. - 1. !
,
4
3
14
,
1 2 4
1 2 3
6 4 2
1
1
1
]
1

,
_

b A
Download at InfoClearinghouse.com 5 2001 - Gilberto E. Urroz
b =
! 1. !
! - 3. !
! - 4. !
-->A_aug = [A b]
A_aug =
! 2. 4. 6. 1. !
! 3. - 2. 1. - 3. !
! 4. 2. - 1. - 4. !
__________________________________________________________________________________
Algorithm for first step in forward elimination
After the augmented matrix has been created the first elimination pass will fill with zeros
those values in the first column below a
11
, i.e.,
.
*
*
* * 0
* * 0
1 ,
1 , 2
1 , 1
2
2 22
1 12 11
1
1
1
1
1
]
1

+
+
+
n n
n
n
nm n
m
m
a
a
a
a a
a a
a a a
!
"
! # ! !
"
"
The procedure is indicated by the expressions:
a*
21
= a*
31
= = a*
n1
= 0; j = 1 (first column)
a*
2,j
= a
2j
- a
1j
a
21
/a
11
; j = 2,3, . . . , n+1
a*
3j
= a
3,j
- a
1j
a
31
/a
11
; j = 2,3, . . . , n+1
. . .
. . .
. . .
a*
n,j
= a
n,j
- a
1j
a
n1
/a
11
; j = 2,3, . . . , n+1
or, more concisely, as
a*
ij
= a
ij
- a
1j
a
i1
/a
11
; for i = 2,3, n; and, j = 2,3,. . .,n+1
a*
ij
= 0; for i = 2,3, , n; and, j = 1
__________________________________________________________________________________
SCILAB example for first step in forward elimination
To illustrate this first step using SCILAB, we first copy the augmented matrix we created into
an array a by using:
-->a = A_aug
a =
Download at InfoClearinghouse.com 6 2001 - Gilberto E. Urroz
! 2. 4. 6. 1. !
! 3. - 2. 1. - 3. !
! 4. 2. - 1. - 4. !
Next, we define n as 3 and implement the first step in the forward elimination through the use
of for..end constructs:
--> n = 3;
-->for i=2:n, for j=2:n+1, a(i,j)=a(i,j)-a(1,j)*a(i,1)/a(1,1); end; a(i,1) = 0;
end;
To see the modified augmented matrix we use:
-->a
a =
! 2. 4. 6. 14. !
! 0. - 8. - 8. - 24. !
! 0. - 6. - 13. - 32. !
__________________________________________________________________________________
Algorithm for second step in forward elimination
After the second elimination pass, the generic augmented matrix is:
1
1
1
1
1
1
]
1

+
+
+
+
1 ,
1 , 3
1 , 2
1 , 1
3
3 33
2 23 22
1 13 12 11
* *
* *
*
* * * * 0 0
* * * * 0 0
* * * 0
n n
n
n
n
nn n
n
n
n
a
a
a
a
a a
a a
a a a
a a a a
!
"
! # ! ! !
"
"
"
with rows i = 3, 4, n, recalculated as:
a**
ij
= a*
ij
- a*
2j
a*
i2
/a*
22
; for i = 3,4, n; and, j = 3,4,. . .,n+1
a**
ij
= 0; for i = 3,4,,n; and, j = 2
__________________________________________________________________________________
SCILAB example for second step in forward elimination
For the SCILAB example under consideration, this second step will be the final step in the
forward elimination. This step is implemented as follows:
--> for i=3:n, for j=3:n+1, a(i,j)=a(i,j)-a(2,j)*a(i,2)/a(2,2); end; a(i,2) =
0; end;

Download at InfoClearinghouse.com 7 2001 - Gilberto E. Urroz
The result is:
a =
! 2. 4. 6. 14. !
! 0. - 8. - 8. - 24. !
! 0. 0. - 7. - 14. !
__________________________________________________________________________________
Algorithm for third and subsequent steps in the forward elimination
For a general nx(n+1) augmented matrix, we can predict that in the next (third) elimination
pass the new coefficients in the augmented matrix can be written as:
a***
ij
= a**
ij
- a**
3j
a**
i3
/a**
33
; for i = 4,5, n; and, j = 4,5,. . .,n+1
a***
ij
= 0; for i = 4,5,,n; and, j = 3
Notice that the number of asterisks in the new coefficients calculated for each elimination pass
corresponds to the number of the elimination pass, i.e., we calculated a*
ij
in the first pass, a**
ij
in the second pass, and so on. We may venture to summarize all the equations we have
written for the new coefficients as:
a
(k)
ij
= a
(k-1)
ij
- a
(k-1)
kj
a
(k-1)
ik
/a
(k-1)
kk
; for i = k+1,k+2, n; and, j = k+1,k+2,. . .,n+1,
a
(k)
ij
= 0; for i = k+1,k+2,,n; and, j = 1,2,,k
where k = 1,2,. . .,n-1.
Notice that k represents the order of the elimination pass, i represents the row index, and j
represents the column index.
After the forward elimination passes have been completed, the modified augmented matrix
should look like this:
.
0 0 0 0
0 0 0
0 0
0
1 ,
) 1 (
1 , 1
) 2 (
1 , 3
) 2 (
1 , 2
) 1 (
1 , 1
,
) 1 (
, 1
) 2 (
1 , 1
) 2 (
3
) 2 (
1 , 3
) 2 (
33
) 2 (
2
) 1 (
1 , 2
) 1 (
23
) 1 (
22
) 1 (
1 1 , 1 13 12 11
1
1
1
1
1
1
1
1
]
1

+
+
+

n n
n
n n
n
n
n
n
n n
n
n n
n
n n
n
n n
n n
n n
a
a
a
a
a
a
a a
a a a
a a a a
a a a a a
!
"
"
! ! # ! ! !
"
"
"
If we use the array a
ij
to store the modified coefficients, we can simply write:
a
ij
= a
ij
- a
kj
a
ik
/a
kk
; i = k+1,k+2,...,n; j = k+1,k+2,...,n+1.
a
ij
= 0; for i = k+1,k+2,,n; and, j = k
Download at InfoClearinghouse.com 8 2001 - Gilberto E. Urroz
for k = 1,2,...,n-1.
Note: By using the array a
ij
to store the newly calculated coefficients you loose the information
stored in the original array a
ij.
Therefore, when implementing the algorithm in SCILAB, if you
need to keep the original array available for any reason, you may want to copy it into a
different array as we did in the SCILAB example presented earlier.
__________________________________________________________________________________
SCILAB example implementing the full forward elimination algorithm
For the 3x4 augmented matrix presented earlier, the following SCILAB commands implement
the algorithm for forward elimination:
-->a = A_aug
a =
! 2. 4. 6. 1. !
! 3. - 2. 1. - 3. !
! 4. 2. - 1. - 4. !
-->n=3;
-->for k=1:n-1, for i=k+1:n, for j=k+1:n+1, a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end; for j = 1:k, a(i,j) = 0; end; end; end;
-->a
a =
! 2. 4. 6. 14. !
! 0. - 8. - 8. - 24. !
! 0. 0. - 7. - 14. !
__________________________________________________________________________________
Algorithm for backward substitution
The next step in the algorithm is to calculate the solution x
1
, x
2
,..., x
n
, by back substitution,
starting with
x
n
= a
n,n+1
/a
nn
.
and continuing with
x
n-1
= (a
n-1,n+1
-a
n-1,n
x
n
)/a
n-1,n-1
Download at InfoClearinghouse.com 9 2001 - Gilberto E. Urroz
x
n-2
= (a
n-2,n+1
-a
n-2,n-1
x
n-1
-a
n-2,n
x
n
)/a
n-2,n-2
.
.
.
11
2
1 1 , 1
1
a
x a a
x
n
k
k k n

In summary, the solution is obtained by first calculating:


x
n
= a
n,n+1
/a
nn
.
and, then, calculating
,
1
1 ,
ii
n
i k
k ik n i
i
a
x a a
x

+
+

for i = n-1, n-2, ..., 2,1 (i.e., counting backwards from (n-1) to 1).
__________________________________________________________________________________
SCILAB example for calculating the unknown xs
Using the current value of the matrix a that resulted from the forward elimination, we can
calculate the unknowns in the linear system as follows. First, the last unknown is:
-->x(n) = a(n,n+1)/a(n,n);
Then, we calculate the remaining unknowns using:
-->for i = n-1:-1:1, sumk=0; for k=i+1:n, sumk=sumk+a(i,k)*x(k); end;
x(i)=(a(i,n+1)-sumk)/a(i,i); end;
The solution is:
-->x
x =
! - 1. !
! 1. !
! 2. !
_________________________________________________________________________________
SCILAB function for Gaussian elimination
Download at InfoClearinghouse.com 10 2001 - Gilberto E. Urroz
The following SCILAB function implements the solution of the system of linear equations,
Ax=b. The functions arguments are a nxn matrix A and a nx1 vector b. The function returns
the solution x.
function [x] = gausselim(A,b)
//This function obtains the solution to the system of
//linear equations A*x = b, given the matrix of coefficients A
//and the right-hand side vector, b
[nA,mA] = size(A)
[nb,mb] = size(b)
if nA<>mA then
error('gausselim - Matrix A must be square');
abort;
elseif mA<>nb then
error('gausselim - incompatible dimensions between A and b');
abort;
end;
a = [A b]; //Matrix augmentation
//Forward elimination
n = nA;
for k=1:n-1
for i=k+1:n
for j=k+1:n+1
a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end;
end;
end;
//Backward substitution
x(n) = a(n,n+1)/a(n,n);
for i = n-1:-1:1
sumk=0
for k=i+1:n
sumk=sumk+a(i,k)*x(k);
end;
x(i)=(a(i,n+1)-sumk)/a(i,i);
end;
//End function
Note: In this function we did not include the statements that produce the zero values in the
lower triangular part of the augmented matrix. These terms are not involved in the solution
at all, and were used earlier only to illustrate the effects of the Gaussian elimination.
Application of the function gausselim to the problem under consideration produces:
->A = [2,4,6;3,-2,1;4,2,-1]; b = [14;-3;-4];
-->getf('gausselim') //Loading the function
Download at InfoClearinghouse.com 11 2001 - Gilberto E. Urroz
-->gausselim(A,b) //Function call without assignment
ans =
! - 1. !
! 1. !
! 2. !
-->x = gausselim(A,b) //Function call with assignment
x =
! - 1. !
! 1. !
! 2. !
This simple implementation of Gaussian elimination is commonly referred to as nave Gaussian
elimination, because it does not account for the possibility of having zero terms in the
diagonal. Such terms will produce a division by zero in the Gaussian elimination algorithm
effectively terminating the process in SCILAB. A technique known as partial pivoting can be
implemented to account for the case of zero diagonal terms. Details on pivoting are presented
next.
__________________________________________________________________________________
Pivoting
If you look carefully at the row operations in the examples shown above, you will notice that
many of those operations divide a row by its corresponding element in the main diagonal. This
element is called a pivot element, or simply, a pivot. In many situations it is possible that the
pivot element become zero, in which case a division by zero occurs. Also, to improve the
numerical solution of a system of equations using Gaussian or Gauss-Jordan elimination, it is
recommended that the pivot be the element with the largest absolute value in a given column.
This operation is called partial pivoting. To follow this recommendation is it often necessary
to exchange rows in the augmented matrix while performing a Gaussian or Gauss-Jordan
elimination.
While performing pivoting in a matrix elimination procedure, you can improve the numerical
solution even more by selecting as the pivot the element with the largest absolute value in the
column and row of interest. This operation may require exchanging not only rows, but also
columns, in some pivoting operations. When row and column exchanges are allowed in
pivoting, the procedure is known as full pivoting.
When exchanging rows and columns in partial or full pivoting, it is necessary to keep track of
the exchanges because the order of the unknowns in the solution is altered by those exchanges.
One way to keep track of column exchanges in partial or full pivoting mode, is to create a
permutation matrix P = I
nn
, at the beginning of the procedure. Any row or column exchange
required in the augmented matrix A
aug
is also registered as a row or column exchange,
respectively, in the permutation matrix. When the solution is achieved, then, we multiply the
permutation matrix by the unknown vector x to obtain the order of the unknowns in the
solution. In other words, the final solution is given by Px = b, where b is the last column of
the augmented matrix after the solution has been found.
Download at InfoClearinghouse.com 12 2001 - Gilberto E. Urroz
A function such as lu in SCILAB automatically take care of using partial or full pivoting in the
solution of linear systems. No special provision is necessary in the call to this function to
activate pivoting. The function lu take care of reincorporating the permutation matrix into
the final result.
__________________________________________________________________________________
Consider, as an example, the linear system shown below:
2Y + 3Z = 7,
2X + 3Z = 13,
8X +16Y - Z = -3.
The augmented matrix is:
The zero in element (1,1) will not allow the simple Gaussian elimination to proceed since a
division by zero will be required. Trying a solution with function gausselim, described earlier,
produces an error:
-->A = [0,2,3;2,0,3;8,16,-1], b = [7;13;-3]
A =
! 0. 2. 3. !
! 2. 0. 3. !
! 8. 16. - 1. !
b =
! 7. !
! 13. !
! - 3. !
-->getf('gausselim')
-->x = gausselim(A,b)
!--error 27
division by zero...
at line 26 of function gausselim called by :
x = gausselim(A,b)
As expected, gausselim produces a division-by-zero error. However, lu, which already
incorporates pivoting, will produce the following solution through LU decomposition:
-->[L,U,P] = lu(A)
P =
! 0. 0. 1. !
! 0. 1. 0. !
! 1. 0. 0. !
U =
.
3 1 16 8
13 3 0 2
7 3 2 0
1
1
1
]
1

aug
A
Download at InfoClearinghouse.com 13 2001 - Gilberto E. Urroz
! 8. 16. - 1. !
! 0. - 4. 3.25 !
! 0. 0. 4.625 !
L =
! 1. 0. 0. !
! .25 1. 0. !
! 0. - .5 1. !
The right-hand side vector is modified by using the permutation matrix:
-->c = P*b
c =
! - 3. !
! 13. !
! 7. !
The solution to the system of linear equations through LU decomposition proceeds in two parts:
-->y = L\c
y =
! - 3. !
! 13.75 !
! 13.875 !
-->x = U\y
x =
! 2. !
! - 1. !
! 3. !
Gaussian elimination with partial pivoting
In this section we modify the function gausselim to incorporate partial pivoting. The resulting
function is called gausselimPP. The algorithm for partial pivoting requires us to compare the
current pivot at the k-th pass of the forward elimination part, namely a
kk
, with all the
elements of the same column, a
ij
, for i>k. When the largest absolute value of the column is
found, the corresponding row and row k are exchanged before proceeding with the forward
elimination algorithm. The backward substitution part of the algorithm proceeds as in the
original gausselim function.
Here is the listing of function gausselimPP:
function [x] = gausselimPP(A,b)
//This function obtains the solution to the system of
//linear equations A*x = b, given the matrix of coefficients A
//and the right-hand side vector, b. Gaussian elimination with
//partial pivoting.
Download at InfoClearinghouse.com 14 2001 - Gilberto E. Urroz
[nA,mA] = size(A)
[nb,mb] = size(b)
if nA<>mA then
error('gausselim - Matrix A must be square');
abort;
elseif mA<>nb then
error('gausselim - incompatible dimensions between A and b');
abort;
end;
a = [A b]; // Augmented matrix
n = nA ; // Matrix size
//Forward elimination with partial pivoting
for k=1:n-1
kpivot = k; amax = abs(a(k,k)); //Pivoting
for i=k+1:n
if abs(a(i,k))>amax then
kpivot = i; amax = a(k,i);
end;
end;
temp = a(kpivot,:); a(kpivot,:) = a(k,:); a(k,:) = temp;
for i=k+1:n //Forward elimination
for j=k+1:n+1
a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end;
end;
end;
//Backward substitution
x(n) = a(n,n+1)/a(n,n);
for i = n-1:-1:1
sumk=0
for k=i+1:n
sumk=sumk+a(i,k)*x(k);
end;
x(i)=(a(i,n+1)-sumk)/a(i,i);
end;
//End function
Application of the function gausselimPP for the case under consideration is shown next:
-->A = [0,2,3;2,0,3;8,16,-1]; b = [7;13;-3];
-->getf('gausselimPP')
-->gausselimPP(A,b)
ans =
! 2. !
! - 1. !
! 3. !
Download at InfoClearinghouse.com 15 2001 - Gilberto E. Urroz
Solving multiple set of equations with the same coefficient matrix
Suppose that you want to solve the following three sets of equations:
x
1
+2x
2
+3x
3
= 14, 2x
1
+4x
2
+6x
3
= 9, 2x
1
+4x
2
+6 x
3
= -2,
3x
1
-2 x
2
+ x
3
= 2, 3x
1
-2x
2
+ x
3
= -5, 3x
1
-2x
2
+ x
3
= 2,
4x
1
+2 x
2
- x
3
= 5, 4x
1
+2x
2
- x
3
= 19, 4x
1
+2x
2
- x
3
= 12.
We can write the three systems of equations as a single matrix equation: AX = B, where
In the unknown matrix X, the first sub-index identifies the original sub-index, while the second
one identifies to which system of linear equations a particular variable belongs.
The solution to the system, AX = B, can be found in SCILAB using the backward slash operator,
i.e., X = A\B, or the inverse matrix, X = A
-1
B. For the matrices defined above we can write:
-->A = [1,2,3;3,-2,1;4,2,-1], B=[14,9,-2;2,-5,2;5,19,12]
A =
! 1. 2. 3. !
! 3. - 2. 1. !
! 4. 2. - 1. !
B =
! 14. 9. - 2. !
! 2. - 5. 2. !
! 5. 19. 12. !
Solution using the backward slash operator:
-->X1 = A\B
X1 =
! 1. 2. 2. !
! 2. 5. 1. !
! 3. - 1. - 2. !
Solution using the inverse matrix of A:
-->X2 = inv(A)*B
X2 =
! 1. 2. 2. !
! 2. 5. 1. !
! 3. - 1. - 2. !
In both cases, the result is:
.
12 19 5
2 5 2
2 9 14
, ,
1 2 4
1 2 3
3 2 1
33 32 31
23 22 21
13 12 11
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

B X A
x x x
x x x
x x x
.
2 1 3
1 5 2
2 2 1
:
1
1
1
]
1


x
Download at InfoClearinghouse.com 16 2001 - Gilberto E. Urroz
Gaussian elimination for multiple sets of linear equations
In this section, function gausselimPP is modified to account for a multiple set of linear
equations. The augmented matrix for this case is A
aug
= [A B], where A is a nxn matrix, and B
is a nxm matrix. The solution is now a nxn matrix X.
function [x] = gausselimm(A,B)
//This function obtains the solution to the system of
//linear equations A*X = B, given the nxn matrix of coefficients A
//and the nxm right-hand side matrix, B. Matrix X is nxm.
[nA,mA] = size(A)
[nB,mB] = size(B)
if nA<>mA then
error('gausselim - Matrix A must be square');
abort;
elseif mA<>nB then
error('gausselim - incompatible dimensions between A and b');
abort;
end;
a = [A B]; // Augmented matrix
n = nA ; // Number of rows and columns in A, rows in B
m = mB ; // Number of columns in B
//Forward elimination with partial pivoting
for k=1:n-1
kpivot = k; amax = abs(a(k,k)); //Pivoting
for i=k+1:n
if abs(a(i,k))>amax then
kpivot = i; amax = a(k,i);
end;
end;
temp = a(kpivot,:); a(kpivot,:) = a(k,:); a(k,:) = temp;
for i=k+1:n //Forward elimination
for j=k+1:n+m
a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end;
end;
end;
//Backward substitution
for j = 1:m
x(n,j) = a(n,n+j)/a(n,n);
for i = n-1:-1:1
sumk=0
Download at InfoClearinghouse.com 17 2001 - Gilberto E. Urroz
for k=i+1:n
sumk=sumk+a(i,k)*x(k,j);
end;
x(i,j)=(a(i,n+j)-sumk)/a(i,i);
end;
end;
//End function
Next, function gausselimm is applied to the matrices A and B defined earlier:
-->A = [1,2,3;3,-2,1;4,2,-1]; B=[14,9,-2;2,-5,2;5,19,12];
-->getf('gausselimm')
-->gausselimm(A,B)
ans =
! 1. 2. 2. !
! 2. 5. 1. !
! 3. - 1. - 2. !
Calculating an inverse matrix using Gaussian elimination
If the right-hand side matrix B of a set of multiple systems of linear equations is the nxn
identity matrix, i.e., B = I
nxn
, the solution X from the matrix system AX = B, is the inverse of
matrix A, i.e., X = A
-1
.
For example, to find the inverse of the matrix A presented earlier through the use of function
gausselimm, you can use the following SCILAB command:
-->Ainv = gausselimm(A,eye(3,3))
Ainv =
! 0. .1428571 .1428571 !
! .125 - .2321429 .1428571 !
! .25 .1071429 - .1428571 !
You can check that this result is the same as that obtained from SCILABs function inv:
-->inv(A)
ans =
! 0. .1428571 .1428571 !
! .125 - .2321429 .1428571 !
! .25 .1071429 - .1428571 !
Calculating the determinant with Gaussian elimination
You can prove that the determinant of a matrix can be obtained by multiplying the elements in
the main diagonal after the forward elimination part of a Gaussian elimination procedure is
Download at InfoClearinghouse.com 18 2001 - Gilberto E. Urroz
completed. In other words, after completing the forward elimination of a nave Gaussian
elimination, the determinant of a nxn matrix A = [a
ij
] is calculated as
. ) det(
1

n
k
kk
a A
If partial pivoting is included, however, the sign of the determinant changes according to the
number of row switches included in the pivoting process. In such case, if N represents the
number of row exchanges, the determinant is given by
. ) 1 ( ) det(
1


n
k
kk
N
a A
The calculation of the determinant is included in the Gaussian elimination function called
gausselimd. The call to this function includes return variables for the solution X to the system
A X = B as well as for the determinant of A. A listing of the function is shown below:
function [x,detA] = gausselimd(A,B)
//This function obtains the solution to the system of
//linear equations A*X = B, given the nxn matrix of coefficients A
//and the nxm right-hand side matrix, B. Matrix X is nxm.
[nA,mA] = size(A);
[nB,mB] = size(B);
if nA<>mA then
error('gausselim - Matrix A must be square');
abort;
elseif mA<>nB then
error('gausselim - incompatible dimensions between A and b');
abort;
end;
a = [A B]; // Augmented matrix
n = nA ; // Number of rows and columns in A, rows in B
m = mB ; // Number of columns in B
//Forward elimination with partial pivoting
nswitch = 0;
for k=1:n-1
kpivot = k; amax = abs(a(k,k)); //Pivoting
for i=k+1:n
if abs(a(i,k))>amax then
kpivot = i; amax = a(k,i);
nswitch = nswitch+1
end;
end;
temp = a(kpivot,:); a(kpivot,:) = a(k,:); a(k,:) = temp;
for i=k+1:n //Forward elimination
for j=k+1:n+m
a(i,j)=a(i,j)-a(k,j)*a(i,k)/a(k,k);
end;
end;
end;
Download at InfoClearinghouse.com 19 2001 - Gilberto E. Urroz
//Calculating the determinant
detA = 1;
for k = 1:n
detA = detA*a(k,k);
end;
detB = (-1)^nswitch*detA;
//Checking for singular matrix
epsilon = 1.0e-10;
if abs(detB)<epsilon then
error('gausselimd - singular matrix');
abort;
end;
//Backward substitution
for j = 1:m
x(n,j) = a(n,n+j)/a(n,n);
for i = n-1:-1:1
sumk=0
for k=i+1:n
sumk=sumk+a(i,k)*x(k,j);
end;
x(i,j)=(a(i,n+j)-sumk)/a(i,i);
end;
end;
//End function
An application of function gausselimd for a non-singular matrix A is shown next:
-->A = [3,5,-1;2,2,3;1,1,2]
A =
! 3. 5. - 1. !
! 2. 2. 3. !
! 1. 1. 2. !
-->b = [-4;17;11]
b =
! - 4. !
! 17. !
! 11. !
-->getf('gausselimd')
-->[x,detA] = gausselimd(A,b)
detA =
- 2.
x =
! 2. !
! - 1. !
! 5. !
Download at InfoClearinghouse.com 20 2001 - Gilberto E. Urroz
For a singular matrix A, function gausselimd provides an error message:
-->A = [2,3,1;4,6,2;1,1,2]
A =
! 2. 3. 1. !
! 4. 6. 2. !
! 1. 1. 2. !
-->[x,detA] = gausselimd(A,b)
!--error 9999
gausselimd - singular matrix
at line 53 of function gausselimd called by :
[x,detA] = gausselimd(A,b)
Gauss-Jordan elimination
Gauss-Jordan elimination consists in continuing the row operations in the augmented matrix
that results from the forward elimination from a Gaussian elimination process, until an identity
matrix is obtained in place of the original A matrix. For example, for the following augmented
matrix, the forward elimination procedure results in:
We can continue performing row operations until the augmented matrix gets reduced to:
The first matrix is the same as that one obtained before at the end of the forward elimination
process, with the exception that all rows have been divided by the corresponding diagonal
term, i.e., row 1 was divided by 2, row 2 was divided by -8, and row 3 was divided by 7. The
final matrix is equivalent to the equations: X = -1, Y = 1, Z= 2, which is the solution to the
original system of equations.
.
2
1
1
1 0 0
0 1 0
0 0 1
2
1
1
1 0 0
0 1 0
0 2 1
2
1
7
1 0 0
1 1 0
3 2 1
2
3
7
1 0 0
1 1 0
3 2 1

,
_

,
_

,
_

,
_

,
_

,
_

,
_


14
24
14
7 0 0
8 8 0
6 4 2
.
32
24
14
13 6 0
8 8 0
6 4 2
4
3
14
1 2 4
1 2 3
6 4 2
aug
A
Download at InfoClearinghouse.com 21 2001 - Gilberto E. Urroz
SCILAB provides the function rref (row-reduced echelon form) that can be used to obtain a
solution to a system of linear equations using Gauss-Jordan elimination. The function requires
as argument an augmented matrix, for example:
-->A = [2,4,6;3,-2,1;4,2,-1]; b = [14;-3;-4];
-->A_aug = [A b]
A_aug =
! 2. 4. 6. 14. !
! 3. - 2. 1. - 3. !
! 4. 2. - 1. - 4. !
-->rref(A_aug)
ans =
! 1. 0. 0. - 1. !
! 0. 1. 0. 1. !
! 0. 0. 1. 2. !
The last result indicates that the solution to the system is x
1
= -1, x
2

= 1, and x
3
= 2.
Calculating the inverse through Gauss-Jordan elimination
The calculation of an inverse matrix is equivalent to calculating the solution to the augmented
system [A | I ]. For example, for the matrix A
we would write this augmented matrix as
The SCILAB commands needed to obtain the inverse using Gauss-Jordan elimination are:
-->A = [1,2,3;3,-2,1;4,2,-1] //Original matrix
A =
! 1. 2. 3. !
! 3. - 2. 1. !
! 4. 2. - 1. !
-->A_aug = [A eye(3,3)] //Augmented matrix
A_aug =
! 1. 2. 3. 1. 0. 0. !
! 3. - 2. 1. 0. 1. 0. !
! 4. 2. - 1. 0. 0. 1. !
.
1 0 0
0 1 0
0 0 1
1 2 4
1 2 3
3 2 1
1
1
1
]
1


aug
A
,
1 2 4
1 2 3
3 2 1
1
1
1
]
1

A
Download at InfoClearinghouse.com 22 2001 - Gilberto E. Urroz
The next command produces the Gauss-Jordan elimination:
-->A_aug_inv = rref(A_aug)
A_aug_inv =
! 1. 0. 0. 0. .1428571 .1428571 !
! 0. 1. 0. .125 - .2321429 .1428571 !
! 0. 0. 1. .25 .1071429 - .1428571 !
The inverse is obtained by extracting rows 4, 5, and 6, of the previous result:
-->A_inv_1 = [A_aug_inv(:,4), A_aug_inv(:,5), A_aug_inv(:,6)]
A_inv_1 =
! 0. .1428571 .1428571 !
! .125 - .2321429 .1428571 !
! .25 .1071429 - .1428571 !
To check that the matrix A_inv_1 is indeed the inverse of A use:
-->A*A_inv_1
ans =
! 1. 0. 0. !
! 0. 1. 0. !
! 0. 0. 1. !
This exercise is presented to illustrate the calculation of inverse matrices through Gauss-Jordan
elimination. In practice, you should use the function inv in SCILAB to obtain inverse matrices.
For the case under consideration, for example, the inverse is obtained from:
-->A_inv_2 = inv(A)
A_inv_2 =
! 0. .1428571 .1428571 !
! .125 - .2321429 .1428571 !
! .25 .1071429 - .1428571 !
The result is exactly the same as found above using Gauss-Jordan elimination.
E Ei ig ge en nv va al lu ue es s a an nd d e ei ig ge en nv ve ec ct to or rs s
Given a square matrix A, we can write the eigenvalue equation
Ax = x,
where the values of that satisfy the equation are known as the eigenvalues of matrix A. For
each value of , we can find, from the same equation, values of x that satisfy the eigenvalue
equation. These values of x are known as the eigenvectors of matrix A.
The eigenvalues equation can be written also as
Download at InfoClearinghouse.com 23 2001 - Gilberto E. Urroz
(A I)x = 0.
This equation will have a non-trivial solution only if the characteristic matrix, (A I), is
singular, i.e., if
det(A I) = 0.
The last equation generates an algebraic equation involving a polynomial of order n for a
square matrix A
nn
. The resulting equation is known as the characteristic polynomial of matrix
A. Solving the characteristic polynomial produces the eigenvalues of the matrix.
Using SCILAB we can obtain the characteristic matrix, characteristic polynomial, and
eigenvalues of a matrix as shown below for a symmetric matrix
.
4 6 5
6 3 2
5 2 3
1
1
1
]
1

A
First, we define the matrix A:
-->A = [3,-2,5;-2,3,6;5,6,4]
A =
! 3. - 2. 5. !
! - 2. 3. 6. !
! 5. 6. 4. !
Next, the characteristic matrix B = (A I) corresponding to a square matrix A is calculated as
follows. Notice that we define the symbolic variable lam (for lambda, ) before forming the
characteristic matrix:
-->lam = poly(0,'lam')
lam =
lam
-->charMat = A-lam*eye(3,3)
charMat =
! 3 - lam - 2 5 !
! !
! - 2 3 - lam 6 !
! !
! 5 6 4 - lam !
The characteristic equation can be determined using function poly with matrix A and the
variable name lam as arguments, i.e.,
-->charPoly = poly(A,'lam')
charPoly =
Download at InfoClearinghouse.com 24 2001 - Gilberto E. Urroz
2 3
283 - 32lam -10lam + lam
The function spec produces the eigenvalues for matrix A:
-->lam = spec(A)
lam =
! - 5.4409348 !
! 4.9650189 !
! 10.475916 !
Calculating the first eigenvector
We can form the characteristic matrix for a particular eigenvalue, say
1
= lam(1), by using:
-->B1 = A - lam(1)*eye(3,3)
B1 =
! 8.4409348 - 2. 5. !
! - 2. 8.4409348 6. !
! 5. 6. 9.4409348 !
This, of course, is a singular matrix, which you can check by calculating the determinant of B1:
--> det(B1)
ans =
5.973E-14
which, although not exactly zero, is close enough to zero to ensure singularity.
Because the characteristic matrix is singular, there is no unique solution to the problem (A-

1
I)x = 0. The equivalent system of linear equations is:
8.4409348x
1
- 2x
2
+ 5x
3
= 0
- 2x
1
+ 8.4409348x
2
+ 6x
3
= 0
5x
1
+ 6 x
2
+ 9.4409348x
3
= 0
The three equations are linearly dependent. This means that we can select an arbitrary value
of one of the solutions, say, x
3
= 1, and solve two of the equations for the other two solutions,
x
1
and x
2
. We could try, for example, to solve the first two equations (with x
3
= 1), re-written
as:
8.4409348x
1
- 2x
2
= -5
- 2x
1
+ 8.4409348x
2
= -6
Using SCILAB we can get the solution to this system as follows:
-->C1 = B1(1:2,1:2), b1 = -B1(3,1:2)
C1 =
! 8.4409348 - 2. !
Download at InfoClearinghouse.com 25 2001 - Gilberto E. Urroz
! - 2. 8.4409348 !
-->b1 = -B1(1:2,3)
b1 =
! - 5. !
! - 6. !
The solution for
1
= [x
1
,x
2
]
T
is:
-->xi1 = C1\b1
xi1 =
! - .8060249 !
! - .9018017 !
The eigenvector x
1
is, therefore:
-->x1 = [xi1;1]
x1 =
! - .8060249 !
! - .9018017 !
! 1. !
If we want to find the corresponding unit eigenvector, e
x1
, we can use:
-->e_x1 = x1/norm(x1)
e_x1 =
! - .5135977 !
! - .5746266 !
! .6371983 !
Calculating eigenvectors with a user-defined function
Following the procedure outlined above for calculating the first eigenvector, we can write a
SCILAB user-defined function to calculate all the eigenvectors of a matrix. The following is a
listing of this function:
function [x,lam] = eigenvectors(A)
//Calculates unit eigenvectors of matrix A
//returning a matrix x whose columns are
//the eigenvectors. The function also
//returns the eigenvalues of the matrix.
[n,m] = size(A);
if m<>n then
error('eigenvectors - matrix A is not square');
abort;
end;
lam = spec(A)'; //Eigenvalues of matrix A
Download at InfoClearinghouse.com 26 2001 - Gilberto E. Urroz
x = [];
for k = 1:n
B = A - lam(k)*eye(n,n); //Characteristic matrix
C = B(1:n-1,1:n-1); //Coeff. matrix for reduced system
b = -B(1:n-1,n); //RHS vector for reduced system
y = C\b; //Solution for reduced system
y = [y;1]; //Complete eigenvector
y = y/norm(y); //Make unit eigenvector
x = [x y]; //Add eigenvector to matrix
end;
//End of function
Applying function eigenvectors to the matrix A used earlier produces the following eigenvalues
and eigenvectors:
-->getf('eigenvectors')
-->[x,lam] = eigenvectors(A)
lam =
! - 5.4409348 4.9650189 10.475916 !
x =
! - .5135977 .7711676 .3761887 !
! - .5746266 - .6347298 .5166454 !
! .6371983 .0491799 .7691291 !
We can separate the eigenvectors x
1
, x
2
, x
3
from matrix x by using:
-->x1 = x(1:3,1), x2 = x(1:3,2), x3 = x(1:3,3)
x1 =
! - .5135977 !
! - .5746266 !
! .6371983 !
x2 =
! .7711676 !
! - .6347298 !
! .0491799 !
x3 =
! .3761887 !
! .5166454 !
! .7691291 !
The eigenvectors corresponding to a symmetric matrix are orthogonal to each other, i.e., x
i
x
j
= 0, for ij. Checking these results for the eigenvectors found above:
-->x1'*x2, x1'*x3, x2'*x3
ans =
- 3.678E-16
ans =
Download at InfoClearinghouse.com 27 2001 - Gilberto E. Urroz
- 5.551E-17
ans =
9.576E-16
For a non-symmetric matrix, the eigenvalues may include complex numbers:
Example 1 - non-symmetric matrix, real eigenvalues
-->A = [3, 2, -1; 1, 1, 2; 5, 5, -2] A =
! 3. 2. - 1. !
! 1. 1. 2. !
! 5. 5. - 2. !
-->[x,lam] = eigenvectors(A)
lam =
! .9234927 4.1829571 - 3.1064499 !
x =
! - .5974148 .3437456 .2916085 !
! .7551776 .5746662 - .4752998 !
! .2698191 .7426962 .8300931 !
Example 2 - non-symmetric matrix, complex eigenvalues
-->A = [2, 2, -3; 3, 3, -2; 1, 1, 1]
A =
! 2. 2. - 3. !
! 3. 3. - 2. !
! 1. 1. 1. !
-->[x,lam] = eigenvectors(A)
warning
matrix is close to singular or badly scaled.
results may be inaccurate. rcond = 7.6862E-17
lam =
! 4.646E-16 3. - i 3. + i !
x =
! - .7071068 .25 - .25i .25 + .25i !
! .7071068 .75 - .25i .75 + .25i !
! 2.826E-16 .5 .5 !
Generating the characteristic equation for a matrix
Download at InfoClearinghouse.com 28 2001 - Gilberto E. Urroz
This section represents an additional exercise on SCILAB function programming, since we
already know that SCILAB can generate the characteristic equation for a square matrix through
function poly. The method coded in the function uses the equations:
p
n+1
= -1.0, B
n+1
= A
B
j
= A (B
j+1
- p
j+1
I), for j = n,n-1, , 3, 2,1
P
j
= (-1)
j
p
j
, j = 1, 2, ,n+1
The matrix I shown in the formula is the nxn identity matrix. The coefficients of the
polynomial will be contained in the 1x(n+1) vector
p = [p
1
p
2
p
n+1
].
The characteristic equation is given by
p
1
+p
2
+p
3

2
+ +p
n

n-1
+p
n+1

n
= 0.
Here is a listing of the function:
function [p]=chreq(A)
//This function generates the coefficients of the characteristic
//equation for a square matrix A
[m n]=size(A);
if(m <> n)then
error('matrix is not square.')
abort
end;
I = eye(n,n); //Identity matrix
p = zeros(1,n); //Matrix (1xn) filled with zeroes
p(1,n+1) = -1.0;
B = A;
p(1,n)=trace(B);
for j = n-1:-1:1,
B = A*(B-p(1,j+1)*I),
p(1,j) = trace(B)/(n-j+1),
end;
p = (-1)^n*p;
p = poly(p,"lmbd","coeff");
p
//end function chreq
In this function we used the function poly to generate the final result in the function. In the
call to poly shown above, we use the vector of coefficients p as the first argument, "lmbd" is
the independent variable , and "coeff" is required to indicate that the vector p represent the
coefficients of the polynomial. The following commands show you how to load the function and
run it for a particular matrix:
-->getf('chreq')
-->A = [1 3 1;2 5 -1;2 7 -1]
A =
! 1. 3. 1. !
Download at InfoClearinghouse.com 29 2001 - Gilberto E. Urroz
! 2. 5. - 1. !
! 2. 7. - 1. !
-->CheqA = chreq(A)
CheqA =
2 3
- 6 - 2lmbd - 5lmbd + lmbd
Generalized eigenvalue problem
The generalized eigenvalue problem involves square matrices A and B of the same size, and
consists in finding the values of the scalar and the vectors x that satisfy the matricial
equation:
A x = B x.
The following function, geingenvectors (generalized eigenvectors), calculates the eigenvalues
and eigenvectors from the generalized eigenvalue problem A x = B x, or (A-B)x = 0.
function [x,lam] = geigenvectors(A,B)
//Calculates unit eigenvectors of matrix A
//returning a matrix x whose columns are
//the eigenvectors. The function also
//returns the eigenvalues of the matrix.
[nA,mA] = size(A);
[nB,mB] = size(B);
if (mA<>nA | mB<>nB) then
error('geigenvectors - matrix A or B not square');
abort;
end;
if nA<>nB then
error('geigenvectors - matrix A and B have different dimensions');
abort;
end;
lam = poly(0,'lam'); //Define variable "lam"
chPoly = det(A-B*lam); //Characteristic polynomial
lam = roots(chPoly)'; //Eigenvalues of matrix A
x = []; n = nA;
for k = 1:n
BB = A - lam(k)*B; //Characteristic matrix
CC = BB(1:n-1,1:n-1); //Coeff. matrix for reduced system
bb = -BB(1:n-1,n); //RHS vector for reduced system
y = CC\bb; //Solution for reduced system
y = [y;1]; //Complete eigenvector
y = y/norm(y); //Make unit eigenvector
x = [x y]; //Add eigenvector to matrix
end;
Download at InfoClearinghouse.com 30 2001 - Gilberto E. Urroz
//End of function
An application of this function is presented next:
-->A = [4,1,6;8,5,8;1,5,5]
A =
! 4. 1. 6. !
! 8. 5. 8. !
! 1. 5. 5. !
-->B = [3,9,7;3,3,2;9,3,4]
B =
! 3. 9. 7. !
! 3. 3. 2. !
! 9. 3. 4. !
-->getf('geigenvectors')
-->[x,lam] = geigenvectors(A,B)
lam =
! - .2055713 - 1.1759636i - .2055713 + 1.1759636i - 1.5333019 !
x =
! - .2828249 - .0422024i - .2828249 + .0422024i - .0202307 !
! - .5392352 + .2691247i - .5392352 - .2691247i - .7437197 !
! .7450009 .7450009 .6681854 !
Download at InfoClearinghouse.com 31 2001 - Gilberto E. Urroz
S Sp pa ar rs se e m ma at tr ri ic ce es s
Sparse matrices are those that have a large percentage of zero elements. When a matrix is
defined as sparse in SCILAB, only those non-zero elements are stored. The regular definition
of a matrix, also referred to as a full matrix, implies that SCILAB stores all elements of the
matrix, zero or otherwise.
Creating sparse matrices
SCILAB provides the function sparse to define sparse matrices. The simplest call to the
function is
A_sparse = sparse(A)
Where A is a sparse or full matrix. Obviously, if A is already a sparse matrix, no action is taken
by the call to function sparse. If A is a full matrix, the function sparse squeezes out those zero
elements from A. For example,
-->A = [2, 0, -1; 0, 1, 0; 0, 0 , 2]
A =
! 2. 0. - 1. !
! 0. 1. 0. !
! 0. 0. 2. !
-->As = sparse(A)
As =
( 3, 3) sparse matrix
( 1, 1) 2.
( 1, 3) - 1.
( 2, 2) 1.
( 3, 3) 2.
Notice that SCILAB reports the size of the matrix (3,3), and those non-zero elements only.
These are the only elements stored in memory. Thus, sparse matrices are useful in minimizing
memory storage particularly when large-size matrices, with relatively small density of non-zero
elements, are involved.
Alternatively, a call to sparse of the form
A_sparse = sparse(index,values)
Where index is a nx2 matrix and values is a nx1 vector such that values(i,1) represents the
element in row index(i,1) and column index(i,2) of the sparse matrix. For example,
-->row = [2, 2, 3, 3, 6, 6, 10] //row position for non-zero elements
row =
! 2. 2. 3. 3. 6. 6. 10. !
-->col = [1, 2, 2, 3, 1, 4, 2] //column position for non-zero elements
col =
Download at InfoClearinghouse.com 32 2001 - Gilberto E. Urroz
! 1. 2. 2. 3. 1. 4. 2. !
-->val = [-0.5, 0.3, 0.2, 1.5, 4.2, -1.1, 2.0] //values of non-zero elements
val =
! - .5 .3 .2 1.5 4.2 - 1.1 2. !
-->index = [row' col'] //indices of non-zero elements (i,j)
index =
! 2. 1. !
! 2. 2. !
! 3. 2. !
! 3. 3. !
! 6. 1. !
! 6. 4. !
! 10. 2. !
-->As = sparse(index,val) //creating the sparse matrix
As =
( 10, 4) sparse matrix
( 2, 1) - .5
( 2, 2) .3
( 3, 2) .2
( 3, 3) 1.5
( 6, 1) 4.2
( 6, 4) - 1.1
( 10, 2) 2.
The function full converts a sparse matrix into a full matrix. For example,
-->A =full(As)
A =
! 0. 0. 0. 0. !
! - .5 .3 0. 0. !
! 0. .2 1.5 0. !
! 0. 0. 0. 0. !
! 0. 0. 0. 0. !
! 4.2 0. 0. - 1.1 !
! 0. 0. 0. 0. !
! 0. 0. 0. 0. !
! 0. 0. 0. 0. !
! 0. 2. 0. 0. !
The following call to sparse includes defining the row and column dimensions of the sparse
matrix besides providing indices and values of the non-zero values:
A_sparse = sparse(index,values,dim)
Here, dim is a 1x2 vector with the row and column dimensions of the sparse matrix. As an
example, we can try:
-->As = sparse(index,val,[10,12])
Download at InfoClearinghouse.com 33 2001 - Gilberto E. Urroz
As =
( 10, 12) sparse matrix
( 2, 1) - .5
( 2, 2) .3
( 3, 2) .2
( 3, 3) 1.5
( 6, 1) 4.2
( 6, 4) - 1.1
( 10, 2) 2.
Getting information about a sparse matrix
The function spget, through a call of the form
[index, val, dim] = spget(As)
returns the location of the values contained in val described by the row and column indices
(listed in the first and second columns of index), as well as the row and column dimensions of
the matrix contained in vector dim. For example, for the matrix As defined above, function
spget produces:
-->[index, values, dim] = spget(As)
dim =
! 10. 12. !
values =
! - .5 !
! .3 !
! .2 !
! 1.5 !
! 4.2 !
! - 1.1 !
! 2. !
index =
! 2. 1. !
! 2. 2. !
! 3. 2. !
! 3. 3. !
! 6. 1. !
! 6. 4. !
! 10. 2. !
Sparse matrix with unit entries
To create a matrix including values of 1.0 with the same structure of an existing sparse matrix
use the function spones, for example:
Download at InfoClearinghouse.com 34 2001 - Gilberto E. Urroz
-->A1 = spones(As)
A1 =
( 10, 12) sparse matrix
( 2, 1) 1.
( 2, 2) 1.
( 3, 2) 1.
( 3, 3) 1.
( 6, 1) 1.
( 6, 4) 1.
( 10, 2) 1.
Sparse identity matrices
To create a sparse identity matrix use the function speye. For example, giving the dimensions
of the desired identity matrix we can use:
-->speye(6,6)
ans =
( 6, 6) sparse matrix
( 1, 1) 1.
( 2, 2) 1.
( 3, 3) 1.
( 4, 4) 1.
( 5, 5) 1.
( 6, 6) 1.
To produce a sparse identity matrix with the same dimensions of an existing matrix As use:
-->speye(As)
ans =
( 10, 12) sparse matrix
( 1, 1) 1.
( 2, 2) 1.
( 3, 3) 1.
( 4, 4) 1.
( 5, 5) 1.
( 6, 6) 1.
( 7, 7) 1.
( 8, 8) 1.
( 9, 9) 1.
( 10, 10) 1.
Sparse matrix with random entries
The function sprand generates a sparse matrix with random non-zero entries. The function
requires the dimensions of the matrix as the two first arguments, and a density of non-zero
Download at InfoClearinghouse.com 35 2001 - Gilberto E. Urroz
elements as the third argument. The density of non-zero values is given as a number between
zero and one, for example:
-->As = sprand(6,5,0.2)
As =
( 6, 5) sparse matrix
( 1, 3) .0500420
( 2, 1) .9931210
( 2, 4) .7485507
( 3, 2) .6488563
( 3, 5) .4104059
( 4, 2) .9923191
To see the full matrix use:
-->full(As)
ans =
! 0. 0. .0500420 0. 0. !
! .9931210 0. 0. .7485507 0. !
! 0. .6488563 0. 0. .4104059 !
! 0. .9923191 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
Sparse matrices with zero entries
The function spzeros is used to define a sparse matrix given the dimensions of the matrix.
Since all the elements of the matrix are zero, what the function does is to reserve memory
space for the matrix ensuring its sparse character. A couple of examples of application of
spzeros are shown next:
->spzeros(As)
ans =
( 6, 5) zero sparse matrix
-->spzeros(6,4)
ans =
( 6, 4) zero sparse matrix
The function spzeros can be used, for example, in programming SCILAB functions that require
sparse matrices to reserve a matrix name for future use.
Visualizing sparse matrices
The following function, spplot (sparse matrix plot), can be used to visualize the distribution of
non-zero elements for relatively large sparse matrices. A listing of the function follows:
function spplot(As)
//Plot a schematic of non-zero elements
//for a sparse matrix As
Download at InfoClearinghouse.com 36 2001 - Gilberto E. Urroz
wnumber = input('Enter graphics window number:');
A = sparse(As);
[index,vals,dim] = spget(A);
xx=index(:,1);yy=index(:,2);
xset('window',wnumber);
xset('mark',-2,2);
plot2d(xx,yy,-2);
//end function
The function requires as input the name of the sparse matrix and it requests the number of the
graphics window where the plot is to be displayed. The following is an example that uses this
function to visualize a sparse matrix of dimensions 40x40 with a density of non-zero numbers of
0.2:
-->As = sprand(40,40,0.2);
-->getf('spplot')
-->spplot(As)
Enter graphics window number:
--> 2
The result, shown in SCILAB graphics window number 2 is reproduced next:
Factorization of sparse matrices
SCILAB provides functions lufact, luget, and ludel, to produce the LU factorization of a sparse
matrix A. Function lufact returns the rank of the matrix as well as a pointer or handle to the
LU factors, which are then retrieved with function luget. Function ludel is required after
performing a LU factorization to clear up the pointer or handle generated with lufact.
The call to function lufact has the general form:
Download at InfoClearinghouse.com 37 2001 - Gilberto E. Urroz
[hand,rk] = lufact(As)
where hand is the handle or pointer, rk is the rank of sparse matrix As. Be aware that hand is
a pointer to locate the LU factors in memory. SCILAB produces no display for hand.
The call to function luget has the general form:
[P,L,U,Q] = luget(hand)
where P and Q are permutation matrices and L and U are the LU factors of the sparse matrix
that generated hand through a call to function lufact. Matrices P, Q, L, and U are related to
matrix As by P*Q*L*U = As.
After a LU factorization is completed, it is necessary to call function ludel to clear the pointer
generated in the call to function lufact, i.e., use
ludel(hand)
The following example shows the LU factorization of a 6x6 randomly-generated sparse matrix
with a density of non-zero numbers of 0.5:
-->As = sprand(5,5,0.5);
To see the full matrix just generated try:
-->full(As)
ans =
! .7019967 .7354560 0. 0. .5098139 !
! .5018194 0. .6522234 .2921187 .3776263 !
! 0. .4732295 .9388094 .6533222 0. !
! .7860680 0. 0. .9933566 0. !
! 0. .9456872 .2401141 .4494063 0. !
The first step in the LU factorization is:
-->[hand,rk] = lufact(As)
rk =
5.
hand =
The rank of the matrix is 5. Notice that SCILAB shows nothing for the handle or pointer hand.
The second step in the LU factorization is to get the matrices P, L, U, and Q, such that P*L*U*Q
= As, i.e.,
-->[P,L,U,Q] = luget(hand);
These matrices are shown in full as follows:
-->full(P)
ans =
! 0. 0. 1. 0. 0. !
! 0. 0. 0. 1. 0. !
Download at InfoClearinghouse.com 38 2001 - Gilberto E. Urroz
! 0. 1. 0. 0. 0. !
! 1. 0. 0. 0. 0. !
! 0. 0. 0. 0. 1. !
-->full(L)
ans =
! .9933566 0. 0. 0. 0. !
! .6533222 .9388094 0. 0. 0. !
! 0. 0. .7019967 0. 0. !
! .2921187 .6522234 .6298297 - .9886182 0. !
! .4494063 .2401141 - .2233988 1.0586985 .0768072 !
-->full(U)
ans =
! 1. 0. .7913251 0. 0. !
! 0. 1. - .5506871 .5040741 0. !
! 0. 0. 1. 1.047663 .726234 !
! 0. 0. 0. 1. .0806958 !
! 0. 0. 0. 0. 1. !
-->full(Q)
ans =
! 0. 0. 0. 1. 0. !
! 0. 0. 1. 0. 0. !
! 1. 0. 0. 0. 0. !
! 0. 1. 0. 0. 0. !
! 0. 0. 0. 0. 1. !
To check if the product P*L*U*Q is indeed equal to the original sparse matrix As, use:
-->full(P*L*U*Q-As)
ans =
! 0. 1.110E-16 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 1.110E-16 0. 0. 0. !
The resulting matrix has a couple of small non-zero elements. These can be cleared by using
the function clean as follows:
-->clean(full(P*L*U*Q-As))
ans =
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
At this point we can use function ludel to clear up the handle or pointer used in the
factorization:
--> ludel(hand)
Download at InfoClearinghouse.com 39 2001 - Gilberto E. Urroz
Solution to system of linear equations involving sparse matrices
The solution to a system of linear equations of the form Ax = b, where A is a sparse matrix and
b is a full column vector, can be accomplished by using functions lufact and lusolve. Function
lufact provides the handle or pointer hand, from [hand,rk] = lufact(A), which then used by
lusolve through the function call
x = lusolve(hand,b)
The following example illustrates the uses of lufact and lusolve to solve a system of linear
equations whose matrix of coefficients is sparse:
-->A=sprand(6,6,0.6); //Generate a random, sparse matrix of coefficients
-->b=rand(6,1); //Generate a random, full right-hand side vector
First, we get the rank and handle for the LU factorization of matrix A:
-->[hand,rk] = lufact(A)
rk =
5.
hand =
Next, we use function lusolve with the handle hand and right-hand side vector b to obtain the
solution to the system of linear equations:
-->x = lusolve(hand,b)
x =
! - .8137973 !
! - .5726203 !
! .8288245 !
! .9152823 !
! .4984395 !
! 0. !
Function lusolve also allows for the direct solution of the system of linear equations without
having to use lufact first. For the case under consideration the SCILAB command is:
-->x = lusolve(A,b)
!--error 19
singular matrix
However, matrix A for this case is singular (earlier we found that its rank was 5, i.e., smaller
than the number of rows or columns, thus indicating a singular matrix), and no solution is
obtained. The use of lufact combined with lusolve, as shown earlier, forces a solution by
making one of the values equal to zero and solving for the remaining five values.
After completing the solution we need to clear the handle for the LU factorization by using:
-->ludel(hand)
Download at InfoClearinghouse.com 40 2001 - Gilberto E. Urroz
A second example of solving a system of linear equations is shown next:
-->A = sprand(5,5,0.5);
-->b = full(sprand(5,1,0.6));
Notice that to generate b, which must be a full vector, we first generated a sparse random
vector with a non-zero value density of 0.6. Next, we used the function full to convert that
sparse vector into a full one.
A call to function lufact will provide the rank of matrix A:
-->[hand,rk] = lufact(A)
rk =
5.
hand =
Because the matrix has full rank (i.e., its rank is the same as the number of rows or columns),
it is possible to find the solution by using:
-->x = lusolve(A,b)
x =
! - .2813594 !
! .5590971 !
! 1.0143263 !
! - .1810458 !
! - 1.1233045 !
Do not forget to clear the handle generated with lufact by using: -->ludel(hand)
Solution to system of linear equations using the inverse of a sparse
matrix
If sparse matrix A in the system of linear equations Ax = b is non-singular (i.e., a full-rank
matrix), it is possible to find the inverse of A, A
-1
,
through function inv. The solution x can then
be found with x = A
-1
b. For example, using matrix A from the previous example we can write:
-->Ainv = inv(A) //Calculate inverse of a sparse matrix
Ainv =
( 5, 5) sparse matrix
( 1, 1) - .7235355
( 1, 2) - .7404212
( 1, 3) .3915968
( 1, 4) 2.0631067
( 1, 5) .1957261
( 2, 1) 1.4075482
( 2, 2) 1.2851264
( 2, 3) - .6179589
( 2, 4) .0607043
( 2, 5) - .5012662
( 3, 1) 2.9506345
( 4, 1) - 2.1683689
Download at InfoClearinghouse.com 41 2001 - Gilberto E. Urroz
( 4, 3) 1.8737551
( 5, 1) - 2.4138658
( 5, 2) - .0298745
( 5, 3) - .9542841
( 5, 4) - 2.1488971
( 5, 5) 2.5469151
-->x = Ainv*b
x =
! - .2813594 !
! .5590971 !
! 1.0143263 !
! - .1810458 !
! - 1.1233045 !
Alternatively, you can also use left-division to obtain the solution:
-->x=A\b
x =
! - .2813594 !
! .5590971 !
! 1.0143263 !
! - .1810458 !
! - 1.1233045 !
S So ol lu ut ti io on n t to o a a s sy ys st te em m o of f l li in ne ea ar r e eq qu ua at ti io on ns s w wi it th h a a t tr ri i- -
d di ia ag go on na al l m ma at tr ri ix x o of f c co oe ef ff fi ic ci ie en nt ts s
A tri-diagonal matrix is a square matrix such that the only non-zero elements are those in the
main diagonal and those in the two diagonals immediately off the main one. A tri-diagonal
matrix system will look as follows:
1
1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
1
]
1



n
n
n
n
n
n
nn n n
n n n n n n
n n n n
b
b
b
b
b
b
x
x
x
x
x
x
a a
a a a
a a
a a
a a a
a a
1
2
3
2
1
1
2
3
2
1
, 1
, 1 1 , 1 2 , 1
1 , 2 2 , 2
33 32
23 22 21
12 11
0 0 0 0
0 0 0
0 0 0 0
0 0 0 0
0 0 0
0 0 0 0
! !
"
"
"
! ! ! # ! ! !
"
"
"
This system can also be written as Ax = b, where the nxn matrix A, and the nx1 vectors x
and b are easily identified from the previous expression.
Since each column in the matrix of coefficients only uses three elements, we can enter the
data as the elements of a nx3 matrix,
Download at InfoClearinghouse.com 42 2001 - Gilberto E. Urroz
1
1
1
1
1
1
1
1
]
1


nn n n
n n n
a a
a a a
a a a
a a a
a a
1 ,
3 , 1 2 , 1 1 , 1
33 32 31
23 22 21
12 11
0
0
! ! !
A
Thomas algorithm for the solution of the tri-diagonal system of linear equations is an
adaptation of the Gaussian elimination procedure. It consists of a forward elimination
accomplished through the recurrence formulas:
a
i2
= a
i2
- a
i1
a
i-1,3
/a
i-1,2
, b
i
= b
i
- a
i1
b
i-1
/a
i-1
,
2
, for i = 2, 3, , n
The backward substitution step is performed through the following equations:
x
n
= b
n
/a
n2
,
and
x
i
= (b
i
- a
i3
x
i+1
/a
i2
), for i = n-1, n-2, , 3, 2, 1 (counting backwards).
The following function implements the Thomas algorithm for the solution of a system of linear
equations whose matrix of coefficients is a tri-diagonal matrix:
function [x] = Tridiag(A,b)
//Uses Thomas algorithm for tridiagonal matrices
//The matrix A is entered as a matrix of three columns and n rows
//containing the main diagonal and its two closest diagonals
[n m] = size(A); // determine matrix size
//Check if number of columns = 3.
if m <> 3 then
error('Matrix needs to have three columns only.')
abort;
end
[nv mv] = size(b); // determine vector size
if ((mv <> 1) | (nv <> n)) then
error('Vector needs to be a column vector or it does not have same
number of rows as matrix')
end
//Recalculate matrix A and vector b
AA = A; bb = b;
for i = 2:n
AA(i,2) = AA(i,2)-AA(i,1)*AA(i-1,3)/AA(i-1,2);
bb(i) = bb(i) - AA(i,1)*bb(i-1)/AA(i-1,2);
end;
//Back calculation of solution
x(n) = bb(n)/AA(n,2);
for i = n-1:-1:1
x(i) = (bb(i)-AA(i,3)*x(i+1))/AA(i,2)
end;
//show solution
x
//end function
Download at InfoClearinghouse.com 43 2001 - Gilberto E. Urroz
As an exercise, enter the following SCILAB commands:
-->A = [0,4,-1;-1,4,-1;-1,4,-1;-1,4,0]; b=[150;20;150;100];
-->getf('Tridiag')
-->x = Tridiag(A,b)
x =
! 44.976077 !
! 29.904306 !
! 54.641148 !
! 38.660287 !
Solution to tri-diagonal systems of linear equations using sparse
matrices
Because a tri-diagonal matrix is basically a sparse matrix, we can use functions lufact and
lusolve to obtain the solution to a tri-diagonal system of linear equations. The following
function takes the nx3 matrix that represents a tri-diagonal matrix and produces the index and
values of the corresponding sparse matrix:
function [index,val] = tritosparse(A)
//Converts the nx3 matrix A that represents
//a tri-diagonal matrix, into the corresponding
//nxn sparse matrix
krow = 0; kcol = 0; kval = 0;
krow = krow+1; row(krow) = 1;
kcol = kcol+1; col(kcol) = 1;
kval = kval+1; val(kval) = A(1,2)
krow = krow+1; row(krow) = 1;
kcol = kcol+1; col(kcol) = 2;
kval = kval+1; val(kval) = A(1,3)
for i = 2:n-1
krow = krow+1; row(krow) = i;
kcol = kcol+1; col(kcol) = i-1;
kval = kval+1; val(kval) = A(i,1)
krow = krow+1; row(krow) = i;
kcol = kcol+1; col(kcol) = i;
kval = kval+1; val(kval) = A(i,2)
krow = krow+1; row(krow) = i;
kcol = kcol+1; col(kcol) = i+1;
kval = kval+1; val(kval) = A(i,3)
end;
krow = krow+1; row(krow) = n;
kcol = kcol+1; col(kcol) = n-1;
kval = kval+1; val(kval) = A(n,1)
krow = krow+1; row(krow) = n;
kcol = kcol+1; col(kcol) = n;
kval = kval+1; val(kval) = A(n,2)
Download at InfoClearinghouse.com 44 2001 - Gilberto E. Urroz
index = [row col];
//end function
An application, using the compact tri-dimensional matrix A presented earlier is shown below:
First, we load the function:
-->getf('tritosparse')
A call to the function with argument A produces the values and index for a sparse matrix:
-->[index,val] = tritosparse(A)
val =
! 4. !
! - 1. !
! - 1. !
! 4. !
! - 1. !
! - 1. !
! 4. !
! - 1. !
! - 1. !
! 4. !
index =
! 1. 1. !
! 1. 2. !
! 2. 1. !
! 2. 2. !
! 2. 3. !
! 3. 2. !
! 3. 3. !
! 3. 4. !
! 4. 3. !
! 4. 4. !
Next, we put together the sparse matrix using the matrix index and the vector val:
-->As = sparse(index,val)
As =
( 4, 4) sparse matrix
( 1, 1) 4.
( 1, 2) - 1.
( 2, 1) - 1.
( 2, 2) 4.
( 2, 3) - 1.
( 3, 2) - 1.
( 3, 3) 4.
( 3, 4) - 1.
( 4, 3) - 1.
( 4, 4) 4.
To see the full form of the sparse matrix we use:
-->full(As)
Download at InfoClearinghouse.com 45 2001 - Gilberto E. Urroz
ans =
! 4. - 1. 0. 0. !
! - 1. 4. - 1. 0. !
! 0. - 1. 4. - 1. !
! 0. 0. - 1. 4. !
For the right-hand side vector previously defined we can use function lusolve to obtain the
solution of the tri-diagonal system of linear equations:
-->x = lusolve(As,b)
x =
! 44.976077 !
! 29.904306 !
! 54.641148 !
! 38.660287 !
I It te er ra at ti iv ve e s so ol lu ut ti io on ns s t to o s sy ys st te em ms s o of f l li in ne ea ar r e eq qu ua at ti io on ns s
With a powerful numerical environment like SCILAB, which provides a number of pre-
programmed functions to obtain the solution to systems of linear equations, there is really no
need to use iterative methods. They are presented here only as programming exercises.
Iterative methods result from re-arranging the system of linear equations
a
11
x
1
+ a
12
x
2
+ a
13
x
3
+ + a
1,n-1
x
n-1
+ a
1,n
x
n
= b
1
,
a
21
x
1
+ a
22
x
2
+ a
23
x
3
+ + a
2,n-1
x
n-1
+ a
2,n
x
n
= b
2
,
. . . . . .
. . . . . .
a
n1
x
1
+ a
n2
x
2
+ a
n3
x
3
+ + a
n,n-1
x
n-1
+ a
n,n
x
n
= b
n
.
as
x
1
= (b
1
- a
12
x
2
- a
13
x
3
- - a
1n
x
n
)/a
11
x
2
= (b
2
- a
22
x
2
- a
23
x
3
- - a
2n
x
n
)/a
22
. . . . . .
x
n
= (b
n
- a
n2
x
2
- a
n3
x
3
- - a
nn
x
n
)/a
nn
The solution is obtained by first assuming a first guess for the solution x
(0)
= [x
10
, x
20
, , x
n0
],
which is then replaced in the right-hand side of the recursive equations shown above to
produce a new solution x
(1)
. With this new value in the right-hand side, a new approximation,
x
(2)
is obtained, and so on until the solution converges to a fixed value.
If you replace each value of x
i
in the recursive equations as it gets calculated the approach is
called the Gauss-Seidel method. An alternative, presented below as a SCILAB function, is the
Jacobi iterative method.
Download at InfoClearinghouse.com 46 2001 - Gilberto E. Urroz
Jacobi iterative method
In the Jacobi iterative method the new values of x
i
s are replaced only once in each iteration.
This method calculates the residual, R, from the equation Ax = b, as R = Ax - b, and adjusts
the value of R recalculating x until the average of the residuals is smaller than a set value of
tolerance, .
To program the Jacobi iterative method we create three files, as follows:
function [x]= Jacobi(A,b)
//Uses Jacobi iterative process to calculate x
getf('xbar.txt'); // this function calculates average absolute value
getf('Nextx.txt');// this function calculates a new value of x
[n m] = size(A); // determine size of matrix A
// check if matrix is square
if n <> m then
error('Matrix must be square');
abort;
end;
//initialize variables
x = zeros(n,1); //x = [0 0 ... 0]
Itmax = 100.; eps = 0.001; //max iterations and tolerance
Iter = 1; R = b-A*x; Rave = xbar(R); //first value of residuals
disp(Iter, 'iteration'); disp(Rave,' Average residual:')
while ((Rave > eps) & (Iter < Itmax)) //Loop while no convergence
x = Nextx(x,R,A)
R = b-A*x;
Rave = xbar(R);
Iter = Iter + 1
disp(Iter, 'iteration'); disp(Rave,' Average residual:')
end;
x
//end function Jacobi
function [xb] = xbar(x)
//Calculates average of absolute values of vector x
[n m] = size(x);
suma = 0.0;
for i = 1:n
suma = suma + abs(x(i,1));
end;
xb = suma/n
//end function xbar
function [x]=Nextx(x,R,A)
//calculates residual average for the system A*x = b
[n m] = size(A);
for i = 1:n
x(i,1) = x(i,1) + R(i,1)/A(i,i)
end;
Download at InfoClearinghouse.com 47 2001 - Gilberto E. Urroz
x
//end function Nextx
As an exercise, within SCILAB enter the following:
-->A = [4 -1 0 0; -1 4 -1 0; 0 -1 4 -1; 0 0 -1 4]; b = [150; 200; 150; 100];
-->getf('Jacobi.txt')
-->Jacobi(A,b)
A few things to notice in these functions:
1. Function Jacobi loads the functions xbar.txt and Nextx.txt.
2. The function size, when used with a matrix or vector, requires two
components. For that reason, whenever we use it in any of the functions Jacobi, xbar or
Nextx, we always use [n m] = size(). Therefore, n = number of rows, m = number of
columns.
3. To initialize the vector x we use the statement: x = zeros(n,1). The general
form of the function zeros is zeros(m,n). See also, help ones.
4. The vector x having one column is treated as a matrix. See also the use of
vector x in functions xbar.txt and Nextx.txt.
5. The function disp (display) prints the values in the list of variables, from the
last to the first. Thus, the command disp(iter, 'iteration: ') prints a line with
'iteration:' and the next line with the value of iter.
6. A while loop is used in function Jacobi.txt, which uses a compound logical
expression, namely, ((Rave > eps) & (Iter < Itmax)). So, as long as the average residual
is larger than the tolerance (eps) and the number of iterations is less than the maximum
number of iterations allowed (Itmax), the loop statements are repeated.
7. In function xbar, the word suma is used to hold a sum. The word sum is
reserved by SCILAB (try: help sum) and cannot be redefined as a variable. By the way, suma is
Spanish for sum.
Download at InfoClearinghouse.com 48 2001 - Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
Download at InfoClearinghouse.com 49 2001 - Gilberto E. Urroz
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Matrix Applications with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 - Gilberto E. Urroz
Matrix applications 2
Electric circuits 2
Structural mechanics 4
Dimensionless numbers in fluid mechanics 7
Stress at a point in a solid in equilibrium 10
Principal stresses at a point 15
Multiple linear fitting 17
Polynomial fitting 19
Selecting the best fitting 24
Exercises 26
Download at InfoClearinghouse.com 2 2001 - Gilberto E. Urroz
Matrix applications
In this section we explore some applications of matrices in the physical sciences.
Electric circuits
Consider the simple electrical circuit shown in the figure below.
Given the values of the electric resistance, R
1
= R
3
= R
5
= R
7
= 1.5 k, R
2
= R
4
= R
6
= R
8
= 800 ,
and the known steady voltages V
1
= 12 V, V
2
= 24 V. We are asked to determine the electrical
currents I
1
, I
2
, and I
3
, associated with the circulation loops shown in the figure.
The circulation loops shown pre-determine for us a preferred direction in each loop to write
Kirchoff law of voltage in a closed loop. Basically, we start at a node in the circuit and move
around a given loop subtracting voltages RI if the current is in the same direction as the loop
direction, or adding voltages if the current and the loop directions are opposite. When
encountering a voltage source, the voltage from the source is added or subtracted according to
the orientation of the voltage source with respect to the loop circulation direction. We stop
back at the same node were we started to complete the voltage equation for a given loop.
For the case shown in the figure we can write:
I
1
: -R
1
I
1
R
2
I
1
- R
3
(I
1
I
2
) V
1
= 0
I
2
: -R
4
I
2
R
5
I
2
R
6
(I
2
I
3
) R
3
(I
2
-I
1
)=0
I
3
: -V
2
R
8
I
3
R
7
I
3
R
6
I
3
= 0
Replacing the values of the resistances and voltage sources:
I
1
: -1500I
1
800I
1
- 1500 (I
1
I
2
) 12 = 0
I
2
: -800I
2
1500I
2
800 (I
2
I
3
) 1500 (I
2
-I
1
)=0
I
3
: -24 800I
3
1500I
3
800(I
3
I
2
) = 0
Algebraic manipulation of the equations reduce them to the following system of linear
equations:
-3800I
1
+1500I
2
= 12
1500I
1
- 4600I
2
+ 800I
3
= 0
800I
2
- 3100I
3
= 24
Download at InfoClearinghouse.com 3 2001 - Gilberto E. Urroz
This system can be written as a matricial system:
.
24
0
12
, ,
3100 800 0
800 4600 1500
0 1500 3800
3
2
1
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

b x A
I
I
I
A solution can be obtained by using left division, an inverse matrix, or function linsolve. The
three methods are illustrated below:
-->A=[-3800,1500,0;1500,-4600,800;0,800,-3100]
A =
! - 3800. 1500. 0. !
! 1500. - 4600. 800. !
! 0. 800. - 3100. !
-->b = [12;0;24]
b =
! 12. !
! 0. !
! 24. !
Using left division:
-->x = A\b
x =
! - .0042929 !
! - .0028753 !
! - .0084840 !
Using the inverse of matrix A:
-->x = inv(A)*b
x =
! - .0042929 !
! - .0028753 !
! - .0084840 !
Using linsolve:
-->c = -b
c =
! - 12. !
! 0. !
! - 24. !
-->x = linsolve(A,c)
x =
! - .0042929 !
! - .0028753 !
! - .0084840 !
Download at InfoClearinghouse.com 4 2001 - Gilberto E. Urroz
Regardless of the method used to obtain the solution, the final results are:
I
1
= -0.0042929 A = -4.2929 mA, I
2
= -0.0028753 A = -2.8753 mA, I
3
= -0.0084840 A = 8.484 mA.
__________________________________________________________________________________
Structural mechanics
Consider the truss structure shown in the figure below. Horizontal and vertical bars are of
length 1.0 m, and diagonal bars 1.4142 m. All acute angles in the truss are 45
o
.
By isolating each node, as shown in the figure below, we can write the following equations for
node equilibrium (i.e., Fx = 0, Fy = 0):
F
2
+ F
1
cos 45
o

= 0,
25 + F
1
sin 45
o
= 0,
-F
2
+ F
6
= 0,
-5 + F
3
= 0,
F
4
F
1
cos 45
o
+ F
5
cos 45
o
= 0,
-20 F
3
F
1
cos 45
o
F
5
cos 45
o
=0,
-F
4
+ F
7
cos 45
o
= 0,
Download at InfoClearinghouse.com 5 2001 - Gilberto E. Urroz
-15 F
8
F
7
cos 45
o
= 0,
-F
6
+ F
9
F
5
cos 45
o
= 0,
-10 + F
8
+ F
5
cos 45
o
= 0,
-F
9
F
7
cos 45
o
= 0,
25 + F
7
cos 45
o
= 0.
With sin 45
o
= cos 45
o
= 0.866, then we have:
0.866 F
1
+ F
2
= 0
0.866 F
1
= -25
-F
2
+ F
6
= 0
F
3
= 5
-0.866F
1
+F
4
+0.866F
5
= 0
-0.866F
1
-F
3
-0.866F
5
=20
-F
4
+ 0.866F
7
= 0
-0.866F
7
F
8
= 15
-0.866F
5
F
6
+ F
9
= 0
0.866F
5
+F
8
=10
-0.866F
7
F
9
= 0
0.866 F
7
= -25
We have a total of 12 equations with 9 unknowns. The system is over-determined, so we
choose, arbitrarily, the first 9 equations:
0.866 F
1
+ F
2
= 0
0.866 F
1
= -25
-F
2
+ F
6
= 0
F
3
= 5
-0.866F
1
+F
4
+0.866F
5
= 0
-0.866F
1
-F
3
-0.866F
5
=20
-F
4
+ 0.866F
7
= 0
-0.866F
7
F
8
= 15
-0.866F
5
F
6
+ F9 = 0
Writing the system as a matrix equation:
1
1
1
1
1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
1
1
1
1
]
1

0
15
0
20
0
5
0
25
0
1 0 0 1 866 . 0 0 0 0 0
0 1 866 . 0 0 0 0 0 0 0
0 0 866 . 0 0 0 1 0 0 0
0 0 0 0 866 . 0 0 1 0 866 . 0
0 0 0 0 866 . 0 1 0 0 866 . 0
0 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 1 0
0 0 0 0 0 0 0 0 866 . 0
0 0 0 0 0 0 0 1 866 . 0
9
8
7
6
5
4
3
2
1
F
F
F
F
F
F
F
F
F
Download at InfoClearinghouse.com 6 2001 - Gilberto E. Urroz
The coefficient matrix for this problem is a sparse matrix. To solve this problem using SCILAB
we need to load vectors containing the indices and the values of the non-zero elements of the
matrix A, i.e.,
-->index =
[1,1;1,2;2,1;3,2;3,6;4,3;5,1;5,4;5,5;6,1;6,3;6,5;7,4;7,7;8,7;8,8;9,5;9,6;9,9];
-->values = [0.866,1,0.866,-1,1,1,-0.866,1,0.866,-0.866,-1,-0.866,-1,0.866,-
0.866,-1,-0.866,-1,1];
-->dim = [9,9];
To check that the dimensions of the matrix index and vector values are compatible use the
function size
-->size(index), size(values)
ans =
! 19. 2. !
ans =
! 1. 19. !
Next, we create the matrix of coefficients A as a sparse matrix:
-->A = sparse(index,values,dim);
The full matrix can be seen by using:
-->full(A)
ans =
! .866 1. 0. 0. 0. 0. 0. 0. 0. !
! .866 0. 0. 0. 0. 0. 0. 0. 0. !
! 0. - 1. 0. 0. 0. 1. 0. 0. 0. !
! 0. 0. 1. 0. 0. 0. 0. 0. 0. !
! - .866 0. 0. 1. .866 0. 0. 0. 0. !
! - .866 0. - 1. 0. - .866 0. 0. 0. 0. !
! 0. 0. 0. - 1. 0. 0. .866 0. 0. !
! 0. 0. 0. 0. 0. 0. - .866 - 1. 0. !
! 0. 0. 0. 0. - .866 - 1. 0. 0. 1. !
Next, we define the right-hand side vector:
-->b = full(sparse(indexb,valuesb,dimb));
-->b
b =
! 0. !
! - 25. !
! 0. !
! 5. !
! 0. !
! 20. !
! 0. !
! 15. !
! 0. !
Download at InfoClearinghouse.com 7 2001 - Gilberto E. Urroz
The solution to the system is:
-->x = lusolve(A,b)
x =
! - 28.86836 !
! 25. !
! 5. !
! - 25. !
! 0. !
! 25. !
! - 28.86836 !
! 10. !
! 25. !
i.e.,
F
1
=-28.87 kN, F
2
= 25 kN, F
3
= 5 kN,
F
4
= -25 kN, F
5
= 0 kN, F
6
= 25 kN,
F
7
= -28.87 kN, F
8
= 10 kN, F
9
= 25 kN.
Dimensionless numbers in fluid mechanics
Dimensional analysis is a technique used in fluid mechanics, and other sciences, to reduce the
number of variables involved in an experiment by creating dimensionless numbers that
combine the original set of variables. In order to obtain these dimensionless numbers, we
make use of the principle of dimensional homogeneity, which basically states that an equation
derived from conservation laws and material properties should have the same dimensions on
both sides of the equation. For example, the equation for the distance traveled by a projectile
dropped from rest at a certain elevation above the ground is given by d = gt
2
, where g =
9.806 m/s
2
, is the acceleration of gravity, and t is the time in seconds. The distance d is given
in meters. Instead of dealing with units, we refer to three (sometimes more) fundamental
dimensions: length (L), time (T), and mass (M). We use brackets to refer to the dimensions of
a quantity, thus, [d] = L, g = [LT
-2
], and t = [T]. Replacing dimensions in the formula for d we
have:
[d] = [1/2][g][t]
2
= 1LT
-2
T
2

= L,
as expected. Thus, we say that the equation d = gt
2
is dimensionally homogeneous.
Suppose that we have an experiment that involves the following variables (showed with their
dimensions attached):
D = a diameter (L)
V = a flow velocity (LT
-1
)
= kinematic viscosity of the fluid (L
2
T
-1
)
= density of the fluid (ML
-3
)
E = bulk density of the fluid (ML
-1
T
-2
)
= surface tension of the fluid (MT
-2
)
p = a characteristic pressure drop in the flow (ML
-1
T
-2
)
g = acceleration of gravity (LT
-2
)
Download at InfoClearinghouse.com 8 2001 - Gilberto E. Urroz
There are m = 8 variables which need n = 3 dimensions to be expressed (i.e., L, T, and M).
Buckinghams theorem indicates that you can form r = m n = 8 3 = 5 dimensionless
parameters. The technique consists in selecting one geometric variable, in this case we have
no choice but to select D, the only variable that represents geometry alone; a kinematic
variable, V (you can also choose ), i.e., a variable involving length and time; and, finally, a
dynamic variable, say , i.e., a variable involving length, time, and mass. These three
variables, D, V and , become repeating variables, i.e., variables that will participate in each
of the dimensionless parameters to be formed. Each dimensionless parameter ,or number, is
formed by multiplying the repeating variables raised to a certain unknown power and
multiplying one of the remaining variables. For example, we can form for this case the
following parameters:

1
=
x
D
y
V
z
,

2
=
x
D
y
V
z
E,

3
=
x
D
y
V
z
,

4
=
x
D
y
V
z
p,

5
=
x
D
y
V
z
g.
Since the numbers are dimensionless, we can write [
i
] = L
0
T
0
M
0
, for i = 1,2, 3, 4, 5.
Replacing the dimensions of the variables involved in each dimensionless parameters we can
write, for example, for
1
:
L
0
T
0
M
0
= (ML
-3
)
x
(L)
y
( LT
-1
)
z
( L
2
T
-1
) = (L)
3x+y+z+2
(T)
z 1
(M)
x
,
From which we get the following equations:
3x+y+z+2 = 0
z 1= 0
x = 0
Or,
If we replace the dimensions of the non-repeating variables in the remaining parameters, we
can expand the matrix equation shown above to read:
So, the independent vector b has become a matrix B, and we can write the matrix equation AX
= B. The columns of B are the negatives of the exponents of the dimensions, L, T, and M, in
that order, of each of the non-repeating variables as shown in the parameters that we set
up.
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

0
1
2
0 0 1
1 0 0
1 1 3
z
y
x
1
1
1
]
1

1
1
1
]
1

1
1
1
]
1

0 1 1 1 0
2 2 2 2 1
1 1 0 1 2
0 0 1
1 0 0
1 1 3
z
y
x
Download at InfoClearinghouse.com 9 2001 - Gilberto E. Urroz
To solve for the variables x,y,z for each parameter using SCILAB, we propose using function
gausselimd:
-->A = [-3,1,1;0,0,-1;1,0,0]
A =
! - 3. 1. 1. !
! 0. 0. - 1. !
! 1. 0. 0. !
-->B = [-2,1,0,1,-1;1,2,2,2,2;0,-1,-1,-1,0]
B =
! - 2. 1. 0. 1. - 1. !
! 1. 2. 2. 2. 2. !
! 0. - 1. - 1. - 1. 0. !
-->getf('gausselimd')
-->[x,detA]=gausselimd(A,B)
detA =
1.
x =
! 0. - 1. - 1. - 1. 0. !
! - 1. 0. - 1. 0. 1. !
! - 1. - 2. - 2. - 2. - 2. !
The result is the matrix
each column representing the values of x,y,z, for the repeating variables in each of the
dimensionless parameters, thus we have:

1
=
0
D
-1
V
-1
= /DV,

2
=
-1
D
0
V
-2
E = E/V
2
,

3
=
-1
D
-1
V
-2
= /DV
2

4
=
-1
D
0
V
-2
p = p/V
2
,

5
=
0
D
1
V
-2
g = gD/V
2
.
Note: if you dont want to use function gausselimd you can use, for example, left-division:
-->A\B
ans =
! 0. - 1. - 1. - 1. 0. !
! - 1. 0. - 1. 0. 1. !
,
2 2 2 2 1
1 0 1 0 1
0 1 1 1 0

,
_




X
Download at InfoClearinghouse.com 10 2001 - Gilberto E. Urroz
! - 1. - 2. - 2. - 2. - 2. !
Or, a Gauss-Jordan elimination with function rref:
-->A_aug = [A B]
A_aug =
! - 3. 1. 1. - 2. 1. 0. 1. - 1. !
! 0. 0. - 1. 1. 2. 2. 2. 2. !
! 1. 0. 0. 0. - 1. - 1. - 1. 0. !
-->rref(A_aug)
ans =
! 1. 0. 0. 0. - 1. - 1. - 1. 0. !
! 0. 1. 0. - 1. 0. - 1. 0. 1. !
! 0. 0. 1. - 1. - 2. - 2. - 2. - 2. !
Stress at a point in a solid in equilibrium
Consider a solid body in equilibrium under the action of a system of forces and moments, as
illustrated in the figure below. If we were to make an imaginary cut through the solid body, so
that we can separate it into two parts at section S.
The effect of the part that we remove to the right of the cut surface S is replaced by the force
F, which in turn can be decomposed into a normal component, F
N
, and a shear or tangential
component F
S
.
Suppose now that we isolate a small particle off this solid body, and we do it by cutting the
body with four planes so that we can draw the particle as shown in the left-hand side of the
figure below.
Download at InfoClearinghouse.com 11 2001 - Gilberto E. Urroz
Three of the planes are chose to be perpendicular to each other so that they help us identify a
Cartesian coordinate system (x
1
, x
2
, x
3
) as shown above. The surface S, limiting the particle
from above, has a normal unit vector n = [cos
1
, cos
2
, cos
3
], where cos
1
, cos
2
, and cos

3
are the direction cosines of n. The other three surfaces limiting the particle are S
1
, S
2
, and
S
3
, where the sub-index indicates the axis that is normal to the surface. The effect of the solid
body on this particle is represented by the forces F, F
1
, F
2
, and F
3
, acting, respectively, upon
surfaces S, S
1
, S
2
, and S
3
. Let the areas corresponding to each surface S, S
1
, S
2
, and S
3
be given
by A, A
1
, A
2
, and A
3
. It is possible to show, from the geometry of the figure, that
A
1
= Acos
1
, A
2
= Acos
2
, and A
3
= Acos
3
.
The force F on surface S can be decomposed into a normal component,
F
N
= F
N
n = F
N
[cos
1
, cos
2
, cos
3
] = F
N
( cos
1
e
1
+ cos
2
e
2
+ cos
3
e
3
) = F
N
cos
j
e
j
, (*)
(*) using Einsteins repeated index convention.
and a shear component,
F
S
= F F
N
,
as shown in the figure above. The vectors are the unit vectors corresponding to the three
coordinate directions.
The forces on surfaces S
1
, S
2
, and S
3
can be written in terms of the stress components,
ij
,
shown in the figure below, as
F
i
= [-
i1
, -
i2
, -
i3
] A
i
= (-
i1
e
1
-
i2
e
2
-
i3
e
3
)A
i
= (-
i1
e
1
-
i2
e
2
-
i3
e
3
)Acos
i
(i = 1,2,3).
Using Einsteins repeated index convention we can write
Download at InfoClearinghouse.com 12 2001 - Gilberto E. Urroz
F
i
= [-
i1
, -
i2
, -
i3
] A
i
= -
ij
e
j
A
i
= -
ij
e
j
Acos
i
(i = 1,2,3).
The sub-indices identifying each stress components are chosen so that the first sub-index
represents the sub-index of the axis normal to the surface of interest, and the second
represents the direction along which the stress acts.
Stresses with the same sub-index,
ii
(i = 1,2,3) act normal to the appropriate surface and are
known as normal stresses. The other two components on each of the surfaces S
1
, S
2
, and S
3
,
are known as shear stresses, i.e.,
ij
, ij. The direction of action as shown in the figure below
is the conventional way to represent the stresses, namely, the stresses are positive when acting
in the negative coordinate directions, so that the resulting forces have a negative sign, as
shown in the equation above.
The stress components illustrated in the figure above can be written as a matrix known as the
stress tensor,
The set up of the Cartesian coordinate system and the stresses in the particle under
consideration can be used to define the stress condition at a point in the limit when the
dimensions of the particle tend to zero. Under such conditions you can prove that the stress
tensor is symmetric, i.e.,
ij
=
ji
. Therefore, to define completely the state of stress at a
point we need only to know the three normal stresses and three of the shear stresses.
For the equilibrium of force on the particle we can write
.
33 32 31
23 22 21
13 12 11
1
1
1
]
1




T
Download at InfoClearinghouse.com 13 2001 - Gilberto E. Urroz
F + F
i
= F + (-
ij
e
j
Acos
i
) = 0, or F =
ij
e
j
Acos
i
[using Einsteins convention, with both i and j repeated]
If we let
F = A,
where s is the stress vector on surface S, and replace this value in the previous equation we get
=
ij
cos
i
e
j
= cos
i
e
j

ij
= nT
To find the magnitude of the normal component of the stress vector, i.e., the projection of the
stress along the unit normal vector n, we use

n
= n / |n| = n =(
ij
cos
i
e
j
) (cos
k
e
k
) =
ij
cos
i
cos
k
( e
j
e
k
).
We can prove that for the unit vectors in the Cartesian coordinate system,
e
j
e
k
=
jk
,
where
jk
is Diracs delta function. Thus, the normal component of the stress on surface S is

n
=
ij
cos
i
cos
k

jk
Since the product indicated in this expression is zero if jk, then the only terms surviving are
those for which j = k, i.e.,

n
=
ij
cos
i
cos
j
= cos
j

ij
cos
i
.
You can prove that this latter result can be written in vector and matrix notation as the
quadratic form

n
= nTn
T

,
where
n = cos
j
e
j
.
Thus, the normal stress magnitude can be written as a quadratic form for any normal unit
vector n = n
j
e
j
, written as a row vectors, with n
j
= cos
j
, j = 1, 2, 3. Also, the normal stress
as a vector will be written as

n
=
n
n = (nTn
T
) n
The normal force is given by
F
N
=
n
A = (
n
A)n.
The shear force can be written in terms of shear stress on surface S, F
S
= F F
N
=
S
A, so that

S
=
n
.
Download at InfoClearinghouse.com 14 2001 - Gilberto E. Urroz
Example:
Let the stress at a point be given by
Determine the total stress , the normal stress
n
, and shear stress
S
, if the surface S has a
normal unit vector n = [0.5 0.25 0.8292]. What are the total force F, the normal force F
n
, and
the shear force F
S
, if the surface S has an area of 0.00001 m
2
Solution:
To calculate the total stress we use
This result can be obtained by using SCILAB as follows:
-->T = [25,-10,20;-10,30,15;20,15,40], n = [0.5,0.25,0.829]
T =
! 25. - 10. 20. !
! - 10. 30. 15. !
! 20. 15. 40. !
n =
! .5 .25 .829 !
-->sigma = T*n'
sigma =
! 26.58 !
! 14.935 !
! 46.91 !
To calculate the normal stress, use:
Using SCILAB:
-->sigma_n = n*T*n'
sigma_n =
Pa
1
1
1
]
1

40 15 20
15 30 10
20 10 25
T
1
1
1
]
1

1
1
1
]
1


8292 . 0
25 . 0
5 . 0
40 15 20
15 30 10
20 10 25
e T n
! !

1
1
1
]
1

1
1
1
]
1


8292 . 0
25 . 0
5 . 0
40 15 20
15 30 10
20 10 25
e T n
! !

Download at InfoClearinghouse.com 15 2001 - Gilberto E. Urroz


55.91214
The result is
n
= 55.93 Pa.
The shear stress is given by

S
=
n
,
or, using SCILAB:
-->sigma_s = sigma - sigma_n*n'
sigma_s =
! - 1.37607 !
! .956965 !
! .5588359 !
The forces can be calculated by multiplying the stresses times the area of the surface S, i.e., F
= A, F
n
=
n
A, and F
t
=
t
A. Using Maple, the forces are calculated as:
-->A = 0.00001
A =
.00001
-->Fn = sigma_n*n'*A
Fn =
! .0002796 !
! .0001398 !
! .0004635 !
-->Ft = sigma_s*A
Ft =
! - .0000138 !
! .0000096 !
! .0000056 !
-->F = sigma*A
F =
! .0002658 !
! .0001494 !
! .0004691 !
In paper, these forces are written as
F
n
= (2.79i +1.39j+4.63k)10
-4
N,
F
s
= (-1.38i +0.96j+0.54k)10
-5
N,
F

= (2.66i +1.49j+4.69k)10
-4
N.
Principal stresses at a point
Download at InfoClearinghouse.com 16 2001 - Gilberto E. Urroz
Given the stress tensor T representing the state of stress at a point P in a Cartesian coordinate
system (x
1
, x
2
, x
3
), suppose that you want to find the normal vector, or vectors, n for which the
stress is only in the normal direction. In other words, we are trying to find n and
n
such that
Tn =
n
n.
This equation is the eigenvalue equation for the matrix T with eigenvalues
n
and eigenvectors
n.
Recall that this equation can be written also as
(T
n
I) n = 0,
which has non-trivial solution if
det(T
n
I) = 0.
For the previous example, we can write
To obtain the eigenvalues and eigenvectors of T we use function eigenvectors in SCILAB:
-->getf('eigenvectors')
-->[x,sigm] = eigenvectors(T)
sigm =
! 1.1203785 37.800525 56.079096 !
x =
! - .6673642 - .6009826 .4398237 !
! - .5119885 .7991229 .3150721 !
! .5408260 .0149168 .8410022 !
In paper we would write:
n
1
= [-0.667 -0.512 0.541], (
n
)
1
= 1.12,
n
2
= [-0.601 0.800 0.015], (
n
)
2
= 37.80,
n
3
=[0.440 0.315 0.841], (
n
)
3
= 56.08.
The three normal stresses found are known as the principal stresses at the point. The
eigenvalues represent the normal vectors to the surfaces where those principal stresses act.
These directions are known as the principal axes.



0
40 15 20
15 30 10
20 10 25
) det(
n
n
n
n

I T
Download at InfoClearinghouse.com 17 2001 - Gilberto E. Urroz
Multiple linear fitting
Consider a data set of the form
x
1
x
2
x
3
x
n
y
x
11
x
21
x
31
x
n1
y
1
x
12
x
22
x
32
x
n2
y
2
x
13
x
32
x
33
x
n3
y
3
. . . . .
. . . . . .
x
1,m-1
x
2,m-1
x
3,m-1
x
n,m-1
y
m-1
x
1,m
x
2,m
x
3,m
x
n,m
y
m
Suppose that we search for a data fitting of the form
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3
+ + b
n
x
n
.
You can obtain the least-square approximation to the values of the coefficients
b = [b
0
b
1
b
2
b
3
b
n
],
by putting together the matrix X
_ _
1 x
11
x
21
x
31
x
n1
1 x
12
x
22
x
32
x
n2
1 x
13
x
32
x
33
x
n3
. . . . .
. . . . . .
1 x
1,m
x
2,m
x
3,m
x
n,m
_ _
Then, the vector of coefficients is obtained from
b = (X
T
X)
-1
X
T
y,
where y is the vector
y = [y
1
y
2
y
m
]
T
.
For example, use the following data to obtain the multiple linear fitting
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3,
x
1
x
2
x
3
y
1.20 3.10 2.00 5.70
2.50 3.10 2.50 8.20
Download at InfoClearinghouse.com 18 2001 - Gilberto E. Urroz
3.50 4.50 2.50 5.00
4.00 4.50 3.00 8.20
6.00 5.00 3.50 9.50
With SCILAB you can proceed as follows:
First, enter the vectors x
1
, x
2
, x
3
, and y, as row vectors:
->x1 = [1.2,2.5,3.5,4.0,6.0]
x1 =
! 1.2 2.5 3.5 4. 6. !
-->x2 = [3.1,3.1,4.5,4.5,5.0]
x2 =
! 3.1 3.1 4.5 4.5 5. !
-->x3 = [2.0,2.5,2.5,3.0,3.5]
x3 =
! 2. 2.5 2.5 3. 3.5 !
-->y = [5.7,8.2,5.0,8.2,9.5]
y =
! 5.7 8.2 5. 8.2 9.5 !
Next, we form matrix X and replace y by its transpose:
-->X = [ones(5,1) x1' x2' x3']
X =
! 1. 1.2 3.1 2. !
! 1. 2.5 3.1 2.5 !
! 1. 3.5 4.5 2.5 !
! 1. 4. 4.5 3. !
! 1. 6. 5. 3.5 !
The vector of coefficients for the multiple linear equation is calculated as:
-->b =inv(X'*X)*X'*y
b =
! - 2.1649851 !
! - .7144632 !
! - 1.7850398 !
! 7.0941849 !
Thus, the multiple-linear regression equation is:
y^ = -2.1649851-0.7144632x
1
-1.7850398x
2
+7.0941849x
3
.
This function can be used to evaluate y for values of x given as [x
1
,x
2
,x
3
]. For example, for
[x
1
,x
2
,x
3
] = [3,4,2], construct a vector xx = [1,3,4,2], and multiply xx times b, to obtain y(xx):
-->xx = [1,3,4,2]
xx =
Download at InfoClearinghouse.com 19 2001 - Gilberto E. Urroz
! 1. 3. 4. 2. !
-->xx*b
ans =
2.739836
The fitted values of y corresponding to the values of x
1
, x
2
, and x
3
from the table are obtained
from y = Xb:
-->X*b
ans =
! 5.6324056 !
! 8.2506958 !
! 5.0371769 !
! 8.2270378 !
! 9.4526839 !
Compare these fitted values with the original data as shown in the table below:
x
1
x
2
x
3
y y-fitted
1.20 3.10 2.00 5.70 5.63
2.50 3.10 2.50 8.20 8.25
3.50 4.50 2.50 5.00 5.04
4.00 4.50 3.00 8.20 8.23
6.00 5.00 3.50 9.50 9.45
Polynomial fitting
Consider the x-y data set
x y
x
1
y
1
x
2
y
2
x
3
y
3
. .
. .
x
n-1
y
n-1
x
n
y
n
Suppose that we want to fit a polynomial or order p to this data set. In other words, we seek a
fitting of the form
y = b
0
+ b
1
x + b
2
x
2
+ b
3
x
3
+ + b
p
x
p
.
You can obtain the least-square approximation to the values of the coefficients
Download at InfoClearinghouse.com 20 2001 - Gilberto E. Urroz
b = [b
0
b
1
b
2
b
3
b
p
],
by putting together the matrix X
_ _
1 x
1
x
1
2
x
1
3
x
1
p-2
x
1
p-1
1 x
2
x
2
2
x
2
3
x
2
p-2
x
2
p-1
1 x
3
x
3
2
x
3
3
x
3
p-2
x
3
p-1
. . . . . .
. . . . . . .
1 x
n
x
n
2
x
n
3
x
n
p-2
x
n
p-1
_ _
Then, the vector of coefficients is obtained from b = (X
T
X)
-1
X
T
y, where y is the vector y = [y
1
y
2
y
n
]
T
.
Earlier on, in this chapter, we defined the Vandermonde matrix corresponding to a vector x =
[x
1
x
2
x
n
] as
_ _
1 x
1
x
1
2
x
1
3
x
1
n-1
1 x
2
x
2
2
x
2
3
x
2
n-1
1 x
3
x
3
2
x
3
3
x
3
n-1
. . . . .
. . . . . .
1 x
n
x
n
2
x
n
3
x
n
n-1
_ _
Notice that this matrix is similar to the matrix X of interest to the polynomial fitting, but
having only n, rather than (p+1) columns.
We can take advantage of the VANDERMONDE function to create the matrix X if we observe the
following rules:
If p = n-1, X = V
n
.
If p < n-1, then we need to remove columns p+2, , n-1, n from matrix V
n
to form matrix X.
If p > n-1, then we need to add columns n+1, , p-1, p+1, to matrix V
n
to form matrix X.
After X is ready, and having the vector y available, the calculation of the coefficient vector b
is the same as in multiple linear fitting (the previous matrix application).
Because we can fit a polynomial of any degree to our data, we need to be able to evaluate the
fitting by checking on a couple of parameters, namely, the sum of squared errors (SSE) and the
correlation coefficient, r. These parameters are defined as follows:
Given the vectors x and y of data to be fit to the polynomial equation, we form the matrix X
and use it to calculate a vector of polynomial coefficients b. We can calculate a vector of
fitted data, y, by using
y = Xb.
An error vector is calculated by
e = y y.
The sum of square errors is equal to the square of the magnitude of the error vector, i.e.,
Download at InfoClearinghouse.com 21 2001 - Gilberto E. Urroz
SSE = |e|
2
= e e = e
i
2
= (y
i
-y
i
)
2
.
To calculate the correlation coefficient we need to calculate first what is known as the sum of
squared totals, SST, defined as
SST = (y
i
-y)
2
,
where y is the mean value of the original y values, i.e.,
y = (y
i
)/n.
In terms of SSE and SST, the correlation coefficient is defined by
. 1
SST
SSE
r
This value is constrained to the range 1 < r < 1. The closer r is to +1 or 1, the better the data
fitting.
The following function, polyfit, takes as input the vectors x and y and the polynomial order p
and returns the coefficients of the polynomial fitting (vector b), the sum of square errors (SSE),
and the correlation coefficient (r):
function [SSE,r,b] = polyfit(xx,yy,p)
//Calculates the polynomial fitting
//y^ = b(1) + b(2)*x + b(3)*x^2 + ... + b(p)*x^p
//given data sets xx, yy, and the polynomial
//degree p.
//Vectors xx and yy are row vectors.
[n m] = size(xx');
getf('vandermonde');
V = vandermonde(xx); //Get Vandermonde matrix for xx
//Get matrix X for solution
if p == n-1 then
X = V;
elseif p<n-1 then
X = V(1:n,1:p+1);
else
X = V;
for k = n+1:p+1
X = [X xx'^k]
end
end;
//Calculating coefficients b, SSE, and r
b=inv(X'*X)*X'*yy';
yfit = X*b;
err = yy'-yfit;
SSE = err'*err;
ybar = sum(yy)/n;
ybarv = ybar*ones(n,1);
SST = sum((yy'-ybarv)^2);
r = sqrt(1-SSE/SST);
Download at InfoClearinghouse.com 22 2001 - Gilberto E. Urroz
//end function
As an example, use the following data to obtain a polynomial fitting with p = 2, 3, 4, 5, 6.
x y
2.30 179.72
3.20 562.30
4.50 1969.11
1.65 65.87
9.32 31220.89
1.18 32.81
6.24 6731.48
3.45 737.41
9.89 39248.46
1.22 33.45
First, we enter:
--> x =[2.30,3.20,4.50,1.65,9.32,1.18,6.24,3.45,9.89,1.22];
-->
y=[179.72,562.30,1969.11,65.87,31220.89,32.81,6731.48,737.41,39248.46,33.45];
To fit the data to polynomials of order p = 2, 3, 4, 5, 6, 7, and 8 we use the following calls to
function polyfit.
-->getf('polyfit')
-->[SSE,r,b] = polyfit(x,y,2)
b =
! 4527.7303 !
! - 3958.5178 !
! 742.23219 !
r =
.9971908
SSE =
10731140.
-->[SSE,r,b] = polyfit(x,y,3)
b =
! - 998.0541 !
! 1303.2053 !
! - 505.27432 !
! 79.229744 !
r =
.9999768
SSE =
88619.368
-->[SSE,r,b] = polyfit(x,y,4)
b =
! 20.917344 !
! - 2.6108313 !
Download at InfoClearinghouse.com 23 2001 - Gilberto E. Urroz
! - 1.5247295 !
! 6.0491773 !
! 3.5068553 !
r =
1.
SSE =
7.4827578
-->[SSE,r,b] = polyfit(x,y,5)
b =
! 19.083718 !
! .1745033 !
! - 2.9383508 !
! 6.3611564 !
! 3.475986 !
! .0011220 !
r =
1.
SSE =
7.4140764
-->[SSE,r,b] = polyfit(x,y,6)
b =
! - 16.807588 !
! 67.398517 !
! - 48.814654 !
! 21.163051 !
! 1.0603971 !
! .1930681 !
! - .0058903 !
r =
1.
SSE =
3.8884213
-->[SSE,r,b] = polyfit(x,y,7)
warning
matrix is close to singular or badly scaled.
results may be inaccurate. rcond = 1.1558E-19
b =
! 117.79067 !
! - 237.32895 !
! 218.31856 !
! - 96.918027 !
! 29.689084 !
! - 3.6422545 !
! .25902 !
! - .0073389 !
r =
1.
SSE =
Download at InfoClearinghouse.com 24 2001 - Gilberto E. Urroz
1.2829472
-->[SSE,r,b] = polyfit(x,y,8)
warning
matrix is close to singular or badly scaled.
results may be inaccurate. rcond = 1.7245E-23
b =
! 68.081558 !
! - 100.44092 !
! 65.29768 !
! - 6.3024667 !
! - 1.3844292 !
! 2.6919754 !
! - .4920537 !
! .0401628 !
! - .0012344 !
r =
.9999909
SSE =
34695.662
Selecting the best fitting
The following table summarizes the values of r and SSE found for the different polynomial
orders:
p r SSE
2 0.9971908 10731140
3 0.9999768 88619.37
4 1 7.482758
5 1 7.414076
6 1 3.888421
7 1
1.282947
8
0.9999909 34695.66
While the correlation coefficient is very close to 1.0 for all values of p, the values of SSE vary
widely. The smallest value of SSE corresponds to p = 7. However, a warning is reported for
values of p = 7 and 8, indicating that the results may be inaccurate. Thus, we eliminate from
the analysis values for p = 7 and 8.
Discarding those values, the best fitting in terms of the minimum value of SSE is p = 6,
however, there is very little difference in the values of SSE for values of p = 4, 5, or 6 (at least
when compared to those values for p = 2, 3, or 8). Thus, any of the polynomial degrees p = 4,
5, or 6, will produce a good fitting of the original data.
To visualize the original data and the fitted data, we can use the following function plotpoly,
which calls on function polyfit. Function plotpoly requires the user to provide the (row)
vectors x and y, as well as the polynomial degree p. During execution, plotpoly requests from
the user the number of the graphics window where the plot will be produced. The function
Download at InfoClearinghouse.com 25 2001 - Gilberto E. Urroz
returns the plot of the original data points and the polynomial fitting. A listing of the function
follows:
function plotpoly(xx,yy,p)
//Plots original data and polynomial fitting
//for degree p
[m n] = size(xx);
xmin = min(xx); xmax = max(xx);
xs = [xmin:(xmax-xmin)/100:xmax];
[mm nn] = size(xs);
getf('polyfit');
[SSE,r,b] = polyfit(xx,yy,p);
XX = ones(1,nn);
for j = 1:p
XX = [XX;xs^j];
end;
yfit = b'*XX;
ymin = min(yfit); ymax = max(yfit);
nwindow = input('Enter the graphic window number:');
xset('window',nwindow);
xset('mark',-9,3);
//plot2d(xs,yfit);
//plot2d(xx,yy,-9);
plot2d(xx',yy',-9,'010','x',[xmin ymin xmax ymax])
plot2d(xs',yfit',1,'011','x',[xmin ymin xmax ymax])
//end function
Calling the function for p = 5, for example, produces the following:
-->getf('plotpoly')
-->plotpoly(x,y,5)
Enter the graphic window number:
--> 2
Download at InfoClearinghouse.com 26 2001 - Gilberto E. Urroz
Exercises
[1]. Using SCILAB rand function to generate a matrix A
2x2
(-->A = rand(2,2))and a matrix B
2x2
(-->B = rand(2,2)). Then, calculate the following:
(a)A
T
(b)A
-1
(c)B
T
(d)B
-1
(e)A+B (f)A-B
(g) 2A (h) -5B (i) 2A-5B (j) A
T
- B
T
(k) 3A
-1
+5B
T
(l) A B
(m)BA (n)A
-1
A (o)A
-1
A
T
(p) B
-1
A (q)A
-1
B
T
(r) ABB
T
(s) B
T
B (t) B
T
B+ A
-1
A
T
(u) A
-1
A
T
B
T
(v) norm(A,2) (w)det(A) (x)trace(A)
(y) rank(B) (z) cond(A)
[2]. Using SCILAB rand function generate a matrix A
3x3
and a matrix B
3x3
. Then, calculate the
following:
(a)A
T
(b)A
-1
(c)B
T
(d)B
-1
(e)A+B (f)A-B
(g) 2A (h) -5B (i) 2A-5B (j) A
T
- B
T
(k) 3A
-1
+5B
T
(l) A B
(m)BA (n)A
-1
A (o)A
-1
A
T
(p) B
-1
A (q)A
-1
B
T
(r) ABB
T
(s) B
T
B (t) B
T
B+ A
-1
A
T
(u) A
-1
A
T
B
T
(v) norm(A,2) (w)det(A) (x)trace(A)
(y) rank(B) (z) cond(A)
[3]. Using SCILAB rand function generate a matrix A
3x2
and a matrix B
3x2
. Then, if possible,
calculate the following:
(a)A
T
(b)A
-1
(c)B
T
(d)B
-1
(e)A+B (f)A-B
(g) 2A (h) -5B (i) 2A-5B (j) A
T
- B
T
(k) 3A
-1
+5B
T
(l) A B
(m)BA (n)A
-1
A (o)A
-1
A
T
(p) B
-1
A (q)A
-1
B
T
(r) ABB
T
(s) B
T
B (t) B
T
B+ A
-1
A
T
(u) A
-1
A
T
B
T
(v) norm(A,2) (w)norm(A,) (x)rank(A)
[4]. Using SCILAB rand function generate a matrix A
3x2
and a matrix B
2x3
. Then, if possible,
calculate the following:
(a)A
T
(b)A
-1
(c)B
T
(d)B
-1
(e)A+B (f)A-B
(g) 2A (h) -5B (i) 2A-5B (j) A
T
- B
T
(k) 3A
-1
+5B
T
(l) A B
(m)BA (n)A
-1
A (o)A
-1
A
T
(p) B
-1
A (q)A
-1
B
T
(r) ABB
T
(s) B
T
B (t) B
T
B+ A
-1
A
T
(u) A
-1
A
T
B
T
(v) norm(A,2) (w)norm(A,) (x)rank(A)
Download at InfoClearinghouse.com 27 2001 - Gilberto E. Urroz
[5]. Using SCILAB rand function generate matrices A
2x2
, and B
3x3
. Then, obtain symmetric
matrices A and B, and anti-symmetric matrices A and B such that A = A+A, and B = B+B.
Verify the results.
[6]. Generate the Vandermonde matrix, V, corresponding to the following vectors:
(a) [2,3,-1] (b) [5,5,-2,4] (c) [1,1,2,3,9] (d) [1,2,3,4,5,6]
[7]. For the matrices generated in [6] determine:
(a) determinant (b) rank (c) condition number (e)inverse
[8]. Generate the Hilbert matrix, H, of dimensions (a) 22, (b)33, (c) 44, and (d)55.
[9]. For the matrices generated in [6] determine:
(a) determinant (b) rank (c) condition number (e)inverse
[10]. Consider the system of linear equations given by:
(a) Solve the system of linear equations using Cramers rule
(b) solve the system of linear equations using matrices and the function linsolve
(c) solve the system of linear equations using x = A
-1
b
(d) solve the system of linear equations using Gaussian elimination and back substitution
(e) solve the system of linear equations using Gauss-Jordan elimination
(f) solve the system of linear equations using left division, i.e., x = A\b
[11]. Consider the system of linear equations:
2x+4y=2
x+y =2
2x+3y = 2
(a) Sketch the lines represented by the equations in the x-y plane with -5<x<5, -5<y<5. Is
there a unique solution for the system?
(b) Obtain a solution to the system by using SCILAB function leastsq. Sketch the solution
point together with the lines.
(c) Determine the error involved in this solution.
[12]. Consider the system of linear equations:
5x - 2y + 3z = 10
x - 3y + 4z = 20
(a) Obtain a solution to the system using the function linsolve
(b) Obtain a solution to the system using the function leastsq
(c) What is the rank of the matrix of coefficients for this linear system

1
]
1
1
1
1
1
1
1
1
+ X 2 Y Z
+ 2 X 2 Y Z 3 W
+ + + 2 X 5 Y Z W
+ + X 2 Z 2 W

1
]
1
1
1
1
1
1
1
1
3
-1
9
1
Download at InfoClearinghouse.com 28 2001 - Gilberto E. Urroz
[13]. Consider the following systems of linear equations:
X +2Y+3Z = 28, 2X +4Y+6Z = 18, 2X +4Y+6Z = -4,
3X -2Y+ Z = 4, 3X -2Y+ Z = -10, 3X -2Y+ Z = 4,
4X +2Y -Z = 10, 4X +2Y -Z = 38, 4X +2Y -Z = 24.
(a) Solve the multiple linear system by using matrices and the function linsolve
(b) Solve the multiple linear system by using an augmented matrix and Gauss-Jordan
elimination
(c) Solve the multiple linear system by using the inverse matrix of coefficients
(d) Obtain the inverse of the matrix of coefficients by using the appropriate augmented
matrix and Gauss-Jordan elimination. Verify this solution by using the function inv
[14]. Given the matrix
corresponding to the eigenvalue problem: Ax = x,
(a) Obtain the characteristic matrix for the eigenvalue problem
(b) Obtain the characteristic polynomial corresponding to matrix A
(c) Plot the characteristic polynomial in the range -6 < < 10
(d) Solve the characteristic polynomial to obtain the eigenvalues, , of matrix A
(e) Obtain the eigenvalue, , of matrix A using the function spec
(f) Obtain the eigenvectors, x, of matrix A using the user-defined function eigenvectors
[15]. Given the matrices
corresponding to the generalized eigenvalue problem Ax = Bx,
(a) Obtain the characteristic matrix for the eigenvalue problem
(b) Obtain the characteristic polynomial corresponding to matrix A
(g) Plot the characteristic polynomial in the range -6 < < 10
(h) Solve the characteristic polynomial to obtain the eigenvalues, , of matrix A
(i) Obtain the generalized eigenvalues, , of matrix A using the function geigenvectors
(j) Obtain the eigenvectors, x, of matrix A using the function geigenvectors
[16]. Determine the matrices L, U, and P corresponding to the LU decomposition of
:= A

1
]
1
1
1
1
1
1
1
1
-1 0 5 4
0 3 -2 2
5 -2 4 1
4 2 1 3
:= A

1
]
1
1
1
1
1
-1 3 5
3 4 2
5 2 3
:= B

1
]
1
1
1
1
1
2 -3 2
-3 4 2
2 2 10
Download at InfoClearinghouse.com 29 2001 - Gilberto E. Urroz
(a) matrix A in problem [14]
(b) matrix A in problem [15]
(c) matrix B in problem [15].
[17]. Determine the matrices Q and R corresponding to the QR decomposition of
(a) matrix A in problem [14]
(b) matrix A in problem [15]
(c) matrix B in problem [15].
[18]. Determine the matrices U and V of left and right vectors and the vector of singular
values s corresponding to
(a) matrix A in problem [14]
(b) matrix A in problem [15]
(c) matrix B in problem [15].
[19]. Expand the quadratic form f(x)= x
T
Ax for x = [X,Y,Z]
T
, where matrix A represents
(a) matrix A in problem [14]
(b) matrix A in problem [15]
(c) matrix B in problem [15].
[20]. For the electric circuit shown below
Determine the electrical currents I
1
, I
2
, I
3
, and I
4
, associated with the circulation loops shown in
the figure, if
(a) R
1
= R
3
= R
5
= R
7
= R
9
= 1.5 k, R
2
= R
4
= R
6
= R
8
= 800 , V
1
= 12 V, V
2
= 24 V, V
3
= 6 V
(b) R
1
= R
3
= R
5
= R
7
= R
9
= R
2
= R
4
= R
6
= R
8
= 1.2 k, V
1
= 12 V, V
2
= V
3
= 6 V
(c) R
1
= R
3
= R
5
= R
7
= R
9
= 2.2 k, R
2
= R
4
= R
6
= R
8
= 1.2 k, V
1
= V
2
= V
3
= 18 V
(d) R
1
= R
3
= R
5
= R
7
= R
9
= 0.5 k, R
2
= R
4
= R
6
= R
8
= 0.8 k, V
1
= 6 V, V
2
= 12 V, V
3
= 6 V
[21]. The truss shown in the figure below is such that all horizontal and vertical bars are of
length 1.0 m, diagonal bars of length 1.4142 m, and all acute angles in the truss are 45
o
.
Download at InfoClearinghouse.com 30 2001 - Gilberto E. Urroz
Determine the axial forces in the truss elements if
(a) P
1
= 100 kN, P
2
= 200 kN, P
3
= 200 kN, P
4
= 100 kN.
(b) P
1
= P
2
= P
3
= P
4
= 200 kN.
(c) P
1
= 50 kN, P
2
= 150 kN, P
3
= 50 kN, P
4
= 150 kN.
(d) P
1
= 50 kN, P
2
= 150 kN, P
3
= 200 kN, P
4
= 250 kN.
To determine the reactions use the equations of moments taken about the points of application
of R
1
and R
2
, respectively:
- 3R
2

+ 1(P
1
+ P
3
) + 2 (P
2
+ P
4
) = 0
3R
1
- 1 (P
2
+ P
4
) - 2 (P
1
+ P
3
) = 0
[22]. Obtain dimensionless numbers to describe a fluid mechanics experiment that involves the
following variables:
H = a characteristic water depth(L)
Q = a flow rate (LT
-3
)
= dynamic viscosity of a fluid (ML
-1
T
-1
)
= specific weight of a fluid (ML
-2
T
-2
)
P
0
= a characteristic pressure in the flow (ML
-1
T
-2
)
g = acceleration of gravity (LT
-2
)
Let , H, Q and , be the repeating variables in the dimensionless numbers.
[23]. The state of stress at a point within a solid in equilibrium is given by the stress tensor
where the components of T represent stresses in kPa. For a plane passing through the point of
interest with a normal vector given by n = [5, -5, 2], determine:
(a) the total stress on the plane
(b) the normal stress on the plane
(c) the shear stress on the plane
:= T

1
]
1
1
1
1
1
12 -22 40
-22 -10 5
40 5 15
Download at InfoClearinghouse.com 31 2001 - Gilberto E. Urroz
(d) the total, normal, and shear forces on the plane if the area of the plane is 0.0005 m
2
.
[24]. For the stress tensor given in problem [23] determine:
(a) the principal stresses
(b) the vectors corresponding to the principal axes
[25]. The table below shows sediment load data (y, in kg/min) obtained in a laboratory flume
under controlled conditions. The sediment load, y, is known to be a function of the water
discharge, x
1
(lt/sec), of the mean sediment diameter, x
2
(cm), and of the flume slope, x
3
(10
-3
m/m). Using matrices determine a multiple linear fitting of the form
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3
for the data provided in the table.
x
1
x
2
x
3
y
1.20 0.50 3.5 27.35
1.40 0.75 4.5 29.86
1.60 1.00 5.5 35.15
1.80 1.50 3.5 33.45
2.00 2.00 4.5 38.98
2.20 2.50 5.5 43.35
2.40 0.50 3.5 30.72
2.60 0.75 4.5 34.13
2.80 1.00 5.5 38.45
3.00 1.50 3.5 37.12
3.20 2.00 4.5 42.83
3.40 2.50 5.5 47.12
[26]. The table below shows the water discharge, y(10
3
cubic feet per second), measured at a
gage station in a large river as a function of time, x(days), during a 40-day period in the early
spring season. A plot of y vs. x is known as a hydrograph.
x y
1.5 101.42
5.0 176.73
8.5 311.22
12.0 389.61
15.5 546.24
19.0 638.14
22.5 716.99
26.0 743.60
29.5 737.96
33.0 623.16
36.5 492.53
Download at InfoClearinghouse.com 32 2001 - Gilberto E. Urroz
40.0 15.84
(a) Determine a polynomial fitting for this hydrograph with p = 2, 3, 4, 5, and 6, where the
polynomial fitting is of the form
y = b
0
+ b
1
x + b
2
x
2
+ + b
p
x
p
(b) Select the best polynomial fitting for the hydrograph based on the values of the
correlation coefficient and of the sum of squared errors, SSE.
(c) Plot the original hydrograph data and the fitted polynomial in the same set of axes.
(d) The area under the curve for 0 < x < 40 days, represents the total volume of water
passing through the mouth of the river in that period. Using the fitted polynomial and
the function int (integral) estimate the volume of water passing through the gage
station in the 40-day period of interest.
Download at InfoClearinghouse.com 33 2001 - Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 34 2001 - Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Solving Non-linear Equations with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
SOLUTION TO NON-LINEAR EQUATIONS 2
Introduction to complex numbers 2
Examples of basic complex number operations in SCILAB 3
Complex number calculations 5
Examples of operations with complex numbers 6
Solution to quadratic and cubic equations 7
Quadratic equations 7
Cubic equations 9
The many roots of a real or complex number 11
Principal values of a cubic root 13
Polynomials and solutions to polynomial equations 14
Solution of a single non-linear equation 16
Interval-halving or bisection method 17
Pseudo-code for the interval-halving or bisection method 18
SCILAB function for interval-halving or bisection 18
Example of interval-halving (bisection) method application 19
The Newton-Raphson method 20
A SCILAB function for the Newton-Raphson method 21
The Secant Method 24
A SCILAB function for the secant method 25
Application of secant method 25
Fixed -point iteration 26
A SCILAB function for fixed iteration 26
Applications of fixed-point iteration 27
Solving systems of non-linear equations 28
SCILAB function for Newton-Raphson method for a system of non-linear equations 30
Illustrating the Newton-Raphson algorithm for a system of two non-linear equations 31
Solution using function newtonm 32
Secant method to solve systems of non-linear equations 33
Illustrating the secant algorithm for a system of two non-linear equations 34
SCILAB function for secant method to solve systems of non-linear equations 36
Solution using function secantm 37
Solving non-linear equations with the fsolve function 37
Solving single non-linear equations with fsolve 37
Solving a system of non-linear equations with fsolve 38
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Solution to non-linear equations
Non-linear equations covered in this chapter include quadratic, cubic, and polynomial
equations. These are known as algebraic equations. We also consider equations involving
non-algebraic terms such as exponential, logarithmic, trigonometric, and hyperbolic functions.
These would be referred to as transcendental equations, since those types of functions are
known as transcendental functions (i.e., they transcend the realm of algebraic equations).
Even the simplest of algebraic equations, namely, quadratic equations, may produce a complex
solution. Therefore, we start this chapter with a quick introduction to complex numbers. The
chapter continuous with numerical solutions to quadratic, cubic, polynomial, and
transcendental equations. Methods presented include bisection, Newton-Raphson, and secant.
The chapter concludes with the analysis of systems of non-linear equations.
I In nt tr ro od du uc ct ti io on n t to o c co om mp pl le ex x n nu um mb be er rs s
A complex number z is a number written as
z = x + iy,
where x and y are real numbers, and i is the imaginary unit defined by I
2
= -1.
The complex number x+iy has a real part,
x = Re(z),
and an imaginary part,
y = Im(z).
We can think of a complex number as a point P(x,y) in the x-y plane, with the x-axis referred
to as the real axis, and the y-axis referred to as the imaginary axis. Thus, a complex number
represented in the form x+iy is said to be in its Cartesian representation.
A complex number can also be represented in polar coordinates (polar representation) as
z = re
i
= rcos + I rsin
where
r = |z| = (x
2
+y
2
)
1/2
is the magnitude of the complex number z, and
= Arg(z) = arctan(y/x)
is the argument of the complex number z.
The relationship between the Cartesian and polar representation of complex numbers is given
by the Euler formula:
e
i
= cos + i sin
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
The complex conjugate of a complex number z = x + iy = re
i
,, is
z = x iy = re
-I
.
The complex conjugate of z can be thought of as the reflection of z about the real (x-) axis.
Similarly, the negative of z,
z = -x-iy = - re
i
,
can be thought of as the reflection of z about the origin.
Examples of basic complex number operations in SCILAB
The unit imaginary number, i = (-1), is represented in SCILAB by the symbol %i. A sequence of
the first 10 powers of I can obtained by using:
-->ipow = []; for j=1:10, ipow=[ipow %i^j]; end; ipow
ipow =
! i - 1. - i 1. i - 1. - i 1. i - 1. !
A complex number, say z = 3+5i, is written as:
-->z =3+5*%i
z =
3. + 5.i
The functions real and imag can be used to obtain the real and imaginary parts, respectively,
of a complex number, for example:
-->real(z), imag(z)
ans =
3.
ans =
5.
The magnitude and argument of the complex number z are obtained as:
-->abs(z), atan(imag(z)/real(z))
ans =
5.8309519
ans =
1.0303768
If we write the polar representation of a complex number as r exp(%I*theta), SCILAB returns
the Cartesian representation. For example,
-->5*exp(0.25*%i)
ans =
4.8445621 + 1.2370198i
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
SCILAB provides the function polar to obtain the magnitude and argument of a complex
number. The following example illustrates its application:
-->[r,theta] = polar(z)
theta =
1.0303768
r =
5.8309519
The complex conjugate of a complex number is obtained by using the function conj:
-->conj(z)
ans =
3. - 5.i
The negative is simply obtained by adding a minus sign to the number, i.e.,
-->-z
ans =
- 3. - 5.i
Matrices in SCILAB can have complex numbers as elements. The following commands, for
example, produce a 3x4 matrix of complex numbers. First, we generate random matrices with
integer numbers to be the real and imaginary parts of the complex numbers:
-->xMs = int(10*rand(3,3)), yMs = int(10*rand(3,3))
xMs =
! 2. 5. 2. !
! 4. 5. 6. !
! 2. 1. 7. !
yMs =
! 0. 3. 4. !
! 6. 8. 2. !
! 2. 5. 8. !
Next, the complex-number matrix is put together by using:
-->zMs = xMs + yMs*%i
zMs =
! 2. 5. + 3.i 2. + 4.i !
! 4. + 6.i 5. + 8.i 6. + 2.i !
! 2. + 2.i 1. + 5.i 7. + 8.i !
To obtain the magnitude of the elements of the matrix just defined we simply apply the
function abs to the matrix name, i.e.,
-->abs(zMs)
ans =
! 2. 5.8309519 4.472136 !
! 7.2111026 9.4339811 6.3245553 !
! 2.8284271 5.0990195 10.630146 !
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
To find the arguments of the elements we can use the function atanm applied to the term-by-
term division of the imaginary part by the real part of the elements. The function atanm is a
generalization of the function atan used when the argument is a matrix. Here is the
calculation of the arguments:
-->atanm(yMs./xMs)
ans =
! - .8101607 .5954016 .9174249 !
! .7153096 - .0687575 .2626142 !
! .6906707 2.5474625 - .2654686 !
Complex number calculations
Complex numbers can be added, subtracted, multiplied, and divided. The rules for these
operations are shown below:
Let z = x + iy

= re
i
, z
1
= x
1
+ iy
1
= r
1
e
i
1
, and z
2
= x
2
+ iy
2
= r
2
e
i
2
,
be complex numbers. In these definitions the numbers x, y, x
1
, x
2
, y
1
, and y
2
are real numbers.
A Ad dd di it ti io on n: z
1
+ z
2
= (x
1
+ x
2
)+ i (y
1
+ y
2
)
S Su ub bt tr ra ac ct ti io on n: z
1
- z
2
= (x
1
- x
2
)+ i (y
1
- y
2
)
M Mu ul lt ti ip pl li ic ca at ti io on n: z
1
z
2
= (x
1
x
2
- y
1
y
2
) + i(x
1
y
2
+ x
2
y
1
) = r
1
r
2
e
i(
1
+
2
)
.
M Mu ul lt ti ip pl li ic ca at ti io on n o of f a a n nu um mb be er r b by y i it ts s c co on nj ju ug ga at te e results in the square of the numbers magnitude,
i.e.:
zz

= (x + iy)(x - iy) = x
2
+ y
2
= r
2
= |z|
2
D Di iv vi is si io on n:
P Po ow we er rs s: z
n
= (re
i
)
n
= r
n
e
in
R Ro oo ot ts s: because the argument of a complex number z has a periodicity of 2, we can write
z = re
i(+2k)
, for k = 0, 1, 2,
There are n n-th roots of z calculated as
z
z
z
z
z
z
z z
z
x x y y
x y
i
y x x y
x y
r
r
e
i 1
2
1
2
2
2
1 2
2
2
1 2 1 2
2
2
2
2
1 2 1 2
2
2
2
2
1
2
1 2

_
,

_
,

+
+
+

+


| |
.
( )
z z r e k n
n n n
i
k
n

+
1 1
2
0 1 2 1
/ /
( )
, , , , ( ).

!
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
More details on the calculation of roots of complex numbers are presented later in the chapter.
E Ex xa am mp pl le es s o of f o op pe er ra at ti io on ns s w wi it th h c co om mp pl le ex x n nu um mb be er rs s
The following are operations with complex numbers using SCILAB:
-->z1 = -5+2*%i, z2 = 3+4*%i
z1 =
- 5. + 2.i
z2 =
3. + 4.i
Addition, subtraction, multiplication, and division:
-->z1+z2
ans =
- 2. + 6.i
-->z1-z2
ans =
- 8. - 2.i
-->z1*z2
ans =
- 23. - 14.i
-->z1/z2
ans =
- .28 + 1.04i
The following sequence provides the first 5 integer powers of z1:
-->z1pow = []; for j=1:5, z1pow=[z1pow z1^j]; end; z1pow
z1pow =
column 1 to 4
! - 5. + 2.i 21. - 20.i - 65. + 142.i 41. - 840.i !
column 5
! 1475. + 4282.i !
The following SCILAB command attempts to find one cubic root of z1:
-->z1^(1/3)
ans = 1.0613781 + 1.394917i
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
There are, in fact, three cubic roots for any real or complex number. These are calculated,
using the formulas indicated earlier, as follows. First, we calculate the magnitude and
argument of complex number z1:
-->r1=abs(z1), theta1 = atan(imag(z1)/real(z1))
r1 = 5.3851648
theta1 = - .3805064
Next, we create an empty vector called cubic_roots_of_z1:
-->cubic_roots_of_z1 = [];
The vector is then filled with the three roots of z1 as follows:
-->for k = 0:2
--> cubic_roots_of_z1 = [cubic_roots_of_z1
r1^(1/3)*exp(%i*((theta1+2*k*%pi)/3))];
-->end;
To see the final result, enter the name of the vector:
-->cubic_roots_of_z1
cubic_roots_of_z1 =
column 1 to 2
! 1.7387226 - .2217219i - .6773445 + 1.6166389i !
column 3
! - 1.0613781 - 1.394917i !
More details on the calculation of roots of real and complex numbers are presented in a
subsequent section of this chapter.
S So ol lu ut ti io on n t to o q qu ua ad dr ra at ti ic c a an nd d c cu ub bi ic c e eq qu ua at ti io on ns s
Quadratic equations, of the form ax
2
+bx+c = 0, and cubic equations, of the form ax
3
+bx
2
+cx+d =
0, are the simplest non-linear, polynomial equations. SCILAB provides function roots to solve
polynomial equations of any order. Therefore, function roots can be used to solve quadratic
and cubic equations. The methods presented next for solving quadratic and cubic equations,
therefore, are not strictly necessary, however, they are presented here as SCILAB programming
exercises.
Quadratic equations
Quadratic equations are those algebraic equations with one unknown that can be reduced to
the form
ax
2
+bx+c = 0,
where a, b, and c are real numbers.
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
The solution to a quadratic equation can be accomplished by re-writing the equation as
x
2
+(b/a)x= -(c/a)
and completing the square of a binomial in the left hand side. To complete the square of a
binomial, add the term b
2
/(4a
2
) to both sides of the equation:
x
2
+(b/a)x+b
2
/(4a
2
)= -(c/a)+ b
2
/(4a
2
)
The equation can now be re-written as
(x+b/(2a))
2
= (b
2
-4ac)/(4a
2
).
Taking the square root of both sides of the equation results in
x+b/(2a) = (b
2
-4ac)
1/2
/(2a).
Solving for x results in
x = [-b (b
2
-4ac)
1/2
]/(2a).
The quantity under the square root in this result is known as the discriminant of the equation,
i.e.,
D = b
2
-4ac.
According to the sign of the discriminant, the equation can have one or two real solutions, or
two complex conjugate solutions:
If D>0, the quadratic equation has two distinct real solutions:
x
1,2
= (-b D
1/2
)/(2a).
If D=0, the quadratic equation has one real (double) solution,
x
1
= x
2
= -b/(2a).
If D<0, the quadratic equation has two conjugate complex solutions,
x
1,2
= (-b i(-D)
1/2
)/(2a),
where i = (-1)
1/2
is the unit imaginary number.
The basic algorithm (written in pseudo-code) for solving a quadratic equation, would look
something like this:
Enter equation coefficients a, b, c
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Calculate discriminant, D = b
2
-4ac
If D > 0,
Indicate that two distinct real solutions exist
Calculate roots as x
1
= (-b+D
1/2
)/(2a), and x
2
= (-b-D
1/2
)/(2a)
Display roots x
1
and x
2
Else If D = 0,
Indicate that only one real solution exist
Calculate root as x = -b/(2a)
Display single root x
Else (default case D<0)
Indicate that two conjugate complex solutions exist
Calculate the real part of the complex roots as x
r
= -b/(2a)
Calculate the imaginary part of the roots as x
i
= (-D)
1/2
Display the roots as 'x
1
=x
r
+i*x
i
' and 'x
2
=x
r
-i*x
i
'
The reader is invited to write a SCILAB function to solve the quadratic equation given the
values a, b, and c.
Note: Pseudo-code is a way to present an algorithm using English-like statements that are later
coded into a particular computer language. The statements are general enough to be
understood by any programmer and easily translated into a computer language.
Cubic equations
The canonical (simplest) form of the cubic equation is
x
3
+ax
2
+bx+c = 0,
where a, b, c are real numbers.
Substituting the auxiliary variable
y = x + a/3
leads to the reduced form
y
3
+py+q = 0,
with
p = (3b-a
2
)/3,
and
q = c + 2a
3
/27 ab/3.
A discriminant for the reduced cubic equation is given by
D = (p/2)
2
+(q/3)
3
.
An Italian mathematician, Gierolimo Cardano, proposed the following solutions to the cubic
equation:
x
1
= -(a/3)+u+v
x
2,3
= -(a/3)-(u+v)/2 t i3(u-v)/2
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
where
.
2
,
2
3 3
D
q
v D
q
u +
Depending on the sign of the discriminant, D, the solutions to the cubic equation can be
classified as follows:
If D > 0, one real and two complex conjugate solutions (calculated using the formulas shown
above).
If D = 0, three real solutions including a double solution (x
2
and x
3
).
If D < 0, three distinct real solutions (this case is known as the irreducible case).
In the irreducible case, the formulas shown above for the three roots of the cubic equation will
introduce complex expressions because the calculation of u and v involves the square root of
D<0. To avoid introducing such complex expressions, we can use the so-called trigonometric
form of the solution:
),
3
cos(
3
| |
2
3
1
p a
x +
),
3
cos(
3
| |
2
3
2

+
p a
x
and
),
3
cos(
3
| |
2
3
3
+
+
p a
x
where
.
3
| |
2
) cos(
3

,
_


p
q

The algorithm for solving the cubic equation can be written in pseudo-code as follows:
Enter coefficients for the cubic equation: a,b,c
Calculate p and q
Calculate the discriminant D
If D > 0
Indicate that there one real and two complex conjugate solutions
Calculate u and v (use subroutine for cubic equation see below)
Calculate the real root, x
1
= -(a/3)+u+v
Calculate the real part of roots x
2,3
, x
r
= -(a/3)+(u+v)/2
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Calculate the imaginary part of roots x
2,3
, x
i
= 3

(u-v)/2
Display roots x
1
, 'x
2
= x
r
+ x
i
*i' and 'x
3
= x
r
- x
i
*i'
Else D = 0
Indicate that there are three real solutions with one double root
Calculate x
1
= -(a/3)+u+v, x
2,3
= -(a/3) (u+v)/2
Display roots x
1
, x
2
= x
3
Else (Default case, D<0)
Indicate that there are three distinct real solutions
Calculate cos() and

Calculate the three roots using the trigonometric form, i.e.,

1
= /3, x
1
= -a/3 + 2(|p|/3)
1/2

cos
1

2
= ()/3, x
1
= -a/3 + 2(|p|/3)
1/2

cos
2
3 = (+)/3, x1 = -a/3 + 2(|p|/3)
1/2

cos
3
Display roots x1, x2, and x3
The calculation of u and v for the case D>0 requires us to calculate cubic roots, namely,
.
2
,
2
3 3
D
q
D
q
+
I suggest you use the following user-defined SCILAB function to obtain those cubic roots:
function RealCubicRoot(x)
//This function calculates the cubic root of a real number
One_Third = 1.0/3.0
if x < 0 then
RealCubicRoot = -(Abs(x)^One_Third)
elseif x == 0 then
RealCubicRoot = 0
else
Real CubicRoot = Abs(x)^One_Third
end
//End of the function
The function RealCubicRoot calculates the real cubic root corresponding to a real number. The
functions is designed so that if the real number is positive so is its cubic root, and if the
number is negative so is its cubic root. With this function we ensure that we always get a real
cubic root and that the calculation is not encumbered with complex number operations.
T Th he e m ma an ny y r ro oo ot ts s o of f a a r re ea al l o or r c co om mp pl le ex x n nu um mb be er r
Consider a complex number
z = re
i =
rcos + irsin .
Because the functions sin and cos are periodic functions of period 2 in , the complex number
z can be written as
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
z = re
i(+2k)
,
k = 0, t1, t2, t3, ...
In other words, there are infinite ways to represent the complex number z. The most general
representation being
z = re
i(+2k)
.
To obtain the n-th root of this complex number we can write
z
1/n
= r
1/n
e
i[(/n)+2k/n]
.
You can check that we need only use the values k = 0, 1, 2, ... (n-1), to produce all n
independent roots of the number z.
Example 1: Consider the number z = 16e
i
= 16 cos + i 16 sin = -16, which is actually a
real number. Calculate the roots corresponding to z
1/4
:
From
z = 16e
i
= 16e
i(+2k)
= 16e
i(1+2k)
,
it follows that
r = z
1/4
= 16
1/4
e
i(1+2k)(/4)
= 2e
i(1+2k)(/4)
, k = 0, 1, 2, 3
Thus,
!For k = 0, r
1
= 2e
i(1+20)(/4) =
2e
i/4 =
2cos(/4) + i2sin(/4) = 2(1 + i)
!For k = 1, r
2
= 2e
i(1+21)(/4) =
2e
i3/4 =
2cos(3/4) + i2sin(3/4) = 2(-1 + i)
!For k = 2, r
3
= 2e
i(1+22)(/4) =
2e
i5/4 =
2cos(5/4) + i2sin(5/4) = -2(1 + i)
!For k = 3, r
3
= 2e
i(1+23)(/4) =
2e
i7/4 =
2cos(7/4) + i2sin(7/4) = 2(-1 + i)
Using values such as k = -1 or k = 4 in the general expression for the 4-th root of z = -16 will
produce values already accounted for in the four results found above. This example,
therefore, verifies that there are exactly n independent n-th roots of a complex number, with
n = 4 for this case.
Notice that the results of this example actually correspond to the 4-th roots of a real number, z
= -16. Since real numbers are special cases of complex numbers, the approach outlined above
for finding the n-th root of a complex number is also applicable to a real number. The only
requirement is that the number be written in the Polar form, z = re
i
.
Using SCILAB, the previous calculations are performed as follows:
-->z = -16; r=abs(z); theta = -%pi;
-->roots_of_z = [];
-->for k = 0:3
--> roots_of_z = [roots_of_z r^(1/4)*exp(%i*((theta+2*k*%pi)/n))];
-->end;
-->roots_of_z
roots_of_z =
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
column 1 to 2
! 1.4142136 - 1.4142136i 1.4142136 + 1.4142136i !
column 3 to 4
! - 1.4142136 + 1.4142136i - 1.4142136 - 1.4142136i !
Principal values of a cubic root
The principal value for the n-th root of a complex (or real) number,
z = re
i
,
is the value corresponding to k = 0 in the general expression:
z
1/n
= r
1/n
e
i[(/n)+2k/n]
,
i.e.,
z
1/n
= r
1/n
e
i/n
.
For the case of cubic root (n = 3), if the number z is a positive real number, = 0, and the
result is straightforward, z
1/3
= r
1/3
. If the number z is a negative real number, , and
the principal value is z
1/3
= r
1/3
e
i/3 =
r
1/3
(cos /3 + i sin /3). Thus, the principal value
of the root of a complex number is not necessarily always a real number.
The following SCILAB exercise shows the three cubic roots of a positive and a negative number.
Example 1 - Cubic roots of a positive number:
To calculate the cubic roots of z = 81, with r = 81 and = 0, we use:
-->z = 81, r = abs(z), theta = 0
z =
81.
r =
81.
theta =
0.
-->for k = 0:2
--> r^(1/3)*exp(%i*(theta+2*k*%pi)/3)
-->end
ans =
4.3267487
ans =
- 2.1633744 + 3.7470743i
ans =
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
- 2.1633744 - 3.7470743i
Example 2 - Cubic roots of a negative number:
To calculate the cubic roots of z = 8, with r = 9 and = , we use:
-->z = -8, r = abs(z), theta = %pi
z =
- 8.
r =
8.
theta =
3.1415927
-->for k = 0:2
--> r^(1/3)*exp(%i*(theta+2*k*%pi)/3)
-->end
ans =
1. + 1.7320508i
ans =
- 2. + 2.449E-16i // Note: this is equivalent to -2.0
ans =
1. - 1.7320508i
P Po ol ly yn no om mi ia al ls s a an nd d s so ol lu ut ti io on ns s t to o p po ol ly yn no om mi ia al l e eq qu ua at ti io on ns s
Polynomials are one of the categories of SCILAB data types. Polynomials are defined using the
function poly. In Chapter 5 we indicated, for example, that for a square matrix A, the
characteristic equation can be obtained by using poly(A,lam), where lam is the independent
variable defining the resulting polynomial. We also indicated that we can define a simple
symbolic variable, say, s, by using s = poly(0,s).
In this section we are interested in defining a polynomial of order n, namely,
P = a
0
+a
1
x+a
2
x
2
+a
3
x
3
++a
n
x
n
,
we use a row vector containing the coefficients as [a
0
a
1
a
2
a
n
], and call function poly using
this vector, the independent variable that defines the polynomial, say, x, and the qualifier
coeff. For example, to define a fourth-order polynomial with coefficients [1 -5 0 3 2],
use:
-->p4 = poly([1 -5 0 3 2],'x','coeff')
p4 =
3 4
1 - 5x + 3x + 2x
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
A polynomial of order 10 in y is generated next. The coefficients of the polynomial are stored
in vector c:
-->c = [23 0 -122 34 -20 0 4 525 -2 11 3]
c =
column 1 to 10
! 23. 0. - 122. 34. - 20. 0. 4. 525. - 2. 11. !
column 11
! 3. !
The resulting polynomial is:
-->p10 = poly(c,'y','coeff')
p10 =
2 3 4 6 7 8 9 10
23 - 122y + 34y - 20y + 4y + 525y - 2y + 11y + 3y
To find the roots of a polynomial use the function roots. For example, the roots of the fourth-
order polynomial p4 are:
-->roots(p4)
ans =
! .2059620 !
! - 1.3030302 + .9995932i !
! - 1.3030302 - .9995932i !
! .9000985 !
The following example provides the roots of the polynomial p10:
-->roots(p10)
ans =
! .2308180 + .7299993i !
! .2308180 - .7299993i !
! - .3995841 !
! - .5911741 + .5126340i !
! - .5911741 - .5126340i !
! .4935580 !
! .6187957 !
! - 7.1652026 !
! 1.7532393 + 4.6211667i !
! 1.7532393 - 4.6211667i !
The next example combines functions roots and poly to calculate the roots of a polynomial
given its coefficients in the vector [2 -3 5 4 2 100]:
-->roots(poly([2 -3 5 4 2 100],'s','coeff'))
ans =
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
! .3143499 + .2557167i !
! .3143499 - .2557167i !
! - .0539859 + .4715218i !
! - .0539859 - .4715218i !
! - .5407282 !
The fundamental theorem of algebra indicates that a polynomial of integer order n has a total
of n roots, among real and complex roots. It also indicates that if a complex number z is a root
of a polynomial, then its complex conjugate z is also a root of the polynomial. These facts
are illustrated in the examples worked above.
More information on polynomials is provided in Chapter 7.
S So ol lu ut ti io on n o of f a a s si in ng gl le e n no on n- -l li in ne ea ar r e eq qu ua at ti io on n
In an earlier section of this chapter we presented algorithms for the solution of quadratic and
cubic equations. These two types of equations are examples of polynomial equations of the
form:
a
n
x
n
+ a
n-1
x
n-1
+ a
n-2
x
n-2
++ a
2
x
2
+ a
1
x + a
0
= 0,
with n = 2 corresponding to the quadratic equation, and n = 3 to the cubic equation. A linear
equation would be one for which n = 1, and its solution is straightforward. Equations that can
be cast in the form of a polynomial are referred to as algebraic equations. Equations
involving more complicated terms, such as trigonometric, hyperbolic, exponential, or
logarithmic functions are referred to as transcendental equations.
No general algorithms for solution exist for algebraic (or polynomial) equations of order n 4,
or for transcendental equations. The methods presented in this section are numerical methods
that can be applied to the solution of such equations, to which we will refer, in general, as
non-linear equations.
In general, we will we searching for one, or more, solutions to the equation,
f(x) = 0.
We will present the methods of interval halving (or bisection method, the Newton-Raphson
algorithm, the secant method, and the fixed iteration method. In the interval-halving
method as well as in the secant method we need to provide two initial values of x to get the
algorithm started. In the fixed iteration and Newton-Raphson methods only one initial value is
required.
Because the solution is not exact, the algorithms for any of the methods presented herein will
not provide the exact solution to the equation f(x) = 0, instead, we will stop the algorithm
when the equation is satisfied within an allowed tolerance or error, . In mathematical terms
this is expressed as
|f(x
R
)| < .
The value of x for which the non-linear equation f(x)=0 is satisfied, i.e., x = x
R
, will be the
solution, or root, to the equation within an error of units.
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
Interval-halving or bisection method
This method starts by identifying two values of x, a and b, with a<b, for which the
corresponding function values, namely, f(a) and f(b), have opposite signs, i.e., , f(a) f(b) < 0.
Figure 1, below, presents two of such cases.
Obviously, since the function y = f(x) changes sign as x goes from x = a to x = b, somewhere
within the interval (a,b), x must take a value x
R
for which f(x) = 0. The mid-point of the
interval, namely,
c = 1/2(a+b),
will be closer to x
R
than either a or b can be.
Two situations are possible, as illustrated in the figure below: (1) a < x
R
< c < b; or, (2) a < c <
x
R
< b. In the first case, f(a) f(c) < 0, while f(b) f(c) > 0, and x
R
is contained in the interval
(a,c). In the second case, f(a) f(c) > 0, while f(b) f(c) < 0, and x
R
is contained in the interval
(c,b). We can think of c replacing b in the first case in a general interval (a,b), while c
replaces a in the interval (a,b) in the second case. In other words, in the first case b takes the
value of c, b"c, and in the second case, a"c. We can think of the method, therefore, as
finding the center-point of an ever-decreasing interval. The mid-point value replaces the
interval extreme for which the product of its function and that of the mid-point is a positive
number.
The process is then repeated until a value of c is found so that (3) is satisfied, i.e., until
|f(c)| < .
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
P Ps se eu ud do o- -c co od de e f fo or r t th he e i in nt te er rv va al l- -h ha al lv vi in ng g o or r b bi is se ec ct ti io on n m me et th ho od d
The following is one possible pseudo-code for the interval-halving method:
1. Function f(x) must be loaded through deff or getf.
2. Enter initial values of a and b.
3. Check that a < b, if not, send message indicating error in input data and requesting user to
re-enter values of a and b.
4. When input data is correct, check that f(a) f(b) < 0. If that is not the case, inform the
user that his/her initial values of a and b do not satisfy the problem conditions for solution.
5. If problem conditions are satisfied, proceed to calculate c according to c = 1/2(a+b).
6. Check if convergence condition, |f(c)| < , is satisfied. If it is so, print the solution and
stop.
7. If convergence solution is not satisfied, replace values of a or b according to the following
procedure: If f(b) f(c) > 0, b"c, else a" c.
8. Repeat procedure from step 5 on. Stop if the number of iterations is too large. Send a
message to user indicating that the process is not converging after a large number of
iterations.
S SC CI IL LA AB B f fu un nc ct ti io on n f fo or r i in nt te er rv va al l- -h ha al lv vi in ng g o or r b bi is se ec ct ti io on n
The following function, half.txt, can be used to obtain a solution using interval halving. The
function looks like this:
function [x]=half(a,b,f)
//interval halving routine
N = 100; eps = 1.e-5; // define max. no. iterations and error
if (f(a)*f(b) > 0) then
error('no root possible f(a)*f(b) > 0')
abort;
end;
if(abs(f(a)) < eps) then
error('solution at a')
abort;
end;
if(abs(f(b)) < eps) then
error('solution at b')
abort;
end;
while (N > 0)
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
c = (a+b)/2;
if(abs(f(c)) < eps) then
x = c;
x
return;
end;
if(f(a)*f(c) < 0)then
b = c;
else
a = c;
end;
N = N - 1;
end;
error('No convergence')
abort;
//end function
E Ex xa am mp pl le e o of f i in nt te er rv va al l- -h ha al lv vi in ng g ( (b bi is se ec ct ti io on n) ) m me et th ho od d a ap pp pl li ic ca at ti io on n
Let's apply it to an example, first define the function p(x) as follows:
-->deff('[y]=p(x)',['y=x^3-2*x^2-2*x-1'])
We will produce a plot of the function to check possible solutions visually, for example:
-->xx = 0:0.1:5; yy = p(xx); //(x,y) data
-->size(xx) //size of x to produce the x-axis
ans =! 1. 51. !
-->zeroLine = zeros(1,51); //data for the x-axis (all zeros)
-->plot2d([xx' xx'],[yy' zeroLine']) //plot function and x-axis
The figure shows a root between 2 and 3. The next step in the solution is to load function
half, and call it with the proper arguments:
-->getf('half);
-->x = half(2,3,p)
x =
2.8311768
To check the solution evaluate the function at the value of x:
-->p(x)
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
ans =
- .0000048
The function does not evaluates to zero. However, the error involved is in the order of 5x10
-6
,
small enough to accept the solution provided.
The Newton-Raphson method
Consider the Taylor-series expansion of the function f(x) about a value x = x
o
:
f(x)= f(x
o
)+f'(x
o
)(x-x
o
)+(f"(x
o
)/2!)(x-x
o
)2+.
Using only the first two terms of the expansion, a first approximation to the root of the
equation
f(x) = 0
can be obtained from
f(x) = 0 f(x
o
)+f'(x
o
)(x
1
-x
o
)
Such approximation is given by,
x
1
= x
o
- f(x
o
)/f'(x
o
).
The Newton-Raphson method consists in obtaining improved values of the approximate root
through the recurrent application of equation (8). For example, the second and third
approximations to that root will be given by
x
2
= x
1
- f(x
1
)/f'(x
1
),
and
x
3
= x
2
- f(x
2
)/f'(x
2
),
respectively.
This iterative procedure can be generalized by writing the following equation, where i
represents the iteration number:
x
i+1
= x
i
- f(x
i
)/f'(x
i
).
After each iteration the program should check to see if the convergence condition, namely,
|f(x
i+1
)|<,
is satisfied.
The figure below illustrates the way in which the solution is found by using the Newton-
Raphson method. Notice that the equation f(x) = 0 f(x
o
)+f'(x
o
)(x
1
-x
o
) represents a straight
line tangent to the curve y = f(x) at x = x
o
. This line intersects the x-axis (i.e., y = f(x) = 0) at
the point x
1
as given by x
1
= x
o
- f(x
o
)/f'(x
o
). At that point we can construct another straight
line tangent to y = f(x) whose intersection with the x-axis is the new approximation to the root
of f(x) = 0, namely, x = x
2
. Proceeding with the iteration we can see that the intersection of
consecutive tangent lines with the x-axis approaches the actual root relatively fast.
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
The Newton-Raphson method converges relatively fast for most functions regardless of the
initial value chosen. The main disadvantage is that you need to know not only the function
f(x), but also its derivative, f'(x), in order to achieve a solution. The secant method, discussed
in the following section, utilizes an approximation to the derivative, thus obviating such
requirement.
The programming algorithm of any of these methods must include the option of stopping the
program if the number of iterations grows too large. How large is large? That will depend of
the particular problem solved. However, any interval-halving, fixed iteration, Newton-
Raphson, or secant method solution that takes more than 1000 iterations to converge is either
ill-posed or contains a logical error. Debugging of the program will be called for at this point
by changing the initial values provided to the program, or by checking the program's logic.
A A S SC CI IL LA AB B f fu un nc ct ti io on n f fo or r t th he e N Ne ew wt to on n- -R Ra ap ph hs so on n m me et th ho od d
The function newton, listed below, implements the Newton-Raphson algorithm. It uses as
arguments an initial value and expressions for f(x) and f'(x).
function [x]=newton(x0,f,fp)
//newton-raphson algorithm
N = 100; eps = 1.e-5; // define max. no. iterations and error
maxval = 10000.0; // define value for divergence
xx = x0;
while (N>0)
xn = xx-f(xx)/fp(xx);
if(abs(f(xn))<eps)then
x=xn
disp(100-N);
return(x);
end;
if (abs(f(xx))>maxval)then
disp(100-N);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
error('No convergence');
abort;
//end function
We will use the Newton-Raphson method to solve for the equation, f(x) = x
3
-2x
2
+1 = 0. The
following SCILAB commands define the function f(x) and its derivative, fp(x), and load the
function newton.txt:
-->deff('[y]=f(x)','y=x^3-2*x^2+1');
-->deff('[y]=fp(x)','y=3*x^2-2');
-->getf('newton')
To have an idea of the location of the roots of this polynomial we'll plot the function using the
following SCILAB commands:
-->x= (-0.8:0.01:2.0)'; y = f(x);
-->plot(x,y,'x','f(x)','my plot');
-->xgrid()
We see that the function graph crosses the x-axis somewhere between -.8 and -.4, between .8
and 1.2, and between 1.6 and 2.0. The following commands use the function newton with
different initial values. The nature of the function is such that most initial values converge to
either of the two real roots, a few diverge, but it is very difficult to make the algorithm
converge to the negative root. The number listed before the variable name ans in each
solution is the number of iterations required to obtain a solution.
-->newton(-1,f,fp)
0.
ans =
1.
-->newton(-10,f,fp)
52.
ans =
1.6180279
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
-->newton(-5,f,fp)
44.
ans =
1.6180278
-->newton(-0.1,f,fp)
49.
ans =
1.6180279
-->newton(-2,f,fp)
45.
ans =
1.6180405
-->newton(0,f,fp)
1.
ans =
1.
-->newton(0.5,f,fp)
0.
ans =
1.
-->newton(-0.8,f,fp)
47.
ans =
1.6180408
-->newton(-0.2,f,fp)
43.
ans =
1.6180405
-->newton(-0.1,f,fp)
49.
ans =
1.6180279
-->newton(-100,f,fp)
0.
!--error 9999
Solution diverges
at line 15 of function newton called by :
newton(-100,f,fp)
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
-->newton(-10,f,fp)
52.
ans =
1.6180279
-->newton(100,f,fp)
0.
!--error 9999
Solution diverges
at line 15 of function newton called by :
newton(100,f,fp)
-->newton(10,f,fp)
48.
ans =
1.6180405
The Secant Method
In the secant method, we replace the derivative f'(x
i
) in the Newton-Raphson method with
f'(x
i
) (f(x
i
) - f(x
i-1
))/(x
i
- x
i-1
).
With this replacement, the Newton-Raphson algorithm becomes
To get the method started we need two values of x, say x
o
and x
1
, to get the first
approximation to the solution, namely,
As with the Newton-Raphson method, the iteration is stopped when
|f(x
i+1
)|<.
Figure 4, below, illustrates the way that the secant method approximates the solution of the
equation f(x) = 0.
). (
) ( ) (
) (
1
1
1


i i
i i
i
i i
x x
x f x f
x f
x x
). (
) ( ) (
) (
0 1
1
1
1 2
x x
x f x f
x f
x x
o


Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
A A S SC CI IL LA AB B f fu un nc ct ti io on n f fo or r t th he e s se ec ca an nt t m me et th ho od d
The function secant, listed below, uses the secant method to solve for non-linear equations. It
requires two initial values and an expression for the function, f(x).
function [x]=secant(x0,x00,f)
//newton-raphson algorithm
N = 100; eps = 1.e-5; // define max. no. iterations and error
maxval = 10000.0; // define value for divergence
xx1 = x0; xx2 = x00;
while (N>0)
gp = (f(xx2)-f(xx1))/(xx2-xx1);
xn = xx1-f(xx1)/gp;
if(abs(f(xn))<eps)then
x=xn
disp(100-N);
return(x);
end;
if (abs(f(xn))>maxval)then
disp(100-N);
error('Solution diverges');
abort;
end;
N = N - 1;
xx1 = xx2;
xx2 = xn;
end;
disp(100-N);
error('No convergence');
abort;
//end function
A Ap pp pl li ic ca at ti io on n o of f s se ec ca an nt t m me et th ho od d
The following SCILAB commands define the function f(x), load the function secant.txt:
-->deff('[y]=f(x)','y=x^3-2*x^2+1')
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
-->getf('secant.txt')
The following commands call the function secant.txt and converge to a solution:
-->secant(-10.,-9.8,f)
11.
ans =
- .6180354
-->secant(1.0,1.2,f)
0.
ans =
1.
-->secant(5.0,5.2,f)
10.
ans =
1.6180341
Fixed -point iteration
Fixed point iteration consists in re-writing the equation f(x)=0 in the form
x = g(x).
This equation will now represent a recursive relation by writing it as
x
n+1
=g(x
n
).
To get the process started we use an initial value, x = x
0
. Then, we calculate x
1
= g(x
0
), x
2
=
g(x
1
), etc. Convergence is achieved whenever |f(x)| < , or whenever |x
n+1
- x
n
| < .
A A S SC CI IL LA AB B f fu un nc ct ti io on n f fo or r f fi ix xe ed d i it te er ra at ti io on n
The following function will perform the iterations, it will stop if there is divergence or if
convergence is achieved. The function is stored in file fixedp:
function [x]=fixedp(x0,f)
//fixed-point iteration
N = 100; eps = 1.e-5; // define max. no. iterations and error
maxval = 10000.0; // define value for divergence
xx = x0;
while (N>0)
xn = f(xx);
if(abs(xn-xx)<eps)then
x=xn
disp(100-N);
return(x);
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
end;
if (abs(f(xx))>maxval)then
disp(100-N);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
error('No convergence');
abort;
//end function
A Ap pp pl li ic ca at ti io on ns s o of f f fi ix xe ed d- -p po oi in nt t i it te er ra at ti io on n
We use fixed-point iteration to solve the equation
f(x) = 3x - exp(x) + 2 = 0,
with the following recurrence equations:
(a) x = g1(x) = exp(x) - (2x+2); (b) x = g2(x) = (exp(x)-2)/3; and, (c) x = g3(x) = ln(3x+2).
The following SCILAB commands define the three functions required:
-->deff('[y]=g1(x)','y=exp(x)-(2*x+2)');
-->deff('[y]=g2(x)','y=(exp(x)-2)/3');
-->deff('[y]=g3(x)','y=log(3*x+2)');
Next, we get the function fixedp:
-->getf('fixedp')
The following calls using function g1 diverge:
-->fixedp(1.0,g1)
!--error 9999
No convergence
at line 21 of function fixedp called by :
fixedp(1.0,g1)
-->fixedp(-1.0,g1)
!--error 9999
No convergence
at line 21 of function fixedp called by :
fixedp(-1.0,g1)
-->fixedp(0.0,g1)
!--error 9999
No convergence
at line 21 of function fixedp called by :
fixedp(0.0,g1)
The next examples converge to one root:
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
-->fixedp(1.0,g2)
9.
ans =
- .4552324
-->fixedp(-1.0,g2)
7.
ans =
- .4552349
-->fixedp(0.0,g2)
7.
ans =
- .4552309
These three examples converge to a second root.
-->fixedp(1.0,g3)
12.
ans =
2.1253885
-->fixedp(-1.0,g3)
13.
ans =
2.1253935 + .0000049i
-->fixedp(0.0,g3)
13.
ans =
2.1253872
The number shown before the variable ans in these results indicates the number of iterations.
The fixed-point iteration method does not always converge. There is no convergence pattern
for this method. Whether the method converges or not depends on the form of the function
g(x) as well as on the initial value chosen.
S So ol lv vi in ng g s sy ys st te em ms s o of f n no on n- -l li in ne ea ar r e eq qu ua at ti io on ns s
Consider the solution to a system of n non-linear equations in n unknowns given by
f
1
(x
1
,x
2
,,x
n
) = 0
f
2
(x
1
,x
2
,,x
n
) = 0
.
.
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
.
f
n
(x
1
,x
2
,,x
n
) = 0
The system can be written in a single expression using vectors, i.e.,
f(x) = 0,
where the vector x contains the independent variables, and the vector f contains the functions
f
i
(x):
.
) (
) (
) (
) ,..., , (
) ,..., , (
) ,..., , (
) ( ,
2
1
2 1
2 1 2
2 1 1
2
1
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

x
x
x
x f x
n n n
n
n
n
f
f
f
x x x f
x x x f
x x x f
x
x
x
" " "
Newton-Raphson method to solve systems of non-linear equations
A Newton-Raphson method for solving the system of linear equations requires the evaluation of
a determinant, known as the Jacobian of the system, which is defined as:
. ] [
/ / /
/ / /
/ / /
) ,..., , (
) ,..., , (
2 1
1 2 1 1 2
1 2 1 1 1
2 1
2 1
n n
j
i
n n n n
n
n
n
n
x
f
x f x f x f
x f x f x f
x f x f x f
x x x
f f f

1
1
1
1
]
1

!
" # " "
!
!
J
If x = x
0
(a vector) represents the first guess for the solution, successive approximations to the
solution are obtained from
x
n+1
= x
n
- J
-1
f(x
n
) = x
n
- x
n
,
with x
n
= x
n+1
- x
n
.
Convergence criteria for the solution of a system of non-linear equation could be, for example,
that the maximum of the absolute values of the functions f
i
(x
n
) is smaller than a certain
tolerance , i.e.,
. | ) ( | max <
n i
i
f x
Another possibility for convergence is that the magnitude of the vector f(x
n
) be smaller than
the tolerance, i.e.,
|f(x
n
)| < .
We can also use as convergence criteria the difference between consecutive values of the
solution, i.e.,
., | ) ( ) ( | max
1
<
+ n i n i
i
x x
or,
|x
n
| = |x
n+1
- x
n
| < .
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
The main complication with using Newton-Raphson to solve a system of non-linear equations is
having to define all the functions f
i
/x
j
, for i,j = 1,2, , n, included in the Jacobian. As the
number of equations and unknowns, n, increases, so does the number of elements in the
Jacobian, n
2
.
SCILAB function for Newton-Raphson method for a system of non-
linear equations
The following SCILAB function, newtonm, calculates the solution to a system of n non-linear
equations, f(x) = 0, given the vector of functions f and the Jacobian J, as well as an initial
guess for the solution x
0
.
function [x] = newtonm(x0,f,J)
//Newton-Raphson method applied to a
//system of linear equations f(x) = 0,
//given the jacobian function J, with
//J = del(f1,f2,...,fn)/del(x1,x2,...,xn)
//x = [x1;x2;...;xn], f = [f1;f2;...;fn]
//x0 is an initial guess of the solution
N = 100; //define max. number of iterations
epsilon = 1e-10; //define tolerance
maxval = 10000.0; //define value for divergence
xx = x0; //load initial guess
while (N>0)
JJ = J(xx);
if(abs(det(JJ))<epsilon) then
error('newtonm - Jacobian is singular - try new x0');
abort;
end;
xn = xx - inv(JJ)*f(xx);
if(abs(f(xn))<epsilon)then
x=xn;
disp(1000-N);
return(x);
end;
if (abs(f(xx))>maxval)then
disp(100-N);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
error('No convergence');
abort;
//end function
The functions f and the Jacobian J need to be defined as separate functions. To illustrate the
definition of the functions consider the system of non-linear equations:
f
1
(x
1
,x
2
) = x
1
2

+ x
2
2
- 50 = 0,
f
2
(x
1
,x
2
) = x
1
x
2
- 25 = 0,
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
whose Jacobian is
.
2 2
1 2
2 1
2
2
1
2
2
1
1
1
1
]
1

1
1
1
1
]
1

x x
x x
x
f
x
f
x
f
x
f
J
We can define the function f as the following user-defined SCILAB function f2:
function [f] = f2(x)
//f2(x) = 0, with x = [x(1);x(2)]
//represents a system of 2 non-linear equations
f(1) = x(1)^2 + x(2)^2 - 50;
f(2) = x(1)*x(2) -25;
//end function
The corresponding Jacobian is calculated using the user-defined SCILAB function jacob2x2:
function [J] = jacob2x2(x)
//Evaluates the Jacobian of a 2x2
//system of non-linear equations
J(1,1) = 2*x(1); J(1,2) = 2*x(2);
J(2,1) = x(2); J(2,2) = x(1);
//end function
Illustrating the Newton-Raphson algorithm for a system of two non-
linear equations
Before using function newtonm, we will perform some step-by-step calculations to illustrate
the algorithm. We start by loading functions f2 and jacob2x2:
-->getf('f2')
-->getf('jacob2x2')
Next, we define an initial guess for the solution as:
-->x0 = [2;1]
x0 =
! 2. !
! 1. !
Lets calculate the function f(x) at x = x
0
to see how far we are from a solution:
-->f2(x0)
ans =
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
! - 45. !
! - 23. !
Obviously, the function f(x
0
) is far away from being zero. Thus, we proceed to calculate a
better approximation by calculating the Jacobian J(x
0
):
-->J0 = jacob2x2(x0)
J0 =
! 4. 2. !
! 1. 2. !
The new approximation to the solution, x
1
, is calculated as:
-->x1 = x0 - inv(J0)*f2(x0)
x1 =
! 9.3333333 !
! 8.8333333 !
Evaluating the functions at x
1
produces:
-->f2(x1)
ans =
! 115.13889 !
! 57.444444 !
Still far away from convergence. Lets calculate a new approximation, x
2
:
-->x2 = x1 - inv(jacob2x2(x1))*f2(x1)
x2 =
! 6.0428135 !
! 5.7928135 !
Evaluating the functions at x
2
indicates that the values of the functions are decreasing:
-->f2(x2)
ans =
! 20.072282 !
! 10.004891 !
A new approximation and the corresponding function evaluations are:
-->x3 = x2 - inv(jacob2x2(x2))*f2(x2)
x3 =
! 5.1336734 !
! 5.0086734 !
-->f2(x3)
ans =
! 1.4414113 !
! .7128932 !
The functions are getting even smaller suggesting convergence towards a solution.
Solution using function newtonm
Next, we use function newtonm to solve the problem postulated earlier. We start by loading
function newtonm:
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
-->getf('newtonm')
A call to the function using the values of x0, f2, and jacob2x2, already loaded is:
-->[x] = newtonm(x0,f2,jacob2x2)
16.
x =
! 5.0000038 !
! 4.9999962 !
The result shows the number of iterations required for convergence (16) and the solution found
as x
1

= 5.0000038 and x
2
= 4.9999962. Evaluating the functions for those solutions results in:
-->f2(x)
ans =
1.0E-10 *
! .2910383 !
! - .1455192 !
The values of the functions are close enough to zero (error in the order of 10
-10
).
Note: The functions f2 and jacob2x2 can be loaded as line functions by using deff:
-->deff('[f]=f2(x)',['f_1=x(1)^2+x(2)^2-50';'f_2=x(1)*x(2)-25';'f=[f_1;f_2]'])
--
>deff('[J]=jacob2x2(x)',['J11=2*x(1)';'J12=2*x(2)';'J21=x(2)';'J22=x(1)';'J=[J1
1,J12;J21,J22]'])
Secant method to solve systems of non-linear equations
We use the term secant to refer to a method for solving systems of non-linear equations
through the Newton-Raphson algorithm, namely, x
n+1
= x
n
- J
-1
f(x
n
), but approximating the
Jacobian through finite differences. This approach is a generalization of the secant method for
a single non-linear equation. For that reason, we refer to the method applied to a system of
non-linear equations as a secant method, although the geometric origin of the term not
longer applies.
The secant method for a system of non-linear equations free us from having to define the n
2
functions necessary to define the Jacobian for a system of n equations. Instead, we
approximate the partial derivatives in the Jacobian with
,
) , , , , , ( ) , , , , , (
2 1 2 1
x
x x x x f x x x x x f
x
f
n j i n j i
j
i

! ! ! !
where x is a small increment in the independent variables. Notice that f
i
/x
j
represents
element J
ij
in the jacobian J = (f
1
,f
2
,,f
n
)/(x
1
,x
2
,,x
n
).
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
To calculate the Jacobian we proceed by columns, i.e., column j of the Jacobian will be
calculated as shown in the function jacobFD (jacobian calculated through Finite Differences)
listed below:
function [J] = jacobFD(f,x,delx)
//Calculates the Jacobian of the
//system of non-linear equations:
//f(x) = 0, through finite differences.
//The Jacobian is built by columns
[m n] = size(x);
for j = 1:n
xx = x;
xx(j) = x(j) + delx;
J(:,j) = (f(xx)-f(x))/delx;
end;
//end function
Notice that for each column (i.e., each value of j) we define a variable xx which is first made
equal to x, and then the j-th element is incremented by delx, before calculating the j-th
column of the Jacobian, namely, J(:,j). This is the SCILAB implementation of the finite
difference approximation for the Jacobian elements J
ij
= f
i
/x
j
as defined earlier.
Illustrating the secant algorithm for a system of two non-linear
equations
To illustrate the application of the secant algorithm we use again the system of two non-
linear equations defined earlier through the function f2. We start by loading functions f2 and
jacobFD:
-->getf('f2')
-->getf('jacobFD')
We choose an initial guess for the solution as x
0
= [2;3], and an increment in the independent
variables of x = 0.1:
-->x0 = [2;3]
x0 =
! 2. !
! 3. !
-->dx = 0.1
dx =
.1
Variable J0 will store the Jacobian corresponding to x
0
calculated through finite differences
with the value of x defined above:
-->J0 = jacobFD(f2,x0,dx)
J0 =
! 4.1 6.1 !
! 3. 2. !
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
A new estimate for the solution, namely, x
1
, is calculated using the Newton-Raphson algorithm:
-->x1 = x0 - inv(J0)*f2(x0)
x1 =
! 6.1485149 !
! 6.2772277 !
The finite-difference Jacobian corresponding to x
1
gets stored in J1:
-->J1 = jacobFD(f2,x1,dx)
J1 =
! 12.39703 12.654455 !
! 6.2772277 6.1485149 !
And a new approximation for the solution (x
2
) is calculated as:
-->x2 = x1 - inv(J1)*f2(x1)
x2 =
! 6.1417644 !
! 3.9045469 !
The next two approximations to the solution (x
3
and x
4
) are calculated without first storing the
corresponding finite-difference Jacobians:
-->x3 = x2 - inv(jacobFD(f2,x2,dx))*f2(x2)
x3 =
! 5.5599859 !
! 4.4403496 !
-->x4 = x3 - inv(jacobFD(f2,x3,dx))*f2(x3)
x4 =
! 5.2799174 !
! 4.7200843 !
To check the value of the functions at x = x
4
we use:
-->f2(x4)
ans =
! .1567233 !
! - .0783449 !
The functions are close to zero, but not yet at an acceptable error (i.e., something in the order
of 10
-6
). Therefore, we try one more approximation to the solution, i.e., x
5
:
-->x5 = x4 - inv(jacobFD(f2,x4,dx))*f2(x4)
x5 =
! 5.1399583 !
! 4.8600417 !
The functions are even closer to zero than before, suggesting a convergence to a solution.
-->f2(x5)
ans =
! .0391768 !
! - .0195883 !
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
S SC CI IL LA AB B f fu un nc ct ti io on n f fo or r s se ec ca an nt t m me et th ho od d t to o s so ol lv ve e s sy ys st te em ms s o of f n no on n- -l li in ne ea ar r e eq qu ua at ti io on ns s
To make the process of achieving a solution automatic, we propose the following SCILAB user -
defined function, secantm:
function [x] = secantm(x0,x1,f)
///Secant-type method applied to a
//system of linear equations f(x) = 0,
//given the jacobian function J, with
//JJ approximated by (f(x(n)-f(x(n-1)))/(x(n)-x(n-1))
//x = [x1;x2;...;xn], f = [f1;f2;...;fn]
//x0,x1 are the initial guesses of the solution
N = 100; //define max. number of iterations
epsilon = 1e-10; //define tolerance
maxval = 10000.0; //define value for divergence
if abs(x0-x1)<epsilon then
error('x1=x0 - use different values');
abort;
end;
xn = x0; //load initial guesses
xnm1 = x1;
[n m] = size(x1);
while (N>0)
fxn = f(xn);
fxnm1 = f(xnm1);
for i = 1:n
for j = 1:n
JJ(i,j) = (fxn(i)-fxnm1(i))/(xn(j)-xnm1(j))
end;
end;
if abs(det(JJ))<epsilon then
error('newtonm - Jacobian is singular - try new x0,x1');
abort;
end;
xnp1 = xn - inv(JJ)*f(xn);
if abs(f(xnp1))<epsilon then
x=xnp1;
disp(1000-N);
return(x);
end;
if abs(f(xnp1))>maxval then
disp(100-N);
error('Solution diverges');
abort;
end;
N = N - 1;
xnm1 = xn;
xn = xnp1;
end;
error('No convergence');
abort;
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
//end function
S So ol lu ut ti io on n u us si in ng g f fu un nc ct ti io on n s se ec ca an nt tm m
To solve the system represented by function f2, we start by loading function secantm:
-->getf('secantm')
The following call to function secantm produces a solution after 18 iterations:
-->x = secantm(x0,dx,f2)
18.
x =
! 4.9999964 !
! 5.0000036 !
S So ol lv vi in ng g n no on n- -l li in ne ea ar r e eq qu ua at ti io on ns s w wi it th h t th he e f fs so ol lv ve e f fu un nc ct ti io on n
SCILAB provides function fsolve for the solution of non-linear equations. In calling the function
you can either include a derivative (for a Newton-Raphson method) or only the function f(x)
(for a secant-type method). The fsolve function can be called by using:
[x [,v [,info]]]=fsolve(x0,fct [,fjac] [,tol])
where x0 is a real vector representing an initial guess for the solution; fct is an external (i.e
function or list or string) representing the equation fct(x) = 0; fjac is an external (i.e function
or list or string) representing a Jacobian or derivative; and, tol is a real scalar representing
the tolerance for convergence. The default value of tol, if not provided in the function call, is
tol=1.0x10
-10
.
Termination of a fsolve function call occurs when the algorithm estimates that the relative
error between x and the solution is at most tol.
In the left-hand side of the function call, x is a real vector representing the final value of the
solution; v is a real vector representing the value of the function at x; and info is an integer
representing a termination indicator. Info can take any of the following values corresponding
to different termination conditions:
0 : improper input parameters.
1 : relative error between x and the solution is at most tol.
2 : number of calls to fcn reached
3 : tol is too small. No further improvement in the approximate solution x is possible.
4 : iteration is not making good progress.
As indicated above, this function can be used to solve for a system of linear equations,
including a Jacobian matrix (fjac). For a single non-linear equation, fjac is the derivative.
Solving single non-linear equations with fsolve
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
The following examples show how to use the fsolve function for solving equations of the form
f(x) = 0. First, we define f(x) and its derivative, fp(x):
-->deff('[y]=f(x)','y=x^3-2*x^2+1')
-->deff('[y]=fp(x)','y=x*x^2-4*x')
Then, we perform different calls to the function fsolve:
-->fsolve(2.0,f) //No left-hand side, no Jacobian
ans =
1.618034
-->fsolve(2.0,f,fp) //No left-hand side, Jacobian provided
ans =
1.618034
-->fsolve(2.0,f,fp,0.001) //No left-hand side, Jacobian and tol provided
ans =
1.9999745
-->x = fsolve(1.0,f,fp) //solution name, x, in left-hand side
x =
1.
-->[x,f_x] = fsolve(-10,f,fp) //solution and function value in LHS
f_x =
2.220E-16
x =
- .6180340
-->[x,f_x,msg] = fsolve(-2.0,f) //solution, function, message in LHS
msg =
1.
f_x =
- 2.220E-16
x =
- .6180340
-
Solving a system of non-linear equations with fsolve
We use function fsolve to solve the system of two non-linear equations defined by function f2.
To begin with, we load the function f2, and the corresponding Jacobian jacob2x2:
-->getf('f2')
-->getf('jacob2x2')
Next, we define an initial value for the solution, x0, and call function fsolve using f2, jacob2x2:
-->x0 = [3;-2]
x0 =
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
! 3. !
! - 2. !
-->fsolve(x0,f2,jacob2x2)
ans =
! 5. !
! 5. !
A second call to the function includes a left-hand side specifying the solution, x, the value of
the function at the solution, f_x, and information about the solution, info:
-->[x,f_x,info] = fsolve(x0,f2,jacob2x2)
info =
1.
f_x =
! 0. !
! 0. !
x =
! 5. !
! 5. !
You can also call function fsolve without including the Jacobian:
-->[x,f_x,info] = fsolve(x0,f2)
info =
1.
f_x =
! 0. !
! - 3.553E-15 !
x =
! 5.0000001 !
! 4.9999999 !
A second example involves the solution of the system of 3 non-linear equations without using
the Jacobian:
f
1
(x
1
,x
2
,x
3
) = x
1
2

- x
2
2
+ x
3
2
- 4
f
2
(x
1
,x
2
,x
3
) = x
1
x
2
- x
3
+ 1
f
3
(x
1
,x
2
,x
3
) = (x
1
x
2
x
3
)
1/2
- 1.4142
First, we define the system of equations as function f3:
-->deff('[y]=f3(x)',['f_1=x(1)^2-x(2)^2+x(3)^2-4','f_2=x(1)*x(2)-x(3)+1',...
-->'f_3=sqrt(x(1)*x(2)*x(3))-1.4142','y=[f_1;f_2;f_3]'])
We use the following values as first guesses for the solution:
-->x0 = [3,3,3]
x0 =
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
! 3. 3. 3. !
A call to fsolve produces:
-->[xs,fxs,m] = fsolve(x0',f3)
m =
1.
fxs =
1.0E-15 *
! - .4440892 !
! 0. !
! - .2220446 !
xs =
! 1.0000064 !
! .9999808 !
! 1.9999872 !
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Applications of Non-linear Equations
with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
Applications of non-linear equations 2
Projectile motion 2
Analysis of a simple three-bar mechanism 6
Solving the Darcy-Weisbach and Coolebrook-White equations for pipeline flow 9
Solving pipe flow with the Swamee-Jain equation 13
A SCILAB function to solve the Darcy-Weisbach equation with the Swamee-Jain equation 14
Applications of function DWSJ to pipe flow 15
Solving for discharge and head for a pipe-pump system 16
Graphical solution to the pump-pipeline system 17
A function to solve a pipe-pump system 18
Exercises 19
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Applications of non-linear equations
In this section we present solutions of non-linear equations that arise from applications to the
physical sciences.
Projectile motion
The motion of a projectile in a Cartesian coordinate system as shown in the figure below is
described by the equations:
a
x
= 0, a
y
= -g,
v
x
= v
0
cos(
0
), v
y
= v
0
sin(
0
)-gt,
x = x
0
+ v
0
cos(
0
)t, y = y
0
+ v
0
sin(
0
)t - gt
2
/2,
where a stands for acceleration, v for velocity, and (x,y) are the positions of the projectile at
time t. It is implied that the projectile was launched from point (x
0
,y
0
) at time t = 0 with an
initial velocity of magnitude v = v
0
at an angle above the positive x-axis of =
0
. The variable
g represents the acceleration of gravity (g = 9.806 m/s
2
= 32.2 ft/s
2
).
The velocity vector, illustrated in the figure, can be written as v = v
x
i + v
y
j. The equation of
the trajectory can be obtained by replacing t = (x-x
o
)/(v
o
cos(
o
)), into the equation for y,
resulting in
. ) (
) ( cos 2
) ( ) tan(
2
0
2 2
0
0 0 0
x x
v
g
x x y y
o


+

Example 1. A projectile is launched from point (x


0
,y
0
) = (0,0) with a velocity v
0
= 25 m/s at an
unknown angle
0
. If the projectile passes through point (x,y) = (2,3), determine the angle
0
.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Replacing the values of the data given in the problem statement together with the appropriate
value of g in the equation of the trajectory, we can form a single non-linear equation on
0
defined by
. 0 ) (
) ( cos 2
) ( ) tan( ) (
2
0
2 2
0
0 0 0 0


+ y x x
v
g
x x y f
o


The solution using SCILAB can be found as follows:
First, we enter the known data:
-->x0 = 0, y0 = 0, v0 = 25, x = 2, y = 3, g = 9.806
x0 =
0.
y0 =
0.
v0 =
25.
x =
2.
y =
3.
g =
9.806
Next, we define the function f(
o
).
-->deff('[f]=traj(theta0)',...
-->'f = y0+tan(theta0).*(x-x0)-g.*(x-x0)^2.\(2.*v0^2.*cos(theta0)^2)-y')
Notice that in the definition we used the left division (\) in the third term of the function. We
do this in order to be able to plot the function given an array of values of
0
as will be shown
next. The variable th is an array of values of
0
, starting at 0.1 and ending at 6.28 2. The
variable fth stores the values of the function traj(theta0), representing f(
0
), and
corresponding to th.
-->th = [0.1:0.1:6.28]'; fth = traj(th);
We use those variables, th and fth, to plot the function f(
0
) with the purpose of identifying
possible solutions.
-->plot(th,fth,'theta0','f_trajectory','projectile motion')
-->xgrid()
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
The figure is shown above. From the figure we notice that there are two possible solutions,
one near 1.0 and one near 4.0. To obtain the solutions we use SCILABS function fsolve. The
two solutions are shown next. The solutions are given in radians, the natural unit of angular
measurement. We also show the equivalent values in degrees, through the formula,

o

= 180
r
/:
-->th0 = fsolve(1,traj)
th0 =
1.2538009
-->180*th0/%pi
ans =
71.8375
-->th0 = fsolve(4,traj)
th0 =
4.3953936
-->180*th0/%pi
ans =
251.8375
Next, we produce a plot of the projectile trajectory corresponding to
0
= 1.2538009 rad. We
start by clearing the variables and defining the equation of the trajectory:
-->clear
-->deff('[y]=f(x)',...
-->'y=y0+tan(theta0)*(x-x0)-g*(x-x0)^2/(2*v0^2*cos(theta0)^2)')
Next, we load the constant values:
-->x0 = 0; y0 = 0; v0 = 25; x = 2; y = 3; g = 9.806; theta0 = 1.2538009;
The plot is produced by the following statements:
-->xx = [0:0.1:40]';
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
-->yy = f(xx);
-->plot(xx,yy,'x','y','projectile trajectory')
Solving the equation tan(x) =x
The equation tan(x) = x shows up in problems of differential equations when determining so-
called eigenvalues and eigenfunctions. In this example we solve the equation for values of x >
0. To see possible solutions we plot the functions f
1
(x) = x and f
2
(x) = tan(x) in the same set of
axes by using:
-->deff('[y]=f1(x)','y=x')
-->deff('[y]=f2(x)','y=tan(x)')
-->xx = [0:0.1:20]'; yy1 = f1(xx); yy2 = f2(xx);
-->plot2d([xx xx],[yy1 yy2],[1,2],'111','x@tan(x)',[0 0 10 10])
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
We can see at least three roots one is zero, the other two are near 4 and 7.5. To solve the
corresponding equation we define the function f
x
(x) = tan(x)-x, and solve it using SCILABs
function fsolve:
-->deff('[y]=fx(x)','y=tan(x)-x')
-->x1 = fsolve(0.5,fx)
x1 =
9.218E-09
-->x2 = fsolve(4.1,fx)
x2 =
4.4934095
-->x3 = fsolve(7.5,fx)
x3 =
7.7252518
To verify that the solutions satisfy the equation f
x
(x) = 0, try:
-->fx(x1), fx(x2), fx(x3)
ans =
0.
ans =
8.882E-16
ans =
- 2.309E-14
Analysis of a simple three-bar mechanism
Consider the mechanism shown in the figure below. Bar SR has a fixed hinge at S, while bar
PQ has a fixed hinge at P. Bar SR is animated by a rotational motion about point S that drives
the mechanism forcing bar PQ to rotate about point P. The analysis in this case consists in
determining the angle (output angle) given the angle (input angle).
You can verify from the figure that:
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
L
3
sin = L
1
sin + L
2
sin
L
3
cos + L
2
cos - L
1
cos = D
This is a system of two non-linear equations in two unknowns, and , for a given value of .
The system can be written as follows:
f
1
(,) = - L
2
sin + L
3
sin - L
1
sin 0
f
2
(,) = L
2
cos + L
3
cos - L
1
cos - D = 0
These system of non-linear equations can be entered into SCILAB as the following file function:
function [f] = fmech(angle)
//evaluates f1(beta,gama) & f2(beta,gamma)
//for the case of a three-bar mechanism
//angle(1) = beta, angle(2) = gamma
f = zeros(2,1);
f(1) = -L2*sin(angle(1))+L3*sin(angle(2))-L1*sin(alpha);
f(2) = L2*cos(angle(1))+L3*cos(angle(2))-L1*cos(alpha)-D;
//end function
Next, we will use SCILAB function fsolve to obtain a table of values of given a set of values of
. We will use the values D = 4, L
1
= 6, L
2
= 3, L
3
= 7, and = 0, /20, 2 /20, , 19/20.
First, we load the function fmech:
-->getf(fmech)
Next, we define the constant values:
-->L1=6;L2=3;L3=7;D=4;
-->alphav = [0:%pi/20:19*%pi/20];
The size of alphav (the v stands for vector) will be used later in calculating the angles and :
-->[m n] = size(alphav)
n =
20.
m =
1.
The angles and will be obtained as rows in the matrix angles, which is first defined as an
empty array:
-->angles = [];
The next for loop will solve for values of angles and for each value of alphav, which is
temporarily stored in variable alpha:
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
-->for j = 1:m
--> alpha = alphav(j);
--> angles = [angles, fsolve([alpha;alpha],fmech)];
-->end;
The following assignment statements load the values of the angles and in separate vectors:
-->beta = angles(1,:);
-->gama = angles(2,:);
Next, we produce a plot of -vs- (both in radians):
-->plot(alphav,beta,'alpha','beta','three-bar mechanism analysis')
The following is a plot of -vs- (both in radians):
-->plot(alphav,gama,'alpha','gamma','three-bar mechanism analysis')
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Solving the Darcy-Weisbach and Coolebrook-White equations for
pipeline flow
The figure below shows the components of the energy equation for turbulent flow in a pipe.
The datum is a horizontal reference level from which the elevation of the pipe centerline is
measured. The energy line (E.L.) is a graphical representation of the total head along the
pipe. The hydraulic grade line (H.G.L.) represents the piezometric head along the pipe.
Let the pipe length between cross-sections 1 and 2 be L. The slope of the energy line is
defined as S
f
= h
f
/L, where h
f
is the energy losses due to friction on the length L.
The energy equation for the figure above can be written by simply adding the lengths of the
different energy heads shown, i.e.,
f
h
g
V p
z
g
V p
z + + + + +
2 2
2
2 2
2
2
1 1
1

For a constant-diameter pipeline V
1
= V
2
, and using the piezometric head, h = z + p/, the
energy loss is equal to the difference in piezometric heads, i.e.,
h
f
= h
1
-h
2
= (z
1
+ p
1
/)-( z
2
+ p
2
/).
The Darcy-Weisbach equation provides a way to calculate the friction head loss, h
f
, based of
fluid properties and flow characteristics:
,
2
2
g
V
D
L
f h
f

Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
in which, L and D are the length and diameter of the pipe, V is the mean flow velocity, g is the
acceleration of gravity, and f is known as the Darcy-Weisbach friction factor. The friction
factor, f = f(e/D, Re), is a function of the parameters e/D or relative roughness, and the
Reynolds number, Re. The parameter e, known as the absolute roughness of the pipeline is a
measure of the roughness heights of the pipelines inner wall, and the Reynolds number is
defined as
, Re

VD VD

where is the fluid's kinematic viscosity.
An expression describing the variation of f = f(e/D, Re) for turbulent flow is the Coolebrook-
White equation given by:
)
Re
51 . 2
7 . 3
log( 2
1
f
D
e
f
+
Here, log represents the logarithm of base 10. In SCILAB, the function log represents the
natural logarithm, i.e., the logarithm of base e = 2.718281828, which is typically written in
paper as ln. Using natural logarithms, we re-write the Coolebrook-White equation to read:
.
Re
51 . 2
7 . 3
ln 8686 . 0
1

,
_


f
D
e
f
This function is implicit on f, therefore, suitable for solution through the methods of non-linear
equations presented in this chapter.
Example 1. Determining the friction factor.
As an example, try using the following values e = 0.00001m, D = 0.25 m, Re = 110
6

to
determine the corresponding friction factor. Here is the solution using SCILAB: First, we
define the function for the Coolebrook-White equation:
-->deff('[P]=CW(f)','...
-->P=1/sqrt(f)+0.8686*log(e/(3.7*D)+2.51/(Re*sqrt(f)))')
Next, we enter the constant values:
-->e = 0.00001; D = 0.25; Re = 1e6;
The corresponding friction factor is calculated as:
-->f = fsolve(0.02,CW)
f =
.0124687
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Example 2. Plotting the Moody diagram.
The function described by the Coolebrook-White equation is typically plotted in log-log scale
with the Reynolds number, Re, in the x-axis and the friction factor, f, as curves corresponding
to different values of the relative roughness, e/D. The following SCILAB script is used to plot a
simplified version of the Moody diagram for selected values of the relative roughness. The
script is called PlotMoody and it is stored in SCILABs working directory:
//Script to plot Moody diagram
//First we define the function for the Coolebrook-
//White equation:
deff('[P]=CW(f)','...
P=1/sqrt(f)+0.8686*log(e/(3.7*D)+2.51/(Re*sqrt(f)))')
D = 0.025; //Diameter of 2.5 cm, approx. 1.0 inch
//The next two vectors contain values of the relative
//roughness and the Reynolds number
e_v = [0.01 0.001 0.0001 0.00001 0.000001 0.0000001];
Re_v = [1e4 1e5 1e6 1e7 1e8];
[ne me] = size(e_v); //size of vectors
[nR mR] = size(Re_v); //e_v and Re_v
fMoody = zeros(mR,me); //Create matrix for f values
//Calculating friction factors for combinations of values
//of e_v(i) and Re_v(j):
for j = 1:me
for i = 1:mR
Re = Re_v(i); e = D*e_v(j);
if e < 1e-5 then
f0 = 0.01;
else
f0 = 0.02;
end;
fMoody(i,j) = fsolve(f0,CW);
end;
end;
//Plotting the Moody diagram
plot2d1('oll',Re_v',[fMoody(:,1),fMoody(:,2),...
fMoody(:,3),fMoody(:,4), fMoody(:,5), fMoody(:,6)],...
[1:6],'121','0.01@0.001@0.0001@0.00001@0.000001@0.0000001');
xtitle('Moody diagram','Reynolds number','friction factor');
//end script
To execute the script use:
-->exec('PlotMoody')
SCILAB produces a listing of the script before producing the following graph:
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
Example 3. Determining pipe diameter given the flow velocity or discharge
Eliminating the friction factor from the Darcy-Weisbach and Coolebrook-White equation we get
the following equation relating velocity (V) to pipe length (L), diameter (D), and absolute
roughness (e), energy loss (h
f
), and kinematic viscosity():
Suppose that we are given the values g = 9.806 m/s
2
, h
f
= 1.0 m, e = 0.0001 m, L = 2000 m, =
1x10
-5
m
2
/s, V = 1.1 m/s, and asked to determine the value of D. We can define the following
SCILAB function:
-->deff('[P]=VCW(D)',...
-->'P=V+1.2283*sqrt(g*D*hf/L)*log(e/(3.7*D)+1.77*Nu/(D^1.5*sqrt(g*hf/L)))')
The problem data is entered next:
-->V=1.1;g=9.806;hf=1;L=2000;e=0.0001;Nu=1e-5;
The solution is found by using the following call to SCILAB function fsolve with an initial guess D
= 0.5:
-->D = fsolve(0.5,VCW)

,
_



L
h g
D
D
e
L
D h g
V
f
f
2 / 3
77 . 1
7 . 3
ln 2283 . 1

Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
D = 1.9543027
Using continuity, i.e., Q = AV = D
2
V/4, an equation can be written in terms of Q as,
For design problems, this equation is often more useful than the previous one given in terms of
the velocity because more often than not the discharge rather than the velocity is given. To
solve for the diameter given the discharge, we present the following SCILAB example. Given g
= 9.806 m/s
2
, h
f
= 1.0 m, e = 0.0001 m, L = 2000 m, = 1x10
-5
m
2
/s, Q = 2.2 m
3
/s, find the
corresponding pipe diameter. We start by defining the function:
-->deff('[P]=QCW(D)',...
--
>'P=Q+0.9648*D^2.5*sqrt(g*hf/L)*log(e/(3.7*D)+1.78*Nu/(D^1.5*sqrt(g*hf/L)))');
Next, we enter the problem data:
-->Q=2.2;g=9.806;hf=1;L=2000;e=0.0001;Nu=1e-5;
Finally, we solve for D:
-->D = fsolve(0.5,QCW)
D = 1.6782603
Solving pipe flow with the Swamee-Jain equation
To avoid the implicit nature of f in the Coolebrook-White equation we can use the following
explicit approximation for f, referred to as the Swamee-Jain equation:
.
Re
74 . 5
75 . 3
ln
3254 . 1
2
9 . 0 1
]
1

,
_

D
e
f
With the definition of the Reynolds number Re = VD/, the resulting expression for the friction
factor is
.
74 . 5
75 . 3
ln
3254 . 1
2
9 . 0 9 . 0
9 . 0
1
]
1

,
_

D V D
e
f

In design problems it is more convenient to work with the discharge Q. Replacing the velocity
V = 4Q/(D
2
) into the Swamee-Jain equation we get

,
_


L
h g
D
D
e
L
h g
D Q
f
f
2 / 3
2 / 5
78 . 1
7 . 3
ln 9648 . 0

Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
.
618 . 4
75 . 3
ln
3254 . 1
2
9 . 0
1
1
]
1

,
_

,
_

Q
D
D
e
f

If we write the Darcy-Weisbach equation in terms of the discharge, i.e.,


h
f
= f(L/D)V
2
/(2g) = 8fLQ
2
/(
2
gD
5
),
and then introduce the latest version of the Swamee-Jain equation, we will get the following
equation for the friction losses in a pipe:
.
618 . 4
75 . 3
ln
074 . 1
2
9 . 0
5
2
1
1
]
1

,
_

,
_

Q
D
D
e
D g
Q L
h
f

A SCILAB function to solve the Darcy-Weisbach equation with the Swamee-Jain equation
The following function, DWSJ (Darcy-Weisbach equation with Swamee-Jain friction factor) can
be used to solve for any of the variables in the previous equation given appropriate values of
the remaining variables. The function also requires that the user provide an initial value of the
variable that he or she is solving for. Function DWSJ uses SCILAB function fsolve to solve for
the particular unknown of interest. A listing of the function follows:
function [result] = DWSJ(index,gindex,L,D,e,nu,hf,Q)
//This function solves for one of the variables
//in the Darcy-Weisbach equation with the friction
//factor approximated through the Swamee-Jain
//equation. The string variable 'index' determines
//which variable to solve for. Possible values of
//index are:
// 'L' - to solve for the length of the pipe
// 'D' - to solve for the diameter
// 'e' - to solve for the absolute wall roughness
// 'nu' - to solve for the kinematic viscosity
// 'hf' - to solve for the friction losses
// 'Q' - to solve for the discharge
//The variable 'gindex' can take the values 'SI' or
//'ES' corresponding to the system of units to be used:
// 'SI' - for the Systeme International
// 'ES' - for the English (or Imperial) System
//Make sure that the values of the variables are given
//in consistent units, i.e.,
// L(m or ft), D(m or ft), e(m or ft),
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
// nu(m^2/s or ft^2/s), hf(m or ft), Q(m^3/s or ft^3/s)
// The order of the variables does not change in the call
// to the function. The user needs to provide an initial
// guess for the variable he/she is solving for in the
// appropriate position in the function call.
if gindex == 'SI' then
g = 9.806;
elseif gindex == 'ES' then
g = 32.2;
else
error('DWSJ - wrong index for unit system');
abort;
end;
if index == 'L' then
deff('[P] = DWSJEq(LL)',...
'P=1.074*LL*Q^2/(g*D^5*(log(e/(3.75*D)+4.618*(D*nu/Q)^0.9))^2)-hf');
result = fsolve(L,DWSJEq);
elseif index == 'D' then
deff('[P] = DWSJEq(DD)',...
'P=1.074*L*Q^2/(g*DD^5*(log(e/(3.75*DD)+4.618*(DD*nu/Q)^0.9))^2)-hf');
result = fsolve(D,DWSJEq);
elseif index == 'e' then
deff('[P] = DWSJEq(ee)',...
'P=1.074*L*Q^2/(g*D^5*(log(ee/(3.75*D)+4.618*(D*nu/Q)^0.9))^2)-hf');
result = fsolve(e,DWSJEq);
elseif index == 'nu' then
deff('[P] = DWSJEq(nnu)',...
'P=1.074*L*Q^2/(g*D^5*(log(e/(3.75*D)+4.618*(D*nnu/Q)^0.9))^2)-hf');
result = fsolve(nu,DWSJEq);
elseif index == 'hf' then
deff('[P] = DWSJEq(hhf)',...
'P=1.074*L*Q^2/(g*D^5*(log(e/(3.75*D)+4.618*(D*nu/Q)^0.9))^2)-hhf');
result = fsolve(L,DWSJEq);
elseif index == 'Q' then
deff('[P] = DWSJEq(QQ)',...
'P=1.074*L*QQ^2/(g*D^5*(log(e/(3.75*D)+4.618*(D*nu/QQ)^0.9))^2)-hf');
result = fsolve(L,DWSJEq);
else
error('DWSJ - index is L, D, e, nu, hf, or Q enclosed in quotes');
abort;
end;
The function may provide an error message if the initial value for the unknown variable is too
far away from the solution, particularly for those unknowns whose values are typically very
small, such as the viscosity (of the order of 10
-6
to 10
-5
) or the absolute roughness (anywhere
from 0.000001to 0.1). Whenever you receive an error message in a solution, try using different
initial values of the unknown to improve the convergence of the function.
Applications of function DWSJ to pipe flow
Examples of applications of the function DWSJ follow. Recall that the general call to the
function is
[result] = DWSJ(index,gindex,L,D,e,nu,hf,Q)
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
With index determining the unknown to solve for (L,D,e,nu,hf,Q), and gindex
determining the system of units (SI, ES).
-->getf('DWSJ') //Load the function
-->DWSJ('L','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for pipe length
ans =
165.70807
-->DWSJ('D','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for pipe diameter
ans =
.4511914
-->DWSJ('e','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for pipe roughness
ans =
.0022291
-->DWSJ('nu','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for kinematic viscosity
ans =
.0001117
-->DWSJ('hf','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for friction loss
ans =
1.2069418
-->DWSJ('Q','SI',100,0.5,0.0001,1e-5,2,0.5) //Solve for discharge
ans =
.6567565
Solving for discharge and head for a pipe-pump system
Consider the flow through a horizontal pipe between two reservoirs as shown in the figure
below. A pump is necessary to overcome the difference in water surface levels between the
two reservoirs, h = z
2
- z
1
. The pump inserts an energy head H

into the system. If we neglect
minor losses (entrance into the pipe, exit out of the pipe, valves, etc.), the energy equation
for this system is given by
z
1
+ 0 + 0 + H = z
2
+ 0 + 0 + h
f
.
Using the equation presented earlier for h
f
that combines the Darcy-Weisbach and Swamee-Jain
equations, we can write the so-called system equation:
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
.
618 . 4
75 . 3
ln
074 . 1
2
9 . 0
5
2
1
1
]
1

,
_

,
_


+
Q
D
D
e
D g
Q L
h H

The discharge through the pump Q and the energy head H that the pump provides to the
system are related by the pump rating curve, typically described by a quadratic equation, i.e.,
H= a + bQ + cQ
2
.
For a given system, the pipe characteristics, i.e., length(L), diameter (D), and roughness (e), as
well as the kinematic viscosity of the liquid () are known. For a given value of h, the two
equations listed above are solved for the unknowns H and Q. The solution can be
accomplished through graphical means by plotting the two equations in the same set of axis
and determining their point of intersection. This approach is shown next, using SCILAB.
Graphical solution to the pump-pipeline system
To obtain the graphical solution we define the following functions that represent, respectively,
the system equation and the pump rating curve:
-->deff('[HH]=H1(Q)',...
-->'HH = Dh+1.074*L*Q^2/(g*D^5*log(e/(3.75*D)+4.618*(D*nu/Q)^0.9)^2)')
-->deff('[HH]=H2(Q)','HH = a+b*Q+c*Q^2')
Next, we enter the constant values:
-->Qp = [0.1:0.1:2.0]; Hp1 = feval(Qp,H1); Hp2 = feval(Qp,H2);
The next step is to define a vector of values of Q, which we call Qp, and evaluate the two
functions for those values of Q using function feval (function evaluation). Thus, vectors Hp1
and Hp2 store, respectively, the values of H corresponding to the system equation and the
pump rating curve.
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
-->Dh = 20; L = 150; g = 9.806; e = 0.00001;
-->D = 0.25; nu = 1e-6; Q = 0.20;
-->a = 500; b = 0; c = -100;
The plot is obtained by using:
->plot2d([Qp',Qp'],[Hp1',Hp2'],[1,2])
-->xtitle('Pump-pipeline system','Q(m^3/s)','H(m)')
We can estimate the solution from the graphics as Q = 1.42 m
3
/s and H = 290 m.
A function to solve a pipe-pump system
The following SCILAB function, PipePump, is used to code the functions
,
618 . 4
75 . 3
ln
074 . 1
) , (
2
9 . 0
5
2
1
1
1
]
1

,
_

,
_



Q
D
D
e
D g
Q L
h H H Q f

. ) , (
2
2
Q c Q b a H H Q f
This is the listing of the function:
function [P] = PipePump(X)
//This function codes the system equation
//and the pump rating curve for a pipe-pump
//system representing pumping between two
//reservoirs. X(1) = Q and X(2) = H.
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
P = zeros(2,1);
P(1)=X(2)-Dh-1.074*L*X(1)^2/(g*D^5*(log(e/(3.75*D)+4.618*(D*nu/X(1))^0.9))^2);
P(2)=X(2)-a-b*X(1)-c*X(1)^2;
//end function
To load the function we use:
-->getf('PipePump')
The following call to function fsolve produces the solution:
-->fsolve([1.4;300],PipePump)
ans =
! 1.4275835 !
! 296.20053 !
To verify the solution use:
-->PipePump(ans)
ans =
1.0E-11 *
! .5798029 !
! - .4490630 !
Notice that the results found through the use of fsolve and PipePump, i.e., Q = 1.4275835
m
3
/s and H = 296.20053 m, are very close to the estimates we obtained from the graphical
solution.
Exercises
In problems [1] through [6], use the following definitions:
z
1
= -3 + 2i, z
2
= 5 - i, z
3
= -4+3i, z
4
= -2-4i
[1]. Determine the following magnitudes and arguments:
(a) |z
1
| (b) Arg(z
1
) (c) |z
2
| (d) Arg(z
2
)
(e) |z
3
| (f) Arg(z
3
) (g) |z
4
| (h) Arg(z
4
)
[2]. Write the following complex numbers in polar form:
(a) 3-5i (b) 4-4i (c) 3-5i (d) -2-6i
(e) -5+6i (f) -+3i (g) (5-i)/2 (h) 7+5i
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
[3]. N/A.
[4]. Determine the result of the following complex number operations:
(a) z
1
+3z
2
(b) z
1
-z
2
+4z
3
(c) (z
1
-2)(z
4
-z
3
) (d) z
4
/z
2
+ z
3
/z
1
(e) z
1
z
2
z
3
(f) (z
1
-2z
2
)(z
3
/) (g) z
1
z
2
-1/z
3
(h) (z
2
+z
3
)/(z
1
+3z
2
)
[5]. Determine the result of the following complex number operations:
(a) z
1
+z
1
(b) z
2
z
2
(c) z
3
/z
3
(d) (z
1
+z
2
)/(z
3
-5z
4
)
(e) z
1
-z
1
(f) 2(z
1
-z
1
) (g) 1/z
2
+1/z
2
(h) |z
1
|(z
2
+z
3
)
[6]. Determine the result of the following complex number operations:
(a) z
3
3
(b) z
2
(z
1
-2z
3
)
2
(c) z
2
/z
3
4
(d) z
3
2
/z
1
3
(e) z
1
+1/z
2
+1/z
3
2
(f) (1+1/z
2
)
3
(g) z
1
3
+(z
2
-z
3
)
2
(h) (z
2
+1)
2
/z
3
[7]. Solve for z in the following equations:
(a) z
2
+3-2i = 0 (b) (z+1)
2
= 3
1/2
(c) z
3
-i = 0 (d) z
4
+(i-2)
3
= 0
(e) z
2
+2z=4 (f) z
3
=-1 (g) 1/(z+1)
3
= i (h) z(z-1)=2
[8]. Write a SCILAB function to calculate the roots of the quadratic equation: ax
2
+bx+c = 0.
Use the function to solve the quadratic equations whose coefficients are:
(a) a = 2, b = -5, c = 3 (b) a = 12, b = 22, c = -3
(c) a = 10, b = 25, c = 33 (d) a = -5, b = -5, c = 2
[9]. Write a SCILAB function to calculate the roots of the cubic equation: ax
3
+bx
2
+cx+d=0. (a)
(a) a = 2, b = -5, c = 3, d = 23 (b) a = 12, b = 22, c = -3, d = 1
(c) a = 10, b = 25, c = 33, d = -10 (d) a = -5, b = -5, c = 2, d = 10
[10]. Use function roots to solve problems [8] and [9].
[11]. Use function roots to solve the following polynomial equations:
(a) s
5
+ 6s
3
- 27 = 0 (b) y
4
- 23y
3
+ 5y
2
- 3y

+ 2 = 0
(c) r
6
+ r
2
+ r - 2345 = 0 (d) (s+1)
7
- s
2


+ 2 = 0
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
In problems [12] through [16], you are required to solve the equation f(x) = 0. Plot the
corresponding function y = f(x) to obtain guesses of the solution(s), then solve for as many
solutions as possible using:
(a) the bisection method (b) the Newton-Raphson method
(c) the secant method (d) SCILAB function fsolve
[12] f(x) = x
3
+ 18x
2

-22x + sin(2x
2
+x) - 255
[13] f(x) = x
4
+ exp(x-2) + x
3
- 200
[14] f(x) = exp(-0.1x)cos(2x-/2)
[15] f(x) = x
5
+ 5x
2
-23x - 19
[16] f(x) = ln((x
2
+x+2)/(sin x + 1) - 5, for x>0
In problems [17] through [21], solve the system of equations shown using (a) SCILAB function
fsolve, and (b) the Secant method for multiple equations
[17] x
2
+ y
2
- 2xy = 1, (x-2)(y-3) + e
x
+ y = 10.39
[18] x
2
+ y
2

+ z
2
= 17, xy + yz + zx = -4, (x+1)(y+1)(z-3) + xyz = -8
[19] tan(x-1/2) + 5 cos y + z = 0.75, xyz + sin(y) = 4.05, exp(-xyz) + cos(z) = 0.58
[20] ab + bc
2
+ abc = 26, sin(a+b) + exp(c) - ln(a) = -2.05, a + b + c = 0
[21] x ln(y+z) = -3.83, xy
2
+ yz
2
+ zx
2
= -27.20, (x+y)
1/2
+ x = 0.60
[22]. Mannings equation is used to calculate the discharge on an open channel of slope S
0
(typically a relatively small value, i.e., 0.00000001<S
0
< 0.001), whose surface roughness is
characterized by a parameter known as the Mannings coefficient, n (typical values between
0.001 and 0.3. The larger the value of n, the rougher the surface. For example, concrete
surfaces have n = 0.012). The equation is written as
,
0
3 / 2
3 / 5
S
P
A
n
C
Q
u

where C
u
is a coefficient that depends on the system of units used, with C
u
= 1.0 for the
Systeme Internationale (S.I.) and C
u
= 1.486 for the English (or Imperial) System of units (E.S.),
A is the cross-sectional area, P is the wetted perimeter of the cross-section (i.e., the length of
the cross-sectional boundary in contact with the water), and Q is the discharge.
For a symmetric trapezoidal cross section, as shown below, the area and wetted perimeter are
given by
2
1 2 , ) ( z y b P y zy b A + + + ,
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
where b is the bottom width of the cross-section, y is the cross-sectional depth, and z is the
(dimensionless) side slope.
Write a function, along the lines of function DWSJ, that allows the user to select which element
to solve for out of the Mannings equation for a trapezoidal cross-section. Use the function
thus developed to solve for the missing terms in each line of the following table:
System of
Case Units b y z n So Q
(a) S.I. b=? 0.6 1.5 0.012 0.0001 0.2
(b) S.I. 1.5 y=? 0.5 0.023 0.00001 0.15
(c) S.I. 0.5 0.25 z=? 0.01 0.001 0.35
(d) S.I. 1.4 0.4 1 n=? 0.0001 0.6
(e) S.I. 1.2 0.6 0.5 0.018 S=? 0.8
(f) S.I. 0.6 0.3 0.75 0.015 0.0001 Q=?
(g) E.S. b=? 0.6 1.5 0.012 0.0001 3.5
(h) E.S. 3 y=? 0.5 0.023 0.00001 7.2
(i) E.S. 2 1.2 z=? 0.01 0.001 4.5
(j) E.S. 3.5 0.75 1 n=? 0.0001 10
(k) E.S. 4.25 2.1 0.5 0.018 S=? 25
(l) E.S. 5 2.5 0.75 0.015 0.0001 Q=?
[23]. Critical flow conditions in open channel flow are given by the equation
, 1
3
2

gA
T Q
where Q is the flow discharge, T is the top width of the cross-section (for a trapezoidal
channel, T = b+2zy), g is the acceleration of gravity (g = 9.806 m/s
2
in the S.I. and g = 32.2
ft/s
2
in the E.S.), and A is the cross-sectional area. Use SCILAB function fsolve to obtain the
critical depth, y
c
, (i.e., the water depth at critical conditions) for the data shown in the
following table:
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
System of
Case units b z Q
(a) S.I. 0.6 1.5 0.2
(b) S.I. 1.5 0.5 0.15
(c) S.I. 0.5 1 0.35
(d) E.S. 1.4 1 0.6
(e) E.S. 1.2 0.5 0.8
(f) E.S. 0.6 0.75 1
[24]. The conditions of open channel flow at the entrance from a reservoir are determined by
the simultaneous solution of the energy equation and Mannings equation. The energy
equation, for an available head of H
o
at the reservoir, is written as
,
)] ( [ 2
2
2
0
y A g
Q
y H +
while the Mannings equation is written as
,
)] ( [
)] ( [
0
3 / 2
3 / 5
S
y P
y A
n
C
Q
u

where C
u
is a coefficient that depends on the system of units used, with C
u
= 1.0 for the
Systeme Internationale (S.I.) and C
u
= 1.486 for the English (or Imperial) System of units (E.S.),
n is the Mannings coefficient (typically, between 0.001 and 0.3), A(y) is the cross-sectional
area, P(y) is the wetted perimeter of the cross-section (i.e., the length of the cross-sectional
boundary in contact with the water), and Q is the discharge.
Typically the values of n, g, C
u
, S
0
, and the geometry of the cross-section are known. The
simultaneous solution of these two equations produces as a result the values of the water
depth, y, and the flow discharge, Q.
For a trapezoidal cross-section of bottom width b and side slope z, the area and wetted
perimeter are given by
2
1 2 ) ( , ) ( ) ( z y b y P y zy b y A + + + .
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
Use SCILAB function fsolve to obtain the simultaneous solution of the energy and Mannings
equation at the entrance from a reservoir into an open channel using the following data values:
System of
Case units Ho b z n So
(a) S.I. 4.5 1.2 1.5 0.012 0.0001
(b) S.I. 3.5 0.8 0.5 0.023 0.00001
(c) S.I. 2.5 0.4 1 0.01 0.001
(d) E.S. 6 5 1 0.012 0.0001
(e) E.S. 3 3 0.5 0.018 0.00001
(f) E.S. 10 7.5 0.75 0.015 0.0001
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Numerical Integration Using SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
NUMERICAL INTEGRATION USING SCILAB 3
Integrals calculated through summation of rectangles 3
Applications of function Sumint for calculating integrals 6
Trapezoid Rule for Numerical Integration 10
Trapezoid rule calculation using SCILAB function inttrap 11
Additional examples for function inttrap 13
Plotting the trapezoidal approximation 13
Simpson's 1/3 Rule 15
Simpson's 3/8 Rule 18
Newton-Cotes Formulas 20
Romberg Integration 22
Other integrating functions provided by SCILAB 24
Integration by quadrature 24
Integration by spline interpolation 25
Calculation of definite integrals 25
Integrals of functions of a complex variable 26
Functions of a complex variable 26
D De er ri iv va at ti iv ve e o of f a a c co om mp pl le ex x f fu un nc ct ti io on n 27
Integrals of complex functions 28
SCILAB functions for integrals of complex functions 29
Applications of integrals of one variable 31
Areas under curves 31
Area between curves 32
Center of mass of an area 33
Volume of a solid of revolution 35
Moment of inertia of an area 36
Parallel axes theorem 39
Centroidal moments of inertia for a rectangle 39
Moment of inertia of the semicircle using centroidal axes 40
Area enclosed by a curve in polar coordinates 40
Fluid dynamics: calculating discharge in pipe for laminar flow 41
Line integrals 42
Evaluation of line integrals 43
Line integrals independent of path 46
Work of a force as a line integral 47
Double integral in a rectangular domain 48
User-defined function for calculating double integrals 49
Applications of function DoubleIntegral 50
Double integrals transforming integration region into a rectangle 53
Simpsons 1/9 rule for double integrals 54
Application of double integrals 58
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Area properties with double integrals 59
Calculation of areal properties in Cartesian coordinates 60
Change of variables in double integrals 62
Area calculation using double integrals with polar coordinates 63
Double integral applications in polar coordinates 64
Calculation of areal properties in polar coordinates 65
A final note on the numerical calculation of double integrals 67
Exercises 67
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Numerical integration using SCILAB
Integrals can be interpreted as the area under the curve of the function f(x) in a given interval
a < x < b. Such an integral is written as

b
a
dx x f ) (
where the term dx, referred to as the differential of x, indicates the variable of integration. In
the next section, we present methods to estimate the value of an integral by using
summations.
Integrals calculated through summation of
rectangles
The integral of a function f(x) in an interval (a,b), is defined as the limit of the sum
as x
i
! 0, or n ! . The values x
i
represent the length of n sub-intervals in (a,b), so that
the values
i
are contained within the i-th sub-interval, i.e., x
i

i
x
i+1
. The sub-intervals
are limited by the values x
1
, x
2
, , x
n
, x
n+1
, therefore, x
i
= x
i+1
x
i
,
The figure below illustrates the meaning of the terms in the summation. The terms f(
i
)x
i
represent increments of area, A
i
, under the curve y = f(x) in the interval (a,b).
While there are no restrictions in the way we may divide the interval (a,b) to generate n sub-
intervals, or where to select
i
within a sub-interval, dividing it into n equally-spaced sub-
intervals, and selecting the values of
i
in a regular fashion, facilitates the calculation of the
summation.


n
i
i i n
x f S
1
, ) (
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
To divide the interval (a,b) into n sub-intervals we take,
x = (b-a)/n,
therefore,
x
1
= a, x
2
= x
1
+ x, x
3
= x
1
+ 2x, , x
i
= x
1
+ (i-1)x, , x
n
= x
1
+ (n-1)x = b.
The value of can be selected to be the leftmost value in the sub-interval (x
i
, x
i+1
), i.e.,
i
= x
i
,
the center of the sub-interval, i.e.,
i
= (x
i
+ x
i+1
)/2, or the right-most value of the sub-interval,
i.e., = x
i+1
. Suppose that we call SL
n
the summation when
i
= x
i
, then we can write:
If we call SM
n
the summation when
i
= (x
i
+ x
i+1
)/2, then we have
Finally, for the summation when
i
= x
i+1
, we have
The following function, Sumint (Summation as integrals), can be used to calculate the
summations SL
n
, SM
n
, and SU
n
, as defined above, given the values of the integration limits, a
and b, the number of sub-intervals for the summation, n, and the function to be integrated, f.
The general call to the function is
[I] = Sumint(sum_type,a,b,n,f)
where sum_type can take the values L for SL
n
, M for SM
n
, and U for SU
n
. The function
returns the value of the summation requested as well as a graph showing the function y = f(x)
in the interval a<x<b and the rectangles whose area represent the summation. The graph for
SL
n
is shown in SCILAB graphics window 1, that for SM
n
is shown in SCILAB graphics window 2,
and that for SU
n
is shown in SCILAB graphics window 3.
A listing of function Sumint follows:
function [I] = Sumint(stype,a,b,n,f)
//Calculates the summation corresponding
//to the integral:
//I = integral from a to b of f(x) dx
//
//The variable 'stype' can be one of the
//following:
//


,
_

n
i
i n
x f
n
a b
SL
1
). (

,
_

+

,
_

n
i
i i
n
x x
f
n
a b
SM
1
1
.
2

+

,
_

n
i
i n
x f
n
a b
SU
1
1
). (
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
// 'L' - lower sum
// 'M' - middle sum
// 'U' - upper sum
//
//n is the number of sub-intervals in [a,b]
//Checking that the proper value of 'stype' is used:
if (stype<>'L') & (stype<>'M') & (stype<>'U') then
error('Sumint - stype must be L, M, or U between brackets');
abort;
end;
//Calculating parameters for sum and plot
Dx = (b-a)/(n-1); //Increment in x
x = [a:Dx:b]; //List of values of x
y = feval(x,f); //List of values of the function, y = f(x)
[n m] = size(x); //Size of vectors x and y
xmin = min(x); //Minimum value of x
xmax = max(x); //Minimum value of y
ym = f(xmax); //Value of y = f(xmax)
yy = [y(2:m), ym]; //Vector of values of y shifted one Dx to the right
ymin = min(y); //Minimum value of y
ymax = max(y); //Maximum value of y
//Change ymin to zero if ymin is larger than zero:
if ymin>0 then
ymin = 0
end;
//Draw plot of function and rectangles, and calculate summation
if stype == 'L' then
xset('window',1);xbasc(1);
plot2d1('onn',x',y',[1],'011','y',[xmin ymin xmax ymax]); //[1]
plot2d2('onn',x',y',[1],'000'); //[2]
plot2d3('onn',x',y',[1],'000'); //[3]
xtitle('Left sum','x','y'); //[4]
I = sum(y(1:m-1))*Dx; //[5]
elseif stype == 'M' then
xset('window',2);xbasc(2);
yyy = (y+yy)/2;
plot2d1('onn',x',y',[1],'011','y',[xmin ymin xmax ymax]);
plot2d2('onn',x',yyy',[1],'000');
plot2d3('onn',x',yyy',[1],'000');
xtitle('Middle sum','x','y');
I = sum(yyy(1:m-1))*Dx;
else
xset('window',3);xbasc(3);
plot2d1('onn',x',y',[1],'011','y',[xmin ymin xmax ymax]);
plot2d2('onn',x',yy',[1],'000');
plot2d3('onn',x',yy',[1],'000');
xtitle('Right sum','x','y');
I = sum(yy(1:m-1))*Dx;
end;
// Notes:
// [1] Plot the curve y=f(x).
// [2] Plot step function for y = f(x).
// [3] Plot vertical lines for y = f(x).
// [4] Plot title and labels.
// [5] Calculate summation.
//Draw the x-axis if ymin is negative:
if ymin<0 then
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
xpoly([xmin,xmax],[0,0],'lines'); //draw x-axis if any
end;
//end function
Applications of function Sumint for calculating integrals
We use function Sumint to approximate the integral of functions f(x) = 1-x
2
and g(x) = sin(x) +
sin(2x). First, we define the functions:
-->deff('[y]=f(x)','y=1-x^2')
-->deff('[y]=g(x)','y=sin(x)+sin(2*x)')
Next, function Sumint is loaded into SCILAB: -->getf('Sumint')
The following calls to function Sumint calculate the integral of f(x) with a = 0 and b = 1.5 using
n = 20. The figures illustrating the summation calculations are shown after the function calls.
-->Sumint('L',0,1.5,40,f)
ans = .4178994
-->Sumint('M',0,1.5,20,f)
ans = .3734418
-->Sumint('U',0,1.5,20,f)
ans = .2846260
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
The actual value of the integral for f(x) between a = 0 and b = 1.5 is
3
5 . 1
5 . 1 | )
3
1
( ) 1 ( ) (
3
1
0
3
5 . 1
0
2
5 . 1
0


x x dx x dx x f
i.e.,
-->1.5-1.5^3/3
ans =
.375
The values calculated through the summations differ from the actual value of the integral with
relative errors of 11.44% for SL
n
, 0.41% for SM
n
, and 24.09% for SU
n
, as shown next:
-->error_SLn = (0.4178994-0.375)/0.375*100
error_SLn =
11.43984
-->error_SMn = (0.3734418-0.375)/0.375*100
error_SMn =
- .41552
-->error_SUn = (0.2846260-0.375)/0.375*100
error_SUn =
- 24.099733
The summation SM
n
, with an error of less than 1%, provides the best approximation to the
integral out of the three summations calculated above. Improved values of the summation
approximations can be obtained by increasing the number of sub-intervals in the summation.
The following calls to function Sumint use values of n = 100 (graphics are omitted):
-->Sumint('L',0,1.5,100,f)
ans =
.3919881
-->Sumint('M',0,1.5,100,f)
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
ans =
.3749426
-->Sumint('U',0,1.5,100,f)
ans =
.3578972
The corresponding errors are:
-->error_SL100 = (0.3919881-0.375)/0.375*100
error_SL100 =
4.53016
-->error_SM100 = (0.374926-0.375)/0.375*100
error_SM100 =
- .0197333
-->error_SMU100 = (0.357872-0.375)/0.375*100
error_SMU100 =
- 4.5674667
Thus, SL
n
and SU
n
show errors of the order of 4.5% for n = 100. The middle-sum, SM
n
, on the
other hand, shows an error of about 2/10%. Thus, the middle sum seems to provide the
smallest relative error. Also, as the value of n grows larger, the better the approximation to
the integral. The following sequence of SCILAB statements use function Sumint to
approximate the integral with values of n = 10, 100, 1000, 10000, 100000.
-->nn = [10,100,1000,10000,100000];
-->SumintSeq = [];
-->for j = 1:5
--> n = nn(j);
--> SumintSeq = [SumintSeq Sumint('M',0,1.5,n,f)];
-->end;
SCILAB may take longer than usual to finish this operation since the last integral requires
calculation 100000 values of y before summing. After the summation calculations end, the
sequence of values of the integral is:
-->SumintSeq
SumintSeq =
! .3680556 .3749426 .3749994 .3751875 .375 !
These results verify the observation that the approximation to the integral improves as the
value of n increases.
The following calls to function Sumint calculate the integral of g(x) = sin(x) + sin(2x), with a =
0 and b = using n = 40. The figures illustrating the summation calculations are shown after
the function calls.
-->Sumint('L',0,%pi,40,g)
ans = 1.9989184
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
-->Sumint('M',0,%pi,40,g)
ans = 1.9989184
-->Sumint('U',0,%pi,40,g)
ans = 1.9989184
The actual value of the integral for g(x) between a = 0 and b = is


0
0 0
| ) 2 cos
2
1
cos ( ) 2 sin (sin ) ( x x dx x x dx x g +

i.e.,
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
--> -cos(%pi)-(1/2)*cos(2*%pi)-(-cos(0)-(1/2)*cos(2*0))
ans =
2.
For the function y = g(x) = sin(x) + sin(2x) the three summation approximations to the integral
with n = 40, produce results that are very close to the actual value of 2.0.
The examples presented in this section, thus, illustrate the fact that there is always an error
involved in estimating an integral through summations. In the next section we present the
trapezoidal rule for integration, which reduces the error in the numerical calculation of the
integral with respect to the summation of rectangles used in this section.
Trapezoid Rule for Numerical Integration
Numerical methods are often used to calculate integrals, particularly when the integrand does
not have a closed-formed anti-derivative. One such integral, which is used very often in
probability and statistics, as well as in the study of dispersion of contaminants in fluid flow, is
the integral of the Gaussian curve:
We will develop herein a general method to estimate the integral,
dz
z
b
a
)
2
exp(
2

. ) ( dx x f I
b
a

Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz


As illustrated in the figure above we can divide the interval (a, b), where a = x
1
, and b = x
n
,
into (n-1) sub-intervals: (x
1
,x
2
), (x
2
,x
3
),,(x
n-1
, x
n
), or (x
i
, x
i+1
) for i = 1, 2, , n-1. Recalling
that the integral represents the area under the curve y = f(x) between values of x = a and x =
b, we can estimate the area under the curve corresponding to sub-interval i as
A
i
= 1/2(x
i+1
- x
i
) (f(x
i
)+f(x
i+1
)) = 1/2 x
i
(f(x
i
)+f(x
i+1
))
where,
x
i
= x
i+1
- x
i
.
We can simplify the calculation by taking the interval width to be a constant value, say x. In
such case, the integral is approximated by
Because the area calculated in this equation is the area of the trapezoid limited by the sub-
interval (x
i
, x
i+1
) , and the curve y = f(x), this method is known as the trapezoid rule.
For the case of a constant sub-interval width the following relationships apply:
x = (x
n
- x
1
)/(n-1),
and
x
i
= x
1
+ (i-1)x,
for i =1,2,,n.
Trapezoid rule calculation using SCILAB function inttrap
SCILAB provides function inttrap to calculate the numerical integral of a function using the
trapezoid rule. The general call to the function is:
[v] = inttrap([x,] y),
where x is the vector of increasing x coordinate data, its default value being x = [1, 2, , m],
where m is the size of vector y, y is the vector of y coordinate data, and v is the value of the
integral.
The function calculates
where f is a function described by the data set of data y
i
= f(x
i
), with a = x
1
, and b = x
n
.
To check the operation of function inttrap we will use the functions f(x) and g(x) used in the
previous section when approximating integrals through the summation of rectangles. First, we
calculate the integral of f(x) = 1 - x
2
in the interval [0,1.5] using x increments of x = 0.5, 0.25,
0.10, 0.05, and 0.01, corresponding to n = 3, 6, 15, 30, 150 sub-intervals.
-->x=[0:0.5:1.5];inttrap(x,f(x))
ans =
, ) (

b
a
dx x f v
x x f x f x f A I
n
n
i
i
n
i
i
+ +


)] ( ) ( 2 ) ( [
2
1
1
1
1
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
.3125
-->x=[0:0.25:1.5];inttrap(x,f(x))
ans =
.359375
-->x=[0:0.1:1.5];inttrap(x,f(x))
ans =
.3725
-->x=[0:0.05:1.5];inttrap(x,f(x))
ans =
.374375
-->x=[0:0.01:1.5];inttrap(x,f(x))
ans =
.374975
The next set of examples shows the integral of function g(x) = sin(x) + sin(2x) in the interval
[0, ], with values of x = /2, /3, /5, /10, /20, and /100, corresponding to n = 2, 3, 5,
10, 20, and 100.
-->x = [0:%pi/2:%pi];inttrap(x,g(x))
ans =
1.5707963
-->x = [0:%pi/3:%pi];inttrap(x,g(x))
ans =
1.8137994
-->x = [0:%pi/5:%pi];inttrap(x,g(x))
ans =
1.9337656
-->x = [0:%pi/10:%pi];inttrap(x,g(x))
ans =
1.9835235
-->x = [0:%pi/20:%pi];inttrap(x,g(x))
ans =
1.995886
-->x = [0:%pi/50:%pi];inttrap(x,g(x))
ans =
2.0013068
-->x = [0:%pi/100:%pi];inttrap(x,g(x))
ans =
2.0003284
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
As observed in the case of the summation of rectangles presented in the previous section, the
approximation of the integral improves as the number of sub-intervals used in the calculation
increases.
Additional examples for function inttrap
The following examples illustrate different forms for calling function inttrap. The exercises
are provided for the user to try on his or her own.
In this first example, a call to a SCILAB function, namely sin(x), is included as the second
argument to function inttrap:
x=(0:0.1:1.5);
inttrap(x,sin(x))
In the next example, a user-defined SCILAB function is included as the second argument to
function inttrap:
x = (0:0.1:1.0);
deff('[y]=f(x)','y = x^3 - 2*x + sin(x)');
inttrap(x,f(x))
In the next three examples, both x and y are defined as vectors before calling function inttrap:
x = (0.4:0.2:2.0);
y=[5.16, 3.6922, 3.14, 3.0, 3.1067, 3.3886, 3.81,4.3511, 5.0];
inttrap(x,y)
x = (1.0:0.1:2.0);
y=x^(-1) ;
inttrap(x,y)
x=[0., 0.05, 0.1, 0.2, 0.5, 0.6, 0.9, 1.0];
y=[1.2, 1.3, 1.25, 1.45, 2.3, 4.5, 5.0, 1.2];
inttrap(x,y)
Plotting the trapezoidal approximation
To illustrate the trapezoidal approximation to an integral we propose the following user-
defined function plottrap (plot trapezoidal approximation):
function plottrap(x,f)
//This function plots the trapezoidal approximation
//to a function y = f(x) as well as plotting the
//original function on the range of values of x.
[n m] = size(x); //Size of vector x
y = f(x); //Data for trapezoidal fitting
xx = [x(1):(x(m)-x(1))/100:x(m)]; //Generate 100 points xx on x
yy = f(xx); //Generate y = f(xs) for xx
xmin = min(xx); //Minimum value of xx
xmax = max(xx); //Maximum value of xx
ymin = min(yy); //Minimum value of yy
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
ymax = max(yy); //Maximum value of yy
//Change ymin to zero if ymin is larger than zero:
if ymin>0 then
ymin = 0
end;
//Draw plot of function and trapezoids
xset('window',1);xbasc(1);
plot2d1('onn',xx',yy',[1],'011','y',[xmin ymin xmax ymax]); //[1]
xpoly(x',y','lines'); //[2]
plot2d3('onn',x',y',[1],'000'); //[3]
xtitle('Trapezoid rule','x','y'); //[4]
// Notes:
// [1] Plot the curve y=f(x).
// [2] Plot trapezoid top for y = f(x).
// [3] Plot vertical lines for y = f(x).
// [4] Plot title and labels.
//Draw the x-axis if ymin is negative:
if ymin<0 then
xpoly([xmin,xmax],[0,0],'lines'); //draw x-axis if any
end;
//end function
The call to the function requires that the user provide an ordered vector of values of x and the
function f to be plotted. The general call to the function is plottrap(x,f). An example of its
application is shown below:
-->xx = [0:%pi/10:%pi];
-->deff('[y]=g(x)','y=sin(x)+sin(2*x)')
-->plottrap(xx,g)
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
Simpson's 1/3 Rule
The summation of rectangles for estimating integrals can be though of as approximating the
function to a constant value (the height of the rectangle) in each sub-interval [x
i
,x
i+1
]. The
trapezoid rule can be thought of as fitting a straight line between the points (x
i
,f(x
i
)) and
(x
i+1
,f(x
i+1
)) in each sub-interval. Simpsons 1/3 rule results from fitting a quadratic equation,
say,
y = px
2
+ qx + r,
using the three consecutive points (x
i
,f(x
i
)), (x
i+1
,f(x
i+1
)), and (x
i+2
,f(x
i+2
)) that link the two
consecutive sub-intervals [x
i
, x
i+1
] and [x
i+1
, x
i+2
], as illustrated in the figure below (with y
i
=
f(x
i
)):
You can verify that, for sub-intervals of the same length, x = x
i+1
x
i
= x
i+2
x
i
, the expression
for the integral in the two sub-intervals limited by [x
i
, x
i+2
] is given by
). 4 (
3
) (
2 1
2
+ +
+ +

+
i i i
x
x
y y y
x
dx x f
i
i
A similar expression can be found for the integral in the next pair of sub-intervals [x
i+2
,x
i+3
] and
[x
i+3
,x
i+4
], i.e.,
). 4 (
3
) (
4 3 2
4
2
+ + +
+ +

+
+
i i i
x
x
y y y
x
dx x f
i
i
Combining the last two expressions we get an approximation to the integral in the interval
[x
i
,x
i+4
], i.e.,
). 4 2 4 (
3
) (
4 3 2 1
4
+ + + +
+ + + +

+
i i i i i
x
x
y y y y y
x
dx x f
i
i
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
Following this pattern of summation, we can write the integral in the next 4 sub-intervals as
). 4 2 4 (
3
) (
8 7 6 5 4
8
4
+ + + + +
+ + + +

+
+
i i i i i
x
x
y y y y y
x
dx x f
i
i
Adding the last two expressions we can write the integral for the 8 sub-intervals enclosed in
[x
i
,x
i+8
] as:
), 4 2 4 2 4 2 4 (
3
) (
8 7 6 5 4 3 2 1
8
+ + + + + + + +
+ + + + + + + +

+
i i i i i i i i i
x
x
y y y y y y y y y
x
dx x f
i
i
or,
]. ) 2 2 ( 2 ) ( 4 [
3
) (
8 6 4 2 7 5 3 1
8
+ + + + + + + +
+ + + + + + + +

+
i i i i i i i i i
x
x
y y y y y y y y y
x
dx x f
i
i
We can see the general pattern of the integral in the latter expression. One obvious result is
that the number of sub-intervals for the calculation of the integral must be even. This is so
that we can fit quadratic equations through pairs of consecutive sub-intervals. Thus, if we
divide the interval [a,b] into 2n sub-intervals, referring to the values of x limiting those sub-
intervals as x
0
(=a), x
1
, x
2
, , x
2n
(=b), (make i=0 in the previous expression) we can write the
general expression for Simpsons 1/3 rule as
]. ) 2 2 ( 2 ) ( 4 [
3
) (
2 2 2 6 4 2 1 2 5 3 1 0 n n n
b
a
y y y y y y y y y y
x
dx x f + + + + + + + + + + +

! !
From the last expression it is obvious why this approach is referred to as the 1/3 rule. The
following function, simpson13, calculates an integral using Simpson's 1/3 rule given the vectors
x and y containing an odd number of elements. Vector x must contain the limits of the even
number of sub-intervals into which the integration interval [a,b] is divided:
function [I] = simpson13(x,f)
//This function calculates the numerical integration of f(x)dx
//between limits x(1) and x(n) using Simpson's 1/3 rule
//Check that x and y have the same size (which must be an odd number)
//Also, the values of x must be equally spaced with spacing Dx
[nrx,ncx]=size(x)
[nrf,ncf]=size(f)
if((nrx<>1)|(nrf<>1))then
error('x or f, or both, not column vector(s)');
abort;
end;
if((ncx<>ncf))then
error('x and f are not of the same length');
abort;
end;
//check that the size of the vector x and f is odd
if(modulo(ncx,2)==0)then
disp(ncx,"list size =")
error('list size must be an odd number');
abort
end;
n = ncx;
xdiff = mtlb_diff(x);
h = xdiff(1,1);
I = f(1,1) + f(1,n);
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
for j = 2:n-1
if(modulo(j,2)==0) then
I = I + 4*f(1,j);
else
I = I + 2*f(1,j);
end;
end;
I = (h/3.0)*I
//end of function simpson13
The following commands show applications of the function simpson13 for calculating integrals.
The function inttrap is also used to compare the results of the two integration procedures.
The main restriction in using the simpson13 function is that the number of elements in the x,f
vectors must be an odd number. Also, the spacing in the values of x must be constant.
-->getf('simpson13') //Load function
-->x = (0:0.1:1.6); //Generate vector of x values
-->simpson13(x,sin(x)) //Simpson 1/3 rule - Example 1
ans =
1.0292001
-->inttrap(x,sin(x)) //Compare with inttrap
ans =
1.0283417
-->x = (0.4:0.2:2.0); //New vector of values of x
-->y = [5.16, 3.69, 3.14, 3.0, 3.11, 3.39, 3.81, 4.35, 5.0];
-->simpson13(x,)) //Simpson 1/3 rule - Example 2
ans =
5.8666667
-->inttrap(x,y) //Compare with inttrap
ans =
5.914
-->x = (1.0:0.1:2.0); //New vector of values of x
-->y=x^(-1); //Vector y = 1/x
-->simpson13(x,y) //Simpson 1/3 rule - example 3
ans =
.6931502
-->inttrap(x,y) //Compare with inttrap
ans =
.6937714
-->deff('[y]=f(x)','y=x^3-2*exp(x)') //User-defined function
-->x = (1.0:0.1:2.0); //New vector of values of x
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
-->yp = f(x); //Vector of values of y
-->simpson13(x,yp) //Simpson 1/3 rule - example 4
ans =
- 5.5915537
-->inttrap(x,yp) //Compare with inttrap
ans =
- 5.5918319
Simpson's 3/8 Rule
In the previous section we demonstrated that Simpsons 1/3 rule results from fitting quadratic
equations to the three points (x
i
,f(x
i
)) defining two consecutive sub-intervals in the integration
interval (a,b). Simpsons 3/8 rule follows from fitting cubic equations to the four points that
define three consecutive sub-intervals in the integration interval. Skipping the details of the
derivation, we present the general form of the integral approximated by Simpsons 3/8 rule as:

b
a
dx x f ) (
]. ) ( 2 ) ( 3 ) ( 3 [
8
3
3 3 3 6 3 1 3 5 2 2 3 4 1 0 n n n n
y y y y y y y y y y y x + + + + + + + + + + + + +

! ! !
The method requires that the integration interval be divided into a number of sub-intervals
that is a multiple of three. As in the case of Simpsons 1/3 rule, we define y
i
= f(x
i
), and take
x
0
= a and x
3n
= b.
The following function, simpson38, calculates an integral using Simpson's 3/8 rule given a
vector of equally-spaced values of x = [a=x
0
, x
1
,, x
3n
= b], the corresponding vector of values
of y = [y
0
y
1
y
3n
] = [ f(x
0
) f(x
1
) f(x
3n
)]. The function checks that the number of sub-
intervals is indeed a multiple of 3, producing an error otherwise.
function [I] = simpson38(x,f)
//This function calculates the numerical integration of f(x)dx
//between limits x(1) and x(n) using Simpson's 3/8 rule
//Check that x and y have the same size (which must be of the form 3*i+1,
//where i is an integer number)
//Also, the values of x must be equally spaced with spacing h
[nrx,ncx]=size(x);
[nrf,ncf]=size(f);
if((nrx<>1)|(nrf<>1))then
error('x or f, or both, not column vector(s)');
abort;
end;
if((ncx<>ncf))then
error('x and f are not of the same length');
abort;
end;
//check that the size of the lists xL and f is odd
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
if(modulo(ncx-1,3)<>0)then
disp(ncx,"list size =")
error('list size must be of the form 3*i+1, where i=integer');
abort
end;
n = ncx;
xdiff = mtlb_diff(x);
h = xdiff(1,1);
I = f(1,1) + f(1,n);
for j = 2:n-1
if(modulo(j-1,3)==0) then
I = I + 2*f(1,j);
else
I = I + 3*f(1,j);
end;
end;
I = (3/8)*h*I;
//end of function simpson38
The following commands show applications of the function simpson38 for calculating integrals.
The function inttrap is also used to compare the results of the two integration procedures.
The main restriction in using the simpson38 function is that the number of elements in the x,f
vectors must be a number of the form 3*i+1, where i is an integer. Also, the spacing in the
values of x must be constant.
-->getf('simpson38') //Load function
-->x=(0:0.1:1.8); //Vector of values of x
-->simpson38(x,sin(x)) //Simpsons 3/8 rule - example 1
ans =
1.2272036
-->inttrap(x,sin(x)) //Compare with inttrap
ans =
1.2261793
-->x = (0.4:0.2:2.2); //New vector of values of x
-->y = [5.16, 3.70, 3.4, 3.0, 3.11, 3.39, 3.81, 4.35, 5.0, 5.2];
-->simpson38(x,y) //Simpsons 3/8 rule - example 2
ans =
6.96225
-->inttrap(x,y) //Compare with inttrap
ans =
6.988
-->x = (1.0:0.1:1.9); //New vector of values of x
-->y = x^(-1); //Vector of values of y = 1/x
-->simpson38(x,y) //Simpsons 3/8 rule - example 3
ans =
.6418605
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
-->inttrap(x,y) //Compare with inttrap
ans =
.6424556
-->deff('[y]=f(x)','y=x^3-2*exp(x)') //User defined function for integration
-->x = (1.0:0.1:1.9); //New vector of values of x
-->yp = f(x); //Vector of values of y = f(x)
-->simpson38(x,yp) //Simpsons 3/8 rule - example 4
ans =
- 4.9272101
-->inttrap(x,yp) //Compare with inttrap
ans =
- 4.9272868
Newton-Cotes Formulas
The trapezoidal rule and Simpsons 1/3 and 3/8 rules are the first three of a collection of
formulas based on equally spaced values of the independent variable and commonly known as
Newton-Cotes Formulas. The Newton-Cotes formulas are based on fitting polynomials of orders
n = 1, 2, 3, etc. to 2, 3, 4, etc. consecutive points determining the sub-intervals into which the
integration interval [a,b] is divided.. The general expression for the formulas is
....), ( ) (
1 1 0 0
+ +

y y x n dx x f
b
a

where n represents the number of intervals and the degree of the polynomial fitted, and
i
s
are coefficients given in the following table:
n
0

1 1 1 1/2
2 1 4 1 1/6
3 1 3 3 1 1/8
4 7 32 12 32 7 1/90
5 19 75 50 50 75 19 1/288
6 41 216 27 272 27 216 41 1/840
7 751 3577 1323 2989 2989 1323 3577 751 1/17280
The following function, NewtonCotes, allows the estimation of an integral using the Newton-
Cotes formulas up to seventh degree. It is implemented only for functions defined as y = f(x)
(i.e., the function f must be defined using deff or by a file-defined function loaded with getf).
The function NewtonCotes requires the limits of integration (a,b), the function name f(x), and
the degree of the polynomial to be used n (1 n 7).
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
function [I] = NewtonCotes(a,b,f,n)
//This function calculates the numerical integration of f(x)dx
//between limits a and b using Newton-Cotes integration of degree n.
if((n<1)|(n>7))then
disp(n,"n =");
error('n must be an integer between 1 and 7.');
abort;
end;
kappa = [1/2, 1/6, 1/8, 1/90, 1/288, 1/840, 1/17280];
beta = [[1 1 0 0 0 0 0 0];
[1 4 1 0 0 0 0 0];
[1 3 3 1 0 0 0 0];
[7 32 12 32 7 0 0 0];
[19 75 50 50 75 19 0 0];
[41 216 27 272 27 216 41 0];
[751 3577 1323 2989 2989 1323 3577 751]];
h = (b-a)/n;
x = zeros(n+1,1);
y = x;
I = 0.0
for j = 1:n+1
x(j,1) = a + (j-1)*h;
y(j,1) = f(x(j,1));
I = I + beta(n,j)*y(j,1);
end;
I = n*kappa(n)*h*I
//end function NewtonCotes
The following examples show applications of the Newton-Cotes integration formulas. In each
example we define a function using SCILABs function deff and produce a vector containing the
integrals using polynomials of orders n = 1 to n = 7:
-->getf('NewtonCotes')
-->deff('[y]=f(x)','y=1/x');
-->I = []; for k=1:7, I=[I NewtonCotes(1,2,f,k)]; end; I
I =
column 1 to 6
! .75 .6944444 .69375 .6931746 .6931630 .6931481 !
column 7
! .6931477 !
-->deff('[y]=f(x)','y=x');
-->I = []; for k=1:7, I=[I NewtonCotes(1,2,f,k)]; end; I
I =
! 1.5 1.5 1.5 1.5 1.5 1.5 1.5 !
-->deff('[y]=f(x)','y=x^2');
-->I = []; for k=1:7, I=[I NewtonCotes(1,2,f,k)]; end; I
I =
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
column 1 to 6
! 2.5 2.3333333 2.3333333 2.3333333 2.3333333 2.3333333 !
column 7
! 2.3333333 !
-->deff('[y]=f(x)','y=exp(x)');
-->I = []; for k=1:7, I=[I NewtonCotes(1,2,f,k)]; end; I
I =
column 1 to 6
! 5.053669 4.672349 4.6714765 4.6707766 4.6707756 4.6707743 !
column 7
! 4.6707743 !
-->deff('[y]=f(x)','y=log(x)')
-->I = []; for k=1:7, I=[I NewtonCotes(1,2,f,k)]; end; I
I =
column 1 to 6
! .3465736 .3858346 .3860838 .3862879 .3862906 .3862942
!
column 7
! .3862943 !
Romberg Integration
The Romberg algorithm consists in improving the approximation of an integral by first
calculating the integral using a constant increment x and then re-calculating it with an
increment x/2. The two values of the integral thus calculated are then combined through the
formula:
[ ],
3
1
) (
2 / 2 / x x x
I I I Romberg I

+ +
where I
x/2
is the integral calculated with a sub-interval size of x/2 and I
x
is the integral
calculated with a sub-interval size of x.
To calculate the values I
x/2
and I
x
we can simply use the trapezoidal rule (i.e., SCILABs
inttrap function), such as illustrated in function Romberg, listed below:
function [I]=Romberg(a,b,f,h)
//This function calculates the numerical integral of f(x) between
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
//x = a and x = b, with intervals h. Intermediate results are obtained
//by using SCILAB's own inttrap function
x=(a:h:b);
y=f(x);
I1 = inttrap(x,y);
x=(a:h/2:b);
y=f(x);
I2 = inttrap(x,y);
I = I2 + (1.0/3.0)*(I2-I1);
//end function Romberg
The call to function Romberg is similar to that of NewtonCotes except that the increment h is
used in place of a polynomial order. Some examples of Romberg integration are shown below:
-->getf('Romberg.txt')
-->Dx = [0.1,0.05,0.01,0.005,0.001];
-->deff('[y]=f(x)','y=x^(-1)')
-->I=[];for j=1:5, h=Dx(j); I = [I Romberg(1,2,f,h)]; end; I
I =
! .6931474 .6931472 .6931472 .6931472 .6931472 !
-->deff('[y]=f(x)','y=exp(-0.02*x).*sin(x)')
-->I=[];for j=1:5, h=Dx(j); I = [I Romberg(1,2,f,h)]; end; I
I =
! .9280852 .9280852 .9280852 .9280852 .9280852 !
-->deff('[y]=f(x)','y=x^3-3*x+2')
-->I=[];for j=1:5, h=Dx(j); I = [I Romberg(1,2,f,h)]; end; I
I =
! 1.25 1.25 1.25 1.25 1.25 !
-->deff('[y]=f(x)','y=exp(-x^2/2)/sqrt(2*%pi)')
-->I=[];for j=1:5, h=Dx(j); I = [I Romberg(0,1,f,h)]; end; I
I =
! .3413448 .3413447 .3413447 .3413447 .3413447 !
-->deff('[y]=f(x)','y=sin(x)')
-->I=[];for j=1:5, h=Dx(j); I = [I Romberg(0.1,1,f,h)]; end; I
I =
! .4547019 .4547019 .4547019 .4547019 .4547019 !
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
Other integrating functions provided by SCILAB
This section presents functions provided by SCILAB that produce integration by quadrature, by
spline fitting, and by definite integration.
Integration by quadrature
By integration by quadrature we must understand the integral of an external function, as
opposite to the integral of a function provided as a vector of values, such as those obtained
with inttrap. Integration by quadrature is accomplished with the function integrate. The
general call to the function integrate is
[x]=integrate(f,v,a,b [,ea [,er]])
where f = external SCILAB function, v = integration variable entered as a string, a,b = real
numbers (bounds of integration), and ea ,er = real numbers (absolute and relative errors).
The presence of errors
Some examples of applications of the integrate function are shown below. The first example is
the integral
0


sin(x) dx:
--> integrate('sin(x)','x',0,%pi)
ans =
2.
The following call to integrate uses a function defined by an ifelseend programming
construct:
-->integrate('if x<0 then x, else x^2-1, end','x',-2,2)
ans =
4.6666667
The following is the integral
0


dx/sin(x):
-->integrate('1/sin(x)','x',%pi/10,%pi/2)
ans =1.84273
In the next example the function f(x) is defined using deff. Notice that the call to function
integrate includes the name of the function f(x) as a string.
-->deff('[y]=f(x)','y=sin(x)+sin(2*x)')
-->integrate('f(x)','x',0,%pi)
ans = 2.
The following example is the integral
0


dx/x:
-->integrate('1/x','x',1,5)
ans =
1.6094379
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
Integration by spline interpolation
Integration by spline interpolation is accomplished by using the function intsplin, whose
general call is given by
I = intsplin([x,] y),
where x = vector of increasing x coordinate data, y = vector of y coordinate data, and I = value
of the integral. This function computes the integral of f(x) between x = x
0
and x = x
1
, where
the values of f(x) are represented by the vector y. The vector x = {x
0
, , x
n
}, and y
i
=f(x
i
). The
function between discrete values is interpolated using splines. [Note: Splines are piece-wise
curves used to fit data between consecutive data points. Splines are covered in a different
chapter of this book.]
Examples of interpolation using splines follow:
-->x = 0:0.1:%pi; y = sin(x) + sin(2*x);
-->intsplin(x,y)
ans = 2.0008637
-->x = (-4:0.1:0); y = exp(-x^2/2)/sqrt(2*%pi);
-->intsplin(x,y)
ans = .4999685
Calculation of definite integrals
SCILAB offers a function, called intg, that calculates the definite integral of a function. The
general call to function intg is
[I,err]=intg(a,b,f [,ea [,er])
where a,b = limits of integration, f = represents a function, list or string, ea = absolute error
required on the result (default value, ea = 0), er = relative error required on the result
(default value, er = 1.d-8), I = integral value, and err = estimated absolute error on the result.
If f is function its definition must be as follows: y = f(t). If f is a list the list must be as follows:
list(f,x1,x2,...), where f is a function with calling sequence: f(t,x1,x2,...). Finally, if f is a
string it refers to the name of a Fortran subroutine.
An application of function intg follows:
-->deff('[y]=f(x)','y=cos(x)+0.5*cos(2*x)+1.5*cos(3*x)')
-->[I,err] = intg(0,1,f)
err =
1.668E-14
I =
1.1393553
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Integrals of functions of a complex variable
SCILAB provides a couple of functions, intl and intc for calculation of integrals of functions of a
complex variable z = x + iy, where x and y are real variables and i = -1 is the unit imaginary
number. While the application of the functions is simple and straightforward, we need to
provide some definitions related to complex variables and their functions for the benefit of
those who have not covered functions of a complex variable.
Functions of a complex variable
We defined a complex variable z as z = x + iy, where x and y are real variables, and i = (-1)
1/2
.
We can also define another complex variable
w = F(z) = + i,
where, in general,
= (x,y), and = (x,y),
are two real functions of (x, y). These real functions can also be given in terms of the polar
coordinates (r,) if we use the polar representation for z, i.e.,
z = re
i
= r (cos + isin).
In such case,
= (r,), and = (r,).
Recall that the coordinate transformations between Cartesian and polar coordinates are:
r = (x
2
+ y
2
)
1/2
, tan=y/x,
x = r cos, y = r sin
The complex variable w is also known as a complex function. Another name for a complex
function is "mapping." Thus, we say F(z) is a mapping of z. In geometric terms, this means
that any figure in the x-y plane gets "mapped" onto a different figure on the plane by the
complex function F(z).
As an example, take the function
w = F(z) = ln (z) = ln (re
i
) = ln(r) + i.
We can identify the functions
= (r,) = ln(r), and = (r,) = ,
as the real and imaginary components, respectively, of the function ln(z). Using the
transformations indicated above we can also write,
= (x,y) = ln[(x
2
+y
2
)
1/2
] = (1/2) ln(x
2
+y
2
), and = (x,y) = tan
-1
(y/x).
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
D De er ri iv va at ti iv ve e o of f a a c co om mp pl le ex x f fu un nc ct ti io on n
The derivative of the complex variable f(z), to be referred to as f(z) = df/dz, is, by definition,
The definition of a complex derivative requires us to evaluate the function f(z) at a point P(x,y)
corresponding to z = x + iy, and at point Q(x+x, y+y), as illustrated in the figure below.
The figure also illustrates the fact that to get from point z to point z+z in the complex x-y
plane you can follow a multitude of paths. In general, the value of the derivative will depend
on the path we follow to define z. Because we want the derivative df/dz to be uniquely
defined, we need to find some criteria such that, regardless of the path selected to define z,
the value of df/dz remains the same.
In general, we will write z = x+iy. Lets calculate the derivative df/dz utilizing paths for z
along the x-axis alone, i.e., z = x, and along the y-axis alone, i.e., z = iy. Thus, for z =
x, we can write
You can prove, by expressing the derivative in terms of z = iy, that
In order for the last two expressions for df/dz to be the same, we require that
.
) ( ) (
lim ) ( '
0
z
z f z z f
dz
df
z f
z

+


x
y x i y x y x x i y x x
dz
df
x

+ + + +


)] , ( ) , ( [ )] , ( ) , ( [
lim
0

,
_

+
+


x
y x y x x
i
x
y x y x x
dz
df
x
)] , ( ) , ( [ )] , ( ) , ( [
lim
0
.
y
i
y dz
df

. ,
x y y x


Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
These two equations are known as the Cauchy-Riemann differentiability conditions for complex
functions (or, simply, the Cauchy-Riemann conditions). Thus, if the functions (x,y) = Re[f(z)]
and (x,y) = Re[f(z)], satisfy the Cauchy-Riemann conditions, the derivative f(z)=df/dz is
uniquely defined. In such case, the function f(z) is said to be an analytical complex function,
and the functions (x,y) and (x,y) are said to be harmonic functions.
More importantly, if a complex function f(z) is analytical, the rules used for univariate
derivatives can be applied to f(z). For example, we indicated earlier that the function
w = f(z) = ln (z) = ln (re
i
) = ln(r) + i.
can be written in terms of (x,y) as
= (x,y) = ln[(x
2
+y
2
)
1/2
] = (1/2) ln(x
2
+y
2
), and = (x,y) = tan
-1
(y/x).
You can check that the functions (x,y) and (x,y) satisfy the Cauchy-Riemann conditions.
The function f(z) = ln(z) is, therefore, analytical, and its derivative can be calculated by using:
Note: Most of the functions that we commonly use with real variables, e.g., exp, ln, sin, cos,
tan, asin, acos, atan, hyperbolic functions, polynomials, 1/x, square root, etc., are analytical
functions when used with the complex variable z = x +iy. Thus, the rules of derivatives for
these functions are the same as in real variables, e.g., d(sin(z))/dz = cos(z), d(z
2
+z)/dz = 2z+1,
etc.
Integrals of complex functions
If
( ) f z + ( ) , x y i ( ) , x y
is a complex analytic function in a given region of the plane, we
define the integral of f(z) along a path C from point z
1
+ x
1
i y
1
to point z
2
+ x
2
i y
2
, as:
) ( )] , ( ) , ( [ ) ( ) (
) , (
) , (
2 2
1 1
2
1
idy dx y x i y x dz z f dz z f
C
y x
y x
z
z
+ +



+ +
) , (
) , (
) , (
) , (
2 2
1 1
2 2
1 1
) ( ) (
y x
y x
y x
y x
dy dx i dy dx
where dz = dx + i dy.
.
1
) (ln
z
z
dz
d
dz
df

Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
If the integral evaluates to the same value regardless of the nature of path C, we say that the
integral is independent of the path. It depends only on the limits of integration z
1
and z
2
.
We can prove that if the function
( ) f z
is analytic, then the resulting integrals

+ +
) , (
) , (
) , (
) , (
2 2
1 1
2 2
1 1
) ( ) (
y x
y x
y x
y x
dy dx i dy dx
are independent of the path. The conditions for independence of the path for these two
integrals are

_
,

and

,
respectively, which are the Cauchy-Riemann conditions necessary for analycity.
Thus, integrals of any analytic function
( ) f z
can be evaluated as a simple univariate integral
treating the function z as a real variable and evaluating the complex integration limits,
z
1
+ x
1
i y
1
, and z
2
+ x
2
i y
2
, once the anti-derivative of
( ) f z
has been found.
SCILAB functions for integrals of complex functions
SCILAB provides functions intc and intl for the numerical calculation of integrals of a complex
function. Function intc integrates the complex function f(z) between complex numbers z
1
and
z
2
along the straight line connecting the two numbers. Since line integrals of analytic functions
are independent of the path, function intc can be used to evaluate integrals of any analytic
function. The general call of the function is [y]=intc(z
1
,z
2
,f), where f is an external SCILAB
function representing the complex function f(z).
The following examples show how to calculate the integral of the complex function f(z)
between complex numbers z
1
and z
2
along the straight line joining the two points.
-->deff('[w]=f(z)','w=1/z');
-->intc(2+3*%i,-5+4*%i,f)
ans =
.5743114 + 1.484058i
-->deff('[w]=f(z)','w=log(z)')
-->intc(2,3,f)
ans =
.9095425
-->intc(2-%i,%i,f)
ans =
- .7165866 - .2679858i
-->deff('[w]=f(z)','w=z+2/z^2')
-->intc(2-4*%i,3-%i,f)
ans = 9.6 + 5.2i
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
Function intl calculates the line integral of the function of a complex variable w = f(z) on the
curve C represented by the arc of a circle centered at point z = z
0
with radius r extending
between the angles
1
and
2
. The figure below illustrates the geometry of the problem.
The general call to the function is [y] = intl(
1
,
2
,z
0
,r,f), where f is the name of a SCILAB
external function. The other terms in the function call have been defined above.
The following examples show some cases of line integrals of function of a complex variable on
circular arc paths.
-->deff('[w]=f(z)','w=z^2')
-->intl(%pi/6,%pi/2,2-3*%i,0.5,f)
ans =
22.82069 - 35.377982i
-->intl(0.1,3.14,0,1.2,f)
ans =
- 1.1262672 - .1674675i
-->deff('[w]=f(z)','w=1/z+z')
-->intl(%pi/6,%pi/6+2*%pi,2+%i,1,f)
ans =
- 25.132741 + 25.132741i
The current definition of function f(z) has a discontinuity a z = 0, so if we select the center of
the circle as z
0
= 0, the integral does not converge:
-->intl(0,2*%pi,0,0.5,f)
!--error 24
convergence problem...
at line 19 of function intl called by :
intl(0,2*%pi,0,0.5,f)
Other examples of function intl follow:
-->intl(0.1,2*%pi+0.1,2+2*%i,0.5,f)
ans = - 50.265482 + 6.2831853i
-->intl(0.1,2*%pi+0.1,0.1+0.1*%i,0.5,f)
ans =
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
- .1256637 + 6.2831853i
The following example of an integral path centered at z
0
= 0.2i with radius r = 0.5 fails to
converge:
-->intl(0.1,2*%pi+0.1,0.2*%i,0.5,f)
!--error 24
convergence problem...
at line 19 of function intl called by :
intl(0.1,2*%pi+0.1,0.2*%i,0.5,f)
However, moving the center of the circle to z
0
= 0.1(1+i) produces a convergent integral:
-->intl(0.1,2*%pi+0.1,0.1+0.1*%i,0.5,f)
ans =
-.1256637 + 6.2831853i
Applications of integrals of one variable
In this section we present applications of integrals for the solution of problems from the
physical sciences.
Areas under curves
A simple physical interpretation of the integral
I d

(
a
b
( ) f x x
, is the area under the curve y =
f(x) and above the x-axes, between the values x = a and x = b.
A representative differential of area (or area element) is the rectangle of base dx and height y
= f(x) shown in the figure. We will write the area of the differential element as dA = f(x) dx.
The total area is,
A =
For example, to find the area between x = 2 and x = 4, under the curve y = ln(x+1), use:
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
-->integrate('log(x+1)','x',2,4)
ans = 2.7513527
The region under consideration can be depicted as shown below:
-->x=[0:0.1:6];y=log(x+1);
-->xvv = [2:0.1:4];yvv=log(xvv+1);
-->xva=[2 2 xvv 4 4];yva=[0 log(3) yvv log(5) 0];
-->plot(x,y,'x','y','Integral example')
-->xfpoly(xva,yva,2)
Area between curves
The area between the curves y ( ) f
1
x and y ( ) f
2
x , and x = a and x = b, assuming that in
(a,b), ( ) f
2
x ( ) f
1
x , is given by
d

(
(
a
b
( ) f
1
x ( ) f
2
x x
.
For example, let y = f
1
(x) = x and y = f
2
(x) = x
2
, for a=0.25 and b=0.75. The following
statements define the functions in SCILAB and produce the graph of the area to be calculated:
-->deff('[y]=f1(x)','y=x');deff('[y]=f2(x)','y=x^2');a=0.25;b=0.75;
-->x=[a:0.01:b];y1=f1(x);y2=f2(x);
-->xv1=[a,a];yv1=[f1(a),f2(a)];xv2=[b,b];yv2=[f1(b),f2(b)];
-->plot2d([x' x'],[y1' y2'],[1,2],'011',' ',[0 0 1 1])
-->xsegs(xv1,yv1,3);xsegs(xv2,yv2,4);
-->xtitle('Area between y1=x and y2 = x^2, 0.25<x<0.75','x','y')
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
The differential of area used for this exercise is shown below.
To calculate the area we use SCILAB function inttrap:
--> x = [0.25:0.01:0.75];y1=f1(x);y2=f2(x);y=y1-y2;inttrap(x,y);
ans = 0.11475
Center of mass of an area
Consider the rectangular element of area in the figure below, dA = f(x) dx. If the base of the
rectangle is centered at x, then the center of mass of the rectangle is the point
C[x,
( ) f x
2
] ,
i.e.,
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
x
c
x , and
y
c
( ) f x
2
.
First moments of the areas with respect to the x-and y-axes are:
M
x
d

(
(
y
c
A = d

(
(
(
(
a
b
( ) f x ( ) f x
2
x =
1
2
d

(
(
a
b
( ) f x
2
x , and M
y
d

(
(
x
c
A =
d

(
a
b
x ( ) f x x
.
For example, for the region 0 < y < ln(1+x), 2<x<4:
-->deff('[y]=f(x)','y=log(x+1)');a=2;b=4;
-->Mx = integrate('f(x)^2','x',a,b)/2
Mx = 1.9139498
-->My = integrate('x*f(x)','x',a,b)
My = 8.4228659
The center of mass has coordinates,
X
c
M
y
A
, and
Y
c
M
x
A
.
Where,
A d

(
a
b
( ) f x x
-->A = integrate('f(x)','x',a,b)
A = 2.7513527
-->xc = My/A, yc = Mx/A
xc = 3.0613545
yc = .6956396
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
The following plot shows the center of mass (centroid) of the area
-->x=[2:0.1:4];y=log(x+1);xv1=[2 2];yv1=[0 log(3)];xv2=[4 4];yv2=[0 log(5)];
-->plot2d(x,y,1,'011',' ',[0 0 6 2]);xsegs(xv1,yv1,1);xsegs(xv2,yv2,1);
-->xset('mark',-1,1);plot2d([xc],[yc],-1,'010',' ',[0 0 6 2])
-->xstring(xc,yc,'C')
-->xtitle('Centroid of an area','x','y')
Volume of a solid of revolution
The figure below shows a body of revolution generated by the rotation of the curve y = f(x),
a<x<b, about the x-axis. The shadowed rectangle represents the area differential of width dx,
and height f(x), centered at x. The rotation of that differential of area about the x-axis
generates a differential of volume in the shape of a disk (a cylinder) of height dx and base area
equal to
( ) f x
2
. The volume of the elementary disk is
dV ( ) f x
2
dx
. Therefore, the volume
of the body of revolution is V d

(
1 V = d

(
(
a
b
( ) f x
2
x
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
or example, for the region used earlier to find the centroid of the area, the volume of a body
of revolution generated by the rotation of the curve y = ln(1+x), 2<x<4, about the x axis is
given by:
-->V = integrate('%pi*f(x)^2','x',a,b)
V = 12.025702
Moment of inertia of an area
Consider the rectangular region defined by 0<x<B, 0<y<H, as shown below:
The horizontal element of area shown in the left-hand side figure, is a rectangle of width dy
and height L, and centered at y. The element's area is dA = B dy. The distance from the area
element to the x-axis is y, therefore, the moment of inertia of the area element with respect
to the x-axis is
dI
x
y
2
dA
=
y
2
B dy
. The moment of inertia of the area with respect to the x-
axis is
I
x
d

(
1 I
x
= d

(
(
y
2
A = d

(
(
0
H
y
2
B y ,
i.e.,
I
x
1
3
B H
3
For the vertical element shown in the right-hand side figure above, we can write dA = H dx.
Its moment of inertia with respect to the y-axis is,
dI
y
x
2
dA
=
x
2
H dx
, and the total moment
of inertia with respect to the y-axis is
I
y
d

(
1 I
y
= d

((
x
2
A = d

(
(
0
L
x
2
H x ,
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
i.e.,
I
y
1
3
H B
3
These two results can be used to obtain moments of inertia of areas limited by curves in the x-
y plane. For example, for the element of area shown in the figure below we can write,
B = dx,
H = f(x),
dIx
1
3

dx

( ) f x
3
,
and
I
x
1
3
d

(
(
a
b
( ) f x
3
x .
For example, to find the moment of inertia of the semi-circle shown below with respect to the
x-axis, we use:
-->deff('[w]=f(x)','w=sqrt(25-x^2)')
-->Ix = integrate('f(x)^3','x',-5,5)/3
Ix = 245.43693
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
In general, for a semi-circle of radius R:
1
3
d

(
(
R
R
( ) f x
3
x
:= Inertia
x
1
8
R~
4

Suppose that we want to find the moment of inertia of the same semicircle, but with respect
to the x-axis, we will use the element of area shown in the figure below:
The element of area actually consists of two rectangles, of base
dy
, height ( ) x
1
y and ( ) x
2
y ,
centered at
y
. The values of the functions ( ) x
1
y and ( ) x
2
y are obtained by solving for x in
the equation, x
2
+ y
2
= 25, thus, x
1
=
25 y
2
, x
2
=
25 y
2
.
The moment of inertia of the right-hand side element, is
dI
, y 1
1
3

dy
( ) x
1
y
3
, while that of
the left-hand side element is,
dI
, y 2
1
3

dy
( ) x
2
y
3
. The absolute value is used to indicate
that the moment of inertia of the element is based on that of a rectangle, where only the
rectangle's dimensions, and not their signs are important. Since,
( ) x
1
y 25 y
2
= ( ) x
2
y ,
then the moment of inertia of the two elements is dI
y
+ dI
, y 1
dI
, y 2
=
2
3

( ) 25 y
2

_
,

3
2
dy
, and
the moment of inertia of the entire semicircle, with respect to the y axis, is
I
y
2
3
d

(
(
(
(
0
5
( ) 25 y
2

_
,

3
2
y , i.e.,
-->Iy=(2/3)*integrate('(25-y^2)^(3/2)','y',0,5)
Iy = 245.43693
This result turns out to be the same as the moment of inertia of the semicircle with respect to
the x-axis. Since the moment of inertia is a measure of the resistance of a body (or an area) to
angular acceleration about the axis of interest, in the same sense that a mass is a measure of
the resistance of a body to linear acceleration, the fact that I
x
I
y
indicates that is equally
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
difficult to provide the same angular acceleration to a thin body shaped like a semicircle
whether you try to make it rotate about the x- or the y-axis.
Parallel axes theorem
The figure below shows an area in the x-y plane indicating its centroid and a couple of axes,
, x
c
y
c
, parallel to the x- and y-axes, respectively, and passing through the area's centroid. The
distance between the x- and x
c
-axes is d
y
, and that between the y- and y
c
-axes is d
x
.
The parallel axes theorem states that if I
x
represents the moment of inertia of the area with
respect to the x-axis, and I
xc
represents the moment of inertia of the area with respect to the
x
c
-axis, the following relationship holds: I
x
+ I
xc
A d
y
2
Similarly, for the moment of inertia with respect to the y-axis, I
y
, in terms of the moment of
inertia with respect to the y
c
-axis, we can write, I
y
+ I
yc
A d
x
2
Centroidal moments of inertia for a rectangle
Earlier we found the moments of inertia of a rectangle whose sides lay on the coordinate axes,
width (along the x-axis) = L, height (along the y-axis) = H, with respect to those axes as,
I
x
1 B H
3
3
, and
I
y
1 H B
3
3
. With,
A H L
, and
d
x
B
2
, and
d
y
H
2
, we can find the
centroidal moments of inertia of a rectangle as shown below:
The parallel axes theorem indicates that: I
x
= I
xc
+ Ad
y
2
, I
y
= I
yc
+ Ad
x
2
.
For the rectangle:
:= Inertia
x
1
3
B H
3
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
:= Inertia
y
1
3
H B
3
:= A B H
:= d
x
1
2
B
:= d
y
1
2
H
Solving for the centroidal moments of inertia:
{ } Inertia
xc
1
12
B H
3
{ } Inertia
yc
1
12
H B
3
Moment of inertia of the semicircle using centroidal axes
Noticing that moment of inertia of the rectangle of height can attack the problem of finding
the moment of inertia of the semicircle used earlier with respect to the y-axis + ( ) x
1
y ( ) x
2
y ,
and width
dy
, about the y-axis is the centroidal moment of inertia, i.e.,
dI
y
dI
yc
=
1
12

dy
( ) + ( ) x
1
y ( ) x
2
y
3
=
1
12

( ) 2 25 y
2
3
=
2
3

( ) 25 y
2

_
,

3
2
dy
,
which is the same value for dI
y
found earlier.
Area enclosed by a curve in polar coordinates
Consider the area enclosed by the curve r = r(

) between the rays


a
, and
b
. An
element of area in polar coordinates is shown in the figure below. The area of the element
shown is approximated by that of a triangle of height r(

) and base equal to


. ( ) r d
, i.e.,
dA
1
2
(
( ) r . d
)
( ) r
=
1
2

( ) r
2
d

.
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
For example, find the area enclosed by the curve r() = 5 cos(), between the values of

10
and

2
, we use the integral
-->th1 = %pi/10; th2 = %pi/2; A = integrate('0.5*(5*cos(th))^2','th',th1,th2)
A = 6.0171527
Fluid dynamics: calculating discharge in pipe for laminar flow
The figure below shows the profile of laminar flow velocity as a function of the radial distance
r in a pipe.
The velocity distribution is given by the expression,
v(r) = v
s.
[1-(r/r
o
)
2
],
where v
c
is the centerline velocity and r
o
is the radius of the pipe.
We can use this expression to obtain the discharge (volumetric flow) in the pipe by using the
definition


A
dA v Q .
Because the velocity distribution in a pipe depends on the radial distance only, we can use an
element of area consisting of a ring of thickness dr and length 2

r, thus, the area is


Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
dA = 2

rdr.
With this element of area, the discharge is calculated, in general, as
d

(
0
R
( ) v r 2 r r
For the specific case of a laminar flow velocity distribution, you will need to set up the
integral:
d

(
(
(
(
(
0
R
vc

_
,

_
,

r
R
2
2 r r
The result is
vc r0
2

2
. The mean velocity is defined as V = Q/A, with A =
r0
2
, then V =
v
c
/2 for laminar flow.
Line integrals
A line integral is an integral calculated along a curve C in space or in the plane with the
integrand defined at each point of C. Suppose that the curve C has the representation
r(s) = x(s) i + y(s) j + z(s) k,
, a s s b
,
so that r(s) is continuous and has a non-zero, continuous first derivative dr/ds for
, a s s b
.
Then C is said to be a smooth curve, i.e., C has a unique tangent at each of its points whose
direction varies continuously as we travel along the curve. Let
( ) f , , x y z
be a function defined
(at least) at each point of C, and let f be a continuous function of s (i.e., f[x(s),y(s),z(s)] =
g(s)). Then, the line integral of f along C from A to B (where s = a, and s = b, respectively)
is calculated as
The curve C is called the path of integration.
For a line integral over a closed path C, the symbol is sometimes used in the literature.
In the exercises presented in next sub-section is it assumed that all the integration paths are
piecewise smooth, i.e., it consists of finitely many smooth curves.
Some properties of line integrals are shown below.
where C is composed of C
1
and C
2
.
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
Evaluation of line integrals
The formula for evaluating a line integral over a curve C when the curve is described by the
vector r(s), was given earlier as:
If the curve is given by a parameter t, so that the curve C is defined by r = r(t), with
, t
0
t t t
1
, then it can be shown that
Example 1
Integrate the function
( ) f , x y 3 ( ) + x
2
y
2
, along the path y = x from (0,0) to (1,1).
First, we sketch the path of integration:
For the path of integration
y x
, we can arbitrarily set
( ) x s s
, and
( ) y s s
, and use
with a = 0, b = 1, i.e., f[x(s),y(s)] = g(s)=3(s
2
+s
2
) = 6s
2
, and the integral reduces to

1
0
2
. 6 ) , ( ds s ds y x f
C
Using SCILAB:
--> integrate(6*s^2,s,0,1)
ans = 2
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
Example 2
Calculate the line integral of
( ) f , x y k ( ) + x
2
y
2
, k = 1, counterclokwise along the circle
+ x
2
y
2
r
2
, with r=5, from (0,r) to (r,0): One possible representation of the circle
+ x
2
y
2
r
2
is the parametric equations,
x r ( ) cos s
, and
y r ( ) sin s
, with
s

2
at (0,r) and
s 0
at
(r,0). Thus, f[x(s),y(s)]=k(r
2
sin
2
s + r
2
cos
2
s) = kr
2
. The line integral is simply,
.
2
1
) , (
2
0
2 /
2
kr ds kr ds y x f
C



Example 3
Using the curve of Example 2 that the with the integrand being
( ) f , x y x
2
( ) sin y
, we replace
x r ( ) cos s
, and
y r ( ) sin s
, in the function f(x,y) to obtain f[x(s),y(s)]=r
2
cos
2
s sin(r sin(s)),
with s from a = /2 to b = 0. The integral, using SCILAB, can be calculated as follows:
--> integrate(r^2*cos(s)^2*sin(r*sin(s)),s,%pi/2,0)
ans = -6.3445402
Example 4
Evaluate the line integral of
( ) f , x y x y
3
, along the line represented by r(t) = t i + 2t j, for
, -1 t t 1
.
The formula to use is
Notice that

ds
dt
dr
dt
= v v
s
, the speed of a particle moving with position vector r(t) if t
represents time. The velocity vector is
v = dr/dt = i + 2j ,
and the speed is
v
s
= ds/dt = |dr/dt| = 5.
We also need to define f[x(t),y(t)], with x(t) = t, and y(t) = 2t, i.e., f[x(t),y(t)] = (t)(2t)
3
= 8t
4
,
thus, the integral reduces to
. 5 8 ) , (
1
1
4
dt t ds y x f
t
t C



Using SCILAB:
-->integrate(8*t^4*sqrt(5),t,-1,1);
ans = 7.1554175
=================================================================================
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
In many applications the integrands of the line integrals are of the form
( ) g , , x y z

dx
ds
,
( ) g , , x y z

dy
ds
, or
( ) g , , x y z

dz
ds
, where s = variable expressing a length on the integration path,
and x=x(s), y=y(s), z=z(s). Then, the line integrals are
,
and similarly for the other two cases.
For sums of these types of integrals along the same path C we use the following notation:
=================================================================================
Example 5
Evaluate the line integral whose integrand is [
+ + x
2
y dx ( ) x z dy x y z dz
] where C is the arc of
the parabola
y x
2
in the plane z = 2 from A[0,0,2] to B[1,1,2].
Here is a plot of the path of integration:
We can calculate the integral by writing all the terms in terms of x, i.e., replacing
y x
2
,
dy 2 x dx
, z = 2,
dz 0
. Therefore,
[
+ + x
2
y dx ( ) x z dy x y z dz
] = [
+ + x
2
x
2
dx ( ) x 2 2 x dx x y z 0
] =
( ) + x
4
2 x ( ) x 2 dx
.
The integral is now,

+
1
0
4
. )) 2 ( 2 ( dx x x x
To calculate the integral using SCILAB:
-->integrate(x^4+2*x*(x-2),x,0,1)
ans = -1.1333333
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
Line integrals independent of path
Definitions:
(1) An expression of the form
+ + f dx g dy h dz
is known as a first-order differential form.
(2) If
, , f

x
u g

y
u h

z
u
, then the differential form shown above is said to be an
exact differential of a function
( ) u , , x y z
, i.e.,
du + + f dx g dy h dz
, or
du + +

_
,

x
u dx

_
,

y
u dy

_
,

z
u dz
.
In general, a line integral of a differential form, i.e., d

(
f x +
gdy
+
h dz
, over a path C will be
dependent not only on the endpoints of the integration (say from point P to point Q), but also
on the path followed to perform that integration, i.e., on the curve C. However, if the
integrand is an exact differential, then the line integral is independent of the path and we can
write:
For example, the expression
+ e ee e
x
( ) cos y dx e ee e
x
( ) sin y dy 2 dz
is an exact differential form, which you can prove by checking the conditions

y
h

z
g
,

z
f

x
h
,

x
g

y
f
.
Therefore,
+ e ee e
x
( ) cos y dx e ee e
x
( ) sin y dy 2 dz
=
du
, is an exact differential. To find the function
( ) u , , x y z
we write:

x
u f
=
e ee e
x
( ) cos y
, and integrate with respect to x, i.e.,
:= u + e ee e
x
( ) cos y ( ) C , y z
Next, take the derivative of this function with respect to y and make it equal to g(x,y), i.e.,
+ e ee e
x
( ) sin y

_
,

y
( ) C , y z e ee e
x
( ) sin y
which indicates that,

y
( ) C , y z 0
Integrating with respect to y, we get:
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
( ) C , y z + ( ) E z K
Thus,
:= u + + e ee e
x
( ) cos y ( ) E z K
No
w, take the derivative of this last expression with respect to z and make it equal to h, i.e.,

z
( ) E z 2
Which indicates that
( ) E z 2 z
Therefore, the function
( ) u , , x y z
is:
:= u + + e ee e
x
( ) cos y 2 z K
If we want to evaluate the line integral of
+ e ee e
x
( ) cos y dx e ee e
x
( ) sin y dy 2 dz
=
du
between points
P(0,0,0) and Q(
, , 2 2
), we only need to evaluate u(x,y,z) at Q and P and take the difference,
i.e.,
--> exp(2)*cos(%pi)+ 2*2 - exp(0)*cos(0) + 2*0
ans = -4.3890561
Work of a force as a line integral
A force field can be represented by a vector function F(x,y,z) = f(x,y,z)i + g(x,y,z)j + h(x,y,z)k.
If the force acts on a particle of mass m along a path defined by a curve C starting at point P
and ending at point Q, the work exerted by the force field on the particle is, by definition,

+ +
Q
P C
dz z y x h dy z y x g dx z y x f d W ], ) , , ( ) , , ( ) , , ( [ r F
where
dr = dx i +dy j + dz k.
If the quantity Fdr is an exact differential, there exists a function U(x,y,z) such that
dU = Fdr = f(x,y,z)dx + g(x,y,z)dy + h(x,y,z)dz,
thus, the integral for calculating the work exerted by force F(x,y,z) between points P and Q is
independent of the path. The function U(x,y,z) represents the potential energy function
associated with the force F(x,y,z), and the force field itself is said to be conservative. The
force field can be obtained out of the potential energy function by using the gradient operator,
i.e.,
. ) , , ( k j i F
x
U
x
U
x
U
z y x

Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz


For example, for a potential energy function U(x,y,z) = x
2
+y
2
+z
2
, the force field is given by
F(x,y,z) = 2(xi+yj +zk). To calculate the work performed by the force F(x,y,z) along a path
given by x(s) = 2 sin s, y(s) = 2 cos s, and z = 2s, from point P(0,2,0), for s=0, to point
Q(3,1, 2/3), for s= /3, we calculate the integrand
Fdr = 2xdx + 2ydy + 2zdz = 2(2 sin s)(2 cos s ds) + 2(2 cos s)(- 2 sin s ds) + 2 (2s) (2 ds),
Fdr = (8 sin s cos s - 8 sin s cos s + 8s)ds = 8s ds.
The work performed by the force is
.
9
4
| 4 8
2
3 /
0
2
3 /
0
3 /
0

s sds dr F W
s
s
Alternatively, since the force field is conservative,
W = U(Q) - U(P) = 3+1+4
2
/9 - 4 = 4
2
/9.
Double integral in a rectangular domain
As the integral of a single variable, say,
a
b
f(x)dx, can be interpreted as the area under the
curve y = f(x) between x = a and x = b, the double integral
can be interpreted as the volume of the solid that extends between the x-y plane and the
surface defined by z = f(x,y) above the rectangular region R = { a < x < b, c < y < d }, as
illustrated in the figure below.
, ) , (

b
a
d
c
dx dy y x f I
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
To approximate the volume representing the double integral we first divide the region R into a
grid of x and y values as shown in the figure below. There will be n sub-intervals in x and m in
y. Consider the volume element limited by x
i
< x < x
i+1
and y
j
< y < y
j+1
. The base of the
volume element is the shaded rectangle shown in the figure.
The height of the element, h
ij
, is taken to be the average of the four values of the function z
ij
=
f(x
i
,y
j
) evaluated in the four corners of the element of area, i.e.,
( ),
4
1
1 , 1 , 1 1 , + + + +
+ + +
j i j i j i ij ij
z z z z h
for i = 0, 1, 2, , n-1, and j = 0, 1, 2, , m-1. Thus, the element of volume corresponding to
the shaded rectangle in the x-y plane is
( ) ,
4
1
1 , 1 , 1 1 , j i j i j i j i ij j i ij ij
y x z z z z y x h V + + +
+ + + +
where x
i
= x
i+1
- x
i
, and y
j
= y
j+1
- y
j
. In practice, the values of x
i
and y
j
are typically taken
as constant values although not necessarily the same. Thus, the double integral is
approximated by
( )


+ + + +

+ + +
n
i
m
i
j i j i j i ij
n
i
m
j
ij
z z z z y x V V
1 1
1 , 1 , 1 1 ,
1 1
.
4
1
User-defined function for calculating double integrals
The function DoubleIntegral, shown below, calculates a double integral over a rectangular
domain using the approximation outlined above. The function call requires the x integration
limits and the number of x sub-intervals, a, b, n, the y integration limits and number of y sub-
intervals, c, d, m, and the function name, f. The function f is a SCILAB external function (e.g.,
a function defined with command deff.)
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
function [I]=DoubleIntegral(a,b,n,c,d,m,f)
//This function calculates the double integral of f(x,y)
//in the rectangular domain a < x < b, c < y < d through a
//generalization of the trapezoidal rule.
Dx = (b - a)/n;
Dy = (d - c)/m;
x = zeros(1,n+1);
y = zeros(1,m+1);
F = zeros(n+1,m+1);
for i = 1:n+1
x(1,i) = a + (i-1)*Dx;
end;
for j = 1:m+1
y(1,j) = c + (j-1)*Dy;
end;
for i = 1:n+1
for j = 1:m+1
F(i,j) = f(x(1,i),y(1,j));
end;
end;
I = F(1,1) + F(1,m+1) + F(n+1,1) + F(n+1,m+1);
for i = 2:n
I = I + 2*(F(i,1) + F(i,m+1));
end;
for j = 2:m
I = I + 2*(F(1,j) + F(n+1,j));
end;
for i = 2:n
for j = 2:m
I = I + 4*F(i,j);
end;
end;
I = I*Dx*Dy/4;
//end of DoubleIntegral function
Applications of function DoubleIntegral
The following commands show applications of the function DoubleIntegral. First, we load the
function
-->getf('DoubleIntegral.txt')
Next, we will calculate the double integral of the function z = f(x,y) = x+y, in the interval
0<x<2, 0<y<2. Before doing that, however, we produce a plot of the function:
-->deff('[z]=f(x,y)','z=x+y') //define z = f(x,y)
-->xx = [0:0.2:2]; yy = [0:0.2:2]; zz = feval(xx,yy,f); //calculate z = f(x,y)
-->plot3d(xx,yy,zz) //produce plot
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
To calculate the double integral using 10 sub-intervals in both x and y use:
-->DoubleIntegral(0,2,10,0,2,10,f)
ans =
8.
Note: Notice that the plot shows a box extending between 0<x<2, 0<y<2, 0<z<4, whose volume
is 224 = 16. Since the plane representing the function z = x+y divides the box into two
halves, the volume representing the double integral will be half of the volume of the box,
namely, 8.
The following calls to function DoubleIntegral for 0 < x < 1 and 0 < y < 1, use 5, 10, and 20 sub-
intervals with z = f(x,y) = x+y:
-->DoubleIntegral(0,1,5,0,1,5,f)
ans = 1.
-->DoubleIntegral(0,1,10,0,1,10,f)
ans = 1.
-->DoubleIntegral(0,1,20,0,1,20,f)
ans = 1.
In the next exercise we use the function z=f(x,y)=sin(x+y). First, we define the function and
produce a plot for 0<x<2 and 0<y<2:
-->deff('[z]=f(x,y)','z=sin(x+y)')
-->xx = [0:%pi/10:2*%pi]; yy = xx; zz = feval(xx,yy,f);
-->plot3d(xx,yy,zz)
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
Due to the symmetry of the function above and below the xy plane, we suspect that the double
integral will add to zero. A call to function DoubleIntegral in the interval under consideration
confirms our suspicion:
-->DoubleIntegral(0,2*%pi,20,0,2*%pi,20,f)
ans = - 2.049E-15
Similar symmetry occurs for the function in the interval 0<x<2 and 0<y<2, the double integral
on those intervals producing again a zero result:
-->DoubleIntegral(0,%pi,20,0,%pi,20,f)
ans = 3.137E-16
The following plot shows the function z = sin(x+y) for the interval 0<x<1 and 0<y<2.
-->xx = [0:0.1:1]; yy = [0:0.1:2]; zz = feval(xx,yy,f);plot3d(xx,yy,zz)
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
The figure does not show the same symmetry of the surface above and below the x-y plane as
in the two previous intervals. The integral of the function on this region produces indeed a
non-zero value:
-->DoubleIntegral(0,1,20,0,2,20,f)
ans = 1.6079717
Double integrals transforming integration region into a rectangle
Suppose we are to calculate the double integral

b
a
x g
x g
dydx y x f I
) (
) (
2
1
) , (
in the region R = {g
1
(x) y g
2
(x), a x b}, illustrated in part [a] of the figure below.
We can use function DoubleIntegral to calculate the double integral indicated above if we
transform the original region of integration R in the x-y plane into a rectangular region of
integration through the coordinate transformation
y = [g
2
(x)-g
1
(x)]z + g
1
(x).
The resulting rectangular region R = { 0 z 1, a x b } is illustrated in part [b] of the
figure above. Function f(x,y) gets transformed into function (x,z) through
(x,y) = f(x, [g
2
(x)-g
1
(x)]z + g
1
(x)) (x,z).
To put together the double integral in the x-z plane we need to replace the value dy with
dy = g
2
(x)-g
1
(x).
Thus, the double integral to be calculated is now
, ) , ( )] ( ) ( [ ) , ( ) , (
1
0
1
0
1 2
) (
) (
2
1


b
a
b
a
b
a
x g
x g
dzdx z x dzdx x g x g z x dydx y x f I
where
(x,y) = (x,z)[g
2
(x)-g
1
(x)] = f(x, [g
2
(x)-g
1
(x)]z + g
1
(x))[g
2
(x)-g
1
(x)]
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
is the new integrand function.
As an example, to calculate the double integral

3
0
3 /
1
3
) exp(
x
dydx y I
we identify g
1
(x) = 1, g
2
(x) = (x/3)
1/2
, and use the transformation y = ((x/3)
1/2
-1)z+1, to obtain a
new function (x,z) = exp([((x/3)
1/2
-1)z+1]
3
). With dy = ((x/3)
1/2
-1), the double integral itself
gets transformed to

+
3
0
1
0
3
) 1 3 / ( ) ] 1 ) 1 3 / exp([( dzdx x z x I .
For the purpose of calculating the integral with function DoubleIntegral we need to define the
function
(x,z) = exp([((x/3)
1/2
-1)z+1]
3
).((x/3)
1/2
-1).
The SCILAB commands to calculate the double integral are
-->deff('[p]=psi(x,z)',...
-->'p=exp(((sqrt(x/3)-1)*z+1)^3)*(sqrt(x/3)-1)')
-->I=DoubleIntegral(0,3,100,0,1,100,psi)
I = - 1.7189023
Simpsons 1/9 rule for double integrals
In this section we present an outline of the application of Simpsons rule to double integrals on
a rectangular domain. The domain is divided as indicated in the figure below.
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
The function to be integrated is discretized to values f
ij
= f(x
i
,y
j
), i=0,1,,n, j=0,1,,m. The
increments in x and y are x and y, respectively. Also, as with the single-variable Simpsons
1/3 rule, the values n and m must be even for the Simpsons 1/9 rule for double integrals.
Applying the Simpsons 1/3 rule to the nine points in the sub-region
R
1,1
= {x
0
x x
2
, y
0
y y
2
}
results in the expression

+ + + + + + + +
2
0
2
0
]. 16 } { 4 [
9
1
) , (
1 , 1 1 , 2 2 , 1 0 , 1 1 , 0 2 , 2 0 , 2 2 , 0 0 , 0
x
x
y
y
f f f f f f f f f y x dydx y x f
In general, the integral on the nine points contained in each of the 9-point sub-regions
R
ij
= {x
i-1
x x
i+1
, y
i-1
y y
i+2
},
for i = 1,2,,n-1, and j = 1,2,,m-1, is calculated as
]. 16 } { 4 [
9
1
, , 1 1 , 1 , , 1 1 , 1 1 , 1 1 , 1 1 0 , 1 i i i i i i i i i i i i i i i i i i ij
f f f f f f f f f y x S + + + + + + + +
+ + + + + +
To calculate the integral through the full region, i.e., in
R = {x
0
x x
n
, y
0
y y
m
},
one needs to calculate the (n-1)(m-1) summations S
ij
for i = 1,2,,n-1, and j = 1,2,,m-1, and
add them all together. Thus, we can write


1
1
1
1
, ) , (
0 0
n
i
m
j
ij
x
x
y
y
S dydx y x f I
n m
where the sums S
ij
were defined above.
The implementation of Simpsons 1/9 rule for double integrals is left as an exercise for the
reader.
SCILAB function for calculating double integrals
SCILAB provides function int2d to calculate the integral of a function z = f(x,y) over a region R
defined by a number of triangles. The simplest call to the function is
[Int,er] = int2d(X,Y,f),
Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
where Int is the value of the integral, er is the error involved in the calculation, X and Y are
matrices of 3 rows and n columns representing the abscissas and ordinates, respectively, of the
n triangles into which region R is divided.
As an example consider the integral of the function f(x,y) = cos(x+y) in the region R = {0 < x <
5, 0 < y < 5}. The figure below shows two possible ways of triangulating the region.
In the first case we will define the matrices X and Y as
,
5 5
5 0
0 0
,
0 5
5 5
0 0
1
1
1
]
1

1
1
1
]
1

Y X
while in the second case they will be defined as
.
5 5
5 0
0 0
,
0 0
5 5
5 0
1
1
1
]
1

1
1
1
]
1

Y X
The following SCILAB commands show the calculation of the double integral for the two
triangulations shown above:
-->deff('z=f(x,y)','z=cos(x+y)')
Triangles ABC-ACD:
-->X = [0,0;5,5;5,0]; Y = [0,0;0,5;5,5];
-->[Int,er] =int2d(X,Y,f)
er = 1.839E-09
Int = .4063959
Triangles ABD-BCD:
-->X = [0,5;5,5;0,0]; Y = [0,0;0,5;5,5];
Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz
-->[Int,er] =int2d(X,Y,f)
er = 1.839E-09
Int = .4063959
If the region of integration is a single triangle, you need to provide only the coordinates of that
triangle to obtain and integral with function int2d. Consider the case of the double integral of
the function z = f(x,y) = x
2
+y
2
in the triangular region shown below.
The SCILAB commands to calculate this integral are as follows:
-->deff('[z]=f(x,y)','z=x^2+y^2')
-->X = [0;2;1]; Y =[0;0;1];
-->[Int,er] =int2d(X,Y,f)
er = 2.961E-16
Int = 1.3333333
Consider now the double integral of the function z=f(x,y) = x + y, in the region defined by R = {
0<y<1-x
2
, 0<x<1}. The figure below shows the region R with a triangulation based on a
partition of the range 0<x<1 into 10 sub-intervals, x = 0, 0.1, 0.2, , 1.0. The following
SCILAB commands are used to generate the graph:
--> x = [0:0.1:1]; yT = 1-x.^2; yB = zeros(x);
--> xset('mark',-9,1)
--> plot2d([x',x',x'],[yT',yT',yB'],[1,-9,-9])
--> xtitle('triangulation of a region','x','y')
--> n = length(x)
n = 11.
--> for j = 1:n
--> xpoly([x(j),x(j)],[yB(j),yT(j)],"lines")
--> end;
--> for j = 1:n-2
--> xpoly([x(j),x(j+1)],[yB(j),yT(j+1)],"lines")
--> end;
Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
The following forend loops generate the matrices X and Y required for the application of
function int2d:
--> X=[];Y=[];
--> for j = 1:n-2
--> X = [X [x(j);x(j);x(j+1)]]; X = [X [x(j);x(j+1);x(j+1)]];
--> Y = [Y [yB(j);yT(j);yT(j+1)]]; Y = [Y [yB(j);yT(j+1);yB(j+1)]];
-->end;
-->X = [X [x(n-1);x(n-1);x(n)]]; Y = [Y [yB(n-1);yT(n-1);yB(n)]];
The following statement defines the function to integrate:
-->deff('[z]=f(x,y)','z=x+y')
The integral is calculated as follows:
-->[Int,er] = int2d(X,Y,f)
er =
1.283E-16
Int =
.5147233
Application of double integrals
Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
As their one-dimensional counterparts, double integrals can be used to calculate properties of
areas and volumes. Some applications of double integrals are presented in this section.
Area properties with double integrals
For the calculation of area properties through the use of double integrals, refer to the
following figure:
Area: since dxdy represent an element of area in the plane, dA = dxdy, then,
A = .
Centroid: Let ( , x
c
y
c
) represents the centroid of the region R, then
x
c
= , y
c
= .
Areal moments of inertia: Let , I
x
I
y
, and I
0
, represent areal moments of inertia with respect
to the x-,y- and z-axes, respectively, then
I
x
= , and I
y
= , and I
0
= + I
x
I
y
= .
Radius of gyration: The radii of gyration of an area with respect to the x- ,y-, and z-axes are
given by
k
x
I
y
A
,
k
y
I
x
A
, and
k
0
I
0
A
,
respectively.
Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz
Mass: if
( ) f , x y
represents the density (mass/unit area) of a body in the shape of the region
of integration, then a differential of mass associated with the differential of area dA = dy dx, is
dM= f(x,y) dA = dM = f(x,y) dy dx, and the mass of the region is
M = .
Center of mass: The center of mass of the region R has coordinates, , x
c
y
c
, given by:
x
c
= , y
c
= .
Moments of inertia of the mass: The moments of inertia I
x
and I
y
of the mass represented
by region R about the x and y axes, respectively, are
I
x
= , and I
y
= ,
and the polar moment of inertia (i.e., moment of inertia with respect to the origin, or, more
correctly, with respect to the z-axis) is
I
0
= + I
x
I
y
= .
Radius of gyration of the mass: The radii of gyration of the mass represented by region R
with respect to the x- ,y-, and z-axes are given by
k
x
I
y
M
,
k
y
I
x
M
, and
k
0
I
0
M
,
respectively.
Volume: the volume between the x-y plane and the surface z = f(x,y) is provided by the
double integral,
V = .
Calculation of areal properties in Cartesian coordinates
For the quarter of circle defined by the region R ={ x
2
+y
2
= a
2
, 0<x<a}, for a = 2, find the area,
the centroid, and the areal moments of inertia with respect to the x-, y-, and Z-axes in a
Cartesian coordinate system (x,y,Z). [Note: Z is used here instead of z to avoid confusing the Z
coordinate of the Cartesian system with the z variable used in a transformation that converts
the original region into a rectangular region as shown below].
The region of integration can be described as R = { 0 < y < (a
2
-x
2
)
1/2
, 0 < x < a}. To calculate
the double integrals using the user-defined function DoubleIntegral we need to transform the
Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
region R in the x-y plane into a rectangular region R = {0<z<1, 0<x<a} in the z-x plane, by using
the transformation
y = z(a
2
-x
2
)
1/2
,
dy = (a
2
-x
2
)
1/2
dz.
The function to be integrated f(x,y) in the x-y plane gets transformed into
(x,z) = f(x,z(a
2
-x
2
)
1/2
),
thus, we will calculate


a x a a
dzdx x a x a z x dydx y x f
0 0 0
1
0
2 2 2 2
2 2
. ) , ( ) , (
The area is calculated by taking f(x,y) = 1.0 as follows:


a x a a
dzdx x a dydx A
0 0 0
1
0
2 2
2 2
.
Using SCILAB and the user-defined function DoubleIntegral we find the following approximation
to the area:
-->deff('[w]=g(x,y)','w=sqrt(4-x^2)')
-->A = DoubleIntegral(0,2,1000,0,1,50,g)
A = 3.1415555
The coordinates of the centroid are calculated using x
c
= M
y
/A, and y
c
= M
x
/A, with
,
0
1
0
2 2
0 0
2 2
dzdx x a x dydx x xdA M
a
R
a x a
y

and

dzdx x a x a z dydx y ydA M


a
R
a x a
x
0
1
0
2 2 2 2
0 0
) )( (
2 2
. ) (
0
1
0
2 2
dzdx x a z
a

Using SCILAB we calculate the centroidal coordinates as


-->deff('[w]=my(x,z)','w=x*sqrt(4-x^2)')
-->deff('[w]=mx(x,z)','w=z*(4-x^2)')
-->My = DoubleIntegral(0,2,1000,0,1,50,my)
My = 2.6665916
-->Mx = DoubleIntegral(0,2,1000,0,1,50,mx)
Mx = 2.666666
Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz
-->xc=My/A, yc=Mx/A
xc = .8488125
yc = .8488362
The moments of inertia of the area are
,
0
1
0
2 2 2
0 0
2 2
2 2
dzdx x a x dydx x dA x I
a
R
a x a
y

and

dzdx x a x a z dydx y dA y I
a
R
a x a
x
0
1
0
2 2 2 2 2
0 0
2 2
) ( ) (
2 2
. ) (
0
1
0
2 / 3 2 2 2
dzdx x a z
a

Using SCILAB:
-->deff('[w]=ix(x,z)','w=x^2*sqrt(4-x^2)')
-->deff('[w]=iy(x,z)','w=z^2*(4-x^2)^(3/2)')
-->Ix=DoubleIntegral(0,2,1000,0,1,50,ix)
Ix = 3.1414439
-->Iy=DoubleIntegral(0,2,1000,0,1,50,iy)
Iy = 3.142221
The radii of gyration are k
x
= (I
x
/A)
1/2
and k
y
= (I
y
/A)
1/2
, i.e.,
-->kx=sqrt(Ix/A), ky=sqrt(Iy/A)
kx = .9999823
ky = 1.0001059
Change of variables in double integrals
Let R is a region in the x-y plane. Suppose that we want to evaluate the double integral,
,
by changing variables through the coordinate transformation x = x(u,v), y = y(u,v). The
transformation of coordinates maps the region R in the x-y plane into a region R* in the u-v
plane. The double integral in terms of the variables u and v incorporates a quantity known as
the Jacobian of the transformation. We define the Jacobian of the coordinate transformation
as the determinant
Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz
.
With this definition, then we can write
.
The latter equation suggests that the differential of area in the u-v plane is:
.
Jacobian functions can be obtained for any coordinate transformations in systems of three or
more coordinates. Thus, a generalized definition of the Jacobian corresponding to the
transformation (x
1
, x
2
, , x
n
) ! (

1
,

2
, ,

n
), is
Area calculation using double integrals with polar coordinates
Suppose we want to find the area of a circle of radius R using polar coordinates. The
transformations are:
:= x ( ) , r r ( ) cos
:= y ( ) , r r ( ) sin
The Jacobian of the transformation (x,y) to (r,

) is:

1
]
1
1
( ) cos r ( ) sin
( ) sin r ( ) cos
:= J r
Therefore, the differential of area dA
, r
in the r-

plane is dA
, r
= |J|drd

= r drd

. The
shaded element in the figure below is a typical area differential in polar coordinates. Although
it is not shaped as a rectangle, because we are dealing with small quantities, we can
approximate it to a rectangle of dimensions rd

and dr, from which the value dA


, r
= r drd

,
follows.
Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz
Since we are trying to obtain the area of a region in x-y (a circle of radius R), the function to
integrate is
( ) f , x y 1.0
, which transforms to f[
, ( ) x , r ( ) y , r
] = 1.0. The region in the x-y
plane is the circle itself, R = {
+ x
2
y
2
R
2
}, which in polar coordinates is mapped as R* =
{
, 0 r r R
, and
, 0 2
}. Therefore, the integral d

(
d

(
1.0 y x over R, becomes:
d

(
0
2
d

(
0
R
J r
=
R
2

Double integral applications in polar coordinates


Some double integral applications using polar coordinates are shown below:
Area: since dxdy represent an element of area in the plane, dA = dxdy, then,
A = .
Centroid: Let ( , r
c

c
) represents the centroid of the region R*, then
r
c
= ,
c
= .
Polar areal moment of inertia: The moment of inertia I
0
, represents the so-called polar of
inertia, i.e., with respect to the z-axis, is
I
0
= .
Radius of gyration: The radii of gyration of an area with respect to the z-axis is given by
k
0
I
0
A
.
Mass: if
( ) f , r
represents the density (mass/unit area) of a body in the shape of the region
of integration, then a differential of mass associated with the differential of area dA
, r
= rdr
d

, is dM=
( ) f , r
dA
, r
= f(
, r
) rdr d

, , and the mass of the region is


Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz
M = .
Center of mass: The center of mass of the region R has coordinates, , r
c

c
, given by:
r
c
= ,
c
= .
Moments of inertia of the mass: The polar moment of inertia (i.e., moment of inertia with
respect to the origin) is
I
0
= .
Radius of gyration of the mass: The radii of gyration of the mass represented by region R
with respect to the z-axes is given by
k
0
I
0
M
.
Volume: the volume between the x-y plane and the surface z = f(
, r
) is provided by the
double integral,
V = .
Calculation of areal properties in polar coordinates
Calculate the area, centroid, polar moment of inertia, and radius of gyration of the region
defined by R* = {0<r<sin(
2
), 0<

<

2
}.
A plot of the region follows:
The area is calculated as

2 /
0
) 2 sin(
0

rdrd A .
Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz
The region of integration in the r- plane (not the x-y plane using polar coordinates, but a
plane where the abscissas are represented by r and the ordinates by ) will be an irregularly
shaped region R which can be transformed into a rectangular region R in a r-z plane. The
transformation to use is r = z sin(2), dr = sin(2)dz. The area is calculated now as


2 /
0
1
0
2
2 /
0
1
0
) 2 ( sin ) 2 sin( ) 2 sin(

dzd z dzd z A .
Using SCILAB:
-->deff('[w]=g(z,th)','w=z*(sin(2*th))^2')
-->A = DoubleIntegral(0,1,100,0,%pi/2,100,g)
A = .3926991
The coordinates of the centroid are r
c
= (1/A)
A
r
2
drd,
c
= (1/A)
A
rdrd. Using the
transformation r = z sin(2), dr = sin(2)dz, the integrals are calculated as
. ) 2 ( sin
1
, ) 2 ( sin
1
2 /
0
1
0
2
2 /
0
1
0
3 2



dzd z
A
dzd z
A
r
c c
Using SCILAB:
-->deff('[w]=mr(z,th)','w=z^2*(sin(2*th))^3')
-->deff('[w]=mt(z,th)','w=z*th*(sin(2*th))^2')
-->rc = DoubleIntegral(0,1,100,0,%pi/2,100,mr)/A
rc = .5659125
-->thc = DoubleIntegral(0,1,100,0,%pi/2,100,mt)/A
thc = .7853982
The polar moment of inertia is calculated with I
c
=
A
r
3
drd. With the coordinate
transformation r = z sin(2), dr = sin(2)dz, the integral to calculate is
. ) 2 ( sin
1
2 /
0
1
0
4 3
0

dzd z
A
I
Using SCILAB
-->deff('[w]=i0(z,th)','w=z^3*(sin(2*th))^4')
-->I0 = DoubleIntegral(0,1,100,0,%pi/2,100,i0)
I0 = .1472769
The radius of gyration about the origin is calculated as k
0
= (I
o
/A)
1/2
, i.e.,
-->k0=sqrt(I0/A)
k0 = .6124031
Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz
A final note on the numerical calculation of double
integrals
All the examples of applications of numerical calculation of double integrals presented here
have been calculated using the user-defined function DoubleIntegral with coordinate
transformations that convert the original integration region into a rectangular region. It
should be indicated that the approach followed in function DoubleIntegral is based on
approximating the volume represented by the integral
A
f(x,y) dxdy by the sum of
parallelepiped elements whose base has an area xy, and whose height is f(x
i
,y
j
). Function
DoubleIntegral uses a fixed and uniform grid to approximate the integral. The error incurred
in the approximation is of first order, i.e., O(xy). Thus, you may have to use a very fine
grid to obtain reasonable results with this function.
Improved results for double integrals can be obtained by coding Simpsons 1/9 rule as indicated
in a previous section, or by using SCILAB function int2d. Function int2d uses an adaptive
algorithm by which regions in which the function varies rapidly are divided into finer grids and
the integral recalculated until a convergence is achieved. Adaptive methods can also be used
to calculate integrals of one variable. Such algorithms are incorporated in many of SCILABs
predefined integral functions such as intg or integrate.
Exercises
For problems [1] through [10], use the user-defined function Sumint to produce the
approximation to the integral of f(x) between the limits a<x<b using n subintervals. Calculate
the integrals using (a) the left sum, (b) the middle sum, and (c) the right sum.
[1] f(x) = 1/(1+ ln(x)), a = 1, b = 5, n = 30
[2] f(x) = 1 + x
2
+ x
3
+ 1/x, a = 1, b= 3.5, n = 90
[3] f(x) = exp(-x/5)/(1+x
2
), a = -2, b = 2, n = 50
[4] f(x) = (x
2
+1)
1/2
, a = -1 , b = 1, n = 20
[5] f(x) = (x
2
+1)
1/3
+2.5(x
2
+1)+1.5 , a = 0, b = 5, n = 60
[6] f(x) = sinh(x)/(1+x
3
) , a = 0 , b = 3, n = 40
[7] f(x) = exp(-x
2
/2)/(2)
1/2
, a = -4, b = 4 , n = 100
[8] f(x) = 2.5(1+x
2
+x
3
/3) cos(3x
2
+2), a = 0, b = 2, n = 25
[9] f(x) = 4.2x
1/3
+ 3.245x
1/2
+ 1.4142, a = 0.25, b = 1.25 , n = 20
[10] f(x) = 0.33 ln((x+1)/(x
2
+5x+2)) , a = 0, b = 20, n = 40
[11] - [20] Repeat problems [1] through [10] using (a) the trapezoidal rule through SCILAB
function inttrap; (b) Simpsons 1/3 rule through function simpson13 if the number of
Download at InfoClearinghouse.com 68 2001 Gilberto E. Urroz
subintervals is even, and/or Simpsons 3/8 rule through function simpson38 if the number of
subintervals is a multiple of 3.
[21] - [30] Repeat problems [1] through [10] using the Newton-Cotes formulas with (a) n=3, (b)
n=4, (c) n=5, (d) n=6, (e) n=7.
[31] - [40] Repeat problems [1] through [10] using (a) SCILAB function integrate; (b) integration
by splines using SCILAB function intslpin; (c) SCILAB function intg.
In problems [41] through [50], given a function of a complex variable w = F(z), with z = x + iy,
identify the real and imaginary components (x,y) and (x,y) such that F(z) = (x,y) + i
(x,y). Also, produce contour plots of the functions (x,y) and (x,y).
[41] F(z) = 1/z
[42] F(z) = z + 1/z
[43] F(z) = exp(z)
[44] F(z) = z
3
[45] F(z) = z
1/2
[46]-[50]. For problems [46] through [50], integrate the function of a complex variable from
problems [41] through [45] using SCILAB function intl along the straight line joining the points
z
1
and z
2
, where (a) z
1
= 3, z
2
= 2-i; (b) z
1
= -5+2i, z
2
= 2-2i; (c) z
1
= 3i, z
2
= 5+2i; (d) z
1
=
(1+i)/2, z
2
= 3+2i; (e) z
1
= i/2, z
2
= -i/2.
[51]-[55]. For problems [51] through [55] use function intl to calculate the integral of the
function of a complex variable from problems [41] through [45] along the curve defined by z =
z
0
+ re
i
, within the limits
1
and
2
, using the following values: (a) z
0
= 2+3i, r = 1,
1
= 0,
2
=
/4; (b) z
0
= 1+i, r = 0.2,
1
= - /2,
2
= /2; (c) z
0
= 0, r = 1,
1
= ,
2
= 2; (d) z
0
= -1-2i, r =
0.75,
1
= 0,
2
= 3 /4; (e) z
0
= 3, r = 1,
1
= - /2,
2
= .
For problems [56]-[60] use the user-defined function DoubleIntegral to calculate the double
integral of function f(x,y) in the rectangular domain a < x < b, c < y < d.
[56]. f(x,y) = x
2
+y
2
, a = -2, b = 2, c = -1, d = 1.
[57]. f(x,y) = x sin y + y sin x, a = -/2, b = /2 , c = 0, d = .
[58]. f(x,y) = 2xy(x
2
+y
2
+1)
1/2
, a = 0, b = 2, c = 0, d = 2.
[59]. f(x,y) = x ln(y/x) + y ln(x/y), a = 1, b = 2, c = 0.5, d = 0.75.
[60]. f(x,y) = 1-exp(-(x
2
+y
2
)/25), a = -2, b = 2, c = -2, d = 2.
[61]-[65]. Repeat problems [56] through [60] using SCILAB function int2d by dividing the
rectangular region of integration into two triangles.
[66]-[70]. For problems [66] through [70] use function int2d to calculate the double integral of
the functions in problems [56] through [60] on the integration region defined by: (a) R = {-1 < x
< 1, 0 < y < (1-x
2
)}; (b) R = { 0<x< 1, x
2
< y < x}; (c) R = {-1 < x < 1, 0 < y < 1 - x
2
}; (d) R = {-3 < x
< 3, |x| < y < 3}; (e) R = {-4 < x < 4, (25-x
2
)
1/2
< y < (16-x
2
)
1/2
}.
Download at InfoClearinghouse.com 69 2001 Gilberto E. Urroz
[71].Determine the area under the curve y = f(x) limited by the values x = a and x = b:
(a) f(x) = x
2
, a=2, b=5 (b) f(x) = ln(x), a = 2, b = 3
(c) f(x) = 1/(1+x
2
), a = 0, b =1 (d) f(x) = (1-x
2
)
1/2
, a = -0.5, b = 0.5
[2]. Determine the area between the curves y = f
1
(x) and y = f
2
(x) and the values x =a and y =
b. Plot the region to be integrated:
(a) f
1
(x) =x
1/2
, f
2
(x) = x
2
, a = 0, b = 1 (b) f
1
(x) = (1-x
2
)
1/2
, f
2
(x) = -(1-x
2
)
1/2
, a = -1, b = 1
(c) f
1
(x) = 10(1-x/5), f
2
(x) = 2, a =0, b =3 (d) f
1
(x) = (1-x
2
)
1/2
, f
2
(x) = x, a = 0, b = point of
intersection
[73]. Determine the center of mass (centroid) of the figure limited by the x-axis and the curve
y = f(x) between the values x = a and x = b for the functions of problem [71].
[74]. Determine the volume of the solid of revolution generated by the rotation of the curve y
= f(x) between the values x = a and x = b about the x axis for the functions of problem [71].
[75]. Determine the moments of inertia and radii of gyration (k
x
= (I
x
/A)
1/2
,
k
y
= (I
y
/A)
1/2
) with
respect to the x-axis, y-axis, and the origin of the area limited by the x-axis and the curve y =
f(x) between the values x = a and x = b, for the functions of problem [71].
[76]. For the figures in problem [75] determine the moments of inertia and radii of gyration
with respect the centroidal axes and the centroid. Use the parallel axes moment.
[77]. Determine the area defined by r = f() and the radii r =
1
and r =
2
. Plot the region
using polar coordinates:
(a) f() =3 cos ,
1
= 0,
2
= /2 (b) f() = 1-sin ,
1
= -/2,
2
= /2
(c) f() = 1/(1+cos ),
1
= 0,
2
= /2 (d) f() = ,
1
= 0,
2
=
[78]. Calculation of discharge in a pipe. Calculate the discharge in a pipe of radius R = 2.5 ft,
if the velocity distribution in the pipe cross-section is given by:
(a) v(r) = v
0
(1-r/R) (b) v(r) = v
0
[1-(r/R)
3
] (c) v(r) = v
0
[1-(r/R)
1/2
]
The value of v
o
= 3.5 fps.
[79]. Integrate the function f(x,y,z) along curve C using the suggested variable substitutions:
(a) f(x,y,z) = 1-x
2
-y
2
, x(s) = sin(s), y(s) = cos(s), z(s) = 0, s = 0..
(b) f(x,y,z) = xyz, x(s) = 2 sin(s), y(s) = 3 cos(s/2), z(s) = s, s = 0..
(c) f(x,y,z) = sin x cos y, x(s) = s, y(s) = s
2
/2, z(s) = 0, s = 0..2
(d) f(x,y,z) = 1/(x
2
+y
2
+1), x(s) = s
2
, y(s) = ln s, z(s) = 0, s = 1..10
[80]. Calculate the line integral for the differential f(x,y,z)dx + g(x,y,z)dy + h(x,y,z)dz along
the curve C specified:
(a) f(x,y,z)=x+y , g(x,y,z) =xy , h(x,y,z) = z
2
, C:{ y = x+2, z = x-1, 0<x<5 }
(b) f(x,y,z)=x+yz , g(x,y,z) =y+xz , h(x,y,z) =z+xy , C:{x
2
+y
2
=1,0<x<1, y>0, z = 2}
(c) f(x,y,z)=sin x , g(x,y,z) =sin y , h(x,y,z) =sin z , C:{z = x+1, y=2, 0<x<2}
(d) f(x,y,z)=x exp(y) , g(x,y,z) = exp(z), h(x,y,z) =y
2
, C: {x = 2, y = z
2
+1, 0< z<1}
[81]. Check if the differential f(x,y,z)dx + g(x,y,z)dy + h(x,y,z)dz is an exact differential, and
calculate the line integral between the selected points A and B:
(a) f(x,y,z)=2x , g(x,y,z) =2y , h(x,y,z) = 2z, A: (0,0,0), B(1,-1,2)
(b) f(x,y,z)=z cos(x)exp(y) , g(x,y,z) = z
2
sin(x) exp(y), h(x,y,z) = 2 z sin(x) exp(y),
A:(1,1,1), B:(2,2,2)
Download at InfoClearinghouse.com 70 2001 Gilberto E. Urroz
(c) f(x,y,z)= g(x,y,z) = h(x,y,z) =2(x+y+z) , A:(2,-2,1), B:(5,5,-2)
(d) f(x,y,z)=y cos(xy), g(x,y,z) = x cos(xy), h(x,y,z) =0 , A:(-1,1,0), B:(2,2,2)
[82]. Calculate the following double integrals using function DoubleIntegral:

+
2
0
) ( ) (
x
x
dydx xy x a


+
2
2
0
) ( ) (
y
dxdy xy x b

2
0
) (
x
x
dxdy x c

2
0
1
2
2
) (

d d d
[83]. Using double integrals, calculate the area, coordinates of the centroid, areal moments of
inertia with respect to the x- and y-axes and with respect to the origin of the area described by
the region R. Plot the region of interest:
(a) R: {0<y<x
2
+1, 0<x<1} (b) R: {-1 < y < (1-x
2
), 0 < x < 1}
(c) R: {0<y<1-x, 0 < x < 1} (d) R: {0 < x < (1-y
2
)
1/2
, -1 < y < 1}
[84]. Calculate the mass, coordinates of the center of mass, mass moments of inertia with
respect to the x- and y-axes and with respect to the origin of the area described for the regions
R from problem [83] if the corresponding areal densities are given by f(x,y):
(a) f(x,y) = x (b) f(x,y) = (x+1)
1/2
(c) f(x,y) = sin(x) (d) f(x,y) = 1/(x
2
+y
2
)
[85]. Determine the volume of the three-dimensional region described by the surface z = f(x,y)
and limited by the region R:
(a) f(x,y) = x
2
+y, R:{x = -2..2, y = -2..x
1/2
}
(b) f(x,y) = sin(x)cos(y), R:{x = -2..2, y = -2..sin(x)}
(c) f(x,y) = exp(-0.05*x)*cos(y+2), R:{ x = 0..5, y = 0..25}
(d) f(x,y) = |ln(x
2
+y
2
+10)|, R:{ x = -2..2, y = -2..2}
[86]. Calculate the area, coordinates of the centroid, areal moments of inertia with respect to
the x- and y-axes and with respect to the origin of the area described by the region R (polar
coordinates):
(a) R:{ 0 < r < 3, 0 < < } (b) R:{ 0 < r < sin , 0 < < /2}
(c) R:{ 0 < r < (cos 1), 0 < < } (d) R:{ 0 < r < 1/(1+cos ), -/2 < < /2}
[87]. Calculate the mass, coordinates of the center of mass, mass moments of inertia with
respect to the x- and y-axes and with respect to the origin of the area described by the regions
R in problem [11], if the corresponding areal densities are f(r,):
(a) f(r,)=2.5 (b) f(r,) = sin() (c) f(r,) = 1-sin() (d) f(r,) = ||
[88]. Determine the volume of the three-dimensional region described by the surface z =
f(r,), as shown below, and limited by the region R (polar coordinates) from problem [86]:
(a) f(r,)=2.5 exp(-) (b) f(r,) = sin
2
() (c) f(r,) = 1+ (d) f(r,) = (1+sin())
1/2
[89]. The outline for the calculation of double integrals using Simpsons 1/9 rule was
presented earlier. Write a SCILAB function that takes as input the limits of a rectangular
region in the x-y plane, R = {x
0
< x < x
n
, y
0
< y <y
m
}, the (even) values n and m, and the function
f(x,y), and returns the approximation to the double integral
. ) , (
0 0

n m
x
x
y
y
dydx y x f I
Download at InfoClearinghouse.com 71 2001 Gilberto E. Urroz
[90]. Use the function for calculating double integrals through Simpsons 1/9 rule developed in
problem [89] to solve problem [82].
[91]. Use the function for calculating double integrals through Simpsons 1/9 rule developed in
problem [89] to solve problem [83].
[92]. Use the function for calculating double integrals through Simpsons 1/9 rule developed in
problem [89] to solve problem [84].
[93]. Use the function for calculating double integrals through Simpsons 1/9 rule developed in
problem [89] to solve problem [85].
[94]. The table below shows the flow velocity, v(fps), and suspended sediment concentration,
C(mg/l), as functions of the distance from the channel bed, y(ft), measured at a cross-section
of a 800-ft-wide (b = 800 ft), 7.8-ft-deep (h = 7.8 ft) river cross-section that can be
approximated by a rectangle.
y(ft) v(ft/s) C(mg/l)
0.7 4.30 411
0.9 4.50 380
1.2 4.64 305
1.4 4.77 299
1.7 4.83 277
2.2 5.12 238
2.7 5.30 217
2.9 5.40 211
3.2 5.42 196
3.4 5.42 188
3.7 5.50 184
4.2 5.60 165
4.8 5.60 148
5.8 5.70 130
6.8 5.95 80
The flow discharge, Q(cfs), is defined by the integral


A
h
bdy y v dA y v Q
0
, ) ( ) (
with the mean flow velocity calculated as V = Q/A, where A = bh being the cross-sectional
area.
The total flux of suspended sediment through the cross-section is defined as


A
h
s
bdy y C y v dA y C y v Q
0
, ) ( ) ( ) ( ) (
Download at InfoClearinghouse.com 72 2001 Gilberto E. Urroz
with the flux-averaged concentration given by C
f
= Q
s
/Q.
The terms used in the definitions above are illustrated in the figure below:
(a) Plot the velocity profile v(y)-vs-y and the concentration profile C(y)-vs-y.
(b) Determine the flow discharge, mean velocity, sediment flux, and flux averaged
concentration for the data given using integration by splines.
Download at InfoClearinghouse.com 73 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 74 2001 Gilberto E. Urroz
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Data Fitting and Interpolation with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
DATA FITTING AND INTERPOLATION 2
Simple linear interpolation 2
Polynomials in SCILAB 4
Defining a polynomial variable 4
Identifying the polynomial variable, degree, and coefficients 4
Some operations with polynomials 5
Polynomial division 5
Polynomial long division 6
Polynomial fractions 7
Matrices with polynomial elements 8
Determinant of a polynomial matrix 8
Inverse of a polynomial matrix 8
Evaluating a polynomial or polynomial matrix 9
Characteristic matrix and characteristic equations 10
Polynomial functions applied to polynomial matrices 10
Derivatives of polynomials 11
Generating a polynomial given its coefficients 12
Generating a polynomial given its roots 13
Obtaining the roots of a polynomial 13
Indefinite integrals for polynomials 14
Numerical Methods with Polynomials 15
Polynomial deflation 15
Direct fitting of a polynomial 16
Lagrange polynomials 17
SCILAB function mtlb_diff 20
Difference Tables 22
A function to produce a forward-difference table 25
Newton Forward-Difference Polynomial 26
Newton Backward-Difference Polynomial 28
Stirling centered-difference polynomial 29
Bessel centered-difference polynomial 32
Least Squares Polynomial Approximation 34
Linear Data Fitting 36
Other Linearized Data Fittings 37
Interpolation with splines 39
Cubic splines 39
SCILAB functions for cubic splines 40
The function smooth 42
Obtaining the spline polynomials 44
Multi-variate data fitting 49
Successive univariate polynomial interpolation 49
Direct multivariate polynomial fitting 52
Least-square multivariate polynomial fitting 54
Bivariate quadratic fitting 56
SCILAB function datafit 58
Exercises 65
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Data fitting and interpolation
In this chapter we present SCILAB polynomials and their applications, as well as presenting a
number of numerical methods for fitting data to polynomial and other non-linear functions.
Simple linear interpolation
Simple linear interpolation is typically applied to a table of values { (x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
) }
in order to obtain the value y
k
corresponding to a value x
k
located within the range of values of
x. It is assumed that the values of x are shown in increasing order in the table. Suppose that
the value of x
k
is such that x
i
< x
k
< x
i+1
for a certain value of i. The situation is illustrated in
the table below:
x y
x
1
y
2
: :
x
i
y
i
x
k
y
k
x
i+1
y
i+2
: :
x
n
x
n
Assuming a linear variation for y as function of x in the range [x
i
, x
i+1
] we can write the
following relationship involving values (x
i
,y
i
), (x
k
,y
k
), and (x
i+1
,y
i+1
):
,
1
1
i i
i i
i k
i k
x x
y y
x x
y y

+
+
from which,
). (
1
1
i k
i i
i i
i k
x x
x x
y y
y y

+
+
+
SCILAB provides function interpln to obtain linear interpolations out of a matrix of two rows
representing pairs of data values (x
i
,y
i
). The general call to this function is
[y]=interpln(xy_table,x)
where xy_table is a two-row matrix with the first row representing values of x and the second
row representing values of y from the table, and x is a vector of values. The function returns a
vector of values y corresponding to the linear interpolation of the table for the values in vector
x.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
An example of application for function interpln follows. First, we define the table of values
for interpolation:
-->TableXY = [[1,2,3,4,5,6];[2,5,6,8,10,11]]
TableXY =
! 1. 2. 3. 4. 5. 6. !
! 2. 5. 6. 8. 10. 11. !
The following call to function interpln is used to obtain the value of y for x = 2.5:
-->interpln(TableXY,2.5)
ans =
5.5
Next, we use function interpln to interpolate a vector of values:
-->xx = [1.5, 2.5, 3.5, 4.5, 5.5]
xx =
! 1.5 2.5 3.5 4.5 5.5 !
-->yy = interpln(TableXY,xx)
yy =
! 3.5 5.5 7. 9. 10.5 !
To produce a plot of the data from the table and the interpolated data we use the following
SCILAB commands:
-->ymax1 = max(TableXY(2,:)) //Max & min values of y
ymax1 =
11.
-->ymax2 = max(yy)
ymax2 =
10.5
-->rect = [0,0,6,11]; // define the plot rectangle
-->plot2d(TableXY(1,:),TableXY(2,:),9,011,y,rect) //Plot table
-->xset('mark',-9,1)
-->plot2d(xx,yy,-9,'010','y',rect) //Plot interpolated data
-->xtitle('linear interpolation','x','y') //Place title
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
Polynomials in SCILAB
As indicated in previous chapters, SCILAB recognizes polynomials as a particular data type and
provides the function poly with the purpose of defining such data types. The simplest
polynomial that can be defined is a variable name, say, x, y, z, etc. By defining a variable
name as a polynomial type, simple algebraic manipulations, resulting in polynomial forms, can
be accomplished.
Defining a polynomial variable
In the following example, the variable s is defined as a polynomial and used in some simple
operations:
-->s=poly(0,'s') //Defining variable s as a polynomial
s =
s
-->s+2 //Simple algebraic operation
ans =
2 + s
-->y = (s+2)^2 //Defining a new polynomial (y)
y =
2
4 + 4s + s
Identifying the polynomial variable, degree, and coefficients
SCILAB function varn can be used to identify the variable in a polynomial, for example:
-->varn(y)
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
ans =
s
SCILAB function degree is used to determine the degree of a polynomial, for example:
-->degree(y)
ans =
2.
Function coeff can be used to obtain a vector with the coefficients of the polynomial listed in
increasing order of powers of the polynomial variable. For example,
-->x=poly(0,'x') //Define polynomial variable
x =
x
-->p = 3.5*x^2+2*x-5 //Create a polynomial
p =
2
- 5 + 2x + 3.5x
-->c = coeff(p) //Determine the coefficients of the polynomial
c = ! - 5. 2. 3.5 !
Some operations with polynomials
The next command defines a polynomial q in terms of polynomial variable s:
-->p = (s+1)^2
p =
2
1 + 2s + s
Polynomials in the same variable can be added:
-->p+y
ans =
2
5 + 6s + 2s
The next statement defines a polynomial q out of a multiplication of monomial expressions:
-->q = (s+1)*(s+3)
q =
2
3 + 4s + s
Polynomial division
The following example uses polynomial division resulting in the simplification of the result:
-->p/(s+1)
ans =
1 + s
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
-----
1
Function pdiv can be used to obtain the quotient and residual of a polynomial division, for
example:
-->[res,quo] = pdiv(p,q)
quo = 1
res = - 2 - 2s
The quotient quo and the residual res are such that p/q = quo + res/q. For the case under
consideration, in which p = s
2
+2s+1 and q = s
2
+4s +3, we can write:
.
3 4
2 2
1
3 4
1 2
2 2
2
+ +

+
+ +
+ +
s s
s
s s
s s
Polynomial long division
A polynomial division, such as the one just performed, stops whenever the degree of the
residual is smaller than that of the divisor. (For the example shown above, the degree of the
residual is 1 while that of the divisor is 2.) Through the operation of long division we can
continue generating terms for the division allowing for negative powers of the polynomial
variable. SCILAB provides function ldiv to perform the polynomial long division. Since, in
general, one can obtain an infinite number of negative powers of the polynomial variable while
performing the long division, function ldiv requires that the user provides the number of
coefficients to be generated. For example, consider the following polynomial long division in
which 7 coefficients are generated:
-->ldiv(s^3+5,s+1,7)
ans =
! 1. !
! - 1. !
! 1. !
! 4. !
! - 4. !
! 4. !
! - 4. !
This result is to be interpreted in the following manner:
! + + + +
+
+
4 3 2
2
3
4 4 4 4
1
1
5
s s s s
s s
s
s
Thus, the degree of the polynomial variable, s, in the first term of the quotient is equal to the
difference between the degree of the numerator and that of the denominator, i.e., 3-1 = 2.
The degree of subsequent terms decreases by one. [Note: An expression, such as the one
shown above, would be referred to as a Laurent series if the variable s represents the complex
variable x+iy, where i is the unit imaginary number.]
The following example uses the polynomials p and q defined earlier:
-->ldiv(p,q,5)
ans =
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
! 1. !
! - 2. !
! 6. !
! - 18. !
! 54. !
Since both p and q have the same degree (2), the first term in the expression resulting from
the long division of p by q is multiplied by s
0
= 1. Subsequent terms will have increasing
negative powers of s:
.
54 18 6 2
1
3 4
1 2
4 3 2 2
2
! + + +
+ +
+ +
s s s s s s
s s
Polynomial fractions
SCILAB allows the construction of polynomial fractions, i.e., one in which both the numerator
and denominator are polynomials. For example, a SCILAB polynomial fraction is:
-->t = (s+1)*(s+2)/((s-2)*(s+3))
t =
2
2 + 3s + s
----------
2
- 6 + s + s
Functions numer and denom are used to extract the numerator and denominator of a
polynomial fraction, e.g.,
-->numer(t)
ans =
2
2 + 3s + s
-->denom(t)
ans =
2
- 6 + s + s
The function simp is used to simplify the ratio of two polynomials. For the following call to
function simp we seek two polynomials p1 and q1 so that p1/q1 = p/q, and p1 and q1 have no
common factors:
-->[p1,q1] = simp(p,q)
q1 =
3 + s
p1 =
1 + s
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Matrices with polynomial elements
Polynomials can also be used as elements of a matrix, for example:
-->A = [ s+1 s-2; (s+1)^2 s^2/2]
A =
! 1 + s - 2 + s !
! !
! 2 2 !
! 1 + 2s + s .5s !
Determinant of a polynomial matrix
The determinant of matrix A can be calculated using either of functions determ, det, or detr:
-->determ(A)
ans =
2 3
2 + 3s + .5s - .5s
-->det(A)
ans =
2 3
2 + 3s + .5s - .5s
-->detr(A)
ans =
2 3
2 + 3s + .5s - .5s
Inverse of a polynomial matrix
The function inv can be used to obtain the inverse of a polynomial matrix:
-->inv(A)
ans =
! 2 !
! .5s 2 - s !
! -------------------- ------------------- !
! 2 3 2 3 !
! 2 + 3s + .5s - .5s 2 + 3s + .5s - .5s !
! !
! 2 + 2s - 2 !
! ---------- --------- !
! 2 2 !
! - 4 - 2s + s - 4 - 2s + s !
To check that the properties of inverses hold for polynomial matrices too use:
-->inv(A)*A
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
ans =
! 1 0 !
! - - !
! 1 1 !
! !
! 5.773E-15 + 6.217E-15s 1 !
! ---------------------- - !
! 2 !
! - 4 - 2s + s 1 !
The result is not exactly an identity matrix due to some small numerical error. The
coefficients shown in the term belonging to the second row and first column, however, are
negligible small. To eliminate such small coefficients we can use SCILAB function clean:
-->clean(ans)
ans =
! 1 0 !
! - - !
! 1 1 !
! !
! 0 1 !
! - - !
! 1 1 !
The result verifies that the matrix is indeed an identity matrix.
The inverse of a polynomial matrix can be written as A
-1
= N
s
/d, where N
s
is a square matrix
and d is a polynomial. The matrix N
s
and the polynomial d are returned as separate factors by
function coffg, for example:
-->[Ns,d] = coffg(A)
d =
2 3
2 + 3s + .5s - .5s
Ns =
! 2 !
! .5s 2 - s !
! !
! 2 !
! - 1 - 2s - s 1 + s !
Evaluating a polynomial or polynomial matrix
To evaluate a polynomial or polynomial matrix, say p(x), at a particular value of x use the
function horner, for example:
-->horner(p,2)
ans =
9.
-->horner(A,3)
ans =
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
! 4. 1. !
! 16. 4.5 !
Characteristic matrix and characteristic equations
Polynomial matrices can be used, for example, to write the characteristic matrix and the
characteristic equation of a square matrix (see chapter 5). To illustrate this applications, we
create a square random matrix B:
-->B = int(10*rand(3,3))
B =
! 2. 3. 8. !
! 7. 6. 6. !
! 0. 6. 8. !
The characteristic matrix is calculated as B-sI
33
:
-->B-s*eye(3,3)
ans =
! 2 - s 3 8 !
! !
! 7 6 - s 6 !
! !
! 0 6 8 - s !
The determinant of the characteristic matrix is the characteristic polynomial of the matrix B:
-->det(B-s*eye(3,3))
ans =
2 3
192 - 19s + 16s - s
Polynomial functions applied to polynomial matrices
Many of the functions whose application was illustrated above for single polynomials apply as
well to matrices of polynomial elements as illustrated with the following examples:
-->M = [(2-s)/(s^2+s) s^2; 1/(s+1) (s+1)/(s+2)]
M =
! 2 !
! 2 - s s !
! ----- - !
! 2 !
! s + s 1 !
! !
! 1 1 + s !
! ----- ----- !
! 1 + s 2 + s !
-->denom(M)
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
ans =
! 2 !
! s + s 1 !
! !
! 1 + s 2 + s !
-->numer(M)
ans =
! 2 !
! 2 - s s !
! !
! 1 1 + s !
This introduction to polynomials in SCILAB is by no means a comprehensive one. Polynomials
and polynomial matrices are commonly used in signal processing and control system analysis.
The reader is invited to check extensive applications of polynomials and polynomial matrices in
the document Signals.pdf, available in the documentation section of SCILABs web page:
http://www-rocq.inria.fr/scilab/doc.html
Derivatives of polynomials
While SCILAB is basically a numerical environment, it allows a few symbolic operations using
polynomials. One of such functions is the function derivat which is used to calculate the
derivative of polynomials or rational expressions. In the next applications we define a
polynomial variable x and apply function derivat to it and to polynomial and rational
expressions involving variable x.
First, define the polynomial variable x:
-->x=poly(0,'x')
x =
x
Next, we obtain the derivatives of functions y = x, y = x
2
, y = 1/x, and y = 2x
3
-2x:
-->derivat(x)
ans =
1
-->derivat(x^2)
ans =
2x
-->derivat(1/x)
ans =
- 1
-
2
x
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
-->derivat(2*x^3-2*x)
ans =
2
- 2 + 6x
An attempt to obtain the derivative of function y = sin(x) fails since function derivat applies
only to polynomials and rational expressions:
-->derivat(sin(x))
!--error 4
undefined variable : %p_sin
A rational expression is simply a fraction or combination of fractions involving polynomials.
The following is an application of function derivat to a rational expression:
-->derivat((2*x^2-5*x)/(x^3-x^2+2))
ans =
2 3 4
- 10 + 8x - 5x + 10x - 2x
-----------------------
2 3 4 5 6
4 - 4x + 4x + x - 2x + x
Generating a polynomial given its coefficients
We define the polynomial of order n as
P
n
(x) = a
o
+ a
1
x + a
2
x
2
+ + a
n-1
x
n-1
+ a
n
x
n
Using SCILAB we can obtain a polynomial expression by using the function poly. The general
form of the function is:
[p]=poly(a,"x", ["flag"])
where a is a vector, matrix or real number, x is a symbolic variable; and, "flag" is a string
(either "roots", "coeff" -- the default value is "roots") that determines whether the values in a
represent the roots of the polynomial or its coefficients. If a is a vector representing the
coefficients of the polynomial, the vector should be defined or entered as [a
o
a
1
a
2
a
n-1
a
n
].
(Note: If a is a matrix, p is the characteristic polynomial for the eigenvalues of the matrix, i.e.,
p = det(xI-a), x being the symbolic variable.)
For example, if you want to obtain the polynomial whose coefficients are given by a = [3. -5.
1.], use the following command:
-->p = poly([3. -5. 1.],"x","coeff")
p =
2
3 - 5x + x
The command above will store the polynomial expression '3-5x+x
2
' into variable p. You can also
define the vector of coefficients into a variable before calling poly. For example:
-->v = [4. -2. -1. 7.];
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
-->k = poly(v,"y","coeff")
k =
2 3
4 - 2y - y + 7y
will produce the polynomial '4-2y-y
2
+7y
3
' and store it into variable k.
Generating a polynomial given its roots
To obtain a polynomial given its roots use the same function poly as above, but replace "coeff"
for "roots" in the last argument of the function. For example, to generate the polynomial
whose roots are [1. 1. -1.], try:
-->q = poly([1. 1. -1.],"z","roots")
q =
2 3
1 - z - z + z
The result is the polynomial '1-z-z
2
+z
3
' stored into the variable q. Because "roots" is the
default value of the "flag" argument in the definition of poly, we could have use
-->q = poly([1. 1. -1.],"z")
to produce the polynomial.
The vector containing the roots of the polynomial can be stored into a variable and the variable
name used as the first argument in the call of function poly. For example, to obtain the
polynomial whose roots are [2. 3. -1.], we could use:
-->w = [2. 3. -1.];
-->r = poly(w,"n")
r =
2 3
6 + n - 4n + n
The variable r will now contain the polynomial ' 6+n-4n
2
+n
3
'.
Obtaining the roots of a polynomial
To obtain the roots of a polynomial expression use the function roots. Some examples are
shown below, using the polynomials k, p, q, and r defined above :
-->roots(k)
-->roots(p)
-->roots(q)
-->roots(r)
Verify the following results using SCILAB: roots(k) =[ 0.5180404 + .6094027i, 0.5180404 -
.6094027i, - 0.8932236], and roots(p) = [0.6972244, 4.3027756]. The results roots(q) and
roots(r) should be pretty obvious.
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
Indefinite integrals for polynomials
Indefinite integrals for polynomials can be obtained using the following function intpoly:
function [pInt] = intpoly(p)
//This function calculates the indefinite integral
//of polynomial p
c = coeff(p);
n = length(c)-1;
d = [1];
for j=1:n+1
d = [d j];
end;
cc = [0 c];
cc = cc./d;
disp('Indefinite integral - Add integration constant');
printf(' \n');
pInt = poly(cc,varn(p),'coeff');
//end function intpoly
Some examples follow:
-->getf('intpoly')
-->p1 = poly([5 0 -2 3 1 0 2],'s','coeff')
p1 =
2 3 4 6
5 - 2s + 3s + s + 2s
-->intpoly(p1)
Indefinite integral - Add integration constant
ans =
3 4 5 7
5s - .6666667s + .75s + .2s + .2857143s
-->p2 = poly([-2 3 -5],'t','roots')
p2 =
2 3
- 30 - 11t + 4t + t
-->intpoly(p2)
Indefinite integral - Add integration constant
ans =
2 3 4
- 30t - 5.5t + 1.3333333t + .25t
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
Numerical Methods with Polynomials
In this section we present numerical methods used for evaluation of polynomials and of its first
derivative, as well as methods for fitting polynomials to tables of data.
Polynomial deflation
Polynomial deflation refers to the procedure by which a polynomial of degree n-1, say Q
n-1
(x),
is obtained from a polynomial of degree n, say P
n
(x), by dividing out the factor (x-r).
Polynomials P
n
(x) and Q
n-1
(x) and the value r are related by:
P
n
(x) = (x-r)Q
n-1
(x) + R,
Where R is a constant remainder, i.e., P
n
(r) = R. If for a particular value of r, say r = r
0
, P
n
(x)
= (x-r)Q
n-1
(x), (i.e., R = 0) then r
0
is a root of P
n
(x).
The derivative of P
n
(x), in terms of Q
n
(x) and r is:
P
n
(x) = (x-r)Q
n-1
(x) + Q
n-1
(x).
Thus, P
n
(r) = Q
n-1
(r).
The SCILAB commands shown below can be used for polynomial deflation, which in turn allows
us to calculate the first derivative of a polynomial at a given value. For example, using the
polynomial
P
5
(x) = -120 + 274x - 225x
2
+ 85x
3
- 15x
4
+ x
5
,
we can evaluate P
5
(2.5) and P'
5
(2.5), as follows:
-->P5 = poly([-120 274 -225 85 -15 1],'x','coeff') //Define P5(x)
P5 =
2 3 4 5
- 120 + 274x - 225x + 85x - 15x + x
-->R = horner(P5,2.5) //Evaluate P5(2.5)
R =
- 1.40625
-->d5 = x - 2.5 //Prepare factor (x-2.5)
d5 =
- 2.5 + x
-->[R4,Q4] = pdiv(P5,d5) //Deflate polynomial
Q4 =
2 3 4
47.4375 - 90.625x + 53.75x - 12.5x + x
R4 =
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
- 1.40625
-->P5prime = horner(Q4,2.5) //Evaluate derivative P5(x) at x = 2.5
P5prime =
.5625
-->P5prime = derivat(P5) //Evaluate derivative P5(x) with derivat
P5prime =
2 3 4
274 - 450x + 255x - 60x + 5x
-->horner(P5prime,2.5) //Evaluate derivative
ans =
.5625
For x=2, try the following SCILAB commands:
horner(P5,2)
d5=x-2
[R4,Q4]=pdiv(P5,d5)
horner(Q4,2)
Direct fitting of a polynomial
Suppose that we have a set of n+1 data values {(x
0
,y
0
), (x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)} and we want
to fit a polynomial of degree n,
y = P
n
(x) = a
0
+ a
1
x + a
2
x
2
+ + a
n
x
n
,
to these data. A direct fitting of the polynomial will involve the solution of the system of n+1
equations resulting from replacing each pair of data values (x
i
,y
i
), i = 0, 1, 2, , n, into the
polynomial, i.e.,
a
0
+ a
1
x
0
+ a
2
x
0
2
+ + a
n
x
0
n
= y
0
a
0
+ a
1
x
1
+ a
2
x
1
2
+ + a
n
x
1
n
= y
1
.
.
.
a
0
+ a
1
x
n
+ a
2
x
n
2
+ + a
n
x
n
n
= y
n
The unknowns in this system are the coefficients of the polynomial a
0
, a
1
, , a
n
.
The following function, dfp, will produce a direct polynomial fitting given the sets of paired
data values (x
i
,y
i
), i = 0, 1, 2, , n:
function [a]=dfp(x,f)
//Performs direct data fitting for a polynomial given
//data sets {(x0,f0), (x1,f1), ..., (xn,fn)}.
//check if x,f are of the same length
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
[m1,n1]=size(x);
[m2,n2]=size(f);
if((m1 <> m2) | (n1 <> n2)) then
error('x and f are not of the same size');
abort;
end;
m = m1;
n = m - 1; //define order of polynomial
B = ones(m,m); //fill matrix coefficients with 1.0
//Calculate rest of matrix
for i = 1:m
for j = 2:m
B(i,j) = x(i)^(j-1);
end;
end;
a = linsolve(B,-f); //solve for the coefficients
a=a'; //show result as a row vector
//end function dfp
The call to the function is simply dfp(x,f). Values of x and f need to be defined as column
vectors. Try the following example:
-->getf('dfp')
-->x = [3.4 3.5 3.6]; y = [0.294118, 0.285714, 0.277778];
-->a = dfp(x',y')
a =
! .858314 - .2455 .0234 !
To generate the polynomial, use:
-->p = poly(a,'x','coeff')
p =
2
.858314 - .2455x + .0234x
The evaluation of the polynomial for the values of x given above verifies the polynomial fitting:
-->horner(p,x)
ans =
! .294118 .285714 .277778 !
Lagrange polynomials
Lagrange polynomials are used to fit a data set {(x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)}. A Lagrange
polynomial is expanded from the formula:
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
For example, for n = 2, we will write:
The following function, LagPol, will evaluate the Lagrange polynomial of order n at x = x0,
using the data in column vectors x and f.
function [y]=lagpol(x0,x,f,n)
//Evaluates the Lagrange polynomial corresponding
//to the data set {(x1,f1), (x2,f2), ..., (xn,fn)}
//at point x = x0.
//check if x,f are of the same length
[m1,n1]=size(x);
[m2,n2]=size(f);
if((m1 <> m2) | (n1 <> n2)) then
error('x and f are not of the same size');
abort;
end;
m = n1;
if(n>m-1)then
disp(n,"polynomial degree =")
disp(m,"vector size=")
error('use smaller polynomial degree n');
abort
end;
m = n + 1;
N = ones(1,m);
D = N;
C = N;
y = 0.0;
for j = 1:m
for k = 1:m
if(k <> j) then
N(j) = N(j)*(x0-x(k));
D(j) = D(j)*(x(j)-x(k));
end;
end;
C(j) = N(j)/D(j);
y = y + C(j)*f(j);
end;
//end LagPol function
As an example, try the following SCILAB statements:
-->getf('lagpol')
-->x = [3.4 3.5 3.55 3.65]; y = [0.294118,0.285714,0.281690,0.273973];
.
) (
) (
) (
1
, 1
, 1
1 j
n
j
n
j k k
k j
n
j k k
k
n
y
x x
x x
x p

.
) ( ) (
) (
2 1
2 1 1 2 2 1
2
1 2
1
1
2 1
2
1
x x
x y x y x y y
y
x x
x x
y
x x
x x
x p

Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz


-->lagpol(3.44,x,y,1)
ans =
.2907564
-->lagpol(3.44,x,y,2)
ans =
.2906994
-->lagpol(3.44,x,y,3)
ans =
.2906977
-->lagpol(3.44,x,y,4)
polynomial degree =
4.
vector size=
4.
!--error 9999
use smaller polynomial degree n
at line 18 of function lagpol called by :
lagpol(3.44,x,y,4)
The following example demonstrate the use of Lagrange polynomials of orders n = 1, 2, 3, 4, 5,
and 6 to the following data set.
x 0.0 1.2 3.5 4.2 6.2 8.1 11.2
y 15.0 29.0 13.3 -6.4 2.9 17.1 -8.0
The SCILAB commands that follow load the original data and produce Lagrange polynomials for
orders n = 1 to 6. A plot showing the different polynomials and the original data is also
produced.
-->getf('lagpol')
-->x = [0.0,1.2,3.5,4.2,6.2,8.1,11.2]; //Original x data
-->y = [15.0,29.0,13.3,-6.4,2.9,17.1,-8.0]; //Original y data
-->xx = [0.0:0.1:12.0]; //x data for fitting
-->yy1 = []; yy2 = []; yy3 =[]; yy4 = []; yy5 = []; yy6 =[];
-->for j = 1:n //Lagrange polynomial fittings
--> yy1 = [yy1 lagpol(xx(j),x,y,1)];
--> yy2 = [yy2 lagpol(xx(j),x,y,2)];
--> yy3 = [yy3 lagpol(xx(j),x,y,3)];
--> yy4 = [yy4 lagpol(xx(j),x,y,4)];
--> yy5 = [yy5 lagpol(xx(j),x,y,5)];
--> yy6 = [yy6 lagpol(xx(j),x,y,6)];
-->end;
The plot is produced with the following statements:
-->plot2d([xx',xx',xx',xx',xx',xx'],[yy1',yy2',yy3',yy4',yy5',yy6'],...
-->[1:1:6],'111','n=1@n=2@n=3@n=4@n=5@n=6',[0 -50 12 50])
-->plot2d(x,y,-9,'011','y',[0 -50 12 50])
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
-->xtitle('Lagrange polynomials', 'x', 'y')
SCILAB function mtlb_diff
SCILAB provides function mtlb_diff which we will use to generate tables of differences in
subsequent sections. This function can be used, for example, to calculate the differences
between consecutive elements in a vector. The following application uses a vector u with 7
elements:
-->u = [0, 1, 3, 6, 11, 15, 23]
u =
! 0. 1. 3. 6. 11. 15. 23. !
function mtlb_diff produces a vector of 6 elements representing the differences between
consecutive elements of the original vector:
-->Du = mtlb_diff(u)
Du = ! 1. 2. 3. 5. 4. 8. !
Consecutive applications of the function mtlb_diff produce vectors of differences with one less
element than the previous vector:
-->D2u = mtlb_diff(Du)
D2u = ! 1. 1. 2. - 1. 4. !
-->D3u = mtlb_diff(D2u)
D3u = ! 0. 1. - 3. 5. !
-->D4u = mtlb_diff(D3u)
D4u = ! 1. - 4. 8. !
-->D5u = mtlb_diff(D4u)
D5u = ! - 5. 12. !
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
-->D6u = mtlb_diff(D5u)
D6u = 17.
The following example shows the application of function mtlb_diff to a column vector v of six
elements, and consecutive applications to the resulting vector until a single value is obtained:
-->v = [1;3;5;9;12;23]
v =
! 1. !
! 3. !
! 5. !
! 9. !
! 12. !
! 23. !
-->Dv = mtlb_diff(v)
Dv =
! 2. !
! 2. !
! 4. !
! 3. !
! 11. !
-->D2v = mtlb_diff(Dv)
D2v =
! 0. !
! 2. !
! - 1. !
! 8. !
-->D3v = mtlb_diff(D2v)
D3v =
! 2. !
! - 3. !
! 9. !
-->D4v = mtlb_diff(D3v)
D4v =
! - 5. !
! 12. !
-->D5v = mtlb_diff(D4v)
D5v =
17.
The following example shows the application of function mtlb_diff to a 5 4 matrix. The result
of mtlb_diff, when applied to matrices, is another matrix containing the differences between
consecutive rows of the original matrix:
-->A = int(10*rand(5,4))
A =
! 2. 9. 5. 4. !
! 2. 2. 4. 2. !
! 8. 3. 3. 6. !
! 6. 3. 5. 4. !
! 3. 2. 5. 9. !
-->DA = mtlb_diff(A)
DA =
! 0. - 7. - 1. - 2. !
! 6. 1. - 1. 4. !
! - 2. 0. 2. - 2. !
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
! - 3. - 1. 0. 5. !
Continuous application of function mtlb_diff to the resulting matrix continues until the matrix
is reduced to a row vector:
-->D2A = mtlb_diff(DA)
D2A =
! 6. 8. 0. 6. !
! - 8. - 1. 3. - 6. !
! - 1. - 1. - 2. 7. !
-->D3A = mtlb_diff(D2A)
D3A =
! - 14. - 9. 3. - 12. !
! 7. 0. - 5. 13. !
-->D4A = mtlb_diff(D3A)
D4A =
! 21. 9. - 8. 25. !
From this point on, further applications of mtlb_diff will produce row vectors of smaller sizes
until a single value results:
-->D5A = mtlb_diff(D4A)
D5A =
! - 12. - 17. 33. !
-->D6A = mtlb_diff(D5A)
D6A =
! - 5. 50. !
-->D7A = mtlb_diff(D6A)
D7A =
55.
Difference Tables
A difference table corresponding to a data set {(x
0
,y
0
), (x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)} is illustrated
below:
x y y
2
y
3
y
4
y
x
0
y
0
y
0
= y
1
-y
0
x
1
y
1

2
y
0
= y
1
-y
0
y
1
= y
2
-y
1

3
y
0
=
2
y
1
-
2
y
0
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
x
2
y
2

2
y
1
= y
2
-y
1

4
y
0
=
3
y
1
-
3
y
0
y
2
= y
2
-y
1

3
y
1
=
2
y
2
-
2
y
1
x
3
y
3

2
y
2
= y
2
-y
1
y
3
= y
3
-y
2
x
4
y
4

This table corresponds to what is referred to as forward differences. The first two columns
are simply the data values. The third column is the difference of values of y, the fourth
column is the difference of values of the third column, and so forth. The table illustrated
above starts with five points and is able to calculate differences up to the fourth order.
Consider, for example, the following table of forward differences corresponding to the function
y = exp(x) in the range [2,3] with x increments of 0.1:
x y y
2
y
3
y
4
y
5
y
6
y
7
y
2.00 7.389056
0.777114
2.10 8.16617 0.08173
0.858844 0.008596
2.20 9.025013 0.090325 0.000904
0.949169 0.0095 9.51E-05
2.30 9.974182 0.099825 0.000999 1E-05
1.048994 0.010499 0.000105 1.05E-06
2.40 11.02318 0.110324 0.001104 1.11E-05
1.159318 0.011603 0.000116 1.16E-06
2.50 12.18249 0.121926 0.00122 1.22E-05
1.281244 0.012823 0.000128 1.28E-06
2.60 13.46374 0.13475 0.001349 1.35E-05
1.415994 0.014172 0.000142 1.42E-06
2.70 14.87973 0.148921 0.00149 1.49E-05
1.564915 0.015662 0.000157
2.80 16.44465 0.164584 0.001647
1.729499 0.017309
2.90 18.17415 0.181893
1.911392
3.00 20.08554
The following SCILAB commands will generate the columns of the table of differences shown
above:
-->x=[3.0:0.1:4.0]'; y = exp(x);
-->Dy = mtlb_diff(y)
Dy =
! 2.1124144 !
! 2.3345789 !
! 2.5801087 !
! 2.8514611 !
! 3.1513519 !
! 3.4827825 !
! 3.8490699 !
! 4.2538801 !
! 4.7012646 !
! 5.1957009 !
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
-->x=[2.0:0.1:3.0]'; y = exp(x);
-->Dy = mtlb_diff(y)
Dy =
! .7771138 !
! .8588436 !
! .9491690 !
! 1.0489939 !
! 1.1593176 !
! 1.2812441 !
! 1.4159937 !
! 1.564915 !
! 1.7294986 !
! 1.9113916 !
-->D2y = mtlb_diff(Dy)
D2y =
! .0817298 !
! .0903254 !
! .0998250 !
! .1103237 !
! .1219265 !
! .1347496 !
! .1489214 !
! .1645836 !
! .1818930 !
-->D3y = mtlb_diff(D2y)
D3y =
! .0085956 !
! .0094996 !
! .0104987 !
! .0116028 !
! .0128231 !
! .0141717 !
! .0156622 !
! .0173094 !
-->D4y = mtlb_diff(D3y)
D4y =
! .0009040 !
! .0009991 !
! .0011042 !
! .0012203 !
! .0013486 !
! .0014905 !
! .0016472 !
-->D5y = mtlb_diff(D4y)
D5y =
! .0000951 !
! .0001051 !
! .0001161 !
! .0001283 !
! .0001418 !
! .0001568 !
-->D6y =mtlb_diff(D5y)
D6y =
! .0000100 !
! .0000111 !
! .0000122 !
! .0000135 !
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
! .0000149 !
-->D7y = mtlb_diff(D6y)
D7y =
! .0000011 !
! .0000012 !
! .0000013 !
! .0000014 !
A function to produce a forward-difference table
The following function, called Difference_Table(f,n), where f is a column vector and n is the
highest order of the difference, is shown below:
function [Df]=Difference_Table(f,n)
//This function calculates a difference table starting with
//a column vector f
[m,nc]=size(f)
//check that it is indeed a column vector
if(nc<>1)then
error('f is not a column vector.');
abort
end;
//check the difference order
if(n > m)then
disp(n,"n=");
disp(m,"m=");
error('n must be less than or equal to m');
abort
end;
//
Df=zeros(m,n);
for i = 1:m
Df(i,1) = f(i,1);
end;
delf = f;
for j = 2:n
delf = mtlb_diff(delf)
[m,nc]=size(delf);
for i = 1:m
Df(i,j) = delf(i,1);
end;
end;
//end of function Difference_Table
The following SCILAB commands will generate a difference table for the values of y defined
previously. The resulting matrix contains the same information than the table of differences
shown earlier, except for the location of entries. However, the interpretation of the entries is
straightforward. The first column in the matrix is the vector y, the second is the first
difference, the third column is the second difference, and so on. Furthermore, the top row in
the table of differences contains the values y
0
, y
0
,
2
y
0
, and so on:
-->Difference_Table(y,5)
ans =
! 7.3890561 .7771138 .0817298 .0085956 .0009040 !
! 8.1661699 .8588436 .0903254 .0094996 .0009991 !
! 9.0250135 .9491690 .0998250 .0104987 .0011042 !
! 9.9741825 1.0489939 .1103237 .0116028 .0012203 !
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
! 11.023176 1.1593176 .1219265 .0128231 .0013486 !
! 12.182494 1.2812441 .1347496 .0141717 .0014905 !
! 13.463738 1.4159937 .1489214 .0156622 .0016472 !
! 14.879732 1.564915 .1645836 .0173094 0. !
! 16.444647 1.7294986 .1818930 0. 0. !
! 18.174145 1.9113916 0. 0. 0. !
! 20.085537 0. 0. 0. 0. !
Newton Forward-Difference Polynomial
A Newton forward-difference polynomial uses a table of forward differences to approximate
the function of interest, y = f(x), to a polynomial on the variable
s = (x-x
0
)/x,
where x is a particular value of the independent variable where the polynomial is evaluated, x
0
is the first value of x in the table of differences, and x is the increment in the independent
variable. The expression for the Newton forward-difference polynomial is
,
3 2 1
) ( ) (
0
3
0
2
0 0
! +

,
_

,
_

,
_

+ y
s
y
s
y
s
y x P x f
n
where the terms
,
)! ( !
!
r n r
n
r
n

,
_

are binomial coefficients.


The following function calculates the Newton forward-difference polynomial of order n,
evaluated at x. The polynomial needs as input a column vector representing values of x (we
call the vector xL), and a second column vector representing the values of f. The first value of
xL represents x
o
. The function is called NFDP. Notice that in the listing of this function we
also include the listing of functions factorial and binomial, used to calculate, respectively,
factorials and binomial coefficients:
function [P]=NFDP(x,n,xL,f)
//This function calculates a Newton Forward-Difference Polynomial of
//order n, evaluated at x, using column vectors xL, f as the reference
//table. The first value of xL and of f, represent, respectively,
//xo and fo in the equation for the polynomial.
[m,nc]=size(f)
//check that it is indeed a column vector
if(nc<>1)then
error('f is not a column vector.');
abort
end;
//check the difference order
if(n >= m)then
disp(n,"n=");
disp(m,"m=");
error('n must be less than or equal to m-1');
abort
end;
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
//
xo = xL(1,1);
delx = mtlb_diff(xL);
h = delx(1,1);
s = (x-xo)/h;
P = f(1,1);
delf = f;
for i = 1:n
delf = mtlb_diff(delf)
P = P + Binomial(s,i)*delf(1,1)
end;
//end of function NFDP
function[C]=Binomial(s,i)
C = 1.0;
for k = 0:i-1
C = C*(s-k);
end;
C = C/factorial(i)
//end of function Binomial
function[fact]=factorial(nn)
fact = 1.0
for k = nn:-1:1
fact=fact*k
end;
//end of function factorial
The following SCILAB commands will generate the third-order Newton forward-difference
polynomial evaluated at x = 3.44, for the function f(x) = 1/x, with xL a column vector with
values of x: 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 4.0:
-->getf('NFDP.txt')
-->x = [3.4:0.1:4.0]; y = 1./x; //Note: y is a column vector
-->y
y =
! .0353761 !
! .0364166 !
! .0374571 !
! .0384976 !
! .0395380 !
! .0405785 !
-->NFDP(3.44,3,x',y)
ans =
.0357923
With the same values of x and f, the following command will show the Newton forward-
difference polynomial of degrees 0, 1, 2 and 3:
-->for n=0:3, NFDP(3.44,n,x',y), end
ans = .0353761
ans = .0357923
ans = .0357923
ans = .0357923
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
Newton Backward-Difference Polynomial
A backward-difference table is illustrated below. The reference value is the last value (x
0
,y
0
),
thus the table starts with a negative sub-index which increases in value until reaching the value
of zero at the bottom of the table. Backward differences are represented through the use of
the del or nabla operator , i.e., y for the first difference,
2
y for the second, and so on.
x y y
2
y
3
y
4
y
x
-4
y
-4
y
-3
= y
-3
-y
-4
x
-3
y
-3

2
y
-2
= y
-2
-y
-3
y
-2
= y
-2
-y
-3

3
y
-1
=
2
y
-1
-
2
y
-2
x
-2
y
-2

2
y
-1
= y
-1
-y
-2

4
y
0
=
3
y
0
-
3
y
-1
y
-1
= y
-1
-y
-2

3
y
0
=
2
y
0
-
2
y
-1
x
-1
y
-1

2
y
0
= y
0
-y
-1
y
0
= y
0
-y
-1
x
0
y
0

A Sterling centered-difference polynomial uses a table of centered differences to approximate
the function of interest, y = f(x), to a polynomial on the variable
s = (x-x
0
)/x,
where x is a particular value of the independent variable where the polynomial is evaluated, x
0
is the last value of x in the table of differences, and x is the increment in the independent
variable. The expression for the Newton backward-difference polynomial is
.
3
2
2
1
1
) ( ) (
0
3
0
2
0 0
! +

,
_

+
+

,
_

+
+

,
_

+ y
s
y
s
y
s
y x P x f
n
The following function calculates the Newton backward-difference polynomial of order n,
evaluated at x. The polynomial needs as input a column vector representing values of x (we
call the vector xL), and a second column vector representing the values of f. The last value of
xL represents x
o
. The function is called NBDP and is stored in file NBDP.txt:
function [P]=NBDP(x,n,xL,f)
//This function calculates a Newton Backward-Difference Polynomial of
//order n, evaluated at x, using column vectors xL, f as the reference
//table. The first value of xL and of f, represent, respectively,
//xo and fo in the equation for the polynomial.
[m,nc]=size(f)
//check that it is indeed a column vector
if(nc<>1)then
error('f is not a column vector.');
abort
end;
//check the difference order
if(n >= m)then
disp(n,"n=");
disp(m,"m=");
error('n must be less than or equal to m-1');
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
abort
end;
//
xo = xL(m,1);
delx = mtlb_diff(xL);
h = delx(1,1);
s = (x-xo)/h;
P = f(m,1);
delf = f;
for i = 1:n
delf = mtlb_diff(delf);
[m,nc] = size(delf);
P = P + Binomial(s+i-1,i)*delf(m,1)
end;
//end of function NBDP
function[C]=Binomial(s,i)
C = 1.0;
for k = 0:i-1
C = C*(s-k);
end;
C = C/factorial(i)
//end of function Binomial
function[fact]=factorial(nn)
fact = 1.0
for k = nn:-1:1
fact=fact*k
end;
//end of function factorial
The following SCILAB commands will generate the third-order Newton backward-difference
polynomial evaluated at x = 3.44, for the function f(x) = 1/x, with xL a column vector with
values of x: 3.0, 3.1, 3.2, 3.3, 3.4, 3.5:
-->getf('NBDP.txt')
-->x = [3.0:0.1:3.5]'; y = x^(-1); //both x & y are column vectors
-->NBDP(3.44,3,x,y)
ans =
.2906979
With the same values of x and f, the following command will show the Newton forward-
difference polynomial of degrees 0, 1, 2 and 3:
-->for n=0:3, NBDP(3.44,n,x,y), end
ans = .2857143
ans = .2907563
ans = .2906952
ans = .2906979
Stirling centered-difference polynomial
A centered-difference table is illustrated below. The reference value is the middle value
(x
0
,y
0
), thus the table should contain an odd number of data points. Centered differences are
represented through the use of the symbol, i.e., y for the first difference,
2
y for the
second, and so on.
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
x y y
2
y
3
y
4
y
x
-2
y
-2
y
-3/2
= y
-1
-y
-2
x
-1
y
-1

2
y
-1
= y
-1/2
-y
-3/2
y
-1/2
= y
0
-y
-1

3
y
-1/2
=
2
y
0
-
2
y
-1
x
0
y
0

2
y
0
= y
1/2
-y
-1/2

4
y
0
=
3
y
1/2
-
3
y
-1/2
y
1/2
= y
1
-y
0

3
y
1/2
=
2
y
1
-
2
y
0
x
1
y
1

2
y
1
= y
3/2
-y
1/2
y
3/2
= y
2
-y
1
x
2
y
2

A Stirling centered-difference polynomial uses a table of centered differences with an odd
number of data points to approximate the function of interest, y = f(x), to a polynomial on the
variable
s = (x-x
0
)/x,
where x is a particular value of the independent variable where the polynomial is evaluated, x
0
is the middle value of x in the table of differences, and x is the increment in the independent
variable. The expression for the Sterling centered-difference polynomial is
( )
( ) .
4
1
4
2
2
1
2
1
3
1
2 2
1
2
1
2
1
1
) ( ) (
4
2 / 1
3
2 / 1
3
0
2
2 / 1 2 / 1 0
! +

,
_

,
_

+
+

,
_

+
+ +

,
_

+
+

,
_

,
_

,
_

+
+ +

,
_

o
n
y
s s
y y
s
y
s s
y y
s
y x P x f


The following function calculates the Stirling centered-difference polynomial of order n,
evaluated at x. The polynomial needs as input a column vector representing values of x (we
call the vector xL), and a second column vector representing the values of f. The length of the
vectors xL and f must be an odd number. The middle value of the vectors xL and f will
represent x
o
and f
o
. The function is called stirling and is stored in file stirling.txt:
function [P]=stirling(x,n,xL,f)
//This function calculates a Stirling-Difference Polynomial of
//order n, evaluated at x, using column vectors xL, f as the reference
//table. The length of the vectors xL and f must be an odd number,
//with xo, fo being the middle value in the corresponding lists.
[m,nc]=size(f)
//check that it is indeed a column vector
if(nc<>1)then
error('f is not a column vector.');
abort
end;
//check that the size of the lists xL and f is odd
if(modulo(m,2)==0)then
disp(m,"list size =")
error('list size must be an odd number');
abort
end;
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
//check the difference order
if(n > (m-1)/2)then
disp(n,"n=");
disp(m,"m=");
error('n must be less than or equal to (m-1)/2');
abort
end;
//
i0 = floor(m/2)+1
xo = xL(i0,1);
delx = mtlb_diff(xL);
h = delx(1,1);
s = (x-xo)/h;
P = f(i0,1);
delf = f;
for i = 1:n
delf = mtlb_diff(delf)
[m,nc]=size(delf)
j = m/2
k = j+1
P = P + 0.5*Binomial(s+i-1,i)*(delf(j,1)+delf(k,1))
delf = mtlb_diff(delf)
[m,nc]=size(delf)
j = floor(m/2)+1
P = P + 0.5*(Binomial(s+i,2*i)+Binomial(s+i-1,2*i))*delf(j,1)
end;
//end of function stirling
function[C]=Binomial(s,i)
C = 1.0;
for k = 0:i-1
C = C*(s-k);
end;
C = C/factorial(i)
//end of function Binomial
function[fact]=factorial(nn)
fact = 1.0
for k = nn:-1:1
fact=fact*k
end;
//end of function factorial
The following SCILAB commands will generate the third, fourth, and, if possible, the fifth-order
Stirling centered-difference polynomial evaluated at x = 3.44, for the function f(x) = 1/x, with
xL a column vector with values of x: 3.0, 3.1, 3.2, 3.3, 3.4, 3.5,3.6,3.7,3.8 (i.e., vector size =
9):
-->getf('stirling.txt')
-->x = [3.0:0.1:3.8]'; y = x^(-1);
-->stirling(3.44, 3, x, y)
ans =
.2906824
-->stirling(3.44, 4, x, y)
ans =
.2906823
-->stirling(3.44, 5, x, y)
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
n=
5.
m=
9.
!--error 9999
n must be less than or equal to (m-1)/2
at line 22 of function stirling called by :
stirling(3.44, 5, x, y)
With the same values of x and f, the following command will show the Stirling centered-
difference polynomial of degrees 0 through 4:
-->for n = 0:4, stirling(3.44, n, x, y), end
ans = .2941176
ans = .2906952
ans = .2906825
ans = .2906824
ans = .2906823
Bessel centered-difference polynomial
A Bessel centered-difference polynomial uses a table of centered differences with an even
number of data points to approximate the function of interest, y = f(x), to a polynomial on the
variable
s = (x-x
0
)/x,
where x is a particular value of the independent variable where the polynomial is evaluated, x
0
is a middle value of x in the table of differences, and x is the increment in the independent
variable. Bessel centered differences are based on a point between x
0
and x
1
. The expression
for the Bessel centered-difference polynomial is
( )
( ) .
2
1
4
1
3 3
1
2
1
2
1
2 1
1
1 2
1
) (
2
1
) ( ) (
1
4 4
2 / 1
3
1
2
0
2
2 / 1 1 0
! + +

,
_

+
+

,
_

,
_

,
_

+
+
+

,
_

,
_

,
_

,
_

+ +
y y
s
y
s s
y y
s
y
s s
y y x P x f
o
n


The following function calculates the Bessel centered-difference polynomial of order n,
evaluated at x. The polynomial needs as input a column vector representing values of x (we
call the vector xL), and a second column vector representing the values of f. The length of the
vectors xL and f must be an odd number. The middle value of the vectors xL and f will
represent x
o
and f
o
. The function is called stirling and is stored in file stirling.txt:
function [P]=bessel(x,n,xL,f)
//This function calculates a Bessel Centered-Difference Polynomial of
//order n, evaluated at x, using column vectors xL, f as the reference
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
//table. The length of the vectors xL and f must be an even number (m),
//with xo, fo being the value number m/2 in the corresponding lists.
[m,nc]=size(f)
//check that it is indeed a column vector
if(nc<>1)then
error('f is not a column vector.');
abort
end;
//check that the size of the lists xL and f is odd
if(modulo(m,2)<>0)then
disp(m,"list size =")
error('list size must be an even number');
abort
end;
//check the difference order
if(n > (m-1)/2)then
disp(n,"n=");
disp(m,"m=");
error('n must be less than or equal to (m-1)/2');
abort
end;
//
i0 = floor(m/2)
xo = xL(i0,1);
delx = mtlb_diff(xL);
h = delx(1,1);
s = (x-xo)/h;
P = 0.5*(f(i0,1)+f(i0+1,1));
delf = f;
for i = 1:n
delf = mtlb_diff(delf)
[m,nc]=size(delf)
j = (m+1)/2;
P = P + 0.5*(Binomial(s+i-1,i)+Binomial(s+i-2,i))*delf(j,1)
delf = mtlb_diff(delf)
[m,nc]=size(delf)
j = m/2;
k = j+1
P = P + 0.5*Binomial(s+i-1,2*i)*(delf(j,1)+delf(k,1))
end;
//end of function bessel
function[C]=Binomial(s,i)
C = 1.0;
for k = 0:i-1
C = C*(s-k);
end;
C = C/factorial(i)
//end of function Binomial
function[fact]=factorial(nn)
fact = 1.0
for k = nn:-1:1
fact=fact*k
end;
//end of function factorial
The following SCILAB commands will generate the third, fourth, and, if possible, the fifth-order
Bessel centered-difference polynomial evaluated at x = 3.44, for the function f(x) = 1/x, with
xL a column vector with values of x: 3.0, 3.1, 3.2, 3.3, 3.4, 3.5,3.6,3.7,3.8 (i.e., vector size =
9):
-->x = [3.0:0.1:3.9]'; y = x^(-1);
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
-->getf('bessel.txt')
-->bessel(3.44,3,x,y)
ans = .2906944
-->bessel(3.44,4,x,y)
ans = .2906944
With the same values of x and f, the following command will show the Bessel centered-
difference polynomial of degrees 0 through 4:
-->for n = 0:4, bessel(3.44,n,x,y), end
ans = .2899160
ans = .2906977
ans = .2906944
ans = .2906944
ans = .2906944
Least Squares Polynomial Approximation
Given a set of N data points (x
1
,Y
1
), (x
2
, Y
2
), , (x
N
,Y
N
), we try to obtain a polynomial
y = b
o
+ b
1
x + b
2
x
2
+ + b
n-1
x
n-1
+ b
n
x
n
,
such that the sum of the squares of the estimation errors, e
i
= Y
i
- y
i
, i.e.,
is minimized.
The following method (e.g., Johnson, R.A., 1994, "Miller & Freund's Probability and Statistics
for Engineers," Prentice Hall, Englewood Cliffs, New Jersey) uses matrices to obtain the vector
of coefficients b = [b
o
b
1
b
2
b
n-1
b
n
], by creating the matrix X,
and solving for b, from
b = (X
T
X)
-1
X
T
y,
where y is the column vector, y = [ y
1
y
2
y
n-1
y
N
]
T
. For implementation in SCILAB, we use
two column vectors, x = [ x
1
x
2
x
N-1
x
N
]
T
, and y = [ y
1
y
2
y
N-1
y
N
]
T
, as well as the
polynomial degree, n, as input to a function PolyFit, listed below.
function [b]=PolyFit(x,y,n)
//Least-square method fitting a polynomial equation



N
i
i i
N
i
i n o
y Y e b b b S
1
2
1
2
1
) ( ) ,..., , (
,
1
1
1
1
2
3
2
3 3
2
2
2 2
1
2
1 1
1
1
1
1
1
1
]
1

n
N N N
n
n
n
x x x
x x x
x x x
x x x
"
# # # # #
"
"
"
X
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
//x and y are column vectors
//Check that x and y have the same size
[nrx,ncx]=size(x)
[nry,ncy]=size(y)
if((ncx<>1)|(ncy<>1))then
error('x or y, or both, not column vector(s)');
abort;
end;
if((nrx<>nry))then
error('x and y have not the same length');
abort;
end;
N = nrx;
X = ones(N,n+1);
for j = 2:n+1
for i = 1:N
X(i,j) = x(i,1)^(j-1)
end;
end;
XT = X';
M = XT*X;
M = inv(M);
M = M*XT;
b = M*y;
//end PolyFit function
For example, to fit a second-order polynomial to the following data, use the SCILAB commands
shown below:
x y
0 12.0
1 10.5
2 10.0
3 8.0
4 7.0
5 8.0
6 7.5
7 8.5
8 9.0
-->getf('PolyFit.txt')
-->x = [0:1:8]
x =
! 0. 1. 2. 3. 4. 5. 6. 7. 8. !
-->Y = [12,10.5,10,8,7,8,7.5,8.5,9]
Y =
! 12. 10.5 10. 8. 7. 8. 7.5 8.5 9. !
-->b = PolyFit(x',Y',2)
b =
! 12.184848 !
! - 1.8465368 !
! .1829004 !
The vector b now can be used in the function poly to generate the polynomial y = f(x), which
can be evaluated at x = 6.5 by using the function horner, as shown below:
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
-->y = poly(b,'x','coeff')
y =
2
12.184848 - 1.8465368x + .1829004x
-->horner(y,6.5)
ans =
7.9099026
Linear Data Fitting
The function PolyFit, when called with n = 1, can be used to fit a linear function to the data,
namely,
y = b
o
+b
1
x.
For example, fit a linear relationship to the following data using PolyFit:
x y
20 0.18
60 0.37
100 0.35
140 0.78
180 0.56
220 0.75
260 1.18
300 1.36
340 1.17
380 1.65
The SCILAB commands to use are:
-->x = [20:40:380]';
-->Y = [0.18, 0.37, 0.35, 0.78, 0.56, 0.75, 1.18, 1.36, 1.17, 1.65]';
-->b = PolyFit(x,Y,1)
b =
! .0692424 !
! .0038288 !
-->y = poly(b,'x','coeff')
y =
.0692424 + .0038288x
To evaluate y = f(x) at, say, x = 190, use:
-->horner(y,190)
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
ans =
.7967121
We can also obtain the errors for the original data by using the following commands:
-->yh = horner(y,x)
yh =
! .1458182 !
! .2989697 !
! .4521212 !
! .6052727 !
! .7584242 !
! .9115758 !
! 1.0647273 !
! 1.2178788 !
! 1.3710303 !
! 1.5241818 !
-->e = Y-yh
e =
! .0341818 !
! .0710303 !
! - .1021212 !
! .1747273 !
! - .1984242 !
! - .1615758 !
! .1152727 !
! .1421212 !
! - .2010303 !
! .1258182 !
Other Linearized Data Fittings
The table below shows other functions that can easily be linearized and their coefficients
obtained by using the PolyFit function with n = 1.
Independent Dependent
Type of Actual Linearized variable Variable
Fitting Model Model

Linear y = a + bx y = a + bx [same] x y
Logarithmic y = a + b ln(x) y = a + b ln(x) [same] ln(x) y
Exponential y = a e
bx
ln(y) = ln(a) + bx x ln(y)
Power y = a x
b
ln(y) = ln(a) + b ln(x) ln(x) ln(y)
Simplified polynom. y = a + bx
m
y = a + bx
m
[same] x
m
y
Reciprocal linear y = 1/(a+bx) 1/y = a + bx x 1/y
The functions listed can be re-written as
= a + b,
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
by using the variable transformation shown above. Other combinations of variables can also be
linearized, for example, y = 1/(a+bx
m
), and y = a + b (lnx)
m
.
For example, fit an expression of the form y = a + bx
2
, to the following data:
___________
x Y
___________
0 100
110 90
180 80
250 60
300 40
340 20
___________
Use the variables = x
2
and = y. The following SCILAB commands will perform the linear
fitting:
-->x = [0;110;180;250;300;340]
x =
! 0. !
! 110. !
! 180. !
! 250. !
! 300. !
! 340. !
-->Y = [100;90;80;60;40;20]
Y =
! 100. !
! 90. !
! 80. !
! 60. !
! 40. !
! 20. !
-->x2 = x^2
x2 =
! 0. !
! 12100. !
! 32400. !
! 62500. !
! 90000. !
! 115600. !
-->b = PolyFit(x2,Y,1)
b =
! 100.35736 !
! - .0006786 !
-->y = poly(b,'x','coeff')
y =
100.35736 - .0006786x
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
Interpolation with splines
The term splines or spline curves refers to a technique by which a set of points { (x
1
,y
1
), (x
2
,y
2
),
, (x
n
,y
n
)} are fitted with a series of functions (typically, cubic polynomials) that satisfy
continuity of the functions and some of their derivatives at the points being fitted. The term
spline is borrowed from instruments used to fit a continuous smooth shape through a number of
points in drafting. A spline or French curve consists of a flexible ruler that a draftsman can
mold to fit points in paper.
Cubic splines
The method of cubic splines consists in fitting a number of cubic polynomials of the form
y = f
k
(x) = b
0
+ b
1
x + b
2
x
2
+ b
3
x
3
,
through the data set {(x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)}, requiring that not only the functions f
k
(x) but
also their first and second derivatives,
s = f
k
(x) = b
1
+ 2b
2
x + 3b
3
x
2
,
and
= f
k
(x) = 2b
2
+6b
3
x,
be continuous at the interior points (x
2
,y
2
), (x
3
,y
3
), , (x
n-1
,y
n-1
). The first derivatives f(x)
are referred to as the slope and the second derivatives as the curvature of the data fitting.
Suppose that we seek n-1 cubic polynomials to fit the n data points {(x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)}.
Because each cubic polynomial requires 4 coefficients to be determined, we have a total of
4(n-1) = 4n-4 unknowns. To satisfy continuity of the fitting function, we let polynomial
number 1, f
1
(x), fit the data through points x
1
, and x
2
, polynomial number 2, f
2
(x), through
points x
2
, and x
3
, and so on. Thus, there are two data values that satisfy each of the n-1
polynomials, i.e., y
1
= f
1
(x
1
), y
2
= f
1
(x
1
), y
2
= f
2
(x
2
), y
3
= f
2
(x
3
), , y
n-1
= f
n-2
(x
n-1
), y
n-1
= f
n-1
(x
n-1
),
and y
n
= f
n-1
(x
n
). These results constitute a total of 2(n-1) equations.
Continuity of the slope at the interior points requires that f
1
(x
2
) = f
2
(x
2
), f
2
(x
2
) = f
3
(x
2
),,
f
n-2
(x
n-1
) = f
n-1
(x
n-1
), which adds an additional n-2 equations to the system. An additional set
of n-2 equations result from continuity of curvature at the interior points, i.e., f
1
(x
2
) = f
2
(x
2
),
f
2
(x
2
) = f
3
(x
2
),, f
n-2
(x
n-1
) = f
n-1
(x
n-1
). At this point, we count with 2(n-1) + 2(n-2) = 4n-6
equations for the 4n-4 unknown coefficients. Therefore, we need to come up with two
additional equations to uniquely solve for all the unknown coefficients.
There are several options for the additional two equations including:
Set the slope of the fitting curve at the extreme points x = x
1
and x = x
n
to fixed values,
i.e., f
1
(x
1
) = h
1
and f
n-1
(x
n
) = h
n
.
Use zero curvature at the extreme points, i.e., f
1
(x
1
) = f
n-1
(x
n
) = 0.
Make the curvature at the extreme points equal to that of the closest points, i.e.,
f
1
(x
1
) = f
1
(x
2
) and f
n-1
(x
n-1
) = f
n-1
(x
n
).
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
Use a linear extrapolation of the curvature at points x
2
and x
3
to get the curvature at
point x
1
, and a linear extrapolation of the curvature at points x
n-2
and x
n-1
to get the
curvature at point x
n
.
Make the third derivative of the cubic polynomials, f
k
(x) = 6b
3
, continuous at points
x
2
and x
n-1
, i.e., f
1
(x
2
) = f
2
(x
2
), and f
n-2
(x
n-1
) = f
n-1
(x
n-1
). This condition ensures
continuity of the fitting function and all its derivatives not only at the interior points
but also at the extreme points, making the fitting function, in fact, periodic.
SCILAB functions for cubic splines
SCILAB provides functions splin and interp to obtain the spline fitting of a set of data and to
interpolate data after the spline fitting is completed. Function splin requires as input data
vectors x and y, and returns a vector of derivatives of the fitting function d. The general form
of the function call is
d=splin(x,y [,"periodic"]
The optional argument periodic is used when a periodic spline function is sought.
Function interp is used after function splin has been invoked to produce values of the fitted
function y, as well as values of the first, second, and third derivatives, if needed, for a vector
of values xd. The general call to the function is:
[y0 [,y1 [,y2 [,y3]]]]=interp(xd,x,y,d)
Notice that all the terms in the left-hand side of the call are optional. These terms represent
the data fitting (y0), first derivatives (y1), second derivatives (y2), and third derivatives (y3).
An example of application of functions splin and interp is shown next using the data in the
following table:
x 0.0 1.2 3.5 4.2 6.2 8.1 11.2
y 15.0 29.0 13.3 -6.4 2.9 17.1 -8.0
First, we define the data vectors and use function splin to get the derivatives of the fitting
functions:
-->x = [0.0,1.2,3.5,4.2,6.2,8.1,11.2]
x =
! 0. 1.2 3.5 4.2 6.2 8.1 11.2 !
-->y = [15.0,29.0,13.3,-6.4,2.9,17.1,-8.0]
y =
! 15. 29. 13.3 - 6.4 2.9 17.1 - 8. !
-->//simple spline
-->d = splin(x,y)
d =
column 1 to 5
! 13.47637 8.3035909 - 27.66225 - 21.025873 13.960103 !
column 6 to 7
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
! 1.2037304 - 16.821038 !
To produce the curve representing the fitting spline curve we generate x data in the range
(0,11.2) and use function interp to generate the fitted data:
-->xx = [0:0.1:11.2];
-->[y0,y1,y2,y3] = interp(xx,x,y,d);
To produce the plot we need to know about the minimum and maximum values in the
plotting rectangle:
-->min(y),max(y)
ans = - 8.
ans = 29.
-->min(y0), max(y0)
ans = - 11.862191
ans = 32.468457
Based on the minimum and maximum values of the original data x,y and of the fitted data y0
we select the following plotting rectangle:
-->rect = [0 -12 12 40]
rect =
! 0. - 12. 12. 40. !
A plot of the fitted spline and the original data follows:
-->plot2d(xx,y0,9,'011','y',rect)
-->xset('mark',-9,1)
-->plot2d(x,y,-9,'010','y',rect)
-->xtitle('Cubic spline fitting','x','y')
If we want to fit a periodic spline to the original data we need to have the first and last values
in the y data set be the same:
-->y
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
y =
! 15. 29. 13.3 - 6.4 2.9 17.1 - 8. !
-->y(7) = y(1)
y =
! 15. 29. 13.3 - 6.4 2.9 17.1 15. !
The following SCILAB commands produce the spline fitting and the corresponding graph:
-->d = splin(x,y,'periodic')
d =
column 1 to 5
! 8.6838537 9.9142635 - 27.872184 - 20.968424 14.116738 !
column 6 to 7
! .5382762 8.6838537 !
-->xx = [0:0.1:11.2];
-->[y0,y1,y2,y3] = interp(xx,x,y,d);
-->plot2d(xx,y0,9,'011','y',rect)
-->xset('mark',-9,1)
-->plot2d(x,y,-9,'010','y',rect)
-->xtitle('Periodic cubic spline fitting','x','y')
The function smooth
Function smooth is used to generate a data set {(x
1
,ys
1
), (x
2
= x
1
+x,ys
2
), (x
2
= x
1
+2x,ys
3
), ,
(x
n
,ys
n
)} from a cubic spline fitting ys = f(x) based on the data set {(x
1
,y
1
), (x
2
,y
2
), , (x
n
,y
n
)}.
The general call to the function is
[xy_Fitted_Data]=smooth(xy_Original_Table [,step])
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
xy_Original_Table is a two-row matrix with the first row containing the values of x and the
second row containing the values of y from the original data set, step is the value of x that
generates a set of values of x for the fitted data. The x data generated consists of values x
1
+
x, x
2
+ x, , etc. If no value of step is provided, SCILAB uses a default value that generates
100 data points for x. The y data generated by function smooth follows a cubic spline fitting.
The (x,y) data generated by function smooth is returned in the form of a two-row matrix with
the first row corresponding to values of x and the second row corresponding to values of y.
The application of function smooth is equivalent to the application of functions splin and
interp for the range of values of x in the original data table. The following example shows
how to fit the following data through cubic splines by applying function smooth:
x 1.2 4.2 6.2 11.2 12.5 13.9 14.1 15.6 16.5 18.4
y 32.3 -4.2 13.6 -1.7 2.8 22.1 29.7 11.5 14.3 18.5
First, we enter the data for x and y:
-->x = [1.2,4.2,6.2,11.2,12.5,13.9,14.1,15.6,16.5,18.4];
-->y = [32.3,-4.2,13.6,-1.7,2.8,22.1,29.7,11.5,14.3,18.5];
Next, we put together the matrix xy_Original_Table as a two-row matrix:
-->xy_Original_Table = [x;y]
xy_Original_Table =
column 1 to 8
! 1.2 4.2 6.2 11.2 12.5 13.9 14.1 15.6 !
! 32.3 - 4.2 13.6 - 1.7 2.8 22.1 29.7 11.5 !
column 9 to 10
! 16.5 18.4 !
! 14.3 18.5 !
To generate the fitted data use:
-->xy_Fitted_Data = smooth(xy_Original_Table,0.1);
Next, we separate the rows of data into vectors xx and yy:
-->xx=xy_Fitted_Data(1,:); yy = xy_Fitted_Data(2,:);
To determine the plot rectangle to use, we get information on the minimum and maximum
values of xx and yy:
-->min(xx),max(xx),min(yy),max(yy)
ans = .2
ans = 18.4
ans = - 7.8229563
ans = 34.685766
-->rect = [0 -10 20 40]
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
rect = ! 0. - 10. 20. 40. !
The following commands plot the original and the fitted data in the same set of axes:
-->xset('mark',-9,1)
-->plot2d(x,y,-9,'011',' ',rect)
-->plot2d(xx,yy,1,'010',' ',rect)
-->xtitle('Spline through use of function smooth','x','y')
Obtaining the spline polynomials
The user-defined function splinepol, listed below, can be used to generate the n-1 cubic
polynomials that constitute the spline fitting for a data set {(x
1
,y
1
), (x
2
,y
2
),,(x
n
,y
n
)}. The
function requires as input the vectors x and y containing the data set, as well as the vector of
derivatives d obtained from function splin, i.e., d = splin(x,y) or d = splin(x,y,periodic). Also,
the user must provide a polynomial variable (var), say x,s, etc., to generate the
polynomials.
The general call to this function is
[ps] = splinepol(x,y,d,var)
A listing of the function is provided next:
function [ps] = splinepol(x,y,d,var)
//Produces the (n-1) cubic splines that fit the
//data set [(x1,y1),(x2,y2),...,(xn,yn)] of size n.
//Input to the function consists of vectors x, y, d
//and polynomial variable var, where
// x = [x1,x2,...,xn], y = [y1,y2,...,yn].
//Vector d results from the application of function
//splin: d = splin(x,y) or d = splin(x,y,'periodic')
[y0,y1,y2,y3] = interp(x,x,y,d);
n = length(x);
yy0=y0(2:n);yy1=y1(2:n);yy2=y2(2:n);yy3=y3(2:n);
xx = x(2:n);
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
a = zeros(n-1,4);
a(:,4) = yy3./6;
a(:,3) = (yy2-6.*a(:,4).*xx')./2;
a(:,2) = yy1-2.*a(:,3).*xx'-3.*a(:,4).*xx'.^2;
a(:,1) = yy0'-a(:,2).*xx'-a(:,3).*xx'.^2-a(:,4).*xx'.^3;
ps = zeros(n-1,1);
for j = 1:n-1
ps(j) = poly(a(j,:),var,'coeff');
end;
//end of function splinepol
To accompany function splinepol we also create the following function, intersplin, to
interpolate data out of the polynomials ps generated with splinepol. The general call to
function intersplin is
[yy] = intersplin(xx,x,ps)
where xx is a vector containing the values of x for the interpolation, x is a vector of n elements
representing the original x data used in generating the n-1 cubic polynomials listed in vector
ps. The function returns values of y in vector yy of length n.
A listing of function intersplin follows:
function [yy] = intersplin(xx,x,ps)
//interpolates spline polynomials in the range given
//by vector xx. The spline polynomials are
//passed to the function as a column vector
//of polynomials (as generated by function
//splinepol). Vector x represents the original
//x data from which the polynomials were generated.
n = length(x);
m = length(xx);
yy = [];
for i = 1:m
if xx(i) <= x(1) | xx(i) >= x(n) then
yy = [yy 0];
else
for j = 1:n-1
if xx(i)>x(j) & xx(i)<=x(j+1) then
yy = [yy horner(ps(j),xx(i))];
end;
end;
end;
end;
The purpose of including functions splinpol is to provide a way to list the cubic polynomials for
a given spline data fitting. The purpose of function intersplin is to be able to interpolate
those polynomials to check the data fitting. The results out of function intersplin are not
different of those generated with SCILAB function interp, as will be demonstrated in the
following examples.
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
In the examples that follow, we take vectors x and y, of the same length, and generate the
vector of derivatives d from function splin. Next, we interpolate data for y (vector yy) using
function interp given a vector xx. We also use function splinpol to generate the cubic
polynomials corresponding to the spline fitting, and interpolate values of y (vector yyy) using
function intersplin. Finally, we produce a joint plot of the original data (circles), the data
from interp (continuous line), and the data from intersplin (crosses), to verify that the results
provided by the polynomials generated with splinpol coincide with those generated through
SCILAB functions interp. Two examples are presented, one for a non-periodic spline and one
for a periodic spline.
First, we load functions splinepol and intersplin:
-->getf('splinepol')
-->getf('intersplin')
The (x,y) data used for a non-periodic spline fitting are generated with
-->x = [0.5:0.5:5]
x = ! .5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. !
-->y = [0.2 1.4 2.3 1.0 0.4 -0.6 -1.0 -0.5 0.2 1.2]
y = ! .2 1.4 2.3 1. .4 - .6 - 1. - .5 .2 1.2 !
Next, we produce the spline fitting and generate information for the plot:
-->d = splin(x,y); //vector of derivatives
-->xx = [0:0.1:5]; //values of x for interpolation
-->yy = interp(xx,x,y,d); //values of y from interp
-->ps = splinepol(x,y,d,'x') //cubic spline polynomials
ps =
! 2 3 !
! 2.2156991 - 9.5908966x + 13.462796x - 4.6875988x !
! !
! 2 3 !
! 2.2156991 - 9.5908966x + 13.462796x - 4.6875988x !
! !
! 2 3 !
! - 41.408176 + 77.656853x - 44.70237x + 8.2379938x !
! !
! 2 3 !
! 65.010787 - 81.971591x + 35.111852x - 5.0643765x !
! !
! 2 3 !
! - 64.424974 + 73.351323x - 27.017314x + 3.2195122x !
! !
! 2 3 !
! 17.471007 - 8.5446582x + .2813466x + .1863277x !
! !
! 2 3 !
! 92.551597 - 72.89945x + 18.66843x - 1.5648231x !
! !
! 2 3 !
! - 37.866816 + 24.914359x - 5.7850223x + .4729646x !
! !
! 2 3 !
! - 37.866816 + 24.914359x - 5.7850223x + .4729646x !
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
-->yyy = intersplin(xx,x,ps); //interpolating using polynomials
-->min([y yy yyy]), max([y yy yyy]) //determining values for plot
ans = - 1.0050396
ans = 2.3127536
-->rect = [0 -2 5 4]; //define plot rectangle
The next SCILAB commands produce the required plot:
-->plot2d(xx,yy,1,'011',' ',rect)
-->xset('mark',-9,1)
-->plot2d(x,y,-9,'011',' ',rect)
-->plot2d(xx,yyy,-1,'011',' ',rect)
-->xtitle('Spline fittin -- interp + interspline','x','y')
Next, we modify the data in y to produce a periodic spline fitting:
-->x
x = ! .5 1. 1.5 2. 2.5 3. 3.5 4. 4.5 5. !
-->y
y = ! .2 1.4 2.3 1. .4 - .6 - 1. - .5 .2 1.2 !
Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz
-->y(10) = y(1)
y = ! .2 1.4 2.3 1. .4 - .6 - 1. - .5 .2 .2 !
Next, we generate the vector of derivatives using splin, and interpolate data using interp,
splinepol, and intersplin:
-->d = splin(x,y,'periodic');
-->xx = [0:0.1:5];
-->yy = interp(xx,x,y,d);
-->ps = splinepol(x,y,d,'x')
ps =
! 2 3 !
! 1.2431373 - 5.8509804x + 9.0509804x - 3.0431373x !
! !
! 2 3 !
! 3.3294118 - 12.109804x + 15.309804x - 5.1294118x !
! !
! 2 3 !
! - 42.2 + 78.94902x - 45.396078x + 8.3607843x !
! !
! 2 3 !
! 65.596078 - 82.745098x + 35.45098x - 5.1137255x !
! !
! 2 3 !
! - 65.776471 + 74.901961x - 27.607843x + 3.2941176x !
! !
! 2 3 !
! 24.858824 - 15.733333x + 2.6039216x - .0627451x !
! !
! 2 3 !
! 49.743137 - 37.062745x + 8.6980392x - .6431373x !
! !
! 2 3 !
! 198.32353 - 148.49804x + 36.556863x - 2.9647059x !
! !
! 2 3 !
! - 554.97647 + 353.70196x - 75.043137x + 5.3019608x !
-->yyy = intersplin(xx,x,ps);
The following SCILAB commands produce the comparative plot:
-->plot2d(xx,yy,1,'011',' ',rect)
-->xset('mark',-9,1)
-->plot2d(x,y,-9,'011',' ',rect)
-->plot2d(xx,yyy,-1,'011',' ',rect)
-->xtitle('Spline fitting/periodic function -- interp + interspline','x','y')
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
Multi-variate data fitting
In this section we consider interpolation and fitting of the data in a table of values (x
i
,y
j
,z
ij
),
for i=1,2,,n; j=1,2,,m, through a function z = f(x,y). A typical table will look as follows:
x \ y y
1
y
2
y
j
y
m
x
1
z
11
z
12
z
1j
z
1m
x
2
z
21
z
22
z
2j
z
2m
: : : : : :
x
i
z
i1
z
i2
z
ij
z
im
: : : : : :
x
n
z
n1
z
n2
z
nj
z
nm
Successive univariate polynomial interpolation
By successive univariate polynomial interpolation we mean to interpolate data by columns of
the table first, and then by rows using a univariate polynomial in each direction. For example,
we can select polynomials of order k to fit data for the columns of the table, i.e.,
ZC
j
(x) = a
0j
+ a
1j
x

+ a
2j
x
2

+ + a
kj
x
k
,
for j = 1,2,,m. Quadratic polynomials to fit data for the rows of the table would have the
form
ZR
j
(y) = a
i0
+ a
i1
y

+ a
i2
y
2
+ + a
ik
y
k
,
for i = 1,2,,n.
If we are interested in interpolating the value of the function z = f(x,y) for x = x
r
and y = y
s
, we
can start by first generating the polynomial fittings ZC
j
(x) = a
0j
+ a
1j
x

+ a
2j
x
2

+ + a
kj
x
k
, for j =
1,2,,m. From these fittings we can get the data set {zc
1
, zc
2
, ,zc
m
}, where zc
j
= ZC
j
(x
r
).
Next, we attempt a fitting of the form ZR(y) = = a
i0
+ a
i1
y

+ a
i2
y
2
+ + a
ik
y
k
, using the data set
{(y
1
,zc
1
), (y
2
,zc
2
),,(y
m
,zc
m
)}. The value we are searching for is z = f(x
r
,y
s
) = ZR(y
s
).
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
Consider, for example, the following table of values z = f(x,y):
x \ y 12.00 13.00 14.00
2.00 71.09 76.94 81.70
3.00 77.07 82.13 87.34
4.00 85.22 91.63 96.67
Suppose that we want to interpolate the value of z for x = 2.5, y = 13.2, using quadratic
polynomials in both directions. We start by loading the vectors x and y and the table of values
z:
-->x = [2,3,4];y=[12,13,14];
-->z = [71.09,76.94,81.70;77.07,82.13,87.34;85.22,91.63,96.67];
A plot of the z data can be obtained by using:
-->plot3d(x,y,z)
The first step in the successive univariate polynomial fitting requires using function dfp (direct
polynomial fitting) to fit quadratic equations by columns. The polynomials corresponding to
rows 1, 2, and 3 are called p1(x), p2(x), and p3(x), respectively. The coefficients of each of
these polynomials results from the use of function dfp for the data in vector x (passed on to
the function as a column vector, i.e., x) and the data in the corresponding columns of the
table, i.e., z(:,1), z(:,2), and z(:,3).
-->[a1] = dfp(x',z(:,1)), p1 = poly(a1,'xx','coeff')
a1 =
! 65.64 .555 1.085 !
p1 =
2
65.64 + .555xx + 1.085xx
-->[a2] = dfp(x',z(:,2)), p2 = poly(a2,'xx','coeff')
a2 =
! 79.49 - 5.585 2.155 !
p2 =
2
79.49 - 5.585xx + 2.155xx
-->[a3] = dfp(x',z(:,3)), p3 = poly(a3,'xx','coeff')
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
a3 =
! 81.49 - 3.585 1.845 !
p3 =
2
81.49 - 3.585xx + 1.845xx
After obtaining the polynomial fittings per column, we proceed to generate the data zc
corresponding to x = 2.5, by using:
-->zc(1)=horner(p1,2.5);zc(2)=horner(p2,2.5);zc(3)=horner(p3,2.5); zc
zc =
! 73.80875 78.99625 84.05875 !
These data are next fit to a quadratic polynomial on y:
-->b = dfp(y',zc'), zz = poly(b,'yy','coeff')
b =
! 1.80875 6.75 - .0625 !
zz =
2
1.80875 + 6.75yy - .0625yy
The evaluation of the resulting polynomial, zz(y), for y = 13.2, will produce the required
interpolation:
-->zfit = horner(zz,13.2)
zfit =
80.01875
The interpolation performed above can be repeated by first fitting data by rows, i.e.,
generating polynomials p1(y), p2(y), and p3(y) corresponding to the rows of the table. A data
set zc is then generated by evaluating these polynomials for y = 13.2. Fitting data to the data
sets x and zc produces a polynomial zz(x) which is then evaluated at x = 2.5 to obtain the
desired result. The corresponding SCILAB commands are shown next:
-->x = [2,3,4];y=[12,13,14];
-->z = [71.09,76.94,81.70;77.07,82.13,87.34;85.22,91.63,96.67];
-->[b1] = dfp(y',z(1,:)'), p1 = poly(b1,'yy','coeff')
b1 =
! - 84.13 19.475 - .545 !
p1 =
2
- 84.13 + 19.475yy - .545yy
-->[b2] = dfp(y',z(2,:)'), p2 = poly(b2,'yy','coeff')
b2 =
! 28.05 3.185 .075 !
p2 =
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
2
28.05 + 3.185yy + .075yy
-->[b3] = dfp(y',z(3,:)'), p3 = poly(b3,'yy','coeff')
b3 =
! - 98.56 23.535 - .685 !
p3 =
2
- 98.56 + 23.535yy - .685yy
-->zc(1)=horner(p1,13.2);zc(2)=horner(p2,13.2);zc(3)=horner(p3,13.);zc
zc =
! 77.9792 83.16 91.63 !
-->a = dfp(x',zc'), zz = poly(a,'xx','coeff')
a =
! 77.4852 - 3.0422 1.6446 !
zz =
2
77.4852 - 3.0422xx + 1.6446xx
-->z_fit = horner(zz,2.5)
z_fit =
80.15845
The interpolation results obtained through the two approaches shown above are z = 80.01875
and z = 80.15845. Although they are very close to each other, they are not exactly the same as
expected. You may report as the final interpolation the average of these two results, namely,
z = (80.01875 +80.15845)/2 = 80.0886.
Notes:
[1] Function dfp requires that the vectors of data used in the direct polynomial fitting be
passed to the function as column vectors.
[2] The degree of the polynomial whose coefficients are returned by function dfp is n-1 where
n is the length of the vectors x or y passed to the function. Therefore, the 3x3 table of data
used in the examples above had the right size for quadratic polynomial fittings.
[3] Polynomial fittings for any polynomial order can be obtained if we use function polyfit
instead of dfp.
Direct multivariate polynomial fitting
Direct multivariate polynomial fitting is a generalization of the direct (univariate) polynomial
fitting presented earlier in the chapter. We will use a very general multivariate polynomial
function of degree n given by:
z = f(x,y) = a
0
+ a
1
x + a
2
y + a
3
x
2
+ a
4
xy + a
5
y
2
+ a
6
x
3
+ a
7
x
2
y + a
8
xy
2
+ a
9
y
3
+
The degree of the polynomial for direct fitting is determined by the number of data values
available. For example, for the table used in the previous example:
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
x \ y 12.00 13.00 14.00
2.00 71.09 76.94 81.70
3.00 77.07 82.13 87.34
4.00 85.22 91.63 96.67
which contains nine entries, we can, in principle, fit a function with nine coefficients. One
possibility is to use:
z = f(x,y) = a
0
+ a
1
x + a
2
y + a
3
x
2
+ a
4
xy + a
5
y
2
+ a
6
x
3
+ a
7
x
2
y + a
9
y
3
However, the matrix of data for the linear system that results from replacing values of x and y
in the equation, is an ill-conditioned matrix (i.e., a singular matrix or a matrix close to
singularity). Thus, direct multivariate polynomial fitting fails for this case.
One alternative is to try to fit a simpler function, say,
z = f(x,y) = a
0
+ a
1
x + a
2
y + a
3
xy ,
using only four data points from the table. Suppose that we use the following data points:
1. x = 2, y = 12, z = 71.09, a
0
+ 2a
1
+ 12a
2
+ 24a
3
= 71.09
2. x = 3, y = 12, z = 85.22, a
0
+ 3a
1
+ 12a
2
+ 36a
3
= 77.07
3. x = 2, y = 14, z = 81.70, a
0
+ 2a
1
+ 14a
2
+ 28a
3
= 81.70
4. x = 4, y = 14, z = 96.67, a
0
+ 4a
1
+ 14a
2
+ 56a
3
= 71.09
The solution to the linear system that results is obtained using SCILAB as follows:
-->A = [1,2,12,24;1,3,12,36;1,2,14,28;1,4,14,56] //Matrix of coefficients
A =
! 1. 2. 12. 24. !
! 1. 3. 12. 36. !
! 1. 2. 14. 28. !
! 1. 4. 14. 56. !
-->det(A) //Verifying that the matrix is not singular
ans = 8.
-->b = [71.09;77.07;81.70;71.09]
b =
! 71.09 !
! 77.07 !
! 81.7 !
! 71.09 !
-->a = A\b //Function coefficients
a =
! - 139.95 !
! 73.69 !
! 16.59 !
! - 5.6425 !
-->deff('[z]=f(x,y)','z=a(1)+a(2)*x+a(3)*y+a(4)*x*y') //Defining function
-->[f(2,12),f(3,12),f(2,14),f(4,14)]' //Verifying solution
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
ans =
! 71.09 !
! 77.07 !
! 81.7 !
! 71.09 !
Least-square multivariate polynomial fitting
The method of least-squares presented earlier for univariate polynomials can be generalized
for a multivariate polynomial fitting. For example, a simple multivariate linear fitting given
by
z = a
0
+ a
1
x + a
2
y,
produces a sum-of-squared-errors given by
. ) ( ) , , (
1
2
2 1 0 2 1 0


n
i
i
y a x a a z a a a SSE
To minimize function SSE, we set up the equations (SSE)/a
0
= 0, (SSE)/a
1
= 0, and
(SSE)/a
2
= 0, which result in the set of equations:
a
0
n + a
1
x
i
+ a
2
y
i
= z
i
a
0
x
i
+ a
1
x
i
2
+ a
2
x
i
y
i
= x
i
z
i
a
0
y
i
+ a
1
x
i
y
i
+ a
2
y
i
2
= y
i
z
i
The solution to the resulting linear system provides the coefficients of the function, a
0
, a
1
, and
a
2
.
For example, to fit the data in the bivariate table presented earlier, namely,
x \ y 12.00 13.00 14.00
2.00 71.09 76.94 81.70
3.00 77.07 82.13 87.34
4.00 85.22 91.63 96.67
to the proposed bivariate function, z = a
0
+ a
1
x + a
2
y, using SCILAB, we proceed as follows:
First, we define the x,y, and z data:
-->x = [2,3,4]; y = [12,13,14];
-->z = [71.09,76.94,81.70;77.07,82.13,87.34;85.22,91.63,96.67];
To calculate the summations in the set of linear equations presented above, we need to create
matrices of values of x and y corresponding to the entries in the bivariate table. These
matrices, called xx and yy, respectively, are determined as follows:
-->xx = [x',x',x']
xx =
! 2. 2. 2. !
! 3. 3. 3. !
! 4. 4. 4. !
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
-->yy = [y;y;y]
yy =
! 12. 13. 14. !
! 12. 13. 14. !
! 12. 13. 14. !
The next step is to produce the matrix of coefficients for the system:
-->A = zeros(3,3)
A =
! 0. 0. 0. !
! 0. 0. 0. !
! 0. 0. 0. !
-->A(1,1) = n; A(1,2) = sum(xx); A(1,3) = sum(yy);
-->A(2,1) = sum(xx); A(2,2)=sum(xx^2); A(2,3) = sum(xx.*yy);
-->A(3,1) = sum(yy); A(3,2)=sum(xx.*yy); A(3,3) = sum(yy^2);
-->A
A =
! 9. 27. 117. !
! 27. 243. 351. !
! 117. 351. 4563. !
The vector of right-hand side terms in the system of equations is:
-->b = [sum(z);sum(xx.*z);sum(yy.*z)]
b =
! 749.79 !
! 2293.16 !
! 9779.6 !
Before solving the linear system we check that the matrix is not singular:
-->det(A)
ans = 4435236.
The coefficients of the function are obtained by using left division:
-->a = A\b
a =
! 82.360912 !
! .2703086 !
! .0106279 !
The following is the definition of the function:
-->deff('[z]=f(x,y)','z=a(1)+a(2)*x+a(2)*y')
To evaluate the function in a grid we can use function feval as follows:
-->feval(x,y,f)
Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
ans =
! 86.145233 86.415541 86.68585 !
! 86.415541 86.68585 86.956159 !
! 86.68585 86.956159 87.226467 !
Compared with the original values of z we find that the simple linear fitting does not
reproduces those original values very well.
-->z
z =
! 71.09 76.94 81.7 !
! 77.07 82.13 87.34 !
! 85.22 91.63 96.67 !
Bivariate quadratic fitting
To improve the fitting we may want to try using a bivariate quadratic fitting given by the
expression
z = a
0
+ a
1
x + a
2
y + a
3
x
2
+ a
4
xy

+ a
5
y
2
,
whose sum of squared errors is given by
. ) ( ) , , (
1
2 2
5 4
2
3 2 1 0 2 1 0


n
i
i
y a xy a x a y a x a a z a a a SSE
To minimize the function SSE, we set up the equations: (SSE)/a
0
= 0, (SSE)/a
1
= 0,
(SSE)/a
2
= 0, (SSE)/a
3
= 0, (SSE)/a
4
= 0, and (SSE)/a
5
= , which result in the system:
a
0
n + a
1
x
i
+ a
2
y
i
+ a
3
x
i
2
+ a
4
y
i
2
+ a
5
x
i
y
i
= z
i
a
0
x
i
+ a
1
x
i
2
+ a
2
x
i
y
i
+ a
3
x
i
3
+ a
4
x
i
y
i
2
+ a
5
x
i
2
y
i
= x
i
z
i
a
0
y
i
+ a
1
x
i
y
i
+ a
2
y
i
2
+ a
3
x
i
2
y
i
+ a
4
y
i
3
+ a
5
x
i
y
i
2
= y
i
z
i
a
0
x
i
2
+ a
1
x
i
3
+ a
2
x
i
2
y
i
+ a
3
x
i
4
+ a
4
x
i
2
y
i
2
+ a
5
x
i
3
y
i
= x
i
2
z
i
a
0
y
i
2
+ a
1
x
i
y
i
2
+ a
2
y
i
3
+ a
3
x
i
2
y
i
2
+ a
4
y
i
4
+ a
5
x
i
y
i
3
= y
i
2
z
i
a
0
x
i
y
i
+ a
1
x
i
2
y
i
+ a
2
x
i
y
i
2
+ a
3
x
i
3
y
i
+ a
4
x
i
y
i
3
+ a
5
x
i
2
y
i
2
= x
i
y
i
z
i
We will use the data from the table presented above to generate the coefficients for this
matrix. Considering the number of evaluations involved we are going to prepare a function,
multiquad, to produce the coefficients of the function:
function [a] = multiquad(xyzMatrix)
//produces the coefficients a = [a1 a2 a3 ... a6]
//from the function
// f(x,y) = a1+a2*x+a3*y+a4*x^2+a5*x*y+a6*y^3
//xyzMatrix = [x(col1) y(col2) z(col3)]
xx = xyzMatrix(:,1);
yy = xyzMatrix(:,2);
zz = xyzMatrix(:,3);
n = length(xx);
A=zeros(6,6);
A(1,1)=n; A(1,2)=sum(xx);A(1,3)=sum(yy);A(1,4)=sum(xx^2);
Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz
A(1,5)=sum(yy^2);A(1,6)=sum(xx.*yy);
A(2,1)=A(1,2);A(2,2)=sum(xx^2);A(2,3)=sum(xx.*yy);
A(2,4)=sum(xx^3);A(2,5)=sum(xx.*(yy^2));A(2,6)=sum((xx^2).*yy);
A(3,1)=A(1,3);A(3,2)=A(2,3);A(3,3)=sum(yy^2);
A(3,4)=sum((xx^2).*yy);A(3,5)=sum(yy^3);A(3,6)=sum(xx.*(yy^2));
A(4,1)=A(1,4);A(4,2)=A(2,4);A(4,3)=A(3,4);
A(4,4)=sum(xx^4);A(4,5)=sum((xx^2).*(yy^2
A(5,4)=A(4,5);A(5,5)=sum(yy^4);A(5,6)=sum(xx.*(yy^3));
A(6,1)=A(1,6);A(6,2)=A(2,6);A(6,3)=A(3,6);
A(6,4)=A(4,6);A(6,5)=A(5,6);A(6,6)=sum((xx^2).*(yy^2));
b=[sum(zz);sum(xx.*zz);sum(yy.*zz);sum((xx^2).*zz);sum((yy^2).*zz);sum(xx.*yy.*
zz)];
a=A\b;
disp('Coefficients in the function z = f(x,y)')
disp('z=a(1)+a(2)*x+a(3)*y+a(4)*x^2+a(5)*x*y+a(6)*y^2')
//end function multiquad
The function requires as input a matrix of three columns where the columns correspond to
values of x, y, and z.
In the following example, we use function multiquad to produce a fitting of the form
z = a
0
+ a
1
x + a
2
y + a
3
x
2
+ a
4
xy

+ a
5
y
2
,
for the data in the table below:
x \ y 700 800 900
9000 0.031980 0.037948 0.043675
10000 0.028345 0.033827 0.039053
11000 0.025360 0.030452 0.035270
First, we prepare the matrix of data:
-->x = [9000,10000,11000]; y = [700,800,900];
-->z = [.031980,.037948,.4675;.028345,.033827,.039053;.025360,.030452,.035270]
z =
! .03198 .037948 .4675 !
! .028345 .033827 .039053 !
! .02536 .030452 .03527 !
-->xx = [x';x';x']; yy = [y(1) y(1) y(1) y(2) y(2) y(2) y(3) y(3) y(3)]';
-->zz = [z(:,1);z(:,2);z(:,3)];
-->M = [xx yy zz]
M =
! 900. 700. .03198 !
! 1000. 700. .028345 !
! 1100. 700. .02536 !
! 900. 800. .037948 !
! 1000. 800. .033827 !
! 1100. 800. .030452 !
! 900. 900. .4675 !
! 1000. 900. .039053 !
! 1100. 900. .03527 !
Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
To obtain the coefficients of the data fitting, we use:
-->a = multiquad(M)
warning
matrix is close to singular or badly scaled.
results may be inaccurate. rcond = 1.1496E-21
Coefficients in the function z = f(x,y)
z=a(1)+a(2)*x+a(3)*y+a(4)*x^2+a(5)*x*y+a(6)*y^2
a =
! 3.2238377 !
! - .0006434 !
! .0001190 !
! 7.101E-08 !
! .0000071 !
! - .0000011 !
To check how well the fitting reproduces the data we use:
-->deff('[z]=f(x,y)','z=a(1)+a(2)*x+a(3)*y+a(4)*x^2+a(5)*x*y+a(6)*y^2')
-->feval(x,y,f)
ans =
! 47.167934 53.366045 59.542874 !
! 52.809383 59.712584 66.594503 !
! 58.592852 66.201143 73.788152 !
Obviously, the proposed data fitting does not work very well with this data set. The reader
should be advised that there is no guarantee that a given function will fit a particular data set.
The purpose of using the least-square method is to minimize the sum of square errors, SSE,
which is a measure of the overall error involved in the data fitting. However, this procedure
does not ensure that the fitting will be acceptable. There are statistical inference techniques
that can be used as guidelines for the goodness of fit of a particular function to a given data
set. These techniques are covered in a separate chapter.
SCILAB function datafit
Function datafit is used for fitting data to a model by defining an error function e = G(p,z)
where p is a column vector of m rows representing the parameters of the model, and z is a
column vector of n rows representing the variables involved in the model. Function datafit
finds a solution to the set of k equations
e
i
= G(p,z
i
) = 0,
by minimizing the functional
G
T
(p,z
1
)WG(p,z
1
) + G
T
(p,z
2
)WG(p,z
2
) + + G
T
(p,z
n
)WG(p,z
n
),
Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
where z
i
, i = 1,2, , n, are specific values of the vector of variables z, and W is a kk weight
matrix (default value W = I
kxk
, the identity matrix).
The simplest call to function datafit is
[p,err]=datafit(G,Z,p0)
where G is the name of the error function G(p,z), Z is a matrix whose rows consists of the
different vectors of variables, i.e., Z = [z
1
; z
2
; ;z
n
], and p0 is a column vector representing
initial guesses of the parameters p sought.
Example 1 - quadratic polynomial
As an example, we will generate data using the function y = a
1
+ a
2
x + a
3
x
2
+ 100r, where a
1
=
20, a
2
= 30, a
3
= 50, and r is a random component to be generated in SCILAB through the
expression r = 100*(rand()-0.5). Because function rand generates uniform random numbers
in the range (0,1), the values of r will be random numbers uniformly distributed in the range (-
50,50). Thus, the data generated for y is based on the quadratic function a
1
+ a
2
x + a
3
x
2
, but
modified by a random number in the range (-50,50). We will generate data for x in the range
(0,10) with increments of 0.1, and calculate the corresponding values of y. Then, the data will
be placed in a matrix Z with values of x in the first row and values of y in the second row. This
matrix Z will be used in the call to function datafit.
The function to fit will be defined as G(a,z) = z(2) - a(1)-a(2)*z(1)-a(3)*z(1)^2, and
we will provide an initial value for the vector of parameters a as a0 = [10;20;30] for the call to
function datafit.
The following SCILAB commands will take care of generating the data and finding the
parameters a that produce the best fitting. The process by which the best fitting for the
model is obtained is an iterative procedure and may take some time to produce a result.
Therefore, be patient if SCILAB does not produce a result right away. Depending on the
complexity of the function to be fitted or on the number data points involved, datafit may take
up to 10 minutes to return a result.
For this example we start by defining the values of x and y that we want to fit. These are
stored in vectors X and Y, which then get put together into matrix Z:
-->X = [0:0.1:10]; Y=20+30.*X+50.*X^2+100*(rand()-0.5); Z = [X;Y];
Next, we define a function G(a,z) = y - a
1
- a
2
x - a
3
x
2
, that will be used to obtain the
parameters a = [a
1
a
2
a
3
] that define the expression y = a
1
+ a
2
x + a
3
x
2
, with z = [x y]
T
.
-->deff('[e]=G(a,z)','e=z(2)-a(1)-a(2)*z(1)-a(3)*z(1)^2')
Initial values of the parameters a are given by:
-->a0 = [10;20;30];
Application of function datafit produces, after a few minutes, the following results for the
coefficients a
i
:
-->[aa,er] = datafit(G,Z,a0)
er =
3.161E-10
Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz
aa =
! 20.644345 !
! 30.000002 !
! 50. !
To verify the fitting of the data we define function f(x), and produce data YY = f(X) to plot the
original and fitted data:
-->deff('[yy]=f(x)','yy=aa(1)+aa(2)*x+aa(3)*x^2')
-->YY = f(X);
-->min([Y YY]), max([Y YY])
ans =
20.644345
ans =
5320.6443
-->rect = [0 0 10 5500]
rect =
! 0. 0. 10. 5500. !
-->xset('mark',-9,1)
-->plot2d(X,YY,1,'011',' ',rect)
-->plot2d(X,Y,-9,'011',' ',rect)
-->xtitle('datafit for y = a1+a2*x+a3*x^3','x','y')
Example 2 - cubic polynomial
Suppose that we want to fit the data to a cubic polynomial, i.e., y = a
1
+ a
2
x + a
3
x
2

+ a
4
x
3
, we
can use the following SCILAB commands to obtain the coefficients:
-->deff('[e]=G(a,z)','e=z(2)-a(1)-a(2)*z(1)-a(3)*z(1)^2-a(4)*z(1)^3')
Warning :redefining function: G
-->a0 = [10;20;30;10];
Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
-->[aa,er] = datafit(G,Z,a0)
er =
2.012E-07
aa =
! 20.644456 !
! 29.999865 !
! 50.000034 !
! - .0000023 !
The results indicates that the last coefficient a
4


0, thus, confirming that the quadratic fitting
is sufficient to reproduce the data. Notice also that the error is relatively small, er =
2.01210
-7
, indicating a very good fitting of the data. Such a good fitting is to be expected
since the data was originated from a quadratic function
Example 3 - exponential function
In this example we try to fit an exponential function of the form y = f(x) = a
1
+ a
2
exp(a
3
x) to
data generated out of a quadratic function. The data is obtained as follows:
-->X=[0:0.1:5];Y = 2.3+5.8.*X+3.2.*X^2;Z=[X;Y];
The next two function definitions are used to specify functions G(a,z) = y - a
1
+ a
2
exp(a
3
x),
with a = [a
1
;a
2
;a
3
], z = [x;y], and a vector of derivatives corresponding to s = [G/a
1
, G/a
2
,
G/a
3
] = [-1, -exp(a
3
x),-a
2
xexp(a
3
x)]:
-->deff('[e]=G(a,z)','e=z(2)-a(1)-a(2).*exp(a(3).*z(1))')
-->deff('[s]=DG(a,z)','s=[-1,-exp(a(3).*z(1)),-a(2).*z(1).*exp(a(3).*z(1))]')
Using this vector of derivatives, the call to function datafit is now:
-->[aa,er] = datafit(G,DG,Z,a0)
er =
2606.3157
aa =
! 5. !
! 4.7859729 !
! .6494727 !
To verify the fitting we define the function y = a
1
+ a
2
exp(a
3
x), and obtain data points
corresponding to the values of x stored in X:
-->deff('[y]=f(x)','y=aa(1)+aa(2)*exp(aa(3)*x)')
-->YY = f(X);
A plot of the original data (symbols) along with the fitted data (continuous line) is obtained as
follows:
-->plot2d(X,YY,1,'011',' ',rect)
-->plot2d(X,Y,-1,'011',' ',rect)
-->xtitle('Function data fit - y = a1+a2*exp(a3*x)','x','y')
Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz
Example 4 - fitting data to a function r = f(x,y)
In this example we will attempt fitting a data set (x,y,r) to a function of the form
r = a
1
+ a
2
x + a
3
y + a
4
x
2
+ a
5
xy

+ a
6
y
2
.
The function G(a,z), defined below, represents e = r - (a
1
+ a
2
x + a
3
y + a
4
x
2
+ a
5
xy

+ a
6
y
2
).
-->deff('[e]=G(a,z)',...
--> 'e=a(1)+a(2)*z(1)+a(3)*z(2)+a(4)*z(1)^2+a(5)*z(1)*z(2)+a(6)*z(2)^2-z(3)')
The data is generated from the function f
1
(x,y) = exp(0.1x)+exp(0.2y), as shown next:
--> xx = [0:0.1:10]; yy = [0:0.1:10];
-->n = length(X), m = length(Y)
n = 121.
m = 121.
The following commands creates vectors of data X, Y, and R, which are then put together into
matrix Z:
-->X = []; Y = []; R = [];
-->for i = 1:n
--> for j = 1:m
--> X = [X xx(i)]; Y = [Y yy(j)];
--> R = [R exp(0.1*xx(i))+exp(0.2*yy(j))];
--> end;
-->end;
-->Z = [X;Y;R];
The initial value of the function parameters is given (arbitrarily) as:
-->a0=10*ones(6,1)
a0 =
! 10. !
! 10. !
! 10. !
! 10. !
! 10. !
! 10. !
Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz
The following call to function datafit, which took about 5 minutes in produce a result, provides
the best fitting for the parameters. Notice that the overall error is relatively small.
-->[a,err]=datafit(G,Z,a0)
err =
1.0343445
a =
! 2.1356627 !
! .0856497 !
! .0204965 !
! .0084165 !
! - 6.695E-08 !
! .0590437 !
Next, we will attempt the same calculation using derivatives of the function G(a,z) with
respect to the parameters a
i
:
-->deff('[s]=DG(a,z)',...
-->'s=[1,z(1),z(2),z(1)^2,z(1)*z(2),z(2)^2]')
Using the derivatives the call to function datafit takes only about 1.5 minutes:
-->[a,err]=datafit(G,DG,Z,a0)
err =
1.0343445
a =
! 2.1356698 !
! .0856476 !
! .0204943 !
! .0084167 !
! 0. !
! .0590439 !
Notice the difference in value of a(5) in the two results. The next commands are used to
reproduce the original data and the fitted data as three-dimensional plots:
-->deff('[z]=ff(x,y)','z=exp(0.1.*x)+exp(0.2.*y)')
-->deff('[r]=f(x,y)',...
-->'r=a(1)+a(2).*x+a(3).*y+a(4).*x.^2+a(5).*x.*y+a(6).*y.^2')
-->zz = feval(xx,yy,ff); zzz = feval(xx,yy,f);
-->plot3d(xx,yy,zz)
-->xtitle('original function')
Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz
-->plot3d(xx,yy,zzz)
-->xtitle('fitted function')
Function datafit provides other options for calculation which are not presented in this book.
For more information use:
--> help datafit
Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz
Exercises
The following table shows values of the discharge Q(cfs) of a 1.75-ft long spillway as a function
of stage (i.e., water surface elevation) above spillway crest, h(ft) for a weir. The table also
shows the storage available in the reservoir, S(cu.ft), for the different stages.
h(ft) Q(cfs) S(cu.ft)
0.0 0.0 0
0.5 1.9 16641
1.0 5.4 33911
1.5 10.0 51246
2.0 15.3 69103
2.5 21.4 86901
3.0 28.2 104796
3.5 35.5 122772
4.0 43.4 140817
4.5 51.8 158924
5.0 60.7 177085
5.5 70.0 195296
6.0 79.7 213551
Use the data in this table to solve problems [1] through [14].
[1]. Use simple linear interpolation, through function interpln, to obtain the values of the
discharge Q for stage values of h = 0.25, 0.75, 1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75,
5.25, and 5.75 ft.
[2]. Use simple linear interpolation, through function interpln, to obtain the values of the
storage S for stage values of h = 0.25, 0.75, 1.25, 1.75, 2.25, 2.75, 3.25, 3.75, 4.25, 4.75, 5.25,
and 5.75 ft.
[3]. Solve problem [1] using Lagrange polynomials of order n = 2,3, and 4.
[4]. Solve problem [2] using Lagrange polynomials of order n = 2, 3, and 4.
[5]. Produce a forward difference table for the Q-h data.
[6]. Produce a forward difference table for the S-h data.
[7]. Solve problem [1] using a Newton forward-difference polynomial.
[8]. Solve problem [2] using a Newton forward-difference polynomial.
[9]. Solve problem [1] using a Newton backward-difference polynomial.
[10]. Solve problem [2] using a Newton backward-difference polynomial.
[11]. Solve problem [1] using a Sterling centered-difference polynomial.
Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz
[12]. Solve problem [2] using a Sterling centered-difference polynomial.
[13]. Solve problem [1] using a Bessel centered-difference polynomial.
[14]. Solve problem [2] using a Bessel centered-difference polynomial.
Use the following SCILAB polynomials in the solution of problems [15] through [21]:
p = x
3
+ 3x - 2, q = x+2, r = x
4
-x
3
-x
2
-x-1, s = (2x+2)
3
-(5x+3)
[15]. Calculate the following polynomial operations:
(a) p+q (b) q+r (c) r+s (d) p+s (e)p-q (f) q-2r
(g) p+q+r (h) p-2r+q (i) q+2p-r (j) pq (k)pqr (l) pq + rs
(m) 2pq-r
2
(n) (p+q)(r+s) (o) q
3
-x
3
(p) p/q (q)r/q (r) q+2/p
[16]. Determine the quotient and residual of the following polynomial divisions
(a) p/q (b) r/q (c) s/q (d) (p+2s)/q (e) (s+q
2
)/q (f) r/p
[17]. Obtain an expression for the polynomial long division defined by the following
expressions. Use 5 terms in the expansion.
(a) 1/p (b) 1/q (c) 1/r (d) 1/s (e) p/q (f) q/p
(g) p/s (h) s/p (i) r/q (j) q/r (k) s/p (l) q/p
2
[18]. Use function horner to evaluate the polynomials calculated in problem [15] at values of x
= 0, 2, 6, 10, and 20.
[19]. Determine the derivative of the polynomials obtained in problem [15] and evaluate those
derivatives at values of x = 0, 2, 6, 10, and 20.
[20]. Obtain the roots of the polynomials obtained in problem [15].
[21]. Use user-defined function intpoly to obtain the indefinite integrals of the polynomials
obtained in problem [15].
The following data shows the elevation of the bed surface in a flume, y(ft), against the
distance from one of the flume walls, x(ft). The data is to be used in problems [22] - [26].
x(ft) 0.5 1.0 1.5 2.0 2.5 3.0 3.5 4.0
y(ft) 0.23 0.45 0.48 0.40 0.31 0.40 0.59 0.37
[22]. Use the 8 data points to generate a direct-fit polynomial of order 7 that fits the data.
Plot the original data and the fitted polynomial.
[23]. Fit Lagrange polynomials of orders 2, 3, 4, 5, and 6 to the bed profile data. Plot the
original data and the fitted polynomials.
[24]. Produce a forward difference table out of the bed profile data.
Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz
[24]. Use (a) a Newton forward-difference polynomial; (b) a Newton backward-difference
polynomial; (c) a Stirling centered-difference polynomial; and (d) a Bessel centered-difference
polynomial to obtain the bed elevation at points x = 2.75 ft and x = 3.65 ft.
[25]. Fit polynomials of order 2, 3, 4, 5, and 6 for the bed profile data, using a least-square
method.
[26]. Use cubic spline curves to fit the bed profile data. (a) Plot the original data and the
fitted splines. (b) Produce the equations of the cubic spline curves that fit the data.
The following data shows the peak monthly concentration of a certain hydrocarbon component,
C(ppm), measured in a monitoring well throughout the past year.
t(month) 0 1 2 3 4 5 6 7 8 9 10 11
C(ppm) 450 420 150 180 430 620 610 520 320 106 110 190
Use these data in problems [27] through [32].
[27]. Use the 12 data points to generate a direct-fit polynomial of order 11 that fits the data.
Plot the original data and the fitted polynomial.
[28]. Fit Lagrange polynomials of orders 2, 3, 4, 5, and 6 to the concentration-vs-time data.
Plot the original data and the fitted polynomials.
[29]. Produce a forward difference table out of the concentration-vs-time data.
[30]. Use (a) a Newton forward-difference polynomial; (b) a Newton backward-difference
polynomial; (c) a Stirling centered-difference polynomial; and (d) a Bessel centered-difference
polynomial to estimate the hydrocarbon concentration at times t = 2.5 months and t = 6.2
months.
[31]. Fit polynomials of order 2, 3, 4, 5, and 6 for the concentration-vs-time data, using a
least-square method.
[32]. Use cubic spline curves to fit the concentration-vs-time data. (a) Plot the original data
and the fitted splines. (b) Produce the equations of the cubic spline curves that fit the data.
[33]. Develop a SCILAB function to produce successive univariate polynomial approximation for
a function z = f(x,y) based on nine data points as illustrated in the following table:
x\y y
k-1
y
k
y
k+1
x
m-1
z
m-1,k-1
z
m-1,k
z
m-1,k+1
x
m
z
m,k-1
z
m,k
z
m,k+1
x
m+1
z
m+1,k-1
z
m+1,k
z
m+1,k+1
The table entries are values of z
m,k
= f(x
m,
y
k
). The function should use quadratic polynomials to
obtain the value of z
0
= f(x
0
,y
0
), for x
m-1
< x
0
< x
m+1
, y
k-1
< y
0
< y
k+1
, along rows and then
columns, and vice versa. The function should return the values produced by the two
approaches for interpolation, as well as the average.
Download at InfoClearinghouse.com 68 2001 Gilberto E. Urroz
The following table represents the entropy, s(kJ/(kg K)), for superheated steam.
s(kJ /(kg K))
T(
o
C)\P(kPa) 20 40 60 80 101.33
100 8.126 7.801 7.608 7.470 7.607
150 8.367 8.045 7.855 7.719 7.828
200 8.584 8.262 8.074 7.939 8.028
250 8.780 8.460 8.271 8.131 8.210
300 8.961 8.641 8.454 8.320 8.380
350 9.130 8.810 8.622 8.489 8.538
400 9.288 8.968 8.780 8.647 8.687
Use this table for problems [33] through [].
[33]. Using successive univariate polynomial interpolation with quadratic equations calculate
the value of the superheated steam entropy for the following cases:
(a) T = 156
o
C, P = 42 kPa (b) T = 310
o
C, P = 63 kPa
(c) T = 116
o
C, P = 22 kPa (d) T = 156
o
C, P = 42 kPa
(e) T = 210
o
C, P = 92 kPa (f) T = 343
o
C, P = 25 kPa
(g) T = 107
o
C, P = 58 kPa (h) T = 280
o
C, P = 88 kPa
(i) T = 155
o
C, P = 42 kPa (j) T = 306
o
C, P = 21 kPa
[34]. Solve problem [33] by fitting the given data to a function of the form
s(T,P) = b
0
+ b
1
T + b
2
P.
[35]. Solve problem [33] by fitting the given data to a function of the form
s(T,P) = b
0
+ b
1
T + b
2
P + b
3
SP
[36]. Solve problem [33] by fitting the given data to a function of the form
s(T,P) = b
0
+ b
1
T + b
2
P + b
3
SP + b
4
S
2
+ b
5
P
2
.
[37]. Data from radiation counts of a radioactive material is shown in the table below.
t, months 0 2 4 5.5 6 9 10 14 18
N, counts/hour 1260 794 500 454 315 158 125 50 20
The equation for the radiation count as a function of time is N(t) = N
0
exp(-t). Using the data
in the table above determine the values of the parameters N
0
and , (a) using a simple linear
fitting of the linearized equation: ln(N) = -t + ln(N
0
); and (b) using SCILAB function datafit for
the original equation, N = N
0
exp(-t). (c) Plot the original data against the data generated
from the fittings in (a) and (b). (d) The half-life of the radioactive material is that value of
time t for which the mass (or radiation count) falls by one half, i.e., N = N
0
/2. Determine the
half-life for this material.
[38]. The table below shows the flow velocity, v(fps), and suspended sediment concentration,
C(mg/l), as functions of the distance from the channel bed, y(ft), measured at a cross-section
of a 800-ft-wide (b = 800 ft), 7.8-ft-deep (h = 7.8 ft) river cross-section that can be
approximated by a rectangle.
Download at InfoClearinghouse.com 69 2001 Gilberto E. Urroz
y(ft) v(ft/s) C(mg/l)
0.7 4.30 411
0.9 4.50 380
1.2 4.64 305
1.4 4.77 299
1.7 4.83 277
2.2 5.12 238
2.7 5.30 217
2.9 5.40 211
3.2 5.42 196
3.4 5.42 188
3.7 5.50 184
4.2 5.60 165
4.8 5.60 148
5.8 5.70 130
6.8 5.95 80
(a) Fit the velocity data, v(y), to a logarithmic function, i.e., v(y) = b
0
+ b
1
ln(y). Plot the
original data and the fitted data in the same set of axes.
(b) Fit the concentration data, C(y), to a function of the form ln(C) = b
0
+ b
1
ln((h-y)/y).
Plot the original data and the fitted data in the same set of axes.
[39]. For the data in problem [38], the flow discharge, Q(cfs), is defined by the integral


A
h
bdy y v dA y v Q
0
, ) ( ) (
with the mean flow velocity calculated as V = Q/A, where A = bh being the cross-sectional
area. The total flux of suspended sediment through the cross-section is defined as


A
h
s
bdy y C y v dA y C y v Q
0
, ) ( ) ( ) ( ) (
with the flux-averaged concentration given by C
f
= Q
s
/Q.
The terms used in the definitions above are illustrated in the figure below:
(a) Determine the flow discharge, mean velocity, sediment flux, and flux averaged
concentration for the data given using for the integration the functions for v(y) and
C(y) obtained in problem [38]. Compare the results with those obtained in problem
[94], Chapter 7.
Download at InfoClearinghouse.com 70 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 71 2001 Gilberto E. Urroz
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.








Ordinary Differential Equations with
SCILAB


By

Gilberto E. Urroz, Ph.D., P.E.














Distributed by


i nfoClearinghouse.com









2001 Gilberto E. Urroz
All Rights Reserved

Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at
the following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculato
rs/Scilab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu



Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
ORDINARY DIFFERENTIAL EQUATIONS 4
Introduction to differential equations 4
Definitions 5
Ordinary and partial differential equations 5
Order and degree of an equation 5
Linear and non-linear equations 5
Constant or variable coefficients 6
Homogeneous and non-homogeneous equations 6
Solutions 6
General and particular solutions 7
Verifying solutions using SCILAB 7
Initial conditions and boundary conditions 8
Symbolic solutions to ordinary differential equations 8
Solution techniques for first-order, linear ODEs with constant coefficients 9
Integrating factors for first-order, linear ODEs with variable coefficients 11
Exact differential equations 12
Solutions of homogeneous linear equations of any order with constant coefficients 12
Obtaining the particular solution for a second-order, linear ODE with constant coefficients 14
Applications of ODEs I : analysis of damped and undamped free oscillations 17
Undamped motion 17
Damped motion 18
Initial conditions for damped oscillatory motion 19
Creating phase portraits of oscillatory motion 22
Applications of ODEs II : analysis of damped and undamped forced oscillations 24
Applications of ODEs III: Oscillations in electric circuits 27
Finite differences and numerical solutions 29
Finite differences 29
Finite difference formulas based on Taylor series expansions 31
Forward, backward and centered finite difference approximations to the first derivative 32
Forward, backward and centered finite difference approximations to the second derivative 33
Solution of a first-order ODE using finite differences - Euler forward method 33
A function to implement Eulers first-order method 35
Finite difference formulas using indexed variables 39
Solution of a first-order ODE using finite differences - an implicit method 40
Explicit versus implicit methods 42
Outline of explicit solution for a second-order ODE 42
Outline of the implicit solution for a second-order ODE 43
Systems of ordinary differential equations 44
Systems of ordinary differential equations using matrices 44
Systems of linear homogeneous ODEs - solution using matrices 45
Systems of linear nonhomogeneous ODEs - solution using matrices 49
Converting second-order linear equations to a system of equations 50
SCILAB functions for the numerical solutions of initial value problems (IVP) 52

Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Applications of numerical solutions to IVPs 65
Systems of ODEs from mechanical systems 65
System of ODEs from Electric Circuits 68
Solving a fourth-order equation 72
The Van der Pol equation 74
The Rssler flow 77
Solutions to boundary value problems (BVPs) 79
The shooting method 80
A function to implement the shooting method 80
Outline of the implicit solution for a second-order BVP 83
Function bvode for the solution of boundary value problems 84
Function bvode applied to a third-order boundary value problem 88
Application of bvode to a third-order problem with one interior fixed point 89
Application of bvode to a fourth-order problem with two interior fixed points 91
Boundary value problems with eigenvalues 93
Numerical solution to a boundary value problem with eigenvalues 93
A function for calculating eigenvalues for a boundary value problem 95
Exercises 97





Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz

O Or rd di in na ar ry y D Di if ff fe er re en nt ti ia al l E Eq qu ua at ti io on ns s


The chapter starts with a review of concepts of differential equations and symbolic solution
techniques that can be applied using SCILAB. Since SCILAB is not a symbolic environment, its
applications to symbolic solutions of ordinary differential equations (ODEs) is limited.
However, SCILAB can be used to calculate intermediate numerical steps in the solutions. The
strength of SCILAB in solving ODEs is in its numerical applications. Thus, the chapter also
includes a number of numerical solutions to ODEs through user-programmed and pre-
programmed SCILAB functions.



I In nt tr ro od du uc ct ti io on n t to o d di if ff fe er re en nt ti ia al l e eq qu ua at ti io on ns s

Differential equations are equations involving derivatives of a function. Because many physical
quantities are given in terms of rates of change of a certain quantity with respect to one or
more independent quantities, derivatives appear frequently in the statement of physical laws.
For example, the flux of heat, q [J/m
2
], in a one-dimensional direction is given by

q = -k(dT/dx),

where T[K or
o
C] is the temperature, x [m] is positions, and k [J/(m K) or J/(m oC)]. This
equation can be considered as a differential equation if q and k are known, and we are trying
to solve for the temperature as a function of x, i.e., T = T(x). The equation of conservation of
energy for heat transfer in one-dimension, where there are no sources or sink of heat, requires
that the rate of change of the heat flux across an area perpendicular to the x-axis be zero,
i.e., dq/dx = 0,or,


If k is a constant, i.e., not a function of x, then, the equation of conservation of energy
reduces to
d
2
T/dx
2
= 0.

The last two expressions are also differential equations. The solution for these equations will
be a function T = T(x) representing the temperature.





. 0
1
]
1


dx
dT
k
dx
d

Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
D De ef fi in ni it ti io on ns s

The following definitions allow us to classify equations, thus providing general guidelines for
obtaining solutions.




Ordinary and partial differential equations

When the dependent variable is a function of a single independent variable, as in the cases
presented above, the differential equation is said to be an ordinary differential equation
(ODE). If the dependent variable is a function of more than one variable, a differential
equation involving derivatives of this dependent variable is said to be a partial differential
equation (PDE). An example of a partial differential equation would be the time-dependent
would be the Laplaces equation for the stream function, (x,y,z), of a three-dimensional,
inviscid flow:

2
/x
2
+
2
/y
2
+
2
/x
2
= 0.


Order and degree of an equation

The order of a differential equation is the order of the highest-order derivative involved in the
equation. Thus, the ODE
dy/dx + 3xy = 0

is a first-order equation, while Laplaces equation (shown above) is a second-order equation.

The degree of a differential equation is the highest power to which the highest-order derivative
is raised. Therefore, the equation

(d
3
y/dt
3
)
2
+(d
2
y/dx
2
)
5
-xy = e
x
,

is a third order, second-degree ODE, while the equation

y/t = c(y/x),

is a first-order, first-degree PDE.


Linear and non-linear equations

An equation in which the dependent variable and all its pertinent derivatives are of the first
degree is referred to as a linear differential equation. Otherwise, the equation is said to be
non-linear. Examples of linear differential equations are:

d
2
x/dt
2
+ (dx/dt) +
o
x = A sin
f
t,
and
C/t + u(C/x) = D(
2
C/x
2
).





Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
Constant or variable coefficients

The following equation:
d
3
y/dt
3
+ (d
2
y/dx
2
)
2
-5y = e
x
,

where all the coefficients accompanying the dependent variable and its derivative are
constant, would be classified as a third order, linear ODE with constant coefficients. Instead,
the equation

2
C/t
2
u(x,t) (C/x) = 0,

would be classified as a second-order, linear PDE with variable coefficients.


Homogeneous and non-homogeneous equations

Typically, differential equations are arranged so that all the terms involving the dependent
variable are placed on the left-hand side of the equation leaving only constant terms or terms
involving the independent variable(s) only in the right-hand side. When arranged in this
fashion, a differential equation that has a zero right-hand side is referred to as a homogeneous
equation. Examples of homogeneous equations are:

d
2
x/dt
2
+ (dx/dt) +
o
x = 0,
and
(x-1)(dy/dx) + 2xy = 0.

On the other hand, if the right-hand side of the equation, after placing the terms involving the
dependent variable and its derivatives on the left-hand side, is non-zero, the equation is said
to be non-homogeneous. Non-homogeneous versions of the last two equations are:

d
2
x/dt
2
+ (dx/dt) +
o
x = A
o
e
-t/
,
and
(x-1)(dy/dx) + 2xy = x
2
-2x.







S So ol lu ut ti io on ns s

A solution to a differential equation is a function of the independent variable(s) that, when
replaced in the equation, produces an expression that can be reduced, through algebraic
manipulation, to the form 0 = 0. For example, the function
y = sin x,
is a solution to the equation
d
2
y/dx
2
+ y = 0,

because when we replace y into the equation we have

-sin x + sin x = 0,


Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
or, 0 = 0, for all values of x. This follows from the fact that dy/dx = cos(x), and d
2
y/dx
2
=
-sin(x).

General and particular solutions

A general solution is one involving integration constants so that any choice of those constants
represents a solution to the differential equation. For example, the function

x = Ce
-t
,
is a general solution to the equation
dx/dt + x = 0,

because, substituting Ce
-t
for x in the equation produces

- Ce
-t
+ Ce
-t
= 0.

A particular solution is a solution corresponding to a specific value of the integration
constants. For example, the function

y = x
2
/2

is a particular solution to the equation,

dy/dx x = 0.

A general solution for this equation would be

y = x
2
/2 + C,

where C is an arbitrary integration constant.

Given the solution of the homogeneous equation, y
h
(x), the solution of the corresponding non-
homogeneous equation, y(x), can be written as

y(x) = y
h
(x) + y
p
(x),

where y
p
(x) is a particular solution to the ODE.

Verifying solutions using SCILAB

Since SCILAB is not a symbolic environment it is not suitable for the verification of solutions
other than polynomial solutions. As indicated in Chapter 8, SCILAB provides function derivat
to calculate derivatives of polynomials. If we have a function that can be expressed as a
polynomial, we can use function derivat to check if that function satisfies a particular
differential equation as illustrated in the example below:

Check that the function y(x) = x
3
-2x+4 is a solution to the differential equation, d
2
y/dx
2
- 6x = 0
using SCILAB:

-->x = poly(0,'x'); y = x^3-2*x+4;

-->derivat(derivat(y))-6*x
ans =

0

Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz

Initial conditions and boundary conditions

To determine the specific value of the constant(s) of integration, we need to provide values of
the solution, or of one or more of its derivatives, at specific points. These values are referred
to as the conditions of the solution. For example, we could specify that the solution to the
equation

d
2
y/dt
2
+y = 0,
must satisfy the conditions
y(0) = -5,
and
dy/dt = -1 at t = 5.

Initial conditions are provided at a single value of the independent variable so that after
evaluating those conditions at that point all the integration constants are uniquely specified.
In general, first order differential equations include one integration constant, requiring only
one condition to be evaluated to uniquely determine the solution. Thus, this type of equations
needs only one initial condition. The term initial condition is used because many first order
equations involve a derivative with respect to time, and the condition given to specify the
solution is typically the value of the function at time equal to zero, i.e., an initial value of the
function. Boundary conditions, on the other hand, are provided at more then one value of the
independent variable(s). The term boundary conditions is used because the function is
evaluated at the boundaries of the solution domain in order to specify the solution.

An example of initial conditions used in a solution will be to solve the equation

d
2
u/dt
2
+ 2(du/dt) = 0,
given
u(0) = 1, du/dt|
t=0
= -1.

An example of boundary conditions used in a solution will be to solve the equation

d
2
y/dx
2
+y = A sin x,

using
y(0) = A/2, and y(1) = -A/2.

In general, the solution of a n-th order ODE requires n conditions.


S Sy ym mb bo ol li ic c s so ol lu ut ti io on ns s t to o o or rd di in na ar ry y d di if ff fe er re en nt ti ia al l e eq qu ua at ti io on ns s

By symbolic solutions we understand those solutions that can be expressed as a closed-form
function of the independent variable. Because solution of first-order differential equations
imply integrating the derivative involved in the equation, many of the techniques used for
solving first-order ODEs follow from integration techniques. Details of some techniques used for
solving ordinary differential equations follow.



Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Solution techniques for first-order, linear ODEs with constant
coefficients

A first order equation is an equation of the form

a(dy/dx)
n
+ by
m
= f(x),

where a, b, n and m are, in general, real numbers. Some specific techniques for linear
equations, i.e., when n = m = 1, follow. This catalog of solutions for linear ODEs is intended as
a review of the techniques. Numerical solutions using SCILAB will be presented in a later
chapter.


Equations of the form: dy/dx = f(x) -- Direct integration

An equation of the form dy/dx = f(x) can be re-written as

dy= f(x)dx,

and a general solution found by direct integration,

dy = f(x)dx,
or
y = f(x)dx+C.

If an initial condition y(x
o
) = y
o
, is given, then the integration can be calculated as

or,


If the function f(x) is a polynomial, we can use the SCILAB user-defined function intpoly to
produce the indefinite integral. Consider the example in which dy/dx = f(x) = x
3
+ x + 2.
The solution, with the help of SCILAB, is calculated as:

-->f = poly([2,1,0,3],'x','coeff')
f =

3
2 + x + 3x

-->getf(intpoly)

-->fInt = intpoly(f)

Indefinite integral - Add integration constant

fInt =

2 4
2x + .5x + .75x

y
y
x
x
o o
dx x f dy , ) (


x
x
o
dx x f y y . ) (
0

Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz

Thus, the general solution is :

y(x) = 2x+0.5*x
2
+0.75*x
4
+ C


Equations of the form: dy/dx = g(y) -- Inversion and direct integration

Equations of the form dy/dx = g(y), can be re-written as

dy/g(y) = dx.

Thus, an indefinite integral will be given by

dy/g(y) = dx,
or
dy/g(y) = x + C.

From the latter expression, the dependent variable y may be solved for. A similar
approach is followed when using a definite integral, i.e., one with initial condition y(x
o
) =
y
o
. The integration in this case reads:




Equations of the form: dy/dx = f(x)g(y) -- Separation of variables

Equations of the form dy/dx = f(x)g(y), can be separated into

dy/g(y) = dx/g(x),

and then integrated using indefinite integrals for general solutions, or definite integrals
with initial conditions for particular solutions.


Equations of the form: dy/dx = g(y/x)

Using the change of variable
u = y/x,
we have
y = ux,

dy = udx + xdu,
then
(udx + xdu)/dx = g(u),

udx +xdu = g(u)dx,

[g(u)-u] dx = xdu,



y
y
x
x
o
o o
x x dx
y g
dy
.
) (

Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
from which the variables x and u can be separated as

du/[g(u)-u] = dx/x.

After integration, we replace
u = y/x

back in the result, and isolate, if possible, y(x).


Equations of the form: a(dy/dx)+ by = f(x) -- Integrating factors

The expression
a(dy/dx)+ by = f(x)

constitutes the most general form of a first-order, linear, ordinary differential equation.
The equation can be re-written as

dy/dx + (b/a) y = (1/a)f(x),

You can prove that, by multiplying both sides of this form of the equation by a function,

IF(x) = exp(bx/a),




known as an integrating factor, the equation becomes:


This equation can be easily integrated to read:


In terms of the integrating factor, this solution will be:

y(x) = (1/IF(x))[(1/a) IF(x) f(x) dx + C].



Integrating factors for first-order, linear ODEs with variable
coefficients

An equation with variable coefficients such as

K
1
(x)(dy/dx) + K
2
(x)y(x) = K
3
(x),

can be reduced to the form,
). ( exp
1
) ( exp x f
a
x b
a
x y
a
x b
a
dx
d

,
_

,
_


,
_

. ) ( exp
1
exp ) (

,
_

+
,
_



,
_

C x f
a
x b
a a
x b
x y

Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz

dy/dx + g(x)y(x) = f(x),

by dividing the entire equation by K
1
(x). The latter equation can be solved by multiplying both
sides of the equation by the integrating factor

IF(x) = exp(g(x)dx).

After identifying the integrating factor, IF(x), the solution procedure is very similar to the case
of a first-order, constant-coefficient ODEs, i.e.,

y(x) = (1/FI(x))[FI(x) f(x) dx + C].

Exact differential equations

An expression of the form,
F(x,y)dx + G(x,y)dy = 0,

Is said to be an exact differential equation in two dimensions, if the components F(x,y) and
G(x,y) satisfy the conditions
F/y = G/x.

In such case, it is possible to find a function u(x,y), such that

F(x,y) = u/x, G(x,y) = u/y.

The equation, u(x,y) = C, where C is a constant, will represent a solution to the exact
differential equation:
F(x,y)dx + G(x,y)dy = 0.


Solutions of homogeneous linear equations of any order with
constant coefficients

Consider the linear, constant-coefficient, homogeneous ODE of order n:

d
(n)
y/dx
(n)
+ b
n-1
(dy
(n-1)
/dx
(n-1)
) + + b
2
(d
2
y/dx
2
) + b
1
(dy/dx) + b
0
y = 0.

where the coefficients b
0
, b
1
, , b
n-1
, are constant. We can use the operator D
(k)
= d
(k)
/dx
(k)
,
to re-write the equation as

D
(n)
y + b
n-1
D
(n-1)
y + + b
2
D
2
y + b
1
Dy + b0y = f(x).

Treating the operators D
(k)
, (k = n, n-1, , 1), as algebraic terms, the equation is re-written as

[D
(n)
+ b
n-1
D
(n-1)
+ + b
2
D
2
+ b
1
D + b
0
]y = 0.

The idea is that the linear combination of the operators, shown above in square brackets, is
applied to the function y(x), in a similar manner as algebraic terms would be multiplied to it.

Associated with the latter expression is a polynomial known as the characteristic equation of
the ODE, and written as

n
+ b
n-1

n-1
+ + b
2

2
+ b
1
+ b
0
= 0.


Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
Suppose that the characteristic equation has n independent roots, then the general solution of
the linear, constant-coefficient, homogeneous ODE of order n given earlier is

y = C
1
e

1
x
+ C
2
e

2
x
+ + C
n-1
e

n-1
x
+ C
n
e

n
x
.

If out of the n roots there is one that has multiplicity m, then the m terms corresponding to
this root in the solution, will be

C
(1)
e
x
+ C
(2)
xe
x
+ C
(1)
x
2
e
x
+ + C
(1)
x
m-1
e
x
.


Example 1 Determine the general solution to the homogeneous equation

d
3
y/dx
3
-4(d
2
y/dx
2
)-11(dy/dx)+30y = 0.

In terms of the D operator, this ODE can be written as

[ D
3
-4 D
2
-11D +30]y = 0.

The characteristic equation corresponding to this ODE is

3
-4
2
-11 +30 = 0.

To obtain solutions to this equation in SCILAB use:

-->lam = poly(0,'lam')
lam = lam

-->p = lam^3-4*lam^2-11*lam+30
p =
2 3
30 - 11lam - 4lam + lam

-->roots(p)
ans =
! 2. !
! - 3. !
! 5. !

Thus, a general solution to the ODE under consideration is


y = C
1
e
2x
+ C
2
e
-3x
+ C
3
e
5x
.

Example 2 Determine the general solution to the homogeneous ODE:

d
4
y/dx
4
-7(d
3
y/dx
3
)+18(d
2
y/dx
2
)-20(dy/dx)+8y = 0.

In terms of the D operator, this ODE can be written as:

[D
4
-7D
3
+18D
2
-20D+8]y = 0.

Thus, the characteristic equation is

4
-7
3
+18
2
-20 +8 = 0.


Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
To obtain the solution of this equation using SCILAB try the following commands:

-->p = lam^4-7*lam^3+18*lam^2-20*lam+8
p =
2 3 4
8 - 20lam + 18lam - 7lam + lam

-->roots(p)
ans =

! 1. !
! 2. !
! 2. !
! 2. !

Since root = 2 has multiplicity of 3, the solution becomes:

y(x) = C
1
e
x
+ e
2x
(C
2
+ C
3
x + C
4
x
2
).


Obtaining the particular solution for a second-order, linear ODE
with constant coefficients

Thus, how do we come up with a particular solution, y
p
, to complete the solution to a non-
homogeneous equation, y = y
h
+y
p
, given the solution to the homogeneous equation, y
h
? In this
section we present a general method to obtain y
p
for second-order, linear ODEs with constant
coefficients. The reason why we choose second-order equations is not only because they are
the simpler equations to solve (not including first-order equations, which were discussed in
great detail in an earlier section), but also because they are useful to model a number of real-
life situations. Typical systems modeled by second-order ODEs are the damped and undamped
oscillatory behavior in spring-mass and electric circuit systems.

The general expression for a second-order, linear, non-homogeneous ODE with constant
coefficients is
d
2
y/dx
2
+ b
1
(dy/dx) + b
0
y = h(x).

The first step is to obtain the solution to the homogeneous equation

d
2
y/dx
2
+ b
1
(dy/dx) + b
0
y = 0,

by using the solutions to the characteristic equation

2
+ b
1
+ b
0
= 0.

Consider the case in which the solutions to the characteristic equation are real numbers. The
solutions to this quadratic equation can be two different values of , say
1
and
2
, in which
case the homogeneous solution is written as
y
h
(x) = C
1
exp(
1
x) + C
2
exp(
2
x),

or a single solution of multiplicity 2, say
0
, in which case we write

y
h
(x) = (C
1
+C
2
x) exp(
0
x).


Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
If the two solutions to the quadratic (characteristic) equation are complex numbers, they must
be complex conjugates of each other as required by the fundamental theorem of algebra. In
this case we can write

1
=+i, and
2
=-i,

where and are real numbers. Thus, the solution C
1
exp(
1
x) + C
2
exp(
2
x), becomes

C
1
e
(+i)x
+ C
2
e
(-i)x
= C
1
e
x
e
ix
+ C
2
e
x
e
-i x
= e
x
(C
1
cos x + iC
1
sin x + C
2
cos x - iC
2
sin
x) = e
x
[(C
1
+C
2
)cos x + i(C
1
-C
2
)sin x] = e
x
(K
1
cos x + K
2
sin x),

where
K
1
= (C
1
+C
2
), and K
2
= i(C
1
-C
2
).

Thus, for the case of two complex solutions to the characteristic equation, the homogeneous
solution is a sinusoidal function whose amplitude grows (>0) or decreases (<0) with x:

y
h
(x) = e
x
(K
1
cos x + K
2
sin x).

If the solutions are imaginary numbers, i.e., if = 0 in the previous result, the homogeneous
solution is a pure sinusoidal function:

y
h
(x) = K
1
cos x + K
2
sin x.

To obtain the particular solution, y
p
(x), that will produce the overall solution of the non-
homogeneous ODE, y(x) = y
h
(x) +y
p
(x), follow this rule that refers to the sub-sequent table of
functions:

If h(x), in the general non-homogeneous ODE, is given by one of the functions in the
first column of the table shown below, choose for y
p
(x) a linear combination of h(x) and its
linearly independent derivatives, as shown in the second column of the table.


If h(x) is the sum of some of the functions shown in column 1 of the table below,
choose for y
p
(x) the sum of the functions in the corresponding lines.


If a term in h(x) is a solution of the homogeneous equation corresponding to the ODE
under consideration, modify your choice of y
p
(x) by multiplying the appropriate line of
column 2 by x or x
2
, depending on whether the root of the characteristic equation (column
3) is simple or double.

Term in h(x) Choice for y
p
(x) Root of char. eqn.
ce
x
C
0
e
x
, real
cx
n
( n = 0, 1, ) C
n
x
n
+ C
n-1
x
n-1
+ + C
1
x + C
0
0
csin x C
1
sin x + C
2
sin x i, imaginary
ccos x C
1
sin x + C
2
sin x i, imaginary

Once the particular solution is set up by following the rule above, the undetermined
coefficients in y
p
(x) can be determined by substituting yp(x) into the ODE.

Example 1 Obtain the general solution to the non-homogeneous, second-order, linear ODE:


Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
d
2
y/dx
2
- 5(dy/dx) +6y = x
2
.

The characteristic equation of the homogeneous equation is

2
-5 +6 = 0,
or
(-3)(-2) = 0,
with solutions
= 2, and = 3.
Thus, the homogeneous solution is
y
h
(x) = K
1
e
2x
+ K
2
e
3x
.

Since the right-hand side of the non-homogeneous equation is

h(x) = x
2
,
from the table above we select
y
p
(x) = C
2
x
2
+C
1
x+C
0
.

To obtain the values of C
0
, C
1
, and C
2
, replace the solution y
p
(x) into the ODE. The derivatives
are, dy
p
/dx = 2C
2
x+C
1
, and d
2
y
p
/dx
2
= 2C
2
, which replaced into the equation produce

2C
2
-5(2C
2
x+C
1
) + 6(C
2
x
2
+C
1
x+C
0
) = x
2
,
or
6C
2
x
2
+ (6C
1
-10C
2
)x + (6C
0
-5C
1
+2C
2
) = x
2
.

Comparing the coefficients of the terms x
2
, x
1
, and x
0
, in both sides of the resulting equation
allows us to write the following system of linear equations:

6C
2
= 1
6C
1
10C
2
= 0
6C
0
-5C
1
+ 2C
2
= 0

A solution, using SCILAB, produces:

-->A = [0,0,6;0,6,-10;6,-5,2], b = [1;0;0]
A =

! 0. 0. 6. !
! 0. 6. - 10. !
! 6. - 5. 2. !



b =

! 1. !
! 0. !
! 0. !

-->C = A\b
C =

! .1759259 !
! .2777778 !
! .1666667 !


The solution is:

Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
C
0
= 0.1759259, C
1
= 0.2777778, and C
2
= 0.1666667

Thus, y
p
(x) = 0.1666667x
2
+0.2777778x+0.1759259,

and the general equation to the non-homogeneous equation becomes:

y(x) = y
h
(x)+y
p
(x) = K
1
e
2x
+ K
2
e
3x
+ 0.1666667x
2
+0.2777778x+0.1759259.




A Ap pp pl li ic ca at ti io on ns s o of f O OD DE Es s I I : : a an na al ly ys si is s o of f d da am mp pe ed d a an nd d
u un nd da am mp pe ed d f fr re ee e o os sc ci il ll la at ti io on ns s

Consider the mass-spring system shown in the figure below. The mass is removed from its
equilibrium position (x = 0) and released at a position x = x
0
at t=0. At the moment of its
release the body was moving with a speed v = v
o
. The diagram shows the body of mass m being
acted upon by the restoring force of the spring, Fs = - kx, and by a viscous damping force, Fv =
- v = - (dx/dt).


Newtons second law, when applied in the x-direction to the mass m is written as:

kx (dx/dt) =m (d
2
x/dt
2
),

which results in the second-order, linear, ordinary differential equation:

d
2
x/dt
2
+ (/m)(dx/dt)+(k/m)x = 0.
Undamped motion

Let us first consider the case in which the motion is undamped, i.e., b = 0. The equation in
this case reduces to
d
2
x/dt
2
+(k/m)x = 0.

The corresponding characteristic equation is

2
+ (k/m) = 0,
with solutions,
= t i (k/m) = ti
o
.

This result suggest a solution of the form

Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz

x(t) = C
1
cos
o
t + C
2
sin
o
t.

Alternatively, by taking
C
1
= A cos , and C
2
= - A sin ,
the solution can be written as
x(t) = Acos(
o
t + ).

The quantity

o
= (k/m)

is known as the natural angular frequency of the harmonic motion that results when no viscous
damping is present. The frequency of the oscillation can be calculated from

f = 2/

= 1/T,

where T is the period of the oscillation (i.e., the time that the mass takes to return to a pre-
defined position in the motion). The quantity is known as the angular phase of the
oscillation, and A is known as the amplitude.


The velocity of the motion is given by

v = dx/dt = -
o
A sin(
o
t + ),
and its acceleration, is
a = dv/dt = -
o
2
A cos(
o
t + ),

The initial conditions, x(0) = x
o
, v(0) = v
o
, can be used to evaluate the constants A and f, as
follows:
x
o
= x(0) = A cos ,
and
v
o
= v(0) = -
o
A sin .
Thus,
tan = - v
o
/(
o
x
o
), or = tan
-1
(-v
o
/(
o
x
o
)),
and
A = [x
o
2
+ (v
o
/
o
)
2
]
1/2
.


Damped motion

If damping occurs (0), the characteristic equation becomes

2
+ (/m) +
o
2
= 0,
whose solutions are
= -(/(2m))t ([/(2m)]
2
-
o
2
) = - t (
2
-
o
2
),
where
= /(2m).

The nature of the solution will depend on the relative size of the coefficients and
o
, as
follows:

If <
o
, then (
2
-
o
2
) = i
1
, where

1
= (
o
2
-
2
)

Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz

is real, and the solutions of the characteristic equation are

1
= -+ i
1
, and
2
= -- i
1
.

The solution to the ODE, therefore, is written as

x(t) = e
t
(C
1
cos
1
t + C
2
sin
1
t) = A
o
e
t
cos(
1
t +
1
).

The parameter

1
= (
o
2
-
2
) = [(k/m)
2
-(/(2m))
2
] = (4k
2
-
2
)/(2m),

represents the damped angular frequency of the oscillation, and
1
represents the
corresponding angular phase. A
o
is the amplitude of the oscillation at t = 0. If we define
a variable amplitude,
A(t) = A
o
e
t
,

then the solution to the ODE, also known as the signal, can be written as

x(t) = A(t)cos(
1
t +
1
).

Please notice that this solution is very similar to the case of an undamped oscillation,
except for the fact that in a damped oscillation the amplitude decreases with time. The
amplitude decreases, or decays, with time because the parameter = /(2m) is positive.
Therefore, the function exp(-t) decreases with time.

If =
o
, then the characteristic equation produces the solution = -, with
multiplicity 2, in which case the solution becomes

x(t) = e
t
(C
1
+ C
2
t).

This solution represents a linear function of t subjected to a decay factor, exp(-t).

If >
o
, then (
2
-
o
2
) = K is real, and K < , the solutions of the characteristic
equation become

1
= -+ K = -c
1
, and
2
= --K = - c
2
,

both negative. Therefore, the resulting signal can be written as:

x(t) = C
1
exp(-c
1
t) + C
2
exp(-c
2
t).

Notice that the last two cases, namely, =
o
and >
o
, produce signals that decay with
time. These cases correspond to harmonic motions that are said to be over-damped, i.e., the
viscous damping is large enough to quickly damp out any oscillation after the body of mass m is
released.
Initial conditions for damped oscillatory motion

The expression for the position of a damped oscillatory motion is given by

x(t) = A
o
e
t
cos(
1
t +
1
),

while the velocity, v(t) = dx/dt, is given by

v(t) = -A
0
e
-t
( cos(
I
t+
1
) +
I
sin(
I
t+
1
)).

Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz

Given the initial conditions x(t
0
) = x
0
and v(t
0
) = v
0
, we can form a system of two non-linear
equations in the unknowns A
0
and
1
, namely,

f
1
(A
0
,
1
) = A
o
e
t
0
cos(
1
t
0
+
1
) x
0
,

f
2
(A
0
,
1
) = -A
0
e
-t
0
( cos(
I
t
0
+
1
) +
I
sin(
I
t
0
+
1
)) v
0
.

With appropriate values of the parameters ,
I
, t
0
, x
0
, and v
0
, we can use SCILAB function
fsolve to obtain the values of A
0
and
1
, as illustrated in an upcoming example.

Before presenting the example, however, we will write out the expression for the acceleration
so that we can use it in producing the graphics of the example:

a(t) = A
0
e
-t
0
(
2
cos(
I
t+
1
) + 2
I
sin(
I
t+
1
)
I
2
cos(
I
t+
1
)).


Example 1 Damped oscillatory motion: Plot position, velocity, and acceleration
corresponding to the following parameters: m = 1 kg, = 0.1Ns/m, k = 0.5 N/m. To
determine the constants A
o
and
1
, use initial conditions, x
0
= 1.5 m, and v
0
= -5.0 m/s. With
these values,

o
= (k/m)
1/2
= (0.5N/1kgm)
1/2
= (0.5 s
-2
)
1/2
= 0.7071 s
-1
= 0.7071 rad/s,
and
= /(2m) =0.1 Ns/ (21 kgm) = 0.05 s
-1
=0.05 rad/s.

Since, <
o
, the resulting signal is that of a damped oscillation with

1
= (
o
2
-
2
) = (0.7071
2
0.05
2
) 0.7053 rad /s.

To solve for the constants A
o
and
1
with SCILAB, we first define the set of non-linear equations
to be solved. In the function thus defined A
0
is represented by s(1) and
1
is represented by
s(2):


-->deff('[FF]=f(s)',['f1=s(1)*exp(-a*t0)*cos(wI*t0+s(2))-x0';...
-->'f2 = -s(1)*exp(-a*t0)*(a*cos(wI*t0+s(2))+wI*sin(wI*t0+s(2)))-v0';...
-->'FF = [f1;f2]'])


Next, we enter the known values, and select a first guess for the solution s0:


-->w0 = 0.7071; a = 0.05; wI = 0.7053; x0 = 1.5; v0 = -5.0; t0 = 0;

-->s0 = [5;%pi/3]
s0 =

! 5. !
! 1.0471976 !

The solution for s(1) = A
0
and s(2) =
1
is obtained by using:

-->fsolve(s0,f)
ans =


Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
! 7.1421364 !
! 1.3591997 !


Thus, A
0
= 7.1221364 and
1
= 1.3591997, and the position x(t) is given by


x(t) = A
o
exp(-0.05t) cos(0.7053t
1
).


Expressions for the position x(t), velocity v(t), and acceleration a(t) for this motion can be
entered into SCILAB by defining the following functions:


-->A0 = 7.1421364; phi1 = 1.3591997;

-->deff('[xs]=x(t)','xs = A0.*exp(-a.*t).*cos(wI.*t+phi1)')

-->deff('[vs]=v(t)',...
-->'vs =-A0.*exp(-a.*t).*(a.*cos(wI.*t+phi1)+wI.*sin(wI.*t+phi1))')

-->deff('[acc]=aa(t)',...
-->'acc=A0.*exp(-a.*t).*(a^2.*cos(wI.*t+phi1)+...
-->2.*a.*wI.*sin(wI.*t+phi1)-wI^2.*cos(wI.*t+phi1))')


To plot the signals x(t), v(t), and a(t) in the t-interval (0,30) use the following SCILAB
commands:


-->tt = [0:0.1:30]; xx = x(tt); vv = v(tt); -->aaa = aa(tt);

-->plot2d([tt',tt',tt'],[xx',vv',aaa'],[2,3,4],'111',...
-->'position@velocity@acceleration',[0 -10 30 10])

-->xtitle('Damped oscillatory motion', 't', 'x,v,a')



Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
The results are shown in the following graph:



Notice the oscillatory nature of the three functions, as well as their amplitudes decay with
time as expected.


Creating phase portraits of oscillatory motion

A phase portrait for oscillatory, or any kind of, motion is a plot involving the dependent
variable and one of its derivatives, or two derivatives of the dependent variable. For example,
a plot of velocity, v(t), versus position, x(t), represents a phase portrait. Other phase portraits
would be a(t) vs. x(t), and a(t) vs. v(t).


Example 1. Plot the time-dependent plots and phase portraits for the signal obtained in
Example 1 in the previous section. To plot these phase portraits we generate data on position,
velocity, and acceleration as function of time t in the interval [0,90], as follows:


-->tt = [0:0.1:90]; xx = x(tt); vv = v(tt); aaa = aa(tt);


The phase portraits are generated as follows:

-->xset('window',1);plot(xx',vv')
-->xtitle('v-vs-x phase portrait','x','v')




Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz

-->xset('window',2);plot(xx',aaa')
-->xtitle('a-vs-x phase portrait','x','a')





-->xset('window',3);plot(vv',aaa')

-->xtitle('a-vs-v phase portrait','v','a')


Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz


The three phase portraits show orbits spiraling inwards towards the center of the picture, i.e.,
towards (0,0). This is because the amplitude of the variables included in the phase portrait
decreases at about the same rate with time.


A Ap pp pl li ic ca at ti io on ns s o of f O OD DE Es s I II I : : a an na al ly ys si is s o of f d da am mp pe ed d a an nd d
u un nd da am mp pe ed d f fo or rc ce ed d o os sc ci il ll la at ti io on ns s

Earlier we presented the analysis of damped and undamped free oscillations, meaning that,
once the particle subjected to oscillatory motion is released, all forces acting on it (the
restoring force of the spring, and the damping force from the dashpot) are internal to the
system. If the particle is continuously subjected to an external force (an excitation), then the
type of oscillations thus generated are termed forced oscillations. Of interest are excitations
that are themselves oscillatory. The simplest case will be an external force,

F
e
(t) = F
o
cos t.

The differential equation for the mass-spring-dashpot system, including the excitation, F
e
(t), is
now written as:
d
2
x/dt
2
+ (/m)(dx/dt)+(k/m)x = (F
o
/m)cos t.

Lets assume that the values of the parameters m, b, and k are such that the solution of the
homogeneous equation is
x
h
(t) = A
o
e
at
cos(
o
t+).

Also, because the term cos t shows up in the right-hand side term, the table for selecting the
particular solution (shown earlier in this chapter), suggest that we try

x
p
(t) = C
1
cos t + C
2
sin t.

Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz

Because this particular solution must satisfy the governing ODE, we can write

d
2
x
p
/dt
2
+ (/m)(dx
p
/dt)+(k/m)x
p
= (F
o
/m)cos t.

The values of C
1
and C
2
, using
0
2
= k/m, are:




The particular solution can be written now as


Suppose that we want to write this solution as

x
p
(t) = A
p
cos (t +
p
) = A
p
cos t cos
p
A
p
sin t sin
p
,

by comparing the last two expressions we find that

A
p
cos
p
= F
0
m(
o
2
-
2
)/[
2

2
+m
2
(
o
2
-
2
)
2
],
and
A
p
sin
p
= - F
0
/[
2

2
+m
2
(
o
2
-
2
)
2
],

from which,
A
p
2
= F
0
2
/[
2

2
+m
2
(
o
2
-
2
)
2
],
and
tan
p
= - /(m(
o
2
-
2
)).

Thus, the particular solution can be written as:

To analyze the behavior of this particular solution, first we study the case in which no damping
is present, i.e., = 0. In such case,
p
= 0, and the particular solution becomes


For this case, the amplitude of the oscillation, A
p
(), becomes infinity as !
o
. This
condition is known as resonance. Thus resonant conditions will occur if the exciting force has
the same frequency as the natural frequency of the system. In practice, the amplitude of the
.
) (
2 2 2
0
2 2 2
0
2

m
F
C
.
) (
) (
2 2 2
0
2 2 2
2 2
0 0
1


+

m
m F
C
.
) (
) sin( ) cos( ) (
) (
2 2 2
0
2 2 2
2 2
0
0


+
+

m
t t m
F t x
p
). cos(
) (
) (
2 2 2
0
2 2 2
0
p p
t
m
F
t x

+
+

. cos ) ( cos
) / ( 1
) /(
cos
) (
) (
2
0
0 0
2 2
0
0
t A t
m F
t
m
F
t x
p p



Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
undamped oscillations grows without bound until the system is severely damaged or destroyed.
This is important for analyzing building response to earthquakes. Every building has a natural
frequency of vibration. If a building is subjected for a long period of time to an earthquake
with a frequency similar or equal to its natural frequency, the building may suffer severe
damages as consequence of the earthquake.

If damping is present, then the amplitude of the oscillation is given by

which has a maximum

when

2
= 2m
2
(
o
2
-
2
).


Since the general solution of the damped equation,

x
h
(t) = A
o
e
at
cos(
o
t+),

decreases with time, it will eventually become negligible when compared to the particular
solution. Thus, it is said that the general solution represents the transient (temporary)
response of the system to the exciting force, F
e
(t). The particular solution, which turns out to
be a sinusoidal wave, represents the steady-state response of the system.

The following is a graph showing the amplitude, A
p
(), as function of the angular frequency, w,
for a particular set of values of the parameters, namely, F
0
= 25 N, k = 100 N/m, m = 1.0 kg,
which gives w
0
= 3.162277 rad/s. The graph is obtained using SCILAB as shown below. First,
we define the function for A
p
(), and the constant parameters:

-->deff('[AA]=Ap(om)','AA=F0./sqrt(om.^2.*b.^2+m.^2.*(w0.^2-om.^2).^2)')

-->F0 = 25; k = 10; m = 1.0; w0 = sqrt(k/m)
w0 = 3.1622777

Next, we define a vector bb containing 5 different values of the damping parameter , and a
vector om containing values of in the range (0,6). The lengths of vectors bb and om are the
values n and m, respectively:

-->bb = [1.0, 5.0, 10.0, 50.0, 100.0]; om = [0:0.1:6];

-->n = length(bb); m = length(om);

The next step is to create a matrix Amp (Amplitudes) with m rows and n columns whose
elements will contain the values Amp
ij
= Ap(om
i
,bb
j
). The following commands show how to
load matrix Amp:

->Amp = zeros(m,n);

-->for j = 1:n
,.
) (
) (
2 2 2
0
2 2 2
0

m
F
A
p
,
4
2
) (
2 2 2
0

m
mF
A
p

Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
--> b = bb(j); Amp(:,j) = Ap(om');
-->end;

To produce the plot showing curves of A
p
() for different values of is produced by using:

-->plot2d([om',om',om',om',om'],...
-->[Amp(:,1),Amp(:,2),Amp(:,3),Amp(:,4),Amp(:,5)],...
-->[1:1:5],'111','b=1@b=5@b=10@b=50@b=100',[0 0 6 1.8])

-->xtitle('Amplitude of forced oscillation','w','A(w)')




The plot shows that as the value of decreases the amplitude reaches a maximum near the
value of
0
. If there were no damping, i.e., ! 0, then A
p
() ! at that point,
indicating the condition of resonance.


A Ap pp pl li ic ca at ti io on ns s o of f O OD DE Es s I II II I: : O Os sc ci il ll la at ti io on ns s i in n e el le ec ct tr ri ic c
c ci ir rc cu ui it ts s

Electric circuits involving resistors, capacitors, and inductors are often times characterized by
an oscillatory behavior represented by electric current I through the circuit. Consider a simple
series RLC circuit as shown in the figure below.


Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz

In the figure, E(t) stands for the time-dependent voltage (volts), I(t) is the electric current
through the circuit once the switch is set to ON (amperes), R is the equivalent resistance of the
circuit (ohms), L is the equivalent inductance of the circuit (henrys), and C is the equivalent
capacitance (farads). The electric current, I(t), is the rate of change of electric charge with
respect to time, i.e., I(t) = dq/dt, where q = electric charge (coulombs), and t is time (sec).
The properties of resistors, capacitors, and inductors are such that if V represents the voltage
across one of those components the following relationships hold:

Resistors, V
R
= RI = R(dq/dt)
Capacitors, V
C
= q/C
Inductors, V
L
= L(dI/dt) = L(d
2
q/dt
2
)

To put together a differential equation for this circuit we use Kirchoffs law of voltages around
the series circuit: V
R
+ V
L
+ V
C
= E(t), i.e.,

R(dq/dt) + L(d
2
q/dt
2
) + q/C = E(t).

Alternatively, we can take the derivative of this equation with respect to t and write the
equation in terms of the current, I = dq/dt, as follows:

R(dI/dt) + L(d
2
I/dt
2
) + (1/C)I = dE/dt.

Thus, we can either solve the equation in terms of the electric charge, q(t), re-written as:

(d
2
q/dt
2
) + (R/L)(dq/dt) + q/(LC) = E(t)/L,

or, in terms of the electric current, I(t), re-written as:

(d
2
I/dt
2
) + (R/L)(dI/dt) + I/(LC) = (1/L)(dE/dt).

To simplify the solution we introduce the constants
o
2
= 1/(LC) and = R/(2L), and solve the
equation in terms of the electric charge, q(t), i.e.,

(d
2
q/dt
2
) + 2(dq/dt) +
o
2
q = E(t)/L.



Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
Solution to the homogeneous equation

Consider first, the homogeneous case, i.e., E(t) = 0, which is the situation that would occur is
the capacitor is charged and the voltage source is by-passed. The resulting governing equation
is
(d
2
q/dt
2
) + 2(dq/dt) +
o
2
q = 0,

which is the same as the case of free oscillations with damping for a spring-mass-dashpot
system.

If the constant value
2 2
0 1


is real, the solution is a damped oscillation, i.e.,

). cos( ) (
1 0

t e Q t q
t


If
1
is not real, then the solution is a combination of exponential functions, i.e.,




Since the governing equation for q(t) in an RLC circuit is the same as the governing equation
for the position of a particle in a mass-spring-dashpot system, we can borrow many of the
results obtained in the previous two sections to analyze RLC circuits. Some applications are
presented in the exercise section.

F Fi in ni it te e d di if ff fe er re en nc ce es s a an nd d n nu um me er ri ic ca al l s so ol lu ut ti io on ns s

To solve differential equations numerically we can replace the derivatives in the equation with
finite difference approximations on a discretized domain. This results in a number of algebraic
equations that can be solved one at a time (explicit methods) or simultaneously (implicit
methods) to obtain values of the dependent function y
i
corresponding to values of the
independent function x
i
in the discretized domain.

Finite differences

A finite difference is a technique by which derivatives of functions are approximated by
differences in the values of the function between a given value of the independent variable,
say x
0
, and a small increment (x
0
+h). For example, from the definition of derivative,

df/dx = lim
h
!
0
(f(x+h)-f(x))/h,

we can approximate the value of df/dx by using the finite difference approximation

(f(x+h)-f(x))/h
with a small value of h.

The following table shows approximations to the derivative of the function

f(x) = exp(-x) sin (x
2
/2),

t t
e C e C t q
2 1
2 1
) (

+

Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
at x = 2, using finite differences. The actual value of the derivative is -0.23569874791. The
third column in the table shows the error in evaluating the derivative, i.e., the difference
between the numerical derivative f/x and the actual value.

h f/x error
0.1 -0.244160077 0.00846132909
0.01 -0.236684829 0.00098608109
0.001 -0.235798686 0.00009993809
0.0001 -0.235708734 0.00000998609
0.00001 -0.235699726 0.00000097809
0.000001 -0.235698825 0.00000007709
0.0000001 -0.235698734 0.00000001391
0.00000001 -0.235698724 0.00000002391
0.000000001 -0.235698752 0.00000000409

This exercise illustrates the fact that, as h!0, the value of the finite difference
approximation, (f(x+h)-f(x))/h, approaches that of the derivative, df/dx, at the point of
interest.
A plot of the error as a function of h also reveals the fact that the error is proportional to the
value of the x-increment h. The following plots, using different ranges of h, are produced
with SCILAB out of the data in the table.

-->h = [1e-1,1e-2,1e-3,1e-4,1e-5,1e-6,1e-7,1e-8,1e-9];

-->er = [0.00846132909,0.00098608109,0.00009993809,0.00000998609,...
--> 0.00000097809,0.00000007709,0.00000001391,0.00000002391,...
--> 0.00000000409];

-->xset('mark',-9,2)
-->plot2d(h,er,1,'011',' ',[0 0 0.1 0.01])
-->plot2d(h,er,-9,'011',' ',[0 0 0.1 0.01])
-->xtitle('error vs. x-increment','h','error')



Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz


The graphs seem indicate that the error varies linearly with the increment h in the
independent variable. It is very common to indicate this dependency by saying that "the error
is of order h", or error = O(h). The magnitude of the error can be estimated by using Taylor
series expansions of the function f(x+h).





Finite difference formulas based on Taylor series expansions

The Taylor series expansion of the function f(x) about the point x = x
0
is given by the formula


Where f
(n)
(x
0
) = (d
n
f/dx
n
)|
x=x0
, and f
(0)
(x
0
) = f(x
0
).

If we let x = x
0
+h, then x-x
0
= h, and the series can be written as


. ) (
!
) (
) (
0
0
0
) (
n
n
n
x x
n
x f
x f

), (
! 2
) ( "
! 1
) ( '
) (
!
) (
) (
3 2 0 0
0
0
0
) (
0
h O h
x f
h
x f
x f h
n
x f
h x f
n
n
n
+ + + +


Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
Where the expression O(h
3
) represents the remaining terms of the series and indicates that the
leading term is of order h
3
. Because h is a small quantity, we can write 1 > h, and
h>h
2
>h
3
>h
4
> Therefore, the remaining of the series represented by O(h
3
) provides the order
of the error incurred in neglecting this part of the series expansion when calculating f(x
0
+h).

From the Taylor series expansion shown above we can obtain an expression for the derivative
f(x
0
) as


In practical applications of finite differences, we will replace the first-order derivative df/dx
at x = x
0
, with the expression (f(x
0
+h)-f(x
0
))/h, selecting an appropriate value for h, and
indicating that the error introduced in the calculation is of order h, i.e., error = O(h).


Forward, backward and centered finite difference approximations
to the first derivative

The approximation
df/dx = (f(x
0
+h)-f(x
0
))/h

is called a forward difference formula because the derivative is based on the value x = x
0
and
it involves the function f(x) evaluated at x = x
0
+h, i.e., at a point located forward from x
0
by an
increment h.

If we include the values of f(x) at x = x
0
- h, and x = x
0
, the approximation is written as

df/dx = (f(x
0
)-f(x
0
-h))/h

and is called a backward difference formula. The order of the error is still O(h).

A centered difference formula for df/dx will include the points (x
0
-h,f(x
0
-h)) and (x
0
+h,f(x
0
+h)).
To find the expression for the formula as well as the order of the error we use the Taylor series
expansion of f(x) once more. First we write the equation corresponding to a forward
expansion:
f(x
0
+h) = f(x
0
)+f(x
0
)h+1/2f(x
0
)h
2
+1/6f
(3)
(x
0
) h
3
+ O(h
4
).

Next, we write the equation for a backward expansion:

f(x
0
-h) = f(x
0
)-f(x
0
)h+1/2f(x
0
)h
2
-1/6f
(3)
(x
0
) h
3
+ O(h
4
).

Subtracting these two equations results in

f(x
0
+h)- f(x
0
-h) = 2f(x
0
)h+1/3f
(3)
(x
0
) h
3
+O(h
5
).

Notice that the even terms in h, i.e., h
2
, h
4
, , vanish. Therefore, the order of the remaining
terms in this last expression is O(h
5
). Solving for f(x
0
) from the last result produces the
following centered difference formula for the first derivative:


). (
) ( ) (
) (
! 2
) ( " ) ( ) (
) ( '
0 0 2 0 0 0
0
h O
h
x f h x f
h O h
x f
h
x f h x f
x f +
+
+ +
+

), ( ) (
3
1
2
) ( ) (
|
4 2 ) 3 ( 0 0
0
h O h x f
h
h x f h x f
dx
df
x x
+ +


Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz

or,


This result indicates that the centered difference formula has an error of the order O(h
2
), while
the forward and backward difference formulas had an error of the order O(h). Since h
2
<h, the
error introduced in using the centered difference formula to approximate a first derivative will
be smaller than if the forward or backward difference formulas are used.


Forward, backward and centered finite difference approximations
to the second derivative

To obtain a centered finite difference formula for the second derivative, we'll start by using
the equations for the forward and backward Taylor series expansions from the previous section
but including terms up to O(h
5
), i.e.,


f(x
0
+h) = f(x
0
)+f(x
0
)h+1/2f(x
0
)h
2
+1/6f
(3)
(x
0
) h
3
+ 1/24f
(4)
(x
0
) h
4
+ O(h
5
).


and

f(x
0
-h) = f(x
0
)-f(x
0
)h+1/2f(x
0
)h
2
1/6f
(3)
(x
0
) h
3
+ 1/24f
(4)
(x
0
) h
4
O(h
4
).


Next, add the two equations and solve for f(x
0
):

d
2
f/dx
2
= [f(x
0
+h)2f(x
0
)+f(x
0
-h)]/h
2
+ O(h
2
).

Forward and backward finite difference formulas for the second derivatives are given,
respectively, by

d
2
f/dx
2
= [f(x
0
+2h)2f(x
0
+h)+f(x0)]/h
2
+ O(h),
and
d
2
f/dx
2
= [f(x
0
) 2f(x
0
-h)+f(x
0
-2h)]/h
2
+ O(h).


Solution of a first-order ODE using finite differences - Euler forward
method

Consider the ordinary differential equation,

dy/dx = g(x,y),
subject to the boundary condition,
y(x
1
) = y
1
.

To solve this differential equation numerically, we need to use one of the formulas for finite
differences presented earlier. Suppose that we use the forward difference approximation for
dy/dx;, i.e.,
). (
2
) ( ) (
2 0 0
h O
h
h x f h x f
dx
df
+


Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz

dy/dx = (y(x+h)-y(x))/h.

Then, the differential equation is transformed into the following difference equation:

(y(x+h)-y(x))/h = g(x,y),
from which,
y(x+h) = y(x)+hg(x,y).

This result is known as Euler's forward method for numerical solution of first-order ODEs.

Since we know the boundary condition (x
1
,y
1
) we can start by solving for y at x
2
= x
1
+h, then
we solve for y at x
3
= x
2
+h, and so on. In this way, we generate a series of points (x
1
, y
1
), (x
2
,
y
2
), , (x
n
, y
n
), which will represent the numerical solution to the original ODE. The upper
limit of the independent variable x
n
is either given or selected arbitrarily during the solution.

The term "discretizing the domain of the independent variable" refers to obtaining a series of
values of the independent variable, namely, x
i
, i = 1,2;,..., n, that will be used in the solution.
Suppose that the range of the independent variable (a,b) is known, and that we use a constant
value h = x to divide the range into n equal intervals. By making x
1
= a, and x
n
= b, then we
find that the values of x
i
, i = 2,3, ... n, are given by

x
i
= x
1
+(i-1)x = a+(i-1)x,

and that for i = n, x
n
= x
1
+(n-1)x. This latter result can be used to find n given x,

n = (x
n
-x
1
)/ x + 1 = (b-a)/ x + 1,
or, to find x given n,
x = (x
n
-x
1
)/(n-1) = (b-a)/(n-1).

The recurrent equation for solving for y is given by

y
i+1
= y
i
+ xg(x
i
,y
i
),

for i = 1,2, ..., n-1. Because the method solves y
i+1
= f(x
i
,y
i
, x), i.e., one value of the
dependent variable at a time, the method is said to be an explicit method.

The following example illustrates the application of the Euler first-order method to the solution
of the differential equation dy/dx = g(x,y) = x + y using SCILAB. First, we define function
g(x,y):

-->deff('[Df]=g(x,y)','Df=x+y')

We solve the equation in the range of values of x from x
0
= 0 to x
n
= 2.0 with an increment Dx =
0.1. The initial condition is y
0
= 1.0 for x
0
= 0:

-->x0 = 0; y0 = 1; Dx = 0.1; xn = 2.0;

The following commands generate a vector of values of x, a vector y of the same length of x,
initialized with zeros, and determines the value of n as the length of vector y (or x):

-->x=[x0:Dx:xn]; y = zeros(x); n = length(y);

The following forend loop takes care of calculating the values of y
i
for i = 2,3, , n:


Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
-->for j = 1:n-1
--> y(j+1) = y(j) + Dx*g(x(j),y(j));
-->end;

To produce a plot of the results we determine the minimum and maximum values of y:

-->ymin = min(y), ymax = max(y)
ymin =

0.
ymax =

3.7274999

The plot is generated by using:

-->plot2d(x,y,1,'011',' ',[0 0 2 4])

-->plot2d(x,y,-9,'011',' ',[0 0 2 4])

-->xtitle('Euler solution dy/dx = x + y, Dx = 0.1','x','y(x)')



A function to implement Eulers first-order method

The following function, Euler1, implements the calculation steps outlined in the previous
example. The function detects if there is overflowing introduced in the solution and stops the
calculation at that point providing the current results.

function [x,y] = Euler1(x0,y0,xn,Dx,g)

//Euler 1st order method solving ODE
// dy/dx = g(x,y), with initial
//conditions y=y0 at x = x0. The
//solution is obtained for x = [x0:Dx:xn]
//and returned in y

ymaxAllowed = 1e+100;

x = [x0:Dx:xn]; y = zeros(x); n = length(y); y(1) = y0;

for j = 1:n-1
y(j+1) = y(j) + Dx*g(x(j),y(j));
if y(j+1) > ymaxAllowed then
disp('Euler 1 - WARNING: underflow or overflow');

Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
disp('Solution sought in the following range:');
disp([x0 Dx xn]);
disp('Solution evaluated in the following range:');
disp([x0 Dx x(j)]);
n = j; x = x(1,1:n); y = y(1,1:n);
break;
end;
end;

//End function Euler1


Next, we use function Euler1 to solve the differential equation from the previous example,
namely, dy/dx = g(x,y) = x+y, for different values of the x increment, x = 0.5, 0.2, 0.1, and
0.05, with the same initial conditions and range of values of x as before:

-->getf('Euler1')

-->deff('[Df]=g(x,y)','Df = x+y')

-->[x1,y1]=Euler1(0,1,2,0.5,g);

-->[x2,y2]=Euler1(0,1,2,0.2,g);

-->[x3,y3]=Euler1(0,1,2,0.1,g);

-->[x4,y4]=Euler1(0,1,2,0.05,g);


The exact solution for this equation is y(x) = -x - 1 + 2e
x
. Set of values of the exact solution
are calculated as follows:

-->xx = [0:0.1:2]; yy = -xx-1+2.*exp(xx);

To plot the exact and numerical solutions we first determine the minimum and maximum
values of y:

-->ymax = max([y1 y2 y3 y4 y5 yy])
ymax = 11.778112

-->ymin = min([y1 y2 y3 y4 y5 yy])
ymin = 1.

The plot of the solutions is produced through the use of the following calls to function plot2d:

-->plot2d(xx,yy,1,'011',' ',[0 0 2 12])
-->plot2d(x1,y1,-1,'011',' ',[0 0 2 12])
-->plot2d(x2,y2,-2,'011',' ',[0 0 2 12])
-->plot2d(x3,y3,-3,'011',' ',[0 0 2 12])
-->plot2d(x4,y4,-4,'011',' ',[0 0 2 12])
-->xtitle('Euler 1st order - dy/dx = x+y','x','y(x)')



Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz


A second example of application of function Euler1 is shown next for the differential equation
dy/dx = xy + 1, with initial condition x
0
= 0, y
0
= 1, in the range 0 < x < 2, with x = 0.5, 0.2,
0.1, 0.05, and 0.01. The SCILAB commands used are exactly the same as before except for the
definition of function g(x,y) and the title of the plot. The function g(x,y) = xy+1 is defined as:

-->deff('[Df]=g(x,y)','Df=x*y+1')
Warning :redefining function: g

Numerical solutions to the differential equation for the different values of x are obtained
from:

-->[x1,y1]=Euler1(0,1,2,0.5,g);

-->[x2,y2]=Euler1(0,1,2,0.2,g);

-->[x3,y3]=Euler1(0,1,2,0.1,g);

-->[x4,y4]=Euler1(0,1,2,0.05,g);

-->[x5,y5]=Euler1(0,1,2,0.01,g);

Next, we determine the minimum and maximum values of y:

-->ymin = min([y1 y2 y3 y4 y5 yy])
ymin = 1.

-->ymax = max([y1 y2 y3 y4 y5 yy])
ymax = 15.872217

We define a plot rectangle as:

-->rect = [0 0 2 16]
rect =

! 0. 0. 2. 16. !

The plot of the numerical solution is accomplished through:

-->plot2d(x1,y1,-1,'011',' ',rect)
-->plot2d(x2,y2,-2,'011',' ',rect)
-->plot2d(x3,y3,-3,'011',' ',rect)
-->plot2d(x4,y4,-4,'011',' ',rect)
-->xtitle('Euler 1st order - dy/dx = x*y+1','x','y(x)')

Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz




The following example solves the differential equation dy/dx = g(x,y) = x + sin(xy) in the
interval 0< x < 6.5, with initial conditions x
0
= 0, y
0
= 1, for x = 0.5, 0.2, 0.1, 0.05, and 0.01.
The steps are the same as in the two previous example:

-->deff('[Df]=g(x,y)','Df=x+sin(x*y)')
Warning :redefining function: g


-->[x1,y1]=Euler1(0,1,6.5,0.5,g);

-->[x2,y2]=Euler1(0,1,6.5,0.2,g);

-->[x3,y3]=Euler1(0,1,6.5,0.1,g);

-->[x4,y4]=Euler1(0,1,6.5,0.05,g);

-->ymin = min([y1 y2 y3 y4 y5 yy])
ymin = 1.

-->ymax = max([y1 y2 y3 y4 y5 yy])
ymax = 22.628614

-->rect = [0 0 7 25]

rect =
! 0. 0. 7. 25. !


-->plot2d(x1,y1,-1,'011',' ',rect)
-->plot2d(x2,y2,-2,'011',' ',rect)
-->plot2d(x3,y3,-3,'011',' ',rect)
-->plot2d(x4,y4,-9,'011',' ',rect)
-->xtitle('Euler 1st order - dy/dx = x+sin(x*y)','x','y(x)')


Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz



Finite difference formulas using indexed variables

In the presentation of the Euler forward method, above, we demonstrated how you can get,
from the general formula for the first derivative,


dy/dx = [y(x+h)-y(x)]/h,


the recurrence formula for the explicit solution, namely,


y
i+1
= y
i
+ xg(x
i
,y
i
),


for i = 1,2,..., n-1. This suggest re-writing the formula for the derivative as,


dy/dx = (y
i+1
-y
i
)/x + O(x).


Using this sub-index notation, we can summarize the forward, centered, and backward
approximations for the first and second derivatives as shown below:



First Derivative
FORWARD: dy/dx = (y
i+1
y
i
)/x+O(x).

CENTERED: dy/dx = (y
i+1
y
i-1
)/(2x)+O(x
2
).

BACKWARD: dy/dx = (y
i
y
i-1
)/x+O(x).


Second Derivative
FORWARD: d
2
y/dx
2
= (y
i+2
2y
i+1
+y
i
)/(x
2
)+O(x).

CENTERED: d
2
y/dx
2
= (y
i+1
2y
i
+y
i-1
)/(x
2
)+O(x
2
).


Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
BACKWARD: d
2
y/dx
2
= (y
i
2y
i-1
+y
i-2
)/(x
2
)+O(x).



Solution of a first-order ODE using finite differences - an implicit
method

Consider again the ordinary differential equation, dy/dx = g(x,y), subject to the boundary
condition, y(x
1
) = y
1
. This time, however, we use the centered difference approximation for
dy/dx, i.e.
dy/dx = (y(x+h)-y(x-h))/(2*h).

With this approximation the ODE becomes,

(y(x+h)-y(x-h))/(2*h) = g(x,y).

In terms of sub-indexed variables, this latter equation can be written as:

y
i-1
+2xg(x
i
,y
i
)-y
i+1
= 0, ( i = 2,3, ..., n-1 )

where the substitutions y(x) = y
i
,y(x+h) = y
i+1
,y(x-h) = y
i-1
, and h = x, have been used.

If the function g(x,y) is linear in y, then the equations described above consist of a set of (n-2)
equations. For example, if n = 5, we have 3 equations:

y
1
+2x g(x
2
,y
2
)-y
3
= 0
y
2
+2x g(x
3
,y
3
)-y
4
= 0
y
3
+2x g(x
4
,y
4
)-y
5
= 0

Since y
1
is known (it is the initial condition), there are still 4 unknowns, y
2
, y
3
, y
4
, and y
5
. We
need to find a fourth equation to obtain a solution. We could use, for example, the forward
difference equation applied to i = 1, i.e.,

(y
2
-y
1
)/x = g(x
1
,y
1
),
or
y
2
-x g(x
1
,y
1
)-y
1
= 0.

The values of x
i
, and n (or x), can be obtained as in the Euler forward (explicit) solution.


Example 1 -- Solve the ODE
dy/dx = y sin(x),

with initial conditions y(0) = 1, in the interval 0 < x < 5. Use x = 0.5, or n = (5-0)/0.5 + 1 = 11.

Exact solution: the exact is y(x) = exp(-cos(x))/(cosh(1)-sinh(1)).

Numerical solution: Using a centered difference formula for dy/dx, i.e.,

dy/dx = (y
i+1
y
i-1
)/(2x),

into the ODE, we get (y
i+1
y
i-1
)/(2x) = y
i
sin(x
i
), which results in the (n-2) implicit equations:

y
i-1
+ 2xsin(x
i
)y
i
y
i+1
= 0, (i = 2, 3, , n-1).

Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz

We already know that
y
1
= 1

(initial condition), thus we have (n-1) unknowns left. We still need to come up with an
additional equation, which could be obtained by using a forward difference formula for i = 1,
i.e.,

dy/dx|
x= 1
= (y
2
-y
1
)/x = -y
1
sin(x
1
),
or
(1+x sin(x
1
))y
1
- y
2
= 0.

These equations can be written in the form of a matrix equation, for example, for n = 5:

where y0 represents the initial condition for y. [Note: The data requires n = 11. The example
for n = 5 is presented above to provide a sense of the algorithm to fill out the matrix of data].
The matricial equation can be written as A y = b. Matrix A and column vector b can be defined
using SCILAB, as indicated below, and the solution found by using left-division. First, we enter
the basic data for the problem:

-->x0=0;xn=5;Dx=0.5;y0=1;x=[x0:Dx:xn];n=(xn-x0)/Dx+1
n = 11.

Next, we fill the main diagonal, and the two diagonals below the main diagonal in matrix A
using:

-->A=zeros(n,n); A(1,1) = 1; for j =2:n, A(j,j)=-1; end;

-->A(2,1) = 1+Dx*sin(x(1)); for j = 3:n, A(j,j-1)=2*Dx*sin(x(j-1)); end;
//second diagonal

-->for j = 3:n, A(j,j-2) = 1; end; //Third diagonal

The right-hand side vector is defined as:

-->b = zeros(n,1); b(1) = 1; //Right-hand side vector

The implicit solution is obtained from:

-->y = A\b; //Solving for y

To compare the implicit solution we calculate also the explicit solution obtained through the
Euler first-order solution:

-->deff('[z]=ff(x,y)','z=y*sin(x)')
-->getf(Euler1)
-->[xx,yy]=Euler1(x0,y0,xn,Dx,ff);

To produce data reproducing the exact solution we use:
1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1




+
0
0
0
0
0
1 ) sin( 2 1 0 0
0 1 ) sin( 2 1 0
0 0 1 ) sin( 2 1
0 0 0 1 ) sin( 1
0 0 0 0 1
5
4
3
2
1
4
3
2
1
y
y
y
y
y
y
x x
x x
x x
x x

Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz

-->deff('[y]=fE(x)','y=exp(-cos(x))/(cosh(1)-sinh(1))')
-->xE = [0:0.05:5]; yE = fE(xE);

The following commands will generate the plot showing the exact, implicit, and explicit
solution in the same set of axes:

-->plot2d(xE',yE',1,'011',' ', [0 0 5 8])
-->plot2d(x',y,-1,'011',' ',[0 0 5 8])
-->plot2d(xx',yy',-9,'011',' ',[0 0 5 8])
-->xtitle('+ Implicit o Explicit','x','y')







Explicit versus implicit methods

The idea behind the explicit method is to be able to obtain values such as

y
i+1
= f(xi, y
i
), y
i+2
= f(x
i
,x
i+1
,y
i
,y
i+1
), etc.

In other words, your solution proceeds by solving explicitly for a new unknown value in the
solution array, given all previous values in the array. On the other hand, implicit methods
imply the simultaneous solution of n linear algebraic equations that provide, at once, the
elements of the solution array. With this distinction in mind between explicit and implicit
methods, we outline explicit and implicit solutions for second-order, linear ODEs.


Outline of explicit solution for a second-order ODE

For example, to solve the ODE
d
2
y/dx
2
+y = 0,

in the x-interval (0,20) subject to y(0) = 1, dy/dx = 1 at y = 0. Use x = 0.1.

First, we discretize the differential equation using the finite difference approximation

d
2
y/dx
2
= (y
i+2
-2y
i+1
+y
i
)/(x
2
) ,


Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
which results in
(y
i+2
-2y
i+1
+y
i
)/(x
2
)+y
i
= 0.

An explicit solution can be obtained from the recurrence equation:

y
i+2
= 2y
i+1
-(1+x
2
)y
i
, i = 1, 2, ..., n-2;.

This equation is based on the two previous values of y
i
, therefore, to get started we need the
values y = y
1
, and y = y
2
. The value y
1
is provided in the initial condition, y(0) = 1, i.e.,

y
1
= 1.

The value of y
2
can be obtained from the second initial condition, dy/dx = 1, by replacing the
derivative with the finite difference approximation:

dy/dx = (y
2
- y
1
)/x,
which results in
(y
2
- y
1
)/ x = 1,
or
y
2
= y
1
+ x.

The x-domain is discretized in a similar fashion as in the previous examples for first derivatives,
i.e., by making x
1
= a, and x
n
= b, and computing the values of x
i
, i = 2,3, ... n, with

x
i
= x
1
+(i-1)x = a+(i-1)x,
where,
n = (x
n
-x
1
)/x+1 = (b-a)/x+1.

The implementation of the solution for this example is left as an exercise for the reader.

Outline of the implicit solution for a second-order ODE

We use the same problem from the previous section: solve the ODE

d
2
y/dx
2
+y = 0,

in the x-interval (0,20) subject to y(0) = 1, dy/dx = 1 at x = 0. Use x = 0.1.

We discretize the differential equation using the finite difference approximation

d
2
y/dx
2
= (y
i+2
-2y
i+1
+y
i
)/(x
2
) ,
which results in
(y
i+1
-2*y
i
+y
i-1
)/(x
2
)+y
i
= 0.

From this result we get the following implicit equations:

y
i-1
-(2-x
2
)y
i
+y
i+1
= 0,

for i = 2,3, ..., n-1. There are a total of (n-2) equations. Since we have n unknowns, i.e., y
1
,
y
2
, ...,y
n
, we need two more equations to solve a system of linear equations. The remaining
equations are provided by the two initial conditions:

From the initial condition, y(0) = 1, we can write y
1
= 1. For the second initial condition,
dy/dx = 1, at x = 0, we will use a forward difference, i.e.,

Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz

dy/dx = (y
2
- y
1
)/ x,
or
y
2
- y
1
= x.

The x-domain is discretized in a similar fashion as in the previous examples. The n equations
resulting from discretizing the domain can be written as a matrix equation similar to that of
Example 1. Solution to the matrix equation can be accomplished, for example, through the use
of left-division for matrices. The implementation of the solution for this example is left as an
exercise for the reader.

SCILAB provides a number of functions for the numerical solution of differential equations.
These functions are designed to operate on single differential equations (i.e., similar to the
examples presented so far), as well as on systems of differential equations. Therefore, before
presenting the SCILAB functions for solving ordinary differential equations, we present some
concepts related to systems of such equations.

S Sy ys st te em ms s o of f o or rd di in na ar ry y d di if ff fe er re en nt ti ia al l e eq qu ua at ti io on ns s

To introduce the idea of systems of differential equations we will limit the coverage of the
subject to first-order, linear equations with constant coefficients. A system of ordinary
differential equations consists of a set of two or more equations with an equal number of
unknown functions, ( ) y
1
x , ( ) y
2
x , etc. As an example consider the following homogeneous
system:
+
dy
1
dx
3 y
1
2 y
2
0
,
+
dy
2
dx
y
1
y
2
0
.

In a homogeneous system the right-hand sides of the equations are zero. The following
example represents a non-homogeneous system of ordinary differential equations:

+
dy
1
dx
2 y
1
5 y
2
( ) sin x
,
+
dy
2
dx
4 y
1
3 y
2
e ee e
x
.

Systems of ordinary differential equations using matrices

A homogeneous system of ODEs can be written as a single matrix differential equation by using
vector functions and a matrix of coefficients as illustrated in the following example. First, we
re-write the homogeneous system presented above to read:


dy
1
dx
+ 3 y
1
2 y
2
,


dy
2
dx
y
1
y
2
.

Then, we define the vector function f(x) = [y
1
(x) y
2
(x)]
T
, and the matrix A = [-3 2; 1 -1], and
write the differential equation:

d
dx
f(x) = A f(x).

This result is equivalent to writting:

Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz

.
) (
) (
1 1
2 3
) (
) (
2
1
2
1
1
]
1

1
]
1

1
]
1

x y
x y
x y
x y
dx
d


The non-homogeneous system presented earlier can be re-written as


dy
1
dx
+ 2 y
1
5 y
2
( ) sin x
,


dy
2
dx
+ 4 y
1
3 y
2
e ee e
x
.

For this system we will use the same vector function f(x) defined earlier, but change the
matrix A to A = [-2 5; 4 -3]. We also need to define a new vector function, g(x) = [-sin(x)
exp(x)]
T
. With these definitions, we can re-write the non-homogeneous system as:

d
dx
f(x) = A f(x)+ g(x),
or

.
) exp(
) sin(
) (
) (
3 4
5 2
) (
) (
2
1
2
1
1
]
1

+
1
]
1

1
]
1

1
]
1

x
x
x y
x y
x y
x y
dx
d



Systems of linear homogeneous ODEs - solution using matrices

Consider the system of linear nonhomogeneous ODEs with constant coefficients given by:

dy
1
/dx = y
1
+y
3
, dy
2
/dx = y
1
+y
2
-y
3
, dy
3
/dx = 5y
1
+y
2
+y
3.


In matricial form, this can be written as:



1
1
1
]
1

1
1
1
]
1


1
1
1
]
1

) (
) (
) (
1 1 5
1 1 1
1 0 1
) (
) (
) (
3
2
1
3
2
1
x y
x y
x y
x y
x y
x y
dx
d
.

or,
d
dx
f(x) = A f(x).

We can use the eigenvalues and eigenvectors of matrix A to obtain the solution to the system
of homogeneous equations by following this procedure:

1. Determine eigenvalues of the nxn matrix A. Call these eigenvalues
1
,
2
,...,
n
.

2. Determine the eigenvectors of the nxn matrix A. Call these eigenvectors

Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz

x
1
= [ x
11
, x
12
,..., x
, 1 n
], x
2
= [ x
21
, x
22
,..., x
, 2 n
], .... x
n
= [ x
, n 1
, x
, n 2
,..., x
, n n
].

3. The general solutions to the system are put together as follows:

( ) y
1
x = x
11
B
1
exp(
1
x)+ x
21
B
2
exp(
2
x)+.. x
, 1 n
B
n
exp(
n
x),

( ) y
2
x = x
21
B
1
exp(
1
x)+ x
22
B
2
exp(
2
x)+.. x
, 2 n
B
n
exp(
n
x),
.
.
.

( ) y
n
x = x
, n 1
B
1
exp(
1
x)+ x
, n 2
B
2
exp(
2
x)+.. x
, n n
B
n
exp(
n
x).

i.e.,
( ) y
k
x =

j 1
n
x
, k j
B
j
e ee e
( )
j
x
, k = 1,2,..., n

These general solutions include
n
unknown constants, B
1
, B
2
, ..., B
n
. We will need
n

initial conditions to solve for the
n
constants to uniquely determine the solution to the
system.

For the system under consideration, the solution steps can be translated into SCILAB
instructions as shown below. To obtain eigenvalues and eigenvectors we use the user-defined
function eigenvectors defined in Chapter 5.

-->A = [1,0,1;1,1,-1;5,1,1]
A =

! 1. 0. 1. !
! 1. 1. - 1. !
! 5. 1. 1. !

-->getf(eigenvectors)


-->[x,lambda] = eigenvectors(A)
lambda =

! 3.1149075 - .8608059 .7458983 !
x =

! .4170021 - .3827458 - .1983289 !
! - .2198294 .5884340 .9788391 !
! .8819208 .7122156 .0503957 !

The solutions are, therefore,

y
1
(x) = 0.4170021B
1
e
3.1149x

-0.3827458

B
2
e
-0.8608059x
-0.198328 B
3
e
0.7458983x
,
y
2
(x) = - 0.219829 B
1
e
3.1149x

+ 0.5884340B
2
e
-0.8608059x
+ 0.9788391 B
3
e
0.7458983x
,
y
3
(x) = 0.8819208B
1
e
3.1149x

+ 0.7122156 B
2
e
-0.8608059x
+ 0.0503957B
3
e
0.7458983x
.


Substituting the following initial conditions: ( ) y
1
0 1 , ( ) y
2
0 2 , and ( ) y
3
0 3 , produce a
system of linear equations:

Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz

0.4170021B
1
-0.3827458

B
2
-0.198328 B
3
= 1
- 0.219829 B
1
+ 0.5884340B
2
+ 0.9788391 B
3
= 2
0.8819208B
1
+ 0.7122156 B
2
+ 0.0503957B
3
= 3

which can be solved using left-division as follows:

-->AA=x;bb=[1;2;3]
bb =

! 1. !
! 2. !
! 3. !

-->B=AA\bb
B =

! 3.5181246 !
! - .3600066 !
! 3.049763 !


The results are B
1
= 3.5181246, B
2
= -0.3600066, and B
3
= 3.049763.

To determine the coefficients in the solutions we can use:

-->C=AA.*[B B B]

C =

! 1.4670652 - 1.3465474 - .6977459 !
! .0791400 - .2118401 - .3523886 !
! 2.6896495 2.1720889 .153695 !

Thus, the solutions are:

y
1
(x) = 1.4670652e
3.1149x

- 1.3465474e
-0.8608059x
-0.6977459e
0.7458983x
,
y
1
(x) = 0.0791400e
3.1149x

-0.2118401e
-0.8608059x
-0.3523886e
0.7458983x
,
y
1
(x) = 2.6896495e
3.1149x

+ 2.1720889e
-0.8608059x
+ 0.153695e
0.7458983x
.
These solutions can be shown graphically by defining the following three functions:

-->deff('[y]=y1(x)',['y=0';'for j=1:3';'y=y+C(1,j)*exp(lambda(j)*x)';'end'])

-->deff('[y]=y2(x)',['y=0';'for j=1:3';'y=y+C(2,j)*exp(lambda(j)*x)';'end'])

-->deff('[y]=y3(x)',['y=0';'for j=1:3';'y=y+C(3,j)*exp(lambda(j)*x)';'end'])

A plot of the solution is shown next:

-->xx=[0:0.1:1];yy1=real(y1(xx));yy2=real(y2(xx));yy3=real(y3(xx));

-->xset('window',1);xset('mark',[-1 -2 -3],1);

-->plot2d([xx' xx' xx'],[yy1' yy2' yy3'],[-1,-2,-3],'011',' ',[0 -20 1 80])

-->plot2d([xx' xx' xx'],[yy1' yy2' yy3'],[1,2,3],'011',' ',[0 -20 1 80])


Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz



The solution to the non-homogeneous system,
d
dx
f(x) = A f(x), can be accomplished in a
straightforward manner by using the equation,

f(x) = exp(Ax)b,

where b is a vector containing the initial conditions, i.e.,

b = [y
1
(0) y
2
(0) y
n
(0)]
T
.

and the expression exp(At) is a matrix defined as:

exp(At) = I + At +
1
! 2

A
2
t
2
+... = ,
!
1
1

+
k
k k
t A
k
I

where I is the identity matrix,
A
2
= AA,
A
3
=
A
2
A, ... To evaluate the expression exp(Ax),
SCILAB provides function expm.

The solution to the linear system under consideration, using the equation, f(x) = exp(Ax)b, can
be obtained as follows using SCILAB (matrix A is the same matrix of coefficients of the ODE
system defined earlier):

-->deff('[y] = fm(t)','y=real(expm(A*t)*bb)')

A plot of the solution using this matrix approach is produced with the following SCILAB
statements:

-->xx=[0:0.1:1]; n = length(xx)
n = 11.

-->ym=[];for j=1:n, ym=[ym fm(xx(j))]; end;

-->plot2d([xx',xx',xx'],[ym(1,:)',ym(2,:)',ym(3,:)'],[1,2,3])

-->xtitle('Solution to system of ODEs','x','y(x)')



Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz



Systems of linear nonhomogeneous ODEs - solution using matrices

Consider the system of linear nonhomogeneous ordinary differential equations with constant
coefficients given by


dy
1
dx
+ y
1
y
3
+
e ee e
x
,

dy
2
dx
y
2
y
3
+
( ) sin x
, and

dy
3
dx
+ + 5 y
1
y
2
y
3
+
( ) cos x


This system can be written as a matricial differential equation as:

1
]
1
1
1
1
1
1
1
1
1
1
1
1
1

x
( ) y1 x

x
( ) y2 x

x
( ) y3 x
+

1
]
1
1
1
1
1
1 0 1
0 1 -1
5 1 1

1
]
1
1
1
1
1
( ) y1 x
( ) y2 x
( ) y3 x

1
]
1
1
1
1
1
e ee e
x
( ) sin x
( ) cos x


In general, a nonhomogeneous system can be written in matricial form as

d
dx
f(x) = A f(x) + g(x).

For the case under consideration, we have:

:= A

1
]
1
1
1
1
1
1 0 1
0 1 -1
5 1 1

:= g

1
]
1
1
1
1
1
e ee e
x
( ) sin x
( ) cos x


A solution to the system of ODEs is given by

f(x) =

(x)C + (x) d

(
(
( ) x
( ) -1
( ) g x x ,

Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz

where (x) = exp(Ax) is known as a fundamental matrix of the system, (x)
(-1)

is the inverse
matrix of (x), and C is a vector of constants, i.e., C = [C
1
C
2
C
n
].

Unlike the homogeneous case, the presence of the integral in the solution f(x) complicates the
calculation or programming of the solution using SCILAB. The reader is referred to symbolic
packages such as Maple or Mathematica to obtain such solutions. Numerical solutions,
however, are possible with SCILAB, as will be demonstrated in subsequent sections of this
book.


Converting second-order linear equations to a system of equations

A second-order linear ODE of the form + +
d
2
y
dx
2
b dy
dx
c y ( ) r x , can be transformed into a
linear system of equations by introducing the relationship,
( ) u x
dy
dx
, so that
d
2
y
dx
2
du
dx
, thus,
the equation reduces to
+ +
du
dx
b u c y ( ) r x
, or

du
dx
+ b u c y ( ) r x
. The resulting system of
equations is:


du
dx
+ b u c y ( ) r x
,

dy
dx
u
.


Which can be written in matricial form as df/dx = A f(x)+g(x), with


.
0
) (
) ( ,
0 1
, ) (
1
]
1

1
]
1

1
]
1

x r
x g
c b
A
y
u
x f


For example, the solution to the second order differential equation


+
d
2
y
dx
2
5 dy
dx
3 y x ,


can be obtained by solving the equivalent first-order linear system:



du
dx
+ + 5 u 3 y x
,


dy
dx
u
.



Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
The procedure outlined above to transform a second order linear equation can be used to
convert a linear equation of order
n
into a system of first-order linear equations. For
example, if the original ODE is written as:


+
d
n
y
dx
n
a
n 1
d
( ) n 1
y
dx
( ) n 1
+ ... + a
2
d
2
y
dx
2
+
a
1
dy
dx
+ a
0
y =
( ) r x
,

we can re-write it as


d
n
y
dx
n
a
n 1
d
( ) n 1
y
dx
( ) n 1
- ... - a
2
d
2
y
dx
2
-
a
1
dy
dx
- a
0
y +
( ) r x
,

and transform it into a system of n first-order linear equations given by:


du
n 1
dx
a
n 1
u
n 1
- a
n 2
u
n 2
- ... - a
2
u
2
- a
1
u
1
- a
0
y +
( ) r x
,


du
n 2
dx
u
n 1
,

du
n 3
dx
u
n 2
, ...,

du
1
dx
u
2
,

dy
dx
u
1
,


or, in matricial form,

1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
]
1

1
1
1
1
1
1
1
1
]
1

0
0
0
0
) (
) (
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
, , ) (
0 1 3 2 1
1
2
2
1
!
"
! ! # ! ! !
"
"
"
"
!
x r
x g
a a a a a
A
y
u
u
u
u
x
n n n
n
n
f


For example, to transform the following fourth-order (n=4) linear ODE

+ + +
d
4
y
dx
4
3 d
3
y
dx
3
2 d
2
y
dx
2
5 dy
dx
y 0 ,

subjected to
y 1
,

dy
dx
-1
,
d
2
y
dx
2
0 ,
d
3
y
dx
3
-1 , at
x 0
, into a first-order linear system, we
would write:

du
3
/dx = -3u
3
(x)+2u
2
(x)-5u
1
(x)-y(x)+x
2
/2, du
2
/dx = u
3
(x), du
1
/dx = u
2
(x), and dy/dx = u
1
(x),

or


Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
.
0
0
0
2 /
) (
) (
) (
) (
0 1 0 0
0 0 1 0
0 0 0 1
1 5 2 3
) (
) (
) (
) (
2
1
2
3
1
2
3
1
1
1
1
1
]
1

+
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

x
x y
x u
x u
x u
x y
x u
x u
x u
dx
d


with v(x) = [u
3
(x);u
2
(x);u
1
(x); y(x)]
T
, A = [-3,2,-5,-1;1,0,0,0;0,1,0,0;0,0,1,0]; and g(x) = [x
2
/;0;
0; 0]
T
, the system of differential equations is written as dv/dx = Av+g(x). The initial
conditions are y(0) = 1, u
1
(0) = dy/dx = -1, u
2
(0) = du
1
/dx = d
2
y/dx
2
= 0, u
3
(0) = du
2
/dx =
d
2
u
1
/dx
2
= d
3
y/dx
3
= -1, or u
0
= [ -1;0;-1;1].








S SC CI IL LA AB B f fu un nc ct ti io on ns s f fo or r t th he e n nu um me er ri ic ca al l s so ol lu ut ti io on ns s o of f i in ni it ti ia al l
v va al lu ue e p pr ro ob bl le em ms s ( (I IV VP P) )

SCILAB provides function ode for the solution of initial value problems, i.e., those subject to
initial, rather than boundary, conditions.


Function ode

The simplest call to function ode is


y=ode(y0,x0,x,f)


where the point (x0,y0) represents the initial conditions for the differential equation


dy/dx = f(x,y),


and x is a vector of values of the independent variable x.


The function f can be a single function f(x,y) or a vector of functions,


.
) , , , , (
) , , , , (
) , , , , (
) , (
) , (
) , (
) , (
2 1
2 1 2
2 1 1
2
1
1
1
1
1
]
1

1
1
1
1
]
1

n n
n
n
n
y y y x f
y y y x f
y y y x f
y x f
y x f
y x f
y x f
"
!
"
"
!



Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
In the latter case, y is also a vector representing the variables y = [y
1
y
2
y
n
]
T
.

The first example shows the solution to the differential equation dy/dx = xy, in the range
0<x<2, with initial conditions y(0) = 1.

-->deff('[z]=f(x,y)','z=x.*y')

-->x0 = 0; y0 = 1; Dx = 0.1; xn = 2; x = [x0:Dx:xn];

-->y = ode(y0,x0,x,f);

-->plot(x,y,'x','y','ode solution to dy/dx = x*y')



In the second example, we solve the differential equation du/dt = u sin(t), in the range 0<t<10,
with initial condition u(0) = 5.

->deff('[w]=g(t,u)','w=u.*sin(t)')

-->t0=0;Dt=0.1;tn=1;u0=5;t=[t0:Dt:tn];

-->u = ode(u0,t0,t,g);

-->plot(t,u,'t','u','ode solution to du/dt = u*sin(t)')



The next example uses a system of two differential equations, namely,

dy
1
/dx = y
2
+ x

Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
dy
2
/dx = -y
1


with initial conditions y
1
=1, y
2
= 2, for x
0
= 0.

-->deff('[w]=f(x,y)',['y1=y(2)+x','y2=-y(1)','w = [y1;y2]'])

-->x0 = 0; Dx = 0.1; xn = 2; y0 = [1;2];

-->x = [x0:Dx:xn]; y = ode(y0,x0,x,f);

-->min(y),max(y)
ans = - 2.8322936
ans = 2.999147

-->plot2d([x',x'],[y(1,:)', y(2,:)'],[1,-1],'111','y1@y2',[0 -3 2 4])

-->xtitle('ode solution for two functions','x','y')


Numerical methods used in function ode

Function ode uses as default the numerical method known as the Adams method. In general,
Adams methods for solving the differential equation dy/dx = f(x,y) basically consist in obtaining
y
k+1
= y(x
k+1
) through the fitting of a polynomial that interpolates f(x,y) at selected points (x
j
,y
j
)
of the solution. The k-th order Adams-Bashforth method is an explicit method that uses the
current point (x
n
,y
n
) and k-1 points for the solution. The k-th order Adams-Moulton method is
an implicit method using points (x
n+1
,y
n+1
), (x
n
,y
n
) and k-2 previously calculated points. Adams
methods are also known as predictor-corrector methods because their solution involves an
initial predictor step, which is then modified in a corrector step that improves the solution.

Function ode can be called with an additional argument that specifies the numerical method to
be used for a solution. This argument becomes the first argument in the call to function ode.
The function call, if such optional argument is used, is

[y] = ode([type],y0,t0,t,f)

where type is one of the following character string: "adams", "stiff", "rk", "rkf", "fix", "discrete",
or "roots". The meaning of each of these options is presented next:

"adams": Default value. Uses the Adams predictor-corrector method for the solution.

Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz

"stiff": This option invokes the use of a Backward Differentiation Formula (BDF) for the
solution of stiff ordinary differential equations.

"rk": Invokes an adaptive, 4-th order, Runge-Kutta method.

"rkf": Invokes a Fehlberg's Runge-Kutta method of order 4 and 5 (RKF45).

"fix": Same solver as "rkf", a simpler user interface.

"root": ODE solver with root finding capabilities.

"discrete": Discrete time simulation.

The type options root and discrete do not represent different methods for solving ordinary
differential equations. Instead, they represent enhanced abilities for function ode for root
finding and combining discrete and continuous systems. The applications of these options will
be presented later.


Adams-Bashforth methods

These methods involve explicit functions for y
n+1
in terms of k points in the solution. The
equations describing the Adams-Bashforth methods of orders 1 through 5 are shown below. In
these equations the term f
n
represents f(x
n
,y
n
) and x is the increment in the independent
variable x:

y
n+1
= y
n
+ xf
n
(same as Euler 1
st
order method)
y
n+1
= y
n
+ (x /2)(3f
n
-f
n-1
)
y
n+1
= y
n
+ (x /12)(23f
n
-16f
n-1
+5f
n-2
)
y
n+1
= y
n
+ (x /24)(55f
n
-59f
n-1
+37f
n-2
-9f
n-3
)
y
n+1
= y
n
+ (x /720)(1901f
n
-2774f
n-1
+2616f
n-2
-1274f
n-3
+251f
n-4
)


Adams-Moulton methods

The following equations represent the Adams-Moulton methods of orders 1 through 5. These
are implicit methods, in which the term f
n+1
= f(x
n+1
,y
n+1
):

y
n+1
= y
n
+ xf
n+1

y
n+1
= y
n
+ (x /2)(f
n+1
-f
n
)
y
n+1
= y
n
+ (x /12)(f
n+1
+ 8f
n
-f
n-1
)
y
n+1
= y
n
+ (x /24)(9f
n+1
+ 19f
n
-5f
n-1
+f
n-2
)
y
n+1
= y
n
+ (x /720)(251f
n+1
+ 646f
n
-264f
n-1
+106f
n-2
-19f
n-3
+251)


Adams-Bashforth-Moulton methods

The Adams-Bashforth-Moulton predictor-corrector method uses one of the Adams-Bashforth
formulas to produce a first approximation for the solution (a predictor value), followed by an
evaluation of the function f(x
n+1
,y
n+1
) = f
n+1
, which is used in an Adams-Moulton formula to
produce a better approximation to the solution (a corrector value). Depending on the order of
the formula used for the predictor part of the algorithm, we need one or more values of the
solution to get the algorithm started. The Runge-Kutta method, described below, can be used

Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
to generate those few initial data values necessary to get the Adams-Bashforth-Moulton
procedure started.


Runge-Kutta methods

The general approach for the Runge-Kutta methods consists of the following equations

k
1
= x f(x
n
,y
n
)
k
2
= x f(x
n
+h,y
n
+k
1
)
y
n+1
= y
n
+ k
1
+ k
2


where the values of the parameters and are selected from Taylor series expansions
representing different orders of the approximation. For example, for an error of order x
3
,
the Runge-Kutta method is given by

k
1
= x f(x
n
,y
n
)
k
2
= x f(x
n
+ h,y
n
+k
1
)
y
n+1
= y
n
+ (k
1
+ k
2
)/2.

A fourth-order Runge-Kutta method is given by the formulas

k
1
= x f(x
n
,y
n
)
k
2
= x f(x
n
+ x /2,y
n
+k
1
/2)
k
3
= x f(x
n
+ x /2,y
n
+k
2
/2)
k
4
= x f(x
n
+ x, y
n
+ k
3
)
y
n+1
= y
n
+ (k
1
+ 2k
2
+ 2k
3
+k
4
)/6.


A fourth-fifth order Runge-Kutta method produces a fourth order and a fifth order estimate
(y
n+1
,z
n+1
) of the function through the following algorithm:


)
40
11
4104
1859
2565
3544
2
27
8
,
2
(
)
4104
845
513
3680
8
216
439
, (
)
2197
7296
2197
7200
2197
1932
,
13
12
(
)
32
9
32
3
,
8
3
(
)
4
,
4
(
) , (
5 4 3
2
1
6
4 3
2
1
5
3 2 1
4
2 1
3
1
2
1
k k k
k
k
y
x
x f x k
k k
k
k
y x x f x k
k k k
y
x
x f x k
k k
y
x
x f x k
k
y
x
x f x k
y x f x k
n n
n n
n n
n n
n n
n n
+ +

+
+ + +
+ +

+
+ +

+
+

+



6 5 4 3 1 1
5 4 3 1 1
55
2
50
9
56430
28561
12825
6656
135
16
5
1
4104
2197
2565
1408
216
25
k k k k k y z
k k k k y y
n n
n n
+ + + +
+ + +
+
+


Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz


Examples of function ode with different numerical methods

In this first example we solve the differential equation dy/dx = (x+1)e
-0.01y

in the range 0<x<5,
with initial condition y(0) = 0 using different methods within function ode:

-->deff('[z]=f(x,y)','z = (x+1).*exp(-0.01.*y)')

-->x0=0;Dx=0.1;xn=5.0;y0=0;x=[x0:Dx:xn];

-->y1 = ode('adams',y0,x0,x,f);

-->y2 = ode('rk',y0,x0,x,f);

-->y3 = ode('rkf',y0,x0,x,f);

-->min([y1 y2 y3]),max([y1 y2 y3])
ans = 0.
ans = 16.126815

-->plot2d([x',x',x'],[y1',y2',y3'],[-1,-2,-3],'111',...
-->'Adams method@Runge-Kutta 4th order@Runge-Kutta 4-5th order',...
-->[0 0 5 20])

-->xtitle('dy/dx = (x+1)*exp(-0.01*y)','x','y')



In the following example we solve the system of equations

dy
1
/dx = - y
1
, dy
2
/dx = y
2


in the range 0<x<5, with initial conditions y
1
(0) = -1, y
2
(0) = 1:

-->deff('[z]=f(x,y)',['z1=-y(1)','z2=y(2)','z=[z1;z2]'])
-->x0=0;Dx=0.1;xn=5.0;y0=[-1;1];x=[x0:Dx:xn];
-->y1 = ode('adams',y0,x0,x,f);

Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
-->y2 = ode('rk',y0,x0,x,f);
-->y3 = ode('rkf',y0,x0,x,f);

-->min(y1), max(y1)
ans = - 1.
ans = 148.4132

-->plot2d([x',x',x'],[y1(1,:)',y2(1,:)',y3(1,:)'],[-1,-2,-3],'111',...
-->'Adams method@Runge-Kutta 4th order@Runge-Kutta 4-5th order',...
-->[0 -1 5 0])

-->xtitle('System of equations - y(1)','x','y')



-->plot2d([x',x',x'],[y1(2,:)',y2(2,:)',y3(2,:)'],[-1,-2,-3],'111',...
-->'Adams method@Runge-Kutta 4th order@Runge-Kutta 4-5th order',...
-->[0 -1 5 150])





Stiff ordinary differential equations

There are a number of definitions of a stiff matrix (see, for example, Hoffman, J.D., 1992,
Numerical Methods for Engineers and Scientists, McGraw-Hill, Inc., New York) based on their
requirements for stability or on the presence of a rapidly decaying transient. The concept of
stability in the numerical solution of an ordinary differential equation involves the analysis of

Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
the error inherent in the numerical method. If a small error introduced in the first step of the
numerical solution remains constant or decays as solution steps accumulate, the solution is said
to be stable. If, on the other hand, a small error involved in the first step of the solution
increases without bound as the solution steps accumulate the solution is said to be unstable.

Thus, based on the numerical solution of an ordinary differential equation, a stiff equation is
defined as one which requires a step size so small for stability that round-off errors (inherent in
computer calculations) become significant. Alternatively, a stiff equation is one that contains
some transient component that decays rapidly compared to the main transient component.

As an example, consider the ordinary differential equation dy/dx = f(x,y) = -1000(y-x)+2001,
whose exact solution is y(x) = -exp(-1000x)+x+2. The solution is composed of two parts, y
1
(x) =
-exp(-1000x), and y
2
(x) = x+2, which vary at significantly different rates as x varies. The first
component, y
1
(x), goes quickly to zero, while the second component, y
2
(x), produces a simple
linear solution. Component y
1
(x) is only significant for very small values of x. The following
two graphs illustrate the behavior of the solution y(x) for small and relatively large values of x.


-->deff('[y]=f(x)','y=-exp(-1000.*x)+x+2')

-->plot(xs,ys,'x','y','solution for small values of x')







-->xl=[0:0.001:1]; yl=f(xl);

-->plot(xl,yl,'x','y','solution for larger values of x')


Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz

To determine the effect that the increment in the independent variable, x, has on the
solution, we attempt solutions using function Euler1 that implements Eulers first-order
method using values of x = 0.0005, 0.0010, 0.0020, 0.0025. We then plot the solution to
compare the results for different values of x.

-->deff('[dydx]=g(x,y)','dydx=-1000.*(y-x)+2001')
-->getf('Euler1')
-->Dx=0.0005;x0=0;xn=0.01;y0=1;[x1,y1]=Euler1(x0,y0,xn,Dx,g);
-->Dx=0.0010;x0=0;xn=0.01;y0=1;[x2,y2]=Euler1(x0,y0,xn,Dx,g);
-->Dx=0.0020;x0=0;xn=0.01;y0=1;[x3,y3]=Euler1(x0,y0,xn,Dx,g);
-->Dx=0.0025;x0=0;xn=0.01;y0=1;[x4,y4]=Euler1(x0,y0,xn,Dx,g);

-->min([y1 y2 y3 y4]), max([y1 y2 y3 y4])
ans = - 3.0525
ans = 5.3825

-->rect = [0 -4 0.01 6];
-->plot2d(x1,y1,-1,'011',' ',rect)
-->plot2d(x2,y2,-2,'011',' ',rect)
-->plot2d(x3,y3,-3,'011',' ',rect)
-->plot2d(x4,y4,-4,'011',' ',rect)
-->plot2d(x1,y1,1,'011',' ',rect)
-->plot2d(x2,y2,2,'011',' ',rect)
-->plot2d(x3,y3,3,'011',' ',rect)
-->plot2d(x4,y4,4,'011',' ',rect)
-->plot(xl,yl,'x','y','solution for larger values of x')
-->xtitle('Stiff equation numerical solution Euler 1st order','x','y')

The points indicated by the crosses and asterisks correspond to the values of x = 0.0005 and
0.0010. The values indicated by the crosses enclosed in circles correspond to x = 0.0020, and
the dark diamond marks correspond to x = 0.0025. The result indicates that x must be of

Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
size 0.0010 or smaller to ensure stability. This is a characteristic of a stiff ordinary
differential equation since a very small step x is required for stability.

Function ode allows the use of the argument stiff to change the numerical method for the
solution to a Backward Differentiation Formula (BDF) for its solution. The following SCILAB
commands demonstrate the use of function ode with the argument stiff for the solution of the
differential equation under consideration. The graph thus produced shows that the solutions
are stable for the four values of x used.

-->deff('[dydx]=g(x,y)','dydx=-1000.*(y-x)+2001')

-->Dx=0.0005;x0=0;xn=0.01;y0=1;x1=[x0:Dx:xn];y1=ode('stiff',y0,x0,x1,g);

-->Dx=0.0010;x0=0;xn=0.01;y0=1;x2=[x0:Dx:xn];y2=ode('stiff',y0,x0,x2,g);

-->Dx=0.0020;x0=0;xn=0.01;y0=1;x3=[x0:Dx:xn];y3=ode('stiff',y0,x0,x3,g);

-->Dx=0.0025;x0=0;xn=0.01;y0=1;x4=[x0:Dx:xn];y4=ode('stiff',y0,x0,x4,g);

-->min([y1 y2 y3 y4]), max([y1 y2 y3 y4])
ans = 1.
ans = 2.0099544

-->rect = [0 0 0.01 2.5];

-->plot2d(x1,y1,-1,'011',' ',rect)
-->plot2d(x2,y2,-2,'011',' ',rect)
-->plot2d(x3,y3,-3,'011',' ',rect)
-->plot2d(x4,y4,-4,'011',' ',rect)

-->xtitle('Stiff equation - ode function with option stiff','x','y')







Function ode with root finding option

Function ode can be invoked with the first argument root if the solution of the ordinary
differential equation dy/dx = f(x,y) is subject to the constraint g(x,y) = 0. The solution is
calculated for a range of values of x (a vector) and stopped when the constraint g(x,y)=0 is
satisfied. For example, suppose that we are solving the differential equation dy/dx = f(x,y) =

Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz
x sin(y), with initial conditions y(0) = 1, subjected to the constraint g(x,y) = x + y - 5 = 0, in the
range 0 < x < 10. We will obtain the solution by using:

-->deff('[dydx]=f(x,y)','dydx = x*sin(y)')

-->deff('[w]=g(x,y)','w = x+y-5')

-->y0=1;x0=0;x=[0:0.1:10];

-->[y,rd]=ode('root',y0,x0,x,f,1,g);

We can check that the solution was stopped before reaching the maximum value of x by
checking the lengths of the vectors x and y:

-->length(x), length(y)
ans = 101.

ans = 23.

Vector rd contains information on the point where the calculation was stopped.

-->rd
rd =

! 2.1889184 1. !

To plot the results of the recent call to function ode we create a new vector of x values, xx:

-->xx = x(1:23);

-->plot(xx,y,'x','y','ode function with root option')




The following plot shows the truncated solution altogether with the solution for the full x range
(0,10):

-->yf = ode(y0,x0,x,f);

-->plot(x,yf,'x','y','ode function solution')

-->plot2d(xx,y,-9,'011',' ',[0 1 10 3.4])


Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz






Discrete solutions with function ode

Function ode provides the option discrete for the solution of a differential equation dy/dx =
f(x,y) based on a set of discrete (integer) indices kvect, that starts with k0. The initial
condition is y(k0) = y0. The simplest call to the function is

[y] = ode(discrete,y0,k0,kvect,f).

When using this option, the solution is calculated recursively by using y
k+1
=f(k,y
k
), for k=1,2,

As an example, we solve the differential equation dy/dx = x
2
-1, in a discrete domain
k=1,2,,20, with y(1) = 2.5:

-->deff('[dydx]=f(x,y)','dydx=x.^2-1')

-->k0 = 1; kvect = [1:1:20]; y0 = 2.5;

-->y = ode('discrete',y0,k0,kvect,f);

The following plot shows the results of y as function of k:

-->plot(kvect,y,'k','y','ode function with option discrete')



Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz




Changing ODE numerical solution parameters with odeoptions

Function ode uses a number of parameters which can be redefined through the use of the
function odeoptions(). When this command is used, SCILAB generates an input form where the
parameters of the numerical methods for ordinary differential equations can be modified.
Application of the function odeoptions() can be used to modify the global variable
%ODEOPTIONS. This variable is a vector containing the following information:

[itask,tcrit,h0,hmax,hmin,jactyp,mxstep,maxordn,maxords,ixpr,ml,mu]

where the different elements are described as follows:

itask - identifies the task required from the numerical method. Possible values of itask are:

1 : normal computation at specified times
2 : computation at mesh points (given in first row of output of ode)
3 : calculate one step at one internal mesh point and return
4 : normal computation without overshooting tcrit (see below)
5 : one step, without passing tcrit (see below), and return

tcrit - this value applies when itask = 4 or 5, representing a critical value of the independent
variable t.

h0 - represents the first step in the independent variable tried in the numerical solutions
when adaptive methods are used.

hmax - maximum step size

hmin - minimum step size

jactype - Jacobian type. This option can be used when additional derivatives (a Jacobian) are
provided for the solution. Possible values of this option are:

0 : functional iterations, no Jacobian used ("adams" or "stiff" only)
1 : user-supplied full Jacobian
2 : internally generated full Jacobian
3 : internally generated diagonal Jacobian ("adams" or "stiff" only)
4 : user-supplied banded Jacobian (see ml and mu below)

Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz
5 : internally generated banded Jacobian (see ml and mu below)

mxstep - Maximum number of iterations allowed.

maxordn - Maximum non-stiff order allowed for the numerical method (maximum allowed is
12).

maxords - Maximum stiff order allowed, at most 5-th order.

ixpr - print level, either 0 or 1.

ml,mu - see description below:

If jactype = 4 or 5, ml and mu are the lower and upper half-bandwidths of the banded
Jacobian. The band includes those terms of the Jacobian, J
ij
, with i-ml <= j <= ny-1.

If jactype = 4 the Jacobian function must return a matrix J which has (ml+mu+1) rows
and ny columns (where ny is the number of elements of y in dy/dt =f(t,y)), such that
the first column of J is made of mu zeros followed by f
1
/y
1
, f
2
/y
1
, f
3
/y
1
, ... (1+ml
possibly non-zero entries). Column 2 of J is made of mu-1 zeros followed by f
1
/x
2
,
f
2
/x
2
, etc


The default value of the global variable %ODEOPTIONS is [1,0,0,%inf,0,2,500,12,5,0,-1,-1],
i.e., itask = 1, tcrit = 0, h0 = 0, hmax = , hmin = 0, jactype = 2, mxstep = 500, maxordn = 12,
maxords = 5, ixpr = 0, ml = -1, mu = -1.



A Ap pp pl li ic ca at ti io on ns s o of f n nu um me er ri ic ca al l s so ol lu ut ti io on ns s t to o I IV VP Ps s

This section presents the solution to initial value problems (IVPs) some of which are obtained
from the physical sciences, e.g., mechanical systems, electric circuits, etc.


Systems of ODEs from mechanical systems

Systems of ODEs can obtained from the analysis of two linked particles in oscillatory motion or
from the analysis of electric circuits. For example, the figure below shows two particles, P
1

and P
2
, linked by three springs. The figure at the top represents the system in their state of
equilibrium, while the one at the bottom shows the system at any generic point at time t>0.


Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz


The displacement of particle P
1
with respect to its equilibrium position is given by ( ) x
1
t ,
while that of particle P
2
is given by ( ) x
2
t . The magnitude of the force applied by a spring on
an attached particle is given by Hooke's law,

F ( ) k L L
0
,

where
k
is the spring constant,
L
is the stretched length of the spring, and L
0
is the
unstretched length of the spring. The force that spring A, with constant k
A
, applies on
particle P
1
is given by

F
A
= k
A
( ) + L
A
x
1
L
A
= k
A
x
1
,

while that applied by spring B on particle P
1
is

F
B
= k
B
( ) + + + L
A
L
B
L
C
L
A
x
1
L
C
x
2
L
B
= k
B
( ) x
1
x
2


For the position illustrated in the Figure above, both forces will act in the negative direction of
x
1
, thus, writting Newton's second law for particle P
1
of mass m
1
results in:
F
A
F
B
m
1
a
1
, or k
A
x
1
k
B
( ) x
1
x
2
m
1
d
2
x
1
dt
2


Similarly, for particle P
2
, of mass m
2
, with the magnitude of the force applied by spring C
given by

F
C
= k
C
( ) L
C
x
2
L
C
= k
C
x
2
,

we can write
F
B
F
C
m
2
a
2
, or ( ) k
B
x
1
x
2
k
C
x
2
= m
2
d
2
x
2
dt
2


The resulting system of equations can be written as:

d
2
x
1
dt
2
=
+ k
A
k
B
m
1
x
1
+
k
B
m
1
x
2
, and
d
2
x
2
dt
2
=
k
B
m
2
x
1
-
+ k
B
k
C
m
2
x
2
.


Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz
These two second-order equations can generate a system of four first order equations if we
define x
3
= dx
1
/dt, and x
4

= dx
2
/dt. The resulting system is:


dx
1
/dt = x
3

dx
2
/dt = x
4

dx
3
/dt = -(k
A
+k
B
)x
1
/m
1
+ k
B
x
2
/m
1

dx
4
/dt = k
B
x
1
/m
2
- (k
B
+k
C
)x
2
/m
2


or, using vectors, dx/dt = f(t,x), where x = [x
1
x
2
x
3
x
4
]
T
, and


. ) , (
2
2
1
2
2
1
1
1
4
3
1
1
1
1
1
1
1
]
1

+
+

x
m
k k
x
m
k
x
m
k
x
m
k k
x
x
x t f
C B B
B B A



Suppose we use the following values k
A
= 20 N/m, k
B
= 40 N/m, k
C
= 50 N/m, m
1
= 2 kg, m
2
= 5
kg, and given the initial conditions x
1
= 0.5 m, x
2
= 0.2 m, x
3
= dx
1
/dt = -0.1 m/s, x
4
= dx
2
/dt =
0.1 m/s, at t = 0, we will solve the system of equations in the interval 0 < t < 100 s, using a
time increment of t = 0.1. This is the SCILAB solution:

First, we define the function f from dy/dt = f(t,y) and enter the values of the parameters:


-->deff('[ff]=f(t,x)','ff=[x(3);x(4);-(kA+kB).*x(1)/m1+kB.*x(2)/m1;...
-->kB.*x(1)/m2-(kB+kC).*x(2)/m2]')

-->kA = 20; kB = 40; kC = 50; m1 = 2; m2 = 5;


Next, we calculate the parameters for the solution and the solution itself:


-->t0=0;Dt=0.1;tn=10;x0=[0.5;0.2;-0.1;0.1];t=[t0:Dt:tn];

-->x = ode(x0,t0,t,f);


To produce a plot of the positions x
1
(t) and x
2
(t) we use:


-->plot2d([t',t'],[x(1,:)',x(2,:)'],[1,9],'111','x1@x2 ',[0 -1 10 1])

-->xtitle('Two particle oscillation','t(s)','x1(m) & x2(m)')

Download at InfoClearinghouse.com 68 2001 Gilberto E. Urroz


The corresponding velocities, v
1
(t) and v
2
(t), are plotted by using:

-->plot2d([t',t'],[x(3,:)',x(4,:)'],[1,9],'111','v1@v2 ',[0 -2.5 10 2.5])
-->xtitle('Two particle oscillation','t(s)','v1(m/s) & v2(m/s)')



System of ODEs from Electric Circuits

A second application of systems of ordinary differential equations can be obtained from
electric circuits. Consider the circuit shown in the figure below. The circuit consists of two
loops each with a capacitor, an inductor, and a voltage source. The two loops share the
resistor R. The electrical current circulating in the left-hand side loop is referred to as ( ) I
1
t
and it is assumed to be positive in the counterclockwise direction. A similar electric current,
( ) I
2
t , circulates in the right-hand side loop. The voltages ( ) E
1
t and ( ) E
2
t are positive in the
sense indicated by the arrow. The equations for the voltage across individual components are:


Download at InfoClearinghouse.com 69 2001 Gilberto E. Urroz
Resistor, V
R
=
R I

Capacitor,
V
C
q
C

Inductor,
V
L
L dI
dt


where V = voltage (volts), R = resistance (ohms), I = electric current (amperes), q = electric
charge (coulombs), C = capacitance (farads), L = inductance (henrys), t = time (seconds). By
definition,
I
dq
dt
.




For the purpose of writing the governing equations we use Kirchoff law of conservation of
voltage in a closed loop in a circuit with voltages across resistors, capacitors, and inductors
decreasing in the direction of circulation of the current. Thus, for loops number 1 and 2, we
would write, respectively:

E
1
q
1
C
1
L
1
( ) dI
1
t
dt
R ( ) I
1
I
2
0 ,
and

E
2
R ( ) I
2
I
1
L
2
dI
2
dt
q
2
C
2
0 .

Introducing the definition of the electric currents in the equations,
I
1
dq
1
dt
, and
I
2
dq
2
dt
,
and rearranging terms we get a system of four first-order differential equations:


dI
1
dt
+ +
R I
1
L
1
R I
2
L
1
q
1
L
1
C
1
E
1
L
1
,

dq
1
dt
I
1
,
dI
2
dt
+
R I
1
L
2
R I
2
L
2
q
2
L
2
C
2
E
2
L
2
,

dq
2
dt
I
2
.

These four equations can be transformed into a system of differential equations dy/dt = f(t,y),
with y = [y
1
y
2
y
3
y
4
]
T
= [q
1
q
2
I
1
I
2
]
T
, and


Download at InfoClearinghouse.com 70 2001 Gilberto E. Urroz
. ) , (
2
2
2 2
2
2
4
2
3
1
1
1 1
1
1
4
1
3
4
3
1
1
1
1
1
1
1
]
1

+
+ +
L
E
C L
y
L
Ry
L
Ry
L
E
C L
y
L
Ry
L
Ry
y
y
y t f

Suppose that we use the values R = 1800 ohms, L
1
= 500 henrys, L
2
= 800 henrys, C
1
= 0.005
farads, C
2
= 0.010 farads, E
1
= E
2
= 0, with initial conditions q
1
= 100 coulombs, q
2
= I
1
= I
2
= 0,
the following two example illustrate how to produce the solution for the charges q
1
(t), q
2
(t),
and the currents I
1
(t), I
2
(t).

The first example uses constant voltages E
1
and E
2
. First we define the system function f(t,y)
and the parameters of the circuit:

-->deff('[dydt]=f(t,y)','dydt = [y(3);y(4);...
-->-R*y(3)/L1+R*y(4)/L1-y(1)/(L1*C1)+E1/L1;...
--> R*y(3)/L2-R*y(4)/L2-y(2)/(L2*C2)+E2/L2]')

-->R=1800;L1=500;L2=800;C1=0.005;C2=0.010;E1=0;E2=0;

Next, we define the parameters of the solution and calculate the solution using function ode:

-->t0 = 0; Dt = 0.1; tn = 10; t = [t0:Dt:tn];y0 = [100;0;0;0];
-->y = ode(y0,t0,t,f);

To produce plots of electric charge we use:

-->min([y(1,:) y(2,:)]), max([y(1,:) y(2,:)])
ans = - 117.01482, ans = 100.
-->plot2d([t',t'],[y(1,:)',y(2,:)'],[1,9],'111','q1@q2',[0 -120 100 120])
-->xtitle('Electric charge ODE solution','t(s)','q1,q2(coulomb)')

A plot of electric current follows:

-->min([y(3,:) y(4,:)]), max([y(3,:) y(4,:)])

Download at InfoClearinghouse.com 71 2001 Gilberto E. Urroz
ans = - 32.655809
ans = 31.88677

-->plot2d([t',t'],[y(3,:)',y(4,:)'],[1,9],'111','I1@I2',[0 -40 100 40])
-->xtitle('Electric current ODE solution','t(s)','I1,I2(ampere)')



In the following example we introduce sinusoidal driving voltages, E
1
(t) and E
2
(t):

-->deff('[EE1]=E1(t)','EE1=12*cos(120*%pi*t)')

-->deff('[EE2]=E2(t)','EE2= 6*cos( 60*%pi*t)')

Next, we redefine the function f(t,y) to include E
1
(t) and E
2
(t):

-->deff('[dydt]=f(t,y)','dydt = [y(3);y(4);...
-->-R*y(3)/L1+R*y(4)/L1-y(1)/(L1*C1)+E1(t)/L1;...
--> R*y(3)/L2-R*y(4)/L2-y(2)/(L2*C2)+E2(t)/L2]')
The parameters for the solution will be now:

-->t0 = 0; Dt = 0.1; tn = 50; t = [t0:Dt:tn];

Because the system function f(t,y) is now complicated by the presence of time-dependent
voltages E1(t) and E2(t), function ode will take a few minutes to complete the solution.

-->y = ode(y0,t0,t,f);


To produce a plot of electric charges use:


-->min([y(1,:) y(2,:)]), max([y(1,:) y(2,:)])
ans = - 154.45894
ans = 200.

-->plot2d([t',t'],[y(1,:)',y(2,:)'],[1,9],'111','q1@q2',[0 -200 50 200])

-->xtitle('Electric charge ODE solution - CASE 2','t(s)','q1,q2(coulomb)')





Download at InfoClearinghouse.com 72 2001 Gilberto E. Urroz



For a plot of the electric currents use:


-->min([y(3,:) y(4,:)]), max([y(3,:) y(4,:)])
ans = - 65.0047
ans = 61.126823


-->plot2d([t',t'],[y(3,:)',y(4,:)'],[1,9],'111','I1@I2',[0 -70 50 70])
-->xtitle('Electric current ODE solution - CASE 2','t(s)','I1,I2(ampere)')




Solving a fourth-order equation

Consider the fourth-order linear equation presented in an earlier section

+ + +
d
4
y
dx
4
3 d
3
y
dx
3
2 d
2
y
dx
2
5 dy
dx
y 0 ,

subjected to
y 1
,

dy
dx
-1
,
d
2
y
dx
2
0 ,
d
3
y
dx
3
-1 , at
x 0
. To solve this equation we
transform the fourth-order equation into a first-order system of linear equations:

du
3
/dx = -3u
3
(x)+2u
2
(x)-5u
1
(x)-y(x)+x
2
/2,

Download at InfoClearinghouse.com 73 2001 Gilberto E. Urroz
du
2
/dx = u
3
(x),
du
1
/dx = u
2
(x),
dy/dx = u
1
(x),

or

.
0
0
0
2 /
) (
) (
) (
) (
0 1 0 0
0 0 1 0
0 0 0 1
1 5 2 3
) (
) (
) (
) (
2
1
2
3
1
2
3
1
1
1
1
1
]
1

+
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

x
x y
x u
x u
x u
x y
x u
x u
x u
dx
d


With

,
0
0
0
2 /
) ( ,
0 1 0 0
0 0 1 0
0 0 0 1
1 5 2 3
,
) (
) (
) (
) (
) (
2
1
2
3
1
1
1
1
1
]
1

1
1
1
1
]
1

1
1
1
1
]
1

x
x g A
x y
x u
x u
x u
x v

the system of differential equations is written as

dv/dx = Av+g(x).

The initial conditions are y(0) = 1, u
1
(0) = dy/dx = -1, u
2
(0) = du
1
/dx = d
2
y/dx
2
= 0, u
3
(0) =
du
2
/dx = d
2
u
1
/dx
2
= d
3
y/dx
3
= -1,

.
1
1
0
1
) 0 (
1
1
1
1
]
1

u


To implement the solution using SCILAB we first define the function f(x,v) and the parameters
for the solution

-->deff('[dvdx]=f(x,v)','dvdx=A*[v(1);v(2);v(3);v(4)]+[x^2;0;0;0]')
-->A = [-3,2,-5,-1;1,0,0,0;0,1,0,0;0,0,1,0];

-->v0 = [-1;0;-1;1];t0=0;Dt=0.1;tn=10;t=[t0:Dt:tn];

The numerical solution to the system of differential equations is obtained through:

-->v = ode(v0,t0,t,f);

To produce a plot of the solution y(t) we use:

-->min(v(4,:)),max(v(4,:))
ans = - 3.903516
ans = 137.7293

-->plot(t,v(4,:),'t','y(t)','Solution to 4th order equation')

Download at InfoClearinghouse.com 74 2001 Gilberto E. Urroz




The Van der Pol equation

The Van der Pol equation results from the analysis of the Van der Pol oscillator circuit shown in
the figure below.



Application of Kirchoffs laws to this circuit results in the following equations:

C(dE/dt)=I
1
, RI
2
+ L(dI
2
/dt) = E + E
0
, I
3
= f(E), I
0
= I
1
+ I
2
+ I
3


This set of four equations can be reduced to two differential equations

C(dE/dt) = I
0
- I
2
- f(E)
L(dI
2
/dt) = E + E
0
- RI
2


If we eliminate the resistor from the circuit (R=0), the two differential equations can be
combined into a single second order differential equation

C(d
2
E/dt
2
) + f(E)(dE/dt) + (E+E
0
)/L = dI
o
/dt.

Download at InfoClearinghouse.com 75 2001 Gilberto E. Urroz

The function f(E) represents the response of the tunnel diode to the voltage E, and can be
taken to be the third-order polynomial

f(E) = KE(E
2
/3 - (E
1
+E
2
)E/2 + E
1
E
2
).

Tuning the voltage source E so that E
0
= -(E
1
+E
2
)/2, we can write

f(E) = KE(E
2
/3 + E
0
E/2 + E
1
E
2
).

If we define E = (E
2
-E
1
)y/2 + (E
1
+E
2
)/2 = (E
2
-E
1
)y/2 + E
0
, and scale the time so that = t(LC)
1/2
,
we can transform the governing equation into the Van der Pol equation

d
2
y/d
2
+ (y
2
-1)(dy/d) + y = 0,

where = K((E
2
-E
1
)/2)
2
(L/C)
1/2
and dI
0
/dt = 0.

The Van der Pol equation, being a second-order equation, can be transformed into a set of two
first-order equations, by using dy/d = u
1
(), and y = u
2
(). The system of equations is

du
1
/d = -(u
2
2
-1)u
1
- u
2

du
2
/d = u
1

or,
du/dt = f(,u),
with
.
) 1 (
) , ( ,
) (
) (
) (
1
2 1
2
2
2
1
1
]
1

1
]
1

u
u u u
u f
u
u
u



The Van der Pol equation is solved next using SCILABs function ode. The initial conditions
used are u
1
() = du
2
/d = dy/d = 2, u
2
() = y = 1 at = 0. The solution is obtained for the range
0 < < 100, and for values of = 0.01 and = 4.0. First, we define the function f(,u):

-->deff('[ff]=f(t,u)','ff=[-k*(u(2)^2-1)*u(1)-u(2);u(1)]')
-->u0=[1;2];t0=0;Dt=0.1;tn=100.0;t=[t0:Dt:tn];

The solution for k = 0.01 is found first:

-->k=0.01;u1=ode(u0,t0,t,f);

To produce plots of the function we determine the maximum and minimum values of u:

-->min(u1),max(u1)
ans = - 2.2268766
ans = 2.231914

First, we plot the signals u
2
() = y, and u
1
() = dy/d against :

-->plot2d([t' t'],[u1(2,:)' u1(1,:)'],[1,-1],'111','y@dy/dt',[0 -3 100 3])
-->xtitle('Van der Pol equation - k = 0.01','t','y,dy/dt')


Download at InfoClearinghouse.com 76 2001 Gilberto E. Urroz



The phase portrait dy/d vs. y is shown next:

-->plot(u1(1,:),u1(2,:),'y','dy/dt','Van der Pol equation - k = 0.01')



The solution to the Van der Pol equation for = 4, and the corresponding plots are obtained
with the following SCILAB commands:

-->k=4.00;u2=ode(u0,t0,t,f);

-->plot(t,u2(2,:),'t','y','Van der Pol equation - k = 4') //Signal y vs. t




Download at InfoClearinghouse.com 77 2001 Gilberto E. Urroz
-->plot(u2(1,:),u2(2,:),'y','dy/dt','Van der Pol equation - k = 4') //Phase
portrait




The Rssler flow

In the analysis of chaotic dynamical systems, a three-dimensional flow is described by a set of
three ordinary differential equations involving three variables x(t), y(t), and z(t). The Rssler
flow is given by the equations (see Berg et al., 1984, Order within Chaos - Towards a
deterministic approach to turbulence, John Wiley & Sons, New York):

dx/dt = - y - z
dy/dt = x + ay
dz/dt = b + xz - cz

To solve this system using SCILAB function ode we re-write the system as

du/dt = f(t,u)

with

. ) , ( ,
) (
) (
) (
) (
) (
) (
) (
3 3 1
2 1
3 2
3
2
1
1
1
1
]
1

+
+

1
1
1
]
1

1
1
1
]
1

cu u u b
au u
u u
x t g
t z
t y
t x
t u
t u
t u
t u

In the following exercise we solve for the Rssler flow using a = b = 0.2, c = 5.7, in the interval
0 < t < 200, with initial conditions x(0) = y(0) = z(0) = 1. We use a time increment t = 0.1.
The solution starts by defining the function f(t,u) and the parameters of the flow and of the
solution:

-->deff('[w]=f(t,u)','w=[-u(2)-u(3);u(1)+a*u(2);b+u(1)*u(3)-c*u(3)]')
-->a=0.2; b=0.2; c=5.7; t0=0; Dt=0.1; tn=200; t=[t0:Dt:tn]; u0=[1;1;1];

The solution is stored in variable u from:
-->u=ode(u0,t0,t,f);

The following are plots of the signals x(t), y(t), and z(t) resulting from the solution:
-->plot(t,u(1,:),'t','x','Rossler flow')


Download at InfoClearinghouse.com 78 2001 Gilberto E. Urroz


-->plot(t,u(2,:),'t','y','Rossler flow')


-->plot(t,u(3,:),'t','z','Rossler flow')


The behavior of the signals is, in general, aperiodic or chaotic. Phase portraits of signals may
provide additional information regarding strange attractors resulting from the solution. A
phase portrait of signal y vs. x follows:

-->plot(u(1,:),u(2,:),'x','y','Rossler flow')

Download at InfoClearinghouse.com 79 2001 Gilberto E. Urroz

A phase portrait of the rate of change in x, dx/dt, vs. x, is obtained next. First, we determine
the length of vector x = u(1,:), and use function mtlb_diff (see Chapter ) to estimate the
derivative dx/dt. The next step is to produce a vector xx consisting of the data in u(1,:) but
reduced by one to make it of the same length as dx. The phase portrait is shown below:

-->n=length(u(1,:))
n = 2001.
-->dx = mtlb_diff(u(1,:))/Dt;
-->xx=u(1,1:n-1);
-->plot(xx,dx,'x','dx/dt','Rossler Flow')


S So ol lu ut ti io on ns s t to o b bo ou un nd da ar ry y v va al lu ue e p pr ro ob bl le em ms s ( (B BV VP Ps s) )


Download at InfoClearinghouse.com 80 2001 Gilberto E. Urroz
Boundary value problems consist of ordinary differential equations with conditions provided at
different values of the independent variable. Unlike initial boundary problems, which can be
solved using the same numerical solution after transforming the differential equation into a
system of first-order differential equations with appropriate initial conditions, boundary value
problems are not suitable for solution through a single numerical method. In this section, we
explore some approaches to the solution of boundary value problems.


The shooting method

The simplest type of boundary value problems consists of a second order differential equation,
d
2
y/dx
2
= g(x,y,dy/dx) subject to the boundary conditions y(x
0
) = y
0
and y(x
1
) = y
1
. The figure
below illustrate three possible solutions to the initial value problem represented by the same
differential equation, namely, d
2
y/dx
2
= g(x,y,dy/dx), subject to the initial condition y(x
0
) = y
0
,
and different initial derivative conditions. There is one value of dy/dx at x = x
0
that produces
the solution that satisfies the second boundary condition y(x
1
) = y
1
. The solution to the
boundary value problem will be curve (II) in the figure.



The so-called shooting method consists in solving the initial value problem d
2
y/dx
2
=
g(x,y,dy/dx), subject to the initial condition y(x
0
) = y
0
, dy/dx|
x = x0
= y(x
0
) = y
0
, for different
values of y
0
, and obtaining the corresponding boundary values y
1
= y(x
1
). As a result we get a
set of data values { [(y
0
)
1
,(y
1
)
1
], [(y
0
)
2
,(y
1
)
2
], , [(y
0
)
n
,(y
1
)
n
] } from which we can interpolate
the value of y
0


corresponding to the given value of y
1
. The solution to the boundary value
problem, thus, becomes the solution to a number of initial value problems followed by an
interpolation. Once the proper value of y
0
is determined, the initial value problem is solved
one last time.
A function to implement the shooting method

The following SCILAB user-defined function, shooting, produces the solution to a second-order
boundary value problem given the boundary conditions y(x
0
) = y
0
and y(x
1
) = y
1
. The second-
order differential equation d
2
y/dx
2
= g(x,y,dy/dx) is transformed into a first-order system of
two ordinary differential equations of the form du/dx = f(x,u), with u
1
= y(x), u
2
= du
1
/dx =
dy/dx, and

Download at InfoClearinghouse.com 81 2001 Gilberto E. Urroz
.
) , , (
) , ( ,
2 1
2
2
1
1
]
1

1
]
1

u u x g
u
u x f
u
u
u

The general call to the function is

[u, table, y0p] = shooting(yb,yp,x,f)

The boundary conditions are passed on to the function as a vector yb = [y
0
y
1
]. The solution is
obtained on a range represented by vector x. We also need to provide a vector of values of the
derivative y
0
, referred to as yp. The function returns the values of u as a 2n matrix, where
the first row is the function y(x) and the second row is the derivative dy/dx corresponding to
the values of x. The function shooting also returns variable table which is a table of values of
y
0
and the corresponding values of y(x
1
) that were used to interpolate the initial condition for
the derivative at x = x
0
for the final solution. The first row in the table are the values of

y
0
,
while the second row in the table are the values of y(x
1
). In addition to matrices u and table,
function shooting returns also the derivative boundary condition, y0p = dy/dx|
x=x0
, interpolated
from the shooting method.

A listing of the function is shown next:

function [y,xyTable,yderiv] = shooting(yb,yp,x,f)

//Shooting method for a second order
//boundary value problem
//yb = [y0 y1] -> boundary conditions
//x = a vector showing the range of x
//f = function defining ODE, i.e.,
// dy/dx = f(x,y), y = [y(1);y(2)].
//yp = vector with range of dy/dx at x=x0
//xyTable = table for interpolating derivatives
//yderiv = derivative boundary condition

n = length(yp);
m = length(x);
y1 = zeros(yp);

for j = 1:n
y0 = [yb(1);yp(j)];
yy = ode(y0,x(1),x,f);
y1(j) = yy(1,m);
end;

xyTable = [y1;yp];
yderiv = interpln(xyTable,yb(2));
y0 = [yb(1);yderiv];
y = ode(y0,x(1),x,f);

Consider the case of the second order boundary value problem defined by the ordinary
differential equation

d
2
y/dx
2
+ dy/dx + y = sin(3x),

subject to the boundary conditions y(0) = 1, y(5) = -1. We re-cast the ODE into the following
first-order system,

du/dx = f(x,u),


Download at InfoClearinghouse.com 82 2001 Gilberto E. Urroz
with

u
1
= y(x), u
2
= du
1
/dx = dy/dx,


.
) 3 sin(
) , ( ,
) (
) (
) (
2 1
2
2
1
1
]
1

1
]
1

u u x
u
u x f
x u
x u
x u


The boundary conditions are now, u
1
(0) = 1, u
1
(5) = -1. The solution to this boundary value
problem with SCILAB is accomplished as follows:

-->deff('[w]=f(x,u)','w=[u(2);sin(3*x)-u(1)-u(2)]')

-->yb=[1,-1];x0=0;Dx=0.1;xn=5;x=[x0:Dx:xn];yp=[-10:1:10];

-->[u,tabl,y0p]=shooting(yb,yp,x,f);


The plot of the solution is produced by using:

-->plot(x,u(1,:),'x','y','shooting method solution 2nd order BVP')





To illustrate the shooting method applied to the second-order boundary value problem
presented above, we produce numerical solutions to the corresponding second-order ordinary
differential equation using initial conditions y(0) = 1 and different derivative boundary
conditions to produce the following graph. The solution to the boundary value problem with
boundary conditions y(0) = 1 and y(5) = -1 is discontinuous curve in the graph.


-->yp0 = [3,6,9,12,15]; //Different values of dy/dx at x = 0

-->um = zeros(5,m); //Calculate different solutions

-->for k =1:5
--> y0 = [1;yp0(k)];
--> uu = ode(y0,x0,x,f);
--> um(k,:) = uu(1,:);
-->end;


Download at InfoClearinghouse.com 83 2001 Gilberto E. Urroz
-->plot2d([x',x',x',x',x'],[um(1,:)',um(2,:)',um(3,:)',um(4,:)',um(5,:)'],...
-->[1,2,3,4,5],'111','y0p=3@y0p=6@y0p=9@y0p=12@y0p=15',...
-->[0 -5 5 10])

-->plot2d(x',u(1,:)',-1,'011',' ',[0 -5 5 10])

-->xtitle('Boundary value solution - shooting method','x','y')





Outline of the implicit solution for a second-order BVP

In this section we present the outline for an implicit, finite-difference based solution to the
second order boundary value problem

d
2
y/dx
2
+y = 0,

in the x-interval (0,5) subject to y(0) = 1, and y(5) = 0. Use x = 0.1.

We discretize the differential equation using the finite difference approximation

d
2
y/dx
2
= (y
i+2
-2y
i+1
+y
i
)/(x
2
) ,
which results in
(y
i+1
-2*y
i
+y
i-1
)/(x
2
)+y
i
= 0.

From this result we get the following implicit equations:

y
i-1
-(2-x
2
)y
i
+y
i+1
= 0,


Download at InfoClearinghouse.com 84 2001 Gilberto E. Urroz
for i = 2,3, ..., n-1. There are a total of (n-2) equations, corresponding to the (n-2) unknowns
y
2
, y
3
, ...,y
n-2
, y
n-1
[y
1
= y(0) and y
n
= y(5)]. Therefore, the resulting set of linear algebraic
equations has a unique solution. The implementation of the solution for this example is left as
an exercise for the reader.


Function bvode for the solution of boundary value problems

SCILAB provides function bvode for the numerical solution of boundary value problems. The
function works on a general boundary value problem, which, as indicated earlier, requires a lot
of detailed information in its set up. The problem solved through function bvode consists of
the boundary value problem:

.
/
/
/
/
/
/
with ), , (
1 *
2
1
1 * 1 *
2 2
*
3
2
1
*
*
1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1


dx du
dx du
dx du
y
dx y d
dx y d
dx dy
y
u
u
u
u
u u x f
dx
y d
m
m m
m
m
m
! ! !


For example, the second-order differential equation

d
2
y/dx
2
+ 5(dy/dx) + y = cos(x),
is re-written as
d
2
y/dx
2
= cos(x) - y - 5(dy/dx),

or, with, u
1
= y(x), u
2
= dy/dx, as

d
2
y/dx
2
= f(x,u) = cos(x) - u
1
-5u
2
.


External SCILAB functions used with bvode

Using SCILAB, the function f(x,u) can be defined, for example, as

deff([ff] = f(x,u),ff=cos(x)-u(1)-5*u(2))

The general boundary value problem d
m*
y/dx
m*
= f(x,u) is solved in the interval [xL,xR], with
the boundary conditions provided through a function g(i,u) = 0, so that g(i,u) = u
i
- bc
j
, with u
i
=
d
i-1
y/dx
i-1
, and bc
j
being the corresponding boundary condition at point x =
j
. The values of
j
,
j = 1,2, , m*, are provided in vector (zeta), and they are either xL or xR, i.e., the location of
the boundaries for the problem.

For example, if the equation is of order 2 (m*=2), and the boundary conditions are y(xL) = y
0

and y(xR) = y
1
, we will write the vector zeta as = [xL,xR], or
1
= xL,
2
= xR, and g(1,u) = u
1
-
y
0
, g(2,u) = u
2
- y
1
. The latter results are equivalent to u
1
(
1
) = y(xL) = y
0
and u
2
(xR) = y(xR) =
y
1
.

As a second example, assume that the equation to be solved is of order 3 (m*=3) subject to
boundary conditions y(xL) = y
0
, dy/dx|
x=xL
= y
0
, and y(xR) = y
1
. The vector zeta is written as
= [xL,xL,xR], and the function g(i,u) given by g(1,u) = u
1
- y
0
, g(2,u) = u
2
- y
0
, g(3,u) = u
1
- y
1
.
If instead, the boundary conditions are y(xL) = y
0
, and y(xR) = y
1
, dy/dx|
x=xR
= y
1
, the vector
zeta is written as = [xL,xR,xR], and function g(i,u) given by g(1,u) = u
1
- y
0
, g(2,u) = u
1
- y
1,

Download at InfoClearinghouse.com 85 2001 Gilberto E. Urroz
g(3,u) = u
2
- y
1
. Another possibility is that two of the boundary conditions are derivatives,
e.g., y(xL) = y
0
, dy/dx|
x=xL
= y
0
, and dy/dx|
x=xR
= y
1
. In this case we would have =
[xL,xL,xR], and g(1,u) = u
1
- y
0
, g(2,u) = u
2
- y
0

,
g(3,u) = u
2
- y
1
. Thus, there is a one-to-one
relationship between the elements of vector zeta and values of function g(i,u) representing
boundary conditions of the problem.

Function bvode requires that we pass also derivatives of the functions f(x,u) and g(x,u). Lets
refer to the functions that calculate those derivatives as df(x,u) and dg(i,u). Function df
represents a vector [df
1
, df
2
, , df
m*
] with df
i
= df(x,u)/du
i
, while function dg represents a
vector whose elements are dg
ij
= dg
i
/du
j
for a fixed value of i. Referring to the second-order
boundary value problem described earlier, namely, d
2
y/dx
2
= f(x,u) = cos(x) - u
1
-5u
2
, subject
to the boundary conditions y(1) = 0.5 and y(2) = -1.5, we would define the zeta vector as =
[1,2], with g
1
= g(1,u) = u
1
- 0.5, and g
2
= g(2,u) = u
2
+ 1.5. The derivative functions would be
df
1
= f/u
1
= -1, df
2
= f/u
2
= -5, dg
11
= g
1
/u
1
= 1.0, dg
12
= g
1
/u
2
= 0.0, dg
21
= g
2
/u
1
= 0,
dg
22
= g
2
/u
2
= 1.0. Using SCILAB, we will define the functions f(x,u), g(i,u), df(x,u), dg(i,u)
as follows:

deff('[ff] = f(x,u) ','ff=cos(x)-u(1)-5*u(2)');
deff('[dff] = df(x,u)','dff = [-1,-5]');
deff('[gg] = g(i,u) ',['gg=[u(1)-1,u(2)+1]','gg=gg(i)']);
deff('[dgg] = dg(i,u)',['dgg = [1,0;0,1]';'dgg=dgg(i,:)']);

Notice that functions f(x,u) and g(x,u) return a single value, while functions df(x,u) and dg(x,u)
return vectors.

The user may also define a function that provides initial guesses of the solution. The general
call for this function is

[u0,du0] = guess(x)

where x is a vector, e.g., x = [xL:Dx:xR], with Dx = increment in x. Function guess provides
initial guesses for the solution u0 = [(u
1
)
0
; (u
2
)
0
; ;(u
m*
)
0
] and for the derivative of the solution,
du0 = [(du
1
/dx)
0
, (du
2
/dx)
0
, , (du
m*
/dx)
0
]. Typically, this function is not used in the solution,
i.e., no initial guesses of the solution and its derivative are provided, and the function guess
can simply be defined as

deff([u0,du0] = guess(x) , [u0= 0, du0 =0])


General call to function bvode

The general call to the function bvode is as follows:

[u] = =bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,f,df,g,dg,guess)

where x is a vector of values of the independent variable, x = [xL:DX:xR]; n is the number of
differential equations to be solved (n = 1 for the case under consideration); m is a vector whose
elements indicate the order each of the equations being solved (m =[m*] in this case); xL and
xR are the extremes of the interval where the solution is sought; zeta is the vector specifying
the location of the boundary conditions (examples of how to put together vector zeta were
presented earlier); ipar is a vector of 11 solution parameters whose components are described
below; ltol and tol are vectors specifying the number of the component of u and the tolerance
for the solution of those components; fixpnt indicates the number of fixed points in the
interval [xL, xR] other than the extremes (typically, fixpnt = 0); f, df, g, dg, and guess are
external SCILAB functions described earlier.


Download at InfoClearinghouse.com 86 2001 Gilberto E. Urroz

Description of elements of vector ipar

Next, we describe the elements of vector ipar, showing typical values for the solution of simple
boundary value problems. For a more detailed description of these parameters, use

--> help bvode

ipar(1) determines whether the boundary value problem is linear (ipar(1) = 0) or non-
linear (ipar(1) = 1).

ipar(2) determines the number of collocation points per subinterval. Typically, ipar(2)
= 0.

ipar(3) = number of subintervals in the initial mesh. Typically, ipar(3) = 0, in which
case the number of subintervals is set to 5.

ipar(4) = number of solution and derivative tolerances, with 0 < ipar(4) m*.

ipar(5), ipar(6) = dimensions of workspaces (vectors). Select a relatively large
number for those parameters, say 10000 or 15000.

ipar(7) determines the type of output produced by the function bvode. With ipar(7) = -
1, bvode produces a full diagnostic printout, with ipar(7) = 0 bvode produces selected
printout, and with ipar(7) = 1 bvode produces no printout.

ipar(8) controls the type of mesh used in the solution. If ipar(8) = 0 bvode generates a
uniform initial mesh.

ipar(9) is used to indicate whether or not initial guesses for the solution are provided.
For example, if ipar(9) = 0, bvode understands that no initial guess for the solution is
provided.

ipar(10) offers options for breaking out of function bvode in case convergence problems
are detected. For regular problems use ipar(10) = 0.

ipar(11) = number of fixed points in the mesh other than xL and xR. This number is the
same as the dimension of fixpnt. Typically, ipar(11) = 0.

For the solution of the second-order boundary value problem described earlier in this section,
the vector ipar can be put together as:

ipar=0*ones(1,11)
ipar(3)=1;ipar(4)=2;ipar(5)=10000;ipar(6)=2000;ipar(7)=1


Application of function bvode to a second order boundary value problem

The problem to be solved is the one that has been described above, namely,

d
2
y/dx
2
= f(x,u) = cos(x) - u
1
-5u
2
,

subject to the boundary conditions

y(1) = 0.5 and y(2) = -1.5.

Download at InfoClearinghouse.com 87 2001 Gilberto E. Urroz

The parameters n and m in the call to function bvode for this problem are n = 1 and m = [2].
Also, fixpnt = 0. Functions f,df,g,dg, and guess for this problem were defined earlier. We also
defined the vectors zeta and ipar. The boundaries are located at xL = 1 and xR = 2. Using an
x-increment Dx = 0.1, we can define the solution points as x = [xL:Dx:xR]. There is a couple of
arguments, namely, ltol and tol that need to be defined. Vector ltol indicates the indices of u
for which a tolerance for convergence will be defined. For this case we can take ltol = [1,2]
to define convergence tolerance for both the first and second components of u. The tolerances
are relatively small numbers, say, tol = [1e-10, 1e-10].

Having defined all the arguments for function bvode we put all the steps leading to the
function call together in the following SCILAB script:

//Script for 2nd order boundary value problem solution with bvode

deff('[ff] = f(x,u) ','ff=cos(x)-u(1)-5*u(2)');
deff('[dff] = df(x,u)','dff = [-1,-5]');
deff('[gg] = g(i,u) ',['gg=[u(1)-1,u(2)+1]','gg=gg(i)']);
deff('[dgg] = dg(i,u)',['dgg = [1,0;0,1]';'dgg=dgg(i,:)']);
deff('[u0,du0] = guess(x)' , ['u0= 0', 'du0 =0']);

n=1;m=[2];fixpnt=0;xL=0;xR=2;Dx=0.1;
x = [xL:Dx:xR];
zeta=[xL,xR];

ipar=zeros(1,11);
ipar(3)=1;ipar(4)=2;ipar(5)=10000;ipar(6)=2000;ipar(7)=1;

ltol=[1,2];
tol=[1.e-5,1.e-5];

u=bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,f,df,g,dg,guess);

xset('window',1)
plot(x,u(1,:),'x','y(x)','BVODE 2nd order solution')

xset('window',2)
plot(x,u(2,:),'x','dy/dx','BVODE 2nd order solution')


Placing this script in file bvp2 within the SCILAB working directory, it can be executed through:

-->exec(bvp2)

The resulting plot shows the solution for y(x) and dy/dx versus x:


Download at InfoClearinghouse.com 88 2001 Gilberto E. Urroz


Function bvode applied to a third-order boundary value problem

Consider the third-order ordinary differential equation

x
2
(d
3
y/dx
3
) + (1-x)(d
2
y/dx
2
) -3(dy/dx) + y = x+1

subject to the boundary conditions, y(0) = 2.5, y(10) = 4.5, dy/dx|
x=10
= -1. To solve this
problem using function bvode we first re-cast the differential equation as

d
3
y/dx
3
= f(x,u) = (x+1-(1-x)u
3
-3u
2
+ u
1
)/x
2


with u
1
= y(x), u
2
= dy/dx, u
3
= d
2
y/dx
2
, and u = [u
1
;u
2
;u
3
]. The boundary condition functions
will be g
1
= g(1,u) = u
1
- 2.5, g
2
= g(2,u) = u
1
- 4.5, and g
3
= g(3,u) = u
2
+ 1. The derivative
functions will be df = [1/x
2
, -3/x
2
, (1-x)], and dg = [1,0,0;1,0,0;0,1,0]. The zeta vector is zeta
= [0,10,10]. Also, n = 1, m = [3], fixpnt = 0.

The following script solves the boundary value problem and produces graphs of the function
y(x) and its first two derivatives:

//Script for 3rd order boundary value problem solution with bvode
deff('[ff] = f(x,u) ','ff=(x+1-(1-x)*u(3)-3*u(2)+u(1))/x^2');
deff('[dff] = df(x,u)','dff = [1/x^2,-3/x^2,(1-x)/x^2]');
deff('[gg] = g(i,u) ',['gg=[u(1)-2.5,u(1)-4.5,u(2)+1]','gg=gg(i)']);
deff('[dgg] = dg(i,u)',['dgg = [1,0,0;1,0,0;0,1,0]';'dgg=dgg(i,:)']);
deff('[u0,du0] = guess(x)' , ['u0= 0', 'du0 =0']);
n=1;m=[3];fixpnt=0;xL=0;xR=10;Dx=0.1;
x = [xL:Dx:xR];
zeta=[xL,xR,xR];
ipar=zeros(1,11);

Download at InfoClearinghouse.com 89 2001 Gilberto E. Urroz
ipar(3)=1;ipar(4)=3;ipar(5)=50000;ipar(6)=50000;ipar(7)=1;
ltol=[1,2,3];
tol=[1.e-5,1.e-5,1e-5];
u=bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,f,df,g,dg,guess);
xset('window',1);plot(x,u(1,:),'x','y(x)','BVODE 3rd order solution')
xset('window',2);plot(x,u(2,:),'x','dy/dx','BVODE 3rd order solution')
xset('window',3);plot(x,u(3,:),'x','d2y/dx2','BVODE 3rd order solution')





Application of bvode to a third-order problem with one interior
fixed point

The following example solves the same third-order boundary value problem as before, except
that now the boundary conditions are located at three different locations: y(0) = 2.5, y(5) =

Download at InfoClearinghouse.com 90 2001 Gilberto E. Urroz
4.5, y(10) = -1. To account for boundary conditions at three different locations, it is necessary
to change a few of the parameters in the script, including introducing xM = 5, and using fixpnt
= [5], zeta=[xL,xM,xR], ltol=[1], and tol=[1.e-5]. Also, the option ipar(11)=1 is introduced.

//Script for 3rd order boundary value problem solution with bvode
//Case of three different boundary points
deff('[ff] = f(x,u) ','ff=(x+1-(1-x)*u(3)-3*u(2)+u(1))/x^2');
deff('[dff] = df(x,u)','dff = [1/x^2,-3/x^2,(1-x)/x^2]');
deff('[gg] = g(i,u) ',['gg=[u(1)-2.5,u(1)-4.5,u(1)+1]','gg=gg(i)']);
deff('[dgg] = dg(i,u)',['dgg = [1,0,0;1,0,0;1,0,0]';'dgg=dgg(i,:)']);
deff('[u0,du0] = guess(x)' , ['u0= 0', 'du0 =0']);
n=1;m=[3];fixpnt=[5];xL=0;xR=10;Dx=0.1;xM = 5;
x = [xL:Dx:xR];
zeta=[xL,xM,xR];
ipar=zeros(1,11);
ipar(3)=1;ipar(4)=1;ipar(5)=50000;ipar(6)=50000;ipar(7)=1;ipar(11)=1;
ltol=[1];
tol=[1.e-5];
u=bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,f,df,g,dg,guess);
xset('window',1)
plot(x,u(1,:),'x','y(x)','BVODE 3rd order solution')
xset('window',2)
plot(x,u(2,:),'x','dy/dx','BVODE 3rd order solution')
xset('window',3)
plot(x,u(3,:),'x','d2y/dx2','BVODE 3rd order solution')




Download at InfoClearinghouse.com 91 2001 Gilberto E. Urroz


Application of bvode to a fourth-order problem with two interior
fixed points

Consider the fourth-order ordinary differential equation

x
3
(d
4
y/dx
4
) +6x
2
(d
3
y/dx
3
) +6x(d
2
y/dx
2
) = (x+1)
1/2
,

subject to the boundary conditions y(1) = 1, dy/dx|
x=1.2
= -2, d
2
y/dx
2
|
x=1.7
= -0.5, d
3
y/dx
3
|
x=7
= -
0.1. With u
1
= y, u
2
= dy/dx, u
3
= d
2
y/dx
2
, u
4
= d
3
y/dx
3
, and u
5
= d
4
y/dx
4
, the fourth-order
system is written as
d
5
y/dx
5
= f(x,u) = (1-6x
2
u
4
- 6xu
3
)/x
3
+ (x+1)
1/2
.

The boundary conditions will be described by

g
1
=g(1,u) = u
1
- 1, g
2
=g(2,u) = u
2
+ 2, g
3
=g(3,u) = u
3
+ 0.5, g
4
=g(4,u) = u
3
+ 0.1.

The derivatives of functions f and g are given by

df
1
= 0, df
2
= 0, df
3
= -6/x
2
, df
4
= -6/x,

and
.
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1
1
1
1
]
1

dg

The parameters fixptn and ipar(11) are redefined as fixptn = [1.2,1.7] and ipar(11) = 2. Since
we have four possible derivatives to deal with in the boundary conditions we want to redefine
ltol and tol as ltol = [1,2,3,4] and tol=[1e-5,1e=5,1e-5,1e-5], respectively. The value of ipar(4)
is changed to ipar(4) = 4. The value of m is changed to m = [4].

The following SCILAB script produces the solution for the problem just described.

//Script for 4th order boundary value problem solution with bvode
//Problem includes 2 interior points
deff('[ff] = f(x,u) ','ff=(1-6*x**2*u(4)-6*x*u(3))/x**3+sqrt(x+1)');
deff('[dff] = df(x,u)','dff = [0,0,-6/x**2,-6/x]');
deff('[gg] = g(i,u) ',['gg=[u(1)-1,u(2)+2,u(3)+0.5,u(4)+0.1]','gg=gg(i)']);

Download at InfoClearinghouse.com 92 2001 Gilberto E. Urroz
deff('[dgg] = dg(i,u)',['dgg = [1,0,0,0;0,1,0,0;0,0,1,0;0,0,0,1]';...
'dgg=dgg(i,:)']);
deff('[u0,du0] = guess(x)' , ['u0= 0', 'du0 =0']);
n=1;m=[4];fixpnt=[1.3,1.7];xL=1;xR=2;Dx=0.1;x = [xL:Dx:xR];
zeta=[1,1.3,1.7,2];
ipar=zeros(1,11);
ipar(3)=1;ipar(4)=4;ipar(5)=20000;ipar(6)=20000;ipar(7)=1
ipar(11)=2;
ltol=[1,2,3,4]
tol=[1.e-11,1.e-11,1.e-11,1.e-11]
u=bvode(x,n,m,xL,xR,zeta,ipar,ltol,tol,fixpnt,f,df,g,dg,guess);
xset('window',1); plot(x,u(1,:),'x','y(x)','BVODE 4th order solution')
xset('window',2); plot(x,u(2,:),'x','dy/dx','BVODE 4th order solution')
xset('window',3); plot(x,u(3,:),'x','d2y/dx2','BVODE 4th order solution')






Download at InfoClearinghouse.com 93 2001 Gilberto E. Urroz






B Bo ou un nd da ar ry y v va al lu ue e p pr ro ob bl le em ms s w wi it th h e ei ig ge en nv va al lu ue es s

Consider the boundary value problem

, 0
2
2
+ y
dx
y d


subject to y(0) = 0 and y(L) = 0, where is an unknown value. Assuming that > 0, the
solution is a sinusoidal wave, i.e.,

y(x) = C
1
sin(x) + C
2
cos(x).

Replacing the boundary condition y(0) = 0 produces 0 = C
2
, thus, the solution reduces to

y(x) = C
1
sin(x).

The second boundary condition, namely, y(L) = 0, produces the eigenvalue equation

0 = C
1
sin(L).

Since, in general, we want the constant C
1
to be different from zero, the equation is satisfied
if
sin(L) = 0,

i.e., if L = , 2, Thus, the problem has an infinite number of eigenvalues,
n
= n
2

2
/L
2
,
n=1,2, Associated with each eigenvalue is the eigenfunction sin(nx/L). The most general
solution to the original boundary value problem is a linear combination of the eigenfunctions,
i.e.,
. sin ) (
1
2
2 2

,
_


n
n
L
x n
C x y



Notice that the ordinary differential equation that defines the boundary value problem has a
solution only for specific values of the constant .


Numerical solution to a boundary value problem with eigenvalues

Using finite difference approximations for the derivatives, it is sometimes possible to find a
few eigenvalues of a boundary value problem such as the one described above. Using, for
example, a centered finite difference formula for the derivative d
2
y/dx
2
, i.e.,

,
) (
2
2
1 1
2
2
x
y y y
dx
y d
i i i

+



Download at InfoClearinghouse.com 94 2001 Gilberto E. Urroz
into the differential equation
, 0
2
2
+ y
dx
y d


produces the following difference equation

,
2
1 1
) (
2
i
i i i
y
x
y y y

+
+


for i=2,3,,n-1. Implied in the latter result is the fact that the range of values of x, i.e., 0 < x
< L, is divided into n-1 increments of size x = L/(n-1). Thus, y
i
= y(x
i
), where x
i
= ix. Also,
y
1
= y(0) = 0, and y
n
= y(L) = 0.

The problem involves n-2 unknowns y
2
,y
3
,,y
n-1
, in n-2 equations. For example, for L = 1, n =
5, x = 1/(5-1) = 0.25, 1/(x)
2
= 16. The general equation becomes

, 16 32 16
1 1 i i i i
y y y y +
+


for i=2,3,4. We have, therefore, the following three equations:

16y
1
-32y
2
+16y
3
= -y
2
,
16y
2
-32y
3
+16y
4
= -y
3
,
16y
3
-32y
4
+16y
5
= -y
4
.






With y
1
= y
5
= 0, the three equations result in

-32y
2
+16y
3
= -y
2
,
16y
2
-32y
3
+16y
4
= -y
3
,
16y
3
-32y
4
= -y
4
.

The resulting system of equations can be written in matricial form as

,
32 16 0
16 32 16
0 16 32
4
3
2
4
3
2
1
1
1
]
1


1
1
1
]
1

1
1
1
]
1

y
y
y
y
y
y


or, with

, ,
32 16 0
16 32 16
0 16 32
4
3
2
1
1
1
]
1

1
1
1
]
1

y
y
y
y A

as,
Ay = y.

Download at InfoClearinghouse.com 95 2001 Gilberto E. Urroz

This is the classical eigenvalue problem which can be solved using SCILAB function spec or the
user-defined function eigenvectors developed in Chapter 5.


A function for calculating eigenvalues for a boundary value problem

The following function, BVPeigen1, programs the solution to the boundary value problem
described earlier, namely,
, 0
2
2
+ y
dx
y d


subject to y(0) = 0 and y(L) = 0, where is an unknown value. The function call is

[x,y,lam] = BVPeigen1(L,n)

where x is a vector containing the values x
i
, y is a matrix whose columns are the eigenvectors
of the eigenvalue problem developed earlier (these eigenvectors are computed using function
eigenvectors, developed in Chapter 5), and lam is a vector containing the n eigenvalues of the
problem. The arguments of the function are the domain length L and the number of points in
the solution n. The function also plots the eigenvectors for the first five eigenvalues found.
These eigenvectors represent eigenfunctions y
n
(x). A listing of the function follows:

function [x,y,lam] = BVPeigen1(L,n)

Dx = L/(n-1);
x=[0:Dx:L];
a = 1/Dx^2;
k = n-2;

A = zeros(k,k);
for j = 1:k
A(j,j) = 2*a;
end;
for j = 1:k-1
A(j,j+1) = -a;
A(j+1,j) = -a;
end;

getf('eigenvectors');

[yy,lam]=eigenvectors(A);
//disp('yy');disp(yy);

y = [zeros(1,k);yy;zeros(1,k)];
//disp('y');disp(y);


xmin=min(x);xmax=max(x);ymin=min(y);ymax=max(y);
rect = [xmin ymin xmax ymax];
xset('window',1);xset('mark',[-1:-1:-10],1);
if k>=5 then
m = 5;
else
m = k;
end

for j = 1:m
plot2d(x',y(:,j), j,'011',' ',rect);

Download at InfoClearinghouse.com 96 2001 Gilberto E. Urroz
//plot2d(x',y(:,j),-j,'011',' ',rect);
end;
xtitle(Eigenfunctions for D2y+lam*y=0,x,y)


For example, for L = 1 and n = 5, the following solution is obtained:

-->getf('BVPeigen1')

-->[x,y,lam]=BVPeigen1(1,5)

lam = ! 9.372583 32. 54.627417 !


y =

! 0. 0. 0. !
! .5 - .7071068 .5 !
! .7071068 3.140E-16 - .7071068 !
! .5 .7071068 .5 !
! 0. 0. 0. !
x =

! 0. .25 .5 .75 1. !

Notice that the first eigenvalue found is
1
= 9.372583, close to the theoretical value of
2
=
9.8696. A plot of the eigenfunctions follows.



To see the eigenfunctions in a more continuous fashion, we call function BVPeigen1 using n =
50:

-->[x,y,lam]=BVPeigen1(1,50);

The first eigenvalue for n=50 is 9.866224, closer to the theoretical value of
2
= 9.8696 than
the first eigenvalue found earlier for n=5. The plot of the eigenfunctions for n=50 is shown
below.


Download at InfoClearinghouse.com 97 2001 Gilberto E. Urroz


Notice that if the equation under consideration, d
2
y/dx
2
+ y = 0, represents the equation of a
vibrating string, the eigenfunctions represent what are referred to as the different modes of
vibration of the string. The eigenvalues represent the different natural angular frequencies of
vibration of the string.


E Ex xe er rc ci is se es s


[1]. Determine the general solution to the following linear ordinary differential equations using
the corresponding characteristic equation:

(a) d
3
y/dx
3
+ 4(dy/dx) + 5y = 0. (b) d
2
y/dx
2
+ 2(dy/dx)+y = 0.
(c) d
4
y/dx
4
+ d
2
y/dx
2
+ dy/dx + 3y = 0 (d) d
2
y/dx
2
- 3y = 0

[2]. Obtain the particular solution to the following second order equations:

(a) d
2
y/dx
2
+ 3(dy/dx) + y = 2e
-x
(b) d
2
y/dx
2
+ 2y = 2x
2
+ x
(c) d
2
y/dx
2
+ dy/dx = sin(2x) (d) d
2
y/dx
2
+ y = cos x

[3]. Plot the time variation of position, velocity, and acceleration of a damped mechanical
oscillator for the following parameters:

(a) m = 2 kg, = 0.01 Ns/m, k = 2 N/m, x
0
= 0.2 m, v
0
= 1.2 m/s
(b) m = 4 kg, = 0.10 Ns/m, k = 2 N/m, x
0
= 0.2 m, v
0
= 1.2 m/s
(c) m = 1 kg, = 0.02 Ns/m, k = 2 N/m, x
0
= 0.2 m, v
0
= 1.2 m/s
(d) m = 0.5 kg, = 0.25 Ns/m, k = 2 N/m, x
0
= 0.2 m, v
0
= 1.2 m/s

[4]. Plot v-vs-x, a-vs-x, and a-vs-v phase portraits of the motions described in problem [3].

[5]. The mechanical oscillators described in problem [3] are subjected, respectively, to the
driving forces shown below. In each case, plot the time variation of position, velocity, and
acceleration of the resulting motions. Also, plot the v-vs-x, a-vs-x, and a-vs-v phase portraits
of the motions.

(a) F
0
= 2.5 N,

= 0.5 rad/s (b) F
0
= 10 N,

= 0.05 rad/s
(c) F
0
= 0.5 N,

= 1.5 rad/s (d) F
0
= 4 N,

= 0.25 rad/s


Download at InfoClearinghouse.com 98 2001 Gilberto E. Urroz
[6]. For the following functions approximate the derivative df/dx at x = a with (f(a+h)-f(a))/h
using values of h = 0.1, 0.01, 0.001, 0.0001, 0.00001. Plot the error involved in the numerical
estimate of the derivative against the value of h.
(a) f(x) = sin(2x), x = (b) f(x) = (x
2
+3x)/(x+1), x = 2
(c) f(x) = 1/(1+x
2
), x = -1 (d) f(x) = tan(x), x = /4

[7]. Repeat problem [4] but using a centered difference, i.e., (f(a+h)-f(a-h))/(2h).

[8]. Repeat problem [4] for the second derivative d
2
f/dx
2
using the forward difference
approximation (f(a+2h)-2f(a+h)+f(h))/h
2
.

[9]. Repeat problem [4] for the second derivative d
2
f/dx
2
using the centered difference
approximation (f(a-h)-2f(a)+f(a+h))/h
2
.

[10]. Given the ODE,
dy/dx = ysin(x),

and the boundary condition,
y(0) = 1,

write a SCILAB function that uses the Euler method to obtain a numerical solution to this ODE
in the interval 0 < x < 2.5. Use x = 0.25, 0.1, and 0.05. The exact solution is given by

y = 2/(cos x+1).

Plot the numerical solution against the exact solution for comparison.

[11]. Write a SCILAB function to produce an implicit solution for the first-order ODE from
problem [10]. The exact solution is y(x) = 1/x.

[12]. Write a SCILAB function to complete the explicit solution for the second-order ODE
d
2
y/dx
2
+ y = 0. An outline of the solution is presented elsewhere in this chapter. The exact
solution is y(x) = sin x + cos x.

[13]. Write a SCILAB function to complete the implicit solution for the second-order ODE from
problem [12]. The exact solution is y(x) = sin x+cos x.

[14]. Use SCILAB function ode to solve the following ordinary differential equations
numerically. Plot the numerical results for the different values of x.

(a) dy/dx = xy + sin(x), y(0) = 1, a = 0, b = 1, x = 0.2, 0.1, 0.05, 0.01
(b) dy/dx = sin(x)cos(y), y(0) = 0, a = 0, b = , x = /10, /20, p/50, /100
(c) dy/dx = exp(xy), y(0) = 1, a = 0, b = 10, x = 1, 0.5, 0.2, 0.1
(d) dy/dx = x
2
- sin(x), y(0) = 2, a = 0, b = 5, x = 0.5, 0.25, 0.1, 0.05

[15]. Solve the following systems of differential equations using matrices. Plot the solutions
against x.

(a) dy
1
/dx = 2y
1
- y
2
, dy
2
/dx = 2(y
2
-y
1
), y
1
(0) = 1, y
2
(0) = -1
(b) dy
1
/dx = -5y
1
+ y
2

+ x
2
, dy
2
/dx = -y
1
-y
2

- 5x, y
1
(0) = 0, y
2
(0) = 2
(c) dy
1
/dx = 2y
1
- y
2
, dy
2
/dx = 2y
2
-y
1
, dy
3
/dx = y
3
-y
1
, y
1
(0) = 1, y
2
(0) = -1, y
3
(0) = 2
(d) dy
1
/dx = 2y
1
- y
2

+ y
3

+ x, dy
2
/dx = -y
1
+ 2y
2

-2x, dy
3
/dx = y
3
-y
1
, y
1
(0) = 1, y
2
(0) = -1, y
3
(0)
= 2


Download at InfoClearinghouse.com 99 2001 Gilberto E. Urroz
[16]. Solve the systems of differential equations of problem [15] using SCILAB function ode.
Plot the solutions against x.

[17]. Convert the following linear differential equations into systems of first-order ODEs and
solve for y(x) using SCILAB function ode. Plot the solution y(x):

(a) d
2
y/dx
2
+ dy/dx + 2y = x, y(0) = 1, dy/dx = -1 at x = 0.
(b) d
3
y/dx
3
- 5(dy/dx) + y = 0, y(0) = 0, dy/dx = -1 and d
2
y/dx
2
= 1 for x = 0
(c) d
3
y/dx
3
- (d
2
y/dx
2
) + y = x, y(0) = 0, dy/dx = -1 and d
2
y/dx
2
= 1 for x = 0
(d) d
2
y/dx
2
+ 2y = sin(x), y(0) = 1, dy/dx = -1 at x = 0.

[18]. The figure below shows two particles P
1
and P
2
, of mass m
1
and m
2
, respectively, linked
by three springs (k
A
, k
B
, k
C
). The figure at the top represents the system in their state of
equilibrium, while the one at the bottom shows the system at any generic point at time t>0.
The displacement of particle P
1
with respect to its equilibrium position is given by x
1
(t) while
that of particle P
2
is given by x
2
(t). The corresponding velocities are v
1
= dx
1
/dt and v
2
=
dx
2
/dt. The magnitude of the forces applied by the springs on the particles are given by
Hooke's law, F = k(L-L
0
), where L is the stretched length of the spring, L
0
is the unstretched
length of the spring, and k is the spring constant. The particles are also provided by dashpots
that produce a viscous damping force whose magnitude is given by F = v, where b is a damping
constant and v is the speed of the particle, i.e., F = (dx/dt), where x = position of the
particle.




(a) Write down the differential equations describing the motion of the two linked particles
including spring and damping forces as shown in the figure above.

(b) Solve for x
1
(t) and x
2
(t) if m
1
= 10 kg, m
2
= 20 kg, k
A
= 80 N/m, k
B
= 120 N/m, k
C
= 100 N/m,

1
= 1 Ns/m,
2
= 5 Ns/m. The initial conditions are given by x
1
(0) = 0.5 m, x
2
(0) = 0.25 m,
v
1
(0) = 0, v
2
(0) = 1.0 m/s. Plot the signals and the velocity versus time for 0 < t < 5 s.

(c) Solve for x
1
(t) and x
2
(t) if m
1
= 10 kg, m
2
= 20 kg, k
A
= 80 N/m, k
B
= 120 N/m, k
C
= 100 N/m,

1
= 0, for values of
2
=0, 0.1, 0.5, 1, and 5 Ns/m. The initial conditions are given by x
1
(0) =
0.5 m, x
2
(0) = 0.25 m, v
1
(0) = 0, v
2
(0) = 1.0 m/s. Plot the signals and the velocity versus time
for 0 < t < 5 s for the different values of
2
.

(d) Write the differential equations for x
1
(t) and x
2
(t) if an external force F
1
= F
0
sin(
0
t +
0
)
is applied to particle P
1
in addition to the spring and damping forces.

(e) Using the conditions of part (a) of this problem solve for x
1
(t) and x
2
(t) for the case in which
particle P
1
is subject to the external force F
1
= F
0
sin(
0
t +
0
) with F
0
= 20 N,
0
= 2.5 rad/s,

Download at InfoClearinghouse.com 100 2001 Gilberto E. Urroz
and
0
= 1.5
rad
. Plot the signals, velocity, and acceleration versus time for 0 < t < 5 s for the
different values of
2
.

[19]. Consider the following two-loop electric circuit with R
0
= 2500 ohms, R
1
= 1500 ohms, R
2

=
1000 ohms, L
1
= 500 henrys, L
2
= 800 henrys, C
1
= 0.00006 farads, and C
2
= 0.001 farads.



(a) Write down the system of differential equations describing the electric charges
q
1
(t)

and q
2
(t) in the capacitors C
1

and C
2
, respectively, and the electric currents I
1
(t)
and I
2
(t) in the loops when the switches are turned on.

(b) For q
1
(0)

= 0, q
2
(0) = 0, I
1
(0) = 0, I
2
(0) = 0, E
1
(t) = 0, E
2
(t) = 120 cos(30t) volts,
determine the electric currents I
1
(t) and I
2
(t). Plot the results for 0 < t < 120 s.

(c) For q
1
(0)

= 0, q
2
(0) = 0, I
1
(0) = 0.2 amperes, I
2
(0) = 0.1 amperes, E
1
(t) = 6 volts,
E
2
(t) = 12 volts, determine the electric currents I
1
(t) and I
2
(t). Plot the results for 0 < t
< 120 s.

(d) For q
1
(0)

= 50 coulombs, q
2
(0) = 100 coulombs, I
1
(0) = 0, I
2
(0) = 0.12 amperes,
E
1
(t) = 6 sin (10 t) volts, E
2
(t) = 12 cos(30t), determine the electric currents I
1
(t) and
I
2
(t). Plot the results for 0 < t < 120 s.

(e) For q
1
(0)

= 0, q
2
(0) = 0, I
1
(0) = 0, I
2
(0) = 0, E
1
(t) = E
2
(t) = 120 cos(120t),
determine the electric currents I
1
(t) and I
2
(t). Plot the results for 0 < t < 120 s.

[20]. The Zeemans equations can be used to model the fluctuations on the length of the
hearts fibers as the heart pumps blood through the blood vessels of a human body:

dx/dt = k(-y - x
3
/3 + rx)
dy/dt = x/k,

where x is a measure of the fiber length fluctuation, y is a measure of the electrical stimulus
that produces the fiber fluctuations, and k and r are constants. Solve the Zeemans equation
for the following parameters:

(a) k = 0.5, p = 1, x(0) = 0, y(0) = -1 (b) k = 0.5, p =5, x(0) = 0, y(0) = -1
(c) k = 0.5, p = 10, x(0) = 0, y(0) = -1 (d) k = 0.5, p = 20, x(0) = 0, y(0) = -1


Download at InfoClearinghouse.com 101 2001 Gilberto E. Urroz
Plot the signals x vs. t, y vs. t, and the phase portrait x vs. y.


[21]. The Lorenz equations are used to simulate the convection of a layer of fluid of infinite
horizontal extent heated from below. The model is a simplified version of the heating of the
atmosphere. The equations are obtained by expanding the terms for temperature and
pressure involved in the problem with their Fourier series expansion and simplifying the
expansion to the first three modes represented by the variables x, y, and z. The resulting
system of equations is
dx/dt = (-x+y)
dy/dt = rx - y - xz
dz/dt = xy - bz

where , r, and b are constants that result from combining physical parameters of the
problem. (For a detailed derivation refer to Berge, P., Y. Pomeau, and C. Vidal, 1984, Order
within Chaos - Towards a deterministic approach to turbulence, John Wiley & Sons, New
York).

Solve the Lorenz equations for the following combination of parameters:

(a) = 10, r = 25, b = 2.666, x
0
= 1, y
0
= 1, z
0
= 1
(b) = 10, r = 75, b = 2.666, x
0
= 1, y
0
= 1, z
0
= 1
(c) = 10, r = 25, b = 2.666, x
0
= 1, y
0
= 1, z
0
= 1
(d) = 10, r = 25, b = 2.666, x
0
= 1, y
0
= 1, z
0
= 1

Plot the signals x-vs-t, y-vs-t, z-vs-t, as well as the phase portraits x-vs-y, x-vs-z, and y-vs-z.

[22]. The governing equation for a pendulum of length L is the second-order ODE,

d
2
/dt
2
+ (g/L) = 0,

where g is the acceleration of gravity, and is the angle measured from the vertical position of
the string. Solve the pendulum equation for the following conditions:

(a) L = 1.2 m, g = 9.806 m/s
2
,
0
= /3, (d/dt)
0
= -1
(b) L = 3 ft, g = 32.2 ft/s
2
,
0
= /6, (d/dt)
0
= 1
(c) L = 2.0 m, g = 9.806 m/s
2
,
0
= /2, (d/dt)
0
= -0.5
(d) L = 6 ft, g = 32.2 ft/s
2
,
0
= 3/4, (d/dt)
0
= -0.5

Plot the signals -vs-t, and (d/dt)-vs-t. Also, plot the phase portrait d/dt-vs- .

[23]. Repeat the solutions of problem [22] if the pendulum is subjected to a periodic
excitation so that the governing equation becomes

d
2
/dt
2
+ (g/L) = (F
0
/(mL)) cos(t+).

The values to use for each of the cases in problem [22] are as follows:

(a) F
0
= 2.5 N, m

= 0.2 kg, = /2 rad/s, = /3
(b) F
0
= 0.5 N, m

= 0.8 kg, = 1.0 rad/s, = 0
(c) F
0
= 1.5 N, m

= 1.2 kg, = 0.1 rad/s, = 2/3
(d) F
0
= 3.0 N, m

= 0.1 kg, = 0.05 rad/s, = -/3

[24]. Solve the following boundary value problem using the shooting method.

Download at InfoClearinghouse.com 102 2001 Gilberto E. Urroz

(a) d
2
y/dx
2
+ 3(dy/dx) + 2y = sin(2x), y(0) = 1, y(1) = 0
(b) d
2
y/dx
2
- 3y = 1 + x, y(0) = -1, dy/dx|
x = 1
= -0.5
(c) d
2
y/dx
2
+ dy/dx = ln(x), dy/dx|
x = 0
= 0, y(1) = 1
(d) d
2
y/dx
2
- dy/dx - y = 2 sin(x), y(1) = -2, y(2) = 3

[25]. Solve the boundary value ODEs of problem [24] by using an implicit solution with finite
differences.

[26]. Solve the boundary value ODEs of problem [24] by using function bvode.

[27]. Solve the following boundary value ODEs using function bvode:

(a) d
3
y/dx
3
+ y = 1 + x
2
, y(0) = 1, y(1) = 2, y(3) = -1
(b) d
3
y/dx
3
+ d
2
y/dx
2
= x, y(0) = 1, dy/dx|
x=1
= -1, y(2) = 0
(c) d
2
y/dx
2
- dy/dx = e
-x/2
, dy/dx|
x=0
= -1, y(1) = 2
(d) d
3
y/dx
3
+ dy/dx = -1 + x, y(0) = 1, dy/dx|
x=2
= -1, d
2
y/dx
2
|
x=2
= 1

[28]. Determine the first n eigenvalues of the problem d
2
y/dx
2
+ y = 0, subject to y(0) = 0,
y(L) = 0, for the following combinations of values of n and L:

(a) n = 10, L =10 (b) n = 20, L = 5
(c) n = 15, L = 1 (d) n = 30, L = 100

[29]. For the differential equation

x
2
(d
2
y/dx
2
)+x(dy/dx)'+(1+)y=0, y(1) = y(2) = 0,

use centered-difference approximations for the derivatives to perform an implicit numerical
solution. Obtain the first 10 eigenvalues of the problem. Plot the corresponding
eigenfunctions.

[30]. Solve the following system of equations

dx/dt = -y(x
2
+y
2
),

dy/dt = x(x
2
+y
2
),

for the initial conditions x(0) = 2, y(0) = 1. Plot the signals x-vs-t and y-vs-t, as well as the
phase portraits x-vs-y, (dx/dt)-vs-x, (dy/dt)-vs-y, and (dy/dt)-vs-(dx/dt).

[31]. The following system of equations is known as a set of coupled logistic equations and can
be used to model the behavior of two linked populations x
1
(t) and x
2
(t):

dx
1
/dt = kx
1
(1 - (x
1
+x
2
)/N),

dx
2
/dt = kx
2
(1 - (x
1
+x
2
)/N).

Solve the system for the following combination of parameters and initial conditions:

(a) k = 1, N = 1, (x
1
)
0
= 1, (x
2
)
0
= 1 (b) k = 0.5, N = 5, (x
1
)
0
= 0, (x
2
)
0
= -1
(c) k = 2.5, N = 10, (x
1
)
0
= -2, (x
2
)
0
= 0 (d) k = 12, N = 25, (x
1
)
0
= 1, (x
2
)
0
= 1

[32]. The governing equation for gradually varied flow in an open channel is given by


Download at InfoClearinghouse.com 103 2001 Gilberto E. Urroz
,
1
2
0
F
S S
dx
dy
f



where S
0
is the slope of the channel bed, i.e., the rate of change of bed elevation, z, with
distance, x, along the channel bed (S
0
= -dz/dx), S
f
is the slope of the energy head, i.e., the
rate of change of the total energy head, H (energy per unit weight), with x (S
f
= -dH/dx), and F
is a dimensionless quantity known as the Froude number. The energy head, H, is the sum of
the bed elevation z, the water depth y, and the velocity head (kinetic energy per unit weight)
V
2
/(2g), i.e.,
H = z + y + V
2
/2g,

where V is the flow velocity in the cross section (V = Q/A, Q = flow discharge, A = cross-
sectional area). The energy slope is calculated by using Mannings equation with

,
3 / 10
3 / 4
2
A
P
C
nQ
S
u
f

,
_



where n is the Manning coefficient (a measure of the channel bed roughness), C
u
is a constant
that depends on the system of units used (C
u
= 1.0 for the S.I., C
u
= 1.486 for the English
system), and P is the wetted perimeter of the cross-section (part of the channel cross-sectional
perimeter in contact with the water).

The Froude number squared is calculated from

,
3
2
2
gA
T Q
F

where T is the top-width of the cross-section (i.e., the length of the free surface at the cross
section).

For a trapezoidal section of bottom width b, side slope z, and depth y, the area, wetted
perimeter, and top width are given by
A = (b+zy)y
P = b + 2y(1+z
2
)
1/2

T = b + 2zy

For a trapezoidal cross-section open channel with b = 2.5 ft, z = 1, S
0
= 0.00001, g = 32.2 ft/s
2
,
C
u
= 1.486, n = 0.012, Q = 5.0 ft
3
/s, and with initial conditions y = 2.5 ft at x = 10000 ft, plot
the solution y(x) between x = 0 and x = 10000 ft.

Notes on problem [32]:
(1). A plot of the solution y-vs-x is called the water surface profile or a backwater curve.
Backwater curves are created whenever there is a so-called control point in the channel. For
example, the depth of 2.5 ft at position x = 10000 ft, used as initial conditions in this problem,
could be created by a small sill placed across the channel at that position.

(2). At points where the energy slope, S
f
, is the same as the bed slope, S
0
, i.e., S
f
= S
0
, then
dy/dx = 0. At those points the flow is said to have reached uniform conditions (uniform flow)
and the constant depth thus achieved is referred to as the normal depth, y
n
. You can check
that for the conditions of the present problem y
n
= 2.311973 ft by solving for y from the
Mannings equation (S
f
(y) = S
0
).


Download at InfoClearinghouse.com 104 2001 Gilberto E. Urroz
(3). At a point where the Froude number is equal to 1, the flow is said to be critical. The
corresponding depth is referred to as the critical depth, y
c
. You can check that for the
conditions of the present problem y
c
= 0.4673298 ft by solving for y from the equation defining
the Froude number squared, i.e., F
2
(y) = 1.

Download at InfoClearinghouse.com 105 2001 Gilberto E. Urroz
R RE EF FE ER RE EN NC CE ES S ( (f fo or r a al ll l S SC CI IL LA AB B d do oc cu um me en nt ts s a at t I In nf fo oC Cl le ea ar ri in ng gh ho ou us se e. .c co om m) )
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.

Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.

Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.

Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.

Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.

Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.

Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.

Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.

Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.

Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.

Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.

Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.

Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.

Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.

Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.

Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.

Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.

Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.

Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.

Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.

Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.

Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.

Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.

Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.

McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.

Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.

Download at InfoClearinghouse.com 106 2001 Gilberto E. Urroz

Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.

Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.

Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.

Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.

Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.

Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.

Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.

Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.

Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.

Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.

Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.

Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.

Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.

Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.

Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.

Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.

Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.

Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.

Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.

Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.



Orthogonal Functions, Gaussian
Quadrature, and Fourier Analysis with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved

Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz

ORTHOGONAL FUNCTIONS, GAUSSIAN QUADRATURE, AND FOURIER ANALYSIS 3
Solutions to selected specific second-order differential equations 3
The Cauchy or Euler equation 3
Legendres equation 4
Gamma, log-gamma and di-gamma functions 4
Bessels equation 7
Chebyshev polynomials 11
Laguerres equation 12
Webers equation and Hermite polynomials 14
Integration by Gaussian quadrature 16
Gaussian quadrature 16
General form of Gaussian quadrature 18
User-defined SCILAB function for calculating Gaussian quadrature 19
Application of function gaussquad 19
Improper integrals 20
Gauss-Laguerre formulae for improper integrals 21
SCILAB user-defined function for calculating Gauss-Laguerre quadrature 21
Application of function laguerrequad to calculate improper integral 22
Gauss-Hermite formulae for improper integrals 23
SCILAB user-defined function for calculating Gauss-Hermite quadrature 24
Application of function hermitequad for calculating improper integrals 24
Integrals involving sine and cosine terms - Filons formulae 25
SCILAB user-defined function for calculating integrals with Filons formulae 26
Application of function filonquad 27
Orthogonal functions and series expansions 28
Series expansions with orthogonal functions 29
Orthonormal functions 30
Orthogonality with respect to a weight function 31
The Sturm-Liouville problem 33
Fourier Analysis 33
Fourier series 33
Fourier sine series 34
Fourier cosine series 37
Fourier Series 45
A user-defined SCILAB function to calculate Fourier series 46
Applications of function fourierseries 47
Fourier series for a piecewise periodic function 49
Calculating Fourier series coefficients with Filons sine-cosine formulae 52
An alternative for calculating the coefficients of the Fourier series is to use function filonquad, defined
earlier for calculating integrals of the form 52
Use of Fourier series in ordinary differential equations 55
Fourier series for a trapezoidal pulse 55
Solution to the damped oscillator equation with trapezoidal pulse and wave excitation 57
Fourier series for other periodic shapes 58
Solving the damped oscillator equation with a different periodic trapezoidal pulse 59
Fourier transforms 60
Fourier analysis of discrete data 61
SCILAB functions for discrete Fourier transform (DFT) and fast Fourier transform (FFT) 62
Scilabs dft function 62

Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz

Example of DFT applications: Obtaining the discrete Fourier transform 62
Example of DFT applications: Obtaining the inverse Fourier transform 63
SCILABs fft function 69
Example of FFT applications: Obtaining a discrete Fourier transform 69
Example of FFT applications: Obtaining an inverse Fourier transform 70
Discrete Fourier transform for two-dimensional functions 71
Example of FFT applications: Obtaining the discrete Fourier transform for a two-dimensional signal 71
Example of FFT applications: Obtaining a real two-dimensional inverse Fourier transform 72
Exercises 74
REFERENCES (FOR ALL SCILAB DOCUMENTS AT INFOCLEARINGHOUSE.COM) 79


Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz

O Or rt th ho og go on na al l f fu un nc ct ti io on ns s, , G Ga au us ss si ia an n
q qu ua ad dr ra at tu ur re e, , a an nd d F Fo ou ur ri ie er r a an na al ly ys si is s


Orthogonal functions are sets of functions of an independent variable, say x, given in terms of
an index n, i.e., g
n
(x). The functions g
n
(x) and g
m
(x) are said to be orthogonal on an interval
a < x < b, if

for n m. The notation (g
n
,g
m
) is used when defining linear vector spaces to indicate the
inner or dot product of two vectors, thus, for vectors u and v, (u,v) = u v. If vectors u and v
are orthogonal (in the Euclidean sense), (u,v) = u v = 0. By similarity with vectors, we can
state that if (g
n
,g
m
) = 0 for a set of functions g
n
(x), then functions g
n
and g
m
are orthogonal.

Orthogonal functions have a variety of applications in mathematics. For example, sets of
orthogonal sine and cosine functions can be used to expand a function into what is called a
Fourier series. Solutions to certain types of second-order differential equations constitute sets
of orthogonal functions (e.g., Bessel functions, Lagrange polynomials, Laguerre polynomials,
etc.) which can be used for evaluating integrals through a procedure refer to as Gaussian
quadrature.



S So ol lu ut ti io on ns s t to o s se el le ec ct te ed d s sp pe ec ci if fi ic c s se ec co on nd d- -o or rd de er r
d di if ff fe er re en nt ti ia al l e eq qu ua at ti io on ns s

In this section we present and solve specific types of ordinary differential equations whose
solutions are defined in terms of some classical functions, e.g., Bessels functions, Hermite
polynomials, etc. Most of the functions that solve these differential equations are orthogonal
functions.


The Cauchy or Euler equation

An equation of the form
x
2
(d
2
y/dx
2
) + ax (dy/dx) + by = 0,

where a and b are real constants, is known as the Cauchy or Euler equation. A solution to the
Cauchy equation can be found by assuming that

y(x) = x
n
.

Replacing this function into the differential equation and simplifying results in an auxiliary
algebraic equation:

If the equation has two different roots, say n
1
and n
2
, then the general solution of this
equation is
, 0 ) ( ) ( ) , ( = =
!
b
a
m n m n
dx x g x g g g
. 0 ) 1 (
2
= + + b n a n

Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz


y(x) = K
1
x
n
1
+ K
2
x
n
2
.

If b = (1-a)
2
/4, then the equation has a double root n
1
= n
2
= n = (1-a)/2, and the
solution turns out to be
y(x) = (K
1
+ K
2
ln x)x
n
.


Legendres equation

An equation of the form

(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+n (n+1) y = 0,

where n is a real number, is known as the Legendres differential equation. Any solution for
this equation is known as a Legendres function. When n is a nonnegative integer, the solutions
are called Legendres polynomials. Legendres polynomial of order n is given by



where M = n/2 or (n-1)/2, whichever is an integer. The first six Legendre polynomials are

P
0
(x) = 1.0,
P
1
(x) = x,
P
2
(x) = (3x
2
-1)/2,
P
3
(x) =(5x
3
-3x)/2,
P
4
(x) =(35x
4
-30x
2
+3)/8,
P
5
(x) =(63x
5
-70x
3
+15x)/8.


Gamma, log-gamma and di-gamma functions

Notice that the definition of the Legendre polynomials involves the use of factorials. SCILAB,
however, does not provide a factorial function per se. Instead it relies on the use of the
Gamma function, (), defined as

!


=
0
1
, ) ( dt t e
t


which is related to the factorial function for = n integer by:

(n+1) = n!, or (n) = (n-1)!.

m n
M
m
n
m
n
x
m n m n m
m n
x P
2
0
)! 2 ( )! ( ! 2
)! 2 2 (
) 1 ( ) (

=
"

=
... ...
)! 2 ( )! 1 ( ! 1 2
)! 2 2 (
) ! ( 2
)! 2 (
2
2
+

=
n
n
n
n
x
n n
n
x
n
n

Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz

SCILAB also provides function gammaln, which calculates the natural logarithm of the Gamma
function (the log-gamma function), i.e.,

gammaln(x) = ln[(x)].


Related to the log-gamma function is the di-gamma function, defined as the derivative of the
log-gamma function:
(z) = d[ln (z)]/dz = (z)/ (z),

which can be calculated by using SCILAB function dlgamma(x).


Legendres polynomials can be generated by using the following user-defined function
legendrepol:

function [pL] = legendrepol(n,var)

//Generates the Legendre polynomial
//of order n in variable var

if n == 0 then
cc = [1];
elseif n == 1 then
cc = [0 1];
else
if modulo(n,2) == 0 then
M = n/2
else
M = (n-1)/2
end;

cc = zeros(1,M+1);
for m = 0:M
k = n-2*m;
cc(k+1)=...
(-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1));
end;
end;

pL = poly(cc,var,'coeff');

//End function legendrepol

The following SCILAB statements generate the first 10 Legendre polynomials as elements of a
column vector p.

-->getf('legendrepol')

-->p = zeros(10,1);

-->for nn = 1:10
--> p(nn) = legendrepol(nn,'x');
-->end;

-->p
p =

! x !
! !

Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz

! 2 !
! - .5 + 1.5x !
! !
! 3 !
! - 1.5x + 2.5x !
! !
! 2 4 !
! .375 - 3.75x + 4.375x !
! !
! 3 5 !
! 1.875x - 8.75x + 7.875x !
! !
! 2 4 6 !
! - .3125 + 6.5625x - 19.6875x + 14.4375x !
! !
! 3 5 7 !
! - 2.1875x + 19.6875x - 43.3125x + 26.8125x !
! !
! 2 4 6 8 !
! .2734375 - 9.84375x + 54.140625x - 93.84375x + 50.273438x !
! !
! 3 5 7 9 !
! 2.4609375x - 36.09375x + 140.76562x - 201.09375x + 94.960937x !
! !
! 2 4 6 8 10 !
! - .2460938 + 13.535156x - 117.30469x + 351.91406x - 427.32422x + 180.42578x !



A plot of Legendre polynomials P
1
(x), P
2
(x), P
3
(x), P
4
(x), and P
5
(x), is produced using the
following SCILAB commands. A matrix pp of five columns stores the values of the polynomials,
one per column, corresponding to a vector of values of x, xx.


-->getf('legendrepol')

-->xx = [-1:0.01:1];

-->n = length(xx)
n = 201.

-->for j = 1:5
--> pp(:,j) = horner(p(j),xx');
-->end;

-->plot2d([xx',xx',xx',xx',xx'],[pp(:,1),pp(:,2),pp(:,3),pp(:,4),pp(:,5)],...
-->[1:1:5],'111','n=1@n=2@n=3@n=4@n=5',[-1 -1 1 1 ])

-->xtitle('Legendre polynomials','x','Pn(x)')


The result is the following graph:


Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz





The ODE
(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+[n (n+1)-m
2
/(1-x
2
)] y = 0,

has for solution the function

y(x) = P
n
m
(x)= (1-x
2
)
m/2
(d
m
Pn/dx
m
).

This function is referred to as an associated Legendre function.







Bessels equation

The ordinary differential equation

x
2
(d
2
y/dx
2
) + x (dy/dx)+ (x
2
-
2
) y = 0,

where the parameter is a nonnegative real number, is known as Bessels differential
equation. Solutions to Bessels equation are given in terms of Bessel functions of the first
kind of order :

"

=
+
+ +

=
0
2
2
,
) 1 ( ! 2
) 1 (
) (
m
m
m m
m m
x
x x J


Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz



where is not an integer.

If = n, an integer, the Bessel functions of the first kind for n =integer are defined by


For non-integer values , the solution to the Bessel equation is given by

y(x) = K
1
J

(x)+K
2
J
-
(x).

For integer values, the functions J
n
(x) and J
-n
(x) are linearly dependent, since

J
n
(x) = (-1)
n
J
-n
(x),


therefore, we cannot use them to obtain a general function to the equation. Instead, we
introduce the Bessel functions of the second kind defined as


Y

(x) = [J

(x) cos J

(x)]/sin ,

for non-integer , and for n integer, with n > 0, by



where is the so-called Euler constant, defined by




and h
m
represents the harmonic series

"

=
+
+

=
0
2
2
.
)! ( ! 2
) 1 (
) (
m
n m
m m
n
n
m n m
x
x x J
m
m
n m
n m m
m n
n n
x
n m m
h h x x
x J x Y
2
0
2
1
)! ( ! 2
) ( ) 1 (
)
2
(ln ) (
2
) (
+
+
+ + =
"

=
+
+

m
n
m
n m
n
x
m
m n x
2
1
0
2
! 2
)! 1 (



"

..., 0 5772156649 . 0 ] ln
1
...
3
1
2
1
1 [ lim + + + + =

r
r
r


Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz



For the case n = 0, the Bessel function of the second kind is defined as



With these definitions, a general solution of Bessels equation for all values of is given by

y(x) = K
1
J

(x)+K
2
Y

(x).

In some instances, it is necessary to provide complex solutions to Bessels equations by defining
the Bessel functions of the third kind of order as

H
n
(1)
(x) = J

(x)+iY

(x),
and
H
n
(2)
(x) = J

(x)iY

(x),

These functions are also known as the first and second Hankel functions of order .

In some applications you may also have to utilize the so-called modified Bessel functions of
the first kind of order defined as I

(x)= i
-
J

(ix), where i is the unit imaginary number.


These functions are solutions to the differential equation

x
2
(d
2
y/dx
2
) + x (dy/dx)- (x
2
+
2
) y = 0.


The modified Bessel functions of the second kind,

K

(x) = (/2)[I
-
(x)I

(x)]/sin ,

are also solutions of this ODE.

Bessel functions are available in SCILAB through the following functions:

besselj: Bessel functions of the first kind [J

(x)], with general call:



y = besselj(nu,x)


bessely: Bessel functions of the second kind [Y

(x)], with general call:



y = bessely(nu,x))


besseli: modified Bessel functions of the first kind [I

(x)], with general calls:



y = besseli(nu,x)
.
1
...
3
1
2
1
1
m
h
m
+ + + + =
.
) ! ( 2
) 1 (
)
2
(ln ) (
2
) (
2
0
2 2
1
0 0 #
$
%
&
'
(


+ + =
"

m
m
m
m
m
x
m
h x
x J x Y


Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz


y = besseli(nu,x,s)


besselk: modified Bessel functions of the second kind [K

(x)], with general calls:



y = besselk(nu,x)

y = besselk(nu,x,s)


In the calls to functions besselj, bessely, besseli, and besselk, the parameter nu represents the
order of the function , and x is a single non-negative value or vector of non-negative values
where the function is to be evaluated. The calls of functions besseli and besselk may include
the parameter s whose default value is 1. When the value of s is 2, the so-called exponentially
scaled Bessel functions are calculated.

To see a plot of the functions J
n
(x), for n = 0,1,2,3,4, use the following SCILAB commands:


-->xx = [0:0.1:10]; nu = [0,1,2,3,4]; n = length(xx), m = length(nu)

n =
101.
m =
5.

-->bb = zeros(n,m);

-->for j = 1:m
--> bb(:,j) = besselj(nu(j),xx);
-->end;

-->plot2d([xx',xx',xx',xx',xx'],[bb(:,1),bb(:,2),bb(:,3),bb(:,4),bb(:,5)],...
-->[1,2,3,4,5],'111','nu=1@nu=2@nu=3@nu=4@nu=5',[0,-0.6,10,1])

-->xtitle('Bessel functions J_nu(x)','x','J_nu(x)')



Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz



Chebyshev polynomials

The functions

T
n
(x) = cos(ncos
-1
x), and U
n
(x) = sin[(n+1) cos
-1
x]/(1-x
2
)
1/2
,

n = 0, 1, are called Chebyshev polynomials of the first and second kind, respectively. The
polynomials T
n
(x) are solutions of the differential equation

(1-x
2
)(d
2
y/dx
2
) x (dy/dx) + n
2
y = 0.

In SCILAB, the function chepol generates the Chebyshev polynomial of the first kind of order n,
given a value of n > 0. The general call to the function is

[Tn]=chepol(n,var)

where n is the order of the polynomial, and var is a SCILAB polynomial variable.

Chebyshev polynomials of the second kind are obtained from:

U
n
(x) = sin(narccos(x))/sin(arccos(x)).


The first four Chebyshev or Tchebycheff polynomials of the first and second kind are:

T
0
(x) = 1.0, U
1
(x) = 1.0, T
1
(x) = x, U
1
(x) =1.0,

T
2
(x) =2x
2
-1, U
2
(x) =2x, T
3
(x) = 4x
3
-3x, U
3
(x) =24x
2
-1.

The polynomials of the first kind can be obtained using SCILAB function chepol as follows:

-->T0 = chepol(0,'x')
T0 =
1

Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz


-->T1 = chepol(1,'x')
T1 =
x

-->T2 = chepol(2,'x')
T2 =
2
- 1 + 2x

-->T3 = chepol(3,'x')
T3 =
3
- 3x + 4x

-->T4 = chepol(4,'x')
T4 =
2 4
1 - 8x + 8x


Plots of the Chebyshev polynomials of the first kind for n = 0, 1, 2, 3, 4, are shown below:

-->xx = [-1:0.01:1];
-->y0 = horner(T0,xx);y1 = horner(T1,xx);y2 = horner(T2,xx);
-->y3 = horner(T3,xx);y4 = horner(T4,xx);
-->plot2d([xx',xx',xx',xx',xx'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','T0@T1@T2@T3@T4',[-1,-1,1,1])
-->xtitle('Chebyshev polynomials','x','T_n(x)')






Laguerres equation

Laguerres equation is the second-order, linear ODE of the form

x(d
2
y/dx
2
) +(1x) (dy/dx) + ny = 0.

Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz


Laguerre polynomials, defined as


are solutions to Laguerres equation. Laguerres polynomials can also be calculated with:


The term

is the m-th coefficient of the binomial expansion (x+y)
n
. It also represents the number of
combinations of n elements taken m at a time.

The Laguerre polynomials can be generated in SCILAB through the following user-defined
function, laguerrepol:

function [Lp] = laguerrepol(n,var)

//Calculates the Laguerre polynomial
//of order n in polynomial variable var

if n == 0 then
c = [1];

else
c = [];
for m = 0:n
c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))];
end;
end;

Lp = poly(c,var,'coeff');

//end function laguerrepol


The first four Laguerre polynomials are:

-->getf('laguerrepol')

-->L0 = laguerrepol(0,'x')
L0 =
1

-->L1 = laguerrepol(1,'x')
L1 =
1 - x

-->L2 = laguerrepol(2,'x')
L2 =
,... 2 , 1 ,
) (
!
) ( , 1 ) (
0
=

= =

n
dx
e x d
n
e
x L x L
n
x n n x
n
.
!
) 1 (
.... ...
4
) 1 (
1
!
) 1 (
) (
2
0
n
n
m
n
m
m
n
x
n
x
n n
x n x
m
n
m
x L

+ +

+ =
)
)
*
+
,
,
-
.

=
"
=
) , (
)! ( !
!
m n C
m n m
n
m
n
=

=
)
)
*
+
,
,
-
.

Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz

2
1 - 2x + .5x

-->L3 = laguerrepol(3,'x')
L3 =
2 3
1 - 3x + 1.5x - .1666667x

-->L4 = laguerrepol(4,'x')
L4 =
2 3 4
1 - 4x + 3x - .6666667x + .0416667x

The following is the plot of the functions L
0
(x), L
1
(x), L
2
(x), L
3
(x) and L
4
(x):

-->x = [-2:0.01:6];
-->y0 = horner(L0,x); y1 = horner(L1,x); y2 = horner(L2,x);
-->y3 = horner(L3,x); y4 = horner(L4,x);
-->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','L0@L1@L2@L3@L4',[-2,-5,6,5])
-->xtitle('Laguerre polynomials','x','L_n(x)')



Webers equation and Hermite polynomials

Webers equation is defined as

d
2
y/dx
2
+(n+1/2-x
2
/4)y = 0,

For n = 0, 1, 2, A particular solution of this equation is given by the function

y(x) = exp(-x
2
/4)H
*
(x/2),

where the function H
*
(x) is the Hermite polynomial:


Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz


To calculate Hermite polynomials in SCILAB we will use the following recurrence equation

H
*
n+1
(x) = 2xH
*
n
(x)-2nH
*
n-1
(x)

with H
0
*
(x) = 1, H
1
*
(x) = 2x. A user-defined function to produce the Hermite polynomials is
listed below:

function [Hn] = hermitepol(n,var)

//determines the Hermite polynomial of order n in variable var

H = zeros(1,n+1);
H(1) = poly([1],var,'coeff');
H(2) = poly([0 2],var,'coeff');
for j = 2:n
H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1);
end;
Hn = H(n+1);
//end function hermitepol
The following are the first five Hermite polynomials:

-->getf('hermitepol')

-->H0 = hermitepol(0,'x')
H0 =
1

-->H1 = hermitepol(1,'x')
H1 =
2x

-->H2 = hermitepol(2,'x')
H2 =
2
- 2 + 4x

-->H3 = hermitepol(3,'x')
H3 =
3
- 12x + 8x

-->H4 = hermitepol(4,'x')
H4 =
2 4
12 - 48x + 16x


A plot of these polynomials is shown below:

-->x=[-3:0.01:3]; y0 = horner(H0,x); y1 = horner(H1,x);

-->y2 = horner(H2,x); y3 = horner(H3,x); y4 = horner(H4,x);

-->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','H0@H1@H2@H3@H4',[-3,-30,3,20])

-->xtitle('Hermite polynomials','x','H_n(x)')
,... 2 , 1 ), ( ) 1 ( ) ( * , 1 *
2 2
0
= = =

n e
dx
d
e x H H
x
n
n
x n
n

Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz







I In nt te eg gr ra at ti io on n b by y G Ga au us ss si ia an n q qu ua ad dr ra at tu ur re e

In this section we present techniques of integration known as Gaussian quadrature as well as
other quadrature procedures that can be used for the calculation of improper integrals. Most
of the quadrature formulae require coefficients that can be obtained from orthogonal
polynomials such as Legendre, Laguerre, or Hermite polynomials.


Gaussian quadrature

The idea of Gaussian quadrature for the numerical calculation of integrals consists in
identifying a discrete number of points x
i
, i = 1,2,,n, and a corresponding number of
constants, A
i
, i = 1,2,,n, so that the integral is estimated as


!
"
=

b
a
n
i
i i
x f A dx x f
1
) ( ) ( .


The figure below illustrates the meaning of the coefficients A
i
as the base of rectangles whose
heights are given by f(x
i
). The sum of the areas of the rectangles approximates the integral.


Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz



Suppose that we take a = -1 and b = 1 in the integral, and we attempt to determine the values
of x
i
and A
i
for n = 2. We can accomplish this solution by calculating the Gaussian integral

2 ), ( ) ( ) ( ) (
1
1
2 2 1 1
1
= + =
!
"

=
n x f A x f A x f A dx x f
n
i
i i


exactly for the cases f(x) = 1, f(x) = x, f(x) = x
2
, f(x) = x
3
. Replacing these polynomials for f(x)
will produce four equations from which we can solve for the four unknowns, x
1
, x
2
, A
1
, and A
2
.
The equations produced are:

!

+ = = / =
1
1
2 1
, 2 1 1 ) ( For A A dx x f
!

+ = = / =
1
1
2 2 1 1
, 0 ) ( For x A x A dx x x x f
!

+ = = / =
1
1
2
2 2
2
1 1
2 2
,
3
2
) ( For x A x A dx x x x f
!

+ = = / =
1
1
3
2 2
3
1 1
3 3
. 0 ) ( For x A x A dx x x x f

The solution of the system of algebraic equations thus generated is

x
1
= -1/3, x
2
= 1/3, A
1
= 1, A
2
= 1.

The required integral is now written as

!

+
1
1
).
3
1
( )
3
1
( ) ( f f dx x f

The integral calculated with this formula will be exact if f(x) = 1, x, x
2
, or x
3
, and approximate
for any other function. If we want to calculate an integral of function f(x) in the interval [a,b]
we introduce the following change of variable

,
2
a b
b a x
t


=


Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz

so that for x = a, t = -1, for x = b, t = 1, and dx =(1/2)(b-a)dt, thus the integral is calculated as

! !

+
+

=
b
a
dt
a b
t
a b
f a b dx x f
1
1
)
2 2
( ) (
2
1
) (

)].
2 2
3
1
( )
2 2
3
1
( [ ) (
2
1 a b a b
f
a b a b
f a b
+
+

+
+
+




General form of Gaussian quadrature

We can generalize the previous result for any integer n by writing

. )
2 2
( ) (
2
1
)
2 2
( ) (
2
1
) (
1
1
1
"
! !
=

+
+


+
+

=
n
i
i i
b
a
a b
x
a b
f A a b dt
a b
t
a b
f a b dx x f

where x
i
, i = 1,2,,n, are the values obtained from

!
"

1
1
1
) ( ) (
n
i
i i
x f A dx x f .

For values of n>2, the values x
i
and A
i
are calculated by solving the 2n equations of the form

. 1 2 ,..., 2 , 1 , 0 , ,..., 2 , 1 ,
1
1
1
= = =
!
"

=
n k n i dx x x A
k
n
i
k
i i


It can be shown that the values of x
i
, i=1,2,,n, are the roots of the Legendre polynomial P
n
(x),
while the coefficients A
i
, i = 1,2,,n, are related to the derivative of the n-th Legendre
polynomial, P
n
(x) = dP
n
/dx, by

.
)] ( ' )[ 1 (
2
2 2
i n i
i
x P x
A

=

Legendres polynomial of order n are the solutions to the Legendres ordinary differential
equation
(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+n (n+1) y = 0,

and are given by


where M = n/2 or (n-1)/2, whichever is an integer.

m n
M
m
n
m
n
x
m n m n m
m n
x P
2
0
)! 2 ( )! ( ! 2
)! 2 2 (
) 1 ( ) (

=
"

=
... ...
)! 2 ( )! 1 ( ! 1 2
)! 2 2 (
) ! ( 2
)! 2 (
2
2
+

=
n
n
n
n
x
n n
n
x
n
n

Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz

User-defined SCILAB function for calculating Gaussian quadrature

A user-defined SCILAB function, legendrepol, was introduced earlier for generating Legendre
polynomials. The following function, gaussquad, incorporates function legendrepol for the
calculation of Gaussian integrals according to the formulae developed earlier.

function [I] = gaussquad(a,b,f,n)

//Calculates the integral of function f
//in the range [a,b] using the Gaussian
//quadrature approximation with n>=2 intervals
p = legendrepol(n,'x');
xr = roots(p);
A = [];
for j = 1:n
pd = derivat(p);
A = [A 2/((1-xr(j)^2)*(horner(pd,xr(j)))^2)];
end;
tr = ((b-a)/2.*xr)+((b+a)/2);
I = (b-a)/2*A*f(tr);
//end of function gaussquad
//================================================
function [pL] = legendrepol(n,var)
//Generates the Legendre polynomial
//of order n in variable var
if n == 0 then
cc = [1];
elseif n == 1 then
cc = [0 1];
else
if modulo(n,2) == 0 then
M = n/2
else
M = (n-1)/2
end;
cc = zeros(1,M+1);
for m = 0:M
k = n-2*m;
cc(k+1)=...
(-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1));
end;
end;
pL = poly(cc,var,'coeff');
//End function legendrepol


Application of function gaussquad

To illustrate the use of function gaussquad we will use it to integrate the function f(x) =
e
x
/(1+x
2
)
2
for n = 2, 3, 4, 5, 6, 7, 8 , 9, 10, 12, 14, 16, 18, and 20. A plot of the function in the
range of integration is presented below.

-->deff('[y]=f(x)','y=exp(x)./(1+x^2)^2')
-->x=[0:0.05:10];y=f(x);plot(x,y,'x','y','f(x)=exp(x)/(1+x^2)^2')



Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz



The following table shows the values of the integral:

-->Res = [ ];

-->for j=1:length(nn)
--> Res=[Res; [nn(j),gaussquad(0,10,f,nn(j))]];
-->end;

-->disp(' n Int'), disp(Res)




n Int

! 2. 4.7168792 !
! 3. 5.7578069 !
! 4. 5.9621965 !
! 5. 5.8817127 !
! 6. 5.8034783 !
! 7. 5.7774564 !
! 8. 5.778712 !
! 9. 5.7846526 !
! 10. 5.7877236 !
! 12. 5.7879883 !
! 14. 5.7876393 !
! 16. 5.787655 !
! 18. 5.7876657 !
! 20. 5.7876644 !



Improper integrals

Improper integrals is the name given to definite integrals that have infinite limits, for example:

! ! !


+
= =
+
=
1
2
0
2
2
1
2
2
1
.
1
, ,
1 x
dx
I
x
dx
I
x
dx
I


Symbolic integration of improper integrals is accomplished by using limits of the definite
integral as a dummy variable, , approaches plus or minus infinity, e.g.,


Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz

.
4
3
)
2
(
4
)) arctan(
4
( lim | ) arctan( lim
1
lim
1
1
1
2
1
2
1


= = = =
+
=
+
=

! !
x
x
dx
x
dx
I


Gauss-Laguerre formulae for improper integrals

Gauss-Laguerre formulae can be used to numerically evaluate integrals of the form shown
below through a procedure similar to Gaussian quadrature:

!
"


0
1
. ) ( ) (
n
i
i i
x
x g A dx x g e

The values of x
i
, i=1,2,,n, are the roots of the Laguerre polynomial on order n, L
n
(x), and the
coefficients A
i
, i=1,2,,n, are calculated from

.
)] ( ' [
1
)] ( [ ) 1 (
2 2
1
2
i n i n
i
i
x L x x L n
x
A

=
+
=
+



Laguerre polynomials are the solutions to the Laguerres differential equation

x(d
2
y/dx
2
) +(1x) (dy/dx) + ny = 0,

and are given by



where

is the binomial coefficient.

To calculate an improper integral of a general function, f(x), we will use the following formula:

!
"
!

=
0
1
0
. ) ( )} ( { ) (
n
i
i
x
i
x x
x f e A dx x f e e dx x f
i


Of course, the indefinite integral shown can be evaluated only if the integrand decreases
quickly towards zero as x grows without bound. The integral in this case is said to be
convergent.


SCILAB user-defined function for calculating Gauss-Laguerre quadrature

) , (
)! ( !
!
m n C
m n m
n
m
n
=

=
)
)
*
+
,
,
-
.
.
!
) 1 (
.... ...
4
) 1 (
1
!
) 1 (
) (
2
0
n
n
m
n
m
m
n
x
n
x
n n
x n x
m
n
m
x L

+ +

+ =
)
)
*
+
,
,
-
.

=
"
=

Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz

Earlier in this chapter we defined function laguerrepol to generate Laguerre polynomials. The
following function, laguerrequad, which incorporates function laguerrepol, is used to calculate
the improper integral shown above.

function [I] = laguerrequad(f,n)
//Calculates the improper integral of function f(x)
//in the interval [0,infinity] using Gauss-Laguerre
//quadrature with n >= 2 intervals
p = laguerrepol(n,'x');
pd = derivat(p);
pnp1 = laguerrepol(n+1,'x');

xr = roots(p);

A=[];
for i = 1:n
A = [A xr(i)/((n+1)^2*(horner(pnp1,xr(i)))^2)];
end;

I = A*(exp(xr).*f(xr));


//end of function laguerrequad
//===============================================================
function [Lp] = laguerrepol(n,var)

//Calculates the Laguerre polynomial
//of order n in polynomial variable var

if n == 0 then
c = [1];

else
c = [];
for m = 0:n
c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))];
end;
end;

Lp = poly(c,var,'coeff');

//end function laguerrepol

Application of function laguerrequad to calculate improper integral

We use function laguerrequad to calculate the improper integral

!

+
=
0
2
1
.
1 x
dx
I

The following SCILAB commands define the function to be integrated, f(x) = 1/(1+x
2
). A plot of
the function is shown next.

-->deff('[y]=f(x)','y=(1+x^2)^(-1)')

-->x=[0:0.1:10];y=f(x);plot(x,y,'x','y','f(x)=1/(1+x^2)')



Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz



The following commands calculate the improper integral for values of n=2,4,,30:

-->getf('laguerrequad')

-->Res=[]; for j=2:2:30, Res = [Res; [j laguerrequad(f,j)]]; end;

-->disp(Res)

! 2. 1.493257 !
! 4. 1.5011895 !
! 6. 1.5188948 !
! 8. 1.53376 !
! 10. 1.5425763 !
! 12. 1.5478021 !
! 14. 1.5512373 !
! 16. 1.5537377 !
! 18. 1.555679 !
! 20. 1.5572394 !
! 22. 1.558519 !
! 24. 1.5595837 !
! 26. 1.5604823 !
! 28. 1.5612765 !
! 30. 1.567911 !


The exact value of the integral is /2:

-->%pi/2
ans =

1.5707963

Gauss-Hermite formulae for improper integrals

Gauss-Hermite formulae can be used to numerically evaluate integrals of the form shown below
through a procedure similar to Gaussian or Gauss-Laguerre quadrature:

!
"

. ) ( ) (
1
2
n
i
i i
x
x g A dx x g e

The values of x
i
, i=1,2,,n, are the roots of the Hermite polynomial on order n, H
n
(x), and the
coefficients A
i
, i=1,2,,n, are calculated from


Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz

.
)] ( ' [
! 2
)] ( [
! 2
2
1
2
1
2
1
i n
n
i n
n
i
x H
n
x H n
n
A

+

= =


Hermite polynomials are defined as:



To calculate an improper integral of a general function, f(x), we will use the following formula:

!
"
!


= . ) ( )} ( { ) (
1
2 2 2
n
i
i
x
i
x x
x f e A dx x f e e dx x f
i


The indefinite integral shown can be evaluated numerically only if it is convergent.

SCILAB user-defined function for calculating Gauss-Hermite quadrature

Earlier in the chapter we defined function hermitepol to generate Hermite polynomials. The
following function, hermitequad, which incorporates function hermitepol, is used to calculate
the improper integral shown above.

function [I] = hermitequad(f,n)

//Calculates the improper integral of function
//f(x) in the range [-infinity, infinity] using
//n intervals and the Gauss-Hermite formulae
p = hermitepol(n,'x');
pnp1 = hermitepol(n-1,'x');
xr = roots(p);
A = [];
for j = 1:n
A = [A 2^(n-1)*gamma(n+1)*sqrt(%pi)/(n^2*horner(pnp1,xr(j))^2)];
end;
I = A*(exp(xr.^2).*f(xr));
//end function hermitequad
//---------------------------------------------
function [Hn] = hermitepol(n,var)
//determines the Hermite polynomial
//of order n in variable var
H = zeros(1,n+1);
H(1) = poly([1],var,'coeff');
H(2) = poly([0 2],var,'coeff');
for j = 2:n
H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1);
end;
Hn = H(n+1);
//end function hermitepol
Application of function hermitequad for calculating improper integrals

We use function hermitequad to calculate the integral of the function f(x) = exp(-|x|). A plot
of the function is shown next:

-->deff('[y]=f(x)','y=exp(-abs(x))')
,... 2 , 1 ), ( ) 1 ( ) ( , 1
2 2
0
= = =

n e
dx
d
e x H H
x
n
n
x n
n

Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz

-->x=-4:0.1:4;y=f(x);plot(x,y,'x','y','f(x)=exp(-x^2)')

Values of the improper integral for n = 2, 4, , 30, are shown next:

-->getf('hermitequad')
-->Res=[]; for j = 2:2:30, Res=[Res; [j, hermitequad(f,j)]]; end;
-->Res
Res =

! 2. 1.4408859 !
! 4. 1.730544 !
! 6. 1.8420198 !
! 8. 1.8963115 !
! 10. 1.9264848 !
! 12. 1.9447991 !
! 14. 1.9566605 !
! 16. 1.9647431 !
! 18. 1.9704841 !
! 20. 1.9747063 !
! 22. 1.9779055 !
! 24. 1.9803932 !
! 26. 1.9823717 !
! 28. 1.9839769 !
! 30. 1.9853024 !

Integrals involving sine and cosine terms - Filons formulae

Filons formulae is used to evaluate integrals involving a single sine or cosine term. The
general formulae are

, ] )} sin( ) ( ) sin( ) ( { [ ) cos( ) (
0
0 0
!
+ + =
n
x
x
o e n n
kx x f kx x f x dx kx x f

, ] )} cos( ) ( ) cos( ) ( { [ ) sin( ) (
0
0 0
!
+ + =
n
x
x
o e n n
kx x f kx x f x dx kx x f

with
x = (x
n
-x
0
)/n, = kx,
= (
2
+ sin(2 )/2 - 2 sin
2
())/
3
,
= 2{ (1+cos
2
() - sin(2 )}/
3
,

Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz

= 4(sin()- cos())/
3
,
"

=
+ + =
2
,... 6 , 4 , 2
0 0
), cos( ) ( )} cos( ) ( ) cos( ) ( {
2
1
n
i
i i n n e
kx x f kx x f kx x f
"

=
=
1
,... 5 , 3 , 1
), cos( ) (
n
i
i i o
kx x f
"

=
+ + =
2
,... 6 , 4 , 2
0 0
), sin( ) ( )} sin( ) ( ) sin( ) ( {
2
1
n
i
i i n n e
kx x f kx x f kx x f
and
"

=
=
1
,... 5 , 3 , 1
). sin( ) (
n
i
i i o
kx x f

The values of x
i
, i=0,1,2,,n, are calculated using x
i
= x
0
+ix. Values x
0
and x
n
are the limits
of integration, and n is required to be an even number.



SCILAB user-defined function for calculating integrals with Filons formulae

The following function, filonquad, can be used to calculate the integrals involving sine or
cosine function as described above. The first argument in the function call indicates which
type of trigonometric function, sine (sin) or cosine (cos), is to be used in the integral. The
other arguments are the lower and upper limits of integration, x0 and xn, the name of the
function, f, and the number of (even) sub-intervals, n:

function [I] = filonquad(ftype,k,x0,xn,n,f)

//Calculates the integral of the function f(x)*cos(k*x), if ftype = 'cos' or of
//f(x)*sin(k*x), if ftype = 'sin' in the interval [x0,xn] using Filon's
//integration formulae
if modulo(n,2) <> 0 | n<=2 then
error('filonquad - n must be an even number larger than 2');
abort;
end;
Dx = (xn-x0)/n;
x = [x0+Dx:Dx:xn];
xe = [x(2):2*Dx:x(n-2)];
xo = [x(1):2*Dx:x(n-1)];
theta = k*Dx;
alpha = (theta^2+theta*sin(2*theta)/2-2*sin(theta)^2)/theta^3;
beta = 2*(theta*(1+cos(theta)^2)-sin(2*theta))/theta^3;
gama = 4*(sin(theta)-theta*cos(theta))/theta^3;
fxe=[];coskxe=[];sinkxe=[];
for j = 1:length(xe)
fxe = [fxe f(xe(j))];
coskxe = [coskxe cos(k*xe(j))];
sinkxe = [sinkxe sin(k*xe(j))];
end;
fxo=[];coskxo=[];sinkxo=[];
for j = 1:length(xo)
fxo = [fxo f(xo(j))];
coskxo = [coskxo cos(k*xo(j))];
sinkxo = [sinkxo sin(k*xo(j))];

Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz

end;
if ftype == 'cos' then
kappa_e = 0.5*(f(x0)*cos(k*x0)+f(xn)*cos(k*xn)) + fxe*coskxe';
kappa_o = fxo*coskxo';
I = Dx*(alpha*(f(xn)*sin(k*xn)-f(x0)*sin(k*x0))+beta*kappa_e+gama*kappa_o);
else
sigma_e = 0.5*(f(x0)*sin(k*x0)+f(xn)*sin(k*xn)) + fxe*sinkxe';
sigma_o = fxo*sinkxo';
I = Dx*(alpha*(f(x0)*cos(k*x0)-f(xn)*cos(k*xn))+beta*sigma_e+gama*sigma_o);
end;

//end of function filonquad

Application of function filonquad

The following SCILAB commands are used to generate tables of the integrals calculated through
Filons integral formulae with function filonquad. The function f(x) in the integrals is f(x) =
exp(-x), and it is integrated between x0 = 2 and xn = 4.5. The parameter k in sin(kx) or
cos(kx) takes values k = 1,2,3,4,5, one value of k for each column of the tables, while the
number of sub-intervals takes values n = 4,6,,20, corresponding to each row in the tables.
The first table corresponds to integrals involving sine functions while the second table
corresponds to integrals containing cosine functions.

-->deff('[y]=f(x)','y=exp(-x)') //define function

-->x0 = 2; xn = 4.5; //define limits of integration
-->kk = [1:1:5]; //define values of k = 1,2,3,4,5
-->nn = [4:2:20] //define values of n = 4,6,,20
-->mk = length(kk); mn = length(nn);
-->Res=zeros(mn,mk); //Table for function with sin(k*x)

-->for i = 1:mn
--> for j = 1:mk
--> Res(i,j) = filonquad('sin',kk(j),x0,xn,nn(i),f);
--> end;
-->end;

-->Res
Res =

! .0403248 - .0530721 .0322401 .0026027 - .0234170 !
! .0400408 - .0527902 .0322865 .0021268 - .0226910 !
! .0399930 - .0527517 .0323124 .0020419 - .0226145 !
! .0399799 - .0527421 .0323205 .0020210 - .0226019 !
! .0399752 - .0527387 .0323235 .0020140 - .0225983 !
! .0399732 - .0527374 .0323248 .0020110 - .0225970 !
! .0399723 - .0527367 .0323255 .0020096 - .0225964 !
! .0399718 - .0527364 .0323258 .0020089 - .0225961 !
! .0399715 - .0527362 .0323260 .0020085 - .0225959 !

-->Res=zeros(mn,mk); //Table for function with sin(k*x)

-->for i = 1:mn
--> for j = 1:mk
--> Res(i,j) = filonquad('cos',kk(j),x0,xn,nn(i),f);
--> end;
-->end;

-->Res
Res =

! - .0939334 .0268378 .0269223 - .0357034 .0095905 !

Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz

! - .0939411 .0270685 .0264442 - .0350895 .0090338 !
! - .0939457 .0271117 .0263818 - .0350622 .0090964 !
! - .0939472 .0271238 .0263666 - .0350587 .0091129 !
! - .0939479 .0271282 .0263614 - .0350579 .0091186 !
! - .0939482 .0271300 .0263592 - .0350577 .0091209 !
! - .0939483 .0271310 .0263582 - .0350576 .0091220 !
! - .0939484 .0271314 .0263577 - .0350576 .0091226 !
! - .0939484 .0271317 .0263574 - .0350575 .0091229 !

O Or rt th ho og go on na al l f fu un nc ct ti io on ns s a an nd d s se er ri ie es s e ex xp pa an ns si io on ns s

The concept of orthogonal functions apply to the case of functions of an independent variable x
often defined in terms of an index n over a certain interval, for example, g
n
(x) = sin(nx) on
the interval (0,1), with n = 1, 2, 3, The functions g
n
(x) and g
m
(x) are said to be orthogonal
on an interval a < x < b, if


for n m.

If n = m, then the resulting integral represents the square of a quantity, ||g
n
||, known as the
norm of g
n
(x):

Example 1 -- For the functions g
n
(x) = sin(nx) on the interval (0,1), you can check that the
integral

evaluates to

This latter expression is equal to zero for n m (n and m integers), and it is not defined if m =
n. For the case m = n, we can calculate the value of the integral by using:

!
1
0
2
) ( sin dx x n

which evaluates to



If n is an integer, this result evaluates to .
, 0 ) ( ) ( ) , ( = =
!
b
a
m n m n
dx x g x g g g
. 0 ] ) ( [ ) , ( || ||
2 2
= =
!
dx x g g g g
b
a
n n n n
d
0
1
2
0
1
( ) sin n x ( ) sin m x x

+ n ( ) cos n ( ) sin m m ( ) sin n ( ) cos m


( ) + n m ( ) + n m
1
2
+ ( ) cos n ( ) sin n n
n

Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz


To verify the previous integrals for a few combinations of integer values of n and m we can use
the following SCILAB commands. First, we define a function f1(x) corresponding to the
integrand sin(nx)sin(mx):

-->deff('[ff]=f1(x)','ff=sin(n.*%pi.*x).*sin(m.*%pi.*x)')

Next, we calculate the integrals of f1(x) for combinations of values of n and m, verifying that
the integrals are zero (i.e., nearly zero, for numerical integrals) for n m, and equal to for n
= m:

-->m=2;n=5;intg(0,1,f1,0.01)
ans = 8.951E-17

-->m=3;n=2;intg(0,1,f1,0.01)
ans = 4.339E-18

-->m=3;n=3;intg(0,1,f1,0.01)
ans = .5

-->m=4;n=4;intg(0,1,f1,0.01)
ans = .5

Series expansions with orthogonal functions

If a function f(x) can be written as a series expansion of orthogonal functions g
n
(x), i.e.,

over the interval a < x < b, then we can use the property of orthogonality to obtain the
coefficients C
n
of the series as follows:




Multiply the function by g
m
(x), i.e.,


Integrate the resulting expression over the interval a < x < b, i.e.,



Because all integrals in the summation in the right-hand side of the expression above are
zero, except for m = n, the expression simplifies to:

"

=
=
1
), ( ) (
n
n n
x g C x f
"

=
=
1
). ( ) ( ) ( ) (
n
m n n m
x g x g C x g x f
"
! !

=
=
1
. ) ( ) ( ) ( ) (
n
b
a
m n n
b
a
m
dx x g x g C dx x g x f
! !
=
b
a
m m
b
a
m
dx x g C dx x g x f
2
)] ( [ ) ( ) (

Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz



The coefficient C
n
is calculated from the expression above by replacing m with n, i.e.,



Example 2 -- For the orthogonal functions g
n
(x) = sin(nx), 0<x<1, the corresponding series is a
Fourier sine series, i.e.,
and,



Suppose that we want to find a few coefficients for the series expansion of the function f(x) =
x
2
for the orthogonal functions g
n
(x) = sin(nx), 0<x<1, we can use the following SCILAB
commands. First, we define the function f(x):

-->deff('[y]=f(x)','y=x^2')

Next, we define a function representing the integrand in the coefficient:

-->deff('[gg]=g(x)','gg=f(x)*sin(n*%pi*x)')

Finally, we define a function C(n) representing the coefficients C
n
:

-->deff('[CC]=C(n)','CC=2*intg(0,1,g,0.01)')

The following vector represents the first five coefficients of the series:

-->[C(1) C(2) C(3) C(4) C(5)]
ans =

! .3786075 - .3183099 .2026506 - .1591549 .1252599 !

Orthonormal functions

A set of normal functions
n
(x) on an interval a < x < b is said to be orthonormal if

Thus, the norm of an orthonormal function is ||
n
|| = 1.0.

=


=
!
!
|| ||
) , (
)] ( [
) ( ) (
2
n
n
b
a
n
b
a
n
n
g
g f
dx x g
dx x g x f
C
"

=
=
1
), sin( ) (
n
n
x n C x f
!
!
!
!
=

=


=
1
0
1
0
1
0
2
1
0
. ) sin( ) ( 2
2 / 1
) sin( ) (
) ( sin
) sin( ) (
dx x n x f
dx x n x f
dx x n
dx x n x f
C
n

!
3
4
5
=

= =
b
a
m n m n
n m when
n m when
dx x g x g
, 1
, 0
) ( ) ( ) , (

Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz

Given a set of orthogonal functions g
n
(x) on an interval a<x<b, you can generate the
corresponding orthonormal set as

n
(x) = g
n
(x)/||g
n
||.

Example 1 -- Determine the orthonormal functions corresponding to the set of orthogonal
functions
g
n
(x) = cos(nx),
on the interval 0 < x < 1.

First, to verify that the set of functions given is orthogonal, check that the integral


evaluates to zero for m n. Therefore, the functions g
n
(x) are orthogonal.

The norm of the function g
n
(x) is calculated by using




You can check that the latter integral evaluates to . Thus, ||g
n
||
2
= , and ||gn|| = 1/2.
The orthonormal functions will be defined, therefore, as

n
(x) = g
n
(x)/||g
n
|| = 2cos(nx).


Orthogonality with respect to a weight function

Some functions are orthogonal with respect to a weight function p(x), so that


In this case, the norm squared of g
n
(x) is given by


If a function f(x) can be expanded in terms of the functions g
n
(x), i.e.,


. 0 ) ( ) ( ) ( =
!
b
a
m n
dx x g x g x p
. 0 ] ) ( [ ) ( || ||
2 2
=
!
dx x g x p g
b
a
n n
"

=
=
1
), ( ) (
n
n n
x g C x f
d
0
1
2
2
0
1
( ) cos n x
2
x
d
0
1
2
0
1
( ) cos n x ( ) cos m x x

Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz


the coefficients of the series are given, in this case, by




Example 1 -- The Chebyshev polynomials T
1
(x) = x, T
2
(x) = 2x
2
-1, on the interval 1 < x < 1, are
orthogonal with respect to the weight function p(x) = (1-x
2
)
-1/2
, which can be demonstrated by
calculating the integral



You can check that the integral evaluates to zero by using SCILAB as follows. First, we define a
function ff(x) representing the integrand in the previous integral.


-->deff('[y] = f(x)','y = x.*(2.*x.^2-1)./sqrt(1-x.^2)')

Next, we generate points in the interval (-1,1), not including the extremes of the interval, i.e.,
x = -1 and x = 1, where the integrand goes to infinity.

-->xx=[-0.99:0.01:0.99]; yy = f(xx);

A plot of the integrand is produced by:

-->plot(xx,yy);xtitle('integrand for Chebyshev','x','integrand')



=


=
!
!
!
b
a
n
n
b
a
n
b
a
n
n
dx x g x f x p
g
dx x g x p
dx x g x f x p
C ) ( ) ( ) (
|| ||
1
)] ( [ ) (
) ( ) ( ) (
2
d
0
1
2
2
2
2
2
2
-1
1
x ( ) 2 x
2
1
1 x
2
x

Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz

With the vectors xx and yy generated before, we can calculate the integral for the Chebyshevs
polynomials shown earlier by using function inttrap. The result, as expected, is zero:

-->inttrap(xx,yy)
ans =m- 9.021E-17

The Sturm-Liouville problem

Although the details of the proof are not included in this book, you can verify that Bessel
functions, Laguerre polynomials, Hermite polynomials, and Chebyshev (or Tchebycheff)
polynomials are all sets of orthogonal functions. In fact, all of these functions can be obtained
from solving the so-called Sturm-Liouville problem defined by the differential equation



to be solved in the interval a < x < b, and subject to the boundary conditions

1
y(a) +
1
y(a) = 0,
and

2
y(b) +
2
y(b) = 0.

By selecting the values of the functions r(x), p(x), and q(x), as well as the constants
1
,
2
,
1
,
and
2
, we can generate, out of the general ODE shown above, the Bessel equation, Laguerres
equation, etc. The solutions to the Sturm-Liouville ODE are given in terms of different values
of the parameter , known as the eigenvalues of the problem, while the solutions themselves
are known as the eigenfunctions.

F Fo ou ur ri ie er r A An na al ly ys si is s

Fourier analysis involves the expansion of periodic functions into infinite series of sine and
cosines. In general, any continuous function can be expanded into a series of sine functions
(Fourier sine series), cosine functions (Fourier cosine functions), or a series combining sine and
cosine functions (Fourier series). Expansion of functions into Fourier series finds applications
in areas related to any type of wave propagation, be it electromagnetic waves or surface waves
in waters.

In practice, we evaluate Fourier series using only a finite number of sine and/or cosine
functions representing a discrete number of frequencies. Fourier analysis can be extended to a
continuous frequency domain through the use of Fourier transforms. Fourier transforms have
applications in the analysis of periodic and non-periodic signals such as communication signals,
turbulence velocity variations, etc. Most measured signals include a finite number of
(typically) equally spaced data points suitable for analysis through discrete Fourier transforms.

Sine and cosine functions belong to a family of so-called orthogonal functions. Properties of
such functions are presented in the following section.


Fourier series
Fourier series are series involving sine and cosine functions that are typically used to expand
periodic functions. A function f(x) is said to be periodic of period L if f(x+L) = f(x). For
, 0 )] ( ) ( [ ) ( = + +
#
$
%
&
'
(
y x p x q
dx
dy
x r
dx
d


Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz

example, because sin(x+2

) = sin x, and cos(x+2

) = cos x, the functions sin and cos are 2

-
periodic functions. If two functions f(x) and g(x) are periodic of period L, then their linear
combination h(x) = af(x) + bg(x), where a and b are constant, is also periodic of period L.
The figure below shows a periodic function of period L = 2:

-->deff('[y]=f(x)','y=2.*sin(2.*%pi.*x)-3.*cos(%pi.*x)')

-->x = [0:%pi/100;2*%pi]; y = f(x);

-->plot(x,y,'x','y','Example of a periodic function')




To verify that the function is indeed periodic, we try the following evaluations:

-->f(0),f(2),f(4)
ans = - 3.
ans = - 3.
ans = - 3.

Fourier sine series

Fourier sine series are such that,

= ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
n x
L


Because the sine functions
.
-
,
,
+
*
)
)
sin
i x
L
and
.
-
,
,
+
*
)
)
sin
j x
L
are orthogonal, i.e.,

= d
0
1
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
i x
L
.
-
,
,
+
*
)
)
sin
j x
L
x 0 , for
i j
,
then

= d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x b
n
d
0
1
2
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
n x
L
2
x =
L b
n
2
,


Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz

from which it follows that

b
n
=
2
L
d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x

for n = 1, 2, 3,....

Consider the function
= ( ) f x x x
2
. This function is obviously not periodic, as shown in the
figure below.

-->deff('[y]=f(x)','y=x-x^2')

-->x=[-2:0.1:3];y=f(x);

-->plot(x,y,'x','y','y=x-x^2')




However, we can assume that it is periodic with period, say, L = 2, for the purpose of
expanding the function in a Fourier sine series. The next SCILAB commands are used to
calculate a Fourier sine series for the function. Notice that we define two functions, first a
function g(x) which represents the integrand f(x)sin(nx/L), and then a function b(n) that
calculates the coefficient b
n
for the Fourier sine series that approximates f(x).

-->deff('[w]=g(x)','w=f(x)*sin(n*%pi*x/L)')

-->deff('[bb]=b(n)','bb=(2/L)*int(0,L,g)')

We use the following forend loop to generate the first 20 coefficients for the Fourier
expansion (i.e., n = 0, 1, 2, , 19).

-->c = []; for j=1:20, c = [c b(j-1)]; end;

-->c
c =
column 1 to 6

! 0. - .2411904 .6366198 - .3861891 .3183099 - .2463915 !

column 7 to 11

! .2122066 - .1788825 .1591549 - .1400554 .1273240 !

Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz


column 12 to 16

! - .1149737 .1061033 - .0974717 .0909457 - .0845768 !

column 17 to 20

! .0795775 - .0746864 .0707355 - .0668621 !

To calculate the values for the series expansion we will use the following function fouriersine.

function [y] = fouriersine(c,x,L)

//Calculates the Fourier sine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);

y = [];

for i = 1:n
yy = 0;
for j = 1:m
yy = yy + c(j)*sin((j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriersine

A call to fouriersine for L = 2 is used to produce the following plot.

-->getf('fouriersine')
-->x=[-2:0.01:3];y=f(x);yf=fouriersine(c,x,L);
-->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -6 3 6])
-->xtitle('Fourier sine series for y = x-x^2','x','y')



The plot shows the original function (continuous line) and the approximation through 20 terms
of the Fourier sine series (crosses). The approximation is somewhat acceptable in the range 0
< x < L, which corresponds to the artificial period L = 2 that we imposed on the Fourier series.
Still, the sinusoidal character of the fitting is impossible to eliminate, at least with the 20
components shown. Notice that the Fourier sine series fitting makes the resulting function

Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz

periodic of period L = 2, as expected. Also, notice that at x = 2, the fitted function shows
some wild oscillations. This latter effect is known as the Gibbs phenomenon and it is
characteristic of Fourier series. We will see more examples of this phenomenon in other
applications in this chapter.

The following graph represents the Fourier series for the function y = x-x
2
using 40 components
in the series. Obviously, the fitting is much better than when using 20 components only. The
Gibbs phenomenon is still present at x = 2, but up to that value the Fourier sine series fitting is
excellent for this case.



The following SCILAB commands are used to produce graphs of the original function as well as
the Fourier sine series fitting corresponding to 5, 10, 20, 30, and 40 components.

-->x = [-2:0.01:3]; y = f(x);
-->c5=c(1:5); yf5 = fouriersine(c5,x,L);
-->c10=c(1:10); yf10 = fouriersine(c10,x,L);
-->c20=c(1:20); yf20 = fouriersine(c20,x,L);
-->c30=c(1:30); yf30 = fouriersine(c30,x,L);
-->c40=c(1:40); yf40 = fouriersine(c40,x,L);
-->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],[1:1:6],'011','
',[-1.5 -2.5 2.5 2.5])
-->xtitle('Fourier sine series using 5,10,20,30,40 components','x','y')


Fourier cosine series

Fourier cosine series are such that,


Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz

= ( ) f x
"
= n 0

a
n
.
-
,
,
+
*
)
)
cos
n x
L
.


Because the cosine function
.
-
,
,
+
*
)
)
cos
i x
L
and
.
-
,
,
+
*
)
)
cos
j x
L
are also orthogonal, i.e.,

= d
0
1
2
2
2
2
0
L
.
-
,
,
+
*
)
)
cos
i x
L
.
-
,
,
+
*
)
)
cos
j x
L
x 0 , for
i j
,
then
= d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x a
n
d
0
1
2
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
n x
L
2
x =
L a
n
2
,
from which it follows that

a
n
=
2
L
d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x

for n = 1, 2, 3, ... For n = 0, the corresponding coefficient is:

a
0
=
1
L
d
0
1
2
0
L
( ) f x x
.

As an example, to determine the coefficient of the cosine series that fits the function f(x) = (1-
x)(x+1)(x+2) assuming that it is periodic with period L = 1, use the following SCILAB commands:


-->deff('[y]=f(x)','y=(1-x).*(x+1).*(x-2)')

-->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)')

-->deff('[aa]=a(n)',...
['if n==0 then';'aa=intg(0,L,f)/L';'else';'aa=(2/L)*intg(0,L,g)/L';'end'])

-->c=[];for j=1:40, c=[c a(j-1)]; end;

-->c
c =

column 1 to 5

! - 1.0833333 - .8543107 .0506606 - .0705892 .0126651 !

column 6 to 10

! - .0247113 .0056290 - .0125093 .0031663 - .0075428 !


column 11 to 15

! .0020264 - .0050410 .0014072 - .0036058 .0010339 !

column 16 to 20


Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz

! - .0027068 .0007916 - .0021065 .0006254 - .0016859 !

column 21 to 25

! .0005066 - .0013798 .0004187 - .0011501 .0003518 !

column 26 to 30

! - .0009733 .0002998 - .0008344 .0002585 - .0007232 !

column 31 to 35

! .0002252 - .0006329 .0001979 - .0005585 .0001753 !

column 36 to 40

! - .0004964 .0001564 - .0004442 .0001403 - .0003998 !


Function fouriercosine, listed below, is used to calculate the Fourier series given the
coefficients c, a vector of values of x, and the period of the function L.

function [y] = fouriercosine(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);

y = [];
for i = 1:n
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos((j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriercosine

A plot of the original function and the Fourier cosine series for 5, 10, 20, 30, and 40
components are shown in the following plot. The graph shows the original function and the
different Fourier series to be practically indistinguishable from each other in the range 0 < x <
1.

-->x = [0:0.01:1]; y = f(x);

-->c5=c(1:5);yf5=fouriercosine(c5,x,L);
-->c10=c(1:10);yf10=fouriercosine(c10,x,L);
-->c20=c(1:20);yf20=fouriercosine(c20,x,L);
-->c30=c(1:30);yf30=fouriercosine(c30,x,L);
-->c40=c(1:40);yf40=fouriercosine(c40,x,L);

-->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],...
-->[1,2,3,4,5,6],'011',' ',rect)
-->xtitle('Fourier cosine series - 5,10,20,30,40 components','x','y')


Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz



The following figure shows the original function (continuous line) and the Fourier cosine series
(crosses) with 40 components in the range -0.5 < x < 1.5. It is obvious that the Fourier cosine
series fits the original function very well in the range 0 < x < 1. Outside of that range, the
function shows periodicity.

-->x = -0.5:0.1:1.5; y = f(x);

-->c40=c(1:40);yf40=fouriercosine(c40,x,L);

-->plot2d([x' x'],[y' yf40'],[1,-1],'011',' ',[-0.5 -2.5 1.5 0.5])

-->xtitle('Fourier cosine series - 40 components','x','y')



_________________________________________________________________________________
NOTE: If you have a function that is defined in the interval (-L/2, L/2), where L is the period of
the function, the formulas to use for the coefficients of the cosine and sine functions are:

a
0
=
1
L
d
0
1
2

L
2
L
2
( ) f x x , a
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,
and

Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz

b
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x

for n = 1, 2, 3, ... The formulas for the series themselves are:

= ( ) f x + a
0
.
-
,
,
,
+
*
)
)
)
"
= n 1

a
n
.
-
,
,
+
*
)
)
cos
2 n x
L
and = ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
2 n x
L
.


____________________________________________________________________________

For example, a sine series fitting of the function f(x) = x
3
in the interval [-1,1] (i.e., L=2), is
shown below. The following SCILAB commands are used to define the function y = x
3
, and the
functions necessary to calculate the coefficients of the corresponding Fourier sine series
expansion:

-->deff('[y]=f(x)','y=x^3')

-->deff('[y]=g(x)','y=f(x)*sin(2*n*%pi*x/L)')

-->deff('[bb]=b(n)','bb=(2/L)*intg(-L/2,L/2,g)')


Next, we calculate 20 coefficients of the Fourier sine series:

-->L = 2; c=[];for j=1:20, c=[c b(j)]; end;

The function fouriersine2 is a modified version of fouriersine to account for the case in which
the function is fitted in the interval [-L/2,L/2]:

function [y] = fouriersine2(c,x,L)

//Calculates the Fourier sine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);
y = [];
for i = 1:n
yy = 0;
for j = 1:m
yy = yy + c(j)*sin(2*j*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriersine2

The next commands produce a plot of the original function, y = x
3
, and of the Fourier sine
series:

-->x=[-L:0.01:L]; y = f(x); yf = fouriersine2(c,x,L);


Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz

-->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -5 2 5])

-->xtitle('Fourier sine series y = x^3, 20 components','x','y')




The figure shows that the fitting is excellent except near the extremes of the interval, i.e.,
near x = -L/2 and near x = L/2.

The next example shows a cosine series fitting in the interval [-1,1], L = 2, for the function f(x)
=
1 x
2
. First, we define functions f(x) and g(x) and the generic coefficient of the Fourier
cosine series a
n
= a(n). Next, we calculate the first 40 coefficients in the series.

-->deff('[y]=f(x)','y=1-x^2')

-->deff('[y]=g(x)','y=f(x)*cos(2*n*%pi*x/L)')

-->deff('[aa]=a(n)'...
-->,['if n==0 then';'aa=intg(-L/2,L/2,f)/L';'else';...
-->'aa=(2/L)*intg(-L/2,L/2,g)';'end'])

-->L=2; c=[]; for j = 1:40, c = [c a(j-1)]; end;


Function fouriercosine2 is used to calculate the Fourier cosine series:

function [y] = fouriercosine2(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.



m = length(c); n = length(x);
y = [];
for i = 1:n
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz

//end of function fouriercosine2

The following plot shows the original function and the corresponding Fourier cosine series:

-->c5 = c(1:5) ; y5 = fouriercosine2(c5 ,x,L);

-->c10 = c(1:10); y10 = fouriercosine2(c10,x,L);

-->c20 = c(1:20); y20 = fouriercosine2(c20,x,L);

-->c30 = c(1:30); y30 = fouriercosine2(c30,x,L);

-->c40 = c(1:40); y40 = fouriercosine2(c40,x,L);

-->plot2d([x',x',x',x',x',x'],[y',y5',y10',y20',y30',y40'],...
-->[1:1:6],'011',' ',[-1.5 -0.2 1.5 1.1])

-->xtitle('Fourier cosine series y = 1-x^2','x','y')



As in the case of the previous example for the sine series, the fitting of this cosine series is
excellent throughout the whole interval [-L/2,L/2] except very close to the extremes of the
intervals. The Gibbs phenomenon is not evident in this case.

______________________________________________________________________________
The last two examples used a series expansion in the interval [-L/2,L/2]. Alternatively, you
could use as interval of integration [-L,L], i.e., an interval encompassing two periods of the
function, to obtain
= a
0
1
L

d
0
1
2
L
L
( ) f x x
,
= a
n
1
L
d
0
1
2
2
2
2
L
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x , and
= b
n
1
L
d
0
1
2
2
2
2
L
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x ,
for n = 1,2,3, ...
The sine and cosine series will be calculated as:
= ( ) f x +
a
0
2
.
-
,
,
,
+
*
)
)
)
"
= n 1

a
n
.
-
,
,
+
*
)
)
cos
n x
L
and = ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
n x
L
.
_______________________________________________________________________________

For example, the function f(x) = tan(x) can be expanded as sine series using the period L = 1 as
follows. First, we define the functions that are used to produce the coefficients of the Fourier
sine series, followed by the calculation of the first 20 coefficients (including a
0
):

-->deff('[y]=f(x)','y=tan(x)')

Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz

-->deff('[y]=g(x)','y=f(x)*sin(n*%pi*x/L)')
-->deff('[bb]=b(n)','bb=(1/L)*intg(-L,L,g)')
-->L=1; c=[]; for j = 1:20, c = [c b(j-1)]; end;

Next, we produce a plot of the original function and the Fourier sine series for 5, 10, and 20
components:

-->x=[-2.5:0.01:2.5];y=f(x);
-->c5 = c(1:5); y5 = fouriersine(c5 ,x,L);
-->c10= c(1:10);y10 = fouriersine(c10,x,L);
-->c20= c(1:20);y20 = fouriersine(c20,x,L);
-->plot2d([x' x' x' x'],[y' y5' y10' y20'],[-1,1,2,3],'011',' ',[-1.5 -2 1.5 2])

-->xtitle('Fourier sine series for y = tan(x), -L<x<L, L = 2','x','y')




In the following example, we use a cosine series to fit the function f(x) = (1-x
2
)
1/2
in the
interval [-1,1], i.e., L = 1.

-->deff('[y]=f(x)','y=sqrt(1-x^2)')

-->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)')

-->deff('[aa]=a(n)',...
-->['if n==0 then';'aa=intg(-L,L,f)/(2*L)';'else';...
--> 'aa=intg(-L,L,g)/L';'end'])

-->L=1; c=[]; for j=1:20, c=[c a(j-1)]; end;

-->x=[-1:0.01:1];y=f(x);yf=fouriercosine(c,x,L);

-->plot2d([x',x'],[y',yf'],[1,-1])

-->xtitle('Fourier cosine for y = sqrt(1-x^2), 20 components','x','y')


Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz




Fourier Series

A Fourier series is simply a series expansion of a function that includes both sine and cosine
series. The general form of the Fourier series expansion in the interval [-L/2,L/2], i.e.,
assuming a period L, is:

= ( ) f x
a
0
2
+
"
= n 1

.
-
,
,
+
*
)
)
+ a
n
.
-
,
,
+
*
)
)
cos
2 n x
L
b
n
.
-
,
,
+
*
)
)
sin
2 n x
L



with the coefficients calculated as:


a
0
=
2
L
d
0
1
2

L
2
L
2
( ) f x x , a
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,

and
b
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x


If the function is periodic of period L, the coefficients a
n
and b
n
are calculated as



a
0
=
2
L
d
0
1
2
c
0
+ c
0
L
( ) f x x
, a
n
=
2
L
d
0
1
2
2
2
2
c
0
+ c
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,

and


Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz

b
n
=
2
L
d
0
1
2
2
2
2
c
0
+ c
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x
.


Thus, if c
0
= -L/2, we recover the first set of equations for a
n
and b
n
.


A user-defined SCILAB function to calculate Fourier series

The following function, fourierseries, returns the coefficients of the Fourier series and the
evaluation of the same on a range of values of x for different number of components. The
general call to the function is

[a0 a b y] = fourierseries(nn, c0, L, x, f, tol)

where nn is a vector containing values of the components to be used, e.g., nn = [5, 10, 20], L is
the period of the function, x is a vector indicating the values of x where the Fourier series will
be evaluated, and tol is a tolerance for convergence used in function intg (see --> help intg).
The integrals defining a
n
and b
n
are calculated in the interval [c
0
,c
0
+L]. The function returns
values a0, a, b, and y, where a0 is the coefficient a
0
in the series, a and b are vectors
containing the coefficients of the cosine and sine series, respectively, i.e., a
n
and b
n
for
n=1,2,, and y is a matrix with the same number of columns as in vector x and the number of
rows equal to the number of elements in vector nn. Thus, the i-th row of matrix y contains the
values of the Fourier series with the number of components indicated by the i-th component of
nn.

A listing of function fourierseries follows. The function fourierseries includes a sub-function,
fseries, which is called recurrently to produce the rows of matrix y.



function [a0,a,b,y] = fourierseries(n,c0,L,x,f,tol)

//Calculates coefficients and values for the
//Fourier series corresponding to function f
//for n1, n2, ..., nm components, where n is
//the vector n = [n1,n2,...,nm] of length m.
//Integrals are calculated using the interval
//[c0,c0+L] where L is the period of the function.
//e.g., if c0 = -L/2, then the interval is [-L/2,L/2],
//x is a vector of length k for calculating
//values of the Fourier series.
//a0 = first coefficient for cosine term
//a = coefficients of the cosine terms [a1 a2 ...]
//b = coefficients of the sine terms [b1 b2 ...]
//y = matrix of m rows

deff('[gg1]=g1(xi)','gg1=f(xi)*cos(2*nn*%pi*xi/L)');
deff('[gg2]=g2(xi)','gg2=f(xi)*sin(2*nn*%pi*xi/L)');
deff('[aaa]=a1(nn)','aaa=(2/L)*intg(c0,c0+L,g1,tol)');
deff('[bbb]=b1(nn)','bbb=(2/L)*intg(c0,c0+L,g2,tol)');

a0 = (2/L)*intg(c0,c0+L,f,tol);

nmax = max(n);a = []; b = [];
for j = 1:nmax
a = [a a1(j)]; b = [b b1(j)];

Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz

end;

m = length(n); k = length(x);y = zeros(m,k);
for j =1:m
aj = a(1:n(j)); bj = b(1:n(j));
y(j,:) = fseries(a0,aj,bj,x,L);
end;

//end function fourierseries

function [yy] = fseries(a0,aa,bb,xx,L)

//Calculates fourier series given the
//coefficients of the cosine series
//a0 and vector a (of length nn), and
//the coefficients of the sine series,
//vector b (of length nn).
//L = period of function to be fit.

nn = length(aa); mm = length(xx);yy = zeros(1,mm);
for j = 1:mm
yy = a0/2;
for k = 1:nn
yy = yy + aa(k)*cos(2*k*%pi*x/L) + bb(k)*sin(2*k*%pi*x/L);
end;
end;

Applications of function fourierseries

Example 1 - The following example shows the fitting of the function
= ( ) f x + 1 x
2
in the
interval [-1,1] using function fourierseries with the option itype = right. The Fourier series
are calculated for 5, 10, 20, and 40 components. A plot of the original figure and of the
Fourier series with different number of components is also produced.

-->deff('[y]=f(x)','y=sqrt(1+x^2)')
-->getf('fourierseries')
-->L = 2; x=[-1:0.01:1]; y = f(x);
-->[a0,a,b,yy]=fourierseries('right',[5,10,20,40],-L/2,L,x,f,1e-5);
-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])
-->xtitle('Fourier series for y = sqrt(1+x^2) with 5,10,20,40 components',...
-->'x','y')





Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz

Example 2 - To fit the function f(x) = ln(x+2) in the interval [-1,1], i.e., L = 2, using Fourier
series, we try:

-->deff('[y]=f(x)','y=log(x+2)')

-->getf('fourierseries')

-->L= 2; x=-1:0.01:1; y = f(x);
-->[a0,a,b,yy]=fourierseries([5,10,20,40],-L/2,L,x,f,1e-5);

-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])
-->xtitle('Fourier series for y = ln(x+2) with 5,10,20,40 components','x','y')




The fitting of the Fourier series is excellent except near the extremes of the interval where
strong Gibbs phenomenon deviations are observed. Also, the fitting improves as the number of
components in the Fourier series increases.


Notes:

[1] Even if a function is not periodic, you can fit a certain section of the function (say, between
x = 0 and x = L, or between -L/2 and L/2) using Fourier series. Of course, the resulting
trigonometric series will be so that it represents a periodic function of period L.
[2] For any given function you can fit sine, cosine, or full Fourier series. It is recommended,
however, that you plot the original as well as the resulting expansion(s) to determine how well
the Fourier series expansion(s) works.

Example 3 - In the next example we produce the Fourier series for the function g(x) = exp(x),
x in [0,], i.e., with period L = .

-->deff('[y]=f(x)','y=exp(x)')

-->L=%pi; c0 = 0; x=-%pi:%pi/100:%pi; y = f(x);

-->getf('fourierseries')

-->[a0,a,b,yy]=fourierseries([5,10,20,40],c0,L,x,f,1e-5);

-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])

-->xtitle('Fourier series for y = exp(x) with 5,10,20,40 components','x','y')


Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz



The Fourier series for this function does an excellent job fitting the curve except near the
extremes of the interval where the Gibbs phenomenon is evident.

The following graphs shows the Fourier series developed above in the interval [-2,2 ] to
demonstrate the fact that the Fourier series produces a periodic function even though the
original function was not periodic. By calculating the Fourier series coefficients in the interval
[0, ] we generates a periodic function of period .




Fourier series for a piecewise periodic function

The following example shows the fitting of the Fourier series for a continuous piece-wise
function, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere. We assume that the function h(x)
represents a periodic function in [-1,1], with period L = 2. The solution is shown for 4, 6, 8,
and 10 components. For more than 10 components, function fourierseries presents problems
of convergence on function intg which is used to calculate the integrals in the series
coefficients.

-->deff('[y]=f(x)',['if x>-1/3 & x<1/3 then';'y=3';'else';'y=0';'end'])

-->L=2; c0=-L/2; x=[-1:0.1:1];

-->y=[]; for j =1:length(x), y=[y f(x(j))]; end;

-->getf('fourierseries')


Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz

-->[a0,a,b,yy]=fourierseries([4,6,8,10],c0,L,x,f,1e-5);
-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])
-->xtitle(Piecewise continuous function,'x','y')



The following graphs shows the original function (single square pulse) and the periodic function
generated by the corresponding Fourier series using the interval -3 < x < 3. This result
demonstrates how we can generate a square wave out of a single square pulse.



To be able to calculate coefficients for n>10, we can use the definitions for the coefficients a
n

and b
n
corresponding to the function h(x) defined earlier. These coefficients are:

,
3
sin
6
cos 3
2
2
cos ) (
2
3 / 1
3 / 1
2 /
2 /
! !

)
*
+
,
-
.
= )
*
+
,
-
.
= )
*
+
,
-
.
=

n
n
dx
L
X n
dx
L
x n
x h
L
a
L
L
n


and

. 0 sin 3
2
2
sin ) (
2
3 / 1
3 / 1
2 /
2 /
! !

= )
*
+
,
-
.
= )
*
+
,
-
.
= dx
L
X n
dx
L
x n
x h
L
b
L
L
n



Also,


! !

= = =
3 / 1
3 / 1
2 /
2 /
0
. 0 . 2 3
2
2
) (
2
dx dx x h
L
a
L
L


Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz



Using these definitions we can produce a vector of values for the coefficients a
n
as follows:


-->deff('[aa]=a(n)','aa=6*sin(n*%pi/3)/(n*%pi)')
-->c = [1]; for j=1:40, c=[c a(j)]; end;


Notice that the first term in the vector of coefficients, c, is equal to 1, i.e., a
0
/2. Next, we
generate data from the original function (defined earlier):


-->x=-1:0.01:1;y=f(x);


To produce the Fourier series we use function fouriercosine3, which is a modified version of
function fouriercosine, introduced earlier.

function [y] = fouriercosine3(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);y = [];
for i = 1:n
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

To calculate values for the Fourier series with 40 components of cosine we use:


-->getf('fouriercosine3')
-->yy = fouriercosine3(c,x,L);


A plot of the original function and the corresponding Fourier series with 40 components is
shown next:


-->plot2d([x' x'],[y' yy'])

-->xtitle('square wave with 40 components of Fourier series','x','y')



Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz


Calculating Fourier series coefficients with Filons sine-cosine
formulae

An alternative for calculating the coefficients of the Fourier series is to use function filonquad,
defined earlier for calculating integrals of the form

! !

m m
x
x
x
x
dx kx x f dx kx x f
0 0
) cos( ) ( , ) sin( ) (

For Fourier series coefficients the following values apply: k=2/L, x
0
= c
0
, x
n
=c
0
+L, and the
integrals are to be multiplied by (2/L). Care must exercised when using Filons integral
formulas for the function f(x) used must be continuous in the full interval [x
0
,x
n
]. Filons
integral formulae use a parameter m (referred to as n when the formulae where first
introduced earlier in the Chapter) which must be an even number larger than or equal to 4.
Thus, to calculate the coefficients of the cosine terms we will use

a
n
= (2/L)*filonquad(cos,2*n*%pi/L,x
0
,x
n
,m,f),

while those for the sine terms are calculated with

b
n
= (2/L)*filonquad(sin,2*n*%pi/L,x
0
,x
n
,m,f).

For example, to calculate the first five coefficients of the Fourier series corresponding to f(x) =
exp(-x), using Filons sine-cosine quadrature formulae for m = 4,6,8,,30, we prepare the
following table. Columns represents values of n = 1,2,3,4,5, and rows represent values of m =
4,6,8,,30:


-->deff('[y]=f(x)','y=exp(-x)');x0=-1;xn=1;L=2;

-->kk=[1:1:5];mk=length(kk);nn=[4:2:40];mn=length(nn);

-->ResCos = zeros(mn,mk);

-->for i = 1:mn
--> for j = 1:mk
--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResCos
ResCos =

Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz


! - .2201023 .0583263 - .0244558 .0145816 - .0088041 !
! - .2169092 .0591023 - .0262183 .0141662 - .0093004 !
! - .2164390 .0583263 - .0265777 .0148071 - .0092038 !
! - .2163174 .0581622 - .0262908 .0149935 - .0094942 !
! - .2162749 .0581098 - .0262183 .0148593 - .0096020 !
! - .2162569 .0580887 - .0261921 .0148217 - .0095293 !
! - .2162483 .0580789 - .0261806 .0148071 - .0095076 !
! - .2162437 .0580738 - .0261749 .0148002 - .0094986 !
! - .2162412 .0580709 - .0261717 .0147966 - .0094942 !
! - .1933848 .0463011 - .0304248 .0353657 - .0405569 !
! - .2162386 .0580681 - .0261687 .0147932 - .0094904 !
! - .2162380 .0580674 - .0261680 .0147924 - .0094895 !
! - .2162375 .0580669 - .0261675 .0147919 - .0094889 !
! - .2162372 .0580666 - .0261671 .0147915 - .0094885 !
! - .2162370 .0580664 - .0261669 .0147913 - .0094882 !
! - .2162368 .0580662 - .0261667 .0147911 - .0094881 !
! - .2162367 .0580661 - .0261665 .0147910 - .0094879 !
! - .2162366 .0580660 - .0261664 .0147909 - .0094878 !
! - .2162365 .0580659 - .0261664 .0147908 - .0094877 !

-->ResSin = zeros(mn,mk);

-->for i = 1:mn
--> for j = 1:mk
--> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResSin
ResSin =

! - .6795196 .3740782 - .2468433 .1870391 - .1490822 !
! - .6792471 .3650430 - .2493854 .1860151 - .1490260 !
! - .6792918 .3648433 - .2467114 .1870391 - .1491070 !
! - .6793105 .3648335 - .2466187 .1859172 - .1496313 !
! - .6793183 .3648340 - .2466100 .1858691 - .1490599 !
! - .6793218 .3648349 - .2466088 .1858634 - .1490322 !
! - .6793236 .3648356 - .2466087 .1858623 - .1490285 !
! - .6793245 .3648360 - .2466088 .1858620 - .1490276 !
! - .6793251 .3648362 - .2466089 .1858620 - .1490273 !
! - .6940133 .3906049 - .2762026 .2096025 - .1581488 !
! - .6793256 .3648365 - .2466090 .1858620 - .1490272 !
! - .6793258 .3648365 - .2466090 .1858620 - .1490272 !
! - .6793259 .3648366 - .2466090 .1858620 - .1490272 !
! - .6793260 .3648366 - .2466091 .1858620 - .1490272 !
! - .6793260 .3648366 - .2466091 .1858621 - .1490273 !
! - .6793260 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !


For the case of the piecewise continuous function that produces the single rectangular pulse,
namely, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere, we can use Filons sine-cosine
quadrature formulae to calculate the coefficients for the Fourier series by using x
0
= -1/3 and
x
n
= 1/3, since the function is zero in the remaining ranges of the interval (-1,1). The period is
still taken as L = 2. The following SCILAB commands will produce the Fourier series coefficients
for the cosine and sine components for n = 1,2,3,4,5, and m = 4,6,8,,30:

-->deff('[y]=f(x)','y=3');x0=-1/3;xn=1/3;L=2;


Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz

-->ResCos = zeros(mn,mk);
-->for i = 1:mn
--> for j = 1:mk
--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResCos
ResCos =

! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.5838617 .819752 .0616369 - .3132454 - .2446393 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.618222 .8366987 .0487592 - .3600631 - .3111114 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.630336 .8377577 .0368149 - .3797570 - .3271122 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6331939 .8374929 .0333666 - .3843859 - .3298161 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !


-->ResSin = zeros(mn,mk);
-->for i = 1:mn
--> for j = 1:mk
--> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResSin
ResSin =

! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! - 8.651E-17 - 9.985E-17 - 1.471E-16 - 2.321E-17 3.258E-17 !
! 2.935E-17 0. 0. - 6.795E-17 - 6.210E-17 !
! - 1.480E-16 - 8.417E-17 - 9.729E-17 5.351E-17 9.501E-17 !
! - .0650665 - .0966290 - .0772903 - .0151104 .0587416 !
! 1.355E-16 1.230E-16 8.613E-17 - 6.320E-17 - 8.416E-17 !
! 0. - 4.398E-17 - 3.772E-17 2.146E-17 0. !
! 0. 1.383E-16 5.777E-17 - 3.214E-17 - 4.877E-17 !
! - 2.871E-16 - 3.412E-16 - 1.219E-16 1.072E-16 1.111E-16 !
! - .0426227 - .0550420 - .0281511 .0194482 .0540867 !
! 2.354E-16 2.053E-16 6.136E-17 - 2.430E-16 - 7.683E-17 !
! - 1.550E-16 - 9.129E-17 2.827E-17 1.233E-16 1.891E-16 !
! - 2.369E-16 - 1.058E-16 9.830E-17 2.560E-16 2.373E-16 !
! - 1.850E-16 - 1.850E-16 - 6.451E-17 1.243E-16 1.295E-16 !
! - .0313185 - .0378327 - .0142622 .0208907 .0397693 !
! 1.261E-16 1.643E-16 - 2.211E-17 - 1.201E-16 - 1.692E-16 !
! - .0282896 - .0335690 - .0114548 .0201849 .0355957 !
! 1.036E-16 0. 5.427E-17 0. - 1.184E-16 !

As found earlier, the coefficients for the sine components are all zero (nearly zero from the
numerical results). The coefficients for the cosine components are very consistent for most

Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz

values of m except for a few of them. For that reason it is a good idea to check the
convergence of the Filon quadrature formulae for different values of m.
Use of Fourier series in ordinary differential equations

Fourier series can be used to represent periodic functions within ordinary differential
equations. Consider the case of an oscillating mass subject to damping (

)and a linear
restoring force (k):


= + + m
.
-
,
,
+
*
)
)

2
x
2
( ) y x
.
-
,
,
+
*
)
)

x
( ) y x k ( ) y x ( ) F x



The function F(x) in the right-hand side of the equation represents the excitation or driving
force, i.e., an external effect that activates or affects the oscillating system. If the function
F(x) happens to be a periodic function, the expression for F(x) can be replaced by its Fourier
series expansion G(x), before obtaining a solution to the ODE.

As an example, let a trapezoidal driving function f(x)

.
6
6
3
6
6
4
5
< <
)
*
+
,
-
.

< <
< <
=
elsewhere
x x
x
x x
x f
, 0
1 3 / 2 ,
3
2
3 1
3 / 2 3 / 1 , 1
3 / 1 0 , 3
) (


represent the excitation function in the solution of the differential equation for an oscillating
mass. In the solution, we will use both the original pulse function f(x) and the Fourier series
expansion of f(x), G(x), as illustrated below.
Fourier series for a trapezoidal pulse

To obtain the Fourier series of the trapezoidal pulse, first, we define function f(x):

-->deff('[y]=f(x)',['if x>0 & x<1/3 then';'y=3*x';...
--> 'elseif x>1/3 & x<2/3 then';'y=1';'elseif x>2/3 & x<1 then';...
--> 'y=1-3*(x-2/3)';'else';'y=0';'end'])

Next, we generate data for plotting the function in the range 0 < x < 1:

-->x=0:0.01:1;
-->y=[]; for j=1:length(x), y=[y f(x(j))]; end;

The following statements generate the coefficients of a Fourier series with 10 components:

-->L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

Next, we produce a plot of the original function and the Fourier series:

-->plot2d([x' x'],[y' yy'],[1,-1])
-->xtitle('Trapezoid function - Fourier series 20 components','x','y')

Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz



The graph shows that even if we use only 10 components for the Fourier series, the fitting for
the trapezoidal function is excellent. Notice that in the definition of f(x) the function is zero
outside of the range [0,1]. Thus, f(x) defines a single trapezoidal pulse as illustrated in the
figure below in which we plot f(x) in the range -2 < x < 3:


-->xx=[-2:0.01:3];y=[];for j=1:length(xx), y=[y f(xx(j))]; end;

-->plot2d(xx,y,1,'011',' ',[-2 -0.1 3 1.1])

-->xtitle('Single trapezoidal pulse given by f(x)','x','y')







Next, we calculate the Fourier series generated by using the trapezoidal pulse f(x) in the range
0 < x < 5 to demonstrate the periodicity of the Fourier series:

-->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

-->plot(x,yy,'x','y','Trapezoidal periodic wave')



Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz



Solution to the damped oscillator equation with trapezoidal pulse and wave excitation

Next we will solve the second-order differential equation using as driving signals the original
trapezoidal function (a single trapezoidal pulse) and the corresponding Fourier series (a
periodic trapezoidal wave). To solve the problem, we use the variables u
1
= y(x) and u
2
=
dy/dx, to form the vector function u(x) = [u
1
;u
2
], and transform the equation into

du/dx = ff(x,u) = (F(x)-ku
1
-u
2
)/m.

In the latter expression we first replace the function F(x) with the original trapezoidal pulse
f(x), and then with the Fourier series G(x). We will use as initial conditions u
1
(0) = u
2
(0) = 0
and solve the ODE in the range 0 < x < 20 with an increment x = 0.1.

Following we list the SCILAB commands for solving the ODE. The function f(x) describing the
trapezoidal pulse was defined earlier. Also, the coefficients a0, a, and b of the Fourier series
are available from the call to function fourierseries. The next step is to define a function G(x)
that calculates the Fourier series:

-->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';...
--> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end'])


Next, we define functions f1(x,u) and f2(x,u) representing the function ff(x,u), i.e., the right-
hand side in the equation du/dx = ff(x,u). Function f1(x,u) involves the trapezoidal pulse
function f(x) while function f2(x,u) involves the Fourier series function G(x) as indicated below:

-->deff('[w]=f1(x,u)','w=[u(2),(f(x)-k*u(1)-beta*u(2))/m]')

-->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]')

The next step is to define the parameters of the problem and of the solution:

-->k=10;m=5;beta=1;

-->x = [0:0.1:20]; x0 = 0; y0 = [0;0];

The solutions to the ODE for the two cases is calculated using function ode as follows:

-->y1=ode(y0,x0,x,f1);

-->y2=ode(y0,x0,x,f2);


Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz

The following plot shows the solutions corresponding to the single trapezoidal pulse (continuous
line) and to the periodic trapezoidal wave (circles). Notice that the signal corresponding to
the single trapezoidal pulse starts at rest and reaches a maximum amplitude at the beginning,
decaying in a sinusoidal manner afterwards. On the other hand, the signal due to the periodic
trapezoidal wave, although, in general also decaying with x, shows additional secondary peaks
that result from the repeated excitation represented by the periodic function represented by
the Fourier series.

-->plot2d([x' x'],[y1(1,:)' y2(2,:)'],[1,-9])

-->xtitle('Damped oscillator solution','x','y')





Fourier series for other periodic shapes

Thus, we have demonstrated how to use Fourier series to generate a periodic function
representing almost any type of shape. All we need to get started is a single pulse with the
basic shape that needs to be repeated. Thus, Fourier series can be used to produce periodic
functions such as those illustrated in the figure below:





Even when using the same basic function, e.g., f(x) in the examples above, to generate a
Fourier series, you can change the shape of the resulting periodic signal by changing the period
of the function. In the example worked out above, we used a period L = 1.0, thus, the basic
shape (the trapezoid) repeats one after another in the Fourier series. If we want to add some
space between consecutive trapezoids we can change the period to L = 1.5. The following
SCILAB commands show how to generate such signal.

Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz


-->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

-->plot(x,yy,'x','y','Trapezoidal periodic wave')



Solving the damped oscillator equation with a different periodic trapezoidal pulse

With the signal shown above as the excitation to the system represented by the damped
oscillator equation, the solution to the ODE is presented in the following plot:

-->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';...
--> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end'])

-->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]')

-->k=10;m=5;beta=1;x=[0:0.1:20];x0=0;y0=[0;0];

-->y3=ode(y0,x0,x,f2);

-->plot(x,y3(1,:),'Damped oscillator solution','x','y')




Compare the latter result with the previous solution.



Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz

F Fo ou ur ri ie er r t tr ra an ns sf fo or rm ms s

In the previous solution, the variable x can be thought of as representing time, while y may
represent the displacement of an oscillator. When writing out Fourier series for a function of
time we would use

f(t) = a
0
/2 + [ a
n
cos(2nt/T) + b
n
sin(2nt/T),


n = 1,2, , where T is the period of the function, f
0
= 1/T is the fundamental frequency, and
0

= 2/T = 2f
0
is the fundamental angular frequency. The Fourier series can also be written as


f(t) = a
0
/2 + [ a
n
cos(
n
t) + b
n
sin(
n
t)],


where
n
= n
o
, and


! ! !

= = =
2 /
2 /
2 /
2 /
2 /
2 /
0
). sin( ) (
2
), cos( ) (
2
, ) (
2
T
T
n n
T
T
T
T
n n
t t f
T
b t t f
T
a dt t f
T
a


Plots of the Fourier coefficients a
n
and b
n
vs.
n
would be similar to the ones sketched below:



In the figure, = 2/T represents the increment in the discrete values of the frequency
n
.
As the period of the signal, T, increases, the increment in frequency, , becomes a
differential of frequency, d. Using the definitions of the coefficients shown above, the signal
can be written as

. ) sin( ) sin( ) (
2
) cos( ) cos( ) (
2
2
) (
1
2 /
2 /
2 /
2 /
0
"
! !

=

#
$
%
&
'
(
7
8
9
3
4
5
+
7
8
9
3
4
5
+ =
n
n
T
T
n n
T
T
n
dt t t t f
T
t dt t t f
T
a
t f

Using 2/T = /, and replacing the summations with integrals as T ! , the signal is now
written as


Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz

. ) sin( ) sin( ) ( ) cos( ) cos( ) (
2
) (
0
0
! ! !


#
$
%
&
'
(
7
8
9
3
4
5
+
7
8
9
3
4
5
+ = t dt t t f
d
t dt t t f
d a
t f
n n n n




If we define

! !


= = , ) sin( ) (
2
1
) ( , ) cos( ) (
2
1
) ( dt t t f B dt t t f A



we can re-write the signal for an infinitely large period as

[ ] . ) sin( ) ( ) cos( ) ( 2
2
) (
0
0
!

+ + = d t B t A
a
t f

The terms A() and B() are components of a Fourier transform, and the signal f(t) results
from an inverse Fourier transform.

The Fourier transform of the signal f(t) is the term F() = A()-iB(), where i is the unit
imaginary number (i = -1). Using Eulers formula, e
-it

= cos(t)-isin(t), the Fourier
transform is written as
!

= . ) (
2
1
) ( dt e t f F
t i



The signal f(t), i.e., the inverse Fourier transform is written as

!


= . ) ( ) (

d e F t f
t i


The latter result assumes that a
0
= 0, which can be accomplished by subtracting the original
value of a
o
from the original function.

Fourier analysis of discrete data

Many a time, engineers and scientists are faced with the need of analyzing discrete data
resulting from measurements of communication signals, turbulence velocity variations, water
surface records, weather data, etc. We may be interested in determining, for example, what
are the dominant frequencies present in the signal.

Assume that the data is collected at regular intervals t = T/n and consists of the n
measurements {x
0
, x
1
,x
2
,,x
n-1
} taken at times t
r
= rt. An approximation to the Fourier
transform F() presented earlier, is the summation

1 ,..., 1 , 0 ,
1 1
1
0
) / 2 (
1
0
) )( / 2 (
= = =
" "

=

n k e x
n
t e x
T
X
n
r
n kr i
r
n
r
t r T k i
r k

.

This is known as the discrete Fourier transform (DFT). The inverse discrete Fourier transform
(IDFT) provides the data values x
r
in terms of the coefficients X
k
:


Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz

. 1 ,..., 1 , 0 ,
1
0
) / 2 (
= =
"

=
n r e X x
n
k
n kr i
k r



The coefficients X
k
calculated with the discrete Fourier transform, namely, X
k
= a
k
- ib
k

represent coefficients of a Fourier series from which the data values x
r
can be obtained.
However, the coefficients X
k
are only correct for frequencies below the frequency

k
= 2k/(nt) = /t,

i.e., for k = 0, 1, 2, , n/2. Also, if the signal contains frequencies above /t, these
frequencies introduce a distortion of the X
k
-vs -
k
graph known as aliasing. If w
0
is the
maximum component of frequency contained in the signal x(t), aliasing is avoided by selecting
t so that /t >
o
, or 1/(2t) > f
0
. The value 1/(2t) is known as the Nyquist frequency,
which represents the maximum frequency that can be detected by sampling the data at
intervals t.

SCILAB functions for discrete Fourier transform (DFT) and fast
Fourier transform (FFT)

SCILAB provides functions dft and fft to calculate discrete Fourier transforms and fast Fourier
transforms, respectively. The fast Fourier transform (FFT) is an algorithm that applies to
discrete data that includes 2
m
elements (m = integer). FFT takes advantage of the fact that
the size of the sample is a power of 2 to dramatically reduce the amount of computer
operations in the calculation of the DFT that would otherwise be required. This approach thus
provides economy of storage and operations when calculating DFTs. (For details on the
algorithm for the fast Fourier transform, the reader is referred to Newland, D.E., 1993,An
Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition, Longman
Scientific & Technical, Essex, England.)
Scilabs dft function

Function dft applies to single data vectors. The general call to the function is

[xf]=dft(x,flag);

where x is an input vector which may represent signal data (x
r
, r = 0,1,,n-1) or Fourier
transform coefficients (X
k
, k = 0,1, , n-1), and flag determines whether to perform a discrete
transform (flag=-1) or an inverse discrete transform (flag=1). The function returns a vector
containing either the discrete Fourier transform coefficients, X
k
, if flag = -1, or the signal
values, x
r
, if flag = 1.
Example of DFT applications: Obtaining the discrete Fourier transform

First, we generate a set of n=101 random numbers representing discrete values y
r
, r = 0, 1, ,
n-1, of a signal y(t), through the use of function y = f(x) shown below. The function is a sum of
m=10 cosine components plus a couple of random terms. The coefficients in the cosine
components are random numbers generated using SCILAB function rand. The figure below
shows the signal calculated:

-->m=10;
-->k=int(100*rand(1,m));a=int(1000*rand(1,m));phi=int(2*%pi*rand(1,m));
-->deff('[w]=f(x)',...
-->['w=0';'for j=1:m';...
-->'w=w+a(j)*cos(2*k(j)*%pi*x+phi(j))+100*(rand()-0.5)-2000*(rand()-0.5)';...
-->'end'])


Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz

-->x=[0:0.1:10]; y = f(x); plot(y); xtitle('Random data','k','y');
-->m=length(y)m = 101.


The discrete Fourier transform of the signal is obtained by using the following call to function
dft. Of interest to us is the magnitude of the coefficients, i.e., |X
k
| = (A
k
2
+B
k
2
)
1/2
, which we
refer to as XfA. A plot of XfA is shown next.

-->Xf=dft(y,-1); XfA = abs(Xf);
-->plot2d([1:1:101]',XfA,-9)
-->plot2d3('onn',[1:1:101]',XfA)

There are a few frequencies that shown strong components, e.g., for k = 1, 11, 31, 41, 42, 52,
and so on. Also, notice that there is a symmetry in the figure, which results from the periodic
nature assumed for the signal in Fourier analysis. This verifies the statement made earlier that
out of the n components of the discrete Fourier transform, only those for k = 0, 1, , n/2, are
necessary to reproduce the signal.
Example of DFT applications: Obtaining the inverse Fourier transform

In this exercise we generate twenty of complex coefficients representing the Fourier transform
of a signal, i.e., X
k
, k = 0,1, , n-1, with n=20. To visualize the Fourier transform we plot its
absolute value, |X
k
|:

-->Xf2 = int(20*rand(1,20))+%i*int(10*(rand(1,20)-0.5));
-->Xf2A = abs(Xf2);
-->xset('mark',-9,1); plot2d([1:1:20]',Xf2A',-9)
-->plot2d3('onn',[1:1:20]',Xf2A')
-->xtitle('|X(w)| vs. w','w','|X(w)|')


Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz


Through the use of function dft, as shown below, we can generate a signal xs2 based on the
Fourier transform coefficients Xf2. Signal xs2 is the inverse discrete Fourier transform of Xf2:

-->xs2 = dft(Xf2,1)
xs2 =

! 11.3 - .3i !
! .3709582 + .1198511i !
! - 2.2329752 - 1.2846661i !
! 2.1378546 - .3916269i !
! - .1146755 + .0364961i !
! - .55 - 1.95i !
! - .1372551 - .7108113i !
! 1.5447692 - 1.233702i !
! 2.1785955 + 1.5250802i !
! - .2862057 + .1284268i !
! - 1.2 + .3i !
! 1.1105288 + .2007308i !
! 1.604686 - 1.0823751i !
! 1.075967 + .5249291i !
! - .0593008 + .6035164i !
! .45 + 1.35i !
! - 1.468606 + .0707988i !
! 2.1392805 + 1.176793i !
! - .870469 + .8419610i !
! 1.0068474 + .0745981i !

The resulting signal contains complex numbers. We can, therefore, obtain a signal consisting
only of the real part of xs2 (xs2R), one consisting only of the imaginary part of xs2 (xs2I), and
one formed by taking the magnitude of the elements of xs2 (xs2A). The three different signals
are plotted below.

-->xs2R = real(xs2);xs2I = imag(xs2); xs2A = abs(xs2);nn=[1:1:20];
-->plot2d(nn',xs2R,-9,'011',' ',[0 -3 21 13])
-->plot2d(nn',xs2R,1,'011',' ',[0 -3 21 13])
-->xtitle('Real part of signal','r','x(r)')



Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz



-->plot2d(nn',xs2I,-9,'011',' ',[0 -2 21 1.6])
-->plot2d(nn',xs2I,1,'011',' ',[0 -2 21 1.6])
-->xtitle('Imaginary part of signal','r','x(r)')



-->plot2d(nn',xs2A,-9,'011',' ',[0 0 21 12])
-->plot2d(nn',xs2A,1,'011',' ',[0 0 21 12])
-->xtitle('Magnitude of signal','r','x(r)')






Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz

Example of DFT applications: Obtaining a real inverse Fourier transform

In order to obtain a real inverse Fourier transform, the vector of Fourier transform coefficients
must satisfy certain symmetry conditions. Let X represent the vector of Fourier transform
coefficients, the first term must be a real number, representing the coefficient a
0
in the
equation

[ ] . ) sin( ) ( ) cos( ) ( 2
2
) (
0
0
!

+ + = d t B t A
a
t f


If the vector contains an even number of elements n, then, the elements X
k
, k=2,3, ,n in the
vector X must be such that X
n
= X
2
, X
n-1
= X
3
, X
n-2
=X
4
,, or, in general, X
n-j+2
= X
j
, j = 2,3,
, n/2. The termsX represents the complex conjugate of X. Finally, element X
n/2+1
must
also be a real number.

As an example, we construct the following vector of coefficients, Xf3, with n=20 elements:

-->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));

-->n=20;Xf3 = [120 X0 zeros(1,m-1)];

-->for j=2:n/2, Xf3(n-j+2)=conj(Xf3(j)); end;

-->Xf3(n/2+1)=2.0; Xf3'
ans =

! 120. !
! 18. - 20.i !
! 36. - 20.i !
! - 85. + 9.i !
! 88. + 43.i !
! - 74. + 43.i !
! - 59. - 3.i !
! - 60. + 46.i !
! 78. + 18.i !
! - 7. + 12.i !
! 2. !
! - 7. - 12.i !
! 78. - 18.i !
! - 60. - 46.i !
! - 59. + 3.i !
! - 74. - 43.i !
! 88. - 43.i !
! - 85. - 9.i !
! 36. + 20.i !
! 18. + 20.i !

A plot of the Fourier coefficients magnitude is shown next. The symmetry of the coefficients
is apparent from the plot:

-->Xf3A=abs(Xf3);

-->nn=[1:1:20];xset('mark',-9,1);plot2d(nn',Xf3A',-9,'010',' ',[0 0 21 150])
-->plot2d3('onn',nn',Xf3A',1,'010',' ',[0 0 21 150])
-->xtitle('Symmetric Fourier coefficients','w','|X(w)|')


Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz



The corresponding signal is generated using:

-->xs3 = dft(Xf3,1)
xs3 =

! - .4 !
! 20.141905 - 2.176E-15i !
! 13.737285 - 9.770E-16i !
! - 6.6365465 - 4.885E-15i !
! - 5.0329987 + 6.040E-15i !
! 22.8 - 1.137E-14i !
! - 1.157441 + 2.469E-14i !
! - 9.2480507 + 1.226E-14i !
! - 9.98364 + 1.457E-14i !
! 10.952211 - 6.528E-15i !
! 41.2 - 5.351E-14i !
! 1.3207894 + 1.164E-14i !
! - 1.0044013 + 2.549E-14i !
! - 7.835515 - 1.679E-14i !
! - 12.637178 + 8.331E-14i !
! 26.8 - 1.272E-13i !
! 9.92104 - 1.137E-14i !
! 4.9413984 - 6.040E-15i !
! 26.357334 - 3.366E-14i !
! - 4.2361923 + 4.836E-14i !


The generated signal is made of complex numbers, however, their imaginary parts are
negligible. We will use only their real parts:

-->xs3=real(xs3);


A plot of the signal is presented next:

-->plot(xs3,'r','x(r)','Real signal IDFT')

-->plot2d(nn',xs3,-9,'010',' ',[1 -20 21 50])


Download at InfoClearinghouse.com 68 2001 Gilberto E. Urroz



If the vector of Fourier transform coefficients contains an odd number of elements, the
elements X
k
, k=2,3, ,n in the vector X must be such that X
n
= X
2
, X
n-1
= X
3
, X
n-2
=X
4
,, or, in
general, X
n-j+2
= X
j
, j = 2,3, , n/2+1. The first element in vector X must still be a real
number.

As an example, we put together the Fourier coefficients vector Xf4 with 21 components. A plot
of the magnitude of the coefficients is shown next:

-->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));
-->n=21;Xf4 = [50 X0 zeros(1,m)];
-->for j=2:n/2+1, Xf4(n-j+2)=conj(Xf4(j)); end;
-->Xf4A = abs(Xf4);
-->nn=[1:1:21];xset('mark',-9,1);plot2d(nn',Xf4A',-9,'010',' ',[0 0 21 150])
-->plot2d3('onn',nn',Xf4A',1,'011',' ',[0 0 21 150])
-->xtitle('Symmetric Fourier coefficients - odd number','w','|X(w)|')


The signal is generated with the following call to function dft. We replace the resulting signal,
which may still contain negligible imaginary components, with its real part only, and plot the
results as follows:

-->xs4 = dft(Xf4,1); xs4=real(xs4);

-->plot(xs4,'r','x(r)','Real signal IDFT - odd number')





Download at InfoClearinghouse.com 69 2001 Gilberto E. Urroz

SCILABs fft function

SCILABs fast Fourier transform function fft is called in a similar manner as dft. The general
call to the function is
[xf]=fft(x,flag);

where x is an input vector which may represent signal data (x
r
, r = 0,1,,n-1) or Fourier
transform coefficients (X
k
, k = 0,1, , n-1), and flag determines whether to perform a discrete
transform (flag=-1) or an inverse discrete transform (flag=1). As with function dft, the
function returns a vector containing either the discrete Fourier transform coefficients, X
k
, if
flag = -1, or the signal values, x
r
, if flag = 1.

Note: while the FFT algorithm was developed for data sets containing 2
m
, m=integer, data
points, the SCILAB function fft has no restriction with respect to the size of the sample used.

Example of FFT applications: Obtaining a discrete Fourier transform

First we generate a one-dimensional signal by adding random numbers:

-->xs1=[];for j=1:200, xs1=[xs1 100*rand()-50*(rand()-0.5)]; end;
-->plot(xs1,r,x(r),)



The discrete Fourier transform and a graph of the magnitude of the transform coefficients is
shown next:

-->Xf1=fft(xs1,-1);
-->Xf1A=abs(Xf1);
-->plot(Xf1A,'w','|X(w)|')


Download at InfoClearinghouse.com 70 2001 Gilberto E. Urroz



The original signal obtained by adding uniformly generated random numbers (with SCILAB
function rand) produces what is typically known as white noise, i.e., a random signal where it
is very difficult to detect any periodicity. The resulting Fourier transform coefficients show
one single large component, for k=1, and almost negligible components for the rest of the
frequency spectrum.

Example of FFT applications: Obtaining an inverse Fourier transform

To generate a real signal out of a vector of Fourier transform coefficients we use the following
approach so that the vector of coefficients presents the symmetry characteristics defined
earlier. We will use a vector of coefficients with 201 components:

-->m=100;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));
-->n=201;Xf2 = [50 X0 zeros(1,m)];
-->for j=2:n/2+1, Xf2(n-j+2)=conj(Xf2(j)); end;
-->Xf2A=abs(Xf2);
-->plot2d3('onn',[1:1:201]',Xf2A')
-->xtitle('Symmetric Fourier coefficients','k','|X(k)|')



Although the plot of the magnitude of the coefficients seems random, if you observe the graph
carefully you will see the symmetry of the coefficients about the middle point.

The following call to function fft produces the signal. A plot of the real part of the signal is
also shown:

-->xs2=fft(Xf2,1);
-->xs2=fft(Xf2,1);xs2=real(xs2);plot(xs2,'r','x(r)','Real signal from IFFT')

Download at InfoClearinghouse.com 71 2001 Gilberto E. Urroz



Discrete Fourier transform for two-dimensional functions

While function dft applies to a single vector of data, function fft can be applied also to a
matrix representing a two-dimensional signal. You can actually apply fft to an array of N
dimensions (N>2) by using SCILAB hypermatrices (see --> help hypermat). The call to fft
when using hypermatrices is different from the one shown above. For more information the
reader is referred to the SCILAB help facility (--> help fft).

To understand the application of Fourier transforms to two-dimensional data we start with a
two-dimensional array of data in the rectangle defined by 0 < x < L
x
, 0 < y < L
y
. Suppose that
the rectangular region is divided in a grid of n
1
points along the x-axis and n
2
points along the
y-axis so that at point z
rs
= z(x
r
,y
s
) represents the values of the data array. The coefficients
of the two-dimensional Fourier transform for the data array z are given by

, )] ( 2 exp[
1
1
0
1
0 2 1 2 1
1 2
""

=
n
r
n
s
rs km
n
s m
n
r k
i y
n n
Z


for k=0,1,2,,n
1
-1 and m=0,1,2,,n
2
-1. On the other hand, the two-dimensional inverse
Fourier transform will produce the values y
rs
according to


, )] ( 2 exp[
1
0
1
0 2 1
1 2
""

=
n
r
n
s
km rs
n
s m
n
r k
i Y z

for r=0,1,2,,n
1
-1 and s=0,1,2,,n
2
-1.

Example of FFT applications: Obtaining the discrete Fourier transform for a two-dimensional
signal

We will generate a two-dimensional signal by defining the following function, which involves a
random component. A plot of such signal is shown next:

-->deff('[w]=f(x,y)','w=sin(x)*cos(y) + (rand()-0.5)')
-->x=[-6:0.5:6];y=x;z=feval(x,y,f);
-->plot3d(x,y,z)


Download at InfoClearinghouse.com 72 2001 Gilberto E. Urroz




The following call to function fft generates the coefficients of the corresponding two-
dimensional Fourier transform. To visualize the coefficients we plot their magnitudes using
function hist3d (three-dimensional histogram -- see Chapter 3):

-->Z = fft(z,-1);ZA=abs(Z);

-->hist3d(ZA)



The figure depicting the magnitudes of the Fourier transform coefficients show four
components that tower above the remaining set of components. There is indeed only one
major component, the one shown located closest to the origin (0,0). The other three
components are simply reflections of the major component about of lines of symmetry located
in the middle of the ranges of X and Y in the figure. This property of the two-dimensional
Fourier transform of a real signal follows, as in the one-dimensional case, from the fact that
Fourier transforms assume a certain periodicity in the independent variables of the function.

Example of FFT applications: Obtaining a real two-dimensional inverse Fourier transform

In order to obtain a real two-dimensional Fourier transform, the matrix of values Z
km
must
satisfy certain symmetry conditions. For the case in which the n
1
n
2
matrix of coefficients for
the Fourier transform is such that both n
1
and n
2
are even, the values Z(1,1), Z(n
1
/2+1,1),
Z(1,n
2
/+1), and Z(n
1
/2+1,n
2
/2+1) must always be real. The remaining elements of matrix Z are
such that Z(n
1
-k+1,1) = Z(k,1), for k = 1,2,,n
1
/2, Z(1,n
2
-m+1) = Z(1,m+1), for m = 1,2,,n
2
,
and Z(n
1
-k+1,n
2
-m+1) = Z(k,m), for k = 1,2,,n
1
, m = 1,2,,n
2
, whereZ represents the
complex conjugate of Z.

Download at InfoClearinghouse.com 73 2001 Gilberto E. Urroz


The following SCILAB script (called Zgen) produces a matrix of Fourier transform coefficients
from which a signal (inverse Fourier transform) is generated:

n1=16;n2=16; Z=zeros(n1,n2);

for i = 1:n1
for j = 1:n2
Z(i,j) = (int(100*((rand()-0.5)+%i*(rand()-0.5))));
end;
end;

for i = 2:n1/2
Z(n1-i+2,1) = conj(Z(i,1));
end;

for j = 2:n2/2
Z(1,n2-j+2) = conj(Z(1,j));
end;

for i = 2:n1
for j = 2:n2
Z(n1-i+2,n2-j+2) = conj(Z(i,j));
end;
end;
Z(1,1) = 10; Z(n1/2+1,1) = 5.0; Z(1,n2/2+1) = 3.0;
Z(n1/2+1,n2/2+1) = 2.0;

x=[1:1:n1]; y=[1:1:n2];

xset('window',1);plot3d(x,y,abs(Z));
xtitle('Magnitude of Fourier transform coefficients');

z=fft(Z,1);

xset('window',2);plot3d(x,y,real(z));
xtitle('Real signal generated with fft');

To run the script use function exec. Thus, if the script is stored in SCILABs current working
directory, you would use:

-->exec(Zgen)

The results are the following two graphs, the first one showing a plot of the magnitude of the
Fourier series coefficients and the second one showing the signal generated with function fft:



Download at InfoClearinghouse.com 74 2001 Gilberto E. Urroz





E Ex xe er rc ci is se es s

[1]. Verify the following formula using Bessel functions of the first kind, J
n
(x), for values of n =
2, 3, and 4:

. ) ( ) 1 ( ) ( '
2
1
) (
2
2
2
2
1
0
2
#
$
%
&
'
(
+ =
!


n n n
J
n
J dx x J x


[2]. Verify the following formulae using Bessel functions of the second kind, Y
n
(x), for values of
n = 2, 3, and 4 by plotting both sides of the equations in the same set of graphs for 0<x<1.

(a) Y
n+1
(x) = (2n/x) Y
n
(x) - Y
n-1
(x) (b) Y
n
(x) = (1/2) [Y
n-1
(x)-Y
n+1
(x)]





[3]. Rodrigues formula defines the Legendre polynomials, P
n
(x), as

. ) 1 (
! 2
1
) (
2 n
n
n
n
n
x
dx
d
n
x P

=

Generate a sequence of Legendre polynomials using this formula for n = 0, 1, 2, 3, 4, 5. Use
SCILAB polynomials and the function derivat.

[4]. Verify the following recurrence formula for Legendre polynomials for n = 2, 3, 4, by
plotting both sides of the equation in the same set of axes for 0<x<1.

). (
1
) (
1
1 2
) (
1 1
x P
n
n
x xP
n
n
x P
n n n +
+

+
+
=

For problems [5] through [14] generate a table showing the calculation of the integral of f(x)
between the limits x=a and x=b using Gaussian quadrature for values of n = 2,3,4,,20.


Download at InfoClearinghouse.com 75 2001 Gilberto E. Urroz

[5] f(x) = 1/(1+ ln(x)), a = 1, b = 5

[6] f(x) = 1 + x
2
+ x
3
+ 1/x, a = 1, b= 3.5

[7] f(x) = exp(-x/5)/(1+x
2
), a = -2, b = 2

[8] f(x) = (x
2
+1)
1/2
, a = -1 , b = 1

[9] f(x) = (x
2
+1)
1/3
+2.5(x
2
+1)+1.5 , a = 0, b = 5

[10] f(x) = sinh(x)/(1+x
3
) , a = 0 , b = 3

[11] f(x) = exp(-x
2
/2)/(2)
1/2
, a = -4, b = 4

[12] f(x) = 2.5(1+x
2
+x
3
/3) cos(3x
2
+2), a = 0, b = 2

[13] f(x) = 4.2x
1/3
+ 3.245x
1/2
+ 1.4142, a = 0.25, b = 1.25

[14] f(x) = 0.33 ln((x+1)/(x
2
+5x+2)) , a = 0, b = 20

For problems [15] through [19] use Gauss-Laguerre quadrature to obtain the integral of the
functions f(x) in the interval [0,).

[15]. f(x) = 1/(x+1)
2


[16]. f(x) = exp(-x
2
)

[17]. f(x) = ln(1+e
-x
)

[18]. f(x) = 1/(1+x
2
)
2

[19]. f(x) = e
-x
/(x+100)

For problems [20] through [24] use Gauss-Hermite to obtain the integral of the functions f(x) in
the interval (-,).

[20]. f(x) = exp(-x
2
)

[21]. f(x) = exp(-x
2
)cos(x)

[22]. f(x) = 1/(1+x
2
)
2


[23]. f(x) = exp(-x
4
)

[24]. f(x) = 1/x
2
+ 1/x
4

For problems [25] through [29] use Filons formulae to calculate the integral of the functions
f(x) cos(kx) and f(x) cos(kx) between the integration limits x=a and x=b:

[25]. f(x) = x+1, a = 0, b = 2

[26]. f(x) = exp(x), a = 2, b = 10

[27]. f(x) = ln(1+x
2
), a = 0, b = /2


Download at InfoClearinghouse.com 76 2001 Gilberto E. Urroz

[28]. f(x) = 1/(1+x), a = 0, b = 2

[29]. f(x) = |x|, a = -1, b = 1

[30]. Verify, using SCILAB, that Hermite polynomials satisfy the following formula:

7
8
9
3
4
5
=

=
!

n m n
n m
dx x H x H e
n n m
x
, ! 2
, 0
) ( ) (
2



Use values of (n,m) = (2,3) and (n,m) = (3,3).

[31]. Verify, using SCILAB, that Laguerre polynomials are orthogonal in the range [0,) with
respect to the function e
-x
,i.e., verify that

0 ) ( ) (
0
=
!

dx x L x L e
n m
x


Use values of (n,m) = (2,3), (2,4), (3,4).

[32]. (a) Verify that the second-order, ordinary differential equation, y+y = 0, with boundary
conditions y(0) = 0, y(1) = 0, is a Sturm-Liouville system. (b) Find the eigenvalues, , of the
system by first obtaining a general solution of the equation and replacing the boundary
conditions in this solution. (c) Find the corresponding eigenfunctions. (d) Verify that the
eigenfunctions are orthogonal in the interval [0,1].

[33]. Determine and plot the sine series corresponding to the following functions:
f(x) = 4, 0<x<2, and f(x) = -4, 2<x<4, period L = 4
f(x) = -x
2
, - 4 < x < 0, and f(x)= x
2
, 0 < x < 4, period L = 8
f(x) = sin(x
2
+x/2), 0 < x < 5, period L = 5
f(x) = 2 - x, 0 < x < 4, and f(x) = x - 6, 4 < x < 8, period L = 8


[34]. Determine and plot the cosine series corresponding to the functions of problem [33].


[35]. Determine and plot the Fourier series corresponding to the functions of problem [33].


[36]. Determine the Fourier series corresponding to the following functions:
f(x) = tanh(x), 0 < x < , period L =
f(x) = 1/(1+x+x
2
), 0 < x < 1, period L = 1
f(x) = x exp(-x), 0 < x < 2, period L = 2
f(x) = x( -x), 0 < x < , period L =

[37]. Solve the following differential equation replacing the function f(x) with the Fourier
series of problem [16]. Plot the solution for 0 < x < 4L, where L is the period of the function as
specified in problem [16]. Use as initial conditions y(0) = 0, y(0) = :

). (
2
1
3
1
2
2
x f y
dx
dy
dx
y d
= + +



Download at InfoClearinghouse.com 77 2001 Gilberto E. Urroz

[38]. Solve the following differential equation replacing for f(x) each of the Fourier series in
problem [17]. Plot the solution for 0 < x < 4L, where L is the period of the function as
specified in problem [17]. Use as initial conditions y(0) = 1, y(0) = 1:

). (
2
2
x f y
dx
y d
= +

[39]. Solve the following differential equation replacing the periodic signals f(t), of period T =
4, shown in the figure below with their corresponding Fourier series. Plot the solution for 0 < t
< 20. Use as initial conditions, x(0) = 1/3, x(0) = -1:

). (
2
2
t f x
dt
dx
dt
x d
= + +



[40]. SCILABs function rand can be used to generate random numbers that have a uniform
distribution in [0,1]. Use the following commands to generate 1000 random numbers uniformly
distributed in [-0.5,0.5]:

-->rand(uniform); dx = rand(1,1000)-0.5;

The vector dx now contains those random numbers. A plot of the signal dx
k
vs. k, for k =
1,2,1000, represents what is commonly known as random noise or white noise (reference to
the frequency contents of white light in the electromagnetic spectrum).

Next, produce a signal x by using the formula


Download at InfoClearinghouse.com 78 2001 Gilberto E. Urroz

, ,
1
1 1 1 "
=
+
= =
k
i
i k
dx x dx x

for k = 1,2, , 999. The signal thus generated, x
k
, represents what is known as a fractal
Brownian signal.

(a) Obtain and plot the magnitude of the Fourier transform coefficients, |DX(k)|, for the
white noise signal dx. What can be said about the dependence of |DX(k)|, on k, k =
1,2,,1000?
(b) Obtain and plot the magnitude of the Fourier transform coefficients, |X()|, for the
fractal Brownian signal x. . What can be said about the dependence of |DX(k)|, on k, k
= 1,2,,1000?

[41]. Repeat problem [40] using a normally distributed set of random numbers with function
rand. Use the following SCILAB commands to generate signal dx:

-->rand(normal); dx = rand(1,1000)-0.5;

[42]. Fractal signals are characterized by having Fourier transform coefficients that vary
inversely with the frequency, i.e., |X(k)| = b/k, where b = constant. Generate and plot a real
signal x(t) corresponding to 1000 Fourier transform coefficients given by |X(k)| = b/k, for k =
1,2,,1000, and b = 100. Is the signal generated similar in shape to signal x generated in
problems [40] and [41]?

[43]. Generate a two-dimensional signal by using the function

w(x,y) = sqrt(x^2+y^2) + (rand()-0.5)

and ranges of values of [-2:0.1:2] in x and y. Obtain and plot the two-dimensional Fourier
transform using (a) a three-dimensional histogram, and (b) a surface plot.

[44]. A two-dimensional fractal signal can be generated by defining Fourier transform
coefficients of the form |X(k,m)| = b/(k
2
+m
2
)
1/2
. Generate a 20x20 set of Fourier transform
coefficients corresponding to a two-dimensional fractal signal, plot the magnitude of the
coefficients and the resulting two-dimensional signal.


Download at InfoClearinghouse.com 79 2001 Gilberto E. Urroz

R RE EF FE ER RE EN NC CE ES S ( (f fo or r a al ll l S SC CI IL LA AB B d do oc cu um me en nt ts s a at t I In nf fo oC Cl le ea ar ri in ng gh ho ou us se e. .c co om m) )
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.

Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.

Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.

Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.

Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.

Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.

Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.

Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.

Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.

Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.

Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.

Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.

Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.

Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.

Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.

Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.

Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.

Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.

Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.

Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.

Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.

Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.

Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.

Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.

McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.

Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.

Download at InfoClearinghouse.com 80 2001 Gilberto E. Urroz


Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.

Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.

Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.

Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.

Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.

Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.

Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.

Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.

Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.

Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.

Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.

Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.

Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.

Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.

Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.

Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.

Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.

Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.

Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.

Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.




1 2001 Gilberto E. Urroz
All rights reserved
Orthogonal Functions, Gaussian
Quadrature, and Fourier Analysis with
SCILAB



By

Gilberto E. Urroz, Ph.D., P.E.













Distributed by


i nfoClearinghouse.com









2001 Gilberto E. Urroz
All Rights Reserved


2 2001 Gilberto E. Urroz
All rights reserved
O Or rt th ho og go on na al l f fu un nc ct ti io on ns s, , G Ga au us ss si ia an n
q qu ua ad dr ra at tu ur re e, , a an nd d F Fo ou ur ri ie er r a an na al ly ys si is s


Orthogonal functions are sets of functions of an independent variable, say x, given in terms of
an index n, i.e., g
n
(x). The functions g
n
(x) and g
m
(x) are said to be orthogonal on an interval
a < x < b, if

for n m. The notation (g
n
,g
m
) is used when defining linear vector spaces to indicate the
inner or dot product of two vectors, thus, for vectors u and v, (u,v) = u v. If vectors u and v
are orthogonal (in the Euclidean sense), (u,v) = u v = 0. By similarity with vectors, we can
state that if (g
n
,g
m
) = 0 for a set of functions g
n
(x), then functions g
n
and g
m
are orthogonal.

Orthogonal functions have a variety of applications in mathematics. For example, sets of
orthogonal sine and cosine functions can be used to expand a function into what is called a
Fourier series. Solutions to certain types of second-order differential equations constitute sets
of orthogonal functions (e.g., Bessel functions, Lagrange polynomials, Laguerre polynomials,
etc.) which can be used for evaluating integrals through a procedure refer to as Gaussian
quadrature.



S So ol lu ut ti io on ns s t to o s se el le ec ct te ed d s sp pe ec ci if fi ic c s se ec co on nd d- -o or rd de er r
d di if ff fe er re en nt ti ia al l e eq qu ua at ti io on ns s

In this section we present and solve specific types of ordinary differential equations whose
solutions are defined in terms of some classical functions, e.g., Bessels functions, Hermite
polynomials, etc. Most of the functions that solve these differential equations are orthogonal
functions.


The Cauchy or Euler equation

An equation of the form
x
2
(d
2
y/dx
2
) + ax (dy/dx) + by = 0,

where a and b are real constants, is known as the Cauchy or Euler equation. A solution to the
Cauchy equation can be found by assuming that

y(x) = x
n
.

Replacing this function into the differential equation and simplifying results in an auxiliary
algebraic equation:
, 0 ) ( ) ( ) , ( = =
!
b
a
m n m n
dx x g x g g g

3 2001 Gilberto E. Urroz
All rights reserved

If the equation has two different roots, say n
1
and n
2
, then the general solution of this
equation is

y(x) = K
1
x
n
1
+ K
2
x
n
2
.

If b = (1-a)
2
/4, then the equation has a double root n
1
= n
2
= n = (1-a)/2, and the
solution turns out to be
y(x) = (K
1
+ K
2
ln x)x
n
.


Legendres equation

An equation of the form

(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+n (n+1) y = 0,

where n is a real number, is known as the Legendres differential equation. Any solution for
this equation is known as a Legendres function. When n is a nonnegative integer, the solutions
are called Legendres polynomials. Legendres polynomial of order n is given by



where M = n/2 or (n-1)/2, whichever is an integer. The first six Legendre polynomials are

P
0
(x) = 1.0,
P
1
(x) = x,
P
2
(x) = (3x
2
-1)/2,
P
3
(x) =(5x
3
-3x)/2,
P
4
(x) =(35x
4
-30x
2
+3)/8,
P
5
(x) =(63x
5
-70x
3
+15x)/8.


Gamma, log-gamma and di-gamma functions

Notice that the definition of the Legendre polynomials involves the use of factorials. SCILAB,
however, does not provide a factorial function per se. Instead it relies on the use of the
Gamma function, (), defined as

. 0 ) 1 (
2
= + + b n a n
m n
M
m
n
m
n
x
m n m n m
m n
x P
2
0
)! 2 ( )! ( ! 2
)! 2 2 (
) 1 ( ) (

=
"

=
... ...
)! 2 ( )! 1 ( ! 1 2
)! 2 2 (
) ! ( 2
)! 2 (
2
2
+

=
n
n
n
n
x
n n
n
x
n
n

4 2001 Gilberto E. Urroz
All rights reserved
!


=
0
1
, ) ( dt t e
t


which is related to the factorial function for = n integer by:

(n+1) = n!, or (n) = (n-1)!.

SCILAB also provides function gammaln, which calculates the natural logarithm of the Gamma
function (the log-gamma function), i.e.,

gammaln(x) = ln[(x)].


Related to the log-gamma function is the di-gamma function, defined as the derivative of the
log-gamma function:
(z) = d[ln (z)]/dz = (z)/ (z),

which can be calculated by using SCILAB function dlgamma(x).


Legendres polynomials can be generated by using the following user-defined function
legendrepol:

function [pL] = legendrepol(n,var)

//Generates the Legendre polynomial
//of order n in variable var

if n == 0 then
cc = [1];
elseif n == 1 then
cc = [0 1];
else
if modulo(n,2) == 0 then
M = n/2
else
M = (n-1)/2
end;

cc = zeros(1,M+1);
for m = 0:M
k = n-2*m;
cc(k+1)=...
(-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1));
end;
end;

pL = poly(cc,var,'coeff');

//End function legendrepol

The following SCILAB statements generate the first 10 Legendre polynomials as elements of a
column vector p.

-->getf('legendrepol')

5 2001 Gilberto E. Urroz
All rights reserved

-->p = zeros(10,1);

-->for nn = 1:10
--> p(nn) = legendrepol(nn,'x');
-->end;

-->p
p =

! x !
! !
! 2 !
! - .5 + 1.5x !
! !
! 3 !
! - 1.5x + 2.5x !
! !
! 2 4 !
! .375 - 3.75x + 4.375x !
! !
! 3 5 !
! 1.875x - 8.75x + 7.875x !
! !
! 2 4 6 !
! - .3125 + 6.5625x - 19.6875x + 14.4375x !
! !
! 3 5 7 !
! - 2.1875x + 19.6875x - 43.3125x + 26.8125x !
! !
! 2 4 6 8 !
! .2734375 - 9.84375x + 54.140625x - 93.84375x + 50.273438x !
! !
! 3 5 7 9 !
! 2.4609375x - 36.09375x + 140.76562x - 201.09375x + 94.960937x !
! !
! 2 4 6 8 10 !
! - .2460938 + 13.535156x - 117.30469x + 351.91406x - 427.32422x + 180.42578x !



A plot of Legendre polynomials P
1
(x), P
2
(x), P
3
(x), P
4
(x), and P
5
(x), is produced using the
following SCILAB commands. A matrix pp of five columns stores the values of the polynomials,
one per column, corresponding to a vector of values of x, xx.


-->getf('legendrepol')

-->xx = [-1:0.01:1];

-->n = length(xx)
n = 201.

-->for j = 1:5
--> pp(:,j) = horner(p(j),xx');
-->end;

-->plot2d([xx',xx',xx',xx',xx'],[pp(:,1),pp(:,2),pp(:,3),pp(:,4),pp(:,5)],...
-->[1:1:5],'111','n=1@n=2@n=3@n=4@n=5',[-1 -1 1 1 ])

-->xtitle('Legendre polynomials','x','Pn(x)')


6 2001 Gilberto E. Urroz
All rights reserved

The result is the following graph:





The ODE
(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+[n (n+1)-m
2
/(1-x
2
)] y = 0,

has for solution the function

y(x) = P
n
m
(x)= (1-x
2
)
m/2
(d
m
Pn/dx
m
).

This function is referred to as an associated Legendre function.







Bessels equation

The ordinary differential equation

x
2
(d
2
y/dx
2
) + x (dy/dx)+ (x
2
-
2
) y = 0,


7 2001 Gilberto E. Urroz
All rights reserved
where the parameter is a nonnegative real number, is known as Bessels differential
equation. Solutions to Bessels equation are given in terms of Bessel functions of the first
kind of order :



where is not an integer.

If = n, an integer, the Bessel functions of the first kind for n =integer are defined by


For non-integer values , the solution to the Bessel equation is given by

y(x) = K
1
J

(x)+K
2
J
-
(x).

For integer values, the functions J
n
(x) and J
-n
(x) are linearly dependent, since

J
n
(x) = (-1)
n
J
-n
(x),


therefore, we cannot use them to obtain a general function to the equation. Instead, we
introduce the Bessel functions of the second kind defined as


Y

(x) = [J

(x) cos J

(x)]/sin ,

for non-integer , and for n integer, with n > 0, by



where is the so-called Euler constant, defined by



"

=
+
+ +

=
0
2
2
,
) 1 ( ! 2
) 1 (
) (
m
m
m m
m m
x
x x J

"

=
+
+

=
0
2
2
.
)! ( ! 2
) 1 (
) (
m
n m
m m
n
n
m n m
x
x x J
m
m
n m
n m m
m n
n n
x
n m m
h h x x
x J x Y
2
0
2
1
)! ( ! 2
) ( ) 1 (
)
2
(ln ) (
2
) (
+
+
+ + =
"

=
+
+

m
n
m
n m
n
x
m
m n x
2
1
0
2
! 2
)! 1 (



"

..., 0 5772156649 . 0 ] ln
1
...
3
1
2
1
1 [ lim + + + + =

r
r
r


8 2001 Gilberto E. Urroz
All rights reserved

and h
m
represents the harmonic series



For the case n = 0, the Bessel function of the second kind is defined as



With these definitions, a general solution of Bessels equation for all values of is given by

y(x) = K
1
J

(x)+K
2
Y

(x).

In some instances, it is necessary to provide complex solutions to Bessels equations by defining
the Bessel functions of the third kind of order as

H
n
(1)
(x) = J

(x)+iY

(x),
and
H
n
(2)
(x) = J

(x)iY

(x),

These functions are also known as the first and second Hankel functions of order .

In some applications you may also have to utilize the so-called modified Bessel functions of
the first kind of order defined as I

(x)= i
-
J

(ix), where i is the unit imaginary number.


These functions are solutions to the differential equation

x
2
(d
2
y/dx
2
) + x (dy/dx)- (x
2
+
2
) y = 0.


The modified Bessel functions of the second kind,

K

(x) = (/2)[I
-
(x)I

(x)]/sin ,

are also solutions of this ODE.

Bessel functions are available in SCILAB through the following functions:

besselj: Bessel functions of the first kind [J

(x)], with general call:



y = besselj(nu,x)


bessely: Bessel functions of the second kind [Y

(x)], with general call:



.
1
...
3
1
2
1
1
m
h
m
+ + + + =
.
) ! ( 2
) 1 (
)
2
(ln ) (
2
) (
2
0
2 2
1
0 0 #
$
%
&
'
(


+ + =
"

m
m
m
m
m
x
m
h x
x J x Y


9 2001 Gilberto E. Urroz
All rights reserved
y = bessely(nu,x))


besseli: modified Bessel functions of the first kind [I

(x)], with general calls:



y = besseli(nu,x)

y = besseli(nu,x,s)


besselk: modified Bessel functions of the second kind [K

(x)], with general calls:



y = besselk(nu,x)

y = besselk(nu,x,s)


In the calls to functions besselj, bessely, besseli, and besselk, the parameter nu represents the
order of the function , and x is a single non-negative value or vector of non-negative values
where the function is to be evaluated. The calls of functions besseli and besselk may include
the parameter s whose default value is 1. When the value of s is 2, the so-called exponentially
scaled Bessel functions are calculated.

To see a plot of the functions J
n
(x), for n = 0,1,2,3,4, use the following SCILAB commands:


-->xx = [0:0.1:10]; nu = [0,1,2,3,4]; n = length(xx), m = length(nu)

n =
101.
m =
5.

-->bb = zeros(n,m);

-->for j = 1:m
--> bb(:,j) = besselj(nu(j),xx);
-->end;

-->plot2d([xx',xx',xx',xx',xx'],[bb(:,1),bb(:,2),bb(:,3),bb(:,4),bb(:,5)],...
-->[1,2,3,4,5],'111','nu=1@nu=2@nu=3@nu=4@nu=5',[0,-0.6,10,1])

-->xtitle('Bessel functions J_nu(x)','x','J_nu(x)')



10 2001 Gilberto E. Urroz
All rights reserved


Chebyshev polynomials

The functions

T
n
(x) = cos(ncos
-1
x), and U
n
(x) = sin[(n+1) cos
-1
x]/(1-x
2
)
1/2
,

n = 0, 1, are called Chebyshev polynomials of the first and second kind, respectively. The
polynomials T
n
(x) are solutions of the differential equation

(1-x
2
)(d
2
y/dx
2
) x (dy/dx) + n
2
y = 0.

In SCILAB, the function chepol generates the Chebyshev polynomial of the first kind of order n,
given a value of n > 0. The general call to the function is

[Tn]=chepol(n,var)

where n is the order of the polynomial, and var is a SCILAB polynomial variable.

Chebyshev polynomials of the second kind are obtained from:

U
n
(x) = sin(narccos(x))/sin(arccos(x)).


The first four Chebyshev or Tchebycheff polynomials of the first and second kind are:

T
0
(x) = 1.0, U
1
(x) = 1.0, T
1
(x) = x, U
1
(x) =1.0,

T
2
(x) =2x
2
-1, U
2
(x) =2x, T
3
(x) = 4x
3
-3x, U
3
(x) =24x
2
-1.

The polynomials of the first kind can be obtained using SCILAB function chepol as follows:


11 2001 Gilberto E. Urroz
All rights reserved
-->T0 = chepol(0,'x')
T0 =
1

-->T1 = chepol(1,'x')
T1 =
x

-->T2 = chepol(2,'x')
T2 =
2
- 1 + 2x

-->T3 = chepol(3,'x')
T3 =
3
- 3x + 4x

-->T4 = chepol(4,'x')
T4 =
2 4
1 - 8x + 8x


Plots of the Chebyshev polynomials of the first kind for n = 0, 1, 2, 3, 4, are shown below:

-->xx = [-1:0.01:1];
-->y0 = horner(T0,xx);y1 = horner(T1,xx);y2 = horner(T2,xx);
-->y3 = horner(T3,xx);y4 = horner(T4,xx);
-->plot2d([xx',xx',xx',xx',xx'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','T0@T1@T2@T3@T4',[-1,-1,1,1])
-->xtitle('Chebyshev polynomials','x','T_n(x)')






12 2001 Gilberto E. Urroz
All rights reserved

Laguerres equation

Laguerres equation is the second-order, linear ODE of the form

x(d
2
y/dx
2
) +(1x) (dy/dx) + ny = 0.

Laguerre polynomials, defined as


are solutions to Laguerres equation. Laguerres polynomials can also be calculated with:


The term

is the m-th coefficient of the binomial expansion (x+y)
n
. It also represents the number of
combinations of n elements taken m at a time.

The Laguerre polynomials can be generated in SCILAB through the following user-defined
function, laguerrepol:

function [Lp] = laguerrepol(n,var)

//Calculates the Laguerre polynomial
//of order n in polynomial variable var

if n == 0 then
c = [1];

else
c = [];
for m = 0:n
c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))];
end;
end;

Lp = poly(c,var,'coeff');

//end function laguerrepol


The first four Laguerre polynomials are:

-->getf('laguerrepol')
,... 2 , 1 ,
) (
!
) ( , 1 ) (
0
=

= =

n
dx
e x d
n
e
x L x L
n
x n n x
n
.
!
) 1 (
.... ...
4
) 1 (
1
!
) 1 (
) (
2
0
n
n
m
n
m
m
n
x
n
x
n n
x n x
m
n
m
x L

+ +

+ =
)
)
*
+
,
,
-
.

=
"
=
) , (
)! ( !
!
m n C
m n m
n
m
n
=

=
)
)
*
+
,
,
-
.

13 2001 Gilberto E. Urroz
All rights reserved

-->L0 = laguerrepol(0,'x')
L0 =
1

-->L1 = laguerrepol(1,'x')
L1 =
1 - x

-->L2 = laguerrepol(2,'x')
L2 =
2
1 - 2x + .5x

-->L3 = laguerrepol(3,'x')
L3 =
2 3
1 - 3x + 1.5x - .1666667x

-->L4 = laguerrepol(4,'x')
L4 =
2 3 4
1 - 4x + 3x - .6666667x + .0416667x

The following is the plot of the functions L
0
(x), L
1
(x), L
2
(x), L
3
(x) and L
4
(x):

-->x = [-2:0.01:6];
-->y0 = horner(L0,x); y1 = horner(L1,x); y2 = horner(L2,x);
-->y3 = horner(L3,x); y4 = horner(L4,x);
-->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','L0@L1@L2@L3@L4',[-2,-5,6,5])
-->xtitle('Laguerre polynomials','x','L_n(x)')



Webers equation and Hermite polynomials


14 2001 Gilberto E. Urroz
All rights reserved
Webers equation is defined as

d
2
y/dx
2
+(n+1/2-x
2
/4)y = 0,

For n = 0, 1, 2, A particular solution of this equation is given by the function

y(x) = exp(-x
2
/4)H
*
(x/2),

where the function H
*
(x) is the Hermite polynomial:


To calculate Hermite polynomials in SCILAB we will use the following recurrence equation

H
*
n+1
(x) = 2xH
*
n
(x)-2nH
*
n-1
(x)

with H
0
*
(x) = 1, H
1
*
(x) = 2x. A user-defined function to produce the Hermite polynomials is
listed below:

function [Hn] = hermitepol(n,var)

//determines the Hermite polynomial of order n in variable var

H = zeros(1,n+1);
H(1) = poly([1],var,'coeff');
H(2) = poly([0 2],var,'coeff');
for j = 2:n
H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1);
end;
Hn = H(n+1);
//end function hermitepol
The following are the first five Hermite polynomials:

-->getf('hermitepol')

-->H0 = hermitepol(0,'x')
H0 =
1

-->H1 = hermitepol(1,'x')
H1 =
2x

-->H2 = hermitepol(2,'x')
H2 =
2
- 2 + 4x

-->H3 = hermitepol(3,'x')
H3 =
3
- 12x + 8x

-->H4 = hermitepol(4,'x')
,... 2 , 1 ), ( ) 1 ( ) ( * , 1 *
2 2
0
= = =

n e
dx
d
e x H H
x
n
n
x n
n

15 2001 Gilberto E. Urroz
All rights reserved
H4 =
2 4
12 - 48x + 16x


A plot of these polynomials is shown below:

-->x=[-3:0.01:3]; y0 = horner(H0,x); y1 = horner(H1,x);

-->y2 = horner(H2,x); y3 = horner(H3,x); y4 = horner(H4,x);

-->plot2d([x',x',x',x',x'],[y0',y1',y2',y3',y4'],...
-->[1,2,3,4,5],'111','H0@H1@H2@H3@H4',[-3,-30,3,20])

-->xtitle('Hermite polynomials','x','H_n(x)')






I In nt te eg gr ra at ti io on n b by y G Ga au us ss si ia an n q qu ua ad dr ra at tu ur re e

In this section we present techniques of integration known as Gaussian quadrature as well as
other quadrature procedures that can be used for the calculation of improper integrals. Most
of the quadrature formulae require coefficients that can be obtained from orthogonal
polynomials such as Legendre, Laguerre, or Hermite polynomials.


Gaussian quadrature

The idea of Gaussian quadrature for the numerical calculation of integrals consists in
identifying a discrete number of points x
i
, i = 1,2,,n, and a corresponding number of
constants, A
i
, i = 1,2,,n, so that the integral is estimated as

16 2001 Gilberto E. Urroz
All rights reserved


!
"
=

b
a
n
i
i i
x f A dx x f
1
) ( ) ( .


The figure below illustrates the meaning of the coefficients A
i
as the base of rectangles whose
heights are given by f(x
i
). The sum of the areas of the rectangles approximates the integral.



Suppose that we take a = -1 and b = 1 in the integral, and we attempt to determine the values
of x
i
and A
i
for n = 2. We can accomplish this solution by calculating the Gaussian integral

2 ), ( ) ( ) ( ) (
1
1
2 2 1 1
1
= + =
!
"

=
n x f A x f A x f A dx x f
n
i
i i


exactly for the cases f(x) = 1, f(x) = x, f(x) = x
2
, f(x) = x
3
. Replacing these polynomials for f(x)
will produce four equations from which we can solve for the four unknowns, x
1
, x
2
, A
1
, and A
2
.
The equations produced are:

!

+ = = / =
1
1
2 1
, 2 1 1 ) ( For A A dx x f
!

+ = = / =
1
1
2 2 1 1
, 0 ) ( For x A x A dx x x x f
!

+ = = / =
1
1
2
2 2
2
1 1
2 2
,
3
2
) ( For x A x A dx x x x f
!

+ = = / =
1
1
3
2 2
3
1 1
3 3
. 0 ) ( For x A x A dx x x x f

The solution of the system of algebraic equations thus generated is

x
1
= -1/3, x
2
= 1/3, A
1
= 1, A
2
= 1.


17 2001 Gilberto E. Urroz
All rights reserved
The required integral is now written as

!

+
1
1
).
3
1
( )
3
1
( ) ( f f dx x f

The integral calculated with this formula will be exact if f(x) = 1, x, x
2
, or x
3
, and approximate
for any other function. If we want to calculate an integral of function f(x) in the interval [a,b]
we introduce the following change of variable

,
2
a b
b a x
t


=

so that for x = a, t = -1, for x = b, t = 1, and dx =(1/2)(b-a)dt, thus the integral is calculated as

! !

+
+

=
b
a
dt
a b
t
a b
f a b dx x f
1
1
)
2 2
( ) (
2
1
) (

)].
2 2
3
1
( )
2 2
3
1
( [ ) (
2
1 a b a b
f
a b a b
f a b
+
+

+
+
+




General form of Gaussian quadrature

We can generalize the previous result for any integer n by writing

. )
2 2
( ) (
2
1
)
2 2
( ) (
2
1
) (
1
1
1
"
! !
=

+
+


+
+

=
n
i
i i
b
a
a b
x
a b
f A a b dt
a b
t
a b
f a b dx x f

where x
i
, i = 1,2,,n, are the values obtained from

!
"

1
1
1
) ( ) (
n
i
i i
x f A dx x f .

For values of n>2, the values x
i
and A
i
are calculated by solving the 2n equations of the form

. 1 2 ,..., 2 , 1 , 0 , ,..., 2 , 1 ,
1
1
1
= = =
!
"

=
n k n i dx x x A
k
n
i
k
i i


It can be shown that the values of x
i
, i=1,2,,n, are the roots of the Legendre polynomial P
n
(x),
while the coefficients A
i
, i = 1,2,,n, are related to the derivative of the n-th Legendre
polynomial, P
n
(x) = dP
n
/dx, by

.
)] ( ' )[ 1 (
2
2 2
i n i
i
x P x
A

=


18 2001 Gilberto E. Urroz
All rights reserved
Legendres polynomial of order n are the solutions to the Legendres ordinary differential
equation
(1-x
2
)(d
2
y/dx
2
)-2x (dy/dx)+n (n+1) y = 0,

and are given by


where M = n/2 or (n-1)/2, whichever is an integer.

User-defined SCILAB function for calculating Gaussian quadrature

A user-defined SCILAB function, legendrepol, was introduced earlier for generating Legendre
polynomials. The following function, gaussquad, incorporates function legendrepol for the
calculation of Gaussian integrals according to the formulae developed earlier.

function [I] = gaussquad(a,b,f,n)

//Calculates the integral of function f
//in the range [a,b] using the Gaussian
//quadrature approximation with n>=2 intervals
p = legendrepol(n,'x');
xr = roots(p);
A = [];
for j = 1:n
pd = derivat(p);
A = [A 2/((1-xr(j)^2)*(horner(pd,xr(j)))^2)];
end;
tr = ((b-a)/2.*xr)+((b+a)/2);
I = (b-a)/2*A*f(tr);
//end of function gaussquad
//================================================
function [pL] = legendrepol(n,var)
//Generates the Legendre polynomial
//of order n in variable var
if n == 0 then
cc = [1];
elseif n == 1 then
cc = [0 1];
else
if modulo(n,2) == 0 then
M = n/2
else
M = (n-1)/2
end;
cc = zeros(1,M+1);
for m = 0:M
k = n-2*m;
cc(k+1)=...
(-1)^m*gamma(2*n-2*m+1)/(2^n*gamma(m+1)*gamma(n-m+1)*gamma(n-2*m+1));
m n
M
m
n
m
n
x
m n m n m
m n
x P
2
0
)! 2 ( )! ( ! 2
)! 2 2 (
) 1 ( ) (

=
"

=
... ...
)! 2 ( )! 1 ( ! 1 2
)! 2 2 (
) ! ( 2
)! 2 (
2
2
+

=
n
n
n
n
x
n n
n
x
n
n

19 2001 Gilberto E. Urroz
All rights reserved
end;
end;
pL = poly(cc,var,'coeff');
//End function legendrepol


Application of function gaussquad

To illustrate the use of function gaussquad we will use it to integrate the function f(x) =
e
x
/(1+x
2
)
2
for n = 2, 3, 4, 5, 6, 7, 8 , 9, 10, 12, 14, 16, 18, and 20. A plot of the function in the
range of integration is presented below.

-->deff('[y]=f(x)','y=exp(x)./(1+x^2)^2')
-->x=[0:0.05:10];y=f(x);plot(x,y,'x','y','f(x)=exp(x)/(1+x^2)^2')




The following table shows the values of the integral:

-->Res = [ ];

-->for j=1:length(nn)
--> Res=[Res; [nn(j),gaussquad(0,10,f,nn(j))]];
-->end;

-->disp(' n Int'), disp(Res)




n Int

! 2. 4.7168792 !
! 3. 5.7578069 !
! 4. 5.9621965 !
! 5. 5.8817127 !
! 6. 5.8034783 !
! 7. 5.7774564 !
! 8. 5.778712 !
! 9. 5.7846526 !
! 10. 5.7877236 !
! 12. 5.7879883 !
! 14. 5.7876393 !

20 2001 Gilberto E. Urroz
All rights reserved
! 16. 5.787655 !
! 18. 5.7876657 !
! 20. 5.7876644 !



Improper integrals

Improper integrals is the name given to definite integrals that have infinite limits, for example:

! ! !


+
= =
+
=
1
2
0
2
2
1
2
2
1
.
1
, ,
1 x
dx
I
x
dx
I
x
dx
I


Symbolic integration of improper integrals is accomplished by using limits of the definite
integral as a dummy variable, , approaches plus or minus infinity, e.g.,

.
4
3
)
2
(
4
)) arctan(
4
( lim | ) arctan( lim
1
lim
1
1
1
2
1
2
1


= = = =
+
=
+
=

! !
x
x
dx
x
dx
I


Gauss-Laguerre formulae for improper integrals

Gauss-Laguerre formulae can be used to numerically evaluate integrals of the form shown
below through a procedure similar to Gaussian quadrature:

!
"


0
1
. ) ( ) (
n
i
i i
x
x g A dx x g e

The values of x
i
, i=1,2,,n, are the roots of the Laguerre polynomial on order n, L
n
(x), and the
coefficients A
i
, i=1,2,,n, are calculated from

.
)] ( ' [
1
)] ( [ ) 1 (
2 2
1
2
i n i n
i
i
x L x x L n
x
A

=
+
=
+



Laguerre polynomials are the solutions to the Laguerres differential equation

x(d
2
y/dx
2
) +(1x) (dy/dx) + ny = 0,

and are given by



where
) , (
)! ( !
!
m n C
m n m
n
m
n
=

=
)
)
*
+
,
,
-
.
.
!
) 1 (
.... ...
4
) 1 (
1
!
) 1 (
) (
2
0
n
n
m
n
m
m
n
x
n
x
n n
x n x
m
n
m
x L

+ +

+ =
)
)
*
+
,
,
-
.

=
"
=

21 2001 Gilberto E. Urroz
All rights reserved

is the binomial coefficient.

To calculate an improper integral of a general function, f(x), we will use the following formula:

!
"
!

=
0
1
0
. ) ( )} ( { ) (
n
i
i
x
i
x x
x f e A dx x f e e dx x f
i


Of course, the indefinite integral shown can be evaluated only if the integrand decreases
quickly towards zero as x grows without bound. The integral in this case is said to be
convergent.


SCILAB user-defined function for calculating Gauss-Laguerre quadrature

Earlier in this chapter we defined function laguerrepol to generate Laguerre polynomials. The
following function, laguerrequad, which incorporates function laguerrepol, is used to calculate
the improper integral shown above.

function [I] = laguerrequad(f,n)
//Calculates the improper integral of function f(x)
//in the interval [0,infinity] using Gauss-Laguerre
//quadrature with n >= 2 intervals
p = laguerrepol(n,'x');
pd = derivat(p);
pnp1 = laguerrepol(n+1,'x');

xr = roots(p);

A=[];
for i = 1:n
A = [A xr(i)/((n+1)^2*(horner(pnp1,xr(i)))^2)];
end;

I = A*(exp(xr).*f(xr));


//end of function laguerrequad
//===============================================================
function [Lp] = laguerrepol(n,var)

//Calculates the Laguerre polynomial
//of order n in polynomial variable var

if n == 0 then
c = [1];

else
c = [];
for m = 0:n
c = [c (-1)^m*gamma(n+1)/((gamma(m+1))^2*gamma(n-m+1))];
end;
end;

Lp = poly(c,var,'coeff');


22 2001 Gilberto E. Urroz
All rights reserved
//end function laguerrepol

Application of function laguerrequad to calculate improper integral

We use function laguerrequad to calculate the improper integral

!

+
=
0
2
1
.
1 x
dx
I

The following SCILAB commands define the function to be integrated, f(x) = 1/(1+x
2
). A plot of
the function is shown next.

-->deff('[y]=f(x)','y=(1+x^2)^(-1)')

-->x=[0:0.1:10];y=f(x);plot(x,y,'x','y','f(x)=1/(1+x^2)')




The following commands calculate the improper integral for values of n=2,4,,30:

-->getf('laguerrequad')

-->Res=[]; for j=2:2:30, Res = [Res; [j laguerrequad(f,j)]]; end;

-->disp(Res)

! 2. 1.493257 !
! 4. 1.5011895 !
! 6. 1.5188948 !
! 8. 1.53376 !
! 10. 1.5425763 !
! 12. 1.5478021 !
! 14. 1.5512373 !
! 16. 1.5537377 !
! 18. 1.555679 !
! 20. 1.5572394 !
! 22. 1.558519 !
! 24. 1.5595837 !
! 26. 1.5604823 !
! 28. 1.5612765 !
! 30. 1.567911 !

23 2001 Gilberto E. Urroz
All rights reserved


The exact value of the integral is /2:

-->%pi/2
ans =

1.5707963

Gauss-Hermite formulae for improper integrals

Gauss-Hermite formulae can be used to numerically evaluate integrals of the form shown below
through a procedure similar to Gaussian or Gauss-Laguerre quadrature:

!
"

. ) ( ) (
1
2
n
i
i i
x
x g A dx x g e

The values of x
i
, i=1,2,,n, are the roots of the Hermite polynomial on order n, H
n
(x), and the
coefficients A
i
, i=1,2,,n, are calculated from

.
)] ( ' [
! 2
)] ( [
! 2
2
1
2
1
2
1
i n
n
i n
n
i
x H
n
x H n
n
A

+

= =


Hermite polynomials are defined as:



To calculate an improper integral of a general function, f(x), we will use the following formula:

!
"
!


= . ) ( )} ( { ) (
1
2 2 2
n
i
i
x
i
x x
x f e A dx x f e e dx x f
i


The indefinite integral shown can be evaluated numerically only if it is convergent.

SCILAB user-defined function for calculating Gauss-Hermite quadrature

Earlier in the chapter we defined function hermitepol to generate Hermite polynomials. The
following function, hermitequad, which incorporates function hermitepol, is used to calculate
the improper integral shown above.

function [I] = hermitequad(f,n)

//Calculates the improper integral of function
//f(x) in the range [-infinity, infinity] using
//n intervals and the Gauss-Hermite formulae
p = hermitepol(n,'x');
,... 2 , 1 ), ( ) 1 ( ) ( , 1
2 2
0
= = =

n e
dx
d
e x H H
x
n
n
x n
n

24 2001 Gilberto E. Urroz
All rights reserved
pnp1 = hermitepol(n-1,'x');
xr = roots(p);
A = [];
for j = 1:n
A = [A 2^(n-1)*gamma(n+1)*sqrt(%pi)/(n^2*horner(pnp1,xr(j))^2)];
end;
I = A*(exp(xr.^2).*f(xr));
//end function hermitequad
//---------------------------------------------
function [Hn] = hermitepol(n,var)
//determines the Hermite polynomial
//of order n in variable var
H = zeros(1,n+1);
H(1) = poly([1],var,'coeff');
H(2) = poly([0 2],var,'coeff');
for j = 2:n
H(j+1) = poly([0 2],var,'coeff')*H(j)-2*(j-1)*H(j-1);
end;
Hn = H(n+1);
//end function hermitepol
Application of function hermitequad for calculating improper integrals

We use function hermitequad to calculate the integral of the function f(x) = exp(-|x|). A plot
of the function is shown next:

-->deff('[y]=f(x)','y=exp(-abs(x))')
-->x=-4:0.1:4;y=f(x);plot(x,y,'x','y','f(x)=exp(-x^2)')

Values of the improper integral for n = 2, 4, , 30, are shown next:

-->getf('hermitequad')
-->Res=[]; for j = 2:2:30, Res=[Res; [j, hermitequad(f,j)]]; end;
-->Res
Res =

! 2. 1.4408859 !
! 4. 1.730544 !
! 6. 1.8420198 !
! 8. 1.8963115 !
! 10. 1.9264848 !
! 12. 1.9447991 !
! 14. 1.9566605 !
! 16. 1.9647431 !
! 18. 1.9704841 !

25 2001 Gilberto E. Urroz
All rights reserved
! 20. 1.9747063 !
! 22. 1.9779055 !
! 24. 1.9803932 !
! 26. 1.9823717 !
! 28. 1.9839769 !
! 30. 1.9853024 !

Integrals involving sine and cosine terms - Filons formulae

Filons formulae is used to evaluate integrals involving a single sine or cosine term. The
general formulae are

, ] )} sin( ) ( ) sin( ) ( { [ ) cos( ) (
0
0 0
!
+ + =
n
x
x
o e n n
kx x f kx x f x dx kx x f

, ] )} cos( ) ( ) cos( ) ( { [ ) sin( ) (
0
0 0
!
+ + =
n
x
x
o e n n
kx x f kx x f x dx kx x f

with
x = (x
n
-x
0
)/n, = kx,
= (
2
+ sin(2 )/2 - 2 sin
2
())/
3
,
= 2{ (1+cos
2
() - sin(2 )}/
3
,
= 4(sin()- cos())/
3
,
"

=
+ + =
2
,... 6 , 4 , 2
0 0
), cos( ) ( )} cos( ) ( ) cos( ) ( {
2
1
n
i
i i n n e
kx x f kx x f kx x f
"

=
=
1
,... 5 , 3 , 1
), cos( ) (
n
i
i i o
kx x f
"

=
+ + =
2
,... 6 , 4 , 2
0 0
), sin( ) ( )} sin( ) ( ) sin( ) ( {
2
1
n
i
i i n n e
kx x f kx x f kx x f
and
"

=
=
1
,... 5 , 3 , 1
). sin( ) (
n
i
i i o
kx x f

The values of x
i
, i=0,1,2,,n, are calculated using x
i
= x
0
+ix. Values x
0
and x
n
are the limits
of integration, and n is required to be an even number.



SCILAB user-defined function for calculating integrals with Filons formulae

The following function, filonquad, can be used to calculate the integrals involving sine or
cosine function as described above. The first argument in the function call indicates which

26 2001 Gilberto E. Urroz
All rights reserved
type of trigonometric function, sine (sin) or cosine (cos), is to be used in the integral. The
other arguments are the lower and upper limits of integration, x0 and xn, the name of the
function, f, and the number of (even) sub-intervals, n:

function [I] = filonquad(ftype,k,x0,xn,n,f)

//Calculates the integral of the function f(x)*cos(k*x), if ftype = 'cos' or of
//f(x)*sin(k*x), if ftype = 'sin' in the interval [x0,xn] using Filon's
//integration formulae
if modulo(n,2) <> 0 | n<=2 then
error('filonquad - n must be an even number larger than 2');
abort;
end;
Dx = (xn-x0)/n;
x = [x0+Dx:Dx:xn];
xe = [x(2):2*Dx:x(n-2)];
xo = [x(1):2*Dx:x(n-1)];
theta = k*Dx;
alpha = (theta^2+theta*sin(2*theta)/2-2*sin(theta)^2)/theta^3;
beta = 2*(theta*(1+cos(theta)^2)-sin(2*theta))/theta^3;
gama = 4*(sin(theta)-theta*cos(theta))/theta^3;
fxe=[];coskxe=[];sinkxe=[];
for j = 1:length(xe)
fxe = [fxe f(xe(j))];
coskxe = [coskxe cos(k*xe(j))];
sinkxe = [sinkxe sin(k*xe(j))];
end;
fxo=[];coskxo=[];sinkxo=[];
for j = 1:length(xo)
fxo = [fxo f(xo(j))];
coskxo = [coskxo cos(k*xo(j))];
sinkxo = [sinkxo sin(k*xo(j))];
end;
if ftype == 'cos' then
kappa_e = 0.5*(f(x0)*cos(k*x0)+f(xn)*cos(k*xn)) + fxe*coskxe';
kappa_o = fxo*coskxo';
I = Dx*(alpha*(f(xn)*sin(k*xn)-f(x0)*sin(k*x0))+beta*kappa_e+gama*kappa_o);
else
sigma_e = 0.5*(f(x0)*sin(k*x0)+f(xn)*sin(k*xn)) + fxe*sinkxe';
sigma_o = fxo*sinkxo';
I = Dx*(alpha*(f(x0)*cos(k*x0)-f(xn)*cos(k*xn))+beta*sigma_e+gama*sigma_o);
end;

//end of function filonquad

Application of function filonquad

The following SCILAB commands are used to generate tables of the integrals calculated through
Filons integral formulae with function filonquad. The function f(x) in the integrals is f(x) =
exp(-x), and it is integrated between x0 = 2 and xn = 4.5. The parameter k in sin(kx) or
cos(kx) takes values k = 1,2,3,4,5, one value of k for each column of the tables, while the
number of sub-intervals takes values n = 4,6,,20, corresponding to each row in the tables.
The first table corresponds to integrals involving sine functions while the second table
corresponds to integrals containing cosine functions.

-->deff('[y]=f(x)','y=exp(-x)') //define function

-->x0 = 2; xn = 4.5; //define limits of integration

27 2001 Gilberto E. Urroz
All rights reserved
-->kk = [1:1:5]; //define values of k = 1,2,3,4,5
-->nn = [4:2:20] //define values of n = 4,6,,20
-->mk = length(kk); mn = length(nn);
-->Res=zeros(mn,mk); //Table for function with sin(k*x)

-->for i = 1:mn
--> for j = 1:mk
--> Res(i,j) = filonquad('sin',kk(j),x0,xn,nn(i),f);
--> end;
-->end;

-->Res
Res =

! .0403248 - .0530721 .0322401 .0026027 - .0234170 !
! .0400408 - .0527902 .0322865 .0021268 - .0226910 !
! .0399930 - .0527517 .0323124 .0020419 - .0226145 !
! .0399799 - .0527421 .0323205 .0020210 - .0226019 !
! .0399752 - .0527387 .0323235 .0020140 - .0225983 !
! .0399732 - .0527374 .0323248 .0020110 - .0225970 !
! .0399723 - .0527367 .0323255 .0020096 - .0225964 !
! .0399718 - .0527364 .0323258 .0020089 - .0225961 !
! .0399715 - .0527362 .0323260 .0020085 - .0225959 !

-->Res=zeros(mn,mk); //Table for function with sin(k*x)

-->for i = 1:mn
--> for j = 1:mk
--> Res(i,j) = filonquad('cos',kk(j),x0,xn,nn(i),f);
--> end;
-->end;

-->Res
Res =

! - .0939334 .0268378 .0269223 - .0357034 .0095905 !
! - .0939411 .0270685 .0264442 - .0350895 .0090338 !
! - .0939457 .0271117 .0263818 - .0350622 .0090964 !
! - .0939472 .0271238 .0263666 - .0350587 .0091129 !
! - .0939479 .0271282 .0263614 - .0350579 .0091186 !
! - .0939482 .0271300 .0263592 - .0350577 .0091209 !
! - .0939483 .0271310 .0263582 - .0350576 .0091220 !
! - .0939484 .0271314 .0263577 - .0350576 .0091226 !
! - .0939484 .0271317 .0263574 - .0350575 .0091229 !

O Or rt th ho og go on na al l f fu un nc ct ti io on ns s a an nd d s se er ri ie es s e ex xp pa an ns si io on ns s

The concept of orthogonal functions apply to the case of functions of an independent variable x
often defined in terms of an index n over a certain interval, for example, g
n
(x) = sin(nx) on
the interval (0,1), with n = 1, 2, 3, The functions g
n
(x) and g
m
(x) are said to be orthogonal
on an interval a < x < b, if


for n m.
, 0 ) ( ) ( ) , ( = =
!
b
a
m n m n
dx x g x g g g

28 2001 Gilberto E. Urroz
All rights reserved

If n = m, then the resulting integral represents the square of a quantity, ||g
n
||, known as the
norm of g
n
(x):

Example 1 -- For the functions g
n
(x) = sin(nx) on the interval (0,1), you can check that the
integral

evaluates to

This latter expression is equal to zero for n m (n and m integers), and it is not defined if m =
n. For the case m = n, we can calculate the value of the integral by using:

!
1
0
2
) ( sin dx x n

which evaluates to



If n is an integer, this result evaluates to .

To verify the previous integrals for a few combinations of integer values of n and m we can use
the following SCILAB commands. First, we define a function f1(x) corresponding to the
integrand sin(nx)sin(mx):

-->deff('[ff]=f1(x)','ff=sin(n.*%pi.*x).*sin(m.*%pi.*x)')

Next, we calculate the integrals of f1(x) for combinations of values of n and m, verifying that
the integrals are zero (i.e., nearly zero, for numerical integrals) for n m, and equal to for n
= m:

-->m=2;n=5;intg(0,1,f1,0.01)
ans = 8.951E-17

-->m=3;n=2;intg(0,1,f1,0.01)
ans = 4.339E-18

-->m=3;n=3;intg(0,1,f1,0.01)
. 0 ] ) ( [ ) , ( || ||
2 2
= =
!
dx x g g g g
b
a
n n n n
d
0
1
2
0
1
( ) sin n x ( ) sin m x x

+ n ( ) cos n ( ) sin m m ( ) sin n ( ) cos m


( ) + n m ( ) + n m
1
2
+ ( ) cos n ( ) sin n n
n

29 2001 Gilberto E. Urroz
All rights reserved
ans = .5

-->m=4;n=4;intg(0,1,f1,0.01)
ans = .5

Series expansions with orthogonal functions

If a function f(x) can be written as a series expansion of orthogonal functions g
n
(x), i.e.,

over the interval a < x < b, then we can use the property of orthogonality to obtain the
coefficients C
n
of the series as follows:




Multiply the function by g
m
(x), i.e.,


Integrate the resulting expression over the interval a < x < b, i.e.,



Because all integrals in the summation in the right-hand side of the expression above are
zero, except for m = n, the expression simplifies to:



The coefficient C
n
is calculated from the expression above by replacing m with n, i.e.,



Example 2 -- For the orthogonal functions g
n
(x) = sin(nx), 0<x<1, the corresponding series is a
Fourier sine series, i.e.,
"

=
=
1
), ( ) (
n
n n
x g C x f
"

=
=
1
). ( ) ( ) ( ) (
n
m n n m
x g x g C x g x f
"
! !

=
=
1
. ) ( ) ( ) ( ) (
n
b
a
m n n
b
a
m
dx x g x g C dx x g x f
! !
=
b
a
m m
b
a
m
dx x g C dx x g x f
2
)] ( [ ) ( ) (
=


=
!
!
|| ||
) , (
)] ( [
) ( ) (
2
n
n
b
a
n
b
a
n
n
g
g f
dx x g
dx x g x f
C

30 2001 Gilberto E. Urroz
All rights reserved
and,



Suppose that we want to find a few coefficients for the series expansion of the function f(x) =
x
2
for the orthogonal functions g
n
(x) = sin(nx), 0<x<1, we can use the following SCILAB
commands. First, we define the function f(x):

-->deff('[y]=f(x)','y=x^2')

Next, we define a function representing the integrand in the coefficient:

-->deff('[gg]=g(x)','gg=f(x)*sin(n*%pi*x)')

Finally, we define a function C(n) representing the coefficients C
n
:

-->deff('[CC]=C(n)','CC=2*intg(0,1,g,0.01)')

The following vector represents the first five coefficients of the series:

-->[C(1) C(2) C(3) C(4) C(5)]
ans =

! .3786075 - .3183099 .2026506 - .1591549 .1252599 !

Orthonormal functions

A set of normal functions
n
(x) on an interval a < x < b is said to be orthonormal if

Thus, the norm of an orthonormal function is ||
n
|| = 1.0.

Given a set of orthogonal functions g
n
(x) on an interval a<x<b, you can generate the
corresponding orthonormal set as

n
(x) = g
n
(x)/||g
n
||.

Example 1 -- Determine the orthonormal functions corresponding to the set of orthogonal
functions
g
n
(x) = cos(nx),
on the interval 0 < x < 1.

First, to verify that the set of functions given is orthogonal, check that the integral
"

=
=
1
), sin( ) (
n
n
x n C x f
!
!
!
!
=

=


=
1
0
1
0
1
0
2
1
0
. ) sin( ) ( 2
2 / 1
) sin( ) (
) ( sin
) sin( ) (
dx x n x f
dx x n x f
dx x n
dx x n x f
C
n

!
3
4
5
=

= =
b
a
m n m n
n m when
n m when
dx x g x g
, 1
, 0
) ( ) ( ) , (
d
0
1
2
0
1
( ) cos n x ( ) cos m x x

31 2001 Gilberto E. Urroz
All rights reserved


evaluates to zero for m n. Therefore, the functions g
n
(x) are orthogonal.

The norm of the function g
n
(x) is calculated by using




You can check that the latter integral evaluates to . Thus, ||g
n
||
2
= , and ||gn|| = 1/2.
The orthonormal functions will be defined, therefore, as

n
(x) = g
n
(x)/||g
n
|| = 2cos(nx).


Orthogonality with respect to a weight function

Some functions are orthogonal with respect to a weight function p(x), so that


In this case, the norm squared of g
n
(x) is given by


If a function f(x) can be expanded in terms of the functions g
n
(x), i.e.,



the coefficients of the series are given, in this case, by




. 0 ) ( ) ( ) ( =
!
b
a
m n
dx x g x g x p
. 0 ] ) ( [ ) ( || ||
2 2
=
!
dx x g x p g
b
a
n n
=


=
!
!
!
b
a
n
n
b
a
n
b
a
n
n
dx x g x f x p
g
dx x g x p
dx x g x f x p
C ) ( ) ( ) (
|| ||
1
)] ( [ ) (
) ( ) ( ) (
2
"

=
=
1
), ( ) (
n
n n
x g C x f
d
0
1
2
2
0
1
( ) cos n x
2
x

32 2001 Gilberto E. Urroz
All rights reserved
Example 1 -- The Chebyshev polynomials T
1
(x) = x, T
2
(x) = 2x
2
-1, on the interval 1 < x < 1, are
orthogonal with respect to the weight function p(x) = (1-x
2
)
-1/2
, which can be demonstrated by
calculating the integral



You can check that the integral evaluates to zero by using SCILAB as follows. First, we define a
function ff(x) representing the integrand in the previous integral.


-->deff('[y] = f(x)','y = x.*(2.*x.^2-1)./sqrt(1-x.^2)')

Next, we generate points in the interval (-1,1), not including the extremes of the interval, i.e.,
x = -1 and x = 1, where the integrand goes to infinity.

-->xx=[-0.99:0.01:0.99]; yy = f(xx);

A plot of the integrand is produced by:

-->plot(xx,yy);xtitle('integrand for Chebyshev','x','integrand')



With the vectors xx and yy generated before, we can calculate the integral for the Chebyshevs
polynomials shown earlier by using function inttrap. The result, as expected, is zero:

-->inttrap(xx,yy)
ans =m- 9.021E-17

The Sturm-Liouville problem

d
0
1
2
2
2
2
2
2
-1
1
x ( ) 2 x
2
1
1 x
2
x

33 2001 Gilberto E. Urroz
All rights reserved
Although the details of the proof are not included in this book, you can verify that Bessel
functions, Laguerre polynomials, Hermite polynomials, and Chebyshev (or Tchebycheff)
polynomials are all sets of orthogonal functions. In fact, all of these functions can be obtained
from solving the so-called Sturm-Liouville problem defined by the differential equation



to be solved in the interval a < x < b, and subject to the boundary conditions

1
y(a) +
1
y(a) = 0,
and

2
y(b) +
2
y(b) = 0.

By selecting the values of the functions r(x), p(x), and q(x), as well as the constants
1
,
2
,
1
,
and
2
, we can generate, out of the general ODE shown above, the Bessel equation, Laguerres
equation, etc. The solutions to the Sturm-Liouville ODE are given in terms of different values
of the parameter , known as the eigenvalues of the problem, while the solutions themselves
are known as the eigenfunctions.

F Fo ou ur ri ie er r A An na al ly ys si is s

Fourier analysis involves the expansion of periodic functions into infinite series of sine and
cosines. In general, any continuous function can be expanded into a series of sine functions
(Fourier sine series), cosine functions (Fourier cosine functions), or a series combining sine and
cosine functions (Fourier series). Expansion of functions into Fourier series finds applications
in areas related to any type of wave propagation, be it electromagnetic waves or surface waves
in waters.

In practice, we evaluate Fourier series using only a finite number of sine and/or cosine
functions representing a discrete number of frequencies. Fourier analysis can be extended to a
continuous frequency domain through the use of Fourier transforms. Fourier transforms have
applications in the analysis of periodic and non-periodic signals such as communication signals,
turbulence velocity variations, etc. Most measured signals include a finite number of
(typically) equally spaced data points suitable for analysis through discrete Fourier transforms.

Sine and cosine functions belong to a family of so-called orthogonal functions. Properties of
such functions are presented in the following section.


Fourier series
Fourier series are series involving sine and cosine functions that are typically used to expand
periodic functions. A function f(x) is said to be periodic of period L if f(x+L) = f(x). For
example, because sin(x+2

) = sin x, and cos(x+2

) = cos x, the functions sin and cos are 2

-
periodic functions. If two functions f(x) and g(x) are periodic of period L, then their linear
combination h(x) = af(x) + bg(x), where a and b are constant, is also periodic of period L.
The figure below shows a periodic function of period L = 2:
, 0 )] ( ) ( [ ) ( = + +
#
$
%
&
'
(
y x p x q
dx
dy
x r
dx
d


34 2001 Gilberto E. Urroz
All rights reserved

-->deff('[y]=f(x)','y=2.*sin(2.*%pi.*x)-3.*cos(%pi.*x)')

-->x = [0:%pi/100;2*%pi]; y = f(x);

-->plot(x,y,'x','y','Example of a periodic function')




To verify that the function is indeed periodic, we try the following evaluations:

-->f(0),f(2),f(4)
ans = - 3.
ans = - 3.
ans = - 3.

Fourier sine series

Fourier sine series are such that,

= ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
n x
L


Because the sine functions
.
-
,
,
+
*
)
)
sin
i x
L
and
.
-
,
,
+
*
)
)
sin
j x
L
are orthogonal, i.e.,

= d
0
1
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
i x
L
.
-
,
,
+
*
)
)
sin
j x
L
x 0 , for
i j
,
then

= d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x b
n
d
0
1
2
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
n x
L
2
x =
L b
n
2
,

from which it follows that


35 2001 Gilberto E. Urroz
All rights reserved
b
n
=
2
L
d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x

for n = 1, 2, 3,....

Consider the function
= ( ) f x x x
2
. This function is obviously not periodic, as shown in the
figure below.

-->deff('[y]=f(x)','y=x-x^2')

-->x=[-2:0.1:3];y=f(x);

-->plot(x,y,'x','y','y=x-x^2')




However, we can assume that it is periodic with period, say, L = 2, for the purpose of
expanding the function in a Fourier sine series. The next SCILAB commands are used to
calculate a Fourier sine series for the function. Notice that we define two functions, first a
function g(x) which represents the integrand f(x)sin(nx/L), and then a function b(n) that
calculates the coefficient b
n
for the Fourier sine series that approximates f(x).

-->deff('[w]=g(x)','w=f(x)*sin(n*%pi*x/L)')

-->deff('[bb]=b(n)','bb=(2/L)*int(0,L,g)')

We use the following forend loop to generate the first 20 coefficients for the Fourier
expansion (i.e., n = 0, 1, 2, , 19).

-->c = []; for j=1:20, c = [c b(j-1)]; end;

-->c
c =
column 1 to 6

! 0. - .2411904 .6366198 - .3861891 .3183099 - .2463915 !

column 7 to 11


36 2001 Gilberto E. Urroz
All rights reserved
! .2122066 - .1788825 .1591549 - .1400554 .1273240 !

column 12 to 16

! - .1149737 .1061033 - .0974717 .0909457 - .0845768 !

column 17 to 20

! .0795775 - .0746864 .0707355 - .0668621 !

To calculate the values for the series expansion we will use the following function fouriersine.

function [y] = fouriersine(c,x,L)

//Calculates the Fourier sine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);

y = [];

for i = 1:n
yy = 0;
for j = 1:m
yy = yy + c(j)*sin((j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriersine

A call to fouriersine for L = 2 is used to produce the following plot.

-->getf('fouriersine')
-->x=[-2:0.01:3];y=f(x);yf=fouriersine(c,x,L);
-->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -6 3 6])
-->xtitle('Fourier sine series for y = x-x^2','x','y')




37 2001 Gilberto E. Urroz
All rights reserved
The plot shows the original function (continuous line) and the approximation through 20 terms
of the Fourier sine series (crosses). The approximation is somewhat acceptable in the range 0
< x < L, which corresponds to the artificial period L = 2 that we imposed on the Fourier series.
Still, the sinusoidal character of the fitting is impossible to eliminate, at least with the 20
components shown. Notice that the Fourier sine series fitting makes the resulting function
periodic of period L = 2, as expected. Also, notice that at x = 2, the fitted function shows
some wild oscillations. This latter effect is known as the Gibbs phenomenon and it is
characteristic of Fourier series. We will see more examples of this phenomenon in other
applications in this chapter.

The following graph represents the Fourier series for the function y = x-x
2
using 40 components
in the series. Obviously, the fitting is much better than when using 20 components only. The
Gibbs phenomenon is still present at x = 2, but up to that value the Fourier sine series fitting is
excellent for this case.



The following SCILAB commands are used to produce graphs of the original function as well as
the Fourier sine series fitting corresponding to 5, 10, 20, 30, and 40 components.

-->x = [-2:0.01:3]; y = f(x);
-->c5=c(1:5); yf5 = fouriersine(c5,x,L);
-->c10=c(1:10); yf10 = fouriersine(c10,x,L);
-->c20=c(1:20); yf20 = fouriersine(c20,x,L);
-->c30=c(1:30); yf30 = fouriersine(c30,x,L);
-->c40=c(1:40); yf40 = fouriersine(c40,x,L);
-->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],[1:1:6],'011','
',[-1.5 -2.5 2.5 2.5])
-->xtitle('Fourier sine series using 5,10,20,30,40 components','x','y')


38 2001 Gilberto E. Urroz
All rights reserved

Fourier cosine series

Fourier cosine series are such that,

= ( ) f x
"
= n 0

a
n
.
-
,
,
+
*
)
)
cos
n x
L
.


Because the cosine function
.
-
,
,
+
*
)
)
cos
i x
L
and
.
-
,
,
+
*
)
)
cos
j x
L
are also orthogonal, i.e.,

= d
0
1
2
2
2
2
0
L
.
-
,
,
+
*
)
)
cos
i x
L
.
-
,
,
+
*
)
)
cos
j x
L
x 0 , for
i j
,
then
= d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x a
n
d
0
1
2
2
2
2
2
0
L
.
-
,
,
+
*
)
)
sin
n x
L
2
x =
L a
n
2
,
from which it follows that

a
n
=
2
L
d
0
1
2
2
2
2
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x

for n = 1, 2, 3, ... For n = 0, the corresponding coefficient is:

a
0
=
1
L
d
0
1
2
0
L
( ) f x x
.

As an example, to determine the coefficient of the cosine series that fits the function f(x) = (1-
x)(x+1)(x+2) assuming that it is periodic with period L = 1, use the following SCILAB commands:


-->deff('[y]=f(x)','y=(1-x).*(x+1).*(x-2)')


39 2001 Gilberto E. Urroz
All rights reserved
-->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)')

-->deff('[aa]=a(n)',...
['if n==0 then';'aa=intg(0,L,f)/L';'else';'aa=(2/L)*intg(0,L,g)/L';'end'])

-->c=[];for j=1:40, c=[c a(j-1)]; end;

-->c
c =

column 1 to 5

! - 1.0833333 - .8543107 .0506606 - .0705892 .0126651 !

column 6 to 10

! - .0247113 .0056290 - .0125093 .0031663 - .0075428 !


column 11 to 15

! .0020264 - .0050410 .0014072 - .0036058 .0010339 !

column 16 to 20

! - .0027068 .0007916 - .0021065 .0006254 - .0016859 !

column 21 to 25

! .0005066 - .0013798 .0004187 - .0011501 .0003518 !

column 26 to 30

! - .0009733 .0002998 - .0008344 .0002585 - .0007232 !

column 31 to 35

! .0002252 - .0006329 .0001979 - .0005585 .0001753 !

column 36 to 40

! - .0004964 .0001564 - .0004442 .0001403 - .0003998 !


Function fouriercosine, listed below, is used to calculate the Fourier series given the
coefficients c, a vector of values of x, and the period of the function L.

function [y] = fouriercosine(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);

y = [];
for i = 1:n

40 2001 Gilberto E. Urroz
All rights reserved
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos((j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriercosine

A plot of the original function and the Fourier cosine series for 5, 10, 20, 30, and 40
components are shown in the following plot. The graph shows the original function and the
different Fourier series to be practically indistinguishable from each other in the range 0 < x <
1.

-->x = [0:0.01:1]; y = f(x);

-->c5=c(1:5);yf5=fouriercosine(c5,x,L);
-->c10=c(1:10);yf10=fouriercosine(c10,x,L);
-->c20=c(1:20);yf20=fouriercosine(c20,x,L);
-->c30=c(1:30);yf30=fouriercosine(c30,x,L);
-->c40=c(1:40);yf40=fouriercosine(c40,x,L);

-->plot2d([x' x' x' x' x' x'],[y' yf5' yf10' yf20' yf30' yf40'],...
-->[1,2,3,4,5,6],'011',' ',rect)
-->xtitle('Fourier cosine series - 5,10,20,30,40 components','x','y')



The following figure shows the original function (continuous line) and the Fourier cosine series
(crosses) with 40 components in the range -0.5 < x < 1.5. It is obvious that the Fourier cosine
series fits the original function very well in the range 0 < x < 1. Outside of that range, the
function shows periodicity.

-->x = -0.5:0.1:1.5; y = f(x);

-->c40=c(1:40);yf40=fouriercosine(c40,x,L);

-->plot2d([x' x'],[y' yf40'],[1,-1],'011',' ',[-0.5 -2.5 1.5 0.5])

-->xtitle('Fourier cosine series - 40 components','x','y')


41 2001 Gilberto E. Urroz
All rights reserved


_________________________________________________________________________________
NOTE: If you have a function that is defined in the interval (-L/2, L/2), where L is the period of
the function, the formulas to use for the coefficients of the cosine and sine functions are:

a
0
=
1
L
d
0
1
2

L
2
L
2
( ) f x x , a
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,
and
b
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x

for n = 1, 2, 3, ... The formulas for the series themselves are:

= ( ) f x + a
0
.
-
,
,
,
+
*
)
)
)
"
= n 1

a
n
.
-
,
,
+
*
)
)
cos
2 n x
L
and = ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
2 n x
L
.


____________________________________________________________________________

For example, a sine series fitting of the function f(x) = x
3
in the interval [-1,1] (i.e., L=2), is
shown below. The following SCILAB commands are used to define the function y = x
3
, and the
functions necessary to calculate the coefficients of the corresponding Fourier sine series
expansion:

-->deff('[y]=f(x)','y=x^3')

-->deff('[y]=g(x)','y=f(x)*sin(2*n*%pi*x/L)')

-->deff('[bb]=b(n)','bb=(2/L)*intg(-L/2,L/2,g)')


Next, we calculate 20 coefficients of the Fourier sine series:

42 2001 Gilberto E. Urroz
All rights reserved

-->L = 2; c=[];for j=1:20, c=[c b(j)]; end;

The function fouriersine2 is a modified version of fouriersine to account for the case in which
the function is fitted in the interval [-L/2,L/2]:

function [y] = fouriersine2(c,x,L)

//Calculates the Fourier sine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);
y = [];
for i = 1:n
yy = 0;
for j = 1:m
yy = yy + c(j)*sin(2*j*%pi*x(i)/L);
end;
y = [y yy];
end;

//end of function fouriersine2

The next commands produce a plot of the original function, y = x
3
, and of the Fourier sine
series:

-->x=[-L:0.01:L]; y = f(x); yf = fouriersine2(c,x,L);

-->plot2d([x' x'],[y' yf'],[1,-1],'011',' ',[-2 -5 2 5])

-->xtitle('Fourier sine series y = x^3, 20 components','x','y')




The figure shows that the fitting is excellent except near the extremes of the interval, i.e.,
near x = -L/2 and near x = L/2.


43 2001 Gilberto E. Urroz
All rights reserved
The next example shows a cosine series fitting in the interval [-1,1], L = 2, for the function f(x)
=
1 x
2
. First, we define functions f(x) and g(x) and the generic coefficient of the Fourier
cosine series a
n
= a(n). Next, we calculate the first 40 coefficients in the series.

-->deff('[y]=f(x)','y=1-x^2')

-->deff('[y]=g(x)','y=f(x)*cos(2*n*%pi*x/L)')

-->deff('[aa]=a(n)'...
-->,['if n==0 then';'aa=intg(-L/2,L/2,f)/L';'else';...
-->'aa=(2/L)*intg(-L/2,L/2,g)';'end'])

-->L=2; c=[]; for j = 1:40, c = [c a(j-1)]; end;


Function fouriercosine2 is used to calculate the Fourier cosine series:

function [y] = fouriercosine2(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.



m = length(c); n = length(x);
y = [];
for i = 1:n
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;
//end of function fouriercosine2

The following plot shows the original function and the corresponding Fourier cosine series:

-->c5 = c(1:5) ; y5 = fouriercosine2(c5 ,x,L);

-->c10 = c(1:10); y10 = fouriercosine2(c10,x,L);

-->c20 = c(1:20); y20 = fouriercosine2(c20,x,L);

-->c30 = c(1:30); y30 = fouriercosine2(c30,x,L);

-->c40 = c(1:40); y40 = fouriercosine2(c40,x,L);

-->plot2d([x',x',x',x',x',x'],[y',y5',y10',y20',y30',y40'],...
-->[1:1:6],'011',' ',[-1.5 -0.2 1.5 1.1])

-->xtitle('Fourier cosine series y = 1-x^2','x','y')


44 2001 Gilberto E. Urroz
All rights reserved


As in the case of the previous example for the sine series, the fitting of this cosine series is
excellent throughout the whole interval [-L/2,L/2] except very close to the extremes of the
intervals. The Gibbs phenomenon is not evident in this case.

______________________________________________________________________________
The last two examples used a series expansion in the interval [-L/2,L/2]. Alternatively, you
could use as interval of integration [-L,L], i.e., an interval encompassing two periods of the
function, to obtain
= a
0
1
L

d
0
1
2
L
L
( ) f x x
,
= a
n
1
L
d
0
1
2
2
2
2
L
L
( ) f x
.
-
,
,
+
*
)
)
cos
n x
L
x , and
= b
n
1
L
d
0
1
2
2
2
2
L
L
( ) f x
.
-
,
,
+
*
)
)
sin
n x
L
x ,
for n = 1,2,3, ...
The sine and cosine series will be calculated as:
= ( ) f x +
a
0
2
.
-
,
,
,
+
*
)
)
)
"
= n 1

a
n
.
-
,
,
+
*
)
)
cos
n x
L
and = ( ) f x
"
= n 1

b
n
.
-
,
,
+
*
)
)
sin
n x
L
.
_______________________________________________________________________________

For example, the function f(x) = tan(x) can be expanded as sine series using the period L = 1 as
follows. First, we define the functions that are used to produce the coefficients of the Fourier
sine series, followed by the calculation of the first 20 coefficients (including a
0
):

-->deff('[y]=f(x)','y=tan(x)')
-->deff('[y]=g(x)','y=f(x)*sin(n*%pi*x/L)')
-->deff('[bb]=b(n)','bb=(1/L)*intg(-L,L,g)')
-->L=1; c=[]; for j = 1:20, c = [c b(j-1)]; end;

Next, we produce a plot of the original function and the Fourier sine series for 5, 10, and 20
components:

-->x=[-2.5:0.01:2.5];y=f(x);
-->c5 = c(1:5); y5 = fouriersine(c5 ,x,L);
-->c10= c(1:10);y10 = fouriersine(c10,x,L);
-->c20= c(1:20);y20 = fouriersine(c20,x,L);
-->plot2d([x' x' x' x'],[y' y5' y10' y20'],[-1,1,2,3],'011',' ',[-1.5 -2 1.5 2])

-->xtitle('Fourier sine series for y = tan(x), -L<x<L, L = 2','x','y')


45 2001 Gilberto E. Urroz
All rights reserved



In the following example, we use a cosine series to fit the function f(x) = (1-x
2
)
1/2
in the
interval [-1,1], i.e., L = 1.

-->deff('[y]=f(x)','y=sqrt(1-x^2)')

-->deff('[y]=g(x)','y=f(x)*cos(n*%pi*x/L)')

-->deff('[aa]=a(n)',...
-->['if n==0 then';'aa=intg(-L,L,f)/(2*L)';'else';...
--> 'aa=intg(-L,L,g)/L';'end'])

-->L=1; c=[]; for j=1:20, c=[c a(j-1)]; end;

-->x=[-1:0.01:1];y=f(x);yf=fouriercosine(c,x,L);

-->plot2d([x',x'],[y',yf'],[1,-1])

-->xtitle('Fourier cosine for y = sqrt(1-x^2), 20 components','x','y')




Fourier Series


46 2001 Gilberto E. Urroz
All rights reserved
A Fourier series is simply a series expansion of a function that includes both sine and cosine
series. The general form of the Fourier series expansion in the interval [-L/2,L/2], i.e.,
assuming a period L, is:

= ( ) f x
a
0
2
+
"
= n 1

.
-
,
,
+
*
)
)
+ a
n
.
-
,
,
+
*
)
)
cos
2 n x
L
b
n
.
-
,
,
+
*
)
)
sin
2 n x
L



with the coefficients calculated as:


a
0
=
2
L
d
0
1
2

L
2
L
2
( ) f x x , a
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,

and
b
n
=
2
L
d
0
1
2
2
2
2

L
2
L
2
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x


If the function is periodic of period L, the coefficients a
n
and b
n
are calculated as



a
0
=
2
L
d
0
1
2
c
0
+ c
0
L
( ) f x x
, a
n
=
2
L
d
0
1
2
2
2
2
c
0
+ c
0
L
( ) f x
.
-
,
,
+
*
)
)
cos
2 n x
L
x ,

and

b
n
=
2
L
d
0
1
2
2
2
2
c
0
+ c
0
L
( ) f x
.
-
,
,
+
*
)
)
sin
2 n x
L
x
.


Thus, if c
0
= -L/2, we recover the first set of equations for a
n
and b
n
.


A user-defined SCILAB function to calculate Fourier series

The following function, fourierseries, returns the coefficients of the Fourier series and the
evaluation of the same on a range of values of x for different number of components. The
general call to the function is


47 2001 Gilberto E. Urroz
All rights reserved
[a0 a b y] = fourierseries(nn, c0, L, x, f, tol)

where nn is a vector containing values of the components to be used, e.g., nn = [5, 10, 20], L is
the period of the function, x is a vector indicating the values of x where the Fourier series will
be evaluated, and tol is a tolerance for convergence used in function intg (see --> help intg).
The integrals defining a
n
and b
n
are calculated in the interval [c
0
,c
0
+L]. The function returns
values a0, a, b, and y, where a0 is the coefficient a
0
in the series, a and b are vectors
containing the coefficients of the cosine and sine series, respectively, i.e., a
n
and b
n
for
n=1,2,, and y is a matrix with the same number of columns as in vector x and the number of
rows equal to the number of elements in vector nn. Thus, the i-th row of matrix y contains the
values of the Fourier series with the number of components indicated by the i-th component of
nn.

A listing of function fourierseries follows. The function fourierseries includes a sub-function,
fseries, which is called recurrently to produce the rows of matrix y.



function [a0,a,b,y] = fourierseries(n,c0,L,x,f,tol)

//Calculates coefficients and values for the
//Fourier series corresponding to function f
//for n1, n2, ..., nm components, where n is
//the vector n = [n1,n2,...,nm] of length m.
//Integrals are calculated using the interval
//[c0,c0+L] where L is the period of the function.
//e.g., if c0 = -L/2, then the interval is [-L/2,L/2],
//x is a vector of length k for calculating
//values of the Fourier series.
//a0 = first coefficient for cosine term
//a = coefficients of the cosine terms [a1 a2 ...]
//b = coefficients of the sine terms [b1 b2 ...]
//y = matrix of m rows

deff('[gg1]=g1(xi)','gg1=f(xi)*cos(2*nn*%pi*xi/L)');
deff('[gg2]=g2(xi)','gg2=f(xi)*sin(2*nn*%pi*xi/L)');
deff('[aaa]=a1(nn)','aaa=(2/L)*intg(c0,c0+L,g1,tol)');
deff('[bbb]=b1(nn)','bbb=(2/L)*intg(c0,c0+L,g2,tol)');

a0 = (2/L)*intg(c0,c0+L,f,tol);

nmax = max(n);a = []; b = [];
for j = 1:nmax
a = [a a1(j)]; b = [b b1(j)];
end;

m = length(n); k = length(x);y = zeros(m,k);
for j =1:m
aj = a(1:n(j)); bj = b(1:n(j));
y(j,:) = fseries(a0,aj,bj,x,L);
end;

//end function fourierseries

function [yy] = fseries(a0,aa,bb,xx,L)

//Calculates fourier series given the

48 2001 Gilberto E. Urroz
All rights reserved
//coefficients of the cosine series
//a0 and vector a (of length nn), and
//the coefficients of the sine series,
//vector b (of length nn).
//L = period of function to be fit.

nn = length(aa); mm = length(xx);yy = zeros(1,mm);
for j = 1:mm
yy = a0/2;
for k = 1:nn
yy = yy + aa(k)*cos(2*k*%pi*x/L) + bb(k)*sin(2*k*%pi*x/L);
end;
end;

Applications of function fourierseries

Example 1 - The following example shows the fitting of the function
= ( ) f x + 1 x
2
in the
interval [-1,1] using function fourierseries with the option itype = right. The Fourier series
are calculated for 5, 10, 20, and 40 components. A plot of the original figure and of the
Fourier series with different number of components is also produced.

-->deff('[y]=f(x)','y=sqrt(1+x^2)')
-->getf('fourierseries')
-->L = 2; x=[-1:0.01:1]; y = f(x);
-->[a0,a,b,yy]=fourierseries('right',[5,10,20,40],-L/2,L,x,f,1e-5);
-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])
-->xtitle('Fourier series for y = sqrt(1+x^2) with 5,10,20,40 components',...
-->'x','y')




Example 2 - To fit the function f(x) = ln(x+2) in the interval [-1,1], i.e., L = 2, using Fourier
series, we try:

-->deff('[y]=f(x)','y=log(x+2)')

-->getf('fourierseries')

-->L= 2; x=-1:0.01:1; y = f(x);
-->[a0,a,b,yy]=fourierseries([5,10,20,40],-L/2,L,x,f,1e-5);

-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])

49 2001 Gilberto E. Urroz
All rights reserved
-->xtitle('Fourier series for y = ln(x+2) with 5,10,20,40 components','x','y')




The fitting of the Fourier series is excellent except near the extremes of the interval where
strong Gibbs phenomenon deviations are observed. Also, the fitting improves as the number of
components in the Fourier series increases.


Notes:

[1] Even if a function is not periodic, you can fit a certain section of the function (say, between
x = 0 and x = L, or between -L/2 and L/2) using Fourier series. Of course, the resulting
trigonometric series will be so that it represents a periodic function of period L.
[2] For any given function you can fit sine, cosine, or full Fourier series. It is recommended,
however, that you plot the original as well as the resulting expansion(s) to determine how well
the Fourier series expansion(s) works.

Example 3 - In the next example we produce the Fourier series for the function g(x) = exp(x),
x in [0,], i.e., with period L = .

-->deff('[y]=f(x)','y=exp(x)')

-->L=%pi; c0 = 0; x=-%pi:%pi/100:%pi; y = f(x);

-->getf('fourierseries')

-->[a0,a,b,yy]=fourierseries([5,10,20,40],c0,L,x,f,1e-5);

-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])

-->xtitle('Fourier series for y = exp(x) with 5,10,20,40 components','x','y')


50 2001 Gilberto E. Urroz
All rights reserved


The Fourier series for this function does an excellent job fitting the curve except near the
extremes of the interval where the Gibbs phenomenon is evident.

The following graphs shows the Fourier series developed above in the interval [-2,2 ] to
demonstrate the fact that the Fourier series produces a periodic function even though the
original function was not periodic. By calculating the Fourier series coefficients in the interval
[0, ] we generates a periodic function of period .




Fourier series for a piecewise periodic function

The following example shows the fitting of the Fourier series for a continuous piece-wise
function, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere. We assume that the function h(x)
represents a periodic function in [-1,1], with period L = 2. The solution is shown for 4, 6, 8,
and 10 components. For more than 10 components, function fourierseries presents problems
of convergence on function intg which is used to calculate the integrals in the series
coefficients.

-->deff('[y]=f(x)',['if x>-1/3 & x<1/3 then';'y=3';'else';'y=0';'end'])

-->L=2; c0=-L/2; x=[-1:0.1:1];

-->y=[]; for j =1:length(x), y=[y f(x(j))]; end;

51 2001 Gilberto E. Urroz
All rights reserved

-->getf('fourierseries')

-->[a0,a,b,yy]=fourierseries([4,6,8,10],c0,L,x,f,1e-5);
-->plot2d([x' x' x' x' x'],[y' yy(1,:)' yy(2,:)' yy(3,:)' yy(4,:)'])
-->xtitle(Piecewise continuous function,'x','y')



The following graphs shows the original function (single square pulse) and the periodic function
generated by the corresponding Fourier series using the interval -3 < x < 3. This result
demonstrates how we can generate a square wave out of a single square pulse.



To be able to calculate coefficients for n>10, we can use the definitions for the coefficients a
n

and b
n
corresponding to the function h(x) defined earlier. These coefficients are:

,
3
sin
6
cos 3
2
2
cos ) (
2
3 / 1
3 / 1
2 /
2 /
! !

)
*
+
,
-
.
= )
*
+
,
-
.
= )
*
+
,
-
.
=

n
n
dx
L
X n
dx
L
x n
x h
L
a
L
L
n


and

. 0 sin 3
2
2
sin ) (
2
3 / 1
3 / 1
2 /
2 /
! !

= )
*
+
,
-
.
= )
*
+
,
-
.
= dx
L
X n
dx
L
x n
x h
L
b
L
L
n




52 2001 Gilberto E. Urroz
All rights reserved
Also,


! !

= = =
3 / 1
3 / 1
2 /
2 /
0
. 0 . 2 3
2
2
) (
2
dx dx x h
L
a
L
L



Using these definitions we can produce a vector of values for the coefficients a
n
as follows:


-->deff('[aa]=a(n)','aa=6*sin(n*%pi/3)/(n*%pi)')
-->c = [1]; for j=1:40, c=[c a(j)]; end;


Notice that the first term in the vector of coefficients, c, is equal to 1, i.e., a
0
/2. Next, we
generate data from the original function (defined earlier):


-->x=-1:0.01:1;y=f(x);


To produce the Fourier series we use function fouriercosine3, which is a modified version of
function fouriercosine, introduced earlier.

function [y] = fouriercosine3(c,x,L)

//Calculates the Fourier cosine series given
//a vector of coefficients c and a vector
//of values of x. It returns a vector of
//values of y representing the Fourier series.
//L is the period of the series.

m = length(c); n = length(x);y = [];
for i = 1:n
yy = c(1);
for j = 2:m
yy = yy + c(j)*cos(2*(j-1)*%pi*x(i)/L);
end;
y = [y yy];
end;

To calculate values for the Fourier series with 40 components of cosine we use:


-->getf('fouriercosine3')
-->yy = fouriercosine3(c,x,L);


A plot of the original function and the corresponding Fourier series with 40 components is
shown next:


-->plot2d([x' x'],[y' yy'])


53 2001 Gilberto E. Urroz
All rights reserved
-->xtitle('square wave with 40 components of Fourier series','x','y')



Calculating Fourier series coefficients with Filons sine-cosine
formulae

An alternative for calculating the coefficients of the Fourier series is to use function filonquad,
defined earlier for calculating integrals of the form

! !

m m
x
x
x
x
dx kx x f dx kx x f
0 0
) cos( ) ( , ) sin( ) (

For Fourier series coefficients the following values apply: k=2/L, x
0
= c
0
, x
n
=c
0
+L, and the
integrals are to be multiplied by (2/L). Care must exercised when using Filons integral
formulas for the function f(x) used must be continuous in the full interval [x
0
,x
n
]. Filons
integral formulae use a parameter m (referred to as n when the formulae where first
introduced earlier in the Chapter) which must be an even number larger than or equal to 4.
Thus, to calculate the coefficients of the cosine terms we will use

a
n
= (2/L)*filonquad(cos,2*n*%pi/L,x
0
,x
n
,m,f),

while those for the sine terms are calculated with

b
n
= (2/L)*filonquad(sin,2*n*%pi/L,x
0
,x
n
,m,f).

For example, to calculate the first five coefficients of the Fourier series corresponding to f(x) =
exp(-x), using Filons sine-cosine quadrature formulae for m = 4,6,8,,30, we prepare the
following table. Columns represents values of n = 1,2,3,4,5, and rows represent values of m =
4,6,8,,30:


-->deff('[y]=f(x)','y=exp(-x)');x0=-1;xn=1;L=2;

-->kk=[1:1:5];mk=length(kk);nn=[4:2:40];mn=length(nn);

-->ResCos = zeros(mn,mk);

-->for i = 1:mn
--> for j = 1:mk

54 2001 Gilberto E. Urroz
All rights reserved
--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResCos
ResCos =

! - .2201023 .0583263 - .0244558 .0145816 - .0088041 !
! - .2169092 .0591023 - .0262183 .0141662 - .0093004 !
! - .2164390 .0583263 - .0265777 .0148071 - .0092038 !
! - .2163174 .0581622 - .0262908 .0149935 - .0094942 !
! - .2162749 .0581098 - .0262183 .0148593 - .0096020 !
! - .2162569 .0580887 - .0261921 .0148217 - .0095293 !
! - .2162483 .0580789 - .0261806 .0148071 - .0095076 !
! - .2162437 .0580738 - .0261749 .0148002 - .0094986 !
! - .2162412 .0580709 - .0261717 .0147966 - .0094942 !
! - .1933848 .0463011 - .0304248 .0353657 - .0405569 !
! - .2162386 .0580681 - .0261687 .0147932 - .0094904 !
! - .2162380 .0580674 - .0261680 .0147924 - .0094895 !
! - .2162375 .0580669 - .0261675 .0147919 - .0094889 !
! - .2162372 .0580666 - .0261671 .0147915 - .0094885 !
! - .2162370 .0580664 - .0261669 .0147913 - .0094882 !
! - .2162368 .0580662 - .0261667 .0147911 - .0094881 !
! - .2162367 .0580661 - .0261665 .0147910 - .0094879 !
! - .2162366 .0580660 - .0261664 .0147909 - .0094878 !
! - .2162365 .0580659 - .0261664 .0147908 - .0094877 !

-->ResSin = zeros(mn,mk);

-->for i = 1:mn
--> for j = 1:mk
--> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResSin
ResSin =

! - .6795196 .3740782 - .2468433 .1870391 - .1490822 !
! - .6792471 .3650430 - .2493854 .1860151 - .1490260 !
! - .6792918 .3648433 - .2467114 .1870391 - .1491070 !
! - .6793105 .3648335 - .2466187 .1859172 - .1496313 !
! - .6793183 .3648340 - .2466100 .1858691 - .1490599 !
! - .6793218 .3648349 - .2466088 .1858634 - .1490322 !
! - .6793236 .3648356 - .2466087 .1858623 - .1490285 !
! - .6793245 .3648360 - .2466088 .1858620 - .1490276 !
! - .6793251 .3648362 - .2466089 .1858620 - .1490273 !
! - .6940133 .3906049 - .2762026 .2096025 - .1581488 !
! - .6793256 .3648365 - .2466090 .1858620 - .1490272 !
! - .6793258 .3648365 - .2466090 .1858620 - .1490272 !
! - .6793259 .3648366 - .2466090 .1858620 - .1490272 !
! - .6793260 .3648366 - .2466091 .1858620 - .1490272 !
! - .6793260 .3648366 - .2466091 .1858621 - .1490273 !
! - .6793260 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !
! - .6793261 .3648367 - .2466091 .1858621 - .1490273 !



55 2001 Gilberto E. Urroz
All rights reserved
For the case of the piecewise continuous function that produces the single rectangular pulse,
namely, h(x) = 3, -1/3 < x < 1/3, and h(x) = 0 elsewhere, we can use Filons sine-cosine
quadrature formulae to calculate the coefficients for the Fourier series by using x
0
= -1/3 and
x
n
= 1/3, since the function is zero in the remaining ranges of the interval (-1,1). The period is
still taken as L = 2. The following SCILAB commands will produce the Fourier series coefficients
for the cosine and sine components for n = 1,2,3,4,5, and m = 4,6,8,,30:

-->deff('[y]=f(x)','y=3');x0=-1/3;xn=1/3;L=2;

-->ResCos = zeros(mn,mk);
-->for i = 1:mn
--> for j = 1:mk
--> ResCos(i,j)=(2/L)*filonquad('cos',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResCos
ResCos =

! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.5838617 .819752 .0616369 - .3132454 - .2446393 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.618222 .8366987 .0487592 - .3600631 - .3111114 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.630336 .8377577 .0368149 - .3797570 - .3271122 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !
! 1.6331939 .8374929 .0333666 - .3843859 - .3298161 !
! 1.6539867 .8269933 0. - .4134967 - .3307973 !


-->ResSin = zeros(mn,mk);
-->for i = 1:mn
--> for j = 1:mk
--> ResSin(i,j)=(2/L)*filonquad('sin',2*%pi*kk(j)/L,x0,xn,nn(i),f);
--> end;
-->end;

-->ResSin
ResSin =

! 0. 0. 0. 0. 0. !
! 0. 0. 0. 0. 0. !
! - 8.651E-17 - 9.985E-17 - 1.471E-16 - 2.321E-17 3.258E-17 !
! 2.935E-17 0. 0. - 6.795E-17 - 6.210E-17 !
! - 1.480E-16 - 8.417E-17 - 9.729E-17 5.351E-17 9.501E-17 !
! - .0650665 - .0966290 - .0772903 - .0151104 .0587416 !
! 1.355E-16 1.230E-16 8.613E-17 - 6.320E-17 - 8.416E-17 !
! 0. - 4.398E-17 - 3.772E-17 2.146E-17 0. !
! 0. 1.383E-16 5.777E-17 - 3.214E-17 - 4.877E-17 !

56 2001 Gilberto E. Urroz
All rights reserved
! - 2.871E-16 - 3.412E-16 - 1.219E-16 1.072E-16 1.111E-16 !
! - .0426227 - .0550420 - .0281511 .0194482 .0540867 !
! 2.354E-16 2.053E-16 6.136E-17 - 2.430E-16 - 7.683E-17 !
! - 1.550E-16 - 9.129E-17 2.827E-17 1.233E-16 1.891E-16 !
! - 2.369E-16 - 1.058E-16 9.830E-17 2.560E-16 2.373E-16 !
! - 1.850E-16 - 1.850E-16 - 6.451E-17 1.243E-16 1.295E-16 !
! - .0313185 - .0378327 - .0142622 .0208907 .0397693 !
! 1.261E-16 1.643E-16 - 2.211E-17 - 1.201E-16 - 1.692E-16 !
! - .0282896 - .0335690 - .0114548 .0201849 .0355957 !
! 1.036E-16 0. 5.427E-17 0. - 1.184E-16 !

As found earlier, the coefficients for the sine components are all zero (nearly zero from the
numerical results). The coefficients for the cosine components are very consistent for most
values of m except for a few of them. For that reason it is a good idea to check the
convergence of the Filon quadrature formulae for different values of m.
Use of Fourier series in ordinary differential equations

Fourier series can be used to represent periodic functions within ordinary differential
equations. Consider the case of an oscillating mass subject to damping (

)and a linear
restoring force (k):


= + + m
.
-
,
,
+
*
)
)

2
x
2
( ) y x
.
-
,
,
+
*
)
)

x
( ) y x k ( ) y x ( ) F x



The function F(x) in the right-hand side of the equation represents the excitation or driving
force, i.e., an external effect that activates or affects the oscillating system. If the function
F(x) happens to be a periodic function, the expression for F(x) can be replaced by its Fourier
series expansion G(x), before obtaining a solution to the ODE.

As an example, let a trapezoidal driving function f(x)

.
6
6
3
6
6
4
5
< <
)
*
+
,
-
.

< <
< <
=
elsewhere
x x
x
x x
x f
, 0
1 3 / 2 ,
3
2
3 1
3 / 2 3 / 1 , 1
3 / 1 0 , 3
) (


represent the excitation function in the solution of the differential equation for an oscillating
mass. In the solution, we will use both the original pulse function f(x) and the Fourier series
expansion of f(x), G(x), as illustrated below.
Fourier series for a trapezoidal pulse

To obtain the Fourier series of the trapezoidal pulse, first, we define function f(x):

-->deff('[y]=f(x)',['if x>0 & x<1/3 then';'y=3*x';...
--> 'elseif x>1/3 & x<2/3 then';'y=1';'elseif x>2/3 & x<1 then';...

57 2001 Gilberto E. Urroz
All rights reserved
--> 'y=1-3*(x-2/3)';'else';'y=0';'end'])

Next, we generate data for plotting the function in the range 0 < x < 1:

-->x=0:0.01:1;
-->y=[]; for j=1:length(x), y=[y f(x(j))]; end;

The following statements generate the coefficients of a Fourier series with 10 components:

-->L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

Next, we produce a plot of the original function and the Fourier series:

-->plot2d([x' x'],[y' yy'],[1,-1])
-->xtitle('Trapezoid function - Fourier series 20 components','x','y')


The graph shows that even if we use only 10 components for the Fourier series, the fitting for
the trapezoidal function is excellent. Notice that in the definition of f(x) the function is zero
outside of the range [0,1]. Thus, f(x) defines a single trapezoidal pulse as illustrated in the
figure below in which we plot f(x) in the range -2 < x < 3:


-->xx=[-2:0.01:3];y=[];for j=1:length(xx), y=[y f(xx(j))]; end;

-->plot2d(xx,y,1,'011',' ',[-2 -0.1 3 1.1])

-->xtitle('Single trapezoidal pulse given by f(x)','x','y')





58 2001 Gilberto E. Urroz
All rights reserved



Next, we calculate the Fourier series generated by using the trapezoidal pulse f(x) in the range
0 < x < 5 to demonstrate the periodicity of the Fourier series:

-->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

-->plot(x,yy,'x','y','Trapezoidal periodic wave')




Solution to the damped oscillator equation with trapezoidal pulse and wave excitation

Next we will solve the second-order differential equation using as driving signals the original
trapezoidal function (a single trapezoidal pulse) and the corresponding Fourier series (a
periodic trapezoidal wave). To solve the problem, we use the variables u
1
= y(x) and u
2
=
dy/dx, to form the vector function u(x) = [u
1
;u
2
], and transform the equation into

du/dx = ff(x,u) = (F(x)-ku
1
-u
2
)/m.

In the latter expression we first replace the function F(x) with the original trapezoidal pulse
f(x), and then with the Fourier series G(x). We will use as initial conditions u
1
(0) = u
2
(0) = 0
and solve the ODE in the range 0 < x < 20 with an increment x = 0.1.


59 2001 Gilberto E. Urroz
All rights reserved
Following we list the SCILAB commands for solving the ODE. The function f(x) describing the
trapezoidal pulse was defined earlier. Also, the coefficients a0, a, and b of the Fourier series
are available from the call to function fourierseries. The next step is to define a function G(x)
that calculates the Fourier series:

-->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';...
--> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end'])


Next, we define functions f1(x,u) and f2(x,u) representing the function ff(x,u), i.e., the right-
hand side in the equation du/dx = ff(x,u). Function f1(x,u) involves the trapezoidal pulse
function f(x) while function f2(x,u) involves the Fourier series function G(x) as indicated below:

-->deff('[w]=f1(x,u)','w=[u(2),(f(x)-k*u(1)-beta*u(2))/m]')

-->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]')

The next step is to define the parameters of the problem and of the solution:

-->k=10;m=5;beta=1;

-->x = [0:0.1:20]; x0 = 0; y0 = [0;0];

The solutions to the ODE for the two cases is calculated using function ode as follows:

-->y1=ode(y0,x0,x,f1);

-->y2=ode(y0,x0,x,f2);

The following plot shows the solutions corresponding to the single trapezoidal pulse (continuous
line) and to the periodic trapezoidal wave (circles). Notice that the signal corresponding to
the single trapezoidal pulse starts at rest and reaches a maximum amplitude at the beginning,
decaying in a sinusoidal manner afterwards. On the other hand, the signal due to the periodic
trapezoidal wave, although, in general also decaying with x, shows additional secondary peaks
that result from the repeated excitation represented by the periodic function represented by
the Fourier series.

-->plot2d([x' x'],[y1(1,:)' y2(2,:)'],[1,-9])

-->xtitle('Damped oscillator solution','x','y')



60 2001 Gilberto E. Urroz
All rights reserved



Fourier series for other periodic shapes

Thus, we have demonstrated how to use Fourier series to generate a periodic function
representing almost any type of shape. All we need to get started is a single pulse with the
basic shape that needs to be repeated. Thus, Fourier series can be used to produce periodic
functions such as those illustrated in the figure below:





Even when using the same basic function, e.g., f(x) in the examples above, to generate a
Fourier series, you can change the shape of the resulting periodic signal by changing the period
of the function. In the example worked out above, we used a period L = 1.0, thus, the basic
shape (the trapezoid) repeats one after another in the Fourier series. If we want to add some
space between consecutive trapezoids we can change the period to L = 1.5. The following
SCILAB commands show how to generate such signal.

-->x=0:0.01:5;L=1;c0=0;[a0,a,b,yy]=fourierseries(10,c0,L,x,f,1e-5);

-->plot(x,yy,'x','y','Trapezoidal periodic wave')


61 2001 Gilberto E. Urroz
All rights reserved


Solving the damped oscillator equation with a different periodic trapezoidal pulse

With the signal shown above as the excitation to the system represented by the damped
oscillator equation, the solution to the ODE is presented in the following plot:

-->deff('[w]=G(x)',['n=length(a)';'w=a0/2';'for j=1:n';...
--> 'w=w+a(j)*cos(2*j*%pi*x/L)+b(j)*sin(2*j*%pi*x/L)';'end'])

-->deff('[w]=f2(x,u)','w=[u(2),(G(x)-k*u(1)-beta*u(2))/m]')

-->k=10;m=5;beta=1;x=[0:0.1:20];x0=0;y0=[0;0];

-->y3=ode(y0,x0,x,f2);

-->plot(x,y3(1,:),'Damped oscillator solution','x','y')




Compare the latter result with the previous solution.


F Fo ou ur ri ie er r t tr ra an ns sf fo or rm ms s


62 2001 Gilberto E. Urroz
All rights reserved
In the previous solution, the variable x can be thought of as representing time, while y may
represent the displacement of an oscillator. When writing out Fourier series for a function of
time we would use

f(t) = a
0
/2 + [ a
n
cos(2nt/T) + b
n
sin(2nt/T),


n = 1,2, , where T is the period of the function, f
0
= 1/T is the fundamental frequency, and
0

= 2/T = 2f
0
is the fundamental angular frequency. The Fourier series can also be written as


f(t) = a
0
/2 + [ a
n
cos(
n
t) + b
n
sin(
n
t)],


where
n
= n
o
, and


! ! !

= = =
2 /
2 /
2 /
2 /
2 /
2 /
0
). sin( ) (
2
), cos( ) (
2
, ) (
2
T
T
n n
T
T
T
T
n n
t t f
T
b t t f
T
a dt t f
T
a


Plots of the Fourier coefficients a
n
and b
n
vs.
n
would be similar to the ones sketched below:



In the figure, = 2/T represents the increment in the discrete values of the frequency
n
.
As the period of the signal, T, increases, the increment in frequency, , becomes a
differential of frequency, d. Using the definitions of the coefficients shown above, the signal
can be written as

. ) sin( ) sin( ) (
2
) cos( ) cos( ) (
2
2
) (
1
2 /
2 /
2 /
2 /
0
"
! !

=

#
$
%
&
'
(
7
8
9
3
4
5
+
7
8
9
3
4
5
+ =
n
n
T
T
n n
T
T
n
dt t t t f
T
t dt t t f
T
a
t f

Using 2/T = /, and replacing the summations with integrals as T ! , the signal is now
written as


63 2001 Gilberto E. Urroz
All rights reserved
. ) sin( ) sin( ) ( ) cos( ) cos( ) (
2
) (
0
0
! ! !


#
$
%
&
'
(
7
8
9
3
4
5
+
7
8
9
3
4
5
+ = t dt t t f
d
t dt t t f
d a
t f
n n n n




If we define

! !


= = , ) sin( ) (
2
1
) ( , ) cos( ) (
2
1
) ( dt t t f B dt t t f A



we can re-write the signal for an infinitely large period as

[ ] . ) sin( ) ( ) cos( ) ( 2
2
) (
0
0
!

+ + = d t B t A
a
t f

The terms A() and B() are components of a Fourier transform, and the signal f(t) results
from an inverse Fourier transform.

The Fourier transform of the signal f(t) is the term F() = A()-iB(), where i is the unit
imaginary number (i = -1). Using Eulers formula, e
-it

= cos(t)-isin(t), the Fourier
transform is written as
!

= . ) (
2
1
) ( dt e t f F
t i



The signal f(t), i.e., the inverse Fourier transform is written as

!


= . ) ( ) (

d e F t f
t i


The latter result assumes that a
0
= 0, which can be accomplished by subtracting the original
value of a
o
from the original function.

Fourier analysis of discrete data

Many a time, engineers and scientists are faced with the need of analyzing discrete data
resulting from measurements of communication signals, turbulence velocity variations, water
surface records, weather data, etc. We may be interested in determining, for example, what
are the dominant frequencies present in the signal.

Assume that the data is collected at regular intervals t = T/n and consists of the n
measurements {x
0
, x
1
,x
2
,,x
n-1
} taken at times t
r
= rt. An approximation to the Fourier
transform F() presented earlier, is the summation

1 ,..., 1 , 0 ,
1 1
1
0
) / 2 (
1
0
) )( / 2 (
= = =
" "

=

n k e x
n
t e x
T
X
n
r
n kr i
r
n
r
t r T k i
r k

.

This is known as the discrete Fourier transform (DFT). The inverse discrete Fourier transform
(IDFT) provides the data values x
r
in terms of the coefficients X
k
:

64 2001 Gilberto E. Urroz
All rights reserved

. 1 ,..., 1 , 0 ,
1
0
) / 2 (
= =
"

=
n r e X x
n
k
n kr i
k r



The coefficients X
k
calculated with the discrete Fourier transform, namely, X
k
= a
k
- ib
k

represent coefficients of a Fourier series from which the data values x
r
can be obtained.
However, the coefficients X
k
are only correct for frequencies below the frequency

k
= 2k/(nt) = /t,

i.e., for k = 0, 1, 2, , n/2. Also, if the signal contains frequencies above /t, these
frequencies introduce a distortion of the X
k
-vs -
k
graph known as aliasing. If w
0
is the
maximum component of frequency contained in the signal x(t), aliasing is avoided by selecting
t so that /t >
o
, or 1/(2t) > f
0
. The value 1/(2t) is known as the Nyquist frequency,
which represents the maximum frequency that can be detected by sampling the data at
intervals t.

SCILAB functions for discrete Fourier transform (DFT) and fast
Fourier transform (FFT)

SCILAB provides functions dft and fft to calculate discrete Fourier transforms and fast Fourier
transforms, respectively. The fast Fourier transform (FFT) is an algorithm that applies to
discrete data that includes 2
m
elements (m = integer). FFT takes advantage of the fact that
the size of the sample is a power of 2 to dramatically reduce the amount of computer
operations in the calculation of the DFT that would otherwise be required. This approach thus
provides economy of storage and operations when calculating DFTs. (For details on the
algorithm for the fast Fourier transform, the reader is referred to Newland, D.E., 1993,An
Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition, Longman
Scientific & Technical, Essex, England.)
Scilabs dft function

Function dft applies to single data vectors. The general call to the function is

[xf]=dft(x,flag);

where x is an input vector which may represent signal data (x
r
, r = 0,1,,n-1) or Fourier
transform coefficients (X
k
, k = 0,1, , n-1), and flag determines whether to perform a discrete
transform (flag=-1) or an inverse discrete transform (flag=1). The function returns a vector
containing either the discrete Fourier transform coefficients, X
k
, if flag = -1, or the signal
values, x
r
, if flag = 1.
Example of DFT applications: Obtaining the discrete Fourier transform

First, we generate a set of n=101 random numbers representing discrete values y
r
, r = 0, 1, ,
n-1, of a signal y(t), through the use of function y = f(x) shown below. The function is a sum of
m=10 cosine components plus a couple of random terms. The coefficients in the cosine
components are random numbers generated using SCILAB function rand. The figure below
shows the signal calculated:

-->m=10;
-->k=int(100*rand(1,m));a=int(1000*rand(1,m));phi=int(2*%pi*rand(1,m));
-->deff('[w]=f(x)',...

65 2001 Gilberto E. Urroz
All rights reserved
-->['w=0';'for j=1:m';...
-->'w=w+a(j)*cos(2*k(j)*%pi*x+phi(j))+100*(rand()-0.5)-2000*(rand()-0.5)';...
-->'end'])

-->x=[0:0.1:10]; y = f(x); plot(y); xtitle('Random data','k','y');
-->m=length(y)m = 101.


The discrete Fourier transform of the signal is obtained by using the following call to function
dft. Of interest to us is the magnitude of the coefficients, i.e., |X
k
| = (A
k
2
+B
k
2
)
1/2
, which we
refer to as XfA. A plot of XfA is shown next.

-->Xf=dft(y,-1); XfA = abs(Xf);
-->plot2d([1:1:101]',XfA,-9)
-->plot2d3('onn',[1:1:101]',XfA)

There are a few frequencies that shown strong components, e.g., for k = 1, 11, 31, 41, 42, 52,
and so on. Also, notice that there is a symmetry in the figure, which results from the periodic
nature assumed for the signal in Fourier analysis. This verifies the statement made earlier that
out of the n components of the discrete Fourier transform, only those for k = 0, 1, , n/2, are
necessary to reproduce the signal.
Example of DFT applications: Obtaining the inverse Fourier transform

In this exercise we generate twenty of complex coefficients representing the Fourier transform
of a signal, i.e., X
k
, k = 0,1, , n-1, with n=20. To visualize the Fourier transform we plot its
absolute value, |X
k
|:

-->Xf2 = int(20*rand(1,20))+%i*int(10*(rand(1,20)-0.5));

66 2001 Gilberto E. Urroz
All rights reserved
-->Xf2A = abs(Xf2);
-->xset('mark',-9,1); plot2d([1:1:20]',Xf2A',-9)
-->plot2d3('onn',[1:1:20]',Xf2A')
-->xtitle('|X(w)| vs. w','w','|X(w)|')


Through the use of function dft, as shown below, we can generate a signal xs2 based on the
Fourier transform coefficients Xf2. Signal xs2 is the inverse discrete Fourier transform of Xf2:

-->xs2 = dft(Xf2,1)
xs2 =

! 11.3 - .3i !
! .3709582 + .1198511i !
! - 2.2329752 - 1.2846661i !
! 2.1378546 - .3916269i !
! - .1146755 + .0364961i !
! - .55 - 1.95i !
! - .1372551 - .7108113i !
! 1.5447692 - 1.233702i !
! 2.1785955 + 1.5250802i !
! - .2862057 + .1284268i !
! - 1.2 + .3i !
! 1.1105288 + .2007308i !
! 1.604686 - 1.0823751i !
! 1.075967 + .5249291i !
! - .0593008 + .6035164i !
! .45 + 1.35i !
! - 1.468606 + .0707988i !
! 2.1392805 + 1.176793i !
! - .870469 + .8419610i !
! 1.0068474 + .0745981i !

The resulting signal contains complex numbers. We can, therefore, obtain a signal consisting
only of the real part of xs2 (xs2R), one consisting only of the imaginary part of xs2 (xs2I), and
one formed by taking the magnitude of the elements of xs2 (xs2A). The three different signals
are plotted below.

-->xs2R = real(xs2);xs2I = imag(xs2); xs2A = abs(xs2);nn=[1:1:20];
-->plot2d(nn',xs2R,-9,'011',' ',[0 -3 21 13])
-->plot2d(nn',xs2R,1,'011',' ',[0 -3 21 13])
-->xtitle('Real part of signal','r','x(r)')



67 2001 Gilberto E. Urroz
All rights reserved


-->plot2d(nn',xs2I,-9,'011',' ',[0 -2 21 1.6])
-->plot2d(nn',xs2I,1,'011',' ',[0 -2 21 1.6])
-->xtitle('Imaginary part of signal','r','x(r)')



-->plot2d(nn',xs2A,-9,'011',' ',[0 0 21 12])
-->plot2d(nn',xs2A,1,'011',' ',[0 0 21 12])
-->xtitle('Magnitude of signal','r','x(r)')





68 2001 Gilberto E. Urroz
All rights reserved

Example of DFT applications: Obtaining a real inverse Fourier transform

In order to obtain a real inverse Fourier transform, the vector of Fourier transform coefficients
must satisfy certain symmetry conditions. Let X represent the vector of Fourier transform
coefficients, the first term must be a real number, representing the coefficient a
0
in the
equation

[ ] . ) sin( ) ( ) cos( ) ( 2
2
) (
0
0
!

+ + = d t B t A
a
t f


If the vector contains an even number of elements n, then, the elements X
k
, k=2,3, ,n in the
vector X must be such that X
n
= X
2
, X
n-1
= X
3
, X
n-2
=X
4
,, or, in general, X
n-j+2
= X
j
, j = 2,3,
, n/2. The termsX represents the complex conjugate of X. Finally, element X
n/2+1
must
also be a real number.

As an example, we construct the following vector of coefficients, Xf3, with n=20 elements:

-->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));

-->n=20;Xf3 = [120 X0 zeros(1,m-1)];

-->for j=2:n/2, Xf3(n-j+2)=conj(Xf3(j)); end;

-->Xf3(n/2+1)=2.0; Xf3'
ans =

! 120. !
! 18. - 20.i !
! 36. - 20.i !
! - 85. + 9.i !
! 88. + 43.i !
! - 74. + 43.i !
! - 59. - 3.i !
! - 60. + 46.i !
! 78. + 18.i !
! - 7. + 12.i !
! 2. !
! - 7. - 12.i !
! 78. - 18.i !
! - 60. - 46.i !
! - 59. + 3.i !
! - 74. - 43.i !
! 88. - 43.i !
! - 85. - 9.i !
! 36. + 20.i !
! 18. + 20.i !

A plot of the Fourier coefficients magnitude is shown next. The symmetry of the coefficients
is apparent from the plot:

-->Xf3A=abs(Xf3);

-->nn=[1:1:20];xset('mark',-9,1);plot2d(nn',Xf3A',-9,'010',' ',[0 0 21 150])

69 2001 Gilberto E. Urroz
All rights reserved
-->plot2d3('onn',nn',Xf3A',1,'010',' ',[0 0 21 150])
-->xtitle('Symmetric Fourier coefficients','w','|X(w)|')



The corresponding signal is generated using:

-->xs3 = dft(Xf3,1)
xs3 =

! - .4 !
! 20.141905 - 2.176E-15i !
! 13.737285 - 9.770E-16i !
! - 6.6365465 - 4.885E-15i !
! - 5.0329987 + 6.040E-15i !
! 22.8 - 1.137E-14i !
! - 1.157441 + 2.469E-14i !
! - 9.2480507 + 1.226E-14i !
! - 9.98364 + 1.457E-14i !
! 10.952211 - 6.528E-15i !
! 41.2 - 5.351E-14i !
! 1.3207894 + 1.164E-14i !
! - 1.0044013 + 2.549E-14i !
! - 7.835515 - 1.679E-14i !
! - 12.637178 + 8.331E-14i !
! 26.8 - 1.272E-13i !
! 9.92104 - 1.137E-14i !
! 4.9413984 - 6.040E-15i !
! 26.357334 - 3.366E-14i !
! - 4.2361923 + 4.836E-14i !


The generated signal is made of complex numbers, however, their imaginary parts are
negligible. We will use only their real parts:

-->xs3=real(xs3);


A plot of the signal is presented next:

-->plot(xs3,'r','x(r)','Real signal IDFT')

-->plot2d(nn',xs3,-9,'010',' ',[1 -20 21 50])

70 2001 Gilberto E. Urroz
All rights reserved



If the vector of Fourier transform coefficients contains an odd number of elements, the
elements X
k
, k=2,3, ,n in the vector X must be such that X
n
= X
2
, X
n-1
= X
3
, X
n-2
=X
4
,, or, in
general, X
n-j+2
= X
j
, j = 2,3, , n/2+1. The first element in vector X must still be a real
number.

As an example, we put together the Fourier coefficients vector Xf4 with 21 components. A plot
of the magnitude of the coefficients is shown next:

-->m=10;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));
-->n=21;Xf4 = [50 X0 zeros(1,m)];
-->for j=2:n/2+1, Xf4(n-j+2)=conj(Xf4(j)); end;
-->Xf4A = abs(Xf4);
-->nn=[1:1:21];xset('mark',-9,1);plot2d(nn',Xf4A',-9,'010',' ',[0 0 21 150])
-->plot2d3('onn',nn',Xf4A',1,'011',' ',[0 0 21 150])
-->xtitle('Symmetric Fourier coefficients - odd number','w','|X(w)|')


The signal is generated with the following call to function dft. We replace the resulting signal,
which may still contain negligible imaginary components, with its real part only, and plot the
results as follows:

-->xs4 = dft(Xf4,1); xs4=real(xs4);

-->plot(xs4,'r','x(r)','Real signal IDFT - odd number')


71 2001 Gilberto E. Urroz
All rights reserved



SCILABs fft function

SCILABs fast Fourier transform function fft is called in a similar manner as dft. The general
call to the function is
[xf]=fft(x,flag);

where x is an input vector which may represent signal data (x
r
, r = 0,1,,n-1) or Fourier
transform coefficients (X
k
, k = 0,1, , n-1), and flag determines whether to perform a discrete
transform (flag=-1) or an inverse discrete transform (flag=1). As with function dft, the
function returns a vector containing either the discrete Fourier transform coefficients, X
k
, if
flag = -1, or the signal values, x
r
, if flag = 1.

Note: while the FFT algorithm was developed for data sets containing 2
m
, m=integer, data
points, the SCILAB function fft has no restriction with respect to the size of the sample used.

Example of FFT applications: Obtaining a discrete Fourier transform

First we generate a one-dimensional signal by adding random numbers:

-->xs1=[];for j=1:200, xs1=[xs1 100*rand()-50*(rand()-0.5)]; end;
-->plot(xs1,r,x(r),)




72 2001 Gilberto E. Urroz
All rights reserved
The discrete Fourier transform and a graph of the magnitude of the transform coefficients is
shown next:

-->Xf1=fft(xs1,-1);
-->Xf1A=abs(Xf1);
-->plot(Xf1A,'w','|X(w)|')



The original signal obtained by adding uniformly generated random numbers (with SCILAB
function rand) produces what is typically known as white noise, i.e., a random signal where it
is very difficult to detect any periodicity. The resulting Fourier transform coefficients show
one single large component, for k=1, and almost negligible components for the rest of the
frequency spectrum.

Example of FFT applications: Obtaining an inverse Fourier transform

To generate a real signal out of a vector of Fourier transform coefficients we use the following
approach so that the vector of coefficients presents the symmetry characteristics defined
earlier. We will use a vector of coefficients with 201 components:

-->m=100;X0 = int(200*(rand(1,m)-0.5))+%i*int(100*(rand(1,m)-0.5));
-->n=201;Xf2 = [50 X0 zeros(1,m)];
-->for j=2:n/2+1, Xf2(n-j+2)=conj(Xf2(j)); end;
-->Xf2A=abs(Xf2);
-->plot2d3('onn',[1:1:201]',Xf2A')
-->xtitle('Symmetric Fourier coefficients','k','|X(k)|')


73 2001 Gilberto E. Urroz
All rights reserved


Although the plot of the magnitude of the coefficients seems random, if you observe the graph
carefully you will see the symmetry of the coefficients about the middle point.

The following call to function fft produces the signal. A plot of the real part of the signal is
also shown:

-->xs2=fft(Xf2,1);
-->xs2=fft(Xf2,1);xs2=real(xs2);plot(xs2,'r','x(r)','Real signal from IFFT')


Discrete Fourier transform for two-dimensional functions

While function dft applies to a single vector of data, function fft can be applied also to a
matrix representing a two-dimensional signal. You can actually apply fft to an array of N
dimensions (N>2) by using SCILAB hypermatrices (see --> help hypermat). The call to fft
when using hypermatrices is different from the one shown above. For more information the
reader is referred to the SCILAB help facility (--> help fft).

To understand the application of Fourier transforms to two-dimensional data we start with a
two-dimensional array of data in the rectangle defined by 0 < x < L
x
, 0 < y < L
y
. Suppose that
the rectangular region is divided in a grid of n
1
points along the x-axis and n
2
points along the
y-axis so that at point z
rs
= z(x
r
,y
s
) represents the values of the data array. The coefficients
of the two-dimensional Fourier transform for the data array z are given by


74 2001 Gilberto E. Urroz
All rights reserved
, )] ( 2 exp[
1
1
0
1
0 2 1 2 1
1 2
""

=
n
r
n
s
rs km
n
s m
n
r k
i y
n n
Z


for k=0,1,2,,n
1
-1 and m=0,1,2,,n
2
-1. On the other hand, the two-dimensional inverse
Fourier transform will produce the values y
rs
according to


, )] ( 2 exp[
1
0
1
0 2 1
1 2
""

=
n
r
n
s
km rs
n
s m
n
r k
i Y z

for r=0,1,2,,n
1
-1 and s=0,1,2,,n
2
-1.

Example of FFT applications: Obtaining the discrete Fourier transform for a two-dimensional
signal

We will generate a two-dimensional signal by defining the following function, which involves a
random component. A plot of such signal is shown next:

-->deff('[w]=f(x,y)','w=sin(x)*cos(y) + (rand()-0.5)')
-->x=[-6:0.5:6];y=x;z=feval(x,y,f);
-->plot3d(x,y,z)




The following call to function fft generates the coefficients of the corresponding two-
dimensional Fourier transform. To visualize the coefficients we plot their magnitudes using
function hist3d (three-dimensional histogram -- see Chapter 3):

-->Z = fft(z,-1);ZA=abs(Z);

-->hist3d(ZA)


75 2001 Gilberto E. Urroz
All rights reserved


The figure depicting the magnitudes of the Fourier transform coefficients show four
components that tower above the remaining set of components. There is indeed only one
major component, the one shown located closest to the origin (0,0). The other three
components are simply reflections of the major component about of lines of symmetry located
in the middle of the ranges of X and Y in the figure. This property of the two-dimensional
Fourier transform of a real signal follows, as in the one-dimensional case, from the fact that
Fourier transforms assume a certain periodicity in the independent variables of the function.

Example of FFT applications: Obtaining a real two-dimensional inverse Fourier transform

In order to obtain a real two-dimensional Fourier transform, the matrix of values Z
km
must
satisfy certain symmetry conditions. For the case in which the n
1
n
2
matrix of coefficients for
the Fourier transform is such that both n
1
and n
2
are even, the values Z(1,1), Z(n
1
/2+1,1),
Z(1,n
2
/+1), and Z(n
1
/2+1,n
2
/2+1) must always be real. The remaining elements of matrix Z are
such that Z(n
1
-k+1,1) = Z(k,1), for k = 1,2,,n
1
/2, Z(1,n
2
-m+1) = Z(1,m+1), for m = 1,2,,n
2
,
and Z(n
1
-k+1,n
2
-m+1) = Z(k,m), for k = 1,2,,n
1
, m = 1,2,,n
2
, whereZ represents the
complex conjugate of Z.

The following SCILAB script (called Zgen) produces a matrix of Fourier transform coefficients
from which a signal (inverse Fourier transform) is generated:

n1=16;n2=16; Z=zeros(n1,n2);

for i = 1:n1
for j = 1:n2
Z(i,j) = (int(100*((rand()-0.5)+%i*(rand()-0.5))));
end;
end;

for i = 2:n1/2
Z(n1-i+2,1) = conj(Z(i,1));
end;

for j = 2:n2/2
Z(1,n2-j+2) = conj(Z(1,j));
end;

for i = 2:n1
for j = 2:n2
Z(n1-i+2,n2-j+2) = conj(Z(i,j));

76 2001 Gilberto E. Urroz
All rights reserved
end;
end;
Z(1,1) = 10; Z(n1/2+1,1) = 5.0; Z(1,n2/2+1) = 3.0;
Z(n1/2+1,n2/2+1) = 2.0;

x=[1:1:n1]; y=[1:1:n2];

xset('window',1);plot3d(x,y,abs(Z));
xtitle('Magnitude of Fourier transform coefficients');

z=fft(Z,1);

xset('window',2);plot3d(x,y,real(z));
xtitle('Real signal generated with fft');

To run the script use function exec. Thus, if the script is stored in SCILABs current working
directory, you would use:

-->exec(Zgen)

The results are the following two graphs, the first one showing a plot of the magnitude of the
Fourier series coefficients and the second one showing the signal generated with function fft:







77 2001 Gilberto E. Urroz
All rights reserved
E Ex xe er rc ci is se es s

[1]. Verify the following formula using Bessel functions of the first kind, J
n
(x), for values of n =
2, 3, and 4:

. ) ( ) 1 ( ) ( '
2
1
) (
2
2
2
2
1
0
2
#
$
%
&
'
(
+ =
!


n n n
J
n
J dx x J x


[2]. Verify the following formulae using Bessel functions of the second kind, Y
n
(x), for values of
n = 2, 3, and 4 by plotting both sides of the equations in the same set of graphs for 0<x<1.

(a) Y
n+1
(x) = (2n/x) Y
n
(x) - Y
n-1
(x) (b) Y
n
(x) = (1/2) [Y
n-1
(x)-Y
n+1
(x)]





[3]. Rodrigues formula defines the Legendre polynomials, P
n
(x), as

. ) 1 (
! 2
1
) (
2 n
n
n
n
n
x
dx
d
n
x P

=

Generate a sequence of Legendre polynomials using this formula for n = 0, 1, 2, 3, 4, 5. Use
SCILAB polynomials and the function derivat.

[4]. Verify the following recurrence formula for Legendre polynomials for n = 2, 3, 4, by
plotting both sides of the equation in the same set of axes for 0<x<1.

). (
1
) (
1
1 2
) (
1 1
x P
n
n
x xP
n
n
x P
n n n +
+

+
+
=

For problems [5] through [14] generate a table showing the calculation of the integral of f(x)
between the limits x=a and x=b using Gaussian quadrature for values of n = 2,3,4,,20.

[5] f(x) = 1/(1+ ln(x)), a = 1, b = 5

[6] f(x) = 1 + x
2
+ x
3
+ 1/x, a = 1, b= 3.5

[7] f(x) = exp(-x/5)/(1+x
2
), a = -2, b = 2

[8] f(x) = (x
2
+1)
1/2
, a = -1 , b = 1

[9] f(x) = (x
2
+1)
1/3
+2.5(x
2
+1)+1.5 , a = 0, b = 5

[10] f(x) = sinh(x)/(1+x
3
) , a = 0 , b = 3

[11] f(x) = exp(-x
2
/2)/(2)
1/2
, a = -4, b = 4


78 2001 Gilberto E. Urroz
All rights reserved
[12] f(x) = 2.5(1+x
2
+x
3
/3) cos(3x
2
+2), a = 0, b = 2

[13] f(x) = 4.2x
1/3
+ 3.245x
1/2
+ 1.4142, a = 0.25, b = 1.25

[14] f(x) = 0.33 ln((x+1)/(x
2
+5x+2)) , a = 0, b = 20

For problems [15] through [19] use Gauss-Laguerre quadrature to obtain the integral of the
functions f(x) in the interval [0,).

[15]. f(x) = 1/(x+1)
2


[16]. f(x) = exp(-x
2
)

[17]. f(x) = ln(1+e
-x
)

[18]. f(x) = 1/(1+x
2
)
2

[19]. f(x) = e
-x
/(x+100)

For problems [20] through [24] use Gauss-Hermite to obtain the integral of the functions f(x) in
the interval (-,).

[20]. f(x) = exp(-x
2
)

[21]. f(x) = exp(-x
2
)cos(x)

[22]. f(x) = 1/(1+x
2
)
2


[23]. f(x) = exp(-x
4
)

[24]. f(x) = 1/x
2
+ 1/x
4

For problems [25] through [29] use Filons formulae to calculate the integral of the functions
f(x) cos(kx) and f(x) cos(kx) between the integration limits x=a and x=b:

[25]. f(x) = x+1, a = 0, b = 2

[26]. f(x) = exp(x), a = 2, b = 10

[27]. f(x) = ln(1+x
2
), a = 0, b = /2

[28]. f(x) = 1/(1+x), a = 0, b = 2

[29]. f(x) = |x|, a = -1, b = 1

[30]. Verify, using SCILAB, that Hermite polynomials satisfy the following formula:

7
8
9
3
4
5
=

=
!

n m n
n m
dx x H x H e
n n m
x
, ! 2
, 0
) ( ) (
2



Use values of (n,m) = (2,3) and (n,m) = (3,3).

79 2001 Gilberto E. Urroz
All rights reserved

[31]. Verify, using SCILAB, that Laguerre polynomials are orthogonal in the range [0,) with
respect to the function e
-x
,i.e., verify that

0 ) ( ) (
0
=
!

dx x L x L e
n m
x


Use values of (n,m) = (2,3), (2,4), (3,4).

[32]. (a) Verify that the second-order, ordinary differential equation, y+y = 0, with boundary
conditions y(0) = 0, y(1) = 0, is a Sturm-Liouville system. (b) Find the eigenvalues, , of the
system by first obtaining a general solution of the equation and replacing the boundary
conditions in this solution. (c) Find the corresponding eigenfunctions. (d) Verify that the
eigenfunctions are orthogonal in the interval [0,1].

[33]. Determine and plot the sine series corresponding to the following functions:
f(x) = 4, 0<x<2, and f(x) = -4, 2<x<4, period L = 4
f(x) = -x
2
, - 4 < x < 0, and f(x)= x
2
, 0 < x < 4, period L = 8
f(x) = sin(x
2
+x/2), 0 < x < 5, period L = 5
f(x) = 2 - x, 0 < x < 4, and f(x) = x - 6, 4 < x < 8, period L = 8


[34]. Determine and plot the cosine series corresponding to the functions of problem [33].


[35]. Determine and plot the Fourier series corresponding to the functions of problem [33].


[36]. Determine the Fourier series corresponding to the following functions:
f(x) = tanh(x), 0 < x < , period L =
f(x) = 1/(1+x+x
2
), 0 < x < 1, period L = 1
f(x) = x exp(-x), 0 < x < 2, period L = 2
f(x) = x( -x), 0 < x < , period L =

[37]. Solve the following differential equation replacing the function f(x) with the Fourier
series of problem [16]. Plot the solution for 0 < x < 4L, where L is the period of the function as
specified in problem [16]. Use as initial conditions y(0) = 0, y(0) = :

). (
2
1
3
1
2
2
x f y
dx
dy
dx
y d
= + +


[38]. Solve the following differential equation replacing for f(x) each of the Fourier series in
problem [17]. Plot the solution for 0 < x < 4L, where L is the period of the function as
specified in problem [17]. Use as initial conditions y(0) = 1, y(0) = 1:

). (
2
2
x f y
dx
y d
= +


80 2001 Gilberto E. Urroz
All rights reserved
[39]. Solve the following differential equation replacing the periodic signals f(t), of period T =
4, shown in the figure below with their corresponding Fourier series. Plot the solution for 0 < t
< 20. Use as initial conditions, x(0) = 1/3, x(0) = -1:

). (
2
2
t f x
dt
dx
dt
x d
= + +



[40]. SCILABs function rand can be used to generate random numbers that have a uniform
distribution in [0,1]. Use the following commands to generate 1000 random numbers uniformly
distributed in [-0.5,0.5]:

-->rand(uniform); dx = rand(1,1000)-0.5;

The vector dx now contains those random numbers. A plot of the signal dx
k
vs. k, for k =
1,2,1000, represents what is commonly known as random noise or white noise (reference to
the frequency contents of white light in the electromagnetic spectrum).

Next, produce a signal x by using the formula

, ,
1
1 1 1 "
=
+
= =
k
i
i k
dx x dx x

for k = 1,2, , 999. The signal thus generated, x
k
, represents what is known as a fractal
Brownian signal.

81 2001 Gilberto E. Urroz
All rights reserved

(a) Obtain and plot the magnitude of the Fourier transform coefficients, |DX(k)|, for the
white noise signal dx. What can be said about the dependence of |DX(k)|, on k, k =
1,2,,1000?
(b) Obtain and plot the magnitude of the Fourier transform coefficients, |X()|, for the
fractal Brownian signal x. . What can be said about the dependence of |DX(k)|, on k, k
= 1,2,,1000?

[41]. Repeat problem [40] using a normally distributed set of random numbers with function
rand. Use the following SCILAB commands to generate signal dx:

-->rand(normal); dx = rand(1,1000)-0.5;

[42]. Fractal signals are characterized by having Fourier transform coefficients that vary
inversely with the frequency, i.e., |X(k)| = b/k, where b = constant. Generate and plot a real
signal x(t) corresponding to 1000 Fourier transform coefficients given by |X(k)| = b/k, for k =
1,2,,1000, and b = 100. Is the signal generated similar in shape to signal x generated in
problems [40] and [41]?

[43]. Generate a two-dimensional signal by using the function

w(x,y) = sqrt(x^2+y^2) + (rand()-0.5)

and ranges of values of [-2:0.1:2] in x and y. Obtain and plot the two-dimensional Fourier
transform using (a) a three-dimensional histogram, and (b) a surface plot.

[44]. A two-dimensional fractal signal can be generated by defining Fourier transform
coefficients of the form |X(k,m)| = b/(k
2
+m
2
)
1/2
. Generate a 20x20 set of Fourier transform
coefficients corresponding to a two-dimensional fractal signal, plot the magnitude of the
coefficients and the resulting two-dimensional signal.




528 2001 Gilberto E. Urroz
All rights reserved

Partial Differential Equations with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
PARTIAL DIFFERENTIAL EQUATIONS 2
D De ef fi in ni it ti io on ns s 2
C Cl la as ss si if fi ic ca at ti io on n o of f l li in ne ea ar r, , s se ec co on nd d- -o or rd de er r P PD DE Es s 3
Analytical solutions of PDEs 3
Analytical solutions to parabolic equations: one-dimensional solution of the heat equation 3
Analytical solutions to parabolic equations: One-dimensional solution of the wave
equation 9
Analytical solutions to hyperbolic equations: Two-dimensional solution to Laplace's
equation in a rectangular domain. 13
More solutions to Laplace equation in a rectangular domain 17
Superposition of solutions for cases [1] and [2] 21
Laplacian in polar-cylindrical coordinates 24
Solution to Laplace's equation in an annulus 24
Solution to the Laplace equation in a disk 28
Numerical solutions to partial differential equations 32
Numerical solution to parabolic PDEs: heat equation 32
A function for the explicit numerical solution of the heat equation 33
Numerical solution to hyperbolic PDEs: the wave equation 35
A function for the solution of the wave equation 36
Numerical solution to elliptic PDEs: Laplace equation 38
A function to solve Laplace equation in a rectangular domain 40
Exercises 42
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Partial Differential Equations
This chapter introduces basic concepts and definitions for partial differential equations (PDEs)
and solutions to a variety of PDEs. Applications of the method of separation of variables are
presented for the solution of second-order PDEs. The application of this method involves the
use of Fourier series that were introduced in Chapter 13. This chapter also presents numerical
solutions to simple second-order PDEs that describe physical systems. The aim of the chapter
is to demonstrate SCILAB applications in the solution of well-known PDEs from mathematical
physics, rather than being a comprehensive presentation on partial differential equations.
D De ef fi in ni it ti io on ns s
Equations involving one or more partial derivatives of a function of two or more
independent variables are called partial differential equations (PDEs).
Well known examples of PDEs are the following equations of mathematical physics in
which the notation: u =u/x, u
xy
=u/yx, u
xx
=
2
u/ x
2
, etc., is used:
[1] One-dimensional wave equation: u
tt
= c
2
u
xx
[2] One-dimensional heat equation: u
t
= c
2
u
xx
[3] Laplace equation: u
xx
+u
yy
= 0, (2-D), or u
xx
+u
yy
+u
zz
= 0 (3-D)
[4] Poisson equation: u
xx
+u
yy
= f(x,y ),(2-D), or u
xx
+u
yy
+u
zz
= f(x,y,z) (3-D)
The order of the highest derivative is the order of the equation. For example, all of
the PDEs in the examples shown above are of the second order.
A PDE is linear if the dependent variable and its functions are all of first order. All of
the PDEs shown above are also linear.
A PDE is homogeneous if each term in the equation contains either the dependent
variable or one of its derivatives. Otherwise, the equation is said to be non-homogeneous.
Equations [1], [2], and [3] above are homogeneous equations. Equation [4] is non-
homogeneous.
A solution of a PDE in some region R of the space of independent variables is a
function, which has all the derivatives that appear on the equation, and satisfies the
equation everywhere in R. For example, u = x
2
y
2
, u = e
x
cos(y), and u = ln(x
2
+y
2
), are all
solutions to the two-dimensional Laplace equation (equation [3] above).
In general there should be as many boundary or initial conditions as the highest order
of the corresponding partial derivative. For example, the one dimensional heat equation
(equation [2]) applied to a insulated bar of length L, will require an initial condition, say
u(x,t=0) = f(x), 0 < x < L,
as well as two boundary conditions, e.g., u(x=0,t) = u
0
and u(x=L,t) = u
L
, or, u
x
(x=0,t) = u
x0
and u
x
(x=L) = u
xL
, or some combination of these, for t >0.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
C Cl la as ss si if fi ic ca at ti io on n o of f l li in ne ea ar r, , s se ec co on nd d- -o or rd de er r P PD DE Es s
Linear, second-order PDEs, as the examples shown above as equations [1] through [4], are
commonly encountered in science and engineering applications. For that reason special
attention is paid in this section to this type of equations. First, we learn how to classify linear,
second-order PDEs as follows:
An equation of the form:
Au
xx
+ 2Bu
x
+ Cu
yy
= F(x,y,u,u
x
,u
y
),
is said to be:
elliptic, if AC B
2
>0, e.g., heat flow and diffusion-type problems.
parabolic, if AC B
2
= 0, e.g., vibrating systems and wave motion problems.
hyperbolic, if AC B
2
< 0, e.g., steady-state, potential-type problems.
Analytical solutions of PDEs
There are a variety of methods for obtaining symbolic, or closed-form, solutions to differential
equations. The method of separation of variables can be used to obtain analytical solutions
for some simple PDEs. The method consists in writing the general solution as the product of
functions of a single variable, then replacing the resulting function into the PDE, and
separating the PDE into ODEs of a single variable each. The ODEs are solved separately and
their solutions combined into the solution of the PDE.
In many cases, the ODEs resulting from the separation of variables produce solutions that
depend on a parameter known as an eigenvalue (if the eigenvalue appears in a sine or cosine
function that depends on time, it is referred to as an eigenfrequency). The solutions involving
eigenvalues are known as eigenfunctions.
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o p pa ar ra ab bo ol li ic c e eq qu ua at ti io on ns s: : o on ne e- -d di im me en ns si io on na al l
s so ol lu ut ti io on n o of f t th he e h he ea at t e eq qu ua at ti io on n
The flow of heat in a thin, laterally insulated homogeneous rod is modeled by
u/t = k(
2
u/x
2
),
where u = temperature, k = a parameter resulting from combining thermal conductivity and
density. The PDE is subject to the initial condition
u(x,0) = f(x)
and constant-value boundary conditions
u(0,t) = u
0
, and u(L,t) = u
L
.
The physical phenomenon described by this PDE and its initial and boundary conditions is
illustrated in the figure below with u
0
= u
L
= 0.
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
We will try to find a solution by the method of separation of variables. This method assumes
that the solution, u(x,t), can be expressed as the product of two functions, X(x) and T(t):
u(x,t) = X(x)T(t).
With this substitution, the initial condition, u(x,t=0) = f(x) = X(x)T(t), can be treated as the set
of conditions: X(x) = f(x), when t = 0 [i.e., T(t) = 1]. Also, the boundary conditions, u(0,t) =
X(0)T(t) = u
0
, and u(L,t) = X(L)T(t) = u
L
, can be treated as X(0) = u
0
, and X(L) = u
L
, as long as
T(t) 0.
The derivatives of u(x,t) are calculated as follows:
and
Replacing these derivatives in the heat equation we get:

2
x
2
( ) X x ( ) T t

t
( ) u , x t ( ) X x

t
( ) T t
= ( ) X x

t
( ) T t k

2
x
2
( ) X x ( ) T t

2
x
2
( ) u , x t
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
Dividing by u(x,t) = X(x)T(t):
This result is only possible if both sides of the equation are equal to a constant, say -, since
the left-hand side is only a function of t and the right hand side is only function of x. The left-
hand side of the heat equation produces an ODE with independent variable t:
Whose solution is:
On the other hand, the right-hand side of the heat equation produces an ODE with independent
variable is x:
A general solution for X(x) is:
Next, we replace the boundary condition X(0) = 0, which results in the equation 0 = _C2, or
_C2 = 0. With this result, the solution simplifies to:
= ( ) T t e
( ) t
= ( ) X x _C1

sin

k
x
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
The second boundary condition, X(L) = 0, produces:
The latter result indicates an eigenfunction problem. We need to find all possible values of
for which this equation is satisfied. Since we want _C1 0, then we set
This equation has multiple solutions located at,
! ! , 3 , 2 , , 0 , , 2 , 3

=
k
L
i.e.,
or,
Therefore, with these values of the solution for X(x) now becomes:
=

sin L

k
0
:=
n
2

2
k
L
2
= ( ) X x _C1

sin
n
2

2
L
2
x
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
The value of _C1 remains somewhat arbitrary, requiring a different approach to find it. To
simplify notation we will replace _C1 with b
n
:
With the value of found earlier, the solution for T(t) is now:
There will be a different expression for u(x,t) = X(t)T(t) for each value of n = 0 , 1, 2, 3, ....
Therefore, we will call the solution corresponding to a particular value of n u
n
(x,t) and write:
The form of the n-th solution, u
n
, suggests an expansion similar to a Fourier series expansion
for the overall solution, u(x,t), restricting the values of n to positive integers, i.e.,
. exp sin ) , ( ) , (
2
2 2
1 1

= =

=
L
kt n
L
x n
b y x u y x u
n
n
n
n n

with the values of b
n
obtained from the boundary condition, u(x,0) = f(x), i.e.,
The latter result is a Fourier sine series with the coefficients b
n
given by:
= ( ) X x b
n

sin
n
2

2
L
2
x
= ( ) T t e

n
2

2
k t
L
2
=
= n 1

sin
n x
L
b
n
( ) f x
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Example 1 Determine the solution for the one-dimensional heat equation subjected to u(0,t)
= u(L,t) = 0, if the initial conditions are given by u(x,0) = f(x) = 4(x/L) (1-x/L)
.
Use values of
k=1 and L=1.
-->deff('[w]=f(x)','w=4*(x/L)*(1-x/L)')//Define initial conditions, u(x,0)=f(x)
-->deff('[w]=g(x)','w=f(x)*sin(n*%pi*x/L)')//Integrand for Fourier coefficients
-->deff('[bb]=b(n)','bb=intg(0,L,g,0.001)') //Define Fourier coefficients
-->L=1;k=1;
-->bb=[];for n=1:40, bb=[bb b(n)]; end; //Calculate Fourier coefficients
Next, we define the function u(x,t), and calculate values of u(x,t) in the ranges 0<x<1,
0<t<0.25, which are plotted in three dimensions:
-->deff('[uu]=u(x,t)',['uu=0';'for j=1:40';...
-->'uu=uu+bb(j)*sin(j*%pi*x/L)*exp(-j^2*%pi^2*k*t/L^2)';'end'])
-->xx=[0:0.05:1];tt=[0:0.025:0.25];
-->uu=feval(xx,tt,u);
-->plot3d(xx,tt,uu,45,45,'x@t@u(x,t)')
Plots of the functions u(x,t
0
) = f
0
(x), for specific values of t (i.e., t = t
0
) are shown in the
following plot:
-->plot2d([xx' xx' xx' xx' xx'],[uu(:,1),uu(:,3),uu(:,6),uu(:,9),uu(:,11)],...
-->[1:1:5],'111','t=0@t=0.075@t=0.130@t=0.195@t=0.25',[0,0,1,0.5])
-->plot2d([xx' xx' xx' xx' xx'],[uu(:,1),uu(:,3),uu(:,6),uu(:,9),uu(:,11)],...
-->[-1:-1:-5],'111','t=0@t=0.075@t=0.130@t=0.195@t=0.25',[0,0,1,0.5])
-->xtitle('Heat equation solution','x','u(x,t)')
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o p pa ar ra ab bo ol li ic c e eq qu ua at ti io on ns s: : O On ne e- -d di im me en ns si io on na al l s so ol lu ut ti io on n o of f
t th he e w wa av ve e e eq qu ua at ti io on n
The wave equation, shown below, can be used to model the displacement of an elastic string or
the longitudinal vibration of a beam:
For the case of an elastic string,
= c
2
T

, where T is the constant tension in the string and

is
the mass per unit length of the string. For the case of longitudinal vibration of a beam,
= c
2
g E

, where g is the acceleration of gravity, E is the modulus of elasticity, and

is the
density of the beam.
Suppose we solve the wave equation for a vibrating string of length L using separation of
variables with the boundary conditions u(0,t) = u(L,0)= 0. Also, the initial shape of the string is
given by u(x,0) = f(x), and the initial speed of the string is given by
=

t
( ) u , x t ( ) g x
at
= t 0
. We
postulate a solution of the form u(x,t) = X(x)T(t), and replace this result in the original PDE:
= ( ) X x

2
t
2
( ) T t c
2

2
x
2
( ) X x ( ) T t
Dividing both sides of the equation by u(x,t), we get:
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
This equation is only possible if the two sides of the equations are equal to a constant, say,

2
. With this we can write two ODEs, one for each side of the equation WaveEqn1, i.e.,
The solutions to these equations are:
Note: the constants _C1 and _C2 in the two solutions are not the same. The boundary
conditions u(0,t) = u(L,t) = 0 translate into X(0) = X(L) = 0. With these boundary conditions, we
can form the following algebraic equations:
:= _C2 0
We have an eigenvalue equation given by
=

sin
L
c
0
. The solution to this equation is
L
c
=
0,

,
2
,..., or

= 0,
c
L
,
2 c
L
,.... Using only the positive values, we can write
=
n c
L
,
n
= 0,1,2,.... Next, using the value of

and _C2 = 1, we obtain an expression for X(x) as


follows:
:=
n c
L
:= Sol2 = ( ) X x

sin
n x
L
Function T(t) gets written as:
= ( ) T t + _C1

cos
n c t
L
_C2

sin
n c t
L
The function u(x,t) is now:
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Application of the initial conditions provide the following equations:
Since the last two equations need to apply for n = 0,1,2, ..., we recognize in them the
equations that define Fourier series if we use _C1 = a
n
, and _C2 = b
n
, i.e.,
= ( ) f x a
n

sin
n x
L
= ( ) g x

sin
n x
L
b
n
n c
L
The equations defining coefficients a
n
and b
n
are given by:
a
n
=
2
L
d

0
L
( ) f x

sin
n x
L
x , and b
n
=
2
n c
d

0
L
( ) g x

sin
n x
L
x , for
n
=
, , 1 2 3
,...
The final solution is, therefore,
Example 1 - Consider the case of a vibrating string with the initial displacement given by f(x)
=
x
L
(
1
x
L
), and the initial velocity given by g(x) =

x
L
2
(
1
x
L
). The boundary conditions
are u(0,t) = 0, u(L,t) = 0. Determine the solution u(x,t) for this problem using components of
the resulting Fourier series for n = 1, 2, ..., 20, if c = 1 and L = 1.
The first three definitions correspond to function f(x), the integrand for a
n
, and a
n
itself. The
next three definitions correspond to function g(x), the integrand for b
n
, and b
n
itself.
-->deff('[w]=f(x)','w=(x/L)*(1-x/L)')
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)')
-->deff('[aa]=a(n)','aa=(2/L)*intg(0,L,ff,0.001)')
-->deff('[w]=g(x)','w=(x/L)^2*(1-x/L)')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[bb]=b(n)','bb=(2/(n*%pi*c))*intg(0,L,gg,0.001)')
Next, we define the values of L and c and calculate the coefficients a
n
and b
n
:
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
-->L=1;c=1;
-->aa=[];for n=1:20, aa=[aa a(n)]; end;
-->bb=[];for n=1:20, bb=[bb b(n)]; end;
The next step is to define the function u(x,t) and calculate values of the same for x in the
range 0<x<1 and t in the range 0<t<4. A three dimensional plot of u(x,t) is shown next:
-->deff('[uu]=u(x,t)',['uu=0';'for n=1:20';
-->'uu=uu+sin(n*%pi*x/L)*(aa(n)*cos(n*%pi*c*t/L)+bb(n)*sin(n*%pi*c*t/L))';
-->'end'])
-->x=[0:0.05:1];t=[0:0.1:4];
-->uu=feval(x,t,u);
-->plot3d(x,t,uu,45,45,'x@t@u(x,t)')
An alternative way to present the result is to plot u(x,t
0
) vs. x for selected values of t
o
as shown
in the next SCILAB commands:
-->plot2d([x' x' x' x' x'],[uu(:,1),uu(:,5),uu(:,10),uu(:,15),uu(:,20)],...
-->[-1:-1:-5],'111','t=0.05@t=0.25@t=0.50@t=0.75@t=1.0',[0 -0.3 1 0.3])
-->plot2d([x' x' x' x' x'],[uu(:,1),uu(:,5),uu(:,10),uu(:,15),uu(:,20)],...
-->[1:1:5],'011','t=0.05@t=0.25@t=0.50@t=0.75@t=1.0',[0 -0.3 1 0.3])
-->xtitle('Wave equation solution','x','u(x,t)')
The plot thus generated is shown below:
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
A An na al ly yt ti ic ca al l s so ol lu ut ti io on ns s t to o h hy yp pe er rb bo ol li ic c e eq qu ua at ti io on ns s: : T Tw wo o- -d di im me en ns si io on na al l
s so ol lu ut ti io on n t to o L La ap pl la ac ce e' 's s e eq qu ua at ti io on n i in n a a r re ec ct ta an ng gu ul la ar r d do om ma ai in n. .
Laplace's equation in two-dimensions is given by
= +

2
x
2
( ) u , x y

2
y
2
( ) u , x y 0 .
In problems related to heat transfer, the two-dimensional Laplace equation describes the
steady state distribution of temperature u(x,y) in the x-y plane. In fluid mechanics, u(x,y)
could describe the velocity potential or the streamfunction for a two-dimensional potential
flow. The problem requires two boundary conditions in the two independent variables x and
y.
Laplace equation is solved in a rectangular domain so that 0<x<L, 0<y<H, a suitable set of
boundary conditions may be

= ( ) u , x 0 0
,
= ( ) u , x H ( ) g x
,
( ) u , 0 y
=
0
,
( ) u , L y
=
0
.
as illustrated in the figure below:
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
Separation of variables suggests that we use a solution of the form,
= ( ) u , x y ( ) X x ( ) Y y
.
Solving the equation through separation of variables proceeds in the following fashion:
=

2
x
2
( ) X x ( ) Y y ( ) X x

2
y
2
( ) Y y
Dividing by u(x,y) = X(x)Y(y) results in:
For the two sides of the resulting equation to be equal, they both must be equal to a constant,

2
, resulting in the following two ordinary differential equations:
The solution to the first ODE is:
We next use the boundary conditions:
= ( ) X 0 0
, and X(L) =
0
, to determine the constants of
integration:
_C2 = 0
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
This second equation produces an eigenvalue equation with the eigenvalues given by
=
n
L
.
:=
n
L
The solution for X(x), with _C1 = 1 (since the value _C1 is arbitrary), is, therefore:
= ( ) X x

cos
n x
L
The solution to the second ODE is:
Utilizing the boundary condition: Y(0) = 0, we find for Y(y):
:= _C3 0
= ( ) Y y _C4

sinh
n y
L
The product X(x)Y(y), which now depends on the value of the eigenvalue
=
n
n
L
, is referred
to as = ( ) v
n
, x y ( ) X x ( ) Y y , with the constant replaced by a
n
:
The solution will be the sum of all possible functions ( ) v
n
, x y , i.e.,
:= u ( ) , x y
= n 1

( ) v
n
, x y
The expression for the function
( ) u , x y
is:
If we now evaluate the boundary condition,
= ( ) u , x H ( ) g x
, we find the following equation:
This result is a Fourier series expansion to
( ) g x
, such that the constants a
n
are calculated by
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
Example 1: Suppose that the dimensions of the solution domain are L = 2 and H = 1, and the
boundary condition at y = H is given by
= ( ) g x 100

x
(
L x
)
3
.
We start by defining function g(x), the integrand for the Fourier series coefficients, gg(x), and
the Fourier series coefficients themselves, a(n).
-->deff('[w]=g(x)','w=100*x*(L-x)^3')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[aa]=a(n)','aa=2*intg(0,L,gg,0.0001)/(L*sinh(n*%pi*H/L))')
Next, we define the values of L and H, calculate 20 Fourier coefficients, and define and
evaluate the u(x,y) in the ranges 0<x<L, 0<y<H. A three-dimensional plot of the function is
shown:
-->L=2;H=1;
-->aa=[];for n=1:20, aa=[aa a(n)]; end;
-->deff('[uu]=u(x,y)',['uu=0';'for n=1:20';...
-->'uu=uu+aa(n)*sin(n*%pi*x/L)*sinh(n*%pi*y/L)';'end'])
-->x=[0:L/20:L];y=[0:H/20:H];
-->uu=feval(x,y,u);
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Solutions to Laplaces equation in two-dimensions can also be represented by contour plots as
shown below:
-->contour(x,y,uu,15)
-->xtitle('Contour plots for Laplace equation solution','x','y')
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
More solutions to Laplace equation in a rectangular domain
The solution obtained above was facilitated by the use of zero boundary conditions in three of
the boundaries. The zero boundary conditions at x = 0 and x = L produced the eigenvalues
=
n
n
L
,
while the zero boundary condition at y = 0 produced the series solution
= ( ) u , x y
= n 1

a
n

sin
n x
L

sinh
n y
L
.
Finally, the non-zero boundary condition at y = H, produced the coefficients a
n
for the
corresponding Fourier series. The case solved above is referred to as Case [1] in the figure
below.
Solutions to the case of a rectangular domain where only one of the boundaries is non-zero can
be found for cases [2] through [4] in the figure below using a procedure similar to that outlined
above for case [1].
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
Because Laplace's equation is a linear equation, i.e., , solutions
with different boundary conditions, in the same domain, can be superimposed. Therefore,
linear combinations of the solutions for the four cases illustrated in the figure above can be
added to solve problems involving non-zero boundary conditions in more than one boundary.
Suppose that ( ) u
1
, x y , ( ) u
2
, x y , ( ) u
3
, x y , and ( ) u
4
, x y , represent the solution for cases
[1],[2],[3], and [4], respectively, in the figure above. The most general problem in the
rectangular domain will use the following non-zero boundary conditions:
= ( ) u , x 0
1
( ) g x , = ( ) u , x H
2
( ) f x , = ( ) u , 0 y
3
( ) p y , and = ( ) u , L y
4
( ) q y .
The solution will be obtained as
= ( ) u , x y + + +
1
( ) u
1
, x y
2
( ) u
2
, x y
3
( ) u
3
, x y
4
( ) u
4
, x y .
Solution to case [2]
To solve case [2] in the figure above, we start by using separation of variables, u(x,y) =
X(x)Y(y), on the Laplace equation:
=

2
x
2
( ) X x ( ) Y y ( ) X x

2
y
2
( ) Y y
Dividing by u(x,y) = X(x)Y(y) results in:
For the two sides of the resulting equation to be equal, they both must be equal to a constant,

2
, resulting in the following two ordinary differential equations:
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
The solution to the first ODE is:
We next use the boundary conditions:
= ( ) X 0 0
, and X(L) =
0
, to determine the constants of
integration:
0 = _C2
0 = _C1 sin(x)
This second equation produces an eigenvalue equation with the eigenvalues given by
=
n
L
.
The solution for X(x), with _C2 = 1 (since the value _C2 is arbitrary), is, therefore:
= ( ) X x

cos
n x
L
The solution to the second ODE is:
Utilizing the boundary condition: Y(H) = 0, we find for Y(y):
or
:= _C4
_C3

cosh
n H
L

sinh
n H
L
= ( ) Y y
_C3

cosh
n y
L

sinh
n H
L

cosh
n H
L

sinh
n y
L

sinh
n H
L
The product X(x)Y(y), which now depends on the value of the eigenvalue
=
n
n
L
, is referred
to as = ( ) v
n
, x y ( ) X x ( ) Y y , with the constant replaced by a
n
:
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
The solution will be the sum of all possible functions ( ) v
n
, x y , i.e.,
The expression for the function
( ) u , x y
is:
If we now evaluate the boundary condition,
= ( ) u , x 0 ( ) g x
, we find the following equation:
This result is a Fourier series expansion to
( ) f x
, such that the constants a
n
are calculated by
Example 1: Suppose that the dimensions of the solution domain are L = 2 and H = 1, and the
boundary condition at y = H is given by
= ( ) f x 100

x
(
L x
)
3
.
-->L=2;H=1;
-->deff('[w]=f(x)','w=100*x*(L-x)^3') //Function f(x)
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)') //Integrand Fourier series coeff.
-->deff('[aa]=a(n)','aa=(2/L)*intg(0,L,ff,0.0001)') //Fourier coeff. defined
-->aa=[]; for n=1:20, aa=[aa a(n)]; end; //Calculate Fourier coeff.
The following statements define the solution u(x,y):
-->deff('[uu]=u(x,y)',['uu=0';'for n=1:20';
-->'uu=uu+aa(n)*sin(n*%pi*x/L)/sinh(n*%pi*H/L)*...
-->(cosh(n*%pi*y/L)*sinh(n*%pi*H/L)-cosh(n*%pi*H/L)*sinh(n*%pi*y/L))';
-->'end'])
To produce a three-dimensional plot of the solution we use:
-->x=[0:L/20:L];y=[0:H/20:H];uu=feval(x,y,u);
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
The solution can also be shown as a contour plot.
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - case 2','x','y')
Superposition of solutions for cases [1] and [2]
The superposition of the solutions for cases [1] and [2] will be the solution to the problem
whose boundary conditions are illustrated in the figure below:
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
We use for the solution
= ( ) f x x ( ) L x
3
, and
= ( ) g x x
3
( ) L x
. The following SCILAB statements
produce the solution to this problem.
-->deff('[w]=f(x)','w=x*(L-x)^3')
-->deff('[w]=g(x)','w=x^3*(L-x)')
-->deff('[w]=ff(x)','w=f(x)*sin(n*%pi*x/L)')
-->deff('[w]=gg(x)','w=g(x)*sin(n*%pi*x/L)')
-->deff('[aa1]=a1(n)','aa1=2*intg(0,L,gg,0.0001)/(L*sinh(n*%pi*H/L))')
-->deff('[aa2]=a2(n)','aa2=(2/L)*intg(0,L,ff,0.0001)')
-->L=2;H=1;
-->aa1=[]; for n=1:20, aa1=[aa1 a1(n)]; end;
-->aa2=[]; for n=1:20, aa2=[aa2 a2(n)]; end;
-->deff('[uu1]=u1(x,y)',['uu1=0';'for n=1:20';...
-->'uu1=uu1+aa1(n)*sin(n*%pi*x/L)*sinh(n*%pi*y/L)';'end'])
-->deff('[uu2]=u2(x,y)',['uu2=0';'for n=1:20';...
-->'uu2=uu2+aa2(n)*sin(n*%pi*x/L)/sinh(n*%pi*H/L)*...
-->(cosh(n*%pi*y/L)*sinh(n*%pi*H/L)-cosh(n*%pi*H/L)*sinh(n*%pi*y/L))';...
-->'end'])
-->x=[0:L/20:L];y=[0:H/20:H];
-->uu1=feval(x,y,u1);uu2=feval(x,y,u2);uu=uu1+uu2;
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - cases 1 & 2','x','y')
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
Suppose now, that the boundary conditions are such that = ( ) f
1
x 3 ( ) f x =
3 x ( ) L x
3
and
= ( ) g
1
x 5 ( ) g x =
5 x
3
( ) L x
. Since we already have the solutions corresponding to f(x) and g(x),
we can obtain the solution to the problem with the new set of boundary conditions by
constructing the following solution = ( ) u , x y + 3 ( ) u
1
, x y 5 ( ) u
2
, x y :
-->uu=3*uu1+5*uu2;
The three-dimensional plot corresponding to this solution is shown below:
-->plot3d(x,y,uu,45,45,'x@y@u(x,y)')
Contour plots of the solution are produced by using:
-->contour(x,y,uu,15)
-->xtitle('Laplace equation - cases 1 & 2','x','y')
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
Laplacian in polar-cylindrical coordinates
Consider the function
= u ( ) u , , r z
in polar coordinates. The Laplacian of this function in polar
coordinates can be obtained from Maple using:
+ + +

r
( ) u , , r z r

2
r
2
( ) u , , r z

2
( ) u , , r z
r
r

2
z
2
( ) u , , r z
r
You can check that the first two terms are equivalent to

r
r

r
( ) u , , r z
, i.e.,
+

r
( ) u , , r z r

2
r
2
( ) u , , r z
Thus, the Laplacian of u(x,y,z) can be written as:
=
1
r
+

r
r

r
( ) u , , r z
1
r
2

2
( ) u , , r z +

2
z
2
( ) u , , r z
In the next section we will discuss the solution of the Laplace equation in two dimensions using
polar-cylindrical coordinates, the corresponding Laplacian, taking
= u ( ) u , r
and
=

z
u 0
, is
=
1
r
+

r
r

r
( ) u , r
1
r
2

2
( ) u , r
Solution to Laplace's equation in an annulus
Consider the domain of solution to Laplace equation to be an annulus defined, in polar
coordinates, as r
1
<
r
< r
2
, and 0 <

<
2
, subjected to the boundary conditions:
= ( ) u , r
1
u
1
= constant, and = ( ) u , r
2
u
2
= constant, as illustrated in the figure below:
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
In polar coordinates, Laplace's equation is written as:
1
r
+

r
r

r
( ) u , r
1
r
2

2
( ) u , r = 0.
If the solution is assumed to depend only on the radial coordinate,
= u ( ) u r
, and
=

u 0
.
Thus, Laplace's equation reduces to
1
r

r
r

r
( ) u r
= 0. The later result represents an
ordinary differential equation and we could write it as
1
r

d
dr
(
r

du
dr
) = 0,
or
d
dr
(
r

du
dr
) = 0.
Integration of this equation produces: u(r) = _C1 + _C2 ln(r).
Using the boundary conditions indicated above we obtain:
.
ln
) ln( ) ( ) ln( ) ln(
) (
2
1
1 2 2 1 1 2


=
r
r
r u u r u r u
r u
For example, if u
1
=1, r
1
=1, u
2
= 10, r
2
= 5, the distribution of u(r) is illustrated in the following
figure.
-->u1=1;r1=1;u2=10;r2=5;
-->deff('[uu]=u(r)','uu=(u2*log(r1)-u1*log(r2)-(u2-u1)*log(r))/log(r1/r2)')
-->r=[r1:(r2-r1)/100:r2];uu=u(r);
-->plot(r,uu,'r','u(r)','Laplace equation in a disk')
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
__________________________________________________________________________________
Three-dimensional surface plot in polar coordinates
To produce a three-dimensional surface plot of a function z = f(r,) we propose the following
function, plotpolar. The call to the function is
plotpolar(r_range,theta_range,nx,ny,f)
where r_range is the vector [rmin,rmax], theta_range is the vector [theta_min, theta_max]
where the function f is defined. Since the function uses function plot3d to produce the surface
plot, we need to produce a rectangular x-y grid. The number of divisions in the x and y
directions are given by the numbers nx and ny, respectively. The function returns two plots, a
three-dimensional surface plot and a contour plot of the function f. A listing of function
polarplot follows:
function [] = plotpolar(rrange,trange,n,m,f)
//A function to produce a three-dimensional
//plot out of a function z = f(r,theta)
xmin=-rrange(2);xmax=+rrange(2);xrange=xmax-xmin;
ymin=-rrange(2);ymax=+rrange(2);yrange=ymax-ymin;
xx=[xmin:xrange/n:xmax];
yy=[ymin:yrange/m:ymax];
nx=length(xx);ny=length(yy);
zz=zeros(nx,ny);
for i = 1:nx
for j = 1:ny
rr=norm([xx(i) yy(j)]);
if xx(i) == 0 & yy(j) == 0 then
th = 0.0
else
th=atan(xx(i),yy(j));
end;
if rr>=rrange(1) & rr<=rrange(2) &...
th>=trange(1) & th<=trange(2) then
zz(i,j) = f(rr,th);
end;
end;
end;
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
xset('window',1);xbasc();plot3d(xx,yy,zz);
xset('window',2);xbasc();contour(xx,yy,zz,10);
The range of values for both x and y is [-rmax,rmax]. To obtain a full surface plot of the
function f use theta_range = [-%pi,%pi]. To obtain half-a-plot use theta_range=[0,%pi].
The function f must be defined as a function of both r and , even if it depends only on one of
the variables. Thus, for the solution of the Laplace equation in an annulus obtained above, we
will re-define the function u as shown below.
-->u1=1;r1=1;u2=10;r2=5;
-->deff('[uu]=u(r,t)','uu=(u2*log(r1)-u1*log(r2)-(u2-u1)*log(r))/log(r1/r2)')
To produce a half-plot of the function u(r) we use:
-->r_range = [r1,r2]; theta_range = [0,%pi];
-->plotpolar(r_range,theta_range,40,40,u)
Next, we redefine the range of to produce a full plot of the function:
-->r_range = [r1,r2]; theta_range = [-%pi,%pi];
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
-->plotpolar(r_range,theta_range,40,40,u)
__________________________________________________________________________________
Solution to the Laplace equation in a disk
Consider the case in which
= u ( ) u , r
as solution to the Laplace equation on a disk centered at
the origin and subjected to the boundary condition,
= ( ) u , R ( ) f
, where R is the radius of the
disk. Using separation of variables we can write
= ( ) u , r ( ) R r ( ) T
, requiring that
( ) T
be
periodic in

with period
2
. Thus, we can write
( ) T
as a Fourier series, i.e.,
( ) T
= +
a
0
2

= k 1

( ) + a
k
( ) cos k b
k
( ) sin k .
The procedure for the solution using separation of variables proceeds as follows. The Laplace
equation in cylindrical coordinates is written as:
To separate the equation into ordinary differential equations we use u(r, ) = R(r)T():
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
The differential equations are:
Since the expression
( ) ln r
is equivalent to
( ) ln r

, we can re-write R(r) as:


+ _C1


1
2
r

1
2
1
r

_C2

+
1
2
r

1
2
1
r

Or, redefining the constants,


Typically, the values of

are positive values, therefore, the term


r
( )
grows unbounded as
r 0
. This requires that we eliminate the term
r
( )
from the solution by making _C2 = 0,
thus:
= ( ) R r _C1 r

The second ODE obtained from separation of variables and its solution are:
The latter result indicates that the initial assumption of a periodic function for
( ) T
is
acceptable if we take
= k
. We now replace the solution to the second ODE with:
The combined solution,
( ) u , r
=
( ) R r ( ) T
, is now:
The boundary condition,
= ( ) u , R ( ) f
, produces:
With the coefficients of the resulting Fourier series given by:

= a
0
1

0
2
( ) f
, a
k
=
1
R
k

d

0
2
( ) f ( ) cos k
, and b
k
=
1
R
k

d

0
2
( ) f ( ) sin k
.
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
For example, if R = 10, and
= ( ) f + ( ) sin 2.5 ( ) sin 2 1.5 ( ) cos 3
, we can obtain the solution
u(r,) using the following SCILAB commands:
-->deff('[w]=f(t)','w=sin(t)-2.5*sin(2*t)+1.5*cos(3*t)') //define f(theta)
-->R = 10;
-->a0=(1/%pi)*intg(0,2*%pi,f,0.0001) //Calculate a0
a0 = - 1.414E-16
-->deff('[w]=fc(t)','w=f(t)*cos(k*t)') //Integrand for cosine coefficients
-->deff('[w]=fs(t)','w=f(t)*sin(k*t)') //Integrand for sine coefficients
-->deff('[aa]=a(k)','aa=intg(0,2*%pi,fc,0.0001)/(%pi*R^k)')//cosine coeff. def
-->deff('[bb]=b(k)','bb=intg(0,2*%pi,fs,0.0001)/(%pi*R^k)')//sine coeff. def.
-->aa=[];for k=1:20, aa=[aa a(k)]; end; aa = clean(aa) //cosine coeff.
aa =
column 1 to 11
! 0. 0. .0015 0. 0. 0. 0. 0. 0. 0. 0. !
column 12 to 20
! 0. 0. 0. 0. 0. 0. 0. 0. 0. !
-->bb=[];for k=1:20, bb=[bb b(k)]; end; bb = clean(bb) //sine coeff.
bb =
column 1 to 11
! .1 - .025 0. 0. 0. 0. 0. 0. 0. 0. 0. !
column 12 to 20
! 0. 0. 0. 0. 0. 0. 0. 0. 0. !
Next, we define the function u(r,), and produce a plot of the solution using function
plotpolar, first on an x-y grid of 30 30 and then on a 40 40 grid.
-->deff('[uu]=u(r,t)',['uu=a0/2';'for k=1:20';
-->'uu=uu+r^k*(aa(k)*cos(k*t)+bb(k)*sin(k*t))';'end'])
-->r_range=[0,R]; theta_range=[-%pi,%pi];
-->getf('plotpolar')
-->plotpolar(r_range,theta_range,30,30,u)
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
-->plotpolar(r_range,theta_range,40,40,u)
A contour plot of the function u(r,) on a 40 40 x-y grid is shown next.
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
Numerical solutions to partial differential equations
The numerical solution to partial differential equations is a subject that may fill several
volumes. In this section we present some simple numerical solutions to elliptic, parabolic, and
hyperbolic partial differential equations. The examples presented herein are aimed at
demonstrating the use of SCILAB in simple numerical solutions to PDEs.
Numerical solution to parabolic PDEs: heat equation
The unsteady heat equation in one-dimension is given by
u/t = K(
2
u/x
2
),
for 0<x<L, t>0. In this equation u(x,t) represents the temperature on a uniform bar at location
x and at time t, and K is a constant containing information on the heat transfer ability of the
material under consideration. The solution to this equation requires one initial condition,
u(x,0)=f(x), and two boundary conditions, e.g., u(0,t) = u
0
(t), u(L,t) = u
L
(t).
Replacing the second-order partial derivatives
2
u/x
2
with a centered finite difference
approximation and the first-order partial derivatives with a forward difference approximation
results in
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
,
) (
) 2 (
2
, 1 , 1 1 ,
x
u u u K
t
u u
j i ij j i ij j i

+
=

+ +
or,
u
i,j+1
= u
ij
+(u
i-1,j
-2u
ij
+u
i+1,j
),
for i=1,2,,n, and j=1,2,, with
= Kt/(x)
2
.
The result shown above for u
i,j+1
= u(x
i
,t
j+1
) is an explicit solution for the temperature. Stability
conditions for this explicit solution requires that < 0.5.
To produce a solution we use a solution grid as shown below:
We load the initial conditions at t=0 and the boundary conditions at x = 0 and x = L for any time
t > 0, and proceed to calculate u
i,j+1
= u
ij
+(u
i-1,j
-2u
ij
+u
i+1,j
), for i=2,3,,n-1, and j=2,3,. The
result is a matrix u such that u
ij
= u(x
i
,t
j
).
A A f fu un nc ct ti io on n f fo or r t th he e e ex xp pl li ic ci it t n nu um me er ri ic ca al l s so ol lu ut ti io on n o of f t th he e h he ea at t e eq qu ua at ti io on n
The following function, HeatPDE1, produces the explicit solution to the heat equation. The
call to the function is
[u]=HeatPDE1(xrange,trange,uinit,u0bound,uLbound,K)
where xrange is a vector of length n containing the range of values of x uniformly distributed,
i.e., xrange = [x1:Dx:xn], trange is a vector of length m containing the range of values of t,
also uniformly distributed, i.e., trange = [t0:Dt:tm]. The vector uinit is a vector of length n
containing the initial conditions for u(x,t). Vectors u0bound and uLbound are vectors of length
m containing the values of the lower and upper boundary conditions for the different times t in
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
trange. The value K is the constant from the heat equation. The function return the matrix u
of size nxm representing the solution of the heat equation.
function [u]=HeatPDE1(xrange,trange,uinit,u0bound,uLbound,K)
n=length(xrange);
m = length(trange);
u = zeros(n,m);
Dx = (xrange(n)-xrange(1))/(n-1);
Dt = (trange(m)-trange(1))/(m-1);
u(:,1)=uinit';
u(1,:)=u0bound;
u(n,:)=uLbound;
alpha = K*Dt/(Dx)^2;
if alpha >= 0.5 then
printf('WARNING: The value of alpha is larger than or equal to 0.5.');
printf('The explicit solution is unstable and it will not be calculated.');
abort;
end;
for j = 1:m-1
for i = 2:n-1
u(i,j+1) = u(i,j)+alpha*(u(i-1,j)-2*u(i,j)+u(i+1,j));
end;
end;
Example of application of function HeatPDE1
We will solve the heat equation using an x range of [0:0.1:1] with a t range [0:0.005:1.5] and K
= 1.0. The initial conditions will be simply zero values, while the boundary conditions will be
u(0,t) = exp(-2t)sin(50t) and u(1,t) = exp(-3t)cos(50t).
-->K=1.0; //Heat eqn. constant
-->x=[0:0.1:1];t=[0:0.005:1.5];ui=zeros(x); //x,t,initial conditions
-->u0=exp(-2*t).*sin(50*t);uL=exp(-3*t).*cos(50*t); //boundary conditions
-->[u]=HeatPDE1(x,t,ui,u0,uL,K); //Solution u(x,t)
-->plot3d(x,t,u,45,45,'x@t@u(x,t)') //Three-dim. plot
-->xtitle('Heat equation solution')
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
Animation of the results can be produced by using the following SCILAB commands. The
animation presents the values of u(x,t) as function of x as time increases.
-->xset('pixmap',1);xbasc();
-->for j = 1:301,plot2d(x',u(:,j),1,'011',' ',[0 -1 1 1]),...
-->xset('wshow'),xset('wwpc'),end
This animation may run too fast to appreciate the details of the temperature variation. To
slow down the animation include an empty loop inside the main loop, as shown next.
-->for j = 1:301,plot2d(x',u(:,j),1,'011',' ',[0 -1 1 1]), for k=1:1000,end,
-->xset('wshow'),xset('wwpc'),end
The reader is invited to try the animations in his or her own SCILAB application.
Numerical solution to hyperbolic PDEs: the wave equation
The wave equation is given by

2
u/t
2
= c
2
(
2
u/x
2
),
where c is the velocity of propagation of one-dimensional waves in a medium along the x-
direction, with 0<x<L, and t>0. The equation requires for its solution two initial conditions,
typically, u(x,0) = f(x), and u/t|
t=0
= g(x), and two boundary conditions, u(0,t) = u
L
(t), and
u(L,t) = u
L
(t).
For the numerical solution of the wave equation we use a grid similar to that used in the
solution of the heat equation, with x increment x, and time increment t. The numerical
solution seeks to find the values u
ij
= u(x
i
,y
j
). The PDE is replaced by an algebraic equation
resulting of approximating the second-order derivatives with centered finite-difference
approximations:
(u
i,j-1
-2u
ij
+u
i,j+1
)/(t)
2
= c
2
(u
i-1,j
-2u
ij
+u
i+1,j
)/(t)
2
.
Defining a parameter

2
= c
2
(t)
2
/(x)
2
,
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
an explicit solution for u
i
,
j+1
= u(x
i
,t
j+1
) is
u
i,j+1
=
2
(u
i-1,j
+u
i+1,j
)+2(1-
2
)u
ij
- u
i,j-1
.
To introduce the initial conditions into the solution we first use the fact that u
i1
= u(x
i
,t
1
) =
g(x
i
).
The result obtained above for u
i,j+1
, when j=1 introduces the term u
i0
which is not defined:
u
i,2
=
2
(u
i-1,1
+u
i+1,1
)+2(1-
2
)u
i1
- u
i0
.
However, when we introduce the derivative initial condition, u/t|
t=0
= g(x), re-writing it in
terms of the centered finite-difference approximation,
(u
i,2
-u
i,0
)/(2t) = g(x
i
),
the term u
i0
appears again. From the last expression, we can write
u
i0
= -2tg(x
i
) + u
i2
,
which replaced in the expression for u
i2
results in
u
i2
=
2
(u
i-1,1
+u
i+1,1
)+2(1-
2
)u
i1
+ 2tg(x
i
) - u
i2
.
Solving for u
i2
we get
u
i2
=
2
(u
i-1,1
+u
i+1,1
)/2 + (1-
2
)u
i1
+ tg(x
i
) .
Thus, the latter result is used to calculate u for the first time step. For values of j=2,3,, we
calculate the solution with
u
i,j+1
=
2
(u
i-1,j
+u
i+1,j
)+2(1-
2
)u
ij
- u
i,j-1
.
A A f fu un nc ct ti io on n f fo or r t th he e s so ol lu ut ti io on n o of f t th he e w wa av ve e e eq qu ua at ti io on n
The following solution, WavePDE1, can be used to obtain the solution u(x,t) for the wave
equation. The call to the function is
[u] = WavePDE1(xrange,trange,uinit,duinit,u0bound,uLbound,c)
where xrange is a vector of length n containing the range of values of x uniformly distributed,
i.e., xrange = [x1:Dx:xn], trange is a vector of length m containing the range of values of t,
also uniformly distributed, i.e., trange = [t0:Dt:tm]. Vector uinit is a vector of length n
containing the initial conditions for u(x,t), while vector duinit is also a vector of length n
containing the derivative boundary condition, u/t|
t=0
= g(x). Vectors u0bound and uLbound
are vectors of length m containing the values of the lower and upper boundary conditions for
the different times t in trange. The value c is the constant from the wave equation. The
function return the matrix u of size nxm representing the solution of the wave equation.
function [u] = WavePDE1(xrange,trange,uinit,duinit,u0bound,uLbound,c)
n=length(xrange);
m = length(trange);
u = zeros(n,m);
Dx = (xrange(n)-xrange(1))/(n-1);
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
Dt = (trange(m)-trange(1))/(m-1);
u(:,1)=uinit';
u(1,:)=u0bound;
u(n,:)=uLbound;
alpha2 = c^2*Dt^2/Dx^2;
if alpha2 > 1.0 then
printf('WARNING: The value of alpha is larger than 1.0.');
printf('The explicit solution is unstable and it will not be calculated.');
abort;
end;
//first time step
for i = 2:n-1
u(i,2) = ...
alpha2*(u(i-1,1)+u(i+1,1))/2+(1-alpha2)*u(i,1)+Dt*duinit(i);
end;
//subsequent time steps
for j = 2:m-1
for i = 2:n-1
u(i,j+1)=...
alpha2*(u(i-1,j)+u(i+1,j))+2*(1-alpha2)*u(i,j)-u(i,j-1);
end;
end;
Example of application of function WavePDE1
In this example we solve the wave equation in the range 0 < x < 1, for 0 < t < 1.5, with initial
conditions f(x) = x(1-x), and g(x) = 0.2. The boundary conditions are u(0,t) = u(1,t) = 0. The
results obtained are shown as a three-dimensional surface u(x,t).
-->x=[0:0.1:1];t=[0:0.01:1.5];uinit=x.*(1-x);duinit=0.2*ones(x);
-->u0=zeros(t);uL=zeros(t);c=1;
-->u=WavePDE1(x,t,uinit,duinit,u0,uL,c);
-->plot3d(x,t,u,45,45,'x@t@u(x,t)')
-->xtitle('Wave Equation Solution')
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
Next, we re-calculate the solution to the wave equation for the time range 0 < t < 9 in order to
produced an animation of the function u(x,t) vs. x for different values of t. The instructions
for the animation follow. The user is invited to try the animation in his or her own SCILAB
application.
-->x=[0:0.1:1];t=[0:0.01:9];uinit=x.*(1-x);duinit=0.2*ones(x);
-->u0=zeros(t);uL=zeros(t);c=1;
-->u=WavePDE1(x,t,uinit,duinit,u0,uL,c);
-->size(u)
ans =
! 11. 901. !
-->min(u),max(u)
ans = - .2770746
ans = .2821966
-->xset('pixmap',1);xbasc();
-->for j = 1:901,plot2d(x',u(:,j),1,'011',' ',[0 -0.3 1 0.3]),...
-->xset('wshow'),xset('wwpc'),end
N Nu um me er ri ic ca al l s so ol lu ut ti io on n t to o e el ll li ip pt ti ic c P PD DE Es s: : L La ap pl la ac ce e e eq qu ua at ti io on n
Consider the solution to the two-dimensional Laplace equation

2
u/x
2
+
2
u/y
2
= 0,
in the solution region R = {x
1
<x<x
n
,y
1
<y<y
m
}, subject to the boundary conditions uy1 = u(x,y
1
) =
f
1
(x), uym = u(x,y
m
) = f
2
(x), ux1 = u(x
1
,y) = g
1
(y), uxn = u(x
n
,y) = g
2
(y), as illustrated below.
The names uy1, uym, ux1, uxn, will be used to store the boundary condition values as vectors
when using SCILAB.
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
To proceed with the solution we divide the solution region R into a grid defined by
x
1
= 0, x
n
= L, y
1
= 0, y
m
= H, x = (x
n
-x
1
)/(n-1), y = (y
m
-y
1
)/(m-1),
so that the grid in the x direction consists of the values [x
1
, x
2
, , x
n
] and the grid in the y
direction consists of the values [y
1
, y
2
, , y
m
]. The grid values are calculated as
x
i
= x
1
+ (i-1) x, y
j
= y
1
+ (j-1)y.
The figure below shows a sketch of the solution grid.
The original PDE gets replaced by an algebraic equation by using centered finite difference
approximations for the second order derivatives, i.e.,

2
u/x
2
(u
i+1,j
- 2u
ij
+ u
i-1,j
)/(x)
2
,
2
u/y
2
(u
i,j+1
- 2u
ij
+ u
i,j-1
)/(y)
2
.
The resulting algebraic equation is
(u
i+1,j
- 2u
ij
+ u
i-1,j
)/(x)
2
+ (u
i,j+1
- 2u
ij
+ u
i,j-1
)/(y)
2
= 0,
from which we can get an explicit expression for u
ij
:
.
] ) ( ) [( 2
) ( ) ( ) ( ) (
2 2
, 1 , 1
2
1 , 1 ,
2
y x
u u y u u x
u
j i j i j i j i
ij
+
+ + +
=
+ +
To proceed with the solution we will first load the boundary conditions in vectors uy1, uym,
ux1, uxn. Vectors uy1, uym are vectors of length n, while vectors ux1, uxn are vectors of
length m. Then, we calculate improved values for u
ij
, i=1,2,,n, and j=1,2,,m, using the
equation developed above. We will repeat the calculations for u
ij
and keep recalculating until
the values of u
ij
converge to their final values. The convergence criteria to be used will be to
ensure that |(u
ij
)
k+1
-(u
ij
)
k
| < , where is a tolerance value, and k represents the current
approximation step.
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
A A f fu un nc ct ti io on n t to o s so ol lv ve e L La ap pl la ac ce e e eq qu ua at ti io on n i in n a a r re ec ct ta an ng gu ul la ar r d do om ma ai in n
The following function, LaplacePDF1, calculates the solution to Laplace equation in a
rectangular domain. The general call to the function is
[u,k] = LaplacePDE1(xrange,yrange,ux1,uxn,uy1,uym,epsilon,nmax)
where xrange is a vector of n elements, i.e., xrange = [x1:Dx:xn], and yrange is a vector of m
elements, i.e., yrange = [y1:Dy:ym]. Vectors xrange and yrange define the solution grid. The
values ux1, uxn, uy1, uym, are vectors representing the boundary conditions at x = x
1
, x = x
n
, y
= y
1
, and y = y
m
, respectively. The value epsilon represents the tolerance for convergence,
and nmax represents the maximum number of iterations allowed. The function returns a nxm
matrix u, whose elements represent the solution, i.e., u
ij
=u(x
i
,y
j
), and the value k which
indicates the number of the iteration at which a solution was achieved, if any.
A listing of the function follows:
function [u,kk] = LaplacePDE1(xrange,yrange,ux1,uxn,uy1,uym,epsilon,nmax)
n=length(xrange);m=length(yrange);
Dx=(xrange(n)-xrange(1))/(n-1);
Dy=(yrange(m)-yrange(1))/(m-1);
u=ones(n,m);uk=ones(n,m);
u(:,1)=uy1';u(:,m)=uym';
u(1,:)=ux1;u(n,:)=uxn;
printf('Iteration started.');
for k =1:nmax
ke = 0;kk=k;
if modulo(kk,10) == 0 then
printf('Current iteration is number %g.',k);
end;
for i = 2:n-1
for j = 2:m-1
uk(i,j)=...
(Dx^2*(u(i,j-1)+u(i,j+1))+Dy^2*(u(i-1,j)+u(i+1,j)))/(2*(Dx^2+Dy^2));
if abs(uk(i,j)-u(i,j))>epsilon then
ke = ke+1
end;
u(i,j)=uk(i,j);
end;
end;
if ke == 0 then
break
end;
end;
if kk==nmax then
printf('Maximum number of iterations %g reached',kk);
end;
Example of application of function LaplacePDE1
Consider the case in which the solution region is defined by R = {0<x<1,0<y<1}, with boundary
conditions given by u(0,y) = 0, u(1,y) = 1, u(x,0) = 1-x
2
, u(x,1) = 0. The following application of
function LaplacePDE1 will calculate the solution to this Laplace equation problem. The first
solution using x = y = 0.1, = 0.001, converges after 42 iterations.
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
-->getf('LaplacePDE1')
-->x=[0:0.1:1];y=[0:0.1:1];
-->ux1=zeros(y);uxn=ones(y);uy1=1-x^2;uym=zeros(x);
-->epsilon=0.001;nmax=1000;
-->[u,k]=LaplacePDE1(x,y,ux1,uxn,uy1,uym,epsilon,nmax);
(Output suppressed)
-->k
k = 42
-->plot3d(x,y,u,45,45,'x@y@u(x,y)')
A solution with a more refined grid, x = y = 0.05, is produced next. Convergence is achieved
after 128 iterations.
-->x=[0:0.05:1];y=[0:0.05:1];
-->ux1=zeros(y);uxn=ones(y);uy1=1-x^2;uym=zeros(x);
-->epsilon=0.001;nmax=1000;
-->[u,k]=LaplacePDE1(x,y,ux1,uxn,uy1,uym,epsilon,nmax);
(output suppressed)
-->k
k = 128.
-->plot3d(x,y,u,45,45,'x@y@u(x,y)')
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
The solution is also shown as contour curves:
-->contour(x,y,u,10)
-->xtitle('Solution to Laplace equation','x','y')
Exercises
In the following exercises we use the following PDEs from physical science:
One-dimensional wave equation: u
tt
= c
2
u
xx
One-dimensional heat equation: u
t
= c
2
u
xx
Two-dimensional Laplace equation: u
xx
+u
yy
= 0
Two-dimensional Poisson equation: u
xx
+u
yy
= f(x,y)
With the notation u
t
= u/t, u
xx
=
2
u/
2
x, etc.
[1]. Determine if the following functions u(x,t) satisfy the one-dimensional wave equation:
(a) u(x,t) = cxt (b) u(x,t) = ln(x
2
-c
2
t
2
)
(c) u(x,t) = x
2
+ c
2
t
2
(d) u(x,t) = x/(x
2
+c
2
t
2
)
[2]. Determine if the following functions u(x,t) satisfy the one-dimensional heat equation:
(a) u(x,t) = exp(k(x+kc
2
t)) (b) u(x,t) = t+x
2
/(2c
2
)
(c) u(x,t) = 2c
2
/(2c
2
t+x
2
) (d) u(x,t) = x
2
+ 2c
2
t
2
[3]. Determine if the following functions u(x,y) satisfy the two-dimensional Laplace equation:
(a) u(x,t) = k(x
2
+y
2
) (b) u(x,t) = tan
-1
(y/x)
(c) u(x,t) = ln(x
2
+y
2
) (d) u(x,t) = x
3
-3xy
2
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
[3]. Determine if the functions u(x,y) satisfy the two-dimensional Poisson equation for the
given function f(x,y):
(a) u(x,y) = x cos y, f(x,y) = -x cos y
(b) u(x,y) = x
2
-2xy+y
2
, f(x,y) = 4
(c) u(x,y) = x ln y, f(x,y) = -x/y
2
(d) u(x,y) = (x+y)
3
, f(x,y) = 12(x+y)
[4]. Classify the following second-order partial differential equations as parabolic, hyperbolic,
or elliptic:
(a) u
t
- u
tx
- u
xx
= 0 (b) u
xx
- 2u
xy
+ u
yy
= xy
(c) u
xx
+ u
xt
- u
tt
+ u
x
+ u
t
= 0 (d) u
xx
+ u
yy
= 0
Solve problems [5] through [12] using separation of variables and Fourier series expansions.
[5]. Solution to the heat equation with derivative boundary conditions. Using separation of
variables, solve the one-dimensional heat equation for the temperature u(x,t) on a bar of
length L = 1, using as boundary conditions u/x = 0 for x = 0 and x = L. Assume that the initial
temperature distribution is given by u(x,0) = f(x) = x(1-x), and use k=1.
[6]. Solution to the heat equation with heat convection loss . Consider the one-dimensional
heat equation including convection losses: u/t = k(
2
u/x
2
) - u, and subject to the boundary
conditions: u(0,t) = 0, u(L,t) = 0, and the initial condition: u(x,0) = f(x).
(a) Using the transformation u(x,t) = exp(-t)w(x,t), verify that the original PDE is
transformed into w/t = k(
2
w/x
2
), and that the corresponding boundary and initial
conditions transform into w(0,t) = 0, w(L,t) = 0, and w(x,0) = f(x).
(b). For L = 1, k = 1, = 0.01, and f(x) = x(1-x) determine the solution u(x,t) to the heat
equation with convection loss.
[7]. Solution to the diffusion-convection equation with constant flow velocity. Many
equations related to diffusion in moving fluids include a term involving the flow velocity v in
the x-direction: u/t + v(u/x)= k(
2
u/x
2
). If v is a constant, this equation can be reduced
to the form: w/t = k(
2
w/x
2
),by using the transformation
(a) Verify that the transformation indicated above produces the modified PDE: w/t =
k(
2
w/x
2
).
(b) Solve the diffusion-convection equation with constant flow velocity in the domain
0<x<L for L = 1, k = 1, v = 1, and k = 1, subject to the boundary conditions u(0,t) = 0,
and u(L,t) = 0, and initial conditions u(x,0) = x(1-x).
[8]. Solve the one-dimensional wave equation for c = 1 in the domain 0 < x < L, with L = 1,
given the boundary conditions u(0,t) = 0, and u/x = 0 at x = L, and the initial condition u(x,0)
= f(x) = cos(x).
[9]. Prove that u(x,t) = F(x+ct) + F(x-ct) is a solution to the one-dimensional wave equation.
Plot the result if F(x) = exp(-x
2
/2) with c = 1 in the interval -10 < x < 10, and -10 < t < 10.
Produce a three-dimensional surface plot as well as an animation of the results.
). , ( )]
2
1
(
2
exp[ ) , ( t x w t v x
k
v
t x u

=
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
[10]. Referring to the following figure, solve case [3] of Laplaces equation in the rectangular
domain L = 2, H = 1, if p(y) = (y/H)(1-y/H). Produce a surface plot as well as a contour plot of
the results.
[11]. Referring to the figure of problem [12], solve case [4] of Laplaces equation in the
rectangular domain L = 2, H = 1, if q(y) = (y/H)
2
(1-y/H). Produce a surface plot as well as a
contour plot of the results.
[12]. Using the results of problems [12] and [14] determine the solution to Laplaces equation
in the rectangular domain L = 2, H = 1, if the boundary conditions are u(x,0) = u(x,H) = 0, u(0,y)
= p(y) = (y/H)(1-y/H), and u(L,y) = q(y) = (y/H)
2
(1-y/H). Produce a surface plot as well as a
contour plot of the results.
[13]. Solve problem [5] numerically using finite differences.
[14]. Solve problem [6] numerically using finite differences.
[15]. Solve problem [7] numerically using finite differences.
[16]. Solve problem [8] numerically using finite differences.
[17]. Solve problem [9] numerically using finite differences.
[18]. Solve problem [10] numerically using finite differences.
[19]. Solve problem [11] numerically using finite differences.
[20]. Solve problem [12] numerically using finite differences.
[21]. The solution to the first degree PDE
u/t + c (u/x) = 0
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
can be accomplished by using the following finite-difference explicit scheme (known as the Lax
method)
) (
2
) (
2
1
, 1 , 1 , 1 , 1 1 , k i k i k i k i k i
u u
x
t c
u u u
+ + +



+ = ,
where u
i,k
= u(x
i
,t
k
), x
i
=x
1
+(i-1)x, for i=1,2,,n, and t
k
=t
1
+(k-1)t, for k=1,2,,m. For the
method to be stable -- i.e., to dampen out any error in the solution as the solution progresses -
- the Lax method must satisfy the so-called Courant-Friedrichs-Lewy stability condition, i.e.,
. 1
| |

x
t c
Write a SCILAB function to solve the PDE: u/t + c (u/x) = 0, with c = 1 subject to the initial
condition u(x,0) = f(x) = (x/L)(1-x/L), in the interval 0 < x < L, with L = 1, for 0 < t < 10, using
(a) x = 0.10, t = 0.05
(b) x = 0.10, t = 0.1
(c) x = 0.10, t = 0.2
[22]. A finite-difference approximation to the first-order PDE: u/t + c (u/x) = 0, is given
by
). (
2
, 1 , 1
1 ,
k i k i
ik k i
u u
x
c
t
u u
+
+

Re-write the finite-difference approximation as an equation of the form


u
i,k+1
-u
ik
+ (/2)(u
i+1,k
-u
i-1,k
) = 0,
with = ct/x, and obtain an implicit solution to the PDE for the conditions of problem [21].
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Optimization Techniques with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
OPTIMIZATION USING SCILAB 2
Definitions 2
Graphical solution 2
Linear programming 4
SCILAB function for solving linear programming problems 5
Applications of function linpro - case 1: inequality constraints and bounds present 6
Applications of function linpro - case 2: inequality constraints and bounds present 7
Graphical illustration of linear programming solution 8
Lagrange multipliers 9
What are Lagrange multipliers? 10
Quadratic programming 10
SCILAB function for solving quadratic programming problems 11
An application of function quapro 11
SCILAB non-linear programming functions 13
Function optim 13
Application of function optim 13
Specifying the method of solution 14
Obtaining the function gradient at the optimum point 14
Obtaining a work vector for quasi-Newton method 15
Placing simple constraints on the design variables 15
Function leastsq 16
Application of function leastsq 16
Function leastsq using the functions gradient 17
Function leastsq with different output options 17
Function leastsq returning function gradient at optimum point 18
Specifying the method of solution for function leastsq 18
Function leastsq with upper and lower boundary constraints for the design variables 18
Exercises 19
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Optimization using SCILAB
The objective of optimization techniques is to determine the values of the independent
variables that minimize or maximize a function (known, in general, as the objective function)
subject to a number of constraints. Techniques presented in this chapter include graphic
approach for functions of two variables, linear programming, quadratic programming, and
general non-linear programming.
Definitions
The general minimization problem can be stated as
} ),..., ( ), ( { min
2 1 m
D x
f x f x f

,
where f
i
(x), i=1,2,,m, are scalar objective functions that map a vector x, known as the design
variable vector, into an objective space. The values of x are restricted to a feasible domain
region D described by N inequality constraints and M equality constraints. The domain D is
described as
D = {x : g
j
(x)0, h
k
(x)=0, j=1,2,,N, k=1,2,,M}.
A linear programming problem results when the objective function and the constraints are
linear functions of the design variables. Non-linear programming problems result from having
at least one of the objective or constraint functions be non-linear.
Graphical solution
A graphical solution is presented here for an objective function of two variables, w = f(x,y) =
sin(x)cos(y). The purpose of this solution is to illustrate the idea of optimization with a simple
function whose graph provides a good idea of the solution. Methods for pinpointing the
solution will be presented later.
The idea behind the graphical solution is to produce a contour plot of the function in the
feasible domain as well as a combined three-dimensional and contour plot. These two visual
aids would be, in many cases, enough to determine the values of the design variables that
produce a minimum or a maximum of the function.
The problem to be solved is specified as
)}, , ( { min
,
y x f w
D y x
=

where D is defined by
D = {(x,y) : 0<x<6, 0<y<6}.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Using SCILAB we will define the function f(x,y) and produce a contour plot of the function in
the feasible domain:
-->deff('[w]=f(x,y)','w=sin(x)*cos(y)')
-->x=[0:0.25:6];y=[0:0.25:6];z=feval(x,y,f);
-->contour(x,y,z,10)
-->xtitle('Contour plot of w=sin(x)*cos(y)','x','y')
The contour plot is good enough to help us determine the solution. Within the solution domain
we can identify three maximum values (points near (1.5,0.0), (1.5,6.0), and (4.7,3.2)) and
three minimum values (points near(1.5,3.2), (4.7,0), and (4.7,6)).
A combined three-dimensional plot with contours can be generated using functions contour and
plot3d as shown next. First, we define a parallelepiped (rect) large enough to contain the
three-dimensional plot and to be able to show the projected contour plots in a plane parallel
to the x-y plane. The values in rect are [x
min
,x
max
,y
min
,y
max
,z
min
,z
max
]. The parameters in the
call to contour are as follows: x,y

are vectors containing values of x and y coordinates; z is the
matrix of values of z = f(x,y) evaluated earlier; the next three values are nc = 10, the number
of contours to be shown, = 35 and = 45 are the values, in degrees, of the angles of the
viewpoint used for the three-dimensional graph; the string x@y@z indicates the axes labels to
be used; the vector [1,1,1] is a vector specifying the graph style (see --> help plot3d); rect
is the vector defined above; and, zl = -8 is the value of the plane z = zl where the contour plot
is to be projected. We chose a value of -8 to allow enough spacing between the surface, which
ranges in -1 < z < 1, and the contour plane. The parameters in the call to function plot3d are
very similar to those in the call to function contour, except that a number of contours is not
required.
-->rect=[0,6,0,6,-8,1]
rect =
! 0. 6. 0. 6. - 8. 1. !
-->contour(x,y,z,10,35,45,'x@y@z ',[1,1,1],rect,-8)
-->plot3d(x,y,z,35,45,'x@y@z',[2,1,4],rect)
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
-->xtitle('Function w = sin(x)*cos(y)')
The combined three-dimensional and contour plot serves to verify the observations already
made from the contour plot alone about the location of the minimum and maximum values of
the function in the domain of interest.
A graphical solution is limited to functions of one or two variables and provides only an
approximate location for the solution. The methods presented next for linear, quadratic, and
non-linear functions will produce more accurate solutions.
Linear programming
Linear programming is an optimization method applied to the solution of problems in which
both the objective function and the constraint functions are linear functions of the design
variables. The problem may be expressed as
minimize f(x) = c
T
x
subject to
Ax = b,
Gx h,
and
x
L
x x
U
where x is a vector containing the n design variables, i.e., x = [x
1
, x
2
, , x
n
]
T
, c is the constant
column vector containing the n coefficients of the objective function, A is an m n matrix, b is
a column vector with m components, G is an p q matrix, h is a column vector with p
components, and x
L
and x
U
are vectors indicating constraints on the values of the design
variables.
As an example, consider the problem of minimizing the objective function
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
f(x) = 3x
1
+ 5x
2
-2x
3
,
subject to
x
1
+3x
2
= 5
x
1
+x
2
- x
3
= 2
2x
1
- x
2
3
x
1
+ x
2
+ x
3

25
0 x
1
5,
0 x
2
10,
0 x
3
3
The problem can be re-written as minimize f(x) = c
T
x, subject to Ax = b, Gx h, and x
L
x
x
U
, with
, ,
2
5
,
1 1 1
0 3 1
, ,
2
5
,
1 1 1
0 3 1
, ,
2
5
3
3
2
1

= h G b A x c
x
x
x
.
3
10
5
,
0
0
0

=
U L
x x
SCILAB function for solving linear programming problems
SCILAB provides function linpro for the solution of linear programs. The function can be called
using any of the following forms:
[xopt,lagr,fopt]=linpro(c,C,d [,x0])
[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,[,x0])
[xopt,lagr,fopt]=linpro(c,C,d,xL,xU, m, [,x0])
[xopt,lagr,fopt]=linpro(c,C,d,xL,xU, m,x0 [,imp])
The function returns values xopt, lagr, and fopt, where xopt is the value of the design
variables vector that minimizes the objective function, f(x) = c*x, lagr is a vector of Lagrange
multipliers (see below), and fopt is the optimal value of the function, i.e., fopt = f(xopt). The
arguments of the function call are:
the vector of n coefficients of the objective function, c,
a matrix C of (m+p) rows and n columns containing the rows of matrices A and G in that
order,
a column vector d with (m+p) rows containing the elements of vectors b and h in that
order,
column vectors xL and xU representing the vectors of lower and upper bounds of the
design variables, i.e., x
L
and x
U
, respectively,
m is the number of equality constraints (i.e., the number of rows in matrix A),
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
x0, an optional argument for the first three forms of the function call, is the vector
containing the n initial guesses for the solution,
imp, also an optional argument is an integer value that determines the amount of
information provided by function linpro (try values imp = 7,8, )
The first form of the call to linpro, namely, [xopt,lagr,fopt]=linpro(c,C,d,xL,xU,[,x0]), is used
when there are only inequality constraints. The second form, [xopt,lagr,fopt] = linpro(c, C, d,
xL, xU,[,x0]), is used when inequality constraints as well as lower and upper boundaries for the
design variables. The second form of the function call, [xopt,lagr,fopt]=linpro(c,C,d,xL,xU,
m, [,x0]), is used when there are inequality and equality constraints (m equality constraints),
as well as lower and upper boundaries of the design variables. Finally, the last form of the
function call, [xopt,lagr,fopt]=linpro(c,C,d,xL,xU, m,x0 [,imp]), requires an initial guess x0
besides equality and inequality constraints and upper and lower bounds of the design variables.
To illustrate the application of function linpro we will use the problem stated earlier, namely,
minimize f(x) = c
T
x, subject to Ax = b, Gx h, and x
L
x x
U
, with
, ,
2
5
,
1 1 1
0 3 1
, ,
2
5
,
1 1 1
0 3 1
, ,
2
5
3
3
2
1

= h G b A x c
x
x
x
.
3
10
5
,
0
0
0

=
U L
x x
We will illustrate the use of function linpro by changing the number of constraints of the
problem. First, we load the constant vectors and matrices shown using SCILAB:
-->c=[3;5;-2];A=[1,3,0;1,1,-1];b=[5;2];G=[1,3,0;1,1,-1];h=[5;2];
-->xL = zeros(3,1); xU=[5;10;3];
Applications of function linpro - case 1: inequality constraints and
bounds present
The following SCILAB commands set up and solve the linear programming problem using only
inequality constraints and bounds for the design variables. For this case we take C = G, d = h,
and obtain the solution to the linear programming problem:
-->C=G,d=h
C =
! 1. 3. 0. !
! 1. 1. - 1. !
d =
! 5. !
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
! 2. !
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU)
fopt = - 6.
lagr =
! - 3. !
! - 5. !
! 2. !
! 0. !
! 0. !
xopt =
! 0. !
! 0. !
! 3. !
The optimal solution is x
1
= 0, x
2
= 0, and x
3
= 3, corresponding to a value of -6 for the function.
Applications of function linpro - case 2: inequality constraints and
bounds present
The following SCILAB commands set up and solve the linear programming problem using both
equality and inequality constraints and bounds for the design variables. For this case we take C
= [A;G], d = [b;h], as follows:
-->C=[A;G], d = [b;h]
C =
! 1. 3. 0. !
! 1. 1. - 1. !
! 1. 3. 0. !
! 1. 1. - 1. !
d =
! 5. !
! 2. !
! 5. !
! 2. !
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,2)
fopt =
9.
lagr =
! 0. !
! 0. !
! 0. !
! - 1. !
! - 2. !
! 0. !
! 0. !
xopt =
! .5 !
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
! 1.5 !
! 0. !
Alternative calls to function linpro may include an initial guess for the solution as the last
argument in the function call. In the first case, a specific value of x is provided. In the
second case, the option v indicates that a vertex of the feasible region is to be used as an
initial guess. In the third case, the option g indicates that an arbitrary initial value --
generated by the function -- is to be used. You can check, by using SCILAB, that the solution
is the same in any of the following function calls:
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,2,[0;0;0]);
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,2,'v');
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,2,'g');
Graphical illustration of linear programming solution
A linear programming involving two design variables (x,y) can be used to illustrate the solution
of the problem through graphics. The problem to be solved is to minimize f(x,y) = x
1
/4 + x
2
/3,
subject to the constraints -5x
1
-x
2
-5, -2x
1
+5x
2
-10, x
1
0, x
2
0. The problem can be set up
and solved using SCILAB as follows:
-->c=[1/4;1/3];xL=[0;0];xU=[1e10;1e10]; C = [-5,-1;-2,-5]; d = [-5;-10];
-->[xopt,lagr,fopt]=linpro(c,C,d,xL,xU,0);
-->xopt
xopt =
! .6521739 !
! 1.7391304 !
To illustrate the problem graphically we first define the objective function, w=f(x,y), and plot
a set of contours for it. We also define functions representing the constraints, y1=f1(x) and
y2=f2(x), and plot the lines corresponding to these constraints. The solution is the vertex
where the two constraint lines intercept as labeled in the plot.
-->deff('[w]=f(x,y)','w=c(1)*x+c(2)*y')
-->xx=[0:1:8];yy=[0:1:6];zz=feval(xx,yy,f);
-->contour(xx,yy,zz,10)
-->deff('[y1]=f1(x)','y1=-5*x+5')
-->deff('[y2]=f2(x)','y2=(10-2*x)/5')
-->xxx=[0:0.01:8];yy1=f1(xxx);yy2=f2(xxx);
-->contour(xx,yy,zz,10)
-->plot2d([xxx' xxx'],[yy1' yy2'],[-1,-1],'011',' ',[0 0 8 6])
-->xstring(xopt(1)+0.05, xopt(2)+0.05,'solution')
-->xtitle('Linear regression problem','x','y')
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Notice that the point of intersection of the lines corresponds with the minimum value of the
function illustrated by the contours. The contours decrease towards the origin. The feasible
domain is the region above and to the right of the two thick lines, above the x-axis and to the
right of the y-axis.
Lagrange multipliers
The elements in the vector of Lagrange multipliers lagr provide information about the effect of
upper and lower boundaries as well as on any constraints. . If vectors of lower and upper
bounds, i.e., xL and xU, are provided, the vector of Lagrange multipliers, lagr, will have
(n+m+p) components. Components 1 to n are associated with the n upper and lower bounds of
the constraint variables, components n+1 through (n+m) are associated with the m equality
constraints, while components (n+m+1) through (n+m+q) are associated with the q inequality
constraints.
If any component of the Lagrange multiplier vector is zero, it means that that particular bound
or constraint is not active (i.e., the solution was found without using that particular condition).
If an lower bound constraint is active, then the corresponding Lagrange multiplier is negative.
On the other hand, if an upper bound constraint is active, the corresponding Lagrange
multiplier is positive.
For example, in the first application of function linpro used above, we found the following
results for the vector of Lagrange multipliers:
lagr =
! - 3. !
! - 5. !
! 2. !
! 0. !
! 0. !
The first three elements are related to the upper and lower bounds of the variables x
1
, x
2
, and
x
3
, respectively. The fact that lagr(1) = -3 and lagr(2) = -5 indicates that the lower bound
constraint was used for variables x
1
and x
2
. What this means, in this case, is that x
1
= 0 and x
2
= 0, i.e., variables x
1
and x
2
took their lower bounds in the optimal solution. The fact that
lagr(3) = 2 indicates that the upper bound constraint was used for x
3
, i.e., x
3
= 3.0. Finally,
the fact that lagr(4) = lagr(5) = 0 indicates that none of the two inequality constrains in the
problem was used. The optimal solution found was x
1
= 0, x
2
= 0, and x
3
= 3.
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
In the second example of linpro presented above the Lagrange multipliers vector was found to
be
lagr =
! 0. !
! 0. !
! 0. !
! - 1. !
! - 2. !
! 0. !
! 0. !
This is interpreted as indicating that none of the three upper or lower bound constraints for the
design variables were used (lagr(1) = lagr(2) = lagr(3) = 0), neither were any of the two
inequality constraints (lagr(6) = lagr(7) = 0). The equality constraints, represented by lagr(4)
= -1 and lagr(5) = -2, were both used in the solution.
What are Lagrange multipliers?
Lagrange multipliers are variables introduced in an optimization problem to incorporate the
problem constraints into an expanded objective function. For example, given the objective
function f(x
1
,x
2
,,x
n
) subject to the constraints represented by m(<n) equations

1
(x
1
,x
2
,,x
n
) = 0,
2
(x
1
,x
2
,,x
n
) = 0, ,
n
(x
1
,x
2
,,x
n
) = 0,
we can form the expanded objective function
.. ) ,..., , ( ) ,..., , ( ) ,..., , (
1
2 1 2 1 2 1
=
+ =
m
j
n j n n
x x x x x x f x x x
The variables
j
introduced in function are known as Lagrange multipliers. The solution to
the problem is obtained by solving simultaneously the m constraint equations and the n
equations resulting from the conditions
/x
1
= 0, /x
2
= 0,, /x
n
= 0.
The resulting problem has (n+m) unknowns, i.e., x
1
, x
2
, , x
n
, and
1
,
2
, ,
m
, and (n+m)
equations. Thus, the problem has a unique solution that provides the minimum (or maximum)
value of the objective function f(x
1
,x
2
,,x
n
).
Quadratic programming
Quadratic programming is an optimization method applied to the solution of problems in which
both the objective function is defined, in general, by an objective function consisting of a
quadratic form plus a linear combination of the design variables. The constraint functions of
the problem are still linear functions of the design variables. The problem may be expressed
as
minimize f(x) = x
T
Qx + c
T
x
subject to
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
Ax = b,
Gx h,
and
x
L
x x
U
where x is a vector containing the n design variables, i.e., x = [x
1
, x
2
, , x
n
]
T
, Q is a n n
symmetric square matrix, c is a constant column vector containing n coefficients, A is an m n
matrix, b is a column vector with m components, G is an p q matrix, h is a column vector with
p components, and x
L
and x
U
are vectors indicating constraints on the values of the design
variables.
For the case in which two design variables are present, i.e., x = [x
1
x
2
], the objective function
f(x) = x
T
Qx + c
T
x is a quadratic equation in x
1
and x
2
given by
[ ] [ ] . ) , (
2
1
2 1
2
1
2 , 2 1 , 2
2 , 1 1 , 1
2 1 2 1

=
x
x
c c
x
x
q q
q q
x x x x f
= ) , (
2 1
x x f
+ + + + +
1
2
x
1
2
q
, 1 1
1
2
x
1
x
2
q
, 2 1
1
2
x
2
x
1
q
, 1 2
1
2
x
2
2
q
, 2 2
c
1
x
1
c
2
x
2
Consider, for example, the case in which q
11
= 2, q
12
= q
21
= -1, q
22
= 3, c
1
= 2, and c
2
= 4. The
objective function is,
f(x
1
,x
2
) = x
1
2
- x
1
x
2
+ (3/2)x
2
2

+ 2x
1
+4x
2
.
Subject to the constraints:
-2x
1
-3x
2
-5,
-5x
1
-x
2
-1,
x
1
0, x
2
0.
SCILAB function for solving quadratic programming problems
SCILAB provides function quapro for the solution of a problem composed of a quadratic
objective function subjected to linear constraints. The function can be called using any of the
following forms:
[xopt,lagr,fopt] =quapro(Q,c,C,d [,x0])
[xopt,lagr,fopt]= quapro(Q,c,C,d,xL,xU,[,x0])
[xopt,lagr,fopt]= quapro(Q,c,C,d,xL,xU, m, [,x0])
[xopt,lagr,fopt]= quapro(Q,c,C,d,xL,xU, m,x0 [,imp])
where the parameters c, C, d, xL, xU, m, x0, and imp, and the variables xopt, lagr, and fopt
are exactly the same as in function linpro. The only difference is the first argument Q which
must be a square symmetric matrix.
An application of function quapro
We will solve the quadratic programming presented earlier which we re-write as minimize f(x)
= x
T
Qx + c
T
x, subject to Gx h, and x
L
x x
U
, with
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
.
10 1
10 1
,
0
0
,
1
5
,
1 5
3 2
,
4
2
,
3 1
1 2
10
10

=
U L
x x h G c Q
To use function quapro we use the values of Q, c, xL, and xU, as described above, and replace
C with G and d with h. The following SCILAB script produces the solution using quapro and
shows a graphic illustrating the solution:
//Quadratic programming solution
Q = [2,-1; -1,3]; c=[2;4]; C=[-2,-3;-5,-1]; d=[-5;-1];
xL = [0;0]; xU = [1e10;1e10];
[xopt,lagr,fopt] = quapro(Q,c,C,d,xL,xU);
xopt
//Plot illustrating quadratic programming solution
deff('[w]=f(x1,x2)','w=x1^2-x1*x2+(3/2)*x2^2+2*x1+4*x2')
xx1=[-5:0.5:5]; xx2=[-5:0.5:5]; zz = feval(xx1,xx2,f);
contour(xx1,xx2,zz,10)
deff('[y1]=f1(x)','y1=(5-2*x)/3')
deff('[y2]=f2(x)','y2=1-5*x')
xxx=[-5:0.01:5];yy1=f1(xxx);yy2=f2(xxx);
plot2d([xxx' xxx'],[yy1' yy2'],[1 1],'011',' ',[-5 -5 5 5 ])
xpoly([-5,5],[0,0],'lines'); xpoly([0,0],[-5,5],'lines');
xfrect(xopt(1)-0.05,xopt(2)-0.05,0.1,0.05)
xstring(xopt(1)+0.1, xopt(2)+0.1, 'solution')
xtitle('Quadratic programming solution','x1','x2')
To run the script (called Quad1), assumed stored in the current working directory, use:
-->exec(Quad1)
The results are:
-->xopt
xopt =
! 1.2142857 !
! .8571429 !
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
The contour plot suggest that the absolute minimum of the function occurs somewhere in the
third quadrant (x
1
<0 and x
2
<0). The feasible domain is located in the first quadrant (x
1
>0,
x
2
>0) and to the right and above the two lines shown. The optimal solution is the point labeled
solution in the graph.
SCILAB non-linear programming functions
SCILAB provides functions optim and leastsq to obtain the value of x that minimizes a non-
linear function f(z) allowing only constrains on the lower and upper limits of the design
variables, if any.
Function optim
The simplest call to function optim is
[fopt,xopt]=optim(costf,x0)
where xopt is the value of the design variables vector x that minimizes function costf. The
value of the function at x=xopt is given by fopt. An initial guess to the solution, x0, is provided
as argument to the function.
The function costf must be defined so that the general call to this function is
[f,g,ind]=costf(x,ind)
The function costf is a function which returns f = f(x), i.e., the value of cost (or objective)
function at x, and g = gradient vector of cost function at x. The variable ind is used by
function optim. If ind=1, function costf produces no return. If ind=2, function costf must
provide f as a result. If ind=3, function costf must return the gradient g, and if ind=4, function
costf must return both f and g.
Application of function optim
We will try to minimize the function
f(x
1
,x
2
,x
3
) = {(x
1
-a
1
)
2
+ (x
2
-a
2
)
2

+ (x
3
-a
3
)
2
}
1/2
,
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
whose gradient is given by
g(x
1
,x
2
,x
3
) = [(x
1
-a
1
),(x
2
-a
2
),(x
3
-a
3
)]/{(x
1
-a
1
)
2
+ (x
2
-a
2
)
2

+ (x
3
-a
3
)
2
}
1/2
.
We use the expressions in functions f and g to define function costf, as follows:
-->deff('[f,g,ind]=costf(x,ind)', ['f=norm(x-a)','g=(x-a)/f'])
The simplest call possible for function optim is:
-->[fopt,xopt] = optim(costf,x0)
xopt =
! 2. !
! 3. !
! - 2. !
fopt =
1.271E-12
Specifying the method of solution
Function optim can use a third argument specifying the algorithm used for the solution.
Possible values for this argument are qn for quasi-Newton method, gc for conjugate
gradient, and nd for non-differentiable method. The following call to optim uses the quasi-
Newton method:
-->[fopt,xopt] = optim(costf,x0,'qn')
xopt =
! 2. !
! 3. !
! - 2. !
fopt =
1.271E-12
Obtaining the function gradient at the optimum point
The call to function optim can be modified to include a third value gopt representing the
gradient of the function at the optimal value xopt:
-->[fopt,xopt,gopt]=optim(costf,x0)
gopt =
! - .2181594 !
! - .8261763 !
! .5194605 !
xopt =
! 2. !
! 3. !
! - 2. !
fopt =
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
1.271E-12
Obtaining a work vector for quasi-Newton method
You can also get back from function optim a work array (work) that can be used for re-starting
calculations in a quasi-Newton method:
-->[fopt,xopt,gopt,work]=optim(costf,x0)
work =
column 1 to 5
! 2.838E+11 .1519917 .3348070 6.786E+11 - .0552042 !
column 6 to 12
! 3.772E+11 0. 0. 0. .2181594 .7930178 - .5487239 !
column 13 to 19
! 2. 3. - 2. - .2181594 - .8261763 .5194605 .0407068 !
column 20 to 24
! .5886515 1.0947067 - .2181594 - .8261763 .5194605 !
gopt =
! - .2181594 !
! - .8261763 !
! .5194605 !
xopt =
! 2. !
! 3. !
! - 2. !
fopt =
1.271E-12
Placing simple constraints on the design variables
Function optim allow for constraints to be placed on the design variables by adding as third
argument the string b followed by two arrays representing the lower bound and upper bound
values of the variables. For example, if we restrict the variables to x
1
0, x
2
0,, and x
3
0,
we can use:
-->[fopt,xopt,gopt]=optim(costf,'b',[0;0;0],[1e10;1e10;1e10],x0)
gopt =
! .1960336 !
! .9804776 !
! .0153146 !
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
xopt =
! 27.60096 !
! 131.04521 !
! 0. !
fopt =
2.5495098
Function leastsq
The simplest call to function leastsq is
[fopt,xopt]=leastsq([imp,] f [,Df],x0)
where imp is an optional argument may take the values imp=0 to allow only error reporting,
imp=1 for initial and final reports, imp=2 adds a report per iteration, and imp>2 add reports on
linear search. Function f is the function to be minimized, Df (optional argument) is the
gradient of the function to be minimized. The argument x0, and the returned values fopt and
xopt are the same as in function optim.
Application of function leastsq
As an example, we minimize the function
f(x
1
,x
2
) = x
1
3
- 2x
1
x
2
x
3
+ x
2
2
+ 3x
3
,
whose gradient is
Df = [3x
1
2
- 2x
2
x
3
, -2x
1
x
3
+ 2x
2
, -2x
1
x
2
+ 3].
First, we define the functions f and Df and an initial value of x:
-->deff('[w]=f(x)','w=x(1)^3-2*x(1)*x(2)*x(3)+x(2)^2+3*x(3)')
-->deff('[w]=Df(x)','w=[3*x(1)^2-2*x(2)*x(3),-2*x(1)*x(3)+2*x(2),-
2*x(1)*x(2)+3]')
-->x0=[1;1;1];
The simplest call to function leastsq is:
-->[fopt,xopt]=leastsq(f,x0)
xopt =
! - .0370534 !
! .8658123 !
! - .2446280 !
fopt =
0.
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
Function leastsq using the functions gradient
The following call to leastsq includes the gradient function:
-->[fopt,xopt]=leastsq(f,Df,x0)
xopt =
! - .0370533 !
! .8658122 !
! - .244628 !
fopt =
0.
Function leastsq with different output options
The following calls to the function introduce the argument imp with different values. The
descriptions following the function call will not show up, in general, in the SCILAB screen, but
they will be sent to a text file if the function diary has activated one such file for output.
-->[fopt,xopt]=leastsq(1,f,x0)
1entree dans n1qn1. dimension du probleme 3, de zm 24
mode 1 eps= .22E-15 niter= 100 nsim= 100 imp= 1
n1qn1 9 iters 11 simuls f= .0000000E+00
sortie de n1qn1. norme gradient carre = .0000000E+00
norm of projected gradient lower than 0.0000000E+00
xopt =
! - .0370534 !
! .8658123 !
! - .2446280 !
fopt =
0.
With imp=2 information is provided for each iteration in the solution:
-->[fopt,xopt]=leastsq(2,f,x0)
1entree dans n1qn1. dimension du probleme 3, de zm 24
mode 1 eps= .22E-15 niter= 100 nsim= 100 imp= 2
n1qn1 1 iters 1 simuls f= .9000000E+01
n1qn1 2 iters 3 simuls f= .2559197E+01
n1qn1 3 iters 5 simuls f= .1346007E-02
n1qn1 4 iters 6 simuls f= .4660886E-03
n1qn1 5 iters 7 simuls f= .5002594E-07
n1qn1 6 iters 8 simuls f= .1879045E-11
n1qn1 7 iters 9 simuls f= .7546736E-20
n1qn1 8 iters 10 simuls f= .1232595E-31
n1qn1 9 iters 11 simuls f= .0000000E+00
sortie de n1qn1. norme gradient carre = .0000000E+00
norm of projected gradient lower than 0.0000000E+00
xopt =
! - .0370534 !
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
! .8658123 !
! - .2446280 !
fopt =
0.
Function leastsq returning function gradient at optimum point
The following call to function leastsq provides not only the optimal solution and the value of
the function at that point, but also the gradient of the function at the optimal point:
-->[fopt,xopt,gopt] = leastsq(f,x0)
gopt =
! 0. !
! 0. !
! 0. !
xopt =
! - .0370534 !
! .8658123 !
! - .2446280 !
fopt =
0.
Specifying the method of solution for function leastsq
You can specify the method to be used, for example, for a quasi-Newton method use:
-->[fopt,xopt] = leastsq(f,x0,'qn')
xopt =
! - .0370534 !
! .8658123 !
! - .2446280 !
fopt =
0.
Function leastsq with upper and lower boundary constraints for the design variables
Restrictions on the upper and lower limits of the design variables can be specified as in the
following call to the function:
-->[fopt,xopt] = leastsq(1,f,Df,'b',[0;0;0],[10;10;10],x0)
*********** qnbd ****************
qnbd : pb dans ajour. mode= -1
qnbd : indqn= 8
stop during calculation of estimated hessian
xopt =
! 0. !
! 1. !
! 0. !
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
fopt =
1.
In the following call to function leastsq we specify the quasi-Newton method of solution:
-->[fopt,xopt] = leastsq(1,f,Df,'b',[0;0;0],[10;10;10],x0,'qn')
*********** qnbd ****************
qnbd : pb dans ajour. mode= -1
qnbd : indqn= 8
stop during calculation of estimated hessian
xopt =
! 0. !
! 1. !
! 0. !
fopt =
1.
Exercises
[1]. Minimize the objective function
C = 3.6x
1
+3.08x
2
+2.6x
3
+2.7x
4
subject to the constraints:
x
1
+ x
2
240
x
3
+ x
4
300
x
1
+ x
3
200
x
2
+ x
4
200
with the design variables being x
i
0, for i = 1,2,3,4.
[2]. Minimize the objective function
C = 3.5x
1
+ 3.0x
2
+ 6x
3
+ 4.8x
4
+ 1.8x
5
+ x
6
subject to the constraints
x
1
+ x
2
= 800
x
3
+ x
4
= 500
x
5
+ x
6
= 1500
5x
1
+ 6x
3
+ 3x
5
6000
5x
2
+ 6x
4
+3x
6
8000
with the design variables being x
i
0, for i = 1,2,,6.
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
[3]. Minimize the objective function
C = 0.7x
1
+ x
2
+ 0.6x
3
+ 0.6x
4
+ 0.8x
5
+ x
6
subject to the constraints
x
1
+ x
4
= 4000
x
2
+ x
5
= 3000
x
3
+ x
6
= 4500
x
1
+ x
3
+ x
3
6000
x
4
+ 6x
5
+x
6
8000
with the design variables being x
i
0, for i = 1,2,,6.
[4]. It is required to build an open-top parallelepipedal container for the transportation of sand
in a construction operation. The construction costs for building the container are:
Sides: $50/ft
2
Ends: $60/ft
2
Bottom: $90/ft
2
Salvage value = 10% of construction cost
Useful life = 15 years
Yearly maintenance = $15/ft
2
of outside surface area
Minimum volume needed = 150 ft
3
Interest rate = 8.5% per year
The problem is to determine the container dimensions (x
1
,x
2
,x
3
) for minimum cost. Formulate
the problem and find the container dimensions.
[5]. It is necessary to design a trapezoidal cross-section channel with a flow cross-sectional
area of 150 ft
2
. To minimize the construction costs the amount of excavated material should
be equal to the material used to build the dykes of area A
3
/2, each, on both ends of the cross-
section. Formulate the problem to minimize the excavated area A
1
if z = 1. Notice that the
design allows for a 1-ft freeboard elevation and that the crest of the dykes is 2-ft long. Solve
the problem for the values of y
1
, y
2
, and w that minimize A
1
.
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
[6]. Minimize the objective function
C = (x
1
-3)
2
+ (x
2
-3)
2
subject to x
1
+ x
2
4, x
1


0, and x
2
0.
[7]. Maximize the objective function
C = x
1
+ x
2
+ 2x
3
subject to 3x
2
- 2x
3
= 6, 1 x
1
4, x
2


0, and -1 x
3
2.
[8]. Identify a matrix associated with the quadratic form
Q = x
1
2
+ 4x
1
x
2
+ 2x
1
x
3
- 7x
2
2
- 6x
2
x
3
+ 5x
3
2
and determine the unconstrained minimum or maximum for this function.
[9]. Minimize the objective function
C = x
1
2
+ x
2
2
- 2x
1
- 2x
2
+ 2
subject to -2x
1
- x
2
+ 4 0.
[10]. Maximize the objective function
C = 2x
1
+ 5x
2
- 4.5x
3
+ 1.5x
4
subject to
5x
1
+ 3x
2
+ 1.5x
3
8
1.8x
1
- 6x
2
+4x
3
+ x
4
3
-3.6x
1
+ 8.2x
2
+ 7.5x
3
+ 5x
4
= 15
x
i
0, i=1,2,3,4.
[11]. Minimize the objective function
C = 8x
1
- 3x
2
+ 15x
3
subject to
5x
1
- 1.8x
2
-3.6x
3
2
3x
1
+ 6x
2
+ 8.2x
3
5
-3.6x
1
+ 8.2x
2
+ 7.5x
3
+ 5x
4
= 15
-3.6x
1
+ 8.2x
2
+ 7.5x
3
+ 5x
4
= 15
x
1
0, x
2
0,
no restriction on sign for x
3
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Basic Statistics and Probability with
SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
INTRODUCTION TO STATISTICS AND PROBABILITY 2
Statistics of a sample 2
Percentiles 3
Calculation of percentiles 3
Deciles 4
The coefficient of variation 4
SCILAB script for sample statistics 4
A function that produces a summary of sample statistics 6
Moments of a sample 9
Covariance and correlation 11
Frequency distribution of a sample 12
Selecting the number of classes 13
Histogram and frequency plot 14
Relative frequency 14
Cumulative frequency 14
A SCILAB function for determining frequency distributions 14
Skewness and kurtosis 17
Probability 20
Sample space and events 20
Sets 20
Set operations 20
Venn diagrams 23
Definitions of probability 24
Probability axioms 24
Addition rule 24
Counting 25
The Gamma function and factorials 27
Permutations and combinations using the Gamma function 28
Conditional probability 29
Independent events 30
Total probability 30
Bayes theorem 31
Exercises 31
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Introduction to Statistics and Probability
Statistics is the science of data analysis. Through the use of a number of mathematical
techniques, statistics can be used to summarize and analyze data from samples taken from a
population of data. The results of statistical analyses on samples, combined with concepts of
probability, can be used to make inferences on the population as a whole. Thus, a population
represents the totality of possible values of a particular variable or measurement, while a
sample is a representative sub-set of the population.
Some populations have a finite number of elements (a finite population) while others have an
infinite size (infinite populations). On the other hand, some finite populations have such a
large number of elements that can be considered as infinite in practice.
In order for the inferences made on samples to be representative of the population we must
ensure that we use random samples in the statistical analysis, i.e., a sample composed of
elements that have the same probability (or chance) of being selected. Otherwise, we said
the sample is biased. A biased sample, of course, will most likely produce misleading
information about the population.
A population is described by certain measures (e.g., the mean, , the standard deviation, )
known as the parameters of the population. On the other hand, a sample is described by
certain measures (e.g., the mean, x; the standard deviation, s
x
) known as the statistics of the
sample.
Statistics of a sample
The list of n data values {x
1
, x
2
, x
3
,,x
n-1
,x
n
} represents a sample of a population of a random
variable X. To describe the sample we use measures of central tendency and measures of
dispersion or spread, among other measures.
The measures of central tendency include:
The arithmetic mean (average or, simply, the mean):

=
=
n
i
i
x
n
x
1
1
The geometric mean:
n
n
i
i g
x x

=
=
1
The harmonic mean:

=
=
n i i h
x x
1 1
The median: this is the value that splits the ordered data set right in the middle, i.e., a
value x
m
such that 50// of the sample is located below that value. After the sample has
been ordered in increasing order, the median is calculated as follows:
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
x
m
= x
(n+1)/2
, if n is even
x
m
= (x
n/2
+x
(n+2)/2
), if n is odd
The mode: a mode is easier to identify after a frequency distribution of the data is
performed. The process of determining the frequency distribution of a sample is
presented later. The purpose of this process is to determine the number of data points
(frequency count) located in different sub-ranges (classes) of the random variable. The
mode, or modes, of the distribution represent the mid-point of the class (class mark) or
classes with the highest frequency count.
The measures of dispersion or spread include:
The sample range, range = x
max
-x
min
The mean absolute deviation:

=
n
i
i
x x
n
D A M
1
| |
1
1
. . .
The variance:

=
n
i
i x
x x
n
s
1
2 2
) (
1
1
The standard deviation is simply the square root of the variance, i.e.,

=
n
i
i x
x x
n
s
1
2
) (
1
1
The inter-quartile range:
IQR = Q
3
-Q
1
where Q
1
and Q
3
are known as the first and third quartiles, respectively. The second
quartile is the median, i.e., Q
2
= x
m
. These values are called quartiles because they
represent the points that divide the ordered sample into fourths (Latin: quarta). Thus,
25// of the ordered data is below Q
1
, 50// of the ordered data is below Q
2
= x
m
, and
75// of the ordered data is below Q
3
. The procedure to obtain the first and third
quartiles is presented below in combination with the concept of percentiles.
Percentiles
The word percentile is derived from the word percent, meaning that, if we could split the
ordered data set into 100 parts, the value P
r
, where 0 < r < 1, is such that 100r // of the
ordered sample is located below P
r.
Thus, P
0.25
= Q
1
, P
0.50
= Q
2
= x
m
, and P
0.75
= Q
3
.
Calculation of percentiles
The basic procedure to calculate the 100r-th Percentile (0 < r < 1) in a sample of size n is as
follows:
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
1. Order the n observations from smallest to largest.
2. Determine the product np
A. If np is not an integer, round it up to the next integer and find the
corresponding ordered value. (Integer rounding rule: for a non-integer x.yz, if
y 5, round up to x+1; if y < 5, round down to x.)
B. If np is an integer, say k, calculate the mean of the k-th and (k-1)-th ordered
observations.
Note: There is not a single way to define percentiles. The procedure outlined above may differ
from other definitions existing in the literature.
Deciles
Deciles are the values that split the data into 10 parts (Latin: deca). Thus, there are 9 deciles,
corresponding to percentiles P
0.1
, P
0.2
, P
0.3
, , P
0.9
.
The coefficient of variation
This is a measure that combines a measure of central tendency, the mean, and a measure of
spread, the standard deviation. The coefficient of variation is defined as:
% 100 . . =
x
s
V C
x
x
SCILAB script for sample statistics
The following SCILAB script can be used to obtain sample statistics for the x vector shown. To
run the script, store it in SCILABs working directory and use:
-->exec(statexample)
The script contains a number of pause statements to break down the output of the script into a
parts that can easily be read in one screen. At the pause prompt,
-1-->
type return to continue execution of the script.
// Script name: statexample
//
// This script illustrates the calculation of sample statistics
// for a sample of data values.
// The script requires functions mad and percentile
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
// The data is entered as row vector x
// Type return at the prompt 1--> generated by a pause
//
getf('mad') //Load function mad
getf('percentile') //Load function percentile
//
x = [2.3, 3.2, 1.5, 2.4, 3.2, 4.2, 2.6, 1.8, ...
2.2, 3.4, 4.2, 4.2, 2.5, 2.3, 5.1, 2.5, 1.2, 2.3, 1.1]
pause
//Note: functions mean(), median(), min(), max(), and st_deviation() are
// SCILAB functions
//
xbar = mean(x) //Mean value
xm = median(x) //Median
xmin = min(x) //Minimum value
xmax = max(x) //Maximum value
pause
rang = xmax-xmin //Range
sx = st_deviation(x) //Standard deviation
sx2 = sx^2 //Variance = sx^2
M_A_D=mad(x) //Mean absolute deviation (uses function mad)
pause
Q1 = percentile(x,0.25) //First quartile (uses function percentile)
Q3 = percentile(x,0.75) //Third quartile (uses function percentile)
IQR = Q3-Q1 //Inter-quartile range
CVx = sx/xbar*100 //Coefficient of variation
pause
//The following commands produce the 10-th, 20-th, ..., 90-th percentiles
//also known as deciles
deciles = zeros(1,8);
for k = 2:9
deciles(k-1) = percentile(x,0.1*k);
end
deciles
The functions mad() and percentile() are listed below. These functions should also be
stored in SCILABs working directory since they get loaded from within the script statexample.
function [mm] = mad(x)
// Calculates the mean absolute deviation
// of a sample data given by the row vector x
[m n] = size(x);
if m>n & n ==1
n = m;
end
xb = mean(x);
summad = 0.0;
for i = 1:n
summad = summad + abs(x(i)-xb);
end
mm = summad/(n-1)
function [p] = percentile(x,r)
//This function calculates the 100*r-th percentile
//(0<r<1) for the vector x
xx = sort(x);
[n m] = size(xx);
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
if m>n & n == 1
n = m;
end
if r<0 | r>1 then
disp('Percentile value must be between 0 and 1');
else
k = n*r;
if k-floor(k) ~= 0
p = xx(round(n*r));
else
p = (xx(k)+xx(k+1))/2;
end
end
A function that produces a summary of sample
statistics
The following function will produce a table of sample statistics. The only argument to the
function is the row vector x containing the sample data values.
function describe(x)
// This function calculates statistics for a sample of data
// values passed as a list vector x
//Note: functions mean(), median(), min(), max(), var(), and std()
// are SCILAB functions. Functions mad, and percentile are user-
// defined functions
getf('mad'); getf('percentile');
//
[m n] = size(x);
xbar = mean(x); //Mean value
xm = median(x); //Median
xmin = min(x); //Minimum value
xmax = max(x); //Maximum value
rang = xmax-xmin; //Range
sx = st_deviation(x); //Standard deviation
sx2 = sx^2; //Variance
M_A_D=mad(x); //Mean absolute deviation (uses
function mad)
Q1 = percentile(x,0.25); //First quartile (uses function
percentile)
Q3 = percentile(x,0.75); //Third quartile (uses function
percentile)
IQR = Q3-Q1; //Inter-quartile range
CVx = sx/xbar*100; //Coefficient of variation
//The following commands produce the 10-th, 20-th, ..., 90-th percentiles
//also known as deciles
deciles = zeros(1,9);
for k = 1:9
deciles(k) = percentile(x,0.1*k);
end;
//Display results
printf(' \n')
printf('Sample statistics\n')
printf('===============================================\n')
printf(' Sample size = %10.6g\n',n);
printf(' Mean value = %10.6g\n',xbar);
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
printf(' Median = %10.6g\n',xm);
printf(' Minimum value = %10.6g\n',xmin);
printf(' Maximum value = %10.6g\n',xmax);
printf(' Data range = %10.6g\n',rang);
printf(' Mean absolute deviation = %10.6g\n',M_A_D);
printf(' Variance = %10.6g\n',sx2);
printf(' Standard deviation = %10.6g\n',sx);
printf(' First Quartile = %10.6g\n',Q1);
printf(' Third Quartile = %10.6g\n',Q3);
printf(' Inter-quartile range = %10.6g\n',IQR);
printf(' Coefficient of variation (percent) = %10.6g\n',CVx);
printf('===============================================\n')
printf(' \n')
printf('Table of deciles\n')
printf('================\n')
printf('Number decile\n')
printf('================\n')
for i = 1:9
printf(' %2.0f %10.6g \n',i,deciles(i))
end
printf('================\n')
//End of function describe
The output from function describe for the vector x defined in the script shown earlier is the
following:
-->describe(x)
Sample statistics
===============================================
Sample size = 19
Mean value = 2.74737
Median = 2.5
Minimum value = 1.1
Maximum value = 5.1
Data range = 4
Mean absolute deviation = .91871
Variance = 1.19041
Standard deviation = 1.09106
First Quartile = 2.2
Third Quartile = 3.2
Inter-quartile range = 1
Coefficient of variation (percent) = 39.7129
===============================================
Table of deciles
================
Number decile
================
1 1.2
2 1.8
3 2.3
4 2.3
5 2.5
6 2.5
7 3.2
8 3.4
9 4.2
================
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Please notice that the function describe is designed to work only with a single vector of data,
but it can be easily modified to work with a data matrix. As a matter of fact, pre-defined
statistical functions such as mean, median, and st_deviation can be used to produce the
corresponding statistic for each column or row of a data matrix through the use of the
qualifiers c or r. For example, if we generate a random matrix M with 4 columns and 20
rows, we can apply SCILAB functions mean, median, and st_deviation to each column as
follows:
-->M = int(100*rand(20,4))
M =
! 58. 56. 2. 96. !
! 48. 57. 51. 50. !
! 22. 81. 39. 52. !
! 84. 5. 24. 55. !
! 12. 55. 50. 56. !
! 28. 12. 42. 46. !
! 86. 72. 28. 77. !
! 84. 26. 8. 79. !
! 52. 54. 62. 98. !
! 99. 98. 34. 81. !
! 64. 73. 70. 42. !
! 99. 0. 52. 24. !
! 5. 59. 28. 92. !
! 74. 30. 65. 10. !
! 41. 25. 8. 46. !
! 60. 62. 44. 39. !
! 85. 11. 72. 3. !
! 6. 61. 89. 51. !
! 82. 67. 24. 83. !
! 92. 33. 43. 61. !
-->mean(M,'r')
ans =
! 59.05 46.85 41.75 57.05 !
-->median(M,'r')
ans =
! 62. 55.5 42.5 53.5 !
-->st_deviation(M,'r')
ans =
! 31.189531 27.357814 23.051145 26.851394 !
If we apply the functions mean, median, and st_deviation without the r qualifier, the
functions provide the statistics of the entire matrix:
-->mean(M)
ans =
51.175
-->median(M)
ans =
52.
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
-->st_deviation(M)
ans =
27.691691
In the next example, a matrix M of 4 rows and 6 columns containing random elements is
generated and functions mean, median, and st_deviation applied to each row by using the c
qualifier:
-->M = int(100*rand(4,6))
M =
! 18. 85. 74. 26. 81. 69. !
! 1. 1. 94. 43. 25. 76. !
! 84. 18. 21. 91. 41. 35. !
! 7. 49. 57. 80. 35. 76. !
-->mean(M,'c')
ans =
! 58.833333 !
! 40. !
! 48.333333 !
! 50.666667 !
-->median(M,'c')
ans =
! 71.5 !
! 34. !
! 38. !
! 53. !
-->st_deviation(M,'c')
ans =
! 29.171333 !
! 38.698837 !
! 31.595358 !
! 27.193136 !
Moments of a sample
The mean value of a sample is also known as the first moment of the sample data about the
origin (i.e., about zero). In general, we define the r-th moment of the sample data about the
origin as
,... 2 , 1 ,
1
'
1
= =

=
r x
n
m
n
i
r
i r
Therefore, x = m
1
.
The r-th moment of the sample data about the mean is calculated as:
,... 2 , 1 , ) (
1
1
1
=

=

=
r x x
n
m
n
i
r
i r
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
You can check that m
1
= 0, and m
2
= s
x
2
.
Calculation of moments for a vector of data using SCILAB are illustrated in the following
example. First, we generate a column vector of 20 random elements:
-->x = 100*rand(20,1)
x =
! 54.776339 !
! 9.6228874 !
! 95.611717 !
! 22.074086 !
! 1.4325936 !
! 81.914898 !
! 13.049928 !
! 96.820036 !
! 65.613815 !
! 24.45539 !
! 52.831236 !
! 84.689256 !
! 78.766221 !
! 12.620826 !
! 78.83861 !
! 34.530425 !
! 26.598573 !
! 97.098187 !
! 88.752477 !
! 20.667529 !
The size of the sample is:
-->n=20
n =
20.
The following function defines the rth moment about the origin:
-->deff('[m_r_p]=mrp(r)','m_r_p = sum(x^r)/n')
The following statements produce a vector storing the first five moments about the origin:
-->moments_zero = [];
-->for j = 1:5, moments_zero = [moments_zero mrp(j)]; end;
-->moments_zero
moments_zero =
! 52.038251 3802.8027 311279.1 26702945. 2.348E+09 !
To calculate moments about the mean we start by calculating the mean as follows:
-->muX = mean(x)
muX =
52.038251
The next function defines the rth moment about the mean:
-->deff('[m_r]=mr(r)','m_r = sum((x-muX)^r)/(n-1)')
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
The following statements produce a vector containing the first five moments of the data about
the mean:
-->moments_mean = [];
-->for j = 1:5, moments_mean = [moments_mean mr(j)]; end;
-->moments_mean
moments_mean =
! 0. 1152.4454 - 586.80925 1786608.5 - 3091644.3 !
We can check that the second moment about the mean is the variance of the data by using:
-->st_deviation(x), ans^2
ans =
33.947686
ans =
1152.4454
Covariance and correlation
The concepts of covariance and correlation are used when analyzing the joint behavior of a
paired set of data, (x
i
,y
i
), i = 1, 2, , n. The function describe can be used to obtain separate
set of statistics for each of the data sets x and y, i.e., x, s
x
, y, s
y
, etc. The joint variation of
the two variables is quantified by the covariance, s
xy
, defined as
) ( ) (
1
1
1
y y x x
n
s
i
n
i
i xy

=

=
The correlation coefficient, defined as
,
y x
xy
s s
s
r

=
is a measure of how well the data (x
i
,y
i
) will fit a linear relationship y = mx+b. The value of r
is restricted to the interval 1 r 1. The closest the value of r is to 1, the better the
quality of the linear data fit.
Consider, for example, the vectors x and y shown below:
-->x = [2.1, 3.2, 4.5, 5.9, 6.2];
-->y = [4.5, 6.7, 8.1, 11.0, 14.0];
The standard deviations of x and y are calculated with:
-->sx = st_deviation(x)
sx =
1.7484279
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
-->sy= st_deviation(y)
sy =
3.7165845
SCILAB offers function corr to obtain the covariance s
xy
, and the mean values of x and y, as:
-->[sxy,meanxy] = corr(x,y,1)
meanxy =
! 4.38 8.86 !
sxy =
5.0012
The correlation coefficient is, therefore,
-->rxy = sxy/(sx*sy)
rxy =
.7696309
Frequency distribution of a sample
Given a sample of size n: {x
1
, x
2
, , x
n
}, listed in no particular order, it is often required to
group this data into a series of classes by counting the frequency or number of values
corresponding to each class.
Suppose that the classes will be selected by dividing the interval (x
bot
, x
top
), into k classes by
selecting a number of class boundaries, i.e., {xB
1
, xB
2
, , xB
k+1
}, so that class number 1 is
limited by xB
1
-xB
2
, class number 2 by xB
2
- xB
3
, and so on. The last class, class number k, will
be limited by xB
k
- xB
k +1
.
The value of x corresponding to the middle point of each class is known as the class mark, and
is defined as
xM
i
= (xB
i
+ xB
i+1
)/2, for i = 1, 2, , k.
If the classes are chosen such that the class size is the same, then we can define the class size
as the value
x = (x
max
- x
min
) / k,
and the class boundaries can be calculated as
xB
i
= x
bot
+ (i - 1)x.
Any data point, x
j
, j = 1, 2, , n, belongs to the i-th class if xB
i
x
j
< xB
i+1
.
The values f
i
, i = 1,2, , k, represent the frequency count (i.e., the number of sample values)
in the i-th class. By definition,
.
1
n f
k
i
i
=

=
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
The figure below illustrates the ideas of class boundaries, class marks, and class size. It also
illustrates how data accumulates in each class producing a graphical representation known as a
histogram. Each data item is illustrated by a block that get stacked on top of others if a
particular data item belongs in a given class. Outliers are data items that are smaller than the
smallest class boundary or larger than the largest class boundary. Outliers are illustrated by
the shadowed blocks in the figure.
Selecting the number of classes
While there is no general rule for selecting the number of classes to produce the frequency
distribution of a data set, you can use either of the following formulas to obtain estimates for
k. The values should be rounded to integer numbers:
k = n
k = 1 + 3.3 log
10
n
k = rangen
1/3
/(2IQR),
where range = x
max
x
min
, n = number of data points in the sample, and IQR = Q
3
-Q
1
is the
inter-quartile range.
To show a meaningful variation of the frequency count you need to use a minimum value of k =
5. To avoid the excessive cluttering of the frequency count it is recommended that you use a
maximum value of k = 25.
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
Histogram and frequency plot
A bar plot showing the classes as the bottom of the bars and the frequency count as the height
of the bars is called a histogram. A scatter plot (i.e., plot of (x,y) values) showing the
frequency count against the class marks is called a frequency plot.
Relative frequency
The relative frequency is defined as rf
i
= f
i
/n. The sum of relative frequencies for a frequency
distribution must add to 1.0.
Cumulative frequency
The cumulative frequency is indicated by the values cf
i
, which represent the frequency counts
for x < xB
i+1.
To calculate cumulative frequencies use the following algorithm:
cf
1
= f
1
k i f cf
i
r
r i
,..., 2 ,
1
= =

=
Or, using the recurrent formula cf
i
= cf
i-1
+ f
i
, for i = 2, 3, , k.
A plot of cumulative frequency against the upper limit of the classes, i.e., cf
i
vs. xB
i+1
, is called
a cumulative frequency plot.
You can also define a relative cumulative frequency as rcf
i
= cf
i
/n. A plot of cf
i
vs. xB
i+1
, will
be an increasing function that reaches a value of 1.0 at the largest class boundary.
A SCILAB function for determining frequency distributions
The data for frequency counts, relative frequency, and cumulative frequency is typically
presented as a table. The following function, freqdist, produces such table given the data in a
row vector x and the class boundaries in a row vector xclass. The function also plots a
histogram of relative frequencies, using SCILABs own histplot function, and a cumulative
frequency ogive:
function freqdist(x, xclass)
//This function produces a frequency distribution
//for the data in (row) vector x according to the
//class boundaries contained in the (row) vector
//xclass.
//
//Typical call: freqdist(x,xclass)
[m n] = size(x); //Sample size
[m nB] = size(xclass); //Number of class boundaries
k = nB - 1; //Number of classes
//Calculate class marks
cmark = zeros(1,k);
for ii = 1:k
cmark(ii) = 0.5*(xclass(ii)+xclass(ii+1));
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
end
//Initialize frequency counts to zero
fcount=zeros(2,k);
fbelow=0; fabove=0;
//Accumulate frequency counts
for ii = 1:n
if x(ii) < xclass(1)
fbelow = fbelow + 1;
elseif x(ii) > xclass(nB)
fabove = fabove + 1;
else
for jj = 1:k
if x(ii)>= xclass(jj) & x(ii)< xclass(jj+1)
fcount(jj) = fcount(jj) +1;
end
end
end
end
frel=fcount/n; //Relative frequencies
//Calculate cumulative frequencies
fcumul = zeros(1,k);
fcumul(1) = fcount(1);
for ii = 2:k
fcumul(ii) = fcumul(ii-1) + fcount(ii);
end;
fcumulrel = fcumul/n; //Relative cumulative frequencies
//Produce summary table
disp(' ');
disp('Frequency distribution');
disp('=========================================================================
========')
disp('Class LowBound UppBound Class Mark Freq. RelFreq. CumFreq.
RelCumFreq')
disp('=========================================================================
========')
for ii = 1:k
printf('%5.0f %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g \n', ...
ii,
xclass(ii),xclass(ii+1),cmark(ii),fcount(ii),frel(ii),fcumul(ii),fcumulrel(ii))
;
end
disp('=========================================================================
========')
disp(' ')
if fbelow ~= 0
printf('Outliers below minimum class boundary = %10.6g \n',fbelow)
end
if fabove ~= 0
printf('Outliers above maximum class boundary = %10.6g \n',fabove)
end
disp(' ')
printf('Total number of data points = %10.6g \n',n);
printf('Total number of classes = %10.6g \n',k);
disp(' ');
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
xset('window',1);xbasc(1);
histplot(k,x);
xtitle('histogram','x','rel.f');
xset('window',2);xbasc(2);
xset('mark',-9,2);
plot2d(cmark,fcumulrel,-9);
plot2d(cmark,fcumulrel,1);
xtitle('ogive','x','cum.rel.f');
//end function
In this example, we generate a vector of 100 random data values and apply
function freqdist to it:
-->x=int(100*rand(1,100));
-->getf('freqdist')
-->min(x), max(x)
ans =
1.
ans =
99.
-->xclass = [0:10:100];
-->freqdist(x,xclass)
Frequency distribution
=================================================================================
Class LowBound UppBound Class Mark Freq. RelFreq. CumFreq. RelCumFreq
=================================================================================
1 0 10 5 8 .08 8 .08
2 10 20 15 10 .1 18 .18
3 20 30 25 12 .12 30 .3
4 30 40 35 7 .07 37 .37
5 40 50 45 9 .09 46 .46
6 50 60 55 12 .12 58 .58
7 60 70 65 13 .13 71 .71
8 70 80 75 12 .12 83 .83
9 80 90 85 6 .06 89 .89
10 90 100 95 11 .11 100 1
=================================================================================
Total number of data points = 100
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
Total number of classes = 10
Skewness and kurtosis
The skewness and kurtosis of a data sample are measures related to the shape of the data
frequency distribution. These two measurements are defined in terms of the second, third
and fourth moment of the data about the mean.
The skewness of a sample data is defined as
Sk = m
3
/m
2
1.5

= m
3
/s
x
3
.
For symmetric frequency distributions, Sk = 0. If the distribution is skewed to the left, the
bulk of the distribution is located on the right-hand side of the plot with a long tail of smaller
values. In this case, Sk < 0. If, on the other hand, the distribution is skewed to the right, the
bulk of the distribution is located on the left-hand side of the plot with a long tail of larger
values. In this case, Sk > 0. Sketches of symmetric and skewed distributions are shown
below.
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
The kurtosis is defined as
K = m
4
/m
2
2
= m
4
/s
x
4
.
This is a measure of the peakedness of the frequency distribution usually referred to that of
the normal distribution (see Chapter ), which has a kurtosis of 3.0, i.e., K
normal
= 3.0. A
frequency distribution that has a relatively high peak is called leptokurtic (K > 3), while one
which is flat-topped is referred to as platykurtic (K < 3). A frequency distribution which is
neither highly peaked nor flat, as shown below, is called mesokurtic (K = 3). The figure below
illustrates leptokurtic, platykurtic, and mesokurtic frequency distributions.
As an example, we generate a vector with 100 random numbers as elements:
-->n = 100; x= int(100*rand(1,n));
Using function freqdist we obtain the following histogram for the data:
The mean value and standard deviation of the sample are given by:
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
-->xbar = mean(x)
xbar = 45.66
-->sx = st_deviation(x)
sx = 29.590546
To calculate the skewness of the sample we first calculate the third moment about the mean
and then divide it by the cube of the standard deviation:
-->m3 = sum((x-xbar)^3)/(n-1)
m3 = 6885.0511
-->Sk = m3/sx^3
Sk = .2657347
Calculation of the kurtosis requires the calculation of the fourth moment about the mean,
which is then divided by the fourth power of the standard deviation:
-->m4 = sum((x-xbar)^4)/(n-1)
m4 = 1398425.3
-->K = m4/sx^4
K = 1.8240106
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
Probability
We are familiar with the concept of probability as applied to everyday occurrences such as
precipitation, earthquake occurrence, games of chance, etc. Basically, a probability is a
number between 0 and 1 that provides an estimate of the likelihood that a certain event will
occur. To define probability formally, we need to use the concept of a sample space.
Sample space and events
A sample space, , is the set of all possible outcomes of an experiment (the term experiment
is used here to mean occurrences of any event, and not necessarily a scientific experiment).
For example, tossing a coin can produce only the events head (H) or tail (T), therefore,
the sample space corresponding to this experiment will be = {H, T}. Casting a die, on the
other hand, produces 6 possible outcomes, thus, its sample space is the set = {1, 2, 3, 4, 5,
6}.
An event is a sub-set of the sample space, for example, the event described as obtaining an
even number while casting a die can be described as the set A = {2, 4, 6}. On the other
hand, the event described as obtaining an odd number while casting a die can be written as
B = {1, 3, 5}.
Sets
In terms of mathematical set theory, we say, for example, that element 2 belongs to set A, and
write it as 2A. To indicate, for example, that element 3 does not belong to A, we use the
notation: 3A. We also say that set A is a sub-set of the sample space , and write it as A
. For the case under consideration we can also write B .
The sample space, , which contains all possible outcomes of an experiment, is also referred
to as the universe or universal set. A set that contains no elements is referred to as the
empty set, = { }. The definition of the empty set implies that it is a subset of all other sets,
i.e., , A, and B.
Given a set A ,A , we define the complement of set A as the set A such that the
elements of A are those elements of not contained in A. For example, for the case of the
sample space for casting a die, the complement of set A is A = {1, 3, 5} = B. For that
particular experiment we can also write B = A.
By definition = , and = .
Set operations
The union of two sets is the set that results from incorporating the elements that belong to A
or B, or to both. For example, consider as the universe the set of digits in the decimal system,
i.e., = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, and consider the sets A = {0, 1, 2, 3, 4}, and B = { 3, 4, 5,
6}. The union of A and B, written as A B, is the set, C = AB = {0, 1, 2, 3, 4, 5, 6}. Notice
that the elements that are common to both A and B, i.e., 3,4, are included in the union only
once. In general, when dealing with sets, repeated elements are listed only once.
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
The intersection of two sets is the set that contains elements that belong to A and B
simultaneously. For the sets A and B used before, the intersection will be the set D = AB = {3,
4}.
Notice that the logical particle or is associated with the union of two sets, while the logical
particle and is associated with the intersection of sets. This is particularly important when
describing events for probability calculations. For example, lets define the events A and B as
follows:
A = set of natural numbers (i.e., positive integers) that are multiples of 2 = {2, 4, 6, 8, 10, }
A is the set of the even numbers, and it is an infinite set. Also,
B = set of natural numbers that are multiples of 3 = {3, 6, 9, 12, 15, .}.
Now, define the event C as follows:
C = set of natural numbers that are multiples of 2 or multiples of 3 = {2, 3, 4, 6, 8, 9, 10, 12,
14, 15, }. The logical particle or in this case suggests a union, i.e., C = AB.
The event D is defined as
D = set of natural numbers that are multiples of 2 and 3, simultaneously = {6, 12, 18, 24, }.
In this case, the logical particle and indicates an intersection.
From the definition of the universal set, , and the empty set, , for any set A , the
following properties hold true:
A = , A = A, A=A, A= , AA = , AA = .
SCILAB includes the functions union and intersect to operate on sets of numbers entered as
SCILAB vectors. For example, consider the set:
-->A = [2 3 -1 2 4 5 7 -3 -5 4 6 -2 1 2];
A set of numbers may include repeated elements. To determine the unique entries in a set use
function unique:
-->unique(A)
ans =
! - 5. - 3. - 2. - 1. 1. 2. 3. 4. 5. 6. 7. !
A call to the function unique, when assigned to a vector of two variables, returns not only the
set (or vector) of unique values but also the location of the first occurrence of each element in
the original set. For example, in the following call to function unique, the vector of unique
values is returned in variable AU while the position of those elements in the original vector A is
returned in vector Ak:
-->[AU,Ak] = unique(A)
Ak =
! 9. 8. 12. 3. 13. 1. 2. 5. 6. 11. 7. !
AU =
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
! - 5. - 3. - 2. - 1. 1. 2. 3. 4. 5. 6. 7. !
Next, we generate a vector of 20 integer random elements and determine its unique elements:
-->B = int(10*rand(1,20))
B =
column 1 to 11
! 0. 5. 4. 8. 5. 9. 4. 5. 7. 7. 5. !
column 12 to 20
! 4. 7. 4. 6. 9. 1. 5. 2. 5. !
-->[BU,Bk] = unique(B)
Bk =
! 1. 17. 19. 3. 2. 15. 9. 4. 6. !
BU =
! 0. 1. 2. 4. 5. 6. 7. 8. 9. !
SCILAB provides functions union and intersect to perform the operations of union and
intersection of SCILAB vectors representing sets. For example, using the sets A and B defined
above we can write, for their union:
-->union(A,B)
ans =
column 1 to 11
! - 5. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5. 6. !
column 12 to 14
! 7. 8. 9. !
Function union can be called as shown below to produce not only the union of the two sets, but
also the location where the elements of the union occur in the two original sets:
-->[AunionB,kA,kB] = union(A,B)
kB =
! 1. 4. 6. !
kA =
! 9. 8. 12. 3. 13. 1. 2. 5. 6. 11. 7. !
AunionB =
column 1 to 11
! - 5. - 3. - 2. - 1. 0. 1. 2. 3. 4. 5. 6. !
column 12 to 14
! 7. 8. 9. !
The intersection of sets A and B is obtained by using function intersect, for example:
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
-->intersect(A,B)
ans =
! 1. 2. 4. 5. 6. 7. !
To obtain information on the location, in the original sets, of the elements of the intersection
of two sets we can use, for example:
-->[AintersectB,kA,kB] = intersect(A,B)
kB =
! 17. 19. 3. 2. 15. 9. !
kA =
! 13. 1. 5. 6. 11. 7. !
AintersectB =
! 1. 2. 4. 5. 6. 7. !
Venn diagrams
Venn diagrams are geometrical figures used to represent mathematical set. The universal set,
, is typically represented as a rectangle, while its subsets are represented as circles within
the rectangle. The figures below illustrate the operations of union and intersection of sets A
and B using Venn diagrams.
The complements of sets A and B are shown as shaded areas in the following Venn diagrams:
Venn diagrams can be used to obtain general results for set operations. For example, you can
use Venn Diagrams to prove the following set identity: (AB) = AB. The set (AB) is the
complement of AB, represented by the shadowed area in the following Venn diagram:
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
The same area represents the intersection of the complements of A and B as follows from
superimposing the corresponding Venn diagrams for A and B.
Definitions of probability
For an experiment, such as the casting of a die, where all possible outcomes can be identified,
we use the classical definition of probability: If n

represents the number of all possible
outcomes of an experiment, and n
A
represents the number of outcomes corresponding to event
A, we define the probability of event A as P(A) = n
A
/n
.
In many instances, we cannot identify all possible outcomes of an experiment, but we have
access to records of occurrences of a certain event of interest. For example, if we are
interested in estimating the probability that there will be a flood in a specific river location
during a given year, we can look at the records of water levels kept on that particular location
and determine the number of years out of the total length of the record where a flooding had
occurred. This is the frequency definition of probability. It assumes that the relative
frequency of occurrence of a certain event is a good estimate of the probability of that event.
Thus, if we have a record of n sets of data out of which event A is known to have occurred n
A
times, the frequency definition of probability indicates that P(A) = n
A
/n.
Probability axioms
Having defined probability as a number, the following properties hold:
0 P(A) 1
P() = 1
P() = 0
If AB = , A and B are said to be mutually exclusive events, and P(AB) = P(A) + P(B),
P(A) = 1 P(A)
Addition rule
If A and B are not mutually exclusive events, i.e., if AB , then the probability of their
union is given by
P(AB) = P(A) + P(B) P(AB)
This result can be generalized for the case in which one has n sets A
k
, k = 1, 2, , n, that are
mutually exclusive, i.e., A
k
A
m
, if k m. In such case we can write:
P(A
1
A
2
A
n
) = P(A
1
) + P(A
2
) + + P(A
n
).
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
Counting
The classical definition of probability requires the counting of the number of elements in a
given event. For example, suppose that we want to evaluate a piece of equipment based on
the following criteria:
1.Ease of operation: three categories are defined: E
1
= easy; E
2
= medium; and E
3
= difficult.
2.Price: two categories are defined: P
1
= high price; P
2
= low price.
3.Cost of Repair: three categories are defined: C
1
= high cost; C
2
= medium cost; and C
3
= low
cost.
How many evaluation classes will there be considering all possible combinations of the three
criteria? One easy way to visualize those classes is by creating a three diagram as shown
below. The first "branching" of the tree is the first criteria (ease of operation). Out of each of
the three branches, E1, E2, and E3, we next draw two branches corresponding to the second
criteria (price). At the extreme of each of those branches we draw extra branches
corresponding to the third criteria (cost of repair). The tree diagram shows the existence of 18
different possible combinations of the three criteria.
The number of combinations can be estimated by multiplying the number of options at each
level of branching, i.e., 3 x 2 x 3 = 18. This result can be generalized by saying:
If sets A
1
, A
2
, ..., A
k
contain, respectively, n
1
, n
2
,..., n
k
elements, there are n
1
n
2
n
k
ways
of choosing first an element of A
1
, then an element of A
2
, ..., and finally an element of A
k
.
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Permutations and combinations
In general, if r objects are chosen from a set of n distinct objects, any particular arrangement,
or order, of these objects is called a permutation. For instance, 4 1 2 3 is a permutation of
the four positive integers. Other permutations of the first four integer numbers are 1 2 3 4, 1 3
2 4, 1 4 2 3, 1 4 3 2, etc. In other words, the order in which the objects are taken in important
and each ordering defines a permutation.
The total number of permutations of r objects selected from a set of n distinct objects is
n
P
r
= P(n,r) = n(n-1)(n-2)...(n-r+1) = n!/(n-r)!
where n! (n factorial) is defined as n! = n(n-1)(n-2)...321, and 0! = 1.
If the order in which the elements of a set are selected is not important, instead of a
permutation we have a combination of objects. For example, there is only one combination of
the four first integer numbers {1 2 3 4}, regardless of whether you show them as {1 2 3 4}, or {4
3 2 1}, or {1 3 2 4}, etc.
The total number of combinations of r objects selected from a set of n distinct objects is
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
)! ( !
!
) , (
r n r
n
r
n
r n C C
r n

= =
__________________________________________________________________________________
The Gamma function and factorials
The Gamma function is a specialized mathematical function that has a variety of applications
in probability, differential equations, and other branches of mathematics. It is defined by the
following integral:
The Gamma function has the property that
(z+1) = z (z).
Thus,
(z) = (z-1) (z), (z-1) = (z-2) (z-2),
and so on. If z is an integer, say z = n, then we can write
(n+1) = n(n-1)(n-2)321 = n!
SCILAB provides function gamma to calculate the mathematical function (z), for example, the
value (5) is calculated as:
-->gamma(5)
ans =
24.
You can check that (5) is equal to 4! = 24:
-->4*3*2*1
ans =
24.
A plot of the Gamma function is attempted with the following SCILAB commands:
-->x = [-4:0.1:4]; y = gamma(x);
-->plot(x,y,'z','Gamma(z)','The function Gamma')
However, you will notice that you get an empty plot. The reason for this is the fact that (z)
goes to infinity for negative integer values (i.e., z = , -5, -4, -3, -2, -1). Thus, (-4) is
evaluated in SCILAB as:
-->gamma(-4)
ans =
= ( ) z d

e
( ) t
t
( ) z 1
t
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
1.790+308
While (5) shows a finite value:
-->gamma(-3.9)
ans =
.4919058
When SCILAB attempts to plot the point (-4,1.7910
308
) and points like (-3.9,0.4919058) in the
same plot, the result is the empty plot obtained earlier. To avoid the infinite values we re-
define the range of x to read:
-->x = [-3.9:0.11:3.9]; y = gamma(x);
Next, we attempt again to plot the function Gamma:
-->plot(x,y,'z','Gamma(z)','The function Gamma')
The result is the following plot:
Notice that for negative integer values of z the function shows upward and downward peaks.
These peaks actually represent the trend of the function to become at those values.
__________________________________________________________________________________
Permutations and combinations using the Gamma function
Since SCILAB does not recognize the symbol ! as the factorial operation, we redefine the
calculation of permutations and combinations in terms of the Gamma function, as follows:
n
P
r
= P(n,r) = n(n-1)(n-2)...(n-r+1) = n!/(n-r)! = (n+1)/ (n-r+1),
and
.
) 1 ( ) 1 (
) 1 (
)! ( !
!
) , (
+ +
+
=

= =
r n r
n
r n r
n
r
n
r n C C
r n
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
We can define functions Perm(n,r) and Comb(n,r) in SCILAB to calculate permutations and
combinations as follows:
-->deff('[P]=Perm(n,r)','P=gamma(n+1)/gamma(r+1)')
-->deff('[P]=Comb(n,r)','P=gamma(n+1)/(gamma(r+1)*gamma(n-r+1))')
Some calculations of permutations and combinations are shown next:
-->Perm(10,3)
ans = 604800.
-->Perm(10,10)
ans =
1.
-->Perm(10,9)
ans =
10.
-->Perm(10,8)
ans = 90.
-->Comb(10,3)
ans = 120.
-->Comb(10,10)
ans = 1.
-->Comb(10,9)
ans = 10.
-->Comb(10,8)
ans = 45.
Conditional probability
Conditional probability evaluates the probability of an event, say A, given the fact that event
B has occurred. This is written as
P(A|B) = P(AB)/P(B).
This formula is read as the probability of A given B. The figure below illustrates how
imposing the condition that B has occurred modifies the probability calculation by producing a
reduced sample space, i.e., event B, and a reduced event of interest, namely, AB.
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
Thus, using the classical definition of probability, we can write
P(A|B) = n(AB)/n(B),
Where the function n( ) represents the number of elements in a given set. If n() represents
the number of elements in the original universe, then we can write:
P(A|B) = [n(AB)/n()]/[n(B)/n()] = P(AB)/P(B).
Independent events
Two events A and B are said to be statistically or stochastically independent if
P(A|B) = P(A), if P(B) > 0
or
P(B|A) = P(B), if P(A) > 0.
An implication of this definition is that for independent events
P(AB) = P(A)P(B)
Total probability
Suppose that the sample space is divided into n mutually exclusive events, B
i
, i = 1, 2, , n, as
illustrated in the figure below. Suppose also that we define an event A within the sample
space represented by the shadowed ellipse in the figure.
Because the events B
i
are mutually exclusive, so are the sets resulting from the intersection of
event A and each of the events B
i
. Therefore, we can write
A = (AB
1
)(AB
2
) (AB
n
)
and
P(A) = P(AB
1
) + P(AB
2
) + + P(AB
n
),
i.e.,
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz

=
=
n
i
i
B A P A P
1
) ( ) (
From the definition of conditional probability, P(AB
i
) = P(A|B
i
)P(B
i
), therefore, we can write

=
=
n
i
i i
B P B A P A P
1
) ( ) | ( ) (
This result is known as the theorem of total probability.
Bayes theorem
We refer again to the set of n mutually exclusive and exhaustive events used in the theorem of
total probability. From the definition of conditional probability it follows that
.
) ( ) | (
) ( ) | (
) (
) (
) | (
1

=
n
i
i i
j j j
j
B P B A P
B P B A P
A P
A B P
A B P
Exercises
[1]. Add SCILAB code to the calculation of the geometric mean and the harmonic mean to the
function describe(). Include fprintf statements in the function to report such values.
[2]. Apply the modified describe() function from problem [1] to the following data
representing measurements of the diameter of a cylinder produced for a precision mechanism:
232. 248. 242. 250. 239. 244. 265. 262. 259. 236.
246. 308. 221. 275. 261. 217. 260. 273. 228. 269.
260. 247. 228. 274. 205. 254. 230. 252. 263. 255.
244. 264. 243. 255. 261. 236. 226. 264. 260. 265.
267. 243. 270. 275. 260. 281. 240. 257. 268. 231.
[3]. Use the SCILAB functions hist and bar to plot a histogram for the data from problem [2].
Use at least 5 classes for the histogram.
[4]. Use function freqdist(), shown above, to produce the table for the frequency
distribution for the data from problem [2].
[5]. Use function describe() to obtain a summary of statistics for the following data set
representing the time to failure, in years, of light bulbs.
1.39 1.07 3.22 3.67 .55 .81 1.22 1.26 .05 1.54
.97 1.01 .44 1.97 1.9 .89 3.25 .85 1.04 .43
1.33 .82 2.04 1.02 .53 .13 2.06 2.96 1.96 1.5
3.05 .42 1.17 1.72 2.68 .56 2.13 1.56 2.09 1.26
3.21 .74 3.04 2.74 .83 .79 1.56 1.55 .96 1.23
[6]. Use the SCILAB functions hist and bar to plot a histogram for the data from problem [5].
Use at least 5 classes for the histogram.
[7]. Use function freqdist(), shown above, to produce the table for the frequency
distribution for the data from problem [5].
[8].Use function describe() to obtain a summary of statistics for the following data set
representing the yearly rainfall depth, in mm, recorded at a certain location:
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
126. 82.9 41.5 4.35 346. 102. 830. 12.8 366. 471.
408. 189. 646. 7.82 313. 17.4 165. 24.5 32.6 39.3
277. 13.7 52.3 171. 314. 60.6 29.1 468. 887. 44.5
135. 215. 106. 201. 51. 43. 335. 59.4 174. 870.
[9]. Use the SCILAB functions hist and bar to plot a histogram for the data from problem [8].
Use at least 5 classes for the histogram.
[10]. Use function freqdist(), shown above, to produce the table for the frequency
distribution for the data from problem [8].
[11]. Use function describe() to obtain a summary of statistics for the following data set
representing the number of vehicles stopping at a service station in a given hour:
3. 5. 6. 4. 5. 9. 4. 4. 11. 4.
4. 8. 5. 4. 4. 6. 7. 4. 7. 8.
6. 9. 10. 7. 4. 3. 5. 9. 9. 11.
6. 5. 9. 12. 11. 5. 13. 8. 10. 6.
4. 5. 9. 8. 7. 5. 3. 6. 5. 5.
8. 3. 11. 4. 5. 9. 5. 1. 8. 6.
[12]. Use the SCILAB functions hist and bar to plot a histogram for the data from problem
[11]. Use at least 5 classes for the histogram.
[13]. Use function freqdist(), shown above, to produce the table for the frequency
distribution for the data from problem [11].
[14]. Write a SCILAB function that takes as input a row vector of data, x, and a positive integer
number r, and calculates the r-th moment of the sample data about zero (m
r
) and the r-th
moment of the sample data about the mean (m
r
). Use your function to calculate the first five
moments m
r
and m
r
( r = 1,2,3,4,5) for the data from (a)problem [2], (b) problem [5], (c)
problem [8], and (d) problem [11].
[15]. Using the results from problem [14], calculate the skewness and kurtosis of the data sets
from (a)problem [2], (b) problem [5], (c) problem [8], and (d) problem [11].
[16]. Use Venn diagrams to verify that:
(a) (A

= AB; (b) (AB

)

= A

B.
[17] As a water resources engineer for the state of Utah, you analyze the water surface
elevation at a location of the Colorado river in South Eastern Utah and decide to divide the
ranges of possible water surface elevations into the following sets:
Let WS represent water surface elevation in ft:
A
1
= { WS < 2500 ft}
A
2
= { 2500 ft WS < 2600 ft }
A
3
= { 2600 ft WS < 2700 ft }
A
4
= { WS 2700 ft}
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
Data for the monthly maximum water surface elevation from the last ten years show that the
water surface elevation at the point of interest was determine to belong to the four ranges
detailed above with the following frequencies:
n
1
= n(A
1
) = 10
n
2
= n(A
2
) = 120
n
3
= n(A
3
) = 80
n4= n(A
4
) = 30
Using the frequency definition of probability, and based on this 20-year data, determine the
following probabilities:
(a) P(A
1
) (b) P(A
1

3
) (c) P(A
1

3
) (d) P( A
1
c
)
[18]. Given P(A) = 0.3, P(B) = 0.5, and P(A B) = 0.24, find:
(a)Pr[AB] (b)P(A

B) (c)P(AB

)
(d)P(A

)
[19]. In a sample of 446 cars stopped at a roadblock, only 67 drivers had their seatbelts
fastened. Estimate the probability that a driver stopped on that road will have his or her
seatbelt fastened.
[20]. If events A and B are independent and P(A) = 0.25 and P(B) = 0.40, find:
(a)P(AB) (b)P(A|B) (c)P(AB) (d)P(A

)
[21]. A building inspector has to check the wiring in a new apartment building either on
Monday, Tuesday, Wednesday, or Thursday, and at 8 A.M., 1 P.M., or 2 P.M. Draw a tree
diagram showing the various ways in which the inspector can schedule the required inspection.
[22]. In an optical kit there are 6 concave lenses, 4 convex lenses, and 3 prisms. In how many
ways can one choose one of the concave lenses, one of the convex lenses, and one of the
prisms?
[23]. In how many ways can a television director schedule 6 different commercials during the 6
time slots allocated to commercials during the telecast of the first period of a hockey game?
[24]. Determine the number of ways in which a manufacturer can choose 2 of 15 locations for a
new warehouse.
[25]. If the order does not matter, in how many ways can 4 of 18 robotic arms be chosen for a
special welding job?
[26]. The supply department has 8 different electric motors and 5 different starting switches.
In how many ways can 2 motors and 2 switches be selected for an experiment concerning a
tracking antenna?
[27]. The following frequency table shows the classification of 58 landfills in a state according
to their concentration of the three hazardous chemicals arsenic, barium, and mercury:
Barium
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
Barium
High Low
Mercury Mercury
High Low High Low
Arsenic High 1 4 5 9
Low 4 8 10 18
If a landfill is selected at random, find the probability that it has a:
(a) high concentration of mercury;
(b) high concentration of barium and low concentrations of arsenic and mercury;
(c) high concentrations of any two of the chemicals and low concentration of the third;
(d) high concentration of any one of the chemicals and low concentrations of the other two.
[28]. Refer to exercise 27. Given that a landfill, selected at random, is found to have a high
concentration of barium, what is the probability that its concentration is
(a) high in mercury?
(b) low in both arsenic and mercury?
(c) high in either arsenic or mercury?
[29]. The data in the table below, shows the annual maximum flow for the Ganga River in
India measured at specific station.
Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s)
1885 7241 1907 7546 1929 4545 1951 4458
1886 9164 1908 11504 1930 5998 1952 3919
1887 7407 1909 8335 1931 3470 1953 5470
1888 6870 1910 15077 1932 6155 1954 5978
1889 9855 1911 6493 1933 5267 1955 4644
1890 11887 1912 8335 1934 6193 1956 6381
1891 8827 1913 3579 1935 5289 1957 4548
1892 7546 1914 9299 1936 3320 1958 4056
1893 8498 1915 7407 1937 3232 1959 4493
1894 16757 1916 4726 1938 3525 1960 3884
1895 9680 1917 8416 1939 2341 1961 4855
1896 14336 1918 4668 1940 2429 1962 5760
1897 8174 1919 6296 1941 3154 1963 9192
1898 8953 1920 8174 1942 6650 1964 3024
1899 7546 1921 9079 1943 4442 1965 2509
1900 6652 1922 7407 1944 4229 1966 4741
1901 11409 1923 5482 1945 5101 1967 5919
1902 9164 1924 19136 1946 4629 1968 3789
1903 7404 1925 9680 1947 4345 1969 4546
1904 8579 1926 3698 1948 4890 1970 3842
1905 9362 1927 7241 1949 3619 1971 4542
1906 7092 1928 3698 1950 5899
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Random Variables with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
RANDOM VARIABLES 2
Notation 2
Probability Distribution for Discrete Random Variables 2
The Poisson distribution 4
Function cdfpoi 6
Applications of the function cdfpoi 7
Probability Distribution for Continuous Random Variables 8
The standard normal distribution 9
The expectation operator 12
The mean value, variance, and standard deviation of the distribution 13
Moments 16
Skewness and kurtosis 17
Calculating moments for a discrete distribution 17
Calculating moments for a continuous distribution 19
Multivariate probability distributions 20
Discrete random variables 20
Marginal probability mass functions 21
Joint cumulative distribution function 22
Working with tables of discrete bi-variate distributions in SCILAB 22
Covariance and correlation coefficient 24
Continuous random variables 26
Joint cumulative distribution function 27
Covariance and correlation coefficient 27
SCILAB calculations for bi-variate continuous distributions 28
Exercises 30
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Random Variables
A random variable is a function that assigns a numerical value to a particular event within a
sample space. For example, let X be a random variable representing the number of rainy days
in a particular week in a specific location. Obviously, X can take values of 0, 1, 2, 3, 4, 5, 6,
and 7. The event associated with the value X = 3, for example, can be described as A
3
= {it
rains three days in the week under consideration}.
The example of the number of rainy days presented above is an example of a discrete random
variable, i.e., a variable that can only take a finite number of values. Another example of a
discrete variable is the number of cars driving through a particular tollbooth in a given
highway. This is a discrete variable because it can only take the value zero or a positive
integer. Some discrete variables can take non-integer values. For example, to keep track of
daily parking times in an airport parking lot, the times may be recorded every half hour.
Therefore, if X is a random variable representing the recorded parking times, X can take the
values of 0, 0.5, 1.0, , 23.5, 24.0.
If we are interested, for example, in analyzing the concentration of a particular contaminant in
a water sample, the concentration measured (say, in ppm or mg/l) is a continuous random
variable. The compressive strength of concrete samples selected randomly is also a
continuous variable since any value of the strength can be measured in a particular test. A
continuous random variable, therefore, can take any value in a continuous range. For
probability applications, rather than concentrating on the probability of a specific value of a
continuous random variable, we deal with the probabilities of ranges.
Notation
When dealing with random variables it is customary to use upper-case letters, e.g., X, Y, Z,
etc., to refer to the name of the random variable, and to use lower-case letters, e.g., x, y, z,
etc., to refer to the actual value taken by the variable. This notation allows the writing of
statements such as P(X = x), read as the probability of the random variable X taking the value
x. We can also write statements such as P(x
1
<X<x
2
), interpreted as the probability that the
random variable X takes values between x
1
and x
2
, not including the extremes of the interval.
An expression such as P(Xx) represents the probability that X is at least equal to x.
Probability Distribution for Discrete Random
Variables
To describe the distribution of probabilities for a discrete random variable X we use a function
called the probability mass function (pmf) defined as f
X
(x) = P(X = x). (The reference to this
function as a mass function is in analogy to point mass distributions on the x-axis in
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
mechanics). For example, the following table shows the probability mass function for a
discrete random variable X:
x -2 -1 0 1 2
f
X
(x) 0.2 0.3 0.1 0.2 0.2
A plot of the pmf, produced by SCILAB, is shown below:
-->X = [-2:1:2];
-->fX = [0.3 0.2 0.1 0.2 0.2];
-->plot2d3('gnn',X',fX',1,'111','prob. mass function',[-3,0,3,0.5])
The probability mass function has the following properties:
0 f
X
(x) 1
. 0 . 1 ) (

x all
X
x f
The cumulative distribution function (cdf) is defined as
. ) ( ) ( ) ( ) (



x k
X
x k
X
k f k X P x X P x F
For example, for the pmf defined by the table shown above, the cdf is given by the table:
x -2 -1 0 1 2
F
X
(x) 0.2 0.5 0.6 0.8 1.0
The graph for the cdf is shown below:
-->FX = [0.3 0.5 0.6 0.8 1.0]
FX =
! .3 .5 .6 .8 1. !
-->xset('window',1)
-->plot2d3('gnn',X',FX',1,'111','cumulative prob. function',[-3,0,3,1.2])
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
Most discrete random variables of interest are such that they take only integer values, say, X =
0,1,2, , n. In such case, we can write the following formulas for calculating probabilities:
) ( ) ( x f x X P
X




x
k
x
k
X X
x f k X P x F x X P
0 0
) ( ) ( ) ( ) (

<
1
0
1
0
) ( ) ( ) 1 ( ) 1 ( ) (
x
k
x
k
X X
x f k X P x F x X P x X P



n
x k
X
n
x k
k f k X P x X P ) ( ) ( ) (

+ +
+ >
n
x k
X
n
x k
k f k X P x X P x X P
1 1
) ( ) ( ) 1 ( ) (
The next two formulas use the formula for the probability of the complement [P(A) = 1P(A) ]:

<
1
0
1
0
) ( 1 ) ( 1 ) 1 ( 1 ) 1 ( 1 ) ( 1 ) (
x
k
x
k
X X
x f k X P x F x X P x X P x X P


>
x
k
x
k
X X
x f k X P x F x X P x X P
0 0
) ( 1 ) ( 1 ) ( 1 ) ( 1 ) (
The Poisson distribution
For example, suppose that discrete random variable X stands for the number of vehicles per
hour visiting a service station on a road. This random variable follows the so-called Poisson
distribution. The Poisson distribution has a probability mass function given by
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz

,..., 2 , 1 , 0 ,
!
) ( x
x
e
x f
x
X

To indicate that a discrete random variable X follows the Poisson distribution, the notation X ~
Poisson(), is sometimes used.
The parameter represents the mean value of vehicles per hour visiting the service station.
Suppose that this value has been determined to be 8.5, then the pmf is

,..., 2 , 1 , 0 ,
!
5 . 8
) (
5 . 8
x
x
e
x f
x
X
To define such a function in SCILAB we calculate the factorial through the use of the Gamma
function, i.e., (n+1) = n!. The Gamma function is available in SCILAB as gamma(x). Thus, we
can define the following function fpoisson, representing the probability mass function of the
Poisson distribution, as
-->deff('[fX]=fpoisson(x,lambda)','fX = exp(-lambda)*lambda^x/gamma(x+1)')
Using this function and the formulas shown earlier for probability calculations of discrete
variables, we proceed to calculate the following:
P(exactly 10 vehicles visit the service station in an hour) = P(X = 10) = f
X
(10) = e
-8.5
8.5
10
/10! =
0.1104
-->lambda = 8.5; fpoisson(10)
ans =
.1103883
P(7 vehicles or less will visit the service station in an hour) =
3856 . 0
!
5 . 8
) ( ) 7 ( ) 7 (
7
0
5 . 8 7
0

k
k
k
X X
k
e
k f F X P
-->xx=[0:7], pp = fpoisson(xx), prob = sum(pp)
xx =
! 0. 1. 2. 3. 4. 5. 6. 7. !
pp =
column 1 to 5
! .0002035 .0017295 .0073503 .0208258 .0442549 !
column 6 to 8
! .0752333 .1065806 .1294192 !
prob =
.3855971
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
P(less than 3 vehicles will visit the service station in an hour) =
00928 . 0
!
5 . 8
) ( ) 2 ( ) 2 ( ) 3 (
2
0
5 . 8 2
0

<

k
k
k
X X
k
e
x f F X P X P
-->xx = [0:2]; pp = fpoisson(xx,lambda); sum(pp)
ans =
.0092832
P(more than 5 will visit the service station in an hour) =
8504 . 0 1496 . 0 1
!
5 . 8
1 ) 5 ( 1 ) 5 (
5
0
5 . 8

>

k
k
k
e
X P X P
-->xx=[0:5];pp=fpoisson(xx);1-sum(pp)
ans =
.8504027
P(10 or more vehicles will visit the service station in an hour) =
3470 . 0 6530 . 0 1
!
5 . 8
1 ) 9 ( 1 ) 10 (
9
0
5 . 8

k
k
k
e
X P X P
-->xx=[0:9];pp=fpoisson(xx);1-sum(pp)
ans =
.3470263
SCILAB provides a variety of functions for calculating the cumulative distribution function and
its inverse for a variety of discrete and continuous distributions. These will be presented in
more detail in a later chapter. In this section we describe function cdfpoi corresponding to
the Poisson distribution.
Function cdfpoi
Function cdfpoi stands for cumulative distribution function for the Poisson distribution. A
call to function cdfpoi may involve two or more parameters which we will identify as follows:
p = a probability representing the Poisson cdf, i.e., p = P(X S)
q = 1-p = the probability of the complement, i.e., q = P(X > S) = P(X S+1)
S = the upper value of X in probability p = P(X S)
Xlam = the parameter of the Poisson distribution
Also, any of these parameters may be returned by the function call based on the values of the
other parameters used as arguments in the call to function cdfpoi. The following are the
three possible function calls specifying the arguments to use and the parameters returned:
1) [p,q]=cdfpoi("PQ",S,Xlam)
2) [S]=cdfpoi("S",Xlam,P,Q)
3) [Xlam]=cdfpoi("Xlam",P,Q,S);
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
Function call number 1 returns the probabilities p and q given the value of S,
and of the Poisson distribution parameter = Xlam
Function call number 2 returns the value of S given the Poisson distribution
parameter = Xlam and the probabilities p and q.
Function call number 3 returns the value of the Poisson distribution parameter
= Xlam, given the probabilities p and q corresponding to the value of S.
The string occupying the first position in the list of arguments specifies the type of parameter
requested from the function call. Thus, the strings corresponding to the three different types
of calls to cdfpoi are:
PQ - to request probabilities p and q
S - to request a value of the Poisson variable, from P(X S) = p and P(X>S) = q.
Xlam - to request the parameter of the Poisson distribution
Care must be exercised when calling the function so that the arguments are in the appropriate
order.
Applications of the function cdfpoi
Suppose that X follows the Poisson distribution with = 5.5. Calculate the following
probabilities:
P(X 6) = F
X
(6):
-->cdfpoi("PQ",6,5.5)
ans =
.6860360
P(X<3) = P(X 2) = F
X
(2):
-->cdfpoi("PQ",2,5.5)
ans =
.0883764
P(X > 4) = 1 - P(X 4) = 1 - F
X
(4). The result is the q in the following call to cdfpoi:
-->[p,q] = cdfpoi("PQ",4,5.5)
q =
.642482
p =
.357518
P(X 2) = 1 - P(X<2) = 1 - P(X 1) = 1 - F
X
(1). The result is the value of q in:
-->[p,q] = cdfpoi("PQ",1,5.5)
q =
.9734360
p =
.0265640
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
P(3 X 5) = P(X 5) - P(X 2):
-->cdfpoi("PQ",5,5.5) - cdfpoi("PQ",2,5.5)
ans =
.4405423
P(X = 0) = F
X
(0) = f
X
(0), since X = 0 is the first possible value of X:
-->cdfpoi("PQ",0,5.5)
ans = .0040868
P(X=3) = P(X 3) - P(X 2) = F
X
(3) - F
X
(2) = f
X
(3):
-->cdfpoi("PQ",3,5.5) - cdfpoi("PQ",2,5.5)
ans =
.1133228
The function cdfpoi can be used also to determine the value of S in P(XS) = p, or P(X>S) = q,
given . For example, if it is known that the average number of maintenance vehicles visiting
a service station during a certain period of time is 3.2 vehicles per hour, what would be the
number of vehicles, S, such that the probability that at most S vehicles will visit the service
station in one hour is 0.85? The appropriate call to function cdfpoi is:
-->cdfpoi("S",3.2,0.85,1-0.85)
ans =
4.5411371
To determine the Poisson distribution parameter given the probability p = P(X S) and the
value S, we use a call to cdfpoi as illustrated in the next example:
-->cdfpoi("Xlam",0.75,0.25,4)
ans =
3.3686004
Probability Distribution for Continuous Random
Variables
The calculation of probabilities for continuous random variables is based on a probability
density function (pdf), f
X
(x), such that f
X
(x) 0. The probability that the random variable X is
located between the values x
1
and x
2
is given in terms of the pdf by the equation:
The probability that the continuous random variable X takes a specific value is zero, i.e., P(X =
x). This can be checked by taking x
1
= x x and x
2
= x + x, where x is a small increment,
and calculating the probability
. ) ( ) (
2
1
2 1

< <
x
x
X
dx x f x X x P
. 0 ) ( 2 lim ) ( lim ) ( lim ) (
0 0 0
+ < <

+


x f x dx x f x x X x x P x X P
X
x
x x
x x
X
x x
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
As a consequence, the following probabilities are equivalent for continuous random variables:
In general, the range of values of a continuous random variable encompasses all real numbers,
i.e., - < x < . Therefore, the probability density function, f
X
(x), must satisfy the condition
that
The cumulative distribution function (cdf) for a continuous random variable is defined by


<
x
X X
d f x X P x X P x F ) ( ) ( ) ( ) (
If we plot the pdf, f
X
(x), we can illustrate the value of the cdf F
X
(x) as the area under the curve
f
X
(x) within the limits - and x:
In terms of the cdf we can write the following probability formulas:
The standard normal distribution
Consider for example, the standard normal distribution, whose pdf is given by
f
X
(x) = exp(-z
2
)/(2)
1/2
, - < x <.
A plot of the distribution follows:
-->x=[-3:0.1:3]; y=exp(-x.^2/2)./sqrt(2.*%pi);
-->plot(x,y,'X','fX(x)','normal distribution')
. 0 . 1 ) (


dx x f
X
. ) ( ) ( ) ( ) ( ) (
2
1
2 1 2 1 2 1 2 1

< < < <
x
x
X
dx x f x X x P x X x P x X x P x X x P
). ( ) ( ) ( ) ( ) ( ) (
1 2 2 1
2 2 2
1
x F x F dx x f dx x f dx x f x X x P
X X
x
X
x
X
x
x
X
< <


). ( 1 ) ( ) ( 1 ) ( ) (
2
x F dx x f x X P dx x f x X P
X
x
X
x
X
< >

Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz


Note: In most textbooks, the standard normal variate is referred to as Z, with its pdf defined
as
(z) = exp(-z
2
)/(2)
1/2
,
and the corresponding CDF is referred to as (z).
Function cdfnor
Function cdfnor stands for cumulative distribution function for the normal distribution. A
call to function cdfnor may involve three or more parameters which we will identify as follows:
p = a probability representing the normal cdf, i.e., P = P(X<x)
q = 1-p = the probability of the complement, i.e., q = P(X>x)
x = the value of X in the previous two expressions
mu = the mean value of the distribution ()
sigma = the standard deviation of the distribution ()
Also, any of these parameters may be returned by the function call based on the values of the
other parameters used as arguments in the call to function cdfnor. The following are the four
possible function calls specifying the arguments to use and the parameters returned:
1. [p,q] = cdfnor(PQ,x,mu,sigma)
2. [x] = cdfnor(X,mu,sigma,p,q)
3. [mu] = cdfnor(Mean,sigma,p,q,x)
4. [sigma] = cdfnor(Std,p,q,x,mu)
Function call number 1 returns the probabilities p and q given the value of x,
the mean, mu, and the standard deviation, sigma.
Function call number 2 returns the value of x given the mean, mu, the standard
deviation, sigma, and the probabilities p and q.
Function call number 3 returns the value of the mean, mu, given the standard
deviation, sigma, and the probabilities p and q corresponding to the value of x.
Function call number 4 returns the standard deviation of the distribution,
sigma, given the mean value, mu, and the probabilities p and q corresponding to the
value of x.
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
The string occupying the first position in the list of arguments specifies the type of parameter
requested from the function call. Thus, the strings corresponding to the four different types
of calls to cdfnor are:
PQ - to request probabilities p and q
X - to request a value of the normal variable
Mean - to request the mean of the distribution
Std - to request the standard deviation of the distribution
Care must be exercised when calling the function so that the arguments are in the appropriate
order.
__________________________________________________________________________________
Note: The probability density function, pdf, for a general normal distribution, X, with a mean
value, , and a standard deviation, , is given by
. , 0 ,
2
) (
exp
2
1
) (
2
2
< < >

,
_


x
x
x f
X


The standard normal distribution has mean value = 0 and standard deviation = 1.
__________________________________________________________________________________
Some probability calculations using the standard normal distribution ( = 0, = 1) follow.
First, we define the values of the mean and standard deviation for the standard normal
distribution:
-->mu = 0; sigma = 1.0;
The probability calculations are:
P(X<1.1) = F
X
(1.1) = 0.8643, calculated using
-->cdfnor('PQ',1.1,mu,sigma)
ans =
.8643339
P(X>0.8) = 1 P(X<0.8) = 1 F
X
(0.8) = 0.2118554. This is the value of q calculated
using:
-->[p,q] = cdfnor('PQ',0.8,mu,sigma)
q =
.2118554
p =
.7881446
P(-0.9 < X < 0.6) = P(X<0.6) P(X<-0.9) = 0.5417, calculated using:
-->cdfnor('PQ',0.6,mu,sigma) - cdfnor('PQ',-0.9,mu,sigma)
ans =
.5416868
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
The following probability calculations correspond to a normal distribution with mean = 2.5
and standard deviation = 1.5:
-->mu = 2.5; sigma = 1.5;
The probability P( 1.0 < X < 3.0) = P(X < 3.0) P(X < 1.0) = 0.4719034 is calculated using:
-->cdfnor('PQ',3.0,mu,sigma) - cdfnor('PQ',1.0,mu,sigma)
ans =
.4719034
To determine the mean given P(X<2) = 0.35 with standard deviation = 0.5 use
-->mu = cdfnor("Mean",0.5,0.35,0.65,2)
mu =
2.1926602
To determine the standard deviation P(X<3) = 0.85 with mean = 2.5
-->sigma = cdfnor("Std",0.85,0.15,3,2.5)
sigma =
.4824237
In the previous call to cdfnor we provided as arguments the values of the cumulative
distribution function, p = P(X<x) = F
X
(x), as well as the value of the probability of the
complement, q = P(X>x) = 1 - F
X
(x). By definition, these two probabilities should add to 1.0,
i.e., p + q = 1.0. What would happen if the values of p and q do not add to 1.0:
-->mu = cdfnor("Mean",0.5,0.35,0.50,2)
!--error 999
mu = cdfnor("Mean",0.5,0.35,0.50,2)
To avoid this problem you could simply write p as 0.35 and q as 1-0.35 in the call to function
cdfnor:
-->mu = cdfnor("Mean",0.5,0.35,1-0.35,2)
mu =
2.1926602
The expectation operator
Let X be a discrete random variable with mpf f
X
(x), and let g(X) be a real-valued function of X,
we define the mathematical expectation of the function g(X) as


x all
i X i
x f x g X g E ) ( ) ( )] ( [
For a continuous random variable X with pdf f
X
(x), the expectation of the function g(X) is
defined as
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz

+

dx x f x g X g E
X
) ( ) ( )] ( [
For example, for the discrete random variable X whose pmf is given by the table
x -2 -1 0 1 2
f
X
(x) 0.2 0.3 0.1 0.2 0.2
We can calculate the expectation of the function g(X) = X
2
+X using SCILAB as follows:
-->X = [-2:1:2];
-->fX = [0.3 0.2 0.1 0.2 0.2];
-->g = X.^2+X;
-->EgX = g*fX';
EgX =
2.2
The result is E[g(X)] = E[X
2
+X] = 2.2.
Consider the continuous random variable X whose pdf is given by f
X
(x) = e
x
, > 0, x 0.
This is called the exponential distribution. To obtain the expectation of g(X) = X
2
+X, we need
to calculate an improper definite integral (improper integral are those that have infinite
integration limits), namely,
. ) ( ] [
0
2 2
dx e x x X X E
x
+ +

Such symbolic calculations are not possible in SCILAB, therefore, we need to perform them by
hand or using a symbolic software such as Maple. You can verify that the integral under
consideration results in
E[g(X)] = E[X
2
+X] = (2+)/
2
.
The mean value, variance, and standard deviation of
the distribution
The mean value of a probability distribution is the expectation of X, i.e.,
] [ X E
X
.
The variance of a probability distribution is the expectation of (X-
X
)
2
, i.e.,
Var[X] = E[(X-
X
)
2
] = E[X
2
]-(E[X])
2
= E[X
2
]-
X
2
.
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
The standard deviation of a probability distribution is simply the square root of the variance of
the distribution, i.e.,

X
= (Var[X])
1/2
.
We can define a coefficient of variation the same way we did for statistics of a sample, i.e.,
% 100
X
X
X
CV

Calculation of mean, variance and standard deviation for a discrete random variable
For a discrete probability distribution with pmf f
X
(x) the following formulas apply:


n
i
i X i X
x f x
1
) (
) ( ) ( ] [
2
1
x f x X Var
X
n
i
X i

As an example, we use again the discrete probability distribution represented by the table:
x -2 -1 0 1 2
f
X
(x) 0.2 0.3 0.1 0.2 0.2
We calculate the mean, variance, standard deviation and coefficient of variation using SCILAB:
-->X = [-2:1:2];
-->fX = [0.3 0.2 0.1 0.2 0.2];
-->muX = X*fX'
muX =
- .2
-->VarX = (X-muX).^2*fX'
VarX =
2.36
-->sigmaX = sqrt(VarX)
sigmaX =
1.5362291
-->CVX = (sigmaX/muX)*100
CVX =
- 768.11457
The results are
X
= -0.20, Var[X] = 2.36,
X
= 1.5362, and CV
X
= -768.1146.
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
Calculation of mean, variance and standard deviation for a continuous random variable
For a continuous random variable with pdf f
X
(x)

the following formulas apply:


dx x f x
X X
) (


dx x f x X Var
X X
) ( ) ( ] [
2

For example, the exponential distribution with pdf f


X
(x) = e
x
, > 0, x 0, has the
following values of mean, variance, standard deviation, and coefficient of variation:

X
= 1/, Var(X) = 1/
2
,
X
= 1/, CV
X
= 100%.
If the continuous probability density function under consideration is defined in a finite range, it
is possible to use the numerical integration methods developed or presented in Chapter to
calculate mean values, variances, and probabilities. For example, consider the probability
density function given by
f
X
(x) = k (1-x
2
), 0 < x < 1
The value of k can be determined from the fact that
0
1
f
X
(x) dx = k
0
1
(1-x
2
) dx = 1. To
calculate the integral
0
1
(1-x
2
) dx using SCILAB use the following commands:
-->integrate('1-x^2','x',0,1)
ans =
.6666667
The value of k turns out to be:
-->1/ans
ans = 1.5
A plot of the pdf is obtained by using:
-->deff('[y]=fX(x)','y=1.5*(1-x^2)')
-->xx=[0:0.05:1];yy=fX(xx);plot(xx,yy);
To calculate probabilities, we define the function f(x) and calculate the probabilities using
SCILAB function integrate as indicated below.
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
P(X<0.3)
-->integrate('fX(x)','x',0,0.3)
ans =
.4365
P(X>0.7)
-->integrate('fX(x)','x',0.7,1)
ans =
.1215
P(0.2 < X < 0.6)
-->integrate('fX(x)','x',0.2,0.6)
ans =
.496
The corresponding cumulative distribution function (cdf) is easily calculated by hand, as
.
3
| ]
3
[ ) 1 ( ) (
3
0
3
2
0

,
_

x
x k k d k x F
x
x
X


This function can be plotted using SCILAB:
-->deff('[y]=FX(x)','y=1.5*(x-x^3/3)')
-->YY = FX(xx);plot(xx,YY,'x','FX(x)','CDF')
Moments
The k-th moment of the distribution of a random variable X about the origin (i.e., about zero)
is calculated as
k
= E[X
k
]. The mean value is the first moment about the origin, i.e.,
X
=
1
=
E[X].
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
The k-th moment of the distribution about the mean is calculated as
k
= E[(X-
X
)
k
]. The
variance is the second moment about the mean, i.e., Var[X] =
2
= E[(X-
X
)
2
] = E[X
2
]-(E[X])
2
=
2

1

2
Skewness and kurtosis
The skewness and kurtosis of a probability distribution are defined as
k =
3
/
2
1.5

=
3
/
X
3
and
=
4
/
2
2

=
4
/
X
4
Calculating moments for a discrete distribution
The moments about the origin for a discrete distribution are calculated using
, ) ( '
1


n
i
i X
k
i k
x f x
while the corresponding moments about the mean,
X
=
1
= x
i
f
X
(x
i
), are calculated as
. ) ( ) (
1


n
i
i X
k
X i k
x f x
To illustrate the calculation of these moments we use again the discrete probability mass
function given by the table:
x -2 -1 0 1 2
f
X
(x) 0.2 0.3 0.1 0.2 0.2
Using SCILAB, we first enter the values of X and fX:
-->X = [-2:1:2]; fX = [0.3 0.2 0.1 0.2 0.2];
The following function, named mukp(k), represents
k
:
-->deff('[mu_k_p] = mukp(k)','mu_k_p = sum(X^k.*fX)')
For example, the third moment about the origin,
3
, is calculated with mukp(3), i.e.,
->mukp(3)
ans =
- .8
The fourth moment about the origin for this distribution is:
-->mukp(4)
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
ans =
8.4
The zero-th moment about the origin is by definition
0
= x
i
0
f
X
(x
i
) = f
X
(x
i
) = 1. Checking
with function mukp(0):
-->mukp(0)
ans =
1.
The first moment about the origin is, of course, the mean value of the distribution,
X
=
1
:
-->muX = mukp(1)
ans =
- .2
To calculate the moments about the mean we use function muk(k), defined as:
-->deff('[mu_k] = muk(k)','mu_k = sum((X-mux)^k.*fX)')
The variance of the discrete distribution is the second moment about the mean, i.e., Var(X) =

X
2
=
2
:
-->muk(2)
ans =
2.36
From the definition of the variance, Var(X) = (x
i
-
X
)
2
f
X
(x
i
) = x
i
2
f
X
(x
i
) -
X
2
=
2
- (
1
)
2
.
Checking with SCILAB:
-->mukp(2)-mukp(1)^2
ans =
2.36
The skewness and kurtosis of the discrete probability distribution under consideration are
calculated from the definition of those parameters, i.e., skewness: k =
3
/
2
1.5
-->sigmak = muk(3)/muk(2)^1.5
sigmak =
.1721142
And, kurtosis: =
4
/
2
2
-->kappa = muk(4)/muk(2)^2
kappa =
1.4958345
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
Calculating moments for a continuous distribution
The moments about the origin for a continuous distribution are calculated using
, ) ( ' dx x f x
X
k
k

while the corresponding moments about the mean,


X
=
1
= x
i
f
X
(x
i
), are calculated as
. ) ( ) (


dx x f x
X
k
X k

When the continuous pdf is defined in terms of symbolic parameters, e.g., the exponential
distribution, f
X
(x) = e
-x
, x>0, the moments need to be calculated by hand or using a symbolic
environment (e.g., Maple).
For continuous pdf given by specific functions in finite intervals, moments can be calculated
using the numerical integration functions developed or presented in Chapter . As an
example, for the pdf given by f
X
(x)=1.5(1-x
2
), 0<x<1, we can calculate the first five moments
about the origin using SCILAB as follows:
-->mukp0 = integrate('x^0*fX(x)','x',0,1)
mukp0 =
1.
-->mukp1 = integrate('x^1*fX(x)','x',0,1)
mukp1 =
.375
-->mukp2 = integrate('x^2*fX(x)','x',0,1)
mukp2 =
.2
-->mukp3 = integrate('x^3*fX(x)','x',0,1)
mukp3 =
.125
-->mukp4 = integrate('x^4*fX(x)','x',0,1)
mukp4 =
.0857143
The mean value is the first moment about the origin, thus
X
= 0.375:
-->muX = mukp1
muX =
.375
The first five moments about the mean for the pdf under consideration are calculated using
SCILAB as shown next:
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
-->muk0 = integrate('(x-muX)^0*fX(x)','x',0,1)
muk0 =
1.
-->muk1 = integrate('(x-muX)^1*fX(x)','x',0,1)
!--error 24
convergence problem...
at line 26 of function integrate called by :
muk1 = integrate('(x-muX)^1*fX(x)','x',0,1)
-->muk2 = integrate('(x-muX)^2*fX(x)','x',0,1)
muk2 =
.059375
-->muk3 = integrate('(x-muX)^3*fX(x)','x',0,1)
muk3 =
.0054688
-->muk4 = integrate('(x-muX)^4*fX(x)','x',0,1)
muk4 =
.0076381
Notice that there is a conflict in calculating the first moment. By definition, the first moment
is equal to zero and function integrate has difficulties with its convergence check to obtain this
result.
The skewness and kurtosis for this pdf are calculated as:
-->sigmaX = sqrt(muk2)
sigmaX =
.2436699
-->sigmak = muk3/sigmaX^3
sigmak =
.3779920
-->kappa = muk4/sigmaX^4
kappa =
2.1666007
Multivariate probability distributions
Multivariate probability distributions are functions representing probabilities that depend on
more than one random variable. The simplest case is, of course, the so-called bi-variate
probability distributions, i.e., probability distributions that depend on two variables only.
Discrete random variables
If X and Y are two discrete random variables, we can define a joint probability mass function,
f
XY
(x,y), representing the probability that X = x and Y = y, i.e., P[(X=x)(Y=y)] = f
XY
(x,y). The
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
reason why the pmf is referred to as joint is because such function represents the
intersection of two events, events (X=x) and (Y=y). An example of a joint probability
distribution for two discrete variable is the table shown below. Variable X can take values of
0, 1, 2 and 3, while variable Y takes values of 0, 1, and 2. The entries in the table represent
values of f
XY
(x,y).
f
XY
(x,y) Y =0 Y =1 Y =2
X =0 0.05 0.20 0.01
X =1 0.05 0.10 0.05
X =2 0.02 0.05 0.02
X =3 0.15 0.10 0.20
The joint probability mass function, f
XY
(x,y), satisfies

j
f
XY
(x
i
,y
j
) = 1.0.
Marginal probability mass functions
Using the theorem of total probability we can calculate probabilities such as
P(X=0) = P[(X=0)(Y=0)] + P[(X=0)(Y=1)] + P[(X=0)(Y=2)],
which we can write as
f
X
(0) = f
XY
(0,0) + f
XY
(0,1) + f
XY
(0,2) =
y
f
XY
(0,y) = 0.05 + 0.20 + 0.01 = 0.26.
A complete table of f
X
(x) for x = 0, 1, 2, and 3, is referred to as the marginal probability mass
function of X, i.e.,


j
y all
XY X
y x f x f x X P ). , ( ) ( ) (
Similarly, a marginal probability mass function for Y is defined as:


i
x all
XY Y
y x f y f y Y P ). , ( ) ( ) (
The reason why these single-variable probability mass functions are referred to as marginal is
because the corresponding probabilities can be obtained by adding rows and columns of the
table representing the joint probability mass function. In other words, the values of the
probability mass functions f
X
(x) and f
Y
(y) will appear in the margins of the table. This is
illustrated in the following version of the table:
f
XY
(x,y) Y = 0 Y = 1 Y = 2 f
X
(x)
X = 0 0.05 0.20 0.01 0.26
X = 1 0.05 0.10 0.05 0.20
X = 2 0.02 0.05 0.02 0.09
X = 3 0.15 0.10 0.20 0.45
f
Y
(y) 0.27 0.45 0.28 1.00
Thus, the marginal probability mass function for X corresponds to the first and last columns of
the expanded table, while the marginal probability mass function for Y corresponds to the first
and last rows of the expanded table. The marginal probability mass functions for X and Y are
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
univariate probability distributions with the properties that
i
f
X
(x
i
) = 1.0 and
i
f
Y
(y
j
) = 1.0.
These results are highlighted in the shaded cell of the expanded table shown above.
The table below illustrates the construction of the marginal probability mass functions for both
X and Y out of the original table:
f
XY
(x,y) Y = 0 Y = 1 Y = 2 f
X
(x) X f
X
(x)
X = 0 0.05 0.20 0.01 0.26 0 0.26
X = 1 0.05 0.10 0.05 0.20 1 0.20
X = 2 0.02 0.05 0.02 0.09 2 0.09
X = 3 0.15 0.10 0.20 0.45 3 0.45
f
Y
(y) 0.27 0.45 0.28 1.00
Y 0 1 2
f
Y
(y) 0.27 0.45 0.28
Joint cumulative distribution function
The joint cumulative distribution function, F
XY
(x,y), corresponding to a joint probability mass
function, f
XY
(x,y), is defined as



x x all y y all
XY XY
i j
y x f y Y x X P y x F ). , ( )] ( ) [( ) , (
For the joint probability mass function defined in the table shown above, we can construct a
table corresponding to the joint cumulative distribution function as shown below:
F
XY
(x,y) Y = 0 Y = 1 Y = 2 X F
X
(x)
X = 0 0.05 0.25 0.26 0 0.26
X = 1 0.10 0.40 0.46 1 0.46
X = 2 0.12 0.47 0.55 2 0.55
X = 3 0.27 0.72 1.00 3 1.00
Y 0 1 2
F
Y
(y) 0.27 0.72 1.00
The table also shows the marginal cumulative distribution functions for X and Y.
Working with tables of discrete bi-variate distributions in SCILAB
Due to the matricial nature of the SCILAB data structures, it is relatively simple to store a bi-
variate probability mass function in SCILAB. We illustrate the handling of these tables using
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
the table for the joint bi-variate probability mass function given earlier. To store the X, Y,
and f
XY
(x,y) data use:
-->X = [0:3]; Y = [0:2];
-->fXY=[0.05,0.20,0.01;0.05,0.10,0.05;0.02,0.05,0.02;0.15,0.10,0.20]
fXY =
! .05 .2 .01 !
! .05 .1 .05 !
! .02 .05 .02 !
! .15 .1 .2 !
A three-dimensional plot of this joint probability mass function can be obtained by using the
function hist3d (histogram in 3 dimensions) with value fXY(i,j) representing a relative
frequency in the three-dimensional histogram. Use:
-->hist3d(fXY)
The marginal distribution for X, f
X
(x), is obtained with:
-->fX = sum(fXY,'c')
fX =
! .26 !
! .2 !
! .09 !
! .45 !
The marginal distribution for Y, f
Y
(y), results from:
-->fY = sum(fXY,'r')
fY =
! .27 .45 .28 !
To obtain the joint cumulative distribution function we use:
-->[n m] = size(fXY)
m =
3.
n =
4.
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
-->FXY = zeros(n,m);
-->for i = 1:n
--> for j = 1:m
--> FXY(i,j) = sum(fXY(1:i,1:j));
--> end;
-->end;
-->FXY
FXY =
! .05 .25 .26 !
! .1 .4 .46 !
! .12 .47 .55 !
! .27 .72 1. !
A three-dimensional histogram representing the joint cumulative distribution function of this
example is produced in SCILAB through the use of function hist3d:
-->hist3d(FXY)
The three-dimensional histogram is shown below after rotating the axes:
Covariance and correlation coefficient
The covariance of a bi-variate probability distribution is a quantity that measures the joint
variability of the two variables. The covariance, in general, is defined as the following
expectation:
Cov(X,Y) = E[(X-
X
)(Y-
Y
)].
For discrete random variables X and Y, the covariance is calculated as follows:


i j
x all y all
XY Y j X i
y x f y x Y X Cov ) , ( ) ( ) ( ) , (
Using the vectors X, Y, and the matrix fXY already loaded in SCILAB, we can calculate the co-
variance of X and Y as follows:
-->CovXY = 0.0
CovXY =
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
0.
-->for i = 1:n
--> for j = 1:m
--> CovXY = CovXY + (X(i)-mux)*(Y(j)-muy)*fXY(i,j);
--> end;
-->end;
-->CovXY
CovXY =
.1327
The correlation coefficient is another measure of the joint variability of X and Y, and it is
defined, in general, as
,
) , (
Y X
XY
Y X Cov

where Cov(X,Y) is the covariance and


X
and
Y
are the standard deviations of X and Y,
respectively.
Correlation coefficient for a discrete bi-variate distribution
Using the bi-variate distribution that we stored in SCILAB, the following sequence of SCILAB
commands will produce the correlation coefficient for the distribution:
-->mux = X*fX //Mean of X
mux =
1.73
-->muy = Y*fY' //Mean of y
muy =
1.01
-->VarX = (X-mux)^2*fX //Variance of X
VarX =
1.6171
-->VarY = (Y-muy)^2*fY' //Variance of Y
VarY =
.5499
-->sigmax = sqrt(VarX) //Standard deviation of X
sigmax =
1.2716525
-->sigmay = sqrt(VarY) //Standard deviation of Y
sigmay =
.7415524
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Using the covariance contained in variable CovXY, the correlation coefficient is:
-->rhoXY = CovXY/(sigmax*sigmay)
rhoXY =
.1407216
The correlation coefficient of a bi-variate probability distribution, like its counterpart for a
sample of values (x
i
,y
i
), is limited to the interval [-1,1].
Continuous random variables
Limiting the discussion to the bi-variate case, we can define the joint probability density
function, f
XY
(x,y), of two continuous random variables, X and Y, as a continuous function having
the properties:
0< f
XY
(x,y),
. 1 ) , (


dx dy y x f
XY
and
. ) , ( )] ( ) [(
2
1
2
1
2 1 2 1
dx dy y x f y Y y x X x P
x
x
y
y
XY
< < < <

As an example, consider the joint probability density function given by the function

'

elsewhere
y x xy
y x f
XY
, 0
2 0 , 2 0 ,
4
1
) , (
Notice that the function is non-zero only in the rectangle defined by R = {0 x 2, 0 y 2}.
A plot of the distribution is produced by using function fplot3d in SCILAB evaluating the
function f
XY
(x,y) in that region of the x-y plane:
-->deff('[z]=fXY(x,y)','z=1/4*x*y')
-->x = 0:0.1:2; y = x; fplot3d(x,y,fXY)
We can check that the joint probability density function just defined satisfies the properties
listed above. For example,
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
. 0 . 1 |
4
1
2
1
|
2
1
4
1
4
1
) , (
2
0
2
2
0
2
0
2
0
2
2
0
2
0
2
0
2
0


x dx x dx xy dx dy xy dx dy y x f
XY
Joint cumulative distribution function
The joint cumulative distribution function F
XY
(x,y) corresponding to the cumulative density
function f
XY
(x,y) for two continuous random variables X and Y is defined as
. ) , ( )] ( ) [( ) , ( d d f y Y x X P y x F
x y
XY XY
< <


For the example of joint pdf defined above, the corresponding joint cumulative distribution
function is given by
.
16
1
4
1
)] ( ) [( ) , (
2 2
0 0
y x d d y Y x X P y x F
x y
XY
< <


The proper expression for the joint cumulative distribution is:

'

elsewhere
y x y x
y x F
XY
, 0
2 0 , 2 0 ,
16
1
) , (
2 2
A plot of the joint cumulative distribution function on the region R = {0 x 2, 0 y 2} is
shown in the figure below:
-->deff('[z]=FXY(x,y)','z=1/16*x^2*y^2')
-->x = 0:0.1:2; y = x; fplot3d(x,y,FXY)
Covariance and correlation coefficient
The covariance for a bi-variate continuous distribution is defined as
. ) , ( ) )( ( )] )( [( ) , (


dydx y x f y x Y X E Y X Cov
XY Y X Y X

Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
The correlation coefficient for a bi-variate continuous distribution is defined as that of a bi-
variate discrete distribution, i.e.,
,
) , (
Y X
XY
Y X Cov


with the mean values and standard deviations of X and Y defined in terms of integrals of the
marginal probability densities, i.e.,



dy y f y dx x f x
Y Y X X
) ( , ) (
. ) ( ) ( , ) ( ) (
2 2



dy y f y dx x f x
Y Y Y X X X

SCILAB calculations for bi-variate continuous distributions
We can use the functions for numerical integration developed or presented in Chapter to
calculate parameters or probabilities for specific bi-variate distributions. For example, using
the bi-variate, joint probability density function, f
XY
(x,y), given earlier, namely,

'

elsewhere
y x xy
y x f
XY
, 0
2 0 , 2 0 ,
4
1
) , (
and the corresponding marginal probability density functions, we can calculate parameters
such as the mean values and standard deviations, the co-variance of X and Y, and the
correlation coefficient. Since the joint probability density function is defined within finite
limits, double integrals related to this joint pdf are easily obtained by using the user-defined
function DoubleIntegral first introduced in Chapter
-->getf('DoubleIntegral')
-->deff('[z]=fXY(x,y)','z=x*y/4')
First, we calculate a few probabilities using function DoubleIntegral. We use 20 sub-intervals
in each of the coordinate directions x and y:
P(X<1,Y<1.5)
-->DoubleIntegral(0,1,20,0,1.5,20,fXY)
ans =
.140625
This probability is, by definition, the cumulative probability distribution, F
XY
(x,y) =
x
2
y
2
/16:
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
-->1/16*1^2*1.5^2
ans =
.140625
P(X>0.5,Y<1.2)
-->DoubleIntegral(0.5,2,20,0,1.2,20,fXY)
ans =
.3375
P(X>1, Y<0.5)
-->DoubleIntegral(1,2,20,0,0.5,20,fXY)
ans =
.046875
P(X<1.2,Y>0.7)
-->DoubleIntegral(0,1.2,20,0.7,2,20,fxy)
ans =
.3159
Next, we need to obtain the marginal pdfs by symbolic integration, thus,
,
2
1
|
8
1
4
1
) ( ,
2
1
|
8
1
4
1
) (
2
0
2
0
2
2
0
2
0
2
y y x xydx y f x xy xydy x f
x
x Y
y
y X


and calculate the mean values and standard deviations of each marginal distribution using
SCILAB function integrate as follows:
-->deff('[p]=fX(x)','p=x/2'); deff('[p]=fY(y)','p=y/2');
-->muX = integrate('x*fX(x)','x',0,2)
muX =
1.3333333
-->muY = integrate('y*fY(y)','y',0,2)
muY =
1.3333333
-->VarX = integrate('(x-muX)^2*fX(x)','x',0,2)
VarX =
.2222222
-->VarY = integrate('(y-muY)^2*fY(y)','y',0,2)
VarY =
.2222222
-->sigmaX = sqrt(VarX), sigmaY = sqrt(VarY)
sigmaX =
.4714045
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
sigmaY =
.4714045
Next, we use user-defined function DoubleIntegral to calculate the covariance of X and Y:
-->deff('[p]=g(x,y)','p = (x-muX)*(y-muY)*fXY(x,y)')
-->CovXY = DoubleIntegral(0,2,20,0,2,20,g)
CovXY =
.0000028
Finally, the correlation coefficient is calculated as:
-->rhoXY=CovXY/(sigmaX*sigmaY)
rhoXY =
.0000125
Exercises
[1]. The following table represents the probability mass function for a discrete random variable
X.
x 0 1 2 3 4 5
f
X
(x) 0.06 0.17 0.22 0.28 0.17 0.1
(a) Verify that the table represents indeed a probability mass function.
(b) Plot the probability mass function.
(c) Obtain and plot the cumulative distribution function for X.
(d) Determine the mean, variance, and standard deviation of X.
(e) Determine the skewness and kurtosis of the probability mass function.
(f) Determine the expectation E[g(X)] for the function g(X) = X
2
+X.
[2]. The following table represents the probability mass function for a discrete random variable
X.
x 0.5 0.7 0.9 1.1 1.4 1.7 2.1 2.4 2.7
f
X
(x) 0.05 0.16 0.21 0.25 0.15 0.1 0.05 0.02 0.01
(a) Verify that the table represents indeed a probability mass function.
(b) Plot the probability mass function.
(c) Obtain and plot the cumulative distribution function for X.
(d) Determine the mean, variance, and standard deviation of X.
(e) Determine the skewness and kurtosis of the probability mass function.
(f) Determine the expectation E[g(X)] for the function g(X) = ln(X).
[3]. Let X represent the number of hits that an engineering internet site receives per hour. X
follows the Poisson distribution with parameter = 1800. Determine the following
probabilities:
(a) P(X > 1000) (b) P(X 1500) (c) P(X < 2000) (d) P(X 1500)
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
[4]. If X is a discrete random variable that follows the Poisson distribution with parameter
and P(X 10) = 0.80, determine the value of .
[5]. If X is a discrete random variable that follows the Poisson distribution with parameter =
12.5, and P(X x) = 0.65, determine the value of x.
[6]. If X is a discrete random variable that follows the Poisson distribution with parameter
and P(X > 5) = 0.20, determine the value of .
[7]. If X is a discrete random variable that follows the Poisson distribution with parameter =
8.2, and P(X > x) = 0.85, determine the value of x.
[8]. If X is a discrete random variable that follows the Poisson distribution with parameter
and P(X < 15) = 0.60, determine the value of .
[9]. If X is a discrete random variable that follows the Poisson distribution with parameter =
25.8, and P(X < x) = 0.35, determine the value of x.
[10]. If X is a discrete random variable that follows the Poisson distribution with parameter
and P(X 25) = 0.80, determine the value of .
[11]. If X is a discrete random variable that follows the Poisson distribution with parameter =
82, and P(X x) = 0.25, determine the value of x.
[12]. The function f
X
(x) shown below represents a probability density function

'

elsewhere
x x
x f
X
, 0
1 0 ), 1 ( 3
) (
(a) Verify that f
X
(x) represents indeed a probability density function.
(b) Plot the probability density function.
(c) Obtain and plot the cumulative distribution function for X.
(d) Determine the mean, variance, and standard deviation of X.
(e) Determine the skewness and kurtosis of the probability mass function.
[13]. The function f
X
(x) shown below represents a probability density function

'

elsewhere
x x
x f
X
, 0
0 ), 2 / exp( ) 2 / 1 (
) (
(a) Verify that f
X
(x) represents indeed a probability density function.
(b) Plot the probability density function.
(c) Obtain and plot the cumulative distribution function for X.
(d) Determine the mean, variance, and standard deviation of X.
(e) Determine the skewness and kurtosis of the probability mass function.
[14]. The distribution of the measurement of experimental data follows the normal
distribution. Suppose that X represents measurements of the acceleration of gravity at a
specific point on Earth. Thus, X ~ N( = 9.806 m/s
2
, = 0.005 m/s
2
). Determine the following
probabilities:
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
(a) P(X<9.85) (b) P(X>9.78) (c) P(9.80 < X < 9.81)
[15]. Let X be a continuous random variable that follows the normal distribution with =
120.6, = 40. Determine the value of x given the following probability conditions:
(a) P(X<x) = 0.45 (b) P(X>x) = 0.25
(c) P(80 < X < x) = 0.20 (d) P(-x<X<x) = 0.60
[16]. Let X be a continuous random variable that follows the normal distribution. Given the
following probabilities, determine the mean or standard deviation as requested:
(a) P(X<2.5) = 0.8, = 0.3, determine
(b) P(X>33.5) = 0.6, = 8.5, determine
(c) P(X<122) = 0.45, = 12.22, determine
(d) P(X>33.5) = 0.3, = 83.15, determine
[17]. The table below shows a bivariate probability mass function for two discrete variables X
and Y:
f
XY
(x,y) Y =1 Y =2 Y =3 Y =4
X =0.5 0.05 0.03 0.01 0.01
X =1.5 0.01 0.15 0.1 0.03
X =2.5 0.08 0.12 0.18 0.05
X =3.5 0.03 0.08 0.05 0.02
(a) Verify that the table indeed represents a bivariate probability mass function.
(b) Plot the probability mass function f
XY
(x,y) as a three-dimensional histogram using
function hist3d.
(c) Produce tables of the marginal probability mass functions f
X
(x) and f
Y
(y).
(d) Determine the mean value, variance, and standard deviation for X.
(e) Determine the mean value, variance, and standard deviation for Y.
(f) Produce a table for the cumulative distribution function F
XY
(x,y), and plot this function
using a three-dimensional histogram.
(g) Determine the covariance and correlation coefficient for variables X and Y.
[18]. The table below shows a bivariate probability mass function for two discrete variables X
and Y:
f
XY
(x,y) Y =0.1 Y =0.2 Y =0.3 Y =0.4 Y =0.5
X =0.5 0.05 0.03 0.01 0.01 0.00
X =1.5 0.01 0.11 0.09 0.03 0.03
X =2.5 0.05 0.07 0.10 0.04 0.05
X =3.5 0.03 0.06 0.05 0.02 0.02
X =4.5 0.03 0.05 0.04 0.02 0.00
(a) Verify that the table indeed represents a bivariate probability mass function.
(b) Plot the probability mass function f
XY
(x,y) as a three-dimensional histogram using
function hist3d.
(c) Produce tables of the marginal probability mass functions f
X
(x) and f
Y
(y).
(d) Determine the mean value, variance, and standard deviation for X.
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
(e) Determine the mean value, variance, and standard deviation for Y.
(f) Produce a table for the cumulative distribution function F
XY
(x,y), and plot this function
using a three-dimensional histogram.
(g) Determine the covariance and correlation coefficient for variables X and Y.
[19]. The following function represents the joint probability density function of two continuous
random variables X and Y:

'

elsewhere
y x y x
y x f
XY
, 0
1 0 , 1 0 ), 1 )( 1 ( 4
) , (
(a) Verify that the table indeed represents a bivariate probability mass function.
(b) Plot the probability density function f
XY
(x,y) as a three-dimensional surface using
function plot3d.
(c) Obtain the marginal probability density functions f
X
(x) and f
Y
(y).
(d) Determine the mean value, variance, and standard deviation for X.
(e) Determine the mean value, variance, and standard deviation for Y.
(f) Obtain the cumulative distribution function F
XY
(x,y), and plot this function using a
three-dimensional surface.
(g) Determine the covariance and correlation coefficient for variables X and Y.
[20]. The following function represents the joint probability density function of two continuous
random variables X and Y:

'

elsewhere
y x
y x
y x f
XY
, 0
0 , 0 ),
2
exp(
4
1
) , (
(a) Verify that the table indeed represents a bivariate probability mass function.
(b) Plot the probability density function f
XY
(x,y) as a three-dimensional surface using
function plot3d.
(c) Obtain the marginal probability density functions f
X
(x) and f
Y
(y).
(d) Determine the mean value, variance, and standard deviation for X.
(e) Determine the mean value, variance, and standard deviation for Y.
(f) Obtain the cumulative distribution function F
XY
(x,y), and plot this function using a
three-dimensional surface.
(g) Determine the covariance and correlation coefficient for variables X and Y.
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Probability Distributions with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
PROBABILITY DISTRIBUTIONS 3
Discrete probability distributions 3
Bernoulli probability distribution 3
Binomial probability distribution 4
Poisson probability distribution: 5
Geometric probability distribution: 6
Hypergeometric probability mass function 7
Cumulative distribution functions for discrete probability distributions 9
SCILAB functions for discrete cumulative distribution functions 9
SCILAB function cdfbin 9
Discrete probability calculations through user-defined functions 10
Combinations 11
Binomial distribution 11
Poisson distribution 12
Geometric distribution 13
Hypergeometric distribution 14
Continuous probability functions 15
Factorials and the Gamma function 15
The gamma distribution 16
The exponential distribution 17
The beta distribution 17
The Weibull distribution 19
The uniform distribution 19
User-defined functions for continuous probability distributions 20
Continuous probability distributions used in statistical inference 25
The Normal distribution 25
The Student-t distribution 25
The Chi-squared (
2
) distribution 27
The F distribution 28
Applications of the normal distribution in data analysis 30
Plotting a histogram and its corresponding normal curve 31
Plotting data against their normal scores 34
The lognormal distribution 36
Generating synthetic data 38
Generating normally-distributed synthetic data 38
Additional applications of function rand 39
SCILAB function for generating synthetic data 40
Examples of synthetic data generation using function grand 41
Additional notes on function grand 49
Pseudo-random generators 50
Generating log-normally-distributed data 51
Generating data that follows the Weibull distribution 52
Generating data that follows the Students t distribution 53
Generating data that follows a discrete distribution 54
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Statistical simulation 56
Simulating traffic through a service station 57
An user-defined function to simulate traffic through a service station 58
Modeling traffic through a service station with random input 60
STIXBOX: a rudimentary statistics toolbox 63
Exercises 72
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Probability Distributions
There are a number of mathematical functions that possess the properties of a probability mass
function for discrete random variables or the properties of a probability density function for
continuous random variables. In this section we introduce a number of those functions for the
calculation of probabilities. Because these probability distributions depend on a finite number
of parameters they are typically referred to as parametric distributions.
Discrete probability distributions
Some of the most useful discrete probability distributions are the Bernoulli, Binomial, Poisson,
geometric, and hypergeometric distributions. The definitions of the corresponding probability
mass and distribution functions are shown below. We also present expressions for the mean,
variance, and standard deviation of these distributions.
Bernoulli probability distribution
The Bernoulli probability distribution applies to a discrete random variable that can only have
values of 0 or 1, i.e., X = 0, 1. Let the probability of X = 1 be p, i.e., f
X
(1) = p, then f
X
(0) = 1-p.
This can be summarized as
f
X
(x) = p
x
(1-p)
1-x
, x = 0,1
The mean value of the distribution is

X
= 0 (1-p) + 1 p = p.
The expectation of X
2
, E(X
2
), is needed to calculate the variance Var(X) = E(X
2
)-
X
2
. For the
Bernoulli distribution,
E(X
2
) = 0
2
(1-p) + 1
2
p = p,
and
Var(X) = E(X
2
)-
X
2
= p-p
2
= p(1-p).
Thus, the standard deviation is

X
= [p(1-p)]
1/2
.
These results can be obtained using SCILAB as follows:
-->p=poly(0,'p')
p =
p
-->X = [0,1]
X =
! 0. 1. !
-->Prob = [1-p p]
Prob =
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
! 1 - p p !
-->muX = X*Prob'
muX = p
-->EX2 = X^2*Prob'
EX2 = p
-->VarX = EX2 - muX^2
VarX =
2
p - p
The Bernoulli distribution applies to a simple binary experiments in which only two possible
outcomes exist: 1 or 0, yes or no, success or failure. The value of the probability of success,
p, can be obtained, for example, from the classical or from the frequency definitions of
probability. Bernoulli processes constitute the base of the binomial and geometric
distributions presented below.
Binomial probability distribution
If a Bernoulli experiment with success probability p is repeated n times, the probability of
having x successes out of the n trials is given by
1 0 , ,..., 2 , 1 , 0 , ) 1 (
) 1 ( ) 1 (
) 1 (
) 1 ( ) ( < <
+ +
+

,
_


p n x p p
r n r
n
p p
x
n
x f
x n x x n x
X
with

X
= np, Var(X) = np(1-p), and
x
= [np(1-p)]
1/2
.
In SCILAB, we can define the probability mass function for the Binomial distribution as
-->deff('[f]=fX(x,n,p)',
-->'f=gamma(n+1).*p.^x.*(1-p).^(n-x)./(gamma(x+1).*gamma(n-x+1))')
Next, we use this function to produce a plot of the probability mass function for n = 10, p =
0.10:
-->n=10; p=0.10; xx=[0:1:10]; yy = fX(xx);
-->xset('window',1);xset('mark',-9,2); plot2d(xx',yy',-9)
-->xtitle('Binomial pmf','x','fX(x)')
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
The following commands produce a plot of the cumulative distribution function:
-->yyy = [];for j = 1:n+1, yyy = [yyy sum(yy(1:j))]; end;
-->xset('window',2); xset('mark',-9,2); plot2d(xx',yyy',-9)
-->xtitle('Binomial cdf','x','FX(x)')
Poisson probability distribution:
If X is a Binomial variable with n and p 0, we calculate the parameter = np, and define
the Poisson probability mass function as
. 0 ; ,..., 2 , 1 , 0 ,
!
) ( >

x
x
e
x f
x
X
The Poisson pmf can be used to model the number of occurrences of a certain event in a given
time period or per unit length, area or volume, if represents the mean occurrence of the
even per unit time, length, area or volume, respectively.
The Poisson distribution has the parameters

X
= , Var(X) =
2
, and
x
= .
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
In SCILAB we can define the Poisson distribution pmf as:
-->deff('[p]=fX(x,lambda)','p=exp(-lambda).*lambda.^x./gamma(x+1)')
A plot of the pmf for = 2.5 for values of x between 0 and 20:
-->lambda = 2.5; xx = [0:1:20]; yy =fX(xx,lambda);
-->xset('window',1);xset('mark',-9,2);plot2d(xx',yy',-9)
-->xset('Poisson pmf','x','fX(x)')
A plot of the corresponding cumulative distribution function follows:
-->yyy = []; for j = 1:21, yyy = [yyy sum(yy(1:j))]; end;
-->xset('window',2); xset('mark',-9,2); plot2d(xx',yyy',-9)
-->xset('window',2); xset('mark',-9,2); plot2d(xx',yyy',9)
-->xtitle('Poisson cdf','x','FX(x)')
Geometric probability distribution:
Suppose that we have a Bernoulli experiment with probability of success p being repeated until
a successful outcome occurs. Let X represent the number of repetitions before a success, then
X can be modeled with the geometric pmf:
f
X
(x) = p(1-p)
x-1
, x = 1, 2, ,; 0<p<1.
The Poisson distribution has the parameters

X
= 1/p, Var(X) = (1-p)/p
2
, and
x
= (1-p)
1/2
/p.
The pmf for the geometric distribution and a plot of it is obtained in SCILAB by using:
-->deff('[f]=fX(p,x)','f=p*(1-p)^(x-1)')
-->p = 0.25; xx = [0:1:20]; yy = fX(p,xx);
-->xset('window',1);xset('mark',-9,2);plot2d(xx',yy',-9)
-->xset('window',1);xset('mark',-9,2);plot2d(xx',yy',-9)
-->xtitle('geometric pmf','x','fX(x)')
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
A plot of the geometric distribution CDF is shown next:
-->yyy = [];for j = 1:21, yyy = [yyy sum(yy(1:j))]; end;
-->xset('window',2); xset('mark',-9,2); plot2d(xx',yyy',-9)
-->xtitle('geometric cdf','x','FX(x)')
Hypergeometric probability mass function
Suppose that we have a finite population of N elements, out of which a < N elements are
defective. Suppose also that we take a sample of size n < N out of the population, and let X
represent the number of defective elements in the sample of size n. The probability of X is
given by the following pmf:
. ,..., 1 , 0 , 0 , 0 , ) , , , ( n x N a N n
n
N
x n
a N
x
a
N a n x f
X
< < < <

,
_

,
_

,
_

Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz


Parameters of the distribution are:

X
= na/N, Var(X) = na(N-a)(N-n)/(N
2
(N-1)).
To produce plots of the hypergeometric probability mass function and cumulative distribution
function, we first define a function accounting for the binomial coefficient:
-->deff('[CC]=C(n,r)','CC=gamma(n+1)./(gamma(r+1).*gamma(n-r+1))')
This function is incorporated in the definition of the hypergeometric function:
-->deff('[p]=fX(x)','p=C(a,x).*C(N-a,n-x)./C(N,n)')
Next, we produce plots of the hypergeometric pmf and CDF for N = 100, a = 25, and n = 20:
-->N=100;a=25;n=20;
-->xx=[0:1:20];yy=fX(xx);
-->xset('window',1);xset('mark',-9,2);
-->plot2d(xx',yy',-9);xtitle('Hypergeometric distribution','x','fX(x)');
-->yyy=[];for j=1:21, yyy=[yyy sum(yy(1:j))]; end;
-->xset('window',2);xset('mark',-9,2);
-->plot2d(xx',yyy',-9);xtitle('Hypergeometric distribution','x','FX(x)');
-->plot2d(xx',yyy',9)
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
Cumulative distribution functions for discrete
probability distributions
Out of the five probability distributions presented above, namely, Bernoulli, Binomial, Poisson,
geometric, and hypergeometric, three of them represent finite populations of discrete values
(Bernoulli, Binomial, hypergeometric) and two representing infinite populations (Poisson and
geometric). For the Binomial, Poisson, geometric, and hypergeometric functions, the
cumulative distribution function is calculated using
, ) ( ) (
0

x
k
X X
k f x F
where f
X
(x) represents the corresponding probability mass functions. (This is the definition
used to produce the CDF graphics shown in the previous examples). The cumulative
distribution function F
X
(x) is defined in the same range of values of the discrete random
variable X.
For the geometric distribution, whose domain starts at x = 1, the corresponding expression is
,... 3 , 2 , 1 , ) 1 ( ) ( ) (
1
1
1

x p p k f x F
x
k
k
x
k
X X
SCILAB functions for discrete cumulative distribution functions
SCILAB provides a number of functions for operations with cumulative distribution functions.
For discrete distributions the following functions are provided:
cdfbin - Binomial distribution
cdfnbn - Negative binomial distribution
cdfpoi - Poisson distribution (described in detail in Chapter )
Information on these functions can be obtained by using the help function. Next, we describe
the use of function cdfbin.
SCILAB function cdfbin
There four different forms of the call to function cdfbin:
[P,Q]=cdfbin("PQ",S,Xn,Pr,Ompr)
[S]=cdfbin("S",Xn,Pr,Ompr,P,Q)
[Xn]=cdfbin("Xn",Pr,Ompr,P,Q,S)
[Pr,Ompr]=cdfbin("PrOmpr",P,Q,S,Xn)
The variable Pr in these calls represents the probability of success on any given trial that we
refer to as p in the definition of the Bernoulli pmf shown earlier. On the other hand, OmPr
represents 1-Pr (in some references this is referred to as q = 1 - p), i.e., the probability of
failure in a given trial. The variable P represents the probability P(XS), where X ~
Binomial(Xn,Pr), while Q = 1 - P.
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
The first argument in the calls to function cdfbin is a string that determines which variable is
being sought, according to:
PQ -calculate probabilities, P = P(XS) and Q = 1 - P
S -calculate the inverse CDF, i.e., calculate S from P = P(XS)
Xn -calculate the number of trials (n in the definition of the pdf)
PrOmpr - calculate the probability of success in any given trial (p in the pdf definition)
Care should be exercised in keeping the proper order of the variables in the calls to the
function.
Some examples follow:
-->n = 10; x = 6; p = 0.35; q = 1-p;
-->[P,Q] = cdfbin('PQ',x,n,p,q) //Calculating probabilities
Q =
.0260243
P =
.9739757
-->n=20;p=0.35;q=1-p;P=0.75;Q=1-P;
-->x = cdfbin("S",n,p,q,P,Q) //Calculating the inverse CDF
x =
7.9132062
-->[p,q] = cdfbin("PrOmpr",P,Q,x,n) //Calculating p and q = 1-p
q =
.7391494
p =
.2608506
Notes: Use help cdfnbn to learn more about the function that implements the negative
Binomial distribution. The function cdfpoi was described in detail in Chapter 13.
Discrete probability calculations through user-defined functions
Besides the few pre-programmed cumulative distribution functions provided by SCILAB,
probabilities can be calculated by defining probability mass and cumulative distribution
functions for the different distributions presented earlier. The basic definitions of
probabilities in terms of probability mass and cumulative distribution functions are:
P(X=x) = f
X
(x), pmf


x
x
X
k f x X P
0
), ( ) ( cdf for Binomial, Poisson, and hypergeometric distributions
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz


x
x
X
k f x X P
1
), ( ) ( cdf for geometric distribution
We will define the following functions for the distributions shown earlier:
pmf CDF
Binomial b(x,n,p) B(x,n,p)
Poisson p(x,lambda) P(x,lambda)
geometric g(x,p) G(x,p)
hypergeometric h(x,N,n,a) H(x,N,n,a)
The following is a SCILAB script, called DiscreteProbabilityFunctions, which includes the
definitions for the eight function calls listed in the table immediately above:
//Defining discrete probability distributions
deff('[CC]=C(n,r)','CC=gamma(n+1)./(gamma(r+1).*gamma(n-r+1))') //Binomial coefficient
deff('[bb]=b(x,n,p)','bb=C(n,x).*p.^x.*(1-p).^(n-x)') //Binomial pmf
deff('[BB]=B(x,n,p)','BB=sum(b([0:1:x],n,p))') //Binomial CDF
deff('[pp]=p(x,lambda)','pp=exp(-lambda).*lambda^x./gamma(x+1)') //Poisson pmf
deff('[PP]=P(x,lambda)','PP=sum(p([0:1:x],lambda))') //Poisson CDF
deff('[gg]=g(x,p)','gg=p.*(1-p).^(x-1)') //Geometric pmf
deff('[GG]=G(x,p)','GG=sum(g([1:x],p))') //Geometric CDF
deff('[hh]=h(x,N,n,a)','hh=C(a,x).*C(N-a,n-x)./C(N,n)') //Hypergeometric pmf
deff('[HH]=H(x,N,n,a)','HH=sum(h([0:1:x],N,n,a))') //Hypergeometric CDF
To execute the script that defines the discrete probability functions use:
-->exec('DiscreteProbabilityFunctions')
Combinations
The function C(n,r) represents combinations of n elements taken r by r, or the binomial
coefficient:
-->C(10,5)
ans =
252.
This is a vector of values of C(n,r) for n = 10, and r = 0,1, , 10:
-->C10=[];for j=0:10,C10=[C10 C(10,j)]; end; C10
C10 =
! 1. 10. 45. 120. 210. 252. 210. 120. 45. 10.
1. !
Binomial distribution
For the binomial distribution with n = 10 and p = 0.25, the following call to function b(x,n,p)
calculates the probability P(X=2) = b(2,10,0.25):
-->b(2,10,0.25)
ans =
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
.2815676
The following is a list of values of the binomial pmf for n = 10, p = 0.25, for all possible values
of x = 0,1, , 10:
-->b10=[];for j=0:10,b10=[b10 b(j,10,0.25)]; end; b10
b10 =
column 1 to 7
! .0563135 .1877117 .2815676 .2502823 .145998 .0583992
.016222 !
column 8 to 11
! .0030899 .0003862 .0000286 9.537E-07 !
The binomial CDF for x = 2, n = 10, p = 0.25 is calculated with the following call to function
B(x,n,p). This value represents P(X2):
-->B(2,10,0.25)
ans =
.5255928
This value represents P(X>2) = 1 - P(X2):
-->1-B(2,10,0.25)
ans =
.4744072
The following is a list of values of the binomial CDF for n = 10, p = 0.25, for all values of x =
0,1, , 10:
-->B10=[];for j=0:10,B10=[B10 B(j,10,0.25)]; end; B10
B10 =
column 1 to 7
! .0563135 .2440252 .5255928 .7758751 .9218731 .9802723
.9964943 !
column 8 to 11
! .9995842 .9999704 .9999990 1. !
Poisson distribution
The pmf of the Poisson distribution can be used to calculate probabilities such as P(X=2) for =
5.2:
-->p(2,5.2)
ans =
.0745840
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
For P(X=6), the Poisson distribution with for = 5.2 produces:
-->p(6,5.2)
ans =
.1514803
The cumulative distribution function for the Poisson distribution, with for = 5.2, provides the
probability P(X6):
-->P(6,5.2)
ans =
.7323933
The following SCILAB commands produce a vector of values of the Poisson cdf for x = 0, 1, ,
10, and = 5.2:
-->P10=[];for j=1:10, P10=[P10 P(j,5.2)]; end; P10
P10 =
column 1 to 7
! .0342027 .1087867 .2380655 .406128 .580913 .7323933
.8449216 !
column 8 to 10
! .9180650 .9603256 .9823011 !
Geometric distribution
The probabilities P(X=3) and P(X=5) using the geometric distribution with p = 0.50 are
calculated as:
-->g(3,0.50)
ans =
.125
-->g(5,0.50)
ans =
.03125
The following example shows a way to calculate a vector of values of the geometric
distribution pmf for x = 1, 2, , 10:
-->g([1:10],0.5)
ans =
column 1 to 9
! .5 .25 .125 .0625 .03125 .015625 .0078125
.0039063 .0019531 !
column 10
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
! .0009766 !
The following evaluations of the geometric distribution cdf are used to calculate the
probabilities P(X6), P(X3), and P(X1), respectively:
-->G(6,0.5)
ans =
.984375
-->G(3,0.5)
ans =
.875
-->G(1,0.5)
ans =
.5
A vector of values of the geometric distribution CDF, with p = 0.5, is produced by using the
following commands:
-->G10=[];for j=1:10, G10=[G10 G(j,0.5)]; end; G10
G10 =
column 1 to 9
! .5 .75 .875 .9375 .96875 .984375 .9921875
.9960938 .9980469 !
column 10
! .9990234 !
Hypergeometric distribution
The next line assign values to the parameters N, n, and a in the hypergeometric distribution:
-->N=100;n=20;a=35;
The probability P(X=12) for the hyperbolic distribution with the parameters N, n, and a defined
above is calculated as:
-->h(12,N,n,a)
ans =
.0078581
The cumulative distribution function for the hypergeometric distribution for x = 12 is
calculated as follows:
-->H(12,N,n,a)
ans =
.9976693
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
The value just calculated represents the probability P(X12). The next statement generates a
vector of values of the hypergeometric pdf for x = 0, 1, 2, , 20:
-->h([0:20],N,n,a)
ans =
column 1 to 7
! .0000529 .0008046 .0055295 .0228093 .0633073 .1256018
.1847085 !
column 8 to 14
! .2060210 .1768671 .1179114 .0613139 .0248839 .0078581
.0019176 !
column 15 to 21
! .0003575 .0000501 .0000051 3.698E-07 1.761E-08 4.924E-10
6.060E-12 !
The next line produces a vector of values of the hypergeometric CDF:
-->H10=[];for j=1:10,H10=[H10 h(j,N,n,a)]; end; H10
H10 =
column 1 to 7
! .0008046 .0055295 .0228093 .0633073 .1256018 .1847085
.2060210 !
column 8 to 10
! .1768671 .1179114 .0613139 !
Continuous probability functions
In this section we describe several continuous probability distributions including the gamma,
exponential, beta, and Weibull distributions. Some of these distributions make use of the
Gamma function, (x), which is defined next.
__________________________________________________________________________________
Factorials and the Gamma function (see also Chapter 13)
The Gamma function is defined by
This function has the property that ,
() = (-1) (1), for > 1,
therefore, it can be related to the factorial of a number, i.e.,
dx e x
x


0
1
) (

Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz


() = (-1)!,
when is a positive integer.
Factorials have applications in combinatorics (calculation of combinations and permutations,
etc.), and in some discrete probability distributions (e.g., binomial probability distribution),
while the gamma function has applications in continuous probability distributions (e.g., the
gamma probability distribution.)
__________________________________________________________________________________
The gamma distribution
The probability distribution function (pdf) for the gamma distribution is given by
The parameters and are referred to, respectively, as the shape and scale parameters of the
gamma distribution. Other parameters of this distribution are:
2
,
X X
.
SCILAB provides function cdfgam for operations with the gamma distribution CDF. The calls to
this function take the form
[P,Q]=cdfgam("PQ",X,Shape,Scale)
[X]=cdfgam("X",Shape,Scale,P,Q)
[Shape]=cdfgam("Shape",Scale,P,Q,X)
[Scale]=cdfgam("Scale",P,Q,X,Shape)
where P = P(XX<X), Q = 1- P, Shape = , and Scale = , with XX ~ gamma(,).
The following are examples of applications of function cdfgam. The following three calls
determine, respectively, the probabilities P = P(X<10), P = P(X<3), and P = P(X<0.5), as well as
the probabilities of the complement, Q = 1 - P, for the gamma distribution with = 2, = 3:
-->[P,Q]=cdfgam("PQ",10,2,3)
Q =
2.901E-12
P =
1.
-->[P,Q]=cdfgam("PQ",3,2,3)
Q =
.0012341
P =
.9987659
; 0 , 0 , 0 ), exp(
) (
1
) (
1
> > >

x for
x
x x f
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
-->[P,Q]=cdfgam("PQ",0.5,2,3)
Q =
.5578254
P =
.4421746
The next call to function cdfgam calculates the inverse gamma function, i.e., the value of x for
P = P(X<x) where X follows the gamma distribution with = 2, = 3:
-->x=cdfgam('X',2,3,0.4,0.6)
x =
.4588071
The next call to the function is used to calculate the shape parameter, , given a probability P
= P(X<0.3) = 0.6, Q = 1-P = 0.4, with X following the gamma distribution with a scale parameter
= 2:
-->alpha = cdfgam('Shape',2,0.6,0.4,0.3)
alpha =
.7190660
The next call to function cdfgam calculates the scale parameter, , given a probability P =
(X<1.2) = 0.2, Q = 1-P = 0.8, with X following the gamma distribution with = 3:
-->beta = cdfgam('Scale',0.2,0.8,1.2,3)
beta =
1.2792035
The exponential distribution
The exponential distribution is the gamma distribution with = 1. Its pdf is given by
While its cdf is given by
F
X
(x) = 1 - exp(-x/), for x>0, >0.
Parameters of the exponential distribution include:
.
1
,
1


X X
The beta distribution
; 0 , 0 ), exp(
1
) ( > >

x
x
x f
X
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
The pdf for the beta distribution is given by
As in the case of the gamma distribution, the corresponding cdf for the beta distribution is
also given by an integral with no closed-form solution.
The parameters of the beta distribution include
. ) )( 1 (
) ( ,
2


+ + +

+
X Var
X
SCILAB provides function cdfbet for operations with the cumulative distribution function of the
beta distribution. Calls to the function are the following:
[P,Q]=cdfbet("PQ",X,Y,A,B)
[X,Y]=cdfbet("XY",A,B,P,Q)
[A]=cdfbet("A",B,P,Q,X,Y)
[B]=cdfbet("B",P,Q,X,Y,A)
In these calls P = P(XX<X), Y = 1 - X, Q = 1 - P, A, B are the parameters and of the beta
distribution.
Next, we present some applications of function cdfbet. The first example calculate the
probability P(X<0.35) for the beta distribution with = 2, = 3:
-->[P,Q]=cdfbet('PQ',0.35,1-0.35,2,3)
Q =
.5629813
P =
.4370187
An example that calculates the inverse function of the beta cdf, i.e., the value of x for which P
= P(X<x) = 0.75, for the beta distribution with = 3, = 5 is presented next:
-->[X,Y] = cdfbet("XY",3,5,0.75,1-0.75)
Y =
.5139030
X =
.4860970
The next two examples shows how to obtain the parameters a and b from the beta distribution
given values of X = 0.3, Y = 1-X = 0.7, P = P(X<0.3) = 0.4, and Q = 1-P = 0.6. In the first
application = 3.5, while in the second application = 1.5:
-->alpha = cdfbet("A",3.5,0.4,0.6,0.3,0.7)
alpha =
; 0 , 0 , 1 0 , ) 1 (
) ( ) (
) (
) (
1 1
> > < <

+






x x x x f
X
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
2.0459494
-->beta = cdfbet("B",0.6,0.4,0.8,0.2,1.5)
beta =
.7453948
The Weibull distribution
The pdf for the Weibull distribution is given by
While the corresponding cdf is given by
Parameters of this distribution are:
1
]
1

+ + +

)
1
1 ( )
2
1 ( ) ( ),
1
1 (
2 / 2 / 1



X Var
X
.
The uniform distribution
The uniform distribution for a continuous random variable is defined for values of X such that a
<x<b. The corresponding probability density function is given by
b x a
a b
x f
X
< <

,
1
) (
The cumulative distribution function is
b x a
a b
a x
x F
X
< <

, ) (
The parameters of the uniform distribution are:
.
12
) (
) ( ,
2
2
a b
X Var
b a
X

+

The following function definition implements the cumulative distribution function for the
uniform distribution in SCILAB:
-->deff('[FF]=FX(x)','FF=(x-a)/(b-a)')
For values of a = 2.5 and b = 3.2, we proceed to calculate some probabilities:
0 , 0 , 0 ), exp( ) (
1
> > >



x for x x x f
0 , 0 , 0 ), exp( 1 ) ( > > >

x for x x F
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
--> a = 2.5; b = 3.2;
First, we calculate P(X<2.7) = F
X
(2.7):
-->FX(2.7)
ans =
.2857143
Next, we calculate P(X>3) = 1 - P(X<3) = 1 - F
X
(3):
-->1-FX(3)
ans =
.2857143
The following example calculates P(2.8<X<3) = P(X<3) - P(X<2.8) = F
X
(3) - F
X
(2.8):
-->FX(3)-FX(2.8)
ans =
.2857143
User-defined functions for continuous probability distributions
The following SCILAB script defines the probability density function and the cumulative density
function for four selected continuous distributions: gamma, exponential, beta, and Weibull.
The script is called ContinuousProbabilityFunctions, and is invoked by using:
-->exec('ContinuousProbabilityFunctions')
The listing of the script is the following:
//Define selected continuous probability functions
deff('[gg]=gam(x,a,b)','gg=x.^(a-1).*exp(-x./b)./(b.^a.*gamma(a))')
deff('[GG]=GAM(x,a,b)','GG=intg(0,x,gam)')
deff('[ee]=eex(x,b)','ee=exp(-x./b)./b')
deff('[EE]=EEX(x,b)','EE=1-exp(-x./b)')
deff('[bb]=bet(x,a,b)',...
'bb=gamma(a+b).*x.^(a-1).*(1-x).^b./(gamma(a).*gamma(b))')
deff('[BB]=BET(x,a,b)','BB=intg(0,x,bet)')
deff('[ww]=w(x,a,b)','ww=a.*b.*x^(b-1).*exp(-a.*x.^b)')
deff('[WW]=W(x,a,b)','WW=1-exp(-a.*x.^b)')
The functions defined through the script are summarized in the following table:
pdf CDF
gamma gam(x,,) GAM(x,,)
exponential eex(x,) EEX(x,)
beta bet(x,,) BET(x,,)
Weibull w(x,,) W(x,,)
Applications of these functions follow, starting with the gamma distribution.
The gamma distribution
First, we plot the pdf of the distribution using = 2 and = 3:
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
-->xx=(0:0.1:20);yy=gam(xx,2,3);
-->plot(xx,yy,'x','fX(x)','gamma distribution')
A plot of the gamma distribution CDF for = 2 and = 3 is obtained by using:
-->yyy=[];for x=0:0.1:20, yyy=[yyy GAM(x,2,3)]; end;
-->plot(xx,yyy,'x','FX(x)','gamma distribution')
The CDF can be used to calculate probabilities. The next three lines calculate the following
probabilities P(X<5) = F
X
(5), P(6<X<11) = F
X
(11) - F
X
(5), and P(X>7.5) = 1 - P(X<7.5) = 1 - F
X
(7.5):
-->GAM(5,2,3)
ans = .4963317
-->GAM(11,2,3)-GAM(6,2,3)
ans = .2867187
-->1-GAM(7.5,2,3)
ans = .2872975
The exponential distribution
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
The following commands generate plots of the pdf and CDF for the exponential distribution
using = 2.5:
-->xx=(0:0.1:20);yy=eex(xx,2.5);
-->plot(xx,yy,'x','fX(x)','exponential distribution')
-->yyy=[];for x=0:0.1:20, yyy=[yyy EEX(x,2.5)]; end;
-->plot(xx,yyy,'x','FX(x)','exponential distribution')
The following probability calculations for the exponential distribution with = 2.5 are
presented next: P(X<6) = F
X
(6), P(X>4) = 1 - P(X<4) = 1 - F
X
(4), and P(4<X<6) = F
X
(6)-F
X
(4):
-->EEX(6,2.5)
ans =
.9092820
-->1-EEX(4,2.5)
ans =
.2018965
-->EEX(6,2.5)-EEX(4,2.5)
ans =
.1111786
The beta distribution
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
To plot the pdf and CDF of the beta distribution with = 2.5, = 3.5, we use:
-->xx=(0:0.05:1);yy=bet(xx,2.5,3.5);
-->plot(xx,yy,'x','fX(x)','beta distribution')
-->yyy=[];for x=0:0.05:1, yyy=[yyy BET(x,2.5,3.5)]; end;
-->plot(xx,yyy,'x','FX(x)','beta distribution')
The following probability calculations for the beta distribution with = 2.5 are presented next:
P(X<0.25) = F
X
(0.25), P(X>0.75) = 1 - P(X<0.75) = 1 - F
X
(4), and P(0.3<X<0.8) = F
X
(0.8)-F
X
(0.3):
-->BET(0.25,2.5,3.5)
ans =
.1737696
-->1-BET(0.75,2.5,3.5)
ans =
.4250376
-->BET(0.8,2.5,3.5)-BET(0.3,2.5,3.5)
ans =
.3428804
The Weibull distribution
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
Plots of the pdf and CDF for the Weibull distribution with = 2 and = 3 are obtained as
follows:
-->xx=(0:0.01:2);yy=w(xx,2,3);
-->plot(xx,yy,'x','fX(x)','Weibull distribution')
-->yyy=[];for x=0:0.01:2, yyy=[yyy W(x,2,3)]; end;
-->plot(xx,yyy,'x','FX(x)','Weibull distribution')
The following probability calculations for the Weibull distribution with = 2 and = 3 are
presented next: P(X<1.5) = F
X
(1.5), P(X>0.6) = 1 - P(X<0.6) = 1 - F
X
(4), and P(0.5<X<1.2) =
F
X
(0.8)-F
X
(0.3):
-->W(1.5,2,3)
ans =
.9988291
-->1-W(0.6,2,3)
ans =
.6492094
-->W(1.2,2,3)-W(0.5,2,3)
ans =
.7472451
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
Continuous probability distributions used in statistical inference
Statistical inference is the process by which sample data is used to provide information about
the population. Some of the products of statistical inference are the generation of confidence
intervals and the test of hypotheses for population parameters. There are a number of
continuous probability distributions of great utility in statistical inference. These are:
the standard normal distribution
the Students t distribution
the Chi-square (
2
) distribution
the F distribution
The probability density functions (pdf) for these distributions are presented below:
The Normal distribution
The expression for the normal distribution pdf is:
where is the mean, and
2
the variance of the distribution.
SCILAB provides function cdfnor for operations with the cumulative distribution function for the
normal distribution. Function cdfnor was presented in detail in Chapter . To find on-line
information on this function use the command:
-->help cdfnor
The Student-t distribution
The Student-t, or simply, the t-, distribution has one parameter , known as the degrees of
freedom. The probability density function (pdf) is given by
The following SCILAB commands can be used to plot the pdf for the Student t distribution with
-->deff('[f]=fT(t,nu)',...
-->'f=gamma((nu+1)./2).*(1+t.^2./nu).^(-(nu+1)/2)/(sqrt(%pi*nu)*gamma(nu/2))')
-->tt=[-4:0.1:4];ff=fT(tt,6);
-->plot(tt,ff,'t','fT(t)','Student t - nu = 6')
< < +

+

t
t
t f , ) 1 (
)
2
(
)
2
1
(
) (
2
1 2

],
2
) (
exp[
2
1
) (
2
2


x
x f
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
SCILAB provides function cdft for operations with the cumulative distribution function of the
Students t distribution. The calls to the function are as follows:
[P,Q]=cdft("PQ",T,Df)
[T]=cdft("T",Df,P,Q)
[Df]=cdft("Df",P,Q,T)
In these function calls, P = P(TT<T), Q = 1 - P, Df = degrees of freedom = , with TT ~ Student
t(Df).
-->[P,Q] = cdft("PQ",0.4,6) //Probability calculation
Q =
.3515041
P =
.6484959
-->t = cdft("T",8,0.45,1-0.45) //Inverse CDF calculation
t =
- .1297073
-->nu = cdft("Df",0.7,0.3,0.8) //Obtaining degrees of freedom
nu =
.7716700
A plot of the CDF for the Student t distribution can be produced using the following commands:
-->xx=[-4:0.1:4];
-->yy=[];for x=-4:0.1:4, yy=[yy cdft('PQ',x,6)]; end;
-->plot(xx,yy,'t','fX(t)','Student t - nu = 6')
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
The Chi-squared (
2
) distribution
The Chi-squared (
2
) distribution has one parameter , known as the degrees of freedom. The
probability distribution function (pdf) is given by
A plot of the pdf for the Chi-square distribution with = 10 can be obtained by using:
-->xx = [0:0.1:10];
-->yy=[];for x=0:0.1:10, yy=[yy cdfchi('PQ',x,4)]; end;
-->plot(xx,yy,'t','fX(t)','Chi-square - nu = 4')
SCILAB provides function cdfchi for operations with the cumulative distribution function of the

2

(chi-square) distribution. The calls to this function include:
[P,Q]=cdfchi("PQ",X,Df)
[X]=cdfchi("X",Df,P,Q);
[Df]=cdfchi("Df",P,Q,X)
0 , 0 ,
)
2
( 2
1
) (
2
1
2
2
> >


x e x x f
x

Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz


In these calls to function cdfchi P = P(XX<X), Q = 1 - P, Df = degrees of freedom, with XX ~
2
(Df).
-->[P,Q] = cdfchi("PQ",1,10) //Probability calculation
Q =
.9998279
P =
.0001721
-->[P,Q] = cdfchi("PQ",0.2,10) //Probability calculation
Q =
.9999999
P =
7.668E-08
-->chi2 = cdfchi("X",4,0.4,0.6) //Inverse CDF calculation
chi2 =
2.7528427
-->nu = cdfchi("Df",0.4,0.6,2.7) //Calculating degrees of freedom
nu =
3.9409085
A plot of the CDF for the Chi-square distribution with n = is obtained by using:
-->deff('[f]=fC(x,nu)',...
-->'f=x.^(nu/2-1).*exp(-x./2)/(2.^(nu/2).*gamma(nu./2))')
-->cc=[0:0.1:30];ff=fC(cc,10);
-->plot(cc,ff,'chi^2','fC(chi^2)','Chi-square - nu = 10')
The F distribution
The F distribution has two parameters N = numerator degrees of freedom, and D =
denominator degrees of freedom. The probability distribution function (pdf) is given by
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
D>0, N>0, x>0.
A plot of the F-distribution pdf for nN = 4, nD = 6, is obtained by using:
-->deff('[f]=fF(F,nuN,nuD)',...
-->'f=gamma((nuN+nuD)./2).*(nuN./nuD).^(nuN./2).*F.^(nuN./2-1)./...
-->(gamma(nuN./2).*gamma(nuD./2).*(1+nuN.*F./nuD).^((nuN+nuD)./2))')
-->xx=[0:0.1:10];ff=fF(xx,4,6);
-->plot(xx,ff,'F','fF(F)','F distribution - nuNum = 4 - nuDen = 6')
SCILAB provides the function cdff for operations with the cumulative distribution function of
the F distribution.
[P,Q]=cdff("PQ",F,Dfn,Dfd)
[F]=cdff("F",Dfn,Dfd,P,Q);
[Dfn]=cdff("Dfn",Dfd,P,Q,F);
[Dfd]=cdff("Dfd",P,Q,F,Dfn)
In these calls of the function cdff, P = P(FF<F), Q = 1 - P, Dfn and Dfd = degrees of freedom in
the numerator and denominator of F.
-->[P,Q] = cdff("PQ",1.2,6,12) //Probability calculation
Q =
.3697351
P =
.6302649
-->F = cdff("F",10,2,0.4,0.6) //Inverse CDF calculation
F =
.9944093
-->nuNum= cdff('Dfn',5,0.4,0.6,0.8) //calculating degrees of freedom
nuNum =
5.3847039
)
2
(
1
2 2
) 1 ( )
2
( )
2
(
) ( )
2
(
) (
D N
N N
D
F N D N
F
D
N D N
x f

+

+

Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz


A plot of the F-distribution CDF is produced through the following SCILAB commands:
-->xx = [0:0.1:10];
-->yy=[];for x=0:0.1:10, yy=[yy cdff('PQ',x,4,6)]; end;
-->plot(xx,yy,'t','fX(t)','F - nuNum = 4 - nuDen = 6')
Applications of the normal distribution in data
analysis
The normal distribution, also known as the bell curve, appears commonly when determining
the frequency distribution of different types of physical measurements. We first introduced
the normal distribution in Chapter 14 as an example of a continuous probability distribution. In
this section we present some applications of this probability distribution in data analysis.
The probability density function, pdf, for a general normal distribution, X, with a mean value,
, and a standard deviation, , is given by
. , 0 ,
2
) (
exp
2
1
) (
2
2
< < >

,
_


x
x
x f
X


The standard normal distribution has mean value = 0 and standard deviation = 1.
SCILAB provides function cdfnor for operations with the normal cumulative distribution
function. The different forms of the call to the function were presented in detail in Chapter$,
and are repeated here:
[p,q] = cdfnor(PQ,x,mu,sigma)
[x] = cdfnor(X,mu,sigma,p,q)
[mu] = cdfnor(Mean,sigma,p,q,x)
[sigma] = cdfnor(Std,p,q,x,mu)
where mu is the mean value (m), sigma is the standard deviation (s), p = P(X<x), and q = 1 - p =
P(X>x). The first argument in the different calls to cdfnor is a string that indicates the type of
result expected:
PQ - to request probabilities p and q
X - to request a value of the normal variable
Mean - to request the mean of the distribution
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
Std - to request the standard deviation of the distribution
Because the normal distribution is commonly found in the analysis of physical measurements, it
if often recommended that you check if your data set (your sample) follows the normal
distribution. In this section we present two graphical approaches for checking if your data
follows the normal distribution. The first consists of superimposing a normal distribution pdf,
based on the mean value and standard deviation of the sample, on top of the sample
histogram. The second approach consists in plotting the data against what is commonly known
their normal scores. The resulting graph is equivalent to plotting the data in a normal
probability paper, i.e., a paper with one scale representing the normal probability
corresponding to the data set. These two approaches are described next.
Plotting a histogram and its corresponding normal curve
The purpose of this plot is to visually check if the histogram of a sample, with a suitable
number of classes, matches a superimposed normal curve. For that purpose we propose the
following SCILAB user-defined function, histnorm:
function [chi2,cmark,fcount]=histnorm(x, xclass)
//This function calculates the frequency distribution
//for the data in (row) vector x according to the
//class boundaries contained in the (row) vector
//xclass. It also produces a histogram of the
//data and the normal curve that best fit the data.
//
//Typical call: [chi2,cm,f] = freqdist(x,xclass)
//where cm = class marks, f = frequency count,
// chi2 = chi-square parameter for the fitting
[m n] = size(x); //Sample size
[m nB] = size(xclass); //Number of class boundaries
k = nB - 1; //Number of classes
//Calculate class marks
cmark = zeros(1,k);
for ii = 1:k
cmark(ii) = 0.5*(xclass(ii)+xclass(ii+1));
end
//Initialize frequency counts to zero
fcount=zeros(1,k);
fbelow=0; fabove=0;
//Accumulate frequency counts
for ii = 1:n
if x(ii) < xclass(1)
fbelow = fbelow + 1;
elseif x(ii) > xclass(nB)
fabove = fabove + 1;
else
for jj = 1:k
if x(ii)>= xclass(jj) & x(ii)< xclass(jj+1)
fcount(jj) = fcount(jj) +1;
end
end
end
end
//define normal CDF, calculate xbar, sx, chi-square parameter
nn = sum(fcount);
xbar = mean(x); sx = st_deviation(x);
xmin = min(xclass); xmax = max(xclass);
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
pk = [];
for j = 1:k+1
pk = [pk cdfnor("PQ",xclass(j),xbar,sx)];
end;
p_in_classes = pk(k+1)-pk(1);
pxclass = pk(2:k+1) - pk(1:k);
fc = pxclass*nn*p_in_classes;
//Chi square parameter
chi2=0;
for j = 1:length(fc)
chi2 = chi2 + (fcount(j)-fc(j))^2/fc(j);
end;
//Produce normal distribution for data
Dx = (xmax-xmin)/100;
xx = [xmin:Dx:xmax];
xxx = xx(1:100) + Dx/2;
pkk = [];
for j = 1:101
pkk = [pkk cdfnor("PQ",xx(j),xbar,sx)];
end;
pp = pkk(2:101) - pkk(1:100);
fcc = pp*p_in_classes*nn*100/k;
//Determine plot rectangle
ymin = 0;
ymaxf = max(fcount); ymaxy = max(fcc);
ymax = max(ymaxf,ymaxy);
ymax = int(1.1*ymax);
plotrectangle = [xmin ymin xmax ymax];
//plot the histogram and normal curve
xp = xclass(1:k);
xset('window',1);xbasc(1);
plot2d2('onn',xclass',[fcount fcount(k)]',[1],'011','y',[xmin ymin xmax ymax]);
plot2d3('onn',xp',fcount',[1],'000');
plot2d(xxx',fcc',[2],'000');
xtitle('Histogram with normal curve','x','frequency');
//end function histnorm
Notice that this function uses SCILAB function cdfnor to calculate values of the cumulative
distribution function for the normal distribution where needed. The general call to the
function is:
[chi2,cm,f] = freqdist(x,xclass)
which returns, in general, the class marks, cm, the frequency count, f, and a chi-square
parameter defined as

k
i i
i i
fc
fc f
1
2
2
,
) (

where f
i
is the actual frequency count for the ith class, fc
i
is the estimated frequency count
obtained from the normal distribution for the ith class, and k is the number of classes in the
frequency distribution.
The
2
parameter follows the chi-square distribution with = k-1 degrees of freedom, and it is
used to check the hypothesis that the frequency distribution under consideration follows
indeed the normal distribution. The subject of hypothesis testing is developed in Chapter ,
therefore, we delay until then the use of the parameter returned from function histnorm.
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
Application of the function histnorm
In this example we apply function histnorm to a set of 200 data values between 0 and 100
generated using function rand as follows:
-->x = int(100*rand(1,200));
First, we check the minimum and maximum value of the data:
-->min(x), max(x)
ans =
0.
ans =
99.
A set of class boundaries of 0, 10, 20, , 100, will produce 10 classes for this sample:
-->xclass = [0:10:100];
Next, we load the function histnorm and apply the function to the data stored in x using the
class boundaries stored in xclass
-->getf(histnorm)
-->histnorm(x,xclass)
ans =
1.9583514
The value returned is the chi-square parameter for the normal curve fitting. The plot of the
histogram with the super-imposed normal curve is:
A second example for the same data sample is presented next in which we use 20 classes, with
class boundaries 0, 5, 10, , 95, 100, to classify the data:
-->xclass=[0:5:100];
The results from function histnorm are the chi-square parameter and the following plot:
-->histnorm(x,xclass)
ans =
2.0146916
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
The function can be invoked with a vector of three values in the left-hand side to produce not
only the chi-square parameter and the plot, but also the class marks and the frequency count
of the sample:
-->[X2,cm,f] = histnorm(x,[0:10:100])
f =
column 1 to 9
! 20. 18. 27. 18. 23. 22. 16. 18. 14. !
column 10
! 24. !
cm =
! 5. 15. 25. 35. 45. 55. 65. 75. 85. 95. !
X2 =
1.9583514
Notice that in the two graphs shown above, the normal curve does not fit the histograms very
well. The main reason is that the data was generated from an uniform distribution (i.e., using
the default settings of SCILABs function rand) and not from a normal distribution. Later in
this chapter we deal with the generation of data other than from an uniform distribution, and
will be using function histnorm to check how well those data fit the normal distribution.
Plotting data against their normal scores
Assume that the continuous random variable X follows the normal distribution with mean and
standard deviation . Given a probability p (0<p<1) such that P(X<x)=p with X ~ N(,), then
the value of x is referred to as the normal score for p. [Note: In some references in the
statistical literature the normal scores are related to a probability = 1 - p, so that if P(X>x

) =
, with X ~ N(,), x

is the normal score for .]


Suppose that we have an ordered data set, xp = {xp
1
<xp
2
< <xp
n
} that follows the normal
distribution with mean and standard deviations equal to the samples mean (x) and standard
deviation (s
x
). Also, assume that the probability of the interval [xp
i
, xp
i+1
] is the same for all
values of i = 1, 2, , n-1, say P(xp
i
<X<xp
i+1
) = q. Also, assume that P(X<xp
1
) = P(X>xp
n
) = q.
Thus, the entire area under the normal curve is split into n+1 sub-regions of the same area q as
illustrated in the figure below.
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
The value of q is, therefore, q = 1/(n+1), and we can write:
P(X<xp
1
) = q, P(X<xp
2
) = 2q, , P(X<xp
i
) = iq, , P(X<xp
n
) = nq.
In general,
P(X<xp
i
) = i/(n+1) = p
i
,
for of i = 1, 2, , n. The values p
i
are referred to as plotting positions for they are used to
obtain the normal scores corresponding normal score xp
i
.
Given an ordered data set, x = {x
1
< x
2
< < x
n
}, of size, n, we can generate a vector of
plotting positions, p
i
= i/(n+1), and obtain a set of normal scores xp
i
, by using the function call
cdfnor(X,x,s
x
,p
i
,1-p
i
), where x and s
x
are the mean and standard deviation of the data set.
If the given data set, x, does indeed follow the normal distribution with mean =x, and
standard deviation = s
x
, a plot of normal scores xp versus the original data x should produce a
straight line.
A function to produce a plot of data versus normal scores
The following function, normplot, takes as input a data set, or sample, x = {x
1
, x
2
, , x
n
},
orders it in increasing order, obtains the plotting positions p
i
, calculates the normal scores xp
i
,
and plots the normal scores versus the ordered data. It also plots a straight line representing
y=x, or the exact fitting for a normal distribution. The closer the plot of normal scores vs.
data is to the straight line representing the exact fitting for a normal distribution, the closer
the data set follows the normal distribution.
function normplot(x)
//This function produces a normal probability
//paper plot for the data in (row) vector x
xx = sortup(x); //order sample in increasing order
xm = mean(xx); //mean of sample
sx = st_deviation(xx); //standard deviation of sample
nn = length(x); //sample size
//Calculating plotting positions and normal scores
pp = []; xp = [];
for j = 1:nn
pp = [pp j/(nn+1)];
xp = [xp cdfnor(X,xm,sx,pp(j),1-pp(j))];
end;
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
//Determine the plotting rectangle xmin1 = min(xx); xmin2 = min(xp); xmin =
min(xmin1,xmin2); xmax1 = max(xx); xmax2 = max(xp); xmax = max(xmax1,xmax2);
ymin = min(xp); ymax = max(xp);
//Produce a graduated scale
[xminp, xmaxp, nxp] = graduate(xmin,xmax);
[yminp, ymaxp, nyp] = graduate(ymin,ymax);
//Plot scores vs. data and exact normal distribution fitting
plot2d(xp,xp,[ 1],011,y,[xminp yminp xmaxp ymaxp])
xset(mark,-9,2);
plot2d(xx,xp,[-9],011,y,[xminp yminp xmaxp ymaxp])
xtitle(Normal probability plot,x,z);
//end function normplot
An application of this function is shown next. First, we produce a sample of 200 data points
using a uniform distribution. Next, we load function normplot and produced the normal
probability plot.
-->x =int(100*rand(1,200));
-->getf(normplot)
-->normplot(x)
The resulting graph shows that the data does not follow the normal distribution particularly
near the lowest and highest values of the data set.
The lognormal distribution
If the random variable Y = ln(X) follows the normal distribution with mean
Y
=
ln(X)
and
standard deviation
Y
=
ln(X)
, then we say that the random variable X follows the lognormal
distribution. The probability density function of the lognormal distribution is given by
. 0 ,
2
) (ln
exp
2
1
) (
2
) ln(
2
) ln(
) ln(
>

,
_


x
x
x
x f
X
X
X
X


with
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
( ) ). 2 exp( 1 ) exp( ) exp( ) ( ,
2
1
exp
) ln(
2
) ln(
2
) ln(
2
) ln( ) ln( X X X X X X
X Var

,
_

+
For calculating probabilities we can use the normal distribution cdf by first calculating the
natural log of the variable, for example, if X~lognormal(
ln(X)
=1.2,
ln(X)
=0.5), to calculate the
probability P(X<2) use P(X<2) = P(ln(X)<ln(2)) = P(Y<0.6931) where Y ~ N(1.2, 0.5). We can
use function cdfnor to calculate this probability in SCILAB as follows:
-->cdfnor(PQ,log(2),1.2,0.5)
ans =
.1553616
Suppose that we want to find the inverse cumulative distribution function, i.e., a value of X
for which P(X<x) = 0.35, given
ln(X)
=1.2,
ln(X)
=0.5, we can use:
-->cdfnor(X,1.2,0.5,0.35,0.65)
ans =
1.0073398
The previous result actually gives a value of Y = ln(X) with Y ~ N(1.2, 0.5). The corresponding
value of X is calculated as X = exp(Y), i.e.,
-->exp(ans)
ans =
2.7383068
A graph of the lognormal probability density function for
ln(X)
=1.2,
ln(X)
=0.5 is produced by
using:
-->deff([ff]=fX(x,mu,sigma),...
-->ff=exp(-(log(x)-mu).^2./(2.*sigma.^2))./(sigma.*x.*sqrt(2.*%pi)))
-->mu=1.2;sigma=0.5;xx=[0.01:0.1:10];yy=fX(xx,mu,sigma);
-->plot(xx,yy,x,fX(x),Log-normal pdf)
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
Generating synthetic data
In this section we present pre-defined and user-defined functions that allows us to generate
data that follows a particular probability distribution. We refer to such data as synthetic
data.
Generating normally-distributed synthetic data
In the examples presented in the previous section on applications of the normal distribution we
generated data by using the function rand, which, by default, produces random data uniformly
distributed in the interval [0,1]. The function rand can also be used to produce normally
distributed data, z, that follows the standard normal distribution, i.e., Z ~ N(0,1), by, first,
using the function call
rand(normal)
and next using the function call
rand(n,m)
where n and m are integers. The last call to function rand will produce a matrix of n rows and
m columns whose elements are random numbers following the standard normal function.
Recalling that the standardized normal variate is defined as
Z = (X-)/,
values of x can be obtained from values of z by using
x = + z.
The following example illustrate how to use function rand to produce 200 data points that
follow the normal distribution with mean = 150, and standard deviation = 50:
-->x = 150 + 50.*rand(1,200);
To verify that the data do indeed follow the normal distribution, we use functions histnorm and
normplot applied to this data set. To use function histnorm, we first determine the minimum
and maximum values of the data set to determine which class boundaries use in the histogram:
-->xmin = min(x), xmax = max(x)
xmin = 34.558873
xmax = 317.59609
We select for class boundaries the values 25, 50, 75, , 300, 325:
-->xclass = [25:25:325];
The resulting histogram and superimposed normal curve are shown next:
-->histnorm(x,xclass);
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
The fitting of the histogram to the corresponding normal curve is relatively good, in spite of
the apparent discrepancy towards the center of the data. We can also use function normplot
to check the normality of the data as follows:
-->normplot(x)
The resulting normal probability plot is:
The plot suggests that the data follows the normal distribution for most of the range except for
values larger than about 220.
Additional applications of function rand
SCILABs function rand, as most numerical random number generators, uses a number, known
as the seed, to produce random numbers. To find out the current value of the seed in
function rand use:
-->rand(seed)
ans = 8.096E+08
To find out which type of random number generator is active in function rand (i.e., normal or
uniform) use:
-->rand(info)
ans = normal
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
To change the function rand back to uniform use:
-->rand(uniform)
To change the seed to the number 15, for example, use:
-->rand(seed,15)
The first 10 random numbers generated by rand after seeding it with a value of 15 are:
-->rand(1,10)
ans =
column 1 to 5
! .1018111 .5348560 .9628528 .1235873 .6667947 !
column 6 to 10
! .4106913 .6578733 .6756193 .1201851 .0268646 !
After generating those 10 random numbers the value of seed has changed to:
-->rand(seed)
ans =
57691269.
If, for some reason, you need to re-start the previous sequence of random numbers, you can
simply re-seed function rand with the value of 15:
-->rand(seed,15)
Check that you get the same sequence of random numbers by comparing the following 5
random numbers with the first 5 random numbers generated earlier after using seed = 15:
-->rand(1,5)
ans =
! .1018111 .5348560 .9628528 .1235873 .6667947 !
SCILAB function for generating synthetic data
SCILAB provides function grand (generating random numbers) to generate a vector or matrix
with data that follows, among others, the following distributions: binomial, Poisson, gamma,
beta, exponential, uniform integer, uniform real, normal, chi-squared, and Students t. Two
general calls to the function are:
[x] = grand(m,n,dist_type,dist_parameters)
[x] = grand(A,dist_type,dist_parameters)
where dist_type is a string identifying the type of distribution, and dist_parameters is a list of
the parameters defining the distribution. In the first form of the call the values m and n
represent the number of rows and columns of a matrix to be generated containing random
numbers that follow the desired distribution. In the second form of the function call an
existing matrix A is provided so that the function generates a new matrix with the same
dimensions as A containing the random numbers that follow the desired distribution.
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
The following strings identify the type of distribution requested. We also identify the
parameters required for each distribution:
String Distribution Parameters
bin Binomial N, P
poi Poisson
bet Beta ,
gam Gamma = shape, = scale
exp exponential 1/
nor normal ,
chi chi-square
f F N, D
uin uniform integer a, b
unf uniform real a, b
The specific function calls for each probability distribution are shown next:
Binomial: x=grand(m,n,bin,N,P), x=grand(A,bin,N,P)
Poisson: x=grand(m,n,poi,mu), x=grand(x,poi,)
Beta: x=grand(m,n,bet,,), x=grand(A,bet, ,)
Gamma: x=grand(m,n,gam, ,), x=grand(A,gam, ,)
Exponential: x=grand(m,n,exp,), x=grand(A,exp,)
Normal: x=grand(m,n,nor,, ), x=grand(A,nor, , )
Chi-square: x=grand(m,n,chi,), x=grand(A,chi, )
F-distribution: x=grand(m,n,f, N, D), x=grand(A,f, N, D)
Uniform integer: x=grand(m,n,uin, ,), x=grand(x,uin, a, b)
Uniform real: x=grand(m,n,unf, ,),x=grand(x,unf, a, b)
Examples of synthetic data generation using function grand
The following examples demonstrate how to use function grand to generate sets of 200 data
points that follow specific probability distributions. After the data are generated we
determine their maximum and minimum values, select class boundaries for histograms of the
data, and use functions histnorm and normplot to check how close the data are to normality.
We start the exercises by loading these two functions:
-->getf(histnorm);getf(normplot);
Binomial data
-->x=grand(1,200,bin,20,0.35);xmin=min(x),xmax=max(x)
xmin = 2.
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
xmax = 14.
-->xclass=[2:2:14];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Poisson data
-->x=grand(1,200,poi,12.5);xmin=min(x),xmax=max(x)
xmin =
4.
xmax =
23.
-->xclass=[4:2:24];xset(window,1);histnorm(x,xclass);
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
-->xset(window,2);normplot(x);
Beta data
-->x=grand(1,200,bet,2,3);xmin=min(x),xmax=max(x)
xmin = .0480813
xmax = .9132797
-->xclass=[0:0.1:1];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
Gamma data
-->x=grand(1,200,gam,2,3);xmin=min(x),xmax=max(x)
xmin = .0042184
xmax = 2.6455776
-->xclass=[0:0.4:2.8];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
Normal data
-->x=grand(1,200,nor,2500,1250);xmin=min(x),xmax=max(x)
xmin =
1294.6718
xmax =
6467.2541
-->xclass=[-1000:1000:7000];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Chi-square data
-->x=grand(1,200,chi,12);xmin=min(x),xmax=max(x)
xmin =
3.8312405
xmax =
28.583772
-->xclass=[0:3:30];xset(window,1);histnorm(x,xclass);
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
-->xset(window,2);normplot(x);
F distribution data
-->x=grand(1,200,f,10,5);xmin=min(x),xmax=max(x)
xmin = .110966
xmax = 53.694396
-->xclass=[0:10:60];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
-->xclass=[0:2:12];histnorm(x,xclass);
-->xclass=[0:0.5:6];histnorm(x,xclass);
Uniform integer data
-->x=grand(1,200,uin,-5,5);xmin=min(x),xmax=max(x)
xmin =
-5.
xmax =
5.
Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz
-->xclass=[-5:1:5];xset(window,1);histnorm(x,xclass);
-->xset(window,2);normplot(x);
Uniform real data
-->x=grand(1,200,unf,-5,5);xmin=min(x),xmax=max(x)
xmin =
-4.9677424
xmax =
4.9660118
-->xclass=[-5:1:5];xset(window,1);histnorm(x,xclass);
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
-->xset(window,2);normplot(x);
Additional notes on function grand
The previous examples were used to illustrate applications of function grand to the generation
of data that follows the binomial, Poisson, gamma, beta, exponential, normal, chi-square, F-,
uniform integer, and uniform real distributions. Function grand allows the user to obtain data
that follow other distributions that are not presented in this book, such as the negative
binomial distribution, the multinomial distribution, the non-central F distribution, and the non-
central chi-square distribution. (To find information about these and other distributions
consult a statistics and probability textbook such as Spanos, A., 1999, Probability Theory and
Statistical Inference - Econometric Modeling with Observational Data, Cambridge University
Press, Cambridge, U.K.).
To obtain additional details on the use of function grand use:
-->help grand
Function grand has access to 32 different random number generators that constitute the basis
upon which random numbers that follow a particular probability distribution are generated. By
default, functions rand and grand use generator number 1. To check out which is the current
active random number generator use:
-->grand(getcgn)
ans =
1.
This result indicates that you are currently using SCILABs default random number generator.
The random number generators provided by SCILAB for use with function grand require two
seed numbers. To see the current seed numbers you can use the statement:
-->seeds = grand(getsd)
seeds =
1.0E+08 *
! 20.45933 9.2172801 !
You can re-initialize those seed to the original seeds by using:
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
-->grand(initgn,-1)
ans =
1.
We can check the initial seeds after re-initialization by using:
-->seeds = grand(getsd)
seeds =
1.0E+08 *
! 12.345679 1.2345679 !
You can also re-seed the generator (i.e., provide new seeds) by using the following call to
function grand:
-->grand(setall,10,20)
ans =
setall
To check that the new seeds are active use:
-->seeds=grand(getsd)
seeds =
! 10. 20. !
To change the random number generator from generator number 1 to generator number 5, for
example, use:
-->grand(setcgn,5)
ans =
5.
The following call to function grand can be used to verify that the change of generator has
been made:
-->grand(getcgn)
ans =
5.
To check the values of the seeds for the current generator use:
-->seeds=grand(getsd)
seeds =
! 3.795E+08 77757764. !
Pseudo-random generators
The random number generators used in SCILAB and other computer applications are known as
pseudo-random generators because, after generating a sufficiently long sequence of numbers,
the numbers start repeating. Therefore, they are not strictly random generators, but only
quasi-random or pseudo-random.
The random number generator provided with SCILAB is able to produce 2.310
18
numbers
before repetition of numbers occurs. This collection of numbers is partitioned into 32 pseudo-
random generators, each containing 2
20
=1,048,576 blocks of non-overlapping random numbers.
Each block is 2
30
= 1,073,741,824 in length.
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
Given the size of the sequences of random numbers that can be generated with each of
SCILABs 32 pseudo-random number generators, we are confident that the numbers thus
generated are random enough for most practical applications. Furthermore, use of the default
generator should be enough for most applications unless you
Another application of function grand is in the generation of permutations of a column vector.
For example, the following application produces 10 permutations of the vector M containing
the first five positive integers. The permutations are shown as columns of a matrix.
-->M = [1 2 3 4 5];
-->grand(10,prm,M)
ans =
! 1. 2. 4. 1. 4. 4. 5. 4. 1. 3. !
! 3. 1. 2. 4. 2. 2. 1. 3. 4. 2. !
! 2. 3. 5. 5. 5. 3. 2. 2. 2. 5. !
! 5. 4. 3. 3. 3. 5. 3. 1. 3. 4. !
! 4. 5. 1. 2. 1. 1. 4. 5. 5. 1. !
Generating log-normally-distributed data
To generate log-normally distributed data we first generate a set of normally distributed data
and then apply the exponential function to that data set. For example, if X follows the
lognormal distribution with
ln(X)
=1.2,
ln(X)
=0.5, we can use the following SCILAB commands to
generate a set of 200 data points. We apply functions histnorm and normplot to this data set
to check how close the data are to normality.
-->y=grand(1,200,nor,1.2,0.5); //Generate normal data N(1.2,0.5)
-->x=exp(y); //Generate log-normal data by using exp
-->xmin=min(x),xmax=max(x) //Determine min and max values
xmin =
1.1210567
xmax =
11.161347
-->xclass=[0:2:12];histnorm(x,xclass); //Histogram
-->normplot(x); //Normal probability plot
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
Generating data that follows the Weibull distribution
SCILAB does not provide for a function to generate data that follows the Weibull distribution,
however, using the uniformly-generated random numbers from function rand we can generate
numbers p between 0 and 1 that represent probabilities p = F
X
(x) = P(X<x). Next, we use the
cumulative distribution function for the Weibull distribution, namely,
and solve for x given values of p, i.e.,
.
) 1 ln(
/ 1

1
]
1



p
x
The following SCILAB commands are used to generate 200 data points that follow the Weibull
distribution with a =2, b = 3. We also use functions histnorm and normplot to check how close
these data are to normality.
-->getf(histnorm);getf(normplot) //Load functions
-->p=rand(1,200); //Generate probabilities
-->a=2; b=3; //parameters of Weibull distribution
-->x = (-log(1-p)/a)^(1/b); //generate Weibull data
-->xmin=min(x), xmax = max(x) //check data range
xmin =
.1230276
xmax =
1.3553315
-->xclass = [0:0.1:1.4]; //select classes for histogram
-->histnorm(x,xclass); //plot histogram and normal curve
0 , 0 , 0 ), exp( 1 ) ( > > >

x for x x F
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
-->normplot(x) //create normal probability plot
It is interesting to notice that this Weibull data is very close to normality.
Generating data that follows the Students t distribution
Function grand does not allow for the generation of data following the Students t distribution.
However, SCILAB provides for function cdft which lets you obtain the inverse of the cumulative
distribution. Using an approach similar to that shown above for the Weibull distribution, we
can generate random probability values through function rand, and then use function cdft to
generate the data required.
The following example illustrates the procedure:
-->getf(histnorm);getf(normplot); //Load functions histnorm & normplot
-->pp = rand(1,200); //Generate random probabilities
-->x = []; //This line and the for end
-->for j =1:200 //construct calculate values of x
--> x = [x cdft(T,6,pp(j),1-pp(j))];
-->end;
-->xmin=min(x), xmax=max(x) //Determine min & max values
xmin = 6.9441809
xmax = 3.4425429
-->xclass=[-7:1:4];xset(window,1);histnorm(x,xclass); //Histogram
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
-->xset(window,2);normplot(x); //Normal probability plot
Generating data that follows a discrete distribution
Using function grand we were able to generate discrete data that follows the binomial,
Poisson, and uniform integer distributions. In this section we present a general method for the
generation of data given a discrete distribution in the form of a table. For example, the
following table shows the probability mass function, f
x
(x) = P(X=x), and cumulative distribution
function, F
X
(x) = P(X<x), of a discrete random variable X:
Random numbers
X f
X
(x) F
X
(x) From to
0.5 0.10 0.10 0.00 0.10
1.5 0.25 0.35 0.10 0.35
2.5 0.20 0.55 0.35 0.55
3.5 0.15 0.70 0.55 0.70
4.5 0.15 0.85 0.70 0.85
5.5 0.15 1.00 0.85 1.00
The last two columns of the table represent the range of probabilities corresponding to the
cumulative distribution function for each value of X. The procedure for generating data
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
consists in obtaining a value of random probability p = P(X<x) from a uniform distribution, e.g.,
using function rand, and then assigning a value of X according to the range of values of the
random numbers. Thus, if function rand produces the random number 0.25, we assign to x the
corresponding value X = 1.5.
The following function, discrand, will generate a matrix of dimensions nm random numbers
given vectors of values of X and FX, representing the values of a discrete random variable and
its corresponding cumulative distribution function.
function [x] = discrand(n,m,xx,FX)
//A function to generate a matrix nxm
//following a discrete probability distribution
//represented by vectors xx and FX = P(X<xx)
nx = length(xx);
pp = rand(n,m);
x = zeros(n,m);
FXX = [0.00 FX];
for i = 1:n
for j = 1:m
for k = 1:nx
if pp(i,j)>FXX(k) & pp(i,j)<=FXX(k+1) then
x(i,j) = xx(k);
end;
end;
end;
end;
//end function discrand
An application of the function to generate 200 data points that follow the probability
distribution shown in the table above is presented next. We first load function discrand, then
enter the values of X and F
X
(x), and generate a row vector of 200 points. Next, we load
functions histnorm and normplot to check how well the data follows a normal distribution.
-->getf(discrand)
-->X = [0.5:1.0:5.5]; FX = [0.10,0.35,0.55,0.70,0.85,1.00];
-->x=discrand(1,200,X,FX);
-->getf(histnorm);getf(normplot);
-->xmin=min(x), xmax=max(x)
xmin =
.5
xmax =
5.5
-->xclass=[0.5:0.5:5.5];
-->histnorm(x,xclass)
ans =
24.643214
Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
-->normplot(x)
Statistical simulation
Many physical or other type of systems are described by one or more mathematical
relationships (e.g., algebraic, difference, or differential equations) of diverse degrees of
complexity. We will refer to the set of mathematical relationships that describe a physical
system as a model. A model typically depends of certain constant values known as the
parameters of the model. In the simplest of cases, a model can be represented by a black box
into which a set of input data is provided, and from which a set of output results is obtained.
This is illustrated in the following figure:
If the model is such that for a given set of input data it always produces a predictable result, it
is referred to as a deterministic model. An example of a deterministic model is the equation
Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz
that describes the electric current, I, through a resistor, R, when a voltage, V, is applied across
the terminals of the resistor. The equation is
I = V/R.
If we apply a constant voltage V
o
to the resistor, we get back a constant electric current, I
0
=
V
o
/R. If we instead apply a variable voltage V(t) = V
o
sin(t), we obtain an electric current,
I(t) = (V
o
/R)sin(t). Thus, knowing the value of the resistance R and the input to the system,
i.e., the voltage, V
0
or V(t), we can always find the value of the electric current. We cannot
get more deterministic than this example.
If the input to the model is of a random nature, or if there is a random component to the
model itself, the model is said to be probabilistic or stochastic. For example, the black-box
model described above can be used to describe a hydrological basin. The input data is the
amount and duration of the precipitation falling on the basin on a certain period of time. (A
graphical representation of precipitation vs. time is referred to as a hyetograph). This input
is, by its own nature, random or stochastic. This means that we cannot know exactly the
amount of precipitation that will occur, say, in the next 24 hours.
Although a hydrological basin is extremely more complicated than an electric resistor, the
model used to predict the runoff (output) to the system can be a simple relationship involving
one or two parameters. (A graphical representation of the runoff coming out of the basin as a
function of time is known as a hydrograph). If the input hyetograph is known, then the output
hydrograph can be obtained in a deterministic way. However, because we do not know
exactly the input hyetograph for a particular period of time, except in a statistical manner, the
model is indeed a stochastic one.
Through the keeping of historical records of precipitation in the basin we can get a good idea
of the stochastic nature of precipitation to use as input for our stochastic model. We can then
generate synthetic data representing the precipitation and use it as input to the model. This
approach to modeling physical (or economical, or other type of) systems is known as a Monte
Carlo method. (The name derives from the capital of the European principalty of Monaco, the
city of Monte Carlo, famous for its casinos, where the laws of probability are seen in action
night and day.)
Monte Carlo methods find applicability in all types of models where there is a random
component to the input or parameters of the model. Statistical modeling can be used to
model, for example, economic responses from human populations, the distribution of soil
permeabilities in an aquifer, the distribution of animal or plant populations, traffic patterns in
highways or airports, weather phenomena, etc. A simple application of a Monte Carlo method
to simulate the patterns of traffic through a service station is shown below.
Simulating traffic through a service station
Suppose we want to simulate the traffic through a service station in which only one customer
can be serviced at a time. We also assume that once a customer arrives to the service station,
he or she will not leave until service is provided. This is a simplistic model, but it could be
used to simulate a vehicle service station in a city or highway, a medical emergency room, a
highway service station for state or privately own trucks, a store, etc.
The first customer arrives at a certain arrival time, AT
1
(Arrival Time). He or she is taken care
of right away so that the starting time of service for customer 1, ST
1
(Starting Time), coincides
with his or her arrival time, thus, ST
1
= AT
1
. The waiting time for customer 1 is, therefore,
zero, i.e., WT
1
= 0. The number of customers awaiting service at this point is also zero, i.e.,
Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
NW
1
= 0. The time required to service this first customer is referred to as TS
1
(Time of
Service). The first customer leaves the service station at time ET
1
= ST
1
+ TS
1
(Ending Time).
The second customer arrives at the service station at a time AT
2
. If AT
2
< ET
1
(i.e., the second
customer arrives before service for the first one has finished), the second customer must wait
until the first customer leaves, so that ST
2
becomes ET
1
(ST
2
= ET
1
). In this case, we can
calculate a waiting time for the second customer equal to WT
2
= ET
1
- AT
2
. Also, the number of
customers waiting for service at this point is NW
2
= 1. If, instead, the second customer arrives
at a time AT
2
ET
1
, then ST
2
= AT
2
, and WT
2

= 0. In any event, the ending time for the second
customer is calculated as ET
2
= ST
2
+ TS
2
.
We define the inter-arrival time between customers 1 and 2 as IAT
1
= AT
2
- AT
1
. In general, the
inter-arrival time between customers i and i+1 is IAT
i
= AT
i+1
- AT
i
. The inter-arrival time (IAT
i
)
and the time of service (TS
i
) are considered random variables of discrete nature. Thus, IAT
i
and TS
i
constitute random input to the model.
Suppose that we want to simulate the operation of the service center for n customers, we first
generate n-1 values of inter-arrival time {IAT
1
, IAT
2
, , IAT
n-1
}, as well as n values of the
service time {TS
1
, TS
2
, , TS
n
}. Then, we proceed to calculate the arrival times as
AT
i+1
= AT
i
+ IAT
i
, i = 1, 2, , n-1.
As indicated earlier, the starting and ending times for the first customer are ST
1
= AT
1
, ET
1
=
ST
1
+ TS
1
. Also, the waiting time and number of customers waiting at this stage are both zero,
i.e., WT
1
= 0, and NW
1
= 0. The starting time for customer 2 is obtained as follows:
If AT
2
> ET
1
, then ST
2
= AT
2
, WT
2
= 0, NW
2
= 0
If AT
2
< ET
1
, then ST
2
= ET
1
, WT
2
= ET
1
- AT
2
, and NW
2
= 1.
For the third customer, we need to check the arrival time, AT
3
, against the ending times of
both the first and second customers so we can determine the starting time, the waiting time,
and the number of customers waiting at that point. The following piece of pseudo-code can
be used to determine such values:
for j = 2:n
NW
j
= 0
WT
j
= 0
for k = 1:j-1
if AT
j
< ET
k
then
NW
j
= NW
j
+ 1
WT
j
= ET
k
- AT
j
ST
j
= ET
k
else
ST
j
= AT
j
end
end
ET(
j
) = ST(
j
)+TS(
j
)
End
An user-defined function to simulate traffic through a service
station
The steps outlined above are put together in the following function, service:
function [MR] = service(IAT,TS)
Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
//Simulation of traffic in a service station
//Given n-1 values of inter-arrival time IAT
//and n values of time of service TS.
//Results:
//Arrival time = AT, Starting time = ST
//Ending time = ET, Waiting time = WT
//Number of waiting customers = NW
//
n = length(TS);
AT = zeros(1,n);
ST = zeros(1,n);
ET = zeros(1,n);
NW = zeros(1,n);
WT = zeros(1,n);
IATT = [IAT 0];
ST(1) = AT(1);
ET(1) = ST(1) + TS(1);
for j = 2:n
AT(j) = AT(j-1) + IAT(j-1);
end;
for j = 2:n
NW(j) = 0;
WT(j) = 0;
for k = 1:j-1
if AT(j) < ET(k) then
NW(j) = NW(j) + 1;
WT(j) = ET(k) - AT(j);
ST(j) = ET(k);
else
ST(j) = AT(j);
end;
end;
ET(j) = ST(j)+TS(j);
end;
disp(' ');
printf('===============================================================\n');
printf(' j AT IAT ST TS ET WT NW \n');
printf('===============================================================\n');
for j = 1:n
printf('%3.0f %8.2f %8.2f %8.2f %8.2f %8.2f %8.2f %3.0f\n',...
j,AT(j),IATT(j),ST(j),TS(j),ET(j),WT(j),NW(j));
end;
printf('===============================================================\n');
MR = [AT' IATT' ST' TS' ET' WT' NW']; //Matrix of Results
printf('AT = arrival times IAT = inter-arrival times \n');
printf('ST = starting times TS = time of service \n');
printf('ET = ending times WT = waiting times \n');
printf('NW = number of customers waiting \n');
disp(' AT IAT ST TS ET WT NW');
//end function service
As an example, suppose that we have the following inter-arrival times (IAT) and times of
service (TS):
Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz
-->IAT = [ 0.5 0.75 0.5 0.25 0.5];
-->TS = [ 1 2 1 1 2 1];
We can load function service and run it with the values of IAT and TS defined earlier to obtain
the following results:
-->Matrix_of_results = service(IAT,TS)
===============================================================
j AT IAT ST TS ET WT NW
===============================================================
1 0.00 .50 0.00 1.00 1.00 0.00 0
2 .50 .75 1.00 2.00 3.00 .50 1
3 1.25 .50 3.00 1.00 4.00 1.75 1
4 1.75 .25 4.00 1.00 5.00 2.25 2
5 2.00 .50 5.00 2.00 7.00 3.00 3
6 2.50 0.00 7.00 1.00 8.00 4.50 4
===============================================================
AT = arrival times IAT = inter-arrival times
ST = starting times TS = time of service
ET = ending times WT = waiting times
NW = number of customers waiting
AT IAT ST TS ET WT NW
Matrix_of_results =
! 0. .5 0. 1. 1. 0. 0. !
! .5 .75 1. 2. 3. .5 1. !
! 1.25 .5 3. 1. 4. 1.75 1. !
! 1.75 .25 4. 1. 5. 2.25 2. !
! 2. .5 5. 2. 7. 3. 3. !
! 2.5 0. 7. 1. 8. 4.5 4. !
The function is designed to provide a table of results, as well as a matrix summarizing the
results in case that additional operations on those results are required within SCILAB. The
function, as applied in this case, is purely deterministic in the sense that for the given input we
get a unique result. To work out a stochastic modeling of traffic through a service station we
need to provide random input. The following example shows how to obtain that random input.
Modeling traffic through a service station with random input
Suppose that the inter-arrival times and time of service for the service station model follows
the probability distributions shown in the following table:
x = IAT F
X
(x) x = TS F
X
(x)
0.1 0.05 0.25 0.10
0.2 0.10 0.50 0.20
0.3 0.20 0.75 0.40
0.4 0.35 1.00 0.70
0.5 0.45 1.25 0.80
0.6 0.50 1.50 0.90
0.7 0.70 1.75 0.95
0.8 0.75 2.00 1.00
0.9 0.95
1.0 1.00
Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
We want to analyze the traffic through the service station for 10 customers by generating 9
inter-arrival times and 10 service times from these generations. The inter-arrival times and
times of service can be generated using function discrand as follows:
-->getf('discrand')
-->xIAT = [0.1:0.1:1.0]; FIAT = [0.05,0.1,0.2,0.35,0.45,0.5,0.7,0.75,0.95,1.0];
-->xTS = [0.25:0.25:2]; FTS = [0.1,0.2,0.4,0.7,0.8,0.9,0.95,1];
-->IAT = discrand(1,9,xIAT,FIAT) //generate IAT data
IAT =
! .4 .7 .7 .5 .4 .7 .5 .9 .1 !
-->TS = discrand(1,10,xTS,FTS) //generate TS data
TS =
! 1. .75 1. .75 .5 1.25 .75 .5 1. .5 !
With these values of IAT and ST we now call function service:
-->M = service(IAT,TS)
===============================================================
j AT IAT ST TS ET WT NW
===============================================================
1 0.00 .40 0.00 1.00 1.00 0.00 0
2 .40 .70 1.00 .75 1.75 .60 1
3 1.10 .70 1.75 1.00 2.75 .65 1
4 1.80 .50 2.75 .75 3.50 .95 1
5 2.30 .40 3.50 .50 4.00 1.20 2
6 2.70 .70 4.00 1.25 5.25 1.30 3
7 3.40 .50 5.25 .75 6.00 1.85 3
8 3.90 .90 6.00 .50 6.50 2.10 3
9 4.80 .10 6.50 1.00 7.50 1.70 3
10 4.90 0.00 7.50 .50 8.00 2.60 4
===============================================================
AT = arrival times IAT = inter-arrival times
ST = starting times TS = time of service
ET = ending times WT = waiting times
NW = number of customers waiting
AT IAT ST TS ET WT NW
M =
! 0. .4 0. 1. 1. 0. 0. !
! .4 .7 1. .75 1.75 .6 1. !
! 1.1 .7 1.75 1. 2.75 .65 1. !
! 1.8 .5 2.75 .75 3.5 .95 1. !
! 2.3 .4 3.5 .5 4. 1.2 2. !
! 2.7 .7 4. 1.25 5.25 1.3 3. !
! 3.4 .5 5.25 .75 6. 1.85 3. !
! 3.9 .9 6. .5 6.5 2.1 3. !
! 4.8 .1 6.5 1. 7.5 1.7 3. !
! 4.9 0. 7.5 .5 8. 2.6 4. !
Out of the matrix of results, M, we can extract individual columns of data, for example, the
waiting time data corresponds to the sixth column of M:
Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz
-->WT = M(:,6)
WT =
! 0. !
! .6 !
! .65 !
! .95 !
! 1.2 !
! 1.3 !
! 1.85 !
! 2.1 !
! 1.7 !
! 2.6 !
The number of waiting customers is extracted from the seventh column of matrix M:
-->NW = M(:,7)
NW =
! 0. !
! 1. !
! 1. !
! 1. !
! 2. !
! 3. !
! 3. !
! 3. !
! 3. !
! 4. !
The columns of data extracted from the matrix of results, M, can be used to obtain statistics
such as the mean and standard deviation:
-->WT_mean = mean(WT), WT_sdev = st_deviation(WT)
WT_mean = 1.295
WT_sdev = .7836701
-->NW_mean = mean(NW), NW_sdev = st_deviation(NW)
NW_mean = 2.1
NW_sdev = 1.2866839
We can also function normplot to check how close the data is to normality:
-->getf('normplot')
-->normplot(NW')
Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz
-->normplot(WT')
STIXBOX: a rudimentary statistics toolbox
STIXBOX (an abbreviation of statistical toolbox) is a collection of functions that perform
selected statistical and probability calculations. STIXBOX is available for download from the
SCILAB main web page (http://www-rocq.inria.fr/SCILAB/). Instructions for its installation are
provided with the downloaded functions. The package includes a set of help manual pages
that briefly describe the operation of the functions. Once loaded, the manual pages are
available through the main SCILAB Help window.
Probability mass and probability density functions
Probability mass functions or pmf (for discrete random variables) and probability density
functions of pdf (for continuous random variables) start with the letter d, e.g., dbeta, dbinom,
etc. Mass distribution functions are referred to by p
X
(k) = P[X=k], and probability density
functions by f
X
(x). Thus, if X ~ Binomial(n,p) with n = 10, p = 0.5, P[X=2] = p
X
(2) =
dbinom(2,10,0.5). And, if X ~ Normal(,
2
) with = 1.5, = 0.2, then f
X
(1.75) =
dnorm(1.75,1.5,0.2). The following probability mass and density functions are defined:
dbeta the beta density function
dbinom the binomial probability function
dchisq the chisquare density function
df The F density function [modified by the author, 2/1/2001]
dgamma the gamma density function
dhypgeo the hypergeometric probability function
dnorm the normal density function [modified by the author, 2/1/2001]
dt the student t density function
Cumulative distribution functions
Cumulative distribution functions (cdf) are referred to as distribution functions if dealing with
continuous variable, or as cumulative probability function if dealing with discrete variables.
All cdfs in this package start with a p: pbeta, pbinom, etc. Both, discrete and continuous cdfs
are referred to by F
X
(x) = P[Xx]. Thus, if X ~ Binomial(n,p) with n = 10, p = 0.5, P[X2] =
Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz
F
X
(2) = pbinom(2,10,0.5). And, if X ~ Normal(,
2
) with = 1.5, = 0.2, then F
X
(1.75) =
pnorm(1.75,1.5,0.2). The following cumulative distribution functions are defined:
pbeta the beta distribution function
pbinom the binomial cumulative probability function
pchisq the chisquare distribution function
pf The F distribution function
pgamma the gamma distribution function
phypge the hypergeometric cumulative probability function
pnorm the normal distribution function
pt the student t cdf (modified by the author, 2/1/2001)
Inverse cumulative distribution functions
Inverse cumulative distribution functions start with q: qbeta, qbinom, etc. . If F
X
(q) = P[Xq] =
p, then q = F
X
-1
(p). The value q is also referred to as a quantile of the distribution. The
following inverse cumulative distribution functions are defined:
qbeta the beta inverse distribution function
qbinom the binomial inverse cdf
qchisq the chisquare inverse distribution function
qf The F inverse distribution function
qgamma the gamma inverse distribution function
qhypg the hypergeometric inverse cdf
qnorm the normal inverse distribution function
qt the student t inverse distribution function
quantile empirical quantile (percentile).
Generating synthetic data
The generation of synthetic data that follows a particular distribution can be accomplished
with the following random number generators. The name of the random generator functions
begins with r: rbeta, rbinom, etc. Maple already provides function rand that produces
uniformly distributed random numbers (use help rand for more information). The functions
provided by STIXBOX generates random numbers that follow the distributions suggested by the
names of the functions. Thus, if you want to generate n = 10 data values x that follow the
normal distribution, with = 0.5, and
lnX
= 0.1, use rnorm(10,0.5,0.1).
rbeta random numbers from the beta distribution
rbinom random numbers from the binomial distribution
rchisq random numbers from the chisquare distribution
rexpweib random numbers from the exponential or weibull distributions
rf random numbers from the F distribution
rgamma random numbers from the gamma distribution
rgeom random numbers from the geometric distribution
rhypg random numbers from the hypergeometric distribution
rjbinom random numbers from the binomial distribution (reject method)
rjgamma generates gamma random deviates (reject method)
rjpoiss random numbers from the poisson distribution (reject method)
rnorm normal random numbers
rjpoiss random numbers from the poisson distribution (renewal method)
Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz
rt random numbers from the student t distribution
Logistic regression
These functions involve the logistic population growth model (see, for example, Example 8.3,
page 504, in Kottegoda, N.T. and R. Rosso, 1997, Probability, Statistics, and Reliability for
Civil and Environmental Engineers, The McGraw-Hill Companies, Inc., New York).
lodds log odds function.
loddsinv compute the inverse of log odds.
logitfit fit a logistic regression model.
Statistical graphics
Functions to produce a variety of statistical graphics. A normal probability paper plot is
obtained by using qqnorm. Probability paper plots are also referred to as Q-Q plots. For that
reason the corresponding function names start with qq, e.g., qqgamma, qqnorm, etc. Also of
interest are functions histo, plotsym.
histo plot a histogram
identify identify points on a plot by clicking with the mouse.
pairs pairwise scatter plots (does not work)
plotdens draw a nonparametric density estimate.
plotsym plot with symbols
qqnorm normal probability paper
qqplot plot empirical quantile vs empirical quantile
Binomial coefficients
bincoef calculates binomial coefficients: (
n

r
) = n!/(r!(n-r)!),
Resampling methods
These methods apply to the process of resampling by which an attempt is made to remove any
existing bias in the sample. For a quick introduction to jackknife (named so because the
jackknife, like this method, is an useful tool) and the bootstrap (named so from the expression
"lifting oneself by one's bootstraps"), see pp. 116-117 in Kottegoda, N.T. and R. Rosso, 1997,
Probability, Statistics, and Reliability for Civil and Environmental Engineers, The McGraw-Hill
Companies, Inc., New York.
covboot bootstrap estimate of the variance of a parameter estimate.
covjack Jackknife estimate of the variance of a parameter estimate.
stdboot bootstrap estimate of the parameter standard deviation.
stdjack Jackknife estimate of the standard deviation of a parameter.
rboot simulate a bootstrap resample from a sample.
ciboot various bootstrap confidence interval.
test1b bootstrap t test and confidence interval for the mean.
Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz
Tests, confidence intervals, and model estimation
These are functions related to statistical inference. Of interest for this class are the functions
lsfit, testln, and test2r. Use the help function to obtain additional information on the
functions.
cmpmod compare small linear model versus large one
ciquant nonparametric confidence interval for quantile
kstwo Kolmogorov-Smirnov statistic from two samples (needs function pks)
linreg linear or polynomial regression
lsfit fit a multiple regression model.
lsselect select a predictor subset for regression
test1n tests and confidence intervals based on a normal sample
test1r test for median equals 0 using rank test
test2n tests and confidence intervals based on two normal samples
test2r test for equal location of two samples using rank test
Stixbox demonstrations
These are SCILAB functions that demonstrate some of the functions contained in STIXBOX
stixdemo demonstrate various stixbox routines.
stixtest a second demo for stixbox
Famous datasets
Function getdata is used to load well-known datasets into the SCILAB environment. The data
sets included are:
1 Phosphorus Data
2 Scottish Hill Race Data
3 Salary Survey Data
4 Health Club Data
5 Brain and Body Weight Data
6 Cement Data
7 Colon Cancer Data
8 Growth Data
9 Consumption Function
10 Cost-of-Living Data
11 Demographic Data
To activate function getdata and load data into variable x use:
--> x = getdata()
This function produces a dialog box displaying the list of data sets. The user can type in the
number of the data set and get back some information about the data set before the set is
loaded. The dialog box produced by getdata() is shown below.
Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz
The dialog box shows that we have selected data set number 5. Pressing [OK] will load the
data as well as provide information as shown below.
Examples on probability distributions using STIXBOX
!Plot of the standard normal distribution:
-->z=-4:0.1:4;phi=dnorm(z,0,1);plot(z,phi,'z','phi(z)','standard normal')
Download at InfoClearinghouse.com 68 2001 Gilberto E. Urroz
!Plot of the Student-t distribution for = 2, 5, 10, 15, 20
-->t=-4.0:0.1:4;nu=[2,5,10,15,20];
-->for k=1:5,f=dt(t,nu(k));plot2d(t,f,k,'011',' ',[-4 0 4 0.4]), end
-->xtitle('Student t distribution','t','f(t)')
!Plot of the chi-square distribution for nu=5
-->x=0:0.1:20;nu=5;f=dchisq(x,nu);
-->plot(x,f,'x','f(x)','Chi-square distribution, nu=5')
!Plot the F distribution for nu1=5 and nu2=10:
-->x=0:0.1:5;nu1=5;nu2=10;f=df(x,nu1,nu2);
-->plot(x,f,'F','f(F)','F distribution, nu1=5, nu=10')
Download at InfoClearinghouse.com 69 2001 Gilberto E. Urroz
!Determining z

, such that P(Z>z

) > , or P(Z<z

) > 1- . Also, z
/2 is such that P(Z>z
/2
) >
/2, or P(Z<z
/2
) > 1- /2:
-->alpha = 0.05; z_alpha=qnorm(1-alpha), z_alpha2=qnorm(1-alpha/2)
z_alpha = 1.6448536
z_alpha2 = 1.959964
!Determining t
,,
such that P(T>t

) > , or P(T<t

) > 1- . Also t
,/2 is such that P(T>t
/2
) >
/2, or P(T<t
/2
) > 1- /2:
-->nu=10;alpha=0.01;t_alpha=qt(1-alpha,nu),t_alpha2=qt(1-alpha/2,nu)
t_alpha = 2.7637695
t_alpha2 = 3.1692727
!Determining
2
,
, such that P(X
2
>
2

) > , or P(X
2
>
2

) > 1- . Similar definitions are used


to calculate the values
2
,1
,
2
,/2
,
2
,1/2:
-->nu=6;alpha=0.10;X_alpha=qchisq(1-alpha,nu)
X_alpha = 10.644641
-->X_alpha2=qchisq(1-alpha/2,nu)
X_alpha2 = 12.591587
-->nu=6;alpha=0.10;X_alpha=qchisq(alpha,nu)
X_alpha = 2.2041307
-->X_alpha2=qchisq(alpha/2,nu)
X_alpha2 = 1.6353829
!Generating 20 data points that follow the Weibull distribution, and producing a normal
probability plot for such data:
-->x = rexpweib(20,3,5); qqnorm(x,'o')
Download at InfoClearinghouse.com 70 2001 Gilberto E. Urroz
!Generating 200 data points that follow the binomial distribution. A histogram of the data is
then produced.
-->x = rbinom(200,10,0.35); histo(x);
Other options for function histo( ),using 8 suggested classes (or bins). Parameter odd = 0. The
function histo( )chooses 6 classes:
-->histo(x,8,0)
Download at InfoClearinghouse.com 71 2001 Gilberto E. Urroz
In the next call, we suggest 15 classes, and the odd parameter takes a value odd = 1:
-->histo(x,15,1)
The next call scales area in the histogram bars so that the total area is equal to 1:
-->histo(x,8,0,1)
Download at InfoClearinghouse.com 72 2001 Gilberto E. Urroz
Exercises
[1]. The probability of a flood occurring in a particular section of a river in a given month is
estimated, form existing records, to be 0.15. (a) What is the probability that there will be
three months of flood in the next year. (b) What is the probability that there will be less than
6 months of flood in the next year.
[2]. Data kept at an airport shows an average of five cars per minute stopping to leave or pick
up passengers in the terminal curb. (a) What is the probability that in the next minute there
will be 10 or more cars stopping at the curb? (b) What is the probability that there will be no
cars at the curb in a given minute.
[3] It is known that 25 out of a batch of 200 concrete cylinders were prepared using a defective
type of cement. If a laboratory receives a sample of 15 of those cylinders, what is the
probability that the sample will contain 5 of the defective cylinders?
[4]. If a factory is known to produce 5% defective truck tires, what is the probability that in a
given assembly line the first defective tire is detected after 20 tires have come out of the
assembly line? What is the probability that the first defective tire is detected after 10 tires
have come out of the assembly line?
[5]. The time required to finish the construction of a mile of a particular highway is known to
have a normal distribution with a mean value of 3.5 days and a standard deviation of 0.5 days.
What is the probability that the next mile in the road will be completed between 3 and 5 days?
What is the probability that the construction of the next mile of the road will take more than 7
days?
[6]. Let X represent the intensity of an earthquake in a particular scale. If X is modeled using
the exponential distribution with parameter = 6.5, determine the probability that the
intensity of the next earthquake will be 3.5 or less. Also, determine the probability that the
intensity of the earthquake will be between 2.5 and 4.5.
[7]. The gamma distribution, with parameters =1.2, and = 0.5, is used to model the time
of failure (in hours) of an electronic component. Determine the probability that a particular
component will last 100 hours or more. Determine the probability that the component will last
less than 2 hours.
[8]. If the wind velocity in miles per hour near a harbor is assumed to follow a Weibull
distribution with parameters = 2 and = 3, determine the probability of the wind velocity
being between 15 and 75 mph. Also, determine the probability of the wind velocity being
larger than 10 mph.
[9]. For a large value of n, the Binomial distribution can be approximated by the normal
distribution with parameters = np, = np(1-p). Suppose that you receive a shipment of
1000 resistors produced by a machine that is know to produce 0.5% defective resistors. What is
the probability that there will be more than 200 defective resistors in the shipment by using:
(a) the normal distribution approximation to the Binomial distribution, and (b) the Poisson
distribution to the Binomial distribution.
[10]. Plot the probability mass function, f
X
(x), and the cumulative distribution function, F
X
(x),
for the following discrete distributions:
(a) Binomial with n = 20, p = 0.25 (b) Binomial with n = 20, p = 0.50
(c) Binomial with n = 20, p = 0.75 (d) Poisson with = 5.0, plot for x = 0,1,2,10
(e) Geometric with p = 0.25, for x = 1,2,,10 (f) Geometric with p = 0.50, for x = 1,2,,10
Download at InfoClearinghouse.com 73 2001 Gilberto E. Urroz
(g) Geometric with p = 0.75, for x = 1,2,,10 (h) Hypergeometric with N=100,n=20,a=40
(i) Hypergeometric with N=40, n = 10, a = 20 (j) Hypergeometric with N = 120,n = 80,a = 10
[11]. Let X be a discrete random variable that follows the binomial distribution with
parameters n and p. Let P
0
= P(X x). Calculate:
(a) P
0
given n = 20, p = 0.35, x = 5 (b) n given p = 0.25, x = 8, P
0
= 0.80
(c) p given n = 25, x = 20, P
0
= 0.75 (d) x given n = 10, p = 0.80, P
0
= 0.30
[12]. Plot the probability density function, f
X
(x), and the cumulative distribution function,
F
X
(x), for the following continuous distributions:
(a) Gamma with = 0.5, = 1.5 (b) Gamma with = 2, = 3
(c) Beta with = 0.5, = 1.5 (d) Beta with = 3, = 2
(e) Weibull with = 0.5, = 1.5 (f) Weibull with = 2, = 2
(g) Uniform with a = 2, b = 6 (h) Uniform with a = -3, b = 3
(i) Exponential with = 12.5 (j) Exponential with = 4.8
(k) Normal with = 5, = 5 (l) Normal with = 150, = 25
(m) Student t with = 4 (n) Student t with = 12
(o) Chi-square with = 4 (p) Chi-square with = 12
(q) F distribution with
N
= 4,
D
= 10 (r) F distribution with
N
= 4,
D
= 10
[13]. Let X be a continuous random variable that follows the Gamma probability distribution
with parameters and . Let P
0
= P(X x). Calculate:
(a) P
0
given = 2, = 3, x = 3.5 (b) given P
0
= 0.40, = 1.5, x = 1.2
(c) given P
0
= 0.60, = 5, x = 10.5 (d) x given P
0
= 0.20, = 10.5, = 0.3
[14]. Let X be a continuous random variable that follows the Beta probability distribution with
parameters and . Let P
0
= P(X x). Calculate:
(a) P
0
given = 2, = 3.5, x = 0.35 (b) given P
0
= 0.40, = 2.3, x = 0.76
(c) given P
0
= 0.60, = 2.5, x = 0.45 (d) x given P
0
= 0.20, = 10.5, = 0.3
[15]. Let T be a continuous random variable that follows Student t distribution with degrees
of freedom. Let P
0
= P(T t). Calculate:
(a) P
0
given = 10, t = 1.5
(b) given P
0
= 0.40, t = -0.8
(c) t given P
0
= 0.20, 8
[16]. Let
2
be a continuous random variable that follows the chi-square distribution with
degrees of freedom. Let P
0
= P(
2

2
). Calculate:
(d) P
0
given = 6,
2
= 2.25
(e) given P
0
= 0.40,
2
= -0.8
(f)
2
given P
0
= 0.20, 12
[17]. Let F be a continuous random variable that follows the F distribution with
N
degrees of
freedom in the numerator and
D
degrees of freedom in the denominator. Let P
0
= P(F F).
Calculate:
(a) P
0
given
N
= 4,
D
= 10, F = 2.5 (b)
N
given P
0
= 0.40,
D
= 15, F = 3.2
(c)
D
given P
0
= 0.60,
N
= 3, F = 0.45 (d) F given P
0
= 0.20,
N
= 8,
D
= 12
Download at InfoClearinghouse.com 74 2001 Gilberto E. Urroz
[18]. The following data represent measurements of the diameter of a cylinder produced for a
precision mechanism:
232. 248. 242. 250. 239. 244. 265. 262. 259. 236.
246. 308. 221. 275. 261. 217. 260. 273. 228. 269.
260. 247. 228. 274. 205. 254. 230. 252. 263. 255.
244. 264. 243. 255. 261. 236. 226. 264. 260. 265.
267. 243. 270. 275. 260. 281. 240. 257. 268. 231.
(a) Use function histnorm with a suitable number of classes to plot a histogram of the data as
well as the corresponding normal curve. (b) Use function normplot to produce a normal
probability plot of the data. (c) Based on these two plots, how well do the data follow the
normal distribution?
[19]. The following data set represents the time to failure, in years, of light bulbs.
1.39 1.07 3.22 3.67 .55 .81 1.22 1.26 .05 1.54
.97 1.01 .44 1.97 1.9 .89 3.25 .85 1.04 .43
1.33 .82 2.04 1.02 .53 .13 2.06 2.96 1.96 1.5
3.05 .42 1.17 1.72 2.68 .56 2.13 1.56 2.09 1.26
3.21 .74 3.04 2.74 .83 .79 1.56 1.55 .96 1.23
(a) Use function histnorm with a suitable number of classes to plot a histogram of the data as
well as the corresponding normal curve. (b) Use function normplot to produce a normal
probability plot of the data. (c) Based on these two plots, how well do the data follow the
normal distribution?
[20]. The following data set represents the yearly rainfall depth, in mm, recorded at a certain
location:
126. 82.9 41.5 4.35 346. 102. 830. 12.8 366. 471.
408. 189. 646. 7.82 313. 17.4 165. 24.5 32.6 39.3
277. 13.7 52.3 171. 314. 60.6 29.1 468. 887. 44.5
135. 215. 106. 201. 51. 43. 335. 59.4 174. 870.
(a) Use function histnorm with a suitable number of classes to plot a histogram of the data as
well as the corresponding normal curve. (b) Use function normplot to produce a normal
probability plot of the data. (c) Based on these two plots, how well do the data follow the
normal distribution?
[21]. The following data set represents the number of vehicles stopping at a service station in a
given hour:
3. 5. 6. 4. 5. 9. 4. 4. 11. 4.
4. 8. 5. 4. 4. 6. 7. 4. 7. 8.
6. 9. 10. 7. 4. 3. 5. 9. 9. 11.
6. 5. 9. 12. 11. 5. 13. 8. 10. 6.
4. 5. 9. 8. 7. 5. 3. 6. 5. 5.
8. 3. 11. 4. 5. 9. 5. 1. 8. 6.
(a) Use function histnorm with a suitable number of classes to plot a histogram of the data as
well as the corresponding normal curve. (b) Use function normplot to produce a normal
probability plot of the data. (c) Based on these two plots, how well do the data follow the
normal distribution?
[22]. Generate data sets consisting of k values that follow the indicated distribution with the
parameters listed below. Use functions histnorm and normplot to produce a histogram and a
Download at InfoClearinghouse.com 75 2001 Gilberto E. Urroz
normal probability plot of the data. How well do the data thus generated follow the normal
distribution based on the histogram and probability plot?
(a) Binomial, k = 200, n = 30, p = 0.7
(b) Poisson, k = 300, = 14.5
(c) Beta, k = 150, =3.5, = 5.2
(d) Gamma, k = 100, =3.5, = 5.2
(e) Exponential, k = 500, = 5.75
(f) Normal, k=180, = 5.75, = 1.2
(g) Chi-square, k = 230, = 5
(h) F-distribution, k = 350,
N
= 5,
D
= 5
(i) Uniform integer, k = 125, a = -50, b = 50
(j) Uniform real, k = 200, a = 5.5, b = 17.5
(k) Weibull, k = 200, =7.2, = 2.1
(l) Students t, k = 150, = 12
(m) Log-normal, k = 200,
ln(X)
= 1.2,
ln(x)
= 0.5
[23]. Generate data sets consisting of 250 values that follow the discrete distribution
described by the following probability mass function:
x 1.2 2.3 4.1 5.2 6.1 7.2 8.4 9.3 11.1
f
X
(x) 0.04 0.08 0.12 0.16 0.08 0.04 0.20 0.24 0.04
Use functions histnorm and normplot to produce a histogram and a normal probability plot of
the data. How well do the data thus generated follow the normal distribution based on the
histogram and probability plot?
[24]. Function service was developed to simulate the traffic through a service station. Use
function service to produce a simulation of traffic through a service station that takes as input
50 values of the inter-arrival time (IAT) and 50 values of the time of service (TS) generated out
of the following cumulative distribution functions:
x=IAT F
X
(x) x=TS F
X
(x)
0.2 0.03 0.4 0.05
0.4 0.14 0.8 0.15
0.6 0.08 1.2 0.35
0.8 0.12 1.6 0.25
1.0 0.23 2.0 0.15
1.2 0.10 2.4 0.05
1.4 0.05
1.6 0.05
1.8 0.10
2.0 0.10
Use functions histnorm and normplot to produce a histogram and a normal probability plot of
the waiting time (WT) and number of customers waiting (NW). How well do the WT and NW
data follow the normal distribution?
[25]. One-dimensional random walk. Consider a particle that moves along a straight line
subject to a random motion. The particle starts at x
1
= 0 and moves to position x
2
= x
1
+ x
1
,
where x
1
is a random number. The next position of the particle is x
3
= x
2
+ x
2
, where x
2
is a
second random number. Subsequent positions of the particle are given by x
k+1
= x
k
+ x
k
. The
Download at InfoClearinghouse.com 76 2001 Gilberto E. Urroz
random numbers used must include both positive and negative values so that the particle can
move forward and backward.
(a) Plot the position x
k
vs. k for a one-dimensional random walk that involves 300
displacements x
k
generated from a normal distribution with = 0 and = 1.
(b) Plot the position x
k
vs. k for a one-dimensional random walk that involves 300
displacements x
k
generated from a uniform distribution between -1 and 1.
[26]. Two-dimensional random walk. A two-dimensional random walk involves the displacement
of a particle from a point (x
k
,y
k
) to a point (x
k+1
,y
k+1
) so that
x
k+1
= x
k
+ r
k
cos(
k
), and x
k+1
= x
k
+ r
k
sin(
k
),
where the values r
k
and
k
are random numbers.
(a) Plot the two-dimensional random walk that results form 200 values of r
k
with a normal
distribution with mean = 1 and standard deviation = 0.2, and 200 values of
k
uniformly distributed between 0 and 2.
(b) Plot the two-dimensional random walk that results form 100 values of r
k
with a Weibull
distribution with parameters = 2 and = 3, and 200 values of
k
uniformly distributed
between 0 and 2.
(c) Plot the two-dimensional random walk that results form 150 values of r
k
with a Gamma
distribution with parameters = 0.2 and = 1.3, and 200 values of
k
normally
distributed with mean = and standard deviation /2.
(d) Plot the two-dimensional random walk that results form 250 values of r
k
with a Beta
distribution with parameters = 2 and = 3, and 200 values of
k
uniformly distributed
between 0 and 2.
[27]. The following table shows the annual maximum flow for the Ganga River in India
measured at specific station.
Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s)
1885 7241 1907 7546 1929 4545 1951 4458
1886 9164 1908 11504 1930 5998 1952 3919
1887 7407 1909 8335 1931 3470 1953 5470
1888 6870 1910 15077 1932 6155 1954 5978
1889 9855 1911 6493 1933 5267 1955 4644
1890 11887 1912 8335 1934 6193 1956 6381
1891 8827 1913 3579 1935 5289 1957 4548
1892 7546 1914 9299 1936 3320 1958 4056
1893 8498 1915 7407 1937 3232 1959 4493
1894 16757 1916 4726 1938 3525 1960 3884
1895 9680 1917 8416 1939 2341 1961 4855
1896 14336 1918 4668 1940 2429 1962 5760
1897 8174 1919 6296 1941 3154 1963 9192
1898 8953 1920 8174 1942 6650 1964 3024
1899 7546 1921 9079 1943 4442 1965 2509
Download at InfoClearinghouse.com 77 2001 Gilberto E. Urroz
1900 6652 1922 7407 1944 4229 1966 4741
1901 11409 1923 5482 1945 5101 1967 5919
1902 9164 1924 19136 1946 4629 1968 3789
1903 7404 1925 9680 1947 4345 1969 4546
1904 8579 1926 3698 1948 4890 1970 3842
1905 9362 1927 7241 1949 3619 1971 4542
1906 7092 1928 3698 1950 5899
(a) Use function histnorm with a suitable number of classes to plot a histogram of the data
as well as the corresponding normal curve.
(b) Use function normplot to produce a normal probability plot of the data.
(c) Based on these two plots, how well do the data follow the normal distribution?
The following problems require that you load the functions from the Stixbox SCILAB
toolbox.
[28]. Using function getdata() load data set number 1, described as:
__________________________________________________________________________________
************************ Phosphorus Data **********************************
Source: Snedecor, G. W. and Cochran, W. G. (1967),Statistical Methods,
(6 Edition), Iowa State University, Ames, Iowa, p. 384.
Taken From: Chatterjee and Hadi (1988), p. 82.
Dimension: 18 observations on 3 variables
Description: An investigation of the source from which corn plants obtain
their phosphorus was carried out. Concentrations of phosphorus
in parts per millions in each of 18 soils were measured.
Column Description
1 Concentrations of inorganic phosphorus in the soil
2 Concentrations of organic phosphorus in the soil
3 Phosphorus content of corn grown in the soil at 20 degrees C
__________________________________________________________________________________
(a) Separate the three columns of data into vectors x, y, and z, and use the user-defined
function describe to obtain statistics of each of the columns of data.
(b) Use Stixbox function histo to obtain histograms of each of the columns of data.
(c) Use Stixbox function qqnorm to obtain a normal probability plot of each of the data
columns.
[29].Using function getdata() load data set number 1, described as:
*********************** Scottish Hill Race Data *************************
(...lines removed...)
Column Definition
1 Distance (miles)
2 Climb (ft)
3 Time (seconds)
__________________________________________________________________________________
Download at InfoClearinghouse.com 78 2001 Gilberto E. Urroz
(d) Separate the three columns of data into vectors x, y, and z, and use the user-defined
function describe to obtain statistics of each of the columns of data.
(e) Use Stixbox function histo to obtain histograms of each of the columns of data.
(f) Use Stixbox function qqnorm to obtain a normal probability plot of each of the data
columns.
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Download at InfoClearinghouse.com 79 2001 Gilberto E. Urroz
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Statistical Inference with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
All rights reserved
STATISTICAL INFERENCE 3
Definitions 3
Estimation of Confidence Intervals 3
Sampling distribution of the mean 4
Examples of confidence intervals for the mean 5
Confidence interval for proportions 6
Example of proportion confidence interval for a large sample 7
Example of proportion confidence interval for a small sample 7
Sampling Distribution of Differences and Sums of Statistics 8
Confidence intervals 9
Interval Estimation for the Variance 10
Hypothesis Testing 12
Procedure for hypothesis testing 12
Errors in hypothesis testing 13
Power of hypothesis testing 13
Selecting the values of and 14
Hypothesis testing involving mean values 14
Hypothesis testing on one mean 14
Case I: Knowing , or large sample if unknown 14
Case II: Small sample with unknown 15
A function to perform hypothesis testing on one mean 16
Examples of hypothesis testing on one mean 18
Hypothesis testing on one proportion 21
Examples of hypothesis testing on one proportion 22
Hypothesis testing on two means 23
Testing the difference between two means using known variances 23
Testing the differences between two means when the variances are unknown but equal 23
Testing the difference between two means when the variances are unknown and unequal 24
A user-defined SCILAB function for hypothesis testing on two means 24
Examples of application of function htestmu2 26
Testing the difference between two proportions 33
A function for hypothesis testing on two proportions 34
Examples of application of function htestmu2 35
Characteristic and power equations 35
Hypothesis testing on one variance 37
A function for hypothesis testing on one variance 38
Examples of application of function htestsigma1 39
Hypothesis testing on two variances 40
A function for hypothesis testing with two variances 41
Examples of application of function htestsigma2 43
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
All rights reserved
Chi-square criteria for goodness of fitting 48
Examples of goodness-of-fitting for the normal distribution 49
Examples of goodness-of-fitting for the beta distribution 50
Chi-square criteria for R C tables 58
Exercises 60
REFERENCES (FOR ALL SCILAB DOCUMENTS AT
INFOCLEARINGHOUSE.COM) 66
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
All rights reserved
Statistical Inference
Statistical inference involves the analysis of estimators of population parameters based on the
statistics of samples, as well as the testing of hypotheses about those parameters. In this
chapter we define point estimators and learn how to produce confidence intervals about those
point estimators. We also introduce hypothesis testing on one or two means, and on one or
two variances. Finally, we present some applications of the Chi-square distribution for
statistical inference.
Definitions
A population constitutes the collection of all conceivable results of a random process, while a
sample is a sub-set of a population. Typically, it is very difficult or impractical to evaluate the
entire population for a given parameter. Therefore, we select one or more samples out of the
population to analyze. In order for the sample to be representative of the population, it must
be random, i.e., each element of the sample should have the same probability of being chosen.
If such condition is not fulfilled, the sample is said to be biased, and the information obtained
from such a sample will most likely be useless in estimating population parameters.
In Chapter we introduced the concept of random variables and their probability distributions.
A measurement on a given population follows a given probability distribution. If the
distribution depends on a parameter , a random sample of observations { X
1
, X
2
, , X
n
} of size
n can be used to estimate . Each observation X
1
, X
2
, , X
n
, represents a random variable.
The joint probability distribution of the n observations is referred to as a sampling distribution.
A statistic of a sample is a function of the observations that does not contain any unknown
parameter, e.g., the mean of the sample. Statistics of a sample provide means of estimating
parameters of the population from which the sample originated. Thus, a single value of a given
sample statistic, say

, constitutes a point estimator of the corresponding population


parameter, . A confidence interval is an interval that contains the parameter at a certain
level of probability.
Estimation of Confidence Intervals
A confidence interval is determined by two statistics, C
l
and C
u
, which define an interval
containing the parameter with a certain level of probability. The end points of the interval
are known as confidence limits, and the interval (C
l
,C
u
) is known as the confidence interval.
Let (C
l
,C
u
) be a confidence interval containing an unknown parameter . The confidence level
or confidence coefficient is the quantity (1- ), where 0 < < 1, such that Pr[ C
l
<

< C
u
]
= 1 -

. This relationship defines two-sided confidence limits. A lower one-sided confidence
interval is defined by Pr[ C
l
<

] = 1 -

. An upper one-sided confidence interval is


defined by Pr[

< C
u
] = 1 -

. Typical values of

are 0.01, 0.05, 0.1, corresponding to


confidence levels of 0.99, 0.95, and 0.90, respectively.
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
All rights reserved
Sampling distribution of the mean
Let be the mean of a random sample of size
n
drawn from a population with known
standard deviation

. The 100(1-

) % [i.e., 99%, 95%, 90%, etc.] central two-sided


confidence interval for the population mean

is ( - z

n
, + z

n
), where
z

2
is a standard normal variate that is exceeded with a probability of

2
. The location of
the value z
/2
is illustrated in the figure below using the plot of the standard normal probability
density function.
The standard error of the sample mean, , i s =

n
. The one-sided upper and
lower 100(1-

) % confidence limits for the population mean

are, respectively:
+ z

n
and - z

n
.
The previous result assumes that the standard deviation of the population, , is known. If the
population standard deviation is not known the sample mean follows the Students t
distribution with
= n 1
degrees of freedom where
n
is the size of a random sample. If n
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
All rights reserved
> 30 then the Student t distribution can be approximated by the standard normal distribution,
( ) z
. A sample with size n>30 is called a large sample.
Let and S be the mean and standard deviation of a random sample of size
n
drawn from a
population that follows the normal distribution with unknown standard deviation

. The
100(1-

) % [i.e., 99%, 95%, 90%, etc.] central two-sided confidence interval for the population
mean

is
), , (
2
, 1
2
, 1
n
s
t X
n
s
t X
n n
+


where t
, n 1

2
is Student's t variate with
n 1
degrees of freedom and probability

2
of
excedence.
The one-sided upper and lower 100(1-

) % confidence limits for the population mean

are,
respectively, as follows:
. and ,
2
, 1
2
, 1
n
s
t X
n
s
t X
n n
+


Examples of confidence intervals for the mean
Example 1 - Known population variance. A sample of 25 fuses is used to determine the electric
current at which the fuse fails. The average current for the 25 fuses is calculated to be 180.5
mA. If the sample is known to come from a factory such that the standard deviation of the
current at failure point is 5 mA, determine the 95% confidence interval for the mean value of
the electric current.
The data provided is translated as n = 25,X = 180.5, = 5, = 0.05. To calculate the
confidence interval we need to find z
/2
from P(Z>z
/2
) = /2, or P(Z>z
/2
) = 1 - /2, where Z ~
Normal(0,1), i.e., the standard normal distribution. To calculate z
/2
we can use SCILABs own
cdfnor function with the following call:
z_alpha_2 = cdfnor(X,0,1,1-alpha/2,alpha/2)
or, if you have the statistical toolbox STIXBOX available, you can use:
z_alpha_2 = qnorm(1-alpha/2,0,1)
The SCILAB calculations for this problem will proceed as follows:
-->n=25;xbar=180.5;sigma=5;alpha=0.05;
-->z_alpha_2=cdfnor('X',0,1,1-alpha/2,alpha/2)
z_alpha_2 = 1.959964
-->CL=xbar-z_alpha_2*sigma/sqrt(n)
CL = 178.54004
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
All rights reserved
-->CU=xbar+z_alpha_2*sigma/sqrt(n)
CU = 182.45996
Alternatively, z
a/2
can be calculated using STIXBOXs function qnorm:
-->z_alpha_2 = qnorm(1-alpha/2,0,1)
z_alpha_2 = 1.959964
Example 2 - Small sample with unknown population variance. A sample of 10 carbon
composite cylinders indicate that the mean value of the carbon content in each cylinder is
0.65, with a sample standard deviation of 0.05. Determine the 90% confidence interval for the
carbon content.
The data provided is interpreted as follows: n=10,X = 0.65, s = 0.05, = 0.10. To calculate
the confidence interval we need to find t
n-1,/2
from P(T>t
/2
) = /2, or P(T>t
/2
) = 1 - /2,
where T ~ Student t( = n-1), i.e., the Student t distribution with = n-1 degrees of freedom.
To calculate t
n-1,/2
we can use SCILABs own cdfnor function with the following call:
t_alpha_2=cdft("T",n-1,1-alpha/2,alpha/2)
or, if you have the statistical toolbox STIXBOX available, you can use:
t_alpha_2 = qt(1-alpha/2,n-1)
The SCILAB calculations for this problem will proceed as follows:
-->n=10;xbar=0.65;s=0.05;alpha=0.10;
-->t_alpha_2 = cdft('T',n-1,1-alpha/2,alpha/2)
t_alpha_2 = 1.8331129
-->CU=xbar+t_alpha_2*s/sqrt(n)
CU .6789841
-->CL=xbar-t_alpha_2*s/sqrt(n)
CL = .6210159
The value t
a/2
can be obtained using STIXBOXs function qt:
-->t_alpha_2 = qt(1-alpha/2,n-1)
t_alpha_2 = 1.8331129
Confidence interval for proportions
Let X ~ Bernoulli(p), where p is the probability of success, then E[X] = p, Var[X] = p(1-p). If an
experiment involving X is repeated
n
times and
k
successful outcomes are recorded, then an
estimate of p is given by
^
p =
k
n
, while the standard error of
^
p is =
p ( ) 1 p
n
. In
practice, the sample estimate for p, i.e.,
^
p , replaces p in the standard error formula. For a
large sample size, n>30, and np > 5 and n(1-p)>5, the sampling distribution is very nearly
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
All rights reserved
normal, i.e., The 100(1-

) % central two-sided confidence interval for the population mean


p
is ( ^p - z
/2

^p
, ^p + z
/2

^p
).
Example of proportion confidence interval for a large sample
Suppose an irrigation engineer keeps track of the number of days during a 90-day period in late
spring and early summer in which significant rainfall is available as to not needing to activate
an irrigation sprinkler system in an orchard. Observations taken at random during 70 days in
the last three summers indicate that enough rainfall was recorded only during 20 out of those
70 days. Determine the 90% confidence interval for the proportion p of the number of days
where enough rainfall is available in the orchard.
Estimate for p and the standard error are calculated as:
-->k=20;n=90;alpha=0.10;
-->p_hat = k/n
p_hat = .2222222
-->sigma_p_hat =sqrt(p_hat*(1-p_hat)/n)
sigma_p_hat = .0438228
The parameter z

2
is obtained from:
-->z_alpha_2 = qnorm(1-alpha/2,0,1)
z_alpha_2 = 1.6448536
The lower and upper limits of the confidence interval are:
-->CL=p_hat-z_alpha_2*sigma_p_hat
CL = .1501401
-->CU=p_hat+z_alpha_2*sigma_p_hat
CU = .2943043
For a small sample, n<30, we can estimate a confidence interval using:
( - t
, n 1

2
, + t
, n 1

2
).
Example of proportion confidence interval for a small sample
The same engineer has kept data belonging to the early fall rainfall. His records, however,
include only 25 days in the last three years, and they indicate that 20 out of those 25 days
there was sufficient rainfall as to turn off the sprinkler system. Estimate the 90% confidence
interval for the proportion of days with sufficient rainfall. The following is the SCILAB solution
for this problem:
-->k=20;n=25;alpha=0.10;
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
All rights reserved
-->p_hat = k/n
p_hat = .8
-->sigma_p_hat =sqrt(p_hat*(1-p_hat)/n)
sigma_p_hat = .08
-->t_alpha_2 = cdft('T',n-1,1-alpha/2,alpha/2)
t_alpha_2 = 1.7108821
-->CL=p_hat-t_alpha_2*sigma_p_hat
CL = .6631294
-->CU=p_hat+t_alpha_2*sigma_p_hat
CU = .9368706
Sampling Distribution of Differences and Sums of Statistics
Let S
1
and S
2
be independent statistics from two populations based on samples of sizes n
1
and n
2
, respectively. Also, let the respective means and standard errors of the sampling
distributions of those statistics be

S
1
and

S
2
, and

S
1
and

S
2
, respectively.
The differences between the statistics from the two populations have a sampling distribution
with mean
=
S
1
S
2

S
1

S
2
,
and standard error
=
S
1
S
2
+
S
1
2

S
2
2
.
The sum of the statistics + S
1
S
2
has a mean
=
+ S
1
S
2
+
S
1

S
2
,
and standard error
=
+ S
1
S
2
+
S
1
2

S
2
2
Estimators for the mean and standard deviation of the difference and sum of the statistics S
1
and S
2
are given by:
. ;
2
2
1
2
2 1
2 1
2 1 2 1
n n
X X
S S
S S S S

+ = =

Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
All rights reserved
Confidence intervals
For large samples, i.e., 30 n
1
and 30 n
2
, and assuming that the variances

S
1
2
and

S
2
2
are known, the confidence intervals for the difference and sum of the statistics , S
1
S
2
are given by:

+ + +
2
2
1
2
2 / 2 1
2
2
1
2
2 / 2 1
2 1 2 1
) ( ; ) (
n n
z X X
n n
z X X
S S S S


and

+ + + + +
2
2
1
2
2 / 2 1
2
2
1
2
2 / 2 1
2 1 2 1
) ( ; ) (
n n
z X X
n n
z X X
S S S S


,
respectively.
If one of the samples is small, i.e., < n
1
30 or < n
2
30 , or if the variances

S
1
2
and

S
2
2
are unknown, the confidence intervals for the difference and sum of the statistics , S
1
S
2
are given by:

+ + +
2
2
2
1
2
1
2 / , 2 1
2
2
2
1
2
1
2 / , 2 1
) ( ; ) (
n
S
n
S
t X X
n
S
n
S
t X X

and

+ + + + +
2
2
2
1
2
1
2 / , 2 1
2
2
2
1
2
1
2 / , 2 1
) ( ; ) (
n
S
n
S
t X X
n
S
n
S
t X X

,
respectively, where = + n
1
n
2
2 is the number of degrees of freedom in the variate t

.
Examples of confidence intervals for sum and difference of means
An industrial process consists of two consecutive steps taking times X
1
and X
2
, respectively, for
completion. Measurements from 20 repetitions indicate that the first step takes an average
ofX
1
= 45 minutes with a standard deviation of S
1
= 10 minutes, while measurements from 15
repetitions indicate that the second step takes an average of X
2
= 65 minutes with a standard
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
All rights reserved
deviation of S
2
= 5 minutes. Determine the 99% confidence interval for total process time, X
T
= X
1
+ X
2
.
Using SCILAB we proceed as follows:
-->n1 = 20; X1bar = 45; S1 = 10; n2 = 15; X2bar = 65; S2 = 5;
-->XTbar = X1bar + X2bar
XTbar = 110.
-->sigmaTbar = sqrt(S1^2/n1+S2^2/n2)
sigmaTbar = 2.5819889
-->t_alpha_2 = cdft('T',n1+n2-1,1-alpha/2,alpha/2)
t_alpha_2 = 1.6909243
-->CL=XTbar-t_alpha_2*sigmaTbar
CL = 105.63405
-->CU=XTbar+t_alpha_2*sigmaTbar
CU = 114.36595
Interval Estimation for the Variance
Consider a random sample , X
1
X
2
, ..., X
n
of independent normally distributed variables with
mean

, variance

2
, and sample mean . The statistic

=
n
i
i
X X
n
S
1
3 2
) (
1
1

is an unbiased estimator of the variance

2
. The quantity,

=
=
n
i
i
X X
S
n
1
2
2 2
2
) (
1

) 1 (

follows the
2
distribution with = n - 1 degrees of freedom.
The (
1
)*100 % two-sided confidence interval for the variance
2
is found from
, 1

) 1 (
2
2 / , 1
2
2
2
2 / 1 , 1


n n
S
n P
as illustrated in the figure below.
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
All rights reserved
The confidence interval for the population variance

2
is therefore,
.

) 1 (
,

) 1 (
2
2 / 1 , 1
2
2
2 / , 1
2




n n
S n S n
where

2
2
and
1

2
2
are the values that a
n 1
2
variable exceeds with probabilities

2
and 1

2
, respectively.
The one-sided upper confidence limit for

2
is defined as
.

) 1 (
2
1 , 1
2

n
S n
Two-sided and upper 99% confidence limit for the standard deviation
Suppose that the compressive strengths of 40 test concrete cubes have an estimated standard
deviation of 5.02 N/mm
2
. We will determine the two-sided and the upper 99% confidence
limits as follows:
-->n=40;s=5.02;alpha=0.01;
-->Chi_alpha_2 = cdfchi('X',n-1,1-alpha/2,alpha/2)
Chi_alpha_2 = 65.475571
-->Chi_1_alpha_2=cdfchi('X',n-1,alpha/2,1-alpha/2)
Chi_1_alpha_2 = 19.995868
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
All rights reserved
-->CL=(n-1)*s^2/Chi_alpha_2
CL = 15.010417
-->CU=(n-1)*s^2/Chi_1_alpha_2
CU = 49.150935
Hypothesis Testing
A hypothesis is a declaration made about a population (for instance, with respect to its mean).
Acceptance of the hypothesis is based on a statistical test on a sample taken from the
population. The consequent action and decision-making are called hypothesis testing.
The process consist on taking a random sample from the population and making a statistical
hypothesis about the population. If the observations do not support the model or theory
postulated, the hypothesis is rejected. However, if the observations are in agreement, then
hypothesis is not rejected, but it is not necessarily accepted.
Associated with the decision is a level of significance

. This is complementary to the


probability used earlier for setting confidence limits.
The initial assumption of a significance level removes any subjectivity in the decision making
process, i.e., two or more investigators will reach the same conclusion based on the same
data. Hypothesis testing therefore involves procedures for rejecting or not rejecting a
statement, and the chances of making incorrect decisions of either kind, i.e., rejecting if the
hypothesis is true or accepting if the hypothesis is false
Procedure for hypothesis testing
The procedure for hypothesis testing involves the following six steps:
1. Declare a null hypothesis, H
0
. This is the hypothesis to be tested. For example, H
0
:
=
1

2
0 , i.e., we hypothesize that the mean value of population 1 and the mean value of
population 2 are the same. If H
0
is true, any observed difference in means is attributed to
errors in random sampling.
2. Declare an alternative hypothesis, H
1
. For the example under consideration, it could be
H
1
:
1

2
0 . [Note: this is what we really want to test.]
3. Determine or specify a test statistic, T. In the example under consideration T will be based
on X
1
- X
2
, the difference of observed means.
4. Use the known (or assumed) distribution of the test statistic, T.
5. Define a rejection region (the critical region, R) for the test statistic based on a pre-
assigned significance level

.
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
All rights reserved
6. Use observed data to determine whether the computed value of the test statistic is within or
outside the critical region. If the test statistic is within the critical region then we say that the
quantity we are testing is significant at the
100
percent level.
Notes:
1. For the example under consideration, the alternate hypothesis H
1
:
1

2
0 produces
what is called a two-tailed test. If the alternate hypothesis is H
1
: < 0
1

2
, or H
1
:
<
1

2
0 , then we have a one-tailed test.
2. The probability of rejecting the null hypothesis is equal to the level of significance, i.e.,
Pr[ | H
0
] =

.
Errors in hypothesis testing
In hypothesis testing we use the terms errors of Type I and Type II to define the cases in which
a true hypothesis is rejected or a false hypothesis is accepted (not rejected), respectively. Let
T = value of test statistic, R = rejection region, A = acceptance region, thus, , and
, where

= the parameter space for T. The probabilities of making an error of


Type I or of Type II are as follows:
Rejecting a true hypothesis, P[Type I error] = P[ | H
0
] =

Not rejecting a false hypothesis, P[Type II error] = P[ | H


1
] =

.
Now, let's consider the cases in which we make the correct decision:
Not rejecting a true hypothesis, P[Not(Type I error)] = P[ | H
0
] = 1 -

.
Rejecting a false hypothesis, P[Not(Type II error)] = P [ | H
1
] = 1 -

.
Power of hypothesis testing
The complement of

is called the power of the test of the null hypothesis H


0
vs. the
alternative H
1
. The power of a test is used, for example, to determine a minimum sample
size to restrict errors.
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
All rights reserved
Selecting the values of and
A typical value of the level of significance (or probability of Type I error) is
= .05
, (i.e.,
incorrect rejection once in 20 times on the average). If the consequences of a Type I error are
more serious, choose smaller values of

, say 0.01 or even 0.001.


The value of

, i.e., the probability of making an error of Type II, depends on

, the sample
size
n
, and on the true value of the parameter tested. Thus, the value of

is determined
after the hypothesis testing is performed. It is customary to draw graphs showing

or the
power of the test (
1
) as a function of the true value of the parameter tested. These
graphs are called operating characteristic curves or power function curves, respectively.
Hypothesis testing involving mean values
Hypothesis testing on one mean
Suppose you want to test the hypothesis that the mean of a population is equal to a certain
value, i.e., H
0
: =
0
, at a significance level

.
We could use three different alternate hypothesis for the test. These are:
Two-tailed test: H
1
:
0
One-tailed tests: H
1
: <
0
, or H
1
: <
0
.
A sample of size
n
is taken from the population yielding a mean value, x, and a standard
deviation, s
x
.
Case I: Knowing , or large sample if unknown
Assuming that we know the standard deviation of the population

, we use the standard


normal score:
Z = (X-

)/(

/
n
)
as the test statistic. The particular value for the test is z
0
= ( x -
0
)/(

/
n
) .
If the standard deviation of the population,

, is unknown, but the sample is large (


30 n
),
we can still use the standard normal score as the test statistic, but replacing

with s
x
, i.e.,
z
0
= ( x -
0
)/( s
x
/
n
)
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
All rights reserved
Let
( ) z
be the CDF of the standard normal distribution, i.e., Z~N(0,1).
Two-tailed test
If using a two-tailed test we will find the value of z

2
, from
Pr[Z> z

2
] = 1-

2
=

2
, or

2
= 1

2
.
We will reject the null hypothesis, H
0
, if < z

2
z
0
or if < z
0
z

2
.
In other words, the rejection region is R = { < z

2
z
0
}, while the acceptance region is A =
{ < z
0
z

2
}.
One-tailed test
If using a one-tailed test we will find the value of z

, from
Pr[Z> z

] = 1- ( ) z

, or ( ) z

=
1
.
Reject the null hypothesis, H
0
, if < z

z
0
, and H
1
: <
0
, or if < z
0
z

, and H
1
:
<
0
.
Case II: Small sample with unknown
If the standard deviation of the population,

, is unknown and
< n 30
(small sample), we
use the Student's t score:
t = (X-

)/( S
x
/
n
),
with
= n 1
degrees of freedom, as the test statistic. The particular value for the test is,
t
0
= (x -
0
)/( s
x
/
n
) .
Let ( ) F

t be the CDF of the Student's t variate with

degrees of freedom, i.e., t ~Student's


t(

).
Two-tailed test
If using a two-tailed test we will find the value of t

2
, from
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
All rights reserved
Pr[t > t

2
] =

2
=

2
, or

2
= 1

2
.
Reject the null hypothesis, H
0
, if < t

2
t
0
or if < t
0
t

2
.
In other words, the rejection region is R = { < t

2
t
0
}, while the acceptance region is A =
{ < t
0
t

2
}.
One-tailed test
If using a one-tailed test we will find the value of t

, from
Pr[t> t

] = 1- ( ) F

, or ( ) F

=
1
.
Reject the null hypothesis, H
0
, if < t

t
0
, and H
1
: <
0
, or if < t
0
t

, and H
1
:
<
0
.
A function to perform hypothesis testing on one mean
The following function, htestmu1, can be used to perform hypothesis testing on one mean.
The possible calls to the function are:
[xbar,s,Ta,T0] = htestmu1(altype,alpha,mu0,xbar,s,sigma,n)
[xbar,s,Ta,T0] = htestmu1(altype,alpha,mu0,x)
A listing of the function follows:
function [xbar,s,Ta,T0] = htestmu1(altype,alpha,mu0,x,s,sigma,n)
//Hypothesis testing on one mean. Possible function calls:
//
// [xbar,s,Ta,T0] = htestmu1(altype,alpha,mu0,xbar,s,sigma,n)
// [xbar,s,Ta,T0] = htestmu1(altype,alpha,mu0,x)
//
// altype can be 'one' - for one-sided alternative hypothesis,
// or 'two' - for two sided alternative hypothesis
// alpha = level of significance (typical values = 0.01,0.05,0.10)
// mu0 = value of population mean being tested, H0:mu = mu0
// x = mean value of a sample (xbar) or vector representing the sample
// if x = mean value, then s = standard deviation of sample
// if x = vector representing sample, s = standard deviation of population
// if x = mean value, sigma = standard deviation of population and
// n = sample size
if altype<>'one' & altype<>'two' then
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
All rights reserved
error('htestmu1 - select type of alternative hypothesis = one or two');
abort;
end;
[nargout,nargin] = argn(0)
if nargin == 5 then
if length(x)<1 then
error('htestmu1 - x must be a vector');
abort;
else
sigma = s;
n = length(x);
xbar = mean(x);
s = st_deviation(x);
end;
else
xbar = x;
end;
printf(' \n');
printf('Hypothesis testing on one mean: ' ...
+ altype + '-side alternative hypothesis.\n')
printf(' \n');
if sigma > 0 & altype=='one' then
Ta = cdfnor('X',0,1,1-alpha,alpha);
T0 = (xbar-mu0)/(sigma/sqrt(n));
printf('Test parameter used: z');
elseif sigma >0 & altype=='two' then
Ta = cdfnor('X',0,1,1-alpha/2,alpha/2);
T0 = (xbar-mu0)/(sigma/sqrt(n));
printf('Test parameter used: z');
elseif sigma <=0 & n>=30 & altype=='one' then
Ta = cdfnor('X',0,1,1-alpha,alpha);
T0 = (xbar-mu0)/(s/sqrt(n));
printf('Test parameter used: z');
elseif sigma <=0 & n<30 & altype=='one' then
Ta = cdft('T',n-1,1-alpha,alpha);
T0 = (xbar-mu0)/(s/sqrt(n));
printf('Test parameter used: t');
elseif sigma <=0 & n>=30 & altype=='two' then
Ta = cdfnor('X',0,1,1-alpha/2,alpha/2);
T0 = (xbar-mu0)/(s/sqrt(n));
printf('Test parameter used: z');
else
Ta = cdft('T',n-1,1-alpha/2,alpha/2);
T0 = (xbar-mu0)/(s/sqrt(n));
printf('Test parameter used: t');
end;
if altype == 'two' then
if T0>Ta | T0<-Ta then
printf('Reject the null hypothesis H0 : mu = %f\n',mu0);
else
printf('Do not reject the null hypothesis H0 : mu = %f\n',mu0);
end
else
if T0>Ta then
printf('Reject the null hypothesis H0 : mu = %f\n',mu0);
printf('if the alternative hypothesis is H1 : mu > %f\n',mu0);
elseif T0<-Ta
printf('Reject the null hypothesis H0 : mu = %f\n',mu0);
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
All rights reserved
printf('if the alternative hypothesis is H1 : mu < %f\n',mu0);
else
printf('Do not reject the null hypothesis H0 : mu = %f\n',mu0);
end
end;
printf(' \n');
Examples of hypothesis testing on one mean
In the following examples values of
n
, x, s
x
,
0
, and

are provided and the hypothesis


testing performed by using function htestmu1.
Example 1. Test the null hypothesis H
0
: = 2.0, against an one-sided alternative
hypothesis using data from a sample of size 15, with a sample mean of 2.5 and
sample standard deviation s
x
= 3.5 for a confidence level = 0.05. Assume
that the population standard deviation is known, = 1.5.
-->n=15;xbar=2.5;sx=3.5;mu0=2.0;alpha=0.05;sigma=1.5;
-->[xbar,s,Ta,T0]=htestmu1('one',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: one-side alternative hypothesis.
Test parameter used: z
Do not reject the null hypothesis H0 : mu = 2.000000
T0 = 1.2909944
Ta = 1.6448536
s = 3.5
xbar = 2.5
Example 2. Test the same null hypothesis as in Example 1 but using a two-sided alternative
hypothesis.
-->[xbar,s,Ta,T0]=htestmu1('two',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: two-side alternative hypothesis.
Test parameter used: z
Do not reject the null hypothesis H0 : mu = 2.000000
T0 = 1.2909944
Ta = 1.959964
s = 3.5
xbar = 2.5
Example 3. Test the null hypothesis H
0
: = 6.0 against a one-sided alternative hypothesis
based on a sample of size 45 (large sample) with a sample mean 0f 12.3 and a sample standard
deviation of 2.0 at a confidence level of 0.05. The population standard deviation is not known.
-->n=45;xbar=12.3;sx=2.0;mu0=6.0;alpha=0.05;sigma=0.0;
-->[xbar,s,Ta,T0]=htestmu1('one',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: one-side alternative hypothesis.
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
All rights reserved
Test parameter used: z
Reject the null hypothesis H0 : mu = 6.000000
if the alternative hypothesis is H1 : mu > 6.000000
T0 = 21.130842
Ta = 1.6448536
s = 2.
xbar = 12.3
Example 4. Test the null hypothesis of Example 3 against a two-sided hypothesis.
-->[xbar,s,Ta,T0]=htestmu1('two',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: two-side alternative hypothesis.
Test parameter used: z
Reject the null hypothesis H0 : mu = 6.000000
T0 = 21.130842
Ta = 1.959964
s = 2.
xbar = 12.3
Example 5. Test the null hypothesis H
0
: = 14.0 against a one-sided alternative hypothesis
based on a sample of size 10 (small sample) with a sample mean of 11 and a sample standard
deviation of 1.5 at a level of significance of 0.01. The standard deviation of the population is
unknown.
-->n=10;xbar=11;sx=1.5;mu0=14.0;alpha=0.01;sigma=0.0;
-->[xbar,s,Ta,T0]=htestmu1('one',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: one-side alternative hypothesis.
Test parameter used: t
Reject the null hypothesis H0 : mu = 14.000000
if the alternative hypothesis is H1 : mu < 14.000000
T0 = - 6.3245553
Ta = 2.8214379
s = 1.5
xbar = 11.
Example 6. Test the null hypothesis of Example 5 against a two-sided alternative hypothesis.
-->[xbar,s,Ta,T0]=htestmu1('two',alpha,mu0,xbar,sx,sigma,n)
Hypothesis testing on one mean: two-side alternative hypothesis.
Test parameter used: t
Reject the null hypothesis H0 : mu = 14.000000
T0 = - 6.3245553
Ta = 3.2498355
s = 1.5
xbar = 11.
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
All rights reserved
Example 7. For the vector of data, X, generated below, test the null hypothesis H
0
: = 23
against a one-sided alternative hypothesis at the significance level = 0.01. The population
standard deviation is assumed to be known, = 5.
-->X = int(100*rand(1,10))
X =
! 30. 93. 21. 31. 36. 29. 56. 48. 33. 59. !
-->alpha = 0.01;mu0=23;sigma=5;
-->[xbar,s,Ta,T0]=htestmu1('one',alpha,mu0,X,sigma)
Hypothesis testing on one mean: one-side alternative hypothesis.
Test parameter used: z
Reject the null hypothesis H0 : mu = 23.000000
if the alternative hypothesis is H1 : mu > 23.000000
T0 = 13.028584
Ta = 2.3263479
s = 21.313532
xbar = 43.6
Example 8. Test the null hypothesis of Example 7 against a two-sided alternative hypothesis.
-->[xbar,s,Ta,T0]=htestmu1('two',alpha,mu0,X,sigma)
Hypothesis testing on one mean: two-side alternative hypothesis.
Test parameter used: z
Reject the null hypothesis H0 : mu = 23.000000
T0 = 13.028584
Ta = 2.5758293
s = 21.313532
xbar = 43.6
-->[xbar,s,Ta,T0]=htestmu1('one',alpha,mu0,X,-1)
Example 9. Test the null hypothesis of Example 7 against a one-sided alternative hypothesis
assuming that the population standard deviation is not known.
Hypothesis testing on one mean: one-side alternative hypothesis.
Test parameter used: t
Reject the null hypothesis H0 : mu = 23.000000
if the alternative hypothesis is H1 : mu > 23.000000
T0 = 3.0564112
Ta = 2.8214379
s = 21.313532
xbar = 43.6
Example 10. Test the null hypothesis of Example 7 against a two-sided alternative hypothesis
assuming that the population standard deviation is not known.
-->[xbar,s,Ta,T0]=htestmu1('two',alpha,mu0,X,-1)
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
All rights reserved
Hypothesis testing on one mean: two-side alternative hypothesis.
Test parameter used: t
Do not reject the null hypothesis H0 : mu = 23.000000
T0 = 3.0564112
Ta = 3.2498355
s = 21.313532
xbar = 43.6
Hypothesis testing on one proportion
Suppose that we want to test the null hypothesis, H
0
: = p p
0
, where
p
represents the
probability of obtaining a successful outcome in any given repetition of a Bernoulli trial. To
test the hypothesis, we perform
n
repetitions of the experiment, and find that
k
successful
outcomes are recorded. Thus, an estimate of p is given by
^p = k/n.
The standard deviation for the sample will be estimated as
s
p
=
n
p p ) 1 (
=
k ( ) n k
n
3
Assume that the Z score,
Z
= (
p
- p
0
)/ s
p
follows the standard normal distribution, i.e., Z ~ N(0,1). The particular value of the statistic
to test is
z
0
= (^p-p
0
)/s
p
.
We could use three different alternate hypothesis for the test. These are:
Two-tailed test: H
1
: p p
0
One-tailed tests: H
1
: < p
0
p , or H
1
: < p p
0
.
Two-tailed test
If using a two-tailed test we will find the value of z

2
, from
Pr[Z> z

2
] = 1-

2
=

2
, or

2
= 1

2
.
Reject the null hypothesis, H
0
, if < z

2
z
0
or if < z
0
z

2
.
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
All rights reserved
In other words, the rejection region is R = { < z

2
z
0
}, while the acceptance region is A =
{ < z
0
z

2
}.
One-tailed test
If using a one-tailed test we will find the value of z

, from
Pr[Z> z

] = 1- ( ) z

, or ( ) z

=
1
.
Reject the null hypothesis, H
0
, if < z

z
0
, and H
1
: < p
0
p , or if < z
0
z

, and H
1
:
< p p
0
.
Examples of hypothesis testing on one proportion
Example 1. Test the null hypothesis H
0
:p
0
= 0.25 against a one-sided alternative hypothesis
based on 100 repetitions of a test out of which 20 successful outcomes are recorded using a
significance level of 0.05.
-->k=20;n=100;p0=0.25;alpha=0.05;
-->[p_hat,sigma,Ta,T0]=htestprop1('one',alpha,p0,k,n)
Hypothesis testing on one proportion: one-side alternative hypothesis.
Test parameter used: z
Do not reject the null hypothesis H0 : p = .250000
T0 = - 1.25
Ta = 1.6448536
sigma = .04
p_hat = .2
Example 2. Test the same null hypothesis as in Example 1 against a two-sided alternative
hypothesis.
-->[p_hat,sigma,Ta,T0]=htestprop1('two',alpha,p0,k,n)
Hypothesis testing on one proportion: two-side alternative hypothesis.
Test parameter used: z
Do not reject the null hypothesis H0 : p = .250000
T0 = - 1.25
Ta = 1.959964
sigma = .04
p_hat = .2
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
All rights reserved
Hypothesis testing on two means
Assume that we have two populations, population 1 and population 2, with mean values
1
and
2
, respectively, and with standard deviations,
1
and
2
, respectively. A sample of
size n
1
is taken out of population 1 yielding a mean value x
1
and standard deviation s
1
.
Similarly, a sample of size n
2
is taken out of population 2 yielding a mean value x
2
and
standard deviation s
2
.
Testing the difference between two means using known variances
If both population 1 and population 2 are normal, the statistic
2 1
) ( ) (
2 1 2 1
X X
X X
Z


has a N(0,1) distribution. The standard error of the difference between the two means is:
=
+

1
2
n
1

2
2
n
2
.
The criteria for rejection of the null hypothesis, H
0
:
1

2
= , is the same as for a single mean
value, =
1

2
= .
Testing the differences between two means when the variances are unknown but equal
This could be the case in which the two samples are taken from the same population, or when
there is evidence that the standard deviation of two different populations are the same. In
this case, we obtain a "pooled estimate" of the common standard deviation of the two
populations,

, as:
= s
p
2
+ ( ) n
1
1 s
1
2
( ) n
2
1 s
2
2
+ n
1
n
2
2
.
Then, the random variable
has the Student's
t
distribution with = + n
1
n
2
2 degrees of freedom.
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
All rights reserved
Testing the difference between two means when the variances are unknown and unequal
For observations taken from normal populations with unknown and unequal variances, the
statistic
has an approximate Student's
t
distribution with
degrees of freedom.
For the last two cases in which a t-parameter is used for the test, the criteria for rejection of
the null hypothesis, H
0
:
1

2
= , is the same as for a single mean value, =
1

2
= .
A user-defined SCILAB function for hypothesis testing on two means
The procedure for hypothesis testing on two means is coded in the following function,
htestmu2, which has possible calls
[X1Info,X2Info,sp,nu,T0,Ta] = htestmu2()
[X1Info,X2Info,sp,nu,T0,Ta] = htestmu2(Xdata)
[X1Info,X2Info,sp,nu,T0,Ta] = htestmu2(X1data,X2data)
Xdata, X1data, and X2data are row vectors of data. X1Info is a vector that contains the sample
size, n1, the mean value, x1bar, and the standard deviation, s1, of sample 1. X2Info is a
vector containing n2, x2bar, and s2. The value sp represents the standard deviation for the
two samples which could be the value
X1-X2
or s
p
, as defined above. The value nu represents
the degrees of freedom of the Students t distribution, is a t parameter is used in the test. T0
is the actual value of the z or t parameter used in the test. Ta represents z

, z
/2
, t

, or t
/2
,
depending on the test parameter used and on the type of alternative hypothesis (one- or tw-
sided) used.
The function operates interactively requesting information from the user and provides verbose
information on the type of test parameter and alternative hypothesis used, as well as providing
a recommendation about the rejection or not-rejection of the null hypothesis.
If the function call [X1Info,X2Info,sp,nu,T0,Ta] = htestmu2() is used, the user will be prompted
for the summary information on the samples, i.e., the samples sizes, mean values, and
standard deviations. If the function call [X1Info,X2Info,sp,nu,T0,Ta] = htestmu2(Xdata) is
used, the user is asked to identify the vector Xdata as sample 1 or sample 2, and then is
prompted for the summary data for the other sample. Finally, if the function call
[X1Info,X2Info,sp,nu,T0,Ta] = htestmu2(X1data,X2data) is used, the function calculates the
sample summary data all by itself.
The function will also prompt the user for the following information:
The difference of means to be tested, i.e., =
1

2
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
All rights reserved
The level of confidence of the test, i.e.,
The type of alternative hypothesis to be used, i.e., one- or two-sided
The standard deviation of the populations,
1
and
2
, if known
For unknown
1
and
2
, the function asks if the user suspects if the values of
1
and
2
,
are equal or not. This helps the function select the t parameter to use.
A listing of the function is shown below.
function [X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X1,X2)
[nargout,nargin]=argn(0)
if nargin == 0 then
X1in = input('For sample 1 enter n, xbar, s :')
n1 = X1in(1);x1bar = X1in(2);s1 = X1in(3);
X2in = input('For sample 2 enter n, xbar, s :')
n2 = X2in(1);x2bar = X2in(2);s2 = X2in(3);
elseif nargin == 1 then
disp('You entered a vector as input to the function.')
disp('Do you want this vector to represent sample 1 or 2?')
idsample = input(' ')
if idsample == 1 then
n1 = length(X1); x1bar = mean(X1); s1 = st_deviation(X1);
printf('n1 = %g x1bar = %g s1 = %g',n1,x1bar,s1)
X2in = input('For sample 2 enter n, xbar, s :')
n2 = X2in(1);x2bar = X2in(2);s2 = X2in(3);
else
n2 = length(X1); x2bar = mean(X1); s2 = st_deviation(X1);
printf('n2 = %g x2bar = %g s2 = %g',n2,x2bar,s2)
X1in = input('For sample 1 enter n, xbar, s :')
n1 = X1in(1);x1bar = X1in(2);s1 = X1in(3);
end
else
n1 = length(X1); x1bar = mean(X1); s1 = st_deviation(X1);
printf('n1 = %g x1bar = %g s1 = %g',n1,x1bar,s1)
n2 = length(X2); x2bar = mean(X2); s2 = st_deviation(X2);
printf('n2 = %g x2bar = %g s2 = %g',n2,x2bar,s2)
end
X1Info = [n1,x1bar,s1]; X2Info = [n2,x2bar,s2];
delta = ...
input('Enter the difference of population means to be tested:');
disp('Enter the level of confidence, alpha, for the test:')
disp('(Typical values: 0.01, 0.05, 0.10)')
alpha = input(' ');
disp('Enter the type of alternative hypothesis to test:')
disp(' 1 - one-sided 2 - two-sided');
atype = input(' ');
disp('Enter population standard deviations, sigma1 & sigma2.')
disp('Note: Enter zero if sigma1 or sigma2 are unknown.')
sigmas = input('');
if sigmas == 0 then
sigma1 = 0; sigma2 = 0;
else
sigma1 = sigmas(1); sigma2 = sigmas(2);
end
if sigma1<=0 | sigma2<=0 then
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
All rights reserved
disp('Do you suspect that the unknown population standard')
disp('deviations are equal? If so enter 1, otherwise enter 0')
answer = input('')
if answer == 1 then
nu=int(((s1^2/n1)+(s2^2/n2))^2/((s1^2/n1)^2/(n1-1)+(s2^2/n2)^2/(n2-1)));
sp=sqrt(s1^2/n1+s2^2/n2);
T0=((x1bar-x2bar)-delta)/sp;ttype = ' t';
else
nu=n1+n2-2;
sp=sqrt(((n1-1)*s1^2+(n2-1)*s2^2)/nu);
T0=((x1bar-x2bar)-delta)/(sp*sqrt(1/n1+1/n2));ttype = ' t';
end
if atype == 1 then
Ta = cdft('T',nu,1-alpha,alpha)
else
Ta = cdft('T',nu,1-alpha/2,alpha/2)
end
else
sp=sqrt(sigma1^2/n1+sigma2^2/n2);
T0=((x1bar-x2bar)-delta)/sp;ttype = ' z';nu=[];
if atype == 1 then
Ta = cdfnor('X',0,1,1-alpha,alpha)
else
Ta = cdfnor('X',0,1,1-alpha/2,alpha/2)
end
end;
if atype == 1 then
printf('Hypothesis testing on two means: one-sided test.\n')
printf('Test parameter used:' + ttype +'\n');printf(' \n');
if T0<-Ta then
printf('Reject the null hypothesis H0:mu1-mu2=%f, \n',delta);
printf('if the alternative hypothesis is H1:mu1-mu2<%f. \n',delta);
elseif T0>Ta
printf('Reject the null hypothesis H0:mu1-mu2=%f, \n',delta);
printf('if the alternative hypothesis is H1:mu1-mu2>%f. \n',delta);
else
printf('Do not reject the null hypothesis H0:mu1-mu2=%f, \n',delta);
end
else
printf('Hypothesis testing on two means: two-sided test.\n')
printf('Test parameter used:' + ttype +'\n');printf(' \n');
if T0<-Ta | T0>Ta then
printf('Reject the null hypothesis H0:mu1-mu2=%f, \n',delta);
else
printf('Do not reject the null hypothesis H0:mu1-mu2=%f, \n',delta);
end
end;
Examples of application of function htestmu2
Example 1. Two samples taken from two different populations are described by the statistics
n
1
= 100,x
1
= 2.3, n
2
= 75,x
2
= 2.5. The populations are known to have the standard
deviations
1
= 5.5 and
2
= 3.0. Test the null hypothesis H
0
:
1
-
2
= 0 (i.e., = 0) at the level
of confidence = 0.05 against (a) a two-sided alternative hypothesis, and (b) a one-sided
alternative hypothesis. [Note: since the populations standard deviations are given, we do not
need to use the samples standard deviation (which are not given, anyway). In this case we
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
All rights reserved
simply enter them as zero when prompted by function htestmu2.] The users input requested
by function htestmu2 is shown in italics:
-->Solution to Example 1 - part (a)
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2()
For sample 1 enter n, xbar, s : 100, 2.3, 0.0
For sample 2 enter n, xbar, s : 75, 2.5, 0.0
Enter the difference of population means to be tested: 0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.05
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
5.5 3.0
Hypothesis testing on two means: two-sided test.
Test parameter used: z
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 1.959964
T0 = - .3076923
nu = []
sp = .65
X2Info = ! 75. 2.5 0. !
X1Info = ! 100. 2.3 0. !
-->Solution to Example 1 - part (b)
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2()
For sample 1 enter n, xbar, s : 100, 2.3, 0.0
For sample 2 enter n, xbar, s : 75, 2.5, 0.0
Enter the difference of population means to be tested: 0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.05
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
All rights reserved
1
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
5.5 3.0
Hypothesis testing on two means: one-sided test.
Test parameter used: z
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 1.6448536
T0 = - .3076923
nu = []
sp = .65
X2Info = ! 75. 2.5 0. !
X1Info = ! 100. 2.3 0. !
Example 2. Sample 1 is given by X
1
= [2.4,3.2,1.1,2.5,4.2,3.6], while sample 2 is characterized
by the statistics n
2
= 8,x
2
= 3.2, s
2
= 0.5. Test the null hypothesis H
0
:
1
-
2
= 0 (i.e., = 0) at
the level of confidence = 0.10 against (a) a two-sided alternative hypothesis, and (b) a one-
sided alternative hypothesis. The standard deviations of the populations are unknown, and
suspected to be different.
-->//Example 2 - part(a)
-->X1 = [2.4,3.2,1.1,2.5,4.2,3.6]
X1 = ! 2.4 3.2 1.1 2.5 4.2 3.6 !
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X1)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
1
n1 = 6 x1bar = 2.83333 s1 = 1.08566
For sample 2 enter n, xbar, s :
8 3.2 0.5
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
0
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
All rights reserved
Hypothesis testing on two means: two-sided test.
Test parameter used: t
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 1.7822876
T0 = - .8507016
nu = 12.
sp = .7980880
X2Info = ! 8. 3.2 .5 !
X1Info = ! 6. 2.8333333 1.0856642 !
-->//Example 2 - part(b)
-->X1 = [2.4,3.2,1.1,2.5,4.2,3.6]
X1 = ! 2.4 3.2 1.1 2.5 4.2 3.6 !
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X1)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
1
n1 = 6 x1bar = 2.83333 s1 = 1.08566
For sample 2 enter n, xbar, s :
8 3.2 0.5
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
0
Hypothesis testing on two means: one-sided test.
Test parameter used: t
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 1.3562173
T0 = - .8507016
nu = 12.
sp = .7980880
X2Info = ! 8. 3.2 .5 !
X1Info = ! 6. 2.8333333 1.0856642 !
Example 3. Sample 2 is given by X
2
= [12.4,13.2,11.1,12.5,14.2,13.6], while sample 1 is
characterized by the statistics n
1
= 15,x
1
= 16.2, s
1
= 2.0. Test the null hypothesis H
0
:
1
-
2
=
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
All rights reserved
0 (i.e., = 0) at the level of confidence = 0.01 against (a) a two-sided alternative hypothesis,
and (b) a one-sided alternative hypothesis. The standard deviations of the populations are
unknown, and suspected to be the same.
-->//Example 3 - part(a)
-->X2= [12.4,13.2,11.1,12.5,14.2,13.6]
X2 =
! 12.4 13.2 11.1 12.5 14.2 13.6 !
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X2)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
n2 = 6 x2bar = 12.8333 s2 = 1.08566
For sample 1 enter n, xbar, s :
15 16.2 2.0
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
1
Hypothesis testing on two means: two-sided test.
Test parameter used: t
Reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 2.9207816
T0 = 4.9471778
nu = 16.
sp = .6805227
X2Info = ! 6. 12.833333 1.0856642 !
X1Info = ! 15. 16.2 2. !
-->//Example 3 - part(b)
-->X2= [12.4,13.2,11.1,12.5,14.2,13.6]
X2 =
! 12.4 13.2 11.1 12.5 14.2 13.6 !
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
All rights reserved
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X2)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
n2 = 6 x2bar = 12.8333 s2 = 1.08566
For sample 1 enter n, xbar, s :
15 16.2 2.0
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
1
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
1
Hypothesis testing on two means: one-sided test.
Test parameter used: t
Reject the null hypothesis H0:mu1-mu2=0.000000,
if the alternative hypothesis is H0:mu1-mu2>0.000000.
Ta = 2.5834872
T0 = 4.9471778
nu = 16.
sp = .6805227
X2Info = ! 6. 12.833333 1.0856642 !
X1Info = ! 15. 16.2 2. !
Example 4. Given samples X
1
= [3.2, 3.1, 3.0, 3.2], and X
2
= [2.8,3.0, 2.9, 2.7, 3.1], test the
null hypothesis H
0
:
1
-
2
= 0 (i.e., = 0) at the level of confidence = 0.01 against (a) a two-
sided alternative hypothesis, and (b) a one-sided alternative hypothesis. The standard
deviations of the populations are unknown, and suspected to be the different.
-->//Example 4 - part(a)
-->X1=[3.2,3.1,3.0,3.2], X2=[2.8,3.0,2.9,2.7,3.1]
X1 = ! 3.2 3.1 3. 3.2 !
X2 = ! 2.8 3. 2.9 2.7 3.1 !
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X1,X2)
n1 = 4 x1bar = 3.125 s1 = .0957427
n2 = 5 x2bar = 2.9 s2 = .15811
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
All rights reserved
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
0
Hypothesis testing on two means: two-sided test.
Test parameter used: t
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 3.4994833
T0 = 2.4852506
nu = 7.
sp = .1349603
X2Info = ! 5. 2.9 .1581139 !
X1Info = ! 4. 3.125 .0957427 !
-->X1=[3.2,3.1,3.0,3.2], X2=[2.8,3.0,2.9,2.7,3.1]
X1 = ! 3.2 3.1 3. 3.2 !
X2 = ! 2.8 3. 2.9 2.7 3.1 !
-->[X1Info,X2Info,sp,nu,T0,Ta]=htestmu2(X1,X2)
n1 = 4 x1bar = 3.125 s1 = .0957427
n2 = 5 x2bar = 2.9 s2 = .15811
Enter the difference of population means to be tested:
0
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter population standard deviations, sigma1 & sigma2.
Note: Enter zero if sigma1 or sigma2 are unknown.
0
Do you suspect that the unknown population standard
deviations are equal? If so enter 1, otherwise enter 0
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
All rights reserved
0
Hypothesis testing on two means: one-sided test.
Test parameter used: t
Do not reject the null hypothesis H0:mu1-mu2=0.000000,
Ta = 2.9979516
T0 = 2.4852506
nu = 7.
sp = .1349603
X2Info = ! 5. 2.9 .1581139 !
X1Info = ! 4. 3.125 .0957427 !
Testing the difference between two proportions
Suppose that we want to test the null hypothesis, H
0
: = p
1
p
2
p
0
, where the
p
's
represents the probability of obtaining a successful outcome in any given repetition of a
Bernoulli trial for two populations 1 and 2. To test the hypothesis, we perform n
1
repetitions
of the experiment from population 1, and find that k
1
successful outcomes are recorded.
Thus, an estimate of p
1
and p
2
are given, respectively, by
^p
1
= k
1
/n
1
and ^p
2
= k
2
/n
2
.
The standard deviations for the samples will be estimated, respectively, as
,
) ( ) 1 (
3
1
1 1 1
1
1 1
1
n
k n k
n
p p
s

=

= and .
) ( ) 1 (
3
2
2 2 2
2
2 2
2
n
k n k
n
p p
s

=

=
and the standard deviation of the difference of proportions is estimated from:
= s
p
2
+ s
1
2
s
2
2
Assume that the Z score,
Z
= (( p
1
p
2
)- p
0
)/ s
p
follows the standard normal distribution, i.e., Z ~ N(0,1). The particular value of the statistic
to test is
z
0
=

k
1
n
1
k
2
n
2
p
0
s
p
,
We could use three different alternate hypothesis for the test. These are:
Two-tailed test: H
1
: p
1
p
2
p
0
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
All rights reserved
One-tailed tests: H
1
: < p
0
p
1
p
2
, or H
1
: < p
1
p
2
p
0
.
Two-tailed test
If using a two-tailed test we will find the value of z

2
, from
Pr[Z> z

2
] = 1-

2
=

2
, or

2
= 1

2
.
Reject the null hypothesis, H
0
, if < z

2
z
0
or if < z
0
z

2
.
In other words, the rejection region is R = { < z

2
z
0
}, while the acceptance region is A =
{ < z
0
z

2
}.
One-tailed test
If using a one-tailed test we will find the value of z

, from
Pr[Z> z

] = 1- ( ) z

, or ( ) z

=
1
.
Reject the null hypothesis, H
0
, if < z

z
0
, and H
1
: < p
0
p
1
p
2
, or if < z
0
z

, and
H
1
: < p
1
p
2
p
0
.
A function for hypothesis testing on two proportions
Function htestprop2 performs hypothesis testing on two proportions, based on measurements
that show k
1
successful outcomes out of n
1
repetitions, and k
2
successful outcomes out of n
2
repetitions. The null hypothesis is H
0
:p
1
-p
2
=p
0
.
function [p1,p2,s1,s2,sp,z0,za] = htestprop2(atype,k1,n1,k2,n2,p0,alpha)
//Hypothesis testing in two proportions. Test the null hypothesis
//H0:p1-p2=p0, given k1, k2 successful outcomes out of n1, n2
//repetitions, respectively. Significance level = alpha.
//Variable atype represents the type of alternative hypothesis, i.e.,
//atype = 'one' for one-sided test, atype = 'two' for two-sided test
p1 = k1/n1; p2 = k2/n2;
s1 = sqrt(p1*(1-p1)/n1);
s2 = sqrt(p2*(1-p2)/n2);
sp = sqrt(s1^2+s2^2);
z0 = (p1-p2-p0)/sp;
printf('Hypothesis testing on two proportions:'+atype+'-sided test.')
if atype == 'one' then
za = cdfnor('X',0,1,1-alpha,alpha)
if z0>za then
printf('Reject the null hypothesis H0:p1-p2=%g \n',p0)
printf('if the alternative hypothesis is H1:p1-p2>%g \n',p0)
elseif z0<-za then
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
All rights reserved
printf('Reject the null hypothesis H0:p1-p2=%g \n',p0)
printf('if the alternative hypothesis is H1:p1-p2<%g \n',p0)
else
printf('Do not reject the null hypothesis H0:p1-p2=%g \n',p0)
end
else
za = cdfnor('X',0,1,1-alpha/2,alpha/2)
if z0>za | z0<-za then
printf('Reject the null hypothesis H0:p1-p2=%g \n',p0)
else
printf('Do not reject the null hypothesis H0:p1-p2=%g \n',p0)
end
end
Examples of application of function htestmu2
Example 1. Test the null hypothesis H
0
:p
1
-p
2
= 0 at a significance level a = 0.05 based on the
values k
1
= 33, n
1
= 90, k
2
= 44, n
2
= 100. (a) Use a one-sided test. (b) Use a two-sided test.
-->getf('htestprop2')
-->//part (a)
-->[p1,p2,s1,s2,sp,z0,za]=htestprop2('one',10,200,45,100,0.3,0.1)
Hypothesis testing on two proportions:one-sided test.
Reject the null hypothesis H0:p1-p2= .3
if the alternative hypothesis is H1:p1-p2< .3
za = 1.2815516
z0 = - 13.44043
sp = .0520817
s2 = .0497494
s1 = .0154110
p2 = .45
p1 = .05
-->//part (b)
-->[p1,p2,s1,s2,sp,z0,za]=htestprop2('two',10,200,45,100,0.3,0.1)
Hypothesis testing on two proportions:two-sided test.
Reject the null hypothesis H0:p1-p2= .3
za = 1.6448536
z0 = - 13.44043
sp = .0520817
s2 = .0497494
s1 = .0154110
p2 = .45
p1 = .05
Characteristic and power equations
Consider the two-tailed test: H
0
: =
0
, H
1
:
0
. Suppose that it is correct to reject
H
0
because the true value of

is +
0
c , where
c
is a constant. The probability

of a
Type II error is given by
=

2
c n

2
c n

,
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
All rights reserved
where
( ) z
is the CDF of the standard normal distribution. Notice that the probability is a
function =

f , , n
c

. Curves representing

vs.

are called characteristic curves.


The complement of

is the power function = 1 -

= probability of rejecting the null


hypothesis when it is not true.
Power = 1-

= 1 - [

2
c n

2
c n

].
Characteristic and power curves are shown below for =0.05, and c/= 0.25, 0.50, 0.875, 1.0.
//Script to plot characteristic and power curves for alpha = 0.05
alpha = 0.05; z = cdfnor('X',0,1,1-alpha/2,alpha/2);
n=[0:1:100]; cs = [0.25,0.50,0.875,1.0];
b=zeros(length(n),length(cs));p=b;
for i = 1:length(n)
for j = 1:length(cs)
b(i,j) = ...
cdfnor('PQ',z-cs(j)*sqrt(n(i)),0,1) -cdfnor('PQ',-z-cs(j)*sqrt(n(i)),0,1);
p(i,j)=1-b(i,j);
end;
end;
xset('window',1);minn=min(n);maxn=max(n);minb=min(b);maxb=max(b);
rect1=[minn,minb,maxn,maxb];
plot2d([n',n',n',n'],[b(:,1),b(:,2),b(:,3),b(:,4)],[1,2,3,4],...
'111','c/sigma=0.25@c/sigma=0.50@c/sigma=0.875@c/sigma=1',rect1);
xtitle('Characteristic curves for alpha = 0.05','n','beta');
xset('window',2);minp=min(p);maxp=max(p);rect2=[minn,minp,maxn,maxp];
plot2d([n',n',n',n'],[p(:,1),p(:,2),p(:,3),p(:,4)],[1,2,3,4],...
'111','c/sigma=0.25@c/sigma=0.50@c/sigma=0.875@c/sigma=1',rect2);
xtitle('Power curves for alpha = 0.05','n','power');
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
All rights reserved
Hypothesis testing on one variance
Suppose that a sample of size
n
is taken out of a population of mean

and variance

2
.
The sample yields a mean x and variance s
x
. We will use these data to test the null
hypothesis, H
0
: =
2

0
2
, at a level of confidence

. The test statistic to be used is a chi-


square statistic,
=
0
2
( ) n 1 s
x
2

0
2
=
s
x
2

0
2
where
= n 1
represents the degrees of freedom of a

2
distribution.
Let
( ) F

2
= Pr[
<
2

2
] be the CDF corresponding to the chi-square distribution with

degrees of freedom.
Two-tailed test
In this case, the alternate hypothesis is H
1
:
2

0
2
, then we will reject the null hypothesis
if <

2
2

0
2
, or if <
0
2

2
2
, where
Pr[ <

2
2

2
] = 1

2
2
=

2
, and Pr[ <
1

2
2

2
] = 1

2
2
=
1

2
.
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
All rights reserved
One-tailed test
We consider two possibilities:
(1) If the alternate hypothesis is H
1
: <
0
2

2
, then we will reject the null hypothesis if
<

0
2
, where
Pr[ <

2
] = 1 ( ) F

2
=

.
(2) If the alternate hypothesis is H
1
: <
2

0
2
, then we will reject the null hypothesis if
<
0
2

1
2
, where
Pr[ <
1
2

2
] = 1 ( ) F

1
2
=
1
.
A function for hypothesis testing on one variance
The following function, htestsigma1, can be used to test the null hypothesis H
0
:
2
=
0
2
, at the
level of significance . There are two possible calls to the function:
[n,s,X0,Xa,X1a] = htestmu1(altype,alpha,sigma0_2,var,n)
[n,s,X0,Xa,X1a] = htestmu1(altype,alpha,sigma0_2,x)
In the first call the sample variance (s
2
) and the sample size (n) are given, besides the type of
alternative hypothesis (altype, which could be equal to one or two corresponding to one- or
two-sided tests), the level of significance, , and the value of
0
2
. In the second call, instead
of providing the sample variance and sample size, the user provides the actual sample as a
vector x.
The function returns n and s, the samples size and standard deviation, as well as the chi-
square test parameter, X0 = X
0
2
, and the values Xa and X1a which represent
2

and
2
1-
,
respectively, if using a one-sided test, or
2
/2
and
2
1- /2
, respectively, if using a one-sided
test.
A listing of the function follows:
function [n,s,X0,Xa,X1a] = htestsigma1(altype,alpha,sigma0_2,x,n)
//Hypothesis testing on one variance. Possible function calls:
//
// [s,X0,Xa,X1a] = htestmu1(altype,alpha,sigma0_2,s,n)
// [s,X0,Xa,X1a] = htestmu1(altype,alpha,sigma0_2,x)
//
// altype can be 'one' - for one-sided alternative hypothesis,
// or 'two' - for two sided alternative hypothesis
// alpha = level of significance (typical values = 0.01,0.05,0.10)
// sigma0 = value of population standard deviation being tested,
// i.e., H0:sigma^2 = sigma0^2
// x = sample variance (s^2) or vector containing sample (x)
// if x = sample variance, n = sample size
// X0 = test statistics
// Xa = X_alpha/2 for altype='two' or X_alpha for altype='one'
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
All rights reserved
// X1a = X_(1-alpha/2) for 'two' or X_(1-alpha) for 'one'
if altype<>'one' & altype<>'two' then
error('htestmu1 - select type of alternative hypothesis = one or two');
abort;
end;
[nargout,nargin] = argn(0)
if nargin == 4 then
if length(x)<1 then
error('htestmu1 - x must be a vector');
abort;
else
n = length(x);
s = st_deviation(x);
end
else
s = sqrt(x);
end;
printf(' \n');
printf('Hypothesis testing on one variance: ' ...
+ altype + '-side alternative hypothesis.\n')
printf(' \n');
X0 = (n-1)*s^2/sigma0_2;
if altype == 'one' then
Xa = cdfchi('X',n-1,1-alpha,alpha);
X1a = cdfchi('X',n-1,alpha,1-alpha);
if X0>Xa then
printf('Reject the null hypothesis H0:sigma^2=%g \n',sigma0_2);
printf('if the alternative hypothesis is H1:sigma^2>%g \n',sigma0_2);
elseif X0<X1a then
printf('Reject the null hypothesis H0:sigma^2=%g \n',sigma0);
printf('if the alternative hypothesis is H1:sigma^2<%g \n',sigma0_2);
else
printf('Do not reject the null hypothesis H0:sigma^2=%g \n',sigma0_2);
end
else
Xa = cdfchi('X',n-1,1-alpha/2,alpha/2);
X1a = cdfchi('X',n-1,alpha/2,1-alpha/2);
if X0>Xa | X0<X1a then
printf('Reject the null hypothesis H0:sigma^2=%g \n',sigma0_2);
else
printf('Do not reject the null hypothesis H0:sigma^2=%g \n',sigma0_2);
end
end;
Examples of application of function htestsigma1
Example 1. A sample of size 10 produces a variance of 20. With a confidence level of 0.05,
test the null hypothesis H
0
:
0
2
=25, using a two-sided test.
-->alpha=0.05; sigma0_2 = 25; var = 20; n = 10;
-->[n,s,X0,Xa,X1a]=htestsigma1('two',alpha,sigma0_2,var,n)
Hypothesis testing on one mean: two-side alternative hypothesis.
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
All rights reserved
Do not reject the null hypothesis H0:sigma^2=25
X1a = 2.7003895
Xa = 19.022768
X0 = 7.2
s = 4.472136
n = 10.
Example 2. Given the sample X = [3.5 2.2 1.5 4.2 3.2 1.4 5.6 2.3 4.8], with a confidence level
of 0.05, test the null hypothesis H
0
:s
0
2
=25, using a two-sided test.
-->alpha=0.05; sigma0_2 = 25;
-->X = [3.5 2.2 1.5 4.2 3.2 1.4 5.6 2.3 4.8]
X = ! 3.5 2.2 1.5 4.2 3.2 1.4 5.6 2.3 4.8 !
-->[n,s,X0,Xa,X1a]=htestsigma1('two',alpha,sigma0_2,X)
Hypothesis testing on one mean: two-side alternative hypothesis.
Reject the null hypothesis H0:sigma^2=25
X1a = 2.1797307
Xa = 17.534546
X0 = .6939556
s = 1.4726205
n = 9.
Hypothesis testing on two variances
If two populations are normal and independent samples of sizes n
1
and n
2
are drawn from them,
then the statistic
follows the F distribution with n
1
-1 degrees of freedom for the numerator and n
2
-1

degrees of
freedom for the denominator.
We can test the null hypothesis, H
0
: =

1
2

2
2
1 , against the two-tailed alternate hypothesis,
H
1
:

1
2

2
2
1 , where
1
2
and
2
2
are the variances of populations 1 and 2, respectively,
by taking samples from the two populations and evaluating their variances, s
1
2
and s
2
2
. Let
the size of sample 1 and 2 be n
1
and n
2
, respectively, and let

be the level of confidence.


Let s
M
2
and s
m
2
be the largest and smallest of the variances s
1
2
and s
2
2
, respectively.
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
All rights reserved
Calculate the statistic F
0
=
s
M
2
s
m
2
, and the quantiles F

2
, with the appropriate degrees of
freedom for numerator and denominator. Reject the null hypothesis if F
0
> F

2
.
If the alternate hypothesis is H
1
:

1
2

2
2
> 1, use the statistic F
0
=
s
1
2
s
2
2
, reject the null
hypothesis if, F
0
> F

.
If, on the other hand, the alternate hypothesis is H
1
:

1
2

2
2
< 1, use the statistic F
0
=
s
2
2
s
1
2
,
reject the null hypothesis if F
0
> F

.
A function for hypothesis testing with two variances
The following function, htestsigma2, can be used for hypothesis testing in two variances.
Possible calls to the function are:
[X1Info,X2Info,nuInfo,F0,Fa]= htestsigma2()
[X1Info,X2Info,nuInfo,F0,Fa]= htestsigma2(Xdata)
[X1Info,X2Info,nuInfo,F0,Fa]= htestsigma2(X1data,X2data)
Xdata, X1data, and X2data are row vectors of data. X1Info is a vector that contains the sample
size, n1, and the standard deviation, s1, of sample 1. X2Info is a vector containing n2, and s2.
The vector nuInfo contains the degrees of freedom for the numerator and denominator,
respectively, of the F distribution. F0 is the F parameter used in the test. Fa represents F

.
The function operates interactively requesting information from the user and provides verbose
information on the type of test parameter and alternative hypothesis used, as well as providing
a recommendation about the rejection or not-rejection of the null hypothesis.
If the function call [X1Info,X2Info,nuInfo,F0,Fa]= htestsigma2() is used, the user will be
prompted for the summary information on the samples, i.e., the samples sizes and standard
deviations. If the function call [X1Info,X2Info,nuInfo,F0,Fa]= htestsigma2(Xdata) is used, the
user is asked to identify the vector Xdata as sample 1 or sample 2, and then is prompted for
the summary data for the other sample. Finally, if the function call
[X1Info,X2Info,nuInfo,F0,Fa] = htestsigma2(X1data,X2data) is used, the function calculates the
sample summary data all by itself.
The function will also prompt the user for the following information:
The level of confidence of the test, i.e.,
The type of alternative hypothesis to be used, i.e., one- or two-sided
The type of one-sided alternative hypothesis to be tested.
A listing of the function is shown below.
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
All rights reserved
function [X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X1,X2)
[nargout,nargin]=argn(0)
if nargin == 0 then
X1in = input('For sample 1 enter n, s :')
n1 = X1in(1);s1 = X1in(2);
X2in = input('For sample 2 enter n, s :')
n2 = X2in(1);s2 = X2in(2);
elseif nargin == 1 then
disp('You entered a vector as input to the function.')
disp('Do you want this vector to represent sample 1 or 2?')
idsample = input(' ')
if idsample == 1 then
n1 = length(X1); s1 = st_deviation(X1);
printf('n1 = %g s1 = %g',n1,s1)
X2in = input('For sample 2 enter n, s :')
n2 = X2in(1);s2 = X2in(2);
else
n2 = length(X1); s2 = st_deviation(X1);
printf('n2 = %g s2 = %g',n2,s2)
X1in = input('For sample 1 enter n, s :')
n1 = X1in(1);s1 = X1in(2);
end
else
n1 = length(X1); s1 = st_deviation(X1);
printf('n1 = %g s1 = %g',n1,s1)
n2 = length(X2); s2 = st_deviation(X2);
printf('n2 = %g s2 = %g',n2,s2)
end
X1Info = [n1,s1]; X2Info = [n2,s2];
disp('Enter the level of confidence, alpha, for the test:')
disp('(Typical values: 0.01, 0.05, 0.10)')
alpha = input(' ');
disp('Enter the type of alternative hypothesis to test:')
disp(' 1 - one-sided 2 - two-sided');
atype = input(' ');
if atype == 1 then
disp('Enter the type of one-sided alternative hypothesis to test:')
disp(' 1 - H1:sigma1^2/sigma2^2>1');
disp(' 2 - H1:sigma1^2/sigma2^2<1');
onetype = input(' ');
printf('Hypothesis testing on two variances: one-sided test.\n')
if onetype == 1 then
nuInfo = [n1-1,n2-1];
F0 = (s1/s2)^2; Fa = cdff('F',n1-1,n2-1,1-alpha,alpha)
printf('The alternative hypothesis is H1:sigma1^2/sigma2^2>1');
if F0>Fa then
printf('Reject the null hypothesis H0:sigma1^2/sigma2^2=1.\n');
else
printf('Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1.\n');
end
else
nuInfo = [n2-1,n1-1];
F0 = (s1/s2)^2; Fa = cdff('F',n2-1,n1-1,1-alpha,alpha);
printf('The alternative hypothesis is H1:sigma1^2/sigma2^2<1.\n');
if F0>Fa then
printf('Reject the null hypothesis H0:sigma1^2/sigma2^2=1.\n');
else
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
All rights reserved
printf('Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1.\n');
end
end
else
printf('Hypothesis testing on two variances: two-sided test.\n')
if s1>=s2 then
sM = s1; nM = n1; sm = s2; nm = n2;
else
sM = s2; nM = n2; sm = s1; nm = n1;
end;
nuInfo = [nM,nm];
F0 = (sM/sm)^2; Fa = cdff('F',nM-1,nm-1,1-alpha/2,alpha/2);
if F0>Fa then
printf('Reject the null hypothesis H0:sigma1^2/sigma2^2=1. \n');
else
printf('Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1. \n');
end
end;
Examples of application of function htestsigma2
In the following examples, user input is given in italics.
Example 1. Given two samples with n
1
= 25, s
1
= 2.3, n
2
= 15, s
2
= 3.2, test the null hypothesis
H
0
:
1
2
/
2
2
= 1, at a significance level of 0.10, against (a) a two-sided hypothesis; (b) a one-
sided hypothesis, H
1
:
1
2
/
2
2
> 1; and, (c) a one-sided hypothesis, H
1
:
1
2
/
2
2
< 1.
-->//Part (a) - Two sided hypothesis
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2()
For sample 1 enter n, s : 25, 2.3
For sample 2 enter n, s : 15, 3.2
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Hypothesis testing on two means: two-sided test.
Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 2.1297969
F0 = 1.9357278
nuInfo = ! 14. 24. !
X2Info = ! 15. 3.2 !
X1Info = ! 25. 2.3 !
-->//Part (b) - One-sided hypothesis, H1: sigma1^2/sigma2^2 > 1
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2()
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
All rights reserved
For sample 1 enter n, s : 25, 2.3
For sample 2 enter n, s : 15, 3.2
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
1
Enter the type of one-sided alternative hypothesis to test:
1 - H1:sigma1^2/sigma2^2>1
2 - H1:sigma1^2/sigma2^2<1
1
Hypothesis testing on two variances: one-sided test.
The alternative hypothesis is H1:sigma1^2/sigma2^2>1
Do not eject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 1.937663
F0 = .5166016
nuInfo = ! 24. 14. !
X2Info = ! 15. 3.2 !
X1Info = ! 25. 2.3 !
-->//Part (c) - One-sided hypothesis, H1: s1^2/s2^2 < 1
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2()
For sample 1 enter n, s : 25, 2.3
For sample 2 enter n, s : 15, 3.2
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
1
Enter the type of one-sided alternative hypothesis to test:
1 - H1:sigma1^2/sigma2^2>1
2 - H1:sigma1^2/sigma2^2<1
2
Hypothesis testing on two variances: one-sided test.
The alternative hypothesis is H1:sigma1^2/sigma2^2<1
Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 1.7974154
F0 = .5166016
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
All rights reserved
nuInfo = ! 14. 24. !
X2Info = ! 15. 3.2 !
X1Info = ! 25. 2.3 !
Example 2. Given the sample X
1
= [3.2, 2.1, 4.5, 6.2, 3.4], and a second sample with n
2
=10,
s
2
=0.5, test the null hypothesis H
0
:
1
2
/
2
2
= 1, at a significance level of 0.05, against (a) a two-
sided hypothesis; (b) a one-sided hypothesis, H
1
:
1
2
/
2
2
> 1.
--> Example 2 - part (a)
--> X1 = [3.2, 2.1, 4.5, 6.2, 3.4]
X1 =
! 3.2 2.1 4.5 6.2 3.4 !
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X1)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
1
n1 = 5 s1 = 1.55145
For sample 2 enter n, s :
10, 0.5
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.05
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Hypothesis testing on two variances: two-sided test.
Reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 4.7180785
F0 = 9.628
nuInfo = ! 4. 9. !
X2Info = ! 10. .5 !
X1Info = ! 5. 1.5514509 !
--> Example 2 - part (b)
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X1)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
1
n1 = 5 s1 = 1.55145
For sample 2 enter n, s :
10, 0.5
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
All rights reserved
0.05
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter the type of one-sided alternative hypothesis to test:
1 - H1:sigma1^2/sigma2^2>1
2 - H1:sigma1^2/sigma2^2<1
1
Hypothesis testing on two variances: one-sided test.
The alternative hypothesis is H1:sigma1^2/sigma2^2>1
Reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 3.6330885
F0 = 9.628
nuInfo = ! 4. 9. !
X2Info = ! 10. .5 !
X1Info = ! 5. 1.5514509 !
Example 3. Given the sample X
2
= [0.9,11.1,0.2,3.4,5.6,2.1,8.2,3.2] and sample 1 with n
1
= 22
and s
1
= 0.5, test the null hypothesis H
0
:
1
2
/
2
2
= 1, at a significance level of 0.05, against (a)
a two-sided hypothesis; (b) a one-sided hypothesis, H
1
:
1
2
/
2
2
> 1.
-->X2 = [0.9,11.1,0.2,3.4,5.6,2.1,8.2,3.2]
X2 =
! .9 11.1 .2 3.4 5.6 2.1 8.2 3.2 !
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X2)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
2
n2 = 8 s2 = 3.7485
For sample 1 enter n, s :
22, 0.5
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Hypothesis testing on two variances: two-sided test.
Reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 4.1789302
F0 = 6.245
nuInfo = ! 7. 21. !
X2Info = ! 8. 3.7484997 !
X1Info = ! 22. 1.5 !
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
All rights reserved
--> Example 3 - part (b)
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X2)
You entered a vector as input to the function.
Do you want this vector to represent sample 1 or 2?
2
n2 = 8 s2 = 3.7485
For sample 1 enter n, s :
22, 0.5
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.01
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Enter the type of one-sided alternative hypothesis to test:
1 - H1:sigma1^2/sigma2^2>1
2 - H1:sigma1^2/sigma2^2<1
1
Hypothesis testing on two variances: one-sided test.
The alternative hypothesis is H1:sigma1^2/sigma2^2>1
Do not eject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 6.1323795
F0 = .1601281
nuInfo = ! 21. 7. !
X2Info = ! 8. 3.7484997 !
X1Info = ! 22. 1.5 !
Example 4 - Using the samples X
1
and X
2
, defined in examples 2 and 3, respectively, test the
null hypothesis H
0
:
1
2
/
2
2
= 1, at a significance level of 0.05, against (a) a two-sided
hypothesis; (b) a one-sided hypothesis, H
1
:
1
2
/
2
2
> 1.
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X1,X2)
n1 = 5 s1 = 1.55145
n2 = 8 s2 = 3.7485
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
2
Hypothesis testing on two means: two-sided test.
Do not reject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 6.0942109
Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz
All rights reserved
F0 = 5.837661
nuInfo = ! 8. 5. !
X2Info = ! 8. 3.7484997 !
X1Info = ! 5. 1.5514509 !
-->[X1Info,X2Info,nuInfo,F0,Fa]=htestsigma2(X1,X2)
n1 = 5 s1 = 1.55145
n2 = 8 s2 = 3.7485
Enter the level of confidence, alpha, for the test:
(Typical values: 0.01, 0.05, 0.10)
0.10
Enter the type of alternative hypothesis to test:
1 - one-sided 2 - two-sided
1
Enter the type of one-sided alternative hypothesis to test:
1 - H1:sigma1^2/sigma2^2>1
2 - H1:sigma1^2/sigma2^2<1
1
Hypothesis testing on two variances: one-sided test.
The alternative hypothesis is H1:sigma1^2/sigma2^2>1
Do not eject the null hypothesis H0:sigma1^2/sigma2^2=1.
Fa = 2.9605341
F0 = .1713015
nuInfo = ! 4. 7. !
X2Info = ! 8. 3.7484997 !
X1Info = ! 5. 1.5514509 !
Chi-square criteria for goodness of fitting
Function histnorm, introduced in Chapter 15, produces a histogram for a given data set, x,
based on a number of class boundaries, xclass. The function, whose call is
[chi2,cm,f] = freqdist(x,xclass)
returns vectors of class marks, cm, and frequency, f, as well as the parameter chi2
corresponding to a chi-square statistic calculated as

=
k
i i
i i
fc
fc f
1
2
2
,
) (

where f
i
is the actual frequency count for the ith class, fc
i
is the estimated frequency count
obtained from the normal distribution for the ith class, and k is the number of classes in the
frequency distribution.
The parameter
2
, defined above, follows the chi-square distribution with = k-3 degrees of
freedom, where k is the number of classes in the histogram. To produce a fitting of the
normal distribution based on a sample of size n we use not only the sample size, n, but also the
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
All rights reserved
mean value,x, and the sample standard deviation, s. Thus, the number of degrees of
freedom is k-3, since three parameters are already known in the data fitting.
The idea of goodness of fitting for the normal distribution, for example, means to test the
hypothesis H
0
: {the data fits the normal distribution with =x and = s}, tested against the
alternative hypothesis H
1
: {the data does not fit the normal distribution with =x and = s}.
The latter is a form of one-sided alternative hypothesis. Given a significance level , we
calculate the parameter
2
based on the observed and predicted frequencies, and compare its
value against the parameter
2

obtained from the chi-square distribution with k-3 degrees of
freedom. If
2
>

, we reject the null hypothesis H


0
.
Examples of goodness-of-fitting for the normal distribution
Example 1. Consider the sample x loaded below into SCILAB. Its frequency distribution is to be
obtained using the class boundaries indicated in vector xclass. The following SCILAB
commands are used to generate the test statistic for testing the hypothesis H
0
: {the data fits
the normal distribution with =x and = s}:
-->x=[2.3,3.2,1.1,4.5,6.2,8.4,1.3,2.2,4.5,3.6,2.2,1.0];
-->min(x),max(x)
ans = 1.
ans = 8.4
-->xclass = [1:0.5:9]; k = length(xclass)-1, nu = k-3
k = 16.
nu = 13.
-->[chi2,xmark,f] = histnorm(x,xclass);
-->chi2
chi2 = 33.263408
-->chi_a = cdfchi('X',nu,1-alpha,alpha)
chi_a = 19.811929
The results are
2
= 33.263408,
2

= 19.811929. Because
2
>

, we reject the null


hypothesis that the data in vector x belongs to a normal distribution. The histogram is shown
in the following figure.
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
All rights reserved
Example 1. In this second example the data analyzed is generated from a normal distribution
using function grand. The null hypothesis H
0
: {the data fits the normal distribution with =x
and = s} is tested at a level of significance of 0.01.
-->X=grand(1,200,'nor',350,100);
-->min(X),max(X)
ans = 119.33031
ans = 655.10351
-->Xclass=[100:50:700];k=length(Xclass)-1,nu=k-3
k = 12.
nu = 9.
-->[chi2,Xmark,Xfreq] = histnorm(X,Xclass);
-->chi2
chi2 = 10.142254
-->alpha = 0.01;
-->chi_a = cdfchi('X',nu,1-alpha,alpha)
chi_a = 21.665994
The results are
2
= 10.142254,
2

= 21.665994. Because
2
<

, we cannot reject the null


hypothesis that the data in vector x belongs to a normal distribution. The histogram is shown
in the following figure.
Examples of goodness-of-fitting for the beta distribution
The approach followed in function histnorm for checking the goodness-of-fit of a sample to the
normal distribution can be used, in general, for other probability distributions. The key is to
determine the parameters of the distribution based on statistics of the data. For the normal
distribution, X ~ N(,), for example, we use the parameters =x and = s
x
. In this section
we present a function, histbeta, that can be used to check the goodness-of-fit of data sets to
the beta distribution. The beta distribution, introduced in Chapter 15, requires two
parameters, and , which can be obtained from a sample by making =x and = s
x
and
solving the following two equations
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
All rights reserved
. ) )( 1 (
,
2
2 2

+ + +

= =
+
= =
x X
s x
The solution can be accomplished numerically by using SCILABs function fsolve. The listing of
function histbeta, incorporating such numerical solution, follows. Notice that the function
returns not only the parameter
2
(chi2), the class mark (cmark), and the frequency count
(fcount), but also the parameters of the beta distribution (a) and (b).
function [a,b,chi2,cmark,fcount]=histbeta(x, xclass)
//This function calculates the frequency distribution
//for the data in (row) vector x according to the
//class boundaries contained in the (row) vector
//xclass. It also produces a histogram of the
//data and the beta distribution that best fit the data
//Note: the beta distribution works only for data between 0 and 1
//
//Typical call: [chi2,cm,f] = freqdist(x,xclass)
//where cm = class marks, f = frequency count,
// chi2 = chi-square parameter for the fitting
if min(x)<0 | max(x)>1 then
error('histbeta - sample contains data outside of [0,1]');
abort;
end;
[m n] = size(x); //Sample size
[m nB] = size(xclass); //Number of class boundaries
k = nB - 1; //Number of classes
//Calculate class marks
cmark = zeros(1,k);
for ii = 1:k
cmark(ii) = 0.5*(xclass(ii)+xclass(ii+1));
end
//Initialize frequency counts to zero
fcount=zeros(1,k);
fbelow=0; fabove=0;
//Accumulate frequency counts
for ii = 1:n
if x(ii) < xclass(1)
fbelow = fbelow + 1;
elseif x(ii) > xclass(nB)
fabove = fabove + 1;
else
for jj = 1:k
if x(ii)>= xclass(jj) & x(ii)< xclass(jj+1)
fcount(jj) = fcount(jj) +1;
end
end
end
end
//Calculate sample size, mean, standard deviation, and
//minimum and maximum values for the plot
nn = sum(fcount);
xbar = mean(x); sx = st_deviation(x);
xmin = min(xclass); xmax = max(xclass);
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
All rights reserved
//Calculate values of a (alpha) and b (beta)
deff('[w]=ff(xx)',['ff1=xbar-xx(1)/(xx(1)+xx(2))';...
'ff2=sx^2-xx(1)*xx(2)/((xx(1)+xx(2)+1)*(xx(1)+xx(2))^2)';...
'w=[ff1;ff2]']);
xx0 = [1;1];xxs=fsolve(xx0,ff); a = xxs(1); b = xxs(2);
//Calculate predicted frequencies
pk = [];
for j = 1:k+1
pk = [pk cdfbet("PQ",xclass(j),1-xclass(j),a,b)];
end;
p_in_classes = pk(k+1)-pk(1);
pxclass = pk(2:k+1) - pk(1:k);
fc = pxclass*nn*p_in_classes;
//Calculate chi-square parameter
chi2=0;
for j = 1:length(fc)
chi2 = chi2 + (fcount(j)-fc(j))^2/fc(j);
end;
//Produce beta distribution for data
Dx = (xmax-xmin)/100;
xx = [xmin:Dx:xmax];
xxx = xx(1:100) + Dx/2;
pkk = [];
for j = 1:101
pkk = [pkk cdfbet("PQ",xx(j),1-xx(j),a,b)];
end;
pp = pkk(2:101) - pkk(1:100);
fcc = pp*p_in_classes*nn*100/k;
//Determine plot rectangle
ymin = 0;
ymaxf = max(fcount); ymaxy = max(fcc);
ymax = max(ymaxf,ymaxy);
ymax = int(1.1*ymax);
plotrectangle = [xmin ymin xmax ymax];
//plot the histogram and normal curve
xp = xclass(1:k);
xset('window',1);xbasc(1);
plot2d2('onn',xclass',[fcount fcount(k)]',[1],'011','y',[xmin ymin xmax ymax]);
plot2d3('onn',xp',fcount',[1],'000');
plot2d(xxx',fcc',[2],'000');
xtitle('Histogram with normal curve','x','frequency');
//end function histbeta
For this function we would be testing the hypothesis H
0
: {the data fits the beta distribution},
tested against the alternative hypothesis H
1
: {the data does not fit the normal distribution}.
As with the test of the normal distribution testing, given a significance level , we calculate
the parameter
2
based on the observed and predicted frequencies, and compare its value
against the parameter
2

obtained from the chi-square distribution with k-3 degrees of
freedom. If
2
>

, we reject the null hypothesis H


0
. Please notice that the beta distribution
is used for data whose values are between 0 and 1 only.
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
All rights reserved
Example 1. Data from a uniform distribution
-->rand('info')
ans = uniform
-->X=rand(1,25);min(X),max(X)
ans = .0002211
ans = .9329616
-->Xclass=[0:0.1:1];k=length(Xclass)-1, nu = k - 3
k = 10.
nu = 7.
-->[a,b,chi2,Xmark,Xfreq]=histbeta(X,Xclass)
Xfreq =
! 2. 1. 5. 4. 0. 2. 5. 2. 3. 1. !
Xmark =
column 1 to 8
! .05 .15 .25 .35 .45 .55 .65 .75 !
column 9 to 10
! .85 .95 !
chi2 = 9.6996796
b = 1.127886
a = 1.0598387
-->alpha=0.01;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a =
18.475307
-->alpha=0.05;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a =
14.06714
-->alpha=0.1;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a =
12.017037
At confidence levels of = 0.01, 0.05, and 0.10, we cannot reject the null hypothesis that the
data fits a beta distribution.
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
All rights reserved
Example 2. Data generated from a normal distribution. Data X is generated from a normal
distribution. Data Y is obtained from X so that values of Y to are between 0 and 1.
-->rand('normal')
-->X = rand(1,100);min(X), max(X)
ans = - 2.0552251
ans = 1.9347752
-->Y = (X-min(X))/(max(X)-min(X));min(Y),max(Y)
ans = 0.
ans = 1.
-->Yclass = [0:0.1:1];k = length(Yclass)-1, nu = k - 3
k = 10.
nu = 7.
-->[a,b,chi2,Ymark,Yfreq]=histbeta(Y,Yclass)
Yfreq =
! 1. 4. 12. 10. 21. 17. 12. 10. 6. 6. !
Ymark =
column 1 to 8
! .05 .15 .25 .35 .45 .55 .65 .75 !
column 9 to 10
! .85 .95 !
chi2 = 8.9939089
b = 1.9850001
a = 2.2248757
-->alpha=0.01;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 18.475307
-->alpha=0.05;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 14.06714
-->alpha=0.1;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 12.017037
As in Example 1, at confidence levels of = 0.01, 0.05, and 0.10, we cannot reject the null
hypothesis that the data fits a beta distribution.
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
All rights reserved
Example 3 - Data generated from a beta distribution with = 0.5 and = 6.
-->X = grand(1,50,'bet',0.5,6);min(X),max(X)
ans = .0001030
ans = .3289092
-->Xclass = [0:0.05:0.35]; k = length(Xclass)-1, nu = k-3
k = 6.
nu = 3.
-->[a,b,chi2,Xmark,Xfreq]=histbeta(X,Xclass)
Xfreq = ! 25. 10. 5. 3. 3. 3. !
Xmark = ! .025 .075 .125 .175 .225 .275 !
chi2 = 3.4613321
b = 8.0386858
a = .6906572
-->alpha=0.01;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 11.344867
-->alpha=0.05;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 7.8147279
-->alpha=0.1;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 6.2513886
As expected, at confidence levels of = 0.01, 0.05, and 0.10, we cannot reject the null
hypothesis that the data fits a beta distribution.
Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
All rights reserved
The three examples above have produced goodness-of-fit results that indicate that we should
not reject the hypothesis that the data belongs to a beta distribution. These results indicate
the versatility of the distribution and the variety of shapes it can fit by using the values of
and obtained from the set of equations
. ) )( 1 (
,
2
2 2

+ + +

= =
+
= =
x X
s x .
On the other hand, if we force the values of and , rather than using those from the two
equations above, we may find situations where the hypothesis of the data fitting the required
beta distribution must be rejected. To try such cases we modify function histbeta to create
function histbeta1 which requires that the values of and be given by the user:
function [chi2,cmark,fcount]=histbeta1(x,xclass,a,b)
//This function calculates the frequency distribution
//for the data in (row) vector x according to the
//class boundaries contained in the (row) vector
//xclass. It also produces a histogram of the
//data and the beta distribution that best fit the data
//Note: the beta distribution works only for data between 0 and 1
//
//Typical call: [chi2,cm,f] = freqdist(x,xclass)
//where cm = class marks, f = frequency count,
// chi2 = chi-square parameter for the fitting
if min(x)<0 | max(x)>1 then
error('histbeta - sample contains data outside of [0,1]');
abort;
end;
[m n] = size(x); //Sample size
[m nB] = size(xclass); //Number of class boundaries
k = nB - 1; //Number of classes
//Calculate class marks
cmark = zeros(1,k);
for ii = 1:k
cmark(ii) = 0.5*(xclass(ii)+xclass(ii+1));
end
Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz
All rights reserved
//Initialize frequency counts to zero
fcount=zeros(1,k);
fbelow=0; fabove=0;
//Accumulate frequency counts
for ii = 1:n
if x(ii) < xclass(1)
fbelow = fbelow + 1;
elseif x(ii) > xclass(nB)
fabove = fabove + 1;
else
for jj = 1:k
if x(ii)>= xclass(jj) & x(ii)< xclass(jj+1)
fcount(jj) = fcount(jj) +1;
end
end
end
end
//Calculate sample size, mean, standard deviation, and
//minimum and maximum values for the plot
nn = sum(fcount);
xbar = mean(x); sx = st_deviation(x);
xmin = min(xclass); xmax = max(xclass);
//Calculate predicted frequencies
pk = [];
for j = 1:k+1
pk = [pk cdfbet("PQ",xclass(j),1-xclass(j),a,b)];
end;
p_in_classes = pk(k+1)-pk(1);
pxclass = pk(2:k+1) - pk(1:k);
fc = pxclass*nn*p_in_classes;
//Calculate chi-square parameter
chi2=0;
for j = 1:length(fc)
chi2 = chi2 + (fcount(j)-fc(j))^2/fc(j);
end;
//Produce beta distribution for data
Dx = (xmax-xmin)/100;
xx = [xmin:Dx:xmax];
xxx = xx(1:100) + Dx/2;
pkk = [];
for j = 1:101
pkk = [pkk cdfbet("PQ",xx(j),1-xx(j),a,b)];
end;
pp = pkk(2:101) - pkk(1:100);
fcc = pp*p_in_classes*nn*100/k;
//Determine plot rectangle
ymin = 0;
ymaxf = max(fcount); ymaxy = max(fcc);
ymax = max(ymaxf,ymaxy);
ymax = int(1.1*ymax);
plotrectangle = [xmin ymin xmax ymax];
//plot the histogram and normal curve
xp = xclass(1:k);
xset('window',1);xbasc(1);
plot2d2('onn',xclass',[fcount fcount(k)]',[1],'011','y',[xmin ymin xmax ymax]);
Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
All rights reserved
plot2d3('onn',xp',fcount',[1],'000');
plot2d(xxx',fcc',[2],'000');
xtitle('Histogram with beta distribution','x','frequency');
//end function histbeta1
Applying function histbeta1 to the data of Example 3 shown above, with = 1 and = 5,
produces the following results:
-->[chi2,Xmark,Xfreq]=histbeta1(X,Xclass,1,5)
Xfreq =
! 25. 10. 5. 3. 3. 3. !
Xmark =
! .025 .075 .125 .175 .225 .275 !
chi2 = 28.784445
-->alpha=0.01;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 11.344867
-->alpha=0.05;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 7.8147279
-->alpha=0.1;chi_a=cdfchi('X',nu,1-alpha,alpha)
chi_a = 6.2513886
We will reject the null hypothesis that the data follows the beta distribution with = 1 and =
5, under levels of significance = 0.01, 0.05, or 0.10. [Note: be careful distinguishing the level
of confidence from the beta distribution parameter ]. A plot of the sample histogram and
the fitted data is shown below.
Chi-square criteria for RC tables
The terms RC tables refers to tables summarizing frequency counts of observations classified
according to two different criteria. For example, the following table summarizes the
frequency counts of slope damages due to rain summarized by a geotechnical engineer. The
slope damages are classified according to the depth of erosion into three categories (0-2.5 cm,
Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
All rights reserved
2.5-5.0 cm, > 5 cm) or according to the percentage of vegetation cover into four categories (0-
25%, 25%-50%, 50%-75%, and 75%-100%).
vegetation cover
erosion depth 0-25% 25%-50% 50%-75% 75%-100% Totals
0-2.5 cm 17 23 15 6 61
2.5 cm - 5.0 cm 24 13 40 10 87
>5.0 cm 20 10 20 5 55
Totals 61 46 75 21 203
If the vegetation cover and erosion depth criteria are independent, the expected frequency
counts for each of the cells in the table can be calculated by multiplying the corresponding row
total times the corresponding column total and dividing by the overall total (203, in this case).
For example, the expected frequency count for erosion depth of 2.5 cm - 5.0 cm and
vegetation cover of 50%-75% will be 87 75/203 = 32.14. This procedure follows from the
calculation of probabilities for independent events, i.e., if A = { erosion depth of 2.5 cm - 5.0
cm } and B = { vegetation cover of 50%-75% }, P(A) = 87/203, P(B) = 75/203, and P(AB) =
P(A)P(B) = (87/203)(75/203) = 8775/203
2
. Since a probability represents a relative frequency,
the actual frequency count will be the probability multiplied by the total number of
occurrences (i.e., 203) to produce (8775/203
2
) 203 = 87 75/203 = 32.14.
The chi-square criteria can be used to determine how well the predicted frequency counts fc
ij
approximate the measured frequency counts f
ij
. The chi-square statistic to be used is
,
) (
1 1
2
2

= =

=
n
i
m
j ij
ij ij
fc
fc f

for i=1,2,,n (rows in the table) and j=1,2,,m (columns in the table). The parameter thus
defined will follow the chi-square distribution with = (n-1)(m-1) degrees of freedom.
The following function, RC, calculates the predicted frequency count, the chi-square statistics,
the degrees of freedom, and provides a recommendation regarding the rejection or not
rejection of the null hypothesis H
0
:{criteria for the RC table are independent}. A listing of
the function follows:
function [nu,chi_a,chi2,fPred] = RC(fObs,alpha)
//Determines the chi-square statistic for an RxC table
//passed on to the function as a matrix fObs. The
//function calculates the predicted frequency counts also.
TR = sum(fObs,'c'); TC = sum(fObs,'r'); TT = sum(fObs);
[n m] = size(fObs); fPred = zeros(fObs);
chi2 = 0.0;
for i = 1:n
for j = 1:m
fPred(i,j) = TR(i)*TC(j)/TT;
chi2 = chi2 + (fObs(i,j)-fPred(i,j))^2/fPred(i,j);
end;
end;
nu = (n-1)*(m-1);
chi_a=cdfchi('X',nu,1-alpha,alpha);
if chi2 > chi_a then
printf('Reject the null hypothesis H0:independent criteria.')
Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz
All rights reserved
else
printf('Do not reject the null hypothesis H0:independent criteria')
end;
As an example, we will use the RC table presented earlier to check the hypothesis of
independence of the classification criteria:
-->fObs = [17,23,15,6;24,13,40,10;20,10,20,5]
fObs =
! 17. 23. 15. 6. !
! 24. 13. 40. 10. !
! 20. 10. 20. 5. !
-->[nu,chi_a,chi2,fPred]=RC(fObs,0.1)
Reject the null hypothesis H0:independent criteria
fPred =
! 18.330049 13.82266 22.536946 6.3103448 !
! 26.142857 19.714286 32.142857 9. !
! 16.527094 12.463054 20.320197 5.6896552 !
chi2 = 14.524802
chi_a = 10.644641
nu = 6.
Exercises
[1]. A sample of 150 electric components is tested for temperature response by measuring the
temperature of the component, X, after 10 minutes of operation. The mean value of the
temperature for the sample is found to bex = 86
o
F with a standard deviation of s
x
= 5.5
o
F. The
records from the factory indicate that the standard deviation of the 10-minute temperature
measurements for the entire population of electric components is = 10
o
F. Obtain confidence
intervals for the mean value of the 10-minute temperature measurement for the population of
electric components m using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[2]. A sample of 10 electric bulbs is used to determine the number of on-off cycles to produce
failure of the filament. The data shows a mean value of 1500 cycles with a standard deviation
of 50 cycles. Obtain confidence intervals for the mean value of the 10-minute temperature
measurement for the population of electric components m using levels of confidence of (a) =
0.01, (b) = 0.05, and (c) = 0.10.
[3]. Records of traffic accidents in the main road through a small town shows that in the last
300 days there have been 10 days where a major closure of the road has been registered.
Obtain confidence intervals for the proportion of days of closure of the road using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[4]. Measurements of the specific density in 10 soil samples show values of 1.25, 1.30, 1.45,
1.55, 1.20, 1.23, 1.90, 1.40, 1.35, 1.40. Obtain confidence intervals for the mean value of the
specific density using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[5]. In order to determine the need to keep a service station open after the regular closing
hour of 5:00 pm, a test is carried out in which the station is kept open for an extra hour for 20
Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
All rights reserved
business days. The number of clients visiting the service station after 5 pm during those 20
days are the following:
2 3 5 6 3 2 1 0 3 4 2 5 7 8 7 6 5 8 2 3
Obtain confidence intervals for the mean value of the number of clients visiting the service
station after 5:00 pm using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[6]. In reference to problem [5], a successful day is one in which 4 or more clients show up at
the service station after 5:00 pm. Obtain confidence intervals for the proportion of successful
days using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[7]. A routine construction project consists of two stages. If T
1
represents the time required
to complete stage 1 and T
2
the time required to complete stage 2, determine the confidence
interval for the total construction time if for samples of sizes n
1
= 10 and n
2
= 8, the mean
completion times aret
1
= 25 days andt
2
= 40 days, with standard deviations s
1
= 3 days and s
2
= 5 days . Use levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[8]. To put together a mechanical component a factory needs to produce two separate
metallic pieces that then get assembled together. The time required to finish the first piece
has a mean value of t
1
= 12 minutes, while the second piece requires an average of t
2
= 14.0
minutes. The corresponding standard deviations are deviations s
1
= 3 days and s
2
= 5 days. The
sample sizes used in the measurements are n
1
= 50 and n
2
= 45. Of interest for optimizing the
operation of the factory is the difference between the times of completion of the two pieces,
i.e., T
1
-T
2
. Obtain confidence intervals for the time difference using levels of confidence of (a)
= 0.01, (b) = 0.05, and (c) = 0.10.
[9]. Obtain confidence intervals for the variance
2
for the data of problem [1] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[10]. Obtain confidence intervals for the variance
2
for the data of problem [2] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[11]. Obtain confidence intervals for the variance
2
for the data of problem [3] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[12]. Obtain confidence intervals for the variance
2
for the data of problem [4] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[13]. Obtain confidence intervals for the variance
2
for the data of problem [5] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[14]. Obtain confidence intervals for the variance
2
for the data of problem [6] using levels of
confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[15]. Obtain confidence intervals for the variance
2
for each of the standard deviations in
problem [7] using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[16]. Obtain confidence intervals for the variance
2
for each of the standard deviations in
problem [8] using levels of confidence of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[17]. A sample of 20 measurements of the density of a liquid indicate a mean value of x =
3.25 mg/l with a standard deviation of s
x
= 0.25 mg/l. The manufacturer of the liquid claims
that the mean density of the population is = 3.30 mg/l. Should the manufacturers claim be
Download at InfoClearinghouse.com 62 2001 Gilberto E. Urroz
All rights reserved
rejected at levels of confidence of (a) = 0.01?, (b) = 0.05?, (c) = 0.10?. {Note: the
alternative hypothesis is 3.30 mg/l}.
[18]. A more detailed study for the liquid density of problem [17] includes measurements in
300 liquid samples. This study reveals that the mean value of the 300 samples isx = 3.35
mg/l with a standard deviation of s
x
= 0.15 mg/l. Should the manufacturers claim be rejected
at levels of confidence of (a) = 0.01, (b) = 0.05, (c) = 0.10, based on the new evidence?
[19]. Tests of a new pavement are conducted by measuring the time required for different
cars to stop after reaching speeds of 35 mph. The following stop times are recorded for 12 car
tests:
12.5 s, 24.3 s, 18.7 s, 15.6 s, 18.2 s, 12.4 s , 23.2 s, 40.3 s, 18.2 s, 19.3 s, 15.4 s, 14.4 s
Test the claim that the mean stop time is 15.5 s against the null hypothesis that the actual
mean stop time is larger than 15.5 s at levels of confidence of (a) = 0.01, (b) = 0.05, (c) =
0.10.
[20]. Twenty specimens of light-weight concrete taken from a concrete manufacturer indicate
that, in general, the standard deviation of the concrete density is 150 kg/m
3
. A sample of 20
concrete cubes shows a mean value of 1200 kg/m
3

with a standard deviation of 100 kg/m
3
.
Test the null hypothesis that the mean value of the population of concrete densities is 1250
kg/m
3
against the alternative hypothesis that the mean value of the population of concrete
densities is less than 1250 kg/m
3
.
[21]. A remote sensing device that measures the potential evapotranspiration of crops is
purported to produce accurate results 90% of the time. To check this claim, 10 crop test sites
are instrumented in the ground for measuring evapotranspiration and overflights with the
remove sensing device are scheduled to compare measurements. It is found that the remote
sensing device produced accurate measurements, as compared with the ground-based
measurements, in 7 out of the 10 measurements performed. Should we reject the assertion
that the remote sensing device is accurate 90% of the time based on these data, against an
alternative hypothesis that the device is accurate less than 90% of the data? Use confidence
levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[22]. A monitoring site in a small stream is checked daily to verify that the levels of a certain
contaminant produced by a farm operation are kept below the allowed limit. The local
regulating agency records indicate that the levels of the contaminant were in violation of
regulations for 5 out of the last 35 days. Test the hypothesis that the farm operation violates
the regulations only 10% of the time against the alternative hypothesis that the farm operation
violates the regulations more than 10% of the time. Use confidence levels of (a) = 0.01, (b)
= 0.05, and (c) = 0.10.
[23]. A new regulation regarding the amount of ozone produced by cars is being considered. A
sample of 40 cars is picked up at random for testing, and it is found that 12 of those cars
produce larger ozone concentrations than is considered safe by the local regulating agency.
The agency hypothesizes that 20% of the cars currently on the road produce excessive amounts
of ozone. Based on the data described above, should be accept this hypothesis against the
alternative hypothesis that the proportion of cars in violation of the ozone levels is not 20%?
Use confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[24]. Traffic studies are performed at two intersections of a city to determine whether a new
turn-signal is necessary. The study is aimed at determining the average number of left turns at
each intersection during a selected period of 1 hour. Intersection 1 is monitored through 20
consecutive days for 1 hour showing an average of 12.6 cars turning left with a standard
Download at InfoClearinghouse.com 63 2001 Gilberto E. Urroz
All rights reserved
deviation of 3.2 cars. Intersection 2 is monitored through 10 consecutive days at the same 1
hour period an it shows an average of 10.8 cars turning left with a standard deviation of 2.5
cars. It is hypothesized that the difference in the mean values of the populations of cars
turning left at intersections 1 and 2 is 2, i.e., H
0
:
1
-
2
= 2. Test this null hypothesis against
the alternative hypothesis H
1
:
1
-
2
2, using confidence levels of (a) = 0.01, (b) = 0.05,
and (c) = 0.10. Assume that the variances of the populations of left-turning cars in each
intersection are unknown and unequal.
[25]. Precision parts are delivered from two factories that use the same type of machine for
manufacturing the parts. A sample of 100 parts from factory number 1 shows an average
length of 12.5 cm with a standard deviation of 0.25 cm, while a sample of 50 parts from factory
number 2 shows an average length of 11.9 cm with a standard deviation of 0.30 cm. Test the
null hypothesis H
0
:
1
-
2
= 0 against the alternative hypothesis H
1
:
1
-
2
> 0, using confidence
levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10. Assume that the standard deviation of the
populations are unknown but equal.
[26]. Measurements of the chlorine levels out of 11 sample bottles from site number 1 indicate
an average of 2.5 mg/l with a standard deviation of 0.2 mg/l. In a neighboring site (site
number 2) the following values of chlorine concentrations are measured (in mg/l):
1.2 4.3 2.5 3.2 1.7 2.8 4.5 6.3 7.2
Test the null hypothesis H
0
:
1
-
2
= 0 against the alternative hypothesis H
1
:
1
-
2
< 0, using
confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10. Assume that the variances of
the population of chlorine concentrations are unknown and unequal.
[27]. The manager of a service station is trying to estimate the average time of service for a
particular task. He monitors the service time at two specific periods during the day obtaining
the following data (in minutes):
For period number 1: 5.0 12.5 15.0 8.5 6.2 7.8 11.4 12.5 10.0 9.2 8.7 11.2
For period number 2: 8.2 7.5 6.7 9.0 11.2 14.3 8.7 6.3 9.2
Test the null hypothesis H
0
:
1
-
2
= 0 against the alternative hypothesis H
1
:
1
-
2
< 0, using
confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10. Assume that the variances of
the populations of service times are unknown but equal.
[28]. The traffic study of problem [24] is repeated with the purpose of determining the
proportion of cars through each intersection that perform a left turn during the selected
period. A sample from intersection 1 indicates that out of 450 cars counted, 60 made a left
turn during the period of study. On the other hand, at intersection 2, it was determined that
50 out of 300 cars made a left turn. Let p
1
and p
2
represent the proportions of the population
of cars making left turns at intersections 1 and 2, respectively. Test the null hypothesis H
0
:p
1
-
p
2
= 0.2 against the alternative hypothesis H
1
:p
1
-p
2
0.2, using confidence levels of (a) =
0.01, (b) = 0.05, and (c) = 0.10.
[29]. A computer manufacturer is testing the proportion of defective chips received from two
different factories. A sample of 1000 chips from factory number 1 shows a total of 25
defective chips, while a sample of 300 chips from factory number 2 shows a total of 10
defective chips. Let p
1
and p
2
represent the proportions of the population of defective chips
from factories 1 and 2, respectively. Test the null hypothesis H
0
:p
1
-p
2
= 0 against the
alternative hypothesis H
1
:p
1
-p
2
0, using confidence levels of (a) = 0.01, (b) = 0.05, and (c)
= 0.10.
Download at InfoClearinghouse.com 64 2001 Gilberto E. Urroz
All rights reserved
[30]. Plot the characteristic and power curves for the hypotheses tests of problems [13]
through [23] using a suitable range of population mean values and a confidence level of 0.05.
[31]. The standard deviation of a sample of 25 measurements of soil density is found to be 25
kg/m
3
. Let
2
be the variance of the population of soil specimens from which the sample was
taken. Test the null hypothesis H
0
:
2
= 30 kg/m
3
, against the alternative hypothesis H
1
:
2

30 kg/m
3
, using confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[32]. The standard deviation of a sample of 25 measurements of soil density is found to be 25
kg/m
3
. Let
2
be the variance of the population of soil specimens from which the sample was
taken. Test the null hypothesis H
0
:
2
= 900 kg
2
/m
6
, against the alternative hypothesis H
1
:
2

900 kg
2
/m
6
, using confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[33]. Repeat the hypothesis test of problem [32] if the alternative hypothesis is H
1
:
2
< 900
kg
2
/m
6
.
[35]. The following data set represents measurements of hydrocarbon concentration (mg/l)
out of specimens taken from wells in a contaminated site:
3.5 5.6 2.3 4.5 8.5 2.3 4.5 1.2 5.6 3.2
Let
2
be the variance of the population of water specimens from which the sample was taken.
Test the null hypothesis H
0
:
2
= 30 kg/m
3
, against the alternative hypothesis H
1
:
2
30 kg/m
3
,
using confidence levels of (a) = 0.01, (b) = 0.05, and (c) = 0.10.
[36]. Two samples of car speeds are taken at a selected site of a major highway. The first
sample, consisting of 40 measurements, shows a standard deviation of 10 mph while the second
sample, consisting of 20 measurements, shows a standard deviation of 5 mph. Test the null
hypothesis H
0
:
1
2
/
2
2

= 1 against the alternative hypothesis (a) H
1
:
1
2
/
2
2

< 1, (b) H
1
:
1
2
/
2
2

>
1, and, (c) H
1
:
1
2
/
2
2

1, for a confidence level of = 0.01.
[37]. Laboratory tests are performed on a batch of water specimens to detect the
concentration of coliforms producing the following results (mg/l):
0.10 0.20 0.35 0.15 0.25 0.05 0.23 0.35 0.42
Refer to this data set as sample 1. A previous batch of 5 specimens, taken the previous day
(sample 2), showed a mean value of coliform concentration of 0.20 mg/l with a standard
deviation of 0.05 mg/l. Test the null hypothesis H
0
:
1
2
/
2
2

= 1 against the alternative
hypothesis (a) H
1
:
1
2
/
2
2

< 1, (b) H
1
:
1
2
/
2
2

> 1, and, (c) H
1
:
1
2
/
2
2

1, for a confidence level
of = 0.05.
[38]. Two batches of erosion control tests are performed to determine the effectiveness of a
new type of hydromulch in controlling erosion at construction sites. The reported rates of
erosion (lb/acre/hr) for the two batches are:
batch 1 = { 175 276 280 125 456 235 172 180 235 }
batch 2 = { 150 175 350 120 275 178 200 }
Test the null hypothesis H
0
:
1
2
/
2
2

= 1 against the alternative hypothesis (a) H
1
:
1
2
/
2
2

< 1, (b)
H
1
:
1
2
/
2
2

> 1, and, (c) H
1
:
1
2
/
2
2

1, for a confidence level of = 0.05.
[39]. The following data set represents the diameter in mm of a sample of sand grains.
Download at InfoClearinghouse.com 65 2001 Gilberto E. Urroz
All rights reserved
6.81 2.66 3.78 4.29 3.66 4.00
3.51 2.64 2.96 4.12 1.16 3.41
5.02 4.33 2.53 4.30 4.83 2.58
3.95 3.50 6.18 5.23 4.33 3.20
4.24 2.81 2.35 3.44 4.29 5.08
3.57 3.42 5.45 3.61 5.66 3.83
5.00 2.41 2.96 3.47 4.67 3.47
4.59 5.86 4.12 2.57 5.11 4.21
5.39 3.11 4.54 6.47 3.65 3.36
5.39 4.14 3.44 3.86 3.58 3.43
(a) Use user-defined function histnorm to check the hypothesis that the data follows the
normal distribution if the data is grouped into 10 classes of the same width at a level of
confidence = 0.05.
(b) Use user-defined function histbeta to check the hypothesis that the data follows the
beta distribution if the data is grouped into 10 classes of the same width at a level of
confidence = 0.10.
[40]. Write a SCILAB function, along the lines of functions histnorm and histbeta that produces
the chi-square parameter needed to test the goodness-of-fit of a vector of data for the Weibull
distribution. Use this function to check the hypothesis that the data from problem [39]
follows the Weibull distribution a level of confidence = 0.05.
[41]. Write a SCILAB function, along the lines of functions histnorm and histbeta that
produces the chi-square parameter needed to test the goodness-of-fit of a vector of data for
the exponential distribution. Use this function to check the hypothesis that the data from
problem [39] follows the exponential distribution a level of confidence = 0.05.
[42]. Samples of a particular species of fish are taken out of four fishing ponds and tested for
swirling disease. The table below summarizes the number of fish that tested positive and
negative for the disease in the four ponds. Use user-defined function RC to test the hypothesis
that the two criteria for classification in the table, namely, pond of origin and test result, are
independent at a level of confidence = 0.05.
Location
Pond 1 Pond 2 Pond 3 Pond 4
Positive 122 11 3 28
Negative 289 26 8 33
[43]. The following table is based on a number of laboratory tests on soil samples. The
samples are classified according to the typical grain size as sand, lime, or clay. Tests are then
performed in a rainfall simulator and the soil samples get classified according to a high,
medium, or low erosion potential. Use user-defined function RC to test the hypothesis that
the two criteria for classification in the table, namely, soil type and erosion potential, are
independent at a level of confidence = 0.10.
Potential
Soil type Low Medium High
Sand 45 12 8
Lime 23 10 10
Clay 12 5 2
Download at InfoClearinghouse.com 66 2001 Gilberto E. Urroz
All rights reserved
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 67 2001 Gilberto E. Urroz
All rights reserved
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Regression Analysis with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
REGRESSION ANALYSIS 2
Simple linear regression 2
Covariance and Correlation 6
Additional equations and definitions 6
Standard error of the estimate 7
A function for calculating linear regression of two variables 8
Confidence intervals and hypothesis testing in linear regression 9
A function for a comprehensive linear regression analysis 11
An example for linear regression analysis using function linregtable 11
SCILAB function reglin 13
Graphical display of multivariate data 13
Multiple linear regression 16
Example of multiple linear regression using matrices 17
Covariance in multiple linear regression 18
Confidence intervals and hypotheses testing in multiple linear regression 20
Coefficient of multiple determination 22
A function for multiple linear regression analysis 22
Application of function multiplelinear 25
A function for predicting values from a multiple regression 28
Simple linear regression using function multiplelinear 29
Analysis of residuals 32
Scaling residuals 34
Influential observations 35
A function for residual analysis 35
Applications of function residuals 36
Multiple linear regression with function datafit 40
Polynomial data fitting 42
Exercises 46
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Regression Analysis
The idea behind regression analysis is to verify that a function
y = f(x)
fits a given data set
{(x
1
,y
1
), (x
2
,y
2
),,( x
n
,y
n
)}
after obtaining the parameters that identify function f(x). The value x represents one or more
independent variables. The function f(x) can be, for example, a linear function, i.e.,
y = mx+b, or y = b
0
+ b
1
x
1
+ + b
k
x
k
),
a polynomial function, i.e.,
y = b
0
+ b
1
x
1
+ + b
p
x
p
,
or other non-linear functions. The procedure consists in postulating a form of the function to
be fitted, y = f(x), which will depend, in general, of a number of parameters, say {b
0
, b
1
, ,
b
k
}. Then we choose a criteria to determine the values of those parameters. The most
commonly used is the least-square criteria, by which the sum of the squares of the errors (SSE)
involved in the data fitting is minimized. The error involved in fitting point i in the data set is
given by
e
i
= y
i
-
i
y ,
thus, the quantity to be minimized is
. ) (
1
2
1
2



n
i
i i
n
i
i
y y e SSE
Minimization of SSE is accomplished by taking the derivatives of SSE with respect to each of the
parameters, b
0
, b
1
, , b
k
, and setting these results to zero, i.e., (SSE)/b
0
= 0, (SSE)/b
0
= 0,
, (SSE)/b
k
= 0. The resulting set of equations is then solved for the values b
0
, b
1
, , b
k
.
After finding the parameters by minimization of the sum of square errors (SSE), we can test
hypotheses about those parameters under certain confidence levels to complete the regression
analysis. In the following section we present the regression analysis of a simple linear
regression.
Simple linear regression
Consider the data set represented in the figure below and represented by the set {(x
1
,y
1
),
(x
2
,y
2
),,( x
n
,y
n
)}. Suppose that the equation
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
y = mx+b,
representing a straight line in the x-y plane is used to represent the relationship between the
values x and y from the set. The fitted value of y corresponding to point x
i
is
i
y = mx
i
+b,
and the corresponding error in the fitting is
e
i
= y
i
-
i
y = y
i
-(mx
i
+b) = y
i
-mx
i
-b.
The sum of square errors (SSE) to be minimized is
. ) ( ) , (
1
2


n
i
i i
b mx y b m SSE
To determine the values of m and b that minimize the sum of square errors, we use the
conditions

a
SSE ( ) 0

b
SSE ( ) 0
from which we get the so-called normal equations:


+
n
i
i
n
i
i
x m n b y
1 1


+
n
i
i
n
i
i
n
i
i i
x m x b y x
1
2
1 1
This is a system of linear equations with m and b as the unknowns. In matricial form, these
equations are written as
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
1
1
1
1
]
1

1
]
1

1
1
1
1
]
1

n
i
i i
n
i
i
n
i
i
n
i
i
n
i
i
y x
y
b
m
x x
n x
1
1
1 1
2
1
.
For example, consider the data set given in the following table
x 1.2 2.5 4.3 8.3 11.6
y 6.05 11.6 15.8 21.8 36.8
The following SCILAB commands will calculate the values of m and b to minimize SSE. A plot of
the original data and the straight line fitting is also produced. The column vector p stores the
values of m and b. Thus, for this case m=2.6827095 and b=3.4404811.
-->x=[1.2,2.5,4.3,8.3,11.6];y=[6.05,11.6,15.8,21.8,36.8];
-->Sx=sum(x);Sx2=sum(x^2);Sy=sum(y);Sxy=sum(x.*y);n=length(x);
-->A=[Sx,n;Sx2,Sx];B=[Sy;Sxy];p=A\B
p =
! 2.6827095 !
! 3.4404811 !
-->deff('[y]=yh(x)','y=p(1).*x+p(2)')
-->plot2d(xf,yf,1,'011',' ',rect)
-->plot2d(x,y,-1,'011',' ',rect)
-->xtitle('Simple linear regression','x','y')
The value of the sum of square errors for this fitting is:
-->yhat=yh(x);err=y-yhat;SSE=sum(err^2)
SSE = 23.443412
To illustrate graphically the behavior of SSE(m,b) we use the following function
SSEPlot(mrange,brange,x,y), where mrange and brange are vectors with ranges of values of m
and b, respectively, and x and y are the vectors with the original data.
function [] = SSEPlot(mrange,brange,x,y)
n=length(mrange); m=length(brange);
SSE = zeros(n,m);
deff('[y]=f(x)','y=slope*x+intercept')
for i = 1:n
for j = 1:m
slope = mrange(i);intercept=brange(j);
yhat = f(x);err=y-yhat;SSE(i,j)=sum(err^2);
end;
end;
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
xset('window',1);plot3d(mrange,brange,SSE,45,45,'m@b@SSE');
xtitle('Sum of square errors')
xset('window',2);contour(mrange,brange,SSE,10);
xtitle('Sum of square errors','m','b');
The function produces a three-dimensional plot of SSE(m,b) as well as a contour plot of the
function. To produce the plots we use the following SCILAB commands:
-->mr = [2.6:0.01:2.8];br=[3.3:0.01:3.5];
-->getf('SSEPlot')
-->SSEPlot(mr,br,x,y)
The following two lines modify the contour plot.
-->plot2d([p(1)],[p(2)],-9,'011',' ',[2.600 3.30 2.790 3.50])
-->xstring(p(1)+0.002,p(2)+0.002,'minimum SSE')
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
Covariance and Correlation
The concepts of covariance and correlation were introduced in Chapter 14 in relation to
bivariate random variables. For a sample of data points (x,y), such as the one used for the
linear regression in the previous section, we define covariance as
The sample correlation coefficient for x,y is defined as
where s
x
, s
y
are the standard deviations of x and y, respectively, i.e.
The correlation coefficient is a measure of how well the fitting equation, i.e., ^y = mx+b, fits
the given data. The values of r
xy
are constrained in the interval (-1,1). The closer the value of
r
xy
is to +1 or -1, the better the linear fitting for the given data.
Additional equations and definitions
Let's define the following quantities:
) )( (
1
1
1
y y x x
n
s
i
n
i
i xy

y x
xy
xy
s s
s
r

2
1
2
) (
1
1
y y
n
s
n
i
i y

,
_

,
_




n
i
i
n
i
i
n
i
i i xy
n
i
i i xy
y x
n
y x s n y y x x S
1 1 1 1
2
1
) 1 ( ) )( (
2
1 1
2 2
1
2
1
) 1 ( ) (
,
_




n
i
i
n
i
i x
n
i
i xx
x
n
x s n x x S
2
1 1
2
2
1
2
1
) 1 ( ) (
,
_




n
i
i
n
i
i y
n
i
i yy
y
n
y s n y y S
2
1
2
) (
1
1
x x
n
s
n
i
i x

Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz


From which it follows that the standard deviations of x and y, and the covariance of x,y are
given, respectively, by
Also, the sample correlation coefficient is
In terms ofx,y, S
xx
, S
yy
, and S
xy
, the solution to the normal equations is:
Standard error of the estimate
The function
i
y = mx
i
+b in a linear fitting is an approximation to the regression curve of a
random variable Y on a random variable X,
Y = Mx + B + ,
where is a random error. If we have a set of n data points (x
i
, y
i
), then we can write
1

n
S
s
xx
x
1

n
S
s
yy
y
1

n
S
s
xy
xy
.
yy xx
xy
xy
S S
S
r

2
x
xy
xx
xy
s
s
S
S
m
x m y b
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Y
i
= Mx
i
+ B +
i
,
i = 1,2,,n, where Y
i
are independent, normally distributed random variables with mean
( + x
i
) and common variance
2
, and
i
are independent, normally distributed random
variables with mean zero and the common variance
2
.
Let y
i
= actual data value, ^y
i
= mx
i
+ b

= least-square prediction of the data. Then, the
prediction error is:
e
i
= y
i
-
^
y
i
= y
i
- (mx
i
+b).
The prediction error being an estimate of the regression error , an estimate of
2
is the so-
called standard error of the estimate,
A function for calculating linear regression of two variables
Using the definitions provided in this section, we can use the following user-defined function,
linreg, to calculate the different parameters of a simple linear regression. The function
returns the slope, m, and intercept, b, of the linear function, the covariance, s
xy
, the
correlation coefficient, r
xy
, the mean and standard deviations of x and y (x,s
x
,y,s
y
), and the
standard error of the estimate, s
e
. The function also produces a plot of the original data and of
the fitted equation. A listing of the function follows:
function [rxy,sxy,slope,intercept]=linreg(x,y)
n=length(x);m=length(y);
if m<>n then
error('linreg - Vectors x and y are not of the same length.');
abort;
end;
Sxx = sum(x^2)-sum(x)^2/n;
Syy = sum(y^2)-sum(y)^2/n;
Sxy = sum(x.*y)-sum(x)*sum(y)/n;
sx = sqrt(Sxx/(n-1));
sy = sqrt(Syy/(n-1));
sxy = Sxy/(n-1);
rxy = Sxy/sqrt(Sxx*Syy);
xbar = mean(x);
ybar = mean(y);
slope = Sxy/Sxx;
intercept = ybar - slope*xbar;
se = sqrt((n-1)*sy^2*(1-rxy^2)/(n-2));
xmin = min(x);
xmax = max(x);
xrange = xmax-xmin;
xmin = xmin - xrange/10;
xmax = xmax + xrange/10;
xx = [xmin:(xmax-xmin)/100:xmax];
deff('[y]=yhat(x)','y=slope*x+intercept');
) 1 (
2
1
2
/ ) (
2
)] ( [
2
1
2 2
2
2
1
2
xy y
xx xy yy
i
n
i
i e
r s
n
n
n
S S S
n
SSE
b mx y
n
s

Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz


yy = yhat(xx);
ymin = min(y);
ymax = max(y);
yrange = ymax - ymin;
ymin = ymin - yrange/10;
ymax = ymax + yrange/10;
rect = [xmin ymin xmax ymax];
plot2d(xx,yy,1,'011',' ',rect);
xset('mark',-9,1);
plot2d( x, y,-9,'011',' ',rect);
xtitle('Linear regression','x','y');
As an example, we will use the following data set:
x 4.5 5.6 7.2 11.2 15 20
y 113 114 109 96.5 91.9 82.5
First we enter the data into vectors x and y, and then call function linreg.
-->x=[4.5,5.6,7.2,11.2,15,20];y=[113,114,109,96.5,91.9,82.5];
-->[rxy,sxy,slope,intercept] = linreg(x,y)
xbar = 10.583333
ybar = 101.15
sx = 6.0307269
sy = 12.823221
intercept = 123.38335
slope = - 2.1007891
sxy = - 76.405
rxy = - .9879955
The correlation coefficient r
xy
= -0.9879955 corresponds to a decreasing linear function. The
fact that the value of the correlation coefficient is close to -1 suggest a good linear fitting.
Confidence intervals and hypothesis testing in linear regression
The values m and b in the linear fitting
i
y = mx
i
+b are approximations to the parameters M and
B in the regression curve
Y = Mx + B + .
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
Therefore, we can produce confidence intervals for the parameters M and B for a confidence
level . We can also perform hypotheses testing on specific values of the parameters.
Confidence limits for regression coefficients:
For the slope ():
m (t
n-2,/2
)s
e
/S
xx
< < m + (t
n-2,/2
)s
e
/S
xx
,
For the intercept ():
b (t
n-2,/2
)s
e
[(1/n)+x
2
/S
xx
]
1/2

< < b + (t
n-2,/2
)s
e
[(1/n)+x
2
/S
xx
]
1/2
,
where t follows the Students t distribution with = n 2, degrees of freedom, and n
represents the number of points in the sample.
Confidence interval for the mean value of Y at x = x
0
, i.e., mx
0
+ b:
[mx
0
+b (t
n-2,/2
)s
e
[(1/n)+(x
0
-x)
2
/S
xx
]
1/2
; mx
0
+b +(t
n-2, /2
)s
e
[(1/n)+(x
0
-x)
2
/S
xx
]
1/2
]
Limits of prediction: confidence interval for the predicted value Y
0
=Y(x
0
):
[mx
0
+b (t
n-2,/2
)s
e
[1+(1/n)+(x
0
-x)
2
/S
xx
]
1/2
; mx
0
+b+(t
n-2, /2
)s
e
[1+(1/n)+(x
0
-x)
2
/S
xx
]
1/2
]
Hypothesis testing on the slope, :
The null hypothesis, H
0
: =
0
, is tested against the alternative hypothesis, H
1
:
0
.
The test statistic is
t
0
= (m-
0
)/(s
e
/S
xx
),
where t follows the Students t distribution with = n 2, degrees of freedom, and n
represents the number of points in the sample. The test is carried out as that of a mean
value hypothesis testing, i.e., given the level of significance, , determine the critical
value of t, t
/2
, then, reject H
0
if t
0
> t
/2
or if t
0
< - t
/2
.
If you test for the value
0
= 0, and it turns out you do not reject the null hypothesis, H
0
:
= 0, then, the validity of a linear regression is in doubt. In other words, the sample data
does not support the assertion that 0. Therefore, this is a test of the significance of
the regression model.
Hypothesis testing on the intercept , :
The null hypothesis, H
0
: =
0
, is tested against the alternative hypothesis, H
1
:
0
.
The test statistic is
t
0
= (b-
0
)/[(1/n)+x
2
/S
xx
]
1/2
,
where t follows the Students t distribution with = n 2, degrees of freedom, and n
represents the number of points in the sample. The test is carried out as that of a mean
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
value hypothesis testing, i.e., given the level of significance, , determine the critical
value of t, t
/2
, then, reject H
0
if t
0
> t
/2
or if t
0
< - t
/2
.
A function for a comprehensive linear regression analysis
The following function, linregtable, produces a comprehensive analysis of linear regression
returning not only the basic information produced by function linreg, but also including a table
of data including the fitted values, the errors, and the confidence intervals for the mean value
and the predicted values of the regression line. The function also returns estimated errors of
the slope and intercept, and performs hypotheses testing for the cases M = 0 and B = 0.
function [se,rxy,sxy,slope,intercept,sy,sx,ybar,xbar]=linreg(x,y)
n=length(x);m=length(y);
if m<>n then
error('linreg - Vectors x and y are not of the same length.');
abort;
end;
Sxx = sum(x^2)-sum(x)^2/n;
Syy = sum(y^2)-sum(y)^2/n;
Sxy = sum(x.*y)-sum(x)*sum(y)/n;
sx = sqrt(Sxx/(n-1));
sy = sqrt(Syy/(n-1));
sxy = Sxy/(n-1);
rxy = Sxy/sqrt(Sxx*Syy);
xbar = mean(x);
ybar = mean(y);
slope = Sxy/Sxx;
intercept = ybar - slope*xbar;
se = sqrt((n-1)*sy^2*(1-rxy^2)/(n-2));
xmin = min(x);
xmax = max(x);
xrange = xmax-xmin;
xmin = xmin - xrange/10;
xmax = xmax + xrange/10;
xx = [xmin:(xmax-xmin)/100:xmax];
deff('[y]=yhat(x)','y=slope*x+intercept');
yy = yhat(xx);
ymin = min(y);
ymax = max(y);
yrange = ymax - ymin;
ymin = ymin - yrange/10;
ymax = ymax + yrange/10;
rect = [xmin ymin xmax ymax];
plot2d(xx,yy,1,'011',' ',rect);
xset('mark',-9,1);
plot2d( x, y,-9,'011',' ',rect);
xtitle('Linear regression','x','y');
An example for linear regression analysis using function linregtable
For an application of function linregtable consider the following (x,y) data. We use a
significance level of 0.05.
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
x 2.0 2.5 3.0 3.5 4.0
y 5.5 7.2 9.4 10.0 12.2
The following SCILAB commands are used to load the data and perform the regression analysis:
-->getf('linregtable')
-->x=[2.0,2.5,3.0,3.5,4.0];y=[5.5,7.2,9.4,10.0,12.2];
-->linregtable(x,y,0.05)
Regression line: y = 3.24*x + -.86
Significance level = .05
Value of t_alpha/2 = 3.18245
Confidence interval for slope = [2.37976;4.10024]
Confidence interval for intercept = [-3.51144;1.79144]
Covariance of x and y = 2.025
Correlation coefficient = .98972
Standard error of estimate = .42740
Standard error of slope = .27031
Standard error of intercept = .83315
Mean values of x and y = 3 8.86
Standard deviations of x and y = .79057 2.58805
Error sum of squares = .548
---------------------------------------------------------------------------------------
x y ^y error C.I. mean C.I. predicted
---------------------------------------------------------------------------------------
2 5.5 5.62 -.12 4.56642 6.67358 3.89952 7.34048
2.5 7.2 7.24 -.04 6.49501 7.98499 5.68918 8.79082
3 9.4 8.86 .54 8.25172 9.46828 7.37002 10.35
3.5 10 10.48 -.48 9.73501 11.225 8.92918 12.0308
4 12.2 12.1 .1 11.0464 13.1536 10.3795 13.8205
---------------------------------------------------------------------------------------
Reject the null hypothesis H0:Slope = 0.
Test parameter for hypothesis testing on the slope (t) = 11.9863
Do not reject the null hypothesis H0:Intercept = 0.
Test parameter for hypothesis testing on the intercept (t) = -.44117
The plot of the original data and the fitted data, also produced by function linregtable, is
shown next:
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
The graph shows a good linear fitting of the data confirmed by a correlation coefficient
(0.98972) very close to 1.0. The hypotheses testing indicate that the null hypothesis H
0
:b = 0
cannot be rejected, i.e., a zero intercept may be substituted for the intercept of -0.86 with a
95% confidence level. On the other hand, the null hypothesis H
0
:m=0 is rejected, indicating a
proper linear relationship.
SCILAB function reglin
SCILAB provides function reglin, with call: [m,b,sig] = reglin(x,y), which returns the values of
the slope, m, the intercept, b, and the standard deviation of the residual, , for the linear
fitting y =mx+b. For the data of the example above, using reglin we obtain:
--> [m,b,sig] = reglin(x,y)
sig = 0.3310589, b = -0.85, m = 3.24
Graphical display of multivariate data
In the next section we will present techniques of analysis for multiple linear regression in
which we use fittings of the form y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3
+ + b
n
x
n
. Before we present
the details of the analysis, however, we want to introduce a simple way to visualize
relationships between pairs of variables in a multivariate data set. The proposed graph is an
array of plots representing the relationships between independent variables x
i
and x
j
, for ij,
as well as the relationship of the dependent variable y and each of the independent variables
x
i
. Function multiplot, which takes as input a matrix X whose columns are values of the
independent variables, and a (row) vector y, which represents the dependent variable,
produces such array of plots. A listing of the function multiplot follows next.
function [] = multiplot(X,y)
//Produces a matrix of plots:
// ---x1---- x1-vs-x2 x1-vs-x3 ... x1-vs-y
// x2-vs-x1 ---x2--- x2-vs-x3 ... x2-vs-y
// . . . .
// y-vs-x1 y-vs-x2 y-vs-x3 ... ---y---
[m n] = size(X);
nr = n+1; nc = nr;
XX = [X y'];
xset('window',1);
xset('default');
xbasc();
xset('mark',-1,1);
for i = 1:nr
for j = 1:nc
mtlb_subplot(nr,nc,(i-1)*nr+j);
if i <> j then
rect= [min(XX(:,j)) min(XX(:,i)) max(XX(:,j)) max(XX(:,i))];
plot2d(XX(:,j),XX(:,i),-1);
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
if i==nr & j == nc then
xtitle(' ','y','y');
elseif i==nr then
xtitle(' ','x'+string(j),'y');
elseif j==nc then
xtitle(' ','y','x'+string(i));
else
xtitle(' ','x'+string(j),'x'+string(i))
end;
end;
end;
end;
xset('font',2,5);
for i = 1:nr
for j = 1:nc
mtlb_subplot(nr,nc,(i-1)*nr+j);
if i==j then
plot2d([0],[0],1,'010',' ',[0 0 10 10]);
if i==nr & j==nc then
xstring(3,5,'y');
else
xstring(3,5,'x'+string(i));
end;
end;
end;
end;
To sub-divide the plot window into subplots, function multiplot uses function mtlb_subplot, a
function that emulates Matlab

s function subplot (which explains the prefix mtlb_). Details of


this, and other functions with the mtlb_ prefix are presented in more detail in Chapter 20.
To illustrate the use of function multiplot we will use the following data set:
____________________
x
1
x
2
y
____________________
2.3 21.5 147.47
3.2 23.2 165.42
4.5 24.5 170.60
5.1 26.2 184.84
6.2 27.1 198.05
7.5 28.3 209.96
____________________
The SCILAB commands to produce the plot array are shown next.
-->x1 = [2.3 3.2 4.5 5.1 6.2 7.5];
-->x2 = [21.5 23.2 24.5 26.2 27.1 28.3];
-->y = [147.47 165.42 170.60 184.84 198.05 209.96];
-->X=[x1' x2'];
-->getf('multiplot')
-->multiplot(X,y)
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
__________________________________________________________________________________
__________________________________________________________________________________
A result like this array of plots is useful in determining some preliminary trends among the
variables. For example, the plots above show strong dependency between x
1
and x
2
, besides
the expected dependency of y on x
1
or y on x
2
. In that sense, variables x
1

and x
2
are not
independent of each other. When we refer to them as the independent variables, the meaning
is that of variables that explain y, which is, in turn, referred to as the dependent variable.
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
Multiple linear regression
The subject of multiple linear regression was first introduced in Chapter 5 as an example of
applications of matrix operations. For multiple linear regression fitting consider a data set of
the form
x
1
x
2
x
3
x
4
x
5
y
x
11
x
21
x
31
x
n1
y
1
x
12
x
22
x
32
x
n2
y
2
x
13
x
32
x
33
x
n3
y
3
. . . . .
. . . . . .
x
1,m-1
x
2,m-1
x
3,m-1
x
n,m-1
y
m-1
x
1,m
x
2,m
x
3,m
x
n,m
y
m
to which we fit an equation of the form
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3
+ + b
n
x
n
.
If we write the independent variables x
1
, x
2
, , x
n
into a row vector, i.e., x
i
= [x
1i
x
2i
x
ni
], and
the coefficients b
0
b
1
b
2
b
n
into a column vector b = [b
0
b
1
b
2
b
3
b
n
]
T
, we can write
i
y = x
i
b.
If we put together the matrix, X = [x
1
x
2
x
n
]
T
, i.e.,
_ _
1 x
11
x
21
x
31
x
n1
1 x
12
x
22
x
32
x
n2
1 x
13
x
32
x
33
x
n3
. . . . .
. . . . . .
1 x
1,m
x
2,m
x
3,m
x
n,m
_ _
and the vector, , ] .... [
2 1
T
n
y y y y we can summarize the original table into the expression
. b X y
The error vector is
e = y - y ,
and the sum of squared erros, SSE, is
SSE = e
T
e = (y - y )
T
(y - y ) = (y-Xb)
T
(y-Xb).
To minimize SSE we write (SSE)/b = 0. It can be shown that this results in the expression
X
T
X b = X
T
y, from which it follows that
b = (X
T
X)
-1
X
T
y
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
An example for calculating the vector of coefficients b for a multiple linear regression was
presented in Chapter 5. The example is repeated here to facilitate understanding of the
procedure.
Example of multiple linear regression using matrices
use the following data to obtain the multiple linear fitting
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3,
x
1
x
2
x
3
y
1.20 3.10 2.00 5.70
2.50 3.10 2.50 8.20
3.50 4.50 2.50 5.00
4.00 4.50 3.00 8.20
6.00 5.00 3.50 9.50
With SCILAB you can proceed as follows:
First, enter the vectors x
1
, x
2
, x
3
, and y, as row vectors:
->x1 = [1.2,2.5,3.5,4.0,6.0]
x1 = ! 1.2 2.5 3.5 4. 6. !
-->x2 = [3.1,3.1,4.5,4.5,5.0]
x2 = ! 3.1 3.1 4.5 4.5 5. !
-->x3 = [2.0,2.5,2.5,3.0,3.5]
x3 = ! 2. 2.5 2.5 3. 3.5 !
-->y = [5.7,8.2,5.0,8.2,9.5]
y = ! 5.7 8.2 5. 8.2 9.5 !
Next, we form matrix X:
-->X = [ones(5,1) x1' x2' x3']
X =
! 1. 1.2 3.1 2. !
! 1. 2.5 3.1 2.5 !
! 1. 3.5 4.5 2.5 !
! 1. 4. 4.5 3. !
! 1. 6. 5. 3.5 !
The vector of coefficients for the multiple linear equation is calculated as:
-->b =inv(X'*X)*X'*y
b =
! - 2.1649851 !
! - .7144632 !
! - 1.7850398 !
! 7.0941849 !
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
Thus, the multiple-linear regression equation is:
y^ = -2.1649851-0.7144632x
1
-1.7850398x
2
+7.0941849x
3
.
This function can be used to evaluate y for values of x given as [x
1
,x
2
,x
3
]. For example, for
[x
1
,x
2
,x
3
] = [3,4,2], construct a vector xx = [1,3,4,2], and multiply xx times b, to obtain y(xx):
-->xx = [1,3,4,2]
xx = ! 1. 3. 4. 2. !
-->xx*b
ans = 2.739836
The fitted values of y corresponding to the values of x
1
, x
2
, and x
3
from the table are obtained
from y = Xb:
-->X*b
ans =
! 5.6324056 !
! 8.2506958 !
! 5.0371769 !
! 8.2270378 !
! 9.4526839 !
Compare these fitted values with the original data as shown in the table below:
x
1
x
2
x
3
y y-fitted
1.20 3.10 2.00 5.70 5.63
2.50 3.10 2.50 8.20 8.25
3.50 4.50 2.50 5.00 5.04
4.00 4.50 3.00 8.20 8.23
6.00 5.00 3.50 9.50 9.45
This procedure will be coded into a user-defined SCILAB function in an upcoming section
incorporating some of the calculations for the regression analysis as shown next.
An array of plots showing the dependency of the different variables involved in the multiple
linear fitting is shown in the following page. It was produced by using function multiplot.
-->multiplot(X,y);
(See plot in next page).
Covariance in multiple linear regression
In simple linear regression we defined a standard error of the estimate, s
e
, as an approximation
to the variance of the distribution of errors. The standard error of the estimate, also known
as the mean square error, for multiple linear regression is defined as
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
s
e
= MSE = SSE/(m-n-1),
where m is the number of data points available and n is the number of coefficients required for
the multiple linear fitting.
The matrix C = s
e
2
(X
T
X)
-1
is a symmetric matrix known as the covariance matrix. The diagonal
elements c
ii
are the variances associated with each of the coefficients b
i
, i.e.,
Var(b
i
) = c
ii
,
while elements off the diagonal, c
ij
, ij, are the covariances of b
i
and b
j
, i.e.,
Cov(b
i
,b
j
) = c
ij,
ij.
The square root of the variances Var(b
i
) are referred to as the standard error of the estimate
for each coefficient, i.e.,
s
e
(b
i
) = c
ii
= [Var(b
i
)]
1/2
.
__________________________________________________________________________________
__________________________________________________________________________________
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
Confidence intervals and hypotheses testing in multiple linear
regression
The multiple linear relationship defined earlier, namely,
y = b
0
+ b
1
x
1
+ b
2
x
2
+ b
3
x
3
+ + b
n
x
n
,
is an approximation to the multiple linear model
Y =
0
+
1
x
1
+
2
x
2
+
3
x
3
+ +
n
x
n
+ ,
where Y is a normally-distributed random variable with mean y , is also normally-distributed
with mean zero. The standard deviation of the distributions of both Y and is . An
approximation to the value of is the standard error of the estimate for the regression, s
e
,
defined earlier.
Confidence intervals for the coefficients
Using a level of confidence , we can write confidence intervals for each of the
coefficients
i
in the linear model for Y, as
b
i
(t
m-n,/2
)s
e
(b
i
)

<
i
< b
i
+ (t
m-n,/2
) s
e
(b
i
)

,
for i=1,2,,n, where b
i
is the i-th coefficient in the linear fitting, t
m-n,/2
is the value of the
Students t variable for = m-n degrees of freedom corresponding to a cumulative
probability of 1-/2, and s
e
(b
i
) is the standard error of the estimate for b
i
.
Confidence interval for the mean value of Y at x = x
0
, i.e., x
0
T
b:
[x
0
T
b (t
m-n,/2
) [x
0
T


C

x
0
]
1/2
; x
0
T
b + (t
m-n,/2
)[x
0
T


C

x
0
]
1/2

2
]
where C is the covariance matrix.
Limits of prediction: confidence interval for the predicted value Y
0
=Y(x
0
):
[x
0
T
b (t
m-n,/2
)s
e
[1+x
0
T


(C/s
e
)

x
0
]
1/2
; x
0
T
b + (t
m-n,/2
)s
e
[1+x
0
T


(C/s
e
)

x
0
]
1/2

2
]
Hypothesis testing on the coefficients,
i
:
The null hypothesis, H
0
:
i
=
0
, is tested against the alternative hypothesis, H
1
:
i

0
.
The test statistic is
t
0
= (b
i
-
0
)/s
e
(b
i
)
where t follows the Students t distribution with = m-n, degrees of freedom. The test is
carried out as that of a mean value hypothesis testing, i.e., given the level of significance,
, determine the critical value of t, t
/2
, then, reject H
0
if t
0
> t
/2
or if t
0
< - t
/2
. Of
interest, many times, is the test that a particular coefficient b
i
be zero, i.e., H
0
:
i
= 0.
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
Hypothesis testing for significance of regression
This test is aimed at determining if a linear regression indeed exists between y and x. The
null hypothesis to be tested is H
0
:
1
=
2
=
3
==
n
=0, against the alternative hypothesis,
H
1
:
i
0 for at least one value of i. The appropriate test to be conducted is an F test
based on the test parameter
,
) 1 /(
/
0
MSE
MSR
n m SSE
n SSR
F

where MSR = SSR/n is known as the regression mean square, MSE = SSE/(m-n-1) = s
e
2
is the
mean square error, SSE is the sum of squared errors (defined earlier), and SSR is the
regression sum of squares defined as
, ) (
2
1
y y SSR
m
i
i


wherey is the mean value of y. The parameter F
0
follows the F distribution with n
degrees of freedom in the numerator and m-n-1 degrees of freedom in the denominator.
For a given confidence level , we will determine the value F

from the appropriate F


distribution, and reject the null hypothesis H
0
if F
0
> F

.
Analysis of variance for testing significance of regression
Analysis of variance is the name to the general method that produces the parameters
described above for the testing of significance of regression. The method is based on the
so-called analysis of variance identity,
SST = SSR + SSE,
where SSE and SSR were described above and SST, the total corrected sum of squares, is
described as
. ) (
2
1
y y SST
m
i
i


The term SSR accounts for the variability in y
i
due to the regression line, while the terms
SSE accounts for the residual variability not incorporated in SSR.

The term SSR has n
degrees of freedom, i.e., the same number of coefficients in the multiple linear fitting.
The term SSE has m-n-1 degrees of freedom, while SST has n-1 degrees of freedom.
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
Analysis of variance for the test of significance of regression is typically reported in a table
that includes the following information:
Variation source Sum of squares Degrees of freedom Mean square F
0
Regression SSR n MSR MSR/MSE
Residual/error SSE m-k-1 MSE
Total SST m-1
Coefficient of multiple determination
The coefficient of multiple determination R
2
is defined in terms of SST, SSR, and SSE, as
, 1
2
SST
SSE
SST
SSR
R
while the positive square root of this value is referred to as the multiple correlation
coefficient, R. This multiple correlation coefficient, for the case of a simple linear regression,
is the same as the correlation coefficient r
xy
. Values of R
2
are restricted to the range [0,1].
Unlike the simple correlation coefficient, r
xy
, the coefficient of multiple determination, R
2
, is
not a good indicator of linearity. A better indicator is the adjusted coefficient of multiple
determination,
.
) 1 /(
) 1 /(
1
2



m SST
n m SSE
R
adj
A function for multiple linear regression analysis
The following function, multiplelinear, includes the calculation of the coefficients for a
multiple linear regression equation, their standard error of estimates, their confidence
interval, and the recommendation for rejection or not rejection for the null hypotheses H
0
:
i
=
0. The function also produces a table of the values of y, the fitted values y , the errors, and
the confidence intervals for the mean linear regression Y, and for the predicted linear
regression. The analysis-of-variance table is also produced by the function. Finally, the
function prints the values of the standard error of the estimate, s
e
, the coefficient of multiple
determination, the multiple correlation coefficient, the adjusted coefficient of multiple
determination, and the covariance matrix. The function returns the vector of coefficients, b,
the covariance matrix, cov(x
i
,x
j
), and the standard error of the estimate, s
e
. The arguments
of the function are a matrix XA whose columns include the column data vectors x
1
, x
2
, , x
n
, a
column vector y containing the dependent variable data, and a level of confidence, alpha
(typical values are 0.01, 0.05, 0.10).
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
A listing of the function follows:
function [b,C] = multiplelinear(XA,y,alpha)
[m n] = size(XA); //Size of original X matrix
X = [ones(m,1) XA]; //Augmenting matrix X
b=inv(X'*X)*X'*y; //Coefficients of function
yh = X*b; //Fitted value of y
e=y-yh; //Errors or residuals
SSE=e'*e; //Sum of squared errors
MSE = SSE/(m-n-1); //Mean square error
se = sqrt(MSE); //Standard error of estimate
C = MSE*inv(X'*X); //Covariance matrix
[nC mC]=size(C);
seb = []; //Standard errors for coefficients
for i = 1:nC
seb = [seb; sqrt(C(i,i))];
end;
ta2 = cdft('T',m-n,1-alpha/2,alpha/2); //t_alpha/2
sY = []; sYp = []; //Terms involved in C.I. for Y, Ypred
for i=1:m
sY = [sY; sqrt(X(i,:)*C*X(i,:)')];
sYp = [sYp; se*sqrt(1+X(i,:)*(C/se)*X(i,:)')];
end;
CIYL = yh-sY; //Lower limit for C.I. for mean Y
CIYU = yh+sY; //Upper limit for C.I. for mean Y
CIYpL = yh-sYp; //Lower limit for C.I. for predicted Y
CIYpU = yh+sYp; //Upper limit for C.I. for predicted Y
CIbL = b-ta2*seb; //Lower limit for C.I. for coefficients
CIbU = b+ta2*seb; //Upper limit for C.I. for coefficients
t0b = b./seb; //t parameter for testing H0:b(i)=0
decision = []; //Hypothesis testing for H0:b(i)=0
for i = 1:n+1
if t0b(i)>ta2 | t0b(i)<-ta2 then
decision = [decision; ' reject '];
else
decision = [decision; ' do not reject'];
end;
end;
ybar = mean(y); //Mean value of y
SST = sum((y-ybar)^2); //Total sum of squares
SSR = sum((yh-ybar)^2); //Residual sum of squares
MSR = SSR/n; //Regression mean square
MSE = SSE/(m-n-1); //Error mean square
F0 = MSR/MSE; //F parameter for significance of regression
Fa = cdff('F',n,m-n-1,1-alpha,alpha); //F_alpha
R2 = 1-SSE/SST; R = sqrt(R2); //Coeff. of multiple regression
R2a = 1-(SSE/(m-n-1))/(SST/(m-1)); //Adj. Coeff. of multiple regression
//Printing of results
printf(' ');
printf('Multiple linear regression');
printf('==========================');
printf(' ');
printf('Table of coefficients');
printf('-----------------------------------------------------------------------
--');
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
printf(' i b(i) se(b(i)) Lower Upper t0
H0:b(i)=0');
printf('-----------------------------------------------------------------------
--');
for i = 1:n+1
printf('%4.0f %10g %10g %10g %10g %10g '+decision(i),...
i-1,b(i),seb(i),CIbL(i),CIbU(i),t0b(i));
end;
printf('-----------------------------------------------------------------------
--');
printf(' t_alpha/2 = %g',ta2);
printf('-----------------------------------------------------------------------
--');
printf(' ');printf(' ');
printf('Table of fitted values and errors');
printf('-----------------------------------------------------------------------
----------');
printf(' i y(i) yh(i) e(i) C.I. for Y C.I.
for Ypred');
printf('-----------------------------------------------------------------------
----------');
for i = 1:m
printf('%4.0f %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g',...
i,y(i),yh(i),e(i),CIYL(i),CIYU(i),CIYpL(i),CIYpU(i));
end;
printf('-----------------------------------------------------------------------
----------');
printf(' ');printf(' ');
printf('Analysis of variance');
printf('--------------------------------------------------------');
printf('Source of Sum of Degrees of Mean')
printf('variation squares freedom square F0');
printf('--------------------------------------------------------');
printf('Regression %10.6g %10.0f %10.6g %10.6g',SSR,n,MSR,F0');
printf('Residual %10.6g %10.0f %10.6g ',SSE,m-n-1,MSE);
printf('Total %10.6g %10.0f ',SST,m-1);
printf('--------------------------------------------------------');
printf('With F0 = %g and F_alpha = %g,',F0,Fa);
if F0>Fa then
printf('reject the null hypothesis H0:beta1=beta2=...=betan=0.');
else
printf('do not reject the null hypothesis H0:beta1=beta2=...=betan=0.');
end;
printf('--------------------------------------------------------');
disp(' ');
printf('Additional information');
printf('---------------------------------------------------------');
printf('Standard error of estimate (se) = %g',se);
printf('Coefficient of multiple determination (R^2) = %g',R2);
printf('Multiple correlation coefficient (R) = %g',R);
printf('Adjusted coefficient of multiple determination = %g',R2a);
printf('---------------------------------------------------------');
printf(' ');
printf('Covariance matrix:');
disp(C);
printf(' ');
printf('---------------------------------------------------------');
//Plots of residuals - several options
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
for j = 1:n
xset('window',j);xset('mark',-9,2);xbasc(j);
plot2d(XA(:,j),e,-9)
xtitle('Residual plot - error vs. x'+string(j),'x'+string(j),'error');
end;
xset('window',n+1);xset('mark',-9,2);
plot2d(y,e,-9);
xtitle('Residual plot - error vs. y','y','error');
xset('window',n+2);xset('mark',-9,2);
plot2d(yh,e,-9);
xtitle('Residual plot - error vs. yh','yh','error');
Application of function multiplelinear
Consider the multiple linear regression of the form y = b
0
+b
1
x
1
+b
2
x
2
for the data shown in the
following table:
x
1
x
2
x
3
y
1.20 3.10 2.00 5.70
2.50 3.10 2.50 8.20
3.50 4.50 2.50 5.00
4.00 4.50 3.00 8.20
6.00 5.00 3.50 9.50
First, we load the data:
-->x1=[1.2,2.5,3.5,4.0,6.0];x2=[3.1,3.1,4.5,4.5,5.0];
-->x3=[2.0,2.5,2.5,3.0,3.5];y =[5.7,8.2,5.0,8.2,9.5];
-->X=[x1' x2' x3']
X =
! 1.2 3.1 2. !
! 2.5 3.1 2.5 !
! 3.5 4.5 2.5 !
! 4. 4.5 3. !
! 6. 5. 3.5 !
-->y=y'
y =
! 5.7 !
! 8.2 !
! 5. !
! 8.2 !
! 9.5 !
Then, we call function multiplelinear to obtain information on the multiple linear regression:
-->[b,C,se]=multiplelinear(X,y,0.1);
Multiple linear regression
==========================
Table of coefficients
-------------------------------------------------------------------------
i b(i) se(b(i)) Lower Upper t0 H0:b(i)=0
-------------------------------------------------------------------------
0 -2.16499 1.14458 -5.50713 1.17716 -1.89152 do not reject
1 -.71446 .21459 -1.34105 -.0878760 -3.3295 reject
2 -1.78504 .18141 -2.31477 -1.25531 -9.83958 reject
3 7.09418 .49595 5.64603 8.54234 14.3043 reject
-------------------------------------------------------------------------
t_alpha/2 = 2.91999
-------------------------------------------------------------------------
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
Table of fitted values and errors
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) C.I. for Y C.I. for Ypred
-------------------------------------------------------------------------------
1 5.7 5.63241 .0675944 5.54921 5.7156 5.5218 5.74301
2 8.2 8.2507 -.0506958 8.15624 8.34515 8.13913 8.36226
3 5 5.03718 -.0371769 4.93663 5.13772 4.92504 5.14931
4 8.2 8.22704 -.0270378 8.12331 8.33077 8.11459 8.33949
5 9.5 9.45268 .0473161 9.3565 9.54887 9.34096 9.56441
-------------------------------------------------------------------------------
Analysis of variance
--------------------------------------------------------
Source of Sum of Degrees of Mean
variation squares freedom square F0
--------------------------------------------------------
Regression 14.2965 3 4.7655 414.714
Residual .0114911 1 .0114911
Total 14.308 4
--------------------------------------------------------
With F0 = 414.714 and F_alpha = 53.5932,
reject the null hypothesis H0:beta1=beta2=...=betan=0.
--------------------------------------------------------
Additional information
---------------------------------------------------------
Standard error of estimate (se) = .10720
Coefficient of multiple determination (R^2) = .99920
Multiple correlation coefficient (R) = .99960
Adjusted coefficient of multiple determination = .99679
---------------------------------------------------------
Covariance matrix:
! 1.3100522 .2388806 - .1694459 - .5351636 !
! .2388806 .0460470 - .0313405 - .1002469 !
! - .1694459 - .0313405 .0329111 .0534431 !
! - .5351636 - .1002469 .0534431 .2459639 !
The results show that, for a confidence level = 0.1, the hypothesis H
0
:
0
= 0 may not be
rejected, meaning that you could eliminate that term from the multiple linear fitting. On the
other hand, the test for significance of regression indicates that we cannot reject the
hypothesis that a linear regression does exist. Plots of the residuals against variables x
1
, x
2
, x
3
,
y, and ^y are shown next.
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
A function for predicting values from a multiple regression
The following function, mlpredict, takes as arguments a row vector x containing values of the
independent variables [x
1
x
2
x
n
] in a multiple linear regression, b is a column vector
containing the coefficients of the linear regression, C is the corresponding covariance matrix,
se is the standard error of the estimate, and alpha is the level of confidence. The function
returns the predicted value y and prints the confidence intervals for the mean value Y and for
the predicted value of Y.
function [y] = mlpredict(x,b,C,se,alpha)
nb = length(b); nx = length(x);
if nb<>nx+1 then
error('mlpredict - Vectors x and b are of incompatible length.');
abort;
else
n = nx;
end;
[nC mC] = size(C);
if nC<>mC then
error('mlpredict - Covariance matrix C must be a square matrix.');
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
abort;
elseif nC<>n+1 then
error('mlpredict - Dimensions of covariance matrix incompatible with vector
b.');
abort;
end;
xx = [1 x]; //augment vector x
y = xx*b; //calculate y
CIYL = y - sqrt(xx*C*xx'); //Lower limit C.I. for mean Y
CIYU = y + sqrt(xx*C*xx'); //Upper limit C.I. for mean Y
CIYpL = y - se*sqrt(1+xx*(C/se)*xx'); //Lower limit C.I. for predicted Y
CIYpU = y + se*sqrt(1+xx*(C/se)*xx'); //Upper limit C.I. for predicted Y
//Print results
printf(' ');
disp(' ',x,'For x = ');
printf('Multiple linear regression prediction is y = %g',y);
printf('Confidence interval for mean value Y = [%g,%g]',CIYL,CIYU);
printf('Confidence interval for predicted value Y = [%g,%g]',CIYpL,CIYpU);
An application of function mlpredict, using the values of b, C, and se obtained from function
multiplelinear as shown above, is presented next:
-->y=mlpredict(x,b,C,se,0.1);
For x =
! 2. 3.5 2.8 !
Multiple linear regression prediction is y = 10.0222
Confidence interval for mean value Y = [9.732,10.3123]
Confidence interval for predicted value Y = [9.87893,10.1654]
Simple linear regression using function multiplelinear
Function multiplelinear can be used to produce the regression analysis for a simple linear
regression as illustrated in the following example. The data to be fitted is given in the
following table:
x y
1.02 90.02
1.05 89.14
1.25 91.48
1.34 93.81
1.49 96.77
1.44 94.49
0.94 87.62
1.30 91.78
1.59 99.43
1.40 94.69
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
A plot of the data is produced with:
-->plot2d(x,y,-1);xtitle(Simple linear regression example, x,y);
The regression analysis using function multiplelinear follows:
-->[b,C,se] = multiplelinear(X,Y,0.05);
Multiple linear regression
==========================
Table of coefficients
-------------------------------------------------------------------------
i b(i) se(b(i)) Lower Upper t0 H0:b(i)=0
-------------------------------------------------------------------------
0 72.2564 2.03849 67.645 76.8678 35.446 reject
1 16.1206 1.5701 12.5688 19.6724 10.2672 reject
-------------------------------------------------------------------------
t_alpha/2 = 2.26216
-------------------------------------------------------------------------
Table of fitted values and errors
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) C.I. for Y C.I. for Ypred
-------------------------------------------------------------------------------
1 90.02 88.6994 1.32059 88.1769 89.2219 87.552 89.8468
2 89.14 89.183 -.0430274 88.6967 89.6694 88.052 90.3141
3 91.48 92.4071 -.92714 92.081 92.7333 91.3363 93.4779
4 93.81 93.858 -.0479932 93.5232 94.1928 92.7845 94.9315
5 96.77 96.2761 .49392 95.8173 96.7349 95.1568 97.3953
6 94.49 95.4701 -.98005 95.0634 95.8767 94.3715 96.5686
7 87.62 87.4098 .21024 86.7835 88.036 86.2107 88.6089
8 91.78 93.2132 -1.43317 92.8897 93.5366 92.1432 94.2831
9 99.43 97.8881 1.54186 97.307 98.4692 96.7124 99.0639
10 94.69 94.8252 -.13523 94.4535 95.1969 93.7394 95.9111
-------------------------------------------------------------------------------
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
Analysis of variance
--------------------------------------------------------
Source of Sum of Degrees of Mean
variation squares freedom square F0
--------------------------------------------------------
Regression 109.448 1 109.448 105.416
Residual 8.30597 8 1.03825
Total 117.754 9
--------------------------------------------------------
With F0 = 105.416 and F_alpha = 5.31766,
reject the null hypothesis H0:beta1=beta2=...=betan=0.
--------------------------------------------------------
Additional information
---------------------------------------------------------
Standard error of estimate (se) = 1.01894
Coefficient of multiple determination (R^2) = .92946
Multiple correlation coefficient (R) = .96409
Adjusted coefficient of multiple determination = .92065
---------------------------------------------------------
Covariance matrix:
! 4.1554489 - 3.1603934 !
! - 3.1603934 2.4652054 !
---------------------------------------------------------
Compare the results with those obtained using function linreg:
-->[se,rxy,sxy,m,b,sy,sx,ybar,xbar]=linreg(x,y);
-->[m,b]
ans = ! 16.120572 72.256427 !
-->se
se = 1.0189435
-->rxy
rxy = .9640868
The data fitting produced with linreg is shown in the next figure:
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
Analysis of residuals
Residuals are simply the errors between the original data values y and the fitted values y ,
i.e., the values
e = y- y .
Plots of the residuals against each of the independent variables, x
1
, x
2
, , x
n
, or against the
original data values y or the fitted values y can be used to identify trends in the residuals. If
the residuals are randomly distributed about zero, the plots will show no specific pattern for
the residuals. Thus, residual analysis can be used to check the assumption of normal
distribution of errors about a zero mean. If the assumption of normal distribution of residuals
about zero does not hold, the plots of residuals may show specific trends.
For example, consider the data from the following table:
__________________________________________________________
x
1
x
2
p q r
__________________________________________________________
1.1 22.1 1524.7407 3585.7418 1558.505
2.1 23.2 1600.8101 3863.4938 1588.175
3.4 24.5 1630.6414 4205.4344 1630.79
4.2 20.4 1416.1757 3216.1131 1371.75
5.5 25.2 1681.7725 4409.4029 1658.565
6.1 23.1 1554.5774 3876.8094 1541.455
7.4 19.2 1317.4763 2975.0054 1315.83
8.1 18.2 1324.6139 2764.6509 1296.275
9.9 20.5 1446.5163 3289.158 1481.265
11. 19.1 1328.9309 2983.4153 1458.17
__________________________________________________________
The table shows two independent variables, x
1
and x
2
, and three dependent variables, i.e., p =
p(x
1
,x
2
), q = q(x
1
,x
2
), and r = r(x
1
,x
2
). We will try to fit a multiple linear function to the three
functions, e.g., p = b
0
+ b
1
x
1
+ b
2
x
2
, using function multiplelinear, with the specific purpose of
checking the distribution of the errors. Thus, we will not include in this section the output for
the function calls. Only the plots of residuals (or errors) against the fitted data will be
presented. The SCILAB command required to produce the plots are shown next.
-->x1=[1.1 2.1 3.4 4.2 5.5 6.1 7.4 8.1 9.9 11.0];
-->x2 = [22.1 23.2 24.5 20.4 25.2 23.1 19.2 18.2 20.5 19.1];
-->p = [ 1524.7407 1600.8101 1630.6414 1416.1757 1681.7725 ...
--> 1554.5774 1317.4763 1324.6139 1446.5163 1328.9309 ];
-->q = [ 3585.7418 3863.4938 4205.4344 3216.1131 4409.4029 ...
--> 3876.8094 2975.0054 2764.6509 3289.158 2983.4153 ];
-->r = [ 1558.505 1588.175 1630.79 1371.75 1658.565 1541.455 ...
--> 1315.83 1296.275 1481.265 1458.17 ];
-->X = [x1' x2'];
-->[b,C,se] = multiplelinear(X,p',0.01);
-->[b,C,se] = multiplelinear(X,q',0.01);
-->[b,C,se] = multiplelinear(X,r',0.01);
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
The following table summarizes information regarding the fittings:
Fitting R
2
R R
2
adj
Significance
p=p(x
1
,x
2
) 0.97658 0.98822 0.96989 reject H
0
q=q(x
1
,x
2
) 0.99926 0.99963 0.99905 reject H
0
r=r(x
1
,x
2
) 0.8732 0.93445 0.83697 reject H
0
The three fitting show good values of the coefficients R
2
, R, and R
2
adj
, and the F test for the
significance of the regression indicates rejecting the null hypothesis H
0
:
1
=
2
= =
n
= 0 in
all cases. In other words, a multiple linear fitting is acceptable for all cases. The residual
plots, depicted below, however, indicate that only the fitting of p=p(x
1
,x
2
) shows a random
distribution about zero. The fitting of q=q(x
1
,x
2
) shows a clear non-linear trend, while the
fitting of r=r(x
1
,x
2
) shows a funnel shape.
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
Scaling residuals
The errors e
i
may be standardized by using the values
ze
i
= e
i
/s
e
,
where s
e
is the standard error of the estimate from the multiple linear fitting. If the errors
are normally distributed then approximately 95% of them will fall within the interval (-2,+2).
Any residual located far outside of this interval may indicate an outlier point. Outlier points
are points that do not follow the general trend of the data. They may indicate an error in the
data collection or simply an extremely large or low value in the data. If sufficient justification
exists, outlier points may be discarded and the regression repeated with the remaining data
points.
Another way to scale residuals is to use the so-called studentized residuals defined as
,
1
ii e
i
i
h s
e
r

i = 1,2,,m, where h
ii
are the diagonal elements in the hat matrix, H, defined as
H = X(X
T
X)
-1
X.
This matrix relates the observed values y to the fitted values y , i.e., y = Hy. Thus, the
name hat matrix. Using the values h
ii
we can write the studentized standard error of the i-th
residual as
s
e
(e
i
) = s
e
(1-h
ii
)
1/2
.
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
Influential observations
Sometimes in a simple or multiple linear fitting there will be one or more points whose effects
on the regression are unusually influential. Typically, these so-called influential observations
correspond to outlier points that tend to drag the fitting in one or other direction. To
determine whether or not a point is influential we computed the Cooks distance defined as
,
) 1 )( 1 ( ) 1 )( 1 ( ) 1 ( ) 1 (
2
2
2
2 2
2
ii
ii i
ii
ii i
ii e
ii i
i
h n
h r
h n
h ze
h s n
h e
d
+

+ +

+ +

i=1,2,,m. A value of d
i
> 1 may indicate an influential point.
A function for residual analysis
The following function, residuals, produces a comprehensive residual analysis including the
standardized and studentized residuals, studentized standard error of estimates for the
residuals, and the corresponding Cooks distances. The function takes as input a matrix X
whose columns represent the independent variables, i.e., X = [x
1
x
2

x
n
], and the column
vector y containing the dependent variable. The function produces a table of results, plots of
the residuals and Cook distances, and returns the values s
e
(e
i
) , ze
i
, r
i
, and d
i
. A listing of the
function follows:
function [e,stderr,ze,r,d] = residuals(XA,y)
//Multiple linear regression - residual analysis
//e = residuals
//stderr = standard errors of residuals
//ze = standardized residuals
//r = studentized residuals
//d = Cook's distances
[m n] = size(XA); //Size of original X matrix
X = [ones(m,1) XA]; //Augmenting matrix X
H = X*inv(X'*X)*X'; //H ('hat') matrix
yh = H*y; //Fitted value of y
e=y-yh; //Errors or residuals
SSE=e'*e; //Sum of squared errors
MSE = SSE/(m-n-1); //Mean square error
se = sqrt(MSE); //Standard error of estimate
[nh mh] = size(H); //Size of matrix H
h=[]; //Vector h
for i=1:nh
h=[h;H(i,i)];
end;
see = se*(1-h).^2; //standard errors for residuals
ze = e/se; //standardized residuals
r = e./see; //studentized residuals
d = r.*r.*h./(1-h)/(n+1); //Cook's distances
//Printing of results
printf(' ');
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
printf('Residual Analysis for Multiple Linear Regression');
printf('-----------------------------------------------------------------------
----------');
printf(' i y(i) yh(i) e(i) se(e(i)) ze(i) r(i)
d(i)');
printf('-----------------------------------------------------------------------
----------');
for i = 1:m
printf('%4.0f %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g %10.6g',...
i,y(i),yh(i),e(i),see(i),ze(i),r(i),d(i));
end;
printf('-----------------------------------------------------------------------
----------');
printf('Standard error of estimate (se) = %g',se);
printf('-----------------------------------------------------------------------
----------');
printf(' ');
//Plots of residuals - several options
xset('window',1);xset('mark',-9,2);
plot2d(yh,e,-9);
xtitle('Residual plot - residual vs. y','y','e');
xset('window',2);xset('mark',-9,2);
plot2d(yh,ze,-9);
xtitle('Residual plot - standardized residual vs. yh','yh','ze');
xset('window',3);xset('mark',-9,2);
plot2d(yh,r,-9);
xtitle('Residual plot - studentized residual vs. yh','yh','r');
xset('window',4);xset('mark',-9,2);
plot2d(yh,d,-9);
xtitle('Cook distance plot','yh','d');
Applications of function residuals
Applications of function residuals to the multiple linear fittings p = p(x
1
,x
2
), q = q(x
1
,x
2
), and r
= r(x
1
,x
2
), follow. First, we load function residuals:
-->getf(residuals)
Residual analysis for p = p(x
1
,x
2
)
-->residuals(X,p');
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
Residual Analysis for Multiple Linear Regression
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) se(e(i)) ze(i) r(i) d(i)
-------------------------------------------------------------------------------
1 1524.74 1521.69 3.04682 8.02336 .13006 .37974 .0340687
2 1600.81 1578.51 22.3035 13.2116 .95204 1.68817 .31503
3 1630.64 1645.41 -14.7688 12.6407 -.63042 -1.16835 .16442
4 1416.18 1424.51 -8.33603 13.589 -.35583 -.61344 .0392612
5 1681.77 1678.61 3.16424 6.70004 .13507 .47227 .0646745
6 1554.58 1565.08 -10.5036 15.6687 -.44835 -.67035 .0333676
7 1317.48 1353.87 -36.3932 14.7089 -1.55347 -2.47422 .53468
8 1324.61 1298.97 25.6415 10.9047 1.09453 2.35141 .85834
9 1446.52 1418.35 28.1663 11.9558 1.2023 2.35587 .73966
10 1328.93 1341.25 -12.3207 9.18077 -.52592 -1.34202 .35865
-------------------------------------------------------------------------------
Standard error of estimate (se) = 23.427
-------------------------------------------------------------------------------
Notice that most of the standardized residuals are within the interval (-2,2), and all of the
values d
i
are less than one. This residual analysis, thus, does not reveal any outliers. Similar
results are obtained from the following table corresponding to the fitting q = q(x
1
,x
2
).
Residual analysis for q = q(x
1
,x
2
)
-->residuals(X,q');
Residual Analysis for Multiple Linear Regression
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) se(e(i)) ze(i) r(i) d(i)
-------------------------------------------------------------------------------
1 3585.74 3594.19 -8.45215 5.89407 -.49112 -1.43401 .48583
2 3863.49 3869.77 -6.27968 9.70543 -.36489 -.64703 .0462771
3 4205.43 4196.81 8.62144 9.28605 .50096 .92843 .10383
4 3216.11 3221.54 -5.43054 9.98268 -.31555 -.54400 .0308755
5 4409.4 4388.96 20.4452 4.92194 1.188 4.15388 5.00333
6 3876.81 3891.61 -14.8006 11.5105 -.86001 -1.28584 .12277
7 2975.01 2970.09 4.91247 10.8054 .28545 .45463 .0180525
8 2764.65 2738.01 26.6417 8.01076 1.54806 3.32574 1.71704
9 3289.16 3310.89 -21.7285 8.78291 -1.26257 -2.47396 .81567
10 2983.42 2987.34 -3.92933 6.74432 -.22832 -.58261 .0675956
-------------------------------------------------------------------------------
Standard error of estimate (se) = 17.2098
-------------------------------------------------------------------------------
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
Residual analysis for r = r(x
1
,x
2
)
-->residuals(X,r');
Residual Analysis for Multiple Linear Regression
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) se(e(i)) ze(i) r(i) d(i)
-------------------------------------------------------------------------------
1 1558.51 1493.88 64.627 17.7424 1.2475 3.64252 3.13458
2 1588.18 1558.26 29.9105 29.2154 .57737 1.02379 .11586
3 1630.79 1634.99 -4.20322 27.953 -.0811352 -.15037 .00272348
4 1371.75 1419.35 -47.6025 30.05 -.91888 -1.58411 .26181
5 1658.57 1683.84 -25.2722 14.8161 -.48783 -1.70573 .84366
6 1541.46 1574.41 -32.9551 34.6489 -.63614 -.95112 .0671713
7 1315.83 1372.19 -56.3571 32.5265 -1.08787 -1.73265 .26221
8 1296.27 1322.31 -26.0318 24.1141 -.50249 -1.07953 .18091
9 1481.27 1455.37 25.8962 26.4384 .49988 .97949 .12786
10 1458.17 1386.18 71.9883 20.3018 1.3896 3.5459 2.50387
-------------------------------------------------------------------------------
Standard error of estimate (se) = 51.8051
-------------------------------------------------------------------------------
The table corresponding to the fitting q=q(x
1
,x
2
) shows two residuals, e
1
and e
10
whose Cooks
distance is larger than one. These two points, even if their standardized residuals are in the
interval (-2,2), may be considered outliers. The residual analysis eliminating these two
suspected outliers is shown next.
Residual analysis for r = r(x
1
,x
2
) eliminating suspected outliers
To eliminate the outliers we modify matrix X and vector r as follows:
-->XX = X(2:9,:)
XX =
! 2.1 23.2 !
! 3.4 24.5 !
! 4.2 20.4 !
! 5.5 25.2 !
! 6.1 23.1 !
! 7.4 19.2 !
! 8.1 18.2 !
! 9.9 20.5 !
-->rr = r(2:9)
rr =
! 1588.175 1630.79 1371.75 1658.565 1541.455 1315.83
1296.275 1481.265 !
-->residuals(XX,rr');
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
Residual Analysis for Multiple Linear Regression
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) se(e(i)) ze(i) r(i) d(i)
-------------------------------------------------------------------------------
1 1588.18 1542.72 45.459 10.9925 1.27186 4.13546 4.57875
2 1630.79 1627.12 3.67448 17.1966 .10280 .21367 .00672191
3 1371.75 1393.76 -22.008 14.0957 -.61574 -1.56133 .48136
4 1658.57 1681.94 -23.3752 9.70312 -.65399 -2.40904 1.77832
5 1541.46 1563.69 -22.2351 22.8787 -.62210 -.97187 .0786800
6 1315.83 1345.33 -29.4969 18.9967 -.82527 -1.55274 .29870
7 1296.27 1291.79 4.48228 12.618 .12541 .35523 .0287305
8 1481.27 1437.77 43.4995 8.21833 1.21703 5.29299 10.1365
-------------------------------------------------------------------------------
Standard error of estimate (se) = 35.7423
-------------------------------------------------------------------------------
Even after eliminating the two influential observations we find that the remaining e
1
and e
8
are
influential in the reduced data set. We can check the analysis of residuals eliminating these
two influential observations as shown next:
-->XXX = XX(2:7), rrr = rr(2:7)
XXX =
! 3.4 !
! 4.2 !
! 5.5 !
! 6.1 !
! 7.4 !
! 8.1 !
rrr =
! 1630.79 1371.75 1658.565 1541.455 1315.83 1296.275 !
-->residuals(XXX,rrr');
Residual Analysis for Multiple Linear Regression
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) se(e(i)) ze(i) r(i) d(i)
-------------------------------------------------------------------------------
1 1630.79 1601.8 28.9923 33.2701 .20573 .87142 .40176
2 1371.75 1557.26 -185.509 65.1625 -1.31635 -2.84688 1.9071
3 1658.57 1484.88 173.68 96.7163 1.23241 1.79577 .33395
4 1541.46 1451.48 89.9739 96.4308 .63844 .93304 .0909297
5 1315.83 1379.11 -63.2765 63.918 -.44900 -.98996 .23759
6 1296.27 1340.14 -43.8605 35.9466 -.31123 -1.22016 .72952
-------------------------------------------------------------------------------
Standard error of estimate (se) = 140.927
-------------------------------------------------------------------------------
Even after eliminating the two points e
1
and e
8
from the reduced data set, another influential
point is identified, e
2
. We may continue eliminating influential points, at the risk of running
out of data, or try a different data fitting.
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
Multiple linear regression with function datafit
SCILAB provides function datafit, introduced in Chapter 8, which can be used to determine the
coefficients of a fitting function through a least-square criteria. Function datafit is used for
fitting data to a model by defining an error function e = G(b,x) where b is a column vector of m
rows representing the parameters of the model, and x is a column vector of n rows
representing the variables involved in the model. Function datafit finds a solution to the set
of k equations e
i
= G(b,x
i
) = 0.
The simplest call to function datafit is
[p,err]=datafit(G,X,b0)
where G is the name of the error function G(b,x), X is a matrix whose rows consists of the
different vectors of variables, i.e., X = [x
1
; x
2
; ;x
n
], and b0 is a column vector representing
initial guesses of the parameters b sought.
Function datafit can be used to determine the coefficients of a multiple linear fitting as
illustrated in the example below. The data to be fit is given in the following table:
x
1
x
2
x
3
x
4
y
25 24 91 100 240
31 21 90 95 236
45 24 88 110 290
60 25 87 88 274
65 25 91 94 301
72 26 94 99 316
80 25 87 97 300
84 25 86 96 296
75 24 88 110 267
60 25 91 105 276
50 25 90 100 288
38 23 89 98 261
First, we load the data and prepare the matrix XX for the application of function datafit.
-->XY=...
-->[25 24 91 100 240
-->31 21 90 95 236
-->45 24 88 110 290
-->60 25 87 88 274
-->65 25 91 94 301
-->72 26 94 99 316
-->80 25 87 97 300
-->84 25 86 96 296
-->75 24 88 110 267
-->60 25 91 105 276
-->50 25 90 100 288
-->38 23 89 98 261];
-->XX=XY';
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
Next, we define the error function to be minimized and call function datafit:
-->deff('[e]=G(b,z)',...
--> 'e=b(1)+b(2)*z(1)+b(3)*z(2)+b(4)*z(3)+b(5)*z(4)-z(5)')
-->[b,er]=datafit(G,XX,b0)
er = 1699.0093
b =
! - 102.71289 !
! .6053697 !
! 8.9236567 !
! 1.4374508 !
! .0136086 !
You can check these results using function multiplelinear as follows:
-->size(XY)
ans =
! 12. 5. !
-->X=XY(1:12,1:4);y=XY(1:12,5); //Prepare matrix X and vector y
-->[bb,C,se]=multiplelinear(X,y,0.10); //Call function multiplelinear
Multiple linear regression
==========================
Table of coefficients
-------------------------------------------------------------------------
i b(i) se(b(i)) Lower Upper t0 H0:b(i)=0
-------------------------------------------------------------------------
0 -102.713 207.859 -489.237 283.81 -.49415 do not reject
1 .60537 .36890 -.0806111 1.29135 1.64103 do not reject
2 8.92364 5.30052 -.93293 18.7802 1.68354 do not reject
3 1.43746 2.39162 -3.00988 5.88479 .60104 do not reject
4 .0136093 .73382 -1.35097 1.37819 .0185458 do not reject
-------------------------------------------------------------------------
t_alpha/2 = 1.85955
-------------------------------------------------------------------------
Table of fitted values and errors
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) C.I. for Y C.I. for Ypred
-------------------------------------------------------------------------------
1 240 258.758 -18.758 248.31 269.206 214.676 302.84
2 236 234.114 1.88623 219.86 248.367 175.738 292.49
3 290 266.689 23.3109 255.836 277.542 221.107 312.271
4 274 282.956 -8.95646 271.601 294.312 235.506 330.407
5 301 291.815 9.18521 284.131 299.498 257.72 325.909
6 316 309.356 6.64355 296.747 321.966 257.204 361.509
7 300 295.186 4.81365 287.17 303.203 259.916 330.457
8 296 296.157 -.15677 286.462 305.851 254.842 337.472
9 267 284.85 -17.8502 273.464 296.236 237.285 332.415
10 276 288.938 -12.9376 281.73 296.145 256.503 321.372
11 288 281.378 6.62157 274.517 288.24 250.134 312.622
12 261 254.802 6.19798 247.76 261.844 222.94 286.664
-------------------------------------------------------------------------------
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
Analysis of variance
--------------------------------------------------------
Source of Sum of Degrees of Mean
variation squares freedom square F0
--------------------------------------------------------
Regression 4957.24 4 1239.31 5.10602
Residual 1699.01 7 242.716
Total 6656.25 11
--------------------------------------------------------
With F0 = 5.10602 and F_alpha = 2.96053,
reject the null hypothesis H0:beta1=beta2=...=betan=0.
--------------------------------------------------------
Additional information
---------------------------------------------------------
Standard error of estimate (se) = 15.5793
Coefficient of multiple determination (R^2) = .74475
Multiple correlation coefficient (R) = .86299
Adjusted coefficient of multiple determination = .59889
---------------------------------------------------------
Covariance matrix:
! 43205.302 - 10.472107 - 142.32333 - 389.41606 - 43.653591 !
! - 10.472107 .1360850 - 1.4244286 .4289197 - .0095819 !
! - 142.32333 - 1.4244286 28.095536 - 5.3633513 .1923068 !
! - 389.41606 .4289197 - 5.3633513 5.7198487 - .1563728 !
! - 43.653591 - .0095819 .1923068 - .1563728 .5384939 !
---------------------------------------------------------
Notice that the error, e, returned by datafit is the sum of squared errors, SSE, returned by
function multiplelinear. The detailed regression analysis provided by multiplelinear indicates
that the hypothesis for significance of regression is to be rejected, i.e., the linear model is not
necessarily the best for this data set. Also, the coefficient of multiple regression and its
adjusted value are relatively small.
Note: Function datafit can be used to fit linear and non-linear functions. Details of the
application of function datafit were presented in Chapter 8.
Polynomial data fitting
A function for polynomial data fitting was developed in Chapter 5 to illustrate the use of matrix
operations. In this section, we present the analysis of polynomial data fitting as a special case
of a multilinear regression. A polynomial fitting is provided by a function of the form
y = b
0
+ b
1
z + b
2
z
2
+ + b
n
z
n
.
This is equivalent to a multiple linear fitting if we take x
1
= z, x
2
= z
2
, , x
n
= z
n
.
Thus, given a
data set {(z
1
,y
1
), (z
2
,y
2
), , (z
m
,y
m
)}, we can use function multiplelinear to obtain the
coefficients [b
0
,b
1
,b
2
,,b
n
] of the polynomial fitting. As an example, consider the fitting of a
cubic polynomial to the data given in the following table:
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
z 2.10 3.20 4.50 6.80 13.50 18.40 21.00
y 13.41 46.48 95.39 380.88 2451.55 5120.46 8619.14
The SCILAB instructions for producing the fitting are shown next. First, we load the data:
-->z=[2.10,3.20,4.50,6.80,13.50,18.40,21.00];
-->y=[13.41,46.48,95.39,380.88,2451.55,5120.46,8619.14];
Next, we prepare the vectors for the multiple linear regression and call the appropriate
function:
-->x1 = z; x2 = z^2; x3 = z^3; X = [x1' x2' x3']; yy = y';
-->[b,C,se]=multiplelinear(X,yy,0.01)
Multiple linear regression
==========================
Table of coefficients
-------------------------------------------------------------------------
i b(i) se(b(i)) Lower Upper t0 H0:b(i)=0
-------------------------------------------------------------------------
0 -467.699 664.835 -3528.66 2593.26 -.70348 do not reject
1 223.301 262.938 -987.289 1433.89 .84925 do not reject
2 -23.3898 26.1662 -143.861 97.0817 -.89390 do not reject
3 1.56949 .74616 -1.86592 5.00491 2.10341 do not reject
-------------------------------------------------------------------------
t_alpha/2 = 4.60409
-------------------------------------------------------------------------
Table of fitted values and errors
-------------------------------------------------------------------------------
i y(i) yh(i) e(i) C.I. for Y C.I. for Ypred
-------------------------------------------------------------------------------
1 13.41 -87.3819 100.792 -351.859 177.095 -4793.08 4618.32
2 46.48 58.78 -12.3 -115.377 232.937 -3048.97 3166.53
3 95.39 206.529 -111.139 25.4059 387.653 -3024.28 3437.34
4 380.88 462.697 -81.8173 221.167 704.228 -3836.65 4762.04
5 2451.55 2145.6 305.95 1889.3 2401.9 -2415.28 6706.48
6 5120.46 5499.33 -378.865 5272.7 5725.95 1463.9 9534.75
7 8619.14 8441.76 177.38 8143.74 8739.78 3141.72 13741.8
-------------------------------------------------------------------------------
Analysis of variance
--------------------------------------------------------
Source of Sum of Degrees of Mean
variation squares freedom square F0
--------------------------------------------------------
Regression 6.64055e+07 3 2.21352e+07 222.864
Residual 297964 3 99321.4
Total 6.67034e+07 6
--------------------------------------------------------
With F0 = 222.864 and F_alpha = 29.4567,
reject the null hypothesis H0:beta1=beta2=...=betan=0.
--------------------------------------------------------
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
Additional information
---------------------------------------------------------
Standard error of estimate (se) = 315.153
Coefficient of multiple determination (R^2) = .99553
Multiple correlation coefficient (R) = .99776
Adjusted coefficient of multiple determination = .99107
---------------------------------------------------------
Covariance matrix:
! 442005.47 - 166781.09 15518.351 - 412.44179 !
! - 166781.09 69136.156 - 6728.9534 183.73635 !
! 15518.351 - 6728.9534 684.66847 - 19.297326 !
! - 412.44179 183.73635 - 19.297326 .5567618 !
---------------------------------------------------------
With the vector of coefficients found above, we define the cubic function:
-->deff('[y]=yf(z)','y=b(1)+b(2)*z+b(3)*z^2+b(4)*z^3')
Then, we produce the fitted data and plot it together with the original data:
-->zz=[0:0.1:25];yz=yf(zz);
-->rect = [0 -500 25 15000]; //Based on min. & max. values of z and y
-->xset('mark',-1,2)
-->plot2d(zz,yz,1,'011',' ',rect)
-->plot2d(z,y,-1,'011',' ',rect)
-->xtitle('Fitting of a cubic function','z','y')
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
Alternatively, we can use function datafit to obtain the coefficients of the fitting as follows:
-->XX = [z;y]
XX =
! 2.1 3.2 4.5 6.8 13.5 18.4 21. !
! 13.41 46.48 95.39 380.88 2451.55 5120.46 8619.14 !
-->b0=ones(4,1)
b0 =
! 1. !
! 1. !
! 1. !
! 1. !
-->[b,er]=datafit(G,XX,b0)
er =
297964.09
b =
! - 467.6971 !
! 223.30004 !
! - 23.389786 !
! 1.5694905 !
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
Exercises
[1]. To analyze the dependency of the mean annual flood, Q(cfs), on the drainage area, A(mi
2
)
for a given region, data from six experimental watersheds is collected. The data is summarized
in the table below:
A(mi
2
) 16.58 3.23 16.8 42.91 8.35 6.04
Q(cfs) 455 105 465 1000 290 157
(a) Use function linreg to perform a simple linear regression analysis on these data. The
purpose is to obtain a relationship of the form Q = mA + b. (b) Determine the covariance of A
and B, (c)the correlation coefficient, and (d) the standard error of the estimate.
[2] For the data of problem [1] use function linregtable to perform the linear fitting. (a) What
is the decision regarding the hypothesis that the slope of the linear fitting may be zero at a
level of confidence of 0.05? (b) What is the decision regarding the hypothesis that the
intercept of the linear fitting may be zero at a level of confidence of 0.05?
[3] For the data of problem [1] use function multiplelinear to produce the data fitting. (a)
What is the decision regarding the hypothesis that the slope of the linear fitting may be zero at
a level of confidence of 0.05? (b) What is the decision regarding the hypothesis that the
intercept of the linear fitting may be zero at a level of confidence of 0.05? (b) What is the
decision regarding the hypothesis that the linear fitting may not apply at all?
[4]. The data shown in the table below represents the monthly precipitation, P(in), in a
particular month, and the corresponding runoff, R(in), out of a specific hydrological basin for
the period 1960-1969.
Year 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969
P (in) 1.95 10.8 3.22 4.51 6.71 1.18 4.82 6.38 5.97 4.64
R (in) 0.46 2.85 0.99 1.4 1.98 0.45 1.31 2.22 1.36 1.21
(a) Use function linreg to perform a simple linear regression analysis on these data. The
purpose is to obtain a relationship of the form R = mP + b. (b) Determine the covariance of P
and R, (c)the correlation coefficient, and (d) the standard error of the estimate.
[5] For the data of problem [1] use function linregtable to perform the linear fitting. (a) What
is the decision regarding the hypothesis that the slope of the linear fitting may be zero at a
level of confidence of 0.05? (b) What is the decision regarding the hypothesis that the
intercept of the linear fitting may be zero at a level of confidence of 0.05?
[6] For the data of problem [1] use function multiplelinear to produce the data fitting. (a)
What is the decision regarding the hypothesis that the slope of the linear fitting may be zero at
a level of confidence of 0.05? (b) What is the decision regarding the hypothesis that the
intercept of the linear fitting may be zero at a level of confidence of 0.05? (b) What is the
decision regarding the hypothesis that the linear fitting may not apply at all?
[7]. The following table shows data indicating the monthly precipitation during the month of
February, P
f
, and during the month of March, P
m
, as well as the runoff during the month of
March, R
m
, for a specific watershed during the period 1935-1958.
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
Year Pm Pf Rm
1935 9.74 4.11 6.15
1936 6.01 3.33 4.93
1937 1.30 5.08 1.42
1938 4.80 2.41 3.60
1939 4.15 9.64 3.54
1940 5.94 4.04 2.26
1941 2.99 0.73 0.81
1942 5.11 3.41 2.68
1943 7.06 3.89 4.68
1944 6.38 8.68 5.18
1945 1.92 6.83 2.91
1946 2.82 5.21 2.84
1947 2.51 1.78 2.02
1948 5.07 8.39 3.27
1949 4.63 3.25 3.05
1950 4.24 5.62 2.59
1951 6.38 8.56 4.66
1952 7.01 1.96 5.40
1953 4.15 5.57 2.60
1954 4.91 2.48 2.52
1955 8.18 5.72 6.09
1956 5.85 10.19 4.58
1957 2.14 5.66 2.02
1958 3.06 3.04 2.59
a. Use function multiplot to show the dependence of the many variables.
b. Use function multiplelinear to check the multiple linear fitting R
m
= b
0
+ b
1
P
m
+
b
2
P
f
.
c. For a level of confidence of 0.05, what are the decisions regarding the hypotheses
that each of the coefficients may be zero?
d. What is the decision regarding the hypothesis that the linear fitting may not apply
at all for the same level of confidence?
e. What value of runoff for the month of March is predicted if the precipitation in
the month of March is 6.2 in, and that of the month of February is 3.2 in?
f. What are the confidence intervals for the mean value and the prediction for the
data of question (e) at a confidence level 0.05?
[8]. In the analysis of runoff produced by precipitation into a watershed, often we are
required to estimate a parameter known as the time of concentration (t
c
) which determines
the time to the peak of the hydrograph produced by the watershed. It is assumed that the
time of concentration is a function of a characteristic watershed length (L), of a characteristic
watershed slope (S), and of a parameter known as the runoff curve number (CN). Runoff curve
numbers are numbers used by the U.S. Soil Conservation Service in the estimation of runoff
from watersheds. Runoff curve numbers are typically functions of the location of the
watershed and of its soil and vegetation covers. The following table shows values of the time
of concentration, t
c
(hr), the watershed length, L(ft), the watershed slope, S(%), and the runoff
curve number (CN) for 5 experimental watersheds.
Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz
t
c
(hr) 0.2 0.2 0.2 0.3 0.3
L (ft) 800 1200 2100 2000 1500
S (%) 2 3 4 6 1
CN 75 84 88 70 85
(a) Use function multiplot to show the interdependence of the various variables in the table.
(b) Assuming that a multiple-linear fitting can be used to explain the dependence of t
c
on L, S,
and CN, use function multiplelinear to determine the coefficients of the fitting. (c) For a level
of confidence of 0.01, what are the decisions regarding the hypotheses that each of the
coefficients may be zero? (d) What is the decision regarding the hypothesis that the linear
fitting may not apply at all for the same level of confidence? (e) What value of the time of
concentration is predicted for L = 1750 ft, S = 5%, and CN = 80. (f) What are the confidence
intervals for the mean value and the prediction for the data of question (e) at a confidence
level 0.05?
[9]. The data in the table below shows the peak discharge, q
p
(cfs), the rainfall intensity,
i(in/hr), and the drainage area, A(acres), for rainfall events in six different watersheds.
q
p
(cfs) 23 45 44 64 68 62
i(in/hr) 3.2 4.6 5.1 3.8 6.1 7.4
A(acres) 12 21 18 32 24 16
(a) Use function multiplot to show the interdependence of the various variables in the table.
(b) Assuming that a multiple-linear fitting can be used to explain the dependence of q
p
on i,
and A, use function multiplelinear to determine the coefficients of the fitting. (c) For a level
of confidence of 0.1, what are the decisions regarding the hypotheses that each of the
coefficients may be zero? (d) What is the decision regarding the hypothesis that the linear
fitting may not apply at all for the same level of confidence? (e) What value of the time of
concentration is predicted for i = 5.6 in/hr and A = 25 acres. (f) What are the confidence
intervals for the mean value and the prediction for the data of question (e) at a confidence
level 0.10?
[10]. Measurements performed across a pipeline diameter produce the following table of
velocities, v(fps), as function of the radial distance, r(in), measured from the pipe centerline.
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
r(in) V(fps) r(in) V(fps) r(in) V(fps) r(in) V(fps)
2.7 57.73 1.3 64.40 0.9 65.50 2.5 58.90
2.6 58.30 1.2 64.80 1.1 64.80 2.6 58.40
2.5 59.25 1.1 64.80 1.3 64.10 2.7 57.50
2.4 59.70 1.0 65.20 1.4 63.70 2.8 57.00
2.3 59.80 0.9 65.50 1.5 63.40 2.9 56.60
2.2 60.60 0.8 65.50 1.6 63.00 3.0 55.90
2.1 61.20 0.7 65.90 1.7 62.70 3.1 54.80
2.0 61.50 0.5 66.30 1.8 62.50 3.2 54.20
1.9 62.20 0.3 66.40 1.9 62.10 3.3 53.20
1.8 62.70 0.1 66.50 2.0 61.25 3.4 52.35
1.7 62.90 0.1 66.50 2.1 61.20 3.5 50.80
1.6 63.05 0.3 66.30 2.2 60.55 3.6 49.50
1.5 63.65 0.5 66.00 2.3 60.00 3.7 47.70
1.4 64.10 0.7 65.70 2.4 59.40 3.8 44.45
(a) Plot the velocity profile indicated in the table. Notice that the values of r start at 2.7 in
and decrease to 0.1in, just to increase again from 0.1 in to 3.8 in. What these values of r
represent are velocity measurements at both sides of the centerline along a diameter of the
pipe. To produce an accurate plot of the velocity distribution, take the values of r from 2.7 in
down to 0.1 in as negative, and the remaining values as positive. (b) Using SCILAB function
datafit, fit a logarithmic function of the form v = b
0
+ b
1
ln(y) to the data, where y = R-r, and R
is the pipe radius measured as R = 3.958 in.
[11]. The tables below show measurements of stagnation pressure on an air jet in a test set
up. The values of x represent distances from the jet outlet, where the values of r represent
distances from the jet centerline measured radially outwards. The stagnation pressure values
are used to calculate air velocities at the locations indicated by using v = (2gh)
1/2
, where g is
the acceleration of gravity. To make the units consistent, we recommend that you transform
the data to feet by using 1 in = (1/12) ft, and 1 cm = 0.0328 ft, and calculate the velocities
using g = 32.2 ft/s
2
.
Along centerline Across jet at x = 12 in
x(in) h(cm) r(in) h(cm) r(in) h(cm)
-0.25 20.58 8.50 0.00 -0.50 6.08
0.00 20.38 5.00 0.00 -1.00 5.06
0.50 20.16 4.50 0.15 -1.50 3.97
1.00 20.16 4.20 0.20 -2.00 2.73
3.00 20.16 3.70 0.47 -2.50 1.78
5.00 19.6 3.50 0.62 -3.00 1.11
6.00 18.25 3.09 0.94 -3.50 0.70
6.50 17.11 2.83 1.19 -4.20 0.25
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
8.00 13.52 2.55 1.62 -4.50 0.17
10.00 9.28 2.00 2.62 -4.80 0.12
15.00 4.14 1.50 3.91 -5.00 0.07
20.00 2.23 1.00 5.12 -5.30 0.02
0.50 6.07 -5.50 0.00
0.00 6.51
(a) Convert the data columns to feet and calculate the velocities corresponding to the different
values of h. (b) Plot the centerline velocities against the distance x and fit an equation of the
form
v(x) = b
0
/(b
1
+ b
2
x)
to the data resulting from the first table. (c) Plot the velocity v at section x = 12 in against the
radial distance |r|, and fit an equation of the form
v(r) = b
0
exp(-b
1
x
2
)
to the data resulting from the second table.
[12]. For relatively low pressures, Henrys law relates the vapor pressure of a gas, P, to the
molar fraction of the gas in mixture, x. The law is stated as P = kx, where k is known as
Henrys constant. To determine Henrys constant in practice we use data of P against x and
fit a straight line, i.e., P = b
0
+ b
1
x. If the value of b
0
can be taken as zero, then, b
1
= k.
Given the pressure-molar fraction data shown in the next table, use function linreg to
determine Henrys constant for the solubility of the following elements or compouns in water
at temperature indicated:
Sulfur dioxide, 23
o
C Carbon monoxide, 19
o
C Hydrogen, 23
o
C
P(mmHg) x(10
3
) P(mmHg) x(10
3
) P(mmHg) x(10
3
)
5 0.3263 900 2.417 1100 1.861
10 0.5709 1000 2.685 2000 3.382
50 2.329 2000 5.354 3000 5.067
100 4.213 3000 8.000 4000 6.729
200 7.448 4000 10.630 6000 9.841
300 10.2 5000 13.230 8000 12.560
6000 15.800
7000 18.280
8000 20.670
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
[13]. In the analysis of liquid mixtures it is often necessary to determine parameters known as
activity coefficients. For the mixture of two liquids the van Laar equations can be used to
determine the activity coefficients,
1
and
2
, in terms of the molecular fractions x
1
and x
2
:
2
2
1
2
2
2
1
1
1
ln ,
1
ln

,
_

,
_

x A
x B
A
x B
x A
A
.
The molecular fractions are related by
x
1
+ x
2
= 1.
The table below shows the activity coefficients for a liquid mixture as functions of the
molecular fraction x
1
. Use these data and SCILAB function datafit to obtain the values of the
van Laar coefficients A and B for the data.
x
1

2
0.1 4.90 1.05
0.2 2.90 1.20
0.3 1.95 1.30
0.4 1.52 1.50
0.5 1.30 1.70
0.6 1.20 2.00
0.7 1.10 2.25
0.8 1.04 2.60
0.9 1.01 2.95
[14]. An alternative relationship between the activity coefficients,
1
and
2
, in terms of the
molecular fractions x
1
and x
2
are the Margules equations:
ln
1
= (2B-A)x
2
2
+ 2(A-B)x
2
3
ln
2
= (2A-B)x
1
2
+ 2(B-A)x
2
3
.
Using the data of problem [13] and SCILAB function datafit, determine the coefficients A and B
of the Margules equations.
[15]. Infiltration into soil is typically modeled using Hortons equation
f = f
c
+ (f
0
-f
c
)e
-kt
,
where f is the infiltration rate, f
c
is the infiltration rate at saturation, f
0
is the initial
infiltration rate, t is time, and k is a constant that depends primarily on the type of soil and
vegetation of the area of interest.
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
The following table shows measurements of the infiltration rate, f, as function of time, t, for a
specific storm in a watershed.
t(hr) f(cm/hr) t(hr) f(cm/hr)
1 3.9 14 1.43
2 3.4 16 1.36
3 3.1 18 1.31
4 2.7 20 1.28
5 2.5 22 1.25
6 2.3 24 1.23
8 2 26 1.22
10 1.8 28 1.2
12 1.54 30 1.2
(a) Use SCILABs function datafit to obtain the parameters f
0
, f
c
, and k for the Hortons
equation.
(b) Plot the original data and the fitted data in the same set of axes.
[16]. The following data represents different properties of granite samples taken at the
locations indicated by the coordinates x(mi) and y(mi) on a specific site. The properties listed
in the table are as follows: x
1
= percentage of quartz in the sample, x
2
= color index (a
percentage), x
3
= percentage of total feldspar, w = specific gravity (water = 1.0).
x1 x2 x3 w y x
21.3 5.5 73.0 2.63 0.920 6.090
38.9 2.7 57.4 2.64 1.150 3.625
26.1 11.1 62.6 2.64 1.160 6.750
29.3 6.0 63.6 2.63 1.300 3.010
24.5 6.6 69.1 2.64 1.400 7.405
30.9 3.3 65.1 2.61 1.590 8.630
27.9 1.9 69.1 2.63 1.750 4.220
22.8 1.2 76.0 2.63 1.820 2.420
20.1 5.6 74.1 2.65 1.830 8.840
16.4 21.3 61.7 2.69 1.855 10.920
15.0 18.9 65.6 2.67 2.010 14.225
0.6 35.9 62.5 2.83 2.040 10.605
18.4 16.6 64.9 2.70 2.050 8.320
19.5 14.2 65.4 2.68 2.210 8.060
34.4 4.6 60.7 2.62 2.270 2.730
26.9 8.6 63.6 2.63 2.530 3.500
28.7 5.5 65.8 2.61 2.620 7.445
28.5 3.9 67.8 2.62 3.025 5.060
38.4 3.0 57.6 2.61 3.060 5.420
28.1 12.9 59.0 2.63 3.070 12.550
37.4 3.5 57.6 2.63 3.120 12.130
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
0.9 22.9 74.4 2.78 3.400 15.400
8.8 34.9 55.4 2.76 3.520 9.910
16.2 5.5 77.6 2.63 3.610 11.520
2.2 28.4 69.3 2.74 4.220 16.400
29.1 5.1 65.7 2.64 4.250 11.430
24.9 6.9 67.8 2.70 4.940 5.910
39.6 3.6 56.6 2.63 5.040 1.840
17.1 11.3 70.9 2.71 5.060 11.760
0.0 47.8 52.2 2.84 5.090 16.430
19.9 11.6 67.2 2.68 5.240 11.330
1.2 34.8 64.0 2.84 5.320 8.780
13.2 18.8 67.4 2.74 5.320 13.730
13.7 21.2 64.0 2.74 5.330 12.450
26.1 2.3 71.2 2.61 5.350 1.430
19.9 4.1 76.0 2.63 5.610 4.150
4.9 18.8 74.30 2.77 5.850 13.840
15.5 12.2 69.70 2.72 6.460 11.660
0.0 39.7 60.20 2.83 6.590 14.640
4.5 30.5 63.90 2.77 7.260 12.810
0.0 63.8 35.20 2.92 7.420 16.610
4.0 24.1 71.80 2.77 7.910 14.650
23.4 12.4 63.10 2.79 8.470 13.330
29.5 9.8 60.40 2.69 8.740 15.770
(a) Use function multiplot to show the interdependence of the various variables in the table.
(b) Assuming that a multiple-linear fitting can be used to explain the dependence of w on x
1
,
x
2
, and x
3
, use function multiplelinear to determine the coefficients of the fitting. (c) For a
level of confidence of 0.1, what are the decisions regarding the hypotheses that each of the
coefficients may be zero? (d) What is the decision regarding the hypothesis that the linear
fitting may not apply at all for the same level of confidence? (e) What value of the time of
concentration is predicted for x
1
=17, x
2
= 25, and x
3
= 55. (f) What are the confidence
intervals for the mean value and the prediction for the data of question (e) at a confidence
level 0.10?
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Time Series and Spatial Data
Analysis with SCILAB
By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
TIME SERIES AND SPATIAL DATA 2
Time series 2
Lag plots 6
Periodic stationary components 8
Autocovariance and autocorrelation 10
Cross-covariance and cross-correlation 12
Convolution 13
Moving averages 15
Differencing as convolution 18
Removal of seasonal components 18
Spatial data and geostatistics 19
Contouring 20
Trend identification 23
A function for trend identification and removal 26
Spatial autocorrelation - the semivariogram 29
Calculation of the experimental semivariogram 30
A function to calculate the experimental semivariogram 31
Semivariogram models 33
Kriging 37
Kriging model verification 40
Generating one-dimensional signals 43
Generating two-dimensional signals 48
Exercises 52
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Time series and spatial data
In this chapter we present the subjects of time series and spatial data, aspects of statistical
analysis of interest in hydrology, geology, and other Earth sciences. The subject of time series
is of interest in the analysis of time-dependent phenomenon such as temperature variations in
the atmosphere or the ocean, precipitation, economic patterns, transportation volumes in
highways and airports, and even TV ratings. Spatial data and geostatistics involve the
application of selected statistical techniques to data with spatial distributions, such as
vegetation patterns in selected basins, spatial precipitation patterns, aquifer properties, etc.
We will start the chapter with the treatment of time series.
Time series
As the name indicates, a time series is simply a collection of data values that vary with time.
One of the main applications of time series analysis is the determination of periodic
components in the series (also referred to as a time-dependent signal). To facilitate the
analysis, the data included in time series is typically collected at regular intervals. Thus,
government agencies such as the National Weather Service or the U.S. Geological Survey
maintain data bases of such parameters as daily minimum, maximum, and mean temperature,
pressure, precipitation, flow discharges, water surface elevations, etc.
Non-stationary time series of natural variables present a certain gradual trend, i.e., a slowly-
varying increase or decrease of the quantity measured that is apparent if the periodic
components were removed. Even in non-stationary time series there are some statistical
properties that present some degree of stationarity. The trend can be removed out of a non-
stationary time series by simply taking the first difference of the signal values, i.e., the
increments between consecutive values in the time series. If u
i
, i=1,2,,n, represents the time
series, with u
i
=u(t
i
), and t
i
increasing at regular intervals, the first difference in the time
series is simply the vector of quantities
u
i
= u
i+1
-u
i
,
for i=1,2,3,,n-1.
To illustrate the use of first differences for removing the trend in a non-stationary time series,
we generate a synthetic time series, u(t), using the following SCILAB statements:
-->t = [0:0.1:30]; u = 10*sin(t)+20*sin(2*t)+5*sin(3*t);
-->u = u + 2*t + 50 + 0.5*(rand(1,length(t))-0.5);
A plot of the time series is shown next. There is obviously an increasing trend apparent from
the signal, upon which the periodic components of the signal are superimposed.
-->xset('window',0);plot(t,u,'t','u','Time series')
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
The first difference vector can be calculated using the SCILAB function mtlb_diff. The
resulting vector, ud, has n-1 components. To plot this first difference vector we use a time
vector, td, that incorporates components 1 through n-1 of the original vector t. The plot of
the first differences is shown next.
-->ud=mtlb_diff(u);td=t(1:length(t)-1);
-->xset('window',1);plot(td,ud,'t','u_diff','Time series first difference')
A phase portrait of the first difference ud against the original signal u is obtained by using:
-->plot(ud,u(1:length(ud),ud,u
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
The following SCILAB statements produce a non-stationary time signal with a curvilinear trend.
-->v = u + 2*t.*(30-t)/10 + 50 + 0.5*(rand(1,length(t))-0.5);
-->xset('window',2);plot(t,v,'t','v','Time series')
Plots of the first and second difference for this signal are shown next.
-->vd=mtlb_diff(v);td=t(1:length(t)-1);
-->xset('window',3);plot(td,vd,'t','v_diff','Time series first difference')
-->vdd=mtlb_diff(vd);tdd=td(1:length(td)-1);
-->xset('window',4);plot(tdd,vdd,'t','v_diff2','Time series second difference')
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
The plot of first differences or the second signal, v(t), is very similar to the plot of first
differences for the first signal, u(t), since the stationary (periodic) components were generated
using the same function. The second difference plot for the second signal shows also a
periodic behavior.
Notice that the last graph (second difference of v(t)) shows a periodic behavior modified by
some random components that were introduced in the signal by using SCILAB function rand().
This is another characteristic of time series, i.e., the presence of a certain random noise that
many a times needs to be removed from the actual periodic signal.
A phase portrait of the first difference vd against the signal v is shown next.
-->plot(vd,v(1:length(vd),v,vd,Phase portrait - vd vs. v)
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
Lag plots
Lag plots are representations of the signal u(t+) against the signal u(t). The value is known
as the lag between the two signals. Lag plots can be used to identify periodicity in the signal.
Periodic signals show certain regular orbits as illustrated below with a few lag plots for signal
u(t). To produce the lag plots we use the following function:
function [] = lagplot(u,lag)
//Plot signal u(t+lag) vs u(t) given a vector
//of data u and an integer lag
n = length(u)
uu = u(lag+1:n);
up = u(1:length(uu));
plot(uu,up,'u(t+'+string(lag)+')','u(t)','Lagplot for lag = '+string(lag));
The following plots are lag plots for signal u(t). The lag is identified in the plot labels.
-->lagplot(u,1)
-->lagplot(u,5)
-->lagplot(u,10)
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
The periodicity of the signal is obvious from the regular orbits that result from the lag plots.
Lag plots of the first difference are shown next. Because the trend of the signal has been
removed in the first difference vector, resulting in a stationary periodic function, the result is
a single orbit, affected, of course, by the random noise the was included in the original signal.
-->lagplot(ud,1)
-->lagplot(ud,5)
-->lagplot(ud,10)
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Periodic stationary components
Once the trend has been removed from a non-stationary time series, the resulting stationary
signal may present a periodic behavior as demonstrated earlier. The identification of the
period or frequency of each of the periodic components in a stationary time series constitutes
an important application of time series analysis. It is often assumed that a stationary time
series posses a certain fundamental period T
0
, i.e., the time interval after which the signal
repeats itself. In many instances, such a period is not easily identified and it is taken as the
length of the signal itself. The frequency of a periodic component of period T is
f = 1/T,
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
while the angular frequency is given by
= 2/T = 2 f.
The techniques of Fourier analysis, presented in Chapter 10, are extremely useful in time series
analysis. Through the use of Fourier transforms it is possible to convert a signal from the time
domain, i.e., f(t), to the frequency domain, i.e., F(f) or F(), and vice versa.
As indicated in Chapter 10, SCILAB provides functions dft and fft to determine the Fourier
transforms from a signal v given as a vector of data. For example, for the data v(t) generated
earlier, dft produces the following Fourier transform.
-->V = dft(v,-1);VA = abs(V);plot(VA);
-->xtitle('Fourier transform for v','n','V(w)');
Notice that there is very large component at n = 1, and a tiny peak near 10. The very large
component at n = 1 is associated with the length of the signal, i.e., the discrete Fourier
transform assumes a fundamental period equal to the length of the signal, or T
0
= 30.
Eliminating the trend, we can eliminate that large component for n =1. A plot of the Fourier
transform of the first differences in the signal v(t) is shown next:
-->Vd = dft(vd,-1);VdA = abs(Vd);
-->plot(VdA);xtitle('Fourier transform for vd','n','Vd(w)');
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
This graph of the absolute value of the Fourier transform of the second differences in signal
v(t) reveals three main components located at values of n close to 10. A plot showing the
components n=1 to 20 reveals that the main components of the Fourier transform correspond
to values of n close to 6, 11, and 15. Recall that the initial time series was generated by
adding three sine functions with different frequencies. Thus, the Fourier transform of the first
differences reveals also three main components.
-->plot2d3(onn,[1:1:20],VdA(1:20));
-->xtitle(Fourier transform for vd,n,Vd(w))
Autocovariance and autocorrelation
We will define autocovariance as the covariance between the signal u(t) and the lagged signal
u(t+), i.e.,
R() = E[u(t) u(t+ )].
Some references (e.g., Newland, D.E., 1993, An Introduction to Random Vibrations, Spectral
& Wavelet Analysis, Longman Scientific & Technical, London) use the term autocorrelation
function for R(). On the other hand, Middleton (Middleton, G.V., 2000, Data Analysis in the
Earth Sciences Using MATLAB, Prentice Hall, Upper Saddle River, New Jersey) indicates that
the term autocorrelation refers to R() divided by the variance of the signal u(t).
SCILAB provides function corr to calculate the autocovariance function out of a vector signal u.
The call to the function with this purpose is
[cov,ubar] = corr(u,nlags)
where cov is the vector of length nlags containing the values of R(), where represents
(integer) lags from 1 to nlags, and ubar is the mean value of the signal u.
The following plot shows the autocovariance function R() for the signal u(t) defined earlier.
-->[cov,xbar]=corr(u,300);
Download at InfoClearinghouse.com 11 2001 Gilberto E. Urroz
-->xbar
xbar = 80.994022
-->plot(cov,'t lag','R(t lag)','Autocovariance')
The autocovariance of the first and second differences of signal v(t) is shown next.
-->[covvd,meanvd]=corr(vd,300);
-->plot(covvd,'t lag','R(t lag)','Autocovariance of vd')
-->[covvdd,meanvdd]=corr(vdd,299);
-->plot(covvdd,'t lag','R(t lag)','Autocovariance of vdd')
Download at InfoClearinghouse.com 12 2001 Gilberto E. Urroz
Cross-covariance and cross-correlation
The cross-covariance (also referred as cross-correlation function) of two time series u(t) and
v(t) is defined as
R
uv
= E[u(t)v(t+)].
Cross-covariances can be calculated using function corr with the call
[cov,means] = corr(u,v,nlags)
where cov is the vector of length nlags containing the values of R
uv
(), where represents
(integer) lags from 1 to nlags, and means is a vector containing the mean values of the signals
u and v.
To illustrate the use of cross-covariance calculations, we first generate a vector of values of
the function vv(t) = exp(-0.1t), for the values of t defined earlier, and calculate the cross-
covariance of signal u(t) with signal vv(t) as follows:
-->vv = exp(-0.1*t);
-->[couvv,means]=corr(u,vv,300);
-->plot(couvv,'t lag','Ruv(t lag)','Cross-covariance of u and vv')
The cross-covariance of signal vv(t) and u(t) is calculated as follows:
-->[couvv,means]=corr(u,vv,300);
-->plot(couvv,'t lag','Ruv(t lag)','Cross-covariance of u and vv')
Download at InfoClearinghouse.com 13 2001 Gilberto E. Urroz
You can check that the cross-covariance of u(t) with vv(t) is the same as that of vv(t) with u(t).
Convolution
The operation of convolution is defined as



t
d v t u t v u t y ) ( ) ( ) )( * ( ) (
If U(), V(), and Y(), represent the Fourier transforms of the signals u(t), v(t), and y(t),
respectively, then
Y() = U()V().
A physical interpretation of the convolution operation is found in the calculation of a
hydrograph in a hydrologic basin. A hydrologic basin can be considered as a (linear) system.
The input to the system is a set of values of precipitation i(t) whose graph is known as a
hyetograph. The basin is characterized by a function h(t), known as the unit hydrograph,
which represents the discharge output corresponding to a unit precipitation on the basin. The
output from the system is the water discharge out of the basin q(t),which is calculated as
. ) ( ) ( ) )( * ( ) (



t
d i t h t i h t q
These ideas are illustrated in the figure shown below.
For a couple of discrete signals, u = [u
1
u
2
u
n
], v = [v
1

v
2
v
m
], the convolution y(t) = (u*v)(t)
is achieved by calculating the elements of signal y as
Download at InfoClearinghouse.com 14 2001 Gilberto E. Urroz
.
1

i
j
j i j i
v u y
This operation is implemented in SCILAB using function convol, which is described below.
SCILAB function convol
SCILAB provides function convol to calculate the convolution of two functions. The simplest
call to the function is
[y] = convol(u,v)
where y is the vector of data representing the convolution function y(t) = (u*v)(t).
For example, consider the convolution of the following two functions h(t), representing a unit
hydrograph, and i(t), representing a hyetograph. The unit hydrograph is shaped as a triangle:
-->h=[0 1 2 3 4 5 4.5 4 3.5 3 2.5 2 1.5 1 0.5 0];
-->plot(h,'t','h(t)','Unit hydrograph')
The hyetograph is given by six values of precipitation:
-->i=[2 6 4 3 2 1]; ii = [i i(6)];
-->plot2d3('onn',[1:6]', i',1,011, ,[0 0 6 6.5]);
-->plot2d2('onn',[0:6]',ii',1,011, ,[0 0 6 6.5]);
-->xtitle('t','i(t)','Hyetograph')
Download at InfoClearinghouse.com 15 2001 Gilberto E. Urroz
The convolution q(t) = (h*i)(t) produces the hydrograph:
-->q = convol(h,i);
-->plot(q,'t','q(t)','Hydrograph')
Moving averages
A moving average consists in replacing a signal v(t), represented by a vector v of length n, by a
signal v
k
(t) where k is a positive integer, such that
( ) ,
1
1

k j
j i
i j k
v
k
v
j=1,2,,n-k+1. The resulting signal is represented by a vector vk with n-k+1 elements. This
approach will produce a reduced-size smoothed signal.
An alternative definition is to use the convolution of a vector of n ones, u=[1 1 1], with the
signal v divided by n, i.e.,
vk = (u*v)(t)/n.
Download at InfoClearinghouse.com 16 2001 Gilberto E. Urroz
To illustrate the use of moving averages, we present the following function, ranf, that
generates a signal consisting of random components combined in a Fourier series. A listing of
the function follows:
function [y] = ranf(x)
//Random Fourier series
y=[];
for j = 1:length(x)
y = [y ff(x(j))];
end;
function [y] = ff(x)
n=100*rand();
aa = int(10*(rand(1,n)-0.5));
bb = int(10*(rand(1,n)-0.5));
y = 0.0;
for j = 1:n
y = y + aa(j)*sin(2*%pi*x)+bb(j)*cos(2*%pi*x);
end;
The function requires as input a vector of values of x. We will generate a signal of 301 points
by using the following SCILAB commands:
-->getf('ranf')
-->x=[0:1:300];u=ranf(x);plot(u,'t','u(t)','Generated signal')
Next, we define a function to calculate the moving averages using the two approaches outlined
above. The function is listed next.
function [uk,ukp] = moving(u,k)
//Calculation of moving averages
//Averaging reduces vector size to n-k+1 elements
n = length(u);
m = n-k+1;
uk = zeros(1,m);
for j = 1:k
uk = uk + u(j:m+j-1);
end;
Download at InfoClearinghouse.com 17 2001 Gilberto E. Urroz
uk = uk/k;
//Using convolution
uu = ones(1,n);
ukp = convol(uu,u)/k;
To obtain the moving averages for n=50, we first load the function moving. The moving
averages using the approach that produces a shorter signal are stored in variable u50A, while
those calculated through convolution are stored in variable u50B. Plots of the functions are
shown below.
-->getf('moving')
-->[u50A,u50B] = moving(u,50);
-->xset('window',1);plot(u50A,'t','u50A','Moving average for n = 50')
-->xset('window',2);plot(u50B,'t','u50B','Moving average for n = 50')
Notice that both approaches produce smoother signals. The first approach, in which a shorter
signal is produced, reduced the size of the moving averages to about 250 from the original
signal length of 300. The second approach, in which convolution is used, actually increases the
size of the moving averages to about twice that of the original signal. This is a characteristic
of the discrete convolution operation involved in function convol. To produce the first n
moving averages (in this case n=50), the convolution approach pads the original signal with
zeros thus producing values of doubtful validity. The first approach, in which the actual
averages are calculated, will produce more reliable results.
Download at InfoClearinghouse.com 18 2001 Gilberto E. Urroz
Differencing as convolution
Differencing or taking the first difference of a signal can be obtained as the convolution of the
original signal u and the vector v = [1 -1]. As an example we generate a time series using the
function randf and calculate the convolution for differencing as follows:
-->x=[0:0.1:39.9];u=2.5*x+ranf(x);
-->xset('window',0);plot(u,'t','u(t)','Signal with a trend')
-->ud=convol(u,[1 -1]);
-->xset('window',1);plot(ud,'t','u_diff','First differences')
Removal of seasonal components
Seasonal components are removed by subtracting from the data the seasonal averages. To
facilitate calculations, we use data whose length is a multiple of the seasonal length. For
example, if a month represents a season, the number of terms in a season may be 4 if weekly
data is used. The monthly averages will be calculated for every four data values. If we call
m the size of the season (e.g., m=4), and n the length of the signal (e.g., n=48), then the
number of seasons will be s = n/m (e.g., s=48/4=12).
Download at InfoClearinghouse.com 19 2001 Gilberto E. Urroz
As an example consider the data for the stationary signal ud calculated earlier. We will
produce the signal us after removing seasonal averages assuming that a season has 40
elements. To produce the removal of seasonal components we propose the following function,
removeseason:
function [us] = removeseason(u,m)
n = length(u); //length of signal
s = int(n/m); //number of seasons in signal
if modulo(n,m)<>0 then //modify signal if the signal length
k = s*m+m-n; // is not a multiple of m
lmean = mean(u(s*m:n)) //mean value for last season
uu = [u lmean*ones(1,k)]; //extend last season
s = s+1;
else
uu = u;
end;
ur = matrix(uu,m,s)'; //rearrange matrix with seasons in each row
um = mean(ur,'c'); //calculate seasonal mean (by rows)
umean=[] //put together matrix with seasonal means
for j = 1:m
umean = [umean um];
end;
us = ur-umean; //subtract seasonal mean from signal
us = matrix(us,1,length(us)); //convert modified signal to a row vector
if modulo(n,m)<>0 then //reduce resulting signal length if needed
us = us(1:n);
end;
The SCILAB commands required to produced the adjusted signal are as shown below. A plot of
the adjusted signal follows.
-->us = removeseason(ud,40);
-->plot(us,'t','us','Seasonal averages removed - m = 40');
Spatial data and geostatistics
Of interest to Earth scientists, geologists, hydrologists, etc., is the analysis of spatially
distributed data. In this chapter we present the practical operation of contouring for
irregularly spaced data. Geostatistics refers to the application of statistical techniques to the
Download at InfoClearinghouse.com 20 2001 Gilberto E. Urroz
analysis of spatially distributed data. Some geostatistics techniques are also presented in this
chapter.
Contouring
Contouring refers to the development of contour plots in a two-dimensional area given an
irregularly located set of data points. SCILAB provides function fac to produce a contour plot
for such type of data distribution. Details for the operation of function fac can be obtained by
using the help facility in SCILAB, i.e., -->help fac
The following function, contouring, uses function fac but it includes a contouring scale and
provides additional information on the graph. The function can be used to generate contour
plots of irregularly spaced data. The input to the function consists of vectors x, y, and z, all
of length n representing the coordinates of points in space. The matrix triang has m rows and
4 columns. The first column of triang represents an identification number for the triangles
into which the contouring area is divided. The three remaining columns of triang contain the
indices of the vertices included in each of the triangles. The vector zinfo has the form
[zmin,Dz,zmax] representing the minimum, increment, and maximum values of the contours to
be plotted. The function returns a contour plot.
function [] = contouring(x,y,z,triang,zinfo)
zmin=zinfo(1);
Dz=zinfo(2);
zmax=zinfo(3);
N=(zmax-zmin)/Dz+1;
[n m] = size(triang);
triang = [triang ones(n,1)];
xbasc()
// use the upper part of graphic window for contour plot
xsetech([0,0,1.0,3/4])
xset('foreground',1);
xset("colormap",hotcolormap(N));
fec(x,y,T,z); //SCILAB contouring function
plot2d(x,y,-1);
for j = 1:length(z), xstring(x(j),y(j),string(z(j))); end;
xtitle('Contouring','x','y');
// second picture to show the contouring scale
xsetech([0,3/4,1,1/5]) ; // use the lower part of graphic window
Matplot((1:xget("lastpattern")),"010",[0,0.5,N+2,1.5],[1,N+2,1,1]);
for j=0:N+2
xstring(j,0.1,string(zmin+j*Dz));
end;
xtitle('Contouring scale');
As an example of contouring, consider the figure below including 11 vertices and 13 triangles
for contouring.
Download at InfoClearinghouse.com 21 2001 Gilberto E. Urroz
The following vertex data giving the coordinates of the vertices of a region to be contoured:
____________________
x y z
____________________
5. 5. 20.5
17. 4.5 21.2
40. 5. 15.5
6. 20. 10.6
15. 15. 22.3
33. 10. 20.3
38. 17. 19.2
10. 33. 17.5
15. 28. 23.5
27. 22. 24.8
30. 35. 20.2
____________________
These values will be entered into SCILAB as follows:
-->x=[5,17,40,6,15,33,38,10,15,27,30];
-->y=[5,4.5,5,20,15,0,17,33,28,22,35];
-->z=[20.5,21.2,15.5,10.6,22.3,20.3,19.2,17.5,23.5,24.8,20.2]
The following matrix, T, includes the information regarding the triangles (i.e., matrix triang).
The first column represents the triangle number, and the remaining three columns represent
the vertices of each triangle.
-->T =
[1. 1. 5. 2...
2. 2. 5. 6...
Download at InfoClearinghouse.com 22 2001 Gilberto E. Urroz
3. 2. 6. 3...
4. 6. 7. 3...
5. 1. 4. 5...
6. 5. 10. 6...
7. 6. 10. 7...
8. 4. 9. 5...
9. 5. 9. 10...
10. 10. 11. 7...
11. 4. 8. 9...
12. 9. 8. 11...
13. 10. 9. 11];
Next, we load function contouring and call it using the data values x, y, z, and T. We also
include in the call to the function the vector indicating the minimum value of z for the
contours, the increment in the contour values, and the final value of z for the contours. For
example, in the first cased illustrated below z
min
= 10, z = 1, and z
max
= 25.
-->getf('contouring')
-->contouring(x,y,z,T,[10,1,25])
In the next example we use the same x, y, z, and T data, but change the description of the
contours to z
min
= 10, z = 0.5, and z
max
= 25.
-->contouring(x,y,z,T,[10,0.5,25])
Download at InfoClearinghouse.com 23 2001 Gilberto E. Urroz
Trend identification
Trend identification, or trend analysis, in spatial data distribution is nothing but data fitting
using a variety of functions of positions (x,y). We can obtain data fitting to almost any
function by using SCILABs own datafit function. (Function datafit was described in detail in
Chapters 8 and 17.) In this section we use it to fit functions of the forms:
f(x,y) = b
0
+ b
1
x + b
2
y
f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy
f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy + b
4
x
2
+ b
5
y
2
to the data used in the previous example for contouring. The general procedure used consists
on defining function G(b,z) as required by function datafit, an initial value of the coefficients,
b0, and using function datafit to determine the coefficients of the fitting. After that step is
completed, we proceed to define the appropriate function f(x,y), and to evaluate it an a
regular grid, 0 < x < 40 , 0 < y < 40. The x- and y-grids are stored in variables xx and yy,
respectively. Function feval is used to calculate the fitted values of z into matrix zz.
SCILABs function contour is used to produce contour plots of the fitted data.
Case1. Linear function f(x,y) = b
0
+ b
1
x + b
2
y
-->Z = [x;y;z]; //This definition applies to the three cases
-->deff('[e]=G(b,z)','e=z(3)-b(1)-b(2)*z(1)-b(3)*z(2)')
-->b0=[1;1;1];
-->[b,err]=datafit(G,Z,b0)
err = 153.14402
b =
! 18.285047 !
Download at InfoClearinghouse.com 24 2001 Gilberto E. Urroz
! .0389328 !
! .0271277 !
-->deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y')
-->xx=[0:2.5:40];yy=xx;zz=feval(xx,yy,f);
-->contour(xx,yy,zz,10)
-->plot2d(x,y,-1,'010',' ',[0 0 40 40])
-->for j=1:11, xstring(x(j),y(j),string(z(j))); end;
-->xtitle('Trend identification with z = b0+b1*x+b2*y')
Case 2. Including one non-linear term, f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy
-->deff('[e]=G(b,z)','e=z(3)-b(1)-b(2)*z(1)-b(3)*z(2)-b(4)*z(1)*z(2)')
-->b0=[1;1;1;1];
-->[b,err]=datafit(G,Z,b0)
err =
124.19016
b =
! 22.567309 !
! - .1616097 !
! - .2578275 !
! .0138546 !
-->deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y+b(4)*x*y')
-->xx=[0:2.5:40];yy=xx;zz=feval(xx,yy,f);
-->contour(xx,yy,zz,10);
-->for j=1:11, xstring(x(j),y(j),string(z(j))); end;
-->plot2d(x,y,-1,'011',' ',[0 0 40 40])
-->xtitle('Trend identification using z = b0+b1*x+b2*y+b3*x*y','x','y')
Download at InfoClearinghouse.com 25 2001 Gilberto E. Urroz
Case 3. Quadratic relationship, f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy + b
4
x
2
+ b
5
y
2
-->deff('[e]=G(b,z)','e=z(3)-b(1)-b(2)*z(1)-b(3)*z(2)-b(4)*z(1)*z(2)-
b(5)*z(1)^2-b(6)*z(2)^2')
-->b0=[1;1;1;1;1;1];
-->[b,err]=datafit(G,Z,b0)
err =
58.673471
b =
! 13.775238 !
! 1.0137698 !
! - .2015857 !
! .0086525 !
! - .0247882 !
! - .0011964 !
-->deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y+b(4)*x*y+b(5)*x^2+b(6)*y^2')
-->xx=[0:2.5:40];yy=xx;zz=feval(xx,yy,f);
-->contour(xx,yy,zz,10)
-->plot2d(x,y,-1,'010',' ',[0 0 40 40])
-->for j=1:11, xstring(x(j),y(j),string(z(j))); end;
-->xtitle('Trend identification with z = b0+b1*x+b2*y+b3*x*y+b4*x^2+b5*y^2','x','y')
Based purely on the size of the error incurred in the three models, it seems that the quadratic
fitting, namely, f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy + b
4
x
2
+ b
5
y
2
, produces the best result. Notice also
that the quadratic function is the closest to the contour pattern found earlier using function
Download at InfoClearinghouse.com 26 2001 Gilberto E. Urroz
contouring. The user must realize that the identification of trends is only an approximation to
the actual distribution of the functions analyzed; therefore, the resulting data fittings must be
used very carefully.
A function for trend identification and removal
The following function, trend2d, provides a way to select one out of the three models used
earlier, namely, f(x,y) = b
0
+ b
1
x + b
2
y, f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy, or f(x,y) = b
0
+ b
1
x + b
2
y +
b
3
xy + b
4
x
2
+ b
5
y
2
, to produce a surface fitting. The function takes an input the row vectors x,
y, and u, all of the same length. The function is interactive requesting input from the user in
terms of selecting the fitting to use. It also requests input from the user regarding whether or
not the values of the signal, u(x,y), should be printed in the contour plot that the function
produces. The function returns as output the signal ud equal to the original signal minus the
corresponding fitted values. The general call to the function is
[ud,er] = trend2d(x,y,u)
A listing of the function follows:
function [ud,er] = trend2d(x,y,u)
//Check model to use
printf(' ');
printf('Models available for trend identification:')
printf('1 - u(x,y) = b(1)+b(2)*x+b(3)*y')
printf('2 - u(x,y) = b(1)+b(2)*x+b(3)*y+b(4)*x*y')
printf('3 - u(x,y) = b(1)+b(2)*x+b(3)*y+b(4)*x*y+b(5)*x^2+b(6)*y^2')
idmod = input('Select the model to fit:')
printf(' ');
if idmod <> 1 & idmod <> 2 & idmod <> 3 then
error('trend2d - model id number must be 1, 2, or 3');
abort;
end;
//Prepare data for data fitting
X = [x;y;u];
xmin = min(x); xmax = max(x);xrange = xmax-xmin;
xmin = xmin-xrange/10;xmax = xmax+xrange/10;
xrange = xmax-xmin; xx = [xmin:xrange/20:xmax];
ymin = min(y); ymax = max(y);yrange = ymax-ymin;
ymin = ymin-yrange/10;ymax = ymax+yrange/10;
yrange = ymax-ymin; yy = [ymin:yrange/20:ymax];
rect = [xmin ymin xmax ymax];
//Obtain parameters for selected data fitting
if idmod == 1 then
deff('[e]=G(b,z)',...
'e=b(1)+b(2)*z(1)+b(3)*z(2)-z(3)');
b0 = [1;1;1];
[b,er] = datafit(G,X,b0);
title = 'Linear model, u = b1+b2*x+b3*y';
deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y');
elseif idmod == 2 then
deff('[e]=G(b,z)',...
'e=b(1)+b(2)*z(1)+b(3)*z(2)+b(4)*z(1)*z(2)-z(3)');
b0 = [1;1;1;1];
[b,er] = datafit(G,X,b0);
title = 'Model with one non-linear term, u = b1+b2*x+b3*y+b4*x*y';
deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y+b(4)*x*y');
Download at InfoClearinghouse.com 27 2001 Gilberto E. Urroz
else
deff('[e]=G(b,z)',...
'e=b(1)+b(2)*z(1)+b(3)*z(2)+b(4)*z(1)*z(2)+...
b(5)*z(1)^2+b(6)*z(2)^2-z(3)');
b0 = [1;1;1;1;1;1];
[b,er] = datafit(G,X,b0);
title = 'Quadratic model, u = b1+b2*x+b3*y+b4*x*y+b5*x^2+b6*y^2';
deff('[w]=f(x,y)','w=b(1)+b(2)*x+b(3)*y+b(4)*x*y+b(5)*x^2+b(6)*y^2');
end;
//Plot contour plots of signal
uu = feval(xx,yy,f);
xset('window',1);
xbasc();
xset('mark',-1,1);
plot2d(x,y,-1,'011',' ',rect);
xtitle(title,'x','y');
contour(xx,yy,uu,10);
printf(' ');
answer = input(...
'Plot original points in the original contour plot? y/n:',...
'string');
if answer == 'y' then
for j=1:length(x)
xstring(x(j),y(j),' '+string(u(j)),0,1);
end;
end;
//Remove trend
ud = zeros(1,length(u));
for j =1:length(ud)
ud(j) = u(j)-f(x(j),y(j));
end;
As an example of application of function trend2d, consider the following spatial signal u =
u(x,y):
x y u
20 120 25.7
10 80 37.8
35 90 39.0
50 120 23.0
72 122 27.0
67 88 42.0
40 75 36.0
70 60 47.5
42 46 42.5
20 60 37.0
First, we load the data into SCILAB:
-->x=[20,10,35,50,72,67,40,70,42,20];
-->y=[120,80,90,120,122,88,75,60,46,60];
-->u=[25.7,37.8,39.0,23.0,27.0,42.0,36.0,47.5,42.5,37.0];
Next, we use function trend2d to produce the fitting using each of the three models:
-->[ud,er]=trend2d(x,y,u)
The fitting mean square errors (MSE) for each of the fitting models for this case are:
Download at InfoClearinghouse.com 28 2001 Gilberto E. Urroz
Model 1 - error = 120.1648
Model 2 - error = 99.508868
Model 3 - error = 31.133571
Contour plots for the three fitting functions are shown next:
Download at InfoClearinghouse.com 29 2001 Gilberto E. Urroz
Spatial autocorrelation - the semivariogram
In the analysis of the dependency of two variables x,y, given a data set {(x
1
,y
1
), (x
2
,y
2
), ,
(x
n
,y
n
)}, the semivariogram of the data set is defined as
. ) (
2
1
1
2


n
i
i i XY
y x
n

Letx andy be the mean values of the vectors x and y, s


X
and s
Y
, the standard deviation of the
vectors x and y, and s
XY
the covariance of the data. You can prove that the semivariogram is
related to these statistics by
2
XY
= s
X
2

+ s
Y
2
+ (x-y)
2
- 2s
XY
.
Similar to the case of time-dependent signals, spatial data tend to be autocorrelated. A
measure of spatial autocorrelation is a function called the semivariogram. Consider a spatial
series u
i
, i=1,2,,n, so that u
i
= u(x
i
), where x represents a spatial variable, with values of x
equally spaced by an amount x. The semivariogram of the signal u(x) for a spatial lag = kx
is given by


A A
dx u u
A
, )] ( ) ( [
) ( 2
1
) (
2
! x x
where AA
-
is the intersection of a region A and the translated region A
-
, so that if x is
contained in the intersection, AA
-
, then both x and x+

are in A, and A() is the measure of
the intersection (i.e., an area or volume). Implied in this definition is the fact that x and
are vectors of one, two, or three dimensions.
For a discrete signal, along a single direction, the semivariogram corresponding to a spatial lag
= kx, i.e., a lag of k positions, is given by
. ) (
2
1
1
2

+

k n
i
k i i k
u u
n

The same expression applies to an isotropic (independent of direction) signal distributed in a


plane or in space.
The semivariogram will be related to the (spatial) autocovariance R
k
by

k
= (C
0
+ C
N
) - R
k
,
where C
0
and C
N
are constant values. The figure below shows a typical semivariogram. The
semivariogram reaches the constant value
k
= C
0
+ C
N
for very large values of the lag k, starting
at a value k=k
0
. The value C
0
+ C
N
is referred to as the sill of the variogram. For a given
variogram, thus, the autocovariance R
k
= C
0
+ C
N
-
k
is a decreasing value of the lag, k. The
variation of the autocovariance is also illustrated in the figure below.
Download at InfoClearinghouse.com 30 2001 Gilberto E. Urroz
In theory, the semivariogram should be zero at zero lag, i.e.,
0
= 0, or C
N
= 0. In practice,
however, we allow for the existence of a small value, C
N
, referred to as the nugget value.
Calculation of the experimental semivariogram
The practical calculation of the semivariogram in an isotropic field is outlined by Kitanidis(op.
cit.) as shown next. It is assumed that we have a series of n measurements {u
1
(x
1
), u
2
(x
2
), ,
u
n
(x
n
)}, where x represents a vector or array of coordinates where the data was collected,
e.g., x
1
= (x
1
,y
1
), etc. The steps in the determination of the semivariogram are the following:
1. Plot the n(n-1)/2 squared differences, u
ij
= (u(x
i
)-u(x
j
))
2
/2, against the distances k
ij
=
||x
i
-x
j
|| (ij). The resulting figure is known as the raw variogram.
2. Determine the minimum and maximum value of the distances, k, and produce a series
of classes of values of k, similar to the classes used in the development of frequency
distributions (Chapter 13). Say, that you use N classes whose class boundaries are
given by:
kb = [kb
1
, kb
2
, , kb
N+1
].
3. Compute a value of the variogram, (k
m
), for class k
m
, as


m
M
i
i i
m
m
u u
M
k
1
2
, )] ' ( ) ( [
2
1
) ( x x
where M
k
is the number of distances k
i
= ||x
i
-x
j
|| contained within class number m,
i.e., kb
i
||x
i
-x
j
|| < kb
i+1
. The value of k
m
is taken as the average of the M
m
distances k
i
= ||x
i
-x
j
|| contained within class number m:
. || ' ||
1
1


m
M
i
i i
m
m
M
k x x
4. Plot the values (k
m
, (k
m
)), m=1,2,,N, and join these points to produce what is called
the experimental semivariogram.
Download at InfoClearinghouse.com 31 2001 Gilberto E. Urroz
Notes:
(1) The value of k
m
can be taken also to be the median of the M
m
distances k
i
=
||x
i
-x
j
|| contained within class number m, or simply as the class mark for
class number m, i.e., k
m
= (kb
m
+kb
m+1
)/2.
(2) The algorithm outlined above is for an isotropic medium. Anisotropic
applications require to take into account the direction in which the distances
are measured.
A function to calculate the experimental semivariogram
The following function, semivariogram, implements Kitanidis algorithm to calculate the
semivariogram for data distributed in a plane. The function takes as input vectors of values of
x, y, and u, and returns the set of values of k and Du computed in the first step of the
algorithm, as well as the semivariogram data (kk,gam) = (k
m
, (k
m
)). The function is
interactive, in the sense that the user needs to provide a vector with the class boundaries for k
after the function has determined the minimum and maximum values of k from step 1 in the
algorithm. The function also produces a plot showing the values (k,Du) as discrete points, and
the values (kk,gam) as a continuous line.
function [k,Du,kk,gam]=semivariogram(x,y,u)
//First step: calculate distances and differences of u squared
n = length(x);
X = [x' y'];
P = n*(n-1)/2;
Du= zeros(1,P);
k = zeros(1,P);
l = 0
for i = 1:n
for j = i+1:n
l = l+1;
Du(l) = (u(i)-u(j))^2;
k(l) = norm(X(i,:)-X(j,:));
end;
end;
//Request class boundary information from user:
kmin = min(k);kmax = max(k);
disp(' ');
printf('Values of minimum and maximum distances = %g %g',kmin,kmax);
kb = input('Enter vector of class boundaries for k:');
//Obtain frequency count and semivariogram information
N = length(kb)-1;
M = zeros(1,N);
kk = zeros(1,N);
gam = zeros(1,N);
for i = 1:P
for j = 1:N
if k(i)>=kb(j) & k(i)<kb(j+1) then
M(j) = M(j)+1;
kk(j) = kk(j) + k(i);
gam(j)= gam(j) + Du(i);
end;
Download at InfoClearinghouse.com 32 2001 Gilberto E. Urroz
end;
end;
//Calculate semivariogram
NN=0;
for j = 1:N
if M(j) <> 0
NN = NN + 1;
kk(j) = kk(j)/M(j);
gam(j)= gam(j)/M(j);
end
end;
//Modify semivariogram data eliminating empty classes
if NN<>N then
gamm = [];kkt = [];
for j = 1:N
if M(j)<>0 then
gamm = [gamm gam(j)];
kkt = [kkt kk(j)];
end;
end;
gam = gamm;kk = kkt;
end;
//Plot semivariogram data
gmin = min([gam Du]); gmax = max([gam Du]);
rect=[0 0 kmax gmax];
xset('window',1);xset('mark',-1,1);
plot2d(k,Du,-1,'011',' ',rect);
plot2d(kk,gam,1,'011',' ',rect);
xtitle('Semivariogram','k','gamma(k)');
The following script loads data taken from Kitanidis, P.K.,1997, Introduction to Geostatistics -
Applications in Hydrogeology, Cambridge University Press, Cambridge CB2 1RP, UK. The first
set of data corresponds to position (x,y) and water table elevation (u), all in ft, in an aquifer.
The second set of data corresponds to position (X,Y), in km, and aquifer transmissivity, in
m
2
/day. The data is loaded using the following SCILAB script:
x=[6.86,4.32,5.98,11.61,5.52,10.87,8.61,12.64,14.70,13.91,9.47,...
14.36,8.99,11.93,11.75,13.23,13.56,8.06,10.95,14.71,16.27,...
12.33,13.01,13.56,13.76,12.54,8.97,9.22,9.64];
y=[6.41,5.02,6.01,4.99,3.79,8.27,3.92,6.77,10.43,10.91,5.62,...
11.03,7.31,6.78,10.8,10.18,9.74,5.76,3.72,11.41,7.27,6.87,...
7.05,7.42,8.35,9.04,8.6,8.55,3.38];
u = [1061,1194,1117, 880,1202, 757,1038, 817, 630, 617, 986,...
625, 840, 847, 645, 662, 685,1023, 998, 584, 611, 847, 745,...
725, 688, 676, 768, 782,1022];
X=[0.876 0.188 2.716 2.717 3.739 1.534 2.078 3.324];
Y=[0.138 0.214 2.119 2.685 0.031 1.534 0.267 1.670];
U=[2.9 2.5 4.7 4.2 4.2 2.1 2.4 5.8 ];
A plot of the semi-variogram for the first data set is obtained by using:
-->exec('c:\myScilab\LoadXYU')
(output removed)
Download at InfoClearinghouse.com 33 2001 Gilberto E. Urroz
-->[k,Du,kk,gam]=semivariogram(x,y,u);
Values of minimum and maximum distances = .25495 12.1977
Enter vector of class boundaries for k:
[0:1:13];
A plot of the semi-variogram for the second data set is obtained by using:
-->[k,Du,kk,gam]=semivariogram(X,Y,U);
Values of minimum and maximum distances = .56600 3.55571
Enter vector of class boundaries for k:
[0.5:0.3:3.7];
Semivariogram models
Semivariograms obtained from real data can be written as linear combinations of a number of
basic semivariogram models. For a stationary function, i.e., one whose mean value is constant
and whose two-point covariance function depends only on the distance between the two
points, we can use the following semivariogram models [see Middleton, G.V., 2000, Data
Analysis in the Earth Sciences Using Matlab, Prentice Hall, Upper Saddle River, New Jersey, or
Download at InfoClearinghouse.com 34 2001 Gilberto E. Urroz
Kitanidis, P.K.,1997, Introduction to Geostatistics - Applications in Hydrogeology, Cambridge
University Press, Cambridge CB2 1RP, UK]:
The spherical model

'

>

,
_

0 0
0 0
3
0
3
0
,
0 ,
2 2
3
k k C
k k C
k
k
k
k
k

Here, C
0
=
2
, is a variance, and k
0
is referred to as the range.
The exponential model

'

>

,
_

otherwise
k C
k
k
, 0
0 ,
| |
exp( 1
0

Here, is an integral scale parameter, C


0
=
2
, and the range is k
0
3.
The Gaussian model

'

>

,
_

,
_

,
_

otherwise
k C
L
k
k
, 0
0 , exp 1
0
2

Here, again, C
0
=
2
, and the range is defined as the distance at which the correlation is 0.05,
i.e., k
0
7L/4.
The hole-effect model

'

>
,
_


,
_

otherwise
k C
L
k
L
k
k
, 0
0 , exp 1
0

Once more, C
0
=
2
. This model is commonly used for one-dimensional functions in hydrology.
The nugget-effect model
( )

'
>

otherwise
k C k
k
, 0
0 , ) ( 1
0

where C
0
is the nugget variance, and (k) is the Kronecker delta function. The nugget effect is
named after mining applications where the distribution of mineral tends to be in the form of
discrete nuggets rather than a continuous distribution.
Download at InfoClearinghouse.com 35 2001 Gilberto E. Urroz
For non-stationary functions, i.e., those whose semivariogram grows unbounded as the distance
k grows unbounded, the following semivariogram models are available:
The power model

k
= k
r
,
where > 0 and 0 < r < 2. This model is used to describe self-similar functions.
The linear model

k
= k,
a special case of the power model (r=1), containing only one parameter, .
The logarithmic model

k
= A ln(k),
where A>0. This model is typically used on finite volumes.
Linear combinations of the semivariogram models are typically used to fit data from the
experimental semivariogram. For example, a combination of the nugget-effect and
exponential models will produce a semivariogram defined by

'

>

,
_

,
_

otherwise
k C
L
k
L
k
C
N
k
, 0
0 , exp 1
0

Once we have selected a particular model, or combination of models, to fit a particular


experimental semivariogram, we can use function datafit (see Chapters 8 and 18) to obtain the
unknown parameters in the model. For details on the procedures used in practice to obtain a
semivariogram function out of the experimental semivariogram the reader is referred to
Kitanidis (op. cit., Chapter 4). The following example shows a simple application using the
spatial signal u(x,y) described in the following table (this data set was used earlier in
applications of function trend2d for trend identification and trend removal):
x y u
20 120 25.7
10 80 37.8
35 90 39.0
50 120 23.0
72 122 27.0
67 88 42.0
40 75 36.0
70 60 47.5
42 46 42.5
20 60 37.0
The following four figures correspond to the experimental semivariogram for distance classes k
identified as follows:
Download at InfoClearinghouse.com 36 2001 Gilberto E. Urroz
Case 1: [15,20,25,,85]
Case 2: [15,25,35,,85]
Case 3: [15,30,45,,90]
Case 4: [15,35,55,,95]
These figures are the result of using function semivariogram, i.e.,
-->[k,Du,kk,gam] = semivariogram(x,y,u);
Download at InfoClearinghouse.com 37 2001 Gilberto E. Urroz
The figures suggest a linear model for the variogram. To determine the linear model
corresponding to each of the four figures above, we use function linreg, developed in Chapter
17. The following table summarizes the results of using function linreg:
Case m b k s
k
s

s
k
r
k
s
e
1 3.80 -35.24 49.78 153.95 21.20 128.26 1708.20 0.63 103.86
2 4.24 -62.74 50.23 150.42 21.65 101.58 1989.24 0.90 47.45
3 4.75 -88.72 51.41 155.50 22.32 115.10 2367.04 0.92 51.69
4 4.66 -90.09 54.57 164.19 21.71 104.13 2195.76 0.97 30.27
The columns represent m = slope of linear fitting, b = intercept of linear fitting, k = mean
value of distances, = mean value of semivariograms, s
k
= standard deviation of distances, s

=
standard deviation of semivariogram, s
k
= covariance of k and , r
k
= correlation coefficient, s
e
= standard error of estimate. The correlation coefficient for the first last three cases is in the
range 0.90-0.97 indicating good correlation for a linear fitting. The main drawback of the
fittings is that the intercept is negative. We may just keep the slope and force the intercept
to zero, i.e., (k) = mk. We notice that the slopes are very similar. We could select, for
example, the average of the three last cases, i.e., m = 4.55, and use the semivariogram model
(k) = 4.55k.
Kriging
Kriging is a method of interpolation of spatial data using the semivariogram. The method was
developed first by a South African mining engineer by the name of D. G. Krige, thus the name
kriging. Predictions by using kriging are of two types: point kriging, which consists of
interpolating point values, and block kriging, consisting of estimating an average value on a
spatial region or block. Point kriging uses the semivariogram to predict the best values of
certain weigth factors, w
i,
so that the value of the function of interest, u(x), at a point P, i.e.,
u(x
P
), is estimated as
.
1

n
i
i i P
u w u
The kriging system is a linear system of equations that results from applying the method of
Lagrange multipliers (see Chapter 12) to the problem of minimizing the mean square error
between u
P
and u(x
P
), i.e., minimizing the objective function E[(u
P
- u(x
P
))
2
]. The kriging
system consists of n+1

equations with n+1 unknowns:
Download at InfoClearinghouse.com 38 2001 Gilberto E. Urroz
n i d k w
n
i
ij i
,..., 2 , 1 , ) ( ) (
1
0
+


and
. 1
1

n
i
i
w
The value in the system is a Lagrange multiplier, and the values k
ij
represent the distances k
ij
= ||x
i
-x
j
||, between points i and j.
Using matrix notation, the kriging system can be rewritten as Ax=b, where
.
1
) (
) (
) (
, ,
0 1 1 1
1 0 ) ( ) (
1 ) ( 0 ) (
1 ) ( ) ( 0
0
20
10
2
1
2 1
2 21
1 12
1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1

1
1
1
1
1
1
]
1

n n n n
n
n
k
k
k
b
w
w
w
x
k k
k k
k k
A




! !
"
"
! ! # ! !
"
"
The solution to the kriging system includes the n weigth coefficients used in the calculation of
u
P
, as well as the Lagrange multiplier , which is used to calculate the variance of the error,

+
n
i
i i e
k w s
1
0
2
. ) (
A function that calculates a point estimate using kriging
The following function, kriging, can be used to calculate a point estimate using the technique
of kriging. The general call to the function is
[uP,w,lambda,MSE] = kriging(x,y,u,x0,gam)
where uP is the point estimate being calculated, w is a vector containing the weight
coefficients w
i
, lambda is the Lagrange multiplier , and MSE is the mean square error or
variance of the error. The function requires an input vectors x, y, u, and x0. Vectors x, y,
and u are the data vectors describing the signal u(x,y), and x0 is a vector containing the
coordinates of the point where the estimate is to be evaluated. The input parameter gam is a
SCILAB external function representing the semivariogram model to be used in the kriging
procedure.
A listing of the function follows:
function [uP,w,lambda,MSE] = kriging(x,y,u,x0,gam)
//Calculates the estimate uP = u(x0)at point x0 (a vector)
//using the kriging system. The variables x, y, and u are row vectors
//of the same length with u(j) = u(x(j),y(j)), and x0 is a row vector
Download at InfoClearinghouse.com 39 2001 Gilberto E. Urroz
//representing the point where the value of u is to be estimated.
//gam is a SCILAB external function representing the semivariogram of
//the signal u(x). MSE = means square error.
n = length(x);
A = zeros(n+1,n+1);
A(1:n,n+1) = ones(n,1);
A(n+1,1:n) = ones(1,n);
b = zeros(n,1);
b(n+1) = 1.0;
for i = 1:n
b(i) = -gam(norm([x(i),y(i)]-x0));
for j = 1:n
if i==j then
A(i,j) = 0.0;
else
A(i,j) = -gam(norm([x(i),y(i)]-[x(j),y(j)]));
end;
end;
end;
xsol = A\b;
w = xsol(1:n);
lambda = xsol(n+1);
uP = u*w;
MSE = sqrt(-b'*xsol);
As an example, we will use the data vectors x, y, and u loaded for the example on
semivariogram model determination, namely,
-->x=[20,10,35,50,72,67,40,70,42,20];
-->y=[120,80,90,120,122,88,75,60,46,60];
-->u=[25.7,37.8,39.0,23.0,27.0,42.0,36.0,47.5,42.5,37.0];
We will use the semivariogram model (k) = 4.55k, developed in an earlier section, to produce
the kriging estimates. As an example, suppose that we wan to estimate the value of the signal
u and point x
0
= [20,80], we would use the following SCILAB commands:
-->deff('[g]=gam(k)','g=4.55*k')
-->getf('kriging')
-->[uP,w,lambda,MSE] = kriging(x,y,u,[20,80],gam)
MSE = 7.3340059
lambda = 5.9538866
w =
! .0172807 !
! .5206510 !
! .2341965 !
! - .0085810 !
! - .0034533 !
! - .0192623 !
! .1448531 !
! - .0097721 !
Download at InfoClearinghouse.com 40 2001 Gilberto E. Urroz
! - .0172351 !
! .1413225 !
uP = 37.405745
Kriging model verification
Model verification in the context of kriging applications consists in testing a couple of statistics
based on the model residuals. The residuals are calculated as

k
= u
k
- (u
P
)
k
,
for k=2,3,,n, where u
k
is the k-th value in the signal of interest, while (u
P
)
k
is the estimated
value found through the kriging procedure. Kriging for the k-th estimate uses the first k-1
values of the signal u
k
. The normalized residuals are defined as

k
=
k
/
k
,
where
k
is the standard error

k
= (MSE
k
)
1/2
.
The value of
k
is also obtained through the kriging procedure.
The test statistics that can be used for model verification are

n
i
k
n
M
2
1
,
1
1

and

n
i
k
n
M
2
2
2
.
1
1

Kitanidis (op. cit) indicates that M


1
follows a normal distribution with mean zero and standard
deviation 1/(n-1)
1/2
, thus, with a level of significance of = 0.05, the model is rejected if
.
1
2
| |
1

>
n
M
Alternatively, we can use the statistic M
2
, which follows the chi-square (
2
) distribution with
(n-1) degrees of freedom, to verify the model validity. For a level of significance = 0.05, we
need to obtain an upper limit U =
2

, and a lower limit L =


2
1-
, and reject the kriging model
if
Download at InfoClearinghouse.com 41 2001 Gilberto E. Urroz
M
2
> U or M
2
< L.
A function for kriging model verification
The following function, kriging_all, is used to calculate the estimates of the signal (u
P
)
i
,
residuals
i
, standard errors
i
, and normalized residuals
i
, for i=2,3,,n. The function
provides a table listing all these values. The function also calculates the statistics M
1
and M
2
,
and test these values against the normal or chi-square variates corresponding to a level of
significance = 0.05. The function returns a vector with the values of the estimates u
P
. The
general call to the function is
[uP] = kriging_all(x,y,u,gam)
where uP is the vector of estimates u
P
; x, y, and u are vectors describing the original spatial
signal u(x,y), and gam is a SCILAB external function representing the semivariogram model for
the kriging application. A listing of the function follows:
function [uP] = kriging_all(x,y,u,gam)
n = length(u);
delta = zeros(1,n);
uP = zeros(1,n);
sigma = zeros(1,n);
epsilon = zeros(1,n);
uA(1) = u(1);
for j = 2:n
xx = x(1:j-1); yy = y(1:j-1); uu = u(1:j-1);
[uA,w,lambda,MSE]=kriging(xx,yy,uu,[x(j),y(j)],gam);
uP(j)=uA;sigma(j)=sqrt(MSE);delta(j)=u(j)-uP(j);
epsilon(j) = delta(j)/sigma(j);
end;
printf(' ');
printf('Kriging model verification');
printf('------------------------------------------------------------');
printf(' i u(i) uP(i) sigma(i) delta(i) epsilon(j)');
printf('------------------------------------------------------------');
for i = 2:n
printf('%4.0f %10.6f %10.6f %10.6f %10.6f %10.6f',...
i,u(i),uP(i),sigma(i),delta(i),epsilon(i));
end;
printf('------------------------------------------------------------');
M1 = sum(epsilon)/(n-1);
if abs(M1) > 2/sqrt(n-1) then
printf('Reject kriging model based on the statistic M1 = %10.6f',M1);
else
printf('Do not reject kriging model based on the statistic M1 = %10.6f',M1);
end;
printf('------------------------------------------------------------');
M2 = sum(epsilon.^2)/(n-1);
nu = n-1;
alpha = 0.05;
Uchisq = cdfchi('X',nu,1-alpha,alpha);
Lchisq = cdfchi('X',nu,alpha,1-alpha);
if M2 > Uchisq | M2 < Lchisq then
printf('Reject kriging model based on the statistic M2 = %10.6f',M2);
else
Download at InfoClearinghouse.com 42 2001 Gilberto E. Urroz
printf('Do not reject kriging model based on the statistic M2 = %10.6f',M2);
end;
printf('------------------------------------------------------------');
printf(' Degrees of freedom = %8.4f',nu);
printf(' Chi-square lower limit = %10.6f',Lchisq);
printf(' Chi-square upper limit = %10.6f',Uchisq);
printf(' Significance level = 0.05');
printf('------------------------------------------------------------');
The application of function kriging_all to the data used above with function kriging produces
the following table of results:
-->getf('kriging_all')
-->uP = kriging_all(x,y,u,gam);
Kriging model verification
------------------------------------------------------------
i u(i) uP(i) sigma(i) delta(i) epsilon(j)
------------------------------------------------------------
2 37.800000 25.700000 4.401153 12.100000 2.749280
3 39.000000 32.720674 3.657278 6.279326 1.716940
4 23.000000 31.283744 3.796221 -8.283744 -2.182102
5 27.000000 24.841117 3.745999 2.158883 .576317
6 42.000000 33.769924 3.693156 8.230076 2.228467
7 36.000000 41.661315 3.202054 -5.661315 -1.768026
8 47.500000 40.825938 3.775930 6.674062 1.767528
9 42.500000 42.984844 3.648739 - .484844 - .132880
10 37.000000 39.345939 3.215456 -2.345939 - .729582
------------------------------------------------------------
Do not reject kriging model based on the statistic M1 = .469549
------------------------------------------------------------
Reject kriging model based on the statistic M2 = 3.040690
------------------------------------------------------------
Degrees of freedom = 9.0000
Chi-square lower limit = 3.325113
Chi-square upper limit = 16.918978
Significance level = 0.05
------------------------------------------------------------
The test on statistic M
1
indicates not rejecting the kriging model, while the test on the statistic
M
2
suggest we should reject the model. The chi-square testing on M
2
bases the rejection of
the kriging model on the fact that M
2
< L. However, the values M
2
and L are relatively close,
and not rejecting the kriging model may be the correct decision.
Generating synthetic signals
Synthetic signals can be generated to produce, for example, input data or parameter
distributions for simulation of certain geophysical (or other type of) models. Synthetic signals
can be generated in both the time and space domains. Synthetic time signals can be used, for
example, to simulate precipitation series in hydrologic models, traffic pattern in transportation
simulation, seasonal energy demands, etc. On the other hand, synthetic spatial signals can be
used to simulate, for example, spatial distribution of aquifer properties, or to simulate the
distribution of geological properties.
Download at InfoClearinghouse.com 43 2001 Gilberto E. Urroz
Generating one-dimensional signals
The procedure to generate a one-dimensional signal, given a particular autocovariance or
autocorrelation function R(), for time signals, or R(), for spatial signals, is to generate a
vector of values of R that serve as the basis from which the Fourier transform of the series will
be generated. For details on the justification of using the autocovariance to generate
synthetic signals the reader is referred to Chapter 6 in Bras, R.L. and I. Rodriguez-Iturbe,
,Random Functions and Hydrology, Addison-Wesley Publishing Company, Reading,
Massachussetts.
Recall from the applications of Fourier transforms in Chapter 10 that, to generate a real signal
x using SCILAB function dft, the n Fourier transform coefficients X must have a real number in
the first position X
1
, while the remaining n-1 values must be complex numbers such that X
n-j+2
=
X
j
, for j=2,3,,(n+1)/2. (X stands for the complex conjugate of X). In this case, the value of
n must be an odd number.
Based on a vector of m components representing values of the autocovariance R, we can
construct a vector of Fourier transforms Xf with n=2m-1 elements satisfying the conditions of
symmetry outlined above. Afterwards, we will use function dft (discrete Fourier transform) to
generate the signal. The following function, synthetic, takes as input the vector of values of
R, and returns the signal x. The function also produces plots of the absolute values of the
Fourier transform coefficients and of the signal itself. The plots are produced without labels to
allow the user to label them at their will. A list of the function follows:
function [x] = synthetic(X)
//Given a number of values of the spectrum X(w)
//this function generates a signal x(t)
m=length(X);n=2*m-1; //lengths of vectors
rand('uniform'); //uniform random numbers
th = [0 2*%pi*rand(1,m-1)]; //generate angles in [0,2*%pi]
XX = X.*(cos(th)+%i*sin(th)); //generate 1/2 of spectrum
Xf = [XX zeros(1:m-1)]; //extend spectrum vector
for j=2:(n+1)/2 //make spectrum vector symmetric
Xf(n-j+2)=conj(Xf(j));
end;
XfA = abs(Xf); //spectrum magnitude
xset('window',1);plot(XfA); //plot spectrum magnitude
x = real(fft(Xf,1)); //generate signal
xset('window',2);plot(x); //plot signal
__________________________________________________________________________________
Notice that function synthetic generates a number of random phase angles, th, to calculate the
synthetic signal. This is necessary in order to obtain the complex numbers that represent the
Fourier transform coefficients. The values of th are randomly generated from a uniform
distribution so that 0 < th < 2. You can add randomness to the magnitude of the Fourier
transform coefficients by multiplying the values in vector X by random numbers generated out
of a uniform or a normal distribution. An example of randomness in the magnitude of the
coefficients will be shown later in this section.
__________________________________________________________________________________
Download at InfoClearinghouse.com 44 2001 Gilberto E. Urroz
The correlation models, R
k
, for applications in geostatistics, can be obtained from the
semivariogram models since
R
k
= C
0
-
k
.
Thus, for the exponential model, in which
,
| |
exp( 1
0
C
k
k

,
_

we get the following autocovariance function


R
k
= C
0
exp(-|k|/).
Choosing arbitrary values of the parameters C
o
and , we can generate the vector of values of
R necessary to generate a synthetic signal x, and then use function synthetic to generate the
signal itself. For example, for C
0
= 150, and = 20, we will generate a vector of values of R
with the distance k varying between 0 and 50 with k increasing by 1, and use function synthetic
to produced the corresponding signal:
-->k=[0:1:50];R=150*exp(-abs(k)/20);x=synthetic(R);
-->xtitle('Synthetic signal - exponential model for covariance','x','u(x)')
-->xtitle('Fourier transform coefficients','n','X(n)')
A plot of the Fourier transform coefficients is shown next:
The synthetic signal generated out of the Fourier transform coefficients shown above is
presented in the graphic below:
Download at InfoClearinghouse.com 45 2001 Gilberto E. Urroz
An example using the autocovariance function for the Gaussian model, i.e.,
, exp
2
2
0

,
_


L
k
C R
k
with arbitrary values C
0
= 120, L = 50, we can generate a signal using:
-->k=[0:1:50];R=150*exp(-k^2/400);x=synthetic(R);
-->xtitle('Synthetic signal - Gaussian model for covariance','x','u(x)')
-->xtitle('Fourier transform coefficients','n','X(n)')
Plots of the Fourier transform coefficients and of the synthetic signal are shown next:
Download at InfoClearinghouse.com 46 2001 Gilberto E. Urroz
In the following example we use the spherical model for the autocovariance, namely,
R
k
= C
0
(1-1.5(k/k
0
)+0.5(k/k
0
)
3
).
We will use the values C
0
= 1.0 and k
0
= 0.15 for the parameters in the spherical distribution.
The following figures show the Fourier transform coefficients and the signal generated using
this spherical model.
-->k=[0:0.01:1];X=[];for j=1:length(k),X=[X f(k(j))]; end;
-->u=synthetic(X);
-->xtitle('Synthetic signal - spherical model w/o randomness','k','u(k)')
-->xtitle('Fourier transform coefficients - spherical model w/o
randomness','k','X(k)')
Download at InfoClearinghouse.com 47 2001 Gilberto E. Urroz
Next, we incorporate randomness into the magnitude of the Fourier transform coefficients by
multiplying the vector of values of R
k
times a vector of normally distributed random numbers.
The following SCILAB commands will be used to generate the randomized vector of Fourier
transform coefficients and the corresponding signal and graphics:
-->rand('normal');XR = X.*rand(1,length(k));u=synthetic(XR);
-->xtitle('Synthetic signal - spherical model with randomness','k','u(k)')
-->xtitle('Fourier transform coefficients - spherical model with randomness','k','XR(k)')
Download at InfoClearinghouse.com 48 2001 Gilberto E. Urroz
The general shapes of the signals generated with or without randomness incorporated in the
magnitude of the Fourier transform coefficients are very similar. To generate synthetic data
that simulates real measurements, however, the inclusion of randomness into the magnitude of
the Fourier series coefficients is recommended.
Generating two-dimensional signals
The generation of two-dimensional signals using Fourier transforms was first introduced in
Chapter 10. The Fourier transform coefficients must be provided as a matrix of (mostly)
complex numbers that satisfies certain conditions of symmetry. For the case in which the n
1

n
2
matrix of coefficients for the Fourier transform is such that both n
1
and n
2
are even, the
values Z(1,1), Z(n
1
/2+1,1), Z(1,n
2
/+1), and Z(n
1
/2+1,n
2
/2+1) must always be real. The
remaining elements of matrix Z are such that Z(n
1
-k+1,1) =Z(k,1), for k = 1,2,,n
1
/2, Z(1,n
2
-
m+1) =Z(1,m+1), for m = 1,2,,n
2
, and Z(n
1
-k+1,n
2
-m+1) =Z(k,m), for k = 1,2,,n
1
, m =
1,2,,n
2
, whereZ represents the complex conjugate of Z.
The magnitude of the coefficients, for the case of spatially distributed data based on
autocovariance models, is generated by using expressions of the form R(k) = R((x
2
+y
2
)
1/2
). The
following function, synthetic2d, can be used to generate a two-dimensional signal z(x,y) given
vectors of values of x and y, i.e., xr = [x0:Dx:xn] and yr = [y0:Dy:yn], and the function f(x,y)
that represents the autocovariance. The general call to the function is
[z,yy,xx] = synthetic2d(xr,yr,f)
where z is a matrix representing the two dimensional signal z(x,y), and xx and yy are vectors
corresponding to the x and y values over which the signal was calculated. If the input vectors
xr and yr contained an even number of elements, the vectors xx and yy returned by the
function are exactly the same as xr and yr, respectively. If the number of elements of either
xr or yr is odd, the corresponding vector (or vectors) gets recalculated so that the number of
elements is even. The function produces plots of the magnitudes of the Fourier series
coefficients and of the signal, and returns vectors xx, and yy, and matrix z. A listing of the
function is shown next.
function [z,yy,xx] = synthetic2d(xr,yr,f)
Download at InfoClearinghouse.com 49 2001 Gilberto E. Urroz
//Generates two-dimensional signal using Fourier transforms
//Check x-vector, modify it to make it of even length if needed
x0 = xr(1);Dx = xr(2)-xr(1);
n1 = length(xr);xn1 = xr(n1);
if modulo(n1,2) <> 0 then
n1 = n1+1;
Dx = (xn1-x0)/(n1-1);
xx = [xr(1):Dx:xn1];
else
xx = xr;
end;
//Check y-vector, modify it to make it of even length if needed
y0 = yr(1);Dy = yr(2)-yr(1);
n2 = length(yr);yn2 = yr(n2);
if modulo(n2,2) <> 0 then
n2 = n2+1;
Dy = (yn2-y0)/(n2-1);
yy = [yr(1):Dy:yn2];
else
yy = yr;
end;
//Generate basic values of 2-D Fourier transform
Z = feval(xx,yy,f);
rand('normal'); //Remove this and next line if no
Z = Z.*rand(n1,n2); //random effects desired in magnitude of Z
rand('uniform');
th = 2*%pi*rand(n1,n2);
Z = Z.*exp(%i*th);
//Symmetry conditions for Fourier transform coefficient
for i = 2:n1/2
Z(n1-i+2,1) = conj(Z(i,1));
end;
for j = 2:n2/2
Z(1,n2-j+2) = conj(Z(1,j));
end;
for i = 2:n1
for j = 2:n2
Z(n1-i+2,n2-j+2) = conj(Z(i,j));
end;
end;
Z(1,1) = abs(Z(1,1));
Z(n1/2+1,1) = abs(Z(n1/2+1,1));
Z(1,n2/2+1) = abs(Z(1,n2/2+1));
Z(n1/2+1,n2/2+1) = abs(Z(n1/2+1,n2/2+1));
//Plot magnitude of Fourier transform coefficients and signal
x=[1:1:n1]; y=[1:1:n2];
xset('window',1);xbasc();plot3d(x,y,abs(Z));
z=real(fft(Z,1));
xset('window',2);xbasc();plot3d(x,y,z);
printf(' ');
Download at InfoClearinghouse.com 50 2001 Gilberto E. Urroz
printf('=======================================================================
');
printf('Notes:')
printf('1. Original xr and yr vectors may have been modified in xx and yy.');
printf('2. Magnitude of Fourier transform coefficients shown in graph window
1.');
printf('3. Signal shown in SCILAB graph window 2.');
printf('=======================================================================
');
printf(' ');
As an example consider the two-dimensional exponential model for the covariance R(x,y) = C
0
exp(-(x
2
+y
2
)
1/2
/), with values of C
0
= 1.0 and = 0.05. We define the function R(x,y), and the
vectors xr and yr as follows:
-->deff('[w]=f(x,y)','w=exp(-norm([x,y])/0.05)')
-->xr = [0:0.1:1]; yr = [0:0.1:1];
The call to function synthetic2d produce the following plot for the Fourier transform
coefficients:
-->[u,yu,xu] = synthetic2d(xr,yr,f);
The plot of the signal is shown next:
Download at InfoClearinghouse.com 51 2001 Gilberto E. Urroz
A Gaussian model for the two-dimensional covariance has the form
. exp ) , (
2
2 2
0

,
_

+

L
y x
C y x R
We use the Gaussian model to generate a two-dimensional signal with C
0
= 1.0 and L = 5 using
function synthetic2d:
-->deff('[w]=f(x,y)','w=exp(-norm([x,y])^2/5^2)')
-->xr = [-0.5:0.1:0.5]; yr = [-0.5:0.1:0.5];
-->[u,yu,xu] = synthetic2d(xr,yr,f);
Download at InfoClearinghouse.com 52 2001 Gilberto E. Urroz
Exercises
The following function, rsignal, can be used to generate a pseudo-random time series. The
input is n, the number of points in the signal. The signal is returned as a row vector u.
function [u] = rsignal(n)
m = grand(1,1,'uin',5,15);
rand('normal');
aa = int(100*(rand(1,m)-0.5));
bb = int(100*(rand(1,m)-0.5));
a0 = int(100*(rand(1,1)-0.5));
rand('uniform');
x = [0:1/(n-1):1];
u = zeros(1,n);
for i = 1:n
u(i) = a0;
for j = 1:m
u(i)=u(i)+...
aa(j)*sin(2*j*%pi*x(i))+bb(j)*cos(2*j*%pi*x(i))+50*rand();
end;
end;
umin=min(u);umax=max(u);
umin=umin-abs(umax-umin)/10;
u = u-umin;
[1]. Use the user-defined function rsignal, shown above, to obtain a time series with 300
points.
a) Produce a plot of the first difference of the signal.
b) Produce a plot of the second difference of the signal.
c) Produce a phase portrait of the signal against its first difference.
d) Produce a lag plot of the signal for a lag of 5.
e) Produce a lag plot of the signal for a lag of 10.
f) Produce a lag plot of the signal for a lag of 20.
[2]. Use the user-defined function rsignal, shown above, to obtain a time series with 500
points.
a) Produce a plot of the first difference of the signal.
b) Produce a plot of the second difference of the signal.
c) Produce a phase portrait of the signal against its first difference.
d) Produce a lag plot of the signal for a lag of 5.
e) Produce a lag plot of the signal for a lag of 10.
f) Produce a lag plot of the signal for a lag of 20.
Download at InfoClearinghouse.com 53 2001 Gilberto E. Urroz
[3]. Use the user-defined function rsignal, shown above, to obtain a time series with 300
points.
a) Produce a plot of the first difference of the signal.
b) Produce a plot of the second difference of the signal.
c) Produce a phase portrait of the signal against its first difference.
d) Produce a lag plot of the signal for a lag of 5.
e) Produce a lag plot of the signal for a lag of 10.
f) Produce a lag plot of the signal for a lag of 20.
[4]. Use the user-defined function rsignal, shown above, to obtain a time series with 300
points.
a) Use SCILAB function dft and plot the magnitude of the Fourier series coefficients for
the signal.
b) Plot the autocovariance function for this signal.
c) Plot the autocovariance for the first differences of the signal.
[5]. Use the user-defined function rsignal, shown above, to obtain a time series with 100
points.
a) Use SCILAB function dft and plot the magnitude of the Fourier series coefficients for
the signal.
b) Plot the autocovariance function for this signal.
c) Plot the autocovariance for the first differences of the signal.
[6]. Use the user-defined function rsignal, shown above, to obtain a time series with 500
points.
a) Use SCILAB function dft and plot the magnitude of the Fourier series coefficients for
the signal.
b) Plot the autocovariance function for this signal.
c) Plot the autocovariance for the first differences of the signal.
[7]. Use the user-defined function rsignal, shown above, to obtain two time series u and v,
both with 300 points.
a) Plot the cross-covariance function for signals u and v.
b) Plot the convolution of signals u and v.
[8]. Use the user-defined function rsignal, shown above, to obtain two time series u and v,
both with 150 points.
a) Plot the cross-covariance function for signals u and v.
b) Plot the convolution of signals u and v.
[8]. Use convolution to obtain the hydrograph produced by the following hyetograph and unit
hydrograph.
hyetograph unit hydrograph
t(hr) i(mm/hr) q(m
3
hr/(mm s))
0 1 0
1 3 24.5
2 2 52
3 1 35.5
4 23.2
5 11.2
6 1.5
7 0
Download at InfoClearinghouse.com 54 2001 Gilberto E. Urroz
[9]. Use convolution to obtain the hydrograph produced by the following hyetograph and unit
hydrograph.
hyetograph unit hydrograph
t(hr) i(mm/hr) q(m
3
hr/(mm s))
0 1 0
1 3 112
2 5 231
3 2 556
4 4 423
5 1 325
6 305
7 245
8 209
9 180
10 150
11 115
12 95
13 60
14 20
15 0
[10]. The following table shows the annual maximum flow for the Ganga River in India
measured at specific station.
Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s) Year Q(m
3
/s)
1885 7241 1907 7546 1929 4545 1951 4458
1886 9164 1908 11504 1930 5998 1952 3919
1887 7407 1909 8335 1931 3470 1953 5470
1888 6870 1910 15077 1932 6155 1954 5978
1889 9855 1911 6493 1933 5267 1955 4644
1890 11887 1912 8335 1934 6193 1956 6381
1891 8827 1913 3579 1935 5289 1957 4548
1892 7546 1914 9299 1936 3320 1958 4056
1893 8498 1915 7407 1937 3232 1959 4493
1894 16757 1916 4726 1938 3525 1960 3884
1895 9680 1917 8416 1939 2341 1961 4855
1896 14336 1918 4668 1940 2429 1962 5760
1897 8174 1919 6296 1941 3154 1963 9192
1898 8953 1920 8174 1942 6650 1964 3024
1899 7546 1921 9079 1943 4442 1965 2509
1900 6652 1922 7407 1944 4229 1966 4741
1901 11409 1923 5482 1945 5101 1967 5919
1902 9164 1924 19136 1946 4629 1968 3789
1903 7404 1925 9680 1947 4345 1969 4546
1904 8579 1926 3698 1948 4890 1970 3842
1905 9362 1927 7241 1949 3619 1971 4542
1906 7092 1928 3698 1950 5899
Download at InfoClearinghouse.com 55 2001 Gilberto E. Urroz
a) Plot the time series for the discharge Q and the first difference of the series in
separate graphs.
b) Produce lag plots for the discharge Q using lags of 1, 5, 10, and 15 years.
c) Plot the magnitude of the Fourier transform coefficients for the discharge time series.
d) Plot the autocovariance of the discharge time series.
e) Produce the moving average plot for an averaging period of 5 years.
f) Produce the moving average plot for an averaging period of 10 years.
g) Produce the moving average plot for an averaging period of 15 years.
h) Produce the moving average plot for an averaging period of 20 years.
i) Remove the 5-year seasonality in the data (assuming such seasonality exists).
j) Remove the 10-year seasonality in the data (assuming such seasonality exists).
k) Remove the 15-year seasonality in the data (assuming such seasonality exists).
l) Remove the 20-year seasonality in the data (assuming such seasonality exists).
[11]. The following table shows the coordinates (x,y) and the elevations, above mean sea level,
of a particular type of sandstone.
x(mi) y(mi) z(ft) x(mi) y(mi) z(ft) x(mi) y(mi) z(ft)
0.4 3.8 607 2.6 2.5 236 4.6 1.6 255
0.9 3.5 491 2.7 2.1 292 4.7 1.9 285
1.4 3.7 555 2.8 2.4 246 5.2 1.6 286
1.7 2.9 359 2.9 2.2 255 5.6 1.4 326
1.9 3.2 406 3.2 2.7 305 5.8 1.4 354
3.6 4.1 761 3.7 2.2 247 5.5 0.4 513
3.9 3.4 556 3.4 1.8 243 5.6 0.7 676
2.2 2.0 238 3.6 1.9 249
2.4 2.4 231 4.2 3.1 594
a) Produce contours of the sandstone elevation using the user-defined function
contouring.
b) Produce contours that fit the data from the table to a linear function of the form
z = f(x,y) = b
0
+ b
1
x + b
2
y
c) Produce contours that fit the data from the table to a non-linear function of the form
z = f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy
d) contours that fit the data from the table to a quadratic function of the form
z = f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy + b
4
x
2
+ b
5
y
2
e) Obtain the experimental variogram of the original elevation data z.
f) Obtain the experimental variogram of the elevation data after removing the trend from
the linear function identified in part (b).
Download at InfoClearinghouse.com 56 2001 Gilberto E. Urroz
g) Obtain the experimental variogram of the elevation data after removing the trend from
the non-linear function identified in part (c).
h) Obtain the experimental variogram of the elevation data after removing the trend from
the quadratic function identified in part (d).
[12]. For the original sandstone elevation data of problem [11] obtain the parameters that fit
the semivariogram to the following models:
a) spherical model
b) exponential model
c) Gaussian model
d) hole-effect model
[13]. The following data represents different properties of granite samples taken at the
locations indicated by the coordinates x(mi) and y(mi) on a specific site. The properties listed
in the table are as follows: x
1
= percentage of quartz in the sample, x
2
= color index (a
percentage), x
3
= percentage of total feldspar , and w = specific gravity (water = 1.0)
x1 x2 x3 w y x
21.3 5.5 73.0 2.63 0.920 6.090
38.9 2.7 57.4 2.64 1.150 3.625
26.1 11.1 62.6 2.64 1.160 6.750
29.3 6.0 63.6 2.63 1.300 3.010
24.5 6.6 69.1 2.64 1.400 7.405
30.9 3.3 65.1 2.61 1.590 8.630
27.9 1.9 69.1 2.63 1.750 4.220
22.8 1.2 76.0 2.63 1.820 2.420
20.1 5.6 74.1 2.65 1.830 8.840
16.4 21.3 61.7 2.69 1.855 10.920
15.0 18.9 65.6 2.67 2.010 14.225
0.6 35.9 62.5 2.83 2.040 10.605
18.4 16.6 64.9 2.70 2.050 8.320
19.5 14.2 65.4 2.68 2.210 8.060
34.4 4.6 60.7 2.62 2.270 2.730
26.9 8.6 63.6 2.63 2.530 3.500
28.7 5.5 65.8 2.61 2.620 7.445
28.5 3.9 67.8 2.62 3.025 5.060
38.4 3.0 57.6 2.61 3.060 5.420
28.1 12.9 59.0 2.63 3.070 12.550
37.4 3.5 57.6 2.63 3.120 12.130
0.9 22.9 74.4 2.78 3.400 15.400
8.8 34.9 55.4 2.76 3.520 9.910
16.2 5.5 77.6 2.63 3.610 11.520
2.2 28.4 69.3 2.74 4.220 16.400
29.1 5.1 65.7 2.64 4.250 11.430
24.9 6.9 67.8 2.70 4.940 5.910
39.6 3.6 56.6 2.63 5.040 1.840
17.1 11.3 70.9 2.71 5.060 11.760
0.0 47.8 52.2 2.84 5.090 16.430
19.9 11.6 67.2 2.68 5.240 11.330
1.2 34.8 64.0 2.84 5.320 8.780
13.2 18.8 67.4 2.74 5.320 13.730
Download at InfoClearinghouse.com 57 2001 Gilberto E. Urroz
13.7 21.2 64.0 2.74 5.330 12.450
26.1 2.3 71.2 2.61 5.350 1.430
19.9 4.1 76.0 2.63 5.610 4.150
4.9 18.8 74.30 2.77 5.850 13.840
15.5 12.2 69.70 2.72 6.460 11.660
0.0 39.7 60.20 2.83 6.590 14.640
4.5 30.5 63.90 2.77 7.260 12.810
0.0 63.8 35.20 2.92 7.420 16.610
4.0 24.1 71.80 2.77 7.910 14.650
23.4 12.4 63.10 2.79 8.470 13.330
29.5 9.8 60.40 2.69 8.740 15.770
For each of the variables x
1
, x
2
, x
3
, and w,
a) Produce contours of the variable using the user-defined function contouring.
b) Produce contours that fit the data for each variable to a linear function of the form
z = f(x,y) = b
0
+ b
1
x + b
2
y
c) Produce contours that fit the data for each variable to a non-linear function of the
form
z = f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy
d) contours that fit the data for each variable to a quadratic function of the form
z = f(x,y) = b
0
+ b
1
x + b
2
y + b
3
xy + b
4
x
2
+ b
5
y
2
e) Obtain the experimental variogram of the original values for each variable.
f) Obtain the experimental variogram for each variable after removing the trend from the
linear function identified in part (b).
g) Obtain the experimental variogram for each variable after removing the trend from the
non-linear function identified in part (c).
h) Obtain the experimental variogram for each variable after removing the trend from the
quadratic function identified in part (d).
[14]. For the original x
1
data of problem [13] obtain the parameters that fit the semivariogram
to the following models:
a) spherical model
b) exponential model
c) Gaussian model
d) hole-effect model
[15]. For the original x
2
data of problem [13] obtain the parameters that fit the semivariogram
to the following models:
a) spherical model
b) exponential model
c) Gaussian model
d) hole-effect model
[16]. For the original x
3
data of problem [13] obtain the parameters that fit the semivariogram
to the following models:
Download at InfoClearinghouse.com 58 2001 Gilberto E. Urroz
a) spherical model
b) exponential model
c) Gaussian model
d) hole-effect model
[17]. For the original w data of problem [13] obtain the parameters that fit the semivariogram
to the following models:
a) spherical model
b) exponential model
c) Gaussian model
d) hole-effect model
[15]. This problem refers to the sandstone elevation data of problem [11]. Use kriging to
obtain the sandstone elevation at point (x,y) = (1.8,3.0). Use each of the semivariogram
models of problem [12] to estimate the elevation.
[16]. This problem refers to the data of problem [11]. Use function kriging_all to verify the
four kriging models developed in problem [12]. Which models should be rejected, if any?
[17]. This problem refers to the x
1
data of problem [13]. Use kriging to obtain x
1
at point (x,y)
= (4.25,2.80). Use each of the semivariogram models of problem [14] to estimate x
1
.
[18]. This problem refers to the data of problem [13]. Use function kriging_all to verify the
four kriging models for x
1
developed in problem [14]. Which models should be rejected, if any?
[19]. This problem refers to the x
2
data of problem [13]. Use kriging to obtain x
2
at point (x,y)
= (4.25,2.80). Use each of the semivariogram models of problem [15] to x
2
.
[20]. This problem refers to the data of problem [13]. Use function kriging_all to verify the
four kriging models for x
2
developed in problem [15]. Which models should be rejected, if any?
[21]. This problem refers to the x
3
data of problem [13]. Use kriging to obtain x
3
at point (x,y)
= (4.25,2.80). Use each of the semivariogram models of problem [16] to estimate x
3
.
[22]. This problem refers to the data of problem [13]. Use function kriging_all to verify the
four kriging models for x
3
developed in problem [16]. Which models should be rejected, if any?
[23]. This problem refers to the w data of problem [13]. Use kriging to obtain w at point (x,y)
= (4.25,2.80). Use each of the semivariogram models of problem [16] to estimate w.
[24]. This problem refers to the data of problem [13]. Use function kriging_all to verify the
four kriging models for w developed in problem [17]. Which models should be rejected, if any?
[25]. The one-dimensional signal corresponding to a fractal Brownian motion (fBm) has Fourier
transform coefficients whose magnitude is given by |X(k)|
2
= C/k
5-2D
, where 1 < D < 2. The
value D is known as the fractal dimension of the resulting signal. Produce one-dimensional
fBms consisting of 300 values for C = 1000000 (this is an arbitrary constant, you can choose a
different value if you want) and the following fractal dimensions: (a) D = 1.2; (b) D = 1.4; (c) D
=1.6; (e) D = 1.8.
[26]. A two-dimensional fractal signal has Fourier transform coefficients whose magnitude is
given by
Download at InfoClearinghouse.com 59 2001 Gilberto E. Urroz
( )
, | ) , ( |
4
2 2
2
D
m k
C
m k X

+

where k and m represent components of the frequency in two orthogonal directions, D is the
fractal dimension, and 2 < D < 3. Produce one-dimensional fBms based on a 64x64 grid of k
and m values for C = 1000000 (this is an arbitrary constant, you can choose a different value if
you want) and the following fractal dimensions:(a) D = 2.2; (b) D = 2.4; (c) D = 2.6; (e) D = 2.8.
[27]. The simulation of smooth surfaces such as those of sea waves or cloud surfaces utilizes
the so-called Pierson-Moskowics spectrum, |X(k)|
2
= (C/k
5
) exp(-b/k
4
), where C and f are
constants. Produce a one-dimensional smooth surface consisting of 300 values based on the
Pierson-Moskowics spectrum for C = 1000000 and b = 100000.
[28]. The two-dimensional Pierson-Moskowics spectrum can be calculated using |X(f)|
2
= (C/f
5
)
exp(-b/f
4
), where f = (k
2
+m
2
)
1/2
. Produce a two-dimensional smooth surface based on the
Pierson-Moskowics spectrum for C = 1000000 and b = 100000.
Download at InfoClearinghouse.com 60 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Download at InfoClearinghouse.com 61 2001 Gilberto E. Urroz
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.
Comparison of SCILAB Syntax and
Functions to MATLAB

By
Gilberto E. Urroz, Ph.D., P.E.
Distributed by
i nfoClearinghouse.com
2001 Gilberto E. Urroz
All Rights Reserved


A "zip" file containing all of the programs in this document (and other
SCILAB documents at InfoClearinghouse.com) can be downloaded at the
following site:

http://www.engineering.usu.edu/cee/faculty/gurro/Software_Calculators/Scil
ab_Docs/ScilabBookFunctions.zip


The author's SCILAB web page can be accessed at:

http://www.engineering.usu.edu/cee/faculty/gurro/Scilab.html


Please report any errors in this document to: gurro@cc.usu.edu


Download at InfoClearinghouse.com 1 2001 Gilberto E. Urroz
COMPARING SCILAB AND MATLAB

2
Functions 2
Comment lines 2
Variables 2
Strings 2
Boolean variables 2
Polynomials 3
Empty matrices 3
Plotting 3
Scicos 3
A table of Matlab and SCILAB equivalent functions 3
SCILAB functions that emulate Matlab

functions 4
Graphics functions with mtlb_ prefix 5
Function mtlb_mesh 7
Download at InfoClearinghouse.com 2 2001 Gilberto E. Urroz
Comparing SCILAB and Matlab

If the reader has previously used Matlab

, he or she would notice that the operation and, in


many instances, the syntax, of SCILAB commands are very similar to those of Matlab

. Some of
the information presented in this chapter is taken from the SCILAB web page:
http://www-rocq.inria.fr/scilab/
It is intended as a guideline highlighting the differences between SCILAB and Matlab

. The
differences are presented according to the subjects of functions, comment lines, strings,
Boolean variables, polynomials, operations on empty matrices, plotting, and SCICOS (SCILABs
system simulation software).
Functions
Functions in SCILAB are not considered as separate files, such as Matlab m-files, but as
variables in the SCILAB environment. One or several user-defined functions can be defined in
a single file, and the name of the file is not necessarily related to the name of the function(s).
Also, the function(s) are not automatically loaded into SCILAB, as they are in Matlab after
their name is invoked. Usually you have to execute the command getf(function_name)
before being able to use a function.
Functions can also be defined on-line (referred to as inside functions) by using the command
deff. Many examples of the use of getf and deff are provided throughout the book.
To execute a script file you must use exec("filename") in SCILAB, as in Matlab, you just
need to type the name of the file.
Comment lines
SCILAB comments begins with: //
Matlab comments begins with: %
Variables
Predefined variables usually have the % prefix in SCILAB (%i, %inf, ...). They are write
protected, i.e., they can not be redefined. Matlab predefines variables i and j as the unit
imaginary number. This predefinitions can wreck havoc in programming if you try to use i or j
as index variables. Such a problem does not exist in SCILAB.
Strings
Strings are considered as 1 by 1 matrices of strings in SCILAB. Each entry of a string matrix has
its own length.
Boolean variables
Boolean variables are %T, %F in SCILAB and 0, 1 in Matlab. They correspond to the Boolean
statements true and false, respectively. Indexing with Boolean variables may not produce
the same result in SCILAB as it does in Matlab . For example x=[1,2];x([1,1]) [which is NOT
x([%T,%T])] returns [1,1] in SCILAB and [1,2] in Matlab. Also if x is a matrix x(1:n,1)=[]
or x(:)=[] are not valid in Matlab.
Download at InfoClearinghouse.com 3 2001 Gilberto E. Urroz
Polynomials
Polynomials and polynomial matrices are defined by the function poly in SCILAB. They are
considered as vectors of coefficients in Matlab. For more details on SCILAB polynomials see
Chapter 8 in this book.
Empty matrices
[]+1 returns 1 in SCILAB and [] in Matlab.
Plotting
Except for the simple plot and mesh (Matlab) and plot3d(SCILAB) functions, SCILAB and
Matlab graph functions are not compatible. In a subsequent section we introduce some
SCILAB functions written specifically to emulate Matlab function.
Scicos
SCICOS (SCILAB) and Simulink (Matlab) are not compatible. SCICOS and Simulink are
graphically-based, system modeling software programs. To obtain more information about
SCICOS, visit SCILABs main web page (http://www-rocq.inria.fr/scilab/).
A table of Matlab and SCILAB equivalent functions
Most built in functions are identical in Matlab and SCILAB. Some of them have a slightly
different syntax. Here is a brief, partial list of commands with significantly different syntax.
__________________________________________
Matlab SCILAB .
all and
any or
balance balanc
clock unix('date')
computer unix_g('machine')
cputime timer
delete unix('rm file')
dir unix_g('ls')
echo mode
eig spec or bdiag
eval evstr
exist exists + type
fclose file('close')
feof
ferror
feval evstr and strcat
filter rtitr
finite (x < %inf)
fopen file('open')
fread read
fseek file
Download at InfoClearinghouse.com 4 2001 Gilberto E. Urroz
ftell
fwrite writeb
global
home
isglobal
isinf(a) a == %inf
isnan(a) a ~= a
isstr(a) type(a) == 10
keyboard pause + resume
lasterr
lookfor apropos
more lines
pack stacksize
pause halt
qz gspec+gschur
randn rand
rem modulo
setstr code2str
strcmp(a,b) a == b
uicontrol
uimenu getvalue
unix unix_g
version
which whereis
nargin [nargout,nargin]=argn(0)
nargout
__________________________________________________
SCILAB functions that emulate Matlab

functions
For those who have used Matlab

, and for those who want to learn how to use them, SCILAB
provides a number of functions that emulate Matlab

functions. These functions start with the


prefix mtlb_. A list of the Matlab

emulating functions follows:


mtlb mtlb_all mtlb_any mtlb_axes
mtlb_cell mtlb_choices mtlb_clf mtlb_cumsum
mtlb_diff mtlb_e mtlb_eval mtlb_exist
mtlb_eye mtlb_fft mtlb_filter mtlb_find
mtlb_findstr mtlb_fliplr mtlb_flipud mtlb_fprintf
mtlb_fread mtlb_fscanf mtlb_fwrite mtlb_get
mtlb_hold mtlb_i mtlb_ifft mtlb_is
mtlb_ishold mtlb_isreal mtlb_length mtlb_load
mtlb_loglog mtlb_max mtlb_mean mtlb_median
mtlb_mesh mtlb_meshdom mtlb_min mtlb_ones
mtlb_plot mtlb_prod mtlb_qz mtlb_rand
mtlb_save mtlb_semilogx mtlb_semilogy mtlb_sprintf
mtlb_sscanf mtlb_subplot mtlb_sum mtlb_zeros
Out of these functions we have used mtlb_diff to produce table of differences in the
polynomial approximations of Chapter 8 and for the first differences in time series of Chapter
18, and mtlb_subplot in function multiplot presented in Chapter 17 in relation to multiple
linear regression.
Download at InfoClearinghouse.com 5 2001 Gilberto E. Urroz
Graphics functions with mtlb_ prefix
Help for these functions is not available in SCILAB. My take on this is that SCILAB developers
try to encourage the use of the equivalent SCILAB functions. However, some mtlb_ functions,
particular those related to handling of graphs, may be easier to use than their SCILAB
counterparts. As an example, take functions mtlb_loglog, mtlb_semilogx, mtlb_semilogy, and
mtlb_subplot. Functions mtlb_semilogx and mtlb_semilogy are intended to produce plots
with logarithmic scales in x and y, respectively, while function loglog produces plots with two
logarithmic scales. Examples of using mtlb_loglog, mtlb_semilogx, and mtlb_semilogy, are
shown below. Function mtlb_subplot will be illustrated later.
-->x=[0.001 0.01 0.1 1.0 10. 100]; y=[20 30 40 50 60 70];
-->mtlb_semilogx(x,y)
-->xtitle('Logarithmic scale in the x axis','x','y')
-->x=[1:0.1:10];y=2*x^2;
-->mtlb_semilogy(x,y)
-->xtitle('Logarithmic scale in y','x','y')
Download at InfoClearinghouse.com 6 2001 Gilberto E. Urroz
-->x=[0.001,0.01,0.1,1.0,10.0,100.0];y=x^3;
-->mtlb_loglog(x,y)
-->xtitle('Log-log plot','x','y')
The function mtlb_subplot can be used to produce multiple plot frames in the same window.
The call to mtlb_subplot is
mtlb_subplot(m,n,j)
The effect of this function is to split the plot area in a window into a graphics matrix of m rows
and n columns, making the sub-area j available for plotting. The values of j range from 1 to p
= mn, with subplot j=1 corresponding to the upper left corner of the window, subplot j=2 being
the next subplot to the right, j=3 the next subplot to the right until reaching j=m. Subplot
j=m+1 is the first subplot in the second line, and so on. The position and numbering of the
subplots is shown in the next sketch.
Download at InfoClearinghouse.com 7 2001 Gilberto E. Urroz
Thus, the plot at location (i,k) is subplot number j = (i-1)m+k. To fill the window with plots
you need to call function mtlb_subplot a total of p times using fixed values of m and n and
varying j according to the position of the plot.
An example of application of function mtlb_subplot is provided next in the form of a SCILAB
script:
//Script to produce four plots in the same window
x=[0.0:0.1:1.0];y=x^2;z=sin(x)+sin(2*x);t=(1./(1+x))';r=abs(x-0.5);
mtlb_subplot(2,2,1);plot2d(x,y,-2);xtitle('Plot 1','x','y');
mtlb_subplot(2,2,2);plot2d(x,z,-1);xtitle('Plot 2','x','z');
mtlb_subplot(2,2,3);plot2d(x,t,-9);xtitle('Plot 3','x','t');
mtlb_subplot(2,2,4);plot2d(x,r,-5);xtitle('Plot 4','x','r');
The result of the script is shown next:
Function mtlb_mesh
Function mtlb_mesh can be used to produce a three-dimensional surface plot that emphasizes
the coordinate mesh in the final plot. The function produces a plot similar to that produced
with function plot3d.
-->deff([w]=f(x,y),w=sin(x)*cos(y))
--> x=[0:0.2:6];y=[0:0.2:6]; z = feval(x,y,f);
-->mtlb_mesh(x,y,z);
Download at InfoClearinghouse.com 8 2001 Gilberto E. Urroz
Note: Function mtlb_e does not have a Matlab

equivalent (at least not with the name e).


The function is intended to extract characters out of a string, for example:
--> a = tres tristes tigres
--> mtlb_e(a,[1:3])
ans = t
Download at InfoClearinghouse.com 9 2001 Gilberto E. Urroz
REFERENCES (for all SCILAB documents at InfoClearinghouse.com)
Abramowitz, M. and I.A. Stegun (editors), 1965,"Handbook of Mathematical Functions with Formulas, Graphs, and
Mathematical Tables," Dover Publications, Inc., New York.
Arora, J.S., 1985, "Introduction to Optimum Design," Class notes, The University of Iowa, Iowa City, Iowa.
Asian Institute of Technology, 1969, "Hydraulic Laboratory Manual," AIT - Bangkok, Thailand.
Berge, P., Y. Pomeau, and C. Vidal, 1984,"Order within chaos - Towards a deterministic approach to turbulence," John
Wiley & Sons, New York.
Bras, R.L. and I. Rodriguez-Iturbe, 1985,"Random Functions and Hydrology," Addison-Wesley Publishing Company,
Reading, Massachussetts.
Brogan, W.L., 1974,"Modern Control Theory," QPI series, Quantum Publisher Incorporated, New York.
Browne, M., 1999, "Schaum's Outline of Theory and Problems of Physics for Engineering and Science," Schaum's
outlines, McGraw-Hill, New York.
Farlow, Stanley J., 1982, "Partial Differential Equations for Scientists and Engineers," Dover Publications Inc., New
York.
Friedman, B., 1956 (reissued 1990), "Principles and Techniques of Applied Mathematics," Dover Publications Inc., New
York.
Gomez, C. (editor), 1999, Engineering and Scientific Computing with Scilab, Birkhuser, Boston.
Gullberg, J., 1997, "Mathematics - From the Birth of Numbers," W. W. Norton & Company, New York.
Harman, T.L., J. Dabney, and N. Richert, 2000, "Advanced Engineering Mathematics with MATLAB - Second edition,"
Brooks/Cole - Thompson Learning, Australia.
Harris, J.W., and H. Stocker, 1998, "Handbook of Mathematics and Computational Science," Springer, New York.
Hsu, H.P., 1984, "Applied Fourier Analysis," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego.
Journel, A.G., 1989, "Fundamentals of Geostatistics in Five Lessons," Short Course Presented at the 28th International
Geological Congress, Washington, D.C., American Geophysical Union, Washington, D.C.
Julien, P.Y., 1998,Erosion and Sedimentation, Cambridge University Press, Cambridge CB2 2RU, U.K.
Keener, J.P., 1988, "Principles of Applied Mathematics - Transformation and Approximation," Addison-Wesley
Publishing Company, Redwood City, California.
Kitanidis, P.K., 1997,Introduction to Geostatistics - Applications in Hydogeology, Cambridge University Press,
Cambridge CB2 2RU, U.K.
Koch, G.S., Jr., and R. F. Link, 1971, "Statistical Analysis of Geological Data - Volumes I and II," Dover Publications,
Inc., New York.
Korn, G.A. and T.M. Korn, 1968, "Mathematical Handbook for Scientists and Engineers," Dover Publications, Inc., New
York.
Kottegoda, N. T., and R. Rosso, 1997, "Probability, Statistics, and Reliability for Civil and Environmental Engineers,"
The Mc-Graw Hill Companies, Inc., New York.
Kreysig, E., 1983, "Advanced Engineering Mathematics - Fifth Edition," John Wiley & Sons, New York.
Lindfield, G. and J. Penny, 2000, "Numerical Methods Using Matlab," Prentice Hall, Upper Saddle River, New Jersey.
Magrab, E.B., S. Azarm, B. Balachandran, J. Duncan, K. Herold, and G. Walsh, 2000, "An Engineer's Guide to
MATLAB", Prentice Hall, Upper Saddle River, N.J., U.S.A.
McCuen, R.H., 1989,Hydrologic Analysis and Design - second edition, Prentice Hall, Upper Saddle River, New Jersey.
Middleton, G.V., 2000, "Data Analysis in the Earth Sciences Using Matlab," Prentice Hall, Upper Saddle River, New
Jersey.
Download at InfoClearinghouse.com 10 2001 Gilberto E. Urroz
Montgomery, D.C., G.C. Runger, and N.F. Hubele, 1998, "Engineering Statistics," John Wiley & Sons, Inc.
Newland, D.E., 1993, "An Introduction to Random Vibrations, Spectral & Wavelet Analysis - Third Edition," Longman
Scientific and Technical, New York.
Nicols, G., 1995, Introduction to Nonlinear Science, Cambridge University Press, Cambridge CB2 2RU, U.K.
Parker, T.S. and L.O. Chua, , "Practical Numerical Algorithms for Chaotic Systems, 1989, Springer-Verlag, New York.
Peitgen, H-O. and D. Saupe (editors), 1988, "The Science of Fractal Images," Springer-Verlag, New York.
Peitgen, H-O., H. Jrgens, and D. Saupe, 1992, "Chaos and Fractals - New Frontiers of Science," Springer-Verlag, New
York.
Press, W.H., B.P. Flannery, S.A. Teukolsky, and W.T. Vetterling, 1989, Numerical Recipes - The Art of Scientific
Computing (FORTRAN version), Cambridge University Press, Cambridge CB2 2RU, U.K.
Raghunath, H.M., 1985, "Hydrology - Principles, Analysis and Design," Wiley Eastern Limited, New Delhi, India.
Recktenwald, G., 2000, "Numerical Methods with Matlab - Implementation and Application," Prentice Hall, Upper
Saddle River, N.J., U.S.A.
Rothenberg, R.I., 1991, "Probability and Statistics," Harcourt Brace Jovanovich College Outline Series, Harcourt Brace
Jovanovich, Publishers, San Diego, CA.
Sagan, H., 1961,"Boundary and Eigenvalue Problems in Mathematical Physics," Dover Publications, Inc., New York.
Spanos, A., 1999,"Probability Theory and Statistical Inference - Econometric Modeling with Observational Data,"
Cambridge University Press, Cambridge CB2 2RU, U.K.
Spiegel, M. R., 1971 (second printing, 1999), "Schaum's Outline of Theory and Problems of Advanced Mathematics for
Engineers and Scientists," Schaum's Outline Series, McGraw-Hill, New York.
Tanis, E.A., 1987, "Statistics II - Estimation and Tests of Hypotheses," Harcourt Brace Jovanovich College Outline
Series, Harcourt Brace Jovanovich, Publishers, Fort Worth, TX.
Tinker, M. and R. Lambourne, 2000, "Further Mathematics for the Physical Sciences," John Wiley & Sons, LTD.,
Chichester, U.K.
Tolstov, G.P., 1962, "Fourier Series," (Translated from the Russian by R. A. Silverman), Dover Publications, New York.
Tveito, A. and R. Winther, 1998, "Introduction to Partial Differential Equations - A Computational Approach," Texts in
Applied Mathematics 29, Springer, New York.
Urroz, G., 2000, "Science and Engineering Mathematics with the HP 49 G - Volumes I & II", www.greatunpublished.com,
Charleston, S.C.
Urroz, G., 2001, "Applied Engineering Mathematics with Maple", www.greatunpublished.com, Charleston, S.C.
Winnick, J., , "Chemical Engineering Thermodynamics - An Introduction to Thermodynamics for Undergraduate
Engineering Students," John Wiley & Sons, Inc., New York.

Potrebbero piacerti anche