01 Introduction PDF
01 Introduction PDF
01 Introduction PDF
Introduction
1 Course goals
Most of your engineering courses teach you theory and analytic methods using the techniques of
mathematics (calculus, linear algebra, differential equations, and so on). The purpose is to train
you to understand significant engineering problems from a fundamental theoretical perspective.
You need this foundation to be able to design effective solutions to difficult problems – it's
practically impossible to solve a problem you don't thoroughly understand.
Unfortunately, the set of engineering problems that can be solved analytically (with pencil, paper
and even a calculator) is limited to relatively simple and idealized cases. Indeed, many “textbook
problems” are carefully designed so that you can end up with the “exact answer.” However, most
applications in your engineering career will involve more complex systems for which you can
mathematically formulate the problem (if you've learned your theory) but cannot solve the
resulting equations analytically. People have long realized that in these cases numerical solutions
are possible. Yet before the development of digital computers, when calculations had to be done
by hand or slide rule, the application of “numerical computing” was fairly limited. As a result in
the past engineers were forced to make many simplifying assumptions, build scale models, use
trial and error and so on.
In the last several decades the development of computer technology has revolutionized
engineering. Almost any problem that can be formulated mathematically can be solved using
numerical techniques. Systems as complex as the supersonic flow of air through a jet engine or
the operation of an integrated circuit containing millions of transistors are now routinely
analyzed in minute detail using numerical methods implemented on computers.
This is the motivation behind “EE 221 Numerical Computing for Engineers.” The purpose of this
course is for you to:
1. learn fundamental numerical methods so you can understand how to formulate numerical
solutions to difficult engineering problems, and
2. develop programing skills using Scilab/Matlab so you are able to effectively implement
your numerical solutions.
The beginning of the course will be devoted to basic programing techniques, control structures,
input/output, graphics and the like. For the remainder of the course we will use those skills to
learn and implement numerical methods for important classes of problems.
2 Course topics
The main topics we will cover are listed below. Additional topics and programming project
examples will be covered as time permits.
Scilab/Matlab Basics
• Introduction
• Arrays
• Programming structures
• Input and output
• 2D plots
• 3D plots and animation
Numerical methods
• Root finding
• Polynomials
• Linear algebra
• Linear systems
• Nonlinear systems
• Interpolation
• Optimization
• Curve fitting
• Numerical calculus
• Random numbers
• Sparse systems
4.1 History
(From http://en.wikipedia.org/wiki/Matlab)
Short for "matrix laboratory", MATLAB was invented in the late 1970s by Cleve Moler,
then chairman of the computer science department at the University of New Mexico. He
designed it to give his students access to LINPACK and EISPACK without having to
learn Fortran. It soon spread to other universities and found a strong audience within the
applied mathematics community. Jack Little, an engineer, was exposed to it during a visit
Moler made to Stanford University in 1983. Recognizing its commercial potential, he
joined with Moler and Steve Bangert. They rewrote MATLAB in C and founded The
MathWorks in 1984 to continue its development.
(From http://en.wikipedia.org/wiki/Scilab)
Scilab was created in 1990 by researchers from INRIA and École nationale des ponts et
chaussées (ENPC). It was initially named Ψlab[12] (Psilab). The Scilab Consortium was
formed in May 2003 to broaden contributions and promote Scilab as worldwide reference
software in academia and industry.[13] In July 2008, in order to improve the technology
transfer, the Scilab Consortium joined the Digiteo Foundation. […] Since July 2012,
Scilab is developed and published by Scilab Enterprises.
There are other free/open-source Matlab alternatives in addition to Scilab. One of the most
widely used is GNU Octave. In some ways Octave is arguably even more Matlab compatible than
Scilab. However, it's primarily written for Linux systems as is not as “clean” to install on
Windows PCs or Macs. The Python programing language has been gaining ground in the
numerical computing community, but differs significantly from Matlab. For these reasons I prefer
Scilab as the “best” free/open-source Matlab alternative.
There should also be a Scilab entry in the Start Menu. When you start Scilab you should see a
window something like that shown below.
In the middle is the “console” where we will do most of our work. The other windows can X'd
out for now. We can open them later if need be. This leaves just the console.
Type “help” at the prompt, or use the “?” menu to open the help browser. From there you can
search or browse the documentation. This is a good way to find out about all the capabilities of
Scilab. In this class we will use only a small subset of these.
6 Interactive use
There are two basic ways to use Scilab/Matlab. As an interactive environment you can type a
command directly into the console at the prompt and Scilab will print the results. Then type
another command and so on. This is how we will start out using Scilab, as essentially a fancy
calculator. Later we'll learn how to use the editor to write structured programs.
6.1 Arithmetic
From the command line you can enter arithmetic expressions involving the addition, subtraction,
multiplication and division operators (+ - * /). For example
-->10+6/3-4*2
ans =
4.
The ans variable stores the result of the last calculation and can be used in the next calculation.
Here is the same series of calculations performed one step at a time.
-->10
ans =
10.
-->ans+6/3
ans =
12.
-->ans-4*2
ans =
4.
Operator precedence rules apply. Operations are performed from left to right. Multiplication and
division are done before addition and subtraction. These can be overridden with parentheses. For
example
-->3+2/4
ans =
3.5
first divides 2 by 4 and then adds 3 to get 3.5; the + operator appears first (from left to right) but
multiplication has precedence over addition. However
-->(3+2)/4
ans =
1.25
Here the parenthesis tell Scilab to perform the addition operation first 3+2=5, followed by
division 5/4=1.25. I find it good practice in all but the simplest cases to use parentheses to
make the order of operations explicit. It is perfectly fine to use redundant parenthesis if this aids
the readability of an expression. For example the parenthesis in the expression
-->3+(2/4)
ans =
3.5
have no effect, but you might find they make the order of operations explicit.
A very useful feature of Scilab/Matlab is that by pressing the up or down arrow keys (Ctrl-P and
Ctrl-N) on your keyboard, you can cycle through the command line history. This allows you to
easily repeat previous commands. The commands can also be edited using the arrow and
Backspace keys.
6.2 functions
Scilab/Matlab also has many built-in functions. The help menu provides complete listings. For
example, the trig functions sin, cos, tan as well as their inverses asin, acos, and
atan are available. You use parentheses to denote the argument.
-->sin(0.5)
ans =
0.4794255
-->asin(ans)
ans =
0.5
In Scilab the atan function is an “overloaded function” which allows you to provide different
numbers of arguments. The expression atan(z) returns the angle between -π/2 and π/2 for
which the tan function is z. It is a “two-quadrant” inverse tangent. The expression atan(y,x)
returns the angle between -π and π for which the tan function is y/x and the angle corresponds to
the polar angle of the rectangular coordinates point (x,y). In Matlab the same functionality is
provided by atan2(y,x).
The standard trig functions operate in radians. Thus cos(1) is the cosine of 1 radian. Since it is
often useful to work in degrees, Scilab provides the functions cosd, sind, atand and so
on where the appended “d” indicates degrees. So
-->atan(1)
ans =
0.7853982
-->atand(1)
ans =
45.
-->log(ans)
ans =
3.
In many engineering books log x is used to refer to the base 10 logarithm. In Scilab/Matlab the
base 10 logarithm is denoted by log10(x).
-->log10(2)
ans =
0.30103
-->10^ans
ans =
2.
This distinction has caused problems for many an engineering student because we are so used to
using log(x) to denote the base-10 logarithm. You should pay particular attention to this.
6.3 variables
You can define variables and then operate on those. For example,
-->x = 2
x =
2.
-->x^2+3*x+7
ans =
17.
Typing in the variable name displays the variable value. If no such variable exists you'll get an
error message.
-->x
x =
2.
-->y
!--error 4
undefined variable : y
6.4 strings
Variables are typically numerical in nature. However, variables can be assigned text values using
quotation marks, as in the following examples.
-->a = 'this is a string'
a =
this is a string
Note that in Scilab you can also use double quotes as in “this is a string” but not in
Matlab. Therefore if you use single quotes in Scilab your code will be more Matlab compatible.
You can concatenate two strings with the + operator in Scilab. In Matlab you can form an array or
use the strcat function.
-->a+b //a+b in Scilab, in Matlab [a,b] or strcat(a,b)
ans =
this is a string and here is some more text
String are particularly useful for labeling and describing numerical output.
-->%pi
%pi =
3.1415927
The % prefix “protects” the constant and keeps you from redefining it.
-->%pi = 3
!--error 13
redefining permanent variable
Many people find this notation messy but it has a very real advantage (see next section). If you
don't like this you can simply create a regular variable with the same value, as in
-->pi = %pi
pi =
3.1415927
This is a weakness of Matlab, in my opinion, because you can accidentally redefine these, as in
>> pi = 3
pi =
3
If you later use the variable pi thinking it's value is π you'll end up with erroneous results.
2. + 3.i
Note that you need to explicitly include the multiplication operator *. You cannot just juxtapose
two variables to denote multiplication. If you don't like the % sign notation, you can define a
variable to equal %i and use that instead. For example,
-->j = %i
j =
i
-->z = 1+2*j
z =
1. + 2.i
In Matlab the imaginary unit is the predefined variable i (also j). Again this is dangerous because
you can redefine this variable accidentally. In fact i and j are very commonly used as index
variables in for loops.
>> i
ans =
0 + 1.0000i
>> i = 2
i =
2
To avoid this problem Matlab defines 1i as a protected variable equal to the imaginary unit
>> 2+3*1i
ans =
2.0000 + 3.0000i
In Matlab you can leave out the * operator as in
-->3+2i
ans =
3.0000 + 2.0000i
which is a nice feature that Scilab does not have. Scilab/Matlab functions can operate on and
return complex numbers.
-->exp(%i*3)
ans =
- 0.9899925 + 0.1411200i
-->y = 3;
-->z = tan(y/x);
-->w = sqrt(z)
w =
3.7551857
-->format('e');
-->10*%pi
ans =
3.142D+01
The “D+01” notation represents the exponent of 10 in scientific notation. “D” stands for “double
precision” (more on that later). So, the last line above indicates a value 3.1412⋅101 . You can also
control the number of digits that Scilab outputs using an optional second argument, as in
-->format('v',5);
-->%pi
%pi =
3.14
-->format('v',10);
-->%pi
%pi =
3.1415927
1.
in file part1.txt
-->x = 3
x =
3.
-->sqrt(3)
ans =
1.7320508
-->diary(0)
in file part1.txt
-->x = 3
x =
3.
-->sqrt(3)
ans =
1.7320508
-->diary(0)
that is, everything in the interactive session after the diary command. The diary command also
works in Matlab. The only difference is that the file closed with the command diary('off')
rather than diary(0).
8 Numerical limitations
If
x=(1+10 20 ) 1
then what is x? Obviously x=10 20 . Let's verify this with Scilab
-->x = (1+1e-20)-1
x =
0.
Scilab says x=0 , which is wrong. Why? Is 10 20 too small for Scilab to represent?
-->x = 1e-20
x =
1.000D-20
Clearly that's not the problem. Instead we are seeing a round-off error. Scilab, like your
calculator, is limited in the number of digits it can use to represent a number.
Suppose we represent numbers in scientific notation as
x=±d 1 . d 2 d 3 d 4⋅10±e e1 2
-->delta = 1e-3;
-->(exp(delta)-exp(0))/delta
ans =
1.000500166708385
-->delta = 1e-6;
-->(exp(delta)-exp(0))/delta
ans =
1.000000499962184
-->delta = 1e-9;
-->(exp(delta)-exp(0))/delta
ans =
1.000000082740371
-->delta = 1e-12;
-->(exp(delta)-exp(0))/delta
ans =
1.000088900582341
-->delta = 1e-15;
-->(exp(delta)-exp(0))/delta
ans =
1.110223024625157
Notice as we decrease δ , initially the approximation to the derivative improves. For δ=10 9
we get
df
≈1.000000082740371
dx
which is accurate to about 8 digits. But further reduction of δ actually leads to worse accuracy.
For δ=10 15 we have
df
≈1.110223024625157
dx
which is not even accurate to 2 digits! In fact going to δ=10 20 results in
-->delta = 1e-20;
-->(exp(delta)-exp(0))/delta
ans =
0.
which is completely wrong! The lesson is that we need to consider numerical limitations very
carefully when we develop and implement numerical algorithms.
9 References
Numerical methods books – used as fundamental references throughout these notes
1. Press, W. H., Flannery, B. P., Teukolsky, S. A. and Vetterling, W. T., Numerical Recipes in
C, Cambridge, 1988, ISBN: 0-521-35465-X.
2. Recktenwald, G., Numerical Methods with Matlab: Implementation and Application,
Prentice Hall, 2000, ISBN: 0-201-30860-6.
3. Heath, M. T., Scientific Computing: An Introductory Survey, McGraw Hill, 2002, ISBN:
0-07-239910-4.
4. Urroz, G. E., Numerical and Statistical Methods with SCILAB for Science and
Engineering Vol. 1, BookSurge Publishing, 2001, ISBN-13: 978-1588983046.
5. http://www.mathworks.com/moler/
Software sites
1. http://www.scilab.org, official Scilab website
2. http://www.mathworks.com, official MatLab website
3. https://www.gnu.org/software/octave/, official GNU Octave website
4. https://www.python.org/, official Python website
5. http://www.gnu.org/software/gsl/, GNU Scientific Library
Wikipedia articles
1. http://en.wikipedia.org/wiki/Scilab
2. http://en.wikipedia.org/wiki/Matlab
3. http://en.wikipedia.org/wiki/GNU_Octave
4. http://en.wikipedia.org/wiki/Python_(programming_language)