R. Verf Urth
R. Verf Urth
R. VERFÜRTH
Contents
1. Introduction 1
2. Installing Scilab 2
3. Installing Numerics 2
4. Using Numerics 2
4.1. Data structures 2
4.2. Structure of Numerics functions 4
4.3. Start 5
4.4. Help 5
4.5. Interpolation 5
4.6. Numerical integration 8
4.7. Nonlinear equations 8
4.8. Iterative solvers for linear systems of equations 9
4.9. Eigenvalue problems 11
4.10. Initial value problems for odes: fixed step-size methods 12
4.11. Initial value problems for odes: variable step-size
methods 14
4.12. Boundary value problems for odes 17
4.13. Finite difference methods for pdes 17
4.14. Linear optimization 19
4.15. Discrete optimization 20
4.16. Nonlinear optimization 23
References 25
Index 27
1. Introduction
Numerics is a library of Scilab functions implementing many of the
algorithms presented in our courses on numerical analysis for math-
ematicians and engineers [5, 6, 7, 8, 9, 10]. It covers the following
areas:
• interpolation,
• numerical integration,
• nonlinear equations,
Date: January 5, 2012.
1
2 R. VERFÜRTH
2. Installing Scilab
Scilab is a numerical programming environment comparable to
Matlab with a similar syntax. It was developed by INRIA, France and is
currently maintained by the Digiteo-foundation in collaboration with
INRIA. Contrary to Matlab, Scilab is completely free of charge. There
are versions for the operating systems Mac OsX, Unix and Windows. To
use Numerics you first have to get the appropriate version of Scilab
from
http://www.scilab.org/
and install it on your computer following the instructions of that web-
site. Useful introductions to Scilab are e.g. [1, 2]; [4] may be of par-
ticular interest for those familiar with Matlab.
3. Installing Numerics
To install Numerics click the link Numerics.zip on
http://www.rub.de/num1/softwareE.html
or
http://www.rub.de/num1/software.html.
This installs a zip-archive Numerics.zip on your computer. Unpack
the zip-archive and put the created folder Numerics at any place on
your computer that suits you. Now you can start Numerics as described
in section 4.3.
4. Using Numerics
4.1. Data structures. We refer to [1, 2, 4] for a detailed introduction
to Scilab. Here, we briefly present those data structures of Scilab
that are used for input arguments of Numerics functions.
Boolean: Boolean arguments are transferred to a Numerics function
by either directly entering %t for the value true or %f for the value
false or by creating an own boolean variable myboolean by typing
myboolean = %t or myboolean = %f in the Scilab console and then
NUMERICS USER GUIDE 3
in 10 equidistant points on the interval [−1, 1]. Then you first have to
create the corresponding function by entering
function y = myfunction(x)
y = x/(2 + x ∗ x)
endfunction
in the Scilab console. Then you may start the interpolation by typing
(without the full stop at the end!)
lagrange(myfunction,10).
lagrange(exp,10)
lagrange(exp,10,-2)
lagrange(exp,10,[ ],2)
lagrange(exp,10,-4,3)
yield the Lagrange interpolation of the built-in function exp in 10
equidistant points on the intervals [−1, 1], [−2, 1], [−1, 2] and [−4, 3],
respectively.
4.3. Start. Before using any Numerics function you must make it
known to Scilab by first entering
mylib = lib(path)
to the console. Here, mylib may be any other name different from
the name of any Numerics function, any built-in function and any of
your user-defined functions. path is a string giving the full path to the
Numerics library. Suppose for example that you are using Mac OsX,
that Gargantua is your user name and that you have put the Numerics
folder into the subfolder Pantagruel of your documents folder. Then
the above command must read (without the full stop at the end!)
mylib =
lib("/users/Gargantua/documents/Pantagruel/Numerics").
If everything is correctly installed, Scilab returns the names of all
functions in the Numerics library. This includes auxiliary functions
which are not user-relevant and which will not be described below.
4.4. Help. Numerics comes with a help function numerics help which
may be used with any number of arguments including an empty ar-
gument list. Typing numerics help() yields a list of all user-relevant
functions for which help is available. When specifying arguments, these
must be strings with the names of Numerics functions. Thus typing
numerics help("newton","secant rule","regula falsi")
provides information on the functions newton, secant rule and
regula falsi.
4.5. Interpolation. Numerics provides the functions
neville, lagrange, hermite, cubic spline, goertzel
for interpolation. These functions can be used in two different ways
which are determined by the type of their arguments:
• interpolation of a function,
• interpolation of discrete data.
The function neville realizes interpolation by Neville’s scheme (see
[5, Algorithmus I.2.7]). When interpolating a given function the user
must provide the following arguments the first three being mandatory:
y: scalar, vector or matrix of points in which the interpolation
polynomial should be evaluated,
f: function that should be interpolated,
6 R. VERFÜRTH
df: derivative Df of f ,
pr: if %t results are printed in each iteration, default is %f,
nod: if %t damping is switched off, default is %f i.e. damping is
performed, q
tol: error tolerance ε, iteration is stopped once n1 f (x) · f (x) ≤
ε, default is 1.0D−8,
nmax: maximal number of iterations, default is 10.
The function
q newton prints the required number of iterations, the final
residual n1 f (x) · f (x) and the approximate solution x.
The function secant rule realizes the secant rule for scalar nonlinear
equations f (x) = 0 in R (see [5, Algorithmus III.3.1], [8, Algorithmus
II.3]). It has the following arguments the first three being mandatory:
x1: first initial value x0 ,
x2: second initial value x1 ,
f: function f ,
pr: if %t results are printed in each iteration, default is %f,
tol: error tolerance ε, iteration is stopped once |f (x)| ≤ ε, default
is 1.0D−8,
nmax: maximal number of iterations, default is 10.
The function secant rule prints the required number of iterations, the
final residual |f (x)| and the approximate solution x.
The function regula falsi implements the regula falsi for scalar non-
linear equations f (x) = 0 in R (see [5, Algorithmus III.3.2]). It has the
same arguments as secant rule and produces the same output but
the function values f (x0 ) and f (x1 ) of the two initial values must now
be of different sign.
The functions cg and pcg ssor realize the conjugate gradient algorithm
(see [5, Algorithmus IV.7.7], [9, Algorithmus VI.4.1], [10, §III.3.2])
and the preconditioned conjugate gradient algorithm with SSOR-pre-
conditioning (see [5, Algorithmen IV.7.10, IV.7.14], [9, Algorithmen
VI.4.2, VI.4.3], [10, §§III.3.3, III.3.4]). cg has the same arguments as
richardson except the missing relaxation parameter om. pcg ssor has
the same arguments as richardson with the default value 1.5 for the
relaxation parameter om. Notice that for both algorithms the matrix
A must by symmetric positive definite.
The functions bicg stab and pbicg stab ssor implement the stabi-
lized bi-conjugate gradient algorithm (see [9, Algorithmus VI.7.1], [10,
Algorithm III.5.1]) and the preconditioned stabilized bi-conjugate gra-
dient algorithm with SSOR-preconditioning. They have the same ar-
guments as their ‘symmetric’ counterparts cg and pcg ssor up to an
additional optional final argument nmaxrestart which limits the max-
imal number of restarts with the default value being 10.
The function compare solvers compares various direct and iterative
solvers for the solution of the linear system of equations arising from
the five-point difference discretization of the Poisson equation with ho-
mogeneous Dirichlet boundary conditions on the unit square (see [6,
§III.3], [9, §III.1]). It has the following arguments none being manda-
tory:
methods: a vector of strings specifying the chosen methods,
available methods are:
LU: sparse LU-factorization,
CG: the conjugate gradient algorithm,
PCG: the preconditioned conjugate gradient algorithm with
ssor preconditioning,
GS: gauss-seidel iteration,
NUMERICS USER GUIDE 11
(see [6, Algorithmus I.2.2], [8, §IV.1], [10, §I.2.2]). It has the following
arguments the first five being mandatory:
f: force function f ,
df: derivative Dx f w.r.t. x of the force function f ,
x0: initial value x0 ,
T: final time T ,
−t0
nt: number of time steps, the step-size is h = T nt ,
t0: initial time t0 , default is t0 = 0,
pc: integer vector of length 2 fixing the components of x that will
be plotted, default settings are:
[0,1]: for scalar odes, i.e. plot x(t) versus t,
[1,2]: for systems of odes, i.e. plot the curve (x1 (t), x2 (t)).
Note that the user-defined functions f and df must have the two argu-
ments t and x (in this order!).
The function crank nicolson fs implements the Crank-Nicolson me-
thod (see [6, Algorithmus I.2.3], [8, §IV.1], [10, §I.2.2]). It has the same
arguments as implicit euler fs.
The function rk fs implements the classical Runge-Kutta scheme (see
[6, §I.2], [8, §IV.6.4], [10, §I.2.3]). It has the same arguments as the
function explicit euler fs.
The function rkf fs implements Runge-Kutta-Fehlberg schemes of or-
der 2 with 3 stages, of order 3 with 4 stages and of order 4 with 6 stages
(see [6, Beispiel I.4.7], [8, Beispiel VI.7]). It has the same arguments as
the function explicit euler fs except that pc now is the seventh ar-
gument and that a new sixth argument order determines the method.
This argument may take the values 2, 3 and 4 with 3 being its default
value.
The function sdirk fs implements a strongly diagonally implicit
Runge-Kutta scheme of order 4 (see [8, §VI.4], [10, §I.2.4]). It has
the same arguments as implicit euler fs.
The function nystroem fs implements the Nyström two-step method
(see [6, Beispiel I.5.1]). The first two approximations are computed
with the explicit Euler scheme. The function nystroem fs has the
same arguments as explicit euler fs.
The function adams bashforth fs implements the Adams-Bashforth
two- and three-step schemes (see [6, Beispiel I.5.1]). The first two or
three, resp. steps are computed with the explicit Euler scheme. The
function adams bashforth fs has the same arguments as the function
explicit euler fs except that pc now is the seventh argument and
that a new sixth argument steps determines the method. This argu-
ment may take the values 2 or 3 with 2 being its default value.
The function adams moulton fs implements the Adams-Moulton two-
and three-step schemes (see [6, Beispiel I.5.1]). The first two or three,
resp. steps are computed with the implicit Euler scheme. The function
14 R. VERFÜRTH
with variable step-size methods. The function rkf vs controls the step-
size by comparing methods of different order (see [6, Algorithmus I.4.6],
[8, Algorithmus VI.5]); the other functions perform the step-size con-
trol by comparing results for different step-sizes (see [6, Algorithmus
I.4.3], [8, Algorithmus VI.6]). All functions plot the computed solution
according to their argument pc.
The function explicit euler vs implements the explicit Euler scheme
(see [6, Algorithmus I.2.1], [8, §IV.1], [10, §I.2.2]). It has the following
arguments the first four being mandatory:
f: force function f ,
x0: initial value x0 ,
T: final time T ,
dt: initial step-size,
t0: initial time t0 , default is t0 = 0,
tol: required tolerance ε,
the function strives at obtaining an approximation η(t) for the
solution x(t) such that kη(t) − x(t)k ≤ ε kx(t)k holds for all t,
default value is ε = 0.001,
ntmax: maximal number of time steps, default is 10000,
sfdt: safety factor for step-size variation,
1
reduce the step-size at most by the factor sfdt and increase it
at most by the factor sfdt,
default value is 10,
maxr: maximal number of step-size reductions in a single time
step, default is 10,
pc: integer vector of length 2 fixing the components of x that will
be plotted, default settings are:
[0,1]: for scalar odes, i.e. plot x(t) versus t,
[1,2]: for systems of odes, i.e. plot the curve (x1 (t), x2 (t)).
Note that the user-defined function f must have the two arguments t
and x (in this order!).
The function implicit euler vs implements the implicit Euler scheme
(see [6, Algorithmus I.2.2], [8, §IV.1], [10, §I.2.2]). It has the following
arguments the first five being mandatory:
f: force function f ,
df: derivative Dx f w.r.t. x of the force function f ,
x0: initial value x0 ,
T: final time T ,
dt: initial step-size,
t0: initial time t0 , default is t0 = 0,
tol: required tolerance ε,
the function strives at obtaining an approximation η(t) for the
solution x(t) such that kη(t) − x(t)k ≤ ε kx(t)k holds for all t,
default value is ε = 0.001,
16 R. VERFÜRTH
Note that the user-defined functions f and df must have the two argu-
ments t and x (in this order!).
The function crank nicolson vs implements the Crank-Nicolson me-
thod (see [6, Algorithmus I.2.3], [8, §IV.1], [10, §I.2.2]). It has the same
arguments as implicit euler vs.
The function rk vs implements the classical Runge-Kutta scheme (see
[6, §I.2], [8, §IV.6.4], [10, §I.2.3]). It has the same arguments as the
function explicit euler vs.
The function rkf vs implements Runge-Kutta-Fehlberg schemes of or-
der 2 with 3 stages, of order 3 with 4 stages and of order 4 with 6 stages
(see [6, Beispiel I.4.7], [8, Beispiel VI.7]). It has the same arguments as
the function explicit euler vs except that pc now is the eleventh ar-
gument and that a new tenth argument order determines the method.
This argument may take the values 2, 3 and 4 with 3 being its default
value.
The function sdirk vs implements a strongly diagonally implicit
Runge-Kutta scheme of order 4 (see [8, §VI.4], [10, §I.2.4]). It has
the same arguments as implicit euler vs.
The function ode vs allows a comparison of the above methods. Its
first argument is an array sm of strings which identify the methods that
should be compared and which may take the following values:
The remaining arguments of ode vs are the same as for the function
implicit euler vs. Note that the argument df is also present for
NUMERICS USER GUIDE 17
The function dual simplex implements the dual simplex method (see
[7, Algorithmus I.5.5]). It has the following arguments the first three
being mandatory:
clp: cost vector ct ∈ Rn (clp should be a row-vector!),
Alp: constraint matrix A ∈ Rm×n ,
blp: constraint vector b ∈ Rm (blp should be a column-vector!)
Jlp: first basis J, default setting is J = {n − m + 1, . . . , n}.
prl: print level determines the print output, default is 2
(0) no print output,
(1) print optimal value ct x at the end,
(2) print optimal value ct x and vector x at the end,
(3) print value ct x at every iteration,
(4) print value ct x and vector x at every iteration,
(5) print value ct x, vector x and simplex tableau at every iter-
ation.
The function inner point implements an inner point method for lin-
ear optimization (see [7, Algorithmus I.7.7]). It has the following ar-
guments the first three being mandatory:
clp: cost vector ct ∈ Rn (clp should be a row-vector!),
Alp: constraint matrix A ∈ Rm×n ,
blp: constraint vector b ∈ Rm (blp should be a column-vector!)
xys: initial column-vector of the form [x; y; s] satisfying
Ax = b, At y + s = c, x > 0, s > 0,
default setting is xys = [ ], inner point then tries to automat-
ically determine vectors x, y and s with the above properties,
prl: print level determines the print output, default is 2
(0) no print output,
(1) print optimal value ct x at the end,
(2) print optimal value ct x and vector x at the end,
(3) print value ct x at every iteration,
(4) print value ct x and vector x at every iteration,
exact: : default is %f,
if %t, inner point tries to compute the exact solution of the
optimization problem by guessing an admissible basis from the
computed approximate solution and starting a simplex algo-
rithm with this basis,
itm: maximal number of iterations, default is 1000,
tol: tolerance ε, default is ε = 0.001,
the iteration is stopped if the relative change of two consecu-
tive x-vectors in the maximum norm is less than ε or if the
parameter µ of inner-point method is less than ε.
4.15. Discrete optimization. Numerics provides the functions
branch and bound, cutting planes,
NUMERICS USER GUIDE 21
a network. It prints results according to its argument prl and has the
following arguments the first three being mandatory:
Adjc: one of the following two objects:
• a square matrix such that Adjc[i,j] gives the capacity of
the arc connecting nodes i and j of the graph and equals
-%inf if nodes i and j are not connected by an arc or if
i = j,
• a list of vectors [a,b,c] describing the properties of the
arcs such that c is the capacity of the arc connecting nodes
a and b,
Adjp: one of the following two objects:
• a square matrix such that Adjp[i,j] gives the cost of the
arc connecting nodes i and j of the graph and equals %inf
if nodes i and j are not connected by an arc or if i = j,
• a list of vectors [a,b,c] describing the properties of the
arcs such that c is the cost of the arc connecting nodes a
and b,
val: requested value Φ of the flow,
first: number of starting node s, default is 1,
last: number of terminal node t, default is size(Adjc,"c"),
prl: print level, default is 2,
(0) no print output,
(1) print the value of the final flow x,
(2) print the value of the final flow x and the corresponding
matrix where the element [i,j] gives the flow through the
arc connecting nodes i and j,
(3) print in every iteration the value of the current flow x,
(4) print in every iteration the value of the current flow x and
the corresponding matrix where the element [i,j] gives
the flow through the arc connecting nodes i and j.
References
[1] Michaël Baudin, Introduction to Scilab.
http://wiki.scilab.org/Tutorials
[2] , Programming in Scilab.
http://wiki.scilab.org/Tutorials
[3] Arieh Iserles, A First Course in the Numerical Analysis of Differential Equa-
tions. Cambridge Texts in Applied Mathematics, Cambridge University Press
1997
[4] Eike Rietsch, An Introduction to Scilab from a Matlab User’s Point of View.
http://wiki.scilab.org/Tutorials
26 R. VERFÜRTH
jacobi, 9
lagrange, 6
lax friedrichs, 18
lax wendroff, 18
matrix, 3
midpoint rule, 8
minimal cost flow, 22
nelder mead, 25
neville, 5
newton, 8
27