Nonlinear Workbook (3rd)

607

Click here to load reader

description

Advanced Algorithms Textbook

Transcript of Nonlinear Workbook (3rd)

Page 1: Nonlinear Workbook (3rd)
Page 2: Nonlinear Workbook (3rd)

I NONLINEARE WORKBOOK

3rd Edition

Page 3: Nonlinear Workbook (3rd)
Page 4: Nonlinear Workbook (3rd)

; NONLINEARE WORKBOOKChaos, Fractals,

Cellular Automata,

Neural Networks,

Genetic Algorithms,

Gene Expression Programming,

Support Vector Machine, Wavelets,

Hidden Markov Models, Fuzzy Logic

with C++, Java and SymbolicC++ Programs

Willi-Hans SteebInternational School for Scientific Computing

University of Johannesburg

in collaboration with

Yorick HardyInternational School for Scientific Computing

Ruedi StoopInstitute for Neuroinformatics, University of Zurich/ETHZ

Yf> World ScientificN E W J E R S E Y • L O N D O N • S I N G A P O R E • B E I J I N G - S H A N G H A I • H O N G K O N G • T A I P E I • C H E N N A I

3rd Edition

Page 5: Nonlinear Workbook (3rd)

Published by

World Scientific Publishing Co. Pte. Ltd.

5 Toh Tuck Link, Singapore 596224

USA office: 27 Warren Street, Suite 401-402, Hackensack, NJ 07601

UK office: 57 Shelton Street, Covent Garden, London WC2H 9HE

British Library Cataloguing-in-Publication DataA catalogue record for this book is available from the British Library.

THE NONLINEAR WORKBOOK - Third Edition

Copyright © 2005 by World Scientific Publishing Co. Pte. Ltd.

All rights reserved. This book, or parts thereof, may not be reproduced in any form or by any means,electronic or mechanical, including photocopying, recording or any information storage and retrievalsystem now known or to be invented, without written permission from the Publisher.

For photocopying of material in this volume, please pay a copying fee through the Copyright ClearanceCenter, Inc., 222 Rosewood Drive, Danvers, MA 01923, USA. In this case permission to photocopyis not required from the publisher.

ISBN 981-256-278-8ISBN 981-256-291-5 (pbk)

Printed in Singapore by World Scientific Printers (S) Pte Ltd

Page 6: Nonlinear Workbook (3rd)

Preface

The study of chaos, fractals, cellular automata, neural networks, genetic algorithmsand fuzzy logic is one of the most fascinating subjects in science. Most of these fieldsare interrelated. Chaotic attractors are used in neural networks. Genetic algorithmscan be used to train neural networks. Fractals are used in data compression. Neuralnetworks and fuzzy logic are often combined when the input values of the systemare not crisp.

In this book we give all the basic concepts in these fields together with the definitions,theorems and algorithms. The algorithms are implemented using C++, Java andSymbolicC++. The level of presentation is such that one can study the topic earlyon while studying science. There is a balance between practical computation andthe underlying mathematical theory.

In chapter 1 we consider one and two-dimensional nonlinear maps. All the relevantquantities to characterize chaotic systems are introduced. Algorithms are givenfor all the quantities which are used to describe chaos such as invariant density,Liapunov exponent, correlation integral, autocorrelation function, capacity, phaseportrait, Poincare section, Fourier transform, calculations of exact trajectories, fixedpoints and their stability, etc.

Quite often a dynamical system cannot be modelled by difference equations or differ-ential equations, but an experiment provides a time series. In chapter 2 we considerquantities for the study of chaotic time-series. We also include the Hurst exponentwhich plays an important role in the study of financial markets.

In chapter 3 we describe the classification of fixed points in the plane. Furthermorethe most important two-dimensional dynamical systems are studied, such as thependulum, limit cycle systems and a Lotka-Volterra model. Homoclinic orbits arealso introduced.

Chapter 4 reviews integrable and chaotic Hamilton systems. Among other con-cepts we introduce the Lax representation for integrable Hamilton systems and thePoincare section.

v

Page 7: Nonlinear Workbook (3rd)

vi PREFACE

In chapter 5 nonlinear dissipative systems are studied. The most famous dissipativesystem with chaotic behaviour, the Lorenz model, is introduced. We also discussHopf bifurcation and hyperchaotic systems.

Nonlinear driven systems play a central role in engineering, in particular in elec-tronics. In most cases the driving force is periodic. Chapter 6 is devoted to thesesystems. As examples we consider among others the driven pendulum and the drivenvan der Pol equation.

Controlling and synchronization of chaotic systems is very important in applicationsin engineering. In chapter 7 we discuss the different concepts of controlling chaosand synchronization of chaos. The Ott-Grebogi-Yorke method for controlling chaoticsystems is also studied.

Fractals have become of increasing interest, not only in art, but also in many differentareas of science. In chapter 8 we introduce iterated function systems, the Mandelbrotset, the Julia set and the Weierstrass function. The famous Cantor set is consideredas an example.

Cellular automata are discrete dynamical systems. We describe in chapter 9 oneand two-dimensional cellular automata. The famous game of life with a C++ im-plementation and the button game with a Java implementation are also considered.

Chapter 10 is about integration of differential equations. We describe the Eulermethod, the Runge-Kutta method, the Lie series technique, symplectic integration,Verlet method, etc.. Furthermore we discuss ghost solutions, invisible chaos andintegration in the complex domain.

Chapter 11 is devoted to neural networks. We introduce the Hopfield algorithm,the Kohonen self-organizing map and the back propagation algorithm. One of theapplications is the traveling salesman problem.

Genetic algorithm are used to solve optimization problems. Chapter 12 is devotedto this technique. We discuss optimization problems with and without constraints.We also study simulated annealing.

Gene expression programming is a new genetic algorithm that uses encoded individ-uals. Gene expression programming individuals are encoded in linear chromosomeswhich are expressed or translated into expression trees. The linear chromosome isthe genetic material that is passed on with modifications to the next generation.Chapter 13 gives an introduction to this technique together with a C++ program.

In chapter 14 optimization we consider the Lagrange multiplier method, the Karush-Kuhn-Tucker condition and the support vector machine. The Kernel-Adatron

Page 8: Nonlinear Workbook (3rd)

PREFACE vii

algorithm, a fast and simple learning procedure for support vector machines, is alsoimplemented.

Wavelet theory is a form of mathematical transformation, similar to the Fouriertransform in that it takes a signal in time domain, and represents it in frequencydomain. Wavelet functions are distinguished from other transformations in thatthey not only dissect signals into their component frequencies, they also vary thescale at which the component frequencies are analyzed. Chapter 15 provides anintroduction. An application as a filter is given. As examples, the Haar wavelet andDaubechies wavelet are studied. Two-dimensional wavelets are also considered.

Discrete Hidden Markov Models are introduced in chapter 16. The forward-backwardalgorithm, Viterbi algorithm, and Baum-Welch algorithm are described. The appli-cation concentrates on speech recognition.

Since its inception 30 years ago the theory of fuzzy sets has advanced in a vari-ety of ways and in many disciplines not only in science. Chapter 17 is devoted tofuzzy logic. Fuzzy numbers and arithmetic are also considered. Furthermore deci-sion making problems and controlling problems using fuzzy logic are also described.Fuzzy clustering is also included.

In each chapter we give C++, Java and SymbolicC++ implementations of the al-gorithms.

Without doubt, this book can be extended. If you have comments or suggestions, Iwould be pleased to have them. The author can be contacted via e-mail:

steeb_wh.@yahoo. comwhsOna.rau.ac.za

The web pages of the author are:

http://issc.rau.ac.zahttp://zeus.rau.ac.za

The International School for Scientific Computing (ISSC) provides certificate coursesfor these subjects. Please contact the author if you want to do any of these courses.

Page 9: Nonlinear Workbook (3rd)
Page 10: Nonlinear Workbook (3rd)

Contents

1 Nonlinear and Chaotic Maps 11.1 One-Dimensional Maps 1

1.1.1 Exact and Numerical Trajectories 31.1.2 Fixed Points and Stability 151.1.3 Invariant Density 171.1.4 Liapunov Exponent 221.1.5 Autocorrelation Function 261.1.6 Discrete Fourier Transform 281.1.7 Fast Fourier Transform 301.1.8 Logistic Map and Liapunov Exponent for r G [3,4] 361.1.9 Logistic Map and Bifurcation Diagram 381.1.10 Random Number Map and Invariant Density 391.1.11 Random Number Map and Random Integration 421.1.12 Circle Map and Rotation Number 441.1.13 Newton-Raphson Method 451.1.14 Feigenbaum's Constant 471.1.15 Symbolic Dynamics 491.1.16 Chaotic Repeller 52

1.2 Two-Dimensional Maps 531.2.1 Introduction 531.2.2 Phase Portrait 541.2.3 Fixed Points and Stability 631.2.4 Liapunov Exponents 641.2.5 Correlation Integral 661.2.6 Capacity 671.2.7 Hyperchaos 701.2.8 Domain of Attraction 741.2.9 Newton Method in the Complex Domain 751.2.10 Newton Method in Higher Dimensions 771.2.11 Ruelle-Takens-Newhouse Scenario 791.2.12 JPEG file 81

2 Time Series Analysis 832.1 Introduction 832.2 Correlation Coefficient 84

ix

Page 11: Nonlinear Workbook (3rd)

x CONTENTS

2.3 Liapunov Exponent from Time Series 852.3.1 Jacobian Matrix Estimation Algorithm 862.3.2 Direct Method 88

2.4 Hurst Exponent 952.4.1 Introduction 952.4.2 Implementation for the Hurst Exponent 982.4.3 Random Walk 102

2.5 Complexity 107

3 Autonomous Systems in the Plane 1113.1 Classification of Fixed Points I l l3.2 Homoclinic Orbit 1133.3 Pendulum 1143.4 Limit Cycle Systems 1163.5 Lotka-Volterra Systems 120

4 Nonlinear Hamilton Systems 1244.1 Hamilton Equations of Motion 124

4.1.1 Hamilton System and Variational Equation 1264.2 Integrable Hamilton Systems 128

4.2.1 Hamilton Systems and First Integrals 1284.2.2 Lax Pair and Hamilton Systems 1294.2.3 Floquet Theory 132

4.3 Chaotic Hamilton Systems 1354.3.1 Henon-Heiles Hamilton Function and Trajectories 1354.3.2 Henon Heiles and Surface of Section Method 1374.3.3 Quartic Potential and Surface of Section Technique 139

5 Nonlinear Dissipative Systems 1425.1 Fixed Points and Stability 1425.2 Trajectories 1475.3 Phase Portrait 1515.4 Liapunov Exponents 1545.5 Generalized Lotka-Volterra Model 1575.6 Hyperchaotic Systems 1605.7 Hopf Bifurcation 1635.8 Time-Dependent First Integrals 166

6 Nonlinear Driven Systems 1686.1 Introduction 1686.2 Driven Anharmonic Systems 171

6.2.1 Phase Portrait 1716.2.2 Poincare Section 1726.2.3 Liapunov Exponent 1746.2.4 Autocorrelation Function 176

Page 12: Nonlinear Workbook (3rd)

CONTENTS xi

6.2.5 Power Spectral Density 1806.3 Driven Pendulum 180

6.3.1 Phase Portrait 1806.3.2 Poincare Section 183

6.4 Parametrically Driven Pendulum 1856.4.1 Phase Portrait 1856.4.2 Poincare Section 187

6.5 Driven Van der Pol Equation 1886.5.1 Phase Portrait 1886.5.2 Liapunov Exponent 191

6.6 Parametrically and Externally Driven Pendulum 193

7 Controlling and Synchronization of Chaos 1977.1 Introduction 1977.2 Ott-Yorke-Grebogi Method 198

7.2.1 One-Dimensional Maps 1987.2.2 Systems of Difference Equations 202

7.3 Small Periodic Perturbation 2067.4 Resonant Perturbation and Control 2097.5 Synchronization of Chaos 209

7.5.1 Synchronization Using Control 2097.5.2 Synchronizing Subsystems 212

7.6 Phase Coupled Systems 216

8 Fractals 2228.1 Introduction 2228.2 Iterated Function System 224

8.2.1 Introduction 2248.2.2 Cantor Set 2258.2.3 Heighway's Dragon 2288.2.4 Sierpinski Gasket 2318.2.5 Koch Curve 2328.2.6 Fern 235

8.3 Mandelbrot Set 2378.4 Julia Set 2408.5 Weierstrass Function 242

9 Cellular Automata 2449.1 Introduction 2449.2 One-Dimensional Cellular Automata 2469.3 Two-Dimensional Cellular Automata 2489.4 Button Game 253

Page 13: Nonlinear Workbook (3rd)

xii CONTENTS

10 Solving Differential Equations 25710.1 Introduction 25710.2 Euler Method 25810.3 Lie Series Technique 25910.4 Runge-Kutta-Fehlberg Technique 26310.5 Ghost Solutions 26410.6 Symplectic Integration 26810.7 Verlet Method 27310.8 Stormer Method 27510.9 Invisible Chaos 27610.10 First Integrals and Numerical Integration 277

11 Neural Networks 27911.1 Introduction 27911.2 Hopfield Model 283

11.2.1 Introduction 28311.2.2 Synchronous Operations 28511.2.3 Energy Function 28811.2.4 Basins and Radii of Attraction 28911.2.5 Spurious Attractors 29011.2.6 Hebb's Law 29011.2.7 Example 29211.2.8 Program 29411.2.9 Asynchronous Operation 29911.2.10 Translation Invariant Pattern Recognition 300

11.3 Similarity Metrics 30111.4 Kohonen Network 306

11.4.1 Introduction 30611.4.2 Algorithm 30711.4.3 Example 30911.4.4 Traveling Salesman Problem 315

11.5 Perceptron 31911.5.1 Introduction 31911.5.2 Boolean Functions 32211.5.3 Linearly Separable Sets 32311.5.4 Perceptron Learning 32411.5.5 One and Two Layered Networks 32811.5.6 Perceptron Learning Algorithm 32911.5.7 The XOR Problem and Two-Layered Networks 332

11.6 Multilayer Perceptrons 33711.6.1 Introduction 33711.6.2 Cybenko's Theorem 33811.6.3 Back-Propagation Algorithm 339

11.7 Chaotic Neural Networks 348

Page 14: Nonlinear Workbook (3rd)

CONTENTS xiii

12 Genetic Algorithms 35012.1 Introduction 35012.2 The Sequential Genetic Algorithm 35112.3 Schemata Theorem 35512.4 Bitwise Operations 35712.5 A Bit Vector Class 36412.6 Maximum of One-Dimensional Maps 36712.7 Maximum of Two-Dimensional Maps 37512.8 Finding a Fitness Function 38412.9 Problems with Constraints 389

12.9.1 Introduction 38912.9.2 Knapsack Problem 39112.9.3 Traveling Salesman Problem 397

12.10 Simulated Annealing 40612.11 Parallel Genetic Algorithms 409

13 Gene Expression Programming 42313.1 Introduction 42313.2 Example 426

14 Optimization 43514.1 Lagrange Multiplier Method 43514.2 Karush-Kuhn-Tucker Conditions 44014.3 Support Vector Machine 444

14.3.1 Introduction 44414.3.2 Linear Decision Boundaries 44414.3.3 Nonlinear Decision Boundaries 448

15 Discrete Wavelets 45315.1 Introduction 45315.2 Multiresolution Analysis 45515.3 Pyramid Algorithm 45715.4 Biorthogonal Wavelets 46315.5 Two-Dimensional Wavelets 469

16 Discrete Hidden Markov Processes 47216.1 Introduction 47216.2 Markov Chains 47416.3 Discrete Hidden Markov Processes 47816.4 Forward-Backward Algorithm 48216.5 Viterbi Algorithm 48516.6 Baum-Welch Algorithm 48616.7 Distances between HMMs 48716.8 Application of HMMs 48816.9 Program 491

Page 15: Nonlinear Workbook (3rd)

xiv CONTENTS

17 Fuzzy Sets and Fuzzy Logic 50217.1 Introduction 50217.2 Operators for Fuzzy Sets 510

17.2.1 Logical Operators . 51017.2.2 Algebraic Operators 51317.2.3 Defuzzification Operators 51517.2.4 Fuzzy Concepts as Fuzzy Sets 51617.2.5 Hedging 51817.2.6 Quantifying Fuzzyness 51917.2.7 C++ Implementation of Discrete Fuzzy Sets 52017.2.8 Applications: Simple Decision-Making Problems 540

17.3 Fuzzy Numbers and Fuzzy Arithmetic 54717.3.1 Introduction 54717.3.2 Algebraic Operations 54817.3.3 LR-Representations 55217.3.4 Algebraic Operations on Fuzzy Numbers 55417.3.5 C++ Implementation of Fuzzy Numbers 55617.3.6 Applications 562

17.4 Fuzzy Rule-Based Systems 56317.4.1 Introduction 56317.4.2 Fuzzy If-Then Rules 56717.4.3 Inverted Pendulum Control System 56817.4.4 Application 570

17.5 Fuzzy C-Means Clustering 57217.6 Fuzzy Truth Values and Probabilities 576

Bibliography 577

Index 585

Page 16: Nonlinear Workbook (3rd)

Symbol Index

0 empty setZ the set of integersN the set of positive integers: natural numbersQ the set of rational numbersR the set of real numbersR + nonnegative real numbersC the set of complex numbersR™ the n-dimensional real linear spaceCn the n-dimensional complex linear space{0, l } n n-dimensional binary hypercube[a, 6] closed interval of real-valued numbers between a and b[0,1] unit intervalAd B the subset A of the set BAD B the intersection of the sets A and BA U B the union of the sets A and Bf mapping (map)fog composition of mappings (/ o g)(x) = f{g{x))/<") n-th iterate1 := v ^Wtz real part of the complex number zSsz imaginary part of the complex number zx £ Rn the element x of R", column vectorxT transpose of x (row vector)xTy scalar product in Rn

t time: discrete and continuous depending on contextx dependent variable: discrete systemsx* fixed pointu dependent variable: continuous systemsp invariant densityr bifurcation parameter, control parameter||. || normtr trace of a square matrixdet determinant of a square matrixH Hamilton function5jk Kronecker delta with Sjk = 1 for j = k and 5jk = 0 for j ^ k

xv

Page 17: Nonlinear Workbook (3rd)

xvi SYMBOL INDEX

A eigenvalueT] learning rate/ identity matrixw weight vector (column vector)Aw small change applied to wW weight matrix0 bias vector{xfc,dfc} kth. training pairsnet weighted sum or wTxf(net) differentiable activation function, usually a sigmoid functionf'(net) derivative of / with respect to netH% membersh ip function (fuzzy logic)

Page 18: Nonlinear Workbook (3rd)

Comments to the Programs

The C++ programs comply to the ANSI C++ standard. Thus they should rununder all compilers.

SymbolicC++ is a symbolic manipulation tool (Tan Kiat Shi, W.-H. Steeb andY. Hardy [91]) written completely in Ansi C++. It includes classes (abstract datatypes) to do symbolic and numeric manipulations. The classes are

Verylong, Rational, Complex, Quaternion, Derive, Vector, Matrix,

Groebner, Array, String, BitVector, Lis t , Polynomial, Set .

The symbolic manipulation is done by the classes Symbol and Sum.

We have tested the C++ programs with GCC 3.0.1 and Microsoft Visual Studio.net(VC7). For template friend functions there is a difference in the declaration. Forexample, for a function f unc and operator + in the class Sum in SymbolicC++ wehave

friend Sum<T> func<>(...)friend Sum<T> operator + <>(...)

for GCC 3.0.1 and

friend Sum<T> func(...)friend Sum<T> operator + <T>(...)

for VC7.

In C++ graphics does not belong to the standard. Here we use GnuPlot (GNU-PLOT Copyright(c) 1986-1993, 1998, Colin Kelly and Thomas Williams) to drawthe figures.

The Java programs have been tested with JDK 1.5. The JDK (Java DevelopmentKit) is a product of Sun Microsystems, Inc. The JDK allows us to develop appletsthat will run in browsers supporting the Java platform 1.5. The Java tools we usingare the Java Compiler (javac) which compiles programs written in the Java pro-gramming language into bytecodes and the Java Interpreter (java) that executesJava bytecodes. In other words, it runs programs written in the Java programminglanguage. Applet Viewer allows us to run one or more Java applets that are calledreferences in a web page (HTML file) using the APPLET tag. The Applet Viewerfinds the APPLET tags in the HTML file and runs the applets (in separate win-dows) as specified by the tags.

Most of the programs are written so that they can be understood by beginners.Thus some of the programs can be improved and written in a more sophisticatedmanner.

xvii

Page 19: Nonlinear Workbook (3rd)
Page 20: Nonlinear Workbook (3rd)

Chapter 1

Nonlinear and Chaotic Maps

1.1 One-Dimensional Maps

In this section we consider nonlinear and chaotic one-dimensional maps

/ : 5 - > 5 , S C R .

In most cases the set S will be S = [0,1] or S = [—1,1]. The one-dimensional mapcan also be written as a difference equation

x t + i = f { x t ) , i = 0 , 1 , 2 , . . . x o e S .

Starting from an initial value XQ G 5 we obtain, by iterating the map, the sequence

x0, . xi, x2, . . .

or

so, /(*o), /(/(*<.)), / ( / ( / ( i o ) ) ) , . . . •

For any XQ £ S the sequence of points x0, x\, x2, • • -is called the forward orbit (orforward trajectory) generated by xo- The goal of a dynamical system is to understandthe nature of all orbits, and to identify the set of orbits which are periodic, eventuallyperiodic, asymptotic, etc. Thus we want to understand what happens if t —> oo. Insome cases the long-time behaviour is quite simple. Consider, for example, the map/ : R + —> R + with f(x) = y/x. For all Xo £ R+ the forward trajectory tends to 1.Another simple example is the map / : [0,1] —>• [0,1], f{x) = x2. If XQ = 0, thenf(x0) = 0. Analogously, if XQ = 1, then f(xo) = 1. The points 0 and 1 we call fixedpoints for this map. For x £ (0,1), the forward trajectory tends to 0. In most casesthe behaviour of a map is much more complex.

Next we introduce some basic definitions for dynamical systems (Devaney [23],Holmgren [47]).

Definition. A point x* £ S is called a fixed point of the map / if f(x*) = x*.

1

Page 21: Nonlinear Workbook (3rd)

2 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

Example. Consider the map / : [0,1] -» [0,1] with f(x) = 4a;(1 - x). Thenx* = 3/4 and x* = 0 are fixed points.

Definition. A point x* G 5 is called a periodic point of period n if

/(">(z*)=z*

where / ' " ' denotes the n-th iterate of / . The least positive integer n for which/'"'(a;*) _ x* j s cane(j the p r j m e period of x*. The set of all iterates of a periodicpoint form a periodic orbit.

Example. Consider the map / : R —>• R and f(x) - x2 - 1. Then the points 0 and—1 lie on a periodic orbit of period 2 since /(0) = —1 and /(—I) = 0 .

Definition. A point x* is eventually periodic of period n if a;* is not periodic butthere exists m > 0 such that

f{n+i\x*) = fW(x*)

for all i > m. That is, f^(x*) is periodic for i > m.

Definintion. Let a;* be a periodic point of prime period n. The point x* is hyperbolicif

I(/W)'(z*)l / 1where ' denotes the derivative. The number (f^)'(x*) is called the multiplier of theperiodic point.

Theorem. Let x* be a hyperbolic fixed point with |/'(z*)| < 1. Then there is anopen interval U about a;* such that if x € U, then

lim f{n)(x)=x*.

In the following sections we also introduce the following concepts important in thestudy of nonlinear and chaotic maps. The concepts are:

1) Fixed points2) Liapunov exponent3) Invariant density4) Autocorrelation functions5) Moments6) Fourier transform7) Bifurcation diagrams8) Feigenbaum number9) Symbolic dynamics10) Chaotic repeller

Page 22: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 3

The one-dimensionial maps we consider in our examples are:

1) the logistic map2) the tent map3) the Bernoulli map4) the Gauss map5) a bungalow-tent map6) the circle map

1.1.1 Exact and Numerical TrajectoriesIn this section we calculate trajectories for one-dimensional maps. In our first ex-ample we consider the map / : N -» N denned by

ti \ _ / 2/2 if a: is evenJ[X) : ~ \3x + 1 ifx is odd

where N denotes the natural numbers. For this map it is conjectured that for allinitial values the trajectory finally tends to the period orbit

. . . 4 2 1 4 2 1 . . .

The data type unsigned long (4 bytes) in C++ is restricted to the range

0...4294967295

and the data type long (4 bytes) in C++ is restricted to the range

-2147483648...+2147483647

To check the conjecture for larger initial values we use the abstract data typeVerylong in SymbolicC++ (Tan Kiat Shi et al [91]). In this class all arithmeticoperators are overloaded. The overloaded operators are

+, - , *, / , X, +=, -=, •=. /=

For the initial value 56 we find the sequence

28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1, .

Thus the orbit is eventually periodic.

Two different initial values are considered in the program trajecl .cpp, namely 56and 998123456789.

Page 23: Nonlinear Workbook (3rd)

4 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

II trajectoryl.cpp

#include <iostream> // for coutttinclude "verylong.h" // for data type Verylong of SymbolicC++using namespace std;

int main(void){unsigned long y = 56; // initial valueunsigned long T = 20; // number of iterationsunsigned long t;

for(t=0;t<T;t++){if((y7.2) == 0) y = y/2;else y = 3*y + 1;cout « y « endl;>

Verylong x("998123456789"); // initial valueVerylong zero("0");Verylong oneC'l");Verylong two("2");Verylong three("3");T = 350;

for(t=0;t<T;t++)

{if((x*/.two) == zero) x = x/two;else x = three*x + one;cout « x « endl;>

return 0;

}

Java provides a class Biglnteger. Since operators such as+, - , *, / , 7, cannotbe overloaded in Java, Java uses methods to do the arithmetic operations. Themethods are

add(), sub t rac tO, multiply() , d iv ideO, mod() .

The constructor Biglnteger (String val) translates the decimal String represen-tation of a Biglnteger into a Biglnteger. The class Biglnteger also provides thedata fields Biglnteger.ONE and Biglnteger.ZERO.

Page 24: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 5

/ / Trajectory!..Java

import j ava.math.*;

public class Trajectorylipublic static void main(String[] args)

{Biglnteger X = new BigIntegerC'998123456789");Biglnteger TWO = new Biglnteger("2");Biglnteger THREE = new Biglnteger("3");

int T = 350;for(int t=0;t<T;t++){if((X.mod(TWO)).equals(Biglnteger.ZERO))X = X.divide(TWO);else{X = X.multiply(THREE);X = X.add(Biglnteger.ONE);>System.out.printlnC'X = " + X);>>

}

In our second example we consider the trajectories for the logistic map. The logisticmap f : [0,1] —> [0,1] is given by f(x) = 4x(l — x). The logistic map can also bewritten as the difference equation

xt+1 = Axt{l - xt)

where t = 0,1, 2 , . . . and x0 € [0,1]. It follows that xt € [0,1] for all t € N. LetXQ = 1/3 be the initial value. Then we find that

_ 8 _ 32 _ 6272 _ 72504321 1 ~ 9' l 2 ~ 8 r l 3 ~ 6 5 6 l ' * 4 ~ 43046721' '"

The exact solution of the logistic map is given by

xt = - - - cos(2* arccos(l - 2xQ)).Zi Zi

In the C++ program trajectory2.cpp we evaluate the exact trajectory up tot = 10 using the abstract data type Verylong of SymbolicC++. For t = 7 we find

_ 3383826162019367796397224108032Xl ~ 3433683820292512484657849089281'

Page 25: Nonlinear Workbook (3rd)

6 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

II trajectory2.cpp

#include <iostream>#include "verylong.h" // for data type Verylong#include "rational.h" // for data type Rationalusing namespace std;

inline void map(Rational<Verylong>& x){RationaKVerylong> one("l" ,"i"); // number 1RationaKVerylong> four("4","1"); // number 4Rational<Verylong> xl = four*x*(one - x);

x = xl;

}

int main(void)

{RationaKVerylong> xO("l","3"); // initial value 1/3unsigned long T = 10; // number of iterationsRationaKVerylong> x = xO;cout « "x[0] = " « x « endl;

for(unsigned long t=O;t<T;t++)

{map(x);

cout « "x[" « t+1 « "] = " « x « endl;

}

return 0;

>

In the C++ program trajectory3.cpp we evaluate the numerical trajectory usingthe basic data type double. We find that the difference between the exact valueand the numerical value for t = 40 is

X^exact - XiOapprox = 0.055008 - 0.055015 = -0.000007.

// trajectory3.cpp

#include <iostream>using namespace std;

inline void map(double& x)

idouble xl = 4.0*x*(1.0 - x);x = xl;

}

Page 26: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 7

int main(void)

•C

double xO = 1.0/3.0; // initial valueunsigned long T = 10; // number of iterationsdouble x = xO;cout « "x[0] = " « x « endl;

for(unsigned long t=0;t<T;t++){map(x);cout « "x[" « t+1 « "] = " « x « endl;}return 0;

>

As a third example we consider the Bernoulli map. Let / : [0,1) -»• [0,1). TheBernoulli map is defined by

f(x) := 2x mod 1 = frac(2z).

The map can be written as the difference equation

_ f 2xt ioiO<xt<l/2Xt+1 ~ \ {2xt - 1) for 1/2 < xt < 1

where t = 0,1,2,. . . and Xo € [0,1). The map admits only one fixed point x* — 0.The fixed point is unstable. Let x0 = 1/17. Then we find the periodic orbit

17' 17' 17' 17' 17' 17' 17' 17' 1 7 ' " 'If x0 is a rational number in the interval [0,1), then xt is either periodic or tends tothe fixed point x* = 0. The solution of the Bernoulli map is given by

xt = 2lxa mod 1

where XQ is the initial value. For almost all initial values the Liapunov exponent isgiven by In 2. Every XQ 6 [0,1) can be written (uniquely) in the binary representation

oo

xo = Ylak'2~k, 0*6 (0 ,1} .fc=i

One now definesoo

(01,02,03,...) := 53 a * 2 ~*k=\

and considers the infinite sequence (01,02,03,...). For example, XQ = 3/8 can berepresented by the sequence (0,1,1,0,0,...). Instead of investigating the Bernoullimap we can use the map r defined by

T(OI, o2, a3,...) := (a2, a3, a4, . . .) .

Page 27: Nonlinear Workbook (3rd)

8 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

This map is called the Bernoulli shift. In the C++ program trajectory!.cpp wefind the trajectory of the Bernoulli map using the data type Rational and Verylongof SymbolicC++. The initial value is 1/17.

/ / trajectory4.cpp

#include <iostream>#include "verylong.h" // for data type Verylong#include "rational.h" // for data type Rationalusing namespace std;

inline void map(Rational<Verylong>& x)

{Rational<Verylong> one("l","1"); // number 1RationaKVerylong> two("2","l"); // number 2RationaKVerylong> half("1","2"); // number 1/2Rational<Verylong> xl;if(x < half) xl = two*x;else xl = two*x - one;

x = xl;

>

int main(void)

{RationaKVerylong> xOC'l","17"); // initial value 1/17unsigned long T = 10; // number of iterationsRational<Verylong> x = xO;cout « "x[0] = " « x « endl;

for(unsigned long t=0;t<T;t++)

{map(x);cout « "x[" « t+1 « "] = " « x « endl;

}

return 0;

}

As a third example we consider the tent map. The tent map / : [0,1] —> [0,1] isdefined as

/ 2x if i e [0,1/2)/ W - j 2(1 - i ) if z e [1/2,1] •

The map can also be written as the difference equation

_ / 2xt \{xt £ [0,1/2)Xt+l-\2(l-xt)iixt£ [1/2,1]

Page 28: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 9

where t = 0,1,2,. . . and x0 G [0,1]. Let x0 = 1/17 be the initial value. Then theexact orbit is given by

1 2 4 8 16 2

*0 = i7' Xl = Tr X2 = Tr X3=vi> l4 = Tr ^ r r -This is an example of an eventually periodic orbit. If the initial value is a rationalnumber then the orbit is eventually periodic, periodic or tends to a fixed point. Forexample the initial value 1/16 tends to the fixed point 1. To find chaotic orbits theinitial value must be an irrational number, for example XQ = 1/TT. The fixed pointsof the map are given by x* = 0, x* = 2/3. These fixed points are unstable. The mapshows fully developed chaotic behaviour. The invariant density is given by p(y) = 1.For almost all initial values the Liapunov exponent is given by A = In 2. For theautocorrelation function we find

° » W - \ 0 f o r r > l •

In the C++ program trajectory5. cpp we find the trajectory of the tent map withthe inital value 1/17.

/ / trajectory5.cpp

#include <iostream>#include "verylong.h"#include "rational.h"using namespace std;

inline void map(Rational<Verylong>& x)

{Rational<Verylong> oneC'l","1"); // number 1RationaKVerylong> two("2","l"); // number 2RationaKVerylong> half ("1" , "2"); // number 1/2Rational<Verylong> xl;if(x < half) xl = two*x;else xl = two*(one-x);x = xl;

}

int main(void)

{RationaKVerylong> xO("l","17"); // initial value 1/17unsigned long T = 10; // number of iterationsRationaKVerylong> x = xO;cout « "x[0] = " « x « endl;

for(unsigned long t=0;t<T;t++)

Page 29: Nonlinear Workbook (3rd)

10 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

{map(x) ;cout « "x[" « t+i « "] = " « x « endl;>return 0;

}

As a fourth example we consider a bungalow-tent map. Our bungalow-tent map/ : [0,1] ->• [0,1] is denned by

1 — ax for x € [0, a)

2a 1 - 3o, M T^2aX + T^Ta f ° r X&[a^fa[x) := i

^ ( l - ^ + i^ffor^tlAl-.)

]—^(l-x) for i e [ l - o , l ]where a € (0,1/2) is the control parameter. The map is continuous, but not differ-entiable at the points a, 1 — a (a / 1/3) and x — 1/2. The map is piecewise linear.The fixed points are 0 and 1 — a. For a = 1/3 we obtain the tent map. The map fa

is a special bungalow-tent map. The intersection point P of the line in the interval[1/2,1 — o) and the line in the interval [1 — a, 1] lies on the diagonal y = x. Theinvariant density is given by

, , 1 . . 1 - 2a

where \ ls the indicator function, i.e. XA{X) = 1 if x £ A and XA{X) = 0 if x $. A.Thus the invariant density is constant in the interval [0,1 — a). At 1 — a the invariantdensity jumps to another constant value. The Liapunov exponent is given by

. . . 1 - a , / l - a \ l - 2 a / 2o \

For a = 1/3 we obviously obtain A(l/3) = In 2. This is the Liapunov exponent forthe tent map. For n - > 0 w e obtain X(a -» 0) = fin2. For a -» 1/2 we obtainA(o -* 1/2) = 0. A (a) has a maximum for a = 1/3 (tent map). Furthermore \(a) isa convex function in the interval (0,1/2). Thus we have

X(a) < l n 2 .

The C++ program trajectory6. cpp finds the trajectory of the bungalow-tent mapfor the control parameter a — 1/7 and the initial value XQ = 1/17. We find X\ =6/17, x2 = 16/17, x3 = 6/17. Thus the orbit is eventually periodic.

Page 30: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 11

/ / trajectory^.cpp

#include <iostream>#include "verylong.h"#include "rational.h"using namespace std;

inline void map(Rational<Verylong>& x,Rational<Verylong>& a)<Rational<Verylong> oneC'l","1"); // number 1Rational<Verylong> two("2","l"); // number 2Rational<Verylong> three("3","1"); // number 3Rational<Verylong> half("1","2"); // number 1/2Rational<Verylong> xl;if(x < a) xl = (one-a)*x/a;else if((x >= a) && (x < half))xl = two*a*x/(one-two*a) + (one-three*a)/(one-two*a);else if((x >= half) && (x < one - a))xl = two*a*(one-x)/(one-two*a) + (one-three*a)/(one-two*a);else if((x <= one) && (x > one - a))xl = (one-a)*(one-x)/a;

x = xl;

}

int main(void)

{RationaKVerylong> xO("l","17"); // initial value 1/17RationaKVerylong> a("l","7"); // control parameter 1/7unsigned long T = 10; // number of iterationsRational<Verylong> x = xO;cout « "x[0] = " « x « endl;

for(unsigned long t=0;t<T;t++)

imap(x,a);cout « "x[" « t+1 « "] = " « x « endl;}return 0;

>

As another example we consider the Gauss map. The Gauss map / : [0,1] —> [0,1]is defined as

, . _ / 0 if x = 0Hx)-\[l/x}iix^0

Page 31: Nonlinear Workbook (3rd)

12 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

where [y] denotes the fractional part of y. For example

!3,H0,, [£]-§.Owing to the definition x* = 0 is a fixed point. Let XQ = 23/101 be the initial value.Then the orbit is given by

9 5 4 1

where x5 — 0 is a fixed point. The Gauss map possesses an infinite number ofdiscontinuities and is not injective since each x0 € [0,1] has countable infinite images.The map admits an infinite number of unstable fixed points and shows chaoticbehaviour. For example x* — (VE — l)/2 is a fixed point, since x* = f(x*). TheGauss map preserves the Gauss measure on [0,1] which is given by

m(A) := / dx.

The periodic points of the Gauss map are the reciprocal of the reduced quadraticirrationals. These numbers are dense in [0,1).

// trajectory7.cpp

#include <iostream>#include "verylong.h"#include "rational.h"using namespace std;

inline void map(Rational<Verylong>& x)

{RationaKVerylong> oneO'l", "1"); // number 1Rational<Verylong> zero("0","1"); // number 0Rat ionaK Very long> xl;if(x == zero) return;xl = one/x;while(xl >= one) xl = xl - one;

x = xl;

}

int main(void)

{

RationaKVerylong> xO("9","23"); // initial value 9/23

unsigned long T = 10; // number of iterations

Rational<Verylong> x = xO;

cout « "x[0] = " « x « endl;

Page 32: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 13

for(unsigned long t=0;t<T;t++)i.map(x);cout « "x[" « t+1 « "] = " « x « endl;}return 0;

}

The next program datagnu. cpp shows how to write the output data from an itera-tion to a file. We consider the logistic map as an example. The output is stored ina file called timeev.dat. We use the C++ style for the file manipulation.

// datagnu.cpp

#include <fstream> // for ofstream, closeusing namespace std;

int main(void)

iofstream dataCtimeev.dat");unsigned long T = 100; // number of iterationsdouble xO = 0.618; // initial valuedouble xl;

for(unsigned long t=0;t<T;t++){xl = 4.0*x0*(1.0 - xO);data « t « " " « xO « "\n";xO = xl;}data.close();return 0;

>

The data files can now be used to draw a graph of the time evolution using GNU-plot. After we entered GNU-plot using the command gnuplot the plot command isas follows:

plot [0:10] ' t imeev.dat '

This command plots the first eleven points of the time evolution. Furthermore wecan create a postscript file using the commands:

set term postscript defaultset output "timeev.ps"plot 'timeev.dat'

Page 33: Nonlinear Workbook (3rd)

14 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

The next program shows how to write data to a file and read data from a file usingJAVA. As an example we consider the logistic map. The data are stored in a filecalled "timeev.dat". In the second part of the program we read the data back.

In JAVA the filename and the class name which includes the

public s t a t i c void main(String args[])

method must coincide (case sensitive). We output data using a DataOutputStreamthat is connected to a FileOutputStream via a technique called chaining of streamobjects. When the DataOutputStream object output is created, its constructoris supplied a FileOutputStream object as an argument. The statement createsa DataOutputStream object named output associated with the file timeev.dat.The argument "timeev.dat" is passed to the FileOutputStream constructor whichopens the file.

Class DataOutputStream. A data output stream lets an application write primi-tive (basic) Java data types to an output stream in a portable way.

Class FileOutputStream. A file output stream is an output stream for writingdata to File or a FileDescriptor.

The method

void writeDouble(double v)

converts the double argument to a long using the doubleToLongBits method inclass Double, and then writes that long value to the underlying output stream asan 8-byte quantity, high byte first. The method

double readDoubleO

reads eight input bytes and returns a double value.

// FileManipulation.java

import java.io.*;import java.lang.Exception;

public class FileManipulation

{public static void main(String args[])

{

DataOutputStream output;

try

{

Page 34: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 15

output = new DataOutputStream(new FileOutputStreamCtimeev.dat"));

int T = 10;double xO = 0.618; double xl;output.writeDouble(xO);System.out.printlnC'The output is " + xO);

for(int t=O;t<T;t++)

{xl = 4.0*x0*(1.0 - xO);System.out.printlnC'The output is " + xl);output.writeDouble(xl);xO = xl;

}try{ output.flushO; output.close(); }catch(IOException e){System.err.printlnC'File not closed properly\n" + e.toStringO);System.exit(l);}}catch(IOException e)

{System.err.printlnC'File not opened properly\n" + e.toStringO);System.exit(l);

}

System.out.println("\nReading file:");try{FilelnputStream fin = new FilelnputStreamC'timeev.dat");DatalnputStream in = new DataInputStream(fin);while(true) System.out.print(in.readDouble() + " " ) ;}

catch(Exception e) { }

}

}

1.1.2 Fixed Points and Stability

Consider a map / : [0,1] —» [0,1]. The fixed points are defined as the solutions ofthe equation

/(**) = x*.

Page 35: Nonlinear Workbook (3rd)

16 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

Let us assume that the map / is differentiable. Then the variational equation ofxt+i = f(xt) is defined as

Vt+\ = -rf(x + ty) = —(x = xt)yt.u£ „ ax

£=O,i=i(,j/=ye

A fixed point is called stable if

When we consider the logistic map / : [0,1] -> [0,1], f(x) = 4x(l - x) we have tosolve the quadratic equation

Ax*(1 - x*) = x*

to find the fixed points. Therefore the fixed points are given by x\ = 0, x\ = 3/4. Inthe C++ program we consider the stability of the fixed points for the logistic map

xt+i = Axt{l - xt).

We evaluate the variational equation of the logistic equation and determine thestability of the fixed points. For the logistic map we find

f=4-8*.ax

For the logistic map we find that the fixed points x\ = 0 and xi, = 3/4 are unstable.

In the following C++ program f ixpointlog.cpp we test whether the fixed pointsof the logistic map f(x) = 4a;(l — x) are unstable. We use the header file derive.hfrom SymbolicC++ to do the differentiation.

// fixpointlog.cpp

#include <iostream>#include <cmath> // for fabs#include "verylong.h"#include "rational.h"#include "derive.h"using namespace std;

int main(void)

{double xl = 0.0;double x2 = 3.0/4.0;Derive<double> Cl(l.O); // constant 1.0Derive<double> C4(4.0); // constant 4.0Derive<double> XI;

Page 36: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 17

Derive<double> X2;

Xl.set(xl);Derive<double> Rl = C4*X1*(C1 - XI);

double resultl = df(Rl);cout « "resultl = " « resultl « endl;if(fabs(resultl) > 1)cout « "fixpoint xl unstable " « endl;X2.set(x2);

Derive<double> R2 = C4*X2*(C1 - X2);double result2 = df(R2);cout « "result2 = " « result2 « endl;if(fabs(result2) > 1)cout « "fixpoint x2 unstable ";return 0;

}

1.1.3 Invariant DensityConsider a one-hump fully developed chaotic map / : [0,1] —>• [0,1]

xt+i = f{xt)

where t = 0,1,2,... . We define the invariant density (also called probability density)of the iterates, starting from an initial point Xg, by

p(x):= l imi £*(*-/«(a*))1 t=o

where f^{x0) = Xo and

fW{x0) = f(x0) =xu... , /«>(*„) = f{t-1](f(x0)) = f(f(t-1](x0)) = xt

with t > 1. Not all starting points XQ € [0,1] are allowed in the definition. Thosebelonging to an unstable cycle must be excluded since we are only interested in thestable chaotic trajectory. For any arbitrary (but integrable in the Lebesgue sense)function g on the unit interval [0,1] the mean value of that function along the chaotictrajectory is

1 T~1 f1

(9(x)) := lim 7f H 9(xt) = / p(x)g(x)dx .T->oo T J jJ JO

Choosing g(x) = 1 we obtain the normalization conditionf1

/ p(x)dx = 1.Jo

Since the probability density is independent of the starting point XQ, the expressionfor p can also be written as

1 T~1

p(x) = T l i m - ^ S(x - xt+k), k = 0,1, 2 , . . . .t=0

Page 37: Nonlinear Workbook (3rd)

18 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

An integral equation for p can be derived as follows: Let a be defined as

o{y):= fQ&{y-f{k){x))P{x)dx.

Let g be an arbitrary (but integrable in the sense of Lebesgue) function on [0,1].Then

f o{y)g{y)dy = / ' /* 5{y - fk\x))p{x)g{y)dydx.Jo Jo Jo

Therefore we obtain

fo a(y)g(y)dy = Um ± £ jf * S(x - / « (xo))g(fk\x))dx.

Using the properties of the delta function we arrive at

fo a(y)g(y)dy = lim i ^ ff(/(m)(^o)).

Hence

jo <r{y)g{y)dy = ^ - J2 Jo S(y ~ r 'M)g(y)dy = J p(y)g(y)dy.

Since the function g is arbitrarily chosen, we have to set a(y) = p{y). Thus theprobability density p obeys the integral equation

/•l

p(y) - J dx5(y - f(x))p(x) •

This equation is called the Frobenius-Perron integral equation. This equation hasmany solutions (Kluiving et al [53]). Among these are the solutions associatedwith the unstable periodic orbits. If these unstable solutions are left out of con-sideration and the map is one-hump fully developed chaotic, then there is only onestable chaotic trajectory exploring the unit interval [0,1] and the Frobenius-Perronequation has a unique solution associated with the chaotic orbit.

In the C + + program we determine numerically the invariant density for the logisticmap

xt+1 =ixt(l -xt).

For the stable chaotic trajectory exploring the unit interval [0,1] the Frobenius-Perron integral equation has the unique solution

p{x) = .2nyjx(l — x)

where/ p(x) dx = 1Jo

Page 38: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 19

and p(x) > 0 for x 6 [0,1]. We see that p(x) -t oo for x ->• 0 and a; -> 1, respectively.The solution can be found by iteration of

Pt+i{y) = Jo dxS{y - f{x))pt(x)

with the initial density Po(x) = 1.

In the C++ program invdensity.cpp we find the histogram for the logistic map.We divide the unit interval [0,1] into 20 bins with bin size 0.05 each. We calculatehow many points exist in the intervals [0.05 • i, 0.05• (i +1)), where i = 0,1,2, . . . , 19.This gives us an approximation for the invariant density given above. For example,the number of points in the intervals [0,0.05) and [0.95,1.0] is much higher than inthe other intervals (bins).

// invdensity.cpp

#include <iostream>#include <cmath> // for floor, sqrtusing namespace std;

void histogram(double* x.int* hist,double T,double xmax,double xmin.int n_bins)

•c

double grad = n_bins/(xmax - xmin);

for(int t=0;t<T;t++)

++hist[((int) floor(grad*(x[t]-xmin)))] ;

}

int main(void)

{int T = 10000; // number of iterationsdouble* x = new double[T];double xmax = 1.0; // length of interval xmax - xmindouble xmin =0.0;double bin_width = 0.05;

int n_bins = (int) (xmax - xmin)/bin_width;cout << "number of bins = " « n_bins « endl;

// generating the data for the histogramx[0] = (sqrt(5) - 1.0)/2.0; // initial valuefor(int t=0;t<(T-l);t++) x[t+l] = 4.0*x[t]*(l .0-x[t] ) ;

int* hist = new int[n_bins];// setting hist[i] to zero

Page 39: Nonlinear Workbook (3rd)

20 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

for(int i=0;i<n_bins;i++) h is t [ i ] = 0;

histogram(x,hist,T,xmax,xmin,n_bins);

for(int i=0;i<n_bins;i++)cout « "hist[" « i « "] = " « h is t [ i ] « endl;

delete[] x; delete[] his t ;return 0;

>

As a second example we consider the sine map. The sine map / : [0,1] —> [0,1] isdefined by

f(x) := sin(7ra;).

The map can also be written as a difference equation

xt+i = sin(7ra;t)

where XQ G [0,1]. The fixed points are determined by the solution of the equation

x* = sin(?ra;*).

The map admits two fixed points. One fixed point is given by x\ = 0. The otherfixed point is determined from x^ = sin^rr^) and x*2 > 0. We find

x\ = 0.73648... .

The variational equation of the sine map takes the form

yt+i = ncos{7rxt)yt.

Both fixed points are unstable. This can be seen by inserting x\ and x\ into thevariational equation.

To find the invariant density for the sine-map we replace in program invdensity. cppthe line

x[t+l] = 4.0*x[t]*(1.0-x[t]);

with

x[t+l] = sin(pi*x[t]) ;

and add const double pi = 3.14159; in front of this statement. The numericalresult suggests that the density for the sine map is quite similar to that of the logisticmap.

Page 40: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 21

Next we find the invariant density for the bungalow-tent map fa : [0,1] -> [0,1]

x for x G [0, a)

2a 1 - 3a r , ,- — 1 + - — for x G a, 1/2)

, / s 1 — 2a 1 — la

—— ( 1 - z ) for xe [ 1 - a , 1]

where a € (0,1/2). To find the invariant density exactly we solve the Frobenius-Perron integral equation. The Frobenius-Perron integral equation is given by

/•I

Pa(x) = Jo Pa(y)S(x - fa(y))dy.

We apply the identities for the delta function

S{cy) = FT 5{y)

and

6{g{y))^¥^n)\S{V~yn)

where the sum runs over all zeros with multiplicity 1 and g'(yn) denotes the derivativeof g taken at yn. Taking these identities into account and differentiating in the senseof generalized functions we obtain the invariant density

. , 1 , . 1 — 2<z . ,Pa{x) = J^-a^,i-a)(x) + ^3ajXil-a>l]{x)

where x 1S the indicator function, i.e.,

yAx)~lUixeAXA[X) - [Otix £ A •

Thus the invariant density is constant in the interval [0,1 — a). At 1 — a the invariantdensity jumps to another constant value. In our calculations we have to considertwo domains for x, [0,1 — a) and [1 — a, 1]. The Liapunov exponent is calculatedusing

A(a) = / o1 p a ( x ) l n ^ d i

where we differentiated in the sense of generalized functions. Thus we find that theLiapunov exponent as a smooth function of the control parameter a is given by

, . , 1 - a , / l - o \ l - 2 a , / 2a \A(fl) = 2-3^ l n ( — ) + 2^3alR [l^Ya) '

Page 41: Nonlinear Workbook (3rd)

22 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

For a = 1/3 we obviously obtain A(l/3) = In 2. This is the Liapunov exponent forthe tent map. For a —> 0 we obtain

A(a-s-0) = ^ l n 2 .

For a -» 1/2 we obtain X(a ->• 1/2) = 0. The Liapunov exponent A (a) has amaximum for a = 1/3 (tent map). Furthermore A (a) is a convex function in theinterval (0,1/2). We have A(o) < In2. The numerical simulation confirms the resultfor the invariant density, i.e. constant in the interval [0,1-a) and another constantin the interval [1 — a, 1]. In our numerical simulation we have to set one of the binsboundary points to 1 — a.

1.1.4 Liapunov Exponent

Here we calculate the Liapunov exponent A for one-dimensional chaotic maps. Con-sider the one-dimensional map

xt+l = f(xt)

where t = 0,1,2,. . . and xo € [0,1]. The variational equation (also called thelinearized equation) of this map takes the form

df, ,yt+i = fayxt)yt

with j/o 7 0. We assumed that / is differentiable. The Liapunov exponent A isdefined as

A(zo,yo) := l i m ^ l n — •

Here we calculate the Liapunov exponent for the logistic map xt+i = 4rrt(l — Xt).Thus f(x) = 4z(l - x) and

f=4-8*.ax

Consequently we obtain the variational equation

yt+i = (4 - 8xt)yt

with 1/0 ¥" 0- The exact solution of the logistic map is given byXt — cos(2* arccos(l — 2xQ)).

For almost all initial values we find that the Liapunov exponent is given by A = In 2.

In the C++ program we evaluate the Liapunov exponent by using the variationalequation. Overflow occurs if T is made too large. In an alternative method we usenearby trajectories and reset the distance between the two trajectories after eachtime step. Thus we avoid overflow for large T.

Page 42: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 23

/ / liapunovl.cpp

#include <iostream>#include <cmath> // for fabs, logusing namespace std;

int main(void)

{double x = 0.3; // initial value for logistic mapdouble y = 1.0; // initial value for variational mapdouble xl, yl;unsigned long T = 200; // number of iterations

for(unsigned long t=0;t<T;t++)

{

xl = x; yl = y;x = 4.0*xl*(1.0 - xl); // logistic mapy = (4.0 - 8.0*xl)*yl; // variational map}// notice that y becomes large very quicklydouble lambda = log(fabs(y))/((double) T); // Liapunov exponentcout « "lambda = " « lambda « endl;

// alternative methoddouble eps = 0.001;double xeps, xepsl;x = 0.3; xeps = x - eps;// x and xeps are nearby pointsdouble sum = 0.0;T = 1000;double distance;for(unsigned long t=O;t<T;t++)ixl = x; xepsl = xeps;x = 4.0*xl*(1.0 - xl);xeps = 4.0*xepsl*(1.0 - xepsl);double distance = fabs(x - xeps);sum += log(distance/eps);xeps = x - eps;}lambda = sum/((double) T);cout « "lambda = " « lambda « endl;return 0;

>

Page 43: Nonlinear Workbook (3rd)

24 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

In the following program we use the Rational, Verylong and Derive class ofSymbolicC++ to find an approximation of the Liapunov exponent. The Deriveclass provides the derivative. Thus the variational equation is obtained via exactdifferentiation

// Liapunov2.cpp// Iteration of logistic equation and variational equation

#include <iostream>#include <cmath> // for fabs, log

#include "verylong.h"#include "rational.h"#include "derive.h"using namespace std;

int main(void)

{int T = 100; // number of iterationsdouble x = 1.0/3.0; // initial valuedouble xl;double y = 1.0;

Derive<double> Cl(l.O); // constant 1.0Derive<double> C4(4.0); // constant 4.0Derive<double> X;

cout « "t = 0 x = " « x « " " « "y = " « y « endl;for(int t=l;t<=T;t++)

{xl = x;x = 4.0*xl*(1.0 - xl);X.set(xl);Derive<double> Y = C4*X*(C1 - X);y = df(Y)*y;cout « "t = " « t « " " « "x = " « x « " "

« "y = " « y « endl;>double lambda = log(fabs(y))/((double) T);cout « "Approximation value for lambda = " « lambda « endl;cout « endl;

int M = 9;Rational<Verylong> ul ;RationaKVerylong> u (" l " , "3" ) , v ( " l " ) ;RationaKVerylong> KlC'l");

Page 44: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 25

Rat i onal<Very1ong> K2("4");Derive<Rational<Verylong> > D1(K1); // constant 1Derive<RationaKVerylong> > D4(K2); // constant 4Derive<RationaKVerylong> > U;

cout « "j = 0 u = " « u « " " « "v = " « v « endl;for(int j=l;j<=M;j++)

{ul = u; u = K2*ul*(Kl - ul);U.set(RationaKVerylong>(ul));Derive<RationaKVerylong> > V = D4*U*(D1 - U);

v = df(V)*v;cout « "j = " « j « " "

« "u = " « u « " " « "v = " « v « endl;}lambda = log(fabs(double(v)))/((double) M);cout « "Approximation value for lambda = " « lambda « endl;return 0;

}

As a second example we consider the sine map. The sine map f : [0,1] ->• [0,1] isdefined by

f(x) := sin(7ra;).

The map can be written as the difference equation

xt+i = sin(?ra;t)

where i = 0,1,2,. . . and XQ S [0,1]. The variational equation of the sine equation isgiven by

Vt+i = -^{x = xt)yt = ircos(irxt)yt.

To find the Liapunov exponent for the sine-map we replace in program liap.cppthe two lines

x = 4.0*xl*(1.0 - x l ) ;xeps = 4.0*xepsl*(1.0 - xepsl);

by

x = s in(pi*xl) ;

xeps = sin(pi*xepsl);

and add

double pi = 3.14159;in front of this statement. For T = 5000 we find A = 0.689. Thus there is numericalevidence that the sine-map shows chaotic behaviour.

Page 45: Nonlinear Workbook (3rd)

26 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.1.5 Autocorrelation FunctionConsider a one-dimensional difference equation / : [0,1] -»• [0,1]

xt+i = f(xt)

where t = 0,1, 2, The time average is defined as

1 T~1

(xt) '•= lim — y^ x* •X U T->oo T ^

Obviously, (xt) depends on the initial value x0. The autocorrelation function isdefined as

1 T - 1

C J X M := lim - Ylixt - {xt))(xt+T - (xt))T ^ o o i (=0

where T = 0 , 1 , 2 , . . . . The autocorrelation function depends on the initial value x0-

For the logistic map / : [0,1] ->• [0,1], f{x) = 4x(l - x) we find that the timeaverage for almost all initial conditions is given by

The autocorrelation function is given by

r n _ / 8 for T = °^xx{T)- | 0 o t h e r w i s e

for almost all initial conditions. The C++ program autocorrelation.cpp calcu-lates the time average and autocorrelation function for the logistic map.

// autocorrelation.cpp

#include <iostream>using namespace std;

double average(double* x.int T)

{double sum =0.0;for(int t=0;t<T;t++) { sum += x[ t ] ; >double av = sum/((double) T);

return av;

>

void autocorr(double* x,double* CXX.int T.int length,double av)

{

fordnt tau=0;tau<length;tau++)

Page 46: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 27

{double C = 0.0;double diff = (double) (T-length);for(int t=0;t<diff;t++){ C += (x[t] - av)*(x[t+tau] - av); }CXX[tau] = C/(diff + 1.0);} / / end for loop tau

}

int main(void)

{const int T = 4096;double* x = new double[T];

x[0] = 1.0/3.0; // initial valuefor(int t=0;t<(T-l);t++)•C x[t+l] = 4.0*x[t]*(1.0 - x[t]); }double av = average(x,T);cout << "average value = " « av « endl;

int length = 11;double* CXX = new double[length];autocorr(x,CXX,T,length,av);delete[] x;

for(int tau=0;tau<length;tau++)cout « "CXX[" « tau « "] = " « CXX [tau] « endl;delete[] CXX;return 0;

>

The output is

average value = 0.497383CXX[O] = 0.125707CXX[1] = 0.00134996CXX[2] = -0.000105384CXX [3] = -0.000289099CXX[4] = 0.00477107CXX[5] = -0.00186259CXX[6] = 0.00383531CXX[7] = -0.00425356CXX[8] = -0.00288615CXX[9] = -0.00110183CXX[10] = -0.00148765

Page 47: Nonlinear Workbook (3rd)

28 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.1.6 Discrete Fourier TransformThe discrete Fourier transform is an approximation of the continuous Fourier trans-form. The discrete transform is used when a set of function sample values, x(t), areavailable at equally spaced time intervals numbered t = 0 , 1 , . . . ,T — 1. The dis-crete Fourier transform maps the given set of function values into a set of uniformlyspaced sine waves whose frequencies are numbered k = 0 , 1 , . . . , T - 1, and whoseamplitudes are given by

1 T~1 / t\f (k) = £ XW exP (~i2^k7f •

This equation can be written as

* ( * ) = | E *(*) c° s ( 2 ? r 4 ) " %T ? x { t ) sin ( 2 ? r 4 ) •The inverse discrete Fourier transformation is given by

T-l f k\*W = £ z(fc) exp H27rt- .

fc=0 \ X ITo find the inverse Fourier transformation we use the fact that

Eexpf^jO^W^k=o \ x I

where 8nm denotes the Kronecker symbol.

In our first C++ program (Fourier. cpp) we consider the time series

x(t) = cos(27ri/T)

where T — 8 and t = 0,1,2, . . . , T - 1. We find the discrete Fourier transform x(k)(k = 0,1,2, . . . , T - l ) . We have

^) = i£cOS(^)e-W.o t=Q \ o /

Using the identityei2irt/8 _|_ e-i2irt/8

COS(2TI-£/8) =

we find(jfc) = . 1 ^(e**rt(l-*)/8 + e-^(l+*)/8).

Consequently,r \ for A = 1

i(fc) = \ \ for fc = 7 .

[ 0 otherwise

Page 48: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 29

In our second program (fourierlog.cpp) we consider the logistic map

xt+i = 4zt(l - xt)

where t = 0,1,2,. . . and XQ £ [0,1]. We assume that we have a set of T samplesfrom the logistic map, i.e., XQ, X\, X%, . . . , XT-I-

II fourier.cpp

#include <iostream>

#include <cmath> // for cos, sinusing namespace std;

int main(void)iconst double pi = 3.14159;int T = 8;double* x = new double[T];

for(int t=0;t<T;t++) x[t] = cos(2.0*pi*t/((double) T));

double* rex = new double[T];double* imx = new doubled];

for(int k=0;k<T;k++)

idouble cossum =0.0, sinsum =0.0;for(int t=0;t<T;t++)

{cossum += x[t]*cos(2.0*pi*k*t/((double) T));sinsum += x[t]*sin(2.0*pi*k*t/((double) T));

}rex[k] = cossum/((double) T);imx00 = -sinsum/((double) T);

}

// display the outputfor(int k=0;k<T;k++){

cout « "rex[" « k « "] = " « rex[k] « " ";cout « "imx[" « k « "] = " « imx[k] « endl;>delete[] x; delete[] rex; deleted imx;return 0;

>

Page 49: Nonlinear Workbook (3rd)

30 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

II fourierlog.cpp

#include <iostream>#include <cmath> // for cos, sinusing namespace std;

int main(void){const double pi = 3.14159;int T = 256;double* x = new double[T];x[0] = 0.5;for (int t=0;t<(T-l);t++) x[t+l] = 4.0*x[t]*(l .0-x[t] ) ;

double* rex = new double[T];double* imx = new double[T];

for(int k=0;k<T;k++)

{double cossum = 0.0, sinsum = 0.0;for(int t=0;t<T;t++)

{cossum += x [ t ] * c o s ( 2 . 0 * p i * k * t / ( ( d o u b l e ) T ) ) ;

s i n s u m += x [ t ] * s i n ( 2 . 0 * p i * k * t / ( ( d o u b l e ) T ) ) ;

>

rex[k] = cossum/((double) T);imx[k] = -sinsum/((double) T);

}

/ / display the outputfor(int k=0;k<T;k++){cout « "rex[" « k « "] = " « rex[k] « " ";cout « "imx[" « k « "] = " « imx[k] « endl;}delete[] x; deleted rex; deleted imx;return 0;

}

1.1.7 Fast Fourier Transform

Let n > 1. The discrete Fourier transform transforms an n-vector with real compo-nents into a complex n-vector. Methods that compute the discrete Fourier trans-form in O(Nlog N) complex floating-point operations are referred to as fast Fouriertransforms , FFT for short. Based on the odd-even decomposition of a trigonomet-

Page 50: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 31

ric polynomial, a problem of size n = 2k is reduced to two problems of size 2k~1.Subsequently, two problems of size 2h~l are reduced to two problems of size 2k~2.Ultimately, n — 2k problems of size 1 are obtained, each of which is solved trivially.Let w be a primitive nth root of 1, i.e.

u = exp(2ni/n).

The matrix Fn denotes the n x n matrix with entries

hi •= w y

where 0 < i,j < n — 1. The discrete Fourier transform of the n-vector

P7'= (po,Pi,---,pn-i)

is the product FnP. The components of FnP are

(FnP)0 = UJ°Po + LJ°Pl + •••+ W°Pn-2 + W°pn_i

(FnP)i = LJ°Po + 0JpX + • • • + w " - 2 p n - 2 + W n " 1 pn-1

(FnP), = W°p0 + W'pi + • • • + Wi(B-2W-2 + OJ^-^Pn-x

(FnP^ = W°po + w"-1?! + • • • + a / - 1 " " - 2 ' ^ + w ^ ^ X - V - i •

Rewritten in a slightly different form, the ith component is

pn_1(wi)n"1 + Pn-2(ui)n~2 + • ' ' + PlW* + PO •

Thus if we interpret the components of P as coefficients of the polynomial

p(x) = Pn-lX71'1 + pn-2Xn-2 + • • • + PiX + p0

then the zth component is p(toz) and computing the discrete Fourier transform ofP means evaluating the polynomial p(x) at w°, w,w2,... ,un~1, i.e., at each of thenth roots of 1. We describe a Divide and Conquer algorithm first and then examineit closely to remove the recursion. We assume that n = 2k for some k > 0. Thestrategy of Divide and Conquer is to divide the problem into smaller instances,solve those, and use the solutions to get the solution for the current instance. Here,to evaluate p at n points, we evaluate two smaller polynomials at a subset of thepoints and then combine the results appropriately. Since uinl2 = — 1 we have for0 < ; < n/2 - 1,

w(»/2)+> = _^i _

We group the terms of p(x) with even powers and the terms with odd powers asfollows

n- l n/2-1 n/2-1

p{x) = Y, PiX1 = Y, P2«Z2i + X Y, P2i+lX2i •i=0 i=0 t=0

Page 51: Nonlinear Workbook (3rd)

32 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

We definen/2-1 n/2-1

Peven{x) •= J2 P*^ > Podd{x) '•= Ys P2i+lx% •i=0 i-0

Then

p(x) = Peven(x2) + X • podd(x2), P(-X) = peven{x2) - X • podd{x2) .

To evaluate p at

1, ui, . . . , u ^ - \ - 1 , - W , . . . . - o ; ^ 2 ) - 1

it suffices to evaluate peven and poa at

i, OJ2, ... Mn'2)'1)2

and then do n/2 multiplications (for x -podd{x2)) and n additions and subtractions.The polynomials peven and podd can be evaluated recursively by the same scheme.That is, they are polynomials of degree n/2 — 1 and will be evaluated at the n/2throots of unity

1, W2, . . . , ( W W2)-1 } 2 .

/ / ffty.cpp

#include <iostream>

#include <cmath> // for cos, sinusing namespace std;

void p(double wre,double wim,double *re,double *im,

double fefftre,double &fftim,const int M,int step.int init)

{double pre, pirn, w2re, w2im;if (step == (1«M)){fftre = re[init]*wre-im[init]*wim;fftim = im[init]*wre+re[init]*wim;return;>

w2re = wre*wre-wim*wim;w2im = 2.0*wre*wim;p(w2re,w2im,re,im,pre,pim,M,step«l,init); // pevenfftre = pre; fftim = pim;

p(w2re,w2im,re,im,pre,pim>M,step«l,init+step); // podd

fftre += wre*pre-wim*pim;

fftim += wre*pim+wim*pre;

}

Page 52: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 33

void fft(double *re,double *im,double *ftre,double *ftim,const int M)

{const double pi = 3.1415927;int N = 1 « M;double fftre, fftim, wre, wim, w2re, w2im;for(int i=0;i<(N»l) ;i++)

{wre = cos(i*2.0*pi/N); wim = sin(i*2.0*pi/N);w2re = wre*wre-wim*wim; w2im = 2.0*wre*wim;

p(w2re,w2im)re,im,fftre)fftim,MJ2,0); // peven

ftre[i] = ftre[i+(N»l)] = fftre;ftim[i] = ftim[i+(N»l)] = fftim;p(w2re,w2im,re,im,fftre,fftim,M,2,1); // poddftre[i] += wre*fftre-wim*fftim;ftre[i+(N»l)] -= wre*fftre-wim*fftim;ftim[i] += wre*fftim+wim*fftre;ftim[i+(N»l)] -= wre*fftim+wim*fftre;>

}

int main(void){const double pi = 3.1415927;const int M = 3;int T = 1 « M;double* re = new double[T];double* im = new double[T];

double* fftre = new double[T];double* fftim = new double[T];

for(int i=0;i<T;i++){ re[i] = cos(2.0*i*pi/T); }

for(int k=0;k<T;k++){ im[k] = 0.0; }fft(re,im,fftre,fftim,M);for(int k=0;k<T;k++)cout « "fftre [" « k « "]=" « fftre [k]/T « endl;cout « endl;for(int k=0;k<T;k++)cout « "fftim["«k«"]=" « fftim[k]/T « endl;delete[] re; delete[] im;delete[] fftre; delete[] fftim;return 0;

}

Page 53: Nonlinear Workbook (3rd)

34 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

A nonrecursive version is given below. We use in place substitution.

/ / FFT.cpp

#include <iostream>#include <cmath> // for sqrt, cosusing namespace std;

// dir = 1 gives the FFT tranform// dir = -1 gives the inverse FFT transform// n = 2~m is the length of the time series// x[] is the real part of the signal// y[] is the imaginary part of the signal

void FFT(int dir,unsigned long m,double* x,double* y)

{unsigned long n, i, il, j, k, i2, 1, 11, 12;double cl, c2, tx, ty, tl, t2, ul, u2, z;

// number of points n = 2"m

n = 1;for(i=0;i<m;i++) n *= 2;

// bit reversal

i2 = n » 1;

j = 0;for(i=0;i<n-l;i++)

{if(i < j)

{

tx = x[i] ; ty = y[i] ;x[i] = x[j]; y[i] = y[j];x[j] = tx; y[j] = ty;>k = i2;while(k <= j){j "= k;

k »= 1;

}

j += k;} // end for loop

// compute the FFTcl = -1.0; c2 = 0.0;

Page 54: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 35

12 = 1;for(l=0;Km;l++){11 = 12;12 «= 1;ul = 1.0; u2 = 0.0;for(j=0;j<ll;j++){for(i=j;i<n;i+=12)

ii l = i + 11;t l = ul*x[il] - u2*y[il]; t2 = ul*y[il] + u2*x[il];x[il] = x[i] - t l ; y[il] = y[i] - t2;x[i] += t l ; y[i] += t2;>z = ul*cl - u2*c2;u2 = ul*c2 + u2*cl;ul = z;>c2 = sqrt((1.0 - cl)/2.0);if(dir == 1) c2 = -c2;cl = sqrt((1.0 + cl)/2.0);}

if(dir == 1)

for(i=0;i<n;i++){x[i] /= n; y[i] /= n;}}

} / / end function FFT

unsigned long power(unsigned long m){

unsigned long r = 1;for(unsigned long i=0;i<m;i++) r *= 2;return r;

>

int main(void){unsigned long m = 3;const double pi = 3.14159;unsigned long n = power(m);

Page 55: Nonlinear Workbook (3rd)

36 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

double* x = new double[n];double* y = new double[n];

unsigned long k;for(k=0;k<n;k++){x[k] = cos(2.0*pi*k/n);y[k] = 0.0;>/ / call FFTFFT(l,m,x,y);for(k=0;k<n;k++){ cout « x[k] « " " « y[k] « endl; }

cout « "calling inverse FFT" « endl;// call inverse FFTFFT(-l,m,x,y);for(k=0;k<n;k++){ cout « x[k] « " " « y[k] « endl; }

return 0;

}

1.1.8 Logistic Map and Liapunov Exponent for r 6 [3,4]We consider the logistic map

xt+x = rxt(l - xt)

where t = 0 ,1 ,2 , . . . , XQ € [0,1] and r S [3,4]. Here r is the bifurcation parameter.Thus the Liapunov exponent depends on r. We evaluate the Liapunov exponent forr 6 [3,4]. The variational equation is given by

Vt+i = r(l - 2xt)yt.

The Liapunov exponent is defined as

A(xo,2/o) := lim - I n — .T->oo 1 J/o

The point r = 3 is a bifurcation point. The Liapunov exponent is given by A = 0.In the range 3 < r < 3.5699 we find periodic solutions. The Liapunov exponentis negative. We also find period doubling. In the region

3.5699... < r < 4

we find chaotic behaviour (positive Liapunov exponent) but also periodic windows.For example in the region

3.828... < r < 3.842...

Page 56: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 37

we have a trajectory with period 3. The Liapunov exponent can be evaluated ex-actly only for r = 4. One finds \(r = 4) = In 2 for almost all initial values.

In the program lambdaf. cpp the Liapunov exponent is evaluated for the intervalr € [3.0,4.0] with step size 0.001.

// lambdaf.cpp

#include <fstream>#include <cmath> // for fabs, logusing namespace std;

int main(void){ofstream data("lambda.dat");int T = 10000; // number of iterationsdouble x = 0.618; // initial value

double xl;double eps = 0.0005;double xeps = x - eps;double xepsl;double r = 3.0;double sum = 0.0;

while(r <= 4.0){for(int t=0;t<T;t++){.xl = x; xepsl = xeps;x = r*xl*(1.0 - xl);xeps = r*xepsl*(1.0 - xepsl);double distance = fabs(x - xeps);sum += log(distance/eps);xeps = x - eps;>double lambda = sum/((double) T);data « r « " " « lambda « "\n";sum =0.0;r += 0.001;} // end whiledata.closeO ;return 0;

}

Page 57: Nonlinear Workbook (3rd)

38 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.1.9 Logistic Map and Bifurcation DiagramWe consider the logistic map

xt+i = rxt{\ - xt)

where r G [2,4] and Xo G [0,1], Here r is a bifurcation parameter. We now studythe bifurcation diagram. For r € [2,3) the fixed point x* = 1 - 1/r is stable. Thefixed point x* = 0 is unstable in the range (2,4]. For r = 3 (bifurcation point) thestable fixed point x* = 1 — 1/r becomes unstable. We find a stable orbit of period2. With increasing r we find a period doubling process with repeated bifurcationfrom

2 4 8 2n

z, t, o, . . . , i, , . . . .

There is a threshold valuer-oo = 3.5699...

for the parameter r where the limit 2n, n —> oo of the periodicity is reached. Forr = 4 the logistic map and all its iterates are ergodic and mixing. Within theinterval

(r«x,,4)

period triplings p3n and quadruplings p4n etc. also occur (so-called periodic win-dows) .

In the Java program Bifurcationlo. Java we display the bifurcation diagram forthe interval r € [2.0,4.0].

// Bifurcationlo.Java

import j ava.awt.*;import j ava.awt.Frame;import java.awt.event.*;import java.awt.Graphics;

public class Bifurcationlo extends Frame

{public Bifurcationlo()

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); >});

}

public void paint(Graphics g)

{int xmax = 600; int ymax = 400;

Page 58: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 39

int j, k, m, n;

double x, xplot, yplot;double r = 2.0; // bifurcation parameterwhile(r <= 4.0)

{xplot = xmax*(r - 2.0)/2.0;x = 0.5;for(j=0;j<400;j++) { x = r*x*(1.0 - x); >for(k=0;k<400;k++){x = r*x*(1.0 - x);yplot = ymax*(1.0 - x);m = (int) Math.round(xplot);n = 50 + (int) Math.round(yplot);g.drawLine(m,n,m,n);

}r += 0.0005;} // end while

}

public static void main(String[] args)

•C

Frame f = new BifurcationloO ;f.setVisible(true);>

}

1.1.10 Random Number Map and Invariant DensityWe consider methods for generating a sequence of random fractions, i.e., randomreal numbers ut, uniformly distributed between zero and one. Since a computer canrepresent a real number with only finite accuracy, we shall actually be generatingintegers Xt between zero and some number m. The fraction

ut = xt/m, t = 0 , 1 , 2 , . . .

will then lie between zero and one. Usually m is the word size of the computer,so xt may be regarded as the integer contents of a computer word with the radixpoint assumed at the extreme right, and ut may be regarded as the contents of thesame word with the radix point assumed at the extreme left. The most popularrandom number generators are special cases of the following scheme. We select fournumbers:

m, the modulus; m > 0a, the multiplier; 0 < a < mc, the increment; 0 < c < mSo, the initial value; 0 < Xo < m.

Page 59: Nonlinear Workbook (3rd)

40 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

The desired sequence of pseudo-random numbers xo,Xi,x2,... is then obtained bythe one-dimensional difference equation

xt+i = (axt + c) mod m, t — 0 , 1 , 2 , . . . .

This is also called a linear congruential sequence. Taking the remainder mod m issomewhat like determining where a ball will land in a spinning roulette wheel. Forexample, the sequence obtained when m = 10 and x0 = a = c = 7 is

7, 6, 9, 0, 7, 6, 9, 0, . . . .

This example shows that the sequence is not always "random" for all choices of m, a,c, and xo. The example illustrates the fact that congruential sequences always "getinto a loop"; i.e., there is ultimately a cycle of numbers which is repeated endlessly.The repeating cycle is called the period. The sequence given above has a period oflength 4. A useful sequence will of course have a relatively long period. In our firstC++ program we implement a linear congruential sequence. In our second C++program we consider the sequence

xt+i = (?r + xt)5 mod 1 = frac(?r + xt)

5

and ask whether the sequence is uniformly distributed.

/ / modulus.cpp

#include <iostream>using namespace std;

int main(void)

{unsigned long a = 7; unsigned long c = 7;unsigned long m = 10; // modulusunsigned long xO = 7; // initial valueunsigned long xl;

int T = 10; // number of iterationsfor(int t=O;t<T;t++)

{xl = a*x0 + c;while(xl >= m) xl = xl - m;

xO = xl;cout « "x[" « t « "] = " « xO « endl;

}

a = 3125; c = 47;m = 2048; // modulusxO = 3; // initial value

Page 60: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 41

T = 12; // number of iterationsfor(int t=0;t<T;t++){xl = a*xO + c;while(xl >= m) xl = xl - m;xO = xl;cout « "x[" « t « "] = " « xO « endl;>return 0;

>

// randoml.cpp

#include <iostream>#include <cmath> // for sqrt, fmodusing namespace std;

int main(void){const double pi = 3.14159;int T = 6000; // number of iterationsdouble* x = new double[T];

x[0] = (sqrt(5.0)-1.0)/2.0; // initial valuefor(int t=O;t<(T-l);t++)

{double r = x[t]+pi;x[t+l] = fmod(r*r*r*r*r,l);>

const int N = 10;double hist[N];

for(int j=O;j<N;j++) hist[j] = 0.0;

fordnt k=0;k<T;k++)hist[(int) floor(N*x[k])] = hist[(int) f loor(N*x[k])] + 1;

fordnt l=0;KN;l++)cout « "hist[" « 1 « "] = " « hist[l] « endl;

return 0;

Page 61: Nonlinear Workbook (3rd)

42 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.1.11 Random Number Map and Random IntegrationWe describe the Monte Carlo method for the calculation of integrals. We demon-strate the technique on one-dimensional integrals. Let / : [0,1] -> [0,1] be a con-tinuous function. Consider the integral

1 = fj{x)dx.

We choose N number pairs (XJ, yj) with uniform distribution and define zt by

m_jOiiyj>f(xj)Zi- \lifVj-</(!,•).

Putting

i

we have n/N ~ /. More precisely, we find

I = n/N + O{N~1/2).

The accuracy here is not very good. The traditional formulas, such as Simpson'sformula, are much better. However, in higher dimensions the Monte Carlo techniqueis extremely favourable, at least if the number of dimensions is > 6. We considerthe integral as the mean value of /(£) where £ is uniform. An estimate of the meanvalue is

This formula can easily be generalized to higher dimensions.

In the C++ program we use the map

f(x) = {x + TT)5 mod 1

as random number generator and evaluate

f1 sm{x)dx = 0.459697694132.Jo

II randint.cpp

#include <iostream>#include <cmath>using namespace std;

void randval(double* x,double pi)

{*x = fmod((*x+pi)*(*x+pi)*(*x+pi)*(*x+pi)*(*x+pi),1);

Page 62: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 43

>

int main(void)•C

const double pi = 3.14159;unsigned long T = 20000; // number of iterationsdouble x = 0.5; // initial valuedouble sum = 0.0;

for(int t=0;t<T;t++)

•C

randval(&x,pi);

sum += sin(x);

>cout « "The integral is = " « sum/((double) T);return 0;

>

In the Java program we use the same map as in the C++ progam for the randomnumber generator.

// Randoml.Java

class WrappedDouble

{WrappedDouble(final double value) { this.value = value; }public double value() { return value; }public void value(final double newValue) { value = newValue; }private double value;

>

class MathUtils

ipublic static void randval (WrappedDouble x)

{double y = Math.pow(x.value()+Math.PI,5);x.value(y-Math.floor(y));

}

>

class Randoml

{public static void main(String[] args){int n = 20000;

Page 63: Nonlinear Workbook (3rd)

44 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

double sum = 0.0;

WrappedDouble x = new WrappedDouble(0.5);

for(int i=0;i<n;++i){MathUtils.randval(x);sum += Math.sin(x.value());}System.out.println("The integral is " + sum/n);>

}

1.1.12 Circle Map and Rotation NumberThe circle map is given by

xt+1 = f{xt) H xt + ft - —- sin(27T2;()ATT

which may be regarded as a transformation of the phase of one oscillator througha period of the second one. The map depends on two bifurcation parameters: ftdescribes the ratio of undisturbed frequencies while the bifurcation parameter rgoverns the strength of the nonlinear interaction. The subcritical (r < 1) mappingsare diffeomorphisms (and thus invertible) whereas the supercritical ones (r > 1) arenon-invertible and may exhibit chaotic behaviour. The borderline between these twocases consists of the critical circle mappings - homeomorphisms with one (usuallycubic) inflection point. This corresponds to r = 1 in the family of this map. Thedynamics of the map may be characterized by the rotation number (also calledwinding number)

p:=\imo^T\x)-x).

When / is invertible, the rotation number is well defined and independent of x. f~x

does not exist for r > 1. For subcritical and critical maps this number does notdepend on the initial point x. The dependence p(ft) is the so-called devil's staircase,in which each rational p = p/q is represented by an interval of ft values (which isnamed the p/g-locking interval). The set of all these intervals has a full measure inthe critical case. The locked motion in subcritical and critical cases is representedby a stable periodic orbit of period q. The rotation number is the mean number ofrotations per iteration, i.e., the frequency of the underlying dynamical system. Ifr = 0 we obviously find p = ft. Under iteration the variable xt may converge to aseries which is either periodic,

Xi+Q = Xt+P

with rational rotation number p = P/Q; quasiperiodic, with irrational rotationnumber p = q; or chaotic where the sequence behaves irregularly.

Page 64: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 45

/ / circle.cpp

#include <fstream>#include <cmath> // for sinusing namespace std;

int main(void)

{ofstream dataCcircle.dat");

int T = 8000; // number of iterationsconst double pi = 3.14159;double r = 1.0; // parameter of mapdouble Omega = 0.0;

while(Omega <= 1.0)

idouble x = 0.3; // inital valuedouble xO = x;double xl;

for(int t=O;t<T;t++)

{xl = x;x = xl + Omega - r*sin(2.0*pi*xl)/(2.0*pi); // circle map

}double rho = (x - x0)/((double) T);data « Omega « " " « rho « "\n";Omega += 0.005;} // end while

data.close();return 0;

}

1.1.13 Newton-Raphson Method

Consider the equation f(x) = 0 where it is assumed that / : R —> R is at leasttwice differentiable. Let I be some interval containing a root of / . A root is a pointx such that f(x) = 0. We assume that the root is simple (also called multiplicityone). The Newton-Raphson method can be derived by taking the tangent line to thecurve y = f(x) at the point (xt, f(xt)) corresponding to the current estimate, xt, ofthe root. The intersection of this line with the x-axis gives the next estimate to theroot, xt+i- The gradient of the curve y = f(x) at the point (xt, f{xt)) is f'{xtj- Thetangent line at this point has the form y = f'{x)x + b. Since this passes through

Page 65: Nonlinear Workbook (3rd)

46 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

(xt, f{xt)) we see that b = f(xt) - xtf'(xt). Therefore the tangent line is

y = f'(xt)x + f(xt)-xtf'(xt).

To determine where this line cuts the rc-axis we set y = 0. Taking this point ofintersection as the next estimate, xt+\, to the root we have

0 = f(xt)xt+1+f(xt)-xtf'(xt).We obtain the first order difference equation

* > « = * . - $ • $ . * = 0 , l , 2 , . . . .

This is the Newton-Raphson method. This scheme has the form 'next estimate =current estimate + correction term'. The correction term is —f(xt)/f'(xt) and thismust be small when xt is close to the root if convergence is to be achieved. This willdepend on the behaviour of f'(x) near the root and, in particular, difficulty will beencountered when f'(x) and f(x) have roots close together. The Newton-Raphsonmethod is of the form xt+i = g(xt) with

a(x)-x f{x)

The order of the method can be examined. Differentiating this equation leads to

ql{x) _ /(*)/*(*)

For convergence we require that

(/'(*))2

for all x in some interval / containing the root. Since f(x) — 0, the above conditionis satisfied at the root x = x provided that f'(x) ^ 0. Then provided that g(x)is continuous, an interval I must exist in the neighbourhood of the root and overwhich the condition above is satisfied. Difficulty is sometimes encountered when theinterval I is small, because the initial guess must be taken from this interval. Thisusually arises when f(x) and f'(x) have roots close together, since the correctionterm is inversely proportional to f'(x).

In our C++ program Newton, cpp we consider the function

f(x) = x - sin(7ra;)

in the interval [0.5,1]. This means we find the fixed point of the sine-map

xt+i = sin(7ra;()

in the interval [0.5,1]. We have

f'(x) = 1 - 7rcos(7r:r), f"{x) = 7r2sin(7nr).

Thus the condition for convergence is satisfied.

Page 66: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 47

/ / Newton.cpp

#include <iostream>#include <cmath> // for sin, cos, fabsusing namespace std;

double f(double x){const double pi = 3.14159;return x - sin(pi*x);

}

double fder(double x) // derivative of f{const double pi = 3.14159;return 1.0 - pi*cos(pi*x);

}

double newtonmeth(double initial,double eps)

{double xO, xl;xl = initial;do{xO = xl; xl = xO - f(x0)/fder(x0);

}while(fabs(xl - xO) > eps);

return xO;

>

int main(void)

{double initial = 0.5;double eps = 0.0001;double result = newtonmeth(initial,eps);cout << "result = " « result « endl;

return 0;

>

1.1.14 Feigenbaum's Constant

In a number of mappings which depend on a bifurcation parameter r we find a pe-riod doubling cascade. We consider the bifurcation parameter values where period-doubling events occur. The limit of the ratio of distances between consecutive

Page 67: Nonlinear Workbook (3rd)

48 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

doubling values is Feigenbaum 's constant. It has the value

4.669201609102990671853...

Mappings which show this transition are

xt+1 = rxt{\ - xt), xt+i = 1 - rx\, xt+1 = x\ + r .

The C++ program f eigenbauml. cpp finds the Feigenbaum constant using the equa-tion xt+i = x\ + r. The program shows the constant computed for two doublingcascades. The first one starts with the period 1 cardioid and the second starts withthe period 3 cardioid. Newton's method is used to find the root of a; — x2 + riterated n times.

// feigenbauml.cpp

#include <stdio.h>#include <stdlib.h>#include <math.h> // for fabs

double newtondong n,double c)

idouble x, xl;double nc = c;double absx = 1.0;long i, j ;j = 0;while((j < 7) && (absx > 1E-13))

x = 0.0; xl = 0.0;

for(i=0;i<n;i++)

{xl = 2.0*xl*x + 1.0; x = x*x + nc;}nc -= x/xl; absx = fabs(x);>

return nc;

}

void go(long nO,double a.double b)

{double f = 4.0;double tmp = a;double newc = a + (a-b)/f;double oldc = b;

Page 68: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 49

long n = 2*nO;for(int i=0;i<10;++i){newc = newton(n,newc);f = (tmp - oldc)/(newc - tmp);

printf('7..161f */..161f */..161f */.. 161f\n" ,oldc,tmp,newc,f);oldc = tmp;tmp = newc;

newc += (newc - oldc)/f;n *= 2;>

}

int main(void)

{double a, b;long n;printfO'cl c2 c3");printfC f: (c2-cl)/(c3-c2) " ) ;

printf("\n");b = 0.0; a = -1.0;n = 2;go(n,a,b);printf("\n");a = -1.7728929033816238;b = -1.75487766624669276;n = 6;go(n,a,b);return 0;

}

1.1.15 Symbolic Dynamics

Consider a one-dimensional nonlinear map f(x,r), which maps points from an in-terval / into the same interval

x t + i = f ( x t , r ) , x t e l

where r is a control parameter. The function / may have several monotone branches,divided by turning points, denoted symbolically by Cj (called also critical points).For smooth maps the derivative df/dx vanishes at x = Cj. The turning points C;and the end points of the interval / divide / into subintervals /;. We label each ofIi by a symbol Si. By iterating the map, we obtain a numerical sequence

X0, X1=f{x0), X2 = }{xl), . . . , Xt-1 = f(xt-2), Xt = f{xt_l)

Page 69: Nonlinear Workbook (3rd)

50 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

We juxtapose the numerical sequence with a symbolic sequence and call it by thenumber xo which has originated the numerical sequence

XQ = GQOI<T2 • • • <?n-l&n • • •

where o-j stands for one of the symbols Sj or Cj, depending on whether xt belongsto the corresponding subinterval or coincides with a turning point. If we want toreverse the numerical sequence, expressing x0 through xt, we must indicate whichof the monotone branches of / has been used at each iteration. To do so, we attacha subscript a to / . The symbol a is chosen by the argument of /

X0, X i = f a o ( x 0 ) , X 2 = f a i ( x 1 ) , . . . , Xt = fat-! fe-l), ••••

Now we are in a position to reverse this sequence. We obtain

So = / - 1 o / - 1 o - - - o / - i i ( a ; t ) .

To simplify the notation, we denote each inverse monotone branch f~l by its sub-script, i.e. we define

°{v) = fa\v)

where a is one of the symbols S;. For example, the logistic map

xt+1 = l-rx2t, i t G [-1,1], rG (0,2)

has two inverse branches

R(y) = y/(l-y)/r, L(y) = -J(i-y)/r.

Now we findx0 = a0 o o\ o • • • o at-i(xt).

Thus we found the number-symbol-inverse function correspondence.

The logistic map given above f(x, r) = 1 — rx2 is a unimodal map. Since df/dx =—2rx we find that C = 0 is the only critical point. The iterate of the critical pointC = 0 leads to the rightmost point f(C) on the interval that one can ever reachby iterating the map from any point on the interval. The point f(C) thus startsa special symbolic sequence, called the kneading sequence. The kneading sequenceis named f(C) and sometimes denoted by K (for kneading). For instance, at theparameter value r = 1.85 the logistic map has a kneading sequence

K = f(C) = RLLRLRLRRL • • •

The second iterate of C i.e. f^(C), gives the leftmost point that one can everreach by iterating the map twice starting from any point on the interval. All theinteresting dynamics takes place on the subinterval

lf{2)(C),f(C)}

Page 70: Nonlinear Workbook (3rd)

1.1. ONE-DIMENSIONAL MAPS 51

of / . Once a point is in this subinterval, its iterates can never get out. Therefore,this subinterval defines an invariant dynamical range. In principle, one can choosean initial point outside this subinterval, but after a trivial transient (in fact, a fewiterations), it will fall into the invariant dynamical range. For maps with multiplecritical points each C; leads to a kneading sequence; one collects the dynamicalrange of all turning points and finds the overall range of interest dynamics. Ingeneral one concentrates on the invariant dynamical range only, neglecting trivialtransients. Each kneading sequence is represented by a number. This number maybe taken as the parameter for the map. This happens to be very convenient formaps with multiple critical points. In other words, one can parameterize a map byits independently changing kneading sequences. In the C++ program we find thekneading sequence for the logistic map with r = 17/20.

// kneading.cpp

#include "rational.h"#include "verylong.h"using namespace std;

int main(void)

{int n = 12;char* symstr = new char[n];

RationaKVerylong> one("l" , "1"), zeroO'O" , "1") ;Rational<Verylong> r("37","20"); // control parameterRational<Verylong> x = zero; // initial value 0

if(x < zero) symstr[0] = 'L';if(x == zero) symstr[0] = 'C;if(x > zero) symstr[0] = 'R';

for(int i=l;i<(n-l);i++)

{x = one - r*x*x;if(x < zero) symstr[i] = 'L' ;if(x == zero) symstr[i] = 'C;if(x > zero) symstr[i] = 'R';>cout << "symbolic sequence = " « symstr;delete[] symstr;return 0;

}

The output is given by symbolic sequence = CRLLRLRLRRL.

Page 71: Nonlinear Workbook (3rd)

52 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.1.16 Chaotic RepellerConsider the logistic map

xt+1 = f(xt) = rxt(l - xt)

where r > 4.0, for example r = 4.1. Thus, for example, if x0 = 0.5, then /(x0) =1.025 > 1.0 and f{f(x0)) = -0.1050625. We find that /(">(zo) tends to -oo ifn —\ oo. Letting s = r/4 — 1 the map has a gap of size \Js/{l + s). In this gapf(x) > 1.0. Initial conditions chosen from this gap maps out of the unit interval[0,1] in one iteration and goes to —oo. Almost all initial conditions in the unitinterval eventually escape from it except for a set of Lebesgue measure zero. Thisset, by construction, is a fractal Cantor set. A chaotic repeller is a set of points onthe attractor that never visit the gap. The chaotic repeller can be used for encodingdigital information (Lai [57]).

// Repeller.cpp

ttinclude <iostream>using namespace std;

int main(void)

{int count = 0;double xO = 1.0/3.0;double xl;

do

{xl = 4.1*xO*(l.O-xO);count++;xO = xl;} while(xO <= 1.0);cout « "xO = " « xO « endl;cout « "count = " « count « endl;return 0;

>

Page 72: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 53

1.2 Two-Dimensional Maps

1.2.1 Introduction

Most of the two-dimensional maps (Arrowsmith [2], Steeb [85], [86]) we consider inthis section are diffeomorphisms. Let U be an open subset of Rn and f : U —> R™ bea nonlinear diffeomorphism with an isolated fixed point at x* 6 U. The linearizationof f at x* is given by the n x n matrix

where x\,...,xn are coordinates on U.

Definition. A fixed point x* of a diffeomorphism f is said to be hyperbolic if themap Di(x*) is a hyperbolic, linear diffeomorphism.

Definition. A linear diffeomorphism A : R" —> R" is said to be hyperbolic if it hasno eigenvalues with modulus equal to unity.

The following theorems allow us to obtain valuable information from Df(x*).

Theorem. (Hartman-Grobman) Let x* be a hyperbolic fixed point of the dif-feomorphism f : U —> R™. Then there is a neighbourhood N C U of x* and aneighbourhood N' C R" containing the origin such that f jiV is topologically conju-gate to Df{x*)\N'.

It follows that there are An topological types of hyperbolic fixed point for diffeomor-phisms f : U -» R".

Theorem. (Invariant Manifold) Let f : U —¥ W be a diffeomorphism witha hyperbolic fixed point at x* G U. Then on a sufficiently small neighbourhoodN C U of x*, there exist local stable and unstable manifolds,

wioc(**) := { x € C/ | fw(x) -> x* as t -> oo }

W?oc(x*) := { x € U | f(i)(x) H> x* as t ->• -oo }

of the same dimensions as Es and Eu for Df(x*) and tangent to them at x*.

This theorem allows us to define global stable and unstable manifolds at x* by

Ws(x*):= | J f(-m>(W70C(x'))meZ+

TV"(x*):= U f(m)TCc(x*)).mgZ+

Page 73: Nonlinear Workbook (3rd)

54 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

The behaviour of the stable and unstable manifold Ws(x*) and Wu(x*) reflects inthe complexity of the dynamics of the map f. In particular, if W3(x*) and W"(x*)meet transversely at one point, they must do so infinitely many times and a homo-clinic tangle results. The theorems given above have an extension to the periodicpoint of f. Let x* belong to a g-cycle of f then it is said to be a hyperbolic periodicpoint of f if it is a hyperbolic fixed point of the q-th iterated map f . The orbitof x* under f is referred to as a hyperbolic periodic orbit and its topological type isdetermined by that of the corresponding fixed point of f'?'. Moreover, informationabout stable and unstable manifolds at each point of the (/-cycle can be obtained byapplying the theorem to f(?'.

The Hopf bifurcation theorem for maps in the plane f : R2 —> R2 is as follows.

Theorem. (Hopf bifurcation theorem) Let f (r, x) be a one-parameter familyof maps in the plane satisfying:a) f(r, x) = 0 for r near 0.b) Df(r, 0) has two nonreal eigenvalues \(r) and A(r) for r near 0 with |A(0)| = 1c)

d\X(r)\ > Q

d) A = A(0) is not a q-th. root of unity for q — 1,2,3 or 4.

Then there is a smooth r-dependent change of coordinates such that

f ( r ,x)=g(r ,x) + O(|x|5)

where, in polar coordinates (R,0),

g(r, R, 9) = (\X(r)\R + c3(r)R3,6 + p{r) + d2(r)R2)

and c3, d2, /3 are smooth functions of r. If 03(0) < 0 (03(0) > 0) then, for r < 0(r > 0), the origin is stable (unstable) and, for r > 0 (r < 0), the origin is un-stable (stable) and surrounded by an attracting (repelling) invariant circle. Whenc3(0) < 0 (c3(0) > 0) the bifurcation at r — 0 is said to be supercritical (subcritical).

We consider the Henon map, the Lozi map, standard map, the Ikeda laser map anda coupled logistic map.

1.2.2 Phase Portrait

For a two-dimensional map

xit+i = h{xu, 3>2t), x2t+i = h&u, x2t)

where t = 0 , 1 , 2 , . . . we can plot the points (xu, x2t) for t = 0 ,1 , 2 , . . . in the (11,2:2)plane R2. This is called a phase portrait. We also use the notation x = xx and

Page 74: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 55

y = X2- As our first example we consider the Henon map f : R2 —> R2 which isgiven by

i{x, y):=(y + l - ax2, bx)

where a and b are bifurcation parameters with 6 ^ 0 . The Henon map is the moststudied two-dimensional map with chaotic behaviour. The map can also be writtenas a system of difference equations

xt+i = l + yt-ax2, yt+i = bxt

where t = 0,1,2, The map is invertible if 6 0. The inverse map is given by

1 a 2xt = -j^yt+u Vt = xt+i - 1 + -jpVt+i •

In order to visualize the action of the map, note that vertical lines are mapped tohorizontal lines, while for a > 0 horizontal lines map to parabolas opening to the left.

In the C++ program henon.cpp we evaluate the phase portrait (xt,yt) for a — 1.4and b = 0.3. The data are written to a file named henon.dat. Then we use GNUplot to display the phase portrait. In the Java program Henon. Java the graphics isincluded and the phase portrait is displayed.

/ / henon.cpp

#include <fstream>

using namespace std;

int main(void)-Cofstream dataChenon.dat");const int T = 2000; // number of iterationsdouble xO = 0.1, yO = 0.3; // initial valuesdouble xl, yl;

for(int t=0;t<T;t++){xl = 1.0 + yO - 1.4*xO*xO; yl = 0.3*x0;data « xl « " " « yl « "\n";xO = xl; yO = yl;>data.close();return 0;

>

The data in the file "henon.dat" can now be used to display the phase portraitusing GNU-plot. The command is

Page 75: Nonlinear Workbook (3rd)

56 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

plot 'henon.dat' with dots

/ / Henon.java

import java.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class Henon extends Frame{public Henon()isetSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});>

public void paint(Graphics g){double xl, yl;double x = 0.0, y = 0 . 0 ; // initial values

int T = 4000; // number of iterationsfor(int t=0;t<T;t++){xl = x; yl = y;x = 1.0 + yl - 1.4*xl*xl; y = 0.3*xl;int mx = (int) Math.floor(200*x + 250 + 0.5);int ny = (int) Math.floor(200*y + 150 + 0.5);g. drawLine (mx, ny, mx, ny);}}

public static void main(String[] args)

•C

Frame f = new Henon();f.setVisible(true);>

>

The Lozi map f : R2 —> R2 is a piecewise linear map which has been introduced tosimplify the Henon map. It is given by

{{x,y) - (1 + y - a\x\, bx)

Page 76: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 57

where b > 0. It can be shown that if

fee (0,1), a > 0 , 2a + 6 < 4 , b < ^——|-, a \ / 2 > 6 + 2

then there is a hyperbolic fixed point H of saddle type given by

such that the strange attractor is the closure of their unstable manifold.

In the Java program for the Henon model Henon. java we replace the line

x = 1.0 + yl - 1.4*xl*xl; y = 0.3*xl;

by

x = 1.0 + yl - a*Math.fabs(xl); y = b*xl;

The parameter values for the Lozi map are a = 1.7, b = 0.4. For these parametervalues the system shows a strange attractor. For these values the conditions givenabove are satisfied.

The following C++ program finds the largest and smallest values in the x and ydirection of the Henon attractor for the parameter values a = 1.4 and 6 = 0.3.

/ / maxmin.cpp

#include <iostream>using namespace std;

void maxminxy(double* x,double* y,doublet xmax,doubleft xmin,doublefe ymax,double& ymin.int T)

{xmax = x [0] ; xmin = x [0] ;ymax = y [0] ; ymin = y [0] ;for( in t t=l;t<T;t++){i f (x [ t ] < xmin) xmin = x[ t ] ;if (x [t] > xmax) xmax = x [t] ;if (y [t] < ymin) ymin = y [t] ;i f (y [ t ] > ymax) ymax = y [ t ] ;}

>

Page 77: Nonlinear Workbook (3rd)

58 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

int main(void){

unsigned long T = 10000; / / number of iterationsdouble* x = new double[T];double* y = new double[T];

x[0] = 1.161094; y[0] = -0.09541356; / / init ial valuesfor(int t=0;t<(T-l);t++){x[t+l] = 1.0 + y[t] - 1.4*x[t]*x[t];y[t+l] = 0.3*x[t];}double xmax, xmin, ymax, ymin;maxminxy(x,y,xmax,xmin,ymax,ymin,T);cout « "xmax = " « xmax « endl;cout « "xmin = " « xmin « endl;cout « "ymax = " « ymax « endl;cout « "ymin = " « ymin « endl;delete [] x; delete [] y;return 0;

}

The standard map is defined as

It+i =It + ksm(9t)9t+l =et + It + k sin{9t) = et + It+i

where 0 < 9 < 2TT. The quantities I, 9 are the action-angle variables. It can bederived from the Hamilton function

H(Pe, 9) = V\ + k cos 9 £ 5{t - n)

of a one-dimensional periodically kicked rotor. The standard map can be consideredas a discrete Hamilton system. We have

V di de IThis map displays all three types of orbits: periodic cycles, KAM tori and chaoticorbits. The first two types of orbits (the regular ones) dominate the (I, 9) phasespace for small k (k <C 1). KAM tori extending over the entire 9 interval (0 <9 < 2TT) divide the (/, 9) phase plane into disconnected regions: orbits in one regioncannot cross the bounding KAM tori into the other regions, so that the variationsin / are bounded. For

k>kc» 0.9716

Page 78: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 59

the bounding KAM tori break, making it possible for chaotic orbits to be unboundedin the / direction. At kc the last, most robust KAM torus with the winding number

w = 2?r(v/5- l)/2

(and the other equivalent KAM tori of the standard map with winding numbers

±w + 2TTO

(n integer)) disintegrates. It corresponds to a nonanalytic continuous curve, exhibit-ing fractal self-similar structure.

The Java program Standard. Java displays the phase portrait for k = 0.8.

/ / Standard.java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Standard extends Frame

{public Standard()

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); » ) ;}

public void paint(Graphics g)

{double I = 0.5, theta =0.8; // initial valuesdouble II, thetal;

int T = 10000; II number of iterations

for(int t=0;t<T;t++)

iII = I;thetal = theta;I = II + 0.8*Math.sin(thetal);theta = thetal + II + 0.8*Math.sin(thetal);if(theta > 2*Math.PI) theta = theta - 2.0*Math.PI;if(theta < 0.0) theta = theta + 2.0*Math.PI;int m = (int) Math.floor(90*1 + 200 + 0.5);int n = (int) Math.floor(90*theta + 10 + 0.5);

Page 79: Nonlinear Workbook (3rd)

60 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

g.drawLine(n,m,n,m);}

}

public s t a t i c void main (String [] args){Frame f = new StandardO;f . se tVis ib le( t rue) ;}

}

Optical bistability only represents the simplest amongst the large variety of dynam-ical behaviours which can occur in passive nonlinear optical cavities. In particular,temporal instabilities leading to various forms of self-oscillations and chaos havebeen identified. Ikeda predicted the occurrence of period doubling cascades andchaos treating the cavity dynamics by means of a nonlinear mapping of the complexfield amplitude. Let z be a complex number. The Ikeda laser map f : C —¥ C isgiven by

/ c3 \f(z) =p + c2zexp i(ci - |2) .

\ 1 ~r lzl /The real bifurcation parameters are p, c\, c2 and C3. With z = x + iy and x,y £Hwe can write the map as a system of difference equations

xt+1 =p + c2{xt cos(rt) - yt sin fa))

2/*+i = c2{xt sinfa) + yt cosfa))

wherer t : = c i - i + ! + 2 / ?

and t = 0,1,2,... . In the Java program Ikeda. Java we evaluate the phaseportrait (xt, yt)- The parameter values are C\ = 0.4, c2 = 0.9, C3 = 9.0, p = 0.85.

/ / Ikeda.java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Ikeda extends Frame

{public IkedaO

{setSize(400,300);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event)

Page 80: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 61

{ System.exit(0); } } ) ;>

public void paint(Graphics g)

{double x = 0.5, y=0.5; // initial valuesdouble xl, yl;double cl = 0.4, c2 = 0.9, c3 = 9.0;double rho = 0.85;

int T = 20000; // number of iterationsfor(int t=0;t<T;t++){xl = x; yl = y;double taut = cl - c3/(1.0 + xl*xl + yl*yl);x = rho + c2*xl*Math.cos(taut) - yl*Math.sin(taut);y = c2*(xl*Math.sin(taut) + yl*Math.cos(taut));int m = (int) Math.floor(90*x + 200 + 0.5);int n = (int) Math.floor(90*y + 200 + 0.5);g.drawLine(m,n,m,n);

}

}

public static void main(String[] args){Frame f = new IkedaO ;f.setVisible(true);}

>

The coupled logistic map is given by

xt+i=rxt(l - xt) + e(yt - xt)

yt+i=ryt{l - Vt) + e(xt - yt)

where t = 0,1,2,. . . and r and e are bifurcation parameters with 1 < r < 4. Fore = 0 we have two uncoupled logistic equations. The four fixed points are given by

(s'.y*) = (0,0), (x - l 2 / * )=(^ ,^ )

(x*> V*) = (o+. a-), (z*, y*) = (a-, a+)

where

a± = ^ [(r - 1 - 2e) ± y/(r_ l - 2e)(r -1+ 2e)] .

Depending on the initial conditions and the parameter values one can find the fol-lowing behaviour: (i) orbits tend to a fixed point, (ii) periodic behaviour, (iii)

Page 81: Nonlinear Workbook (3rd)

62 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

quasiperiodic behaviour, (iv) chaotic behaviour, (v) hyperchaotic behaviour and(vi) xt and yt explode, i.e. for a finite time the state variables Xt and (or) yt tendto infinity. There is numerical evidence of hyperchaos for r = 3.70, e = 0.06.

In the Java program Couplog. Java we calculate the phase portrait {xt, yt) for theseparameter values.

// Couplog.java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Couplog extends Frame

{public CouplogO

{setSize(400,300);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void paint(Graphics g)

{double x=0.1, y=0.2; // initial values

double xl, yl;

double r=3.7, e=0.06; // control parameters

int T = 60000; // number of iterations

for(int t=0;t<T;t++){xl = x; yl = y;x = r*xl*(1.0-xl) + e*(yl-xl);

y = r*yl*(1.0-yl) + e*(xl-yl);int m = (int) Math.floor(400*x + 150 + 0.5);int n = (int) Math.floor(400*y + 150 + 0.5);g.drawLine(m,n,m,n);>>

public static void main(String[] args)

{Frame f = new CouplogO ;

Page 82: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 63

f.setVisible(true);>

}

1.2.3 Fixed Points and StabilityGiven a two-dimensional map

Xlt+l = fl(xit, X2t), X2t+\ = h{xU, X2t) •

The fixed points (x*, x^) are defined as the solution of the equations

Mxl,x*2)=xl, f2(x{,x*) = x*2.

As an example we consider the Henon map

xt+i = 1 + yt - ax2, yt+1 = bxt

where a and b are bifurcation parameters and t = 0,1,2, For a > 0 and 1 > b > 0the map has two fixed points

(6 - 1) ± v/(l - b)2 + 4o

2a ' ^ = te*-

These fixed points are real for

(1 - bfa>ao = -———.

4

The map has been studied in detail for a = 1.4 and b = 0.3. For these values thefixed points are unstable. There is numerical evidence that the map shows chaos forthese parameter values.

In the C++ program we determine the stability of the fixed points for a = 1.4 andb = 0.3. Since

& = _*., f,h f = b, f=0ox ay ox ay

we obtain the characteristic equation

Alj2 = -ax* ± Vb + a2x*2 .

II henonstability.cpp

#include <iostream>#include <cmath> // for sqrtusing namespace std;

Page 83: Nonlinear Workbook (3rd)

64 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

in t main(void){

double a = 1.4, b = 0.3; // parameter values

// first fixed point

double xfl = ((b-1.0)+sqrt((1.0-b)*(1.0-b)+4.0*a))/(2.0*a);double yfl = b*xfl;

double lambdal = -a*xfl + sqrt(b+a*a*xfl*xf1);double Iambda2 = -a*xfl - sqrt(b+a*a*xfl*xf1);cout « "lambdal for fixpoint 1 = " « lambdal « endl;cout « "Iambda2 for fixpoint 1 = " « Iambda2 « endl;

// second fixed point

double xf2 = ((b-1.0)-sqrt((1.0-b)*(1.0-b)+4.0*a))/(2.0*a);double yf2 = b*xf2;

double Iambda3 = -a*xf2 + sqrt(b+a*a*xf2*xf2);double Iambda4 = -a*xf2 - sqrt(b+a*a*xf2*xf2);cout « "Iambda3 for fixpoint 2 = " « Iambda3 « endl;cout « "Iambda4 for fixpoint 2 = " « Iambda4 « endl;return 0;

}

1.2.4 Liapunov Exponents

Consider a system of difference equations

xt+l = fi{xt,yt), 2/t+i = fiixuVt)

where we assume that fx and / 2 are smooth functions. Then the variational equationis given by

ut+1 = —-(x = xt, y = yt)ut + ~{x = xt,y = yt)vtox ay

vt+i ^-~(x = xt,y = yt)ut + -^-{x = xuy- yt)vt •

The maximal one-dimensional Liapunov exponent is given by

A= lim - ln ( |u T | + |wT|).T—»oo i

As an example in the program we consider the Henon map described by

xt+\ = 1 + yt - ax\, yt+i = bxt

Page 84: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 65

where a and 6 are bifurcation parameters and £ = 0 ,1 ,2 , . . . . Since

fi{x,y) = 1 + y-ax2, f2(x,y)-bx

we obtain the variational equation

ut+i = -2axtut + vt, vt+i = but •

For the parameter value a — 1.4 and b = 0.3 we find from the numerical analysisthat the (maximal) one-dimensional Liapunov exponent is given by A w 0.42. Thereis numerical evidence that the map shows chaos for these parameter values.

// henonliapunov.cpp

#include <iostream>#include <cmath> // for fabs, logusing namespace std;

double fl(double x,double y){ double a = 1.4; return 1.0 + y - a*x*x; >

double f2(double x,double y){ double b = 0.3; return b*x; }

double vfl(double x,double y,double u,double v){ double a = 1.4; return -2.0*a*x*u + v; }

double vf2(double x,double y,double u,double v){ double b = 0.3; return b*u; }

int main(void)-Cint T = 1000; // number of iterationsdouble x = 0.1, y = 0.2; // initial valuesdouble u = 0.5, v = 0.5; // initial valuesdouble xl, yl, ul, vl;for(int t=0;t<T;t++){xl = x; yl = y; ul = u; vl = v;x = fl(xl,yl); y = f2(xl,yl);u = vfl(xl,yl,ul,vl); v = vf2(xl,yl,ul,vl);}double lambda = log(fabs(u) + fabs(v))/((double) T);cout « "lambda = " « lambda « endl;

return 0;

>

Page 85: Nonlinear Workbook (3rd)

66 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.2.5 Correlation Integral

Dissipative dynamical systems (for example the Henon map) which exhibit chaoticbehaviour often have an attractor in phase space which is strange. Strange attrac-tors are typically characterized by a fractal dimension D which is smaller than thenumber of degrees of freedom F, D < F. Among the fractal dimensions we havethe capacity and the Hausdorff dimension. These fractal dimensions have been themost commonly used measure of the strangeness of attractors. Another measureis obtained by considering correlations between points of a long-time series on theattractor. Denote the T points of such a long-time series by

{xjL = {x(i + ir)}f=1

where r is an arbitrary but fixed time increment. The definition of the correlationintegral is

C(r):= l im-^ E #(r - | |* - Xj||)

where H(x) is the Heaviside function, i.e.

H(x)=lli0TX-°^ ' ' 10 otherwise

and | | . . . || denotes the Euclidean norm. The function C(r) behaves as a power of rfor small r

C{r) oc r" .

The exponent v is called the correlation dimension. Moreover, the exponent v isclosely related to the capacity D.

In the C++ program henoncorrelation.cpp we evaluate the C(r) for the Henonmap

xt+i = \ + yt-ax2t, yt+1 = bxt

with parameter values a = 1.4 and b — 0.3 and i = 0,1, 2 , . . . . From C(r) we findthat v « 1.2.

// henoncorrelation.cpp

#include <iostream>#include <cmath> // for sqrtusing namespace std;

unsigned long H(double* x,double* y,double r,double T)

{double norm;unsigned long sum = 0;

Page 86: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 67

for(unsigned long i=0;i<T;i++)for(unsigned long j=O;j<T;j++){

if(i != j){norm = sqrt((x[i]-x[j])*(x[i]-x[j] ) + (y [i]-y [j])*(y [i]-y [j] )) ;if(r >= norm) sum++;>}return sum;

}

int main(void)

{unsigned long T = 20000; // number of iterationsdouble* x = new double[T];double* y = new double[T];x[0] = 1.161094; y[0] = -0.09541356; // initial values

for(unsigned long j=0;j<T-l;j++){x[j+l] = 1.0 + y[j] - 1.4*x[j]*x[j];yCj+1] = 0.3*x[j];}double r = 0.001;while(r <= 0.008){double Cr = H(x,y,r,T);cout « "r = " « r « " " « "Cr = " « Cr/((double)(T*T)) « endl;r += 0.001;}delete [] x; delete [] y;return 0;

}

1.2.6 Capacity

Let M be a subset of R". We assume that the set M is contained in an invariantmanifold of some dynamical system (in our case the strange attractor of the Henonmodel). If Ne is the minimum number of boxes of side c in Rn needed to cover theset M, the capacity (also called box-counting dimension) is defined as

C := lim . . , , .<-K> ln(l/e)

Page 87: Nonlinear Workbook (3rd)

68 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

If a set has volume V, the number of boxes of side e needed to cover the set isroughly

Nc « Ve~c.

This equation may be rewritten as

lnJV e «Cln( l /e )+lnV

which provides a more practical method of computing the capacity C than the def-inition, since the latter has a slowly vanishing correction to the capacity V/ \n(l/e).By plotting

logiVe versus log(l/e)

for decreasing values of e, the formula for C gives the capacity as the asymptoticslope. Let DH be the Hausdorff dimension. Then we have DJJ < C.

In the C++ program capacity.cpp we find the capacity for the Henon map, wherea = 1.4 and b = 0.3. The numerical simulation yields the value C « 1.26.

// capacity.cpp

#include <iostream>#include <cmath> // for floor, logusing namespace std;

unsigned long Neps_func(double* x,double* y.unsigned T,double eps)

{.unsigned i, j, k, Nx, Ny, Neps = 0;double mx, my, xmin, ymin, xmax, ymax;xmin = x [0]; xmax = x [0]; ymin = y [0] ; ymax = y [0] ;for(i=l;i<T;i++){i f (x [ i ] < xmin) xmin = x [ i ] ; i f (x [ i ] > xmax) xmax = x [ i ] ;i f (y [ i ] < ymin) ymin = y[i] ; i f (y [ i ] > ymax) ymax = y [ i ] ;}Nx = (unsigned) ((xmax - xmin)/eps + 1.0);Ny = (unsigned) ((ymax - ymin)/eps + 1.0);mx = ((double)Nx - 1.0)/(xmax - xmin);my = ((double)Ny - 1.0)/(ymax - ymin);

unsigned long** box = NULL;box = new unsigned long* [Ny];for(j=0;j<Ny;j++) box[j] = new unsigned long[Nx];

for(i=0;i<Ny;i++)for(j=0;j<Nx;j++)box[i][j] = 0;

Page 88: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 69

for(i=0;i<T;i++)

{k = (unsigned long) floor(mx*(x[i]-xmin) +0.5);j = (unsigned long) floor(my*(y[i]-ymin) + 0.5);box[j][k] = 1;

>for(i=0;i<Ny;i++)for(j=0;j<Nx;j++)Neps += box[i] [ j ] ;

for(i=0;i<Ny;i++) delete[] box[i];delete[] box;return Neps;

>

int main(void){

unsigned long T = 200000;double* x = new double[T];double* y = new double[T];x[0] = 1.161094; y[0] = -0.09541356;

for(int j=O;j<T-l;j++){x[j+l] = 1.0 + y[j] - 1.4*x[j]*x[j]; y[j+l] = 0.3*x[j];}

double eps = 0.01;unsigned Neps = Neps_func(x,y,T,eps);cout « "Neps = " « Neps « " " « "eps = " « eps « endl;cout « "log(Neps) = " « log((double) Neps) « " "

« "log(1.0/eps) = " « log(1.0/eps) « endl;

eps = 0.005;Neps = Neps_func(x,y,T,eps);cout « "Neps = " « Neps « " " « "eps = " « eps « endl;cout « "log(Neps) = " « log((double) Neps) « " "

« "log(1.0/eps) = " « log(1.0/eps) « endl;

eps = 0.002;Neps = Neps_func(x,y,T,eps);cout « "Neps = " « Neps « " " « "eps = " « eps « endl;cout « "log(Neps) = " « log((double) Neps) « " "

« "log(1.0/eps) = " « log(1.0/eps) « endl;

Page 89: Nonlinear Workbook (3rd)

70 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

eps =0.001;

Neps = Neps_func(x,y,T,eps);cout « "Neps = " « Neps « " " « "eps = " « eps « endl;cout « "log(Neps) = " « log((double) Neps) « " "

« "log(1.0/eps) = " « log(1.0/eps) « endl;

deleted x; deleted y;

return 0;

>

1.2.7 Hyperchaos

We consider a system of first order autonomous ordinary difference equations

aat+i = /iOcit,:r2t)

x2t+i = h{xu, x2t) •

We assume that /i and f2 are smooth functions. We assume that the solution(xu,x2t) is bounded. For certain systems we can find so-called hyperchaos. Thismeans the system admits two one-dimensional Liapunov exponents (A{, X2) andone two-dimensional Liapunov exponent. Next we derive an equation for the two-dimensional Liapunov exponent AJ/.

The variational equation is given by

2/14+1 = 5 ^ ( x = Xf)2/lf + ^ ( x = Xt)2/2t

0/2, x , df2.V2t+1 = d^{x = Xt)yit+d^2

{x = Xt)mt-

Let (vu, v2t) satisfy the variational equation, i.e.

vu+i = ^ - ( x = xt)vit + ~{y. = xt)v2tax\ ax2

v2t+\ = ^ - ( x = y.t)vu + - ^ ( x = xt)v2fOX1 OX2

Let {ei,e2} be the standard basis in R2, i.e.,

We can writey« = J/itei + y2te2, vt = vuei + v2te2.

Page 90: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 71

Next we calculate yt A vt, where A denotes the Grafimann product (also calledexterior product or wedge product). We find

yt A vt = (ylte1 + y2te2) A (vltei + v2te2)

= J/it«2tei A e2 + 2/2t^ite2 A ei

= {yuV2t ~ V2tVit)ei A e2

where we have used the distributive law

{aei + bej) A (cek + det) = (ac)e; A efc + (ad)e; A e; + (6c)ej A e* + (bd)ej A e;

and thatei A e = — ej A e^.

It follows that ej A e7 = 0. We define

wt := yitv2t - y2tvu

and evaluate the time evolution of wt. Since

Wt+l = Vu+lV2t+l - J/2t+l«lt+l

we find

/ 9 / i , 3/x \ fdf2 df2f , \

-(t^»+£H(t(xtK+£H-Consequently

•^-(£<<<*>-£<<<«>)•*where the initial value w0 is given by Wo := 2/10 20 — 2/20 10- The two-dimensionalLiapunov exponent is given by

\ n := lim — In \U>T\

where A77 depends on XIQ, X2O, J/IO, 2/20, 10 and 20- Let A7 be the maximal one-dimensional Liapunov exponent and let A7/ be the maximal two-dimensional Lia-punov exponent. If A7 > 0 and A77 > 0 we say that the system shows hyperchaoticbehaviour. We have

A" = A7 + A7

where A[ and A7, are the two one-dimensional Liapunov exponents.

Page 91: Nonlinear Workbook (3rd)

72 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

As an example we consider the coupled logistic equation. It is given by

xu+i = rxu(l - xu) + e{x2t - xu)x2t+i = rx2t(l - x2t) + e(xu - x2t)

where t = 0,1,2,. . . and r and e are bifurcation parameters with 1 < r < 4. Fore = 0 we have two uncoupled logistic equations. Depending on the initial conditionsand the parameter values one can find the following behaviour: (i) orbits tendto a fixed point, (ii) periodic behaviour, (iii) quasiperiodic behaviour, (iv) chaoticbehaviour, (v) hyperchaotic behaviour and (vi) xu and x2t explode, i.e. for a finitetime the state variables xu and (or) x2t tend to infinity. Let

fi(xi,x2)=rxi(l - xi) + e(x2 - Xi)f2(xl,x2) = rx2(l -x2) + e(xi -x2).

Thus we find for the variational equation

Vit+i = (r(l - 2xu) - e)yu + ey2t

V2t+i = eyu + (r(l - 2x2t) - e)y2t.

Furthermore we find

wt+i = (r2(l - 2i l t ) ( l - 2x2t) - 2re(l - xlt - x2t))wt.

In the C++ program hyperchaos. cpp we evaluate the one-dimensional and two-dimensional Liapunov exponent for e = 0.06 and r in the range r G [3.2...3.7]. Forexample, for e = 0.06 and r = 3.7 there is numerical evidence that the system showshyperchaotic behaviour.

// hyperchaos.cpp

#include <fstream> // for ofstream, close#include <cmath> // for fabs, logusing namespace std;

double fKdouble xl,double x2,double r,double e)

{ return r*xl*(1.0 - xl) + e*(x2 - xl); }

double f2(double xl.double x2,double r,double e)

{ return r*x2*(1.0 - x2) + e*(xl - x2); }

double vKdouble xl,double x2,double yl.double y2,double r,double e)

{ return (r - 2.0*r*xl - e)*yl + e*y2; }

double v2(double xl,double x2,double yl,double y2,double r,double e)

{ return e*yl + (r - 2.0*r*x2 - e)*y2; }

Page 92: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 73

double varext(double xl,double x2,double w,double r,double e){ return (r*r*(1.0-2.0*xl)*(1.0-2.0*x2)-2.0*e*r*(1.0-xl-x2))*w; }

int main(void)

{int T = 700; // number of iterationsdouble r, e; // bifurcation parametersdouble rmin =3.2, rmax =3.7;r = rmin; e = 0.06;ofstream data("lambda.dat");while(r <= rmax)

•Cdouble xll = 0.7, x22 = 0.3; // initial valuesdouble xl, x2;// remove the transientsfor(int t=0;t<10;t++)ixl = xll; x2 = x22;xll = fl(xl,x2,r,e); x22 = f2(xl,x2,r,e);>double yll = 0.5, y22 = 0.5;double w;double wl = 0.5;double yl, y2;

for(int t=0;t<T;t++){xl = xll; x2 = x22; yl = yll; y2 = y22; w = wl;xll = fl(xl,x2,r,e); x22 = f2(xl,x2,r,e);yll = vl(xl,x2,yl,y2,r,e); y22 = v2(xl,x2,yl,y2,r,e);wl = varext(xl,x2,w,r,e);

>

double lambdal = log(fabs(yll) + fabs(y22))/((double) T);double lambdall = log(fabs(wl))/((double) T);data « r « " " « lambdal « " " « lambdall « "\n";r += 0.005;}

data.closeO;return 0;

>

Page 93: Nonlinear Workbook (3rd)

74 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

1.2.8 Domain of AttractionConsider a system of nonlinear first order autonomous difference equations

x t + i = f ( x t , r ) , t = 0,1,2,. . .

where r = (ri,r2, • • • ,rp) are bifurcation parameters. Let us assume that the ini-tial values x0 are given. The behaviour for t -t oo depends on the bifurcationparameters. In particular one is interested in finding the domain for the bifurcationparameter values r where the solution escapes to infinity, i.e.,

||x(|| -» oo for t-> oo.

The domain of attraction can have a fractal structure. Obviously, the domain alsodepends on the initial values. Thus one keeps the initial values fixed.

As an example we consider the coupled logistic equation

xlt+i = rxu(l - xu) + e{x2t - xu)

X2t+i = rx2t(l - x2t) + e{xu - x2t)

where t = 0,1,2,. . . and r and e are bifurcation parameters with 1 < r < 4. Fore = 0 we have two uncoupled logistic equations. Depending on the initial condi-tions and the parameter values one can find the following behaviour for t —> oo: (i)orbits tend to a fixed point, (ii) periodic behaviour, (iii) quasiperiodic behaviour,(iv) chaotic behaviour, (v) hyperchaotic behaviour and (vi) xu and (or) x2t tend toinfinity.

In the C++ program domain.cpp we find the escape domain, for the parameterregions

e e [0.04,0.09] and r e [3.5,4.0].

The initial values are fixed to xw = 0.7 and x20 — 0.3. Obviously, the initial valuesmust be chosen so that xw ^ x2o otherwise the coupling term will cancel out andwe have two uncoupled logistic maps.

/ / domain.cpp

#include <fstream> // for ofstream, close#include <cmath> // for fabs, logusing namespace std;

double fl(double xl,double x2,double r,double e){ return r*xl*(1.0 - xl) + e*(x2 - xl); }

double f2(double xl.double x2,double r,double e){ return r*x2*(1.0 - x2) + e*(xl - x2); }

Page 94: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 75

int main(void)

{int T = 800;double r, e; // bifurcation parametersdouble rmin =3.6, rmax = 4.0;double emin = 0.04, emax = 0.08;r = rmin; e = emin;ofstream data("domain.dat");

while(e <= emax){while(r <= rmax){

double xll = 0.7, x22 = 0.3;double xl, x2;for(int t=O;t<=T;t++){xl = xll; x2 = x22;xll = fl(xl,x2,r,e); x22 = f2(xl,x2,r,e);>if((fabs(xll) > 20) || (fabs(x22) > 20)){data « r « " " « e « "\n";}r += 0.0005;}e += 0.00005;r = rmin;}data.close0;return 0;

}

1.2.9 Newton Method in the Complex Domain

In connection with chaotic behaviour and fractals the Newton method is consideredin the complex domain C. Let / be a differentiable complex valued function. Givenan approximate value of ZQ to the solution of f(z) = 0 the Newton method finds thenext approximation by calculating

Page 95: Nonlinear Workbook (3rd)

76 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

where i = 0,1, 2 , . . . and f'(zt) ^ 0 is the derivative of / at z = zt. One calls

the Newton transformation associated with the function / . The general expectationis that a typical orbit {/(t)(z0) } which starts from an initial guess z0 e C, willconverge to one of the roots.

Consider the polynomialF(z) = z * - l

for z eC. There are four distinct complex numbers, a, (j = 1,2,3,4) such that

F(aj) = 0.

These are called the roots, or the zeros, the polynomial F(z). The roots are givenby

ai = 1, a,i = —1, a3 = i, at = —i.

The Newton method tells us to consider the dynamical system

with F' = dF/dz = iz3. We call / the Newton transformation associated with thefunction F. The general expectation is that a typical orbit

{f{t\zo)} = {zo,f(zo),f(f(zo)),...}

which starts from an initial "guess" ZQ £ C, will converge to one of the roots of F.For the present case we find that the Newton transformation is given by

f(z] - 3*4 + 1f{z} ~ ~ 4 ^ ~ '

We expect the orbit of ZQ to converge to one of the numbers ai, a^, a$ or 04. If wechoose zg close enough to a,j then it is readily proved that

nlim/W(zb)=aJ-) for j = 1,2,3,4.

If, on the other hand, ZQ is far away from all of the a,'s, then what happens? Perhapsthe orbit of Zo converges to the root of F(z) closest to z0? Or perhaps the orbit doesnot settle down, but wanders, hopelessly, forever?

In the C++ program complexnewton. cpp we use z = x + iy, where x and y arereal. Then the equation in the complex plane decomposes into two equations (realpart and imaginary part). We iterate these two coupled maps.

Page 96: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 77

/ / complexnewton.cpp

#include <iostream>using namespace std;

int main(void)

{double xl = 0.4, yl = 0.2; // initial valuesint T = 1000; // number of iterationsdouble x, y, D, R, I;

for(int t=0;t<T;t++)

{x = xl; y = yl;double x2 = x*x;double y2 = y*y;double x4 = x2*x2;double y4 = y2*y2;D = 4.0*(x2+y2)*(x2+y2)*(x2+y2);R = 3.0*x4*(x2*x+3.0*x*y2)+3.0*x*y4*(3.0*x2+y2)+x*(x2-3.0*y2);1=3.0*y4*(y2*y+3.0*x2*y)+3.0*x4*y*(x2+3.0*y2)+y*(y2-3.0*x2);xl = R/D; yl = I/D;>cout « "xl = " « xl « endl;cout « "yl = " « yl « endl;return 0;

>

1.2.10 Newton Method in Higher DimensionsWe consider the system

fi(x1,x2,...,xn) = 0, . . . , fn{xux2,...,xn) = 0 .

We denote by x the vector with components (x\, X2, • • •, xn) and by f the vector withcomponents (/i, f2,..., fn). Then the system can be written as one vector equation,f (x) = 0. If we take the gradients of the components, we obtain a function matrixcalled the Jacobian matrix J. It is defined as follows

/xt \ dxi dx2 dxn

J(f(x)) := (p.) ^ ;^dXkJ dfn dfn . . . dfn

V dxi dx2 dxn JOur problem is now to solve the equation f (x) = 0. We suppose that the vectory is the exact solution and that our present approximation x can be written as

Page 97: Nonlinear Workbook (3rd)

78 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

x = y + h. We compute fi{x\,..., xn) and call these known values ft. Hence

fi{yi + h, y2 + h2,..., yn + hn) = g{.

The first term is zero by definition, and neglecting higher order terms we obtainwith Jik = (dfi/dxk)x=y:

Jh — g and h = J - 1 g .

We suppose that the matrix J is nonsingular. The vector h found in this way ingeneral does not give us an exact solution. We arrive at the iteration formula

X(t+D = x(t) _ j- if(x( t)) .

In one dimension this formula becomes the usual Newton formula. The matrix Jmust be nonsingular. The matrix J changes from step to step. This suggests asimplification to reduce computational efforts: replace J (x^ ) by J(x'0').

In the C++ program we consider the case n = 2 with

fi{xi, x2) = x\ + x\-l, f2(xu x2) =xi-x2.

This system admits two solutions, namely (xi,x2) = ( l / \ /2, l/\/2) and (11,2:2) =(-1/V2,-1/V2).

II twonewton.cpp

#include <iostream>#include <cmath> // for fabsusing namespace std;

double gl(double,double);double g2(double,double);

int main(void)

{double xO, yO, xl, yl, eps;int t = 0;xl = 3.5, yl = 20.3; // initial valueseps = 0.0005;

do

{

xO = xl; yO = yl;

xl = gl(x0,y0); yl = g2(x0,y0);

t++;} while((fabs(xO - xl) > eps) && (fabs(yO - yl) > eps));

Page 98: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 79

cout « "t = " « t « endl;cout « "xl = " « xl « endl;cout « "yl = " « yl « endl;return 0;

>

double gl(double x,double y){ return (x-(x*x-y*y+2.0*x*y-l,0)/(2.0*(x+y))); }

double g2(double x,double y){ return (y-(-x*x+y*y+2.0*x*y-l.0)/(2.0*(x+y))); >

1.2.11 Ruelle-Takens-Newhouse Scenario

In this route to chaos we have the following sequence when the bifurcation param-eter r is changed. A stationary point (fixed point) bifurcates to a periodic orbit,which then bifurcates to a doubly periodic orbit formed by the surface of a torus,which then bifurcates to a system with chaotic behaviour. Newhouse, Ruelle andTakens [71] conjectured that small nonlinearities would destroy triply periodic mo-tion. They proved the following.

Theorem. Let V be a constant vector field on the torus Tn = R n /Z n . If n > 3every C2 neighbourhood of V conatins a vector field VT with a strange Axiom Aattractor. If n > 4, we may take C°° instead of C2.

A dynamical system is (or satisfies) Axiom A if its nonwandering set(i) has a hyperbolic structure, and(ii) is the closure of the set of closed orbits of the system.

We define a point p to be non-wandering if, for all neighbourhoods U of p, f(U) \ Uis nonempty for arbitrary large t € R or t G N. The term non-wandering point is anunhappy one, since not only may the point wander away from its original positionbut it may never come back again. The set of all non-wandering points of the map/ are called non-wandering set of / .

We consider the map (Lopez-Ruiz and Perez-Garica [60], [61])

xt+i = r(3yt + l)a:t(l - xt), yt+i = r(3xt + l)yt{l - Vt)

which shows the Ruelle-Takens-Newhouse transition to chaos. The control parame-ter is r. We calculate the variational equation symbolically

ut+i =r{3yt + 1)(1 - 2xt)ut + 3rxt(l - xt)vt

vt+1=3ryt(l - yt)ut + r(3xt + 1)(1 - 2yt)vt

Page 99: Nonlinear Workbook (3rd)

80 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

and then iterate these four equations using the data type double. The largestone-dimensional Liapunov exponent is calculated approximately

X m-\n(\ur\ + \vT\)

where r = 1.0834 and T is large. The fixed points of the map are given by thesolution of the system

r(3y* + l ) i ' ( l - x*) = x", r(3z* + l)y*(l - y*) = y*.

We find five fixed points

x[ = — {-yjAr2 - 3r + r) , y{ - — f-\/4r2 - 3r + r)ir x ' 3r v '

x*2 = Yr (^ 4 r 2 - 3r + r) , 2/2* = ^ (V4r2 - 3r + r)

a:; = (r - l)/r, 2/3* = 0

z: = o, y4* = oz*=0, l/B* = ( r - l ) / r .

The fixed points (x\,y\) and (I^J/J) exist only for r > 3/4.

// Ruelle.cpp

#include <iostream>#include <cmath> // for fabs, log#include "msymbol.h"using namespace std;

template <class T> T f(T x,T y,T r){ return r*(T(3)*y+T(l))*x*(T(l)-x); }

template <class T> T g(T x,T y,T r){ return r*(T(3)*x+T(l))*y*(T(l)-y); }

int main(void)

{int T = 1000; // number of iterationsdouble x2, y2, u2, v2;Sum<double> x("x",0), xl("xl",0), y("y",0), yl("yl",O), r("r",0),

u("u",0), ul("ul",0), v("v",0), vl("vl",O);xl = f(x,y,r); yl = g(x,y,r);

cout « "xl = " « xl « endl;cout « "yl = " « yl « endl;ul = df(xl,x)*u + df(xl,y)*v; // variational equation

Page 100: Nonlinear Workbook (3rd)

1.2. TWO-DIMENSIONAL MAPS 81

vl = df(yl,y)*v + df(yl,x)*u; // variational equationcout « "ul = " « ul « endl;cout « "vl = " « vl « endl; cout « endl;x.set(0.3); y.set(0.4); r.set(1.0834); // initial valuesu.set(0.5); v.set(0.6); // initial valuesfor(int t=l;t<T;t++)

{x2 = xl.nvalueO; y2 = yl.nvalueO;u2 = ul.nvalueO; v2 = vl.nvalueO;x.set(x2); y.set(y2); u.set(u2); v.set(v2);cout « "The Liapunov exponent for t = " « t « " is "

« log(fabs(u.nvalue())+fabs(v.nvalue()))/t « endl;}return 0;

}

1.2.12 JPEG fileJPEG, unlike other formats like PPM, PGM, and GIF, is a lossy compression tech-nique; this means visual information is lost permanently. The key to making JPEGwork is choosing what data to throw away. JPEG is the image compression stan-dard developed by the Joint Photographic Experts Group. It works best on naturalimages (scenes). We describe general JPEG compression for greyscale images; how-ever, JPEG compresses color images just as easily. For instance, it compresses thered-green-blue parts of a color image as three separate greyscale images - each com-pressed to a different extent, if desired. JPEG divides up the image into 8 by 8 pixelblocks, and then calculates the two-dimensional discrete cosine transform (DCT) ofeach block. The two-dimensional discrete cosine transform is given by

f{ku k2)=4Yl E x(nun2) cos •> — cos ^ —r1

ni=0 n2=0 V / J V 1 / V ^iV2 /

where x(n\, n2) is an input image with n\ = 0 , 1 , . . . , iVi — 1 and n2 = 0 , 1 , . . . , N2 — l.The ki, k2 are the coordinates in the transform domain, where k\ = 0 , 1 , . . . , N\ andk2 = 0 , 1 , . . . , N2. A quantizer rounds off the DCT coefficients according to thequantization matrix. This step produces the "lossy" nature of JPEG, but allows forlarge compression ratios. JPEG's compression technique uses a variable length codeon these coefficients, and then writes the compressed data stream to an output file(*. jpg). For decompression, JPEG recovers the quantized DCT coefficients fromthe compressed data stream, takes the inverse transforms and displays the image.Instead of the discrete cosine transform in newer versions discrete wavelets are used.

In the Java program JPEG1. Java we convert the phase-portrait for the Ikeda-Lasermap into a JPEG file (output.jpg).

/ / JPEGl.java

Page 101: Nonlinear Workbook (3rd)

82 CHAPTER 1. NONLINEAR AND CHAOTIC MAPS

import com.sun.image.codec.jpeg.*;import j ava.awt.*;import Java.awt.geom.Line2D;import java.awt.image.Bufferedlmage;import java.io.FileOutputStream;

public class JPEG1 extends Frame{Bufferedlmage bi;Graphics2D g2;

public JPEG1O{bi = new Bufferedlmage(400,400,Bufferedlmage.TYPE_INT_RGB);g2 = bi.createGraphicsO;double x = 0.5, y = 0.5; // initial valuesdouble xl, yl;double cl = 0.4, c2 = 0.9, c3 = 9.0, rho = 0.85;int T = 20000; // number of iterationsfor(int t=0;t<T;t++)

{xl = x; yl = y;double taut = cl - c3/(1.0 + xl*xl + yl*yl);x = rho + c2*xl*Math.cos(taut) - yl*Math.sin(taut);y = c2*(xl*Math.sin(taut) + yl*Math.cos(taut));double m = 90*x + 200; double n = 90*y + 200;g2.draw(new Line2D.Double(m,n,m,n));

>try {FileOutputStream jpegOut = new File0utputStream("output.jpg");JPEGImageEncoder jie = JPEGCodec.createJPEGEncoder(jpegOut);jie.encode(bi);jpegOut.close 0;System.exit(0);

}catch(Exception e) { }} // end constructor JPEGK)

public static void main(String args[])

{

JPEG1 jp = new JPEGK);

}

>

Page 102: Nonlinear Workbook (3rd)

Chapter 2

Time Series Analysis

2.1 Introduction

Detecting the existence of deterministic chaos and its characteristics is one of theimportant studies from the viewpoint of time series analysis on chaos. For quan-titative characterization of deterministic chaos, we have several quantities such asthe

1) autocorrelation function2) correlation dimension (Grassberger-Procaccia algorithm)3) capacity of the attractor4) Liapunov exponents5) Hurst exponent6) complexity

The autocorrelation function, correlation dimension and the capacity (fractal di-mension) has been already introduced in chapter 1.

In this chapter we consider the correlation coefficient, Liapunov exponents, the Hurstexponent, and a complexity measure. As for estimating the fractal dimensions, theGrassberger-Procacia algorithm (Grassberger and Procacia [36]) has been widelyapplied to real time series data. The Liapunov exponents and its spectrum are alsoimportant statistics to quantify deterministic chaos. Several methods of estimatingLiapunov spectra have been proposed (Sano and Sawada [81], Eckmann et al. [26],Wolf et al, [96]). Even if the observable is only a single-variable time series in caseof observing with enough number of data points, the Liapunov exponent and itsspectrum of the original dynamical systems can be estimated with high accuracy.The Hurst exponent plays a central role in characterizing Brownian motion, pinknoise and black noise. The Hurst exponent can also be calculated for chaotic timeseries.

83

Page 103: Nonlinear Workbook (3rd)

84 CHAPTER 2. TIME SERIES ANALYSIS

2.2 Correlation Coefficient

Consider two T-dimensional data vectors

x = (a: 0 ,a : i , . . . ,a ;T- i ) , y = (yo, Vi, • • • , 3 / T - I ) -

The linear correlation coefficient r of the two vectors is defined by

T-I r-i T-I

t=0 t=0 t=0T-I r-i T-I r-i

x (T E *? - (E xtf)(T £ %2 - ( E vt?)\ t-o (=o (=0 t=o

and measures the strength of the linear relationship between x and y. If the x andy values are related by

y = ex

where c is any positive constant we find r = 1. If the x and y values are related by

y = - ex

where c is any positive constant we find r — — 1. Completely uncorrelated data willgive r = 0.

The following C++ program correlation, epp calculates the correlation coefficientr for a number of data points given by the user. The program first asks the user toenter the number of data. Then the data are entered by the user. Thus the memoryfor the vectors has to be dynamically allocated.

// correlation.epp

#include <iostream>#include <cmath> // for sqrtusing namespace std;

void sums(double* x,double* y.int T,double* a.double* b,double* c.double* d,double* e);

int main(void)

{unsigned long T;

cout « "number of data points: "; cin » T;double* x = new double[T];double* y = new double[T];

for(unsigned long t=0;t<T;t++)

Page 104: Nonlinear Workbook (3rd)

2.3. LIAPUNOV EXPONENT FROM TIME SERIES 85

{cout « "x[" « t « "] = "; cin » x[t];cout « "y[" « t « "] = "; cin » y[t];}

double a, b, c, d, e;a = 0.0; b = 0.0; c = 0.0; d = 0.0; e = 0.0;sums(x.y.T.&a.&b.&c.&d,fee);double r = (T*c-a*b)/sqrt((T*d-a*a)*(T*e-b*b));delete[] x; deleted y;cout « " r = " « r « "\n";return 0;

} // end main

void sums(double* x,double* y,int T,double* a.double* b,

double* c,double* d,double* e)

{double sum_x = 0.0, sum_y = 0.0;double sum_xy = 0.0, sum_x2 = 0.0, sum_y2 = 0.0;for(unsigned long t=0;t<T;t++) {sum_x += x [t] ; sum_y += y [t] ;sum_xy += x [ t ] * y [ t ] ; sum_x2 += x [ t ]*x [ t ] ; sum_y2 += y [ t ]*y [ t ] ;}*a = sum_x; *b = sum_y;*c = sum_xy; *d = sum_x2; *e = sum_y2;

}

2.3 Liapunov Exponent from Time Series

For a dynamical system, sensitivity to initial conditions is quantified by the Liapunovexponents. For example, consider two trajectories with nearby initial conditions onan attracting manifold. When the attractor is chaotic, the trajectories diverge, onaverage, at an exponential rate characterized by the largest one-dimensional Lia-punov exponent (Eckmann and Ruelle [25]). This concept is also generalized forthe spectrum of one-dimensional Liapunov exponents, Xj (j = 1,2,..., n), by con-sidering a small n-dimensional sphere of initial conditions, where n is the numberof first order ordinary differential equations used to describe the dynamical system.As time t evolves, the sphere evolves into an ellipsoid whose principal axes expand(or contract) at rates given by the one-dimensional Liapunov exponents. The pres-ence of a positive exponent is sufficient for diagnosing chaos and represents localinstability in a particular direction. Note that for the existence of an attractor,the overall dynamics must be dissipative, i.e. globally stable, and the total rateof contraction must outweigh the total rate of expansion. Thus, even when thereare several positive one-dimensional Liapunov exponents, the sum across the entirespectrum is negative. In most case a differential equation or difference equation is

Page 105: Nonlinear Workbook (3rd)

86 CHAPTER 2. TIME SERIES ANALYSIS

not given only a data set from an experiment, i.e., a time series.

A large number of authors have discussed the calculation of the spectrum of the one-dimensional Liapunov exponents from time series (Wolf [96], Sano [81], Eckmann etal [26], Sato [82], Rosenstein et al [78], Kantz [52]).

There are two types of methods to find Liapunov exponents. One is the Jaco-bian matrix estimation algorithm (Sano and Sawada [81], Eckmann et al. [26]).The Jacobian matrix estimation algorithm can find the whole spectrum of the one-dimensional Liapunov exponents. It involves the least-square-error algorithm andthe Gram-Schmidt procedure. Since this algorithm does not have built-in checksagainst noise, except the fact that the Liapunov spectrum must not depend on thenumber of near neighbours and the dimension of the reconstructed state space, itwould be better to use other methods which have a built-in-check. The methodis called the direct method for finding the largest Liapunov exponent. As for esti-mating largest Liapunov exponents, several algorithms have been already proposed,for example, an algorithm by Wolf et al. [96], and its modifications by Sato et al.[82], Rosenstein et al. [78] and Kantz [52]. These algorithms can be called a directmethod, since they calculate the divergence rates of nearby trajectories and canevaluate whether the orbital instabilities are exponential on t or a power of t.

2.3.1 Jacobian Matrix Estimation Algorithm

We follow in our presentation the work of Eckmann et al [26] and Sano and Sawada[81]. Let

X0, Xi,... ,XT-1

denote a scalar time series, where the number of data is T. We choose an embeddingdimension ds and construct a d# dimensional orbit representing the time evolutionof the system by the time-delay method. Thus

x t : = \xti xt+i> • • • >xt+dE-\)

for t = 0 , 1 , . . . , T — d,E- Next we have to find the the neighbours of xt, i.e. thepoints xT of the orbit which are contained in an e-neighbourhood centered at xt

| | X T - X ( | | < £ .

Thus we have to introduce a norm ||.||. In most cases the Euclidean norm is used.However, using the max-norm

l|w|| := max K |

is much more useful in numerical implementations. We now set

y« := xfci - Xj for llx^ - Xj\ | < t.

Page 106: Nonlinear Workbook (3rd)

2.3. LIAPUNOV EXPONENT FROM TIME SERIES 87

Thus y, is the displacement vector between the vectors x^ and Xj. After the evolu-tion of a time interval m, the orbital point Xj will proceed to xJ + m and neighbouringpoints {xki} to {xki+m}. The displacement vector y, is thereby mapped to

z* := xki+m - xj+m for Ux^ - Xj\\ < e.

If the radius e is small enough for the displacement vectors {y;} and {z;} to beregarded as good approximations of tangent vectors in the tangent space, evolutionof yi to z,- can be represented by some matrix Aj, as

Zi = AjYi.

The matrix Aj is an approximation of the flow map at Xj. Next we have to findthe optimal estimation of the linearized flow map Aj from the data sets {y*} and{z;}. An optimal estimation is the least-square-error algorithm, which minimizesthe average of the squared error norm between z, and Ajy^ with respect to allcomponents of the matrix Aj as follows

1 N

min S = min — £ ||zf - A,-y,-||2.

Denoting the {k,l) component of matrix Aj by AM(J) and applying this condition,one obtains d x d equations to solve,

dAkl(j)

We obtain the following expression for Aj, namely AjV = C, where the matrices Vand C are given by

1 n l N

{V)kl = -TTY^ V'kVih {C)kl = T7 Yl ZikVil •

The d x d matrices V and C are called covariance matrices, and j / ^ and za- arethe k components of vectors y, and zt, respectively. If N > d and there is nodegeneracy, the equation AjV = C has a solution for a^j). Since we found thevariational equation in the tangent space along the experimentally obtained orbit,the one-dimensional Liapunov exponents can be computed as

^ = ^Tpn\\A3ei\\

foii = l,2,...,d, where Aj is the solution of AjC = V and { e\} (i = 1,2,..., d)is a set of basis vectors of the tangent space at x^. In the numerical procedure,choose an arbitrary set {e^}. Operate with the matrix Aj on { e } and renormalizeAj&\ to have length 1. Using the Gram-Schmidt procedure, we maintain mutualorthogonality of the basis. We repeat this procedure for n iterations.

Page 107: Nonlinear Workbook (3rd)

88 CHAPTER 2. TIME SERIES ANALYSIS

2.3.2 Direct Method

The simplest implementation of the direct method in the one-dimensional case is asfollows. For our illustration we assume that the scalar time series is generated by thelogistic map f : [0,1] -)• [0, l], f(x) = Ax{l-x) with the initial value x[0] = 0.333333.We assume that we have 20 data points. Thus the time series given is

x[0] = 0.333333 x[l] = 0.888888x[2] = 0.395063 x[3] = 0.955953x[4] = 0.168427 x[5] = 0.560239x[6] = 0.985485 x[7] = 0.0572163x[8] = 0.21577 x[9] = 0.676854x[10] = 0.874891 x [ l l ] = 0.437828x[12] = 0.984539 x[13] = 0.0608888x[14] = 0.228725 x[15] = 0.70564x[16] = 0.830848 x[17] = 0.562158x[18] = 0.984545 x[19] = 0.060863

We start at t = 0 with x[0] = 0.333333. Next we search for the point in thetime series which is closest to x[0]. This is obviously x[2] = 0.395063. Now wecalculate the absolute value of the difference of these two points

d[0] = |x[0] - x [2] | = 0.061730 .

Next we calculate the absolute value difference of the two consecutive points of x [0]and x [2], namely x [1] and x [3]. Thus

dl[0] = |x[ l ] - x [3] | = 0.067065 .

Now we repeat this procedure for the point at t = 1, x [ l ] . The closest point in thetime series to x[l] is x[10]. Thus

d[l] = |x[ l ] - x[10]| = 0.013997 .

For the consecutive points x[2] and x [ l l ] we obtain

d l [ l ] = |x[2] - x [ l l ] | = 0.042765 .

We repeat the procedure up to t = 18. The last point is not taken into accountsince it has no succeeding point. Thus we obtain the following pairs of distances

d[0] = 0.0617301 dl[0] = 0.0670646d[l] = 0.0139979 dl[l] = 0.0427652d[2] = 0.0427652 dl[2] = 0.0285858d[3] = 0.0285858 dl[3] = 0.107539d[4] = 0.0473429 dl[4] = 0.116615d[5] = 0.00191966 dl[5] = 0.00093984

Page 108: Nonlinear Workbook (3rd)

2.3. LIAPUNOV EXPONENT FROM TIME SERIES 89

d[6] = 0.00093984 dl[6] = 0.00364669d[7] = 0.0036725 dl[7] = 0.0129551d[8] = 0.0129551 dl[8] = 0.0287863d[9] = 0.0287863 dl[9] = 0.0440425d[10] = 0.0139979 dl[10] = 0.0427652d[ l l ] = 0.0427652 d l [ l l ] = 0.0285858d[12] = 6.6586e-006 dl[12] = 2.5811e-005d[13] = 0.0036725 dl[13] = 0.0129551d[14] = 0.0129551 dl[14] = 0.0287863d[15] = 0.0287863 dl[15] = 0.0440425d[16] = 0.0440425 dl[16] = 0.12433d[17] = 0.00191966 dl[17] = 0.00093984d[18] = 6.6586e-006 dl[18] = 2.5811e-005

Now the approximative one-dimensional Liapunov exponent is evaluated as

. 1 " (dl[t}\

We find A w 0.651626 which is quite a good agreement with the exact value A = ln(2)if we take into account that we only had 20 data points. Furthermore we did notintroduce an epsilon neighbourhood for each point of the reference trajectory.

// Liapunovserl.cpp

#include <iostream>

#include <cmath> // for fabsusing namespace std;

void find(double* a.int length.int point,doublefc min.intft position){int i = 0;if (point == 0) { min = fabs(a[i] - a[ l]) ; }if (point != 0) { min = fabs(a[i] - a[point]); }position = i;double distance;

for(i=l;i<(length-l);i++)-Cif(i != point){distance = fabs(a[i] - a[point]);if(distance < min){min = distance;position = i;

Page 109: Nonlinear Workbook (3rd)

90 CHAPTER 2. TIME SERIES ANALYSIS

} // end if} // end if} // end for

}

int main(void)

•C// generate time seriesint T = 20; // length of time seriesdouble* x = new double[T];x[0] = 0.333333; // initial value of time seriesfor(int t=0;t<(T-l);t++)ix[t+l] = 4.0*x[t]*(1.0-x[t]);>double* d = new double[T-l]; // memory allocationdouble* dl = new double[T-l]; // memory allocationint point = 1;int position;double min;

for(int t=0;t<(T-l);t++)

{

find(x,T,t,min,position);d[t] = fabs(x[t] - x [position]);dl[t] = fabs(x[t+l] - x[position+l]);}

for(int t=0;t<(T-l);t++){cout « "d[" « t « "] = " « d[t] « " "

« "dl[" « t « "] = " « dl[t] « endl;}double sum =0.0;

for(int t=0;t<(T-l);t++) { sum += log(dl[t]/d[t]); }

double lambda = sum/((double) (T-l));cout « "lambda = " « lambda « endl;delete[] x; delete[] d; delete[] dl;return 0;

}

The evaluation of the Liapunov exponent can be improved by considering a smalle-neighbourhood of each point of the reference trajectory. Then for each point which

Page 110: Nonlinear Workbook (3rd)

2.3. LIAPUNOV EXPONENT FROM TIME SERIES 91

falls in the e-neighbourhood we calculate the expansion to the next point.

// Liapunovser2.cpp

#include <iostream>

#include <cmath> // for fabsusing namespace std;

int find(double* array,int length,int t,double x,int*& positions,

int& n_positions,double eps)

{for(int s=O;s<length;s++){if ((fabs (array [s]-x) < eps) && (s != t)){positions Depositions] = s;n_positions++;}>

if(n_positions > 0)

icout « "n_positions = " « n_positions « endl;return 1;>

return 0;

}

int main(void)

{// generate time seriesint T = 2000; // length of time seriesdouble* x = new double[T]; // memory allocationx[0] = 0.333333; // initial value of time seriesfor(int t=0;t<(T-l);t++){x[t+l] = 4.0*x[t]*(1.0-x[t]);}double eps = 0.005;int n_positions = 0;int* positions = new int[T-l];double sumt =0.0;

for(int t=0;t<(T-l);t++)

Page 111: Nonlinear Workbook (3rd)

92 CHAPTER 2. TIME SERIES ANALYSIS

cout « "t = " « t « endl;n_positions = 0;int result = find(x,T,t,x[t].positions,impositions,eps);double sumn = 0.0;for(int n=0;n<n_positions;n++){if((result == 1) && (positions[n] != t)){int t l = positions[n];double d = fabs(x[t]-x[tl]); double dl = fabs(x[t+l]-x[tl+l]);sumn += log(dl/d);}}sumt += sumn/((double) n_positions);}double lambda = sumt/((double) T);cout « "lambda = " « lambda « endl;delete[] x; delete[] positions;return 0;

}

The direct method of Wolf et al [96] is a follows. Let

XQ, XI, X2, •• • ,XT_i

be a scalar time series. In the fixed evolution time program the time step

A : = tjt+i — tk

between replacements is held constant and normalized to 1. A de-dimensional phaseportrait (rig embedding dimension) is reconstructed with delay coordinates, i.e., apoint on the attractor is given by

X« = (Xt,Xt+i, • • • ,Xt+dE-l) •

Let ||.|| denote a norm, for example the Euclidian norm, the max norm or sum norm.Using the selected norm we find the nearest neighbour vector to the initial pointvector

x0 = (xo,...,xo+dE-i) •

We denote the distance between these two points by d(0). At a later time 1, the ini-tial length d(0) will have evolved to length d'(l). The length element is propagatedthrough the attractor for a time short enough so that only small scale attractorstructure is likely to be examined. If the evolution time is too large we may see d'shrink as the two trajectories which define it pass through a folding region of theattractor. This would lead to an underestimation of the largest one-dimensionalLiapunov exponent A. We now look for a new data point that satisfies the following

Page 112: Nonlinear Workbook (3rd)

2.3. LIAPUNOV EXPONENT FROM TIME SERIES 93

two criteria:(i) its separation, d(l), from the evolved reference point is small,(ii) and the angular separation between the evolved and replacement elements issmall.

If an adequate replacement point cannot be found, we retain the points that werebeing used. This procedure is repeated until the reference trajectory has traversedthe entire data file, at which point we estimate the largest one-dimensional Liapunovexponent as

A- 1 f i n d'W

"Mti d{k-Dwhere M is the total number of replacement steps. In the limit of an infinite amountof noise-free data our procedure always provides replacement vectors of infinitesimalmagnitude with no orientation error, and A is obtained by definition.

The algorithm proposed by Kantz [52] evaluates the following quantity

i T-l / M \

S(T) = - £ l n £ d(x(,x,,;T)1 t=0 \ki=l ]

where T is the number of data points from the scalar time series, x( is a referencepoint, Xfc( is an e-near neighbour of x(. M is the number of nearest neighbours andT is the relative time and d(xt, x^; T) is the distance between x(+T and Xfci+T. If theanalyzed time series is produced from nonlinear dynamical systems with a positivelargest one-dimensional Liapunov exponent, there is a positive constant slope of thefunction S(T) which corresponds to the largest one-dimensional Liapunov exponent.

In the C++ program Kantz. cpp we evaluate the largest Liapunov exponent for datagenerated from the logistic map. Thus the Euclidean distance is calculated in onedimension. The embedding dimension is set to 1.

/ / Kantz.cpp

#include <iostream>#include <cmath> // for sqrt, fabs, logusing namespace std;

int neighbourhood(double* al,double* a2,int length.double eps)

•Cdouble distance = 0.0;for(int i=0;i<length;i++){ distance += (al [i]-a2[i])*(al [i]-a2 [i] ) ; }distance = sqrt(distance);if(distance < eps) return 1;

Page 113: Nonlinear Workbook (3rd)

94 CHAPTER 2. TIME SERIES ANALYSIS

else return 0;

}

int main(void)

{// generate time seriesint T = 2048; // length of time seriesdouble* x = new doubled]; // memory allocationx[0] = 0.618; // initial value of time seriesfordnt i=0;i<(T-l);i++){ x[i+l] = 4.0*x[i]*(1.0-x[i]); }

int m = 1;double** W = NULL; W = new double*[T-m+1];for(int j=0;j<(T-m+l);j++) { W[j] = new double[m]; }

int count = 0;for(int j=0;j<(T-m+l);j++)

<for(int i=0;i<m;i++)

{W[j] [i] = x[i+count];>if(m == 1) count++;else count += m - 1;

}

double eps = 0.01;int taurange = 6;double* S = new double[taurange];for(int tau=0;tauCtaurange;tau++){ S[tau] = 0.0; }

for(int tau=0;tau<taurange;tau++)

{double sumT =0.0;for(int t=0;t<(T-m+l-tau);t++)

{

double sumN =0.0;int numbert = 0;for(int s=0;s<(T-m+l-tau);s++)-[if(s != t){int result = neighbourhood(W[t],W[s],m,eps);

Page 114: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 95

if(result == 1)

{numbert++;sumN += fabs(x[t+tau] - x[s+tau]);}>} / / end sif(numbert > 0){sumT += log(sumN);

}} / / end tS[tau] =1.0/((double) T)*sumT;} / / end tau

for(int tau=O;tau<taurange;tau++){cout « "S[" « tau « "] = " « S[tau] « endl;}return 0;

}

2.4 Hurst Exponent

2.4.1 Introduction

Working extensively on the Nile River Dam Project, Hurst (Hurst [50], Peters [75])encountered the 847-year record that the Egyptians had kept of the Nile River.Most hydrologists assumed that the inflow into a reservoir was a completely randomprocess. However, in his examination of the Nile's records, Hurst felt that the datadid not represent a random structure, though standard statistical methods did notshow any correlation between the observations. Thus, Hurst developed a new set ofstatistical tools to examine data that may not have an underlying Gaussian distri-bution.

Einstein had done an extensive study on Brownian motion. This study became themain model for random walks in the study of statistics. Einstein discovered that thedistance covered by a random particle undergoing random collisions from all sidesis directly related to the square root of time. Thus

R = kT1'2

where R is the distance covered, k is some constant and T is the time index. Usingrescaled range analysis (Hurst [50], Peters [75]), Hurst proposed a generalization ofBrownian motion that could apply to a broader class of time-series. His generalized

Page 115: Nonlinear Workbook (3rd)

96 CHAPTER 2. TIME SERIES ANALYSIS

equation isR/S = kTH

where R/S = rescaled range (range/standard deviation), T = index for number/timeof observations, K = some constant for the time-series, H = Hurst exponent.

Thus, Hurst generalized the T1/2 law to a TH law. Analogously, Brownian mo-tion can be generalized to fractal Brownian motion. Fractal Brownian motion existswhenever the Hurst exponent is well-defined.

The R/S value is called the rescaled range and is a dimensionless ratio formed bydividing the range by the standard deviation of the observations. It scales as oneincreases the time increment by a power law value equal to H. This is the key pointin Hurst's analysis: by rescaling, Hurst could compare divers data points, includ-ing periods of time that may be separated by many years. In addition, this typeof analysis can be used to describe time series that possess no characteristic scale.This equation has a characteristic of fractal geometry: it scales according to a powerlaw. In the lung, for instance, the size of each branch decreases in scale accordingto an inverse-power law. Likewise, the R/S function increases as a power of H. Ifthe data of the system being measured were independently distributed, or followeda random walk, the equation would fit with Einstein's "T to the one-half rule, andthe value of the Hurst exponent would be 1/2. When Hurst investigated the NileRiver he found H = 0.91. Thus, the rescaled range was increasing at a faster ratethan the square root of time and the system (measured by the changes in water)was covering more "distance" than a purely random process. Thus, the values ofthe Nile River overflow had to be influencing one another. The river had a memoryof past floods.

There are three possibilities for values of H (Peters [75]). If H — 0.5 the systemfollows a random walk. We recover the original scenario of Brownian motion. Ifnot, the observations are not independent; each carries a memory of events whichprecede it.

• # = 0.5

Independent series. (Brown noise, or Brownian motion) The series is arandom walk.

• 0 < # < 0 . 5

Antipersistent series. (Pink noise) The system is covering less distance thana random walk. Thus, it has a tendency to reverse itself often. If increasing, itis more likely to be decreasing the next period; if decreasing, it is more likelyto be increasing.

• 0.5 < H < 1

Persistent series. (Black noise) This series covers more distance than arandom walk. Thus, if the system increases in one period, it is more likely to

Page 116: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 97

keep increasing in the immediately following period. This is commonly calledthe Joseph effect, in that it tends to lead to "seven years" of fortune followedby "seven years" of famine. Such a series also has the potential of suddencatastrophes, the so-called Noah Effect.

Thus the Hurst exponent is a useful measure for fractal distributions. There is nocharacteristic time scale in such a distribution. Hence an exponential, or relative,relation dominates over a polynomial, or absolute, characterization.

The following statements are believed equivalent for a time-series:

1. The Hurst exponent is well-defined for the time-series.2. The time-series exhibits fractional Brownian motion.3. The probability distribution is stable (Paretian or Levy).4. The slope of the log-log R/S graph is constant.

The value \/H is the fractal dimension of the probability space (Mandelbrot [65]).The random walk has a fractal dimension (capacity) of 1/0.5 = 2. Thus it completelyfills the phase space. The value 2 — H is the fractal dimension of the time-series.The value 1H -I-1 is the rate of decay of the Fourier series. This means the Fouriercoefficients decrease in proportion to l/f(2H+1\ Estimations of H can be found bytaking the slope of the log/log graph of R/S versus T, where

\og(R/S) = \og{kTH) = log(ft) + H\og{T).

If there is no long term memory present, scrambling the data should have no effecton this estimate of H. If, however, we destroy the structure by randomizing the datapoints, the estimate of H should be much lower. Therefore, the Hurst exponent isa meaningful measure of the memory of a system. The R/S statistics for a discretetime series ut is defined as follows

X M :=][><-(«>r)i=0

R(T):= max X(t,r)- min X(t,r)0 < K T - 1 V ' ' 0 < K T - 1 V ' '

/jr-l \l/2

S(r):=^!>-<«>r)2J

R/S(r):=R(r)/S(r).Regarding the sequence of random numbers ut as spatial increments in a one-dimensional random walk, then

r - l

t=0

is the position of the walker after a time T. In the quantity X(t, r) the mean overthe time lag r

1 T~1

(U)T :=-Y,utT t=o

Page 117: Nonlinear Workbook (3rd)

98 CHAPTER 2. TIME SERIES ANALYSIS

is subtracted to remove a trend when the expectation value of ut is not zero. R(T)is the self-adjusted range and R/S(T) is the self-rescaled self-adjusted range.

2.4.2 Implementation for the Hurst ExponentIn the following we use the notation as in our C++ program. Let

W0, UU . . . , UT-1

be a given time series of length T. We divide this time period into a contiguoussubperiods of length n, such that an = T. We label each subperiod Ij, with j =0,1,2, . . . , a — 1. Each element in Ij is labeled ./V[j][fc] such that k = 0,1,2, . . . , n— 1.Thus N is an a x n matrix. For each Ij of length n, the average value is defined as

n k=o

where Ej is the average value of the ut contained in subperiod Ij of length n.The time series of accumulated departures X[j][fc] from the mean value for eachsubperiod Ij (j = 0 , 1 , . . . , a — 1) is defined as

X[j][k] := £(AT[j]M -Ej), k = 0,1, 2 , . . . ,n - 1.»=o

Thus X is also an a x n matrix. The range is defined as the maximum minus theminimum value of X[j][fc] within each subperiod Ij

Rj. := max {X{j}[k}) - min (X\j][k]).

The sample standard deviation calculated for each subperiod Ij (j = 0 , 1 , . . . , a — 1)is

/I""1 \ 1 / 2

\ n k=0 I

Each range, RIp is now normalized by dividing by the 5^. corresponding to it.Therefore, the rescaled range for each Ij subperiod is equal to RiJSir We had acontiguous subperiods of length n. Therefore, the average R/S value for a fixedlength n is defined as

( i ? / 5 ) n-=«§vThe length n is increased to the next higher value, and (T — l) /n is an integer value.We use values of n that include the beginning and ending points of the time series,and steps described above are repeated until n = (T — l)/2. We can now apply

(R/S)n = cnH

Page 118: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 99

or\og((R/S)n)=log(c)+H\og(n)

by performing an ordinary least squares regression on log(n) as the independentvariable and \og(R/S)n as the dependent variable. The intercept is the estimate forlog(c), the constant. The slope of the equation is the estimate of the Hurst expo-nent, H. In general, one runs the regression over values of n > 10. Small values ofn produce unstable estimates when sample sizes are small.

In our C++ program Hurstl.cpp we generate a time series from the logistic map.The length of the time series is 4096. The smallest length of contiguous subperiodsis n = 16. Thus a = 256 for this case.

/ / Hurstl.cpp

#include <iostream>

#include <cmath> // for logusing namespace std;

double sum(double* array,int length){double result = 0.0;for(int i=0;i<length;i++) { result += array[i]; }return result;

}

double max(double* array,int length)

{double max_value = array[0];for(int i=l;i<length;i++){ if(max_value < array[i]) max_value = array[i]; }return max_value;

>

double min(double* array,int length)

{double min_value = array[0];for(int i=l;i<length;i++){. if (min_value > array [i] ) min_value = array [i] ; }

return min_value;

>

int main(void){// generate time seriesint T = 4096; // length of time series

Page 119: Nonlinear Workbook (3rd)

100 CHAPTER 2. TIME SERIES ANALYSIS

double* u = new doubled]; / / memory allocationu[0] = 0.618; / / initial value of time seriesfor(int i=0;i<(T-l);i++) { u[i+l] = 4.0*u[i]*(1.0-u[i]); >

int n = 16; // smallest length of contiguous subperiodsint n_numbers = 0;while(n <= T/2) { n_numbers++; n += n; >cout « "n_numbers = " « n_numbers « endl;

n = 16;int* n_values = NULL; n_values = new int[n_numbers];for( in t i=0;i<n_numbers;i++){ n_values[i] = n; n += n; }

double* RDS = new double[n_numbers];

n = 16;for(int l=0;l<n_numbers;l++)

{int a = T/n;double** N = NULL; N = new double*[a];for(int j=O;j<a;j++){ N[j] = new double [n] ; }

double* E = new double[a];

int count = 0;for(int j=O;j<a;j++)ifor(int i=0;i<n;i++){N[j] [i] = u[i+count] ;}count += n;>

for(int j=O;j<a;j++){E[j] = 1.0/((double) n)*sum(N[j] ,n);}

double** X = NULL; X = new double*[a];

for(int j=O;j<a;j++)

{ X[j] = new double [n] ; >

Page 120: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 101

for(int j=0;j<a;j++)

for(int k=0;k<n;k++)

idouble temp =0.0;for(int i=0;i<=k;i++)

{

temp += N[j][i] - E[j];>X[j][k] = temp;}>

double* R = new double[a];for(int j=O;j<a;j++){ R[j] = max(X[j],n) - min(X[j],n); }

for(int j=O;j<a;j++){for(int k=0;k<n;k++){N[j][k] = (N[j][k] - E[j])*(N[j][k] - E[j]);>>

double* S = new double[a];for(int j=O;j<a;j++){ S[j] = sqrt( 1.0/((double) n)*sum(N[j] ,n)); }

double* D = NULL; D = new double[a];for(int j=O;j<a;j++){ D[j] = R[j]/S[j]; }

delete[] E; delete[] R; delete[] S;for(int j=O;j<a;j++) { deleted N[j]; }delete [] N;for(int j=O;j<a;j++) { deleted X[j]; >deleted X;

RDS[1] = 1.0/((double) a)*sum(D,a);delete [] D;n += n;} // end for loop for 1

deleted u;

Page 121: Nonlinear Workbook (3rd)

102 CHAPTER 2. TIME SERIES ANALYSIS

for(int i=0;i<n_numbers;i++)cout « "RDS[" « i « "] = " « RDS[i] « " " «

"n = " « n_values[i] « endl;cout « endl;

for(int i=0;i<n_numbers;i++)cout « "logRDS[" « i « "] = " « log(RDS[i]) « " " «

"log(n) = " « log((double) n_values[i]) « endl;

delete [] RDS;return 0;

>

2.4.3 Random Walk

The random walk plays a central role in calculating the Hurst exponent. Hurst ran-dom walks are discrete random walks which reverse directions with probability h.Given an undirected, connected graph G{V, E) (V denotes the vertices, E denotesthe edges) with \V\ = n, \E\ = m (n number of vertices, m number of edges) arandom step in G is a move from some node u to a randomly selected neighbour v.A random walk is a sequence of these random steps starting from some initial node.

The C++ program RandomWalk. cpp implements a random walk on a two-dimensionalsquare lattice. No boundary conditions are implemented.

// randomwalk.cpp

#include <iostream>#include <cstdlib>using namespace std;

const int n = 24;

void init(char s[n][n]){

for(int i=0;i<n;i++)for(int j=O;j<n;j++) s[i][ j] = ' . ' ;

}

void display(char s[n][n]){

for(int i=0;i<n;i++){for(int j=O;j<n;j++) cout « s [ i ] [ j ] ;

Page 122: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 103

cout « endl;}

>

int main(void)

{char s [n] [n] ;int x = 12, y = 12;init(s) ;s[x][y] = ' 0 ' ; / / start cellcout « "enter a seed: ";unsigned int seed;cin » seed;srand(seed);int east = 0; int west = 0; int north = 0; int south = 0;

for(int i=l;i<=n/2;i++){switch (rand ()°/.4){case 0: x += 1; west++; break;case 1: y += 1; north++; break;case 2: x -= 1; east++; break;case 3: y -= 1; south++;}s[x][y] = '$>;} / / end for loops[x][y] = 'E' ;display(s);cout « "north = " « north « "\t" « "south = " « south « "\t"

« "west = " « west « "\t" « "east = " « east;return 0;

}

The Java program RandomWalk. j ava also implements a random walk on a two-dimensional square lattice. Cyclic boundary conditions are implemented. The reddot is the initial position. The Java program is an application and an Applet.

// RandomWalk.java

import j avax.swing.*;import j ava.awt.*;import java.util.Random;

public class RandomWalk extends JApplet

Page 123: Nonlinear Workbook (3rd)

104 CHAPTER 2. TIME SERIES ANALYSIS

{Random r;int xO, yO, x, y;boolean firstTime;int size = 370;

public void initO

{r = new Random();xO = 150; yO = 150;x = xO; y = yO;firstTime = true;}

public void paint(Graphics g)

{super.paint(g);// draw a grid 18 x 18 with each square 20 pixels widefor(int i=10;i<size;i+=20)for(int j=10;j<size;j+=20)g.drawRectd, j ,20,20);

g.setColor(Color.RED);g.fill0val(x0-3,y0-3,6,6); // initial position// draw a blue dot that represents an antg. setColor (Color. BLUE);g.fill0val(x-3,y-3,6,6);if (f irstTime) walkO ;}

private void walkO

{firstTime = false;

new Thread(new RunnableO

{int count = 0;public void run()

{while(count++ < 400)

{int d = r.nextlnt(4); //switch(d){case 0: y += -20; if(y == -10) y = 350; break;case 1: x += +20; if(x == 370) x = 10; break;

Page 124: Nonlinear Workbook (3rd)

2.4. HURST EXPONENT 105

case 2: y += +20; if(y == 370) y = 10; break;case 3: x += -20; if(x == -10) x = 350;}

repaint();

try

{Thread.sleep(200) ;}catchdnterruptedException ie)iSystem.err.println("interrupt " + ie.getMessageO);break;

}}showResultsO ;>}) . s ta r t ( ) ;}

private void showResultsO{double distance = (Math.abs(x-xO) + Math.abs(y-y0))/20.0;JOptionPane.showMessageDialog(null,"effective distance moved = " +

distance + "blocks!","results",JOptionPane.INFORMATION_MESSAGE);>

public static void main(String[] args)

iJApplet applet = new RandomWalk();JFrame f = new JFrameO;f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);f.getContentPaneO.add(applet);f.setSize(400,400);f.setLocation(200,200);applet. ini tO ;f.setVisible(true);} / / end main

}

Brownian motion (in one dimension) is a random walk on the line where the steplength is given by a mean zero Gaussian (normal) probability distribution. Sinceeach of the steps are independent the cumulative position X is known to obey

(X(t)-X(0))=0

([X(t)-X(0)]2)1/2<x\t\1/2

Page 125: Nonlinear Workbook (3rd)

1Q6 CHAPTER 2. TIME SERIES ANALYSIS

so the standard deviation from the origin grows as tll2. Mandelbrot and Wallis[63] and Mandelbrot and Van Ness [64] introduced fractional Brownian motion as ageneralization to processes which grow at different rates tH

{[XB(t) - XH(0)]2)1 / 2 OC \t\H

where 0 < H < 1 is called the Hurst exponent. Successive increments £#• of afractional Brownian motion are called fractional Gaussian noise

tH(t)=XH(t + 6)-XB(t)

where 6 can always be rescaled to one. The autocorrelation funcion which measuresthe covariance of a data series with itself at some lag r is formally defined as

C(T) .= afa(*)-(fr(*))]fa(*-T)-(M*-r))])1 • «[&(*) - &r(t)>]2><[£*(« - r) - ( M i - T))]2))V2 •

For a fractional Gaussian noise process the definition yields

C(r) = i(|T + l | 2 " - 2 | r | 2 i r + | r - i n

which is zero for H = 1/2 (except for T = 0 where the autocorrelation is alwaysone) while for general H =fi 1/2 and large r we have

lim C(T) OC T2H~2 .T-»OO X '

Thus the correlations decay slowly and the resulting fractional Browian motionexhibits long memory effects. Correlations are positive for H > 1/2 (persistence)and negative for H < 1/2 (antipersistence). As for standard Brownian motion, allfractional Brownian motion are self-affine

XH(at) := aHXH(t)

meaning that the series appears statistically indentical under scaling the time axisby some factor a and the displacement XH by aH. Thus, frational Brownian motionlacks any charateristic time scale and when generating or sampling a fractionalBrownian motion series, an arbitrary step length of one unit may be used withoutloss of generality. Self-affine signals can be described by a fractional dimensionD (capacity) which is related to the Hurst exponent by D = 2 — H for fractionalBrownian motion. The fractal dimension D can be loosely interpreted as the numberof dimensions the signal fills up. The power spectrum (defined as the amplitude-squared contributions from the frequencies ±f, S{f) = \FH(f)\2+\FH(-f)\2, whereFH is the Fourier transform oiXH) of fractional Brownian motion also demonstratesscaling behaviour. For low frequencies it can be approximated by a power lawS(f) ~ 1//2 H + 1-

Page 126: Nonlinear Workbook (3rd)

2.5. COMPLEXITY 107

2.5 Complexity

Many different definitions of complexity have been proposed in the literature. Amongthem are: algorithmic complexity (Kolmogorov-Chaitin), the Lempel-Ziv complex-ity, the logical depth of Bennet, the effective measure complexity of Grassberger,the complexity of a system based on its diversity, the thermodynamic depth, and astatistical measure of complexity (see Steeb [89] and references therein).

In the following we consider the Lempel-Ziv complexity [58], A definition of com-plexity (Kolmogorov-Chaitin [12]) of a string (a string of zeros and ones) is given bythe number of the bits of the shortest computer program which can generate thisstring. A general algorithm which determines such a program cannot be given.

Lempel and Ziv [58] have chosen from all possible programs one class that allowsonly two operations: copying and inserting. For the reconstruction of the givenstring of length n over a finite alphabet, using these two operations, they haveintroduced a complexity measure c(n). Here an algorithm can be given. First wegive some definitions. A finite nonempty set is called an alphabet, and the elementsof the set are called symbols or letters. A word (or string) over an alphabet A is anyfinite, possibly empty, sequence of symbols from A, written without punctuation.For example, if

A:={0,l}

then the following are words over A

11, 0001, 101010, 0.

The empty word is the empty sequence of symbols, and is written A. The length ofa word 5 (written \S\) is the number of symbols in S, including repetition. Thusthe words 11, 0001, 10101, 0, A have length 2, 4, 5, 1, 0, respectively. The set of allwords over A is denoted by A*. If A = { 0,1}, then

A* = { A, 0, 1, 00, 01, 10, 11, 000, 001, . . . } .

The quantity S(i,j) denotes the substring

S(i,j) = S j S j + i - --Sj.

Definition. A vocabulary of a string 5, denoted by v(S), is the subset of A* formedby all the substrings, or words, S(i, j) of S.

Definition. If S = S1S2 • •. sm and R = T\TI . . . rn are words over A, and each s;and Tj in A, then the catenation of S and R is the word

Sis2.- .smrir2.. .rn

denoted by SR.

Page 127: Nonlinear Workbook (3rd)

108 CHAPTER 2. TIME SERIES ANALYSIS

Obviously we have

(i) S(RQ) = (SR)Q(ii) S\ = XS(hi) \SR\ = \S\ + \R\(iv) SR ^ RS in general

Let us now describe how the Lempel and Ziv complexity is evaluated (Steeb [89]).Given a string of finite length

5 = sis2- • -sn .

The complexity in the sense of Lempel and Ziv of a finite string is evaluated fromthe point of view of a simple self-delimiting learning machine which, as it scans agiven n digit string 5 = S1S2 • • • sn from left to right, adds a new word to its memoryevery time it discovers a substring of consecutive digits not previously encountered.Thus the calculation of the complexity c(n) proceeds as follows. Let us assume thata given string Sis2 • • • sn has been reconstructed by the program up to the digit sr

and that sr has been newly inserted, i.e. it was not obtained by simply copying itfrom S\S2 • • • sr_i. The string up to sr will be denoted by

R := sis2 •••sro

where the o indicates that sr is newly inserted. In order to check whether the restof R, i.e., sr+iSr+2 • • • sn can be reconstructed by simple copying or whether onehas to insert new digits, we proceed as follows: first, one takes Q = sr+i and askswhether this term is contained in the vocabulary of the string R so that Q cansimply be obtained by copying a word of it!. This is equivalent to the question ofwhether Q is contained in the vocabulary v(RQn) of RQTT where RQn denotes thestring which is composed of R and Q (concatenation) and TT means that the lastdigit has to be deleted. This can be generalized to situations where Q also containstwo (i.e., Q = sr+iSr+2) or more elements. Let us assume that sr+i can be copiedfrom the vocabulary of R. Then we next ask whether Q = sr+iSr+2 is contained inthe vocabulary of RQir and so on until Q becomes so large that it can no longerbe obtained by copying a word from V(RQTT) and one has to insert a new digit.The number c of production steps to create the string 5, i.e., the number of newlyinserted digits (plus one if the last copy step is not followed by inserting a digit), isused as a measure of the complexity of a given string.

For example, a binary string consisting only of 0's (or l's) must have the lowestcomplexity, namely 2, since 0 o 000 . . . . A string consisting of a sequence of 0 l'si.e. "010101- • -01" has complexity 3, since 0 o 1 o 0101 • • 01. For the binary string0001101001000101 we find 0 o 001 o 10 o 100 o 1000 o 101.

In order to obtain a complexity measure which is independent of the string lengthwe use a normalized complexity measure. For very large strings it makes sense to

Page 128: Nonlinear Workbook (3rd)

2.5. COMPLEXITY 109

normalize them. To normalize them we consider the interval [0,1]. The rationalnumbers in this interval are of Lebesgue measure zero. For almost all numbers inthe interval [0,1] (the irrational numbers) the string of zeros and ones which repre-sents their binary decomposition is not periodic. Therefore almost all strings whichcorrespond to a binary representation of a number x S [0,1] should be random andhave maximal complexity. The complexity tends to the same value for n —»• oo,namely n/log2n for a binary alphabet. For a string with a ternary alphabet wehave n/log3n. We use this quantity to normalize the complexity c(n). Thus thelargest value the normalized complexity can take is equal to 1.

A comment is in order in calculating the Lempel and Ziv complexity. We assumethat the information needed in Lempel and Ziv coding is one unit for each newword. Only then we obtain complexity 3 for an alternating string "0101010- • •", andonly then a random string would have complexity n/ log2 n. One is of course freeto count complexity in any units one wants. The usual units are bits. In this casethe information needed to specify one among n words is sa log2 n instead of 1. Thusthe Lempel and Ziv complexity of a fully random binary string is 1 bit/symbol andnot n/ log2 n units. The average Lempel and Ziv complexity per symbol for randomstrings coincides with the Shannon entropy, and hence the Lempel and Ziv complex-ity per symbol for the logistic map at fully developed chaos is equal to 1 bit/symbol.This assumes already the limit n —>• oo. For finite n there are several sources for log-arithmic corrections, one of them being that the Lempel and Ziv complexity is onlydefined for finite strings in this picture, whence one has to specify also the stringlength. Thus the Lempel and Ziv complexity for the alternating string "0101010- • •"of length n is > log2 n in this picture, and not finite.

Next we have to find a string from the one-dimensional map. This is done usingsymbolic dynamics. To construct the symbolic dynamics of a dynamical system, thedetermination of the partition and the ordering rules for the underlying symbolicsequences is of a crucial importance. In the case of one-dimensional maps, thepartition is composed of all the critical points.

// Lempelziv.cpp

#include <iostream>#include <cmath> // for log

using namespace std;

unsigned long complexity(const unsigned long* S,unsigned long T){unsigned long c = 1, 1 = 1;do-Cunsigned long kmax = 1;for(unsigned long i=0;i<l;i++)

Page 129: Nonlinear Workbook (3rd)

110 CHAPTER 2. TIME SERIES ANALYSIS

{unsigned long k = 0;while (S[i+k] == S[l+k]){++k;if(l+k >= T-l) return (++c);}if (k >= kmax) kmax = k + 1;>++c;1 += kmax;} while(1 < T);return c;

}

int main(void)

{unsigned long T = 4000; // length of stringunsigned long* S = new unsigned long[T];

// time series from logistic map -> symbolic dynamicsdouble x = 0.618; // initial valueSCO] = 1; // since x > 0.5

for(unsigned long t=l;t<T;t++)

{double xl = x;x = 4.0*xl*(1.0 - xl);if(x >= 0.5) S[t] = 1;else S[t] = 0 ;}

unsigned long sumc = complexity(S,T);cout « "sumc = " « sumc « endl;double cnorm;cnorm = sumc*log((double) T)/(log(2)*((double) T));cout << "cnorm = " << cnorm;delete [] S;return 0;

}

Page 130: Nonlinear Workbook (3rd)

Chapter 3

Autonomous Systems in the Plane

3.1 Classification of Fixed Points

In this chapter we consider autonomous systems of ordinary differential equationsin the plane (Hirsch and Smale [45])

dui . . du2 , ,— = /i(wi, u2), — = f2{uuu2)

where fi,f2 6 C2. The fixed points {u\,u£) (also called equilibrium points, time-independent solutions) are given as the solution of the equation

/i(ui,u;) = o, / 2 («>2) = o.

For example, the system (Van der Pol equation)

dtl\ d%l2 ,, 2\ -r.at at

has only the fixed point {u\,u£) = (0,0). The system

du\ , 9 9 . du2 ,„ o o,- ^ = «i(l-u?-«i), -^- = u2{l-u\-u\)

has the manifold (circle) 1 - u\2 - W22 = 0 and the point (u{, u2) = (0,0) as its fixedpoints. Approximation to a nonlinear system by linearizing it at a fixed point is amost important and generally used technique. The linearized equation (also calledvariational equation) is given by

dvx dhjujt)) , 9/i(u(t))- 7 - = — 5 «i H 5 «2a< a«i ou2

dv2 = 9/2(u(t)) + d/2(u(t))^

111

Page 131: Nonlinear Workbook (3rd)

112 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

If we assume that the only fixed point is at the origin (0,0), then the linearizedequation simplifies to

dvt dv2— = avi + bv2, — = cvi+ dv2at atwhere

°:=f£(0'°>' ' - K ' 0 - * ' "s f M - «-g«w-We expect the solutions of this linearized equation will be geometrically similar tothose of the original system near the origin (0,0), an expectation fulfilled in mostcases. Non-trivial solutions exist if and only if

, , fa- X b \ „. det I , , 1 = 0 .

V c d — X)

It follows thatA2 - (a + d)X + (ad - be) = 0

which is called the characteristic equation. When this equation has two differentroots, Ai, A2, two linearly independent families of solutions are generated. Wedefine

p := a + d, q := ad — be.

The characteristic equation becomes

A2 - pX + q - 0.

Let A := p2 + 4q be the discriminant. Then the roots A1; A2 are given by

A1 = i(p + A1/2), A2 = i ( p - A V ) .

The following table lists the possible cases.(i) Ai,A2 real, unequal, same sign A > 0, q-> 0 Node(ii) Ai = A2 (real) 6 ^ 0 , c / 0 A = 0 , p / 0 Inflected Node(iii) Ai, A2 complex, non-zero real part A < 0,p ^ 0 Spiral(iv) Ax 0, A2 = 0 7 = 0 Parallel Lines(v) Ai,A2 real, different sign q < 0 Saddle Point(vi) Ai, A2 pure imaginary q > 0,p = 0 Centre

In classifying the fixed points as in the preceding examples we have taken for grantedan unproved assumption, that the phase paths of the original equation and those ofthe linearized equation near the fixed point are of the same character. This is truein general for spirals, nodes, and saddle points, but not for a centre. For example,the linear approximation may predict a centre where the original equation had aspiral. Conversely, the system of differential equations

du\ dv,2 Q

has a centre at the origin, but the linearized system of equationsdvi dv2

~df = V2> -dt=°has not.

Page 132: Nonlinear Workbook (3rd)

3.2. HOMOCLINIC ORBIT 113

3.2 Homoclinic Orbit

Anharmonic systems with a homoclinic orbit play a special role in the study ofchaotic systems. By definition, these orbits tend to the same fixed point for botht —¥ oo and t —> — oo. As an example we consider the anharmonic system

(fu u 9 ,

Introducing U\ := u and u% := du/dt we obtain the autonomous system in the plane

dill dU2 U\ 2 3

^T = U2' ~dT = J~Ul~Uv

The three fixed points of this system are given by

K,^) = (o,o), K,u;) = (-(>/3 + i)/2,o)) («!,«;) = ((>/3-i)/2,o).

The half plane Wi > 0 contains a unique homoclinic orbit F(t) = (wi(i), «2(*)) givenexplicitly by

2et/v/5

The fixed point (0,0) is a hyperbolic fixed point and the other two fixed points areelliptic. For the fixed point (0,0) the eigenvalues of the functional matrix are realand for the two other fixed points the eigenvalues are purely imaginary.

In the program Homoclinic .Java we apply the Lie series technique to calculate thehomoclinic orbit of the differential equation given above.

// Homoclinic.Java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Homoclinic extends Frame

{public Homoclinic()

•C

setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); >});}

public void paint(Graphics g){

Page 133: Nonlinear Workbook (3rd)

114 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

double t = 0.0001; double count =0.0;double ull = -0.001, u22 =0.001; // initial valuesdouble ul, u2;double V2;

g.drawRect(40,40,500-40,400-40);g.drawLine(40,220,500,220); g.drawLine(400,40,400,400);while(count < 42.5){ul = ull; u2 = u22;V2 = ul*(0.5 - ul - ul*ul);ull = ul + t*u2 + t*t*V2/2.0;u22 = u2 + t*(ul/2.0 - ul*ul - ul*ul*ul) +

t*t*u2*(0.5 - 2.0*ul - 3.0*ul*ul)/2.0;int ml = (int) Math.floor(150*ul + 400);int nl = (int) Math.fIoor(150*u2 + 220);int m2 = (int) Math.floor(150*ull + 400);int n2 = (int) Math.floor(150*u22 + 220);g.drawLine(ml;nl,m2,n2);count += t;} // end while} // end paint

public static void main(String[] args)

{Frame f = new HomoclinicO;f.setVisible(true);}

>

3.3 Pendulum

The equation for the pendulum is given by

g+w2sin(u)=0, J:=9-

where L is the length of the pendulum, g the acceleration due to gravity, and u is theangular displacement of the pendulum from its position of equilibrium. Introducingthe quantities u(t{t)) = u(t), t(t) = cut we can write the pendulum equation indimensionless form

d?u . ._.

-jfr + s m(«) = ° •We omit the tilde in the following. Introducing the quantity u\ = u and u^ =du/dt = dui/dt we obtain the autonomous first order system

du\ du2 . , .— - = u2, —- = - s i n (w i ) .dt at

Page 134: Nonlinear Workbook (3rd)

3.3. PENDULUM 115

Thus we obtain the fixed points (nn, 0), n 6 Z. The variational equation is given

bydvi dv2 . s.

Inserting the fixed points (nir, 0) into these equations we find

dv\ __ dv2 j —v\ if n even~dt ~ V2' ~dt ~ \ vi if n odd

In the first case the eigenvalues are i, —i. Thus we have a centre. In the second casethe eigenvalues are 1 , - 1 . Thus we have an unstable node.

In the Java program Pendulum. j ava we integrate the dynamical system using asymplectic integrator.

// Pendulum.java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Pendulum extends Frame

•Cpublic Pendulum()

isetSize(400,300);addWindowListener(new WindowAdapterQ{. public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void paint(Graphics g)

{double t = 0.001;int steps = 20000;double ql, pi;double q = 1.0, p = 1.75; // initial valuesdouble mu = 5.0; // parameter value

for(int i=0;i<steps;i++)

{ql = q; pi = p;q = ql + t*pl; p = pi - t*Math.sin(q);int mxl = (int) Math.floor(40*ql + 250 + 0.5);int nyl = (int) Math.floor(40*pl + 350 + 0.5);int mx = (int) Math.floor(40*q + 250 + 0.5);

Page 135: Nonlinear Workbook (3rd)

116 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

int ny = (int) Math.floor(40*p + 350 + 0.5);g.drawLine(mxl,nyl,mx,ny);}}

public static void main(String[] args)

•C

Frame f = new Pendulum();f.setVisible(true);>

}

3.4 Limit Cycle Systems

Consider the autonomous system of first order differential equations in the plane

LetF(u) = fo

Uf(s)ds.

Assume that / is an even and g an odd function, both are continuous for all u andg satisfies the Lipschitz condition. Assume further that

/(0) < 0, ug(u) > 0 for u =£ 0, F(u) -> ±oo if u ->• oo

and that / has a single zero at u = b (u > 0) and is monotonically increasing foru> b. Then the system

dui du2 ,, . , .-Tr = u2, -nr = -f{ui)u2-g(ui)at dt

has a stable limit cycle. As our first example we consider the famous Van der Polequation

dui du2 , 2.— =U2, — = -Ui+M(l-U1)u2.

The Van der Pol equation admits only one fixed point at the origin, namely (0,0).The variational equation is given by

dV\ di>2 / , „ \ ,, ON— - = V2, — = -{l+2fJ,UiU2)Vi + H(l-ui)V2.at at

Inserting the fixed point (0,0) yields

dvi dv2-TT=V2, -jT = -Vl + VV2 •

dt at

Page 136: Nonlinear Workbook (3rd)

3.4. LIMIT CYCLE SYSTEMS 117

Thus for \x > 0 the eigenvalues have a positive real part. Hence the fixed point (0,0)is unstable.

In the C++ program vdpol. cpp we find the phase portrait of the Van der Polequation using the Runge-Kutta technique. The parameter value is fj, = 5.0.

/ / vdpol.cpp

#include <fstream>using namespace std;

const int N = 2;

void fsystem(double h,double t,double u[N].double hf[N]){

double mu = 5.0;hf [0] = h*u[l ] ;hf [ l ] = h*(-u[0] + mu*(1.0 - u[0]*u[0] )*u[ l ] ) ;

}

void map(double u[N],int steps,double h,double t ){

double uk[N] ;double tk;double a[6] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[6] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[6] [5];b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0][4] = 0.0;b[ l ] [0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0.0; b[ l] [3] = 0.0;b[ l ] [4] = 0.0;b[2][0] = 3.0/32.0; b[2][ l ] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0;b[4] [4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;b[5] [4] = -11.0/4.0;

double f [6] [N];int i , j , 1, k;

Page 137: Nonlinear Workbook (3rd)

118 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

f or (i=0; i<steps; i++){

fsystem(h,t,u,f [0]);

for(k=l;k<=5;k++)

itk = t + a[k]*h;

for(l=0;KN;l++)

{

uk[l] = u[l];

for(j=0;j<=k-l;j++)

uk[l] += b[k][j]*f[j][l];

}

fsystem(h,tk.uk,f[k]);

}

for(l=0;KN;l++)

for(k=0; k<6; k++)

u[l] += c[k]*f[k][l];

}

}

int main(void)

{ofstream data;

data.openCphase_data.dat");

int steps = 1;double h = 0.005;double t = 0.0;double u[N] = { 0.1, 0.2 }; // initial conditions

int i;// wait for transients to decayfor(i=0;i<1000;i++)

{ t += h; map(u,steps,h,t); }

t = 0.0;for(i=0;i<40000;i++){

t += h;map(u,steps,h,t);data « u[0] « " " « u[l] « "\n";}data, close();return 0;

}

Page 138: Nonlinear Workbook (3rd)

3.4. LIMIT CYCLE SYSTEMS 119

As our second example we consider the system

du\ du2 . , .~dT=U2' - 5 r = -« i -A ' sm(« 2 ) .

The system shows infinitely many limit cycles. We assume that \i > 0. Then (0,0)is an unstable fixed point.

In the Java program LimitCycles .Java we calculate the phase portrait for differentinitial values. We consider two different initial conditions. One is close to the originand the other one is between the first and second limit cycle. We apply symplecticintegration.

// LimitCycles.Java

import java.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class LimitCycles extends Frame

ipublic LimitCycles()

{setSize(400,300);addWindowListener(new WindowAdapter()•i public void windowClosing(WindowEvent event){ System.exit(0); >});

>

public void paint(Graphics g)

idouble t = 0.005;int T = 10000; // number of iterationsdouble xl, yl;double x = 0.05, y = 0.01; // initial valuesdouble mu = 5.0; // parameter value

for(int i=0;i<T;i++)•C

xl = x; yl = y;x = xl + t*yi;y = yl*Math.exp(-t*mu*(x*x - 1.0)) - t*x;int mxl = (int) Math.floor(40*xl + 250 + 0.5);int nyl = (int) Math.floor(40*yl + 350 + 0.5);int mx = (int) Math.floor(40*x + 250 + 0.5);int ny = (int) Math.floor(40*y + 350 + 0.5);

Page 139: Nonlinear Workbook (3rd)

120 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

g.drawLine(mxl,nyl,mx,ny);} // end for loop> // end paint

public static void main(String[] args){Frame f = new LimitCyclesO;f.setVisible(true);}

}

3.5 Lotka-Volterra SystemsLotka- Volterra systems describe the interaction of two (or more) competing species.The simplest model is given by

du\ , dui .= au\ — buiu2, = — cuo + ouiu-i

dt dt

where a, 6, c are positive constants. Owing to the term au\ the species 1 wouldgrow exponentialy. However, owing to the term —bu\U2 the species 1 will decrease.Similarly, for species 2 the term — cui will decrease exponentially but the term bu\U2

gives a growing contribution. Thus we expect that the solution is periodic aroundthe fixed point u\ = c/b, u2 = a/b. As an example we consider the special case(o = b = c = 1)

dui du2— = lii - WlU2, — = ~U2 + UXU2dt dt

where u\{t = 0) > 0 and u2(t = 0) > 0. Since we assume that Ui(t) > 0 andU2(t) > 0 we find that the system has one fixed point at (uj ,^) = (1,1). The fixedpoint is a centre. Thus in a neighbourhood of this fixed point the solutions areapproximatively circles. Integrating the equation

du\ du2

U\ — UiM2 —«2 + «1«2

yields the constant of motion

ln(«i) + ln(u2) - u i - U2 — C

oruxu2e~ule~U2 = d .

This constant can be used to check how accurate the integration procedure is.

Page 140: Nonlinear Workbook (3rd)

3.5. LOTKA-VOLTERRA SYSTEMS 121

/ / Lotka.cpp

#include <fstream>using namespace std;

const int N = 2;

void f system(double h,double t,double u[N] .double hf[N]){

hf[O] = h*(u[0] - u[O]*u[l]);hf [ l ] = h*(-u[l] + u[0]*u[l]) ;

}

void map(double u[N],int steps,double h,double t ){

double uk[N];double tk;double a[6] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[6] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[6][5];b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l ] [2] = 0.0; b[ l ] [3] = 0.0;b[ l ] [4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2][4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3][3] = b[3][4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0;b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5][ l ] = 2.0;b[5][2] = -3544.0/2565.0; b[5][3] = 1859.0/4104.0;b[5][4] = -11.0/4.0;

double f [6] [N] ;int i, j, 1, k;for(i=0;i<steps;i++)

{fsystem(h,t,u,f[0]);for(k=l;k<=5;k++){tk = t + a[k]*h;for(l=0;KN;l++){

Page 141: Nonlinear Workbook (3rd)

122 CHAPTER 3. AUTONOMOUS SYSTEMS IN THE PLANE

uk[l] = u[l] ;for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];}fsystem(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];>

}

int main(void){ofstream data;data.openCphase_data.dat");

int steps = 1;double h = 0.001; // step lengthdouble t = 0.0;double u[N] = { 2.0, 1.5 }; // initial conditions

for(int i=0;i<20000;i++){t += h;map(u,steps,h,t);data « u[0] « " " « u[l] « "\n";}data.closeO ;return 0;

>

In the following program we show that

ln(«i) + ln(u2) -Ui - u2 = C

is a constant of motion of the Lotka-Volterra system using SymbolicC++.

/ / firstintegral.cpp

#include <iostream>#include "msymbol.h"using namespace std;

int main(void)

{

Page 142: Nonlinear Workbook (3rd)

3.5. LOTKA-VOLTERRA SYSTEMS 123

Sum<double> f("f",O), ul("ul",O), u2("u2",0), t("t",O), r("r",O);f.depend(ul);f.depend(u2);ul.depend(t);u2.depend(t);f = ln(ul) + In(u2) - ul - u2;r = df(f ,t);cout « "r = " « r « endl;r.put(df(ul,t),ul-ul*u2);r.put(df(u2,t),-u2+ul*u2);cout « "r = " « r « endl;r.expand();cout « "r = " « r;return 0;

>

Page 143: Nonlinear Workbook (3rd)

Chapter 4

Nonlinear Hamilton Systems

4.1 Hamilton Equations of Motion

Let us consider a closed classical system with 3N degrees of freedom (for example,N particles in a three-dimensional box). The state of such a system is completelyspecified in terms of a set of 6N independent real variables (pN,qN){pN and qN

denote the set of vectors pN = (pi, p 2 , . . . , p#) and q" = (qi, q2, . . . , qjy), respec-tively; Pj and q are the momentum and position of the ith particle). If the statevector Xw = X.N(pN, qN) is known at one time, then it is completely determined forany other time from Newton's laws. If we can define a Hamilton function, H(X, t),for the system, then the time evolution of the quantities p ; and q,- (j = 1 , . . . , N)is given by Hamilton's equations of motion

dpj _ dH dqj _ dHdt dqj' dt dpj

If the Hamilton function does not depend explicitly on time, then it is a constantof motion

H (XN) = E

where E is the total energy of the system. In this case the system is called conser-vative. Let us now associate to the system a 6W-dimensional phase space, F. Thestate vector X N (p w ,q w ) then specifies a point in the phase space. As the systemevolves in time and its state changes, the system point XN traces out a trajectory inF-space. Since the subsequent motion of a classical system is uniquely determinedfrom the initial conditions, it follows that no two trajectories in phase space cancross. If they did, one could not uniquely determine the subsequent motion of thetrajectory. Let us consider the Hamilton system given above and write Hamilton'sequations in the form

dqu _ dqn _ dqi3 _ dq2\ _ _ dpn _ _ dpN3 _dH ~ dH ~ dH ~ dH ~""~ _dH ~ "" ~ _ dH~d~Pn Wu d~Pi3 3pn d~q^ dqm

124

Page 144: Nonlinear Workbook (3rd)

4.1. HAMILTON EQUATIONS OF MOTION 125

This equation provides us with 6N - 1 equations between phase space coordinateswhich, when solved, give us 6./V — 1 constraints or integrals of the motion

where Cj is a constant. However, these integrals of the motion can be divided intotwo kinds, isolating and nonisolating. Isolating integrals define a whole surface inthe phase space and are important in ergodic theory, while nonisolating integralsdo not define a surface and are unimportant. One of the main problems of ergodictheory is to determine how many isolating integrals a given system has. An exampleof an isolating integral is the total energy. For N particles in a box it is probablythe only isolating integral.

Let us consider a system for which the only isolating integral of the motion is thetotal energy and assume that the system has total energy E. Then trajectories inP-space (the iV-dimensional phase space) which have energy E will be restricted tothe energy surface SE- The energy surface SE is a (6JV— l)-dimensional "surface" inphase space. The flow of state points on the energy surface is defined to be ergodicif almost all points X.N (pN, qN) on the surface move in such a way that they passthrough every small finite neighbourhood RE on the energy surface. Or, in otherwords, each point samples small neighbourhoods over the entire surface during thecourse of its motion (a given point XJV(piV,qJV) cannot pass through every point onthe surface, because a line which cannot intersect itself cannot fill a surface of twoor more dimensions). Note that not all points need sample the surface, only "almostall". We can exclude a set of measure zero from this requirement.

We consider now the special case of a Hamilton function (H : R4 —> R) with twodegrees of freedom

H(p,<l):=l(p21+Pl) + V(qu<h).

The Hamilton equations of motion are given by

dgl_dH__ dPj _ dH _ dVdt ~ dpj ~Pj' dt ~ ~dqj ~ ~ dqj

where j = 1, 2. In the C++ program using SymbolicC++ we evaluate the Hamiltonequations of motion for the Henon-Heiles model

H{p, q) := ^(Pi +PI + QI + <&) + 9??2 - - ? | •

We find

dqi dq2 dpi dp2 22

~dl=Pu ~d7=P2> -dF = -Ql~2giq2> -dT = -Q2-qi+q*-

Page 145: Nonlinear Workbook (3rd)

126 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

II hamiltoneq.cpp

#include <iostream>#include "rational.h"#include "msymbol.h"using namespace std;

int main(void){Sum<Rational<int> > h("h",0);Sum<Rational<int> > ql("ql",O);Sum<Rational<int> > q2("q2",0);Sum<Rational<int> > plO'pl",0);Sum<Rational<int> > p2("p2",0);Sum<Rational<int> > ptl, pt2, qtl, qt2;Sum<Rational<int> > half(Rational<int>(1,2));Sum<Rational<int> > third(Rational<int>(1,3));// Hamilton functionh = half*(pl*pl+p2*p2+ql*ql+q2*q2)+ql*ql*q2-third*q2*q2*q2;// Hamilton equations of motionptl = -df(h,ql); pt2 = -df(h,q2);qtl = df(h,pl); qt2 = df (h,p2) ;cout « "dpl/dt = " « ptl «endl;cout « "dp2/dt = " « pt2 «endl;cout « "dql/dt = " « qtl «endl;cout « "dq2/dt = " « qt2 «endl;return 0;

}

4.1.1 Hamilton System and Variational EquationConsider the phase space

M : = R 2 W = {(p,q)}

where p = (pi, p2, •.., PN ) and q = (gi, q2,. •., gjv) • From the Hamilton function

ff(p,q)=4x>* + (q)

we obtain the Hamilton equation of motion

dt ~ dpj ~Pj' dt dqj ~ dqj

where j = 1,2,..., N. The variational equation is defined as

dJi-v.N <%+* .. f d*Vdt ~Vl+N' dt ~ f^dqidq?1

Page 146: Nonlinear Workbook (3rd)

4.1. HAMILTON EQUATIONS OF MOTION 127

where j = 1,2,...,N. In the C++ program using SymbolicC++ we evaluate thevariational equation for the Henon-Heiles model.

// hamiltonvar.cpp

#include <iostream>#include "rational.h"#include "msymbol.h"using namespace std;

int main(void)

iSum<Rational<int> > h("h",0);Sum<Rational<int> > ql("ql",0); Sum<Rational<int> > q2("q2",0);Sum<Rational<int> > pl("pl",O); Sum<Rational<int> > p2("p2",0);Sum<Rational<int> > ul("ul",0); Sum<Rational<int> > u2("u2",0);Sum<Rational<int> > vl("vl",0); Sum<Rational<int> > v2("v2",0);Sum<Rational<int> > q[2]={ql,q2};Sum<Rational<int> > p[2]={pl,p2};Sum<Rational<int> > u[2]={ul,u2};Sum<Rational<int> > v[2]={vl,v2};Sum<Rational<int> > qt[2]; Sum<Rational<int> > pt[2];Sum<Rational<int> > ut[2]; Sum<Rational<int> > vt[2];Sum<Rational<int> > half(Rational<int>(l,2));Sum<Rational<int> > third(Rational<int>(1,3));// Hamilton functionh = half*(p[0]*p[0]+p[l]*p[l]+q[0]*q[0]+q[l]*q[l])

+ q[O]*q[O]*q[l]-third*q[l]*q[l]*q[l] ;for(int j=0;j<2;j++)

•Cpt[j] = -df(h,q[j]); qt[j] = df(h,p[j]);cout « "dp" « j « "/dt = " « pt[j] «endl;cout « "dq" « j « "/dt = " « qt[j] «endl;>for(int j=0;j<2;j++){ut[j] = v[j]; vt[j] = Rational<int>(0);for(int 1=O;1<2;1++){ vt[j] += -df(df(h,q[j]),q[l])*u[l]; }cout « "du" « j « "/dt = " « ut[j] « endl;cout « "dv" « j « "/dt = " « vt[j] « endl;}return 0;

>

Page 147: Nonlinear Workbook (3rd)

128 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

4.2 Integrable Hamilton Systems

4.2.1 Hamilton Systems and First IntegralsConsider a Hamilton system

dqj _ dH dpj _ dHdt dpj dt dqj

where j = 1,2,..., TV. A smooth function /(p, q) is called a first integral of theHamilton system if

| / ( p , q ) = 0.

This equation can also be written as

A (dH dl dH dl\_JT[\dqjdpj dp j dqj) ~

In the C++ program we consider the three body periodic Toda lattice

H(p, q) = ^(Pi + & + Pi) + exP(?i ~ 92) + exp(92 - q3) + exp(g3 - qx).

For the first integrals we make the ansatz

A(p,q) = P i + P 2 + P s

and

^(P, q) = P1P2P3 - P\ exp(g2 - 93) - P2 exp(g3 - qx) - p3 exp(qx - q2).

We find that these functions are first integrals.

/ / hamiltonin.cpp

#include <iostream>#include "rational.h"#include "msymbol.h"using namespace std;

int main(void)

{Sum<Rational<int> > h("h",0);Sum<Rational<int> > ql("ql",0);Sum<Rational<int> > q2("q2",0);Sum<Rational<int>' > q3("q3",0);Sum<Rational<int> > pl("pl",0);Sum<Rational<int> > p2("p2",0);

Page 148: Nonlinear Workbook (3rd)

4.2. INTEGRABLE HAMILTON SYSTEMS 129

Sum<Rational<int> > p3("p3",0);Sum<Rational<int> > q[3] = {ql,q2,q3};Sum<Rational<int> > p[3] = {pl,p2,p3};Sum<Rational<int> > pt[3] ,qt[3] ,I1,R1,I2,R2;Sum<Rational<int> > zero(Rational<int>(0,1));Sum<Rational<int> > half(Rational<int>(1,2));int j, 1;// Hamilton functionh = half*(p[0]*p[0]+p[l]*p[l]+p[2]*p[2])

+exp(q[0]-q[l])+exp(q[l]-q[2])+exp(q[2]-q[0]);

for(j=0;j<3;j++)

{pt[j] = -df(h,q[j]); qt[j] = df(h,p[j]);cout « "dp" « j « "/dt = " « pt[j] « endl;cout « "dq" « j « "/dt = " « qt[j] « endl;}11 = p[0]+p[l]+p[2];Rl = zero;for(j=0;j<3;j++) Rl += pt [j]*df (Il,p[j] )+qt [j] *df (Il,q[j] ) ;if(Rl == zero) cout « "II is a first integral." «endl;else cout « "II is not a first integral." «endl;

12 = p[0]*p[l]*p[2]-p[0]*exp(q[l]-q[2])-p[l]*exp(q[2]-q[0])-p[2]*exp(q[0]-q[l]);

R2 = zero;for(j=0;j<3;j++) R2 += pt[j]*df (I2,p[j])+qt[j]*df (I2,q[j]);if(R2 == zero) cout « "12 is a first integral." « endl;else cout « "12 is not a first integral." « endl;return 0;

}

4.2.2 Lax Pair and Hamilton Systems

A number of Hamilton systems can be written in the form

where L and A are time-dependent n x n matrices. This is called the Lax repre-sentation of the Hamilton system. As an example consider the Hamilton function(Toda lattice)

H{p, q) = -{p\ + p\ + pi) + exp(ft - q2) + exp(g2 - q3) + exp(q3 - ft).

Page 149: Nonlinear Workbook (3rd)

130 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

Introducing the quantities

ai ~ g e x p (2 (Qi ~ 9 j + 1V ' bj : = 2Pj

and cyclic boundary conditions (i.e., q^ = qi) we find that the Hamilton equationsof motion take the form (with 63 = 0)

where j = 1,2. Introducing the matrices

( 61 ai 0 \ / 0 - a i 0 \

ai b2 a2 \ , A := \ a% 0 — o2

0 a2 63 / V 0 a2 0 /the equations of motion can be written as Lax representation. From L we find thefirst integral as tr(L"), where n = 1,2,..., where tr denotes the trace. We obtain

trL = bi + b2 + 63tr(L2) = b2

1+b22 + bj + 2a2

1 + 2a22.

II Lax.cpp

#include <iostream>#include "msymbol.h"#include "matrix.h"#include "rational.h"using namespace std;

int main(void)

{Matrix<Sum<Rational<int> > > L(3,3), A(3,3), Lt(3,3); // Lt=dL/dtSum<Rational<int> > al("al",0), a2("a2",0),

bl("bl",0), b2("b2",0), b3("b3",0),alt, a2t, bit, b2t, b3t;

Rational<int> zero(0);

L[0][0] = bl; L[0][l] = al; L[0][2] = zero;L[l][0] = al; L[1][1] = b2; L[l] [2] = a2;L[2][0] = zero; L[2][l] = a2; L[2][2] = b3;A[0][0] = zero; A[0][l] = -al; A[0][2] = zero;A[l][0] = al; A[1][1] = zero; A[l][2] = -a2;A[2][0] = zero; A[2][l] = a2; A[2] [2] = zero;

Lt = A*L - L*A;

Page 150: Nonlinear Workbook (3rd)

4.2. INTEGRABLE HAMILTON SYSTEMS 131

cout « "Lt =\n" « Lt « endl;

bit = Lt [0] [0] ; b2t = Lt [1] [1] ; b3t = Lt [2] [2] ;

alt = Lt[O][l]; a2t = Lt[l][2];cout « "bit = " « bit « ", b2t = " « b2t

« ", b3t = " « b3t « endl;cout « "alt = " « alt « ", a2t = " « a2t « endl;cout « endl;

// Show that I[0] ,I[1] ,I[2] are first integralsint n = 3;Sum<Rational<int> > result;Vector<Sum<Rational<int> > > I(n);

I[0] = L.traceO; cout « "I[0] = " « I[0] « endl;I[l] = (L*L). trace (); cout « "I[l] = " « I[l] « endl;I[2] = L.determinant(); cout « "I[2] = " « I[2] « endl;cout « endl;

for(int i=0;i<n;i++)

{result = blt*df (I[i],bl) + b2t*df (I[i] ,b2) + b3t*df (I[i] ,b3)

+ alt*df(I[i],al) + a2t*df (I[i] ,a2) ;

cout « "result" « i+1 « " = " « result « endl;}return 0;

}

The output is:

Result

Lt =[-2*al~(2) -al*b2+bl*al 0][al*bl-b2*al 2*al~(2)-2*a2~(2) -a2*b3+b2*a2][0 a2*b2-b3*a2 2*a2"(2)]

bit = -2*al~(2), b2t = 2*al~(2)-2*a2~(2), b3t = 2*a2~(2)alt = -al*b2+bl*al, a2t = -a2*b3+b2*a2

I[0] = bl+b2+b3I[l] = bl-(2)+2*al~(2)+b2-(2)+2*a2-(2)+b3~(2)I[2] = bl*b2*b3-bl*a2~(2)-al"(2)*b3

Page 151: Nonlinear Workbook (3rd)

132 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

resultl = 0result2 = 0result3 = 0

4.2.3 Floquet Theory

The Floquet theory has been developed to treat systems of linear differential equa-tions with periodic coefficients. Consider the system of differential equations

— = A(t)u(t), u=(ui,u2,...,un)T

where A(t) = A(t + T) for some T > 0 and all t. The transition matrix $(i, t0) isdefined by

where $(£, t) — I for all t and / denotes the n x n unit matrix. We find that

*(t + T,t<,) = *(Mo)C

where C is a constant matrix. Then there exists a constant matrix RT such thatC = exp(RT), where T is the period. We can prove that

$(<o + r , i o )=exp ( i ? r ) .

Consequently, the state-transition matrix, and the solution to du/dt = A(i)u, con-sists of a periodically modulated exponential matrix function. Therefore the systemof differential equations du/dt = A{t)u is asymptoticaly stable if the eigenvalues ofR all have negative real parts or that

det(/A - exp(RT)) = 0

implies that |Aj| < 1 for all j = 1,2,..., n. The eigenvalues of R are the charac-teristic exponents of A(t), and Xj are called that characteristic multipliers of A(t).The eigenvalues of R are related to the eigenvalues of exp(-RT) by

In A,ft = -jT •

As a special case consider the Hill equation

where a(t + T) = a(t), T > 0 for all t. Then with ux = u, u2 = du/dt we havedu/dt = A(t)u, where

Page 152: Nonlinear Workbook (3rd)

4.2. INTEGRABLE HAMILTON SYSTEMS 133

We find that( ft+T \

AiA2 = exp I / trA(s)ds I = 1 •\Jt J

The characteristic equation of A is given by

A2 - tr(C)A + 1 = 0.

Thus if |trC| > 2 we find that A/s are real and the system is stable. If |trC| < 2 wefind that the A/s are complex conjugate with | A_,-1 = 1. The system is stable, butnot asymptotically stable. If |trC| = 2 we have Ai — X2 — ±1.

As an example let us consider a Mathieu's equation with coefficients having period

<Pu— + (<5 + 2ecos(2i))u = 0at'-

where 5 and e are constants. Since this is a linear second-order homogeneous differ-ential equation there exists two linear independent solutions u\{t), u2(t) which arecalled a fundamental set of solutions. Any other solution is a linear combination of«i and u2.

u1(t + T) = auu1{t) + auu2{t)

u2(t + T)= a2iUi(t) + a22«2(<) •

The theorem of Floquet states that

Ul (t + T) = Aim (t), u2{t + T) = A2w2 (*) •

ConsequentlyUj(t + nT) = XjUj(t) for all integers n.

Therefore as t —¥ 00 we find

M f 0 if |A,| < 1

If A = +1, then u has period T, if A = - 1 , then u has period IT.

The Java program Floquet. Java first computes a fundamental set of solutions ofthe Mathieu equation by numerical integration for two independent initial conditionsover one period T = -K. Then it determines the matrix A = (<jy) and calculates theeigenvalues Ai, A2. The parameters are set to S — 1, and e = 0.5. We can find theregions in the (6, e)-plane which correspond to bounded solutions (Strutt diagram).As described above we have AiA2 = 1.

// Floquet.Java

public class Floquet

Page 153: Nonlinear Workbook (3rd)

134 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

ipublic static void main(String[] args){double delta = 1.0;double eps = 0.5;double dt = Math.PI/100.0;double dt2 = dt*dt;

double [] du = new double [2] ;double [] [] u = new double [2] [3] ;double [] [] A = new double [2] [2] ;double [] [] B = new double [2] [2] ;

u[0][l] = 1.0; u[0] [2] = 1.0; / / ini t ial value = 1, slope = 0.0;u[l] [1] = 0.0; u[l][2] = dt; / / ini t ia l value = 0, slope = dt

double [] [] uu = new double [2] [2] ;uu[0][0] = u[0][l];uu[0][l] = u[ l ] [ l ] ;uu[l][0] = (u[0][2] - u[O][l])/dt;uu[l][l] = (u[l][2] - u[ l][ l ]) /dt ;

for(int i=0;i<=l;i++)

{

double t = dt;do{u[i][0] =u[ i ] [ l ] ; u[i][l] =u[i][2];u[i][2] = 2.0*u[i][l] - u[i][0]

-dt2*(delta + 2.0*eps*Math.cos(2.0*t))*u[i] [1] ;t += dt;>while(t <= Math.PI);du[i] = (u[i][2] - u[i][0])/(2.0*dt);} / / end for loop

B[0][0] = u[0][l]; B[0][l] = u[l] [1];B[l][0] = du[0]; B[1][1] = du[l];

double [] [] uui = new double [2] [2] ; / / inverse of uudouble detuu = uu[0] [0]*uu[l] [1] - uu[0] [I]*uu[l] [0];uui[0][0] = uu[l][l] /detuu;uui[0][l] = - uu[0][l]/detuu;uui[l][0] = - uu[l][0]/detuu;uui[l][l] = uu[0][0]/detuu;

Page 154: Nonlinear Workbook (3rd)

4.3. CHAOTIC HAMILTON SYSTEMS 135

A[0][0] = B[O][O]*uui[O][O] + B[0] [ l]*uui[ l] [0];A[0][l] = B[0] [0] *uui [0] [1] + B[0] [ l ]*uu i [ l ] [ l ] ;A[l][0] = B[l][0]*uui[0][0] + B[ l ] [ l ]*uu i [ l ] [0 ] ;A[l][l] = B[l][0]*uui[0][l] + B[ l ] [ l ]*uui [ l ] [ l ] ;

double D = A[0] [1]*A[1] [0] - A[0] [0]*A[l] [1]+ (A [0] [0] +A [1] [1]) * (A [0] [0] +A [1] [1]) /4 .0;

double lambdal, Iambda2;

if(D >= 0.0){lambdal = (A[0] [0]+A[l] [ l ] ) /2 .0 + Math.sqrt(D);Iambda2 = (A[0] [0]+A[l] [ l ] ) /2 .0 - Math.sqrt(D);System.out.println("lambdal = " + lambdal);System.out.println("lambda2 = " + Iambda2);}if(D < 0.0)idouble real = (A[0] [0]+A[l] [i])/2.0;double imag = Math.sqrt(-D);System.out.println("real part lambdal = " + real);System.out.println("imaginary part lambdal = " + imag);System.out.println("real part lambdal = " + real);System.out.println("imaginary part lambdal = " + -imag);}

} // end main

>

4.3 Chaotic Hamilton Systems

4.3.1 Henon-Heiles Hamilton Function and Trajectories

We consider the Henon-Heiles Hamilton function

H(p, q) = -(p? + V\ + q\ + ql) + q\qi - -Q23 .

The Hamilton equations of motion are given by

dqi dq2

dpi dp2 22

Page 155: Nonlinear Workbook (3rd)

136 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

Henon and Heiles studied the bounded motion of orbits for this system. Thetrajectories move in a four-dimensional phase space but are restricted to a three-dimensional surface because the energy is a constant of motion

It is possible to study a two-dimensional cross-section of the three-dimensional en-ergy surface (see next section). With growing energy the system becomes more andmore chaotic. At an energy of E — 0.16667 almost no stable motion remains.

In the Java program Heilestime. Java we evaluate the time evolution of q\ withthe initial values ?i(0) = 0.5, q2(Q) = 0.1, pi(0) = 0.1, and p2(0) - 0.1.

// Heilestime.Java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Heilestime extends Frame

{public HeilestimeO

{setSize(600,500);addWindowListener(new WindowAdapterO

{ public void windowClosing(WindowEvent event)

{ System.exit(0); }});

>

public void paint(Graphics g)

{

g.drawLine(10,10,10,400); g.drawLine(10,200,630,200);g.drawRect(10,10,630,400);double t = 0.005; double t2 = t*t;double count = 0.0;double xll = 0.5; double x22 =0.1; // initial valuesdouble x33 =0.1; double x44 =0.1; // initial valuesdouble xl, x2, x3, x4;do{xl = xll; x2 = x22; x3 = x33; x4 = x44;xll = xl+t*x3+t2*(-xl-2.0*xl*x2)/2.0;x22 = x2+t*x4+t2*(-x2-xl*xl+x2*x2)/2.0;x33 = x3+t*(-xl-2.0*xl*x2)+t2*(-x3-2.0*x3*x2-2.0*x4*xl)/2.0;

X44 = x4+t*(-x2-xl*xl+x2*x2)+t2*(-x4-2.0*xl*x3+2.0*x2*x4)/2.0;int m = (int)(5.0*count+10.0+0.5); int n = (int)(200.0-200*xl+0.5);

Page 156: Nonlinear Workbook (3rd)

4.3. CHAOTIC HAMILTON SYSTEMS 137

int p = (int)(5.0*(count+t)+10+0.5); int q = (int)(200-200*xll+0.5);g.drawLine(m,n,p,q);count = count + t;> while(count < 124.0);} // end paint

public static void main(String[] args){Frame f = new HeilestimeO ;f.setVisible(true);

}>

4.3.2 Henon Heiles and Surface of Section MethodThe surface-of-section method (also called Poincare section method) is particularlysuited for Hamilton systems with two degrees of freedom. In this technique onefollows the successive crossings of a trajectory through a surface intersecting theenergy shell, for example the (^2,92) plane at the point

<h = 0 •

The position of the system, at a given energy, on such a surface completely specifiesits state to within a sign. This is, of course, only true for Hamilton systems with twodegrees of freedom which are also quadratic in the momenta. After a large numberof crossings a pattern emerges. In the case of quasiperiodic motion the crossingpoints appear to lie on a smooth curve. This curve is often called an invariantcurve. It corresponds to the intersection of the torus, on which the trajectory lies,with the surface of section. Should the trajectory under consideration be one thatdoes not close upon itself (irrational winding number), then the successive pointseventually fill up the curve densely. This is a manifestation of the fact that suchorbits are ergodic on the torus. On the other hand, in the case of an orbit beingclosed, that is, a torus of commensurable frequencies, one only sees a finite numberof fixed points. If the trajectory is irregular, no such pattern emerges. One sees a"random splatter" of points that fill up some area.

In the program HeilesPoincare. Java we consider the Henon Heiles model. Weevaluate the surface of section for the energy E — 1/12 for two different initialconditions. The initial conditions are

Qi(t = O) = ^=, Q2(t = 0) = 0, P l ( i = 0 ) = 0 , p2(t = 0) = - L

and

qi{t = 0) = 0, ffi!(t = 0) = 0, Pl{t = 0) = ~ , pj(t = O) = O.

We find two invariant curves.

Page 157: Nonlinear Workbook (3rd)

138 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

II HeilesPoincare.java

import java.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class HeilesPoincare extends Frame

•Cpublic HeilesPoincare()

{setSize(640,480);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});

}

public void paint(Graphics g)

{g.drawRect(20,20,600,400);g.drawLine(20,210,600,210);g.drawRect(310,20,310,400);

// we identify ql -> xl, q2 -> x2, pi -> x3, p2 -> x4double t = 0.005;double xl [] = new double [3] ; double x2 [] = new double [3] ;double x3 [] = new double [3] ; double x4 [] = new double [3] ;

// initial condition Ix l [ l ] = Math . sq r t ( 1 . 0 / 8 . 0 ) ; x2 [ l ] = 0 .0 ;x3 [ l ] = 0 .0 ; x 4 [ l ] = Math . sq r t ( 1 .0 /24 .0 ) ;// initial condition IIxl[2] = 0.0; x2[2] = 0.0;x3[2] = M a t h . s q r t ( 1 . 0 / 6 . 0 ) ; x4[2] = 0.0;

f o r ( i n t j=0; j<2; j++)

{double count = 0.0;

do

{xl[0] = xl[j+l]; x2[0] = x2[j+l];x3[0] = x3[j+l]; x4[0] = x4[j+l];xl[j+l] = xl[0] + t*x3[0];x2[j+l] = x2[0] + t*x4[0];x3[j + l] = x3[0] - t*(xl[j + l] + 2.0*xl[j+l]*x2[j + l ] ) ;x4[j+l] = x4[0] - t*(x2[j+l] + xl[j+l]*xl[j+l] - x2[j+l]*x2[j+l]);

Page 158: Nonlinear Workbook (3rd)

4.3. CHAOTIC HAMILTON SYSTEMS 139

i f ( ( x l [ O ] * x l [ j + l ] < 0.0) && (x l [0 ] > 0 .0 ) ){double scale = 400.0;

i n t m = ( i n t ) ( sca le*x2[ j+ l ] + 310 + 0 . 5 ) ;i n t n = ( i n t ) ( - s ca l e*x4[ j+ l ] + 210 + 0 . 5 ) ;g.drawLine(m,n,m,n);

>count = count + t;} while(count < 4000.0);

}>

public static void main(String[] args)•f

Frame f = new HeilesPoincareO;f.setVisible(true);>

}

4.3.3 Quartic Potential and Surface of Section TechniqueWe consider the quartic Hamilton function

H (P, q) = \{p\ + p\) + ^ ( « I + 4) + \<lU

where r e [0,1]. For r = 0 the system is completely integrable with all motion lyingon invariant tori. The second first integral besides the Hamilton function is

7(p, q) = 3p!P2 + qig2(q2 + 92) •

With increasing r the system becomes more and more chaotic. For the Hamiltonequations of motion we find

dqi dq2

dpi 1 - r 3 2 dP2 1 - r 3 2

-dT = — 3 ~ 9 1 ~ gig2> la = — 3 ~ 9 2 - 9 l 9 2 'For r = 1 the system is almost completely chaotic, although there is a very smallregion with regular behaviour. In this case, i.e. r = 1, the system can be derivedfrom Yang-Mills theory (Steeb [88]).

In the Java program QuarticPotential. Java we evaluate the surface of section forr = 0.15 and the initial values pi(0) = 1.5, p2(0) = 1.0, <h(0) = 1.0, q2{0) = 1.0.

Page 159: Nonlinear Workbook (3rd)

140 CHAPTER 4. NONLINEAR HAMILTON SYSTEMS

II QuarticPotential.Java

import j ava.awt.*;import Java.awt.event.*;import j ava.awt.Graphi c s;

public class QuarticPotential extends Frame{public QuarticPotential()

•C

setSize(600,500);addWindowListener(new WindowAdapterO{. public void windowClosing(WindowEvent event){ System.exit(0); >});>

public void func_system(double h,double t,double u[].double hf[]){double r = 0.15;hf[O] = h*(-(1.0-r)*u[2]*u[2]*u[2]/3.0 - u[2]*u[3]*u[3]) ;hf [ l ] = h*(-(1.0-r)*u[3]*u[3]*u[3]/3.0 - u[2]*u[2]*u[3]);hf[2] = h*u[0]; hf [3] = h*u[l] ;>

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0.0; b[ l][3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2][4] = 0.0;b[3][0] = 1932.0/2197.0; b[3][ l ] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0; b[5] [2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5] [4] = -11.0/40.0;double f [] [] = new double [6] [N];int i , j , 1, k;

Page 160: Nonlinear Workbook (3rd)

4.3. CHAOTIC HAMILTON SYSTEMS 141

f or (i=0; Ksteps; i++){func_system(h,t,u,f[0]);for(k=l;k<=5;k++){tk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u[l];for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>func_system(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}}

public void paint(Graphics g)

{g.drawRect(30,30,570,400);int steps = 1; int N = 4;double h = 0.005; double t = 0.0;double u[] ={1.5, 1.0, 1.0, 1.0}; // initial conditionsfor(int i=0;i<20000000;i++)

{t += h;double temp = u[2] ;map(u,steps,h,t,N);if(((temp*u[2]) < 0.0) && (temp > 0.0))

{int m = ((int)(200.0*u[3])+300); int n = ((int)(250.0*u[l])+600);g.drawLine(m,n,m,n);

}}

>

public static void main(String[] args)•f

Frame f = new QuarticPotentialO;f.setVisible(true);}

}

Page 161: Nonlinear Workbook (3rd)

Chapter 5

Nonlinear Dissipative Systems

5.1 Fixed Points and Stability

We suppose that the dynamical behaviour of the system is modeled by the solutioncurves of a system of differential equations (or dynamical system)

^ = f(u), f : U -> R"

where U is an open subset of Rn. We suppose f is C1. A point u* € U is called afixed point (also called equilibrium point or stationary point) of the dynamical systemif

f(u') = O.

By uniqueness of solutions, no other solution curve can pass through u*. Let

$,:U-tR"

be the flow associated with the dynamical system. The set U C Rn is an open set,and for each u € U the map t —> $(£, u) = $t(u) is the solution passing through uwhen t = 0; it is defined for t in some open interval. If u* is a fixed point, then

$t(iO = u*

for all t € R. Another name for u* is a singular point of the vector field f. Supposef is linear. We write f(u) = Au, where A is a linear operator on R™. Then theorigin 0 s Rn is a fixed point. When A < 0 is greater than the real parts of theeigenvalues of A, then solutions $t(u) approach 0 exponentially

|*t(u)| < Cext

for some C > 0. Now suppose f is a C1 vector field with fixed point 0 £ R". Wethink of the derivative

Di (0) = A

142

Page 162: Nonlinear Workbook (3rd)

5.1. FIXED POINTS AND STABILITY 143

of f at 0 as a linear vector field which approximates f near 0. We call it the linearpart of f at 0. If all eigenvalues of Di(0) have negative real parts, we call 0 a sink.More generally, a fixed point u* is a sink if all eigenvalues of Di (u*) have negativereal parts. We also say the linear flow etA is a contraction. It can be shown that 0 isa sink if and only if every trajectory tends to 0 as t -> oo. This is called asymptoticstability. It follows that the trajectories approach a sink exponentially.

The following theorem (Hirsch [45], Arrowsmith [2]) says that a nonlinear sink u*behaves locally like a linear sink: nearby solutions approach u* exponentially.

Theorem. Let u* £ U be a sink of the dynamical system. Suppose every eigenvalueof Di(u*) has real part less than —c, c > 0. Then there is a neighbourhood N C Uof u* such that

(a) $t(u) is defined and in TV for all u £ TV, £ > 0.

(b) There is a Euclidean norm on R™ such that

| * t (u ) -u* | < e - c t | u - u * |

for all u € TV, t > 0.

(c) For any norm on Rn, there is a constant B > 0 such that

| * t ( u ) - u * | < B e - * | u - u * |

for all u e TV, t > 0.

In particular, $t(u) —> u* as t —> oo for all u £ JV.

A fixed point is stable if nearby solutions stay nearby for all future time. Sincein applications of dynamical systems one cannot pinpoint a state exactly, but onlyapproximately, a fixed point must be stable to be physically meaningful. The math-ematical definition is

Definition. Suppose u* € U is a fixed point of the dynamical system, wheref : U —> R" is a C1 map from an open set U of the vector space Rn into R™.Then u* is a stable fixed point if for every neighbourhood TV of u* in U there is aneighborhood N\ of u* in TV such that every solution u(t) with u(0) in TVi is definedand in N for all t > 0.

Definition. If N\ can be chosen so that in addition to the properties described inthe definition given above, lim^oo u(i) = u*, then u* is asymptotically stable.

Definition. A fixed point u* that is not stable is called unstable. This means thereis a neighbourhood N of u* such that for every neighbourhood TVi of u* in N, thereis at least one solution u(i) starting at u(0) £ Ni, which does not lie entirely in TV.

Page 163: Nonlinear Workbook (3rd)

144 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

A sink is asymptotically stable and therefore stable. An example of a fixed pointthat is stable but not asymptotically stable is the origin in Rn for a linear equationdu/dt = Au, where A has pure imaginary eigenvalues. The orbits are all ellipses.

Theorem. Let U C R" be open and f : U -»• R" continuously differentiate. Sup-pose f(u*) = 0 and u* is a stable fixed point of the equation du/dt = f (u). Thenno eigenvalue of Di(u*) has a positive real part.

We say that a fixed point u* is hyperbolic if the derivative £>f(u*) has no eigenvaluewith real part zero.

Corollary. A hyperbolic fixed point is either unstable or asymptotically stable.

Let

£ = f(»), uet/at

be such that f (u*) = 0, u* e U, where U is an open subset of Rn. Then thelinearization of du/dt = f (u) at u* is the linear system of differential equations

£ = «<»>where

and v = (vi,..., vn)T are local coordinates at u*.

Definition. A singular point u* of a vector field f is said to be hyperbolic if noeigenvalue of Di(u*) has a zero real part.

If u* is a singular point of f, then it is a fixed point of the flow of du/dt = f(u).Thus u* is a hyperbolic singular point of f if the flow, exp(Df(u*)t)u, of the lineari-sation of du/dt = f (u) is hyperbolic. It is sometimes convenient to distinguish thosenon-hyperbolic singular points for which Df(u') has at least one zero eigenvalue.Such points are said to be non-simple.

Theorem. (Hartman-Grobman) Let u* be a hyperbolic fixed point of du/dt —f (u) with flow $t : U C Rn -» R". Then there is a neighbourhood N of u* onwhich $( is topologically conjugate to the linear flow exp(.Df (u*)t)u.

The Invariant Manifold Theorem is also valid for flows. We have

W?oc := { u € U | $4(u)->u*}

as t —¥ oo andH^:={ne[/|$,(u)->u'}

Page 164: Nonlinear Workbook (3rd)

5.1. FIXED POINTS AND STABILITY 145

as t —> —oo. Global stable and unstable manifolds, W" and Wu, are defined bytaking a union over real t > 0 of $_t(W,gC) and $t(W;"c), respectively.

The following classifies hyperbolic fixed points.

Theorem. Let u* be a hyperbolic fixed point of du/dt = f (u) with flow $ t : U CR" -» Rn. Then there is a neighbourhood N of u* on which $( is topologicallyequivalent to the flow of the linear system of differential equations

^ = - u s l U . 6 R -at

dUu „ ,, <=R"-—— — U u , Uu fc JK. ,

at

where nu = n—ns. Here ns is the dimension of the stable eigenspace of exp(Df (u*)i).As an example, let us consider the stability of the fixed points of the Lorenz model.The Lorenz model is obtained as an approximation to partial differential equationsdescribing convection in a fluid layer heated below (Benard problem). The Lorenzmodel is given by

dui— =a{u2-Ul)du2— = -uiu3 + rux - u2atdu3— =uiu2 - bu3dt

where a, r and b are positive constants. The fixed points are determined by thesystem of algebraic equations

u*2-u\=Q

—u\u*z + ru\ — u*2 = 0u\u2 - bu\ = 0.

We find that u* = u*2 = u*3 = 0 is a fixed point for all a, r and b. This fixed pointexits for all parameter values of a, r and b. If r < 1 this fixed point is attracting (asink). If r becomes larger than 1, this fixed point loses its attracting character (oneeigenvalue becomes positive) and two new fixed points appear. For r > 1 we findthe fixed points

u\ = u*2 = ±yjb(r - 1), u*3 = r - 1.

The C++ program lorenzfix.cpp using SymbolicC++ finds the characteristicequation for the eigenvalues from the variational equations and then determine thestability of the fixed point (0,0,0). The parameter values are r = 40, a = 16and 6 = 4. For r > 1 the origin (0,0,0) becomes unstable, i.e. one of the threeeigenvalues becomes positive (while the other two remain negative).

Page 165: Nonlinear Workbook (3rd)

146 CHAPTERS. NONLINEAR DISSIPATIVE SYSTEMS

II lorenzfix.cpp

#include <iostream>ttinclude <cmath> // for cos, acos#include "msymbol.h"using namespace std;

int main(void){Sum<double> ul("ul",0), u2("u2",0), u3("u3",0);Sum<double> s("s",0), b("b",0), r("r",O);Sum<double> VI, V2, V3;Sum<double> All, A12, A13, A21, A22, A23, A31, A32, A33;

VI = s*(u2 - ul); // Lorenz modelV2 = -ul*u3 + r*ul - u2; // Lorenz modelV3 = ul*u2 - b*u3; // Lorenz model

All = df(Vl,ul); A12 = df(Vl,u2); A13 = df(Vl,u3);A21 = df(V2,ul); A22 = df(V2,u2); A23 = df(V2,u3);A31 = df(V3,ul); A32 = df(V3,u2); A33 = df(V3,u3);

Sum<double> lambda("lambda",0);Sum<double> chareq;

// characteristic equationchareq = (All - lambda)*(A22 - lambda)*(A33 - lambda)

+ A12*A23*A31 + A13*A21*A32 - A31*(A22 - lambda)*A13- A32*A23*(A11 - lambda) - (A33 - lambda)*A21*A12;

chareq = -chareq; // sign of lambda"3 = +1

// coefficients

Sum<double> cO = chareq.coeff(lambda,0);Sum<double> cl = chareq.coeff(lambda,1);Sum<double> c2 = chareq.coeff(lambda,2);

double two = 2.0; double three = 3.0; double nine = 9.0;

double n27 = 27.0; double n54 = 54.0;

Sum<double> Q =(Sum<double>(three)*cl - c2*c2)/(Sum<double>(nine));

Sum<double> R =(Sum<double>(nine)*c2*cl - Sum<double>(n27)*cO- Sum<double>(two)*c2*c2*c2)/(Sum<double>(n54));

Sum<double> D = Q*Q*Q + R*R;

Page 166: Nonlinear Workbook (3rd)

5.2. TRAJECTORIES 147

// parameter values

s.set(16.0); b.set(4.0); r.set(40.0);

// fixed point (0,0,0)ul.set(0.0); u2.set(0.0); u3.set(0.0);

double q = Q.nvalueO;double rR = R.nvalueO;double d = D.nvalueQ;double nc2 = c2.nvalue();

if(rR != 0 && q < 0 && d <= 0.0)

•[

double theta = acos(rR/sqrt(-q*q*q));double PI = 3.14159;double Iambi = 2.0*sqrt(-q)*cos(theta/3.0) - nc2/3.0;double Iamb2 = 2.0*sqrt(-q)*cos((theta+2.0*PI)/3.0)-nc2/3.0;double Iamb3 = 2.0*sqrt(-q)*cos((theta+4.0+PI)/3.0)-nc2/3.0;

cout « "Iambi = " « Iambi « endl;cout « "Iamb2 = " « Iamb2 « endl;cout « "Iamb3 = " « Iamb3 « endl;}return 0;

}

5.2 Trajectories

We evaluate the Ui(t) component of the trajectory for the Lorenz model with theinitial values

Ul(Q) = 0.8, u2(0) = 0.8, u3(0) = 0.8.

The parameter values are r = 40, a = 16 and b = 4. For these parameter valuesthere is numerical evidence that the system shows chaotic behaviour.

In our first C++ program Lie. cpp we use SymbolicC++ to evaluate symbolicallythe expansion of the Lie series up to second order with the vector field

V = a(u2 - Mi)- h (-U1W3 + rux - u2)-^ h (uiu2 - ^3)-^— •aui OU2 au3

Then we iterate numerically the resulting map. In the second program Lorenztime -Javawe use a Runge-Kutta-Fehlberg method to solve the Lorenz system numerically.

Page 167: Nonlinear Workbook (3rd)

148 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

II Lie.cpp

#include <iostream>#include "Vector.h"#include "MSymbol.h"using namespace std;

const int N = 3;

Vector<Sum<double> > u(N), ut(N);

// The vector field V

template <class T> T V(const T& ss)

{T sum(O);for(int i=0;i<N;i++) sum += ut[i]*df(ss,u[i]);return sum;

}

int main(void)

{int i,j;Sum<double> ul("ul",0), u2("u2",0), u3("u3",0), t("t",O),s("s",O), b("b",O), r("r",0),pl("pl",O), p2("p2",0), P3("p3",0);

Vector<Sum<double> > us(N), ug(N);double half =0.5;u[0] = ul; u[l] = u2; u[2] = u3;ug[0] = pl; ug[l] = p2; ug[2] = p3;

// Lorenz modelut[0] = s*(u2-ul);ut[l] = -u2 - ul*u3 + r*ul;ut[2] = ul*u2 - b*u3;

// Taylor series expansion up to order 2

for(i=0;i<N;i++)us[i] = u[i] + t*V(u[i]) + half*t*t*V(V(u[i]));

cout « "us =\n" « us « endl;

// Evolution of the approximate solutiont.set(O.Ol); r.set(40.0); s.set(16.0); b.set(4.0);ug[0].set(0.8); ug[l] .set(0.8); ug[2] .set (0.8);

Page 168: Nonlinear Workbook (3rd)

5.2. TRAJECTORIES 149

for(j=0;j<100;j++){for(i=0;i<N;i++)u[i] .set(ug[i] .nvalueO) ;for(i=0;i<N;i++){ug[i] .set(us[i] .nvalueO);cout « "u[" « i « "] = " « ug[i]. value O « endl;}cout « endl;}return 0;

}

The Runge-Kutta-Fehlberg method is used to solve the Lorenz model numerically.The state variable u[0] is displayed as a function of time.

// Lorenztime.java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Lorenztime extends Frame

{public Lorenztime0{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});>

public void func_system (double h,double t,double u[] .double hf[])

{double sigma = 16.0; double r = 40.0; double b = 4.0;hf[O] = h*sigma*(u[l] - u[0]) ;hf [ l ] = h*(-u[0]*u[2] + r*u[0] - u [ l ] ) ;hf[2] = h*(u[0]*u[l] - b*u[2]);>

public void map(double u [ ] , i n t steps,double h.double t . i n t N)idouble uk [] = new double [N] ;double tk;

Page 169: Nonlinear Workbook (3rd)

150 CHAPTERS. NONLINEAR DISSIPATIVE SYSTEMS

double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[l] [2] = 0.0; b[l] [3] = 0.0;b[ l ] [4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3][4] = 0.0;b[4][0] = 439.0/216.0; b[4][ l ] = -8 .0 ;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4] [4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;b[5][4] = -11.0/40.0;

double f [] [] = new double [6] [N] ;int i , j , 1, k;f or (i=0; Ks teps ; i++){func_system(h,t,u,f[0]);for(k=l;k<=5;k++)•C

tk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u[l];for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];}func_system(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}}

public void paint(Graphics g)

{g.drawLine(10,10,10,400);g.drawLine(10,200,630,200) ;

g.drawRect(10,10,630,400);

Page 170: Nonlinear Workbook (3rd)

5.3. PHASE PORTRAIT 151

int steps = 1; int N = 3;double h = 0.005;double t = 0.0;

double uQ = { 0.8, 0.8, 0.8}; // initial conditions

// wait for transients to decayfor(int i=0;i<1000;i++)

{

t += h;

map(u,steps,h,t,N);

}t = 0.0;for(int i=0;i<4800;i++){t += h;int m = (int) (25.0*t + 10.0 + 0.5);int n = (int) (200.0 - 6.0*u[0] + 0.5);map(u,steps,h,t,N);int p = (int)(25.0*(t+h) + 10 + 0.5);int q = (int)(200.0 - 6.0*u[0] + 0.5);g.drawLine(m,n,p,q);}}

public static void main(String[] args)

{Frame f = new LorenztimeO;f.setVisible(true);>

>

5.3 Phase Portrait

In the program Lorenzphase. Java we evaluate the phase portrait (ui(t),U2(t)) ofthe Lorenz model for the initial value

ui(0) = 0.8, u2(0)=0.8, w3(0) = 0.8

and the parameter values a = 16, r = 40 and 6 = 4.

/ / Lorenzphase.java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

Page 171: Nonlinear Workbook (3rd)

152 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

public class Lorenzphase extends Frame{public Lorenzphase(){setSize(600,500);addWindowListenerCnew WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});

}

public void func_system(double h,double t,double u[] .double hf[]){double sigma =16.0,r=40.0,b=4.0;hf[O] = h*sigma*(u[l] - u[0]) ;hf [ l ] = h*(-u[0]*u[2] + r*u[0] - u [ l ] ) ;hf[2] = h*(u[0]*u[l] - b*u[2]);}

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0. 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 >;double b[][] = new double [6] [5];b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0][4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[l] [2] = 0.0; b[ l] [3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0;b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;b[5][4] = -11.0/40.0;

double f [] [] = new double [6] [N] ;int i, j, 1, k;for(i=0;i<steps;i++)

{

Page 172: Nonlinear Workbook (3rd)

5.3. PHASE PORTRAIT 153

func_system(h,t,u,f[0]);for(k=l;k<=5;k++)-Ctk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u [ l ] ;for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>func_system(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}}

public void paint(Graphics g)

g.drawLine(10,10,10,400);g.drawLine(10,200,630,200);g.drawRect(10,10,630,400);

int steps = 1;int N = 3;double h = 0.005;double t = 0.0;double u[] = { 0.8, 0.8, 0.8 >; // initial conditions

// wait for transients to decayfor(int i=0;i<1000;i++)

{t += h;map(u,steps,h,t,N);}t = 0.0;for(int i=0;i<4800;i++){t += h;map(u,steps,h,t,N);int m = (int) (5.0*u[0] + 300);int n = (int) (5.0*u[l] + 200);g.drawLine(m,n,m,n);>

Page 173: Nonlinear Workbook (3rd)

154 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

}

public static void main(String[] args){Frame f = new LorenzphaseO;f.setVisible(true);}

>

5.4 Liapunov Exponents

Liapunov exponents provide a meaningful way to characterize the asymptotic be-haviour of a nonlinear dynamical system

^ = f (u), u(0) =110, u € R"

where f is continuously differentiable. They provide a generalization of the linearstability analysis of fixed points. For ergodic dynamical systems, the Liapunovexponents are the same for almost all initial conditions Uo with respect to anyinvariant measure for the flow. This means their values do not depend on a particulartrajectory. For a given solution trajectory u(t), one considers the linear variationalequation

^ = Dt{u)Y = A(t)Y, Y{0) = /at

where A(t) = di/dn is the Jacobian at u(i) and Y is an n x n time-dependentmatrix. Then, for a n x n fundamental solution matrix Y(t) the symmetric positivedefinite matrix

A : = l i m A U 0 ( t ) = H m ( y W W ) 1 / 2 t

is well-defined, where T denotes transpose. If

{Pj, H • j = l,2,...,n}

denote the eigenvectors and associated eigenvalues of A such that

Apj = PjHj or pjApj = fJ-j

then the one-dimensional Liapunov exponents with respect to the trajectory u(t)are given by

Ai = log(/i,-)=limilog||K(<)pJ-||1—rOO i

where j — 1,2, . . . ,n .

Page 174: Nonlinear Workbook (3rd)

5.4. LIAPUNOV EXPONENTS 155

As an example we consider the Lorenz model to find the largest one-dimensionalLiapunov exponent. The variational equation of the Lorenz model

dui— = a{u2-u{)du2—— = -U1U3 + rui - u2atdu3- 7 - = UXU2 - OU3

at

is given by

dv2 , .— = (-u3 + r)vx -v2- uxv3atdv3— — w2«i + «i«2 - bv3 .at

The largest one-dimensional Liapunov exponent is then given by

A(Ul(0),u2(0),us(0),^(0),v2{0),v3(0)) = ^irn i In | |v(T) | |

where ||. || denotes any norm in R3. We choose the norm

l|v|| = hl + H + hl-In the C++ program we evaluate the Liapunov exponent for the parameter valuesr = 40, a = 16 and 6 = 4. We find A « 1.37. It is conjectured that the methodyields the maximal one-dimensional Liapunov exponent. The accuracy of the one-dimensional Liapunov exponent could be improved when the transients have beendecayed.

// lorenzliapunov.cpp

#include <iostream>#include <cmath> // for fabs, log#include "vector.h"#include "msymbol.h"using namespace std;

const int N = 3;

Vector<Sum<double> >' u(N) , ut(N), y(N), yt(N);

// The vector field V

template <class T> T V(const T& ss)

Page 175: Nonlinear Workbook (3rd)

156 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

iT sum(O);for(int i=O;i<N;i++) sum += ut [i]*df (ss,u[i] );return sum;

}

template <class T> T W(const T& ss)

{T sum(O);

for(int i=0;i<N;i++) sum += yt[i]*df(ss,y[i]);return sum;

}

int main(void)

int i, j ;Sum<double> ul("ul",0), u2("u2",0), u3("u3",0), t("t",O),s("s",O), b("b",O), r("r",0),pl("pl",O), p2("p2",0), p3("p3",0),

yl("yl\O), y2("y2",0), y3("y3",0),ql("ql",O), q2("q2",0), q3("q3",0);

Vector<Sum<double> > us(N), ug(N), ys(N), yg(N);double half =0.5;u[0] = ul; u[l] = u2; u[2] = u3;ug[0] = pl; ug[l] = p2; ug[2] = p3;y[0] = yl; y[l] = y2; y[2] = y3;yg[O] = ql; yg[l] = q2; yg[2] = q3;

// Lorenz modelut[0] = s*(u2-ul);ut[l] = -u2 - ul*u3 + r*ul;ut[2] = ul*u2 - b*u3;

// variational equationsyt[O] = s*(y2 - yl) ;yt[l] = (-u3 + r)*yl - y2 - ul*y3;yt[2] = (u2*yl + ul*y2 - b*y3);

// Taylor series expansion up to order 2for(i=0;i<N;i++)us[i] = u[i] + t*V(u[i]) + Sum<double>(half)*t*t*V(V(u[i]));

for(i=0;i<N;i++)

ys[i] = y[i] + t*W(y[i]) + Sum<double>(half)*t*t*W(W(y[i]));

Page 176: Nonlinear Workbook (3rd)

5.5. GENERALIZED LOTKA-VOLTERRA MODEL 157

// Evolution of the approximate solutiont.set(O.Ol); r.set(40.0); s.set(16.O); b.set(4.0);

ug[0].set(0.8); ug[l] .set(0.8) ; ug[2] .set(0.8) ;yg[0].set(0.8); yg[l] .set(0.8); yg[2] .set(0.8);

int iter = 50000;for(j=0;j<iter;j++){for(i=0;i<N;i++){u[i] .set(ug[i] .nvalueO);y[i] .set(yg[i] .nvalueO);}for(i=0;i<N;i++){ug[i] .set(us[i] .nvalueO);yg[i] .set(ys[i] .nvalueO);}> / / end for loop j

double T = 0.01*iter;double lambda =(log(fabs(y1.nvalue 0)+fabs(y2.nvalue())+fabs(y3.nvalue 0)))/T;

cout « "lambda = " « lambda « endl;return 0;

}

5.5 Generalized Lotka-Volterra Model

The original Lotka- Volterra model can be generalized to higher dimensions n > 3.A three-dimensional generalization of the Lotka-Volterra model is given by

—r- —Ui- uiu2 + cu\ - au3ul

du2— = -u2 + u1u2atduz 2—T— = — ou3 + au\uiaz

where the relations between u\ and u2 form the Lotka-Volterra expressions, whilethe relations between ui and u3 generalize the latter in three dimensions, and wherea,b,c > 0. The parameters a, b and c are bifurcation parameters. In the Javaprogram Lotkaphase. Java we evaluate the phase portrait («i(i), «2(*))•

Page 177: Nonlinear Workbook (3rd)

158 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

II Lotkaphase.Java

import j ava.awt.*;import j ava.awt.event.*;import Java.awt.Graphics;

public class Lotkaphase extends Frame{public Lotkaphase0{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(O); >});>

public void func_system(double h,double t,double u[] .double hf []){double a = 2.9851; double b = 3.0; double c = 7.0;hf[O] = h*(u[0] - u[0]*u[l] + c*u[0]*u[0] - a*u[2]*u[0]*u[0]);hf[ l ] = h*(-u[l] + u[0]*u[l ] ) ;hf [2] = h* (-b*u [2] + a*u [0] *u [0] *u [2] ) ;}

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5];b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[ l ] [0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0.0; b[ l] [3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3][ l ] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0; b[4] [2] = 3680.0/513.0;b[4][3] = -845.0/4104.0; b[4] [4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0; b[5] [2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5] [4] = -11.0/40.0;

double f [] [] = new double [6] [N];

Page 178: Nonlinear Workbook (3rd)

5.5. GENERALIZED LOTKA-VOLTERRA MODEL 159

int i , j , 1, k;f or (i=0; Ks teps ; i++)-Cfunc_system(h,t,u,f[0]);for(k=l;k<=5;k++){tk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u[l];for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];

>func_system(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}}

public void paint(Graphics g)

{g.drawRect(10,10,630,400);int steps = 1;int N = 3;double h = 0.005;

double t = 0.0;double u[] = { 1.0, 1.5, 2.5 >; // initial conditions

// wait for transients to decayfor(int i=0;i<1000;i++)

•C

t += h; map(u,steps,h,t,N);

}

t = 0.0;for(int i=0;i<100000;i++)-Ct += h; map(u,steps,h,t,N);int m = (int) (300.0*u[0]-20.0); int n = (int) (280.0*u[l]-60.0);g.drawLine(m,n,m,n);}}

Page 179: Nonlinear Workbook (3rd)

160 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

public static void main(String[] args){Frame f = new LotkaphaseO ;f.setVisible(true);}

}

5.6 Hyperchaotic Systems

For autonomous systems of first-order ordinary differential equations with n > 4 hy-perchaos can appear, i.e. the first two largest one-dimensional Liapunov exponentscan be positive. We consider the autonomous system of differential equations

where j = 1,2,3,4. The variational equation is given by

Assume that v(i) also satisfies the variational equation. Let {ei,e2,e3,e4} be thestandard basis in R4 and

4 4

Then we obtain

/ 4 \ / 4 \ 4 4

y A u = [Y, yjej) AIZ) u*e* 1 = Yl H %u*eJA e*\j=l j \k=l 1 j=lk-l

where A denotes the exterior product. Using the property of the exterior productthat e, A e, = -e^ A e, (and therefore e, A e = 0) we obtain

y A v = (y1v2 - y2vi) ei A e2 + {yiv3 - y3vi) ei A e3 + (j/i«4 - 3/4«i) ex A e4

+ (2/2 3 - IJ3V2) e2 A e3 + {y2v4 - y\v2) e2 A e4 + (j/3v4 - y4u3) e3 A e4.

We consider now the time evolution of

ay := 2/jUj - yjVi, i < j .

Consequently, using the product rule, we find

da-jj _dyi dvj__dyj_ _ dvj_dt ~ dtVj + Vidt dtVi Vjdf

Page 180: Nonlinear Workbook (3rd)

5.6. HYPERCHAOTIC SYSTEMS 161

Therefore we obtain

da12 (dVx dV2\ dVi dV2 dVx dV2—7— = ^ 1" -K— a12 - T.—023 + W~0.13 - ^—«24 + ^—0.Uat \oui au2j 0U3 ou3 0U4 0U4

dal3 (DVr ^dV3\ dVx dVx dV3 , 3V3

~rr — -5 r "5— a13 + -R—a23 - -5—«34 + "5—«14 + " —0-12dt ydut du3) du2 du4 du4 du2

dau (dVi dVA dVx dVx dV4 dV4

—TT = ^ 1" — a14 + ^ — a 2 4 + ^ — a3 4 + - — O12 + ^ — a1 3

a t yC/Mi OU4/ OU2 OU3 OU2 OU3

da23 (dV2 dV3\ dV2 dV2 dV3 dV3—TT = T. H ^ — «23 + -^—0,13 ~ ^~0-3i - "K—a12 + ^—0,2i

dt \ou2 0U3J aux 0U4 ou\ du^da24 (dV2 dVA\ dV2 dV2 dV4 dV4

-TT — T. 1- ^— «24 + -5—«i4 + j.—a34 - — a12 + -—o23

dt \ou2 ou4j aui ou3 aui ou3

da34 [8V3 dV4\ 8V3 dV3 dV4 dV4

—rr = "5 <~ ^ a34 + "5 O.U + "5 024 - "5 0-13 - -^ O23 •dt \ou3 0U4) aui ou2 aui du2

The two-dimensional Liapunov exponent is given by

A" = i|imiln(||a(D||)

where||a(T)|| = |ou(T)| + |a13(T)| + . . . + |a34(T)|.

// hyperliapunov.cpp

#include <iostream>#include <cmath> // for fabs, log#include "vector.h"#include "msymbol.h"

using namespace std;

const int N = 10;

Vector<Sum<double> > u(N), ut(N);

// The vector field V

template <class T> T V(const T& ss){T sum(O);for(int i=O;i<N;i++) sum += ut[i]*df (ss,u[i]);return sum;

}

int main(void)

{

Page 181: Nonlinear Workbook (3rd)

162 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

int i, j;

Sum<double> ul("ul",O), u2("u2",0), u3("u3",0),u4("u4",0), u5("u5",0), u6("u6",0), u7("u7",0),u8("u8",0), u9("u9",0), ulOC'ulO",0),t("t\O),pl("pl",O), p2("p2",0), p3(»p3",0),

P4("p4",0)> P5("p5",0), p6("p6",0),p7("p7",0), p8("p8",0), p9("p9",0), plOC'plO" ,0);

Vector<Sum<double> > us(N), ug(N);double half =0.5;double rl = 1.0/4.0, r2 = 11.0/5.0,r3 = 1.0/20.0, r4 = 3.0/10.0;

u[0] = ul ; u[ l] = u2; u[2] = u3; u[3] = u4; u[4] = u5;u[5] = u6; u[6] = u7; u[7] = u8; u[8] = u9; u[9] = ulO;ug[O] = p i ; ug[l] = p2; ug[2] = p3; ug[3] = p4;ug[4] = p5; ug[5] = p6; ug[6] = p7; ug[7] = p8;ug[8] = p9; ug[9] = plO;

/ / hyperchaotic modelut[0] = -u2 - u3;u t [ l ] = ul + Sum<double>(rl)*u2 + u4;ut[2] = Sum<double>(r2) + ul*u3;ut[3] = Sum<double>(r3)*u4 - Sum<double>(half)*u3;

ut[4] = Sum<double>(rl)*u5 + u7 + u8;ut[5] = ul*u6 - u8;ut[6] = Sum<double>(r3)*u7 - Sum<double>(half)*u6 - u9 - ulO;ut[7] = (Sum<double>(rl) + ul)*u8 - u3*u5 + u6 - ulO;ut[8] = Sum<double>(r4)*u9 + u7 - Sum<double>(half)*u8;ut[9] = (ul + Sum<double>(r3))*ulO + u3*u7;

// Taylor series expansion up to order 2

for(i=0;i<N;i++)

us[i] = u[i] + t*V(u[i]) + Sum<double>(half)*t*t*V(V(u[i]));

// Evolution of the approximate solutiont.set(O.Ol);// initial values

ug[0] . se t ( -19.0) ; ug[l] . se t (0 .0) ; ug[2] .set(O.O);ug[3] .se t (15.0) ; ug[4] . s e t ( l .0) ; ug[5] . s e t ( l .0) ;ug[6] . se t ( l .O) ; ug[7] . se t ( l .O) ; ug[8] . se t ( l .O) ;ug[9] .se t (1 .0) ;

Page 182: Nonlinear Workbook (3rd)

5.7. HOPF BIFURCATION 163

int iter = 20000;for(j=0;j<iter;j++)

{for(i=0;i<N;i++)

•C

u[i] .set(ug[i] .nvalueO);}for(i=0;i<N;i++){ug[i] .set(us[i] .nvalueO);>} // end for loop j

double T = t.nvalue()*iter;double lambda =(log(fabs(u5.nvalue())+fabs(u6.nvalue())+fabs(u7.nvalue 0 )+fabs(u8.nvalue())+fabs(u9.nvalue())+fabs(ulO.nvalue())))/T;

cout « "lambda = " « lambda « endl;return 0;

}

5.7 Hopf Bifurcation

In the study of nonlinear dynamical systems the Hopf bifurcation plays a central role.

Theorem. Let U be an open connected domain in R" c > 0, and let f be areal analytic function defined on U x [—c, c]. Consider the autonomous system offirst-order ordinary differential equations

— = f(u,r), where u S U, | r | < c .

Suppose there is an analytic, real, vector function g defined on [—c, c] such that

f (g(r) , r )=O.

Thus one can expand f(u, r) about g(r) in the form

f(u,r) = L r u*+r(u* > r) ) u* := u - g(r)

where Lr is an n x n real matrix which depends only on r, and f*(u*,r) is thenonlinear part of f. Suppose there exist exactly two complex conjugate eigenvaluesa(r), a(r) of Lr with the properties

R(a(0)) = 0 and K(Q'(0)) ± 0 ('= d/dr).

Page 183: Nonlinear Workbook (3rd)

164 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

Then there exists a periodic solution P(t,e) with period T(e) with r = r(e), suchthat r(0) = 0, P(t,0) = g{0) and P(t,e) / fl(r(e)) for all sufficiently small c / 0.Moreover r(e), P(t, e), and T(e) are analytic at e = 0, and

T ( o ) = ^ '

These "small" periodic solutions exist for exactly one of three cases: either only forr > 0, or only for r < 0, or only for r = 0.

We apply the Hopf bifurcation to a model for the Belousov-Zhabotinskii reaction.

/ / hopf.cpp

#include <iostream>#include <cmath> // for sqrt, pow#include "msymbol.h"using namespace std;

int main(void){Sum<double> ul("ul",0), u2("u2",0), u3("u3",0);Sum<double> s("s",0), b("b",0), r("r",0);Sum<double> VI, V2, V3;Sum<double> All, A12, A13, A21, A22, A23, A31, A32, A33;

VI = s*(u2 - ul*u2 + ul - ul*u2);V2 = u3 - u2 - ul*u2;V3 = ul - u3;

All = df(Vl,ul); A12 = df(Vl,u2); A13 = df(Vl,u3);A21 = df(V2,ul); A22 = df(V2,u2); A23 = df(V2,u3);A31 = df(V3,ul); A32 = df(V3,u2); A33 = df(V3,u3);

Sum<double> lambda("lambda",0);

Sum<double> chareq;

// characteristic equationchareq = (All - lambda)*(A22 - lambda)*(A33 - lambda)

+ A12*A23*A31 + A13*A21*A32 - A31*(A22 - lambda)*A13- A32*A23*(A11 - lambda) - (A33 - lambda)*A21*A12;

chareq = -chareq; // sign of lambda*3 = +1

Sum<double> cO = chareq.coeff(lambda,0);Sum<double> cl = chareq.coeff(lambda,1);Sum<double> c2 = chareq.coeff(lambda,2);

Page 184: Nonlinear Workbook (3rd)

5.7. HOPF BIFURCATION 165

double two = 2.0; double three = 3.0; double nine = 9.0;double n27 = 27.0; double n54 = 54.0;

Sum<double> Q =(Sum<double>(three)*cl - c2*c2)/(Sum<double>(nine));

Sum<double> R =(Sum<double>(nine)*c2*cl - Sum<double>(n27)*cO- Sum<double>(two)*c2*c2*c2)/(Sum<double>(n54));

Sum<double> D = Q*Q*Q + R*R;

// parameter valuess.set(2.0);// fixed point (0,0,0)ul.set(O.O); u2.set(0.0); u3.set(0.0);

double q = Q.nvalueO;double rR = R.nvalueO;double d = D.nvalueO;double nc2 = c2.nvalue();

if(rR != 0 && q < 0 && d <= 0.0)idouble theta = acos(rR/sqrt(-q*q*q));double PI = 3.14159;double Iambi = 2.0*sqrt(-q)*cos(theta/3.0) - nc2/3.0;double Iamb2 = 2.0*sqrt(-q)*cos((theta + 2.0*PI)/3.0) - nc2/3.0;double Iamb3 = 2.0*sqrt(-q)*cos((theta + 4.0+PD/3.0) - nc2/3.0;cout « "Iambi = " « Iambi « endl;cout « "Iamb2 = " « Iamb2 « endl;cout « "Iamb3 = " « Iamb3 « endl;}

if (d > 0)

{double SQRT, temp, T, S;SQRT = sqrt(d);if(fabs(rR + SQRT) >= 0.0)S = pow(rR + SQRT,1.0/3.0);else S = -pow(-rR - SQRT.l.0/3.0);

if(fabs(rR - SQRT) >= 0.0) T = pow(rR -SQRT,1.0/3.0);else T = -pow(-rR + SQRT,1.0/3.0);

double Iambi = S + T - nc2/3.0;

Page 185: Nonlinear Workbook (3rd)

166 CHAPTER 5. NONLINEAR DISSIPATIVE SYSTEMS

double realpart = - (S + T)/2 - nc2/3.0;double imagpart = sqrt(3)*(S - T)/2.0;cout « "Iambi = " « Iambi « endl;cout « "Iamb2 = " « realpart « " + i*" « imagpart « endl;cout « "Iamb3 = " « realpart « " - i*" « imagpart « endl;>return 0;

}

5.8 Time-Dependent First Integrals

A number of dissipative dynamical systems admit time-dependent first integrals fora certain choice of the control parameter. As an example we consider the Lorenzmodel

dui . .— = a(u2-Ul)

du2— = -uiu3 + rui - u2at

duz—— = uiu2 - bu3at

where a, r and b are positive constants. For example, for r = 0, b = 2 and aarbitrary the Lorenz model admits the time-dependent first integral

/(u(t)) = (u| + u\) exp(2t).

In our program f i r s t 2 . cpp we use SymbolicC++ to find the conditions on r, b anda when we insert the time-dependent first integral into the Lorenz model. We find

-26 + 2 = 0, 2r = 0.

Thus a is arbitrary, r = 0 and 6 = 1 .

/ / f i rs t2.cpp

#include <iostream>#include "msymbol.h"#include "vector.h"using namespace std;

int main(void)

{Vector<Sum<int> > u(3), v(4);Sum<int> term, sum, I, Rl, R2,

ul("ul",0), u2("u2",0), u3("u3",0), t("t",0),

Page 186: Nonlinear Workbook (3rd)

5.8. TIME-DEPENDENT FIRST INTEGRALS 167

s("s",O), b("b",O), r (" r" ,O) ;

u[0] = u l ; u[ l] = u2; u[2] = u3;

// Lorenz Model

v[0] = s*u2- s*ul;v[l] = -u2 - ul*u3 + r*ul;v[2] = ul*u2 - b*u3; v[3] = Sum<int>(l);

// The ansatz

I = (u2*u2 + u3*u3)*exp(Sum<int>(2)*t);

sum = 0;

for(int i=0;i<3;i++) sum += v[i]*df (I,u[i]);

sum += v[3]*df (I,t) ;sum.expand0 ;cout « "sum = " « sum << endl; cout « endl;Rl = sum.coeff(u3,2);Rl = Rl/(exp(Sum<int>(2)*t));Rl.expand() ;cout « "Rl = " « Rl « endl;R2 = sum.coeff(ul,l); R2 = R2.coeff(u2,l);R2 = R2/(exp(Sum<int>(2)*t));cout « "R2 = " « R2 « endl;return 0;

}

Page 187: Nonlinear Workbook (3rd)

Chapter 6

Nonlinear Driven Systems

6.1 Introduction

We consider a dynamical nonlinear system described by a system of first orderordinary differential equations

f-fft-r,

where t € R, u £ R", and r 6 R denote the time, an n-dimensional state and asystem parameter, respectively. We assume

f ; R x R " x R - ) R " , (t, u, r) H-» f (t, u, r)

is a C00 mapping and is periodic in t with period 2TT:

f(t + 27r,u,r)=f(*,u,r).

We also assume that the dynamical system has a solution u(t) = $(t,v,r) de-fined on — oo < t < oo with every initial condition v 6 R™ and every r G R:u(0) = $(0, v,r) = v. By the periodic hypothesis given above, we can define a C°°diffeomorphism Tr from the state space Rn into itself:

Tr : R" -> Rn, v H-). Tr(v) = $(2TT, V, r ) .

The mapping Tr is often called the Poincare mapping and is used for investigatingqualitative properties of the dynamical system given above. If a solution u(i) =$(t, PQ, r) is periodic with period 2n, then the point Po is a fixed point of Tr, i.e.

TT(P0)=P0.

If u(<) is a subharmononic solution of order 1/k (k = 2,3, •••) , i.e., a periodicsolution with least period 2ATT, then PQ is a point with period k such that

168

Page 188: Nonlinear Workbook (3rd)

6.1. INTRODUCTION 169

Trk(P0) = Po

andPo^T?\ j = l,2,...,k-l.

Hence there are always k points Po, A = Tr(P0), • • •, Pk-i = TJ:k-^(Po) which areall fixed points of Tf. An invariant closed curve C such that Tr(C) = C correspondsto quasi-periodic solutions of the dynamical system. Thus in order to study the be-haviour of the solutions of the dynamical system, we have only to study the iteratesof the Poincare mapping Tr.

Next we introduce a hyperbolic fixed point and give its classification. Let P € R™be a fixed point of Tr, then in the neighbourhood of P, the mapping Tr may beapproximated by its derivative DTr(P). This is indeed possible if the fixed point ishyperbolic. We call P a hyperbolic fixed point of TT, if DTr(P) is hyperbolic, i.e.,all the absolute values of the eigenvalues of DTr{P) are different from unity. Wegive a topological classification of a hyperbolic fixed point. Let P be a hyperbolicfixed point of Tr and Eu be the intersection of Rn and the direct sum of the gener-alized eigenspaces of DTr{P) corresponding to the eigenvalues /J, such that \fi\ > 1.Similarly, let Es be the intersection of R" and the direct sum of the generalizedeigenspaces of DTT(P) corresponding to the eigenvalues /i such that \/j,\ < 1. Eu (orEs) is called the unstable (or stable) subspace of DTr(P). Then it is known thatEu and Es have the following properties R" = £ " ® Es and

DTr(Eu) = Eu, DTr{Es) = Es

dimEu = #{^\ | r t | > l } , d im£ ' = #{/i,-| k | < l }

where fit is the eigenvalue of DTT(P) and #{ } indicates the number of elementscontained in the set { }. Let

L» := DTr(P)\E» , L' := DTr(P)\E. .

Then the topological type of a hyperbolic fixed point is determined by the dim Eu

(or dim/?8) and the orientation preserving or reversing property of Lu (or Ls). Thelatter condition is equivalent to the positive or negative sign of detL" (or detLs).We define two types of hyperbolic fixed points.

Definition. A hyperbolic fixed point P is called

(i) a direct type (abbrev. £>-type), if detL" > 0.

(ii) an inversely type (abbrev. /-type), if detL" < 0.

From this definition, at a D-type of fixed point P, Lu is an orientation preservingmapping, whereas at an /-type of fixed point P, Lu is an orientation reversing map-ping. If Eu is the empty set, we identify P as a D-type. Secondly, a hyperbolicfixed point is called a positive type (P-type) or a negative type (iV-type) according

Page 189: Nonlinear Workbook (3rd)

170 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

to the evenness or oddness of dim.Eu, respectively. The name comes from the indexproperty of a fixed point. Combining these definitions, we can discuss the followingfour types of hyperbolic fixed points.

Definition. A hyperbolic fixed point P is called a

(i) PD-type if dimEu is even and detL" > 0

(ii) 7V£>-type if dimEu is odd and det Lu > 0

(iii) P/-type if dimEu is even and det I," < 0

(iv) NI-type if dimEu is odd and detL" < 0.

Assigning d\mEu to the above classification, we obtain all the topologically differenttypes of hyperbolic fixed points:

Proposition. Let TT : Rn —> Rn be the Poincare mapping defined above, thenthere exist 2n topologically different types of hyperbolic fixed points. These typesare:

(i) for even-dimensional case: n = 2m,

2kPD (A = 0 , l , " - ,m) 1 2kPI (fc = l , . . . , m - l )

2k+iND (k = 0,l,2,---,m) 2k+iNI (k = 0 , 1 , . . . ,m)

(ii) for odd-dimensional case: n = 2m + 1,

2 k P D (k = 0 , l , - - - , m ) , n P I ( * = l , - - - , m )

2k+iND (fc = 0 , l , - " , m + l ) zk+iNI (A = 0 , l , - " ,m)

One also uses the notation \Pm denoting a hyperbolic fixed point such that P de-notes the type: PD, ND, PI, and NI, m indicates an m-periodic point of Tr,j indicates the number of a set of Pm when there are several sets of Pm, andk = dimEu. If m = 1 or j = 1, they will be omitted.

For the two-dimensional case: n = 2, we have four types of hyperbolic fixed points:oPD, \ND, \NI, and 2-P-D- These points are called a completely stable, directlyunstable, inversely unstable and completely unstable fixed point, respectively. Forn = 3, we have six types of hyperbolic fixed points: QPD, IND, \NI, 2PD,2PI, and 3ND.

A completely stable fixed point, i.e., dimi?" = 0 is often called a sink,, whereas acompletely unstable fixed point, i.e., dimEu = n is called a source. Other cases,1 < dimEu < n - 1, are simply called saddles.

Page 190: Nonlinear Workbook (3rd)

6.2. DRIVEN ANHARMONIC SYSTEMS 171

6.2 Driven Anharmonic Systems

6.2.1 Phase Portrait

We consider the driven anharmonic system

cPu du , ,_—— + a— +bu + cu = ki + k2 cos(sZi).(XL (XL

Let «i := u and U2 := du/dt. Then the equation can be written as first order system

dv,\ dv,2 , q , , , „ x—— = u2, —— = -au2 - bui - cu? + ki + k2 cos({lt)at at

or using v,3(t) = Q,t

du\ dui , , , , , . rf«3 _—TT — U2, —rr = -au2 - 0U\ - cu, + k\ + k2 cos(u3), —— = U .at at at

In the Java program Anharmonicph .Java we use the values a = 1, b = —10, c = 100,k\ = 0, k2 = 1.2f2 = 3.5 and find the phase portrait (ux(i), u2(t)) using the Lie seriestechnique.

// Anharmonicph.Java

import j ava.awt.*;import j ava.awt.event.*;import Java.awt.Graphics;

public class Anharmonicph extends Frame

{public Anharmonicph()

{

setSize(600,500);addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void paint(Graphics g)ig.drawRect(40,50,500,440);double a, b, c, kl, k2, Omega;double ul, u2, u3, ull, u22, u33;double tau, count;

// parameter values

a = 1.0; b = -10.0; c = 100.0; Omega =3.5;

Page 191: Nonlinear Workbook (3rd)

172 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

kl = 0 .0 ; k2 = 1.2;t a u = 0 .005; count = 0 . 0 ;

// initial valuesull = 1.0; u22 = 0.0; u33 = 0.0;

while(count < 1200.0){ul = ull; u2 = u22; u3 = u33;double vu2 = -a*u2 - b*ul - c*ul*ul*ul + kl + k2*Math.cos(u3);ull = ul + tau*u2 + tau*tau*vu2/2.0;u22 = u2 + tau*vu2 + tau*tau*(-b*u2 - 3.0*c*ul*ul*u2 +

vu2*(-a) - 0mega*k2*Math.sin(u3))/2.0;u33 = 0mega*tau + u3;

if(count > 1000.0) // transients have decayed{int m = (int)(250.0*ul+250.0); int n = (int)(200.0*u2+240.0);int ml = (int)(250.0*ull+250.0); int nl = (int)(200.0*u22+240.0);g.drawLine(m,n,m,n);

>count += tau;

>}

public static void main(String[] args)

{Frame f = new AnharmonicphO;f.setVisible(true);}

}

6.2.2 Poincare SectionThe driven anharmonic system is given by the system

——- = tt2, —7— = — a,U2 — ou\ — cu\ + ki + K2 cos(irt)at at

where a, b, c, k\, &2 and n are bifurcation parameters. This system is invariantunder the transformation t \-t t + 2nn/£l, where n G Z. Thus we can introduce adiffeomorphism on the (u\, 112) plane into itself, which can serve to decide numericallywhether or not there is chaotic behaviour. Assume that

(t, ui(0), Ua(0)) •-»• ui(t, ui(0), «2(0))

(t, ui(0), Ua(O)) u2{t, ui(0), Ua(0))

Page 192: Nonlinear Workbook (3rd)

6.2. DRIVEN ANHARMONIC SYSTEMS 173

is a solution of the driven anharmonic system starting from a point

fli = M0W0))

at t = 0. Let

P l = (u1(27r/n),u2(27r/n)).

Thus we have defined a diffeomorphism

Tr : R2 H-> R2, Po h-> Pl

where r is a bifurcation parameter.

/ / Anharmonicpo.Java

import j ava.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class Anharmonicpo extends Frame

{public Anharmonicpo()

{

setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void paint(Graphics g)ig.drawRect(40,50,500,440);double a, b, c, kl, k2, Omega;a = 1.0; b = -10.0; c = 100.0; Omega = 3.5; kl = 0.0; k2 = 1.2;double ul, u2, u3, ull, u22, u33, t, count;t = 0.005; count =0.0;ull = 1.0; u22 =0.0; u33 =0.0; // initial values

int iter = 1;while(count < 6200.0)

•C

ul = ull; u2 = u22; u3 = u33;double vu2 = -a*u2 - b*ul - c*ul*ul*ul + kl + k2*Math.cos(u3);ull = ul + t*u2 + t*t*vu2/2.0;u22 = u2 + t*vu2 + t*t*(-b*u2 - 3.0*c*ul*ul*u2 +

vu2*(-a) - 0mega*k2*Math.sin(u3))/2.0;u33 = Omega*t + u3;

Page 193: Nonlinear Workbook (3rd)

174 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

if((iter > 10) kk (Math.abs(count-2.0*Math.PI*iter/0mega) < 0.02)){int m = (int)(200.0*ul1+300.0) ;int n = (int)(200.0*u22+200.0);g.drawLine(m,n,m,n);} // end ifcount += t;if(count > 2.0*Math.PI*iter/0mega) iter++;} // end while} // end paint

public static void main(String [] args){Frame f = new AnharmonicpoO;f.setVisible(true);}

>

6.2.3 Liapunov Exponent

The driven anharmonic system is given by

d2u du , .,_ .—r + a— + bu + cu = k cos(Ut)at1 dt

where a > 0. This equation can be written as an autonomous system

dui

dil,2 o ,—— = — au2 — bui — cu\ + k cos(u3jdt

dt

with U3(t = 0) = 0. The variational equation

is given by

dvi

-dT = v>—T- = —av2 — bv\ — ?>cu\vi — ksm{u3)vz

Page 194: Nonlinear Workbook (3rd)

6.2. DRIVEN ANHARMONIC SYSTEMS 175

Without loss of generality we can set ^( i ) = 0. Then the variational system sim-plifies to

dvi

dV2 , „ 2—r- = — aV2 — OVi — 6CU\Vi .

The one-dimensional Liapunov exponent follows from

A:= l im^ln | |v (T) | |

where||v(T)|| = K(T)| + K(T)|.

In the Java program Anharmonicliap. Java we calculate the one-dimensional Lia-punov exponent A for the paramater values

A; = 1.2, o=1.0 , b = -10.0, c = 100.0, Q, = 3.5.

We apply the Lie series technique. We find for the one-dimensional Liapunov expo-nent A « 0.34.

// Anharmonicliap.Java

public class Anharmonicliap

{public static void main(String[] args)

{double a, b, c, k, Omega;double ul, u2, u3, ull, u22, u33;double vl, v2, v3, vll, v22, v33;double t, count;

a = 1.0; b = -10.0; c = 100.0; Omega =3.5;k = 1.2;t = 0.005; count = 0.0;double abl, ab2;

// initial valuesull = 1.0; u22 =0.0; u33 =0.0;vll = 0.5; v22 = 0.5;

while(count < 1500.0)

iul = ull; u2 = u22; u3 = u33;vl = vll; v2 = v22;

Page 195: Nonlinear Workbook (3rd)

176 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

abl = -a*u2 - b*ul - c*ul*ul*ul + k*Math.cos(u3);ab2 = (-b - 3.0*c*ul*ul)*vl - a*v2;ull = ul + t*u2 + t*t*abl/2.0;u22 = u2 + t*abl + t*t*(-b*u2 - 3.0*c*ul*ul*u2 +

abl*(-a) - 0mega*k*Math.sin(u3))/2.0;u33 = Omega*t + u3;vll = vl + t*v2 + t*t*ab2/2.0;v22 = v2 + t*ab2 + t*t*(-6.0*c*ul*u2*vl - (b+3.0*c*ul*ul)*v2

- a*ab2/2.0);count += t;} // end while

double liap = 1.0/count*Math.log(Math.abs(vll) + Math.abs(v22));System.out.println("Liapunov exponent = " + liap);

}}

6.2.4 Autocorrelation FunctionWe consider the driven anharmonic system

d2u du , _ .-rir + a— + bu + cu = k cos(fZi).at1 at

Let Mi := u and w2 := du/dt. Then the system can be written as

du\ du-i ,~r~ — W2, ~r~ = ~au2 — bui — cu\ + k cos(S2t).at dt

The time average of u(t) is denned as

^ ••= ^ lo U{t)dt •

The autocorrelation function of u(t) is defined by

lim i fT(u(t) - (u{t))){u{t + T) - (u(t + T)))dtQ fr\ ._ r->oo 1 Jo

Tlimi j\u{t) - (u(t)))2dt

The autocorrelation function measures the correlation between subsequent signals.It remains constant or oscillates for regular motion and decays rapidly (mostly withan exponential tail) if the signals become uncorrelated in the chaotic regime. Firstwe generate a time series from integrating the differential equation. The time seriesis used for calculating the autocorrelation function.

Page 196: Nonlinear Workbook (3rd)

6.2. DRIVEN ANHARMONIC SYSTEMS 177

/ / Timeseries.cpp

#include <fstream>#include <cmath> // for cos

using namespace std;

void f system (double h, double t,double u[] .double hf[]){

double a = 1.0; double b = -10.0; double c = 100.0;double k = 1.2;double Omega = 3 . 5 ;hf [0] = h*u[l ] ;hf[ l ] = h*(-a*u[l] - b*u[0] - c*u[0]*u[0]*u[0] + k*cos(Omega*t));

>

void map(double u [ ] , i n t steps,double h,double t ){

const int N = 2;double uk[N];double tk;double a[6] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[6] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[6][5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0 . 0 ; b[ l ] [3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4][ l ] = -8 .0; b[4] [2] = 3680.0/513.0;b[4][3] = -845.0/4104.0; b[4] [4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5j [3] = 1859.0/4104.0;b[5][4] = -11.0/40.0;

double f [6] [N];int i , j , 1, k;

f or (i=0; Ks teps ; i++){fsystem(h, t ,u , f [0]) ;for(k=l;k<=5;k++){

Page 197: Nonlinear Workbook (3rd)

178 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

tk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u[l];for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>fsystem(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}

>

int main(void)

{ofstream data;data.openCtime_ser.dat");

int steps = 1;double h = 0.005; double t = 0.0;double u[2] = { 1.0, 0.0 }; // initial conditions

int i; // wait for transients to decay

for(i=0;i<1000;i++)

{t += h;

map(u,steps,h,t);

>t = 0.0;

for(i=0;i<9192;i++)

{t += h;

map(u,steps,h,t);data « u[0] « endl;>data.closeO ;return 0;

}

In the C++ program correlation3. cpp we find the autocorrelation functions usingthe data generated from the program Timeseries. cpp.

Page 198: Nonlinear Workbook (3rd)

6.2. DRIVEN ANHARMONIC SYSTEMS 179

/ / correlations.cpp

#include <iostream>#include <fstream>using namespace std;

double average(double* series,int T)

{double sum = 0.0;for(int t=O;t<T;t++){ sum += series [t] ; }double av = sum/((double) T);return av;

>

void autocorrelation(double* series,double* CXX.int T,int length,double av)

{for(int tau=O;tau<length;tau++){double C = 0.0;double diff = T-length;for(int t=O;t<diff;t++){ C += (series [t]-av)* (series [t+tau]-av) ; }CXXEtau] = C/( (double) dif f+1) ;>

>

int main(void){int T = 8192;double* series = new double[T];ifstream data;

data.openCtime_ser.dat");for(int t=0;t<T;t++)

{data » series [t] ;}double av = average(series,T);cout « "average value = " « av « endl;

int length = 200;double* CXX = new double[length];autocorrelation(series,CXX.T,length,av);

Page 199: Nonlinear Workbook (3rd)

180 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

delete[] series;

for(int tau=O;tau<length;tau++)cout « "CXX[" « tau « "] = " « CXX[tau] « endl;

delete [] CXX;data.closeO ;return 0;

}

6.2.5 Power Spectral DensityThe autocorrelation function of the real scalar signal u(t) is defined by

Ruu(T)=E[u(t + T)u(t)]

W i t h ,« , ,00

E[u{t + r)u(i)] := / / pu{t+T),u(t){x,y)xydxdyJ — oo J — oo

where pu(t+T),u(t){x, V) represents the joint probability density function of u(t + r)and u(t). If u(t) is ergodic then the autocorrelation function of u(t) may be equatedto

1 rT

RUU{T) = lirn — J^ u{t + r)u{t) dt.

The power spectral density SUU(LJ) is now defined as

/•oo

J —OO

It follows that

SM--=^E^f_TU{t)e-^dt^

if u{t) is ergodic. The power spectral density can indicate whether the dynamicalsystem is periodic or quasiperiodic. The power spectral density of a periodic sys-tem with frequency LJ has delta functions at w and its harmonics 2w, 3w, .... Aquasiperiodic system with basic frequencies OJI, ..., u)k has delta functions at thesepositions and also at all linear combinations with integer coefficients.

6.3 Driven Pendulum

6.3.1 Phase PortraitThe equation for the damped and driven pendulum is given by

d2u du , . , ,_.,-—• + a— + 6sinu = kcos(Slt).dt1 dt

Page 200: Nonlinear Workbook (3rd)

6.3. DRIVEN PENDULUM 181

Introducing «i := u and u^ := du/dt we obtain the system

du\-dT=U2

—j— = — au2 — bsinui + kcos(Q,t).

For the C++ program we evaluate the phase portrait (ui(t),ii2{t)) for a = 0.2,6 = 1, k = 1.1 and fi = 0.8. We generate u[0] and u[l] and write it into the filephase_data.dat. Then we can use GNUPLOT to display the data.

// Pendulumphase.cpp

#include <fstream>#include <cmath> // for sin, cosusing namespace std;

void fsystem(double h,double t,double u[] .double hf[])

•C

double a = 0.2; double b = 1.0;double k = 1.1; double Omega = 0.8;hf [0] = h*u[l];

hf[l] = h*(-a*u[l] - b*sin(u[0]) + k*cos(0mega*t)) ;

}

void map(double u[],int steps,double h,double t)<

const int N = 2;double uk[N] ;double tk;double a[6] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[6] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[6][5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0][4] = 0.0;b[l] [0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0.0; b[ l] [3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0 ;b[4][2] = 3680.0/513.0; b[4][3] = -845.0/4104.0;b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;

Page 201: Nonlinear Workbook (3rd)

182 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

b[5][4] = -11.0/40.0;

double f [6] [N] ;int i , j , 1, k;

f or (i=0; i<steps ; i++){fsystem(h,t,u,f[0]);for(k=l; k<=5; k++){tk = t + a[k]*h;for(l=0;KN;l++){uk[l] = u[l];

for(j=0;j<=k-l;j++)

uk[l] += b[k][j]*f[j][l];

}

fsystem(h,tk,uk,f[k]);

}

for(l=0;KN;l++)

for(k=0;k<6;k++)

u[l] += c[k]*f[k][l];

}

}

int main(void)

{ofstream data;data.openCphase_data.dat");

int steps = 1;double h = 0.005;double t = 0.0;double u[2] = { 0.8, 0.6 }; // initial conditions

int i;// wait for transients to decayfor(i=0;i<1000;i++){t += h;map(u,steps,h,t);>

t = 0.0;for(i=0;i<20000;i++)

Page 202: Nonlinear Workbook (3rd)

6.3. DRIVEN PENDULUM 183

{t += h;map(u,steps,h,t);data « u[0] « " " « u[l] « "\n";}data.closeO ;return 0;

}

6.3.2 Poincare SectionThe equation for the damped and driven pendulum is given by

cPu du , .—r-r + a— + osinti = kcos[Ut).at* at

Introducing ux := u and w2 := du/dt we obtain the system

dui~dT = U2

dii2—— = —aui — 6 sin Mi + /ccos(i2i).dt

This system is invariant under the translation

2imtt-+t+ ——, n e Z.

Thus we can study the Poincare map.

In the Java program PendulumPoincare. Java we evaluate the Poincare section(ux(t), u2(t)) for a = 0.2, b = 1, k = 1.1 and fi = 0.8.

// PendulumPoincare.Java

import java.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class PendulumPoincare extends Frame

ipublic PendulumPoincare(){setSize(600,500) ;addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});}

Page 203: Nonlinear Workbook (3rd)

184 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

public void fsystem(double h,double t,double u[] .double hf[]){double a = 0.2, b = 1.0;double k = 1.09; double Omega = 0.8;hf[O] = h*u[l];hf[l] = h*(-a*u[l] - b*Math.sin(u[0]) + k*Math.cos(Omega*t));}

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk[] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 >;double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[ l ] [0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[l] [2] = 0.0; b[l] [3] = 0.0;b[ l ] [4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8 .0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0; b[5] [2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5] [4] = -11.0/40.0;

double f [] [] = new double [6] [N] ;for( in t i=0;i<steps;i++){fsystem(h, t ,u , f [0]) ;for( int k=l;k<=5;k++){tk = t + a[k]*h;for( in t l=0;KN;l++){uk[l] = u[l];for(int j=O;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];}fsystem(h,tk,uk,f[k]);}for(int l=0;KN;l++)

Page 204: Nonlinear Workbook (3rd)

6.4. PARAMETRICALLY DRIVEN PENDULUM 185

for(int k=0;k<6;k++)u[l] += c[k]*f[k][l];}>

public void paint(Graphics g)

{g.drawRect(40,40,500,450);

int steps = 1; int N = 2; int counter = 0;

double h = 0.005; double Omega =0.8; double t = 0.0;

double u[] = { 0.8, 0.6}; // initial conditions

for(int i=0; i<800000; i++)

{t += h;map(u,steps,h,t,N);if((counter > 10) && (Math.abs(t-2.0*Math.PI*counter/0mega) < 0.02)){int m = ((int)(10.0*u[0])+350); int n = ((int) (40.0*u[l])+200);g.drawLine(m,n,m,n);>

if(t > 2.0*Math.PI*counter/0mega) { counter++; }}>

public static void main(String[] args)•C

Frame f = new PendulumPoincareO;f.setVisible(true);>

}

6.4 Parametrically Driven Pendulum

6.4.1 Phase Portrait

We consider the damped parametrically driven pendulum

d IL dti—r-r + a— + (1 + k cos(flt)) sin u = 0.dtL at

This equation can be written as a system of first order differential equations

dui

——- = —au2 — (1 + k cos(Q,t)) sinui.

Page 205: Nonlinear Workbook (3rd)

186 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

We evaluate the phase portrait (ui(t),u2(t)). We take into account that

— 7T < «i < IT.

II ParametricPhase.Java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class ParametricPhase extends Frame

{public ParametricPhase()

{setSize(600,500);addWindowListener(new WindowAdapterO

{ public void windowClosing(WindowEvent event){ System.exit(O); }});}

public void paint(Graphics g)

{g.drawRect(50,30,500,400);double a = 0.15; double k = 0.94; double Omega = 1.56;double ul, u2, u3, ull, u22, u33;double tau = 0.005; double count = 0.0;ull =0.1; u22 =0.2; u33 = 0.0; // initial conditions

while(count < 1200.0)

{

ul = ull; u2 = u22; u3 = u33;double v2 = -a*u2 - (1.0 + k*Math.cos(u3))*Math.sin(ul);ull = ul + tau*u2 + tau*tau*v2/2.0;u22 = u2 + tau*v2 + tau*tau*(-u2*(l.0-k*Math.cos(u3))*Math.cos(ul)

-a*v2+0mega*k*Math.sin(u3)*Math.sin(ul))/2.0;u33 = 0mega*tau + u3;

if(count > 1000.0) // transients have decayed

{if(ull > Math.PI) { ull = ull - 2.0*Math.PI; }if(ull < -Math.PI) { ull = ull + 2.0*Math.PI; }int m = (int) (80.0*ull + 300.0);int n = (int) (60.0*u22 + 200.0);g.drawLine(m,n,m,n);

}count += tau;

Page 206: Nonlinear Workbook (3rd)

6.4. PARAMETRICALLY DRIVEN PENDULUM 187

> // end while} // end paint

public static void main(String[] args)

•C

Frame f = new ParametricPhaseO;f.setVisible(true);}

}

6.4.2 Poincare SectionWe consider the parametrically driven pendulum

d 11 dii-7— + a— + (1 + k cos(flt)) sin u — 0.at1 at

This equation can be written as a system of first order differential equations

—— = u2, —— =-au2 - (I + kcoslut)) sin v,!.at at

This system is invariant under t i-> t + 2vn/Q, n £ Z. Thus we can study thePoincare map of the dynamical system. We take into account that — TT < u < ir.In the Java program ParametricPoincare. Java we use the Lie series technique tointegrate the differential equations.

// ParametricPoincare.Java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class ParametricPoincare extends Frame{public ParametricPoincare()

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System, exit (0); » ) ;

>

public void paint(Graphics g)

{g.drawRect(50,30,500,400);double a = 0.15; double k = 0.94; double Omega = 1.56;

Page 207: Nonlinear Workbook (3rd)

188 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

double ul, u2, u3, ull, u22, u33;double tau = 0.005; double count = 0.0;ull =0.1; u22 = 0.2; u33 =0.0; // initial conditionsint z = 0;while(count < 10000.0){ul = ull; u2 = u22; u3 = u33;double v2 = -a*u2 - (1.0+k*Math.cos(u3))*Math.sin(ul);ull = ul + tau*u2 + tau*tau*v2/2.0;u22 = u2 + tau*v2 + tau*tau*(-u2*(l.0-k*Math.cos(u3))*Math.cos(ul)

-a*v2+0mega*k*Math.sin(u3)*Math.sin(ul))/2.0;u33 = 0mega*tau + u3;

if(count > 1000.0) // transients have decayed

{if(ull > Math.PI) { ull = ull - 2.0*Math.PI; }if(ull < -Math.PI) { ull = ull + 2.0*Math.PI; }if(Math.abs(count - 2.0*Math.PI*z/0mega) < 0.02)

{

int m = (int) (80.0*ull+300.0); int n = (int) (60.0*u22+200.0);g.drawLine(m,n,m,n);}}count += tau;if(count > 2.0*Math.PI*z/0mega) { z++; }} // end while} // end paint

public static void main(String[] args)

{

Frame f = new ParametricPoincareO ;

f.setVisible(true);

}

>

6.5 Driven Van der Pol Equation

6.5.1 Phase Portrait

We consider the driven van der Pol equation

d2u , , ,sdu . .—— + a(uz - 1)— + u = k cos(fii)dtz at

Page 208: Nonlinear Workbook (3rd)

6.5. DRIVEN VAN DER POL EQUATION 189

or equivalently written

du\

dll2 , o , , ,— - = -a{u{ - 1)«2 — ui + k cos u3

(Xh

dt

where u3(t = 0) = 0. We evaluate the phase portrait (wi(i), U2{t)) for the parametervalues a = 5.0, k = 5.0, 0 = 2.466. For these parameter values it is conjecturedthat the system shows chaotic behaviour. We apply the Runge-Kutta technique tointegrate the differential equations.

// VdpPhase.Java

import java.awt.*;import j ava.awt.event.*;import Java.awt.Graphics;

public class VdpPhase extends Frame

•C

public VdpPhase(){setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void fsystem(double h,double t,double u[].double hf[]){double a = 5.0; double k = 5.0; double Omega = 2.466;hf[0] = h*u[l];hf[l] = h*(a*(1.0-u[0]*u[0])*u[l]-u[0]+k*Math.cos(0mega*t));

>

public void map(double u[],int steps.double h,double t . int N)•C

double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 >;double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 >;double b [] [] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0][4] = 0.0;

Page 209: Nonlinear Workbook (3rd)

190 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

b[l][0] = 1.0/4.0; b[l][l] = 0.0; b[l][2] = 0.0; b[l][3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2][4] = 0.0;b[3][0] = 1932.0/2197.0; b[3][l] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8.0;b[4][2] = 3680.0/513.0; b[4][3] = -845.0/4104.0;b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;b[5][4] = -11.0/40.0;

double f [] [] = new double [6] [N] ;int i , j , 1, k;f or (i=0; i<steps; i++){fsystem(h,t,u,f[0]);for(k=l;k<=5;k++){tk = t + a[k]*h;for(l=0;KK;l++){uk[l] = u [ l ] ;for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][ l] ;}fsystem(h,tk,uk,f[k] );}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c [k] *f [k] [1];}}

public void paint(Graphics g)

{g.drawRect(20,40,550,400);int steps = 1; int N = 2;double h = 0.005; double t = 0.0;

double u[] = { 0.8, 0.6}; // initial conditions

// wait for transients to decay

for(int i=0;i<1000;i++)

{t += h;

Page 210: Nonlinear Workbook (3rd)

6.5. DRIVEN VAN DER POL EQUATION 191

map(u,steps,h,t,N);

}

t = 0.0;for(int i=0;i<100000;i++){t += h;map(u,steps,h,t,N);int m = (int)(100.0*u[0] + 350);int n = (int)(15.0*u[l] + 220.0);g.drawLine(m,n,m,n);}}

public static void main(String[] args){Frame f = new VdpPhaseO;f.setVisible(true);}

}

6.5.2 Liapunov ExponentWe consider the driven van der Pol equation described in section 6.5.1. To findthe largest one-dimensional Liapunov exponent we calculate the variational system.The variational system of the driven van der Pol equation is given by

dvi~ T t ^

—— = -(2auiu2 + lhi — a(u\ - l)v2 - fcsin(M3)v3at

dt

Without loss of generality we can set v3 = 0. Then we arrive at

~n- = v2, —^ = -{2auiu2 + l)vi-a{u\-l)v2.at at

The one-dimensional Liapunov exponent A follows from

A:= lim Un(\vx(T)\ + \v2{T)\).

In the C++ program vdpliapunov.cpp we evaluate the one-dimensional Liapunovexponent for the parameter values a = 5.0, k = 5.0, fi = 2.466.

Page 211: Nonlinear Workbook (3rd)

192 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

II vdpliapunov.cpp

#include <iostream>

#include <cmath> // for fabs, log#include "vector.h"#include "msymbol.h"using namespace std;

const int N = 3;

Vector<Sum<double> > u(N), ut(N), y(N-l), yt(N-l);

// The vector field V

template <class T> T VCconst T& ss){T sum(O);for (int i=0;i<N;i++) sum += ut[i]*df (ss,u[i]);return sum;

}

template <class T> T WCconst Tfe ss)

{T sum(O);for( in t i=0;i<(N-l);i++) sum += y t [ i ] *d f ( s s , y [ i ] ) ;return sum;

>

int main(void)

{int i, j;

Sum<double> ul("ul",0), u2("u2",0), u3("u3",0), t("t",O),a("a",0), k("k",O), om("om",0),pl("pl",O), p2("p2",0), p3("p3",0),yl("yl",O), y2("y2",0), ql("ql",O), q2("q2",0);

Vector<Sum<double> > us(N), ug(N), ys(N-l), yg(N-l);

double half =0.5;u[0] = u l ; u[ l ] = u2; u[2] = u3;ug[0] = p l ; ug[l] = p2; ug[2] = p3;y[0] = yl ; y[ l] = y2;yg[O] = ql ; yg[l] = q2;

// Driven van der Pol

ut[0] = u2;u t [ l ] = a*(Sum<double>(l) - ul*ul)*u2 - ul + k*cos(u3);

Page 212: Nonlinear Workbook (3rd)

6.6. PARAMETRICALLY AND EXTERNALLY DRIVEN PENDULUM 193

ut[2] = om;

/ / variat ional equationsyt[O] = y2;y t [ l ] = - (Sum<double>(2.0)*ul*u2 + Sum<double>(l.0))*yl

- a*(ul*ul - Sum<double>(1.0))*y2;

// Taylor series expansion up to order 2for(i=0;i<N;i++)us[i] = u[i] + t*V(u[i]) + Sum<double>(half)*t*t*V(V(u[i]));for(i=0;i<(N-l);i++)ys[i] = y[i] + t*W(y[i]) + Sum<double>(half)*t*t*W(W(y[i]));

// Evolution of the approximate solutiont.set(O.Ol); a.set(5.0); k.set(5.0); om.set(2.466);ug[0]. set (1.5504); ug[l] .set(-0.71214); ug[2] .set(O.O);yg[0].set(0.5); yg[l] .set (0.5);

int iter = 50000;for(j=0;j<iter;j++)

{for(i=0;i<N;i++) { u[i] .set(ug[i] .nvalueO); }for(i=0;i<(N-l);i++) { y[i] .set(yg[i] .nvalueO) ; >for(i=0;i<N;i++) { ug[i] .set(us[i] .nvalueO) ; >for(i=0;i<(N-l);i++) { yg [i] . set (ys [i] . nvalue 0 ) ; }} // end for loop j

double T = 0.01*iter;double lambda = (log(fabs(yl.nvalue())+fabs(y2.nvalue())))/T;cout « "lambda = " « lambda « endl;return 0;

}

6.6 Parametrically and Externally Driven Pendu-lum

The parametrically and externally driven pendulum is given by

-j-£ + a— + (1 + &2 cos(fl2i)) sinu = k\ cos{Q,it).

We set

Ul(t) := u{t), u2(t) := , u3(t) := nxt, u4(t) := Q2t.at

Page 213: Nonlinear Workbook (3rd)

194 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

Thus the second order differential equation can be written as the autonomous systemof first order ordinary differential equations

du\

^ = W2

—— = -au2 - (1 + k2 cos u4) sin ut + k\ cos u3at

£-*dui~dT = Q2

where u3(t = 0) = 0 and w4(t = 0) = 0. In the program we evaluate the phaseportrait (ui(t),u2(t)).

I/ ParametricExtPhase.Java

import j ava.awt.*;import j ava.awt.event.*;import Java.awt.Graphics;

public class ParametricExtPhase extends Frame

{public ParametricExtPhase(){setSize(600,500);addWindowListener(new WindowAdapterO•C public void windowClosing(WindowEvent event)•C System.exit(0); }});>

public void fsystem(double h,double t,double u[],double hf[])

{.

double a = 0.1; double kl = 0.3; double k2 = 0.3;double Omegal = 0.618034; double 0mega2 = 1.618034;hf [0] = h*u[l];hf[1] = h*(-a*u[l] - (1.0 + k2*Math.cos(0mega2*t))*Math.sin(u[0])

+ kl*Math.cos(0megal*t));}

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 >;double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

Page 214: Nonlinear Workbook (3rd)

6.6. PARAMETRICALLY AND EXTERNALLY DRIVEN PENDULUM 195

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b[ l ] [ l ] = 0.0; b[l] [2] = 0.0; b[l][3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4][l] = -8.0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0;b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0;b[5][2] = -3544.0/2565.0; b[5] [3] = 1859.0/4104.0;b[5] [4] = -11.0/40.0;

double f [] [] = new double [6] [N];int i , j , 1, k;f or (i=0; Ksteps; i++){fsystem(h,t,u,f[0]);for(k=l;k<=5;k++){tk = t + a[k]*h;for(l=0;KN;l++)

•C

uk[l] = u[l];for(j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>fsystem(h,tk,uk,f[k]);}for(l=0;KN;l++)for(k=0;k<6;k++)u[l] += c[k]*f[k][l];}>

public void paint(Graphics g)

{g.drawLine(20,200,620,200);g.drawRect(20,40,600,360);

int steps = 1;int N = 2;

Page 215: Nonlinear Workbook (3rd)

196 CHAPTER 6. NONLINEAR DRIVEN SYSTEMS

double h = 0.005;double t = 0.0;

double u[] = { 1.0, 0.0}; // initial conditions

// wait for transients to decayfor(int i=0;i<2000;i++)

{t += h;map(u,steps,h,t,N);}

t = 0.0;for(int i=0;i<50000;i++)

{t += h;map(u,steps,h,t,N);int mx = (int) (150*u[0] + 250.0);int my = (int) (150*u[l] + 200.0);t += h;map(u,steps,h,t,N);int nx = (int) (150.0*u[0] + 250.0);int ny = (int) (150.0*u[l] + 200.0);g.drawLine(mx,my,nx,ny);

>}

public static void main(String [] args)

{Frame f = new ParametricExtPhaseO;f.setVisible(true);

}>

Page 216: Nonlinear Workbook (3rd)

Chapter 7

Controlling and Synchronizationof Chaos

7.1 Introduction

In this chapter we discuss different strategies for controlling classical chaos. Con-trolling chaos plays a central role in engineering in particular in electronics andmechanical systems. Roughly speaking, there are two kinds of ways to controlchaos: feedback control and non-feedback control.

The frame of a chaotic attractor is given by infinitely many unstable periodic orbits.The task is to use the unstable periodic orbits to control chaos. We describe theOtt-Grebogi-Yorke method which uses this fact. By applying small, judiciouslychosen temporal perturbations to an accessible control parameter of the dissipativesystem, they demonstrated that an original chaotic trajectory can be converted toa desired fixed point or periodic orbit. The Ott-Grebogi-Yorke method [72] belongsto feedback control. We apply this method to one and two-dimensional maps. Themaps under consideration are the logistic map and the Henon map. Controllingcan also be achieved by coupling periodic modulations to the control parameter.We study the Lorenz model with this technique in section 7.3. This is a non-feedback control. Resonant perturbation and control is considered in section 7.4and applied to a driven anharmonic oscillator. It is possible to synchronize twochaotic systems by coupling them. Two different types of synchronization havebeen considered. One, called master-slave, or unidirectional coupling, is made upof an element, called drive system, one or several components of its state vectorare transmitted to a second part, called the response system. The other type ofsynchronization is the mutual coupling. Synchronization is studied in section 7.5.Phase coupled chaotic systems are studied in section 7.6.

197

Page 217: Nonlinear Workbook (3rd)

198 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

7.2 Ott-Yorke-Grebogi Method

7.2.1 One-Dimensional Maps

The basic idea of the Ott-Yorke-Grebogi method [72] for stabilizing unstable periodicorbits embedded in a chaotic attractor can be understood by considering a simplemodel system. We consider the logistic map

xt+i = f(xt, r) = rxt(l - xt)

where x is restricted to the unit interval [0,1], and r is a parameter (r 6 [3,4]).The logistic map develops chaos via the period-doubling bifurcation route. Theperiod-doubling cascade accumulates at r = r^ « 3.57..., after which chaos canarise. Consider the case r = 3.8. The system is apparently chaotic for this value ofr and the chaotic attractor is contained in the interval [0,1]. The chaotic attractorcontains an infinite number of unstable periodic orbits embedded within it and theyare dense in it. For example, a fixed point (r = 3.8)

x* = ! - - = 0.7368...r

and a period-2 orbit,

x(l) « 0.3737..., x(2) « 0.8894...

wherex(l) = f[x(2)], x(2) = f[x(l)].

We find x(l) and x(2) by solving the quartic equation

x* = r V ( l - x*){l - rx*{\ - a;*)).

Besides the solutions x* — 0, x* = 1 — 1/r we obtain x(l) and x(2) as fixed pointsof the second iterate of the map.

Now suppose we want to avoid chaos at r = 3.8. In particular, we want trajectoriesresulting from a randomly chosen initial condition XQ to be as close as possible to theperiod-2 orbit assuming that this period-2 orbit gives the best system performance.We can choose the desired asymptotic state of the map to be any of the infinitenumber of unstable periodic orbits, if that periodic orbits gives the best systemperformance. To achieve this goal, we suppose that the parameter r can be finelytuned in a very small range around the value TQ = 3.8, namely, we allow r to vary inthe range [rg — S, r0 + 5], where i < l . Due to the ergodicity of the chaotic attractor,the trajectory that begins from an arbitrary value of Xo will fall, with probabilityone, into the neighbourhood of the desired period-2 orbit at some later time. Thetrajectory would diverge quickly from the period-2 orbit if we do not intervene. Weprogram the parameter perturbations in such a way that the trajectory stays inthe neighbourhood of the period-2 orbit for as long as the control is present. The

Page 218: Nonlinear Workbook (3rd)

7.2. OTT-YORKE-GREBOGI METHOD 199

small parameter perturbations will be time-dependent in general. The logistic mapin the neighbourhood of a periodic orbit can be approximated by a linear equationexpanded around the periodic orbit. Let the target period-m orbit to be controlledbe x(i), i = l , . . . ,m, where x(i + 1) = /[x(i)] and x(rn + 1) = x(l). Assumethat at time t, the trajectory falls into the neighbourhood of the ith component ofthe period-m orbit. The linearized dynamics in the neighbourhood of the (i + l)thcomponent is then

x t + 1 - x ( i + l ) = ^ (xt-x^)+d-l^l Art

OX x=x(i),r=ra x=x(i),r=ro

where the partial derivatives are evaluated at x = x(i) and r = ro. Thus we obtain

xt+i - x(i + 1) = ro(l - 2x(i))(xt - x{i)) + x(i)(l - x{i))Art.

We require xt+\ to stay in the neighbourhood of x(i + 1). Hence, we set

|it+i-i(t +l)| = 0.

From this equation it follows that

_ (2E(t)-l)(st-s(t))Art~r° x(i)(l-x(i» •

This equation holds only when the trajectory xt enters a small neighbourhood of theperiod-m orbit, i.e., when \xt — x(i)\ —>• 0. Hence, the required parameter pertur-bation Art is small. Let the length of a small interval defining the neighbourhoodaround each component of the period-m orbit be 2e. In general, the required max-imum parameter perturbation 6 is proportional to e. Since e can be chosen to bearbitrarily small, 5 also can be made arbitrarily small. However, the average tran-sient time before a trajectory enters the neighbourhood of the target periodic orbitdepends on e (or 5). A larger 5 will mean a shorter average time for the trajectoryto be controlled. Of course, if 6 is too large, nonlinear effects become importantand the linear control strategy might not work. When the trajectory is outside theneighbourhood of the target periodic orbit, we do not apply any parameter pertur-bation and the system evolves at its nominal parameter value ro. We usually setArt = 0 when Art > 5. The parameter perturbations Art depend on xt and aretherefore time-dependent.

In the following C++ program controll. cpp we control the fixed point x* = 1 —1/r,where r = 3.8 for the logistic equation.

// controll.cpp

#include <iostream>#include <cmath> // for fabsusing namespace std;

Page 219: Nonlinear Workbook (3rd)

200 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

int main(void){

double x = 0.28; // initial valuedouble xl;double rO = 3.8; // control parameterdouble T = 650; // number of iterationsint count = 0;double eps = 0.001;

double xf = 1.0 - 1.0/r0; // fixed pointdouble r = rO;

for(int t=O;t<T;t++)

{xl = x; x = r*xl*(1.0-xl);count++;if(fabs(x-xf) < eps)

{cout « "in eps neighbourhood" « endl;cout « "count = " « count « endl;double delta = r0*(2.0*xf-1.0)*(x-xf)/(xf*(1.0-xf));cout « "delta = " « delta « endl;r = rO + delta; // change to control parameter

// for controllingcout « "x = " « x « endl;} // end if} // end for loopreturn 0;

}

In the following C++ program control2. cpp we control a period two-orbit for thelogistic map with r = 3.8.

// control2.cpp

#include <iostream>#include <cmath> // for sqrt, fabsusing namespace std;

int main(void)

{double x = 0.28; // initial valuedouble xl;double rO = 3.8; // control parameter

Page 220: Nonlinear Workbook (3rd)

7.2. OTT-YORKE-GREBOGI METHOD 201

double T = 353; // number of iterations

int count = 0;

double eps = 0.001; // neighbourhood

// periodic pointsdouble ab = (1.0 + 1.0/r0);double xpl = ab/2.0 + sqrt(-ab/r0 + ab*ab/4.0);double xp2 = ab/2.0 - sqrt(-ab/r0 + ab*ab/4.0);

cout « "xpl = " « xpl « endl; // periodic point 0.88942cout « "xp2 = " « xp2 « endl; // periodic point 0.373738cout « endl;

double r = rO;

for(int t=0;t<T;t++)

{xl = x; x = r*xl+(1.0 - xl);count++;if((fabs(x - xpl) < eps) || (fabs(x - xp2) < eps)){

cout « "in eps neighbourhood of xpl or xp2" « endl;

cout « "count = " « count « endl;

if(fabs(x - xpl) < eps)

{double deltal;deltal = r0*(2.0*xpl - 1.0)*(x - xpl)/(xpl*(1.0 - xpl));cout « "deltal = " « deltal « endl;r = rO + deltal;cout « "x = " « x « endl;>else{double delta2;delta2 = rO*(2.O*xp2 - 1.0)*(x - xp2)/(xp2*(l.0 - xp2));cout « "delta2 = " « delta2 « endl;r = rO + delta2;cout « "x = " « x « endl;}}> // end for loopreturn 0;

>

Page 221: Nonlinear Workbook (3rd)

202 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

7.2.2 Systems of Difference EquationsHere we consider a system of difference equations

x(+1 =f(x( , r)

where Xt € Rn, r € R and f is sufficiently smooth in both variables. Here, r isconsidered a real parameter which is available for external adjustment but is re-stricted to lie in some small interval |r — ro| < 5 around a nominal value r0. Weassume that the nominal system (i.e., for r = ro) contains a chaotic attractor. Wevary the control parameter r with time t in such a way that for almost all initialconditions in the basin of the chaotic attractor, the dynamics of the system convergeonto a desired time periodic orbit contained in the attractor. The control strategyis the following. We find a stabilizing local feedback control law which is definedon a neighbourhood of the desired periodic orbit. This is done by considering thefirst order approximation of the system at the chosen unstable periodic orbit. Herewe assume that this approximation is stabilizable. Since stabilizability is a genericproperty of linear systems, this assumption is quite reasonable. The ergodic natureof the chaotic dynamics ensures that the state trajectory eventually enters into theneighbourhood. Once inside, we apply the stabilizing feedback control law in orderto steer the trajectory towards the desired orbit.

Thus the control of unstable periodic orbits Xp € Rn with i = 1, . . . , P, where Pdenotes the period length, can be achieved by small, exactly determined variationsof the system parameter during each iteration t given by the control rt € R.

Hence we have to consider the parametrised system

xt+i =f(xt,rt).

By changing rt slightly the periodic points are also shifted slightly, i.e., xpj(rt) fort = l , . . . , P .

We describe the method as applied to the stabilization of fixed points (i.e., periodone orbits) of the map f. The consideration of periodic orbits of period larger thanone is straightforward. Let x*(r) denote an unstable fixed point on the attractor.For values of r close to r0 and in the neighbourhood of the fixed point x* (r0) themap can be approximated by the linear map

xt+1 - x*(r0) = A[xt - x*(r0)] + B(r - r0)

where A is an n x n Jacobian matrix and B is an n-dimensional column vector

A:=DJ{x,r), B := Drf{x,r).

The partial derivatives are evaluated at x = x*(r0) and r — r0. We now introducethe time-dependence of the parameter r by assuming that it is a linear function ofthe variable xt of the form

r - r o = - t f T ( x ( - x - ( r o ) ) .

Page 222: Nonlinear Workbook (3rd)

7.2. OTT-YORKE-GREBOGI METHOD 203

The l x n matrix KT is to be determined so that the fixed point x*(ro) becomesstable. We obtain

x m - x*(r0) = (A- BKT)(xt - x*(r0))

which shows that the fixed point will be stable provided the n x n matrix

A~BKT

is asymptotically stable: that is, all its eigenvalues have modulus smaller than unity.The solution to the problem of the determination of KT, such that the eigenvaluesof the matrix A — BKT have specified values, is well known from control systemstheory and is called the pole placement technique.

As an example we consider the Henon map in the form

zit+i = a + bx2t - x\t, x2t+i = xu .

For b = 0.3 and a = a — 1.4 there is an unstable saddle point contained in thechaotic attractor. This fixed point is given by

x\ = —C + V c H f l , 3?2 = Xl

where c = (1 — 6)/2 and a > —c2. We obtain

A = J D x f ( x ) = ( - 21

X l J ) , S = £ » r f ( x ) = ( J ) .

The eigenvalues and eigenvectors of A are given by

\. = -xi + Jb + xl, (A2

S), \v = -xi-Jb + xl, (A2U) .

The matrix A — BKT is given by

A-BX* =(-*<[-'* V 2 ) ./ / Controlhenon.cpp

#include <iostream>#include <cmath> / / for sqrtusing namespace std;

int main(void){double T = 3380; // number of iterationsdouble x = -0.760343, y = 1.40007; // initial valuesdouble xl, yl;

Page 223: Nonlinear Workbook (3rd)

204 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

double a = 1.4, b = 0.3; // parameter valuesdouble aO = a;

// fixed point (fixed point is unstable)double c = (1.0 - b)/2.0;double xf = -c + sqrt(c*c + a);double yf = xf;cout « "xf = " « xf « endl; // 0.883896cout « "yf = " « yf « endl; // 0.883896

double kl = -0.1; double k2 = 1.3;int counter = 0;double eps = 0.005; // eps neighbourhood

for(int t=O;t<T;t++)

{xl = x; yl = y;x = aO - xl*xl + b*yl; y = xl;counter++;if((fabs(x - xf) < eps) && (fabs(y - yf) < eps))icout « "in eps neighbourhood" « endl;cout « "counter = " « counter « endl;aO = a - kl*(x - xf) - k2*(y - yf);cout « "x = " « x « endl;cout « "y = " « y « endl;>}cout « "x = " « x « endl;cout « "y = " « y « endl;return 0;

}

In the following C++ program we stabilize a fixed point of the Henon model usingthe OGY-method.

// henonogy.cpp

#include <iostream>#include <cmath> // for sqrtusing namespace std;

double f(double p,double x,double y,double b){ return (p - x*x + b*y); }

Page 224: Nonlinear Workbook (3rd)

7.2. OTT-YORKE-GREBOGI METHOD 205

double fp(double a,double b) // fixed point

{ return ((b-1.0)/2.0 + sqrt((1.0-b)*(1.0-b)/4.0 + a)); >

double Es(double x,double b) // stable eigenvalue{ return (-x + sqrt (x*x + b)); }

double Eu(double x,double b) // unstable eigenvalue{ return (-x - sqrt(x*x +b)); }

int main(void)

•C

double a0=1.4,b=0.3; // parametersdouble xinit = 0.6, yinit =0.7; // initial valuesconst double delta =0.01; // maximum allowed perturbation

double xi = xinit; double yi = yinit;int i = 0; // indentationdouble x_ = xinit; int k = 0;double at; // control parameter

double xf = fp(aO,b); double yf = xf; // fixed point

double lambdas = Es(xf,b); // eigenvalue stabledouble lambdau = Eu(xf,b); // eigenvalue unstable

double fu = sqrt(lambdau*lambdau+1.0)/(lambdau - lambdas);double ful = fu;double fu2 = -lambdas*fu; // eigenvectors

cout « "fixed point x,y: " « xf ;cout « " , " « yf « endl;cout « "eigenvalues: " « lambdas;cout « " , " « lambdau « endl;do

{double deltap = -lambdau*(ful*(xi-xf) + fu2*(yi-yf))/ful;if(fabs(deltap) < delta)

{at = aO + deltap;x_ = xi; xi = f(at,xi,yi,b); yi = x_;cout « "x" « i;cout « " : " « xi;cout « " y" « i;cout « " : " « yi;cout « " p :" « at-aO «endl;i++; k++;

Page 225: Nonlinear Workbook (3rd)

206 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

}else{i++; k = 0;x_ = xi;xi = f(aO,xi,yi,b);yi = x_;>}while(k<=15);return 0;

>

7.3 Small Periodic Perturbation

Control of chaos of dissipative systems with a control parameter can be realizedby coupling weak periodic oscillations to the control parameter. We consider theLorenz equations

dui , ,— =a{u2-u1)du2——=ru1 - uxu2 -u2atdu3— =uiu2 - bu3.dt

We have chosen parameters of Lorenz equations where the system has a strangeattractor, i.e. a = 10, 6 = 0.4 and r = 80. To couple the periodic oscillations to thecontrol parameter, we replace the parameter r in the Lorenz system with

r -> r + k cos(fii)

where Q is the modulation frequency and k is the modulation amplitude. Thus weconsider the time-dependent system

dui— =<T(U2-UI)

—— = (r + kcos(Q.t))ui — u\u2 — u2dt

du3—— = Uiu2 - bu3 .dt

After controlling with the frequency fl — 6.28318, we obtained period one, periodtwo and period four orbits for the values k — 2.11, k = 3 and k = 36.8, respectively.

Page 226: Nonlinear Workbook (3rd)

7.3. SMALL PERIODIC PERTURBATION 207

/ / ControlLorenz.Java

import j ava.awt.*;

import j ava.awt.event.*;

import java.awt.Graphics;

public class ControlLorenz extends Frame

{public ControlLorenz()

{setSize(600,500);addWindowListener(new WindowAdapterO

{ public void windowClosing(WindowEvent event){ System.exit(0); }});

}

public void fsystem(double h,double t,double u[].double hf[])

idouble sigma = 10.0; double r = 80.0; double b = 0.4;double Omega = 6.28318; double k = 2.0;hf[O] = h*sigma*(u[l] - u[0]) ;hf[ l ] = h*(-u[0]*u[2] + (r + k*Math.cos(Omega*t))*u[0] - u [ l ] ) ;hf[2] = h*(u[0]*u[l] - b*u[2]);}

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk[] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 };double c[] = i 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b [] [] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0][4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l][2] = 0.0; b[ l ] [3] = 0.0;b[l] [4] = 0.0;b[2][0] = 3.0/32.0; b[2][ l ] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2][4] = 0.0;b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4][ l ] = -8.0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5] [1] = 2.0; b[5] [2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5] [4] = -11.0/40.0;

Page 227: Nonlinear Workbook (3rd)

208 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

double f [] [] = new double [6] [N] ;for(int i=0;i<steps;i++)ifsystem(h,t,u,f[0]);for(int k=l;k<=5;k++){tk = t + a[k]*h;for(int l=0;KN;l++)•Cuk[l] = u[l];for(int j=O;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];}fsystem(h,tk,uk,f[k]);>for(int l=0;KN;l++)for(int k=0;k<6;k++)u[l] += c[k]*f[k][l];>>

public void paint(Graphics g)

{g.drawRect(40,40,600,400);int steps = 1; int N = 3;double h = 0.005; double t = 0.0;double u[] = { 0.8, 0.8, 0.8}; // initial conditions// wait for transients to decayfor(int i=0;i<4000;i++) { t += h; map(u,steps,h,t,N); }t = 0.0;for(int i=0;i<20000;i++){t += h;map(u,steps,h,t,N);int m = (int)(10.0*u[2] - 400); int n = (int) (10.0*u[0] + 200);g.drawLine(m,n,in,n);

}}

public static void main(String[] args)

{

Frame f = new ControlLorenzO;f.setVisible(true);>

}

Page 228: Nonlinear Workbook (3rd)

7.4. RESONANT PERTURBATION AND CONTROL 209

7.4 Resonant Perturbation and Control

Another method to control a nonlinear dynamical system is to follow a prescribedgoal dynamics (Hiibler [49]). If the experimental dynamics is described by

^ = f ( u , p ( t ) + F ( t ) )

where both the set of parameters p and the driving force F depend only on timet, then the limiting behaviour of u(t) can be made equal to a given goal dynamicsy(t), i.e.,

l"(*)-y(*)|->-0 as i ->oo

where

g = g(y(*),t)by an appropiate choice of F. Complete entrainment occurs if both sets of flows aremade equal, i.e.,

f(y,p(t) + F(t)) = g(y(i),t)

and if the special solution u(i) = y(t) is stable. Whether convergence occurs, i.e.|u(i) — y(t)| —> 0 as t —> oo, depends on the particular choice of F and the initialconditions, i.e., u(0). The regions of the u state space such that convergence occurare called entrainment regions. The applied controls are not typically small andconvergence to the goal is not assured.

7.5 Synchronization of Chaos

7.5.1 Synchronization Using Control

To synchronize two chaotic systems that we call A and B, we imagine that someparameter of one system (assume B) is externally adjustable. We assume that somestate variables of both systems A and B can be measured. Some dynamical variablesof two systems are measured, based on which temporal-parameter perturbations arecalculated and applied to the system B. We assume that before the synchronization,some information about the geometrical structure of the chaotic attractor (e.g., theJacobian matrices along a long chaotic trajectory that practically covers the wholeattractor) has been obtained. Based on this measurement and our knowledge aboutthe system (we can, for example, observe and learn the system first), when it is deter-mined that the state variables of A and B are close, we calculate a small parameterperturbation based on the Ott-Grebogi-Yorke algorithm and apply it to system B.Two systems can then be synchronized, although their trajectories are still chaotic.Under the influence of external noise, there is a finite probability that two alreadysynchronized trajectories may lose synchronization. However, with probability one(due to the ergodicity of chaotic trajectories), after a finite amount of transient time,the trajectories of A and B will get close and can then be synchronized again. In

Page 229: Nonlinear Workbook (3rd)

210 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

this sense, the synchronization method is robust against small external noise.

We consider two almost identical chaotic systems that can be described by two-dimensional maps on the Poincare surface of section

xt + 1 = f(xt,r0) [A], yt+i = f(yt,r) [B]

where x(, yt € R2, f is a smooth function in its variables, r0 for system A is a fixedparameter value, and r for system B is an external controllable parameter. For thepurpose of synchronization, we require that the dynamics should not be substantiallydifferent for system A and B. In other words, any parameter perturbations shouldbe small. Thus, we require

\r-ro\ <S

where 5 is a small number defining the range of parameter variation. Suppose thattwo systems start with different initial conditions. In general, the resulting chaotictrajectories are completely uncorrelated. However, due to ergodicity, with probabil-ity one two trajectories can get arbitrarily close to each other at some later time nc.Without control, two trajectories will separate from each other exponentially again.The objective is to program the parameter r in such a way that |yt — xt| —» 0 forn> nc, which means that A and B are synchronized for t>tc.

The linearized dynamics in the neighbourhood of the "target" trajectory {x(} is

yt+i - x(+1(r0) = J(yt - xt(r0)) + V(Ar)t

where rt '•= rg + (Ar)j, (Ar)( < 5, J is the 2 x 2 Jacobian matrix, and V is atwo-dimensional column vector

J := £>yf(y,r)|y=x,=ro, V := Drf{y,r)\y=Xir=n .

A property of a chaotic trajectory is the existence of both stable and unstable di-rections at almost each trajectory point.

Let es(t) and eu(t) be the stable and unstable unit vectors at xt and fs(t) and fu(t) betwo unit vectors that satisfy

f«(t) • eu(t) = fs(j) • es(t) = 1

f«(t) • es{t) — fs(i) • eu(t) = 0.

To stabilize { yt } around { xt }, we require the next iteration of xt after falling intoa small neighbourhood around xt to lie on the stable direction at X((+1)(r0), i.e.,

[yt+i-X(t+i)(T-0)]-fu(t+i) = 0 .

We obtain the following expression for the parameter perturbations

/A s _ (J[yt ~ x((r0)]) • fu(t+i)

-V • *u(«+l)

Page 230: Nonlinear Workbook (3rd)

7.5. SYNCHRONIZATION OF CHAOS 211

If (Ar)t > 5, we set (Ar)( = 0. Note that fu(t) can be calculated in terms of J.

We illustrate our synchronization algorithm by using the standard Henon map

X\t+\ =a-x\t + 0.3x2*, x2t+i = xu

where a is our control parameter. Consider two such Henon systems

xu+i = a - x\t + 0.3:r2t, x2t+i = xu

3/it+i = a-y\t + 0.3y2t, y2t+i = yu •

We obtain

>=(-?!)• ' - ( ! ) •The eigenvalues and eigenvectors of J are given by

\s = -Xl + Jb + xl (ty, Xn = -Xl-y/b + Xl1, (A

1U) .

One has a fixed parameter value (a = a0 = 1.4) which serves the target and in theother system we adjust a in a small range (1.39,1.41). At time t = 0, we start twosystems with different initial conditions. The two systems then move on completelyuncorrelated chaotic trajectories. At a certain time step, the trajectory points of theuncoupled two systems come close to each other within an epsilon neighbourhood.When this occurs, we turn on the parameter perturbations.

// SynchronHenon.Java

import java.awt.*;

public class SynchronHenon

{public static void main(String [] args)

{double xl, xll, x2, x22, yl, yll, y2, y22;xl = 0.5; x2 = -0.8; // initial valuesyl = 0.001; y2 = 0.001; // initial valuesint T = 2140; // number of iterationsint t;

double b = 0.3, a = 1.4, aO = 1.4;

for(t=0;t<T;t++)

{xll = xl; x22 = x2;yll = yl; y22 = y2;

Page 231: Nonlinear Workbook (3rd)

212 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

xl = aO - xll*xll + b*x22;x2 = xll;yl = a - yll*yll + b*y22;y2 = yll;

double distance = Math.abs(xl - yl) + Math.abs(x2 - y2);double epsilon = 0.01;double range = 0.01;

if(distance < epsilon)

{System.out.printlnC");System.out.println("inside epsilon neighbourhood: t = " + t);double tl = aO - xl*xl + b*x2;double t2 = xl;double lams = - tl + Math.sqrt(tl*tl + b);double lamu = - tl - Math.sqrt(tl*tl + b);

double fl = Math.sqrt(lamu*lamu + l)/(lamu - lams);double f2 = - fl*lams;double deltaa = -((-2.0*xl*(yl-xl)+b*(y2-x2))*fl+(yl-xl)*f2)/f1;System.out.printlnC"deltaa = " + deltaa);

if(Math.abs(deltaa) < range)a = aO + deltaa;

System.out.println("a = " + a);System.out.printC'xl = " + xl) ;System.out.printlnC x2 = " + x2);System.out.printC'yl = " + yl);System.out.println(" y2 = " + y2);}}}

>

7.5.2 Synchronizing Subsystems

Pecora and Carroll [73] showed that certain subsystems of chaotic systems can bemade to synchronize by linking them with common signals. The criterion for thisis the sign of the one-dimensional sub-Liapunov exponents. The method is as fol-lows. Consider a chaotic system described by the autonomous system of first-orderdifferential equations with n state vectors as

du

Page 232: Nonlinear Workbook (3rd)

7.5. SYNCHRONIZATION OF CHAOS 213

We divide the dynamical system into two subsystems

uT = (v,wf

dv dw¥ = g(v,w). - s - = h(v,w)

where v = («I,M2, • • • ,«m), g = (/i,/2, •.. , / m ) , w = («m + 1,«m + 2, . . . ,«„) andh = (/m+i> /m+2, • • •, /n)- Now we create a new subsystem w' identical to the wsystem by replacing the set of variables w for the corresponding w' in the functionh. This yields the system

dv dw dw' . ,^ • = g(v,w), • ^ = h(v,w), _ = h ( v , w ) .

LetAw := w' — w .

The subsystem components w and w' will synchronize only if Aw —> 0 as t —>• oo.In the infinitesimal limit this leads to the variational equation for the subsystem

^ = Dwh(v(*),w(i))y

where £>wh is the Jacobian matrix of the w subsystem vector field with respect tow only. The behaviour of the variational system depends on the one-dimensionalLiapunov exponents of the w subsystem.

We have the following theorem (Pecora and Carroll [73])

Theorem. The subsystems w and w' will synchronize only if the one-dimensionalLiapunov exponents of the w subsystem are all negative.

The above theorem is a necessary, but not sufficient, condition for synchronization.It says nothing about the set of initial conditions in which w' will synchronize withw.

A chaotic system, for example, has at least one positive one-dimensional Liapunovexponent. For the response system of the subsystem to synchronize with the drivesystem, all of its one-dimensional Liapunov exponents must be less than zero.

As an example we consider the Rossler model

dux du2 du3 , . .— = u2-u3, —=ui+au2, — = b + u3(ui -c)at at at

where a, 6 and c are positive constants. The slave system is

du\ . dw', , . . . .

Page 233: Nonlinear Workbook (3rd)

214 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

Thus in the Rossler system it is possible to use the «2 component to drive a (u[,u'3)response system and attain synchronization with the (ui,u3) components of thedriving system.

In the Java program Rossler. j ava we integrate all five equations as an autonomoussystem of differential equations using a Runge-Kutta-Fehlberg method. We displaythe phase-portrait for (MI,US) and (u'ltu'3).

II Rossler.Java

import j ava.awt.*;import java.awt.event.*;import java.awt.Graphics;

public class Rossler extends Frame

ipublic Rossler()

{setSize(600,500);addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent event){ System.exit(O); }});

}

public void fsystem(double h,double t,double u[].double hf[]){double a = 0.2, b = 0.2, c = 9.0;hf[O] = h*(-u[l] - u[2]) ; hf [ l ] = h*(u[0] + a*u[l]) ;hf[2] = h*(b + u[2]*(u[0] - c ) ) ; hf[3] = h*(-u[l] - u[4]) ;hf[4] = h*(b + u[4]*(u[3] - c ) ) ;>

public void map(double u [ ] , i n t steps,double h,double t . i n t N){double uk [] = new double [N] ;double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 >;double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,

-9.0/50.0, 2.0/55.0 };double b[][] = new double [6] [5] ;b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b [ l ] [ l ] = 0.0; b[ l] [2] = 0.0; b[ l] [3] = 0.0;b[ l ] [4] = 0.0;b[2][0] = 3.0/32.0; b[2] [1] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;

Page 234: Nonlinear Workbook (3rd)

7.5. SYNCHRONIZATION OF CHAOS 215

b[3][0] = 1932.0/2197.0; b[3] [1] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3][4] = 0.0;b[4][0] = 439.0/216.0; b[4] [1] = -8.0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4][4] = 0.0;b[5][0] = -8.0/27.0; b[5][ l ] = 2.0; b[5] [2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5] [4] = -11.0/40.0;

double f [] [] = new double [6] [N];for( in t i=O;i<steps;i++){fsys tem(h, t ,u , f [0]) ;for( in t k=l;k<=5;k++){tk = t + a[k]*h;for ( in t l=0;KN;l++){uk[l] = u [ l ] ;for( in t j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>fsystem(h,tk,uk,f[k]);>for(int l=0;KN;l++)for(int k=0;k<6;k++)u[l] += c[k]*f[k][l];}>

public void paint(Graphics g)

ig.drawRect(20,40,560,480);int steps = 1; int N = 5;double h = 0.002; double t = 0.0;double u[] = { 0.8, 0.4, 0.8, 1.1, 2.3 }; // initial conditions// wait for transients to decayfor(int i=0;i<4000;i++) { t += h; map(u,steps,h,t,N); >t = 0.0;for(int i=0;i<40000;i++){t += h;map(u,steps,h,t,N);int ml = (int) (5.0*u[0] + 100); int nl = (int) (5.0*u[2] + 200);g.drawLine(ml.nl,ml.nl);int m2 = (int)(5.0*u[3] + 300); int n2 = (int) (5.0*u[4] + 200);g.drawLine(m2,n2,m2,n2);

Page 235: Nonlinear Workbook (3rd)

216 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

}}

public static void main(String[] args)

iFrame f = new RosslerO;f.setVisible(true);

}}

7.6 Phase Coupled Systems

In this section the stability of a class of coupled identical autonomous systems offirst order nonlinear ordinary differential equations is investigated. These couplingsplay a central role in synchronization of chaotic systems and can be applied in elec-tronic circuits. As applications we consider two coupled Van der Pol equations andtwo coupled logistic maps. When the uncoupled system admits a first integral westudy whether a first integral exists for the coupled system. Finally the relation ofthe Liapunov exponents of the uncoupled and coupled system are discussed.

Consider the autonomous system of first order ordinary differential equations

— - = f ( u ) , u = (u1,u2,.-.,un)T.

at

We assume that the functions fj : Rra -»• R are analytic. Assume that u* is a fixedpoint of this system. The variational equation of the dynamical system is given by

where di/du is the Jacobian matrix. Inserting the fixed point u* into the Jacobianmatrix results in an n x n matrix

with constant coefficients. The eigenvalues Ai, . . . , An of this matrix determine thestability of the fixed point u*. Furthermore the eigenvalues provide information asto whether Hopf bifurcations can occur. In this case we assume that f dependson a (bifurcation) parameter. Moreover, the variational system is used to find theone-dimensional Liapunov exponents.

In controlling the chaos of the autonomous sytem the coupling of two identicalsystems according to

^ = f(u)+c(v-u), ^=f(v)+c(u-v)

Page 236: Nonlinear Workbook (3rd)

7.6. PHASE COUPLED SYSTEMS 217

plays a central role. Here n > 3 and c e R. First we realize that (u*,v*) withv* = u* is a fixed point of the coupled system if u* is a fixed point of the systemdn/dt = f(u). Inserting the fixed point (u*,u*) into the Jacobian matrix yieldsa 2n x 2n matrix. We now show that the eigenvalues of this 2n x In matrix canbe found from the eigenvalues of the n x n matrix given by A. Then from the Ineigenvalues of the uncoupled system we can determine the stability of the fixed point(u*, u*) for the phase coupled system.

To find these eigenvalues we prove the following theorem.

Theorem. Let A be an n x n matrix over the real numbers. Let Ai,. . . , An be theeigenvalues of A. Define the In x In matrix M as

, , fA-d cl \M : = ( cl A-clj-

Then the eigenvalues of M are given by

Ai, A2, . . . , An, Ai - 2c, A2 — 2c,. . . , An - 2c.

Proof. There exists an n x n orthogonal matrix Q such that QTAQ = D + U, whereD := diag(Ai, A2,..., An) and U is a strictly upper-triangular n x n matrix. Let

P-(Q °)

where 0 is the n x n zero matrix. It follows that the inverse is given by

p - i _ ( QT 0 \F ~\-QT QT)

giving

P-iMP-(D + U Cl )

Now P~lMP is an upper-triangular matrix of which the entries on the diagonal areAi,.. . , An, Aj — 2c,. . . , Xn — 2c, the eigenvalues of M. This proves the theorem.

Example. As an application let us consider the Van der Pol equation

dui du2 . 2\_ = U 2 , _ = r ( 1 _ U l ) u 2 _ U l .

Then u* = (0,0) is a fixed point. The eigenvalues of the functional matrix for thisfixed point are given by

Ai,2 = r/2 ± yV2/4 - 1

The uncoupled system shows a Hopf bifurcation. We find a Hopf bifurcation whenthe characteristic exponents cross the imaginary axis. For the Van der Pol equationa stable limit cycle is born. If we consider the coupling we find that the eigenvalues

Page 237: Nonlinear Workbook (3rd)

218 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

of the coupled system are given by ^ = Xu /j,2 = A2, M3 = \i - 2c, fi4 = A2 - 2c.

The theorem stated above also applies to coupled maps. For example it can beapplied to

xt+i = f (xt) + c(yt - xt), yt+1 = f (y() + c(xt - yt).

Example. Consider the logistic map / : [0,1] ->• [0,1] with f(x) = rx(l - x). Themap / admits the fixed points x\ = 0 and x\ = (r — l)/r . We consider the fixedpoint x\. Then we find that the Jacobian matrix (which is a 1 x 1 matrix) at thisfixed point is given by A = A = 2 — r. Consequently the eigenvalues for the coupledsystem are 2 — r and 2 — r — 2c.

Let us now consider the Liapunov exponents of the coupled system. To find a relationbetween the Liapunov exponents of the coupled system and uncoupled system weconsider the time-evolution of

&(t) := u(t) - v(«).

We call © the phase difference. It follows that

d& du dv „. , . . ^

-dT = ^-M={M-fV-2c@-Using a Taylor expansion for f (u) and f (v) and the fact that

£(»(*)) = £(»(*))we obtain

^ = (A(t) - 2cl)& + O(&% A(t) := £(u(«))

where O(02) indicates higher order terms in 0 . Integrating this equation whileneglecting the higher order terms yields

0(t) = (V2c*Texp (J*A{s)ds^ 0(0)

where T is the time ordering operator. The eigenvalues \ij (j = 1, 2 , . . . , n) of

lim (Texp([*A{s)ds))

are related to the Liapunov exponents Xj (j = 1,2,... ,n) via Xj = In \fij\. We find

(|0(t)|)oce(Al-2c' '

where the average is taken over all initial conditions u(0) and all directions of 0(0)and Ai is the largest one-dimensional Liapunov exponent. The equation tells usthat for 2c > Ai both systems stay in phase. Consequently, they have the Ai of the

Page 238: Nonlinear Workbook (3rd)

7.6. PHASE COUPLED SYSTEMS 219

uncoupled system. The two systems get out of phase at the value c* — \\/2. Thusc* provides the largest one-dimensional Liapunov exponent.

In the program Phasecoupling. Java we consider two phase-coupled Lorenz models.The parameter values for the Lorenz model are a = 16, r = 40 and b = 4. For thesevalues we have numerical evidence that the system shows chaotic behaviour. Thelargest one-dimensional Liapunov exponent is given by A = 1.37. Thus for c = 0.75we find that the systems stay in phase, since 2c > A. Thus the phase portrait (u\, Vi)is a straight line. For c = 0.45 we have 2c < A and the systems are out of phase.Thus the phase portrait is no longer a straight line. We display the phase portraitfor different values of c.

// Phasecoupling.Java

import j ava.awt.*;import j ava.awt.event.*;import Java.awt.Graphics;

public class Phasecoupling extends Frame{public PhasecouplingO

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});

>

public void fsystem(double h,double t,double u[].double hf[])

{double sigma = 16.0; double r = 40.0; double b = 4.0;double c = 0.65; // coupling constant change to c = 0.70hf[0] = h* (sigma* (u[l] - u[0]) + c*(u[3] - u [0] ) ) ;hf [ l ] = h*(-u[0]*u[2] + r*u[0] - u[ l] + c*(u[4] - u [ l ] ) ) ;hf[2] = h*(u[0]*u[l] - b*u[2] + c*(u[5] - u[2]) ) ;hf[3] = h*(sigma*(u[4] - u[3]) + c*(u[0] - u[3]) ) ;hf[4] = h*(-u[3]*u[5] + r*u[3] - u[4] + c*(u[l] - u[4] ) ) ;hf[5] = h*(u[3]*u[4] - b*u[5] + c*(u[2] - u[5]) ) ;>

public void map(double u [ ] , i n t steps,double h,double t . i n t N)idouble uk[] = new double[N];double tk;double a[] = { 0.0, 1.0/4.0, 3.0/8.0, 12.0/13.0, 1.0, 1.0/2.0 >;

Page 239: Nonlinear Workbook (3rd)

220 CHAPTER 7. CONTROLLING AND SYNCHRONIZATION OF CHAOS

double c[] = { 16.0/135.0, 0.0, 6656.0/12825.0, 28561.0/56430.0,-9.0/50.0, 2.0/55.0 };

double b[][] = new double [6] [5] ;

b[0][0] = b[0][l]= b[0][2] = b[0][3] = b[0] [4] = 0.0;b[l][0] = 1.0/4.0; b[ l ] [ l ] = 0.0; b[l][2] = 0.0; b[l] [3] = 0.0;b[l][4] = 0.0;b[2][0] = 3.0/32.0; b[2][l] = 9.0/32.0;b[2][2] = 0.0; b[2][3] = 0.0; b[2] [4] = 0.0;b[3][0] = 1932.0/2197.0; b[3][l] = -7200.0/2197.0;b[3][2] = 7296.0/2197.0; b[3] [3] = b[3] [4] = 0.0;b[4][0] = 439.0/216.0; b[4][l] = -8.0;b[4][2] = 3680.0/513.0; b[4] [3] = -845.0/4104.0; b[4] [4] = 0.0;b[5][0] = -8.0/27.0; b[5][l] = 2.0; b[5][2] = -3544.0/2565.0;b[5][3] = 1859.0/4104.0; b[5][4] = -11.0/40.0;

double f [] [] = new double [6] [N];for(int i=0; Ksteps; i++){fsystem(h,t,u,f[0]);for(int k=l;k<=5;k++)•Ctk = t + a[k]*h;for(int l=0;KN;l++){uk[l] = u[l];for(int j=0;j<=k-l;j++)uk[l] += b[k][j]*f[j][l];>fsystem(h,tk.uk,f[k]);

}for(int l=0;KN;l++)for(int k=0;k<6;k++)u[l] += c[k]*f[k][l];

}>

public void paint(Graphics g)

{g.drawRect(20,30,500,400);int steps = 1; int N = 6;double h = 0.005; double t = 0.0;// initial conditionsdouble u[] = { 0.8, 0.8, 0.8, 1.0, 0.4, 0.3 >;// wait for transients to decay

Page 240: Nonlinear Workbook (3rd)

7.6. PHASE COUPLED SYSTEMS 221

for(int i=0;i<2000;i++) { t += h; map(u,steps,h,t,N); }

t = 0.0;

for(int i=0;i<6800;i++)

{t += h;map(u,steps,h,t,N);int m = (int) (5.0*u[0] + 300); int n = (int) (5.0*u[3] + 200);g.drawLine(m,n,m,n);}

}

public static void main (String [] args){Frame f = new PhasecouplingO;f.setVisible(true);>

}

Page 241: Nonlinear Workbook (3rd)

Chapter 8

Fractals

8.1 Introduction

Mandelbrot [65] introduced the term 'fractal' (from the latin fractus, meaning 'bro-ken') to characterize spatial or temporal phenomena that are continuous but notdifferentiable. Unlike more familiar Euclidean constructs, every attempt to split afractal into smaller pieces results in the resolution of more structure. Fractal objectsand processes are therefore said to display 'self-invariant' (self-similar or self-affme)properties. Self-similar objects are isotropic upon rescaling, whereas rescaling ofself-affine objects is directing-dependent (anisotropic). Thus the trace of particu-late Brownian motion in two-dimensional space is self-similar, whereas a plot of therc-coordinate of the particle as a function of time is self-affine.

Fractal properties include scale independence, self-similarity, complexity, and infi-nite length or detail (Peitgen and Richter [74]). Fractal structures do not have asingle length scale, while fractal processes (time series) cannot be characterized by asingle time scale. Nonetheless, the necessary and sufficient conditions for an object(or process) to possess fractal properties have not been formally defined. Indeed,fractal geometry has been described as a collection of examples, linked by a commonpoint of view, not an organized theory. Fractal theory offers methods for describingthe inherent irregularity of natural objects. In fractal analysis, the Euclidean con-cept of 'length' is viewed as a process. This process is characterized by a constantparameter D known as the fractal (or fractional) dimension.

Fractals are all around us, in the shape of a mountain range or in the windings of acoast line. Perhaps the most convincing arguments in favour of the study of fractalsis their sheer beauty. Furthermore the domain of attraction in chaotic systems hasin many cases a fractal structure.

222

Page 242: Nonlinear Workbook (3rd)

8.1. INTRODUCTION 223

There are several fractal dimensions described in the literature. The two most usedare the Hausdorff dimension and capacity. The definition of the Hausdorff dimensionis as follows. Let X be a subset of R". A cover of X is a (possibly infinite) collectionof balls, the union of which contains X. The diameter of a cover A is the maximumdiameter of the balls in A For d, e, we define

a(d, t) := inf T (diam A)d

V ' ' A= coverof X f-1/ '

anda(d) :— \im a(d,e).

It can be shown that there is a unique do such that

d < do —> a{d) = oo

d > d0 ->• a(d) = 0 .

This d0 is defined to be the Hausdorff dimension of X, Written HD{X).

The capacity C is a special case of the Hausdorff dimension. In this case the coversconsist of balls of uniform size e. We put a grid on Rn and count the number of boxesthat intersect the set X. Suppose we have such an e-grid. Let N(e) = the number ofboxes that intersect the set X. Then N(e) • ed would seem to be a simplified versionof a(d, e) and the desired d can sometimes be computed as

lnJV(c)G := lim T^- .

«->" In ( i )

This number C is called capacity. We have the inequality C{X) < HD(X).

The fractal dimension can be viewed as a relative measure of complexity, or as anindex of the scale-dependency of a pattern. The fractal dimension is a summarystatistic measuring overall 'complexity'. Like many summary statistics (e.g. mean),it is obtained by 'averaging' variation in data structure. In doing so, information isnecessarily lost. Excellent summaries of basic concepts of fractal geometry can befound in the books (Mandelbrot [65], Barnsley [6], Edgar [27]).

Definition. A fractal is by definition a set for which the Hausdorff dimensionstrictly exceeds the topological dimension.

Page 243: Nonlinear Workbook (3rd)

224 CHAPTER 8. FRACTALS

8.2 Iterated Function System

8.2.1 IntroductionTo define an iterated function system we need the definition of a complete metricspace. Let X be a non-empty set. A real-valued function d defined on X x X, i.e.ordered pairs of elements in X, is called a metric or a distance function in X, iff itsatisfies, for every a,b,c € X, the following axioms

(i) d{a, b) > 0 and d(a, a) = 0(ii) d(a, b) = d(b, a) symmetry(iii)d(a,c) <d(a,b)+d(b,c)(iv) If a ± b, then d(a, b) > 0.

The nonnegative real number d{a, b) is called the distance from a to 6.

Example. Let X = R, then d(a, b) := \a — b\ is a metric.

Let d be a metric on a non-empty set X. The topology T on X generated by theclass of open spheres in X is called the metric topology. The set X together with thetopology T induced by the metric d is called a metric space and is denoted by (X, d).

A hyperbolic iterated function system consists of a complete metric space (X, d)together with a finite set of contraction mappings

w n : X - > X , n = l,2,...,N

with respective contractivity factors sn, for n = 1,2,..., N.

The notation for the iterated function system is { X : wn, n = 1, 2 , . . . , N } and itscontractivity factor is s := max{sn : n = 1,2,... ,N}. Let (X,d) be a completemetric space and let (%(X),/i(d)) denote the corresponding space of nonemptycompact subsets, with the Hausdorff metric h(d). The Hausdorff metric is definedby as follows. Let A, B £ %{X). We define

d(A, B) := max{ d(x, B) : x € A }

whered{x, B) ~ min { d(x, y) : y € B} .

d(A,B) is called the distance from the set A € U(X) to the set B e H(X). IfA, B,CeU(X), then

d{AuB,C) = d{A,C)vd{B,C)

where we used the notation xVj/to mean the maximum of the two real numbers xand y.

Page 244: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 225

Definition. Let (X, d) be a complete metric space. Then the Hausdorff distancebetween points A and B in %(X) is defined as

h{A,B) = d{A,B)\/d{B,A).

The following theorem summarizes the main facts about a hyperbolic iterated func-tion system.

Theorem. Let {X : wn, n = 1,2,..., N} be a hyperbolic iterated function systemwith contractivity factor s. Then the transformation W : H(X.) -» W(X.) definedby

W(B) := U wn(B)n=l

for all B e "H(X), is a contraction mapping on the complete metric space ('H(X), h(d))with contractivity factor s. That is

h{W{B),W(C))<s-h{B,C)

for all B,C e H(X). Its unique fixed point, A £ H(X), obeys

N

A = W{A)= \Jwn(A)

and is given byA= lim W(n){B)

for any B e n(X).

Definition. The fixed point A G "H(X) is called the attractor of the iteratedfunction system.

8.2.2 Cantor Set

The standard Cantor set is described by the following iterated function system

{R : wu w2}

wherewi(x):=-x, w2(x) = -x + -.

This is an iterated function system. Starting from the unit interval [0,1] we have

wi([0,1]) = [0,1/3], w2([0,1]) = [2/3,1]

iui([0,l/3]) = [0,1/9], t«2([0,l/3]) = [2/3,7/9]

wx([2/3,1]) = [2/9,1/3], «/2([2/3,1]) = [8/9,1]

Page 245: Nonlinear Workbook (3rd)

226 CHAPTER 8. FRACTALS

etc.. Thus the Cantor set is an example of a fractal. The construction of thestandard Cantor set (also called Cantor middle third set or ternary Cantor set) isas follows. We denote the open interval

((3r - 2)/3", (3r - l)/3")

by 7n>r, where r = 1,2,..., 3 n - 1 . We set

3""1 oo

Gn '•= (J In,n G := |J Gn.r=l n=l

The set C = [0,1]\G is called the standard Cantor set.

In other words: let / = [0,1] be the unit interval. We construct the standard Cantorset in the following way. We set

Eo = [0,1]

*-[*K-'l

etc.. This means we remove the open middle third, i.e. the interval (|, f). In thesecond step we remove the pair of intervals (9, | ) and ( | , | ) . Then we continueremoving middle thirds in this fashion. Then £* is the union of 2k disjoint compactintervals and

C:=C\Ek.

The standard Cantor set has cardinal c, is perfect, nowhere dense and has Lebesguemeasure zero. Every x £ C can be written as

x = a^"1 + a23~2 + a33~3 -I where a,- £ {0, 2} .

The corresponding Cantor function is also called the Devil's staircase.

II Cantor.cpp

#include <iostream>#include "rational.h"#include "verylong.h"#include "vector.h"using namespace std;

const Rational<Verylong> a = Rational<Verylong>(0); // lower limitconst RationaKVerylong> b = RationaKVerylong>(l); // upper limit

Page 246: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 227

class Cantor{

private:Vector<Rational<Verylong> > CS;

int currentSize;public:Cantor(int);Cantor(const Cantorfe); // copy constructorint stepO ;void runO ;

friend ostreamfc operator « (ostreamft,const Cantor&);>;

Cantor::Cantor(int numStep) : CS(pow(2,numStep+D), currentSize(2){ CS[O] = a; CS[1] = b; >

Cantor::Cantor(const Cantorfe s): CS(s.CS), currentSize(s.currentSize) { }

int Cantor::stepO{int i, newSize;static RationaKVerylong> three(3), tt(2,3);static int maxSize = CS.lengthO;

if(currentSize < maxSize)

{for(i=0;i<currentSize;i++) CS[i] /= three;newSize = currentSize + currentSize;for(i=currentSize;i<newSize;i++) CS[i] = CS[i-currentSize] + t t ;

currentSize = newSize;

return 1;

>

return 0;

}

void Cantor::run() { while(step() != 0); }

ostreamfe operator << (ostreamft s,const Cantorfe c){for(int i=0;i<c.currentSize;i+=2){s « "[" « c.CS[i] « " ";

Page 247: Nonlinear Workbook (3rd)

2 2 8 CHAPTER 8. FRACTALS

s « c.CS[i+l] « "] ";}return s;

}

int main(void)

{const int N = 6;Cantor C(N);cout « C « endl;for(int i=0;i<N;i++)

{

C.stepO;cout « C « endl;>

return 0;

}

8.2.3 Heighway's DragonThe Heighway dragon is a set in the plane. It is the attractor of an iterated functionsystem. The iterated function system is given by

, s fl/2 -1 /2 \ , > / - 1 / 2 - l / 2 \ / 1 \W l ( x ) =ll/2 1/2 JX' W2W=(l/2 -1/2JX+UJ

where x G R2, i.e. x = (xi,x2)T. We have a hyperbolic IFS with each map being

a similitude of ratio r < 1. Therefore the similarity dimension, d, of the uniqueinvariant set of the IFS is the solution to

y y = 1 = , d = MV2) = M2)ti M1/V2) ln(V2)

where r = l/ \ /2.

The Heighway's dragon can also be constructed as follows. The first approximationPo is a line segment of length 1. The next approximation is Px. It results from Poby replacing the line segment by a polygon with two segments, each of length l/\/2~,joined at a right angle. The two ends are the same as before. There are two choicesof how this can be done. Let us choose the left side. For P2, each line segment inPi is replaced by a polygon with two segments, each having length l/%/2 times thelength of the segment that is replaced. The choices alternate between left and right,starting with left, counting from the endpoint of the bottom. The dragon is thelimit P of this sequence, Pn, of polygons. Closely related to the Heighway dragonis the Levy dragon.

Page 248: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 229

The Java program Dragon. Java asks the user to enter the n-th step in the construc-tion of the dragon.

// Dragon.Java

import java.awt.*;import java.awt.event.*;

public class Dragon extends Frame implements ActionListener

{public Dragon()

{addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});

drawButton.addActionListener(this);setTitleO'Dragon");

Panel parameterPanel = new Panel();parameterPanel.setLayout(new GridLayout(2,1));Panel nStepsPanel = new Panel();nStepsPanel.add(new LabelC'no of steps = "));nStepsPanel.add(nStepsField);Panel buttonPanel = new Panel();buttonPanel.add(drawButton);parameterPanel.add(nStepsPanel);parameterPanel.add(buttonPanel);addO'North".parameterPanel);addC'Center".dragonAttractor);setSize(400,400);setVisible(true);>

public static void main(String[] args){ new Dragon(); }

public void actionPerformed(ActionEvent action){if(action.getSourceO == drawButton)dragonAttractor.setSteps(Integer.parselnt(nStepsField.getText()));System.out.println(Integer.parselnt(nStepsField.getText()));}TextField nStepsField = new TextField("6",6);Button drawButton = new Button("Draw");

Page 249: Nonlinear Workbook (3rd)

230 CHAPTERS. FRACTALS

DragonAttractor dragonAttractor = new DragonAttractorO;}

class DragonAttractor extends Canvas{private int n, scaling;public DragonAttractorO { n = 6; }public void dragonr(int xl,int yl.int x2,int y2,int x3,int y3,int n){if(n == 1)

{

Graphics g = getGraphicsO ;g.drawLine(xl+scaling,yl+scaling,x2+scaling,y2+scaling);g.drawLine(x2+scaling,y2+scaling,x3+scaling,y3+scaling);>else

{int x4 = (xl + x3)/2; int y4 = (yl + y3)/2;int x5 = x3 + x2 - x4; int y5 = y3 + y2 - y4;dragonr(x2,y2,x4>y4,xl,yl,n-l);dragonr(x2,y2,x5,y5,x3,y3,n-l);

}

}

public void paint(Graphics g)

{Dimension size = getSizeO;scaling = Math.min(size.width,size.height)/4;

int xorig = scaling; int yorig = scaling;int xl = xorig + scaling; int yl = yorig;int x2 = xorig; int y2 = yorig - scaling;int x3 = xorig - scaling; int y3 = yorig;dragonr(xl,y1,x2,y2,x3,y3,n);

}

public void setSteps(int nSteps)

{

n = nSteps;repaint();>

}

Page 250: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 231

8.2.4 Sierpinski Gasket

The Sierpinski gasket is the attractor of an iterated function system with X = R2

and

W 1 U ) : = U 0.5 J U )fXl\ (0.5 0 \ (Xl\ (0.5\

Thus N = 3. One of the ways to construct the Sierpinski gasket is by starting witha triangle. Then splitting the triangle into four triangles and removing the openmiddle one. The same procedure then applies to the remaining three triangles. Theterm trema refers to the removed pieces.

In the Java program Sierpienksi .Java we display the p-th step in the constructionof the Sierpinski gasket. We set p = 4.

// Sierpinski.Java

import j ava.awt.*;import j ava.awt.event.*;import j ava.awt.Graphic s;

public class Sierpinski extends Frame

{public Sierpinski0

isetSize(600,500);addWindowLi stener(new WindowAdapter(){ public void windowClosing(WindowEvent event){ System.exit(0); >});>

public void paint(Graphics g)

ig.drawRect(40,40,600,400);int p, n, nl, 1, k, m, ull, u21, vll, v21, xl;double ul, u2, vl, v2, a, h, s, x, y;p = 4; // step in the constructiondouble T [] = new double [p] ;a = Math.sqrt(3.0);for(m=0;m<=p;m++)

{

Page 251: Nonlinear Workbook (3rd)

2 3 2 CHAPTER 8. FRACTALS

for(n=0;n<=((int) Math.exp(m*Math.log(3.0)))-n++){nl = n;for(l=0;K=(m-l);l++){T[l] = niy.3; nl = nl/3;}x = 0.0; y = 0.0;for(k=0;k<=(m-l);k++){double temp = Math.exp(k*Math.log(2.0));x += Math.cos((4.0*T[k]+1.0)*Math.PI/6.0)/temp;y += Math.sin((4.0*T[k]+1.0)*Math.PI/6.0)/temp;}ul = x + a/(Math.exp((m+1.0)*Math.log(2.0)));u2 = x - a/(Math.exp((m+1.0)*Math.log(2.0)));vl = y - 1.0/(Math.exp((m+1.0)*Math.log(2.0)));v2 = y + 1.0/(Math.exp(m*Math.log(2.0)));xl = (int)(100*x+300+0.5);ull = (int)(100*ul+300+0.5); u21 = (int)(100*u2+300+0.5);vll = (int)(100*vl+300+0.5); v21 = (int)(100*v2+300+0.5);g.drawLine(ull,vll,xl,v21);g.drawLine(xl,v21,u21,vll);g.drawLine(u21,vll,ull,vll);}>>

public static void main(String[] args){Frame f = new SierpinskiO;f.setVisible(true);>

}

8.2.5 Koch Curve

The Koch curve is a so-called monster curve. While it is of infinite length, it enclosesa simply connected region of finite area in the xy plane. The method of constructingthe Koch curve is as follows. Take an equilateral triangle and trisect each of it sides;then, on the middle segment of each side, construct equilateral triangles whoseinteriors lie external to the region enclosed by the base triangle and delete themiddle segments of the base triangle. This basic construction is then repeated onall of the sides of the resulting curve, and so on ad nauseam. The curve is definedso that the areas of all triangles lie inside it and it should contain nothing else. The

Page 252: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 233

perimeter is the length of this curve. If r = 0 denotes the stage of evolution whenonly the base triangle of unit side is there, r = 1 denotes the stage when threetriangles of side | have been added, etc.. Then it can be seen that the perimeter PT

of the Koch curve at the r-th stage is

Pr = (4/3)rP0.

The perimeter grows unboundedly as r increases, but the area Ar included by thecurve at the r-th stage is

Ar = Ao (1 + - Y, 4 ' 3"2 ! I •V 4 te{l,2,...,r} /

As r —> oo we find that

AT - > - A Q .0

The number of the straight line segments making up the curve at the r-th stage isfour times that of the line segments making up the curve at the (r — l)-th stage,but the segments become three times smaller in size. The similarity dimension, thecapacity and the Hausdorff dimension of the Koch curve turns out to be

ln(3) •

The topological dimension is equal to one.

The iterated function system for the Koch curve is given by X = R2 and

W l ( x ) : = l 0 1/3 J X

, , / 1/6 -\/3/6\ / l /3\W 2 ( X H V 3 > 1/6 ) X + U ), , ( 1/6 s/?>l§\ { 1/2 \

w^--={o i / 3 J x + i o j -The fixed invariant set of this IFS is the Koch curve. We have a hyperbolic IFS witheach map being a similitude of ration r < 1. Therefore the similarity dimension, d,of the unique invariant set of the IFS is the solution to

Er'=l=>«*= ^1/4 = = 1.2619...ti Hr) ln(3)

where r = 1/3. Koch constructed his curve in 1904 as an example of a non-differentiable curve, that is, a continuous curve that does not have a tangent atany of its points. Karl Weierstrass had first demonstrated the existence of such a

Page 253: Nonlinear Workbook (3rd)

234 CHAPTER 8. FRACTALS

curve in 1872. The length of the intermediate curve at the n-th iteration of the con-struction is (4/3)n, where n = 0 denotes the original straight line segment. Thereforethe length of the Koch curve is infinite. Moreover, the length of the curve betweenany two points on the curve is also infinite since there is a copy of the Koch curvebetween any two points.

Instead of starting from an equilateral triangle, one can also start from the unitinterval. This is done in the Java program Koch. Java.

/ / Koch.java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Koch extends Frame{public KochO

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); >});>

public int power(int a, int n)

{int t = 1;for(int i=l;i<=n;i++) { t = t*a; }return t;

>

public void paint(Graphics g)

{.

g.drawRect(20,40,500,400);int p, m, u, v, ul, vl, a, b;double h, s, x, y, xl, yl;p = 3; // step in the constructionint kmax = 100;int T [] = new int [kmax];m = 0; h = 1.0/((double) power(3,p));x = 0.0; y = 0.0;

for(int n=l;n<=(power(4,p)-l);n++)

{

Page 254: Nonlinear Workbook (3rd)

8.2. ITERATED FUNCTION SYSTEM 235

m = n;ifor(int l=O;K=(p-l);l++){T[ l ] = mV.4; m = m/4;>s = 0 . 0 ;for( in t k=0;k<=(p-l);k++){s += ((double) ((T[k]+1)7.3 - 1 ) ) ;>xl = x; yl = y;x += Math.cos(Math.PI*s/3.0)*h; y += Math.sin(Math.PI*s/3.0)*h;u = (int)(300*x + 100 + 0.5); v = (int)(300*y + 150 + 0.5);ul = (int)(300*xl + 100 + 0.5); vl = (int)(300*yl + 150 + 0.5);g.drawLine(u,v,ul,vl);>}}

public s t a t i c void main(String[] args){Frame f = new KochO ;f . se tVis ible( t rue) ;>

}

8.2.6 Fern

Iterated functions systems can also be used to create fern like patterns. The Ferniterated function system, orginally found by M. Barnsley, is a well known and gen-erates a Black Spleenwort fern. The fern is created using the following four affinetransformations

Wj(x) = (r'C0SfQ*) '">**&) (x>) + ( MJV \rjsm{aj) SjCos(^) ) \x2) \a2jJ

for j = 1, 2, 3,4. For the four transformations we have

-<"-(!! .!.)(£)•(!)

, , / 0 . 2 - 0 . 2 6 \ f x x \ / 0 \^W'10.23 O.22JUJM1.6J

, . /-0.15 0.28\ / i A / 0 \W^={0.26 0 .24 jUJ + l0.44j-

Page 255: Nonlinear Workbook (3rd)

236 CHAPTERS. FRACTALS

The matrix norms of the matrices are smaller than 1., Thus the maps Wi,w2, w3 and11)4 are all contractions. What is the meaning of the maps? w± maps the entire fernonto the fern with the two lowest branches and the lowest part of the stem removed.The fixed point of Wi is the tip of the fern. w2 transforms the entire fern to thebranch on the lower right; it shrinks, narrows, and flips. Similarly, w3 transformsthe entire fern to the branch on the lower left. Finally, W4 maps the entire fern intoa line segment on the %i axis; its fixed point is the origin, and it is used to generatethe stem.

In our Java program Fern. j ava (Applet) we modify the coefficients of the four mapsfor a better graphical display.

/ / Fern.Java

import java.awt.*;import j ava.applet.*;import java.awt.Graphics;

public class Fern extends Applet

{double mxx, myy, bxx, byy;

public void plot(Graphics g)

idouble x, y, xn, yn, r;int pex, pey;int max = 15000;x=0.5; y=0.0; // starting pointconvert(0.0,1.0,1.0,-0.5);setBackground(Color.white);g.setColor(Color.black);for(int i=0;i<=max;i++) // iterations•C

r = Math.randomO; // generate a random number

if(r <= 0.02)

{

xn = 0.5; // map 1

yn = 0.27*y;

}else if((r>0.02) && (r<=0.17)){xn = -0.139*x + 0.263*y + 0.57; // map 2

yn = 0.246*x + 0.224*y - 0.036;

}else if((r>0.17) && (r<=0.3))

Page 256: Nonlinear Workbook (3rd)

8.3. MANDELBROT SET 237

{xn = 0.17*x - 0.215*y + 0.408; // map 3yn = 0.222*x + 0.176*y + 0.0893;

}else

{xn = 0.781*x + 0.034*y + 0.1075; // map 4yn = -0.032*x + 0.739*y + 0.27;

>x = xn; y = yn;pex = (int) (mxx*x + bxx);pey = (int) (myy*y + byy);g.drawLine(pex,pey,pex,pey); // output to screen

}}

void convert(double xiz.double ysu,double xde,double yinf)

{double maxx, maxy, xxfin, xxcom, yyin, yysu;maxx = 600; maxy = 450;xxcom = 0.15*maxx; xxfin = 0.75*maxx;yyin = 0.8*maxy; yysu = 0.2*maxy;mxx = (xxfin-xxcom)/(xde-xiz);bxx = 0.5*(xxcom+xxfin-mxx*(xiz+xde));myy = (yyin-yysu)/(yinf-ysu);byy = 0.5*(yysu+yyin-myy*(yinf+ysu));

}

public void paint(Graphics g)

{plot(g);showStatusC'Done");}

}

8.3 Mandelbrot Set

Let C be the complex plane. Let c € C. The Mandelbrot set M is defined as follows

M := { c e C : c, c2 + c, (c2 + c)2 + c, . . . -ft oo }.

To find the Mandelbrot set we study the recursion relation

Zt+i =zf+C

Page 257: Nonlinear Workbook (3rd)

238 CHAPTER 8. FRACTALS

where t = 0,1, 2 , . . . and the initial value is given by zQ = 0. This is obvious since

z\ = c, z2 = c2 + c, z3 = (c2 + c)2 + c, . . .

etc.. Since z — x + iy and c = c\ 4- ic2 with a;,y, C!,c2 G R we can rewrite therecursion relation as

xt+i — x\ - yt+ci, yt+1 = 2xtyt + c2

with the initial value (xo,3/o) = (0,0). For a given c € C (or (ci,c2) 6 R2) we cannow study whether or not c belongs to M. For example,

(ci,c2) = (0,0)

and(cllC2) = (1/4,1/4)

belong to M. The point (ci,c2) = (1/2,0) does not belong to M. We find that

n 1 3 170 = 0, z1 = - , *a = - , «3= jg , - . .

Thus the set M can be defined using nothing more than complex arithmetic. Thepolynomial fc{z) = z2 + c has a unique critical point w = 0, i.e., a point w wherethe derivative f'c{u>) equals 0. The parameter value c is called the critical valuec = /c(0). Since both the variable z and the parameter c fill out a plane, it cancause some confusion; in particular because we jump back and forth between theseplanes. For fixed c we refer to the z-plane as the dynamical plane for fc, while werefer to the c-plane as the parameter plane.

// Mandelbrot.Java

import java.awt.*;import java.awt.event.*;import java.awt.Graphics;import j ava.awt.image.*;

public class Mandelbrot extends Frame

{public Mandelbrot()

{setSize(600,500);addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent event){ System.exit(0); >});

}

public void paint(Graphics g)

Page 258: Nonlinear Workbook (3rd)

8.3. MANDELBROT SET 239

{Image img;int w = 256; int h = 256;int [] pix = new int [w*h] ;int index = 0;

int iter;double a, b, p, q, psq, qsq, pnew, qnew;for(int y=O;y<h;y++){b = ((double)(y-128))/64;for(int x=0;x<w;x++)

{a = ((double)(x-128))/64;p = q = 0.0;iter = 0;while(iter < 32)

ipsq = p*p; qsq = q*q;if(psq + qsq >= 4.0) break;pnew = psq - qsq + a; qnew = 2.0*p*q + b;p = pnew; q = qnew;iter++;}if(iter == 32){pix[index] = 255 « 24 I 255; // 255 « 24 I 256 => black}index++;}>img = createlmage(new MemoryImageSource(w,h,pix,0,w));g.drawImage(img,0,0,null);>

public static void main(String[] args)-CFrame f = new Mandelbrot();f.setVisible(true);>

>

Page 259: Nonlinear Workbook (3rd)

240 CHAPTER 8. FRACTALS

8.4 Julia Set

Let fx be a mapping in the complex plane, where A is a complex parameter. TheJulia set of f\ can be denned in two different ways.

Dynamical Definition. J(/A) is the closure of the set of repelling periodic points.A point ZQ G C is periodic if

for some n, where /{"' denotes the n-fold composition of fx with itself. This periodicpoint is repelling if

|(/!n))'(*0)| > i

where ' denotes the derivative.

Complex Analytic Definition. J(fx) is the set of points z such that the familyof functions

{/nfails to be a normal family of functions in every neighbourhood of z.

We also find the definition in the form:z is a stable point of the (rational) map fx if there is a neighbourhood U of zon which the iterates

f(n)

form a normal family. Let Q, be the stable set of / . The Julia set is the complementof ft. It is named after the mathematician Julia who, along with Fatou, originatedthis subject in the 1920's. The set ft is open by definition. It must either be emptyor dense in C and is often not connected.

Definition. A family of complex analytic maps {fx}, defined on a domain D iscalled a normal family if it satisfies the following conditions: every infinite subset of{fx} contains a subsequence which converges uniformly on every compact subset ofD.

The Julia set has mainly been studied for the map

fc{z)=Z2 + C

where c € C. In the program Jul ia . Java we consider the Julia set for this map,where c = p + iq and z2 = x2 — y2 + iixy with x, y, p, q are real.

/ / Julia.Java

import java.awt.*;import j ava.awt.event.*;

Page 260: Nonlinear Workbook (3rd)

8.4. JULIA SET 241

import java.awt.Graphics;

public class Julia extends Frame

{public JuliaO

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event)

{ System.exit(0); }});

}

public void paint(Graphics g)

{int a = 400, b = 400, kmax = 200, m = 100;double xO, yO, dx, dy, x, y, r;double p = -0.12256117, q = 0.74486177;double xmin = -1.5, xmax = 1.5;double ymin = -1.5, ymax = 1.5;dx = (xmax-xmin)/((double) a-1); dy = (ymax-ymin)/((double) b-1);for(int nx=0;nx<=a;nx++)for(int ny=0;ny<=b;ny++){int k = 0;xO = xmin + nx*dx; yO = ymin + ny*dy;r = 0;while((k++ <= kmax) && (r < m))

{x = xO; y = yO;xO = x*x - y*y + p; yO = 2*x*y + q; r = x0*x0 + yO*yO;if(r > m) { }}if(r <= m) { g.drawLine(nx,ny,nx,ny); }

}}

public static void main(String[] args){Frame f = new JuliaO ;f.setVisible(true);>

}

Page 261: Nonlinear Workbook (3rd)

242 CHAPTER 8. FRACTALS

8.5 Weierstrass Function

One way to obtain a function whose graph has a fine structure (fractal structure) isto add together a sequence of functions which oscillate increasingly rapidly. Thus if

oo

Yl \ak\ < °°k=l

and Xk —> oo if k —> oo, the function defined by the trigonometric series

oo

f(x) = 52ajSm(Xjx)

might be expected to have a graph of dimension greater than 1 if a* and A/t arechosen suitably. The best-known example of this type is the function

oo

/(z) = £A(a-2Wsin(A'a:)

where 1 < s < 2 and A > 1, constructed by Weierstrass to be continuous butnowhere differentiable. It is conjectured that the Weierstrass function has a graphof dimension s, but this does not appear to have been proven rigorously. The di-mension cannot exceed s. There are a number of variants of the Weierstrass function.

There is a close connection of the Weierstrass function and a Levy-flight randomwalk. We start with the discrete jump-probability distribution

where b > A > 1 are parameters that characterize the distribution, and 6(x,y) isthe Kronecker delta, which is equal to 1 when x = y and otherwise zero. Thisdistribution function allows jumps of length 1, b, b2, b3 However, whenever thelength of the jump increases by an order of magnitude (in base b), its probability ofoccurring decreases by an order of magnitude fin base A). Typically one gets a clusterof A jumps roughly of length 1 before there is a jump of length b. Approximately Asuch clusters, separated by lengths of order 6, are formed before one sees a jump oforder b2. This goes on, forming a hierarchy of clusters within clusters. The Fouriertransform of p(x) is

\ — 1 °°

lX 3=0

which is the self-similar Weierstrass function. The self-similarity of the Weierstrassfunction appears explicitly through the equation

p(fc) = -p(bk) + — = - cos(A:)

Page 262: Nonlinear Workbook (3rd)

8.5. WEIERSTRASS FUNCTION 243

which has a small-/: solution

p(fc)«exp(- | jf)

with/? = log(A)/log(&).

For our Java-Applet program we select s = 3/2 and A = 4. Then we draw thefunction

Mz) = E^sin(4%)

which is an approximation of the Weierstrass function with N = 3. The second andthird pieces of the applet tag indicate the width and the height of the applet in pixel.The upper left corner of the applet is always at z-coordinate 0 and ^/-coordinate 0.

// Weierstrass.Java

import java.awt.Graphics;

public class Weierstrass extends java.applet.Applet

{double f(double x)

{double expr = 1.0/2.0*Math.sin(4.0*x) + 1.0/4.0*Math.sin(16.0*x)

+ 1.0/8.0*Math.sin(64.0*x);return expr*getSize().height/4.0 + 150.0;

>

public void paint(Graphics g){for(int n=0;n<getSize().width;n++)ig.drawLine(n,(int)f(n),n+l,(int)f(n+1));}>

}

The HTML file Weierstrass.html is as follows:

<HTML><C0MMENT> HTML file to link with Weierstrass.class file </C0MMENT><TITLE> Approximation of Weierstrass Function </TITLE><APPLET code="Weierstrass.class" width=200 height=250></APPLET></HTML>

Page 263: Nonlinear Workbook (3rd)

Chapter 9

Cellular Automata

9.1 Introduction

Cellular automata (Wolfram [97]) may be considered as discrete dynamical systems.They are discrete in several aspects. Firstly, they consist of a discrete spatial latticeof sites. Secondly they evolve in discrete time steps, i.e. i = 0 ,1 ,2 , . . . . Thirdlyeach lattice site (or box, or cell) has only a finite discrete set of possible values.The simplest case is that the dependent variable takes two values, 0 and 1. Thesimplest case of a lattice is a linear chain with N lattice sites (or boxes). Periodicboundary conditions (also called cyclic boundary conditions) or open end boundaryconditions can be imposed. In two dimensions we can consider different lattices(grids), for example rectangular, triangular or hexagonal lattices.

Any cellular automata rule in one dimension can be described by an evolution equa-tion of the form

Oi(* + l) = /[o{i}(*)]. 4 = 0,1,2,. . .

where Oj(i) is the state of the site i at time t and f[a,{i}{t)] is a function of the statesof sites in a neighbourhood of lattice site i at time t with the initial states given bya,i(t = 0). In the one-dimensional case (i.e., one has a linear chain) the equation canalso be written as

Oi(t + 1) = f[oi-r(t), Oi-T+i(t),.. .,ai+r(t)}.

The local rule / has the range of r sites. In most cases, one considers the case r = 1.Thus a 1-dimensional cellular automaton consists of a line of sites with values at

between 0 and k — 1. These values are updated in parallel (synchronously) in discretetime steps. Much of the section is concerned with the study of a particular k = 2,r = 1 cellular automaton.

244

Page 264: Nonlinear Workbook (3rd)

9.1. INTRODUCTION 245

For example, the rule for evolution could take the value of a site at a particulartime step to be the sum modulo two of the values of its two nearest neighbours onthe previous time step. The pattern is found to be self-similar, and is characterizedby a fractal dimension Iog2&. Even with an initial state consisting of a randomsequence of 0 and 1 sites, say each with probability \, the evolution of such a cel-lular automaton leads to correlations between separated sites and the appearanceof structure. This behaviour contradicts the second law of thermodynamics for sys-tems with reversible dynamics, and is made possible by the irreversible nature of thecellular automaton evolution. Starting from a maximum entropy ensemble in whichall possible configurations appear with equal probability, the evolution increases theprobabilities of some configurations at the expense of others.

Despite the simplicity of their construction, cellular automata are found to be capa-ble of diverse and complex behaviour. Numerical studies suggests that the patterngenerated in the time evolution of cellular automata from disordered initial statescan be classified as follows:

(i) Evolves to homogeneous state

(ii) Evolves to simple separated periodic structures

(iii) Evolves to chaotic aperiodic patterns

(iv) Evolves to complex pattern of localized structures.

Constants of motion, if any exist, in classical mechanics and conservation laws, ifany exist, in field theory play an important role in studying the behaviour of thedynamical system. If invariants exist for a cellular automaton, then one has a parti-tion of its state space. Both irreversible and reversible cellular automata can exist.A rule of cellular automata is said to be reversible if it is backwards deterministic.

For mathematical purposes, it is often convenient to consider cellular automatawith an infinite number of sites. But practical implementations must contain a fi-nite number of sites iV. These are typically arranged in a circular register to obtainperiodic boundary conditions. It is also possible to arrange the sites in a feedbackshift register.

Cellular automata can be considered as discrete approximations to partial differen-tial equations, and can be used as direct models for a variety of natural systems.They can also be considered as discrete dynamical systems corresponding to con-tinuous mappings on the Cantor set. Finally they can be viewed as computationalsystems whose evolution processes information contained in their initial configura-tions.

For one-dimensional cellular automata with r = 2 and k = 1 we can define rulenumbers as follows. Let j be a fixed lattice point (cell). Then consider its two

Page 265: Nonlinear Workbook (3rd)

246 CHAPTER 9. CELLULAR AUTOMATA

neighbours j — 1 and j + 1. For these three lattice sites we can have the following8 = 23 configurations at time t

111 110 101 100 Oil 010 001 000.

Consider as an example the cellular automata

aj(t + 1) = (%_i(t) + aj+1(t)) mod 2 .

Then the eight configurations given above are mapped into aj(t+ 1) as follows

111 —>• 0, 110 -> 1. 101 ->0, 100 -> 1,

011 -> 1, 010-*0, 001 -> 1, 000-J-0

where we used the mod 2 addition (XOR), i.e.

0 + 0 = 0, 0 + 1 = 1, 1 + 0 = 1 , 1 + 1 = 0.

Thus the map provides the binary string

01011010.

Since1 • 26 + 1 • 24 + 1 • 23 + 1 • 21 = 90

we associate the map given above with the rule 90. Vice versa, from the rule numberwe can derive the map. For example consider the rule 56. Since

56 = 1 • 25 + 1 • 24 + 1 • 23

this number corresponds to the binary string 00111000. Thus we have the maps

111 —> 0, 110-^0, 101 —>• 1, 100->1

011 —»• 1, 010 ->0, 001-»0, 000->0.

9.2 One-Dimensional Cellular Automata

As an example we consider a one-dimensional ring of N sites labelled sequentiallyby the index i starting from zero, i.e. i = 0 , 1 , . . . , N - 1. We impose periodicboundary conditions, i.e. N = 0. Each site i can take the values a{ = 0 or a; = 1.Let aj evolve as a function a,(t) of discrete time steps t = 0,1, 2 , . . . according tothe map

Oi(t + 1) = (a,i-i(t) + ai+i(t)) mod 2.

Since the map involves the sum (a^i + aj+i) mod 2, it is a nonlinear map. Themap can also be expressed using spin variables Sj € { + 1 , - 1 } , where 5, = — 1

Page 266: Nonlinear Workbook (3rd)

9.2. ONE-DIMENSIONAL CELLULAR AUTOMATA 247

corresponds to Oj = 0 and Si = +1 corresponds to a; = 1. Then the map can bewritten as

Si(t + l) = -Si-1(t)Si+1(t).

This map has the following properties

S1(t)Si{t)---SN{t) = (-l)N

for t = 1,2... andSi{t + 2n) = -Si-V{t)Si+v (t)

for t = 0 , 1 , . . . and n = 0 ,1 , . . . .

For N = 2k, k = 1,2,..., one has all Si{t) = - 1 for all times t > 2k~\ irrespectiveof the initial spin configuration of the ring.

In the program Spin.cpp we set N = 5, i.e. we have five cells (or sites). Thenumber of time steps is T = 7. As expected we find an eventually periodic pattern,i.e., S(6) = S(l).

/ / spin.cpp

#include <iostream>using namespace std;

int main(void)

-C// number of cellsint N = 5;

// allocation of memoryint* S = new int[N];int* W = new int[N];

// initial values

SCO] = - 1 ; S[l] = - 1 ; S[2] = 1; S[3] = - 1 ; S[4] = - 1 ;

int T = 7; // number of time steps

// iterations

for(int t=l;t<T;t++)•C

for(int j=O;j<N;j++){W[j] - S[j];}

Page 267: Nonlinear Workbook (3rd)

248 CHAPTER 9. CELLULAR AUTOMATA

for(j=0;j<N;j++){S[j] = -W[((j-l)+N)1/.N]*W[(j+l)*/.N];>

/ / display the result for each time stepcout « "t = " « t « " ";for(j=0;j<N;j++){ cout « "S[" « j « "] = " « S[j] « " "; }cout « endl;}/ / end iteration

delete [] S; delete [] W;return 0;

}

Another example is given by

Uj(t + 1) = sgn(l - \uj-i(t) - Uj(t) + uj+1(t) - 1|)

where Uj{t) S { — 1, +1}. Here we have

Mf-lft) I Uj(t) I Uj+l(t) [I Uj(t+i)- 1 - 1 - 1 - 1- 1 - 1 1 1- 1 1 - 1 - 1- 1 1 1 - 11 - 1 - 1 11 - 1 1 - 11 1 - 1 - 11 I 1 1 1 II 1

9.3 Two-Dimensional Cellular Automata

The extension to two dimensions is significant for comparisons with many exper-imental results on pattern formation in physical systems. Immediate applicationsinclude dendritic crystal growth, reaction-diffusion systems and turbulent flow pat-terns. The Navier-Stokes equations for fluid flow appear to admit turbulent solutionsonly in two or more dimensions. A cellular automaton consists of a regular latticeof sites. Each site takes on k possible values, and is updated in discrete time stepsaccording to a rule / ^hat depends on the value of sites in some neighbourhoodaround it. There are several possible lattices and neighbourhood structures for two-dimensional cellular automata. In literature mainly square lattices with nearest

Page 268: Nonlinear Workbook (3rd)

9.3. TWO-DIMENSIONAL CELLULAR AUTOMATA 249

neighbour interaction are studied. Triangular and hexagonal lattices are also possi-ble, but are not used in the examples given here.

Neighbourhood structures considered for two-dimensional cellular automata. In thecellular automaton evolution, the value of the centre cell is updated according to arule that depends on the values of the neighbouring cells.

Let [i]\j] be a lattice site. Then we can consider a four neighbours square cellularautomaton, where the neighbours are

Htf + l], [i + l]bl, W[? -1], [i-m-

Thus the time evolution is given by

aitj{t +1) = f{aitj{t),aiij+i{t), ai+hj(t), Ojj_i(i), fli-i,j(i)).

Cellular automata with this neighbourhood are termed "five-neighbour square".Here we often consider the special class of totalistic rules, in which the value of asite depends only on the sum of the values in the neighbourhood

Oij(t + 1) = fMt) + Oij+i(t) + Oi+ij(*) + Oij-i(t) + Oi-ij(*)l •

We also can consider the case with eight neighbours given by

[i + l M [i + l]\j + l], Wb + l], [»-l]L; + l]

[ i - i E [ i - i ] b " - 1 ] , Wb ' - i ] , [i + i]\j-i\.

This neighbourhood is termed "nine-neighbour square." These neighbourhoods aresometimes referred to as the von Neumann and Moore neighbourhoods, respectively.

Totalistic cellular automaton rules assumes the value of the centre site depends onlyon the sum of the values of the sites in the neighbourhood. With outer totalisticrules, sites are updated according to their previous values and the sum of the valuesof the other sites in the neighbourhood. The five-neighbour square, triangular, andhexagonal cellular automaton rules may all be considered as special cases of generalnine-neighbour square rules.

The mapOij(* + 1) = f[ai-u(t) + Oij-ift) + ai+hj(t) + aitj+l(t)}

can be specified by a codeC = J2f(n)kn.

nOne can also consider outer totalistic rules, in which the value of a site dependsseparately on the sum of the values of sites in a neighbourhood, and on the value ofthe site itself

aid(t + 1) = g(aitj(t), aitj+1(t) + ai+hj(t) + aitj^(t) + a^hj(t)).

Page 269: Nonlinear Workbook (3rd)

250 CHAPTER 9. CELLULAR AUTOMATA

Such rules are specified by a code

C = Y.9[aAkkn+a •n

We consider two-dimensional cellular automata with values 0 or 1 at each site, cor-responding to k = 2.

An example of an outer totalistic nine-neighbour square cellular automaton is theGame of Life, with a rule specified by code C = 224. This cellular automata wasintroduced by a Cambridge mathematician named John Horton Conway. Game ofLife is played by populating a rectangular, two-dimensional grid of cells with critters.Each cell can take the value 0 (dead) or 1 (alive). Then we observe the behaviour ofthe population over succeeding generations. Each cell [i][j] has eight neighbouringcells at

[t + l M [i + l}[j + l], [i][j + l], [i-l]\j + l]

[i-m, [ < - I ] [ J - I ] > [ W - i ] , [ t + i ] y - i ] .For our implementation we consider N x N cells, where N = 16 and impose a pe-riodic boundary condition in the i and j directions, i.e. N = 0. Both i and j startcounting from zero. The rules for Conway's Game of Life are

a) A new cell is born (set to 1) when there are 3 live neighboursb) A cell stays alive when surrounded by 2 or 3 live cells.c) Each cell with four or more neighbours dies of overpopulation and each cellsurrounded by one or no live cell dies of solitude.

// twocellular.cpp

#include <iostream>using namespace std;

int neighbours(int a, int b.int c.int d.int e.int f.int g.int h)

{i n t r e s u l t = a + b + c + d + e + f + g + h ;

return result;

}

int main(void)

{int i, j;int N = 8; // size of the grid

// allocating memory for N x N array

int** A = new int* [N];

for(i=0;i<N;i++)

Page 270: Nonlinear Workbook (3rd)

9.3. TWO-DIMENSIONAL CELLULAR AUTOMATA 251

A [i] = new int [N] ;int** B = new int* [N];for(i=0;i<N;i++)B [i] = new int [N] ;

// initial configurationfor(i=0;i<N;i++)for(j=0;j<N;j++)A[i][j] = 0;

// set the following grid points aliveA [5] [5] = 1; A [5] [6] = 1; A [6] [5] = 1; A [6] [6] = 1;

/ / iterationsint T = 10; // number of iterationsfor(int t=O;t<T;t++)

ifor(i=0;i<N;i++)

{

for(j=0;j<N;j++){B[i][j] = A[i][j];>}for(i=0;i<N;i++){for(j=0;j<N;j++){int temp =neighbours(B[((i+1)'/.N)] [j] ,B[((i+l)y.N)] [((j+l)'/.K)] ,B[i] [((j+D'/.N)] ,

B[((i-1+N)'/.N)] [((j+l)XK)] ,B[((i-l+N)°/.N)] [j] ,B[((i-1+N)'/.N)] [((j-l+N)'/.N)] ,B[i] [((j-i+N)'/.N)] ,B[((i+l)y.N)][((j-l+N)7.N)]);

/ / rule 1if((B[i][j] == 0) kk (temp == 3)){ A[i][j] = 1; }

/ / rule 2if((B[i][j] == 1) tek ((temp ==2) II (temp == 3)))i A[i][j] = 1; >

/ / rule 3if((B[i][j] == 1) kk ((temp >= 4) II (temp <= 1)))•C A[i][j] = 0; >}

Page 271: Nonlinear Workbook (3rd)

252 CHAPTER 9. CELLULAR AUTOMATA

ycout « endl;cout « "t = " « t;cout « endl;

// output for each time stepfor(i=0;i<N;i++)-Cfor(j=0;j<N;j++){cout « "B[" « i « "]" « "[" « j « "] = " « B[i][j] « " ";>}cout « endl « endl;} // end for loop t

/ / free the memoryfor(i=0;i<N;i++) deleteD A[i];deleted A;for(i=0;i<N;i++) delete [] B[i];delete [] B;return 0;

>

The output is

t = 9B[0][0] = 0 B[0][l] = 0 B[0][2] = 0 B[0] [3] = 0 B[0][4] = 0B[0][5] = 0 B[0][6] = 0 B[0][7] = 0 B[l] [0] = 0 B[l][l] = 0B[l][2] = 0 B[l][3] = 0 B[l][4] = 0 B[l] [5] = 0 B[l][6] = 0B[l][7] = 0 B[2][0] = 0 B[2][l] = 0 B[2][2] = 0 B[2] [3] = 0B[2][4] = 0 B[2][5] = 0 B[2][6] = 0 B[2] [7] = 0 B[3][0] = 0B[3][l] = 0 B[3][2] = 0 B[3][3] = 0 B[3][4] = 0 B[3] [5] = 0B[3][6] = 0 B[3][7] = 0 B[4][0] = 0 B[4][l] = 0 B[4] [2] = 0B[4][3] = 0 B[4][4] = 0 B[4][5] = 0 B[4] [6] = 0 B[4][7] = 0B[5][0] = 0 B[5][l] = 0 B[5][2] = 0 B[5] [3] = 0 B[5][4] = 0B[5][5] = 1 B[5][6] = 1 B[5][7] = 0 B[6][0] = 0 B[6][l] = 0B[6][2] = 0 B[6][3] = 0 B[6][4] = 0 B[6][5] = 1 B[6][6] = 1B[6][7] = 0 B[7][0] = 0 B[7][l] = 0 B[7][2] = 0 B[7][3] = 0B[7][4] = 0 B[7][5] = 0 B[7][6] = 0 B[7][7] = 0

Page 272: Nonlinear Workbook (3rd)

9.4. BUTTON GAME 253

9.4 Button Game

The button game described below is closely related to two-dimensional cellular au-tomata. It is also worth inclusion since the Java program JButtonGame .Java showshow two-dimensional cellular automata can be implemented using the GUI capa-bility (jPanel class, JTextField class, JButton class, etc.) of Java. Given a gridwith 4 x 4 boxes, where one of the boxes is empty. The other 15 boxes contain thenumbers 1, 2, . . . , 15 so that each number occupies one box. For example

8 4 15 52 1 107 6 9 123 11 13 14

Clicking on one of the nearest neighbours of the empty box moves the number of thebox into the empty box and the clicked box with the number becomes the emptybox. For example, clicking on the box with the number 10 we obtain the pattern

8 4 15 52 1 107 6 9 123 11 13 14

Note that there are no cyclic boundary conditions imposed. Nearest neighboursare only on the vertical and horizontal line. Neighbours on the diagonal are notconsidered as nearest neighbours. The task is now to obtain the pattern

1 2 3 45 6 7 89 10 11 12

13 14 15

The class JPanel is a container class in which components can be placed. JPanelis derived from class Container. Components are placed on containers with con-tainer method add. In Java we can divide a top-level window into panels. Pan-els act as (smaller) containers for interface elements and can themselves be ar-ranged inside the window. For example we can have one panel for the buttonsand another for the text fields. Push buttons are created with the JButton class.JTextf ield(Str ing tex t , int cols) constructs a new JTextfieId, where textis the text to be displayed and cols the number of columns. Gridlayout arrangesthe components into rows and columns.

Page 273: Nonlinear Workbook (3rd)

254 CHAPTER 9. CELLULAR AUTOMATA

II JButtonGame.Java

import j ava.awt.*;import j ava.awt.event.*;import javax.swing.*;

class JButtonGame extends JFrame implements ActionListener{private int nRows, nCols, nButtons;private int blankCol, blankRow, clickedRow, clickedCol;private JButton[] [] buttons;private JTextField textField;

public JButtonGame()

{setSize (200,200);addWindowListener(new WindowAdapter (){ public void windowClosing(WindowEvent event){ System.exit(0);>});

nRows = 4; nCols = 4; nButtons = nRows * nCols;JPanel panel = new JPanelO;

panel.setLayout(new GridLayout(nRows,nCols));buttons = new JButton [nRows][nCols];

for(int nRow=0;nRow<nRows;nRow++)

{for( in t nCol=0;nCoKnCols;nCol++){buttons[nRow] [nCol] = new JButtonC'");buttons[nRow][nCol].addActionListener(this);panel.add(buttons[nRow][nCol]);}}

getContentPaneO .addC'Center" .panel);textField = new JTextField("",80);textField.setEditable(false);getContentPaneO.addO'South".textField);

int labelsUsed[] = new'int [nButtons];for(int i=0;i<nButtons;i++)

{boolean labelUsed;

Page 274: Nonlinear Workbook (3rd)

9.4. BUTTON GAME 255

int label;

do

{label = random(nButtons) + 1;labelUsed = false;for(int j=O;j<i;j++)labelUsed = ((labelUsed) II (label == labelsUsed[j]));} while (labelUsed);

labelsUsed[i] = label;int nRow = i/nCols;

int nCol = i - nRow*nCols;buttons[nRow][nCol].setText((new Integer (label)).toStringO);

>getButtonPosition((new Integer (nButtons)).toStringO);blankRow = clickedRow;blankCol = clickedCol;JButton blank = buttons[clickedRow][clickedCol];blank.setTextC");blank.setBackground(Color.green);} // end constructor JButtonGameO

private int random(int k)

{return (int) (k*Math.random() - 0.1);} // end method random(int)

private void getButtonPosition(String label)

•C

for(int nr=0;nr<nRows;nr++)

ifor(int nc=0;nc<nCols;nc++){if (buttons [nr] [nc] .getTextO .equals (label)){clickedRow = nr; clickedCol = nc;textField.setText("[" + nr + ',' + nc + " ] " + label);

>>>

} // end method getButtonPosition(String)

public void actionPerformed (ActionEvent e)

getButtonPosition(e.getActionCommandO);

Page 275: Nonlinear Workbook (3rd)

256 CHAPTER 9. CELLULAR AUTOMATA

textField.setText("[" + blankRow + "," + blankCol+ "] = >' [" + clickedRow + "," + clickedCol + "]");

if(clickedRow == blankRow)•C

if(Math.abs(clickedCol - blankCol) == 1)moveBlank(blankRow,blankCol,clickedRow,clickedCol);}elseif(clickedCol == blankCol){if(Math.abs(clickedRow - blankRow) == 1)moveBlank(blankRow,blankCol,clickedRow.clickedCol);}> // end method actionPerformed

public void moveBlank(int oldRow.int oldCol,int newRow,int newCol)

{JButton oldBlank = buttons[oldRow][oldCol];JButton newBlank = buttons[newRow][newCol];

String label = newBlank.getText();newBlank.setText ("");newBlank.setBackground(Color.green);oldBlank.setText(label);oldBlank.setBackground(Color.lightGray);blankRow = newRow;blankCol = newCol;} // end method moveBlank

public static void main(String [] args)

{

new JButtonGameO.setVisible(true);

} // end method main

>

Page 276: Nonlinear Workbook (3rd)

Chapter 10

Solving Differential Equations

10.1 Introduction

In this chapter we consider numerical methods of integration for nonlinear ordi-nary differential equations. Almost all nonlinear differential equations can not besolved in closed form. Numerical methods must be applied. A number of problemsarise when we solve differential equations numerically. For example, if the differen-tial equations are derived from a conservative Hamilton system then the numericalscheme should preserve the total energy which is a constant of motion. For a non-linear differential equation it also can happen that the discretization scheme leadsto a chaotic map even though the original nonlinear differential equation did notshow chaotic behaviour. Stable fixed points of nonlinear differential equations canbecome unstable under the discretization scheme. We also find so-called spurioussolutions (also called ghost solutions). We consider autonomous

du . .Tt = f (u)

and nonautonomous systems of ordinary differential equations

du . .

We assume that we have an initial value problem u(t = 0) = Uo and that thefunctions f and g are analytic. We discuss the Euler method, the Lie series technique(which is closely related to the Taylor series technique), the Runge-Kutta-Fehlbergfifth order method and symplectic integration. The Verlet method important inmolecular dynamics is also introduced. We also investigate spurious solutions andinvisible chaos. Finally numerical integration of systems of ordinary differentialequations which admit first integrals is discussed.

257

Page 277: Nonlinear Workbook (3rd)

258 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

10.2 Euler Method

The Euler method for solving numerically systems of first order ordinary differentialequations

du^ = g(u,t)

involves computing a discrete set of uk values, for arguments tk, using the differenceequation

Ufc+i = ufc + hg(tk, uk), h := 4+i - tk •

Here h is the step length. By expanding u(t + ft) in a Taylor series with remainderabout t, it can be seen that

u(t + h) - u(t) _ n(t) + ftu'ft) + j/iV'(£) - u(t) , , . 1,h ~ h -uw + 2nu(t)

(t < £ < t + h) so that the left-hand side of the Euler method is an O(h) approxima-tion to the derivative it replaced, provided the second derivative of the solution u isbounded in the time interval of interest. As h goes to zero, the Euler approximationbetter and better represents the original differential equation; that is, it is consistentwith the differential equation. For more details of the error estimates we refer toHairer et al [38].

As an example consider the nonlinear differential equation

du

with the initial condition u(t = 0) — % > 0. This nonlinear differential equationadmits the fixed points u\ = 0, u^ = 1. The fixed point u\ = 0 is unstable, whereasthe fixed point u^ = 1 is stable. The Euler method leads to the difference equation

uk+1 = uk + hf{uk) =uk + huk{\ - uk), k = 0,1, 2 , . . .

where h is the step length. We set h = 0.005 and UQ = 0.1. We find that uk tendsto the stable fixed point u\ = 1 as k —t oo.

/ / Euler.Java

import java.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Euler extends Frame

ipublic Euler()

isetSize(600,500);

Page 278: Nonlinear Workbook (3rd)

10.3. LIE SERIES TECHNIQUE 259

addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); >});

>

public double f(double u) { return u*(1.0 - u); }

public void paint(Graphics g)

{g.drawLine(10,200,410,200); g.drawRect(10,40)400,400);double h = 0.005; // step lengthdouble u = 0.1; // initial valuedouble ul;double t = 0.0;for(int i=0;i<2000;i++){ul = u;int m = (int)(40.0*t+10.0+0.5); int n = (int)(200.0-50.0*ul+0.5);u = ul + h*f(ul);int p = (int) (40.0*(t+h)+10+0.5); int q = (int)(200.0-50.0*u+0.5);g.drawLine(m,n,p,q);t += h;}}

public static void main(String[] args)iFrame f = new EulerO;f.setVisible(true);}

}

10.3 Lie Series TechniqueLet V be the linear differential operator

where the Vi's are functions of the complex variables Zi,z2,..., zn, which are allassumed to be holomorphic in the neighbourhood of one and the same point. If /is a function which is holomorphic in the neighbourhood of the same point, we canapply the operator V to /

V / : = V l W g + V a W ^ + ... + V l l ( , ) g .

Page 279: Nonlinear Workbook (3rd)

260 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

Since the derivatives of a holomorphic function are holomorphic, we again obtaina function which is holomorphic at the same point. This holds for all iteratedoperations. This means that all functions

V2f:=V(Vf), ..., Vnf:=V(Vn-lf),...

are holomorphic at the point under investigation and can be expanded in regularconvergent power series.

Definition. The series

exp(tV)f(z) = £ T i V k t t x ) = / ( z ) + tVf(z) + l-V2f{z) + •••fc=o K- L-

is called a Lie series.

It is formally explained by the symbols for the series written down, for each term iscomposed of a factor tn, n! and a holomorphic function Vnf of the complex variablesZi,... ,zn. We consider t to be a new complex variable, which is independent of thevariables z\,...,zn. This series is not merely of formal significance, it also convergesas a power series in t, and is therefore a holomorphic function of the n + 1 complexvariables zi,...,zn,t. The convergence of the Lie series is proved by the method ofCauchy's majorants.

Theorem. If C is a finite, closed domain of the z-space, in which the differentialoperator V and the function / are holomorphic, a positive number T exists suchthat the Lie series converges absolutely and uniformly for |i| < T throughout theentire domain G, where it represents a holomorphic function of the n + 1 complexvariables z\,... ,zn,t.

The Lie series may be differentiated according to these variables term by term andany number of times in the interior of G and \t\ < T

a / oo fk \ oo fk

s(£B'*'<->)-£iri'Ma / oo j.k \ oo fk a

UZ3 \k=0 K- I k=0K- UZJ

Theorem. For sums and products of Lie series we have

etv(Clh{7.) + c2/2(z)) = Cle*yA(z) + c2etvf2(z)

etv(f1(z)f2(z)) = (etvf1(z))(etvf2(z)).

Corollary. If P(Zi, Z2) denotes a polynomial in Z\, Z2, it holds that

etvP{h{z),h(z)) = P(etvh(z),etv/2(z)).

Page 280: Nonlinear Workbook (3rd)

10.3. LIE SERIES TECHNIQUE 261

Theorem. (Commutation Theorem) If F(z) denotes any function which isholomorphic in a neighbourhood of {zi,... ,zn}, the corresponding power seriesexpansion of which still converges at the point {Zi,..., Zn} (which is certainly thecase for sufficiently small values of t), it is true that

OO J.k

k=o K-

or, written in a different manner

F(etvz) = etvF(z).

Another important result is obtained by the differentiation of the function Zj withrespect to t. Let

Zj := exp(tV)zj, j = l,2,...,n.

Differentiating the Lie series term by term gives

°Z. = etv(Vzj), j = l,2,...,n.

Since VZJ = Vj(z) we findetvVj(z) = V&).

Together, this results in

^ = ^ ( Z ) , j = l,2,...,n.

Theorem. If {zi,..., zn} is a point of the holomorphic domain of V, the func-tions exp(tV)zj in a sufficiently small environment of t = 0 satisfy the system ofdifferential equations

with the initial conditions Zj(t — 0) = Zj. Thus exp(tV)zj are the solutions ofthe system of differential equations which are uniquely determined by these initialconditions.

The uniqueness of the solution can, if uniqueness is to be determined within thedomain of all continuous and continuously differentiable functions, be proved by theknown methods of real analysis.

The Lie series discussed above can be restricted to the real domain. For our numer-ical studies we consider the autonomous system of ordinary differential equations

du-dl = f (U)

where u — (ui,... ,un)T. Let Vj be analytic functions defined on R™. Consider the

vector field

Page 281: Nonlinear Workbook (3rd)

262 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

Then the solution of the initial value problem of the autonomous system for asufficiently small t can be given as a Lie series

Uj(t) = exp(iF)wi|u=u(0)

where j = l,2,...,n. Expanding the exponential function yields

Uj{t) = Uj(0) + ^(M j) | u = u ( 0 ) + ^(K(u,-))|u=u(0) + • • •

where j = l,2,...,n. In most practical cases only a finite number of terms inthis expansion can be taken for the numerical integration of the differential equa-tion. This approach has been used in chapter 3 for dissipative systems. Anotherapproximation is to consider

exp(t(Vl + V2)) = n expicjtVi) exp^iVj) + O(tn+1)3=1

if the vector field V can be written as V = V\ + V2- Up to second order we have

exp(i(K + Va)) = exp(tVi) exp(iV2) + O(t2)

where k = 1 with C\= di — \. Up to third order we have

expftM + V2)) = exp(itVi) exp(t^2) exp^tV,) + O(t3).

These are symplectic integrators (Yoshida [99]).

The study of the singularity structure in the complex time-plane of a dynamicalsystem is of interest for systems with chaotic behaviour. SymbolicC++ with theirComplex class allows the handling of complex numbers. Thus the Lie series techniquecan easily be implemented in SymbolicC++.

// complexlie.cpp

#include <iostream> // for cout#include "mcomplex.h" // for data type complexusing namespace std;

int main(void)

{Complex<double> r(40.0,0.0);Complex<double> s(16.0,0.0);Complex<double> b(4.0,0.0);Complex<double> count(0.0,0.0);Complex<double> eps(0.0,0.01);Complex<double> half(0.5,0.0);

Page 282: Nonlinear Workbook (3rd)

10.4. RUNGE-KUTTA-FEHLBERG TECHNIQUE 263

Complex<double> usl(0.8,0.0);Complex<double> us2(0.8,0.0);Complex<double> us3(0.8,0.0);Complex<double> ul, u2, u3;

while(count.magnitude() < 2.0)

{ul = usl; u2 = us2; u3 = us3;Complex<double> VI = s*(u2-ul);Complex<double> V2 = -u2-ul*u3+r*ul;Complex<double> V3 = ul*u2-b*u3;Complex<double> Wl = s*s*(ul-u2) + s*(-u2-ul*u3+r*ul);Complex<double> W2 = u2+ul*u3-r*ul - s*(u2-ul)*u3

- (ul*u2-b*u3)*ul + r*s*(u2-ul);Complex<double> W3 = s*u2*(u2-ul) + (-u2-ul*u3+r*ul)*ul

-b*(ul*u2-b*u3);usl = ul + eps*Vl + half*eps*eps*Wl;us2 = u2 + eps*V2 + half*eps*eps*W2;us3 = u3 + eps*V3 + half*eps*eps*W3;count += eps;>cout « usl « endl;cout « us2 « endl;cout « us3 « endl;return 0;

}

10.4 Runge-Kutta-Fehlberg Technique

Runge-Kutta methods were developed to avoid the computation of high order deriva-tives which the Taylor method may involve. In place of these derivatives extravalues of the given function g(u, t) are used in a way which essentially duplicatesthe accuracy of a Taylor polynomial. We present the formulas on which a Runge-Kutta-Fehlberg method of order five is based (Hairer et. al. [38]). Suppose that theinitial-value problem

— =g(u , t ) , u(i = 0 ) = u 0

for an autonomous system of ordinary differential equations of first order is to beintegrated, where u(t0) = u0 . A typical integration step approximates u at t =t0 + h, where h is the step length. The formulas are

5

u ( t ) = u o + /i]TcfcgW> t = to + hk=0

Page 283: Nonlinear Workbook (3rd)

264 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

with

g(0) = g(uo), g w = g(u0 + h £ bkjS&).

Thus u(<) approximates the exact solution. The coefficients ck (k = 0 ,1 , . . . , 5) are

<W--£. *] = o, < , - « » ,

The coefficients for &,-*. (j = 0 , 1 , . . . , 5, k — 0 , 1 , . . . , 4) are

6[0][0] = 6[0][l] = 6[0][2] = 6[0] [3] = 6[0][4] = 0

6[l][0] = \, 6[1][1] = 6[1][2] = 6[1][3] = 6[1][4] = 0

M2][0] = | , M2][l] = | , 6[2][2] = 6[2][3] = 6[2][4]=0

1030 _70fin 79QR6[3][0] = 2^7' 6[3][1] = W ' b[m = ' bm = b[3][4]=0

6[4][0] = iff ' ™ = -8' 6w[2] = H ' 6[4][3i = - | S ' 6[4iw=°6[5][0] = - | , 6[5][1] = 2, 6[5][2] = - | | , 6[5][3] = g g , 6[5][4] = - ^ .

For the error estimation we refer to Hairer et al [38].

The method has been used extensively in chapters 3 to 8.

10.5 Ghost Solutions

When we discretize differential equations it can happen that the resulting differenceequation shows chaotic behaviour even if the original differential equations tends toa fixed point. Consider the ordinary differential equation

du- = u ( l - « )

with initial condition u(0) = «o > 0. The fixed points are given by

u* = 0, u* = 1.

The fixed point u* = 1 is asymptotically stable. The exact solution of the differentialequation is given by the formula

r*\ u°etu(t) = -.

1 - u0 4- uoe*

Page 284: Nonlinear Workbook (3rd)

10.5. GHOST SOLUTIONS 265

The exact solution starting from initial value UQ = 0.5 is monotonically increasingand it converges to 1 as t tends to oo. For t = In 9999 « 9.21024 we find that

et QQQ9u(t) = -?— = - ^ - = 0.9999w 1 + e* 10000

so that u(t) is already quite near the asymptotically stable fixed point u* = 1.

In order to integrate this equation by a finite difference scheme, we apply the centraldifference scheme

du un+1 - un-idt 2h

Thus the differential equation takes the form

Un+1 - Un-l „ N

Yh = un(l-un)

with initial conditions

w0 = "o, ui = uo + huo(l - u0).

We obtainy-n+i = Un-i + 2hun(l - un).

Introducing vn = un_i we obtain a system of first order difference equations

Un+1 = Vn + 2hun(l-Un), Vn+1=Un.

In the Java program we compute a numerical solution by the difference equationstarting from initial value w0 = 0.5 and using time-mesh length h = 0.05. Thedifference equation is not stable at the fixed point u* = 1 and we find oscillatingbehaviour. Such a phenomenon is called a ghost solution or a spurious solution.

For 0 < t < 8.5 the numerical solution gives a good approximation of the true solu-tion. The solution un increases monotonically and approaches 1.000. After t = 8.6,the numerical solution is not monotone any more. At t = 9.7, the value un takes avalue slightly greater than 1 for the first time and the solution begins to oscillate.The amplitude of the oscillation grows larger and larger. The growth of this am-plitude is geometric and the rate of growth is such that the amplitude is multipliedby about e = 2.71.. . while t is increased by one, until about t = 17.0, when theoscillation loses its symmetry with respect to w* = 1. The repetition of such cyclesseems to be nearly periodic. The ghost solutions also appear even if h is quite small.One of the reasons for this phenomenon is that the central difference scheme is asecond order difference scheme and that the instability enters at u* = 1 and u* = 0.

The global behaviour of numerical solutions computed by the difference equationis very sensitive to the initial condition, the time-mesh length and the precisionof computation employed. The global behaviour of the numerical solutions also

Page 285: Nonlinear Workbook (3rd)

266 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

depends on the calculators drastically. This phenomenon is caused by roundofferrors. The exact solution un of the central difference scheme takes values quitenear 1, but the numerical solution computed by a digital computer can take onlyvalues with finite digits so that almost all effective digits are lost in the computationwhile un is staying in the vicinity of 1. If we choose a time step sufficiently small sothat the exact solution of the difference scheme approximates the true solution ofthe given time interval 0 < t < T within the prescribed error, the numerical solutionobtained by a computer may not approximate the true solution on the given timeinterval because of roundoff errors if the precision employed is not sufficiently large.The oscillating behaviour is not caused by finite precision or rounding errors.

/ / Ghost.java

import j ava.awt.*;import j ava.awt.event.*;import java.awt.Graphics;

public class Ghost extends Frame

{public Ghost()

{setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});>

public void paint(Graphics g)

idouble ul, vl;double h = 0.05; // step lengthdouble u = 0.5; // initial valuedouble v = u + h*u*(1.0-u); // initial value

double tO = 0.0;

while(tO <= 30.5)

{ul = u; vl = v;u = vl + 2.0*h*ul*(1.0-ul);v = ul;int tb = (int) Math.floor(20*tO + 10);int m = (int) Math.floor(300 - 150*ul);int te = (int) Math.floor(20*(tO+h) + 10);int n = (int) Math.floor(300 - 150*u);

g.drawLine(tb,m,te,n);

Page 286: Nonlinear Workbook (3rd)

10.5. GHOST SOLUTIONS 267

tO += h;}}

public static void main(String[] args)

•C

Frame f = new Ghost();

f.setVisible(true);

}

>

The Lie series technique (C++ program given below) leads to the exact result, i.e.,we do not find oscillating behaviour as in the case of the central difference scheme.Thus with the Lie series technique we find u —> 1 as t —>• oo.

/ / GhostLie.cpp

#include <iostream>using namespace std;

int main(void){

double h = 0.001;double t = 0.0;double u = 0.5; / / i n i t i a l valuedouble u l ;

while(t <= 5.0)•C

t += h;ul = u + h*u*(1.0-u) + h*h*u*(1.0-u)*(1.0-2.0*u)/2.0;cout « "t = " « t « " " « "u = " « ul « endl;u = ul;>return 0;

>

In the C++ program ExGhost.cpp we use the Rational and Verylong classes todo the iteration of the difference scheme. It shows that the oscillating behaviour isnot caused by finite precision and rounding errors.

// Exactghost.cpp

#include <fstream>#include "rational.h"#include "verylong.h"

Page 287: Nonlinear Workbook (3rd)

268 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

using namespace std;

const RationaKVerylong> h("l", "10");const RationaKVerylong> xO("99" ,"100");

int main(void){Rational<Verylong> u, v, ul, twoh, t;RationaKVerylong> zeroC'O"), one("l"), two("2"), fifty("50");ofstream soutCghosts.dat"); // contains the rational number values

// initial values

u = xO; // initial valuev = u + h*u*(one - u); // initial valuet = zero;twoh = two*h;

while(t <= fifty){ul = u; u = v;v = ul + two*u*(one - u);t += h;

sout « t « " " « v « endl;}sout.close();return 0;

}

10.6 Symplectic Integration

The Hamilton's equations of motion in standard or canonical form are given by

dql_dl£ dPi__dHat dpj dt dqj

In the Hamiltonian description of mechanics, the evolution of a system is described interms of 2iV first order differential equations. The 2re variables qi,..., qtf,Pi, • • • ,PNare often referred to as the canonical variables. They define a 2./V-dimensional phasespace. The solution to Hamilton's equations

9j(t) = 9j(qo, Po, t), pj(t) = Pj{q0, Po, t)

where qo = (<7i(0),... ,9AT(0)) and po = (pi(0),... ,PN(0)) are the set of initialconditions, define the state of the system at time t. In its time-evolution, (q(t), p(t))map out a trajectory exploring different regions in the phase-space. Due to thesymmetry of Hamilton's equations, it is natural to consider the qj and pj variables

Page 288: Nonlinear Workbook (3rd)

10.6. SYMPLECTIC INTEGRATION 269

on very much of an equal footing. The N coordinates and n momenta can thus beconsidered as a single set of 2JV coordinates, Zj where

z = {QI,---,QN,PI,---,PN) •

Using this notation, Hamilton's equations can be written in concise form as

% = J2N • vj?(z)at

where V := (d/dzi,..., d/dz2n). The 2n x 2n matrix J2N is called the symplecticmatrix,

T • ( ° MJ™-=\-IN o)

where IN is the N x N unit matrix. They satisfy the incompressibility condition

j t l \dQj dPjJ

This means that the divergence of the Hamilton system is zero. Thus a volume ele-ment in phase-space is preserved under the Hamilton flow. This result is known asLiouville 's theorem, and is one of the fundamental properties of Hamilton dynamicalsystems.

If a dynamical system evolves under a Hamilton flow, a number of important quan-tities are left invariant. The most fundamental of these is a geometric entity thedifferential two-form

N

LJ2:=J2 dpj A dqj .

This means that if we denote the Hamilton phase flow by $(, where <3>t maps theinitial conditions to the solution at time t, we have

($t)*w2 = w2 .

Thus we have the definition:

Definition. A transformation (q, p) -> (Q, P) is called symplectic if it preservesthe 2-form w2.

Any transformation preserving u>2 also preserves the form of Hamilton's equations.The converse is not true. A flow having this property is termed symplectic. Inthe case of one degree of freedom (iV = 1), this simply means preservation of the(oriented) phase-area.

This preservation of the 2-form is a fundamental property of Hamilton systems. Infact, a Hamilton flow can be characterized solely in terms of the 2-form. If a domainD in R2JV is simply connected (i.e., it has no holes), and

^ = f ( q , p ) , ^ = 8 ( q , p )

Page 289: Nonlinear Workbook (3rd)

270 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

is a smooth differential system whose flow preserves the 2-form, then this system ofdifferential equations is a Hamilton system for some Hamilton function H.

The preservation of the 2-form w2 is one of a whole hierarchy of quantities preservedby the Hamiltonian flow first studied by Poincare, which he termed the integralinvariants.

Liouville's Theorem. The Hamilton flow preserves the volume element in phasespace

f N r N

/ I J dp, A dqj = / IJ dPj A dQjJ 3=1 J 3=1

in which the integral sign represents a 2./V-dimensional integration over a prescribedvolume in phase-space.

Another property of conservative Hamilton systems is that H is a constant of mo-tion, i.e., dH/dt = 0. We are particularly interested in numerical discretizations ofHamilton systems, and intuitively it appears to be a good idea to let the discretiza-tion capture as much as possible of the original Hamilton structure. This motivatesa study of transformations preserving the differential two-form. Since a numericaldiscretization of a continuous system cannot be expected to be exact, we cannotpreserve all of the properties of the original flow. For instance, we cannot preservethe original form of the Hamilton function and the area-preserving properties of theoriginal flow - preservation of both these quantities amounts to the exact solutionof the system. Now since preservation of the differential two-form (symplecticity) issuch a fundamental property of Hamilton systems, it seems natural to attempt topreserve this property in the discretized system.

When a continuous flow is discretized, the discrete flow basically becomes a succes-sion of transformations from one time-step to another. Therefore preservation of the2-form can be retained by ensuring that the transformations in the discrete systemare symplectic. Another reason why symplectic transformations are useful, is thatit is often possible to simplify the integration of the equations of motion of a systemby transforming to a different set of coordinates. In the Hamiltonian description ofmechanics, we have two sets of independent variables p and q, which are very muchon an equal footing. Therefore we can transform from one set of phase-space vari-ables (q, p) to a new set (Q,P). This transformation can be symbolically writtenas

Pi = PMi, • • • ,qN,Pu • • • ,PN), Qi = Qi(qi,--.,qN,Pu-.-,PN).

There are various ways of constructing symplectic transformations. The easiest ofthese is the method of generating functions. Consider the one degree of freedomsystem

dq ^dH(p,q) dp _ dH(p,q)dt dp dt dq

Page 290: Nonlinear Workbook (3rd)

10.6. SYMPLECTIC INTEGRATION 271

This system can be discretized with Euler's method, yielding

The discrete system defines a map from (q, p) (the approximate solution at time t)to (Q, P) (the approximate solution at t + r). The Jacobian for this transformationcan be verified to be

d(q,p)

Thus this transformation does not preserve area. Euler's method is therefore notsymplectic. Let us modify the method slightly

P-r-rB-5Ml. o . , + r«fc£>.

aq opIn this case the Jacobian satisfies

d(Q,P) _d(q,p)

The modified method is therefore symplectic.

For a higher dimensional system to be symplectic, the preservation of volume is anecessary but not sufficient property.

Example. Assume a separable planar (n = 1) Hamilton system,

H(i,P) = \p2 + V(q).

Choosing a generating function of the second kind,

F2(q, P, t) = qP + tH(q, P) = qP + l-tP2 + tV(q)

the symplectic map S' : {q,p) —> {Q,P) is obtained as

p=~- = P + tV'(q), Q = ^F = q + tP.

Thus the symplectic scheme is

Pm+i =Pm~ rV'(qm), qm+l = qm + rpm+l .

Consequently

Pm+i =Pm~ TV'(qm), qm+i =qm + r{pm - rV'{qm)).

Page 291: Nonlinear Workbook (3rd)

272 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

Higher order symplectic methods (Yoshida [99]) are constructed as follows. Weassume that the vector field V can be written as V — V\ + V2. If

[Vi,V2] = 0

then for t sufficiently small we have

exp(tV) = exp(tVi + tV2) = exp(tVi) exp{tV2)

where [, ] denotes the commutator. In general, we have

[Vi.vy^o.

The problem is as follows. Let V\ and V2 be two non-commutative vector fields andt be a sufficiently small real number. For a given positive integer n which is calledthe order of integrator, find a set of real numbers ci,c2,... ,Ck and d\, d2, . . . , dk suchthat the difference of the exponential function exp(i(Vi + V2)) and the product ofthe exponential functions

exp(ciiVl) exp(ditV2) exp(c2tVi) exp(d2tV2) • • • exp(cttVi) exp(dktV2)

is of the order tn+1, i.e., the following equality holds

exp(t(VL + V2)) = I I exp(cjty1) exp^tVz) + O(tn+1).3=1

If n = 1, a trivial solution is cx = di = 1 (k = 1), and we have

exp(t(Vi + V2)) = exp(tV0 exp{tV2) + O{t2).

When n = 2, we find that cx = c2 = | , di = 1, d2 = 0 (A; = 2). Thus

exp(t(Vi + V2)) = exp(itVi) exp(il/2) exp(^tn) + O(<3) •

For the construction of higher order symplectic integrators we refer to Yoshida [99].The construction is based on the Baker-Campbell-Hausdorff formula.

The formula is as follows. For any non-commutative operators X and Y, the productof the two exponential functions, exp(X) exp(y), can be expressed in the form of asingle exponential function as

exp(X) exp(y) = exp(Z)

whereZ = X + Y + -[X,Y] + -([X,X,Y] + \Y,Y,X]) + ... .

Here [, ] denotes the commutator, and higher order commutators like

[X,X,Y}:=[X,[X,Y}}.

Page 292: Nonlinear Workbook (3rd)

10.7. VERLETMETHOD 273

The feature of the Baker-Campbell-Hausdorff formula is that only commutators ofX and Y appear except for the linear terms in the series. Symplectic integratorshave been applied in chapter 8.

Another technique which can be used for integration is the Trotter formula. Let Abe the generator of a contractive Co - semigroup exp(M)(>0 on a Banach space E,and let B € C(E) be a linear dissipative operator, where C(E) denotes the vectorspace of all linear bounded maps E —» E. Then A + B generates a Co - semigroupwhich is given by the Trotter's formula

exp(t(A + B)) = lim fexp (-A) exp (-B)Yn->oo v \n J \n J J

where the limit is taken in the strong operator topology.

10.7 Verlet Method

The Verlet algorithm (Verlet [94]) is a method for integrating second order ordinarydifferential equations

g = P(x(t),t).The Verlet algorithm is used in molecular dynamics simulations. It has a fixed timediscretization interval h and it needs only one evaluation of the force F per step.The algorithm is derived by adding the Taylor expansions for the coordinates x att = ±h about 0

x(ft) = x(0) + h*f- + £F(X(0). 0) + £££!) + 0(h<)

x(-fc) = x(0) - * * f + £F(X(0), 0) 4 ^ + O(M)

leading tox(/i) = 2x(0) - x(-/i) + /i2F(x(0), 0) + 0{h4).

Knowing the values of x at time 0 and —h, this algorithm predicts the value of x(/i).Thus we need the last two values of x to produce the next one. If we only have theinitial position x(0) and initial velocity v(0) at our disposal, we approximate x(ft)by

x(/0«x(0) + ftv(0) + yF(x(0),0)

i.e., we setx(0)-x(-/i)

hExample. Consider the one-dimensional harmonic oscillator

Page 293: Nonlinear Workbook (3rd)

274 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

where fi is a constant frequency. We find the approximation

x(t + h) = 2x(t) - x(t - h ) - h2Q2x(t).

The analytic solution to this difference equation can be written in the form

x(t) = exp(iwt)

with UJ satisfying the condition

2 - 2 cos(wft) = h2n2 .

If ft2f22 = {Ml)2 > 4, the frequency w becomes imaginary, and the analytical solutionbecomes unstable. Thus it is useful to introduce a dimensionless time T via

x{r{t)) = x{i), r{t) = nt.

Then the linear differential equation for the harmonic oscillator takes the form

d2x _dr2

In the C++ program we give an implementation of the Verlet method for the one-dimensional pendulum d?9/dt2 = —(g/L) sin(0).

/ / Verlet.cpp

#include <iostream>#include <cmath>using namespace std;

int main(void){

const double pi = 3.141592654;double theta =1.4; // initial angle (in radians)double omega =0.0; // initial velocitydouble g_over_L = 1.0; // the constant g/Ldouble time =0.0; // initial timedouble time_old; // time of previous reversaldouble tau = 0.005; // time step size

// take one backward step to start Verletdouble accel = -g_over_L*sin(theta); // gravitational acceleration

double theta_old = theta - omega*tau + 0.5*tau*tau*accel;

int nStep = 2000; // number of steps

for(int i=l;i<=nStep;i++)

Page 294: Nonlinear Workbook (3rd)

10.8. STORMER METHOD 275

{time += tau;accel = - g_over_L*sin(theta);double theta_new = 2.0*theta - theta_old + tau*tau*accel;cout « "time = " « time « " "

« "theta = " « theta_new « " "« "accel = " « accel;

cout « endl;theta_old = theta;theta = theta_new;

}

return 0;

}

10.8 Stormer Method

We consider the system of second-order ordinary differential equations

m - = F(x)

where x is the collective position vector, m is a diagonal matrix of masses, and Fis the collective force vector. The discretization known as the second-order Stormermethod is given by

~m{Xn+1 - 2Xn + X"-1) = F(Xn)

where At is the timestep, and Xn denotes the difference approximation to x at timenAt. This method can be used as an integrator for molecular dynamics togetherwith the formula

for calculating the velocity v = cix/dt. This combination is equivalent to the leapfrogmethod, defined as

Vn+l/2 = yn-l/2 + A^T^X")

Xn+1 =Xn + Atyn+l/2 _

The implicit discretization scheme with a different right-hand side

^ m ( X " + 1 - 2X" + X - 1 ) = FCX""1) + | F ( X » ) + ^F (X" + 1 )

is often called Cornell's method. Another method which can be used for integratingthe differential equation is

- ^ m ( X n + 1 - 2Xn + X""1) = F (-(X"-1 + Xn + 1)) .

Page 295: Nonlinear Workbook (3rd)

276 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

10.9 Invisible ChaosDiscretization of nonlinear differential equations can lead to maps which show chaoticbehaviour. We consider the anharmonic system

d2u ,

Using a finite difference scheme this differential equation can be written asUn+i - 2un + Mn_i _ _ 3

(A*)2 " Un

to obtain approximate solutions of the differential equation. For a fixed time stepAt, At > 0, this scheme is equivalent to the scheme

Un+l - 2Un + Un-X = -Ul

by the replacement un — Un/At. We define Vn := Z7n_i. Consequently we obtainthe map

t/n+i = -Vn + 2Un - Ul Vn+l = Un.

This map is invertible and area-preserving. Numerical solutions of the differencescheme for small At correspond to orbits near the origin (0,0) of the map via thetransformation un = Un/At. Numerical experiments show that there are invariantcircles of the map around the origin (0,0).

// Invisible.java

import j ava.awt.*;import Java.awt.event.*;import Java.awt.Graphics;

public class Invisible extends Frame

{public Invisible()

{

setSize(600,500);addWindowListener(new WindowAdapterO{ public void windowClosing(WindowEvent event){ System.exit(0); }});}

public void paint(Graphics g)

{double ul, vl;double u=0.9, v=0.75; // initial valuesint T = 3000; // number of iterations

Page 296: Nonlinear Workbook (3rd)

10.10. FIRST INTEGRALS AND NUMERICAL INTEGRATION 277

for(int t=0;t<T;t++) {

ul = u; vl = v;u = -vl + 2.0*ul - ul*ul*ul; v = ul;int mx = (int) Math.floor(100*u + 300 + 0.5);int ny = (int) Math.floor(100*v + 220 + 0.5);g.drawLine(mx,ny,mx,ny);

>

>

public static void main(String[] args)

{

Frame f = new InvisibleO;f.setVisible(true);>

}

10.10 First Integrals and Numerical Integration

Consider the autonomous system of first order ordinary differential equations

dv. . .Yt = f (U)

where the vector field f : Rn -» Rn is analytic. Some of these systems admit a firstintegral, i.e., there exists a scalar function 7 such that

dt

Thus

•=l uuj ui i=l utij

Preserving first integrals in numerical integration is important because of their phys-ical relevance, e.g. in mechanics and astronomy, but also because they can ensurelong-term stabilising effects. Thus we want to find a discrete approximation to thesystem of differential equations

u' - u ,r = g(u, u', T)

(u = u(nr), u' = u((n + l)r)) such that the first integral is preserved exactly, i.e.Z(u') = /(u). For example the implicit midpoint rule

U' - U t(u' + ll\

preserves quadratic integrals.

Page 297: Nonlinear Workbook (3rd)

278 CHAPTER 10. SOLVING DIFFERENTIAL EQUATIONS

McLaren and Quispel ([68]) provided a more general case. The system of ordinary

differential equations can be written in the form

where S is a skew-symmetric n x n matrix over R, i.e. ST = —S and V denotes thegradient. An integral-preserving discrete version of this is

^ ^ = 1S(U,U',T)VJ(U,U')T

where u, u' denote un respectively un+i, and where S is a skew symmetric matrixsatisfying (for consistency)

5(u,u',T) = 5 ( u ) + 0 ( r ) .

The general discrete gradient V7 is defined as

(u' - u) • V7(u', u) := 7(u') - /(u)

and may be expanded in the form

V/(u, u') = VI + B(u)(u' - u) + (u' - u)rM(u)(u' - u) + O(||u' - u||3).

This leads to the conditions

d2l 1 d3lBij + Bji = , Mijk + Miki + Mkij = - .

auiduj 2 ouidujduk

The order of accuracy of an integral-preserving integrator based on the discretizationgiven above is determined by S and by the choice of the discrete gradient V/(u, u'),i.e. by S and the matrix B, the tensor M, and the higher order parts of V7.

Page 298: Nonlinear Workbook (3rd)

Chapter 11

Neural Networks

11.1 Introduction

Neural networks are models of the brain's cognitive process. The brain has a mul-tiprocessor architecture that is highly interconnected. Neural networks have anincredible potential to advance the types of problems that are being solved by com-puters. The neuron is the basic processor in neural networks. Each neuron has oneoutput, which is generally related to the state of the neuron -its activation - andwhich may fan out to several other neurons. Each neuron receives several inputs overthese connections, called synapses. The inputs are the activations of the incomingneurons multiplied by the weights of the synapses. The activation of the neuron iscomputed by applying a threshold function to this product. This threshold functionis generally some form of nonlinear function.

The basic artificial neuron (Fausett [30], Haykin [44], Cichocki and Unbehauen [17],Hassoun [43], Rojas [77]) can be modelled as a multi-input nonlinear device withweighted interconnections w^, also called synaptic weights or strengths. The cellbody (soma) is represented by a nonlinear limiting or threshold function / . Thesimplest model of an artificial neuron sums the n weighted inputs and passes theresult through a nonlinearity according to the equation

( " \

\i=l J

where / is a threshold function, also called an activation function, 6j (9j £ R) isthe external threshold, also called an offset or bias, Wji are the synaptic weightsor strengths, xt are the inputs (i = 1,2,... ,n), n is the number of inputs and ?/,-represents the output. The activation function is also called the nonlinear transfercharacteristic or the squashing function. The activation function / is a monotoni-cally increasing function.

279

Page 299: Nonlinear Workbook (3rd)

280 CHAPTER 11. NEURAL NETWORKS

A threshold value 6j may be introduced by employing an additional input x0 equalto +1 and the corresponding weight u>j0 equal to minus the threshold value. Thuswe can write

Vi• = f Z ! waxi\i=0 /

wherewjo = -0j, x0 = 1.

The basic artificial neuron is characterized by its nonlinearity and the threshold6j. The McCulloch-Pitts model of the neuron used only the binary (hard-limiting)function (step function or Heaviside function), i.e.,

„ , . / 1 if x > 0H^:=\0iix<0.

In this model a weighted sum of all inputs is compared with a threshold 0j. If thissum exceeds the threshold, the neuron output is set to 1, otherwise to 0. For bipolarrepresentation we can use the sign function

( 1 if x > 0sign(x) := < 0 if x = 0

[—lifa; < 0.

The threshold (step) function may be replaced by a more general nonlinear functionand consequently the output of the neuron yj can either assume a value of a discreteset (e.g. {—1,1}) or vary continuously (e.g. between —1 and 1 or generally between1/min and ymax > ymin) • The activation level or the state of the neuron is measuredby the output signal yj, e.g. yj = 1 if the neuron is firing (active) and yj = 0 if theneuron is quiescent in the unipolar case and j/j = — 1 for the bipolar case.

In the basic neural model the output signal is usually determined by a monotonicallyincreasing sigmoid function of a weighted sum of the input signals. Such a sigmoidfunction can be described for example as

1 _ e-2AujVi = tanh(Auj) = 1 + &_2XUj

for a symmetrical (bipolar) representation. For an unsymmetrical unipolar repre-sentation we have

1yi~ 1+ e-A«y

where A is a positive constant or variable which controls the steepness (slope) of thesigmoidal function. The quantity Uj is given by

n

Uj := Y, WjiXi.i=0

Page 300: Nonlinear Workbook (3rd)

11.1. INTRODUCTION 281

The function

satisfies the nonlinear differential equation

£-V,(i-/Jand the function

g\(x) = tanh(Az), A > 0

satisfies the nonlinear differential equation

£ = *-•»This is important for the backpropagation algorithm.

A model of the above sigmoid function can be built using traditional electronic cir-cuit components (Cichocki and Unbehauen [17]). A voltage amplifier simulates thecell body (soma), the wires replace the input structure (dendrites) and output struc-ture (axon) and the variable resistors model the synaptic weights (synapses). Theamplifier output voltage ?/, replaces the variable pulse rate of a real neuron. Thesigmoid activation function is naturally provided by the saturating characteristic ofthe amplifier. The input voltage signals Xi supply current into the wire-dendritesin proportion to the sum of the products of the input voltages and the appropri-ate conductances. By applying Kirchhoff's Current Law at the input node of theamplifier we obtain the expression

n

^GjiXi

Vj = f(uj), Uj := ^

HGfi

where %i (i = 0,1,2, . . . , n) are the input voltages, Uj denotes the input voltage ofthe j-th amplifier, y^ means the output voltage of the j-th neuron, / is the sigmoidactivation function of the amplifier, Gj, = Rj^ is the conductance of the resistorconnecting the amplifier i with the j'-th amplifier and Iji is the current flowingthrough the resistor Rji (from neuron i to neuron j). The above equation can bewritten in the form

/ n \ Q

Vj = f[Y. WHXi . WJi = n ^ •

i=0

Page 301: Nonlinear Workbook (3rd)

282 CHAPTER 11. NEURAL NETWORKS

II threshold.cpp

#include <iostream>#include <cmath> // for exp, tanhusing namespace std;

int H(double* w,double* x.int n)

idouble sum = 0.0;for(int i=0;i<=n;i++) { sum += w[i]*x[i]; >if(sum >= 0.0) return 1;

else return 0;

}

int sign(double* w,double* x.int n){double sum = 0.0;for(int i=0;i<=n;i++) { sum += w[i]*x[i]; >if(sum >= 0.0) return 1;

else return 0;

}

double unipolar(double* w,double* x.int n)

{double lambda = 1.0;double sum = 0.0;for( in t i=0;i<=n;i++) { sum += w[i]*x[i] ; >return 1.0/(1.0 + exp(-lambda*sum));

}

double bipolar(double* w,double* x.int n)

{double lambda = 1.0;double sum = 0.0;for(int i=0;i<=n;i++) { sum += w[i]*x[i]; >return tanh(lambda*sum);

}

int main(void)

{int n = 5; // length of input vector includes biasdouble theta =0.5; // threshold// memory allocation for weight vector wdouble* w = new double[n];w[0] = -theta;

Page 302: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 283

w[l] = 0 .7 ; w[2] = - 1 . 1 ; w[3] = 4 . 5 ; w[4] = 1.5;

// memory allocation for input vector xdouble* x = new double[n];x[0] = 1.0; // biasx[l] = 0.7; x[2] = 1.2; x[3] = 1.5; x[4] = -4.5;

int rl = H(w,x,n);cout « "rl = " « rl « endl;int r2 = sign(w,x,n);cout « "r2 = " « r2 « endl;double r3 = unipolar(w,x,n);cout « "r3 = " « r3 « endl;double r4 = bipolar(w,x,n);cout « "r4 = " « r4 « endl;delete [] w; delete [] x;return 0;

>

11.2 Hopfield Model

11.2.1 IntroductionThe problem is formulated as follows. Store a set of p patterns

Xfc, * = 0 , l , 2 , . . . , p - l

in such a way that when presented with a new pattern s, the network responds byproducing whichever one of the stored patterns most closely resembles s. A binaryHopfield net can be used to determine whether an input vector (pattern) is a knownvector (pattern) (i.e., one that was stored in the net) or an unknown vector (pat-tern). The net recognizes a known vector by producing a pattern of activation onthe units of the net that is the same as a vector stored in the net. It also can hap-pen that an input vector converges to an activation vector that is none of the storedpatterns. Such a pattern is called a spurious stable state. The Hopfield network isa recurrent network that embodies a profound physical principle, namely, that ofstoring information in a dynamically stable configuration. Hopfield's idea was oflocating each pattern to be stored at the bottom of a "valley" of an energy land-scape, and then permitting a dynamical procedure to minimize the energy of thenetwork in such a way that the valley becomes a basin of attraction. The standarddiscrete-time version of the Hopfield network uses the McCulloch-Pitts model forthe neurons. Retrieval of information stored in the network is accomplished via adynamical procedure of updating the state of a neuron selected from among thosethat want to change, with that particular neuron being picked randomly and oneat the time. This asynchronous dynamical procedure is repeated until there are no

Page 303: Nonlinear Workbook (3rd)

284 CHAPTER 11. NEURAL NETWORKS

further state changes to report. In a more elaborate version of the Hopfield net-work, the firing mechanism of the neurons (i.e. switching them on or off) followsa probabilistic law. In such a situation, we refer to the neurons as stochastic neurons.

For the recurrent network with symmetric coupling we may define an energy func-tion (also called a Liapunov function). When the network is started in any initialstate, it will move in a downhill direction of the energy function E until it reachesa local minimum; at that point, it stops changing with time. A recurrent net-work with symmetric coupling cannot oscillate despite the abundant presence offeedback. We refer to the space of all possible states of the network as the phasespace, a terminology borrowed from physics; it is also referred to as the state space.The local minima of the energy function E represent the stable points of the phasespace. These points are also referred to as attractors in the sense that each attrac-tor exercises a substantial domain of influence (i.e. basin of attraction) around it.Accordingly, symmetric recurrent networks are sometimes referred to as attractorneural networks. The Hopfield network may be viewed as a nonlinear associativememory or content-addressable memory, the primary function of which is to retrievea pattern (item) stored in memory in response to the presentation of an incompleteor noisy version of that pattern. The essence of a content-addressable memory is tomap a fundamental memory x^ onto a stable fixed point of a dynamical system. Thestable fixed points of the phase space of the network are the fundamental memoriesor prototypes of the network. We may then represent their particular pattern as astarting point in the phase space. In principle, provided that the starting point isclose to the stable point representing the memory being retrieved, the system shouldevolve with time and finally converge onto the memory state itself. We may there-fore describe a Hopfield network as a dynamic system whose phase space containsa set of stable fixed points representing the fundamental memories of the system.Consequently the Hopfield network has an emergent property which helps it retrieveinformation and cope with errors.

The problem could also be solved by storing the patterns and then calculating theHamming distance between the test pattern s and each of the stored patterns x^.The Hamming distance between two sequences of binary numbers of the same lengthis the number of bits that are different in the two numbers.

In the following we have vectors with elements 1 or — 1. Then we use the followingdefinition for the Hamming distance.

Definition. Let x and y be two vectors of the same length N with xt, j/* G { 1 , - 1 } .Then the Hamming distance is defined as

• N - l

d ( x , y ) : = - £ \xi-Vi-1 i=o

Example. Consider x = (1, - 1 , 1 , l)T, y = (-1,1,1, -1 ) T . Then rf(x, y) = 3.

Page 304: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 285

Our configuration space consists of N cells. Every cell can take two values (in ourcase +1 and —1). Then the number of configurations (or the number of all possiblestates of the network) is

2N.

Within this space the stored pattern Xf. (k = 0,1,2,... ,p — 1) are attractors. Thedynamics of the network maps starting points (initial configurations) into one of theattractors. The whole configuration space is thus divided up into basins of attractionfor the different attractors.

/ / Hamming.cpp

#include <iostream>using namespace std;

int distance(int* x.int* y.int n){

int d = 0;for(int i=0;i<n;i++){if(x[i] != y[i]) d++;}return d;

>

int main(void)•C

int n = 4;int* x = new int [n] ;x[0] = 1; x[l] = - 1 ; x[2] = 1; x[3] = 1;

int* y = new int[n];

y[0] = -1; y[l] = 1; y[2] = 1; y[3] = -1;

int result = distance(x,y,n);cout « "result = " « result « endl;

delete [] x; delete [] y;return 0;

}

11.2.2 Synchronous OperationsThe binary Hopfield model consists of N neurons

s0, su ... , s w _ i

Page 305: Nonlinear Workbook (3rd)

286 CHAPTER 11. NEURAL NETWORKS

where

8i = ±l, i = O,l,...,N-l.

If S{ = 1 we say that the neuron at the site i is active (the neuron fires). If s, = - 1we say that the neuron at site i is inactive (i.e. the neuron does not fire). Theneuron at site i is connected with the neuron at site j with i = 0 , 1 , . . . , N — 1 andj = 0,l,...,N —1. In the binary Hopfield model we first impose p patterns, p > 1,of neuron states. We denote these patterns by

Xo := (zo,o,Zo,i>- • • , ^ O , J V - I ) T , • • •, x p _ ! := ( i p _ l i 0 , a ; p _ 1 ) 1 , . . . , xv-itN-i)T

wherexk:l = ±l, k = 0,l,...,p-l, i = 0 , l , . . . , N - l .

Next we assign connection weights Wij (W is an N x N symmetric matrix)

(P-I

yVij :— < k=0

[ 0 i = j,Q<i,j<N~l

where W^ is the connection weight between node i and node j and i = 0 , 1 , . . . , N—l,j = 0 , 1 , . . . , N — 1. Thus the diagonal elements of the matrix W are equal to zero.

The neurons are updated according to the rule

Si(t + 1) = siga(hi(t)), t = 0,1,2, . . . ,

where the sign function is defined by

. , . f 1 if z > 0*&&••= \-liiz<0

with the convention that S;(£ +1) = Si(t) if /i,(i) = 0. The local fields ht are definedas

J V - l

/*(<):= E ^ W * ) , t = 0,1,2,...j=0

Thus the dynamics of the neurons Sj is governed by

/JV-l \

»i(t + 1) = sign I J^ W^t) J , i = 0,1,2,. . .

where i = 0 , 1 , . . . , iV — 1 with the convention described above that

ai(t+i)=8i(t) if f:wijSj(t)=o.3=0

Page 306: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 287

Consequently the dynamics of the neuron is a system of nonlinear difference equa-tions. Thus all components of the state vector s are updated simultaneously. Thisis the synchronous operation. In compact form the dynamics can be written as

s(* + 1) = sign(Ws(t))

where the sign function is applied to each component of its argument. This opera-tion mode is thus similar to a Jacobi iteration for the solution of linear equations.

Definition. Consider a map / : S —> S, where S is a non-empty set. A fixed pointx* £ S is defined as a solution of x* = f(x*).

Thus for the given case the fixed points of the system of nonlinear difference equa-tions are the solutions of the nonlinear equation

s* = s ign(W).

They are the time-independent solutions, i.e.,

a'{t + l) = B*{t).

Thus the Hop field algorithm is as follows.

1) Initialize the input patterns xo, • •., xp_i. This is the teaching stage of the algo-rithm.

2) Calculate the connection weights W{j from the input pattern, i.e. the matrix W.

3) Give the start configuration s(t = 0), where

s(t = 0) = (so{t = 0), ai(* = 0) , . . . . sw_i(t = 0))T.

4) Iterate

Si{t + 1) = sign f £ WijSjit) 1 , t = 0,1, 2 , . . .

until convergence. The net is allowed to iterate in discrete time steps, until it reachesa stable configuration. This means the output pattern remains unchanged. The netthus converges to a solution.

Hopfield found experimentally that the number of binary patterns p that can bestored and recalled in a net with reasonable accuracy, is given approximately by

p « 0.157V

where N is the number of neurons in the net. Another estimate isN

P~ 21og2iV

For N — 40 Hopfield's estimation is p = 6.

Page 307: Nonlinear Workbook (3rd)

288 CHAPTER 11. NEURAL NETWORKS

11.2.3 Energy Function

Hopfield proved that the discrete Hopfield net will converge to a stable limit point(pattern of activation of the units) by considering an energy function for the system.The energy function is also called a Liapunov function. An energy function is afunction that is bounded below and is a nonincreasing function of the state of thesystem. For the Hopfield network the energy function E is

E(B(t)) := -IZEWiMQsjit) = -\sT(t)Ws{t).Z i=0 j=0 Z

Note that Wtj = Wjt (i.e. W is symmetric) and Wti — 0 for i = 0 , 1 , . . . , N - 1.The factor 1/2 is used because the identical terms WijSi(t)sj(t) and WjiSj(t)st(t)are presented in the double sum. An unknown input pattern represents a particularpoint in the energy landscape. As the network iterates its way to a solution, the pointmoves through the landscape towards one of the hollows. These basins of attractionrepresent the stable states of the network. The solution from the net occurs whenthe point moves into the lowest region of the basin; from there, everywhere else inthe close vicinity is uphill, and so it will stay where it is. This is directly analogousto the three-dimensional case where a ball placed on a landscape of valleys andhillsides will move down towards the nearest hollow, setting into a stable state thatdoes not alter with time when it reaches the bottom. With the assumption givenabove we can show that

E{s{t + 1))-E(s(t)) <0.

This can be seen as follows. First we note that the energy does not depend upon cellnumbering, because Wij = Wji. Now if cell k is chosen to reevaluate its activation,it will keep the same value (and therefore leave the energy unchanged) unless

N-l

1. sk(t)--l and ^WkjSj{t)>0. Then sk{t + 1) becomes + 1

N-l

2. sk(t) = +1 and £ WkjSj{t) < 0. Then sk(t + 1) becomes - 1j=o

In either case the energy changes by

AE := E(s(t + 1)) - E(s(t)) = -(sk(t + 1) - sk(t)) J2 Wk,jSj(t).

ConsequentlyJ V - l

AE = -2sk(t + 1) £ WkjSj{t) < 0.

Notice that we used the fact that the weights are symmetrical (Witj = Wjj) and thatWfc,*: = 0 for k = 0 ,1 , . . •, N — 1. Moreover we used the fact that Sj(t + 1) — Sj(t)

Page 308: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 289

for j 7 k. Thus energy in a Hopfield model can never increase, and whenever a cellchanges activation to —1 the energy strictly decreases. We also have the identity

E(s(t + l))-E(s(t)) =

-(sT(t + 1) - sT(t))Ws(t) - ^(sT(i + 1) - ST(t))W(s(t + 1) - s(t))

where we usedsT{t + l)Ws{t) = sT{t)Ws(t + 1).

Each component in Ws(t) has the same sign as the corresponding component ins(t + 1). Thus, if s(i+ 1) / s(t), the first term gives a strictly negative contributionand this effect is possibly enhanced by the second term.

11.2.4 Basins and Radii of AttractionIn order to measure the dissimilarity of two vectors x and y with

Xj, 1/,-6 { 1 , - 1 }

we introduced the Hamming distance. We define by Nr(y) (Kamp [51]) the neigh-bourhood of y with radius r as the set of vectors x located at most at distance rfrom y. Thus

J V r ( y ) : = { x € { - l , l } n : d(x,y)<r}.

The basin of direct attraction Bi(s*) of a fixed point s* is the largest neighbourhoodof s* such that any vector in the neighbourhood is attracted by the fixed point in asingle iteration

Bi(s*) := maxr{ Nr{s*) : s(0) € NT(s*) -> s(l) = s* }.

One should distinguish between the basin of direct attraction as defined above andthe domain of direct attraction Di(s*) which is the set of all points in { — 1,1 } N

which are attracted by s* in a single iteration

Z? 1 (s*) :={s(0)G{- l , l} i v : s ( l ) = s * } .

The domain of attraction can include points which lie outside the basin Bi(s*) andconsequently Bi(s') C A(s*). The basin of direct attraction is independent of theparticular operation mode, be it synchronous, asynchronous or block-sequential. Weare also interested in examining the dynamics by which a vector is attracted afterseveral iterations. One defines the basin of attraction of order k, denoted B/t(s*), asthe largest neighbourhood of s* such that any vector in this neighbourhood convergesto s* in k iterations at most

Bk{s*) := max, { Nr{s*) : s(0) £ NT(s*) ->• s(A;) = s* } .

The basin of long-term attraction B(s*) is defined as the largest neighbourhood ofa fixed point in which attraction take place in a finite number of iterations

B(s*) := max,. { Nr(s*) : s(0) € iVr(s*) ->• 3 k such that s(<) = s* if t > k } .

Page 309: Nonlinear Workbook (3rd)

290 CHAPTER 11. NEURAL NETWORKS

The radius of attraction of order k, Rk{s*) and the radius of long-term attraction,R(s*) of a fixed point s* are the radii of the neighbourhood Bk(s*) and B(s*),respectively.

11.2.5 Spurious Attractors

When a Hopfield network is used to store p patterns by means of the Hebb pre-scription for the synaptic weights, the network is usually found to have spuriousattractors, also referred to as spurious states. Spurious states represent stable statesof the network that are different from the fundamental memories of the network.How do spurious states arise ?

1) Firstly we note that the energy function

E(s) = ~sTWs

is symmetric in the sense that its value remains unchanged if the states of the neu-rons are reversed (i.e., the state S; is replaced by — s, for all i) since W is a symmetricmatrix and Wu = 0. Accordingly, if the fundamental memory xM corresponds to aparticular local minimum of the energy landscape, that same local minimum alsocorresponds to — x^. This sign reversal need not pose a problem in the retrievalof stored information if it is agreed to reverse all the remaining bits of a retrievalpattern should it be found that a particular bit designated as the "sign" bit is —1instead of +1.

2) Secondly, there is an attractor for every mixture of the stored patterns. A mix-ture state corresponds to a linear combination of an odd number of patterns.

3) Thirdly, for a large number p of fundamental memories, the energy landscapehas local minima that are not correlated with any of these memories embedded inthe network. Such spurious states are sometimes referred to as spin-glass states, byanalogy with spin-glass models in statistical mechanics. If we were to set Wu / 0for all i, additional stable spurious states might be produced in the neighbourhoodof a desired attractor.

11.2.6 Hebb's Law

Based on Hebb's law, the most widely accepted hypothesis explaining the learningmechanism achieved by associative memories is that some functional modificationtakes place in the synaptic links between the neurons. In particular, it is assumedthat correlated neuron activities increase the strength of the synaptic link. Usually,this hypothesis of synaptic plasticity is quantitatively expressed by stating thatthe synaptic weight Wij should increase whenever neurons i and j have simulta-neously the same activity level and that it should decrease in the opposite case.

Page 310: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 291

Consequently, in order to store a prototype x according to Hebb's hypothesis, thesynaptic weight should be modified by an amount

AWij = AWji = rjXiXj

where rj is a positive learning factor. For the complete synaptic matrix the modifi-cation will thus be

AW = TjxxT.

Notice that x is a column vector with N components and therefore x r is a rowvector with N components. Thus W is an N x N matrix. If p prototype vectorsx0, Xi, . . . , xp_i have to be stored, one considers that the resulting synaptic matrixis given by Hebb's law

Since the sign function applies to the vector Ws the factor \/p can also be omitted.Furthermore it is assumed that Wa = 0 for i = 0,1,. . . ,N— 1. With this assumptionwe have

w = Y,xkxT

k-PiNk=0

where IN is the N x N unit matrix. When the prototypes (stored pattern) areorthogonal, i.e.

x£x( = 0 if kj^l

then the stored patterns are fixed points of the nonlinear map since

Wxk = (N-p)xk, k = 0,l,2,...,p-l.

However, if the prototypes are not orthogonal, the correlations between them mayprevent exact retrieval and it is to be expected that the number of vectors which canbe stored will be reduced if the Hamming distance between these vectors becomessmaller. The problem of correlated prototypes is unavoidable since, for most appli-cations the selection of these prototypes is not free but imposed by the patterns tobe stored.

Since Hebb's rule does not always provide perfect retrieval, a modification has beenproposed which allows us to give variable weightings to the prototypes in order toreinforce those which are more difficult to memorize. The weighted Hebbian rule isdefined by

P-I p-i

W = "£\kXk*l, X k < l and £ A * = 1-k=0 k=0

The construction of W is quite similar to the spectral theorem in matrix theory. LetA be an N x N symmetric matrix over the real numbers R. Then the eigenvaluesAo, Ai, . . . , A;v_i of A are real. Assume that the eigenvalues are pairwise different.

Page 311: Nonlinear Workbook (3rd)

292 CHAPTER 11. NEURAL NETWORKS

Then the corresponding normalized eigenvectors x0, x1; ..., xN^x are pairwise or-thonormal. We consider the eigenvectors as column vectors. Then the symmetricmatrix A can be reconstructed as

N-l

As an example consider the 2 x 2 matrix

The eigenvalues are given by Ao = 1, Ai = —1 with the corresponding normalizedeigenvectors

Hence A = x o x j — Xixf.

11.2.7 Example

As an example we consider the case with N = 5 neurons. The input vectors (pat-terns) to be stored are

/ 1 \ / -1 \1 1

x0 = 1 , xi = - 1 .1 1

\ - l / V 1 /We note that

X^Xj = - 1

i.e., the two input patterns are not orthogonal. Obviously, if the length of thevectors is an odd number the vectors cannot be orthogonal. For the entries of theconnection matrix W we have

W = X0XQ + xixf - pi

where p = 2 (number of pattern stored) and I is the 5 x 5 unit matrix. The term—pi is necessary so that the diagonal elements Wjj cancel out. Thus the symmetricconnection matrix W is

/ 0 0 2 0 - 2 \0 0 0 2 0

W= 2 0 0 0 - 2 .0 2 0 0 0

V-2 0 -2 0 0 /

Page 312: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 293

Next we show that the two stored patterns are fixed points, i.e., we have to provethat

s* = sign(WV).

For the first stored pattern Xo we have

/ 4 \ / 1 \2 1

sign(Tyxo) = sign 4 = 1 = x0.2 1

1-4/ V-l/Analogously we prove that xi is a fixed point, i.e.

/-4\ / - 1 \2 1

sign(VKx!) = sign —4 = —1 = Xi.2 1

V 4 / V 1 /

Next we find the energy values from the energy function. From

E = -]-sTWs

we obtain for the first stored pattern Xo the energy E = —8. Analogously, for thesecond stored pattern Xi we find E = — 8. Recall that there are 25 = 32 configura-tions.

Next we look at the time evolution of the initial state

/ 1 \1

8(0)= 1 .1

\ 1 /

We find/ 0 \ / 1 \

2 1VKs(O) = 0 , s(l) = sign(Ws(0)) = 1

2 11-4/ 1-1/

where we used the rule that sign(O) is assigned to the corresponding value in thevector s(0). We find that s(l) is the stored pattern x0. This means we have reacheda stable fixed point. The energy value of the initial state s(0) is E = 0. E = — 8 isthe lowest energy state the system has.

Page 313: Nonlinear Workbook (3rd)

294 CHAPTER 11. NEURAL NETWORKS

Problem. Find the energy value for the other configurations. Recall that there are25 = 32 configurations. Study what happens if the initial state is given by

- 1s(0) = 1 .

1\ 1 /

Find the energy value.

11.2.8 ProgramIn our C++ program we consider synchronous operations. We consider the threepatterns given by the numbers 1, 2 and 4 in the figure. A black box indicates +1and a white box —1. Thus in our example we have N = 40 = 8 • 5 neurons, i.e., wehave 8 rows and 5 columns in our grid.

Figure 11.1 Input Patterns for C++ Program

We consider the three patterns giving the numbers 1, 2 and 4. For the number 1 wehave the pattern

xo = ( - 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, -1, -If.

For the number 2 we have the pattern

xx = ( - l , 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, -If.

For the number 4 we have the pattern

x2 = (l , - 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,-If.

Page 314: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 295

Problem. Calculate the scalar products x^xi, x^x2 and xfx2- Are the patternsorthogonal to each other? Are the patterns fixed points of the map W ?

When we consider the initial configuration

s(* = 0) = (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 , - 1 ) T

the algorithm tends to the pattern of the number 4. The figure shows the inputpattern.

Figure 11.2 Input Pattern

When we consider the initial configuration

s(t = 0) = ( - 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, -if

we obtain the pattern of the number 1. The figure shows the input pattern.

Figure 11.3 Input Pattern

/ / hopfield.cpp

#include <iostream>using namespace std;

// initialize the weight matrix W with the patterns

Page 315: Nonlinear Workbook (3rd)

296 CHAPTER 11. NEURAL NETWORKS

void weights(int** W.int* xO.int* xl.int* x2,int N){for(int i=0;i<N;i++)for(int j=O;j<N;j++)

{ W[i][j] = xO[i]*xO[j] + xl[i]*xl[j] + x2[i]*x2[j]; >for(int k=0;k<N;k++)W[k][k] = 0;

>

/ / calculation of sum over j of W[i][j]*s[j]void mul(int** W.int* s.int* h.int N){for(int i=0;i<N;i++)iint sum = 0;for(int j=O;j<N;j++) { sum += W[i] [j]*s[j] ; >h[i] = sum;

>>

int sign(int y)

{if(y > 0) return 1;else return -1;

>

int check(int* vl,int* v2,int N) // checks whether{ // two vectorsfor(int i=0;i<N;i++) // are the same{ // if the vectors areif(vl[i] != v2[i]) return 0; // unequal return 0> // elsereturn 1; // return 1

}

int energy(int** W.int* s.int N) // energy of the configuration{ // Ising model

int E = 0;for(int i=0;i<N;i++)for(int j=O;j<N;j++)E += W[i][j]*s[i]*s[j];return -E;

}

int main(void)

Page 316: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 297

iint N = 40;int* xO = new int[N];int* xl = new int [N] ;int* x2 = new int[N];

/ / pattern 0xO[O] = - 1 ; xO[l] = - 1 ; xO[2] = 1; xO[3] = - 1 ; xO[4] = - 1 ;xO[5] = - 1 ; xO[6] = 1; xO[7] = 1; xO[8] = - 1 ; xO[9] = - 1 ;x0[10] = - 1 ; xO[ll] = - 1 ; xO[12] = 1; xO[13] = - 1 ; xO[14] = - 1 ;xO[15] = - 1 ; xO[16] = - 1 ; xO[17] = 1; xO[18] = - 1 ; xO[19] = - 1 ;x0[20] = - 1 ; xO[21] = - 1 ; xO[22] = 1; xO[23] = - 1 ; xO[24] = - 1 ;xO[25] = - 1 ; xO[26] = - 1 ; xO[27] = 1; xO[28] = - 1 ; xO[29] = - 1 ;x0[30] = - 1 ; xO[31] = - 1 ; xO[32] = 1; xO[33] = - 1 ; xO[34] = - 1 ;xO[35] = - 1 ; xO[36] = - 1 ; xO[37] = - 1 ; xO[38] = - 1 ; xO[39] = - 1 ;

/ / pattern 1xl[0] = - 1 ; xl[ l] = 1; xl[2] = 1 ; xl[3] = 1; xl[4] = - 1 ;xl[5] = 1 ; xl[6] = - 1 ; xl[7] = - 1 ; xl[8] = - 1 ; xl[9] = 1;xl[10] = - 1 ; x l [ l l ] = - 1 ; xl[12] = - 1 ; xl[13] = - 1 ; xl[14] = 1;xl[15] = - 1 ; xl[16] = - 1 ; xl[17] = 1; xl[18] = 1; xl[19] = - 1 ;xl[20] = - 1 ; xl[21] = 1; xl[22] = - 1 ; xl[23] = - 1 ; xl[24] = - 1 ;xl[25] = 1; xl[26] = - 1 ; xl[27] = - 1 ; xl[28] = - 1 ; xl[29] = - 1 ;xl[30] = 1; xl[31] = 1; xl[32] = 1; xl[33] = 1; xl[34] = 1;xl[35] = - 1 ; xl[36] = - 1 ; xl[37] = - 1 ; xl[38] = - 1 ; xl[39] = - 1 ;

/ / pattern 2x2[0] = 1; x2[l] = - 1 ; x2[2] = - 1 ; x2[3] = 1; x2[4] = - 1 ;x2[5] = 1; x2[6] = - 1 ; x2[7] = - 1 ; x2[8] = 1; x2[9] = - 1 ;x2[10] = 1; x2[ll] = - 1 ; x2[12] = - 1 ; x2[13] = 1; x2[14] = - 1 ;x2[15] = 1; x2[16] = 1; x2[17] = 1; x2[18] = 1; x2[19] = 1;x2[20] = - 1 ; x2[21] = - 1 ; x2[22] = - 1 ; x2[23] = 1; x2[24] = - 1 ;x2[25] = - 1 ; x2[26] = - 1 ; x2[27] = - 1 ; x2[28] = 1; x2[29] = - 1 ;x2[30] = - 1 ; x2[31] = - 1 ; x2[32] = - 1 ; x2[33] = 1; x2[34] = - 1 ;x2[35] = - 1 ; x2[36] = - 1 ; x2[37] = - 1 ; x2[38] = - 1 ; x2[39] = - 1 ;

int** W = NULL; / / allocating memory for WW = new int *[N]; / / allocating memory for Wfor(int i=0;i<N;i++) / / allocating memory for W{ W[i] = new int [N]; } / / allocating memory for W

for(int i=0;i<N;i++)for(int j=O;j<N;j++)W[i][j] = 0;

Page 317: Nonlinear Workbook (3rd)

298 CHAPTER 11. NEURAL NETWORKS

weights(W,xO,xl,x2,N); // weight function to obtain weight matrixint* s = new int[N]; // allocation memory for s

// start configurationsCO] = 1; s[l] = 1; s[2] = - 1 ; s [3] = 1; s [4] = - 1 ;s[5] = - 1 ; s[6] = 1; s [7] = - 1 ; s [8] = 1; s [9] = 1;s[10] = 1; s[ll] = - 1 ; s[12] = - 1 ; s [13] = 1; s[14] = - 1 ;s[15] = 1; s[16] = - 1 ; s[17] = 1; s [18] = 1; s [19] = 1;s[20] = - 1 ; s[21] = - 1 ; s [22] = - 1 ; s[23] = - 1 ; s[24] = - 1 ;s[25] = - 1 ; s[26] = - 1 ; s[27] = 1; s [28] = 1; s [29] = - 1 ;s[30] = - 1 ; s[31] = - 1 ; s[32] = - 1 ; s [33] = 1; s [34] = - 1 ;s[35] = - 1 ; s[36] = - 1 ; s[37] = - 1 ; s [38] = - 1 ; s [39] = - 1 ;

int E = energy(W,s,N);

cout « "energy of initial configuration: " « E « endl;

int* h = new int [N]; // allocating memory for hfor(int p=O;p<N;p++) { h[p] = 0; } // initialising h

int* si = new int [N];

for(i=0;i<N;i++) { sl[i] =s[i]; }int result, count;count = 0;do

{for(int i=0;i<N;i++) { sl[i] = s [ i ] ; }mul(W,s,h,N);for(int j=O;j<N;j++){if(h[j] != 0){s[j] = sign(h[j]) ;}if(h[j] == 0) { s[j] = s l [ j ] ; }>result = check(s,sl,N);count++;cout « "count = " « count « endl;} while((count < 100) && (result != 1));

cout « "number of iterations: " « count « endl;

for(int i=0;i<N;i++)

{

Page 318: Nonlinear Workbook (3rd)

11.2. HOPFIELD MODEL 299

cout « "s[" « i « "] = " « s[i] « " "; / / end configurationif(((i+M5) == 0) { cout « endl; }}E = energy(W,s,N);cout << "energy of end configuration: " « E « endl;

delete [] xO; delete [] xl; delete [] x2;deleted s; delete[] si; delete[] h;for(i=0;i<N;i++) { delete[] W[i] ; }delete[] W;return 0;

}

11.2.9 Asynchronous OperationIn the asynchronous operation each element of the state vector is updated separately,while taking into account the most recent values for the components which havealready been updated. Several variants are still possible, but the most usual oneis sequential updating where the new element values are computed in the order inwhich they appear in the state vector. Thus, when updating element Sj, use will bemade of the fact that new elements have already been computed for the elementss0 up to Si-i according to the formula

Si(t + 1) = sign ( £ WijSj(t + 1) + ]T WljSj(t)) .

This scheme is reminiscent of a Gauss-Seidel iteration in linear algebra. The Gauss-Seidel iteration is used to solve linear equations. Starting from the state vector

s(t) = (so(t),si (*),-.., sjv-i(t))

one complete sequential iteration consists in updating successively each of the Ncomponents until one obtains the new state vector s(i + 1). Of course the compo-nents of the state vector can be updated in some different order, e.g. according toa fixed permutation of the natural order or at random but in such a way that eachelement is at least updated once in a given interval of time.

Thus far we have assumed that the externally applied threshold vector 0 is the zerovector. If we include this vector then the map, in the synchronous operation, isgiven by

s(i + 1) = sign(Ws(£) - 6).

For the asynchronous operation we have

(i - l JV-l \

J2 wijSj(t + i)+J2 WijSjit) - &i3=0 j=i J

where i = 0 , 1 , . . . ,N - 1.

Page 319: Nonlinear Workbook (3rd)

300 CHAPTER 11. NEURAL NETWORKS

11.2.10 Translation Invariant Pattern Recognition

We would like the process of associative recall to be made robust in the sense thatthose patterns which are nearly similar, except for a translation in the plane, areidentified as being similar. This can be done using the two-dimensional discreteFourier transform of the scanned images. The figure shows an example of twoidentical patterns positioned with a small displacement from each other.

Figure 11.4 Identical Pattern with Displacement

The two-dimensional discrete Fourier transform of a two-dimensional array is com-puted by first performing a one-dimensional Fourier transform of the rows of thearray and then a one-dimensional Fourier transform of the columns of the result (orvice versa). The absolute value of the Fourier coefficients does not change undera translation of the two-dimensional pattern. The Fourier transform also has theproperty that it preserves angles, that is, similar patterns in the original domain arealso similar in the Fourier domain. This preprocessing can be used to implementtranslation-invariant associative recall. In this case the vectors which are stored inthe network are the absolute values of the Fourier coefficients for each pattern.

Definition. Let x(ni,n,2) denote an array of real values, where n\, n2 are integerssuch that 0 < nx < N\ - 1 and 0 < n2 < N2 - 1. The two-dimensional discreteFourier transform x{k\, k2) of x(ni,n2) is defined by

^ j Ni-iN2-i / 2n 2TT \

x{kuk2):=— — Yl Y, z(ni,n2)exp(-—inifci-—m2M•<vl -<V2 n , = o n 2 = o \ J n ly2 '

where 0 < kx < Ni - 1 and 0 < k2 < N2 - 1.

Consider two arrays x(ni,n2) and y(711,712) of real values. Assume that

2/(ni, n2) = z(ni +di,n2 + d2)

where d\ and d2 are two given integers. The addition ni + d\ is performed moduloN\ and n2 + di is performed modulo N2 (torus). We show that

| |y(*i,k)| | = | |£(fci,M|.

Page 320: Nonlinear Workbook (3rd)

11.3. SIMILARITY METRICS 301

We have

y{kuk2) = — — Y, Y, x{n1+d1,n2 + d2)expl-—in1ki--—in2k2) .•<vl •''2 n i = o n2=0 ^ l 2 '

With the change of indices

n\ = (rti + di) mod Ni, n'2 = (n2 + d2) mod iV2

we obtain

This can be written as

y{h, fc2) = exp (^Fi di f c i) exP \Wid2k2) &(ku k^ '

This expression tells us that the Fourier coefficients of the array y{ni,n2) are thesame as the coefficients of the array x(ni, n2) except for a phase factor

/ 2?r. , \ / 2?r. , , \exp ( ——idiKi] exp I - — zd2K2 ) •

Taking the absolute value results in

||»(fci1A:2)|| = ||i(fc1,fc2)||.

Thus the absolute values of the Fourier coefficients for both patterns are identical.

11.3 Similarity Metrics

A key component of most pattern-comparison algorithms is a prescribed measure-ment of dissimilarity between two feature vectors. Assume we have two featurevectors, x and y defined on a vector space V. We define a metric or distance func-tion d on the vector space V as a real-valued function on the Cartesian productV x V such that

0 < d(x, y) < oo for x, y e V and d(x, y) = 0 iff x = y

d(x, y) = d(y, x) for x, y £ V

d(x, y) < d(x, z) + d(y, z), x, y, z e V

In, addition, a distance funtion is called invariant if

d(x + z,y + z) = d(x,y).

Page 321: Nonlinear Workbook (3rd)

302 CHAPTER 11. NEURAL NETWORKS

Most distance measures, or metrics, are (P metrics

( n \ I/P

If p = 2, we have the classical Euclidean distance.

The cosine between two row vectors x, y in Rn defines another metrical similaritymeasure

TXV

d(x'y) = H Mwhere T denotes the transpose. The cosine distance is invariant under rotation anddilation.

Most current neural networks require two-dimensional input patterns to be con-verted into multi-dimensional vectors before training and recognition can be carriedout. Learned patterns in these networks are represented as multidimensional vectorsof training weights, and the measure of similarity between the presented input andthe learned pattern is based on some similarity metric. Three common similaritymetrics in use in neural networks today are the Hamming distance, the vector dotproduct, and the Euclidean distance.

In the case of binary { 0,1 } vectors of length n, comparable measures of similaritybetween an input vector x and a weight vector y based on each of these metrics canbe defined as

H := 1 Hammingn

T

DP := ^ - ^ Dot ProductM

E := 1 — \ — EuclideanV n

where d is the number of mismatched elements between x and y, n is the numberof total elements in x (i.e. the length of the vectors x and y). |x| is the number ofl's in the vector x.

Transforming a two-dimensional input pattern into a multi-dimensional vector andthen comparing that vector to learned vectors can produce behaviour that is counter-intuitive. Two-dimensional input patterns that appear very similar (to the humaneye) to a particular learned pattern can generate very poor results when comparedto that pattern using these metrics.

Another in many cases more useful metric is the Hausdorff distance which we intro-duce now. The Hausdorff distance measures the extent to which each point of an

Page 322: Nonlinear Workbook (3rd)

11.3. SIMILARITY METRICS 303

input set lies near some point of a model set. Given two finite sets of vectors in theEuclidean space Rn

A:= {x0, xi, . ..,Xp_i}, B := {y0, yi, . . . ,y ,_ i}

the (undirected) Hausdorff distance is defined as

H{A, B) := max{ h(A, B), h(B, A) }

where the function h(A, B) defines the directed Hausdorff distance from A to B

h(A,B):=max{min{||x-y||}}.

Thusft(£,A):=max{min{||y-x||}}

where ||x — y|| is a norm in Rn, for example the Euclidean norm. The directed Haus-dorff distance identifies that point in A that is furthest from any point in B andmeasures the distance from that point to its nearest neighbour in B. If h(A, B) = d,all points in A are within distance d of some point in B. The (undirected) Hausdorffdistance, then, is the maximum of the two directed distances between two point setsA and B so that if the Hausdorff distance is d, then all points of set A are withindistance d of some point in set B and vice versa.

The pointwise Hausdorff distance for a vector x G A is defined as

M x , f l ) : = m n { | | x - y | | } .

The (undirected) Hausdorff distance has the following properties. It is a metric overthe set of all closed, bounded sets. It is everywhere non-negative and it obeys theproperties of identity, symmetry, and triangle inequality. In the context of patternrecognition this means that a shape is identical only to itself, that the order of com-parison does not matter, and that if two shapes are highly dissimilar they cannotboth be similar to some third shape. This final property (triangle inequality) isparticularly important for reliable pattern classification.

In the following C++ program Hausdorff . cpp we calculate the Hausdorff distancefor the two sets

X = { (0.0,0.2), (0.5,0.8), (0.3,0.7) }, Y = { (1.5,2.1), (0.8,1.5) }

inR2.

/ / Hausdorff.cpp

#include <iostream>#include <cmath> // for sqrt

Page 323: Nonlinear Workbook (3rd)

304 CHAPTER 11. NEURAL NETWORKS

using namespace std;

/ / Euclidean distance between to n-dimensional vectorsdouble distance(double* x,double* y . in t n){double result = 0.0;for(int j=O;j<n;j++){ result += (x[j]-y[j])*(x[j]-y[j]); }return sqrt(result);

}

double dhausdorff(double** x,double** y.int p.int q.int n){double max =0.0;double min, temp;for(int i=0;i<p;i++){min = distance (x [i] ,y[0] ,n) ;for( in t j=O;j<q;j++){temp = distance(x[i] ,y[j] ,n) ;if(temp < min) min = temp;}if(max < min) max = min;}return max;

>

int main(void){

int n = 2; / / dimension of Euclidean spaceint p = 3; / / number of vectors in set Aint q = 2; / / number of vectors in set B

double** x = NULL; x = new double*[p];for( int j=O;j<p;j++) x[j] = new double[n];x[0][0] = 0.0; x[0][ l ] = 0.2;x[l][0] = 0.5; x [ l ] [ l ] = 0.8;x[2] [0] = 0.3; x[2] [1] = 0.7;

double** y = NULL; y = new double*[q];for( in t j=0; j<q; j++) y[j] = new double[n];y[0][0] = 1.5; y[0][ l ] = 2 .1 ;y[l][0] = 0.8; y [ l ] [ l ] = 1.5;

Page 324: Nonlinear Workbook (3rd)

11.3. SIMILARITY METRICS 305

double dhd = dhausdorff(x,y,p,q,n);

cout « "Hausdorff distance = " « dhd « endl;

for( int j=O;j<p;j++) { de le ted x [ j ] ; >delete [] x;for( in t j=O;j<q;j++) { de le t ed y [ j ] ; }delete [] y;return 0;

}

As described above, the directed Hausdorff distance h(A, B) identifies the pointa € A that is farthest from any point of B, and measures the distance from a to itsnearest neighbour in B. Thus the Hausdorff distance H(A, B) measures the degree ofmismatch between to sets, as it reflects the distance of the point of A that is farthestfrom any point of B and vice versa. The Hausdorff distance is very sensitive to evena single "outlying" point of A and B. For example, consider B = A U { x }, wherethe point x is some large distance D from any point of A. In this case H(A, B) = D.This means it is determined solely by the point x. Thus sometimes rather then usingH(A, B) we use a generalization of the Hausdorff distance which does not obey themetric properties on A and B, but does obey them on specfic subsets of A andB. This generalized Hausdorff measure is given by taking the A;-th ranked distancerather than the maximum, or largest ranked one,

hk{A, B) = kth min ||a - b\\a£Ab€B

where fcth denotes the k-th ranked value (or equivalently the quantile of m values).For example, when k = m then kth is max, and thus this measure is the sameas h(-, •). When k = m/2 then the median of the m individual point distancesdetermines the overall distance. Therefore this measure generalizes the directedHausdorff measure, by replacing the maximum with a quantile.

Another useful metric for two-dimensional patterns is as follows. Let A, B be twom x n matrices over R. We can introduce a scalar product

(A,B):=tx{ABT)

where T denotes the transpose. Note that ABT is an m x m matrix. The scalarproduct induces the norm

\\A\\2 = (A,A) = tt(AAT).

Thus we have the metric

||A - B\\2 = (A - B, A - B) = {A, A) - {A, B) - (B, A) + (B, B).

Thus\\A - Bf = ti{AAT - ABT - BAT + BBT).

Page 325: Nonlinear Workbook (3rd)

306 CHAPTER 11. NEURAL NETWORKS

Since the trace is linear we have

\\A - Bf = ti{AAT) - 2tr(ABT) + tr(BBT)

where we used that tr(ABT) = tr(BAT). Finally

m n m n m n

i=i j=i i=\ j=i i=i j=i

11.4 Kohonen Network

11.4.1 Introduction

An interesting feature of neural systems is the ability to produce self-organizing,topology preserving mappings of any given feature space, e.g., the surface of abody. Self-organization means that mapping onto a cortex of neurons is originatedonly by stochastically presenting the neurons with input features. The mappingis called topology preserving when neighbouring neurons represent neighbouringregions in the feature space. In order to profit best from the given number ofneurons, important regions of the feature space (high density of presentation) arerepresented by more neurons, i.e., the resolution of the mapping is optimized.

The self-organizing neural network (also called Kohonen's feature map or topology-preserving map) assumes a topological structure among the cluster units. Thisproperty is observed in the brain, but it is not found in other artificial networks.There are p cluster units, arranged in a one-or two-dimensional array. The inputsignals x^ are vectors. The weight vector w for a cluster unit serves as an exem-plar of the input patterns associated with that cluster. During the self-organizingprocess the cluster unit whose weight vector matches the input pattern most closely(typically the square of the minimum Euclidean distance) is chosen as the winner.The winning unit and its neighbour units (in terms of the topology of the clusterunits) update their weights. The weight vectors w3 of neighbouring units are not, ingeneral, close to the input pattern. For example, for a linear array of cluster units,the neighbourhood of radius r around cluster unit j* consists of all

max(l, j* — r) < j < min(j* +r,p).

Where inputs match the node vectors, that area of the map is selectively optimizedto represent an average of the training data for that class. From a randomly orga-nized set of nodes the grid settles into a feature map that has local representationand is self-organised. The purpose of the self-organizing feature map is to capturethe topology and probability distribution of input data. Mappings from higher tolower dimensions are also possible with a self-organizing feature map and are ingeneral useful for dimensionality of input data.

Page 326: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 307

The topology-preserving maps can be used, for example, for pattern recognition andfor finding solutions to the traveling salesman problem.

The Kohonen's feature map allows us to define a criterion function. The Kohonen'srule is a stochastic gradient-descent search that leads, on average and for smalllearning rates rj, to a local minimum of the criterion function.

11.4.2 Algorithm

T. Kohonen [55] proposed an algorithm producing the desired mapping. A neuronis identified with a prototype w; (i = 0 , 1 , . . . ,n — 1) in the feature space. Whenall neurons are presented with an input feature, x, a winner neuron is determined.To this end, a metric in feature space has to be defined. Taking this metric, theprototype w^. of the winner neuron j* has the smallest distance in the feature spacecompared to the other neurons i

||wj. — x|| < ||WJ — x|| for all i.

Thus it represents best the input feature x. The main item of Kohonen's algorithmis the learning step, which improves the mapping of the feature space onto thecortex of neurons. The neurons in the cortical neighbourhood of the winner neuronj* adapt their prototypes in the direction of the input feature x

Wi{t + 1) = Wi{t) + 7j(t)/»(dy.(t))(x " Wi(t)), t = 0, 1, 2, . . .

where dy-(t) denotes the cortical distance of neuron i to the winner neuron j * . Thefunction /i(rfy. (<)) describes the lateral interaction of neurons. Often a Gaussianfunction

h(x) = exp(-x2/a2)

is used. One also often assumes that

h(0) = 1 and h(d < dmax(i)) = 0

where h(dij*(t)) is monotonous in between. With increasing distance from the win-ner neuron, the relative amount of the learning step decreases. dmax is the maximumdistance of interaction. Thus /i(dy.(i)) is the neighbourhood function. This func-tion is 1 for % = j* and falls off with distance |r — rj | . Thus units close to the winner,as well as the winner j* itself, have their weights changed appreciably, while thosefurther away, where dtj. is small, experience little effect.

The time-dependent parameter 77,

0 < r)(t) < 1

controls the absolute amount of a learning step (learning rate). The learning ratedecreases with t —> 00. The time-dependent parameters r)(t) and dma,x(t) denote the

Page 327: Nonlinear Workbook (3rd)

308 CHAPTER 11. NEURAL NETWORKS

plasticity of the neural network. By repeating the whole algorithm (iterating Wj(t)),a mapping with the desired properties will result if the time-dependent parametersr](t) and dma^(t) are successively decreasing as t —> oo. Thus the term r](t) is a gainterm (0 < rj(t) < 1) that decreases in time, thus slowing the weight adaption. Noticethat the neighbourhood Nj. (t) decreases in size as time goes on, thus localising thearea of maximum activity. Different neighbourhoods can be chosen, for example arectangular grid or a hexagonal grid.

If we have more than one input pattern

x 0j x l i • • • i x m - l

we proceed as follows. We can write the set of weights w; (i = 0 , 1 , . . . , n — 1) as ann x N matrix

W = (wo,w1,. . . ,wn_1)

where N is the length of the vectors x, and w*.

1. Initialise network. Define w,(t = 0) (0 < i < n — 1) to be the weights. Initialiseweights to small random values. Set the initial radius of the neighbourhood aroundnode i, A'i(O), to be large.

2. Present input. Present input patterns x0, xj, . . . , x.m-i to the network.

3. Calculate distances. For i = 0 , 1 , . . . , n — 1 calculate

HWi-XoH-

4. Find the winning neuron j * , i.e.

\\wr - x o | | < Hwj-xoll, i = 0,l, . . . , n - 1

5. Update weights. Update the weight according to

Wi(t = 1,0) = Wi(t = 0,0) +r)(t = O)h{dir{t = 0))(x0 - w*(i = 0,0))

for i = 0 , 1 , . . . , n - 1. Thus the biggest change is at the winning neuron (i = j*)since the function h has the largest value at i = j * . The quantity rj(t = 0) is thelearning rate at t = 0.

6. Goto step 3 and repeat the procedure for the input pattern xi, X2, . . . , xm_i. Foreach input pattern the matrix W is updated according to the formula given above.This is one epoch. Goto step 3 again and repeat for the next time step, where thelearning rate r){t) is decreased.

A stopping condition is that no noticeable changes in the feature map are observed.

Page 328: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 309

Let m be the number of input vectors and n be the number of neurons. One candefine a criterion function

-i m—l n—\

JM:=^J2h(rl-rj.)\\xk-wirZ jfc=0 i=0

where j* is the label of the winner unit upon presentation of the input vector x^,and h(Ti — Tj') is the neighbourhood function. The criterion function is an extensionof the competitive learning criterion function. Performing gradient descent for thecriterion function J

Aw,- := -r}VJ

yields

m-l

k=0

which is the batch-mode version of Kohonen's self-organizing rule. The Kohonen'srule is a stochastic gradient-descent search that leads, on average and for smalllearning rate 77, to a local minimum of J. These minima are given as solutions tothe system of nonlinear equations

m- l

AWJ = J2 h(Ti - rj*)(xfc - w 0 = °-k=0

The solution depends on the choice of the neighbourhood function h. What isdesired is the global minimum of the criterion function J. Local minima of J aretopological defects like kinks in one-dimensional maps and twists in two-dimensionalmaps.

11.4.3 Example

In the example we follow Fausett [30]. We have four input vectors and two weightvectors each of length 4. In the C++ program we can select different numbers ofweight vectors. The counting for all the vectors and matrices and their elementsstarts from zero. The four input vectors (input patterns) are given by

( 1.0\ / 0 .0 \ / 1 .0 \ /0 .0 \

1.0 0.0 _ 0.0 _ 0.00.0 ' X l ~ 0.0 ' X2 ~ 0.0 ' X3 ~ 1.0 •o.o/ Vi-o/ Vo.o/ \i.o/

The initial weight matrix W(t — 0) is given by

( 0.2 0.8 \

0.9 0.3/

Page 329: Nonlinear Workbook (3rd)

310 CHAPTER 11. NEURAL NETWORKS

Thus the initial weight matrix consists of two column vectors (two neurons)

( 0.2\ /0 .8 \

0.9/ Vo.3/The initial value of the learning rate is r](t = 0) = 0.6. The time evolution of thelearning rate is

With only two clusters (neurons) available only one cluster updates its weights ateach step. First we calculate the square of the distance between the first input vectorx0 and wo(i = 0) and the square of the distance between the first input vector Xoand wx(i = 0). We find

||wo(t = 0) - xo||2 = (0.2 - I)2 + (0.6 - I)2 + (0.5 - 0)2 + (0.9 - 0)2 = 1.86||Wl(£ = 0) - xo||2 = (0.8 - I)2 + (0.4 - I)2 + (0.7 - 0)2 + (0.3 - 0)2 = 0.98.

Thus the vector wi(i = 0) is closest to the input vector x0, i.e. j* = 1 (winningneuron). Consequently, we update the column wi(t = 0) in the weight matrixW(t — 0) and leave the column Wo(< = 0) unchanged. Thus we find (rj = 0.6)

W01(t = 1,0) = WOi(t = 0) + V{t = O)(xo,o - W01(t = 0)) = 0.92Wn(t = l,0) = Wn(t = 0) + q(t = O)(aro,i - Wn(t = 0)) = 0.76W2i(t = l,0) = W2i(t = 0) +7]{t = O)(xOl2 - W2l{t = 0)) = 0.28W31{t = 1,0) = W31(t = 0) + v(t = 0)(xOA - W31{t = 0)) = 0.12 .

Thus the updated weight matrix W(t = 1,0) after taking into account the inputvector Xo is given by

( 0.2 0.92\0.6 0.760.5 0.28 •0.9 0.12/

Thus

/0 .2 \ /0.92\

wo(t = l,O)= 0 5 , w 1 ( t = l , 0 ) = 0 2 g .

\ 0 . 9 / \0 .12/For the second input vector Xi we find

||wo(t = 1,0)- Xl | |2 = (0.2)2 + (0.6)2 + (0.5)2 + (0.9 - 1.0)2 = 0.66

||Wl(t = 1,0)- Xl | |2 = (0.92)2 + (0.76)2 + (0.28)2 + (0.12 - 1.0)2 = 2.2768 .

Thus the vector Wo(i = 1,0) is closest to the input vector xi, i.e., j* = 0 (winningneuron). Consequently, we update the column WQ(£ = 1,0) in the weight matrix

Page 330: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 311

W(t = 1,0) and leave the column wx(t = 1,0) unchanged. Thus we find

W00{t = 1,1) = W00{t - 1,0) + 7?(t = 0)(il i0 - W00(t = 1,0)) = 0.08W10(t = 1,1) = WiO(t = 1,0) + r){t = 0)(a;1,1 - W10(i = 1,0)) = 0.24W20{t =1,1) = W20(t = 1,0) + 7j(t = 0)(il i2 - W2o(t = 1,0)) = 0.20W30(t =1 ,1) = W30(t = 1 , 0 ) + ^(t = 0)(zli3 - W30(* = 1,0)) = 0.96

where rj(t = 0) = 0.6. Thus the updated weight matrix after taking into accountthe first input vector Xi is given by

/0.08 0.92 \

wu = a)- °-24 a76

W(t M J ~ 0.20 0.28 •V0.96 0.12/

Thus

( 0.08\ /0.92\

0.24 ,. n . 0.760.20 ' wi(* = 1 ' 1 ) = 0.28 •0.96/ VO.12/

For the third input vector x2 we find||wo(t = 1,1) - x2||2 = 1.8656, ||wi(t = 1,1) - x2||2 = 0.6768.

Thus the vector wi(f = 1,1) is closest to the input vector x2, i.e. j* = 1 (winningneuron). Consequently, we update the column wi(t = 1,1) in the weight matrixW(t = 1,1) and leave the column wo(i = 1,1) unchanged. It follows that

/0.08 0.968 \, _ 9s 0.24 0.304

^ ( * - 1 ' 2 ) = 0.20 0.112 •V0.96 0.048/

Hence/0.08\ / 0.968 \

, . „ 0.24 , , , , 0.304wo(t = l , 2 ) = 0 2 Q , w1(f = l , 2 ) = 0 U 2 .

V0.96/ V 0.048/For the fourth input vector X3 we find

||wo(t = 1,2)- x3||2 = 0.7056, ||Wl(t = 1,2)- x3||2 = 2.724.The vector Wo(t = 1,2) is closest to the input vector X3, i.e. j* = 0 (winningneuron). Consequently, we update the column wo(t = 1,2) in the weight matrix Wand leave the column wj(£ = 1,2) unchanged. All four input vectors have now beentaken into account. We find

( 0.032 0.968 \

0.096 0.3040.680 0.1120.984 0.048/

Page 331: Nonlinear Workbook (3rd)

312 CHAPTER 11. NEURAL NETWORKS

with

[0.032 \ / 0.968 \

0.096 ,. , . 0.3040.680 ' w ^ = 1 ) = 0.112 •0.984/ V 0.048/

This was an epoch (one iteration, one time step). Now reduce the learning tov(t = i) = Y^v(t = o) •

We repeat the steps again for each of the input vectors x0, xi, x2 and x3. After thesecond epoch (second iteration) the weight matrix is

/ 0.016 0.980 \

W ( t - 2 ) - °-°47 0 3 6°W{t-2)-\ Q 6 3 0 Q 0 5 5 .

V 0.999 0.024/

Iterating over 100 epochs we find that the weight matrix converges to the matrix

[0.0 1.0\

1.0 0.0/

The zeroth column is the arithmetic average of the two vectors Xi and X3 and thefirst column is the arithmetic average of the vectors x0 and X2-In the following C++ program we can select different numbers of weight vectors.We consider the case 1, 2, 3, 4 and 5. The weight vectors at t = 0 are selected atrandom using a random number generators. The number of input vectors can alsobe easily changed. In the program const int m = 4 is the number of input vectors.// Fausett.cpp

#include <iostream>#include <cstdlib>#include <ctime>using namespace std;

double euclidean(double *vecl,double *vec2,int n)

{double dist =0.0;for(int i=0;i<n;i++)dist += (vecl[i]-vec2[i])*(vecl[i]-vec2[i]);return dist;

}

Page 332: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 313

double distance(int i.int jstar)

{

return double(i!=jstar);

// returns 1.0 if i != jstar

// returns 0.0 if i == jstar

>

double h(double d) { return 1.0 - d; >

void train(double **W,int n.int cols,double *vec,double rate)

{int i,j;int win = 0;double windist = euclidean(W[0],vec,n).edist;for(i=0;i<cols;i++)if((edist=euclidean(W[i],vec,n)) < windist){ win = i; windist = edist; }for(i=0;i<cols;i++)for(j=0;j<n;j++)W[i] [j] += rate*h(distance(i,win))*(vec[j]-W[i] [j]);

>

int main(void)

•C

int i, j;int T = 10000; // number of iterationsconst int m = 4;int cols;double eta =0.6; // learning rate

// training vectorsdouble xOQn] = -C 1.0,1.0,0.0,0.0 };double xl[m] = { 0.0,0.0,0.0,1.0 >;double x2[m] = { 1.0,0.0,0.0,0.0 };double x3[m] = { 0.0,0.0,1.0,1.0 };

cout « "Enter number of columns for weight matrix: ";cin » cols;

double** W = NULL; W = new double*[cols];for(i=0;i<cols;i++) W[i] = new double[m];

srand(time(NULL));for(i=0;i<cols;i++)

Page 333: Nonlinear Workbook (3rd)

314 CHAPTER 11. NEURAL NETWORKS

for(j=0;j<m;j++)W[i][j] = rand ()/double (RAND_MAX);

for(i=0;i<T;i++)

itrain(W,m,cols,xO,eta);train(W,m,cols,xl,eta);train(W,m,cols,x2,eta);train(W,m,cols,x3,eta);eta /= 1.05; // learning rate decreased>for(i=0;i<cols;i++){cout « "W[" « i « "]= [";for(j=0;j<m;j++)cout « W[i][j] « " ";cout « " ] " « endl;>f or (i=0; Kcols; i++)delete [] W[i] ;delete[] W;

return 0;>

For the output of the program we must keep in mind that it is sensitive to theinitial conditions for the weight vectors which we selected with a random numbergenerator. Furthermore the result is sensitive to the initial value and decrement ofthe learning rate 77. A typical output is

Enter number of columns for weight matrix : 1W[0]= [0.496931 0.245436 0.25464 0.503069]/ / average of xO,xl,x2,x3

Enter number of columns for weight matrix: 2W[0]= [1 0.493825 4.36192e-12 1.17225e-ll]/ / average of xO,x2W[l]= [5.91835e-12 7.1785e-15 0.506175 1]/ / average of xl,x3

Enter number of columns for weight matrix: 3W[0]= [7.88199e-12 1.60492e-ll 0.506175 1]W[l]= [0.999996 1.26278e-06 9.35514e-07 8.32493e-07]W[2]= [0.999999 0.999999 3.48972e-06 2.59697e-06]

Page 334: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 315

Enter number of columns for weight matrix: 4W[0]= [0.999997 1 5.9738e-07 1.93129e-06] / / xOW[l]= [1.74599e-06 2.94386e-06 4.57243e-07 0.999998] / / xlW[2]= [3.24247e-06 2.76703e-06 0.999999 0.999999] / / x3W[3]= [0.999999 1.4358e-06 1.08384e-07 4.02981e-06] / / x2

Enter number of columns for weight matrix: 5W[0]= [1 0.493825 6.02668e-12 1.20011e-12]// average of xO,x2W[l]= [0.799493 0.979553 0.88229 0.325358]W[2]= [2.78495e-06 1.26278e-06 1.37201e-06 1]/ / xlW[3]= [2.09753e-06 1.48308e-06 0.999999 0.999997]/ / x3W[4]= [0.737724 0.589648 0.57268 0.307535]

11.4.4 Traveling Salesman ProblemThe traveling salesman problem is perhaps the most famous problem in all networkand combinatorial optimization. The problem is easy to state. Starting from hishome base, node 1, a salesman wishes to visit each of several cities, represented bynodes 2 , . . . , n, exactly once and return home, doing so at the lowest possible travelcost. We refer to any feasible solution to this problem as a tour (of the cities). Thereare n! possible tours if there are n cities, but some of them are the same.

The traveling salesman problem is a generic core model that captures the combina-torial essence of most routing problems and, indeed, most other routing problemsare extensions of it. For example, in the classical vehicle routing problem, a setof vehicles, each with a fixed capacity, must visit a set of customers (e.g., grocerystores) to deliver (or pick up) a set of goods. We wish to determine the best possibleset of delivery routes. Once we have assigned a set of customers to a vehicle, thatvehicle should take the minimum cost tour through the set of customers assigned toit; that is, it should visit these customers along an optimal traveling salesman tour.The traveling salesman problem also arises in problems that on the surface haveno connection with routing. For example, suppose that we wish to find a sequencefor loading jobs on a machine (e.g., items to be painted), and that whenever themachine processes job i after job j , we must reset the machine (e.g., clear the diesof the colours of the previous job), incurring a setup time cy-. Then in order tofind the processing sequence that minimizes the total setup time, we need to solvea traveling salesman problem - the machine, which functions as the "salesman",needs to "visit" the jobs in the most cost-effective manner.

We present the traveling salesman problem as an embedded (directed) network flowstructure. Let c^ denote the cost of traveling from city i to city j and let j/y be azero-one variable, indicating whether or not the salesman travels from city i to city

Page 335: Nonlinear Workbook (3rd)

316 CHAPTER 11. NEURAL NETWORKS

j . Moreover, let us define flow variables Xy on each arc (i, j) and assume that thesalesman has n— 1 units available at node 1, which we arbitrarily select as a "sourcenode", and that he must deliver 1 unit to each of the other nodes. Then the modelis

Minimize ^ cijVij ia)

subject to£ I/y = l for all i = 1 ,2 , . . . ,n (6)

l<j<n

J2ViJ = 1 for all j = 1,2, . . . , n (c)l<i<n

Nx = b (d)

Xij <(n- l)yij for all (i,j) G A (e)

i y > 0 foraU(t , j )eA (/)

j/y = 0 o r l forall(z,j) £ A (g)

To interpret this formulation, let

A ' : = { ( t , j) : Vij = 1 } , A " := { ( i , j) : X i j > 0 } .

The constraints (b) and (c) imply that exactly one arc of A' leaves and enters anynode i; therefore, A' is the union of node disjoint cycles containing all of the nodes ofN. In general, any integer solution satisfying (b) and (c) will be the union of disjointcycles; if any such solution contains more than one cycle, we refer to each of thecycles as subtours, since they pass through only a subset of the nodes. Constraint(d) ensures that A" is connected since we need to send 1 unit of flow from node 1 toevery other node via arcs in A". M is a node-arc incidence matrix. The "forcing"constraints (e) imply that A" is a subset of A1. Since no arc need ever carry morethan (n — 1) units of flow, the forcing constraint for arc (i, j) is redundant if y^ = 1.These conditions imply that the arc set A' is connected and so cannot contain anysubtours.

We solve the traveling salesman problem using the Kohonen algorithm. The posi-tions of the cities are given by

x[0] = 10.1, y[0] = 20.2, x[l] = 4.0, y[l] = 7.0

x[2] = 0.1, y[2] = 2.0, a; [3] = 0.5, y[3] = 10.7

x[4\ = 10.5, y[4] = 0.6, x[5] = 8.0, y[5] = 10.0

x[&] = 12.0, y[6] = 16.0, x[7] = 18.0, y[7] = 8.0

The distance is given by the Euclidian norm. The output of the program kohonen. cppusing the Kohonen algorithm provides the following solution to the traveling sales-man problem

0 - > 3 - » 5 - H - > 2 - > - 4 - > 7 - > 6 - > 0 .

The C++ program for the traveling salesman problem using the Kohonen featuremap is as follows.

Page 336: Nonlinear Workbook (3rd)

11.4. KOHONEN NETWORK 317

/ / kohonen.cpp

#include <iostream>#include <cmath> // for fabs, exp#include <cstdlib>#include <ctime>#include <iomanip> // for setprecisionusing namespace std;

// find the position in the array d// which possesses the smallest numberint minimum(double* d,int M){

int m = 0;double r = d[0] ;for(int j=l;j<M;j++){if(d[j] < r) { r = d[j] ; m = j ; }>return m;

}

// lateral interaction of neuronsdouble map(double a.double b,double c,double d,double s){double result;result = exp(-(fabs(a-c) + fabs(b-d))/(2*s*s));return result;

}

int main(void)

{int N = 8; // number of citiesdouble* x = new double[N];double* y = new double[N];// coordinates of citiesx[0] = 1 0 . 1 ; x [ l ] = 4 . 0 ; x[2] = 0 . 1 ; x[3] = 0 . 5 ;x[4] = 10 .5 ; x[5] = 8 .0 ; x[6] = 12.0; x[7] = 18 .0 ;y[0] = 20 .2 ; y [ l ] = 7 .0 ; y[2] = 2 . 0 ; y[3] = 10.7 ;y[4] = 0 .6 ; y[5] = 10.0; y[6] = 16.0; y[7] = 8 .0 ;

i n t M = 3*N; / / M number of neuronsdouble* u = new double[M];double* v = new double[M];

Page 337: Nonlinear Workbook (3rd)

318 CHAPTER 11. NEURAL NETWORKS

const double PI = 3.14159;

// neuron pattern at t = 0for(int j=O;j<M;j++){double k = j; double K = M;u[j] = 10.0 + 10.0*sin(2.0*PI*k/K); // neuron patternv[j] = 10.0 + 10.0*cos(2.0*PI*k/K); // neuron pattern>

double* dist = new double[M];double* delWl = new double[M];double* delW2 = new double[M];

double etaO =0.8; // learning rate at t = 0long T = 15000; // number of iterations

srand((unsigned long) time(NULL)); // seed for random numbers

// iteration starts herefor(long cnt=0;cnt<T;cnt++)

{int nrand = randO'/.N;for(j= O;j<M;j++)

{dist[j] = fabs(u[j]-x [nrand]) + fabs (v[j]-y [nrand]);}

int imin = minimum(dist,M);double c = cnt; double it = T;double s;s = etaO*(l.O - c/(it + 10000.0));

for(j=0;j<M;j++)

{delWl[j] = 1.0*map(u[j] ,v[j] ,u[imin] ,v[imin],s)*(x[nrand]-u[j]);delW2[j] = 1.0*map(u[j] ,v[j] ,u[imin] ,v[imin] ,s)*(y [nrand]-v[j] );>

for(j=0;j<M;j++)

{u[j] += delWl[j]; v[j] += delW2[j];}} / / end iteration

Page 338: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 319

/ / display of outputcout « endl « endl;for(j=0;j<M;j++){cout « "u[" « j « "] = " « setprecisionO) « u[j] « " ";cout « "v[" « j « "] = " « setprecisionO) « v[j] « endl;>

delete [] x; delete [] y;delete [] u; delete [] v;delete [] delWl; delete [] delW2;delete [] dist;return 0;

}

The output is sensitive to the initial learning rate 7/(0) and the decrease of thelearning rate at each time step. Furthermore the output is sensitive to the locationof the neurons at time t = 0 and the number of neurons. The number of neuronsshould be chosen between 2N < M < AN. In our program we selected the neuronslying on a circle with centre (10,10) and radius 10. The center c could also beselected using the formula

In chapter 12 we solve the traveling salesman problem using genetic algorithms.

11.5 Perceptron

11.5.1 Introduction

The perceptron (Fausett [30], Haykin [44], Cichocki and Unbehauen [17], Hassoun[43], Rojas [77]) is the simplest form of a neural network used for the classificationof special types of patterns said to be linearly separable (i.e. patterns that lie on op-posite sides of a hyperplane). It consists of a single neuron with adjustable synapticweights Wi and threshold 0.

Definition. A perceptron is a computing unit with threshold 0 which, when receiv-ing the n real inputs x\, X2, • •., xn through edges with the associated weights u/i,u>2, ..., wn, outputs 1 if the inequality

n

i=\

holds otherwise it outputs zero.

Page 339: Nonlinear Workbook (3rd)

320 CHAPTER 11. NEURAL NETWORKS

The origin of the inputs is not important irrespective of whether they come fromother perceptrons or another class of computing units. The geometric interpretationof the processing performed by perceptrons is the same as with McCulloch-Pittselements. A perceptron separates the input space into two half-spaces. For pointsbelonging to one half-space the result of the computation is 0, for points belongingto the other it is 1. We can also formulate this definition using the Heaviside stepfunction

„ , > f lfora; > 0ff(x) = \ 0 f o n < 0 •

Thus

MYw-x- - 0 ) - l l t 0 1 (E^WiX* ~ 9 ) ~ °

H ( 2 , w l X t 9) - | Q for ( E L i ^ ^ _e)<Q

With w\, u;2, ..., wn and 9 given, the equationn

J2 wiXi = et=i

defines a hyperplane which divides the Euclidean space R™ into two half spaces.Example. The plane

xi + 2x2 - 3ar3 = 4

which divides R3 into two half-spaces.

In many cases it is more convenient to deal with perceptrons of threshold zero only.This corresponds to linear separations which are forced to go through the origin ofthe input space. The threshold of the perceptron with a threshold has been convertedinto the weight —9 of an additional input channel connected to the constant 1. Thisextra weight connected to a constant is called the bias of the element. Thus theinput vector (xi, X2,..., xn) must be extended with an additional 1 and the resulting(n + l)-dimensional vector

{l,xl,x2,.--,xn)

is called the extended input vector, where x0 = 1. The extended weight vectorassociated with this perceptron is

(wo,wi,.. .,wn)

whereby WQ — —9.

The threshold computation of a perceptron will be expressed using scalar products.The arithmetic test computed by the perceptron is thus

wTx>0

if w and x are the weight and input vectors, and

w T x > 0

Page 340: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 321

if w and x are the extended weight and input vectors.

Example. If we are looking for the weights and threshold needed to implement theAND function with a perceptron, the input vectors and their associated outputs are

(0,0)i->0, ( 0 , 1 ) ^ 0 , (1,0) M-0, (1,1) M-l.

If a perceptron with threshold zero is used, the input vectors must be extended andthe desired mappings are

( 1 , 0 , 0 ) ^ 0 , ( 1 , 0 , 1 ) ^ 0 , ( 1 , 1 , 0 ) ^ 0 , ( 1 , 1 , 1 ) ^ 1 .

A perceptron with three still unknown weights (uio, w\, W2) can carry out this task.

Example. The AND gate can be simulated using the perceptron. The AND gateis given by

0 I 0 [I 0~_0_J__0_

1 0 0

1 1 1 |1 1

Thus the input patterns are

x o = ( 2 ) ' x i = ( ! ) ' X 2 = ( S ) ' X 3 = G ) -Let

- ( ! ) • - I -Then wT = (1,1) and the evaluation of

tf (wTXi - 6)

for j = 0,1,2,3 yields

H(WTK0 - 6) = H(0 - 5) = H(-|) = 0

H{wTx1-6) = H(l-l) = H(-\) = 0

H(wTx2-e) = H(l-^) = H(-1-) = 0

H(wTx3-9) = H(2-^) = H(l-) = l.

Page 341: Nonlinear Workbook (3rd)

322 CHAPTER 11. NEURAL NETWORKS

11.5.2 Boolean FunctionsWe consider the problem of determining which logical functions can be implementedwith a single perceptron. A perceptron network is capable of computing any logicalfunction since perceptrons are even more powerful than unweighted McCulloch-Pittselements. If we reduce the network to a single element, which functions are still com-putable? Taking the boolean functions of two variables as an example we can gainsome insight into this problem. Let A be the AND operation, V the OR operation,© the XOR operation, and -• the NOT operation.

Since we are considering logical functions of two variables, there are four possiblecombinations for the input. The outputs for the four inputs are four bits whichuniquely distinguish each logical function. We use the number defined by these fourbits as a subindex for the name of the functions. The function (£1,2:2) ^ 0, forexample, is denoted by /o (since 0 corresponds to the bit string 0000). The ANDfunction is denoted by fg (since 8 corresponds to the bit string 1000), whereby theoutput bits are ordered according to the following ordering of the inputs: (1,1),(0,1), (1,0), (0,0).

The sixteen possible functions of two variables are thus

fo(xi,x2) = f0000(^1, x2) = 0fi{xux2)-foooi{xi,x2) = ->{xi Vi2)f2(xu x2) = foow(xi,x2) = xx A ->x2

fs(xi, x2) = foou(xi, x2) = -^x2

fi{x1,X2) = f0100(xi,X2) = -11^X2

f5(x\,X2) = foiOl(xi,X2) =~<Xi

f6(Xi,X2) = foUo(xi,X2) =Xi®X2

f7(xl,x2) = f0in{xi,x2) = ^{xiAx2)

fs{xi, x2) = fioooixx, x2) = XtAx2

fg{xi,X2) = fiooi{xi,X2) = ^{xx@X2)

ha(xx,x2) = fmo{xi,x2) =xi

fn(xi, x2) = /ion(xi,x2) = xi V-.Z2

fu(xi, x2) = /nooOci, x2) = x2

fi3(xi, x2) = hvai{xu x2) = -'Xi V x2

fu{xi, x2) = /mo(zi, x2) = i i V i j

/i5(^i,a:2) = /iiii(a;i!a;2) = 1-

The function /o, for example, is the zero function whereas /14 is the inclusive OR-function. Perceptron-computable functions are those for which the points whosefunction value is 0 can be separated from the points whose function value is 1 usinga line. For the AND function and OR function we can find such a separation.

Page 342: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 323

Two of the functions cannot be computed in this way. They are the function XOR(exclusive OR) (function /6) and the function XNOR /9. It is obvious that no linecan produce the necessary separation of the input space. This can also be shownanalytically.

Let w\ and w2 be the weights of a perceptron with two inputs, and 9 its threshold.If the perceptron computes the XOR function the following four inequalities mustbe fulfilled

xi = 0 x2 = 0WiXi + w2x2 = 0 = > • 0<9Xi = 1 x2 = Owi^i + W2X2 — w\ => wi > 9xi = 0 x2 = l W1Z1 + w2x2 = w2 => w2>9Xi = 1 X2 = 1 W1X1 + W2X2 = U>i + W2 =*• Wi + W2 < 9 .

Since the threshold 9 is positive, according to the first inequality, w\ and w2 arepositive too, according to the second and third inequalities. Therefore the inequality

W\ + w2 < 9

cannot be true. This contradiction implies that no perceptron capable of computingthe XOR function exists. An analogous proof holds for the function /g.

11.5.3 Linearly Separable SetsMany other logical functions of several arguments must exist which cannot be com-puted with a threshold element. This fact has to do with the geometry of then-dimensional hypercube whose vertices represent the combination of logic valuesof the arguments. Each logical function separates the vertices into two classes. Ifthe points whose function value is 1 cannot be separated with a linear cut from thepoints whose function value is 0, the function is not perceptron-computable.

Definition. Two sets of points A and B in the n-dimensional space R" are calledlinearly separable if n + 1 real numbers Wo, uij,... ,wn exist, such that every point(x\,x2, • • •, xn) 6 A satisfies

n

^2 WjXj > Wo

and every point {xx,x2, •. •, xn) € B satisfies Ef=1WjZj < w0.

Definition. Two sets A and B of points in the n-dimensional space Rn are calledabsolutely linearly separable if n+1 real numbers wQ,Wi,...,wn exist such that everypoint (xi, x2, ...,xn)eA satisfies

n

J2 WjXj > W0i=l

and every point (xi, x2,..., xn) G B satisfies Y%=lWiXi < w0.

Page 343: Nonlinear Workbook (3rd)

324 CHAPTER 11. NEURAL NETWORKS

Definition. The open (closed) positive half-space associated with the n-dimensionalweight vector w is the set of all points x e R " for which wTx > 0 (wTx > 0). Theopen (closed) negative half-space associated with w is the set of all points x 6 R"for which wTx < 0) (wTx < 0).

We omit the adjectives "closed" or "open" whenever it is clear from the contextwhich kind of linear separation is being used.

A perceptron can only compute linearly separable functions. How many linearlyseparable functions of n binary arguments are there? When n = 2, 14 out of the 16possible Boolean functions are linearly separable. When n = 3, 104 out of 256 andwhen n = 4, 1882 out of 65536 possible functions are linearly separable. No formulafor expressing the number of linearly separable functions as a function of n has yetbeen found.

11.5.4 Perceptron Learning

A learning algorithm is an adaptive method by which a network of computing unitsself-organizes to implement the desired behaviour. This is done in some learningalgorithms by presenting some examples of the desired input-output mapping to thenetwork. A correction step is executed iteratively until the network learns to pro-duce the desired response. The learning algorithm is a closed loop of presentationof examples and of corrections to the network parameters.

Learning algorithms can be divided into supervised and unsupervised methods. Su-pervised learning denotes a method in which some input vectors are collected andpresented to the network. The output computed by the network is observed andthe deviation from the expected answer is measured. The weights are corrected ac-cording to the magnitude of the error in the way defined by the learning algorithm.This kind of learning is also called learning with a teacher, since a control processknows the correct answer for the set of selected input vectors.

Unsupervised learning is used when, for a given input, the exact numerical outputa network should produce is unknown. Assume, for example, that some points intwo-dimensional space are to be classified into three clusters. For this task we canuse a classifier network with three output lines, one for each class. Each of the threecomputing units at the output must specialize by firing only for inputs correspond-ing to elements of each cluster. If one unit fires, the others must keep silent. Inthis case we do not know a priori which unit is going to specialize on which cluster.Generally we do not even know how many well-defined clusters are present. Sinceno "teacher" is available, the network must organize itself in order to be able toassociate clusters with units.

Supervised learning is further divided into methods which use reinforcement or errorcorrection. Reinforcement learning is used when after each presentation of an input-

Page 344: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 325

output example we only know whether the network produces the desired result ornot. The weights are updated based on this information (that is, the Boolean valuestrue or false) so that only the input vector can be used for weight correction. Inlearning with error correction, the magnitude of the error, together with the inputvector, determines the magnitude of the corrections to the weights, and in manycases we try to eliminate the error in a single correction step. The perceptron learn-ing algorithm is an example of supervised learning with reinforcement. Some of itsvariants use supervised learning with error correction (corrective learning).

The proof of convergence of the perceptron learning algorithm assumes that eachperceptron performs the test wTx > 0. So far we have been working with percep-trons which perform the test wTx > 0.

If a perceptron with threshold zero can linearly separate two finite sets of inputvectors, then only a small adjustment to its weights is needed to obtain an absolutelinear separation. This is a direct corollary of the following proposition.

Proposition. Two finite sets of points, A and B, in n-dimensional space which arelinearly separable are also absolutely linearly separable.

A usual approach for starting the learning algorithm is to initialize the networkweights randomly and to improve these initial parameters, looking at each step tosee whether a better separation of the training set can be achieved. We identifypoints (xi, x2, • • •, xn) in n-dimensional space with the vector x with the same co-ordinates.

Let P and TV be two finite sets of points in R™ which we want to separate linearly.A weight vector is sought so that the points in P belong to its associated positivehalf-space and the points in N to the negative half-space. The error of a perceptronwith weight vector w is the number of incorrectly classified points. The learning al-gorithm must minimize this error function E(w). Now we introduce the perceptronlearning algorithm. The training set consists of two sets, P and N, in n-dimensionalextended input space. We look for a vector w capable of absolutely separating bothsets, so that all vectors in P belong to the open positive half-space and all vectorsin N to the open negative half-space of the linear separation.

Algorithm. Perceptron learningstart: The weight vector w(< = 0) is generated randomlytest: A vector x 6 P U N is selected randomly,

if x € P and w(«)Tx > 0 goto test,if x € P and w(i)Tx < 0 goto odd,if x S N and w(t)Tx < 0 goto test,if x 6 N and w(t)Tx > 0 goto substract,

add: set w(i + 1) = w(t) + x and t:— t + 1, goto testsubtract: set w(t + 1) = w(t) — x and t := t + 1 goto test

Page 345: Nonlinear Workbook (3rd)

326 CHAPTER 11. NEURAL NETWORKS

This algorithm makes a correction to the weight vector whenever one of the selectedvectors in P or N has not been classified correctly. The perceptron convergencetheorem guarantees that if the two sets P and N are linearly separable the vectorw is updated only a finite number of times. The routine can be stopped when allvectors are classified correctly.

Example. Consider the sets in the extended space

P = {(1.0,2.0,2.0), (1.0,1.5,1.5)}

TV ={(1.0,0.0,1.0), (1.0,1.0,0.0), (1.0,0.0,0.0)}.

Thus in R2 we consider the two sets of points

.{(2.0,2.0), (1.5,1.5)}, {(0.0,1.0), (1.0,0.0), (0.0,0.0)}.

These two sets are separable by the line x\ + x2 — §. Thus wT = ( -§ , 1,1). Thefollowing C++ program classify.cpp implements the algorithm.

/ / classify.cpp

#include <iostream>#include <cstdlib>#include <ctime>using namespace std;

void classify(double **P,double **N,int p . in t n.double *w,int d)iint i, j, k, classified = 0;double *x, sum;

srand(time(NULL));for(i=0;i<d;i++) w[i] = double(rand())/RAND_MAX;k = 0;while(!classified)

{i = rand()*/.(p+n-i);if(i<p) x = P[i]; else x = N[i-p];for(j=0,sum=0;j<d;j++) sum += w[j]*x[j] ;if((i<p) && (sum<=0))for(j=0;j<d;j++) w[j] += x [ j ] ;

if((i>=p) && (sum>=0))for(j=0;j<d;j++) w[j] -= x [ j ] ;

k++;classified = 1;// check if the vectors are classified

Page 346: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 327

if((k'/.(2*p+2*n)) == 0){for(i=0;(i<p) && classified;i++)

•Csum = 0 . 0 ;for(j=0,sum=0; j<d; j++) sum += w[j]*P[i] [j] ;if(sum <= 0) classified = 0;}for(i=0;(i<n) && classified;i++){sum = 0.0;for(j=0,sum=0; j<d;j++) sum += w[j]*N[i] [j] ;if(sum >= 0) classified = 0;

}}else classified = 0;}

>

int main(void)•C

double **P = new double* [2];P[0] = new double [3]; P[l] = new double [3];P[0][0] = 1.0; P[0][l] = 2.0; P[0][2] = 2.0;P[l][0] = 1.0; P[1][1] = 1.5; P[l][2] = 1.5;

double **N = new double*[3];N[0] = new double [3]; N[l] = new double [3]; N[2] = new double [3] ;N[0][0] = 1.0; N[0][l] = 0.0; N[0][2] = 1.0;N[l][0] = 1.0; N[1][1] = 1.0; N[l][2] = 0.0;N[2][0] = 1.0; N[2][l] = 0.0; N[2][2] = 0.0;

double *w = new double[3];classifyCP.N^.S.w.S);cout « "w = ( " « w[0] « " , " « w[l]

« " , " « w[2] « " ) " « endl;

dele ted P[0]; delete[] P [ l ] ;delete [] N[0] ; delete [] N[l]; delete [] N[2];delete [] P; delete [] N;delete w;return 0;

}

Page 347: Nonlinear Workbook (3rd)

328 CHAPTER 11. NEURAL NETWORKS

11.5.5 One and Two Layered Networks

We now consider feed-forward networks structured in successive layers of computingunits. The networks we want to consider must be defined in a more precise way interms of their architecture. The atomic elements of any architecture are the comput-ing units and their interconnections. Each computing unit collects the informationfrom n input lines with an integration function E : R" —>• R. The total excitationcomputed in this way is then evaluated using an activation function f : R —> R.In perceptrons the integration function is the sum of the inputs. The activation,also called output function, compares the sum with a threshold. We can generalize/ to produce all values between 0 and 1. In the case of E some functions otherthan addition can also be considered. In this case the networks can compute somedifficult functions with fewer computing units.

Definition. A network architecture is a tuple (/, N,O,E) consisting of a set I of in-put sites, a set ,/V of computing units, a set O of output sites and a set E of weighteddirected edges. A directed edge is a tuple (u, v, w) whereby u £ I U N,v e NliOand w € R.

The input sites are just entry points for information into the network and do notperform any computation. Results are transmitted to the output sites. The set Nconsists of all computing elements in the network. Note that the edges between allcomputing units are weighted, as are the edges between input and output sites andcomputing units.

Layered architectures are those in which the set of computing units TV is subdividedinto £ subsets Ni, N2,. •., Nt in such a way that only connections from units in Nigo to units in N2, from units in N2 to units in N3, etc. The input sites are onlyconnected to the units in the subset Ni, and the units in the subset Nt are the onlyones connected to the output sites. In the usual terminology, the units in Ni are theoutput units of the network. The subsets Ni are called the layers of the network.The set of input sites is called the input layer, the set of output units is called theoutput layer. All other layers with no direct connections from or to the outside arecalled hidden layers. Usually the units in a layer are not connected to each other(although some neural models make use of this kind of architecture) and the outputsites are omitted from the graphical representation. A neural network with a layeredarchitecture does not contain cycles. The input is processed and relayed from thelayer to the other, until the final result has been computed.

In layered architectures normally all units from one layer are connected to all otherunits in the following layer. If there are m units in the first layer and n units in thesecond one, the total number of weights is mn. The total number of connectionscan be rather large.

Page 348: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 329

11.5.6 Perceptron Learning Algorithm

A simple perceptron learning algorithm is

1. Initialize the connection weight w to small random values.

2. Initialize acceptable error tolerance e0

3. Set emax = 0

4. For each of the input patterns { Xj, j = 0 , 1 , . . . , m — 1} do the following

(a) Calculate the output yj via

Mi = #(wTXj- - e)

where H is the Heaviside function and j = 0,1,... ,m — 1.

(b) Calculate the difference between the output yj and the desired output yjof the network

dj '•=$]- Vi •

(c) Calculate the changes in the connection strengths

AWJ := -qdjXj

where rj is the learning rate.

(d) Update the connection weight w according to

w <— w + Awj

(e) Setemax «- max(emax, ||d,-||)

5- If fmax > £o return to step 3.

Example. Consider the AND gate, where m = 4. Let

w r = (0.005,0.006), 0=T:, «o = 0.01, 77 = 0.5

with the input pattern

x o = ( 2 ) ' x i = ( S ) ' x 2 = ( ? ) ' x 3 = ( l ) -The desired output is

2/o = 0, j/i = 0, y2 = 0, j/3 = 1 •

The calculations yield

Page 349: Nonlinear Workbook (3rd)

330 CHAPTER 11. NEURAL NETWORKS

!) (a) 2/o = H(wTx0 - 6) = 0 =>• d0 = y0 - y0 = 0 => Aw = 0

(b) 2/1 = H(wTx1 - 6>) = 0 => di = yx - yx = 0 => Aw = 0

(c) j/2 = i / ( w r x 2 - 0) = 0 =^ d2 = y2 - ?/2 = 0 =*> Aw = 0

(d) 2/3 = H(wTx3 - 6») = 0 => d3 = y3 - y3 = 1 => Aw = 7^(1 ,1 ) = (0.5,0.5)

= > w = (0.505,0.506), emax = l

2) (a) 2/0 = # ( w T x 0 -6») = O=^do = O=4>Aw = O

(b) 2/1 = fl"(wTX! -6 ) ) = 0 = ^ d 1 = 0 = > A w = 0

(c) 2/2 = H(wTx2 -e) = 0=>d2 = 0^> Aw = 0

(d) 2/3 = i7(wTx3 - 0) = 0 ^> d3 = 1 =>• Aw = 0.5(1,1) = (0.5,0.5)

= > w = (1.05,1.06)

3) (a) 2/o = H(wTx0 -6>) = 0=>d0 = 0 ^ A w = 0

(b) 2/1 = i / (wTX l -6») = 0=^di = 0=^Aw = 0

(c) 2/2 = H(wTx2 -6)) = 0=^d2 = 0^>Aw = 0

(d) 2/3 = H(wTx3 - 9) = 1 => d3 = 0 => Aw = 0

Thus with wT = (1.05,1.06), 9 = § we can simulate the AND gate. In the extendedspace we have

wT = (w0, wu w2) = {-9, wi,w2), xT = (1, xi, x2).

In the C++ program perceptronAnd. cpp we use the notation of the extended space.Furthermore, the threshold is also initialized to a small random value at t = 0.

// perceptronAnd.cpp

#include <iostream>#include <cmath> // for fabsusing namespace std;

double H(double z)

{if(z >= 0.0) return 1.0;

Page 350: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 331

else return 0.0;}

double scalar(double* u,double* v.int n)

{double result =0.0;for(int i=0;i<n;i++) result += u[i]*v[i];return result;

>

double distance(double* u,double* v.int n)

-fdouble result = 0.0;for(int i=0;i<n;i++) result += f abs(u[i]-v[i]) ;return result;

>

void change(double** x,double* yt,double* w,double eta,int m,int n)

idouble* d = new double[m];for(int j=O;j<m;j++){.

d[j] = yt[j] - H(scalar(w,x[j],n));for(int i=0;i<n;i++) { w[i] += eta*d[j]*x[j] [i]; }}delete [] d;

}

int main(void)•C

/ / number of input vectors (patterns) i s m = 4/ / length of each input vector n = 3int m = 4, n = 3;double** x = new double*[m];f o r d n t k=0;k<m;k++) x[k] = new doubleEn];x[0][0] = 1.0; x[0][ l ] = 0.0; x[0][2] = 0.0;x[ l ] [0] = 1.0; x [ l ] [ l ] = 0.0; x[ l][2] = 1.0;x[2][0] = 1.0; x[2][ l ] = 1.0; x[2] [2] = 0.0;x[3][0] = 1.0; x[3][ l ] = 1.0; x[3] [2] = 1.0;

// desired outputdouble* yt = new double[m];yt[0] =0.0; yt[l] =0.0; yt[2] =0.0; yt[3] = 1.0;

// weight vector

Page 351: Nonlinear Workbook (3rd)

332 CHAPTER 11. NEURAL NETWORKS

II w[0] = - theta (threshold)double* w = new double[n];// initialized to small random numbersw[0] = 0.01; w[l] = 0.005; w[2] = 0.006;

// learning ratedouble eta = 0.5;

double* wt = new double[n];for(int i=0;i<n;i++) wt[i] = w[i] ;

for(;;){change(x,yt,w,eta,m,n);double dist = distance(w,wt,n);if(dist < 0.0001) break;for(i=0;i<n;i++)wt[i] = w[i];}

// display the output of the weight vectorsfor(i=0;i<n;i++)cout « "w[" « i « "] = " « w[i] « " ";

delete[] w; deleted wt;delete [] yt;for(i=0;i<m;i++){ deleted x[i]; }deleted x;return 0;

}

The output is given by

w[0] = -1.49 w[l] = 1.005 w[2] = 0.506

Thus withwo = -9 = -1.49, Wl = 1.005, w2 = 0.506

we can simulate the AND gate.

11.5.7 The XOR Problem and Two-Layered NetworksThe properties of a two-layered network can be discussed using the case of the XORfunction as an example. A single perceptron cannot compute this function, buta two-layered network can. The network in Figure 11.5 is capable of doing this.

Page 352: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 333

The network consists of an input layer, a hidden layer and an output layer andthree computing units. One of the units in the hidden layer computes the functionXi A -<X2, and the other the function -\Xi A x2. The third unit computes the ORfunction, so that the result of the complete network computation is

{x\ A -1X2) V (-iij A £2).

Figure 11.5 A Three-Layered Network for the Computation of XOR

The calculations for the XOR gate are as follows. We work in the extended space.The input vectors are

H s } * - ( : ) • - - ( i ) - *=(!)•1) input layer —> hidden layer. The weights are

wooo = ~0-5 ' wooi = 1-0' woo2 = -1-0

W010 = -0.5, wou = -1.0, W012 = 1-0 •

The weight has three indexes. The first index indicates the layer, in this case 0 forthe input layer. The second index indicates to which node in the hidden layer itpoints where the number for the hidden node is incremented by 1 so that we canassign the index 0 to the bias in the hidden layer. The third index indicates thenumber of the neurons.

Consider the input vector x0

a) H({wm,won,wow.) 0 ) = H(-0.5) = 0 = z0

W

Page 353: Nonlinear Workbook (3rd)

334 CHAPTER 11. NEURAL NETWORKS

b) -ff((woio,woii,woi2) 0 ) = #(-0.5) = 0 = ZlWConsider the input vector x.i

(l\a) #((wOoo, wool, w002) ° ) = #(-1-5) = 0 = z0

wb ) H{{w010, w01l, w012) I 0 ) = ff(+0.5) = 1 = ^

Consider the input vector X2

a) ff((wOoo,wooii W002) 1 ) = H(+0.5) = 1 = z0

b) ff((t«oio,iubii, W012) I 1 ) = ff(-1.5) = 0 = «i

Consider the input vector x3

a) i/"((wooo, W001, W002) 1 ) = H(-0.5) = 0 = z0

b) F((wOoo, w021, W022) 1 ) = ff(-0.5) = 0 = zx

2) hidden layer —> output. From the above calculations for zo and z\ we find thatthe input pairs from the hidden layer are (1,0,0), (1,0,1), (1,1,0) and (1,0,0).Thus the first and the last patterns are the same. The weights are

wioo = -0 -5 , w1Oi = 1.0, Wio2 = l-O.

Consider input pattern (1,0,0) from hidden layer

a) #((wioo,wlol,w1O2) I 0 ) = #(-0.5) = 0W

Consider input pattern (1,0,1) from hidden layer

Page 354: Nonlinear Workbook (3rd)

11.5. PERCEPTRON 335

b) H{(wm,wwl,w102) 0 ) = #(+0.5) = 1

Consider input pattern (1,1,0) from hidden layer

c) H{(wloo,wm,wW2) 1 ) = #(+0.5) = 1

wConsider input pattern (1,0,0) from hidden layer (already considered above)

d) H({wwo,w10uw102) I 0 ) = #(-0.5) = 0.

WThus we have simulated the XOR gate using a hidden layer.

/ / XORl.cppttinclude <iostream>using namespace std;

double H(double s)

iif(s >= 0.0) return 1.0;

else return 0.0;

}

double map(double*** w,double* testpattern.int size3)

iint k;double* z = new d o u b l e [ s i z e 3 ] ;z[0] = 1.0; z [ l ] = 0 .0 ; z [2] = 0 .0 ;

// input layer to hidden layerfor(k=0;k<size3;k++)

{z[l] += w[0] [0] [k]*testpattern[k] ;z[2] += w[0] [1] [k]*testpattern[k] ;>z[l] = H(z[l]);z[2] = H(z[2]);

// hidden layer to output layerdouble y = 0.0;

Page 355: Nonlinear Workbook (3rd)

336 CHAPTER 11. NEURAL NETWORKS

for(k=0;k<size3;k++)y += w[l][0][k]*z[k];

delete [] z;y=H(y) ;return y;

>

int main(void){

int sizel , size2, size3;sizel = 2; size2 = 2; size3 = 3;int i , j ;

double*** w = NULL;w = new double** [sizel];for(i=0;i<sizel;i++){w[i] = new double* [size2] ;for(j=0;j<size2;j++){ w[i][j] = new double[size3] ; >>w[0][0][0] = -0.5; w[0][0][l] = 1.0; w[0] [0] [2] = -1.0;w[0][l][0] = -0.5; w[0][l][l] = -1.0; w[0] [1] [2] = 1.0;w[l][0][0] = -0.5; w[l] [0] [1] = 1.0; w[l][0][2] = 1.0;w[l][l][0] = 0.0; w[l][l][l] = 0.0; w[l] [1] [2] = 0.0;

// input patternsint p = 4; // number of input patternint n = 3; // length of each input patterndouble** x = NULL;x = new double* [p];for ( in t k=O;k<p;k++){ x [k] = new double [n] ; }x[0][0] = 1.0; x[0][ l ] = 0.0; x[0] [2] = 0.0;x[l][0] = 1.0; x [ l ] [ l ] = 0.0; x[l][2] = 1.0;x[2][0] = 1.0; x[2][ l ] = 1.0; x[2][2] = 0.0;x[3][0] = 1.0; x[3][ l ] = 1.0; x[3][2] = 1.0;

double resu l t = map(w,x[0],size3);cout « "result = " « result « endl; // => 0result = map(w,x[l] ,size3);cout « "result = " « result « endl; / / => 1result = map(w,x[2] ,size3);cout « "result = " « result « endl; / / => 1

Page 356: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 337

resul t = map(w,x[3],size3);cout « "result = " « result « endl; // => 0return 0;

>

11.6 Multilayer Perceptrons

11.6.1 Introduction

In a practical application of the back-propagation algorithm, learning results fromthe many presentations of a prescribed set of training examples to the multilayerperceptron. One complete presentation of the entire training set during the learningprocess is called an epoch. The learning process is maintained on an epoch-by-epochbasis until the synaptic weights and threshold levels of the network stabilize and theaverage squared error over the entire training set converges to some minimum value.It is good practice to randomize the order of presentation of training examples fromone epoch to the next. This randomization tends to make the search in weight spacestochastic over the learning cycles, thus avoiding the possibility of limit cycles in theevolution of the synaptic weight vectors. We follow in our notation closely Hassoun[43]. For a given training set, back-propagation learning may thus proceed in one oftwo basic ways.

Let

be the training data, where k = 0, l , . . . ,m— 1. Here m is the number of trainingexamples (patterns). The sets x/t (k = 0 , 1 , . . . , m— 1) are the input pattern and thesets dfc are the corresponding (desired) output pattern. One complete presentationof the entire training set during the learning process is called an epoch.

1. Pattern Mode. In the pattern mode of back-propagation learning, weight updat-ing is performed after the presentation of each training example; this is the modeof operation for which the derivation of the back-propagation algorithm presentedhere applies. To be specific, consider an epoch consisting of m training examples(patterns) arranged in the order

xo,do, xi ,di , . . . , xm_i ,dm_i .

The first example Xo, do in the epoch is presented to the network, and the sequenceof forward and backward computations described below is performed, resulting incertain adjustments to the synaptic weights and threshold levels of the network.Then, the second example xi, di in the epoch is presented, and the sequence offorward and backward computations is repeated, resulting in further adjustmentsto the synaptic weights and threshold levels. This process is continued until the lasttraining pattern xm_j, dTO_i is taken into account.

Page 357: Nonlinear Workbook (3rd)

338 CHAPTER 11. NEURAL NETWORKS

2. Batch Mode. In the batch mode of back-propagation learning, weight updatingis performed after the presentation of all the training examples that constitute anepoch.

11.6.2 Cybenko's Theorem

Single-hidden-layer neural networks are universal approximators. A rigorous math-ematical proof for the universality of feedforward layered neural networks employingcontinuous sigmoid type activation functions, as well as other more general activa-tion units, was given by Cybenko [21]. Cybenko's proof is based on the Hahn-Banachtheorem. The following is the statement of Cybenko's theorem.

Theorem. Let / be any continuous sigmoid-type function, for example

/(S) = l / ( l + exp(-AS)), A > 1 .

Then, given any continuous real-valued function g on [0,1]" (or any other compactsubset of Rn) and e > 0, there exists vectors wi, w 2 , . . . , w^, a, and 6 and aparameterized function

G(-,w,a,0) : [0, l f - > R

such that|G(x ,w,a ,0 ) - f f (x ) |<e for all x G [0, I f

where

G(x)w,a,0) = £>,-/(wJ'x + 0J-)3=1

andWj € Rn, 0j G R, w=(w 1 ,w 2 , . . . , wN)

a = (aua2,...,aN), 9 = {6u02, • • • ,0N).

For the proof we refer to the paper by Cybenko [21].

Thus a one-hidden layer feedforward neural networks is capable of approximatinguniformly any continuous multivariate function to any desired degree of accuracy.This implies that any failure of a function mapping by a multilayer network mustarise from inadequate choice of parameters, i.e., poor choices for Wi, W2,..., w^, a,and 0 or an insufficient number of hidden nodes.

Hornik et al. [48] employing the Stone-Weierstrass theorem and Funahashi [33]proved similar theorems stating that a one-hidden-layer feedforward neural networkis capable of approximating uniformly any continuous multivariate function to anydesired degree of accuracy.

Page 358: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 339

11.6.3 Back-Propagation AlgorithmWe consider one hidden layer. The notations we use follow closely Hassoun [43].Thus we consider a two-layer feedforward architecture. This network receives a setof scalar signals

XQ, XI, X2,-.- ,Xn-i

where x0 is a bias signal set to 1. This set of signals constitutes an input vectorxfc G R". The layer receiving this input signal is called the hidden layer. The hiddenlayer has J units. The output of the hidden layer is a J dimensional real-valuedvector Zfc = (ZQ,ZI, ... ,zj_i), where we set Zo = 1 (bias signal). The vector z*supplies the input for the output layer of L units. The output layer generates anL-dimensional vector y> in response to the input vector x* which, when the networkis fully trained, should be identical (or very close) to the desired output vector d*associated with x^.

The two activation functions /^ (input layer to hidden layer) and f0 (hidden layerto output layer) are assumed to be differentiable functions. We use the logisticfunctions

A ( 5 ) = l + expt-Ak*)1 fo{s) = l + exp(-AoS)where A>,,A0 > 1. The logistic function

/ ( S ) = 1 + exp(-As)

satisfies the nonlinear differential equation

!-w-/>.The components of the desired output vector d* must be chosen within the rangeof f0. We denote by Wji the weight of the jth hidden unit associated with the inputsignal Xi. Thus the index i runs from 0 to n — 1, where :to = 1 and j runs from 1 toJ — 1. We set Woi = 0. Now we have m input/output pairs of vectors

{ xfc , dk }

where the index k runs from 0 to m — 1. The aim of the algorithm is to adap-tively adjust the (J — l)n + LJ weights of the network such that the underlyingfunction/mapping represented by the training set is approximated or learned. Letwij be the weights from the hidden layer to the output and let Wji be the weightsfrom the input layer to the hidden layer. We can define an error function since thelearning is supervised, i.e. the target outputs are available. We denote by Wij theweight of the Ith output unit associated with the input signal Zj from the hiddenlayer. We derive a supervised learning rule for adjusting the weights Wji and wijsuch that the error function

E(w)=l-j:(dl-yl)2

z i=o

Page 359: Nonlinear Workbook (3rd)

340 CHAPTER 11. NEURAL NETWORKS

is minimized (in a local sense) over the training set. Here w represents the set ofall weights in the network.

Since the targets for the output units are given, we can use the delta rule directlyfor updating the wy weights. We define

SincedE

Awij = -f}0-

dwtj

we find using the chain ruleAwij = f]o(di - yiif'oinetfizj

where I = 0 , 1 , . . . , L - 1 and j = 0 , 1 , . . . , J - 1. Here

J-I

neti := ^ w^Zjj=o

is the weighted sum for the Ith output unit, f'o is the derivative of f0 with respect tonet, and wf^w and wfj are the updated (new) and current weight values, respectively.The Zj values are calculated by propagating the input vector x through the hiddenlayer according to

/n-l \zi = h [Y, wJixi = fh(netj)

\i=0 /

where j = 1,2,..., J — 1 and ZQ = 1 (bias signal). For the hidden-layer weights Wj,we do not have a set of target values (desired outputs) for hidden units. However, wecan derive the learning rule for hidden units by attempting to minimize the output-layer error. This amounts to propagating the output errors (di — yi) back throughthe output layer toward the hidden units in an attempt to estimate dynamic targetsfor these units. Thus a gradient descent is performed on the criterion function

£(w)=4$>'-w)2z (=o

where w represents the set of all weights in the network. The gradient is calculatedwith respect to the hidden weights

FIT?Awji = - r ) h - j ^ , j = l , 2 , . . . , J - l , i = 0 , l , . . . , n - l

where the partial derivative is to be evaluated at the current weight values. We find

dE _ dE dzj dnetjdwji dzj dnetj dwji

Page 360: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 341

wherednetj _ dzj ,

~d^~ ~ Xu a^Tj = fh{netj> •

We used the chain rule in this derivation. Since

o 771 L— 1

^ 7 = - X)(d ' - ydf'oinetijwijozi 1=0

we obtain/L- l \

Awjj = % £ ( d | - yi)f'0(neti)wij f'h(netj)xi •\i=o )

Now we can define an estimated target dj for the jth hidden unit implicitly in termsof the backpropagated error signal as follows

i-idi " ZJ '•= Y^(di - yi)I'o(neti)wlj.

1=0

The complete approach for updating weights in a feedforward neural net utilizingthese rules can be summarized as follows. We do a pattern-by-pattern updating ofthe weights.

1. Initialization. Initialize all weights to small random values and refer to them ascurrent weights wfj and io£.

2. Learning rate. Set the learning rates r\o and r\h to small positive values.

3. Presentation of training example. Select an input pattern x* from the training set(preferably at random) propagate it through the network, thus generating hidden-and output-unit activities based on the current weight settings. Thus find Zj andVi-

4. Forward computation. Use the desired target vector d* associated with x*, andemploy

Aray = r]0{di - yi)f'(neti)zj = rjo{di - yi)\of{neti)(l - f{net{])zj

to compute the output layer weight changes Awy.

5. Backward computation. Use

/L-l \Awji = Vh\^Z(di- yi)f'0{neti)wij f[l{netj)xl

\i=o /or

/L-l \Awji = Vh[^2(di- yi)\ofo(neti)(l - fo(neti))wl:i \hfh{netj){l - fh(netj))xi

\i=o 1

Page 361: Nonlinear Workbook (3rd)

342 CHAPTER 11. NEURAL NETWORKS

to compute the hidden layer weight changes. The current weights are used in thesecomputations. In general, enhanced error correction may be achieved if one employsthe updated output-layer weights

w?™ = weu + AWlj.

However, this comes at the added cost of recomputing y\ and /'(net;).

6. Update weights. Update all weights according to

w?r = w°i+AWji

and

for the output and for the hidden layers, respectively.

7. Test for convergence. This is done by checking the output error function to seeif its magnitude is below some given threshold. Iterate the computation by pre-senting new epochs of training examples to the network until the free parameters ofthe network stabilize their values. The order of presentation of training examplesshould be randomized from epoch to epoch. The learning rate parameter is typi-cally adjusted (and usually decreased) as the number of training iterations increases.

In the C++ program we apply the back-propagation algorithm to the XOR problem,where m = 4 is the number of input vectors each of length 3 (includes the bias input).The number of hidden layer units is 3 which includes the bias input ZQ = 1. Bymodifying m, n, J and L the program can easily be adapted to other problems.

// backpropagationl.cpp

#include <iostream>#include <cmath> // for expusing namespace std;

// activation function (input layer -> hidden layer)double fh(double net)

idouble lambdah = 10.0;

return 1.0/(1.0 + exp(-lambdah*net));

>

// activation function (hidden layer -> output layer)

double fo(double net)

{double lambdao = 10.0;return 1.0/(1.0 + exp(-lambdao*net));

Page 362: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 343

>

double scalar(double* al.double* a2,int length)

idouble result = 0.0;for(int i=O;i<length;i++) { result += al[i]*a2[i]; }return result;

}

int mainO

•C

int k, i, j, 1; // summation index// k runs over all input pattern k = 0, 1, .. , m-1// 1 runs over all output units 1 = 0 , 1, .. , L-l// j runs over all the hidden layer units j = 0, 1, .. , J-l// i runs over the length of the input vector i = 0, 1, .. , n-1

// learning rates

double etao = 0.05, etah = 0.05;

double lambdao = 10.0, lambdah =10.0;

// memory allocations

double** x = NULL;int m = 4; / / number of input vectors for XOR problemint n = 3; / / length of each input vector for XOR problem/ / input vectorsx = new double* [m];for(k=0;k<m;k++) x[k] = new double [n];x[0][0] = 1.0; x[0][l] = 0.0; x[0][2] = 0.0;x[l][0] = 1.0; x[l][ l] = 0.0; x[l][2] = 1.0;x[2][0] = 1.0; x[2][l] = 1.0; x[2] [2] = 0.0;x[3][0] = 1.0; x[3][l] = 1.0; x[3] [2] = 1.0;

// desired output vectors// corresponding to set of input vectors xdouble** d = NULL;// number of outputs for XOR problemint L = 1;d = new d o u b l e * [m] ;

f o r ( k = 0 ; k < m ; k + + ) d [ k ] = new d o u b l e [L] ;

d [ 0 ] [ 0 ] = 0 . 0 ; d [ l ] [ 0 ] = 1 .0 ; d [ 2 ] [0] = 1 . 0 ; d [ 3 ] [0] = 0 . 0 ;

// error function for each input vector

Page 363: Nonlinear Workbook (3rd)

344 CHAPTER 11. NEURAL NETWORKS

double* E = new double[m];

double totalE = 0.0; / / sum of E[k] k = 0, 1, . . , m

/ / weight matrix (input layer -> hidden layer) ;

/ / number of hidden layers includes 0/ / currentint J = 3;double** We = NULL;We = new double* [ J ] ;for(j=0;j<J;j++) Wc[j] = new doubleM;

Wc[0][0] = 0.0; Wc[O][l] = 0.0; We [0] [2] = 0.0;Wc[l][0] = -0 .2 ; Wc[l][l] = 0.5; Wc[l][2] = -0 .5 ;Wc[2][0] = - 0 . 3 ; Wc[2][l] = - 0 . 3 ; We [2] [2] = 0.7;

// newdouble** Wnew = NULL;Wnew = new double* [J];for(j=0;j<J;j++) Wnew[j] = new double[n];

// weight matrix (hidden layer -> output layer)// currentdouble** Whc = NULL;Whc = new double* [L];for(l=0;KL;l++) Whc[l] = new doubled];

Whc[0][0] = -0.2; Whc[0][l] = 0.3; Whc[0][2] = 0.5;

// newdouble** Whnew = NULL; Whnew = new double*[L];for(l=0;KL;l++) Whnew[l] = new double [J];

// vector in hidden layer

double* z = new double[J];

z[0] = 1.0;

// vector output layer (output layer units)

// for the XOR problem the output layer has only one elementdouble* y = new double[L];

// increment matrix (input layer -> hidden layer)double** delW = NULL; delW = new double* [J];for(j=0;j<J;j++) delW[j] = new double[n];

Page 364: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 345

// increment matrix (hidden layer -> output layer)double** delWh = NULL; delWh = new double* [L];for(l=0;KL;l++) delWh[l] = new doubled];

// net vector (input layer -> hidden layer)double* netj = new double[J];netj[0] = 0.0;

// net vector (hidden layer -> output layer)

double* netl = new double[L];

// training session

int T = 10000; // number of iterationsfor(int t=0;t<T;t++)

{// for loop over all input patternfor(k=0;k<m;k++)

{for(j=l;j<J;j++)

{

netj[j] = scalar(x[k] ,Wc[j] ,n);z[j] = fh(netj[j]);}

for(l=0;KL;l++)-Cnetl[l] = scalar(z,Whc[l] ,J);y[l] = fo(netl[l]);

}

for(l=0;KL;l++)for(j=0;j<J;j++)delWh[l][j] =etao* (d [k] [1] -y [1] ) *lambdao*f o (netl [1] ) * (1.0-f o (netl [1] ) ) *z [j] ;

double* temp = new double [J];for(j=0;j<J;j++)temp[j] = 0.0;

for(j=0;j<J;j++)for(l=0;KL;l++)temp[j] +=(d[k] [1] -y [1]) *f o (netl [1]) * (1.0-f o (netl [1])) *Whc [1] [j] ;

for(j=0;j<J;j++)

Page 365: Nonlinear Workbook (3rd)

346 CHAPTER 11. NEURAL NETWORKS

for(i=0;i<n;i++)delWCj] [i] =etah*temp[j]*lambdah*fh(netj[j])*(1.0-fh(netj[j]))*x[k][i];

for(i=0;i<n;i++)delW[O][i] = 0 . 0 ;

// updating the weight matricesfor(j=0;j<J;j++)for(i=0;i<n;i++)Wnew[j][i] = Wc[j][i] + delW[j] [ i ] ;

for(l=0;KL;l++)for(j=0;j<J;j++)Whnew[l][j] = Whc[l][j] + delWh[l] [j] ;

/ / setting new to currentfor(j=0;j<J;j++)for(i=0;i<n;i++)Wc[j][i] = Wnew[j][i];

for(l=0;KL;l++)for(j=0;j<J;j++)Whc[l][j] = Whnew[l][j];

E[k] = 0.0;double sum = 0.0;for(l=0;KL;l++)sum += (d[k][l]-y[l])*(d[k][l]-y[l]);

E[k] = sum/2.0;totalE += E[k] ;} // end for loop over all input patternif(totalE < 0.0005) goto Label;else totalE = 0.0;} // end training session

Label:cout « "number of iterations = " « t « endl;

// output after trainingfor(j=0;j<J;j++)for(i=0;i<n;i++)

cout « "Wc[" « j « "] [" « i « "] = "« Wc[j][i] « endl;

Page 366: Nonlinear Workbook (3rd)

11.6. MULTILAYER PERCEPTRONS 347

cout « endl;

for(l=0;KL;l++)for(j=0;j<J;j++)cout « "Whc[" « 1 « "] [" « j « "] = "

« Whc[l] [j] « endl;

// testing the XOR gate

// input (1,0,0)for(j=l;j<J;j++)

{

netj[j] = scalar (x [0] , We [j] ,n) ;z[j] = fh(netj[j]);}

for(l=0;KL;l++){netl[l] = scalar(z,Whc[l],J);y[l] = fo(netl[l]);cout « "y[" « 1 « "] = " « y[l] « endl;}

/ / input (1,0,1)for(j=l;j<J;j++){netjCj] = scalar(x[l],Wc[j],n);z[j] = fh(netj[j]);>

for(l=0;KL;l++){netl[l] = scalar(z,Whc[l],J);y[l] = fo(netl[l]);cout « "y[" « 1 « "] = " « y[l] « endl;}

/ / input (1,1,0)for(j=l;j<J;j++){netj[j] = scalar (x [2] ,Wc[j] ,n);z[j] = fh(netj[j]);>

for(l=0;KL;l++)

Page 367: Nonlinear Workbook (3rd)

348 CHAPTER 11. NEURAL NETWORKS

netl [1] = scalar (z, Whc [1] , J) ;y[l] = fo(netl[l]);cout « "y[" « 1 « "] = " « y[l] « endl;}

// input (1,1,1)for(j=l;j<J;j++){netj[j] = scalar(x[3] ,Wc[j] ,n) ;z[j] = fh(netj[j]);>for(l=0;KL;l++){netl[l] = scalar (z, Whc [1],J);y[0] = fo(netl[l]);cout « "y[" « 1 « "] = " « y[l] « endl;}return 0;

}

11.7 Chaotic Neural Networks

In designing a neural network, it is usually of prime importance to guarantee theconvergence of dynamics for the corresponding system. On the other hand, richerdynamics provide wider applications. For example, transient chaotic behavioursprovide higher searching ability for globally optimal or near optimal solutions, inusing neural network models as an approximation method for the combinatorialoptimization problems. A chaotic neural network can be constructed with chaoticneurons by considering the spatio-temporal summation of both external inputs andfeedback inputs from other chaotic neurons ([14]). For example

yi{t + 1) = kyi(t) + a | Yl WijXjit) +lA- Zi(t)(Xi(t) - Jo)

zi(t + l) = {l-P)zi{t)

where i = 1, 2 , . . . , N, t = 0 ,1 , . . . , and

Xi(i) output of neuron i (0 < Xi < 1)yi(t) internal state of neuron i

Zi{t) self-feedback connection weight (zi > 0)

with

Page 368: Nonlinear Workbook (3rd)

11.7. CHAOTIC NEURAL NETWORKS 349

Wij connection weight between neuron i and j

A slope parameter of sigmoid functionk decay parameter of y{ (0 < k < 1)a positive scaling parameter for inputsfj decay parameter of z* (0 < /3 < 1)It external input to neuron i

Io positive constant

We assume that a neuron does not receive a feedback from itself, i.e. Wjj = 0. Thetransiently chaotic neural network is characterized as follows. At the initial state,the variable Zj (0) is so large that a network state is chaotic and the network searchesa global solution. Then Zj(t) is gradually decreased with time. The network is alsogradually changed from a chaotic state to a steady state. An optimization process ofthe transiently chaotic neural network is regarded as a chaotic simulated annealing.The network can be used to find solutions of the traveling salesman problem.

Another chaotic neural network considered in the literature ([92], [7]) is given by

Si(t + 1) = / ( £ JijSjit) J , t = 1,2,..., JV

withf{x) := t&nh{ax)e-fix2

where a and j3 are positive constants and t — 0 ,1 , . . . . Depending on the matrixelements Jy and the parameters a and j5, the system can be chaotic or nonchaotic.For example, for N — 4, a = 3, /? = 2, and

(0.01 1.50 0.03 0.01 \

1.60 0.01 0.10 1.000.03 0.02 0.01 1.500.00 1.00 1.70 0.01/

the system is hyperchaotic. This chaotic neural network can be used for patternrecognition using the fixed points of the map as the patterns themselves.

Page 369: Nonlinear Workbook (3rd)

Chapter 12

Genetic Algorithms

12.1 Introduction

Evolutionary methods have gained considerable popularity as general-purpose ro-bust optimization and search techniques. The failure of traditional optimizationtechniques in searching complex, uncharted and vast-payoff landscapes riddled withmultimodality and complex constraints has generated interest in alternate approaches.

The interest in heuristic search algorithms with underpinnings in natural and phys-ical processes began as early as the 1970s. Simulated annealing is based on thermo-dynamic considerations, with annealing interpreted as an optimization procedure.Evolutionary methods draw inspiration from the natural search and selection pro-cesses leading to the survival of the fittest. Simulated annealing and evolutionarymethods use a probabilistic search mechanism to locate the global optimum solutionin a multimodal landscape.

Genetic algorithms (Holland [46], Goldberg [34], Michalewicz [69]) are self-adaptingstrategies for searching, based on the random exploration of the solution space cou-pled with a memory component which enables the algorithms to learn the optimalsearch path from experience. They are the most prominent, widely used representa-tives of evolutionary algorithms, a class of probabilistic search algorithms based onthe model of organic evolution. The starting point of all evolutionary algorithms isthe population (also called farm) of individuals (also called animals, chromosomes,strings). The individuals are composed of genes which may take on a number ofvalues (in most cases 0 and 1) called alleles. This means the value of a gene is calledits allelic value, and it ranges on a set that is usually restricted to {0,1}. Thus theseindividuals are represented as binary strings of fixed length, for example

"10001011101"

350

Page 370: Nonlinear Workbook (3rd)

12.2. THE SEQUENTIAL GENETIC ALGORITHM 351

If the binary string has length N, then 2N binary strings can be formed. Each ofthe individuals represents a search point in the space of potential solutions to agiven optimization problem. Then random operators model selection, reproduction,crossover and mutation. The optimization problem gives quality information (fitnessfunction or short fitness) for the individuals and the selection process favours indi-viduals of higher fitness to transfer their information (string) to the next generation.The fitness of each string is the corresponding function value. Genetic algorithmsare specifically designed to treat problems involving large search spaces containingmultiple local minima. The algorithms have been applied to a large number ofoptimization problems. Examples are solutions of ordinary differential equations,the smooth genetic algorithm, genetic algorithms in coding theory, Markov chainanalysis, the DNA molecule.

In the fundamental approach to finding an optimal solution, a fitness function (alsocalled cost function) is used to represent the quality of the solution. The objectivefunction to be optimized can be viewed as a multidimensional surface where theheight of a point on the surface gives the value of the function at that point. Incase of a minimization problem, the wells represent high-quality solutions while thepeaks represent low-quality solutions. In case of a maximization problem, the higherthe point in the topography, the better is the solution.

The search techniques can be classified into three basic categories.

(1) Classical or calculus-based. This uses a deterministic approach to find the bestsolution. This method requires the knowledge of the gradient or higher-orderderivatives. The technique can be applied to well-behaved problems.

(2) Enumerative. With these methods, all possible solutions are generated andtested to find the optimal solution. This requires excessive computation inproblems involving a large number of variables.

(3) Random. Guided random search methods are enumerative in nature; how-ever, they use additional information to guide the search process. Simulatedannealing and evolutionary algorithms are typical examples of this class ofsearch methods.

12.2 The Sequential Genetic Algorithm

The genetic algorithm evolves a multiset of elements called a population of indi-viduals or farm of animals. Each individual At (i = 1, . . . , n) of the population Arepresents a trial solution of the optimalization problem to be solved. Individualsare usually represented by strings of variables, each element of which is called agene. The value of a gene is called its allelic value, and it ranges on a set that isusually restricted to {0,1}.

Page 371: Nonlinear Workbook (3rd)

352 CHAPTER 12. GENETIC ALGORITHMS

The population of individuals is also called a farm of animals in the literature. Fur-thermore an individual or animal is also called a chromosome or string.

A genetic algorithm is capable of maximizing a given fitness function / computedon each individual of the population. If the problem is to minimize a given objec-tive function, then it is required to map increasing objective function values intodecreasing / values. This can be achieved by a monotonically decreasing function.The standard genetic algorithm is the following sequence:

Step 1. Randomly generate an initial population A(0) := {Ai(0), •• •, An(0))

Step 2. Compute the fitness f(Ai(t)) of each individual Ai(t) of the current popu-lation A(i)

Step 3. Generate an intermediate population Ar(t) by applying the reproductionoperator

Step 4. Generate A(£ + 1) by applying some other operators to Ar(t)

Step 5: t := t + 1 if not (end-test) goto Step 2.

The most commonly used operators are the following:

1) Reproduction (selection). This operator produces a new population, Ar(t), ex-tracting with repetition individuals from the old population, A(<). The extractioncan be carried out in several ways. One of the most commonly used method is theroulette wheel selection, where the extraction probability pr(Ai(t)) of each individ-ual Ai(t) is proportional to its fitness f(Ai(t)).

2) Crossover. This operator is applied in probability, where the crossover probabilityis a system parameter, pc. To apply the standard crossover operator (several vari-ations have been proposed) the individuals of the population are randomly paired.Each pair is then recombined, choosing one point in accordance with a uniformlydistributed probability over the length of the individual strings (parents) and cut-ting them in two parts accordingly. The new individuals (offspring) are formed bythe juxtaposition of the first part of one parent and the last part of the other parent.

3) Mutation. The standard mutation operator modifies each allele of each individualof the population in probability, where the mutation probability is a system param-eter, pm. Usually, the new allelic value is randomly chosen with uniform probabilitydistribution.

4) Local search. The necessity of this operator for optimization problems is still un-der debate. Local search is usually a simple gradient-descent heuristic search thatcarries each solution to a local optimum. The idea behind this is that search in the

Page 372: Nonlinear Workbook (3rd)

12.2. THE SEQUENTIAL GENETIC ALGORITHM 353

space of local optima is much more effective than search in the whole solution space.

The purpose of parent selection (also called setting up the farm of animals) in agenetic algorithm is to give more reproductive chances, on the whole, to those pop-ulation members that are the most fit. We use a binary string as a chromosome torepresent real value of the variable x. The length of the binary string depends onthe required precision. A population or farm could look like

"10101110011111110""00111101010100001"

"11111110101010111" <- individual (chromosome, animal, string)

"10101110010000110"

For the crossover operation the individuals of the population are randomly paired.Each pair is then recombined, choosing one point in accordance with a uniformlydistributed probability over the length of the individual strings (parents) and cuttingthem in two parts, accordingly. The new individuals (offspring) are formed by thepart of one part and the last part of the other. An example is

1011011000100101 parent0010110110110111 parent

I I1011010110110101 child0010111000100111 child

The mutation operator modifies each allele (a bit in the bitstring) of each individualof the population in probability. The new allele value is randomly chosen withuniform probability distribution. An example is

1011011001011001 parent

I1011111001011001 child

The bit position is randomly selected. Whether the child is selected is decided bythe fitness function.

We have to map the binary string into a real number x with a given interval [a, b](a < b). The length of the binary string depends on the required precision. Thetotal length of the interval is b — a. The binary string is denoted by

SN-iSN-2- • -S\S0

where s0 is the least significant bit (LSB) and s^-i is the most significant bit (MSB).In the first step we convert from base 2 to base 10

N-l

m=Y; ^ •i=0

Page 373: Nonlinear Workbook (3rd)

354 CHAPTER 12. GENETIC ALGORITHMS

In the second step we calculate the corresponding real number on the interval [a, b]

b- a

Obviously if the bit string is given by "000. . .00" we obtain x = a and if the bit-string is given by "111. . .11" we obtain x — b.

In higher dimensions we proceed as follows. We consider the two-dimensionalcase. The extension to higher dimensions is straightforward. Consider the two-dimensional domain

[a,b] x [c,d], a < b, c < d

which is a subset of R.2. The coordinates are X\ and x2, i.e. X\ 6 [a, b] and x2 6 [c, d].Given a bitstring

of length N = NX + N2. The block

SNI-1SNI-2 • • • SiS0

is identified with mi, i.e.Ni-i

mi = J2 si2i

and therefore

The blockSN-lsN-2 • • -Sffi

is identified with the variable mi, i.e.

A T - l

m2 = £ Si?-*i=Ni

and therefored — c

X2 = c + m2^-jwhere N2 = N - Ni.

Example. In the one-dimensional case consider the binary string 10101101 oflength 8 and the interval [-1,1]. Therefore

m = 1 • 2° + 1 • 22 + 1 • 23 + 1 • 25 + 1 • 27 = 173.

Thus

^ = - 1 + 1732563T = °-357-Example. In the two-dimensional case consider the binary string 0000000000000000with Ni = N2 - 8 and the domain [-1,1] x [-1,1]. Then we find mx = m2 = 0,Xi = —1 and x2 = —1.

Page 374: Nonlinear Workbook (3rd)

12.3. SCHEMATA THEOREM 355

12.3 Schemata Theorem

A schema (Holland [46],Goldberg [34]) is a similarity template describing a subset ofstrings with similarities at certain string positions. We consider the binary alphabet{0,1}. We introduce a schema by appending a special symbol to this alphabet. Weadd the * or don't care symbol which matches either 0 or 1 at a particular position.With this extended alphabet we can now create strings (schemata) over the ternaryalphabet

{ 0, 1, * } .

A schema matches a particular string if at every location in the schema 1 matchesa 1 in the string, a 0 matches a 0, or a * matches either. As an example, considerthe strings and schemata of length 5. The schema *1O1* describes a subset withfour members

01010, 01011, 11010, 11011

We consider a population of individuals (strings) A,, j = 1,2,... ,n contained inthe population A(t) at time (or generation) t (t — 0,1, 2,...) where the boldfaceis used to denote a population. Besides notation to describe populations, strings,bit positions, and alleles, we need a convenient notation to describe the schematacontained in individual strings and populations. Let us consider a schema H takenfrom the three-letter alphabet

V:={0,l,*}.

For alphabets of cardinality k, there are (k + 1)' schemata, where I is the length ofthe string. Furthermore, recall that in a string population with n members thereare at most n • 2l schemata contained in a population because each string is itself arepresentative of 2! schemata. These counting arguments give us some feel for themagnitude of information being processed by genetic algorithms.

All schemata are not created equal. Some are more specific than others. The schema011*1** is a more definite statement about important similarity than the schema0******. Furthermore, certain schemata span more of the total string length thanothers. The schema 1****1* spans a larger portion of the string than the schema1*1****. To quantify these ideas, two schema properties are introduced: schemaorder and defining length.

Definition. The order of a schema H, denoted by o(H), is the number of fixedpositions (in a binary alphabet, the number of l's and 0's) present in the template.

Example. The order of the schema 011*1** is 4, whereas the order of the schema0****** is 1.

Page 375: Nonlinear Workbook (3rd)

356 CHAPTER 12. GENETIC ALGORITHMS

Definition. The defining length of a schema H, denoted by 6(H), is the distancebetween the first and last specific string position.

Example. The schema 011*1** has defining length (5 = 4 because the last specificposition is 5 and the first specific position is 1. Thus S(H) = 5 - 1 = 4.

Schemata provide the basic means for analyzing the net effect of reproduction andgenetic operators on building blocks contained within the population. Let us con-sider the individual and combined effects of reproduction, crossover, and mutationon schemata contained within a population of strings. Suppose at a given time stept there are m(H, t) examples of a particular schema H contained within the popu-lation A(t). During reproduction, a string is copied according to its fitness, or moreprecisely a string Ai gets selected with probability

ftPi v^n f •

2^=1 Jj

After picking a non-overlapping population of size n with replacement from thepopulation A(i), we expect to have m(H, t+1) representatives of the schema H inthe population at time t+1 as given by

* + .! = f f f lwhere f(H) is the average fitness of the strings representing schema H at time t.The average fitness of the entire population is defined as

1 n

Thus we can write the reproductive schema growth equation as follows

m(H,t+l) = m(H,t)^-.

Assuming that f(H)/f remains relatively constant for t = 0 ,1 , . . . , the precedingequation is a linear difference equation x(t + 1) = ax(t) with constant coefficientwhich has the solution x(t) = alx(Q). A particular schema grows as the ratio of theaverage fitness of the schema to the average fitness of the population. Schemata withfitness values above the population average will receive an increasing number of sam-ples in the next generation, while schemata with fitness values below the populationaverage will receive a decreasing number of samples. This behaviour is carried outwith every schema H contained in a particular population A in parallel. In otherwords, all the schemata in a population grow or decay according to their schemaaverages under the operation of reproduction alone. Above-average schemata growand below-average schemata die off. Suppose we assume that a particular schema

Page 376: Nonlinear Workbook (3rd)

12.4. BITWISE OPERATIONS 357

H remains an amount cf above average with c a constant. Under this assumptionwe find

m(H,t + l) = m(H,t)^+;Cf' = (1 + c)m(H,t).

Starting at t = 0 and assuming a stationary value of c, we obtain the equation

m{H,t)=m{H,0){l + c)t.

This is a geometric progression or the discrete analog of an exponential form. Repro-duction allocates exponentially increasing (decreasing) numbers of trials to above-(below-) average schemata. The fundamental theorem of genetic algorithms is asfollows (Goldberg [34]).

Theorem. By using the selection, crossover, and mutation of the standard geneticalgorithm, then short, low-order, and above average schemata receive exponentiallyincreasing trials in subsequent populations.

The short, low-order, and above average schemata are called building blocks, andthe fundamental theorem indicates that building blocks are expected to dominatethe population. Is this good or bad in terms of the original goal of function op-timization ? The preceding theorem does not answer this question. Rather, theconnection between the fundamental theorem and the observed optimizing proper-ties of the genetic algorithm is provided by the following conjecture.

The Building Block Hypothesis. The globally optimal strings in ft

/ : ft -> R with ft = { 0,1}"

may be partitioned into substrings that are given by the bits of the fixed positionsof building blocks.

12.4 Bitwise Operations

In genetic algorithms bitwise operations play the central role. In this section wedescribe these operations. The basic bit operations se tbi t , clearbit , swapbitand t e s t b i t can be implemented in C++ as follows. The bit position b runs from 0to 31 starting counting from right to left in the bit string since we use the datatypeunsigned long. In C, C++, and Java the bitwise operators are:

& bitwise ANDI bitwise OR (inclusive OR)" bitwise XOR (exclusive OR)" NOT operator (one's complement)» right-shift operator« left-shift operator

Page 377: Nonlinear Workbook (3rd)

358 CHAPTER 12. GENETIC ALGORITHMS

The operation se tb i t sets a bit at a given position b (i.e the bit at the position b isset to 1).

unsigned long b = 3;unsigned long x = 15;

x |= (1 « b); // shortcut for x = x I (1 « b);

The operation clearbi t clears a bit at a given position b (i.e. the bit at the positionb is set to 0).

unsigned long b = 3;unsigned long x = 15;x &= "(1 « b); // short cut for x = x & "(1 « b);

The operation swapbit swaps the bit at the position b, i.e., if the bit is 0 it is setto 1 and if the bit is 1 it is set to 0.

unsigned long b = 3;unsigned long x = 15;x "= (1 « b); // short cut for x = x ~ (1 « b);

The operation t e s t b i t returns 1 or 0 depending on whether the bit at the positionb is set or not.

unsigned long b = 3;unsigned long x = 15;unsigned long result = ((x & (1 « b)) != 0);

The operations setbi t , clearbit , swapbit and t e s t b i t are written as functions.This leads to the following C++ program. Obviously, for s e tb i t ( ) , c learbi t ( )and swapbit () we pass x by reference.

// mysetbit.cpp

#include <iostream>using namespace std;

inline void setbit(unsigned longfe x,unsigned long b){ x 1= (1 « b); }

inline void clearbit(unsigned longfc x,unsigned long b){ x &= "(1 « b); >

inline void swapbit(unsigned longfc x,unsigned long b){ x ~= (1 « b); }

Page 378: Nonlinear Workbook (3rd)

12.4. BITWISE OPERATIONS 359

inline unsigned long testbit(unsigned long x,unsigned long b){ return ((x & (1 « b)) != 0); >

int main(void){unsigned long b = 3;unsigned long x = 10; // binary 1010setbit(x.b);cout « "x = " « x « endl; // 10 => binary 1010clearbit(x,b);cout « "x = " « x « endl; // 2 => binary 10swapbit(x.b);cout « "x = " « x « endl; / / 10 => binary 1010unsigned long r = testbit(x.b);cout « "r = " « r « endl; / / 1return 0;

>

Java has a BitSet class. The constructor BitSetO creates a new bit set. Theconstructor BitSet (int nbits) creates a bit set whose initial size is the specifiednumber of bits. The methods are

void and(BitSet set)

performs a logical AND

void andNot(BitSet set)

clears all of the bits in this BitSet whose corresponding bit is set in the specifiedBitSet

void clear(int bitlndex)

the bit with index bitlndex in this BitSet is changed to the clear (false) state

boolean get(int bitlndex)

returns the value of the bit with the specified index

void or(Bitset set)

performs a logical OR of this bit set with the bit set argument

void xor(BitSet set)

Page 379: Nonlinear Workbook (3rd)

360 CHAPTER 12. GENETIC ALGORITHMS

performs a logical XOR of this bit set with the bit set argument.

The Standard Template Library in C++ provides a bi tset class. It includes all thenecessary bitwise operation, for example set() , f l ipO and tes t ( ) . An exampleis

/ / bitset .cpp

#include <iostream>#include <bitset>#include <string>using namespace std;

int main(void)

{const unsigned long n = 8;bitset<n> s;cout « s.setO « endl; // set all bits to 1cout « s.flip(4) « endl; // flip at position 4cout « s.test(5) « endl; // test if bit position i is true

return 0;

}

We could also use assembly language to do bitwise operations. In assembly language(Intel Pentium) the bitwise commmands are

AND bitwise ANDOR bitwise inclusive ORXOR bitwise exclusive OR

BSF bit scan forward

BSR bit scan reverseBSWAP byte swapBT bit testBTC bit test and complement

BTC saves the value of the bit indicated by thebase (first operand) and the bit offset (second operand)into the carry flag and then complements the bit.

BTR bit test and resetBTR saves the value of the bit indicated by thebase (first operand) and the bit offset (second operand)into the carry flag and then stores 0 in the bit.

BTS bit test and set

BTS saves the value of the bit indicated by thebase (first operand) and the bit offset (second operand)

Page 380: Nonlinear Workbook (3rd)

12.4. BITWISE OPERATIONS 361

into the carry flag and stores 1 in the bi t .

NEG two's complement negation

NOT one's complement negation

RCL rotate through carry left instructionRCR rotate through carry right instructionROL rotate left instructionROR rotate right instruction

SAL shift arithmetic left instructionSAR shift arithmetic right instructionSHL shift left instructionSHR shift right instruction

Using inline assembly language in C++ we can use these operations as follows. Wewant to find the minimum of the function

f(n) = 3 n - 5 n 2 + n3 + 10

where n is of data type unsigned long. Then f(n) is of data type unsigned longwhen n is restricted the proper range. In our simple example the farm consists offour elements, a, b, c and d.

II assembler.cpp

#include <iostream>#include <ctime>#include <cstdlib>using namespace std;

unsigned long f(unsigned long n)

ireturn 3*n - 5*n*n + n*n*n + 10;

}

int main(void)

-cunsigned long a = 1;unsigned long b = 11;unsigned long c = 5017;unsigned long d = 1013;unsigned long rl, r2, r3, r4;srand((unsigned long) time(NULL));int T = 1000;for(int j=O;j<T;j++)

Page 381: Nonlinear Workbook (3rd)

362 CHAPTER 12. GENETIC ALGORITHMS

i_asm{MOV EAX, aMOV EBX, bMOV ECX, cMOV EDX, dBSWAP EAXBSWAP EBXMOV rl, EAXMOV r2, EBXXOR EDX, ECXMOV r3, EDXAND ECX, EAXMOV r4, ECX}if(f(rl) < f(a)) a = rl;if(f(r2) < f(b)) b = r2;if(f(r3) < f(c)) c = r3;if (f (r4) < f (d)) d = r4;

unsigned long s = rand()'/,32;

_asm

{MOV EAX, aMOV ECX, sBTS EAX, ECXMOV rl, EAXMOV EAX, bMOV ECX, sBTR EAX, ECXMOV r2, EAXMOV EAX, cMOV ECX, s

BTC EAX, ECXMOV r3, EAXMOV EAX, dMOV ECX, sBTS EAX, ECXMOV r4, EAX

>if(f(rl) < f(a)) a = rl;if(f(r2) < f(b)) b = r2;if (f (r3) < f (c)) c = r3;

Page 382: Nonlinear Workbook (3rd)

12.4. BITWISE OPERATIONS 363

if ( f ( r4) < f(d)) d = r4;} / / end for loopcout « "f(" « a « ") = " « f(a) « endl;cout « "f(" « b « ") = " « f(b) « endl;cout « "f(" « c « ") = " « f(c) « endl;cout « "f(" « d « ") = " « f(d) « endl;

return 0;}

We can also do bitwise manipulations of floating point numbers, for example on thedata type double (64 bits) in C++. The value of double is stored as

sign bit, 11 bit exponent, 52 bit mantissa

This means

byte 1 byte 2 byte 3 byte 4 byte 8S X X X XXXX XXXX MMMM MMMM MMMM MMMM MMMM . . . MMMM MMMM

The C++ progran shows how to change a bit in double, where I is the bitwise ORand « is the shift operation.

// doublebits.cpp

#include <iostream>

using namespace std;

double doublebitstringdnt bit.double x)

{int* p = (int*) &x;if(bit < 32) *p |= (1 « bit);if(bit >= 32) *(p+l) |= (1 « (bit - 32));

return x;

>

int main(void)

idouble x = 3.14159;for(int bit=0;bit<64;bit++)icout.precision(20); cout « doublebitstring(bit,x) « endl;}

return 0;

>

Page 383: Nonlinear Workbook (3rd)

364 CHAPTER 12. GENETIC ALGORITHMS

12.5 A Bit Vector Class

In the following we provide a Bit Vector class which we use for our genetic algorithmprograms.

/ / Bitvect.h

•include <cstring>using namespace std;

#ifndef __BITVECTORttdefine __BITVECTOR

const unsigned char _BV_BIT[8] = { 1,2,4,8,16,32,64,128 };

class BitVector{

protected:unsigned char *bitvec;int len;

public:BitVectorO ;BitVector(int nbits); // constructorBitVector(const BitVectorft); // copy constructor"BitVectorO; // destructorvoid SetBit(int bit,int val=l);int GetBit(int bit) const;void ToggleBit(int bit);BitVector operator & (const BitVectorft) const;BitVectorfc operator &= (const BitVectorfc);BitVector operator I (const BitVectorfc) const;BitVectorfe operator 1= (const BitVectorfc);BitVector operator " (const BitVectorfe) const;BitVectorfc operator "= (const BitVectorfe);friend BitVector operator " (const BitVectorfe);BitVectorft operator = (const BitVectorfc);int operator[](int bit) const;

void SetLength(int nbits);

>;

BitVector::BitVector()

{len = 0;

bitvec = NULL;

}

Page 384: Nonlinear Workbook (3rd)

12.5. A BIT VECTOR CLASS 365

BitVector::BitVector(int nbits)

{len = nbits/8+((nbits7.8)?l:0);

bitvec = new unsigned char[len];

}

BitVector::BitVector(const BitVectorfe b)

-Clen = b.len;bitvec = new unsigned char[len];

memcpy(bitvec,b.bitvec,len);

>

BitVector::"BitVectorO{ if(bitvec != NULL) delete[] bitvec; }

void BitVector::SetBit(int bit,int val)

{if(bit < 8*len)

iif (val) b i tvec[bi t /8] 1= _BV_BIT[bit'/,8] ;else bitvec [bit /8] &= ~_BV_BIT[bit'/.8] ;>

>

int BitVector::GetBit(int bit) const

{if (bit < 8*len) return ((bitvec[bit/8]&_BV_BIT[bit'/.8])?l:0);

return -1;

>

void BitVector::ToggleBit(int bit)

{

if(bit<8*len) bitvec [bit/8] "= _BV_BIT[bit'/.8] ;

>

BitVector BitVector::operator & (const BitVectorfe b) const

{int mien = (len > b.len)?len:b.len;BitVector ret(mlen*8);for(int i=0;i<mlen;i++)ret.bitvec[i] = bitvec[i] & b.bitvec[i] ;

return ret;

>

Page 385: Nonlinear Workbook (3rd)

366 CHAPTER 12. GENETIC ALGORITHMS

BitVectorfc BitVector::operator &= (const BitVectorfe b)

•C

int mien = (len>b.len)?len:b.len;for(int i=0;i<mlen;i++)bitvec[i] &= b.bitvec[i] ;return *this;

>

BitVector BitVector::operator I (const BitVectorfe b) const

{int mien = (len>b.len)?len:b.len;BitVector ret(mlen*8);for(int i=0;i<mlen;i++)ret .bitvec[i] = bitvec[i] |b.bitvec[i] ;return ret;

>

BitVectorfe BitVector::operator |= (const BitVectorfe b)

{int mien = (len>b.len)?len:b.len;for(int i=0;i<mlen;i++)bitvec[i] |= b.bitvecEi];return *this;

}

BitVector BitVector::operator " (const BitVectorfe b) const

iint mien = (len>b.len)?len:b.len;BitVector ret(mlen*8);for(int i=0;i<mlen;i++)ret.bitvec[i] = bitvec[i] "b.bitvec[i] ;return ret;

}

BitVectorfc BitVector::operator "= (const BitVectorfe b)

{int mien = (len>b.len)?len:b.len;for(int i=0;i<mlen;i++)

bitvec[i] "= b.bitvec[i];return *this;

>

BitVector operator " (const BitVector& b)

Page 386: Nonlinear Workbook (3rd)

12.6. MAXIMUM OF ONE-DIMENSIONAL MAPS 367

BitVector ret(b.len*8);for(int i=0;i<b.len;i++)ret.bitvec[i] = ~b.bitvec[i];return ret;

}

BitVectorfc BitVector::operator = (const BitVectorfc b)

{if(bitvec == b.bitvec) return *this;if(bitvec != NULL) deleted bitvec;len = b.len;bitvec = new unsigned char[len];memcpy(bitvec.b.bitvec,len);

return *this;

}

int BitVector::operator[](int bit) const{ return GetBit(bit); }

void BitVector::SetLength(int nbits)

iif(bitvec != NULL) delete[] bitvec;len = nbits/8 + ((nbits7,8)?l :0) ;bitvec = new unsigned char[len];

}#endif

12.6 Maximum of One-Dimensional Maps

As an example we consider the following fitness function

f(x) = cos(:r) — sin(2:r)

in the interval [0 : 2?r]. In this interval the function / has three maxima. The globalmaximum is at 5.64891 and the two local maxima are at 0 and 2.13862.

A simple C++ program would include the following functions

/ / f i tness function of individualdouble f(double)

/ / f i tness function value of individualdouble f_value(double (*func)(double),int* arr.intfe N,

double a,double b)

Page 387: Nonlinear Workbook (3rd)

368 CHAPTER 12. GENETIC ALGORITHMS

II x_valuedouble x_value(int* arr.intft N,double a.double b)

// setup of farm

void setupdnt** farm.int M.int N)

/ / crossing two individualsvoid crossings(int** farm.int M.int N)

/ / mutate an individualvoid mutate(int** farm.int M.int N)

Here N is the length of the binary string and M is the size of the population, whichis kept constant at each time step. For the given problem we select N = 10 andM = 12. The binary string "SJV-ISA^.-.SO" is mapped into the integer number mand then into the real number x in the interval [0 : 2n] as described above.

The farm is set up using a random number generator. In our implementation thecrossing function selects the two fittest strings from the two parents and the twochildren. The parents are selected by a random number generator. With a popula-tion of 12 strings in the farm we find after 1000 iterations the global maximum at5.64891. For 100 iterations we find as a typical output the global maximum and thesecond highest maximum.

// genetic.cpp

#include <iostream>#include <cstdlib>#include <ctime> // for srand, rand#include <cmath> // for cos, sin, powusing namespace std;

// fitness function where maximum to be found

double f(double x){ return cos(x) - sin(2*x); }

// fitness function value for individual

double f_value(double (*func)(double),int* arr.intfc N,

double a,double b)

{double res;double m = 0.0;for(int j=O;j<N;j++){double k = j;

Page 388: Nonlinear Workbook (3rd)

12.6. MAXIMUM OF ONE-DIMENSIONAL MAPS 369

m += arr[N-j-l]*pow(2.0,k);>double x = a + m*(b-a)/(pow(2.0,N)-1.0);res = func(x);return res;

>

// x_value at global maximumdouble x_value(int* arr.intfc N,double a,double b)

{double m = 0.0;for(int j=O;j<N;j++)

{double k = j;

m += arr[N-j-l]*pow(2.0,k);

}double x = a + m*(b-a)/(pow(2.0,N)-1.0);return x;

}

// setup the population (farm)void setup(int** farm,int M.int N){srand((unsigned long) time(NULL));for(int j=O;j<M;j++)

for(int k=0;k<N; k++)

farm[j][k] = rand()'/.2;}

// cross two individualsvoid crossings(int** farm.intfc M,int& N.doublefe a,double& b)

iint K = 2;int** temp = new int* [K];for(int i=0;i<K;i++) temp[i] = new int[N];

double res[4];int rl = rand()'/.M; int r2 = rand()'/.M;// random returns a value between// 0 and one less than its parameterwhile(r2 == rl) r2 = randO'/.M;

res[0] = f_value(f ,farm[rl] ,N,a,b);res[l] = f_value(f,farm[r2],N,a,b);

Page 389: Nonlinear Workbook (3rd)

370 CHAPTER 12. GENETIC ALGORITHMS

for(int j=O;j<N;j++)itemp[0] [j] = farmErl] [j] ;temp[l][j] = f arm[r2] [j] ;}int r3 = rand()*/0(N-2) + 1;

for(j=r3;j<N;j++){temp[0][j] = farm[r2][j];temp[l][j] = farm[rl] [ j] ;}res [2] = f_value(f ,temp[0] ,N,a,b);res[3] = f_value(f ,temp[l] ,N,a,b);

if (res [2] > res[0]){for(j=0;j<N;j++)f arm[rl] [j] = temp[0] [j] ;res[0] = res [2] ;}

if (res [3] > res[l])ifor(j=0;j<N;j++)farm[r2][j] = temp[l] [j] ;res[l] = res [3] ;}for(j=0;j<K;j++)delete [] temp[j] ;delete[] temp;

}

/ / mutate an individualvoid mutate(int** farm.intft M,int& N,double& a,double& b){

double res[2];int r4 = rand()°/.N; int rl = rand()'/.M;res[0] = f.value(f ,farm[rl] ,N,a,b);int vl = f arm[rl] [r4] ;if(vl == 0) farm[rl] [r4] = 1;if(vl == 1) farm[rl][r4] = 0;double al = f_value(f ,farm[rl] ,N,a,b);if(al < res[0]) farm[rl] [r4] = vl;

Page 390: Nonlinear Workbook (3rd)

12.6. MAXIMUM OF ONE-DIMENSIONAL MAPS 371

int r5 = randO'/.N; int r2 = randO'/.M;res [ i ] = f_value(f ,farm[r2] ,N,a,b) ;int v2 = f arm[r2] [r5] ;if(v2 == 0) f arm[r2] [r5] = 1;if(v2 == 1) farm[r2][r5] = 0;double a2 = f_value(f ,farm[r2] ,N,a,b);if(a2 < r e s [ l ] ) farm[r2] [r5] = v2;

}

int main(void)

int M = 12; // population (farm) has 12 individuals (animals)int N = 10; // length of binary string

int** farm = new int*[M]; / / allocate memory for populationfor(int i=0;i<M;i++) { farm[i] = new int[N]; }

setup(farm,M,N);double a = 0.0, b = 6.28318; // interval [a,b]

for(int k=0;k<1000;k++)

icrossings(farm,M,N,a,b);mutate(farm,M,N,a,b);} // end for loop

fordnt j=O;j<N;j++)cout « "farm[l] [" « j « "] = " « farm[l] [j] « endl;cout « endl;for(j=0;j<M;j++)cout « "fitness f_value[" « j « "] = "

« f_value(f ,farm[j] ,N,a,b)« " " « "x_value[" « j « "] = "« x_value(farm[j] ,N,a,b) « endl;

for(j=0;j<M;j++)delete[] f arm[j];delete[] farm;return 0;

}

In the program given above we store a bit as int. This wastes a lot of memoryspace. A more optimal use of memory is to use a string, for example "1000111101".Then we use 1 byte for 1 or 0. An even more optimal use is to manipulate the bitsitself. In the following we use the bit Vector class described above to manipulate

Page 391: Nonlinear Workbook (3rd)

372 CHAPTER 12. GENETIC ALGORITHMS

the bits. The bit Vector class is included in the header file bitVect. h.

/ / findmax.cpp

#include <iostream>#include <cmath> // for pow#include <cstdlib>#include <ctime>#include "bitvect.h"using namespace std;

double f(double x) { return cos(x)-sin(2*x);}

double f_value(double (*func)(double).const BitVector &arr,

int &N,double a,double b)

{double res, m = 0.0;for(int j=O;j<N;j++){double k = j ;m += arr[N-j-l]*pow(2.0,k);}double x = a+m*(b-a)/(pow(2.0,N)-1.0);res = func(x);

return res;

}

double x_value(const BitVectorfe arr.intfe N,double a,double b)

{double m = 0.0;for(int j=O;j<N;j++){double k = j ;m += arr[N-j-l]*pow(2.0,k) ;

>double x = a + m*(b-a)/(pow(2.0,N)-1.0);

return x;

>

void setup(BitVector *farm,int M.int N)

{srand((unsigned)time(NULL));for(int j=O;j<M;j++)for(int k=O;k<N;k++)farm[j] .SetBit(k,rand()*/,2) ;

Page 392: Nonlinear Workbook (3rd)

12.6. MAXIMUM OF ONE-DIMENSIONAL MAPS 373

>

void crossings(BitVector *farm,int &M,int &N,double &a,double &b){int K = 2, j;BitVector *temp = new BitVector[K];for(int i=0;i<K;i++) temp[i].SetLength(N);double res[4];int r l = rand()'/.M; int r2 = rand()'/.M;while (r2 == rl) r2 = rand()°/.M;res[0] = f_value(f ,farm[rl] ,N,a,b) ;res[l] = f_value(f,farm[r2],N,a,b);for(j=0;j<N;j++){temp[0] .SetBit(j,farm[rl] [j]);temp[l] .SetBit(j ,farm[r2] [j]) ;}int r3 = rand()'/.(N-2)+l;for(j=r3;j<N;j++){temp[0] .SetBit(j ,farm[r2] [j]) ;temp[l] .SetBit(j,farm[rl] [j]);}res[2] = f_value(f ,temp[0] ,N,a,b);res[3] = f_value(f ,temp[0] ,N,a,b);if (res[2]>res[0])•Cfarm[rl] = temp[0] ;res[0] = res [2] ;}if (res [3] > res[l]){farm[r2] = temp[l] ;res[l] = res [3];}delete[] temp;

>

void mutate(BitVector *farm,int &M,int &N,double &a,double &b)

•C

double res[2];int r4 = randO'/.N; int rl = randO'/.M;res[0] = f_value(f ,farm[rl] ,N,a,b);int vl = f arm[rl] [r4] ;farm[rl] .ToggleBit(r4);

Page 393: Nonlinear Workbook (3rd)

374 CHAPTER 12. GENETIC ALGORITHMS

double al = f_value(f,farm[rl],N,a,b);if(al < res[0]) farm[rl].ToggleBit(r4);int r5 = rand()'/.N; int r2 = randO'/.M;res[l] = f_value(f,farm[r2],N,a,b);int v2 = farm [r2] [r5] ;f arm[r2] .ToggleBit(r5) ;double a2 = f_value(f,farm[r2],N,a,b);if(a2 < res[l]) farm[r2] .ToggleBit(r5);

}

int main(void){

int M = 12, N = 10;int j ;BitVector* farm = new BitVector[M];for(int i=0;i<M;i++) farm[i].SetLength(N);setup(farm,M,N);double a = 0.0, b = 6.28318; // interval

for(int k=0;k<1000;k++)

{crossings(farm,M,N,a,b);mutate(farm,M,N,a,b);}for(j=0;j<N;j++)cout « "farm[l] ["«j«"]=" « farm[l] [j] « endl;cout << endl;for(j=0;j<M;j++)cout « "fitness f_value["«j«"]="

« f_value(f ,farm[j] ,N,a,b)« " x_value["«j«"]=" « x_ value (f arm [j] ,N,a,b) « endl;

delete [] farm;}

A typical output is

farm[l] [0]=lfarm[l] [1]=1farm[l] [2]=1farm[l][3]=0farm[l][4]=lfarm[l][5]=0farm[l][6]=0farm[l] [7]=0farm[l] [8]=0

Page 394: Nonlinear Workbook (3rd)

12.7. MAXIMUM OF TWO-DIMENSIONAL MAPS 375

farm[l][9]=0

fitness f_value[O]=l.75411 x_value[0]=5.6997fitness f_value[l]=l.75411 x_value[1]=5.6997fitness f.value[2]=1.75411 x_value[2]=5.6997fitness f_value[3]=1.75411 x_value[3]=5.6997fitness f_value[4]=l.75411 x.value[4]=5.6997fitness f_value[5]=1.75411 x_value[5]=5.6997fitness f_value[6]=l x_value[6]=0fitness f_value[7]=0.59771 x_value[7]=0.196541fitness f_value[8]=1.75411 x_value[8]=5.6997fitness f_value[9]=l x_value[9]=0fitness f_value[10]=1.75411 x_value[10]=5.6997fitness f_value[11]=1.75411 x_value[11]=5.6997

12.7 Maximum of Two-Dimensional Maps

Here we consider the problem how to find the maximum of a two-dimensionalbounded function / : [o, 6] x [c, d] —t R, where a,b,c,d € R, a < b and c < d.We follow in our presentation closely Michalewicz [69]. Michalewicz also gives adetailed example.

We use the following notation. N is the length of the chromosome (binary string).The chromosome includes both the contributions from the x variable and y variable.The size of N depends on the required precision. M denotes the size of the farm(population) which is kept constant at each time step. First we have to decide aboutthe precision. We assume further that the required precision is four decimal placesfor each variable. First we find the domain of the variable x, i.e. 6-o . The precisionrequirement implies that the range [a, b] should be divided into at least (b — a)-10000equal size ranges. Thus we have to find an integer number N\ such that

2JVl~1 < (b - a) • 1000 < 2 N l .

The domain of variable y has length d — c. The same precision requirement impliesthat we have to find an integer A^ such that

2N2~l <{d-c)- 1000 < 2Ni.

The total length of a chromosome (solution vector) is then N = N\ + N2- The firstNi bits code x and the remaining N2 bits code y.

Next we generate the farm. To optimize the function / using a genetic algorithm,we create a population of size = M chromosomes. All N bits in all chromosomesare initialized randomly using a random number generator.

Page 395: Nonlinear Workbook (3rd)

376 CHAPTER 12. GENETIC ALGORITHMS

Let us denote the chromosomes by v0, vi, ..., VM-I- During the evaluation phasewe decode each chromosome and calculate the fitness function values f(x,y) from(x, y) values just decoded.

Now the system constructs a roulette wheel for the selection process. First wecalculate the total fitness F of the population

M - l

F := £ /K) •

Next we calculate the probability of a selection p; and the cumulative probability q,for each chromosome Vi

Pi--=^JT, <&:=!>*, i = 0 , l , . . . , M - l .r k=0

Obviously, qM-i = 1. Now we spin the roulette wheel M times. First we generate a(random) sequence of M numbers for the range [0..1]. Each time we select a singlechromosome for a new population as follows. Let r^ be the first random number.Then <& < r0 < qk+i for a certain k. We selected chromosome k + 1 for the newpopulation. We do the same selection process for all the other M — 1 random num-bers. This leads to a new farm of chromosomes. Some of the chromosomes can nowoccur twice.

We now apply the recombination operator, crossover, to the individuals in the newpopulation. For the probability of crossover we choose pc = 0.25. We proceed inthe following way: for each chromosome in the (new) population we generate arandom number r from the range [0..1]. Thus we generate again a sequence of Mrandom numbers in the interval [0,1]. If r < 0.25, we select a given chromosome forcrossover. If the number of selected chromosomes is even, so we can pair them. Ifthe number of selected chromosomes were odd, we would either add one extra chro-mosome or remove one selected chromosome. Now we mate selected chromosomesrandomly. For each of these two pairs, we generate a random integer number posfrom the range [0..N — 2]. The number pos indicates the position of the crossingpoint. We do now the same process for the second pair of chromosomes and so on.This leads to a new farm of chromosomes.

The next operator, mutation, is performed on a bit-by-bit basis. The probabilityof mutation pm = 0.01, so we expect that (on average) 1% of bits would undergomutation. There are M x M bits in the whole population; we expect (on average)0.01 • N • M mutations per generation. Every bit has an equal chance to be mutated,so, for every bit in the population, we generate a random number r from the range[0..1]. If r < 0.01, we mutate the bit. This means that we have to generate N • Mrandom numbers. Then we translates the bit position into chromosome number andthe bit number within the chromosome. Then we swap the bit. This leads to a new

Page 396: Nonlinear Workbook (3rd)

12.7. MAXIMUM OF TWO-DIMENSIONAL MAPS 377

population of the same size M.

Thus we have completed one iteration (i.e., one generation) of the while loop inthe genetic procedure. Next we find the fitness function for the new population andthe total fitness of the new population, which should be higher compared to theold population. The fitness value of the fittest chromosome of the new populationshould also be higher than the fitness value of the fittest chromosome in the oldpopulation. Now we are ready to run the selection process again and apply thegenetic operators, evaluate the next generation and so on. A stopping conditioncould be that the total fitness does not change anymore.

// twodimensional.cpp

#include <iostream>

#include <cmath> // for exp, pow#include <cstdlib>#include <ctime>using namespace std;

// function to optimize

double f(double x,double y){ return exp(-(x-1.0)*(x-1.0)*y*y/2.0); }

// determines the chromosone length required// to obtain the desired precisionint cLength(int precision,double rangeStart,double rangeEnd)

•Cint length = 0;double total = (rangeEnd - rangeStart)*pow(10.0,precision);while(total > pow(2.0,length)) length++;

return length;

>

void setup(int** farm,int size,int length)

{srand((unsigned long) time(NULL));for(int i=0;i<size;i++)for(int j=0;j<length;j++)

farm[i][j] = rand 07.2;}

void printFarm(int** farm,int length,int size){for(int i=0;i<size;i++)

{

Page 397: Nonlinear Workbook (3rd)

378 CHAPTER 12. GENETIC ALGORITHMS

cout « "\n";for(int j=0; j<length;j++) { cout « farm[i] [j] ; >>

}

double xValue(int* chromosome,int xLength,double* domain){double m = 0.0;for(int i=O;i<xLength;i++){m += chromosome[xLength-i-1]*pow(2.0,i);}double x =domain[0] + m*(domain[1]-domain[0] )/(pow(2.0,xLength)-l .0) ;

return x;}

double yValue(int* chromosome,int yLength.int length,double* domain)

{double m = 0.0;for(int i=O;i<yLength;i++){m += chromosome[length-i-l]*pow(2.0,i);}double y =domain[2] + m*(domain[3]-domain[2])/(pow(2.0,yLength)-l);

return y;

}

double fitnessValue(double (*f)(double,double),int* chromosome,

int length,double* domain,int xLength.int yLength)

{double x = xValue(chromosome,xLength,domain);double y = yValue(chromosome,yLength,length,domain);double result = f(x,y);

return result;

}

// New farm is set up by using a roulette wheel parent selectionvoid roulette(int** farm,int length,int size,double* domain,

int xLength.int yLength)

iint i, j;// fitness matrix contains the fitness of each// individual chromosome on farm

Page 398: Nonlinear Workbook (3rd)

12.7. MAXIMUM OF TWO-DIMENSIONAL MAPS 379

double* fitnessVector = new double[size];

for(i=0;i<size;i++){fitnessVector [i] =fitnessValue(f,farm[i].length,domain,xLength,yLength);>

// fitness vector contains the fitness of// each individual chromosome of the farmdouble totalFitness =0.0;for(i=0;i<size;i++){totalFitness += fitnessVector[i];>

// calculate probability vectordouble* probabilityVector = NULL;probabilityVector = new double[size];f or (i=0; i<size; i++){probabilityVector[i] = fitnessVector[i]/totalFitness;>

// calculate cumulative probability vectordouble cumulativeProb =0.0;double* cum_prob_Vector = new double [size];

for(i=0;i<size;i++){cumulativeProb += probabilityVector[i];cum_prob_Vector[i] = cumulativeProb;}

// setup random vectordouble* randomVector = new double [size];srand((unsigned long) time(NULL));

f or (i=0; Ksize; i++)randomVector[i] = rand()/double(RAND_MAX);

// create new populationint count;int** newFarm = new int* [size];for(i=0;i<size;i++) newFarm[i] = new int[length];

Page 399: Nonlinear Workbook (3rd)

380 CHAPTER 12. GENETIC ALGORITHMS

f or (i=0; Ksize; i++)

{count = 0;

while(randomVector[i] > cum_prob_Vector[count]) count++;for(j=0;j<length;j++){ newFarm[i] [j] = farm[count] [j] ; }}

f or (i=0; Ksize; i++)for(j=0;j<length;j++)farm[i] [j] = newFarm[i] [j] ;

delete [] fitnessVector;deleted probabilityVector;delete[] cum_prob_Vector;delete [] randomVector;

for(i=0;i<size;i++) delete[] newFarm[i] ;delete [] newFarm;

} // end function roulette

void crossing(int** farm,int size,int length)

{int i, j, k, m;int count = 0;int* chosen = new int[size];double* randomVector = new double[size];

srand((unsigned long) time(NULL));

for(i=0;i<size;i++)randomVector[i] = randO/double(RAND_MAX);

// fill chosen with indexes of all random values < 0.25

for(i=0;i<size;i++)

{if(randomVector[i] < 0.25)

{

chosen[count] = i ;count++;>>

// if chosen contains an odd number of chromosomes// one more chromosome is to be selectedif((count'/.2 != 0) II (count == 1))

Page 400: Nonlinear Workbook (3rd)

12.7. MAXIMUM OF TWO-DIMENSIONAL MAPS 381

{.int index = 0;while(randomVector[index] < 0.25) index++;count++;

chosen[count-l] = index;}

// cross chromosomes with index given in chosenint** temp = new int* [2];for(i=0;i<2;i++) temp[i] = new int[length];

for(i=0;i<count;i=i+2)

{for(j=0;j<length;j++)

{temp[0][j] = farm [chosen [i]] [j] ;temp[l] [j] = farm [chosen [i+1]] [j] ;

}int position = rand()'/.length;

for(k=position;k<length;k++)

{temp[0][k] = farm [chosen [i+1] ] [k] ;temp[l] [k] = f arm [chosen [ i ] ] [k] ;>

for(m=0; nKlength;m++)

{.farm [chosen [i] ] [m] = temp[0][m];farm[chosen[i+l]] [m] = temp[l] [m] ;}}

delete[] chosen;delete[] randomVector;for(i=0;i<2;i++) deleteG temp[i] ;delete[] temp;

} // end function crossing

void mutate(int** farm,int size,int length){int totalbits = size*length;double* randomVector = new double[totalbits];

srand((unsigned long) time(NULL));

Page 401: Nonlinear Workbook (3rd)

382 CHAPTER 12. GENETIC ALGORITHMS

for(int i=0;i<totalbits;i++)

randomVector[i] = rand()/double(RAND_MAX);

int a, b;for(int i=0;i<totalbits;i++){if (randomVector[i] < 0.01){.

if(i >= length)

{a = i/length; b = i'/.length;}else { a = 0; b = i; }if (farm [a] [b] == 0) farm [a] [b] = 1;else farm [a] [b] = 0 ;}}

delete [] randomVector;

>

void printResult(int** farm,int length,int size,double* domain,

int xLength.int yLength.int iterations)

{int i;double* fitnessVector = new double[size];

for(i=0;i<size;i++)fitnessVector[i] =fitnessValue(f,farm[i].length,domain,xLength,yLength);

// search for chromosome with maximum fitness

double x, y;int pos = 0;double max = fitnessVector[0];

for(i=l;i<size;i++)

{if(fitnessVector[i] > max)

{max = f itnessVector[i] ;pos = i;

}}

Page 402: Nonlinear Workbook (3rd)

12.7. MAXIMUM OF TWO-DIMENSIONAL MAPS 383

x = xValue(farm[pos],xLength,domain);y = yValue(farm[pos].yLength,length,domain);

// displaying the resultcout « "\n\n After " « iterations

« " iterations the fitnesses are: \n";for(i=0;i<size;i++){cout « "\n fitness of chromosome "

« i « ": " « fitnessVector[i];>cout « "\n\n Maximum fitness: f(" « x « "," « y « ") = "

« max;

deleted fitnessVector;

}

int main(void)

{int size = 32; // population sizeint precision =6; // precisionint iter = 10000; // number of iterationsdouble domain[4]; // variables specifying domaindouble xl, x2, yl, y2;xl = -2.0; x2 = 2.0;yl = -2.0; y2 = 2.0;

domain[0] = xl; domain[1] = x2;

domain[2] = yl; domain[3] = y2;

int xLength = cLength(precision,domain[0],dornain[l]);cout « "\n\n the xLength is: " « xLength;int yLength = cLength(precision,domain[2],domain[3]);cout « "\n the yLength is: " « yLength;

// total lengthint length = xLength + yLength;cout « "\n the chromosone length is: " « length;

// allocate memory for farmint** farm = new int*[size];for(int i=0;i<size;i++) { farm[i] = new int[length]; }setup(farm,size,length);

cout « "\n\n The inital farm: \n";printFarm(farm,length,size);

Page 403: Nonlinear Workbook (3rd)

384 CHAPTER 12. GENETIC ALGORITHMS

cout « endl;

// iteration loopfor(int t=0;t<iter;t++){roulette(f arm,length,s ize,domain,xLength,yLength);crossing(farm,size,length);roulette(farm,length,size,domain,xLength,yLength);mutate(farm,size,length);>

printResult(farm,length,size,domain,xLength,yLength,iter);

for(int k=0;k<size;k++) { deleteD farm[k]; }delete[] farm;return 0;

}

12.8 Finding a Fitness Function

Genetic algorithms can also be applied to some problems where no fitness functionis given ([90]). This means we first have to construct a fitness function for the prob-lem. A number of examples are given below. We discuss finding the (real) roots ofa polynomial. As a second example we consider finding the solution of the bound-ary value problem of an ordinary differential equation. Then two examples studysolutions to systems of linear equations. Finally we consider the four-colour problem.

Example 1. Consider the polynomial, for example,

p(x) = x4 - 7x3 + 8x2 + 2x - 1.

The zeros are given by the solution of the equation

P ( x ' ) = 0 .

As fitness function / we can use

f{x) = -p(x)-p{x)

which we have to maximize, i.e., the zeros of the polynomial p are found where /takes a global maximum. Obviously the global maximum of / is 0. Another possiblefitness function would be f(x) = \p{x)\. This fitness function has to be minimized.For faster calculation of / we use Horner's scheme. We could use the program insection 12.6. The output provides the two zeros 5.6385 and 1.21558. The two otherroots of p have an imaginary part. How do we include this case?

Page 404: Nonlinear Workbook (3rd)

12.8. FINDING A FITNESS FUNCTION 385

Example 2. Consider the nonlinear second-order ordinary differential equation

{u-x)-r-^ +sm\x) = 0

with the boundary values

u(0) = 0, u(l) = l + sin(l), a; €[0,1].

As an ansatz for the solution we use the polynomial

u(x) = Co + C\X + C2X2 + C3X3 + C4Z4 .

Obviously we can set c0 = 0. We use the second boundary condition to eliminateone more coefficient, for example Ci. Then we can define a fitness function as

/ ( e 2 , 03, cO = - £ ( («( i -h)-j- h ) ^ ^ + sin2(j • h)J

where h is the step length (for example h = 0.1). For the implementation of thesecond order derivative we first differentiate u, i.e.,

— = 2c2 + 6c3x + 12c4a:

and then replace x by j • h. Inserting u(j • h) for u(x), j • h for x and d?u/dx2 intothe differential equation yields the fitness function / .

Example 3. Consider the symmetric 10 x 10 matrix

/2 1 0 0 0 0 0 0 0 0\1 2 1 0 0 0 0 0 0 00 1 2 1 0 0 0 0 0 00 0 1 2 1 0 0 0 0 00 0 0 1 2 1 0 0 0 00 0 0 0 1 2 1 0 0 00 0 0 0 0 1 2 1 0 00 0 0 0 0 0 1 2 1 00 0 0 0 0 0 0 1 2 1

\0 0 0 0 0 0 0 0 1 2/

and the linear equationAx = r

wherer = (1 1 1 1 1 1 1 1 1 1)T.

Here T denotes transpose. A possible fitness function is

( 1 9 9 ^\ i U i=0 j=0 /

Page 405: Nonlinear Workbook (3rd)

386 CHAPTER 12. GENETIC ALGORITHMS

where a^ are the matrix elements of A. This function has to be maximized.

Example 4. Let A be a given mxm symmetric positive-semidefinite matrix over R.Let b £ Rm, where b is a given column vector. Consider the quadratic functional

£(x) = ix T Ax - xTbz

where T denotes transpose. The minimum x* of E(x) over Rm is unique and occurswhere the gradient of E(x) vanishes, i.e.,

V£(x = x*) = . 4 x - b = 0.

The quadratic minimization problem is thus equivalent to solving the system oflinear equations Ax = b. Let

H:D- -(!)•We use E as a fitness function to solve the system of linear equations applyinggenetic algorithms. We have to find the minima of the function

E(x)=l-{XuX2)(l } ) ( £ ) - ( * ! , * * ) (1)2 2

= Xl + Z i O ^ + 7yX2 — %1 — %2 •

The solution is X\ = 0, x2 — 1.

Example 5. A map is called n-colourable if each region of the map can be as-signed a colour from n different colours such that no two adjacent regions have thesame colour. The four colour conjecture is that every map is 4-colourable. In 1976Appel and Haken proved the four colour conjecture with extensive use of computercalculations. We can describe the m regions of a map using a m x m adjacencymatrix A where Aij — 1 if region i is adjacent to region j and A\j = 0 otherwise.We set An = 0. For the fitness function we can determine the number of adjacentregions which have the same colour. The lower the number, the fitter the individual.Individuals are represented as strings of characters, where each character representsthe colour for the region corresponding to the characters position in the string. Wewrite a Java program that uses genetic algorithm to find a solution of the four colourproblem given the adjacency matrix.

The data member population is the number of individuals in the population, andmu is the probability that an individual is mutated. The method f i tness () evalu-ates the fitness of a string using the adjacency matrix to determine when adjacentregions have the same colour. If the fitness is equal to 0 we have found a solution.The adjacency matrix can be modified to solve for any map. The method mutate ()determines for each individual in the population whether the individual is mutated,

Page 406: Nonlinear Workbook (3rd)

12.8. FINDING A FITNESS FUNCTION 387

and mutates a component of the individual by randomly changing the colour. Themethod crossingO performs the crossing operation. The genetic algorithm is im-plemented in the method GA(). The arguments are an adjacency matrix, a stringspecifying which colours to use and the number of regions on the map. It returns astring specifying a solution to the problem. One such solution is YBRBYGYRYB, whereR stands for red, G for green, B for blue and Y for yellow.

// FourColor.Java

public class FourColor

{static int population = 1000;

static double mu = 0.01;

public static void main(String[] args)

{i n t [ ] [ ] adjM = {{0,1 ,0 ,1 ,0 ,0 ,0 ,0 ,0 ,0} ,

{1 ,0 ,1 ,0 ,0 ,1 ,0 ,0 ,0 ,0} ,{0 ,1 ,0 ,0 ,0 ,0 ,1 ,0 ,0 ,0} ,{1 ,0 ,0 ,0 ,1 ,1 ,0 ,0 ,0 ,0} ,{0 ,0 ,0 ,1 ,0 ,1 ,0 ,1 ,0 ,0} ,{0 ,1 ,0 ,1 ,1 ,0 ,1 ,0 ,1 ,1} ,{0 ,0 ,1 ,0 ,0 ,1 ,0 ,0 ,0 ,1} ,{0 ,0 ,0 ,0 ,1 ,0 ,0 ,0 ,1 ,0} ,{0 ,0 ,0 ,0 ,0 ,1 ,0 ,1 ,0 ,1} ,{0,0 ,0 ,0 ,0 ,1 ,1 ,0 ,1 ,0}};

System.out.println(GA(adjM,"RGBY",10));}

s t a t i c int f i tness (int [][] adjM,String s . in t N){

int count = 0;

for(int i=0;i<N-l;i++)

{for(int j=i+l;j<N;j++){if ((s.charAt(i) == s.charAt(j)) &Sc (adjM[i] [j] == 1))count++;}}return count;}

static void mutate(String[] p,String colors)

Page 407: Nonlinear Workbook (3rd)

388 CHAPTER 12. GENETIC ALGORITHMS

{int j ;for(int i=0;i<p.length;i++){if(Math.random()<mu)•Cint pos = (int)(Math.randomO*(p[i].length()-l));int mut = (int) (Math.randomO*(colors.length()-2));char[] cal = p[i] .toCharArray 0 ;char[] ca2 = colors.toCharArray();for(j=0;cal[pos] !=ca2[j] ; j++) O;cal[pos] = ca2[(j+mut)7.colors.length()] ;p[i] = new String(cal);}}}

static void crossing(String[] p,int[][] adjM){int pi = (int)(Math.random()*(p.length-1));int p2 = pi;int cl = (int) (Math.random()*(p[0] .length()-D) ;int c2 = cl;while(p2==pl) p2 = (int) (Math.randomO*(p. length-1)) ;while(c2==cl) c2 = (int) (Math.random()*(p[0] .length()-l));if(c2<cl) { int temp = c2; c2 = cl; cl = temp;}String [] temp = new String [4];temp [0] =p [pi] ; temp [1] =p [p2] ;temp[2]=p[pl].substring(0,cl)+p[p2].substring(cl+l,c2)

+p[pl].substring(c2+l,p[pl].length()-l);temp[3]=p[p2],substring(O,cl)+p[pl].substring(cl+l,c2)

+p[p2] .substring(c2+l,p[p2] .length0-1);int i , f;for(i=0,f=0;i<4;i++){if (f itness(adjM,temp[i] ,temp[i] .lengthO)

>fitness(adjM,temp[f] ,temp[f] .lengthO))f = i;>{ String tmp=temp [f] ; temp [f ] =temp [0] ; temp [0] =tmp; }for(i=l,f=l;i<4;i++){if (fitness(adjM,temp[i] ,temp[i] .lengthO)

>fitness(adjM,temp[f] ,temp[f] .lengthO))f = i;

Page 408: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 389

}{ String tmp=temp[f]; temp[f]=temp[l]; temp[l]=tmp; }p[pl] = temp [2]; p[p2] = temp [3];}

static String GA(int[][] adjM,String colors.int N){int maxfitness, mfi = 0;String [] p = new String [population] ;char [] temp = new char [N] ;for(int i=0;i<population;i++)

{for( in t j=O;j<N;j++){temp[j] =

colors.charAt((int)((Math.randomO *colors . length() ) ) ) ;}p[i] = new String (temp) ;}maxf itness=f itness(adjM,p[O] ,p[0] . length()) ;while(maxfitness!=0){mutate(p,colors); crossing(p.adjM);for(int i=0;i<p.length;i++){

if(fitness(adjM,p[i],p[i].lengthO)<maxfitness)imaxf itness=f itness(adjM,p[i] ,p[i] .lengthO) ;mfi = i;>}}return p[mf i] ;}

}

12.9 Problems with Constraints

12.9.1 Introduction

Thus far, we have only discussed genetic algorithms for searching unconstrainedobjective functions. Many practical problems contain one or more constraints thatmust also be satisfied. A typical example is the traveling salesman problem, whereall cities must be visited exactly once. In a more mathematical formulation, the

Page 409: Nonlinear Workbook (3rd)

390 CHAPTER 12. GENETIC ALGORITHMS

traveling salesman problem is stated as follows. For a given n x n distance matrixC = (cy), find a cyclic permutation TT of the set { 1 , 2 , . . . , n} that minimizes thefunction

n

i=l

The value c(ir) is usually referred to as the length (or cost or weight) of the per-mutation 7T. The traveling salesman problem is one of the standard problems incombinatorial optimization and has many important applications like routing orproduction scheduling with job-dependent set-up times. Another example is theknapsack problem, where the weight which can be carried is the constraint. Thenorm of an n x n matrix over the real numbers R is given by

\\A\\ := sup p x | | .IWI=i

This is a problem with the constraint ||x|| = 1, i.e. the length of the vector x € R"must be 1. This problem can be solved with the Lagrange multiplier method, sincethe functions in this problem are differentiable (see chapter 14).

The most difficult problem in genetic algorithms is the inclusion of constraints. Con-straints are usually classified as equality or inequality relations. Equality constraintsmay be included into the system. It would appear that inequality constraints poseno particular problem. A genetic algorithm generates a sequence of parameters to betested using the system model, objective function, and the constraints. We simplyrun the model, evaluate the fitness function, and check to see if any constraints areviolated. If not, the parameter set is assigned the fitness value corresponding to theobjective function evaluation. If constraints are violated, the solution is infeasibleand thus does not have a fitness. This procedure is fine except that many practicalproblems are highly constrained; finding a feasible point is almost as difficult as find-ing the best. As a result, we usually want to get some information out of infeasiblesolutions, perhaps by degrading their fitness ranking in relation to the degree ofconstraint violation. This is what is done in a penalty method. In a penalty method,a constrained problem in optimization is transformed to an unconstrained problemby associating a cost or penalty with all constraint violations. This cost is includedin the objective function evaluation.

Consider, for example, the original constrained problem in minimization form

minimize g(x) subject to

hi{x)>0, i = l,2,...,n

where x is an m vector. We transform this to the unconstrained form:

minimize

$(x)+rf>[A,-(x)]2 = 1

Page 410: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 391

where $ is the penalty function and r is the penalty coefficient. Other approachesuse decoders or repair algorithms.

A detailed discussion of problems with constraints is given by Michalewicz [69]. Heproposes that appropriate data structures and specialized genetic operators shoulddo the job of taking care of constraints. He then introduces an approach to handleproblems with linear constraints (domain constraints, equalities, and inequalities).We consider here the knapsack problem and traveling salesman problem applyinggenetic algorithms.

12.9.2 Knapsack ProblemFormally, the knapsack problem can be stated as follows.

Problem. Given M, the capacity of the knapsack,

{ Wi | Wi > 0, i = 0 , 1 , . . . , n — 1 }

the weights of the n objects, and

{vi\vi>0, i = 0 , l , . . . , n - l }

their corresponding values,

r i - l n - 1

maximize ^ ViXi subject to J^ WiXi < Mi=0 i=0

where a;, £ {0,1}. Here Xi = 0 means that item i should not be included in theknapsack, and x, = 1 means that it should be included.

As an example for the knapsack problem we consider the following problem. Ahiker planning a backpacking trip feels that he can comfortably carry at most 20kilograms. After laying out all the items that he wants to take and discovering thattheir total weight exceeds 20 kilograms, he assigns to each item a "value" rating, asshown in the Table. Which items should he take to maximize the value of what hecan carry without exceeding 20 kilograms ?

Table. An Instance of the Knapsack ProblemCanteen Change of Camp Sleeping Dried

Item Tent (filled) clothes stoves bag foodWeight 11 7 5 4 3 3Value 20 10 11 5 25 50

First-aid Mosquito Rain WaterItem kit repellent Flashlight Novel gear purifier

Weight i3 2 2 2 2 IValue I 15 12 6 4 5 30

Page 411: Nonlinear Workbook (3rd)

392 CHAPTER 12. GENETIC ALGORITHMS

Although we do not know yet how to obtain the solution, the way to fill the knap-sack to carry the most value is to take the

sleeping bag, food, mosquito repellent, first-aid kit, flashlight, water purifier, andchange of clothes,

for a total value of 149 with a total weight of 19 kilograms. An interesting aspectof the solution is that it is not directly limited by the weight restriction. There areways of filling the knapsack with exactly 20 kilograms, such as substituting for thechange of clothes the camp stove and rain gear, but this decreases the total value.

The following C++ program uses a genetic algorithm to solve the problem. We usethe header file b i tvect .h given above.

// knapsack.cpp

#include <iostream>#include <fstream>#include <ctime>#include <cstdlib>#include "bitvect.h"using namespace std;

struct item{char name[50];

double weight, value;

};

void readitems(char *file,item *&list,int fen.double &max)

{ifstream data(file);data » n;l ist = new item[n];for(int i=0;i<n;i++){data » list [i].name;data » list[i].weight;data » list[i].value;>data >> max;

}

void destroyitems(item *list) { delete[] list; }

Page 412: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 393

double value(const BitVector fcb.int n,double max,item *list)

{double tweight =0.0, tvalue =0.0;for(int i=0;i<n;i++){if(b.GetBit(i))

itweight += list[i].weight;

tvalue += list[i].value;

>if(tweight > max) { tvalue = -1.0; i = n; }>return tvalue;

>

void mutate(BitVector *farm,int m.int n.item *list,double max)

{const int tries = 1000;int animal = randO'/.m;int i = 0, pos, pos2;BitVector* newanim = new BitVector(farm[animal]);pos2 = pos = randO'/.n;newanim -> ToggleBit(pos);

while (ictries)

{while(pos2 == pos) pos2 = rand()%n;newanim -> ToggleBit(pos2);if(value(*newanim,n,max,list) > 0) i=tries;else { newanim -> ToggleBit(pos2);i++;pos2=pos; >}if(value(*newanim,n,max,list)>value(farm[animal],n,max,list))farm[animal] = *newanim;delete newanim;

>

void crossing(BitVector *farm,int m.int n.item *list,double max)

iconst int tries = 1000;int animal 1 = rand()'/,m; int animal2 = rand()°/,m;

int pos;

while(animal2 == animall) animal2 = rand()'/.m;

BitVector *newaniml = new BitVector(farm[animall]);

Page 413: Nonlinear Workbook (3rd)

394 CHAPTER 12. GENETIC ALGORITHMS

BitVector *newanim2 = new BitVector(farm[animal2]);pos = rand()'/,n;

for(int i=pos;i<n;i++)

{newaniml -> SetBit(i,farm[animal2][i]);newanim2 -> SetBit(i,farm[animall] [i]);}if(value(*newaniml,n,max,list) > value(farm[animall],n,max,list))farm[animall] = *newaniml;if(value(*newanim2,n,max,list) > value(farm[animal2],n,max,list))farm[animal2] = *newaniml;delete newaniml; delete newanim2;

}

void setupfarm(BitVector *farm,int m.int n.item *list,double max)

•C

const int tries = 2000;double temp;int i, j, k;srand((unsigned long) time(NULL));for(i=0;i<m;i++){for(j=0;j<n;j++) farm[i].SetBit(j,O);temp =0.0;k = 0;while((temp < max) && (k < tries))

{j = rand()7.n;if (!farm[i] .GetBit(j)) temp+=list[j] .weight;if (temp < max) farm[i] .SetBit(j);k++;

}}

}

int main(void)

{item* list = NULL;int n, m = 100, i, iterations = 500, besti = 0;double max, bestv = 0.0, bestw =0.0, temp;BitVector *farm = new BitVector[m];readitemsC'knapsack.dat",list,n,max);

for(i=0;i<m;i++)

Page 414: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 395

farm[i] .SetLength(n);setupfarm(farm,m,n,list,max);

for(i=0;Kiterations;i++)

{crossing(farm,m,n,list,max);mutate(farm,m,n,list,max);

>

for(i=0;i<m;i++)if ((temp=value(farm[i] ,n ,max,l is t)) > bestv){ bestv=temp; bes t i=i ; }

cout « "Items to take: " « endl;

for(i=0;i<n;i++)

{if(farm[besti].GetBit(i)){cout << list[i].name << "," « endl;bestw += list[i].weight;}}cout « endl;cout « "for a weight of " << bestw

« "kg and value of " « bestv «endl;delete[] farm;destroyitems(list);return 0;

>

The input file knapsack.dat is

12tent 11 20canteen_(filled) 7 10change_of_clothes 5 11camp_stoves 4 5sleeping_bag 3 25dried_food 3 50first-aid_kit 3 15mosquito_repellent 2 12flashlight 2 6novel 2 4rain_gear 2 5water_purifier 1 3020

Page 415: Nonlinear Workbook (3rd)

396 CHAPTER 12. GENETIC ALGORITHMS

The output is

Items to take :

change_of_clothes,sleeping_bag,dried_food,f i rs t -a id_ki t ,mosquito_repellent,flashlight,water_purifier,

for a weight of 19kg and value of 149

The knapsack problem can also be solved using dynamic programming. Considerthe following problem

Item (j) 1 Weight (w;) 1 Benefit (bj)1 2 652 3 803 1 1 | 30

and suppose the capacity of the knapsack is 5. Let the stages be indexed by w, theweight filled. The decision is to determine the last item added to bring the weightto w. There is just one state per stage. Let f(w) be the maximum benefit that canbe gained from a w kg knapsack. For item j the following relates f(w) to previouslycalculated / values

f(w) = max{ bj + f(w - Wj) } .

To fill a w kg knapsack, we must end off by adding some item. If we add item j , weend up with a knapsack of size w — Wj to fill. Thus we have

/(0) = 0, /(I) = 30

add item 3.

/(2) = max{ 65 + /(0) = 65, 30 + /(I) = 60 } = 65

add item 1.

/(3) = max{ 65 + /(I) = 95, 80 + /(0) = 80, 30 + /(2) = 95 } = 95

add item 1 or 3

/(4) = max{ 65 + /(2) = 130, 80 + /(I) = 110, 30 + /(3) = 125 } = 130

add item 1

/(5) = max{ 65 + /(3) = 160, 80 + /(2) = 145, 30 + /(4) = 160 } = 160

add item 1 or 3.

This gives a maximum for the benefit of 160, which is gained by adding 2 of theitem 1 and 1 of the item 3.

Page 416: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 397

12.9.3 Traveling Salesman Problem

The traveling salesman problem is a combinatorial optimization problem. Manycombinatorial optimization problems like the traveling salesman problem can beformulated as follows. Let

T = { l ' l , * 2 i • • • , i n }

be some permutation from the set

{1 ,2 , . . . , «} .

The number of permutations is n\. Let Q be a space of feasible solutions (states)and /(TT) the optimality function (criterion). It is necessary to find ir* such that

7T* = {i*i, i*2, • • • ,i*n } = arg{ /(TT) -» min } .

The structure of fi and f(n) depends on the problems considered. A typical prob-lem is the traveling salesman problem. The traveling salesman problem is simple tostate. Given the distances separating a certain number of towns the aim is to findthe shortest tour that visits each town once and ends at the town it started from.There are several engineering and scientific problems equivalent to a traveling sales-man problem. The problem is of practical importance. The number of all possibletours is finite, therefore in principle the problem is solvable. However, the bruteforce strategy is not only impractical but completely useless even for a moderatenumber of towns n, because the number of possible tours grows factorially withn. The traveling salesman problem is the best-known example of the whole classof problems called NP-complete (or NP-hard), which makes the problem especiallyinteresting theoretically. The NP-complete problems are transformable into eachother, and the computation time required to solve any of them grows faster thanany power of the size of the problem. There are strong arguments that a polynomialtime algorithm may not exist at all. Therefore, the aim of the calculations is usuallyto find near-optimum solutions.

The following C++ program permutation, cpp finds all permutations of the num-bers 1,2,..., n. We choose n = 3. The array element p[0] takes the value 0 at thebeginning of the program. The end of the evaluation is indicated by p[0] = 1.

// permutation.cpp

#include <iostream>using namespace std;

int main(void)iint i, j, k, t;unsigned long n = 3;int* p = new int[n+l];

Page 417: Nonlinear Workbook (3rd)

398 CHAPTER 12. GENETIC ALGORITHMS

II starting permutation/ / identity 1 ,2, . . . , n - > l , 2, . . . , nfor(i=0;i<=n;i++){p[i] = i;cout « "p[" « i « "] = " « p[i] « " ";>cout « endl;

int test = 1;do{i = n-1;while(p[i] > p[i+l]) i = i -1 ;if(i > 0) test = 1; else test = 0;

j = n;while(p[j] <= p[i]) j = j - 1;

t = p[i]; p[i] = p[j]; p[j] = t;i = i+1; j = n;while(i < j){t = p[i]; p[i] = p[j]; p[j] = t;i = i+1; j = j - 1 ;>/ / display resultfor(int tau=0;tau<=n;tau++)cout « "p[" « tau « "] = " « p[tau] « " ";cout << endl;} while(test == 1);delete [] p;return 0;

}

Goldberg and Lingle [35] suggested a crossover operator, the so-called partiallymapped crossover. They believe it will lead to an efficient solution of the travelingsalesman problem. We explain with an example how the partially mapped operatorworks. Assume that we have 12 cities and assume that the parents are

(1 2 3 4 5 6 7 8 9 10 11 12) : al(7 3 6 11 4 12 5 2 10 9 1 8) : a2

al and a2 are integer arrays. Positions count from 0 to n — 1, where n = 12. Weselect two random numbers r\ and r2

0 < r l < ( n - l ) , 0 < r 2 < ( n - l ) , r\ < r2

Page 418: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 399

Let r l = 3, r2 — 6. Truncate parents using r\ and rl.

(1 2 3 I 4 5 6 7 I 8 9 10 11 12)(7 3 6 I 11 4 12 5 I 2 10 9 1 8)

We obtain the subarrays s i = ( 4 5 6 7 ) and s2 = (11 4 12 5) . Next we do thecrossing

(1 2 3 I 11 4 12 5 I 8 9 10 11 12)(7 3 6 1 4 5 6 7 1 2 10 9 1 8)

Now some cities occur twice while others are missing in the new array. The crossingdefines the mappings

11 -> 4 4 -> 5 12 -> 6 5 -> 7 (*)4 -> 11 5 -> 4 6 -> 12 7 -> 5 (**)

Positions which must be fixed are indicated by x

(1 2 31 11 4 12 5 I 8 9 10 x x)( x 3 x | 4 5 6 7 I 2 10 9 1 8)

We fix the first array using the mapping (*).

a) number 11 at position 10 must be fixed

i) map 11 (—» 4 but 4 is in array s2

ii) map 4 i—>• 5 but 5 is in array s2

iii) map 5 i-4 7 o.k. 7 is not in array s2

Thus replace number 11 at position 10 by number 7.

a) number 12 at position 11 must be fixed

i) map 12 i-^ 6 o.k. 6 is not in array s2

Thus replace number 12 at position 11 by number 6.

a) number 7 at position 0 must be fixed

i) map 71-4 5 but 5 is in array s i

ii) map 5 H-» 4 but 4 is in array s i

iii) map 4 M- 11 o.k. 11 is not in array s i

Thus replace number 7 at position 0 by number 11

Page 419: Nonlinear Workbook (3rd)

400 CHAPTER 12. GENETIC ALGORITHMS

b) number 6 at position 2 must be fixed

i) map 6 i—>• 12 o.k. 12 is not in array si

Thus replace number 6 at position 2 by number 12. Consequently, the children are

(1 2 3 11 4 12 5 8 9 10 7 6)(11 3 12 4 5 6 7 2 10 9 1 8)

Bac and Perov [3] proposed another operator of crossings using the permutationgroup. We illustrate the operator with an example and a C++ program. Assumethat we have ten cities. Let the parents be given by

(0 1 2 3 4 5 6 7 8 9) -> (8 7 3 4 5 6 0 2 1 9 ) parent 1

(0 1 2 3 4 5 6 7 8 9) -> (7 6 0 1 2 9 8 4 3 5) parent 2

The permutation map yields

0 -> 8 -> 31 -> 7 -> 42 -> 3 -> 1

etc.. Thus the children are given by

(0 1 2 3 4 5 6 7 8 9) -> (3 4 1 2 9 8 7 0 6 5)(0 1 2 3 4 5 6 7 8 9) -> (2 0 8 7 3 9 1 5 4 6)

The implementation of this permutation is straightforward.

/ / tsppermutation.cpp

#include <iostream>

using namespace std;

void crossing(int* al.iivt* a2,int* a3,int* a4,int n)

•C

for(int i=0;i<n;i++){ int p = al [i]; a3 [i] = a2 [p]; }for(int j=O;j<n;j++)•C int q = a2[j]; a4[j] = al[q]; }

}

int main(void)

{int n = 10;int* al = new int [n] ; int* a2 = new int [n] ;int* a3 = new int [n] ; int* a4 = new int [n] ;

Page 420: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 401

al[0] = 8; al[l] = 7; al[2] = 3; al[3] = 4;al[4] = 5; al[5] = 6; al[6] = 0; al[7] = 2;al[8] = 1; al[9] = 9;a2[0] = 7; a2[l] = 6; a2[2] = 0; a2[3] = 1;a2[4] = 2; a2[5] = 9; a2[6] = 8; a2[7] = 4;a2[8] = 3; a2[9] = 5;crossing(al,a2,a3,a4,n);

for(int i=0;i<n;i++){cout « "a3[" « i « "] = " « a3[i] « " ";if (((i+l)'/.2) == 0) { cout « endl; }}for(int j=O;j<n;j++)•C

cout « "a4[" « j « "] = " « a4[j] « " ";if(((j+l)*/.2) == 0) { cout « endl; }>delete [] al; delete [] a2;delete [] a3; delete [] a4;return 0;

}

In the following program tsp.cpp we use these operators to find solutions to thetraveling salesman problem.

/ / tsp.cpp

#include <iostream>#include <fstream>#include <cstdlib>#include <ctime>#include "bitvect.h"using namespace std;

void readdist(char* filename,double**& dist.intfe cities)•C

int i, j;ifstream d(filename);d » cities;dist = new double*[cities];f or (i=0; Kcities; i++)dist[i] = new double [cities] ;f or (i=0; Kcities; i++)for(j=i+l;j<cities;j++)

Page 421: Nonlinear Workbook (3rd)

402 CHAPTER 12. GENETIC ALGORITHMS

id » dist[i][j];dist[j][i] = dist[i][j];>for(i=0;i<cities;i++) dist[i] [i]=O;cout « "d[0][0] = " « dist[0][0] « endl;d.closeO ;

>

void destroydist(double **dist,int cities)

ifor(int i=O;i<cities;i++) delete [] dist[i];

delete [] dist;}

double distance(int *seq,int cities,double **dist)

{double sumdist = 0.0;for(int i=l;i<cities;i++)sumdist += dist[seq[i]] [seq[i-l]];sumdist += dist [seq[0]] [seq[cities-l]] ;return sumdist;

}

void setupfarm(int **farm,int n.int cities)

{BitVector used(cities);int city,i,j;srand(time(NULL));for(i=0;i<n;i++)

•C

for(j=0;j<cities;j++) used.SetBit(j,0);

for(j=0;j<cities;j++)

{city = rand()'/,cities;

if('used.GetBit(city)) { farm[i] [j]=city;used.SetBit(city); >else j — ;}

}}

void mutate(int **farm,int n.int cities,double **dist){

int seq = randO'/.n;int posl = rand()'/,cities;

Page 422: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 403

int pos2 = rand 0 ' / .ci t ies;while(pos2 == posl) pos2 = randO'/.cities;int *mutated = new i n t [ c i t i e s ] ;for( in t i=0;i<cit ies; i++) mutated[i] = farm[seq] [i] ;mutated [posl] = farm[seq] [pos2] ;mutated[pos2] = farm[seq] [posl] ;i f (d is tance(farm[seq] ,c i t ies ,d is t ) > dis tance(mutated,ci t ies ,dis t ) ){delete farm [seq] ; farm[seq] = mutated;}else delete mutated;

>

void permutate(int** farm,int n.int cities,double** dist)

{int seql = rand()'/,n; int seq2 = rand()%n;int *resultl, *result2, *result3, *result4;while(seq2 == seql) seq2 = rand()'/,n;int *childl = new int [cities];int *child2 = new int[cities];for(int i=0;i<cities;i++)

{childl[i] = farm[seq2] [farm[seql] [i]] ;child2[i] = farm [seql] [farm [seq2] [i]] ;>if(distance(farm[seql].cities,dist) > distance(childl,cities,dist))resultl = childl;else resultl = f arm[seql] ;if(distance(farm[seq2].cities,dist) > distance(child2,cities,dist))result2 = child2;else result2 = f arm[seq2];result3 = ((resultl == farm[seql])?childl:farm[seql]);result4 = ((result2 == farm[seq2])?child2:farm[seq2]);farm[seql] = resultl; farm[seq2] = result2;delete [] result3; delete [] result4;

}

int insequence(int el,int *seq,int pi,int p2){

for(int i=pl;i<p2;i++)if(seq[i] == el) return i;return -1 ;

}

void pmx(int **farm,int n.int cities,double **dist)

Page 423: Nonlinear Workbook (3rd)

404 CHAPTER 12. GENETIC ALGORITHMS

{int i, pos;int seql = rand()'/.n; int seq2 = rand()'/,n;int *resultl, *result2, *result3, *result4;while(seq2 == seql) seq2 = rand()'/,n;int posl = rand()'/,cities; int pos2 = rand()'/,cities;while(pos2 == posl) pos2=rand()'/.cities;if(pos2<posl) { i = pos2; pos2 = posl; posl = i; }int *childl = new int[cities];int *child2 = new int[cities];for(i=0;i<cities;i++){if((i<pos2) &fc (i>=posl))

ichildl[i] = farm[seq2] [i] ;child2[i] = farm [seql] [i] ;}else{childl[i] = farm [seql] [i] ;child2[i] = farm[seq2] [i] ;}}

for(i=0;i<cities;i++){if((i<posl) II (i>=pos2))while((pos = insequence(childl[i],childl,posl,pos2)) >= 0)childl[i] = child2[pos];if((i<posl) II (i>=pos2))while((pos = insequence(child2[i],child2,posl,pos2)) >= 0)child2[i] = childl[pos];}

if(distance(farm[seql],cities,dist) > distance(childl,cities,dist))resultl = childl;else resultl = f arm[seql] ;if(distance(farm[seq2].cities,dist) > distance(child2,cities,dist))result2 = child2;else result2 = f arm[seq2] ;result3=((resultl == farm[seql])?childl:farm[seql]);result4=((result2 == farm[seq2])?child2:farm[seq2]);farm[seql] = resultl;farm[seq2] = result2;delete [] result3; delete [] result4;

Page 424: Nonlinear Workbook (3rd)

12.9. PROBLEMS WITH CONSTRAINTS 405

}

int main(void){

int N = 20; // number of animals/chromosomesint i, j;int iterations = 300;cout « N « endl;double** dist = NULL; // array of distancesint cities; /./ number of cities

readdist("tsp.dat",dist,cities);cout « "Cities: " « cities « endl;int** farm = new int*[N];for(i=0;i<N;i++) farm[i] = new int[cities];setupfarm(farm,N,cities);f or (i=0; Kiterations; i++)-Cmutate(farm,N,cities,dist);permutate(farm,N,cities,dist);pmx(farm,N,cities,dist);}for(i=0;i<N;i++){for(j=0;j<cities;j++) cout«farm[i] [j]«" ";cout « " distance:" « distance(farm[i].cities,dist) « endl;}

destroydist(dist,cities);

return 0;

}

The input file t sp .dat is:

814.541320.766313.505919.604110.41394.6097714.53446.341145.093139.12195512.0416

Page 425: Nonlinear Workbook (3rd)

406 CHAPTER 12. GENETIC ALGORITHMS

14.03578.7091910.493811.243218.374218.878814.2137.532612.662517.70719.7267715.472910.53617.211110.19810

A typical output is

Cities: 8

7 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85590 6 5 1 3 2 4 7 distance:66.18757 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85590 6 5 1 3 2 4 7 distance:66.18757 4 2 3 1 5 0 6 distance:64.85594 2 1 3 0 6 5 7 distance:67.98897 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85590 6 5 1 3 2 4 7 distance:66.18750 6 5 1 3 2 4 7 distance:66.18757 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 6 0 distance:66.18757 4 2 3 1 5 0 6 distance:64.85597 4 2 3 1 5 0 6 distance:64.8559

12.10 Simulated Annealing

Annealing is the process of cooling a molten substance with the objective of condens-ing matter into a crystalline solid. Annealing can be regarded as an optimization

Page 426: Nonlinear Workbook (3rd)

12.10. SIMULATED ANNEALING 407

process. The configuration of the system during annealing is defined by the set ofatomic positions 7V A configuration of the system is weighted by its Boltzmannprobability factor,

exp{-E(n)/kT)

where E(ri) is the energy of the configuration, k is the Boltzmann constant, and Tis the temperature. When a substance is subjected to annealing, it is maintained ateach temperature for a time long enough to reach thermal equilibrium.

The iterative improvement technique for combinatorial optimization has been com-pared to rapid quenching of molten metals. During rapid quenching of a moltensubstance, energy is rapidly extracted from the system by contact with a massivecold substrate. Rapid cooling results in metastable system states; in metallurgy, aglassy substance rather than a crystalline solid is obtained as a result of rapid cool-ing. The analogy between iterative improvement and rapid cooling of metals stemsfrom the fact that iterative improvement and rapid cooling of metals accepts onlythose system configurations which decrease the fitness function. In an annealing(slow cooling) process, a new system configuration that does not improve the costfunction is accepted based on the Boltzmann probability factor of the configuration.This criterion for accepting a new system state is called the Metropolis criterion.

If the initial temperature is too low, the process gets quenched very soon and onlya local optima is found. If the initial temperature is too high, the process is veryslow. Only a single solution is used for the search and this increases the chance ofthe solution becoming stuck at a local optimum. The changing of the temperatureis based on an external procedure which is unrelated to the current quality of thesolution, that is, the rate of change of temperature is independent of the solutionquality. These problems can be rectified by using a population instead of a singlesolution. The annealing mechanism can also be coupled with the quality of the cur-rent solution by making the rate of change of temperature sensitive to the solutionquality.

The simulated annealing procedure is presented below. Simulated annealing es-sentially consists of repeating the Metropolis procedure for different temperatures.The temperature is gradually decreased at each iteration of the simulated annealingalgorithm.

procedure simulated annealingbegin

t <- 0initialize temperature Tselect a current string v_c at randomevaluate the fitness of v_crepeatrepeatselect a new string v_n

Page 427: Nonlinear Workbook (3rd)

408 CHAPTER 12. GENETIC ALGORITHMS

in the neighbourhood of v_cby flipping a single bit of v_cif f(v_c) < f(v_n)then v_c <- v_n

else if random [0,1] < exp((f(v_n) - f(v_c))/T)then v_c <- v_nuntil (termination condition)T <- g(T,t)t <- t + 1until (stop-criterion)

end

In the following C++ program Annealing.cpp we use simulated annealing to findthe minimum of the function

f(x) = x2 sin(a;) exp(—z/15.0)

in the range [0,100].

/ / annealing.cpp

#include <iostream>#include <cmath> / / for sin, exp, fmod#include <cstdlib>using namespace std;

inl ine double f(double& x){

return sin(x)*x*x*exp(-x/15.0);}

inline void randval(double* s)

{static const double pi = 3.14159265;

*s = fmod((*s+pi)*(*s+pi)*(*s+pi)*(*s+pi)*(*s+pi),1.0);

}

inline int accept(doublefc Ecurrent.doublefc Enew.doubleft T,double& s)

{

double dE = Enew - Ecurrent;if(dE < 0.0) return 1;if(s < exp(-dE/T)) return 1;else return 0;

}

int main(void)

Page 428: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 409

•C

cout « "Finding the minimum via simulated annealing:" « endl;double xlow =0.0, xhigh = 100.0;double Tmax = 500.0, Tmin =1.0;double Tstep =0.1;double T;

double s = 0.118; // seedrandval(&s);

double xcurrent = s*(xhigh - xlow);double Ecurrent = f(xcurrent);

for(T=Tmax; T>Tmin; T=T-Tstep)

{randval(fes);double xnew = s*(xhigh-xlow);

double Enew = f(xnew);if(accept(Ecurrent,Enew.T.s) != 0)

{xcurrent = xnew;Ecurrent = Enew;

}} // end for loop

cout « "The minimum found is "« Ecurrent « " at x = " « xcurrent;

return 0;>

12.11 Parallel Genetic Algorithms

Genetic algorithms are good candidates for effective parallelization, given their pro-portional nature (Goldberg [34], Maniezzo [66], Cronje and Steeb [20]). However,the classical genetic algorithm is not straightforwardly parallelizable because of theneed, in the selection and in the crossover steps, for global statistics that result inhigh communication costs. Several approaches have been proposed to overcome thisproblem. The different models used for distributing the computation and the paral-lelization follow two main approaches: the island model and the fine-grained model.These two models naturally lend themselves to different parallel hardware: MIMDmachines, typically transputer-based, in the case of the island model, and SIMDmachines, array processors or Connection Machines, in the case of the fine-grainedmodel which has also proved to be very effective with transputer-based implemen-tations.

Page 429: Nonlinear Workbook (3rd)

410 CHAPTER 12. GENETIC ALGORITHMS

In the island model, several isolated subpopulations evolve in parallel, periodicallyexchanging their best individuals with the neighbouring subpopulations. This iscalled migration. In the fine-grained model (or neighbourhood model), a single pop-ulation evolves, each individual of which is placed in a cell of a planar (or toroidal)grid; selection and crossover are applied only between neighboring individuals on thegrid. This distributed model contains a notion of spatiality, in that the individualsof the population are assigned to a specific position in a given spatial environment,usually a two-dimensional grid implemented as a toroidal array in order to avoidborder effects. Thus a single population of individuals is maintained, over which aneighbourhood relation is defined that specifies spatial proximity among individuals.The operators of the genetic algorithm are seen as stochastic local updating rules:the resulting model is thus fully distributed with no need for any global control struc-ture. Selection is performed, cell by cell, only over the individual assigned to thecell and its neighbours; crossover mates neighbouring individuals, while mutationis standard. The basic algorithm shared by all systems following the fine-grainedmodel is as follows:

1: Randomly generate an initial population of individuals and assign each of themto a cell of a grid. Then compute the fitness of each individual.2: For each cell cy- of the grid execute steps 3, 4, 5, and 6.3: Select the new individual to occupy c^ among those assigned to c^ and to itsneighbours.4: Randomly select an individual in the neighbourhood of c^, and with probabilitypc, recombine it with that assigned to cy-; assign one of the offspring to cy.5: Mutate the individual assigned to cy with probability pm.6: Compute the fitness of the new individual assigned to cy-.7: If not end.test goto Step 2.

The end_test is usually a test on the number of generations or on the time-length ofthe run, but it could also cover more subtle phenomena, such as search stagnation.The fine-grained approach provides a more careful exploration of the search spacedue to its local selection mechanism that alleviates the selection pressure. Easierproblems are therefore solved less efficiently, as the extra exploration representsonly extra computational load with no reward, while more difficult problems benefitfrom this thorough search. The optimal neighbourhood size depends on the problemfaced, but even small neighbourhoods (9 or 25 cells, centered on the current one)provide a robust choice ensuring good performance.

Next we show that genetic algorithms can be implemented using a Parallel VirtualMachine and C++. The Parallel Virtual Machine PVM system is a general andflexible computing resource that supports a message passing model of computa-tion. This resource may be accessed at three different levels: the transparent modein which tasks are automatically executed on the most appropriate hosts (generallythe least loaded computer), the architecture-dependent mode where the programmer

Page 430: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 411

can indicate on which architectures certain tasks are to be executed, and lastly thelow-level mode in which a particular host may be specified for a certain task. Suchlayering permits flexibility while retaining the ability to exploit particular strengthsof individual machines on the network. Application programs under PVM may pos-sess arbitrary control and dependency structures. This means that at any point intime during the execution of a concurrent application, the processes comprising theapplication may have arbitrary relationships between each other, and in addition,any process may communicate and/or synchronize with any other process. Thereare two typical structures for doing distributed computations, namely the single-program multiple-date (SPMD) model in which all processes are identical and themaster-slave model in which a set of computational slave processes performs work forone or more master programs. In a master-slave model, the master program spawnsand directs a number of slave programs which perform computations. The mas-ter program first enrolls itself into the virtual machine by calling pvm_mytid(). Itthen calls pvm_spawn() to create a number of slaves processes on a number of otheravailable machines. Instructions are then passed from the master program to theslave programs and the answers for these "queries" are then received from the slaves.

Due to their nature, genetic algorithms can benefit especially from parallel tech-niques. Also the master-slave model serves very well as a paradigm for implementinggenetic algorithms. Our example program therefore makes use of the master-slavemodel. A master program spawns several slave tasks that perform the actual geneticcomputations. The master program does little real work in our case, and merelyspawns the slaves and receives the final set of animals. The slave programs areall identical, and communicate among each other as peers, thus they resemble theSPMD more. In our implementation a farm of specified size is created by each ofiV slave tasks. Each slave performs the genetic operations a prescribed number oftimes and optionally exchanges random segments of the farm with neighbouringslaves after every one hundred generations. Each task then sends their collectionsof animals to the master task. Thus the total size M x N (where N is the numberof slaves tasks) of the farm remains constant. An extension would be to merge thefarms sent from the slaves and select M animals with the highest fitness.

We first discuss the master program. The first thing it does is to enroll itself into thePVM system. This is done with the command pvm_mytid(). This function requestsa new process id from the PVM system. We now ask the user how he would like theslave programs to be configured. This information is then used to spawn the slaveprograms. The slave programs are created with the function call

pvm_spawn(char* task,char** arg.int flag,char* where,int ntastk.int* tids)

Here task is the name of the slave program, and argv is any command line ar-guments we wish to pass to the slave program. The flag and where combinationallows us to specify how and where the slave task is to be executed. This allows useto start a task on a particular architecture or even on a specific host. In the sample

Page 431: Nonlinear Workbook (3rd)

412 CHAPTER 12. GENETIC ALGORITHMS

program we use a flag = 0 and where == "". This means the PVM will decidewhere to spawn the task. The argument ntask specify the number of tasks we wishto spawn and t ids provides a integer array in which the task IDs are stored. Nowwe are ready to send data to the slave programs. This is done with the functionspvm_initsent(), pvm_pkxx() and pvm_mcast(). The function

pvm_initsent(int encoding)

initializes the message buffer. The encoding specifies whether the data must beencoded and if so how. By default the PVM uses the XDR encoding. This mustbe used when different architectures are used in the virtual machine. In our casehowever, we use the PvmDataRaw data format. When using this format, PVM doesnot perform any encoding on the data and therefore packs and unpacks faster. Thisis allowable in our case, since the computer network used consists solely of Pentiumsystems running under Linux. The data we wish to send to all the slave programsneed to be packaged into this message. To do this the function

pvm_pkint(int* ip.int nitem.int stride)

is used. This function packs an array of integers into the message buffer. Thus asingle integer is packed as an array of length 1. We sent the number of animals, thegene length, the number of iterations and a flag, indicating whether we want to slavetasks to exchange animals, to the slave programs. Lastly the function pvm_mcast()is used to send the same message to all the tasks we spawned earlier. The masterprogram now has to wait for the slave programs to finish their tasks. This is donewith a blocking receive loop. This means we wait for the same number of messagesthat we sent earlier. The

pvm_recv(int tid.int msgtag)

function is used with the arguments —1 and 5. This means that it waits for mes-sage number 5 and this message may come from any process. When a message isreceive, the data is unpacked in the same order in which it was packed using thepvm_upkxxx() function corresponding to the pvm_pkxxx() function that was usedto pack the data. The parts of the slave program that uses the PVM works inexactly the same way as the master program. First we enroll in the PVM system,and then instead of spawning processes, we wait for the data from the master pro-gram. When a message is received from the master program, the data needs to beunpacked in the same order that it was packed by the master program. After therequired number of iterations have been completed, the whole farm is sent back tothe master program. This operation is performed by the function pvm_pkAnimal.This function takes the same arguments as the built-in PVM packing functions, andin fact encapsulates two PVM function calls to pack the data members of an Animalobject. The bitstring is sent as a C string. The bitstring itself is a char array witha terminating null character, and can thus be sent without any modifications. Alsothe numerical value of the bitstring is sent. All the animals are displayed by the

Page 432: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 413

master program, along with their numerical value and fitness. The fitness functionis given by

f(x) =sin(a;)a;2exp(-a;2/15.0).

The range of x is between xO = —20 and xl = 20.

A class (abstract data type) called Animal has been defined which encapsulates thebitstring and all the operations that can be performed on it. The operators whichhave been defined are:

• breed () - this creates a new animal

• mutate () - this flips one random bit in the animal

• operator * () - this generates two new animals given two parent animals (i.e.,crossing), the two fittest animals of the resulting four animals are returned

• map() - this calculates the numerical value of the animal

• (char*) - this operator was defined to cast the animal to a C string

• « - the C++ ostream operator was overloaded to use the abstract data typeAnimal in cout calls.

The crossing operator cross () has a very simple implementation. It could bemodified so that there is a certain probability that the third fittest survives.

/ / animal.h

#include <iostream>#include <cmath>#include <cstdlib>#include <cstdio>#include <cstring>#include "pvm3.h"using namespace std;

// Declaration part// Utility functionsdouble f(double x); // The function we want to maximizevoid set_powers(int n); // Generate a lookup table for powers of twodouble pow(int n); // Return a power of twoint Random(int n); // Generate a random number

// The Animal classclass Animal

Page 433: Nonlinear Workbook (3rd)

414 CHAPTER 12. GENETIC ALGORITHMS

{private:

char* bitstring = NULL;int length;double x;void map();void breed(int);

public:Animal();AnimaKconst Animalfe) ; // copy constructorAnimalft operator = (const Animalfe); // overloaded assignment"Animal(); // destructor

// Type casting operatorsoperator double () const;operator char* () const;

// Gene manipulation functionsvoid operator * (Animalfe); // crossingvoid mutateO; // mutation

// static data membersstatic int default_length;static double xO; static double xl;

// friend functions

friend int pvm_pkAnimal(Animal*,int,int);

friend int pvm_upkAnimal(Animal*,int,int);

>;

// PVM packing and unpacking routinesint pvm_pkAnimal(Animal*,int,int);int pvm_upkAnimal(Animal*,int,int);

// stream outputostreamfe operator « (ostreamfe stream,Animalfe inobj);

// Definition part

// fitness function

double f(double x)

ireturn sin(x)*x*x*exp(-x*x/15.0);

}

double* powers = NULL;

Page 434: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 415

void set_powers(int n)

{

powers = new double[n];

for(int i=0;i<n;i++)

powers[i]=pow(2.0,i);

>

double pow(int n) { return powers[n]; }

int Random(int n){return (int)((double)n*(double)rand()/(double)RAND.MAX);

>

// PVM packing routinesint pvm_pkAnimal(Animal* a,int num.int stride) {.for(int i=0;i<num;i+=stride)

{pvm_pkstr(a[i].bitstring);pvm_pkdouble(&(a[i].x),1,1);}

>

int pvm_upkAnimal(Animal* a,int num.int stride)

{for(int i=O;i<num;i+=stride)

{pvm_upkstr(a[i] .bitstr ing);pvm_upkdouble(&(a[i].x,l,l);}

>

// Methods of class AnimalAnimal::Animal() : bitstring(NULL), length(Animal::default_length){breed(length);map();

>

Animal::Animal(const Animalfe al) : bitstring(NULL), length(O){ *this = al; }

Animal::"Animal()

Page 435: Nonlinear Workbook (3rd)

416 CHAPTER 12. GENETIC ALGORITHMS

if(bitstring != NULL)delete bitstring;

}

Animalfc Animal::operator = (const Animalft al)

iif(this != &al) // check for self assignment-Cif(bitstring != Null) delete bitstring;bitstring = new char[al.length+l] ;length = al.length;for(int i=O;i<length;i++)bitstring[i] = al.bitstring[i] ;bitstring[length] = 0;x = al.x;

}

return *this;

}

void Animal::breed(int N)

{if(bitstring != NULL) delete bitstring;bitstring = new char[N+l];for(int i=0;i<N;i++)bitstring[i] = (char)Random(2) + 48;

bitstring[N] = 0;

}

void Animal::map()

{double m = 0.0;for(int i=O;i<length;i++)m += ((double)(bitstring[i]-48))*pow(i);

x = xO + (xl - xO)*m/(pow(length-l)*2.0-1.0);

}

Animal::operator double () const { return x; }

Animal::operator char* () const

ichar* c = new char[length+l];for(int i=length;i>O;i—)c[length-i] = bitstring[i-l] ;c [length] = 0;return c;

Page 436: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 417

>

ostreamfc operator « (ostreamfe stream,Animalfe inobj)istream « (char*)inobj;

return stream;

>

void Animal::operator * (Animalfe al)

{int nl = Random(length); int n2 = Random(length);while(n2 == nl) n2 = Random(length);if(nl > n2) { int temp = nl; nl = n2; n2 = temp; >

Animal family[4];family [0] = *this;family [1] = al;

for(int i=0;i<nl;i++)

{family [2] .bitstring[i] = bitstringEi] ;family [3] .bitstring[i] = al .bitstring[i] ;}for(int i=nl;i<n2;i++){family[2] .bitstring[i] = al.bitstring[i];family [3] .bitstring[i] = bitstr ing [i] ;}for(int i=n2;i<length;i++) {family [2] .bitstring[i] = bitstr ing [i] ;family [3] .bitstring[i] = al.bitstring[i] ;}family[2] .mapO ; f amily[3] .map() ;

int fitness[4]; for(int i=0;i<4;i++) fitness[i] = i;for(int i=0;i<3;i++)for(int j=i+l;j<4;j++) {if (f (family[fitness[i]] .x) < f (family[fitness[j]] .x))iint temp = fitness[i];fitness [i] = fitness[j];fitness [j] = temp;}>*this = family [fitness [0] ] ;

Page 437: Nonlinear Workbook (3rd)

418 CHAPTER 12. GENETIC ALGORITHMS

al = family [fitness [1] ];>

void Animal::mutate0{int n = Random(length);Animal mutant = *this;

if(mutant.bitstring[n] == (char)l) mutant .bitstring[n] = (char)O;else mutant.bitstring[n] == (char)l;mutant .map();if(f(x) < f(mutant.x)) *this = mutant;

}

// Class staticsdouble Animal::xO = 0.0; double Animal::xl = 0.0;

int Animal::default_length = 0;

Next the main program follows.

// master.cpp

#include <iostream>#include <cstdlib>#include "pvm3.h" // for PVM functions#include "animal.h" // for Animal classusing namespace std;

int main(void)

{// enroll in pvmint mytid = pvm_mytid();// set number of slave farmsint nproc, animals, length, iterations, mix;double xO, xl;cout « "How many slave farms?\n"; cin » nproc;cout « "How many animals on each farm?\n"; cin » animals;cout << "How many bits in the gene?\n"; cin » length;cout « "How many iterations (multiples of 100)?\n";cin » iterations;cout « "Enable mixing?(0 or 1)"; cin » mix;cout « "Lower and upper limits?\n"; cin » xO; cin » xl;cout « "Lower and upper limits?\n"; cin » xO; cin >> xl;

// Start the slave farmsint* tids = new int[nproc];

Page 438: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 419

int numt = pvm_spawn("slave_farm",(char**)NULL,0,"",nproc,tids);

if(numt < nproc) {cerr « "Trouble spawning slaves. Aborting. !\n";for(int i=0;i<numt;i++) pvm_kill(tids[i]);pvm_exit();

ex i t ( - l ) ;}

// configure the Animal classAnimal::x0 = xO; Animal::xl = xl;Animal::default_length = length;

srand(time(0));

set_powers(length);

// configure the slave farmspvm_initsend(PvmDataRaw); // don't encode the datapvm_pkint(&animals,l,l); // pack the number of animalspvm_pkint(fclength,l,l); // pack the bitstring lengthpvm.pkint(alterations,1,1); // pack the number of iterationspvm_pkint(&mix,l,l); // enable mixing?pvm_pkdouble(&xO,l,l); // pack the lower limitpvm_pkdouble(&xl,l,l); // pack the upper limitpvm_mcast(tids,nproc,0); // send the info to all the farms

// number the slaves

for(int i=0;i<nproc;i++) {pvm_initsend(PvmDataRaw);pvm_pkint(ftnproc,1,1);pvm_pkint(tids,nproc,l);pvm_pkint(&i,1,1);pvm_send(tids[i] ,0) ;

}// receive results from slavesAnimal* Farm = new Animal[animals*nproc];for(int i=0;i<nproc;i++) {int farmno;pvm_recv(-l,5);

pvm_upkint(&farmno,1,1);pvm_upkAnimal(Farm+farmno*animals,animals,1);

}// print the resultsfor(int i=0;i<nproc;i++) {cout « "Farm " « i « "\n";

Page 439: Nonlinear Workbook (3rd)

420 CHAPTER 12. GENETIC ALGORITHMS

for(int j=0;j<animals;j++)

cout « Farm[i*animals+j] « "\t" « (double)Farm[i*animals+j]« " \t" « f((double)Farm[i*animals+j]) « "\n";

cout « "\n";}deleted Farm;delete tids;pvm_exit(); // all done stop PVM before exitingexit(O);

}

The slave_farm.cpp program is

/ / slave_farm.cpp

#include <iostream>#include <cmath>#include <cstdlib>#include <cstdio>#include <cstring>#include "pvm3.h"#include "animal.h"using namespace std;

int main(void)

{// enroll in pvmint ptid = pvm_parent();

// receive data from masterint animals, iterations, length, mix;double xO, xl;

pvm_recv(ptid,0);

pvm_upkint(&animals,l,1); // unpack the number of animalspvm_upkint(&length,l,l); // unpack the bitstring length

pvm_upkint(&iterations,l,1); // unpack the no of iterationspvm_upkint(tanix,l,l); // enable mixing?pvm_upkdouble(&xO,l,l); // unpack the lower boundpvm_upkdouble(&x1,1,1); // unpack the upper bound

// configure the Animal classAnimal::x0 = xO; Animal::xl = xl;Animal::default_length = length;

Page 440: Nonlinear Workbook (3rd)

12.11. PARALLEL GENETIC ALGORITHMS 421

// setup slave animal systemint nproc, my_no;int* tids = NULL;pvm_recv(ptid,O);pvm_upkint(fcnproc,1,1);tids = new int[nproc];pvm_upkint(tids,nproc,1);pvm_upkint(&my_no,1,1);

// create the farm and breed the animalssrand(pvm_mytid());set_powers(length);Animal* Farm = new Animal[animals];

int nol, no2;

// let the animals evolve for specified no of generationsfor(int i=0;i<iterations;i++) {for(int j=0;j<100;j++) {// mutate a random number of animalsint no = Random(animals/2);for(int k=0;k<no;k++) {nol = Random(animals);Farm [nol] . mutate 0 ;

}// cross two animalsnol = Random(animals);no2 = Random(animals);while(no2 == nol) no2 = Random(animals);Farm[nol]*Farm[no2] ;}if(mix) {II start the animal exchangeif(my_no == 0) {nol = Random(animals); no2 = Random(animals);while(no2 == nol) no2 = Random(animals);if(nol > no2) {int temp = nol; nol = no2; no2 = temp;

}pvm_initsend(PvmDataRaw);pvm_pkint(&nol,1,1);pvm_pkint(&no2,1,1);pvm_mcast(tids+l,nproc-l,l);}else

Page 441: Nonlinear Workbook (3rd)

422 CHAPTER 12. GENETIC ALGORITHMS

{pvm_recv(tids[O] ,1) ;pvm_upkint(ftnol,1,1);pvm_upkint(&no2,1,1);{

// send some of our animals on the next farmint dest = my_no + 1;if(dest == nproc) dest = 0;

pvm_initsend(PvmDataRaw);pvm_pkAnimal(Farm+nol,no2-nol,1);pvm_send(tids[dest],1);

// receive the neighbouring Farms' animalsint source = my_no-l;if(source == -1) source = nproc - 1;

pvm_recv(tids[source],1);pvm_upkanimal(Farm+nol,no2-nol,1);}}

// send result to masterpvm_initsent(PvmDataRaw);pvm_pkint(&my_no,1,1);pvm_pkAnimal(Farm,animals,1);pvm_send(ptid,5);delete[] Farm;delete tids;

// program finished exit PVM before stoppingpvm_exit();

}

Page 442: Nonlinear Workbook (3rd)

Chapter 13

Gene Expression Programming

13.1 Introduction

Whereas genetic algorithms and genetic programming are well-known in literature,gene expression programming (Ferreira [31], Hardy and Steeb [42]) is not yet well-known. Gene expression programming is a genome/phenome genetic algorithmwhich combines the simplicity of genetic algorithms and the abilities of genetic pro-gramming. In a sense gene expression programming is a generalization of geneticalgorithms and genetic programming. Gene expression programming is differentfrom genetic programming because the expense of managing a tree structure andensuring correctness of programs is eliminated. We provide an introduction in thefollowing.

A gene is a symbolic string with a head and a tail. Each symbol represents anoperation. The operation + takes two arguments and adds them. For example, +x2is the operation + with arguments x and 2 giving x+2. The operation * also takestwo arguments and multiplies them. The operation x would evaluate to the value ofthe variable x. The tail consists only of operations which take no arguments. Thestring represents expressions in prefix notation, i.e. 5-3 would be stored as - 5 3.The reason for the tail is to ensure that the expression is always complete. Supposethe string has h symbols in the head which is specified as an input to the algorithm,and t symbols in the tail which is determined from h. Thus if n is the maximumnumber of arguments for an operation we must have

h + t — 1 = hn.

The left hand side is the total number of symbols except for the very first symbol.The right hand side is the total number of arguments required for all operations. Weassume, of course, that each operation requires the maximum number of argumentsso that any string of this length is a valid string for the expression. Thus the equationstates that there must be enough symbols to serve as arguments for all operations.Now we can determine the required length for the tail t = h(n — 1) + 1.

423

Page 443: Nonlinear Workbook (3rd)

424 CHAPTER 13. GENE EXPRESSION PROGRAMMING

Example 1. Consider the symbols x, y and the operations *, + which take twoarguments. Let h = 5. Then t = 6 since n = 2. Thus a gene would be

+*xy2|xyy32y

The vertical line indicates the beginning of the tail. The expression is (x*y)+2. Thetree structure would be

Example 2. Suppose we use h = 8, and n = 2 for arithmetic operations. Thus thetail length must be t = 9. So the total gene length is 17. We could then representthe expression

cos(x2 + 2) - sin(a;)

with the string

-c+*xx2s1x1x226x31

The vertical I is used to indicate the beginning of the tail. Here c represents cosand s represents sin. We can represent the expressions with trees. For the exampleabove, the root of the tree would be '- ' with branches for the parameters. Thus wecould represent the expression as follows

Note that not all of the symbols from the tail are used. Only one symbol from thetail is used in the present example.

Page 444: Nonlinear Workbook (3rd)

13.1. INTRODUCTION 425

A chromosome is a collection of genes. The genes combine to form an expressionusing some operation with the same number of arguments as genes in the chromo-some. For example the expressions of genes of a chromosome may be added together.For operations applied to chromosomes we often concatenate the genes to obtain asingle string of symbols. For example, suppose we have the following genes forminga chromosome

-c+*xx2s1x1x226x31-c+*xx2s1x1x226x31-c++x2*xIxlx226x31

we form the chromosome by concatenation

-c+*xx2sIxlx226x31|-c+*xx2sIxlx226x31|-c++x2*xIxlx226x31

where I indicates the beginning of the head and tail portions of the genes. Weapply operations to chromosomes in a collection which is called the population ofchromosomes.

A number of operations are applied to chromosomes.

• Replication. The chromosome is unchanged. The roulette wheel selectiontechnique can be used to select chromosomes for replication.

• Mutation. Randomly change symbols in a chromosome. Symbols in the tailof a gene may not operate on any arguments. Typically 2 point mutations perchromosome is used. For example, let +*xy21 xyy32y. Then a mutation couldyield +*yy2|xyy32y or ++xy2|xyy32y or +*xy-|xyy32y.

• Insertion. A portion of a chromosome is chosen to be inserted in the head ofa gene. The tail of the gene is unaffected. Thus symbols are removed from theend of the head to make room for the inserted string. Typically a probabilityof 0.1 of insertion is used. As an example suppose +x2 is to be inserted into

-c+*xx2s1x1x226x31

at the fourth position in the head. We obtain

-c++x2*xIxlx226x31

which represents the expression

cos((a; + 2) + x2) - 1.

• Gene transposition. One gene in a chromosome is randomly chosen to bethe first gene. All other genes in the chromosome are shifted downwards inthe chromosome to make place for the first gene.

Page 445: Nonlinear Workbook (3rd)

426 CHAPTER 13. GENE EXPRESSION PROGRAMMING

• Recombination. The crossover operation. This can be one point (the chro-mosomes are split in two and corresponding sections are swapped), two point(chromosomes are split in three and the middle portion is swapped) or gene(one entire gene is swapped between chromosomes) recombination. Typicallythe sum of the probabilities of recombination is 0.7.

Another operation could be swapping. For example, consider again +*xy2|xyy32y.Then swapping the second and seventh position (counting from left and startingfrom zero) we obtain +*yy21 xyx32y.

In the following example we implement these operations. For simplicity we use onlyone gene in a chromosome and only one point recombination.

13.2 Example

In our example we consider smooth one-dimensional maps / : R —> R having theproperties

/(0) = 0, / ( I ) = 0, / ( ! ) = !

5 < ' G ) < ' - 5 < ' ( ! ) < ' -For example the logistic map g(x) := 4x(l — x) satisfies this set of properties. Weuse Gene Expression Programming to perform symbolic regression in order to obtainmaps which satisfy the above properties. We expect that the logistic map shouldbe found since we limit the functions in the implementation to polynomials. Wegeneralize the set of properties as follows. The points of evaluation are specified bya subset o f l x V where X and Y are given by / : X —¥ Y. Denote by F= c X x Ythe subset of all (x, y) € X x Y such that we require of / that f(x) — y, byF> C X x Y the subset of all (x,y) € X x Y such that we require of / thatf(x) > y, by F< C X x Y the subset of all (x, y) € X x Y such that we require of/ that f(x) < y. Thus we can define the fitness function of a function g (where asmaller value indicates a higher fitness)

fitness^) := £ \g{x)-y\+ £ \g(x) - y\H(y - g(x))(x,y)eF= (x,y)eF>

+ £ \g(x)-y\H(g(x)-y){x,y)eF<

whereHtx\ .= f l z > 0

' 10 otherwise

is the step function.

We apply gene expression programming until we find a function g such that

fitness (g) < e

Page 446: Nonlinear Workbook (3rd)

13.2. EXAMPLE 427

for given e > 0. A typical value of e is 0.001.

In the C++ program the function evalr() takes a character string and evaluates(i.e., returns type double) the corresponding function at the given point (the pa-rameter x of type double) using recursion. The function evalQ uses evalrOfor evaluation without modifying the pointer argument. Similarly p r in t rO andprint () are responsible for output of the symbolic expressions in readable form. Acharacter array and an array of type double are passed to the f i tness () function.The array of double consists of triplets: the point of evaluation, the expected valueand the comparison type. If the comparison type is zero then the goal is equality (i.e.we can use the absolute value of the difference between the expected value and theactual value), less than zero then the actual value should be less than the expectedvalue, greater than zero then the actual value should be greater than the expectedvalue. Finally, the function gep() implements the gene expression programmingalgorithm! As arguments gepO takes the data points (point of evaluation, andcomparison with a given value), the number of data points N, the size of the pop-ulation P and the desired fitness eps. The function strncpy of the string headerfile cstr ing is used to copy specific regions of the character strings representing thechromosomes. We need to specify the length since the chromosomes are not nullterminated.

We use 10 symbols (h = 10) for the head portion of the representations, thusthe total gene length is 21 (we use only addition, subtraction and multiplication).Since we only use i as a terminal symbol we obtain polynomials of order up to11, i.e. the highest order polynomial supported by the representation is x11. Werandomly choose terminals (symbols which do not take arguments, such as x) andnon-terminals (symbols which do take arguments, for example +) for the head andthen terminals for the tail. We use 0.1 for the probability of mutation, 0.4 forthe probability of insertion and 0.7 for the probability of recombination. At eachiteration of the algorithm we eliminate the worst half of the population.

// gepchaos.cpp

#include <iostream>#include <cstdlib>#include <ctime>#include <cmath> // for sin, cos#include <cstring>using namespace std;

const double pi = 3.1415927;const int nsymbols = 5;// 2 terminal symbols (no arguments) x and 1const int terminals = 2;// terminal symbols first

Page 447: Nonlinear Workbook (3rd)

428 CHAPTER 13. GENE EXPRESSION PROGRAMMING

const char symbols[nsymbols] = {'1','x','+','-','*'};const int n = 2; // for +,- and * which take 2 argumentsint h = 10;

double evalr(char *&e,double x){switch(*(e++)){case '1': return 1.0;case 'x': return x;case 'y': return pi*x;case 'c': return cos(evalr(e,x));case 's': return sin(evalr(e,x));case '+': return evalr(e,x)+evalr(e,x);case '-': return evalr(e,x)-evalr(e,x);case '*': return evalr(e,x)*evalr(e,x);default : return 0.0;}>

double evaKchar *e,double x){char *c = e;

return evalr(c.x);

>

void printr(char *&e)

iswitch(*(e++)){case '1': cout << '1'; break;case 'x': cout « 'x'; break;case 'y': cout « "pi*x"; break;case 'c': cout « "cos("; printr(e); cout « " ) " ; break;case 's': cout « "sin("; printr(e); cout « " ) " ; break;case '+': cout « '('; printr(e); cout « '+'; printr(e);

cout « ' ) ' ; break;case '-': cout « '('; printr(e); cout « '-'; printr(e);

cout « ' ) ' ; break;case '*': cout « '('; printr(e); cout « '*'; printr(e);

cout«')'; break;

>>

void print(char *e)

Page 448: Nonlinear Workbook (3rd)

13.2. EXAMPLE 429

{

char *c = e;printr(c);

>

double fitness(char *c,double *data,int N)

{double sum =0.0;double d;fordnt j=O;j<N;j++)

id = eval(c,data[3*j])-data[3*j+l];if(data[3*j+2] == 0) sum += fabs(d);else if(data[3*j+2] > 0) sum -= (d > 0.0)?0.0:d;else if(data[3*j+2] < 0) sum += (d < 0.0)?0.0:d;>

return sum;

>

// N number of data points// population of size P// eps = accuracy requiredvoid gep(double *data,int N,int P,double eps)iint i,j,k,replace,replace2,rlen,rp;int t = h*(n-l)+l;int gene_len = h+t;int pop_len = P*gene_len;int iterations = 0;char *population = new char[pop_len] ;char *elim = new char[P];int toelim = P/2;double bestf,f; // best fitness, fitness valuedouble sumf =0.0; // sum of fitness valuesdouble pmutate =0.1; // probability of mutationdouble pinsert =0.4; // probability of insertiondouble precomb = 0.7; // probability of recombinationdouble r.lastf; // random numbers and roulette wheel selectionchar* best = (char*)NULL; // best genechar* iter; // iteration variable

// initialize the populationfor(i=0;i<pop_len;i++)if (i'/.gene_len < h) population[i] = symbols[rand()'/.nsymbols];else population[i] = symbols[randQ'/.terminals] ;

Page 449: Nonlinear Workbook (3rd)

430 CHAPTER 13. GENE EXPRESSION PROGRAMMING

II initial calculationsbestf = fitness(population,data,N);best = population;for(i=0,sumf=0.0,iter=population;i<P;i++,iter+=gene_len)

•C

f = fitness(iter,data,N);sumf += f;if(f<bestf){bestf = f;best = population+i*gene_len;

>}

while(bestf >= eps)

{// reproduction// roulette wheel selectionfor(i=0;i<P;i++) elim[i] = 0;for(i=0;i<toelim;i++)

{

r = sumf*(double(rand())/RAND_MAX);lastf =0.0;for(j=0;j<P;j++){

f = fitness(population+j*gene_len,data,N);

if((lastf<=r) && (r<f+lastf)){elim[j] = 1;j = P;

>lastf += f;}}

for(i=0;i<pop_len;)

{if(population+i == best)i += gene_len; // never modify/replace best geneelse for(j=0;j<gene_len;j++,i++)

{// mutation or elimination due to failure in selection

// for reproductionif((double(randO)/RAND_MAX < pmutate) II elim[i/gene_len])

Page 450: Nonlinear Workbook (3rd)

13.2. EXAMPLE 431

if (i'/,gene_len < h)population [i] = symbols [randO'/.nsymbols] ;else population[i] = symbols[randO'/.terminals] ;

>

// insertionif(double(rand())/RAND_MAX < pinsert)

iII find a position in the head of this gene for insertion// -gene_len for the gene since we have already moved// to the next genereplace = i-gene_len;rp = rand()'/,h;// a random position for insertion sourcereplace2 = rand()7,pop_len;// a random length for insertion from the generlen = rand()'/.(h-rp) ;// create the new genechar *c = new char[gene_len];// copy the shifted portion of the headstrncpy(c+rp+rlen,population+replace+rp,h-rp-rlen);// copy the tailstrncpy(c+h,population+replace+h,t);// copy the segment to be insertedstrncpy(c+rp,population+replace2,rlen);// if the gene is fitter use itif(fitness(c,data,N) < fitness(population+replace,data,N))strncpy(population+replace,c,h);delete [] c;}

// recombinationif(double(rand())/RAND_MAX < precomb)

{// find a random position in the gene for one point recombinationreplace = i-gene_len;rlen = rand()'/.gene_len;// a random gene for recombinationreplace2 = (rand()'/,P)*gene_len;// create the new geneschar *c[5] ;c[0] = population+replace;c[l] = population+replace2;c [2] = new char [gene_len] ;c [3] = new char [gene_len];

Page 451: Nonlinear Workbook (3rd)

432 CHAPTER 13. GENE EXPRESSION PROGRAMMING

c[4] = new char[gene_len] ;strncpy(c[2] ,c[0] ,rlen) ;strncpy(c[2]+rlen,c[1]+rlen,gene_len-rlen);strncpy(c[3] ,c[l] ,rlen) ;strncpy(c[3]+rlen,c[0]+rlen,gene_len-rlen);// take the fittest genesfor(j=0;j<4;j++)for(k=j+l;j<4;j++)if (fitness (c[k] .data, N) < f itness(c[j] ,data,N)){strncpy (c [4] ,c[j] ,gene_len);strncpy (c[j] ,c[k] ,gene_len);strncpy (c[k] ,c[4] ,gene_len);}

delete [] c[2]; delete [] c[3]; delete [] c[4];}}

/ / fitnessfor(i=0,sumf=0.0,iter=population;i<P;i++,iter+=gene_len)

{f = fitness(iter,data,N);sumf += f;if(f < bestf){bestf = f;best = population+i*gene_len;

}}iterations++;

}

print(best);cout << endl;

cout « "Fitness of " « bestf « " after "

« iterations « " iterations." « endl;

delete[] population; delete[] elim;

>

int main(void)

{

srand(time(NULL)); // seed for the random number generatordouble data[] = { 0,0,0,1,0,0,0.5,1,0,0.25,0.5,1.0.75,0.5,1,

0.25,1, -1,0.75,1, -1 };

Page 452: Nonlinear Workbook (3rd)

13.2. EXAMPLE 433

gep(data,7,30,0.001);cout « endl;return 0;

>

We list some typical results below, all with fitness 0.

((1 - a;) * {(x + {{(x -x) + x) + x)) + x)) = 4z(l - x){{x + (((((((i -x)-x)-x)*x)-x)*x) + x)) + x) = x(l ~ x){2x + 3)(x - (((a: + x) * {(x - 1) + x)) - x)) = 4a;(l - a;)((((1 -x)*l)* (((1 + 1) + 1) + 1)) * x) = Ax{\ - x)((((1 + 1) - (((a; + x)*x) + x)) *x)+x)= x(l - x)(2x + 3)((1 * {(x - ({{{x + x) * x) + x) * x)) + x)) +x)= x(l - x)(2x + 3)((1 - x ) * ((((((a; + x)*x) + x)*l) + x) + x)) = x(l - x)(2x + 3)((((1 - (1 * (((a: + x) * x) - 1))) - a;) + 1) * a;) = x(l - x)(2x + 3)

We find that most of the time either 4a;(1 - a;) or x(l - x) (2x + 3) is the fittest map.Of course we find the desired functions with less iterations if the poulation size isincreased. The map

g(x) =a;(l-a;)(2a; + 3)

satisfies the conditions given above, but g(x) has values greater than 1 on the interval

1,2' 2 2) '

Thus we find that almost all initial values XQ G [0, l] escape the interval [0,1] underiterations of the map. The set of all points whose iterate stay in [0,1] are of measurezero and form a Cantor set. We can also change the symbols array to include thesymbols c for cosine, s for sine and y for itx. In this case we find the functionsin(7ra;) nearly every time as the fittest map.

Another application of gene expression programming is to find boolean expressions.In boolean expressions we have the AND, OR, XOR and NOT operation. TheNAND gate is an AND gate followed by a NOT gate. The NAND gate is universalgate, where all other gates can be built from.

As ah example consider the truth table

a 1 b I! O0 0 10 1 11 0 01 I 1 I 1

where a and b are the inputs and O is the output.

Page 453: Nonlinear Workbook (3rd)

434 CHAPTER 13. GENE EXPRESSION PROGRAMMING

Assume that * is the AND-operation, + is the OR operation and — is the NOToperation. Using gene expression programming we can form the expressions

*a+l-blaablObb+b-aIababa

The first boolean expression would be a * (1 + b). The second boolean expressionwould be b + a. In the first case the tree structure would be given by

For the fitness function we compare the output for each entry from the given truthtable with the output from the gene. For example, consider the expression writtenin gene expression programming E(a,b) = -*a+bO. The expression could also bewritten in standard notation

E(a,b) = a * (6 + 0).

Thus we find £"(0,0) = 1, #(0,1) = 1, #(1,0) = 1, #(1,1) = 0. Therefore thefitness is 2, since at two entries the values differ. Of course the fitness we have toachieve is 0.

Page 454: Nonlinear Workbook (3rd)

Chapter 14

Optimization

14.1 Lagrange Multiplier Method

In mathematical optimization problems, Lagrange multipliers are a method for deal-ing with equality constraints. The Lagrange multiplier method is as follows. Let Mbe a manifold and / be a real valued function of class C'1) on some open set con-taining M. We consider the problem of finding the extrema of the function f\M.This is called a problem of constrained extrema. Assume that / has a constrainedextremum at x* = (x{, x\,..., a;*). Let

5 i (x )=0 , . . . , f f m (x )=0

be the constraints (manifolds). We assume that / and gj (j = 1 , . . . , m) are contin-uously differentiable in a neighbourhood of x*. Then there exist real numbers Ai,. . . ,Am such that x* is a critical point of the function

F(x) := /(x) + Al5l(x) + • • • + Amffm(x).

The numbers Ai, . . . , Am are called Lagrange multipliers. Thus we have to solve

VF(x*)=O

&(x ' ) = 0, j = l,2,...,m

where V denotes the gradient and we have to assume that the rank of the matrixVg(x*) is m.

Example 1. The norm of an n x n matrix over the real numbers R is given by

\\A\\ := sup 11,4x11.11*11=1

This is a problem with the constraint ||x|| — 1 i.e., the length of the vector x € R™must be 1. This problem can be solved with the Lagrange multiplier method. Forthe problem to find the norm of an n x n matrix one considers the functions

435

Page 455: Nonlinear Workbook (3rd)

436 CHAPTER 14. OPTIMIZATION

F ( x ) : = | M b c | | 2 + A||x| |2

where A is the Lagrange multiplier. Consider the 2 x 2 ma t r i x

*-(U)-Then we have

|| Ax||2 = {Ax)TAx = xTATAx = 5a;2 + l O a ^ + 5a;2 .

From the constraint ||x||2 = 1 we obtain x\ + x\ = 1. Differentiation the function

F(x) = 5a;2 + lOa;^ + 5rr2 + \{x\ + x\)

and setting the derivative equal to zero yieldsdF dF—— = 10zi + 10^2 + 2Axi = 0 , —— - lOxi + 10z2 + 2\x2 - 0.OXi OXi

Thus we find that xi = X2 and

(H, x2) = (l/v/2, l/\/2), ( n , oc2) = ( - l / \ / 2 , -1 /V2) .

It follows that \\A\\2 = 10.

Note that the constraint

| |x| |2 = l « . i ? + ^ + --- + 4 = l

can be eliminated using n-dimensional spherical coordinates

X\=r cos 0i

^2 = r sin #i cos 82

Xz = r sin #i sin 62 cos 03

£4 = r sin ^i sin ^2 sin #3 cos 64

xn_i = r sin 6\ sin 62 sin 03 • • • sin 0n-i cos ^n_!

xn = r sin #i sin 02 sin #3 • • • sin 0n_2 sin 0n_i

or

ifc-i

a ; j t = r c o s 0 A : I I S ' n ^ ' ^or k = 1,2,... , n - I

n - l

^n = T I I Sm ^

Page 456: Nonlinear Workbook (3rd)

14.1. LAGRANGE MULTIPLIER METHOD 437

with r = 1, — n < 0i < n, and 0 < Oj < n, 2 < j < n — 1. The inverse transform is

/ x \dk = arccos I , , for k = 1,2,..., n — 2

f x \9n_i = arctan I —— .

\Xn-lJ

The Jacobian J of this transform is

J = rn-in(sin0p)-'>-1.P=i

Example 2. We calculate the shortest Euclidean distance between the curves

z2 + (2/-5)2 = l, y = x>

in R2 applying the Lagrange multiplier method. The square of the distance betweentwo points (xi, yi) and (x2,2/2) on the curves is given by

d2 := (n - x2)2 + (2/1 - y2f •

We have two constraints. This means we have two Lagrange multipliers. Thus wedefine

F(xuyux2,y2) := {xi - x2f + (yi - y2f + \i{x2 + (Vl - 5)2 - 1) + A2(j/2 - x\).

Hence we obtain the equations

dF— = 2(xl-x2) + 2\lxl = 0OX\dF— = -2{Xl - x2) - 2X2x2 = 0ax2

dFS - = 2(ffi-ifa) + 2A 1 (y 1 -5 )=0oy\dF— = -2(2/1 -j/2) + A2 = 0.oy2

Adding the first two equations and the last two equations yields

A1X1 = A2z2, 2Ai(2/i - 5) = -A2 .

Of course we still have

(1 + Xi)x1=x2, (I + Xi)(yi - 5) = y2 - 5

from the first set of equations. If Ai — 0, we have A2 = 0 so that x\ = x2 andj/i = y2. Thus we obtain

a:2 + ( :r2-5)2 = l

x\- §x\ + 24 = 0

2 9±v/=:T5

Page 457: Nonlinear Workbook (3rd)

438 CHAPTER 14. OPTIMIZATION

Thus Ai = 0 does not give a valid solution. Suppose A2 — 0. Once again Xi = x2

and yi = y2, thus A2 — 0 does not give a valid solution. Now suppose xx = 0, thus£2=2/2 = 0 and y± = 6 or yx = 4. Lastly suppose xx / 0. Thus

^ ( 2 / l - 5 ) = (l + A1)(2/l-5) = - iA2 2

(l + Ai)(i/i-5) = jte-59

2/2 = -

From 2/2 = %\ we obtain a;2 = ±3/\ /2. Furthermore we have

1 _ £ i _ j/i - 5

1 + Ai a;2 2/2 — 5

From sf + (j/x - 5)2 = 1 we obtain

2-xl = 4(yi-5)2

yi = ± \ / I + 5 -We tabulate the solutions

xi 2/i x2 2/2 Value0 4 0 0 40 6 0 0 6

Ws y/¥s+5 -72 I 3-179449

7Ts - \ / i + 5 ^ I L 1 7 9 4 4 9

- i s ^ + 5 ^ I 3-179449

6_ _ /X_|_c: 3_ 9 i 170440y ^ V 38 + & y/2 2 I J - 1 ' ^ ^

The minimum distance is approximately 1.179449.

Example 3. A firm uses two inputs to produce one output. Its production functionis

f{x1,x2)=xa1x

b2, 0 ,6 > 1 .

The price of the output is p, and the prices of the inputs are u>i and w2. The firmis constrained by a law that says it must use exactly the same number of units ofboth inputs. We use the Lagrange multiplier method to maximize the function

g(xu x2) = pf(xi,x2) - W1Z1 - w2x2

Page 458: Nonlinear Workbook (3rd)

14.1. LAGRANGE MULTIPLIER METHOD 439

subject to x2 — X\ = 0. The Lagrange function is given by

L(Xi, X2) — px\x\ — Wi^! — W2^2 — ^(#2 ~ xl) •

Thus from

dx~1=0> dx~2

=°we find

apx\~lxb2 - wi + A = 0, bpx^xl"1 -w2-X = 0.

Furthermore we have the constraint x2 = X\. These three equations have a singlesolution

( \ 1/(0+6-1)Wl+W2\P(a + b))

and_ bwi - aw2

~ a + bThus

g(xl,x*2)=p(xl)a+b-xl(w1 + w2).Example 4. Given the Lagrange function

L{*, ±) = \ E(i? - <*&) - \m (£, A -1) (i)

where A is the time-dependent Lagrange multiplier. The system describes an n = 3dimensional harmonic oscillator constrainted to a unit n — 1 = 2 sphere, i.e.,

E*> = 1. (2)

Find the equations of motion using the Euler-Lagrange equations

^|£_^l=0, , = 1,2,3. (3)dtdxj axj

From (3) and (1) we find

Xj + LJjXj + \{t)xj = 0 , j = 1,2,3. (4)

To eliminate the Lagrange multiplier A we proceed as follows. From (2) we obtainby differentiating with respect to t

3

and differentiating twice with respect to t yields

3

Y^XjXj + x}) = 0 . (5)

Page 459: Nonlinear Workbook (3rd)

440 CHAPTER 14. OPTIMIZATION

From (4) we obtain xixi + ujx) + \{t)x] = 0. Summation yields

3 3

3=1 J = l

where we used (2). Thus

\(t) = -j:x]Xj - £^x) = j:x)-j:uy33=1 3=1 j = l 3=1

where we used (5). Inserting A into (4) yields the equations of motion

3

Xj + tfxj + Xj Y,^) ~ UJjXj) = 0 .3 = 1

Example 5. We want to minimize f{xx, x2) = x\ +x\ subject to the side conditionx2 = xi - 1. Obviously we find x\ = 1/2 and x2 = -1 /2 . We can also apply thepenalty method to find this solution. The penalty method, which is an approximationmethod where the failure of the minimizer to fulfill the side condition is penalizedand one seeks the minimum of the function

g{xu x2) =x\ + x\ + -(x2 - xx + I)2 .

Here 7, the penalty parameter, is chosen to a fixed large number and not allowedto vary. Note that the side condition must be squared, otherwise the sign wouldpreclude the existence of a minimum. Minimization of g with respect to xi and x2

yields

Xl = 277TT)' X2 = ~207TT)which in the limit 7 —¥ 00 returns X\ — 1/2, x2 = —1/2.

The method of the Lagrange multipliers is generalized by the Karush-Kuhn-Tuckertheorem.

14.2 Karush-Kuhn-Tucker Conditions

Many optimization problems also include inequalities. Karush-Kuhn-Tucker ex-tended the Lagrange multiplier method to include inequality constraints.

Given an optimization problem with convex domain Q. C Rn,

minimize /(x) , x G fisubject to <7i(x) < 0 , i = 1 , . . . , k

hi(x)=0, i = l,...,l

Page 460: Nonlinear Workbook (3rd)

442 CHAPTER 14. OPTIMIZATION

In convex programming problems, the Karush-Kuhn-Tucker conditions are neces-sary and sufficient for a global minimum.

Example 1. A household has the utility function

f(Xl,x2)=x?xl-a

where a > 0 and faces the budget constraint

PiXi + P2X2 < m

with m > 0. We maximize the household's utility using Karush-Kuhn-Tucker con-ditions for the demand functions Xi(pi,p2,m) and x2(pi,p2,rn). We evaluate thedemand functions for {p\,P2,rn) = (1,0.5,10) and a — 0.5 and also find f{x\,x2)for these values. The Lagrangian is

L(x1,x2, A) = x"x\~a + \{m - plxl - p2x2)

with the Karush-Kuhn-Tucker conditions

| ^ = ^ 4 - - A P I = Oax\ X\

d L 1-aTaTl-a , _ 0

dX2~ X2 Xl*2 XP2~0

\{m - piXi - p2x2) = 0

A>0m — p\X\ — p2^2 > 0.

If A = 0 the problem has no solution, since

—xaxx\-a = 0

only if x2 = 0, but then

^^xaxx2-

a = 00.

Thus A > 0. Therefore we have to solve the system of nonlinear equations

—x1x\-a-\pi = QX\

—-x°x2-a - \P2 = Q

m — piXi — p2x2 = 0

where (m,pi,p2,a) are given and the unkowns are (x\,x2, A). The solution is

mX\ — a—

Pi

xa = (l-a)-.

Page 461: Nonlinear Workbook (3rd)

14.2. KARUSH-KUHN-TUCKER CONDITIONS 441

we define the generalized Lagrangian function ask I

L(x,a,/3)=f{x) + $>rf,(x) + £M*(x)

= /(x) + aTg(x)+/3Th(x).

Then we have the following

Theorem. Given the above optimization problem with convex domain Q, C Rn,necessary and sufficient conditions for a normal point x* to be an optimum are theexistence of a* and /3* such that

aL(x*,a*,/3*)=()

dxc>L(x*,a*,/3*)

d/3a*ft(x*) = 0, i = l,...,k

ft(x*)<0, t = l , . . . ,A

a*>0, i = l,...,k.

The third relation is known as Karush-Kuhn-Tucker complementarity condition.That is, either a constraint is active, meaning <?i(x) = 0, or the corresponding mul-tiplier satisfies a* = 0.

We can also formulate the Karush-Kuhn-Tucker condition as follows. Here we as-sume that the constraints 5*(x) > 0. Consider the constraint nonlinear program-ming problem with inequality and equality constraints: minimize the scalar function/ subject to the inequality constraints <?fc(x) > 0, k = 1,2,..., K and the equalityconstraints hm(x) = 0, m = 1,2,...,M. For this problem we can construct theLagrange function

K M

L(x, A, fi) = /(x) - J2 A*5A(X) - Y, MmMx)k=l m=l

where A* and \im are the Lagrange multipliers. If the problem has a solution

V* —- If* T* T*\X — (Xlt X2, • • • ,Xn)

i.e., minx/(x) = /(x*) and all constraints are satisfied, then the following Karush-Kuhn-Tucker conditions hold

K M

v/(x*) - J2 A;V5*(X*) - 32 tiyhm{x*) = o*=1 m=l

and

S*(x')>0 k = l,2,...,K

hm(xt) = 0 m = l , 2 , . . . , M

\tgk(x*) = 0 k = l,2,...,K

\*k>0 k = l,2,...,K.

Page 462: Nonlinear Workbook (3rd)

14.2. KARUSH-KUHN-TUCKER CONDITIONS 443

At the given vector of prices, income, and a, we obtain Xi = 5, a;2 = 10 and theutility is

f{x1 = 5tx2 = 10) = 51/2101'2.

Example 2. Find the minimum of the function

/(x) = (n - 2)2 + (x2 - I)2

under the constraints

gl(x)=x2-x\ > 052(x) = 2-s: 1-a;2 > 0g3(x)=xi > 0 .

The Lagrange function is

L(x, A) = ( n - 2)2 + {x2 - I)2 - Ai(i2 - 4) ~ A2(2 - n - x2) - \3Xl.

Thus we find the Karush-Kuhn-Tucker conditions

2(ii - 2) + 2A1a;1 + A2 - A3 = 0

2 ( a ; 2 - l ) - Ai + A2 = 0a;2 - a;2 > 0

2 - n - x2 > 0ii>0

Ai(a;2-a;2) = 0A2(2-a;i ~x2) = 0

XzXi = 0

andA!>0, A2 > 0, A 3 > 0 .

These equations and inequalities can be solved starting from Aa i = 0 with the casesA = 0 or x\ = 0. We find

o oT* — i -r* — i \* — _ y — \* — n

Xj — 1, X2 — 1, -^i — _ , ^ 2 — X) ^3 — U

for which /(:Ei,£2) = 1.

Example 3. We want to maximizef{xi,x2) = 3.6a;! - 0.4a;? + 1.6a;2 - 0.2^

subject to2xi + a;2 < 10, xx > 0, x2 > 0 .

Page 463: Nonlinear Workbook (3rd)

444 CHAPTER 14. OPTIMIZATION

We need to change the problem to find the minimum of

h{xi,x2) - -3.6a?i + O.4:zi - 1.6z2 + 0.2z2

and gi(xi,x2) = 10-2a:i-a:2, g2{xx,x2) = xu and 33(^1,^2) = x2 in order to applythe Karush-Kuhn-Tucker conditions as follows:

-3.6 + 0.8a?i + 2Ai - A2 = 0-1.6 + 0.4x2 + Ai - A3 = 0

10 - 2zi - x2 > 0Ai(10-2zi-a;2) = 0

A2a;i = 0

A3a;2 = 0

and x1 > 0, x2 > 0, \i > 0, A2 > 0, A3 > 0. A case study of these system ofequations and inequalities yields X\ = 3.5, x2 = 3.0, Ax = 0.4, A2 = 0, A3 = 0. TheHessian for h

/ tof d^ar2] _ /0.8 0 \a2h. gift " I 0 0 4 /

is positive definite. Thus, h is convex and the inequality constraints are all linearand thus concave. Thus the solution (xi,x2) = (3.5,3) is optimal.

14.3 Support Vector Machine

14.3.1 Introduction

The support vector machine is an algorithm for learning linear classifiers. It ismotivated by the idea of maximizing margins. There is an efficient extension tonon-linear support vector machines through use of kernels. We have n vectors Vo,Vi, . . . , vn_! from the vector space Rm which are from two classes. The trainingdata are {(v^, j/,) : j = 0 , 1 , . . . , n - 1} where yj € {+1, - 1 } . The goal is to learn aclassification rule from the data which only makes small classification errors on then-examples but also has good generalizations.

14.3.2 Linear Decision Boundaries

Let the training set of two separate classes be represented by the set of vectors

(vo ,J /o ) , ( v i , j / i ) , • • • ,{vn-i,yn-i)

where Vj (j = 0 , 1 , . . . ,n — 1) is a vector in the m-dimensional real Hilbert spaceRm and yj € { - 1 , +1} indicates the class label. Given a weight vector w and abias b, it is assumed that these two classes can be separated by two margins parallelto the hyperplane

wTV j + b > 1, for Vi = +1 (1)

Page 464: Nonlinear Workbook (3rd)

14.3. SUPPORT VECTOR MACHINE 445

wTVj + b < - 1 , for Vi = - 1 (2)

for j = 0 , 1 , . . . , n—1 and w = (iuo,«>i,... ,wm-i)T is a column vector of m-elements.Inequalities (1) and (2) can be combinded into a single inequality

% (w TV j + 6 ) > l for j = 0 , l , . . . , n - l . (3)

There exist a number of separate hyperplanes for an identical group of trainingdata. The objective of the support vector machine is to determine the optimalweight w* and the optimal bias 6* such that the corresponding hyperplane separatesthe positive and negative training data with maximum margin and it producesthe best generation performance. This hyperplane is called an optimal separatinghyperplane. The equation for an arbitrary hyperplane is given by

wTx + b = 0 (4)

and the distance between the two corresponding margins is

T TY(w, b) = mm T—rr- - max T—T- . (5)

{vU=+i} ||w|| {v|y=-i} ||w|| ;

The optimal separating hyperplane can be obtained by maximizing the above dis-tance or minimizing the norm of ||w|| under the inequality constraint (3), and

7™s = 7 K , b ' ) = ^ . (6)

The saddle point of the Lagrange function

LP(w, b, a) = lwTw - £ a,(%(wTVj- + b) - 1) (7)

gives solutions to the minimization problem, where a.j > 0 are Lagrange multiplier.The solution of this quadratic programming optimization problem requires that thegradient of Lp(w, b, a) with respect to w and b vanishes, i.e.,

8LP dLP _

We obtainw* = S ajVjVj (8)

3=0

andn±ajyj = 0. (9)j=o

Inserting (8) and (9) into (7) yields

n—1 -i n— In—1

LD(OC) = Y, ai ~ 9 Yl 12 aiajViyjvTvj (10)i=0 Z fcO j=0

Page 465: Nonlinear Workbook (3rd)

446 CHAPTER 14. OPTIMIZATION

under the constraintsn - l

5>>% = o (ii)3=0

andaj>0, j = 0 , l , . . . , n - l .

The function Lj){a) has be maximized. Note that Lp and Lp arise from the sameobjective function but with different constraints; and the solution is found by mini-mizing Lp or by maximizing LQ- The points located on the two optimal margins willhave nonzero coefficients a,, among the solutions of max Lp (a) and the constraints.These vectors with nonzero coefficients a.j are called support vectors. The bias canbe calculated as follows

b* = — I min w*Tv,- + max w*TV; | .2\{vj\yj=+l} J {v,-|w=-l} 7

After determination of the support vectors and bias, the decision function thatseparates the two classes can be written as

/(x) = sgn I J2 «j2/jvjx + 6* I .

V=° /Apply this classification technique to the data set (AND gate)

j Training set v. Target yj1 (P) I1 (0,1) 12 (1,0) 13 1 (1,1) 1 - 1

For the present data set we find

3 i i

LD(a) = J2 ai ~ oai + aiQ3 - »al + a2a3 - a\

since for the scalar products we have

v £ v , = 0, j?' = 0,1,2,3

andvfVl = 1, vfv2 = 0, vfv3 = 1, vjfv2 = 1, v£V3 = 1, v^v3 = 2.

The constraints are

Q0 > 0, ai > 0, a2 > 0, a3 > 0

andao + Qi + Q!2 — ^3 = 0 .

Page 466: Nonlinear Workbook (3rd)

14.3. SUPPORT VECTOR MACHINE 447

To apply the Karush-Kuhn-Tucker conditions (which is formulated for a minimum)we have to change LD{OL) to —Lo(a). Thus we have the Lagrangian

3 1 1L(a) = -J2aJ+ oa l ~ a l a 3 + oQ2 - a2a3 + Oil

3=0 l l

-fi(a0 + ai+a2- a3) - \oao - Axai - A2a2 - A3a3.

Thus we have to solve the sytem of equations

dL— = 0->-l-//-A0 = 0oa0

dL•r— = 0 -+ - 1 + a i - a 3 - /x - Ai = 0oa,\dLT:— = 0 -> - 1 + a2 - a3 - fj, - A2 = 0aa2dL-=— = 0 -> - 1 - Qi - a2 + 2a3 + /i - A3 = 0aa3

together with

Aoao = 0, Ai«i = 0, A2a2 = 0, A3a3 = 0

Ao > 0, Ai > 0, A2 > 0, A3 > 0

and the constraints Q0 + ai + a2 - a3 = 0. We find the solution

a0 = 0, a-i = 2, a2 = 2, a3 = 4

Ao = 2, Ai = 0, A2 = 0, A3 = 0, /j, = - 3 .

Thus

For 6* we obtain b* = 3. The decision function that separates the two classes isgiven by

/(x) = sgn(aij/ivf x + a2y2vj x + a3y3v^x + 6*)= sgn(2a;2 + 2 ^ - 4(a;i + x2) + b")= sgn(-2a;1 - 2x2 + 3)

= sgn (-Xi - x2 + -) .

Thus3

xi + x2 = - .This solution can also be seen on inspection of the data set.

Page 467: Nonlinear Workbook (3rd)

448 CHAPTER 14. OPTIMIZATION

14.3.3 Nonlinear Decision BoundariesIn the previous problem we have considered a data set which can be separated by ahyperplane. For nonlinear decision boundaries we can extend the method as follows.The datapoints, Vj only appear inside a scalar product. We map the datapoints intoan alternative higher dimensional space, called feature space, through

where (, ) denotes the scalar product in the feature space. The map 0(v;) does notneed to be known since it is implicitly defined by the choice of the positive definitekernel

A-(vi,vJ-) = <0(vi),0(vJ-)>.

It is assumed that K(vi,Vj) = K(vj,Vi). Examples are the radial base functionkernel

K(vi !vj) = eXp(-| |vi-vj| |2/(2<T2))

and the polynomial kernel

For binary classification with a given choice of kernel the learning task thereforeinvolves maximisation of the Lagrangian

n—1 -I n~\ n—1

LD{(*) = E a i - ; E E otiajViVjKivi, v,)i=0 Z i=0 j=0

subject to the constraints

n-1

Y,aiVi = 0, oii > 0, i = 0,1, . . . , r a - 1.

After the optimal values a* have been found the decision function is given by

/(x) = sign f £ a*yiK(x, v4) + b) .\i=o /

The bias b is found from the primal constraints

b = 4 ((,s!2ij [fo^K(-^yj+{i ™1} (E°J«*(V<, v,)J J.

For the Karush-Kuhn-Tucker conditions (which are formulated for a minimum) wehave to change LD to — Lp. Thus taking into account the constraints we have theLagrangian

n—1 1 n—In—1 n—1 n—1

j=o z i=o j=o i=o j=o

Page 468: Nonlinear Workbook (3rd)

14.3. SUPPORT VECTOR MACHINE 449

From dLldak = 0 we find

n - l

- 1 + J/fc 5Z aLjyjK{\k, Vj) -nyk-Xk = O3=0

for fc = 0 , 1 , . . . , n — 1. The other Kuhn Tucker conditions are

n - l

Q j > 0 , j = 0 , 1 , . . . , n - 1

\jaj = 0, j = 0 , l , . . . , n - l

A;>0, j = 0 , l , . . . , n - l .

Note that there is no condition on the Lagrange multiplier /z.

Let v = (vi, v2)T and a feature map that maps

v -> ^(v) = («?, u|, v^vi«2, V2«i, \/2«2, l ) r .

The kernel function for the feature space is

K(vi, Vj) = (vavjj. + vi2vj2 + I)2 = (1 + (vfvj))2 .

We find the solutions of the Karush-Kuhn-Tucker conditions for the kernel given inabove and the data set

j Training set Vj Target y3-"0 (-1,-1) =11 (-1.+1) +12 (+1,-1) +13 | (+1.+1) 1 -1

Inserting the data set into the kernel we obtain a (positive definite) matrix withK(vi, VJ) = 9 and K(vt, V,-) = 1 for i ± j with i, j = 0,1,2,3. The solution of theKuhn Tucker conditions is given by

1ao = ai = a2 = a3 = -

o

and Ao = Ai = A2 = A3 = fj, = 0. Furthermore we have 6 = 0. Thus

/(x) = sgnt-aarca).

We derive a relation between the learning rate r\ and the kernel using

n—l 1 n—In—1 n—1

£(«) = Yl aJ - o Yl Z) onajyiUjKiyu v^ - \i Y_ otjVj •j=0 Z t=0 j=0 j=0

Page 469: Nonlinear Workbook (3rd)

450 CHAPTER 14. OPTIMIZATION

and choose the gradient ascent algorithm

dL ( n~l \Sak = T]-— = rj 1 - yk Y^ ajyjKivj, vk) - fiyk) .

°ak \ j=o JWe obtain

A L k : = L(a0, . . . , a k + 5 a k , . . . , an-i) - L(a0, . . . , a k , . . . , a n _ i )/ n-i \ x

= Sak I 1 - yk Yl ajVjKivj, vfc) - /xj/fc I - -(toA:)2i;ir(v<:, vk)

Given that ALk > 0 we find 0 < r}K(vk, vk) < 2 and thus

2

When L reaches a maximum value and the we have a stable solution, 5ak = 0. Thusit follows that

n - l n -1

i - Vk J2 ajyjK(vj>v*) ~ w* = yk(vk - Y, <*jyjK(vj, vjt) - /i) = o

where we used that y\ = +1.

The Kernel-Adatron algorithm is given by

1. Initialize a^ = a.\ — • • • = an-i = I, 0 = 0.

2. For i = 0 , 1 , . . . , n — 1 calculate

n - l

3=0

3. Calculate % = yi(zi — 0).

4. Let Scti := 77(1 - 7,) be the proposed change to at.

(a) if a.i + 5ai < 0 then at = 0 .

(b) if Qj + <5 > 0 then a* = a + Sai .

5. Calculate the new threshold

6 := - (min(^) + max(z,r))

where z^ are those patterns i with class label +1 and zf those with class label- 1 .

Page 470: Nonlinear Workbook (3rd)

14.3. SUPPORT VECTOR MACHINE 451

6. If a maximum number of presentations of the pattern set has been exceededor the margin

m := -(mjn(^) - m^x(z~))

has approached 1 then stop, otherwise return to step 2.

The C++ program could be improved by calculating the kernel beforehand.

/ / kerneladatron.cpp

#include <iostream>#include <cmath> / / for fabsusing namespace std;

double K (double vi[2], double vj[2]) / / kernel{

double kl = 1.0 + vi[0]*vj [0]+vi [1] *vj [I] ;return kl*kl;

}

int main(void)

{const int m = 4;

double v[m][2] = { {-1,-1}, {-l.+l}, {+1,-1}, {+1.+1} };double y[m] = { +1, - 1 , - 1 , +1 };double alpha[m];double eta=0.01, eps=0.00001, margin=0.0, theta=0.0;double min, max;int i, j, mininit, maxinit;for(i=0;i<m;i++) alpha[i]=1.0;while(fabs(margin-l.O) > eps)

{mininit=maxinit=l;for(i=0;i<m;i++)

{double z = 0.0;for(j=0;j<m;j++) z += alpha[j] *y[j] *K(v[i] ,v[j] ) ;double delta=eta*(1.0-y[i]*(z-theta));if(alpha[i]+delta<=0.0) alpha[i] = 0.0;else alpha[i] += delta;if((mininit II z<min) && y[i]>0) { min=z; mininit=0; }if((maxinit II z>max) && y[i]<0) { max=z; maxinit=0; }}margin=(min-max)/2.0; theta=(min+max)/2.0;}

Page 471: Nonlinear Workbook (3rd)

452 CHAPTER 14. OPTIMIZATION

for(i=0;i<m;i++)cout « "alpha[" « i « "] = " « alpha[i] « endl;cout « "theta = " « theta « endl;return 0;

>

Page 472: Nonlinear Workbook (3rd)

Chapter 15

Discrete Wavelets

15.1 Introduction

In this section we discuss one-dimensional discrete wavelet transform (Erlebacheret al [28], Benedetto and Frazier [8]). Within the discrete wavelet transform wedistinguish between redundant discrete systems (frames) and orthonormal, semi-orthogonal, and biorthogonal bases of wavelets. In most case the discrete wavelettransform (or DWT) is an orthogonal function which can be applied to a finite groupof data. Functionally, it is very much like the discrete Fourier transform, in thatthe transforming function is orthogonal, a signal passed twice (i.e. a forward and abackward transform) through the transformation is unchanged, and the input signalis assumed to be a set of discrete-time samples. Both transforms are convolutions.Whereas the basis function of the Fourier transform is sinusoidal, the wavelet basisis a set of functions which are defined by a recursive difference equation for thescaling function 4> (also called father wavelet)

M - l</>{x) = Yl CkH2x - k)

where the range of the summation is determined by the specified number of nonzerocoefficients M. Here k is the translation parameter. The number of the coefficientsis not arbitrary and is determined by constraints of orthogonality and normalization.Owing to the periodic boundary condition we have

Ck = Ck+nM

where n G N. We notice that periodic wavelets are only one possibility to deal withsignals defined on an interval. Generally, the area under the scaling function overall space should be unity, i.e.

f <j)(x)dx = 1 .JR

453

Page 473: Nonlinear Workbook (3rd)

454 CHAPTER 15. DISCRETE WAVELETS

It follows that

M-\

k=0

In the Hilbert space L2(R-) with the scalar product

(f,9)~ I f{x)g{x)dx

the function (f> is orthogonal to its translations; i.e.,

/ (j>{x)(j>(x - k)dx = 0 , k G Z \ { 0 } .JR

What is desired is a function ip which is also orthogonal to its dilations, or scales,i.e.,

/ tp(x)ip(2x - k)dx = 0.

Such a function ip (called the mother wavelet) does exist and is given by (the so-calledassociated wavelet function)

M

^(x) = ^2(-l)kCl_k<f>(2x-k)

k=l

which is dependent on the solution of <j>. The following equation follows from theorthonormality of scaling functions

E Ckck-2m = 2<5OmA:

which means that the above sum is zero for all m not equal to zero, and that thesum of the squares of all coefficients is two. Another equation which can be derivedfrom ip(x)J-(/)(x — m) is

£(-l)*Cl-*<*-2m = 0.

A way to solve for </> is to construct a matrix of coefficient values. This is a squareM x M matrix where M is the number of nonzero coefficients. The matrix is des-ignated L with entries Ly = o^-j. This matrix has an eigenvalue equal to 1, andits corresponding (normalized) eigenvector contains, as its components, the value ofthe function (j> at integer values of x. Once these values are known, all other valuesof the function <j> can be generated by applying the recursion equation to get valuesat half-integer x, quarter-integer x, and so on down to the desired dilation. Thisdetermines the accuracy of the function approximation.

An example for a mother wavelet tp is the Haar function

( 1 0 < a ; < |ij){x) := I - 1 \ < x < 1

[ 0 otherwise

Page 474: Nonlinear Workbook (3rd)

15.2. MULTIRESOLUTION ANALYSIS 455

and the scaling function (father wavelet) (f> is given by

, M _ f l 0 < K l^X>'-\Q otherwise "

We have ip(x) = <j>{2x) - <p(2x - 1). The functions

^m<n{x):=2^^{2mx-n), m,neZ

form a basis in the Hilbert space Li (R) . This means that every function / € Li (R)can be expanded with respect to this basis. If we restrict m to m = 0,1,2,... and

n = 0 , l , 2 , . . . , 2 m - l

we obtain a basis in the Hilbert space Li[0,1].

This class of wavelet functions is constrained, by definition, to be zero outside ofa small interval. This is what makes the wavelet transform able to operate ona finite set of data, a property which is formally called compact support. Therecursion relation ensures that a scaling function <j> is non-differentiable everywhere.Of course this is not valid for Haar wavelets. The following table lists coefficientsfor two wavelet transforms. The sum of the coefficients is normalized to 2.

Tab le 15.1 Coefficients for Two Wavelet Functions

Wavelet c0 C\ c-i c%Haar LO LODaubechies-4 | \(\ + y/3) | i(3 + A/3) | | (3 - A/3) \ \{1 - y/3)

15.2 Multiresolution Analysis

A multiresolution analysis of a signal /(£) consists of a collection of nested subspaces{ Vj }j£z of the Hilbert space L2(R), satisfying the following set of properties:

(i) njeZVj = {0}, UjeZVj is dense in L2(R)

(ii) Vj C Vj-i

(iii) /(<) e Vj ^-+ f(2H) € Vo

(iv) There exists a function <fo(i) in Vo, called the scaling function, such that thecollection { 4>o{t — k), k € Z} is an unconditional Riesz basis for the subspace Vo-

Similarly, the scaled and shifted functions {</>j,k(t) = 2"J/2</>0(2"J'£ ~ k),k € Z}constitute a Riesz basis for the space Vj. Performing a multiresolution analysis

Page 475: Nonlinear Workbook (3rd)

456 CHAPTER 15. DISCRETE WAVELETS

of the signal / means successively projecting it into each of the approximationsubspaces Vj

approx^i) = (Projv./)(i) = E af{j, k)<f>jtk(t).kez

Since Vj C Vj-\, approx^ is a coarser approximation of / than is approx^j andtherefore, the key idea of the multiresolution analysis consists in examining the lossof information, that is the detail, when going from one approximation to the next,coarser one:

detailj(i) — approx^.^i) - approx^i).

The multiresolution analysis shows that the detail signal detail^ can be directlyobtained from projections of the signal / onto a collection of subspaces, the Wj,called the wavelet subspaces. We have

V-+1 = Vj © Wj, Wj C Vj+1

where © is the orthogonal sum of two linear spaces. Thus we have two mutuallyorthogonal subspaces, Vj and Wj. Since

closi 2 ( R )( U Vj\=L2(R)(j=-oo J

we obtain the expansion

L 2 (R)= 0 Wj.j=-oo

Any function in the Hilbert space L2(R) may be expanded in a generalized Fourierseries

oo

f(x)= E Wj w i t h Wj £ Wj for a l l j e Z .j=-oo

There exists a function V'o, called the mother wavelet, to be derived from (j>o, suchthat its templates {ipjtk(t) = 2~j/2V>o(2~J* - k), k € Z} constitute a Riesz basis forthe subspace Wj

detail,-(t) = (Projw./)(i) = £ > / ( j , fc)<Mi) •kez

Let Tibe a separable Hilbert space (in our case L2(R)) and { hn } n 6 Z a countablesubset of H. We say that { hn } is a Riesz basis for % if { hn } is complete and if thereare constants 0 < C\ < Ci < oo, for an arbitrary sequence c := {cn}nez € ^(Z)such that

Cl\\c\\<\\ECnhn\\<C2\\c\\.ngZ

Basically, the multiresolution analysis consists in rewriting the information in / asa collection of details at different resolutions and a low-resolution approximation

j=Jf(t) = approxj(t) + ^2 detail,-(£)

j=i

= £ af(J, k)4jt(t) + E E d/0'. k)i>j,k{t) •kez j=ikez

Page 476: Nonlinear Workbook (3rd)

15.3. PYRAMID ALGORITHM 457

The approxy essentially being coarser and coarser approximations of / means that4>o needs to be a low-pass function. The details^, being an information 'differential',indicates rather that V'o is a bandpass function, and therefore a small wave, a wavelet.The multiresolution analysis shows that the mother wavelet ipo must satisfy

/ ipo(t)dt = 0

and that its Fourier transform obeys |^o(w)| ~ u>N, u —> 0, where JV is a positiveinteger called the number of vanishing moments of the wavelet.

Given a scaling function <f>o and a mother-wavelet ip0, the discrete (or non-redundant)wavelet transform is a mapping Z<2(R) ~* (Z) between the two Hilbert spacesL2(R) and £2(Z) given by

f(t) -> {{af(J, k),k€Z},{ df(j,k), j = 1 , . . . , J, k G Z } } .

These coefficients are defined through the inner products of the signal / with twosets of functions

a/0'. *) = </(*), ^,*(*)>df(j,k) = (f(t),i,j:k(t))

o o o o

where ipjk (respectively cj>jk) are shifted and dilated templates of tpg (respectively <f>o),called the dual mother wavelet (respectively the dual scaling function), and whosedefinition depends on whether one chooses to use an orthogonal, semi-orthogonalor bi-orthogonal discrete wavelet transform. They can pratically be computed by afast recursive filter-bank-based pyramidal algorithm whith an extremely low com-putational cost.

15.3 Pyramid Algorithm

The pyramid algorithm operates on a finite set on JV input data Xo,X\,... ,a;jv_i,where JV is a power of two; this value will be referred to as the input block size.These data are passed through two convolution functions, each of which createsan output stream that is half the length of the original input. These convolutionfunctions are filters, one half of the output is produced by the "low-pass filter"

ai = o 22 °2i-j+lXj' « = 0, 1, . . . ,— - 1

and the other half is produced by the "high-pass filter" function

1 N'1 N6, = ; E ( - i ) V ^ , *' = o,i — - i

Page 477: Nonlinear Workbook (3rd)

458 CHAPTER 15. DISCRETE WAVELETS

where N is the input block size, Cj are the coefficients,

X = (xO,Xi,...,XN-i)

is the input sequence, and

a = (ao,ai,...,aif/2-i), b = {bo,bu.. .,6JV/2-I)

are the output sequences. In the case of the lattice filter, the low- and high-passoutputs are usually referred to as the odd and even outputs, respectively. In manysituations, the odd or low-pass output contains most of the information contentof the original input signal. The even, or high-pass output contains the differencebetween the true input and the value of the reconstructed input if it were to bereconstructed from only the information given in the odd output. In general, higherorder wavelets (i.e. those with more nonzero coefficients) tend to put more informa-tion into the odd output, and less into the even output. If the average amplitudeof the even output is low enough, then the even half of the signal may be discardedwithout greatly affecting the quality of the reconstructed signal. An important stepin wavelet-based data compression is finding wavelet functions which cause the eventerms to be nearly zero. However, note that details can only be neglected for verysmooth time series and smooth wavelet filters, a situation which is not satisfied forchaotic time signals.

The Haar wavelet represents a simple interpolation scheme. After passing these datathrough the filter functions, the output of the low-pass filter consists of the averageof every two samples, and the output of the high-pass filter consists of the differenceof every two samples. The high-pass filter contains less information than the lowpass output. If the signal is reconstructed by an inverse low-pass filter of the form

JV/2-1

xj= Y, C2i-i+i«i, j = 0 , 1 , . . . , TV — 1t=0

then the result is a duplication of each entry from the low-pass filter output. This isa wavelet reconstruction with 2x data compression. Since the perfect reconstructionis a sum of the inverse low-pass and inverse high-pass filters, the output of the inversehigh-pass filter can be calculated. This is the result of the inverse high-pass filterfunction

JV/2-1

xf = J2 (-1) V1-2A, 3 = 0,1, • • •, N - 1.i=O

The perfectly reconstructed signal is

x = xL + xH

where x is the vector with elements Xj. Using other coefficients and other orders ofwavelets yields similar results, except that the outputs are not exactly averages anddifferences, as in the case using the Haar coefficients.

Page 478: Nonlinear Workbook (3rd)

15.3. PYRAMID ALGORITHM 459

Let us consider an example. Most discrete signals are filtered using linear differenceequations, for example

y[n] = ay[n - 1] + (1 - a)a;[n], n = 0 , 1 , . . .

where x[n] is the input signal, y[n] the output, y[-l] = 0, x[0] = 0, and a (0 < a < 1)is the filter parameter. Notice that y[n] depends upon previously computed values ofthe filter output. Thus the chosen filter is an infinite response filter. It is well-knownthat filtered chaotic signals can exhibit increases in observed fractal dimensions,for example the Liapunov dimension. We study the Liapunov exponent of filteredchaotic one-dimensional maps using wavelets. Our input signal is given as thesolution of the logistic map

x[t + 1} = 4x[t]{l - x[t\)

where x[0] € [0,1] and t = 0 ,1 , . . . . The properties of the logistic map are well-known(see chapter 1). For almost all initial values we find that the Liapunov exponentof the logistic map is given by ln(2). We apply the Haar wavelet to filter the timeseries (x0, X\,..., ijv_i) resulting from the logistic map in the chaotic regime. Thefilter decomposes the signal into the moving average coefficients { a ;} and details{bi} with i = 0, l,...,N/2 - 1. Owing to Broomhead et al [11] the completeset of coefficients { a,, bi} have the same Liapunov exponent as the original timeseries. The reason is that Haar (and Daubechies) wavelets are finite impulse responsefilters and that the convolution is performed only once. Therefore we deal with anon-recursive finite impulse filter. Using the time series given by the logistic mapwe evaluate xj, xf using equations given above. We have

xj ~xj+i

for j even andXj ~ ~X3+1

for j even. We use the series xj for the calculation of the Liapunov exponent. No-tice that in the case of the logistic equation the even output resulting from Haarwavelets, i.e. the details, contain nearly as much information as the odd output,i.e. the averages. We remove the duplication of each entry of x1- and calculate theLiapunov exponent of this time series. Thus the size of the time series is N/2. Analgorithm to find the Liapunov exponent from time series is described in chapter 2.The Liapunov exponent is much larger than ln(2) for this time series, i.e. the timeseries becomes more chaotic.

Using the coefficients for the Daubehies-4 wavelet we find similar results, i.e., the Li-apunov exponent is much larger than ln(2). Furthermore, for other one-dimensionalchaotic maps we also find similar results.

Page 479: Nonlinear Workbook (3rd)

460 CHAPTER 15. DISCRETE WAVELETS

II waveletliapunov.cpp

#include <iostream>#include <cmath> // for fabs, logusing namespace std;

void find(double* a,int length,int point,doublefe min,int& position)

•C

int i = 0;if (point == 0) { min = fabs(a[i] - a[ l ]) ; }if (point != 0) { min = fabs (a [i] - a[point]); }position = i;double distance;

for(i=l;i<(length-l);i++){if(i != point){distance = fabs(a[i] - a[point]);if(distance < min){min = distance;position = i;> // end if} // end if} // end for

}

int main(void)

•C

const double pi = 3.14159;// generate time seriesint n = 16384; // length of time seriesint m = n/2;int k;double* x = new double[n];int t;x[0] = 1.0/3.0; // initial value of time seriesfor(t=0;t<(n-l);t++){ x[t+l] = 4.0*x[t]*(1.0 - x[t]); } // time series

// Haar waveletdouble* c = new double[n];for(k=0;k<n;k++) c[k] = 0.0;cCO] = 1.0; c[l] = 1.0;

Page 480: Nonlinear Workbook (3rd)

15.3. PYRAMID ALGORITHM 461

double* a = new double[m];for(k=0;k<m;k++) a[k] = 0.0;double* b = new double[m];for(k=0;k<m;k++) b[k] = 0.0;

int i , j ;

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

{if(2*i-j+l < 0) a[i] += c[2*i-j+l+n]*x[j] ;

else a[i] += c[2*i-j+l]*x[j] ;

}

a[i] = 0.5*a[i] ;

>

for(i=0;i<m;i++)

{for(j=0;j<n;j++)

{

if(j-2*i < 0)

b[i] += pow(-1.0, j)*c[j-2*i+n]*x[j] ;

else b[i] += pow(-l.0,j)*c[j-2*i]*x[j];

}b[i] = 0.5*b[i] ;

>

/ / inverse transform

double* xL = new double[n];

double* xH = new double[n];

for(j=0;j<n;j++) xL[j] = 0.0;

for(j=0;j<n;j++) xH[j] = 0.0;

for(j=0;j<n;j++)

{

for(i=0;i<m;i++)

{

if(2*i-j+l < 0) xL[j] += c[2*i-j+l+n]*a[i];

else xL[j] += c[2*i-j+l]*a[i] ;

>

}

for(j=0;j<n;j++)

{

Page 481: Nonlinear Workbook (3rd)

462 CHAPTER 15. DISCRETE WAVELETS

for(i=0;i<m;i++){if(j-2*i < 0) xH[j] += pow(-1.0,j)*c[j-2*i+n]*b[i];else xH[j] += pow(-l .0, j)*c[j-2*i] *b[i] ;}}// input signal reconstructeddouble* y = new double[n];for(k=0;k<n;k++) y[k] = xL[k] + xH[k] ;// end wavelet block

// timeseriesint count = 0;t = 0;double* series = new double[m];while(t < n)

iseries [count] = xL[t];count++;t += 2;}// Liapunov blockdouble* d = new double[m-1];double* dl = new double[m-1];int point = 1;int position;double min;

for(t=0;t<(m-l);t++)

{find(series,m,t,min,position);d[t] = fabs (series [t] - series [position]);dl[t] = fabs (series [t+1] - series [position+1]);}

double sum = 0.0;for(t=0;t<(m-l);t++) { sum += log(dl[t]/d[t]); >

double lambda = sum/((double) (m-1));cout « "lambda = " « lambda « endl;delete[] x; delete[] d; delete[] dl;return 0;

>

Page 482: Nonlinear Workbook (3rd)

15.4. BIORTHOGONAL WAVELETS 463

15.4 Biorthogonal Wavelets

As described before, dilations and translations of the scaling function <f> leading to{(j)jk\ constitute a basis for the subspaces Vj and, similarly {ipjk{t)} for the subspacesWj. We define a dual multiresolution analysis with dual subspaces {V,} and {Wj}generated from a dual scaling function 4> and a dual mother wavelet ip, respectively.This is done so that instead of

(<t>jk, 4>n) = hi, {4>jk, ipfi) = Sjj'Ski, (fak, iPn) = o

as in an orthogonal case, where ( , ) is the scalar product in the Hilbert space L2(R),we have

(<i>jk, 4>ji) = hi, {i>jk, i>j'i) = SjjiSki,

a n d

$jk,<l>ji)=O, (<i>jk,ipji)=O

where <j>jk(t) := 2j/2${2jt - k) and^f c = 2H24>(2H - k). These sets constitute basesfor the dual subspaces {V -} and {VF,}. The conditions displayed above are referredto as biorthogonality conditions and ip and ip biorthogonal wavelets. In terms ofsubspaces, the above biorthogonality conditions can be expressed as

Vj ± Wj, Vj _L Wj, and Wj ± Wf for j ^ j ' .

By definition, a scaling function and a mother wavelet satisfy the dilation equationand the wavelet equation. Consequently

</>{t) = v/2 X] hk4>{2t - k), i/,(t) = y/2 J2 9k<t>(2t - k)kez kez

and4>{t) = V2^2 hkt&t - k), i,(t) = V2^2 9k4>{2t - k).

kez kez

The coefficients in the above equations can be obtained by taking the scalar productwith the appropiate dual function. For example,

hk = (ht.t) = >/2 / 4>{2t - k)4>{t)dt

9k = <0i,*, V'} = V2 [ 4>(2t - k)1>{t)dt.JR.

The roles of the two functions <f> and <j> or rp and •0 can be interchanged. Or if wetake the dual of the above equations we obtain the following relations

hk = (&,*,$ = y/2 [ cf>{2t - k)4>{t)dtJR

9k = <0u, i>) = \/2 / 0(2* - k)${t)dt.JR

Page 483: Nonlinear Workbook (3rd)

464 CHAPTER 15. DISCRETE WAVELETS

The Cohen-Daubechies-Feauveau biorthognal linear spline wavelets ([18], [22]) havecompact support and are known to have optimal time localization for a given num-ber of null moments (frequency localization). An important property of the splinewavelet is that they have zero moments up to order m, i.e.

/ tetjj{t)dt = 0, for £ = 0 , 1 , . . . , m .JR

Spline wavelets are extremely regular and unlike other wavelets, they are symmetricin time. A special case of the Cohen-Daubechies-Feauveau wavelet is implementedin the following Java program. The time series is generated from the logistic map.

/ / CDFl.java

public class CDF1

{public static void main(String [] arg)

idouble [] d = new double [65]; // length for data 2~n + 1 : n integer

d[0] = 1.0/3.0; // logistic mapfor(int k=l;k<d. length ;++k) { d[k] = 4.0*d[k-l]*(l .0-d[k-l] ) ; }

CDF1 cdf = new CDFlO;cdf.transform(d) ;// print transformed dataSystem.out.printlnC'transformed data:");for(int k=0;k<d.length;++k)

{System.out.println("d["+k+"] = " + d[k]);

}cdf.invTransform(d);System.out.printlnC'reconstructed data:");for(int k=0;k<d.length;++k)

{System.out.println("d["+k+"] = "+d[k]);

}

} // end main

public CDFlO { } // default constructor

final static doubled s = { 0.03314563036811941,

-0.06629126073623882,-0.17677669529663687,0.41984465132951254,0.9943689110435824,

Page 484: Nonlinear Workbook (3rd)

15.4. BIORTHOGONAL WAVELETS 465

0.41984465132951254,

-0.17677669529663687,

-0.06629126073623882,

0.03314563036811941 };

final static double[] w = { -0.5, 1.0, -0.5 };

final static double[][] sLeft = { { 1.0275145414117017,

0.7733980419227863,

-0.22097086912079608,

-0.3314563036811941,

0.16572815184059705 >,

{ -0.22189158107546605,

0.4437831621509321,

0.902297715576584,

0.5800485314420897,

-0.25687863535292543,

-0.06629126073623882,

0.03314563036811941 },

{ 0.07549838028293866,

-0.15099676056587732,

-0.0957540432856783,

0.34250484713723395,

1.0330388131397217,

0.41984465132951254,

-0.17677669529663687,

-0.06629126073623882,

0.03314563036811941 },

{ -0.013810679320049755,

0.02762135864009951,

0.011048543456039804,

-0.04971844555217912,

-0.18506310288866673,

0.41984465132951254,

0.9943689110435824,

0.41984465132951254,

-0.17677669529663687,

-0.06629126073623882,

0.03314563036811941 } };

final static doublet] • sRight = {{ 0.03314563036811941,

-0.06629126073623882,

-0.17677669529663687,

0.41984465132951254,

0.9943689110435824,

0.41984465132951254,

-0.18506310288866673,

Page 485: Nonlinear Workbook (3rd)

466 CHAPTER 15. DISCRETE WAVELETS

-0.04971844555217912,0.011048543456039804,0.02762135864009951,-0.013810679320049755 },{ 0.03314563036811941,-0.06629126073623882,-0.17677669529663687,0.41984465132951254,1.0330388131397217,0.34250484713723395,-0.0957540432856783,-0.15099676056587732,

0.07549838028293866 },{ 0.03314563036811941,-0.06629126073623882,-0.25687863535292543,0.5800485314420897,0.902297715576584,0.4437831621509321,-0.22189158107546605 >,{ 0.16572815184059705,-0.3314563036811941,-0.22097086912079608,0.7733980419227863,1.0275145414117017 > };

final static doubled sPrimary = { 0.35355339059327373,0.7071067811865475,0.35355339059327373 };

final static double[] wPrimary = { 0.0234375, 0.046875,-0.125, -0.296875,0.703125, -0.296875,-0.125, 0.046875, 0.0234375 };

final static double[][] sPrimaryLeft = {{ 0.7071067811865475,0.35355339059327373 }};

final static doubled [] sPrimaryRight = {{ 0.35355339059327373,0.7071067811865475 }};

final static doubled [] wPrimaryLeft = { { -0.546875,0.5696614583333334,-0.3138020833333333,-0.103515625,0.10677083333333333,

Page 486: Nonlinear Workbook (3rd)

15.4. BIORTHOGONAL WAVELETS 467

0.043619791666666664,-0.01953125,-0.009765625 },{ 0.234375,-0.087890625,-0.41015625,

0.673828125,-0.2421875,-0.103515625,

0.03515625,0.017578125 }};

final static double[][] wPrimaryRight = {{ 0.017578125,0.03515625,-0.103515625,-0.2421875,0.673828125,-0.41015625,-0.087890625,0.234375 },

{ -0.009765625,-0.01953125,0.043619791666666664,0.10677083333333333,-0.103515625,-0.3138020833333333,0.5696614583333334,-0.546875 >};

public void transform(double[] v)

ifor(int last=v.length;last>=15;last=(last+1)/2)

{transform(v,last);

>> // end method transform

public void invTransform(double[] v)

{int last;for(last=v.length;last>=15;last=last/2+l) { ; }for(;2*last-K=v.length;last=2*last-l) {

invTransform(v,last);

}

Page 487: Nonlinear Workbook (3rd)

468 CHAPTER 15. DISCRETE WAVELETS

} / / end method invTransform

public static void transform(double[] v.int last){double [] ans = new double [last];int half = (last+l)/2;if(2*half-l != last) {

throw new IllegalArgumentExceptionC'Illegal subband : " + last+ " within array of length " + v.length);

}// lowpassfor(int k=O;k<sLeft.length;k++) {

for(int 1=0;KsLeft[k] .length;1++) {ans[k] += sLeft[k] [l]*v[l];

>}for(int k=sLeft.length;k<half-sRight.length;k++) {for (int 1=0 ;Ks. length ;1++) {

ans[k] += s[l]*v[2*k+l-sLeft.length] ;}}

for(int k=O;k<sRight.length;k++) {for(int 1=0;KsRight[k] .length;1++) {

ans[k+half-sRight.length] +=sRight[k] [l]*v[last-sRight[k] .length+1] ;

>}/ / highpassfor(int k=O;k<half-l;k++) {for(int 1=0;Kw.length;1++) {

ans[k+half] += w[l]*v[2*k+l];>}

System.arraycopy(ans,0,v,0,last);} // end method transform

public static void invTransform(double[] v.int last)

{

double [] ans = new double [2*last-l] ;// scale coefficientsfor(int k=O;k<sPrimaryLeft.length;k++) {for(int 1=0;KsPrimaryLeft[k].length;1++) {ans[l] += sPrimaryLef t [k] [l]*v[k] ;

}}

Page 488: Nonlinear Workbook (3rd)

15.5. TWO-DIMENSIONAL WAVELETS 469

for(int k=sPrimaryLeft.length;k<last-sPrimaryRight.length;k++){

for (int 1=0 ;KsPrimary. length ;1++) {ans[2*k-l+l] += sPrimary [1] *v [k];

>}for(int k=O;k<sPrimaryRight.length;k++) {

for(int 1=0;KsPrimaryRight[k] .length; 1++) {ans[1-sPrimaryRight[k].length+ans.length] +=sPrimaryRight[k][l]*v[k+last-sPrimaryRight.length];

}>/ / wavelet coefficientsfor(int k=0;k<wPrimaryLeft.length;k++) {

for(int 1=0;KwPrimaryLeft[k].length;1++) {ans[l] += wPrimaryLef t [k] [1] *v[k+last] ;

>>for(int k=wPrimaryLeft.length;k<last-l-wPrimaryRight.length;k++){for (int 1=0 ;KwPrimary. length ;1++) {

ans[2*(k-l)-l+l] += wPrimary[l]*v[k+last] ;}}for(int k=0;k<wPrimaryRight.length;k++) {

for(int 1=0;KwPrimaryRight[k] .length; 1++) {ans[1-wPrimaryRight[k].length+ans.length] +=wPrimaryRight[k][1]*v[k+2*last-l-wPrimaryRight.length];

}}System.arraycopy(ans,0,v,0,ans.length);} // end method invTransform

> // end class

15.5 Two-Dimensional Wavelets

There are several methods to build bases on functional spaces in dimension greaterthan 1. The simplest one uses separable wavelets. Let us consider d = 2. Thesimplest way to build two-dimensional wavelet bases is to use separable products(tensor products) of a one-dimensional wavelet ip and scaling function <f>. Thisprovides the following scaling function <&(x\,X2) = <f>(xi)(j>(x2) and there are threewavelets

1>m(x1,x2) = </>(x1)rl>{x2), xpV\xux2)=ip{xl)<t>{x2), i>{3){x1:x2) = 1 /^1)^2) •

Page 489: Nonlinear Workbook (3rd)

470 CHAPTER 15. DISCRETE WAVELETS

There are also non-separable two-dimensional wavelets.

Assume that we have a scaling function $ and wavelets

# : = { , / , « : i = l ,2 ,3}

constructed by tensor products described above of a one-dimensional orthogonalwavelet system. If we define

VJ2(x):=2Vi)(2*x-j), 1 = 1,2,3

where k € Z, and j € Z2, then any function / in the Hilbert space L2(R2) can bewritten as the expansion

/(x)=£</,ii>j,*>iMx)

where the sums range over all j £ Z2, all fc S Z, and all ip G *. Here (, ) denotesthe scalar product in the Hilbert space L2(R2), i.e.

(/,vi:i)=c«=/R2/(x)^«(x)rfx.

Thus we have

/(x) = £</, vJM1^) + £</> )^ (x ) + £(/, ^ )^ (x ) .j , * j,fc j , *

Instead of considering the sum over all dyadic levels k, one can sum over k > K fora fixed integer K. For this case we have the expansion

/(x) = Y, cJ,*,vAfc(x) + £ di,K$3,K(x)j£Z2,fc>*-,*e* J6Z2

wherecj,*,V> = / 2 /(x)V'j,*(x)dx, <ij,j<r = y 2 /(x)$j,if (x)dx.

When we study finite domains, e.g., the unit square / , then two changes mustbe made to this basis for all of L2(R2) to obtain an orthonormal basis for 1,2(1).First, one considers only nonnegative scales k > 0, and not all shifts j € Z2, butonly those shifts for which tjjjtk intersects / nontrivially. Second one must adaptthe wavelets that overlap the boundary of I in order to preserve orthogonality onthe domain. Consider a function / defined on the unit square / := [0,1)2, whichis extended periodically to all of R2 by / ( x + j) = /(x) , x € / , j G Z2, whereZ2 := {(j1; j2) : ji,j2 £ Z}. One can construct periodic wavelets on L2{I) that canbe used to decompose periodic fuctions / on L2(I). For example, for ip e 9, k > 0and j e { 0 , 1 , . . . , 2k - 1 }2 one sets

V$(x) := £ i M x + n), x e / .neZ2

Page 490: Nonlinear Workbook (3rd)

15.5. TWO-DIMENSIONAL WAVELETS 471

One constructs $(p) in the same way; we have <&(p' (x) = 1 for all x, since

H ( - - j ) : j e z 2 }

forms a partition of unity. One now has a periodic orthogonal wavelet system onL2(I) such that

f(x) = (fMp)) + £ ( / , </>$) -V$(x).

In practice we are given only a finite amount of data, so we cannot calculate theabove formula for all k > 0 and all translations h € I. Assume that we have 2m

rows of 2m pixels, each of which is the average of / on a square of size 2~m x 2~m.Then using the orthogonal wavelets constructed by Daubechies we can calculatethese formulae for k < m and average over 22m different pixel translations j/2m,j = Ui,J2), 0 < ji,J2 < 2m, instead of averaging over h G I. We obtain

/(x) = f /(y)dy + £ 22(*-> f /(y)V* (y - ±) dy^ (x-±).JI 0<k<mj,ip JI V Z / V z /

For each dyadic level k we need to compute 3 x 22m terms, one for each pixel andone for each tp € \P.

Page 491: Nonlinear Workbook (3rd)

Chapter 16

Discrete Hidden Markov Processes

16.1 Introduction

A Markov chain is a finite state machine with probabilities for each transition, thatis, a probability that the next state is Sj given that the current state is s,.

Equivalently, a Markov chain is described by a weighted, directed graph in whichthe weights correspond to the probability of that transition. In other words, theweights are nonnegative and the total weight of the outgoing edges is positive. Ifthe weights are normalized, the total weight, including self-loops is 1.

The hidden Markov model ([76], [13]) is a finite state machine with probabilities foreach transition, that is, a probability that the next state is Sj given that the currentstate is s;. The states are not directly observable. Instead, each state produces oneof the observable outputs with certain probability.

Computing a model given sets of sequences of observed outputs is very difficult, sincethe states are not directly observable and transitions are probabilistic. Although thestates cannot, by definition, be directly observed, the most likely sequence of setsfor a given sequence of observed outputs can be computed in O(nT), where n isthe number of states and T is the length of the sequence. Thus a hidden Markovmodel is a Markov chain, where each state generates an observation. We only seethe observations, and the goal is to infer the hidden state sequence.

Hidden Markov models are very useful for time-series modelling, since the discretestate-space can be used to approximate many non-linear, non-Gaussian systems.

In a statistical framework, an inventory of elementary probabilistic models of basiclinguistic units (e.g., phonemes) is used to build word representations. A sequenceof acoustic parameters, extracted from a spoken utterance, is seen as a realizationof a concatenation of elementary processes described by hidden Markov models.

472

Page 492: Nonlinear Workbook (3rd)

16.1. INTRODUCTION 473

A hidden Markov model is a composition of two stochastic processes, a hiddenMarkov chain, which accounts for temporal variability, and an observable process,which accounts for spectral variability. This combination has proven to be powerfulenough to cope with the most important sources of speech ambiguity, and flexibleenough to allow the realization of recognition systems with dictionaries of hundredof thousands of words.

Applications of the hidden Markov model are

in mobile robots, where

states = location, observations = sensor input

in biological sequencing

states = protein structure, observations = amino acids

In biological sequencing the objective of the algorithm is: Given the structure of aprotein, such as insulin, find the amino acids that make up that protein. There are20 amino acids.

in speech recognition

states = phonemes, observations = acoustic signal

Given a speech signal, find the most probable sequence of words

words = argmax-P(WOI"ds—speech)

Two formal assumptions characterize hidden Markov models as used in speech recog-nition. The first-order Markov hypothesis states that history has no influence onthe chain's future evolution if the present is specified, and the output independencehypothesis states that neither chain evolution nor past observation influences thepresent oberservation if the last chain transition is specified.

In general, there are many different possible sequences

an ice creamand nice creamand nice scream

A statistical language model can be used to choose the most probable interpretation:

assign probabilities to all possible sequences of words

select most probable sequence from those proposed by the speech analyzer

Page 493: Nonlinear Workbook (3rd)

474 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

In the contemporary speech research community, the hidden Markov model is adominant tool used to model a speech utterance. The utterance to be modeled maybe a phone, a syllable, a word, or, in principle, an intact sentence or entire para-graph. In small vocabulary systems, the hidden Markov model tends to be usedto model words, whereas in a large vocabulary conversational speech recognitionsystem usually the hidden Markov model is usually used to model sub-word unitssuch as phone or syllable. There are two major tasks involved in a typical automaticspeech recognition system.

First, given a series of training obervations and their associated transcriptions, howdo we estimate the parameters of the hidden Markov models which represent thewords or phones covered in the transcriptions?

Second, given a set of trained hidden Markov models and an input speech observa-tion sequence, how do we find the maximum likelihood of this observation sequenceand the corresponding set of hidden Markov models which produce this maximumvalue? This is the speech recognition problem.

Two formal assumptions characterize hidden Markov models as used in speech recog-nition. A first-order Markov hypothesis states that history has no influence on thechain's future evolution if the present is specified, and the output independencehypothesis states that neither chain evolution nor past observations influence thepresent observation if the last chain transition is specified.

We first introduce Markov chains and then, as an extension, hidden Markov pro-cesses.

16.2 Markov Chains

In this section we introduce Markov chains and give a number of examples.

First we introduce some definitions.

Definition. A vector p = {po,Pi, • • • ,PN-I) is called a probability vector if thecomponents are nonnegative and their sum is 1, i.e.,

N-l

t=0

Example 1. The vectorp = (1/4,1/8,0,5/8)

is a probability vector.

Page 494: Nonlinear Workbook (3rd)

16.2. MARKOV CHAINS 475

Example 2. The nonzero vector (2,3,5,0,1) is not a probability vector. Howeversince all numbers are nonnegative it can be normalized to get a probability vector.Since the sum of the components is equal to 11 we obtain the probability vector

p = (2/11,3/11,5/11,0,1/11).

Definition. An N x N square matrix A = (<2y) is called a stochastic matrix if eachof its rows is a probability vector, i.e. if each entry of A is nonnegative and the sumof the entries in each row is 1.

Example. The following matrix is a stochastic matrix

( 0 1 0 0 \

1/2 1/6 1/3 00 1/3 0 2/3 '

1/4 1/4 1/4 1/4/We can easily prove that if A\ and A2 are stochastic matrices, then the matrixproduct A1A2 is also a stochastic matrix.Definition. A stochastic matrix A is called regular if all the entries of some powerAn are positive.Example 1. The stochastic matrix

A - ( ° MA-\\/2 1/2/

is regular since2 _ f l / 2 1/2 \

A ~U/4 3/4 Jis positive in every entry.

Example 2. The stochastic matrix

^ = ( l / 2 I/2)

is not regular since

A ~ U/4 1/4 J •Definition. A fixed point q of a regular stochastic matrix A is defined by

Example. Consider the regular stochastic matrix

/ 0 1 0\A= 0 0 1 .

Vl/2 1/2 Oj

Page 495: Nonlinear Workbook (3rd)

476 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

The vector q can be written as q = (x, y, 1 — x — y) with the constraints x € [0,1]and y £ [0,1]. Then the solution to the fixed point equation qA = q is given by

q = (1/5,2/5,2/5).

The fundamental property of regular stochastic matrices is contained in the follow-ing theorem.

Theorem. Let A be a regular stochastic matrix. Then

(i) A has a unique fixed probability vector q, and the components of q are all positive.

(ii) the sequence of matrices A, A2, A3,... of powers of A approaches the matrixB whose rows are each the fixed point q.

(iii) if p is any probability vector, then the sequence of vectors pA, pA2, pA3, . . .approaches the fixed point q.

Next we consider Markov chains. We consider a sequence of trials whose outcome,say

0 0 , ° l i ° 2 , • ••, O T - 1

satisfy the following two properties:

(i) Each outcome belongs to a finite set of outcomes

{go, Qi, 12, • • -,1N-I }

called the state space of the system. If the outcome on the t-ih trial is qu then wesay that the system is in state ^ at time t or at the t-th step.

(ii) The outcome of any trial depends at most upon the outcome of the immediatelypreceding trial and not upon any other previous outcomes; with each pair of states(qi,qj) there is given the probability ay- that q$ occurs immediately after qt occurs.

Such a stochastic process is called a finite Markov chain. The numbers «„•, calledthe transition probabilities can be arranged in the square N x N matrix

1 OQO a 0 1 • • • aQN-l

aw an . . . OIAT-IA =

\ a ; v _ i o a.fv-11 • • • UN-IN-I)

called the transition matrix. The transition matrix A of a Markov chain is a stochas-tic matrix.

Page 496: Nonlinear Workbook (3rd)

16.2. MARKOV CHAINS 477

Example. A typical example of a Markov chain is a random walk. Given the set(state space)

{ 0 , 1 , 2 , 3 , 4 , 5 }

where 0 is the origin and 5 the end point. A woman is at any of these points.She takes a unit step to the right with probability p or to the left with probabilityq = 1 — p, unless she is at the origin where she takes a step to the right to 1 or thepoint 5 where she takes a step to the left to 4. Let ot denote her position after tsteps. This is a Markov chain with the state space given by the set above. Thus 2

• means that the woman is at the point 2. The transition matrix A is

/0 1 0 0 0 0 \q 0 p 0 0 0

. _ O g O p O O0 0 q 0 p 0 '0 0 0 q 0 p

\Q 0 0 0 1 0 /

Next we discuss the question: What is the probability, denoted by afj , that thesystem changes from the state qi to the state qj in exactly t steps? Let A be thetransition matrix of a Markov chain process. Then the t-step transition matrix isequal to the t-th power of A; that is A® = A1. The a,-'- are the elements of thematrix A®.

Example. Consider again the random walk problem discussed above. Suppose thewoman starts at the point 2. To find the probability distribution after three stepswe do the following calculation. Since

p(0) = (0,0,1,0,0,0)

we find

pM=I*0>A=(0,q,0,p,0,0)p(2) =p^A= (g2,0,2p9,0,p2,0)p(3) =p(2)A = (0> q2 + 2 p g 2 ) 0 ) 3 ^ 0 p 3 )

Thus the probability after three steps to be at the point 1 is q2 + 2pq2. lip = q = 1/2we obtain p'3' = 1/2. Notice that

p^A = p^A3.

A state <?i of a Markov chain is called absorbing if the system remains in the state<& once it enters there. Thus a state qi is absorbing if and only if the i-th row of thetransition matrix A has a 1 on the main diagonal and obviously zeros everywhereelse in the row.

Page 497: Nonlinear Workbook (3rd)

478 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

Example. Consider the following transition matrix

/1/4 0 1/4 1/4 1/4\0 1 0 0 0

A= 1/2 0 1/4 1/4 00 1 0 0 0

V 0 0 0 0 1 /

The states q\ and q± are each absorbing (notice that we count from 0), since each ofthe second and fifth rows has a 1 on the main diagonal.

The transition probabilities of a Markov chain can be represented by a diagram,called the transition diagram, where a positive probability a^ is denoted by anarrow from the state qt to the state qj.

16.3 Discrete Hidden Markov Processes

The following notation is used.

N is the number of states in the model.

M is the total number of distinct observation symbols in the alphabet. If the obser-vations are continuous then M is infinite. We only consider the case for M finite.

T is the length of the sequence of observations (training set), where

1 = 0 , l , . . . , T - l .

Thus there exist NT possible state sequences.

Let&q : = {*>, 9 l i - - - , 9 W - l }

be the finite set of possible states. Let

V := {v0, vu...,vM-i)

be the finite set of possible observation symbols.

qt is the random variable denoting the state at time t (state variable).

ot is the random variable denoting the observation at time t (output variable).

O = (oo, O\,..., o r - i ) is the sequence of actual observations.

The set of state transition probabilities is A = (oy), where i,j = 0,l,...,N — l

a-ij =P(Qt+i = 3\qt = i)

Page 498: Nonlinear Workbook (3rd)

16.3. DISCRETE HIDDEN MARKOV PROCESSES 479

where p is the state-transition probability, i.e. the probability of being in state jat time t + 1 given that we were in state i at time t. We assume that the a^'s areindependent of time t. Obviously we have the conditions

Oy > 0 for i, j = 0 ,1 , . . . , N - 1

J V - l

^ dij = 1 f o r i = 0 , l , . . . , N — 1 .

Thus A is an N x iV matrix.

Example. If we have six states the transition matrix in speech recognition couldlook as follows

/0.3 0.5 0.2 0 0 0\0 0.4 0.3 0.3 0 00 0 0.4 0.2 0.4 00 0 0 0.7 0.2 0.1 •0 0 0 0 0.5 0.5

V 0 0 0 0 0 1.0/The conditional probability distribution of the observation at the t, ot, given thestate j is

bj(k) =p(ot = vk\qt =j)

where je{0,l,...,N — l} and k € { 0 , 1 , . . . , M - 1}, i.e. p is the probability ofobserving the symbol Vk given that we are in state j . Let B := { bj(k) } . We have

bj(k) > 0

for j € {0,1 , . . . , N - 1} and k € {0,1, . . . , M - 1} and

M - l

foij = 0,l,...,N-l.

The initial state distribution n — { TT; }

Ki = p{qo = i)

where i = 0 , 1 , . . . , N — l, i.e. TT, is the probability of being in state i at the beginningof the experiment (t = 0).

Thus we arrive at the definition:

A hidden Markov model (HMM) is a five-tuple (fi9, V, A, B,ir).

Page 499: Nonlinear Workbook (3rd)

480 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

Let A := (A,B,n) denote the parameters for a given hidden Markov model withfixed Q,q and V.

The three problems for hidden Markov models are:

1) Given the observation sequence

O = ( 0 0 , 0 1 , . . . , o T _ i )

and a model A = (A,B,n). Find P(O\X): the probability of the observations giventhe model.

2) Given the observation sequence

O = (o0,ou. ..,OT-I)

and a model A = (A,B,n). Find the most likely state sequence given the modeland observations. In other words, given the model A = (A, B, n) how do we choosea state sequence q = (q0, qlt..., qT-i) so that P{0, q|A), the joint probability of theobservation sequence O = (OQ, O\,..., OT-I) and the state sequence given the modelis maximized.

3) Adjust A to maximize P{O\X). In other words how do we adjust the hiddenMarkov model parameters A = (A, B, n) so that P(O\\) (or P{0, v|A) is maximized.

Example. As an example consider the Urn-and-Ball model. We assume that thereare N urns (number of states) in a room. Within each urn is a number of colouredballs, for example

{ red, blue, green, yellow, white, black } .

Thus the number M of possible observations is 6. The physical process for obtainingobservations is as follows. Randomly we choose an initial urn. From this urn, a ballis chosen at random, and its colour is recorded as the observation. The ball is thenreplaced in the urn from which is was selected. A new urn is then selected accordingto the random selection procedure associated with the current urn (thus there is aprobability that the same urn is selected again), and the ball selection is repeated.This process generates a finite observation sequence of colour, which we would liketo model as the observable output of a discrete hidden Markov model. For example

0 = (blue, green, red, red, white, black, blue, yellow).

Thus the number of observation is T = 8. It is obvious that the simplest hiddenMarkov model that corresponds to the urn-and-ball process is one in which eachstate corresponds to a specific urn, and for which a ball colour probability is definedfor each state. The choice of urns is dictated by the state-transition matrix of thediscrete hidden Markov model. Note that the ball colours in each urn may be the

Page 500: Nonlinear Workbook (3rd)

16.3. DISCRETE HIDDEN MARKOV PROCESSES 481

same, and the distinction among various urns is in the way the collection of colouredballs is composed. Therefore, an isolated observation of a particular colour ball doesnot tell which urn it is drawn from.

Example. We consider a discrete hidden Markov model representation of a coin-tossing experiment. Thus we have M = 2. We assume that the number of states isN = 3 corresponding to three different coins. The probabilities are

state 0 state 1 state 2==========================:=:=========P(Head) 0.5 0.75 0.25P(Tail) 0.5 0.25 0.75

W e set Head = 0 and Tail = 1. Thus

60(0)=0.5, 61(0) = 0.75, 62(0) = 0.25

&o(l) = 0.5, &i(l) = 0.25, 62(l) = 0.75.

Assume that all state-transition probabilities are 1/3 and assume the initial stateprobability of 1/3. Assume we observe the sequence (T = 10)

O=(H,H,H,H,T,H,T,T,T,T).

Since all state transitions are equiprobable, the most likely state sequence is the onefor which the probability of each individual observation is a maximum. Thus foreach Head, the most likely state is 1 and for each Tail the most likely state is 2.Consequently the most likely state sequence is

q = (1,1,1,1,2,1,2,2,2,2).

The probability of O and q given the model is

/ I N 10

P(O,q|A) = (0.75)10(ij .

Next we calculate the probability that the observation came completely from state0, i.e.

q = (0,0,0,0,0,0,0,0,0,0).

Then/ 1 \ 1 0

F(O,q|A) = (0.50)10Qj .

The ratio R of P(O, q|A) to P(O, q|A) is given by

P(O,qlA)_/3N 1 0

Thus the state sequence q is much more likely than the state sequence q.

Page 501: Nonlinear Workbook (3rd)

482 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

16.4 Forward-Backward Algorithm

Given the model A = (A,B,n). How do we calculate P(O\X), the probability ofoccurrence of the observation sequence

O = (O 0 ,0 i , . . . , O r - l ) ?

The most straightforward way to find P(O\X) is to find P(0|q, A) for a fixed statesequence. Then we multiply it by P(q|A) and then sum up over all possible statesequences. We have

P(O|q,A)=n1F(O(|gi,A)t=o

where we have assumed statistical independence of observations. Thus we find

P(O|q, A) = 6 g >o)M°i ) • • -K-MT-I) •

The probability of such a state sequence q can be written as

"\Q|AJ = 'Kqoaqoqiaqiq2 • • • a ? r - 2 9 T - i •

The joint probability of O and q, i.e., the probability that O and q occur simulta-neously, is simply the product of the above two expressions, i.e.

P(O, q|A) = P(O|q,A)P(q|A).

The probability of O (given the model) is obtained by summing this joint probabilityover all possible state sequences q. Thus

P(O |A)=£P(O |q ,A)P(q |A) .allq

Thus we find

P(O\X) = ^Z 7r9o69o(°o)a9o9i&«i(°i) •• • ( V - 2 9 r - A r - i ( ° r - i ) •90,91 9 T - 1

The interpretation of the computation is as follows. At time t = 0 we are in stateqo with probability irgo and generate the symbol OQ (in this state) with probabilitybqo{o0). The time is incremented by 1 (i.e. t — 1) and we make a transition tostate gi from state qo with probability agoqi and generate symbol O\ with probabilitybqi(oi). This process continues in this manner until we make the last transition (attime T — 1) from state qx~2 to state qr-i with probability a?r_29r_1 and generatesymbol oT_i with probability bqT_1(oT-i).

We see that the summand of this equation involves 2T — 1 multiplications and thereexists NT distinct possible state sequences. Hence a direct computation of thisequation will involve of the order of 2TN7* multiplications. Even for small numbers,for example N — 5 and T = 100, this means approximately 1072 multiplications.

Page 502: Nonlinear Workbook (3rd)

16.4. FORWARD-BACKWARD ALGORITHM 483

Fortunately a much more efficient technique exists to solve problem 1. It is calledforward-backward procedure.

The forward procedure is as follows. Consider the forward variable at(i) defined as

at(i) := P{o0Oi...ot,qt = i\X)

where i — O,1,...,N - 1. Thus at(t) is the probability of the partial observationsequence up to time t and the state i at time t, given the model. It can be shownthat at(i) can be computed as follows:

Initialization:«o(«) = Xibi{o0)

where i = 0 , 1 , . . . , N — 1.Recursion: For t = 0 , 1 , . . . , T — 2 and j = 0 , 1 , . . . , N - 1 we have

/N-l \at+i(j) = J2 at(i)aij bj(ot+1)

\t=o /

where j = 0 , 1 , . . . , N - 1 and t = 0 , 1 , . . . , T - 2.

Probability: We have

P(O|A)= E a T - i ( i ) .

The initialization step involves N multiplications. In the recursion step the summa-tion involves N multiplications plus one for the out of bracket bj(ot+i) term. Thishas to be done for j = 0 to N — 1 and t = 0 to T — 2, making the total numberof multiplications in step 2 as (N — 1)N(T + 1). Step 3 involves no multiplicationsonly summations. Thus the total number of multiplications is

N + N{N + 1){T-1)

i.e., of the order N2T as compared to 2TNT required for the direct method. ForN = 5 and T = 100 we need about 3000 computations for the forward method ascompared to 1072 required by the direct method - a saving of about 69 orders ofmagnitude.

The forward algorithm is implemented in the following Java program.

/ / Forward.Java

public class Forward{

public static void main(String[] args){

Page 503: Nonlinear Workbook (3rd)

484 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

int T = 10; / / Number of observationsint M = 2; / / number of observation symbolsint N = 3; / / Number of states

int[] obser = new int[T];obser [0] = 0; obser [1] = 0; obser [2] = 0; obser [3] = 0;obser [4] = 1; obser [5] = 0; obser [6] = 1; obser [7] = 1;obser [8] = 1; obser [9] = 1;

double [] [] b = new double [N] [M] ;b[0][0] = 0.5; b[l][0] = 0.75; b[2] [0] = 0.25;b[0][l] = 0.5; b[i][l] = 0.25; b[2] [1] = 0.75;

double [] [] A = new double [N] [N];A[0][0] = 1.0/3.0; A[0][l] = 1.0/3.0; A[0] [2] = 1.0/3.0;A[l][0] = 1.0/3.0; A[1][1] = 1.0/3.0; A[l] [2] = 1.0/3.0;A[2][0] = 1.0/3.0; A[2][l] = 1.0/3.0; A[2] [2] = 1.0/3.0;

double [] alphaold = new double [N];double [] alphanew = new double [N];

/ / initializationint temp = obser[0];

for(int i=0;i<N;i++){ alphaold[i] = (1.0/3.0)*b[i] [temp] ; }

// iterationfor(int t=0;t<=T-2;t++)

{temp = obser [t+1];for(int j=O;j<N;j++)

{double sum = 0.0;for(int i=0;i<N;i++)

{sum += alphaold[i]*A[i] [j] ;} / / end for loop ialphanew[j] = sum*b[j] [temp] ;} / / end for loop jfor(int k=0;k<N;k++){alphaold[k] = alphanew[k];}} / / end for loop t

Page 504: Nonlinear Workbook (3rd)

16.5. VITERBI ALGORITHM 485

// probabilitydouble P = 0.0;for(int i=0;i<N;i++){ P += alphanew[i]; }System.out.println("P = " + P);} // end main

>

In a similar manner we may define a backward variable j3t(i) as

/3t(i) := P{ot+iOt+2 ... o r_i , qt = i\X)

where i = 0 , l , . . . , i V — 1 . Thus /%(£) is the probability of the observation sequencefrom t + 1 to T — 1 given the state i at time t and the model A. It can be shownthat /3t(i) can be computed as follows

0r- i (O = 1

where i = 0 , 1 , . . .,N - 1. For t = T - 2,T - 1 , . . . , 1,0 and i = 0 , 1 , . . . , JV - 1 wehave

N-l

Pt{i) = Yl aijbj(°t+i)i3t+i(j).3=0

Thus

The computation of P(O\X) using /9((i) also involves of the order of N2T calcula-tions. Hence both the forward and backward method are equally efficient for thecomputation of P(O\X).

16.5 Viterbi Algorithm

The Viterbi algorithm is an algorithm to compute the optimal (most likely) statesequence (qo, qi,..., qr-i) in a hidden Markov model given a sequence of observedoutputs. In other words we have to find a state sequence such that the probabilityof occurrence of the observation sequence

(o0loi,.. .,OT-I)

from this state sequence is greater than that from any other state sequence. Thusour problem is to find q that will maximize P(O, q|A).

In order to give an idea of the Viterbi algorithm as applied to the optimum stateestimation problem a reformulation of the problem will be useful. Consider theexpression for P{O, q|A)

P(O,q\\) = P(O|q, A)P(q|A) = 7rqobqo(o0)aqoqibqi(Ol) . . . ^ . ^ . ^ . . ( o r - ! ) .

Page 505: Nonlinear Workbook (3rd)

486 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

We define

/ T-i \

U(q0, ?i, • • •, 9r-i) := - I ln(7rw6w(o0)) + £ lnK-mM°t)) J .

It follows thatP(P, q|A) = exp(-U{qQ, qu..., qT-i)) •

Consequently the problem of optimal state estimation, namely

maxqtP(O, qo,qu..., qT-i\X)

becomes equivalent tommqtU(q0,qi,. . . , q T ~ i ) .

This reformulation now enables us to view terms like

-\n{amjbqj{ot))

as the cost associated in going from state qi to state qj at time t.

The Viterbi algorithm to find the optimum state sequence can now be described asfollows: Suppose we are currently in state i and we are considering visiting state jnext. We say that the weight on the path from state i to state j is

-\n{aijbj{ot))

i.e., the negative of the logarithm of the probability of going from state i to statej and selecting the observation symbol Ot in state j . Here ot is the observationsymbol selected after visiting state j . This is the same symbol that appears in theobservation sequence

O = (o0,oi,... ,o r - i ) •

When the initial state is selected as state i the corresponding weight is

-\n(nibi(o0)).

We call this the initial weight. We define the weight of a sequence of states as thesum of the weights on the adjacent states. This corresponds to multiplying thecorresponding probabilities. Now finding the optimum sequence is merely a matterof finding the path (i.e. a sequence of states) of minimum weight through which thegiven observation sequence occurs.

16.6 Baum-Welch Algorithm

As described above the third problem in hidden Markov models deals with trainingthe hidden Markov model such a way that if a observation sequence having manycharacteristics similar to the given one be encountered later it should be able to

Page 506: Nonlinear Workbook (3rd)

16.7. DISTANCES BETWEEN HMMS 487

identify it. There are two methods that can be used:

The Segmental K-means Algorithm: In this method the parameters of themodel A = (A, B, n) are adjusted to maximize P{0, q|A), where q here is the opti-mum sequence as given by the solution to the problem 2.

Baum-Welch Algorithm: Here parameters of the model A = ( 4, B, n) are ad-justed so as to increase P(O\X) until a maximum value is reached. As describedbefore calculating P(O\\) involves summing up P(O, q|A) over all possible statesequences.

16.7 Distances between HMMsIf we want to compare two hidden Markov models then we need a measure fordistance between two hidden Markov models. Such a measure is based on theKullback-Leibler distance between two probability distribution function.

The Kullback-Leibler distance measure is given as follows: let p\(x) and p2{x) betwo probability density functions (or probability mass functions) then the Kullback-Leibler distance measure can be used to find out how close the two probability dis-tributions are.

Definition. The Kullback-Leibler distance measure I(p\,p2) for determining howclose the probability density function p2(x) is to p\{x) with respect to Pi(x) is

I(Pl,p2):= T Pi(x)\n(^\)dx.J-oo \P2(XjJ

In case pi{x) and p2(x) are probability mass functions then

'<«•»>- !><*>-(*$)•

Note that the Kullback-Leibler distance measure is not symmetric

I(PuP2) ^I(p2,Pi)

in general. If our objective is to simply compare p\ and p2 we can define a symmetricdistance measure as

Is(PuP2) •= -{I{pup2) + I(p2,Pi))-

It is the use of the Kullback-Leibler distance measure which leads to the definitionof the distance measure between two hidden Markov models. For hidden Markovmodels, the probability function is very complex, and practically it can be onlycomputed via a recursive procedure - the forward/backward or upward/downwardalgorithm ([76]). Thus there is no simple closed form expression for the Kullback-Leibler distance for these models. Commonly, the Monte-Carlo method is used tonumerically approximate the integral given above.

Page 507: Nonlinear Workbook (3rd)

488 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

16.8 Application of HMMs

We look at some of the details of applying the HMM algorithms to speech recog-nition. These include selecting the size of unit to model and connecting modelstogether to make words and phrases.

A Hidden Markov Model is a statistical model of a sequence of feature vector ob-servations. In building a recogniser with HMMs we need to decide what sequenceswill correspond to what models. In the very simplest case, each utterance couldbe assigned an HMM: for example, one HMM for each digit in a digit recognitiontask. To recognise an utterance, the probability metric according to each model iscomputed and the model with the best fit to the utterance is chosen. However, thisapproach is very inflexible and requires that new models be trained if new wordsare to be added to the recogniser. A more general approach is to assign some kindof sub-word unit to each model and construct word and phrase models from these.

The most obvious sub-word unit is the phoneme. If we assign each phoneme toan HMM we would need around 45 models for English; an additional model is alsocreated for silence and background noise. Using this approach, a model for any wordcan be constructed by chaining together models for the component phonemes.

Each phoneme model will be made up of a number of states; the number of statesper model is another design decision which needs to be made by the system designer.Each state in the model corresponds to some part of the input speech signal; wewould like the feature vectors assigned to each state to be as uniform as possibleso that the Gaussian model can be accurate. A very common approach is to usethree states for each phoneme model; intuitively this corresponds to one state forthe transition into the phoneme, one for the middle part and one for the transitionout of the phoneme. Similarly the topology of the model must be decided. Thethree states might be linked in a chain where transitions are only allowed to highernumbered states or to themselves. Alternatively each state might be all linked toall others, the so called ergodic model. These two structures are common but manyother combinations are clearly possible.

When phoneme based HMMs are being used, they must be concatenated to con-struct word or phrase HMMs. For example, an HMM for cat can be constructedfrom the phoneme HMMs for /k/ / a / and / t / . If each phoneme HMM has threestates the cat HMM will have nine states. Some words have alternate pronuncia-tions and so their composite models will have a more complex structure to reflectthese alternatives. An example might be a model for l ives which has two alterna-tives for the pronunciation of ' i ' .

While phoneme based models can be used to construct word models for any wordthey do not take into account any contextual variation in phoneme production. Oneway around this is to use units larger than phonemes or to use context dependant

Page 508: Nonlinear Workbook (3rd)

16.8. APPLICATION OF HMMS 489

models. The most common solution is to use triphone models where there is onedistinct phoneme model for every different left and right phoneme context. Thusthere are different models for the / a i / in / k - a i - t / and in / h - a i - t / . Now, aword model is made up from the appropriate context dependant triphone models:' c a t ' would be made up from the three models [ / s i l -k-a / / k - a - t / / a - t - s i l / ] .

While the use of triphones solves the problem of context sensitivity it presentsanother problem. With around 45 phonemes in English there are 453 = 91125possible triphone models to train (although not all of these occur in speech due tophonotactic constraints). The problem of not having enough data to effectively trainthese models becomes very important. One technique is state tying but another isto use only word internal triphones instead of the more general cross word triphones.Cross word triphones capture coarticulation effects accross word boundaries whichcan be very important for continuous speech production. A word internal triphonemodel uses triphones only for word internal triples and diphones for word finalphonemes; cat would become:

[s i l /k-a/ /k -a - t / / a - t / s i l ]

This will clearly be less accurate for continous speech modelling but the number ofmodels required is smaller (none involve silence as a context) and so they can bemore accurately trained on a given set of data.

There are many free parameters in an HMM, there are some tricks to reducing thenumber to allow better training. We discuss two of these: state tying and diagonalcovariance matrices.

A single HMM contains a number of free parameters whose values must be deter-mined from training data. For a fully connected three state model there are ninetransition probabilities plus the parameters (means and covariance matrices) of threeGaussian models. If we use 24 input parameters (12 Mel-frequency cepstral coeffi-cients plus 12 delta Mel-frequency cepstral coefficients) then the mean vector has24 free parameters and the covariance matrix has 242 = 576 free parameters making609 in all. Multiply this by 45 phoneme models and there are 27,405 free parame-ters to estimate from training data; using context sensitive models there are manymore (around 2.5 billion). With this many free parameters, a very large amount oftraining data will be required to get reliable statistical estimates. In addition, it isunlikely that the training data will be distributed evenly so that some models in atriphone based recogniser will recieve only a small number of training tokens whileothers will recieve many.

One way of addressing this problem is to share states between triphone models. Ifthe context sensitive triphone models consist of three states (for example) then wemight assume that for all / i / vowel models ( / i / in all contexts) the middle statemight be very similar and so can be shared between all models. Similarly, the initial

Page 509: Nonlinear Workbook (3rd)

490 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

state might be shared between all / i / models preceeded by fricatives. Sharing statesbetween models means that the Gaussian model associated with the state is trainedon the data assigned to that state in both models: more data can be used to traineach Gaussian making them more accurate. The limit of this kind of operation isto have all / i / models share all states, in which case we have reduced the modelsto context insensitive models again. In practice, states are shared between a signif-icant number of models based on phonetic similarity of the preceding and followingcontexts.

The covariance matrix associated with each Gaussian inside each state measuresboth the amount of variance along each dimension in the parameter space and theamount of co-variance between parameters. In two dimensions this co-variance givesus the orientation and 'stretch' of the ellipse shape. One simplification that can bemade is to ignore the co-variance part of the matrix and only compute and use theindividual dimension variances. In doing this we retain only the diagonal part ofthe co-variance matrix, setting all of the off-diagonal elements to zero. While thissimplification does lose information it means a significant reduction in the numberof parameters that need to be estimated. For this reason it has found favour in someHMM implementations.

Choosing to build phoneme or triphone based models means that to recognise wordsor phrases we must make composite HMMs from these subword building blocks. Amodel for the word cat can be made by joining together phoneme models for /k// a / and / t / . If each phoneme model has three states then this composite modelhas nine states but can be treated just like any other HMM for the purposes ofmatching against an input observation sequence.

To be able to recognise more than one word we need to construct models for eachword. Rather than have many separate models it is better to construct a networkof phoneme models and have paths through the network indicate the words that arerecognised. The phonemes can be arranged in a tree, each leaf of the tree corre-sponds to a word in the lexicon.

An example lexical tree which links together phoneme models in a network such thatalternate paths through the network represent different words in the lexicon. Thetree of phoneme models ensures that any path through the network correspondsto a real word. Each open circle represents a single HMM which might consistof three states. Each solid circle corresponds to a word boundary. Cases of mul-tiple pronunciations (for 'A') and homonyms ( 'har t ' and 'hear t ' ) can be seenin this network. If triphones models are being used this network would be expanded.

For connected word recognition this network can be extended to link words togetherinto phrases such that the legal paths through the network correspond to phrasesthat we want to recognise. This begs the question of what phrases are to be allowed;the answer lies in what is called a language model who's job is to define possible

Page 510: Nonlinear Workbook (3rd)

16.9. PROGRAM 491

word sequences either via definite patterns such as grammar rules or via statisticalmodels. The language model defines the shape of the network of HMMs; in any-thing but the simplest cases this network will be extremely complicated. While itis useful to think of a static, pre-constructed network being searched by the Viterbialgorithm, in a real recogniser the network is constructed as it is searched.

Assuming that we have designed our individual phoneme or triphone HMMs thenthe first step is to initialise the free parameters in the models. These parameters arethe transition probabilities between states and the means and covariance matricesof the Gaussian models associated with each state. Our next step will be to beginsupervised training where we will force align the models with speech samples andupdate the model parameters to better fit that segmentation. If we begin with apoor set of parameters (for example, by choosing random values for each parameter)the forced aligment will be unlikely to assign appropriate phonemes to each modeland so the model will be unlikely to improve itself. HMM training can be thoughtof as a search for the lowest point on a hilly landscape; if we begin from a pointclose to the lowest point we may well find it but if we begin from a point close toa higher dip, we may get stuck there and be unable to see the better solution overthe horizon. The standard way to initialise the Gaussian models in each state isto use a small amount of hand segmented data and align it to each model. In athree state model, each state might be given four vectors of a twelve vector inputsequence corresponding to one token. In this way the Gaussians are initialised toapproximate the distributions for each phoneme. Transition probabilities are lesstroublesome and are usually initialised to equal values.

This is the largest part of training the low level HMM parameters. The raw data arespeech recordings for which word transcriptions are available. The supervised train-ing procedure looks at each utterance in turn, constructs an HMM corresponding tothat utterance from the sub-word constituent models, recognises the utterance withthe HMM and finally updates the statistical estimates needed for training. After allof the utterances have been treated in this way, the parameters of the constituentmodels are updated according to the statistics collected. This process is repeateduntil the changes to the HMM parameters on each iteration are very small.

16.9 Program

The C++ program implements the hidden Markov model, where N is the numberof states (N = 3 in main), M is the total number of distinct observations (onlyM = 2 possible) and T is the number of observations.

/ / hmmtl.cpp

#include <iostream>#include <string>#include <cmath> // for log

Page 511: Nonlinear Workbook (3rd)

492 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

using namespace std;

class Data {private:

double **transitions;double remissions;double *pi_transitions;

public:Data(int.int);

double get_transition(int i,int j){ return transitions [i] [j] ; )•

double get_emission(int i.int j){ return emissions [i] [j] ; }

double get_pi_transition(int i){ return pi_transitions[i]; >

void set_transition(int i.int j,double v){ transitions [i] [j] = v; }

void set_emission(int i.int j,double v){ emissions[i] [j] = v; >

void set_pi_transition(int i,double v){ pi_transitions[i] = v; }

};

Data::Data(int n=0,int m=0)

•C

transitions = new double*[n+1];for(int i=0;i<n+l;i++)

transitions [i] = new double [n+1];

emissions = new double*[n+1];for(int i=0;i<n+l;i++)

emissions [i] = new double [m+1] ;

pi_transitions = new double[n+1];

>

class HMM {private:

Page 512: Nonlinear Workbook (3rd)

16.9. PROGRAM 493

int N, M, T;str ing o;double** alpha_table;double** beta_table;double* alpha_beta_table;double* xi_divisor;Data* current;Data* reestimated;

public:HMM(int n.int m);

void error(const string s)

{ cerr « "error: " « s « '\n'; >

void init(int si,int s2,double value){ current->set_transition(sl,s2,value); >

void pi_init(int s,double value){ current->set_pi_transition(s,value); >

void o_init(int s,const char c,double value)•[ current->set_emission(s , index (c) .value) ; y

double a(int si,int s2){ return current->get_transition(sl,s2); }

double Mint state,int pos){ return current->get_emission(state,index(o[pos-l])); >

double b(int state,int pos,string o)•i return current->get_emission(state,index(o[pos-1])); }

double pi(int state){ return current->get_pi_transition(state); }

double alpha(const string s);double beta(const string s);double ganuna(int t , int i) ;int index(const char c);double viterbi(const string s.int *best_sequence);double** construct_alpha_table();double** construct_beta_table();double* construct_alpha_beta_table();double xi(int t.int i.int j);

Page 513: Nonlinear Workbook (3rd)

494 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

void reestimate_pi();void reestimate_a();void reestimate_b() ;double* construct_xi_divisor();void maximize(string training,string test);void forward_backward(string s);

>;

HMM::HMM(int n=0,int m=0)

{N = n; M = m;current = new Data(n.m);

reestimated = new Data(n,m);

}

double HMM::alpha(const string s)

{string out;double P = 0.0;out = s;int Tl = out.length();double* previous_alpha = new double[N+l];double* current_alpha = new double[N+l];

// Initialization:for(int i=i;i<=N;i++) previous_alpha[i] = pi(i)*b(i,l,out);

/ / Induction:for(int t=l;t<Tl;t++) {

for(int j=l;j<=N;j++) {double sum = 0.0;for(int i=l;i<=N;i++) {sum += previous_alpha[i]*a(i, j) ;}

current_alpha[j] = sum*b(j,t+l,out);}for(int c=l;c<=N;c++)

previous_alpha[c] = current_alpha[c];}

// Termination:

for(int i=l;i<=N;i++) P += previous_alpha[i];return P;

}

Page 514: Nonlinear Workbook (3rd)

16.9. PROGRAM 495

double HMM::beta(const string s)i

double P = 0 .0 ;o = s;int T = o.lengthO;double* next_beta = new double[N+l];double* current_beta = new double[N+l];

// Initialization:for(int i=l;i<=N;i++)next_beta[i] = 1.0;

// Induction:double sum;

for(int t=T-l;t>=l;t—) {for(int i=l;i<=N;i++) {sum = 0.0;for(int j=l;j<=N;j++) {sum += a(i,j)*b(j,t+l)*next_beta[j];}

current_beta[i] = sum;}for(int c=l;c<=N;c++)

next_beta[c] = current_beta[c] ;}

// Termination:for(int i=l;i<=N;i++) P += next_beta[i]*pi(i)*b(i,1);return P;

}

double HMM::gamma(int t.int i){

return (alpha_table[t][i]*beta_table[t][i])/(alpha_beta_table[t]);}

int HMM::index(const char c) {switch(c) {case 'H': return 0;case 'T': return 1;default: error("no legal input symbol!");return 0;}

}

Page 515: Nonlinear Workbook (3rd)

496 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

double HMM::viterbi(const string s.int best_path[]){double P_star =0.0;string o = s;int *help = new int;int T = o.lengthO ;double* previous_delta = new double[N+l];double* current_delta = new double[N+l];int** psi = new int*[T+l];for(int i=0;i<=T;i++) psi[i] = new int[N+l];

// Initializitaion:for(int i=l;i<=N;i++){ previous_delta[i] = pi(i)*b(i,l); psi[l][i] = 0; >

double tmp, max;

// Recursion:for(int t=2;t<=T;t++) {for(int j=l;j<=N;j++) {max = 0.0;for(int i=l;i<=N;i++) {tmp = previous_delta[i]*a(i,j);if (tmp >= max) { max = tmp; psi[t][j] = i; >

}current _delta[j] =max*b(j,t);

>for(int c=l;c<=N;c++)

previous_delta[c] = current_delta[c];}

// Termination:for(int i=l;i<=N;i++) {

if(previous_delta[i] >= P_star) {P_star = previous_delta[i];best_path[T] = i;

}}

// Extract best sequence:

for(int t=T-l;t>=l;t—)best_path[t] = psi[t+l] [best_path[t+l]] ;

best_path[T+l] = - 1 ;return P_star;

Page 516: Nonlinear Workbook (3rd)

16.9. PROGRAM 497

>

double** HMM::construct_alpha_table(){

double** alpha_table = new double*[T+l];for(int i=0;i<=T+l;i++) alpha_table[i] = new double[N+l];

// Initialization:

for(int i=l;i<=N;i++) alpha_table[l] [i] = pi(i)*b(i,l) ;

// Induction:for(int t=l;t<T;t++) {for(int j=l;j<=N;j++) {double sum = 0.0;for(int i=l;i<=N;i++) {

sum += alpha_table[t] [i]*a(i,j);}alpha_table[t+l] [j] = sum*b(j ,t+l);

}}return alpha_table;

}

double** HMM::construct_beta_table(){double **beta_table = new double*[T+l];for(int i=0;i<=T+l;i++) beta.table[i] = new double[N+1];

// Initialization:for(int i=l;i<=N;i++) beta.table[T][i] = 1.0;

double sum;

// Induction:for(int t=T-l;t>=l;t—) {for(int i=l;i<=N;i++) {sum = 0.0;for(int j=l;j<=N;j++) {

sum += a(i,j)*b(j,t+l)*beta_table[t+l] [j] ;>beta_table[t] [i] = sum;

}}

// Termination:

Page 517: Nonlinear Workbook (3rd)

498 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

fordnt i=l;i<=N;i++)beta.table [1] [i] = beta_table[l] [i]*pi(i)*b(i,l);

return beta_table;}

double* HMM: :construct_alpha_beta_table()-C

double* alpha_beta_table = new double[T+l];

for(int t=l;t<=T;t++) {alpha_beta_table[t] = 0;fordnt i=l;i<=N;i++) {alpha_beta_table [t] += (alpha_table [t] [i] *beta_table [t] [i]);

}}return alpha_beta_table;

}

double* HMM::construct_xi_divisor()

ixi_divisor = new double[T+l];double sum_j;

fordnt t=l;t<T;t++) {xi_divisor[t] = 0.0;fordnt i=l;i<=N;i++) {

sum_j = 0.0;fordnt j=l;j<=N;j++) {sum_j += (alpha_table[t] [i]*ad, j)*b(j ,t+l)

*beta_table[t+l] [j]);}xi_divisor[t] += sum_j ;

}}return xi_divisor;

}

double HMM::xi(int t.int i.int j)

{return ((alpha_table[t] [i]*a(i, j)*b(j ,t+l)*beta_table[t+l] [j])/(xi_divisor[t])) ;

}

void HMM::reestimate_pi()

Page 518: Nonlinear Workbook (3rd)

16.9. PROGRAM 499

{for(int i=l;i<=N;i++) {

reestimated->set_pi_transition(i,gamma(l,i));}

>

void HMM::reestimate_a()•C

double sum_xi, sum_gamma;

for(int i=l;i<=N;i++) {forCint j=l;j<=N;j++) {sum_xi = 0.0; sum_gamma = 0.0;forCint t=l;t<T;t++) { sum_xi += xi(t,i,j); }forCint t=l;t<T;t++) { sum_gamma += gamma(t,i); >reestimated->set_transition(i,j,(sum_xi/sum_gamma));

>>

}

void HMM::reestimate_b()idouble sum_gamma;double tmp_gamma;double sum_gamma_output;forCint j=l;j<=N;j++) {forCint k=0;k<M;k++) {sum_gamma = 0.0; sum_gamma_output = 0.0;forCint t=l;t<=T;t++) {tmp_gamma = gamma(t,j);if(index(o[t-l]) == k) {sum_gamma_output += tmp_gamma;>sunLgamma += tmp_ganuna;

>reestimated->set_emission(j ,k,(sum_gamma_output/sum_gamma));

}}

}

void HMM::forward_backward(string o)•C

T = o.lengthO ;alpha_table = construct_alpha_table();beta_table = construct_beta_table();

Page 519: Nonlinear Workbook (3rd)

500 CHAPTER 16. DISCRETE HIDDEN MARKOV PROCESSES

alpha_beta_table = construct_alpha_beta_table0;xi_divisor = construct_xi_divisor();reestimate_pi();reestimate_a();reestimate_b();

// deletionfor(int t=l;t<=T;t++) deleted alpha.table[t];delete [] alpha_table;

for(int t=l;t<=T;t++)delete [] beta_table [t] ;delete [] beta_table;delete[] alpha_beta_table;delete [] xi_divisor;

Data* tmp_value = current;current = reestimated;reestimated = tmp_value;

}

void HMM::maximize(string o,string test)

{double diff_entropy, old_cross_entropy, new_cross_entropy;int c = 1;int t = test.lengthO ;old_cross_entropy = -((Iogl0(alpha(test))/logl0(2))/t);cout « "Re-estimation:\n";cout « " initial cross_entropy: " « old_cross_entropy « "\n";

do {forward_backward(o);new_cross_entropy = -(Iogl0(alpha(test))/logl0(2))/t;diff_entropy = (old_cross_entropy - new_cross_entropy);

old_cross_entropy = new_cross_entropy;C++;

> while(diff_entropy > 0.0);

cout « " No of iterations: " « c « "\n";cout « " resulting cross_entropy: "

« old_cross_entropy << "\n";

}

int main(void)

{

Page 520: Nonlinear Workbook (3rd)

16.9. PROGRAM 501

HMM hmm(3,2);

hmm.pi_init(1,0.33333333);hmm.pi_init(2,0.33333333);hmm.pi_init(3,0.33333333);hmm.init(1,1,0.33333333);hmm.init(1,2,0.33333333);hmm.init(1,3,0.33333333);hmm.init(2,1,0.33333333);hmm.init(2,2,0.33333333);hmm.init(2,3,0.33333333);hmm.init(3,1,0.33333333);hmm.init(3,2,0.33333333);hmm.init(3,3,0.33333333);hmm.o_init(l,'H',0.5);hmm.o_init(2,'H',0.75);hmm.o_init(3,'H',0.25);hmm.o.initd.'T'.O.S);hmm.o_init(2,'T',0.25);hmm.o_init(3,JT\0.75);

string training = "HTTHTTTHHTTHTTTHHTTHTTTHHTTHTTT""HHTTHTTTHHTTHTTTHHTTHTTTHHTTHTTTH";

String test = "HTHTTHTHTTHTHTHTHTTHHTHTHTTHTHTTHHT";

cout << "\nlnput: " « training « "\n\n";cout « "Probability (forward) : "

« hmm.alpha(training) « "\n";cout « "Probability (backward): "

« hmm.beta(training) « "\n\n";int *best_path = new int[256];cout « "Best-path-probability : "

« hmm.viterbi(training,best_path) << "\n\n";cout « "Best path: ";for(int t=l;best_path[t+l]!=-l;t++)

cout « best_path[t] « "," ;cout « best_path[t] « "\n\n";hmm.maximize(training,test);cout « " Probability (forward) : "

« hmm.alpha(training) « "\n";cout « " Best-path-probability : "

« hmm.viterbi(training,best_path) << "\n";return 0;

}

Page 521: Nonlinear Workbook (3rd)

Chapter 17

Fuzzy Sets and Fuzzy Logic

17.1 Introduction

A classical set (also called crisp set) is defined as a collection of elements or objectsx £ X which can be finite, countable, or overcountable. Each single element caneither belong to or not belong to a set A, A C X. In the former case, the statement xbelongs to A is true, whereas in the latter case this statement is false. Such a classicalset can be described in different ways: one can either enumerate (list) the elementsthat belong to the set; describe the analytically, for instance, by stating conditionsfor membership; or define the member elements by using the characteristic function,in which 1 indicates membership or 0 nonmembership.

Example. The set

N : = { 1 , 2 , 3,...}

is the set of all positive integers. The set is countable.

Example. The set

A :— { x : a: is a letter in the english alphabet, x is a vowel }

is given by

A — {a, e, i, o, u} .

The set is finite.

Example. The set of all real numbers is denoted by R. This set is overcountable.

502

Page 522: Nonlinear Workbook (3rd)

17.1. INTRODUCTION 503

Both C++ using the STL with the class set and Java with the class TreeSet allowset-theoretical operations. We can find the union, intersection and difference of twofinite sets. We can also get the cardinality of the finite set (i.e. the number ofelements). Furthermore, we can find out whether a finite set is a subset of anotherfinite set and whether a finite set contains a certain element.

In C++ the class set is a sorted associative container that stores objects of typeKey. The class set is a simple associative container, meaning that its value type,as well as its key type, is key. It is also a unique associative container meaning thatno two elements are the same. The class set is suited for the set algorithms

includesset_unionset_intersectionset_difference,set_symmetric_difference

The reason for this is twofold. First, the set algorithms require their arguments tobe sorted ranges, and, since the class set is a sorted associative container, theirelements are always sorted in ascending order. Second, the output range of thesealgorithms is always sorted, and inserting a sorted range into a set is a fast opera-tion. The class set has the important property that inserting a new element into aset does not invalidate iterators that point to existing elements. Erasing an elementfrom a set also does not invalidate any iterator, except of course, for iterators thatactually point to the element that is being erased.

The following program shows an application of this class.

/ / se ts t l .cpp

#include <iostream>#include <set>#include <algorithm>#include <cstring>using namespace std;

struct ltstr

{bool operator0 (const char* si,const char* s2) const

{ return strcmp(sl,s2) < 0; >

};

int main(void){.const int N = 3;

Page 523: Nonlinear Workbook (3rd)

504 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

const char* a[N] = { "Steeb", "C++", "80.00" };const char* b[N] = { "Solms", "Java", "80.00" };set<const char*,ltstr> Sl(a,a+N);set<const char*,ltstr> S2(b,b+N);set<const char*,ltstr> S3;

cout « "union of the sets SI and S2: " « endl;set_union(Sl.begin(),Sl.end(),S2.begin(),S2.end(),

ostream_iterator<const char*>(cout," "),ltstr());cout « endl;cout << "intersection of sets SI and S2: " « endl;set_intersection(Sl.begin(),Sl.end(),S2.begin(),S2.end(),

ostream_iterator<const char*>(cout," "),ltstr());cout « endl;set_difference(Sl.begin(),Sl.end(),S2.begin(),S2.end(),

inserter (S3, S3. beginO) ,ltstr()) ;cout « "Set S3 difference of SI and S2: " « endl;copy(S3.begin(),S3.end(),ostream_iterator<const char*>(cout," "));cout « endl;

// S2 subset of SI ?bool bl = includes(SI.beginO,Sl.end(),S2.begin(),S2.end(),ltstr());cout « "bl = " « bl « endl;

/ / S4 subset of S2 ?const char* c[l] = { "Solms" >;set<const char*,ltstr> S4(c,c+1);bool b2 = includes(S2.beginO,S2.end(),S4.beginO,S4.end(),ltstr());cout « "b2 = " « b2;return 0;

>

In Java the interface Set is a Collection that cannot contain duplicates elements.The interface Set models the mathematical set abstraction. The Set interfaceextends Collection and contains no methods other than those inherited fromCollection. It adds the restriction that duplicate elements are prohibited. TheJDK contains two general-purpose Set implementations. The class HashSet storesits elements in a hash table. The class TreeSet stores its elements in a red-blacktree. This guarantees the order of iteration.

The following Java program shows an application of the TreeSet class.

/ / SetOperation.Java

import java.ut i l .*;

Page 524: Nonlinear Workbook (3rd)

17.1. INTRODUCTION 505

public class SetOperation•C

public static void main(String[] args)

iString[] A = { "Steeb", "C++", "80.00" >;StringG B = { "Solms", "Java", "80.00" };

TreeSet SI = new TreeSetO;for(int i=0;i<A.length;i++)SI. add (A [i]);System.out.printlnC'Sl = " + SI);

TreeSet S2 = new TreeSetO;for(int i=0;i<B.length;i++)S2.add(B[i]);System.out.println("S2 = " + S2);

// unionTreeSet S3 = new TreeSet(SI);boolean bl = S3.addAll(S2);System.out.println("S3 = " + S3);System.out.printlnC'Sl = " + SI);

// intersectionTreeSet S4 = new TreeSet(SI);boolean b2 = S4.retainAll(S2);System.out.println("S4 = " + S4);System.out.println("S2 = " + S2);

// (asymmetric) set differenceTreeSet S5 = new TreeSet(SI);boolean b3 = S5.removeAll(S2);System.out.println("S5 = " + S5);

// test for subsetTreeSet S6 = new TreeSet(SI);boolean b4 = S6.containsAll(S2);System.out.println("b4 = " + b4);

// is element of set (contains)boolean b = SI.contains("80.00") ;System.out .printlnC'b = " + b);b = S2.contains("Steeb");System.out.printlnC'b = " + b);

Page 525: Nonlinear Workbook (3rd)

506 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

}}

The output is

51 = [80.00, C++, Steeb]52 = [80.00, Java, Solms]53 = [80.00, C++, Java, Solms, Steeb]51 = [80.00, C++, Steeb]54 = [80.00]52 = [80.00, Java, Solms]55 = [C++, Steeb]b4 = falseb = trueb = true

In fuzzy logic (Yager and Zadeh [98], Bojadziev [9], Bandemer [4], Ross [79], Zimmer-mann [100]) the notion of binary membership is extended to accommodate variousdegrees of membership on the real continuous interval [0,1], where the endpoints of0 and 1 conform to no membership and full membership, respectively, just as theindicator function does for crisp sets, but where the infinite number of values inbetween the endpoints can represent various degrees of membership for an elementx in some set on the universe X. The sets on the universe X that can accommodatedegrees of membership are termed as fuzzy sets. For a fuzzy set, the characteristicfunction allows various degrees of membership for the elements of a given set. Theuniverse of discourse is the universe of all available information on a given problem.Once the universe is defined we are able to define certain events on this informationspace. We describe sets as mathematical abstractions of these events and of theuniverse itself.

Fuzzy logic and fuzzy sets are powerful mathematical tools for modelling: uncertainsystems in industry, nature, and humanity; and facilitators for common-sense rea-soning in decision making in the absence of complete and precise information. Fuzzylogic and fuzzy sets find applications in control theory, signal processing, robotics,intelligent process control, expert systems, image processing, decision making, pat-tern recognition, cluster analysis, and a variety of learning methods, such as neuralnetworks, genetic algorithms, and inductive reasoning.

Let us first give an example where fuzzy sets can be applied.

Example. If we talk about hot and cold we might classify all temperatures above25°C as hot and all temperatures equal to or below 25°C as cold. This would giveus a crisp binary set where any temperature would be classified as either hot orcold. Alternatively we might want to specify a degree of hotness/coldness by thefollowing mapping

Page 526: Nonlinear Workbook (3rd)

17.1. INTRODUCTION 507

T < 0°C -> very cold0°C < T < 18°C ->• moderately cold18°C < T < 25°C ->• just right25°C < T < 30°C -» moderately hot

T > 30°C ->• very hot

Here we map temperatures on a crisp quintary set.

Definition. A fuzzy set, F, on a collection of objects, X, is a mapping

H¥{x) : X->{0,a}.

Here fJ.p(x) indicates the extent to which x has the attribute F. Hence Hp{x) is calledthe membership function. The most common types of fuzzy sets are normalized fuzzysets, F, for which

a = sup/ip(:r) — 1.

A nonempty fuzzy set A can always be normalized by dividing /ij(x) by supx H^{x).

In this case the result of the mapping, ii${x), can be interpreted as a degree of mem-bership or as a level of confidence in the truth or compatibility of the statement.

An alternative definition of a fuzzy set can be given.

Definition. If X is a collection of objects denoted generically by x then a fuzzy setA in X is a set of ordered pairs

A~{(x,^(x))\xeX}.

The function fi^ is called the membership function or grade of membership (alsocalled degree of compatibility or degree of truth) of x in A which maps X to themembership space M. When M contains only two points 0 and 1, A is nonfuzzyand fi%(x) is identical to the characteristic function of a nonfuzzy (crisp) set. Therange of the membership function is a subset of the nonnegative real numbers whosesupremum is finite. Elements with a zero degree of membership are normally notlisted.

Thus we consider ordinary sets as special cases of fuzzy sets, viz. those with only 0and 1 as membership degrees.

Definition. Two fuzzy sets A and B are equal if they have the same membershipfunctions

A = B <£• fi^(x) = ns(x) for all x € X.

Example. Employees of a company might need a minimum of 16MB RAM to runthe CAD package used by the company and should preferably have 64MB to run

Page 527: Nonlinear Workbook (3rd)

508 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

it efficiently. The degree of cost efficiency of a computer may be described by thefollowing fuzzy set

E = { (4,0), (8,0), (16,0.2), (32,0.5), (64,1), (128,0.8), (256,0.5), (512,0.2) } .

Example. A vehicle should be kept close to the desired travelling speed. Thedegree of closeness is modeled by the following continuous membership function

M*)=«*P( 27-J

where d is the desired travelling speed. A fuzzy set of speeds can be denoted by

S = {(x,/i^(x))\xe[0,200}}.

Example. The fuzzy set A "real numbers close to 3" could be modelled by themembership function

Au(a:)=exp(-2|a;-3|) .

Example. The fuzzy set A "integers close to 15" can be expressed by the finitefuzzy set

A = { (12,0.2), (13,0.5), (14,0.8), (15,1.0), (16,0.8), (17,0.5), (18,0.2) }.

The fuzzy set consists of 7 ordered pairs. Thus the membership function \i-^ takesthe following values on [0,1]

^ (12) =0.2, ^ (13) = 0.5, ^ (14) = 0.8, ^ (15) = 1.0,

M^(16) = 0.8, MI(17) = 0.5, Mj(18) = 0.2 .

In many cases the membership function is a trapezoidal function. A C++ imple-mentation is as follows. We assume that a < b < c < d.

II trapez.cpp

#include <iostream>using namespace std;

double trapez(double a,double b,double c,double d,double x)

•Cif((x >= b) kk (x <= c)) return 1.0;

if((x > a) kk (x < b)) return (x-a)/(b-a);

if((x > c) kk (x < d)) return (d-x)/(d-c);

else return 0.0;

>

int main(void)

Page 528: Nonlinear Workbook (3rd)

17.1. INTRODUCTION 509

{

double a = 1.0, b = 2.0, c = 5.3, d = 7.3;double x = 6.1;double result = trapez(a,b,c,d,x);cout « "x = " « x « " " « "result = " « result;cout « endl;x = 4.1;result = trapez(a,b,c,d,x);cout « "x = " « x « " " « "result = " « result;cout « endl;

return 0;

}

Definition. A fuzzy set A can be contained within another fuzzy set B and wewrite

A C B iff nx{x) < ns{x) for all x e X.

A is strictly contained in A C B, iff ^^(x) < fJ,^(x) for all x G X.

Example. The fuzzy set

A = { (0.1,0.2), (0.2,0.4), (0.3,0.7), (0.4,1.0), (0.5,0.2), (0.6,0.1) }

is contained within B

B = { (0.1,0.3), (0.2,0.4), (0.3,1.0), (0.4,1.0), (0.5,0.6), (0.6,0.2) } .

Both, A and B are normalized fuzzy sets.

Definition. The support for a fuzzy set, F, S(F), is the crisp set of all x 6 X suchthat Hp(x) > 0.

Definition. The a-level set is the crisp set of elements that belong to the fuzzy setF with degree of membership equal to or greater than a

Fa:={x^X\n¥>a}.

Example. In the first example the support for the set E is given by {16, 32, 64,128, 256, 512 } and the 0.5-level set is given by

£0.5 = { 32, 64, 128, 256 } .

Definition. A fuzzy set F is convex if its membership function, /J.f;(x), is convex,i.e. if

Mj?(Azi + (1 - A)z2) > minima;!),MpO^)}, xltx2eX, Ae [0 , l ] .

Alternatively, a fuzzy set is convex if all a-level sets are convex.

Page 529: Nonlinear Workbook (3rd)

510 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

A special property of two convex fuzzy sets, say A and B, is that the intersectionof these two fuzzy sets is also a convex fuzzy set.

Definition. The crossover points of a membership function are defined as the ele-ments in the universe for which a particular fuzzy set A has the values equal to 0.5,i.e., for which fi^ = 0.5.

Definition. For a finite fuzzy set A the cardinality \A\ is defined as

\A\ := £ »fa) •xex

The quantity

is called the relative cardinality. Obviously the relative cardinality of a fuzzy setdepends on the cardinality of the universe X. Thus we have to choose the sameuniverse if we want to compare fuzzy sets by their relative cardinality.

Example. Consider the fuzzy set

A = { (1,0.2), (2,0.5), (3,0.8), (4,1.0), (5,0.7), (6,0.3) } .

Then\A\ = 0.2 + 0.5 + 0.8 + 1.0 + 0.7 + 0.3 = 3.5.

Its relative cardinality is

Hi-*?-The relative cardinality can be interpreted as the fraction of elements of X being inA, weighted by their degrees of membership in A.

17.2 Operators for Fuzzy Sets

17.2.1 Logical OperatorsIn this section we give the basic definitions for operations on fuzzy sets. The mem-bership function is obviously the crucial component of a fuzzy set. It is therefore notsurprising that operations with fuzzy sets are defined via their membership func-tions. They constitute a consistent framework for the theory of fuzzy sets. Theyare, however, not the only possible way to extend classical set theory consistently.The main operations of fuzzy sets are:

the intersection of fuzzy sets

the union of fuzzy sets

Page 530: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 511

the complement of fuzzy sets.

Definition. The membership function fJ,g(x) of the intersection

C = AC\B

satisfies for each x 6 X

Vd{x) = min{/zj(z), ^g{x)} .

Definition. The membership function ng(x) of the union

C = Al>B

satisfies for each x G X

fi5(x) = max{nx{x), ns{x)} .

Definition. The membership function fi^(x) of the complement

C<£A

satisfies for each i g l

H5{x) = M^(z) = 1 - /^(z) •

These are extensions of the intersection, union, and complement for classical sets.In the C++ program we implement the intersection of fuzzy sets. We pass themembership function f l ( ) and f2() to the function min() to find the intersection

// fuzzmin.cpp

#include <iostream>#include <cmath> // for exp(), fabsOusing namespace std;

double fl(double x){ return exp(-fabs(x-3.0)); }

double f2(double x){ return exp(-fabs(x-4.0)); }

double minCdouble (*fl)(double).double (*f2)(double).double x){if(fl(x) >= f2(x)) return f2(x);

else return fl(x);

}

Page 531: Nonlinear Workbook (3rd)

512 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

int main(void)

idouble x = 3.5;double result = min(fI,f2,x);cout « "x = " « x « " " « "result = " « result « endl;x = 3.0;result = min(fI,f2,x);cout « "x = " « x « " " « "result = " « result « endl;x = 5.0;result = min(fI,f2,x);cout « "x = " « x « " " « "result = " « result « endl;

return 0;

}

Example. Let E be the fuzzy set of efficient PCs discussed above and let B be thefuzzy set of powerful PCs

P = {(4,0), (8,0), (16,0.2), (32,0.3), (64,0.4), (128,0.5), (256,0.8), (512,1.0)} .

Then the fuzzy set of PCs which are efficient AND powerful is given by

A = EnP

= {(4,0), (8,0), (16,0.2), (32,0.3), (64,0.4), (128,0.5), (256,0.5), (512,0.2)}

while the union operation yields

O = EUP

= {(4,0), (8,0), (16,0.2), (32,0.5), (64,1), (128,0.8), (256,0.8), (512,1.0)} .

The fuzzy set of PCs which are NOT powerful is given by

N=<jLP= {(4,1), (8,1), (16,0.8), (32,0.7), (64,0.6), (128,0.5), (256,0.2), (512,0)} .

Example. Assume that the statement "The weather in Johannesburg is hot" isgiven a confidence level represented by the following membership function

^ ^ = l + exp(-(T-24)/2)

and that the pleasant temperature range is given by

, , / ( T - 2 4 ) 2 \Hp(T) = exp ( ^ ^-Lj .

Page 532: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 513

Thus for the logical AND we have "It is hot AND pleasant", and for the logical ORwe have "It is hot OR pleasant".

Consider the fuzzy sets A, B, and C. We have the following properties

A n A = A idempotence

A U A = A idempotence

A n B = B fl A commutativity

A U B = B U A commutativity

(AnB)nC = Art(BnC) associativity

(AU B) U C = Alt (B U C) associativity

An (BI)C) = (AnB) U (An C) distributivity

Au(BnC) = (Al)B)n(AUC) distributivity

A = A double complement

A n B = A U B DeMorgan's law

AUB = AnB DeMorgan's law

These properties have the same structure as those for classical sets.

Let 0 be the empty fuzzy set. Then we have the rules

A n (X x [0,1]) = A identity

A U 0 = A identity

A n 0 = 0 identity

AU(X x [0,1]) = X x [0,1] identity

17.2.2 Algebraic OperatorsBesides the basic operations on fuzzy sets given above, namely intersection, unionand complement we can define a large number of other algebraic operations whichwe now summarize.

Definition. The algebraic sum, C = A + B, of two fuzzy sets with membershipfunctions / i j and \x-^ is a fuzzy set with membership function

fJ-A+si^ = Vzix) + Vg(x) ~ VA(X^S(X)' xeX.

We writeC = A + B = {{x,nx+s(x))\xeX}.

Page 533: Nonlinear Workbook (3rd)

514 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

Definition. The algebraic product, C = A • B, of two fuzzy sets with membershipfunction \i-^ and fig is a fuzzy set with membership function

VA-B = VA(X)VB(X)> x e X .

We write

Definition. The bounded sum, C = A(B B, of two fuzzy sets with membershipfunction fi^ and /i^ is a fuzzy set with membership function

^A®B = m i n { X> VA(X) + HB(X) } •

We writeC = {(X,/IX(BS(X))\X€X}.

Definition. The bounded difference, C = AQB, of two fuzzy sets with membershipfunction / i j and /zg is a fuzzy set with membership function

f+AeS = m a x ( °. VA(X) +V§(X)-1}-

We writeC = {(x,nXeS{x))\xeX}.

The Cartesian product of fuzzy sets is defined as follows.

Definition. Let A\, ..., An be fuzzy sets in X\, ..., Xn. The Cartesian product isthen a fuzzy set in the product space X\ x . . . x Xn with a membership function

^AIX^XAJ^) = min{/x^.(a;j)|a;= (xi,x2, • • • ,xn), xt € Xt } .

Definition. The mth power of a fuzzy set A is a fuzzy set with the membershipfunction

nm{x) = {n{x))m, x€X.

The Fuzzy AND and Fuzzy OR operators combine the logical AND and OR opera-tors with the arithmetic norm.

Definition. The fuzzy AND of two fuzzy sets A and B is denned as

C = Ar\B

with membership function

fj,5{x) = MJnB = 7min{^(a;),/ig(a;)} + -(1 - 7)(Mjj(z) + / * B W )

where 7 can be varied between 0 and 1 in order to weight the logical AND againstthe arithmetic mean. For 7 = 1 the fuzzy AND reduces to the logical AND and for

Page 534: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 515

7 = 0 the fuzzy AND operator reduces to the arithmetic mean.

Definition. The fuzzy OR of two fuzzy sets A and B is defined as

C = AUB

with membership function

PcW = ^AUB = 7max{^j(:r), fi^x)} + -(1 - i){jifa) + liS{x)), 7 € [0,1].

17.2.3 Defuzzification Operators

In many practical applications we would like to obtain a crisp decision from ourfuzzy analysis of the problem. For example, in a problem where a company usesfuzzy logic to decide on one of many marketing companies the result of the fuzzyanalysis should be exactly one company. Similarly, in a control problem (e.g. theclassic pole-balancing problem) we would like a crisp decision for the force whichshould be applied to the cart. The following operators are commonly used to extracta crisp decision from a fuzzy set.

Definition. The maximum grade operator returns that support value which has themaximum grade (degree of truth). If there is no unique support value correspondingto the maximum grade then it returns any one of these.

Definition. The minimum grade operator returns that support value which has theminimum grade. If there is no unique support value corresponding to the minimumgrade then it returns any one of these.

In many cases a small variation in the membership function can cause a very bigvariation in the decision (conclusion). For example, if we have a non-convex fuzzyset, then a small variation might change the decision from the one maximum to theother. This can be problematic, especially in the case of control problems where itcan be a major cause of instability. Hence, for control problems one often uses thecentroid of the fuzzy set for the crisp control strategy.

Definition. The centroid c (or centre of mass) of a fuzzy set, F with membershipfunction /ip{x), i € l i s denned by

._ Exex x • Hp{x)Exex Hpix)

in the case of a discrete membership function and by

c . _ /sex * ' M F M

fxex ^pte)

Page 535: Nonlinear Workbook (3rd)

516 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

in the case where fJ,p(x) is continuous.

For some applications it can be useful to obtain a crisp set from a fuzzy set. Forexample, when trading with stocks, one might want to use fuzzy logic inference todecide which stocks should be sold.

Definition. The a-cut operator returns a crisp set with a grade of 0 for supportvalues with grade less than a and 1 for support values which have a grade largerthan or equal to a.

Definition. The fi-cut operator returns the crisp set with grade 1 for the /3 supportvalues which have the highest grade and 0 for the remaining support values.

Example. Consider the fuzzy set

F = { (1,0.85), (2,0.7), (3,0.1), (4,0.44), (5,0.87), (6,0.2), (7,0.19) } .

The maximum grade is 5, the minimum grade is 3, the centroid is 3.34 « 3, thea-cut with a = 0.7 returns the crisp set

{ (1,1), (2,0), (3,0), (4,0), (5,1), (6,0), (7,0) }

and the /3-cut with j3 = 3 returns the crisp set

{(1,1), (2,1), (3,0), (4,0), (5,1), (6,0), (7,0)}.

There are a number of other defuzzification operators described in the literature,for example the weighted average method (only applies to symmetrical membershipfunctions), the centre of sums, centre of largest area and first (or last) maxima. Fordetails and examples we refer to the literature (Ross [79]).

17.2.4 Fuzzy Concepts as Fuzzy Sets

It is often convenient to have implemented fuzzy concepts like large, small, near,greater than and less than as fuzzy sets on a given universe. For example, the profitmade on a product might, for arguments sake, be between 0% and 100%. We wouldlike to implement the concept large as a fuzzy set. A simple way of doing this isto assume that the fuzzy set has points on a straight line with grade 1 for 100%profit and grade 0 for 0% profit. Similarly, we could define the concept, small, asa fuzzy set with a straight line as the membership function which gives grade 1 for0% profit and grade 0 for 100% profit.

Definition. The concept small is represented by a fuzzy set, 5 with membershipfunction

fig{x) =%min ^Cniax

Page 536: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 517

and the concept large is represented by a fuzzy set L with membership function

n(x) = -—z^— ••Lmax Jjmin

Another useful concept is near. For example, the profit on a certain product mightbe near 40%. We choose to implement near, as a fuzzy set whose membership func-tion is given by a normal distribution with width 10% of the size of the universe.

Definition. Consider a universe, X. The concept, nearp, with p £ X is representedby a fuzzy set, N, with membership function

1 f-(x-p)2\

where the variance, a, is chosen by default as a = 0-l(xmax — xmin). If a —> 0, thenwe find the crisp is equal to the Dirac-Delta function.

Gaussian membership functions are also used in fuzzy pattern recognition. Supposewe have a one-dimensional universe on the real line, i.e., X — R. Consider twofuzzy sets A and B having normal Gaussian membership functions

n{x) = exp y ^ j , H{x) = exp ( - J .

An inner product can be defined (Ross [79]) yielding

A • B = exp y^+ab)2J = M ( o) = HS{x0)

whereOab + aba

x 0 := • .

If the two fuzzy sets are identical, then the inner product equals one.

Finally we define the fuzzy concepts greater than and less than. For example, theprofit on a certain product might be greater than 20%. We use a Fermi-Dirac dis-tribution whose transition is at 40% to represent this fuzzy concept.

Definition. Consider a universe, X. The concept, greater than p, with p € X isrepresented by a fuzzy set, G, with membership function

where the slope A is chosen by default as A = 0.1(xmax — xmin). Note that for A —»• oowe recover the crisp greater than which is the step function

/ •> I _ / 1 for all x > p/"G^;|A^OO - jofor allx < p

Page 537: Nonlinear Workbook (3rd)

518 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.2.5 HedgingA linguistic hedge or a modifier is an operation which modifies the meaning of aterm. Concentration and dilation hedges are frequently used in fuzzy inference.Typical linguistic terms for concentrators are extremely, very. This shifts the em-phasis towards larger support values, i.e. they increase the restrictiveness of thefuzzy set.

Definition. A linguistic hedge is an operation that modifies the meaning of a termor, more generally, of a fuzzy set. If A is a fuzzy set then the modifier m generatesthe (composite) term B = m(A).

Mathematical models frequently used for modifiers are

concentration Vconz(x) = (vxW>2

dilation ndi\x(x) = {^x))1'2 •For example, assume we choose the following membership function for the fuzzy set,H, corresponding to the linguistic term the weather is hot

The grade of membership (degree of truth) given to various temperatures is givenin the following table

I! T I! 0 I 5 I 10 I 15 I 20 I 25 I 30 I 35 I I TI nz(T) I 0 | 0.125 ~0~l!5~ 0.375 0.5 0.625 0.75 0.825 ~T~

This membership function will give a support of 0.5 to a temperature of 20°C, 0.75to 30°C and 0.825 to 35°C. The set of very hot temperatures should give only highertemperatures significant support. The membership function for the fuzzy set veryhot, veryH, can be obtained from the membership function of H by using a hedge

*WCT) = (M5(T))2

The grade of membership for very hot is given in the following table

^ T [I 0 I 5 1 10 I 15 I 20 I 25 I 30 I 35 I 40 1

^veryg(r) ~Q~ °-0 1 6 °- 0 6 3 °-1 4 1 °-25 °-3 9 1 °- 5 6 3 0-681 T~

Only the temperatures 35° and 40°C have a 0.6-level support for very hot while 25°,30°, 35° and 40°C all have a 0.6-level support for hot. The hedging thus restricts(or compressed) our fuzzy set around high temperatures.

A commonly used mathematical expression for hedging is

^hedgetf = ^H'

with K > 1 for compression and n < 1 for dilation. For example, we could use thefollowing hedge allocation for compression and dilation:

Page 538: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 519

hedge Kvaguely —> 0.05slightly —> 0.25

somewhat —> 0.5very —> 2

extremely —> 3exactly —> oo

Another hedge commonly used is a contrast enhancer

_ / 2 ( ^ ( T ) ) 2 for /i5(T)G[0,i]VenhH 11 — 2(1 — ns{T))2 otherwise

17.2.6 Quantifying Fuzzyness

From information theory we know that we can define the missing information of aprobability distribution by the Shannon entropy

S(x):=-^(p(x)logap(x))

where the base of the logarithm, a, determines the units of information. If a = 2,then the unit of information is the bit (binary digit). This function has a minimumof 0 if the probability distribution is a delta-function. We would expect this sincein this case we know with certainty that the solution is xo (with probability 1) andhence there is no missing information. Similarly, if the probability distribution isflat, i.e.,

p{x) = — for all xsize of X

then the missing information is a maximum. We can define the entropy of a fuzzyset

F={(x,^(x))\xeX}by

£a(F) := Sa(F) + Sa(t F)with

s.(f) = - E ^ ) i o g . W i ) )x€X

where a a positive constant. Thus the entropy is a suitable measure for fuzzyness.

Example. Consider the following two fuzzy sets

A = {(1,0.4), (2,0.8), (3,1.0), (4,0.7), (5,0.3)}

andB = {(1,0.0), (2,0.1), (3,1.0), (4,0.2), (5,0.1)} .

Then the £2{A) = 3.46_bits, while £2(B) = 1.66 bits. As we would expect, A ismuch more fuzzy than B.

Page 539: Nonlinear Workbook (3rd)

520 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.2.7 C + + Implementation of Discrete Fuzzy SetsIn the following header file fuzzy. h we implement the functions described above asmethods within the class Fuzzy. The function name near is replaced by closeTosince near is used in some C++ compilers as keyword.

/ / fuzzy.h

#include <fstream>#include <cmath> // for exp, log, powusing namespace std;

class Fuzzy-Cpublic:Fuzzy ();Fuzzy(const int size,const doubled xMin=0,const doubleft xMax=l);Fuzzy(const Fuzzy& arg);

void setDomain(const doublefc xMin,const doubleft xMax);void fillGrades(const doublefe fillValue);void normalize();

void small 0;void large();void rectangle(const doublefc left,const doubleft right);void triangle(const doubleft left,const doublefe center,

const doublefc right);void trapezoid(const doublefc leftBot.const doubled leftTop,

const doublefe rightTop,const doublefe rightBot);

// Fuzzy greater thanvoid greaterThan(const doublefe arg.double lambda = 0);

// Fuzzy less thanvoid lessThan(const doubleft arg.double lambda = 0);

// Fuzzy nearvoid closeTo(const doubled arg.double sigma = 0);

int supportMaxGradeIndex() const;int supportMinGradelndexO const;double supportMaxGrade() const;double supportMinGradeO const;double minGradeO const;double maxGradeO const;double centroidO const;

Page 540: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 521

double cardinality() const;double relativeCardinalityO const;

Fuzzy limit(const doublefe ceiling);Fuzzy alphaCut(const doublefe alpha) const;Fuzzy betaCut(const int beta) const;

double entropy(const doublefe base = 2) const;

double xMinO const { return _xMin; };double xMaxO const { return _xMax; >;double domainSize () const { return _domainSize; >;double resolution () const { return _resolution; };

int isSubSet(const Fuzzyfe arg) const;

// grade for support idoublefe operator [] (const int i); // Grade for support i// grade at x via linear interpolationdouble operator 0 (const doubled x) const;

Fuzzyfc operator = (const Fuzzyft arg);

Fuzzy operator ! () const; // Logical NOTFuzzy operator && (const Fuzzyft arg) const; // Logical ANDFuzzy operator || (const Fuzzyfe arg) const; // Logical ORFuzzy operator + (const Fuzzy& arg) const; // Bounded +Fuzzy operator - (const Fuzzyfe arg) const; // Bounded -Fuzzy operator '/. (const Fuzzy& arg) const; // Algebraic +Fuzzy operator * (const Fuzzyfc arg) const; // Algebraic *Fuzzy operator & (const Fuzzy& arg) const; // Fuzzy ANDFuzzy operator | (const Fuzzyfc arg) const; // Fuzzy ORFuzzy operator < (const Fuzzyft arg) const; // Less ThanFuzzy operator > (const Fuzzyfc arg) const; // Greater ThanFuzzy operator == (const Fuzzyft arg) const; // Equal ToFuzzy operator >= (const Fuzzyfc arg) const; // Greater or EqualFuzzy operator <= (const Fuzzy& arg) const; // Less or EqualFuzzy operator != (const Fuzzyfe arg) const; // Not Equal To

// Fuzzy AND and Fuzzy ORdouble gammaO const { return _gamma; };void setGamma(const doublefc newGamma) { _gamma = newGamma; };

// Weighted mean of array of fuzzy sets.// If arg weights omitted, regular mean.

Page 541: Nonlinear Workbook (3rd)

522 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

static Fuzzy mean(const Fuzzy* const * const sets,const int nSets,const double* const weights = NULL);

Fuzzy enhanceContrastO const;Fuzzy hedge(const double* hedgeExp) const;// hedge constants:static const double extremely;static const double very;static const double substantially;static const double somewhat;static const double slightly;static const double vaguely;

friend ostreamfe operator « (ostream*,const Fuzzy&);friend istreamfe operator >> (istream*,Fuzzy*);

public:double* _grades;int _size;double .gamma; // For FUZZY AND and FUZZY OR.double _xMin, _xMax, _resolution, _domainSize;void resize(const int newSize);

private:// default value for gamma parameterstatic const double _defaultGamma;

double linearlnterpolate(const double* xO,const double* yO,const double* xl,const double* yl,

const double* x) const;

};

// static class constantsconst double Fuzzy::_defaultGamma = 0.5;// hedge exponents:const double Fuzzy::extremely = 4.0;const double Fuzzy::very = 2.0;const double Fuzzy::substantially = 1.5;const double Fuzzy::somewhat = 0.5;const double Fuzzy:-.slightly = 0.25;const double Fuzzy::vaguely = 0.03;

// Implementation

Fuzzy::Fuzzy(): _gamma(_defaultGamma), _grades(NULL), _size(0)

Page 542: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 523

{setDomain(0,l);

>

Fuzzy::Fuzzy(const int size,const doublefe xMin.const doublefe xMax)

: _gamma(_defaultGamma),.grades(NULL),_size(0)•C

resize(size);

setDomain(xMin,xMax);

}

Fuzzy::Fuzzy(const Fuzzyft arg)

: _gamma(_defaultGamma),_grades(NULL),_size(0){ *this=arg; }

void Fuzzy::setDomain(const doubles xMin.const doublefc xMax)

i_xMin = xMin; _xMax = xMax;_domainSize = xMax - xMin;if(_size != 1) .resolution = _domainSize/(_size - 1.0);

else .resolution = 1.0;

}

void Fuzzy::fillGrades(const doublefe fillValue)

{

for(int i=0;i<_size;i++) _grades[i] = fillValue;}

void Fuzzy::normalize()

{.double max = maxGradeO;

for(int i=0;i<_size;i++) _grades[i] /= max;

}

void Fuzzy::large(){double range = _xMax - _xMin;double slope = (double)I/range;double intercept = -slope*_xMin;double dx = range/(_size - 1.0);double x = _xMin;for(int i=0;i<_size;i++){_grades[i] = slope*x+intercept;x += dx;

Page 543: Nonlinear Workbook (3rd)

524 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

}>

void Fuzzy::small(){large 0;

for(int i=0;i<_size;i++) .grades[i] = 1.0 - .grades[i];}

void Fuzzy::rectangle(const doubled left,const doublefc right){double range = _xMax - _xMin;double dx = range/(_size - 1.0);double x = _xMin;for(int i=0;i<_size;i++){if((x <= left) II (x >= right)) _grades[i] =0.0;else _grades[i] = 1.0;x += dx;

}>

void Fuzzy::triangle(const doublefe left,const doubled center,const double& right)

{double range = _xMax - _xMin;double dx = range/(_size - 1.0);double x = _xMin;for(int i=0;i<_size;i++)

{

if((x <= left) II (x >= right)) .grades[i] =0.0;else if(x < center)_grades[i] = linearlnterpolate(left.O,center,l,x);else_grades[i] = linearlnterpolate(center,1,right,0,x);x += dx;}

}

void Fuzzy::trapezoid(const doubleft leftBot.const double& leftTop,

const doublefe rightTop,const doubleft rightBot)

{double range = _xMax - _xMin;double dx = range/Lsize-1.0);double x = _xMin;

Page 544: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 525

for(int i=O;i<_size;i++)

•Cif((x <= leftBot) || (x >= rightBot))_grades[i] =0.0;else if(x < leftTop)_grades[i] = linearInterpolate(leftBot,0,leftTop,l,x);else if(x <= rightTop).grades[i] = 1.0;else..grades [i] = linear Interpolate (rightTop, 1, rightBot, 0,x) ;

x += dx;>

}

int Fuzzy::supportMinGradeIndex() const

idouble min = 1.0;

int minlndex = 0;

for(int i=0;i<_size;i++)

if(min > .grades[i])

imin = _grades[i];minlndex = i;>return minlndex;

}

int Fuzzy::supportMaxGradeIndex() const

•C

double max = 0.0;int maxlndex = 0;for(int i=0;i<_size;i++)if (max < _grades [i]){max = _grades [i] ;maxlndex = i;>

return maxlndex;

>

double Fuzzy::supportMinGrade0 const•[ return supportMinGradeIndex()*_resolution+_xMin; }

double Fuzzy::supportMaxGrade0 const{ return supportMaxGrade!ndex()*_resolution+_xMin; }

Page 545: Nonlinear Workbook (3rd)

526 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

double Fuzzy. :minGrade() const{ return .grades[supportMinGradelndexO]; }

double Fuzzy::maxGrade() const{ return .grades[supportMaxGradelndexO]; >

double Fuzzy:-.cardinality0 const{double cd = 0.0;for(int i=0;i<_size;i++) cd += _grades[i];return cd;

}

double Fuzzy::relativeCardinality() const{ return cardinalityO/_domainSize; >

int Fuzzy::isSubSet(const Fuzzyfc arg) const

-Cint issubset = 1, i = 0;while((issubset) && (i<_size)){

issubset = (arg._grades[i] >= _grades[i]);i++;>return issubset;

}

Fuzzy Fuzzy::limit(const doubleft ceiling)

{Fuzzy result(*this);for(int i=0;i<_size;i++)if(.grades[i] > ceiling)_grades[i] = ceiling;

return result;>

Fuzzyfc Fuzzy::operator = (const Fuzzyfe arg)

{

if(_size != arg._size) resize(arg._size);for(int i=0;i<_size;i++)_grades[i] = arg..grades[i];

setDomain(arg._xMin,arg._xMax);

return *this;

Page 546: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 527

}

doublefc Fuzzy::operator [] (const int i){ return .grades [i]; }

double Fuzzy::operator () (const doublefe x) const

{int iLow = (x - _xMin)/.resolution;int iHigh = iLow + 1;double xLow = _xMin + _resolution*iLow;double xHigh = _xMin + _resolution*iHigh;

return linearInterpolate(xLow,.grades[iLow],xHigh,.grades[iHigh],x);

}

Fuzzy Fuzzy::operator ! () const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++)result ._grades[i] = 1.0 - .grades [i];

return result;

}

Fuzzy Fuzzy::operator ft& (const Fuzzyfe arg) const

{Fuzzy result(.size,_xMin,_xMax);for(int i=0;i<_size;i++)if (.grades [i] < arg. .grades [i])result..grades[i] = .grades[i];else result..grades[i] = arg..grades[i];

return result;}

Fuzzy Fuzzy::operator II (const Fuzzyfc arg) const{.Fuzzy result(.size,_xMin,_xMax);for(int i=0;i<_size;i++)if(.grades[i] > arg..grades[i])result..grades[i] = .grades[i];

else result..grades[i] = arg..grades[i];return result;

}

Fuzzy Fuzzy: :operator '/, (const Fuzzyfc arg) const

iFuzzy result(.size,_xMin,_xMax);

Page 547: Nonlinear Workbook (3rd)

528 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

for(int i=0;i<_size;i++)result. .grades [i] = _grades[i] + arg._grades[i]

- _grades[i]*arg._grades[i];return result;

}

Fuzzy Fuzzy::operator + (const Fuzzyfe arg) const{

Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++){double rslt = .grades[i] + arg._grades[i];if(rslt < 1.0) result..grades[i] = rslt;else result..grades[i] = 1.0;}

return result;>

Fuzzy Fuzzy::operator * (const Fuzzyfc arg) const

{Fuzzy result(.size,_xMin,_xMax);for(int i=0;i<_size;i++)result..grades [i] = .grades[i]*arg..grades[i] ;

return result;}

Fuzzy Fuzzy::operator - (const Fuzzyfc arg) const

{

Fuzzy result(.size,_xMin,_xMax);

for(int i=0;i<_size;i++)

idouble rslt = _grades[i] + arg..grades[i] - 1.0;if(rslt > 0.0) result..grades[i] = rslt;else result..grades[i] = 0.0;}

return result;

}

Fuzzy Fuzzy::operator ft (const Fuzzyft arg) const

{

Fuzzy result(_size,_xMin,_xMax);

for(int i=0;i<_size;i++)

{double gl = .grades[i], g2 = arg..grades[i];double rslt;

Page 548: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 529

if(gl < g2) rslt = _gamma*gl;else rslt = _gamma*g2;rslt += 0.5*(1.0-_gamma)*(gl+g2);result..grades[i] = rslt;}

return result;

}

Fuzzy Fuzzy::operator | (const Fuzzyfe arg) const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++){double gl = .grades[i], g2 = arg..grades[i] ;double rslt;if(gl > g2) rslt = _gamma*gl;else rslt = _gamma*g2;rslt += 0.5*(l-_gamma)*(gl+g2);result..grades[i] = rslt;

>

return result;

}

Fuzzy Fuzzy::operator > (const Fuzzyfe arg) const

{Fuzzy result(_size,_xMin,_xMax);

for(int i=0;i<_size;i++){if(.grades[i] > arg..grades[i])result [i] = 1.0;else result[i] = 0.0;}

return result;

}

Fuzzy Fuzzy::operator < (const Fuzzyfe arg) const{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++){if(.grades[i] < arg..grades[i]) result[i] = 1.0;else result[i] = 0.0;}

return result;>

Page 549: Nonlinear Workbook (3rd)

530 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

Fuzzy Fuzzy::operator == (const Fuzzyfc arg) const{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++)

{if(.grades[i] == arg..grades[i])result[i] = 1.0;else result[i] = 0.0;>

return result;}

Fuzzy Fuzzy::operator >= (const Fuzzyfe arg) const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++)

{

if (-grades [i] >= arg._grades[i])r e s u l t [ i ] = 1 . 0 ;else r e su l t [ i ] = 0 . 0 ;>

return result;

>

Fuzzy Fuzzy::operator <= (const Fuzzyfe arg) const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++){i f ( .g rades[ i ] <= arg. .grades[ i ] )r e su l t [ i ] = 1 . 0 ;else resul t [i] = 0.0;>

return result;

}

Fuzzy Fuzzy::operator != (const Fuzzyft arg) const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++)•C

i f ( -grades[ i ] != arg.-grades[i])r e su l t [ i ] = 1 . 0 ;else r e s u l t [ i ] = 0.0;

Page 550: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 531

}return result;

>

void Fuzzy::greaterThan(const doubleft arg,double lambda)

idouble range = _xMax - _xMin;if(lambda <= 0.0) lambda = 0.1*range;double dx = range/(_size-l.0);double x = _xMin;for(int i=0;i<_size;i++)

{.grades[i] = 1.0/(1.0 + exp(-(x-arg)/lambda));x += dx;}

}

void Fuzzy::lessThan(const doublefe arg,double lambda)

•C

double range = _xMax - _xMin;if(lambda <= 0.0) lambda = 0.1*range;double dx = range/(_size-l.0);double x = _xMin;for(int i=0;i<_size;i++)

i_grades[i] = 1.0/(1.0 + exp(-(arg-x)/lambda));x += dx;

}>

void Fuzzy::closeTo(const doubleft arg.double sigma)

{double range = _xMax - _xMin;if(sigma <= 0.0) sigma = 0.1*range;double expDenom = 2.0*sigma*sigma;double dx = range/(_size-l.0);double x = _xMin;for(int i=0;i<_size;i++)

•C

_grades[i] = exp(-pow(x-arg,2)/expDenom);x += dx;}

}

Fuzzy Fuzzy::mean(const Fuzzy* const * const sets,

Page 551: Nonlinear Workbook (3rd)

532 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

const int nSets,const double* const weights){

const int size = sets[O]->_size;Fuzzy result(sets[0]->_size,sets[0]->_xMin,sets[0]->_xMax);for(int ng=0;ng<size;ng++){double rs l t =0.0;for(int ns=0;ns<nSets;ns++)

if(weights) rs l t += sets[ns]->_grades[ng]*weights[ns];else rs l t += sets[ns]->_grades[ng] ;

if(!weights) rs l t /= nSets;result._grades[ng] = rs l t ;

}return result;

}

double Fuzzy::entropy(const doublefe base) const

{double result = 0.0;for(int ng=0;ng<_size;ng++){double grade = _grades[ng];if(grade) result -= grade*log(grade);grade = 1.0 - grade;if(grade) result -= grade*log(grade);}

return result/log(base);

>

double Fuzzy::centroid() const // Uses Trapezium Integration

{double numer = (_xMin*_grades[0]+_xMax*_grades[_size-l])/2.0;double denom = (.grades[0]+_grades[_size-l])/2.0;double x = _xMin;for(int i=l;i<=_size-2;i++){x += .resolution;numer += x*_grades [i] ;denom += .grades[i];>

return numer/denom; // step size cancels out

}

Fuzzy Fuzzy::hedge(const doubleft hedgeExp) const

{

Page 552: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 533

Fuzzy result(_size,_xMin,_xMax);

for(int i=0;i<_size;i++)

result..grades[ij = pow(.grades[i].hedgeExp);

return result;

}

Fuzzy Fuzzy::enhanceContrast() const

{

Fuzzy result(_size,_xMin,_xMax);

for(int i=0;i<_size;i++)

•C

double grade = .grades[i];if(grade < 0.5) result..grades[i] = 2.0*pow(grade,2);else result._grades[i] = 1.0-2*pow(l-grade,2);}

return result;

>

Fuzzy Fuzzy::alphaCut(const doublefe alpha) const

{Fuzzy result(_size,_xMin,_xMax);for(int i=0;i<_size;i++)if (_grades[i] >= alpha) result. .grades [i] = 1.0;else result..grades[i] = 0.0;

return result;}

Fuzzy Fuzzy::betaCut(const int beta) const{// Make a copy of the set for bubble sortdouble* grades = new double[.size];int* indexes = new int[_size];for(int i=0;i<_size;i++){grades [i] = .grades [i]; indexes [i] = i;}int exchanged, nFound = 0;

// Bubble sort until the beta largest elements at end of arraydo

•C

exchanged = 0;for(int n=0;n<_size-nFound-l;n++)iif(grades[n] > grades[n+1])

Page 553: Nonlinear Workbook (3rd)

534 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

{double ddum = grades[n+1];grades[n+1] = grades[n];grades[n] = ddum;int idum = indexes[n+1];indexes[n+1] = indexes[n];indexes[n] = idum;exchanged++;}}nFound++;} while((exchanged) &fc (nFound<=beta));

Fuzzy result(_size,_xMin,_xMax);result.fillGrades(0);

for(int nb=l;nb<=beta;nb++)result._grades[indexes[_size-nb]] = 1;delete [] indexes; delete [] grades;return result;

}

ostreamft operator « (ostreamfc os,const Fuzzyfe arg){os « "[";

double x = arg._xMin;

for(int i=0;i<arg._size;i++)

{os « "(" « x « "," « arg..grades[i] « " ) " ;if(i!=arg._size-l)x += arg. ..resolution;}

os « " ] " ;

return os;

}

istreamft operator » (istreamft is.Fuzzyfc arg)

•C

int size;is » size;arg.resize(size);double xMin, xMax;is » xMin » xMax;arg.setDomain(xMin.xMax);for(int i=0;i<size;i++) is » arg._grades[i];

Page 554: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 535

return is ;}

double Fuzzy::linearInterpolate(const doublefe xO,const doublefe yO,const doubled xl,const doubleft yl,const doublefc x) const

{double m = (yl - yO)/(xl - xO);double c = yl - m*xl;return m*x+c;

}

void Fuzzy::resize(const int newSize)

{delete [] .grades;_grades = new double[newSize];

_size = newSize;

>

We supply 3 constructors. The default constructor which allows the user to createan array of fuzzy sets, a constructor allowing the user to create a fuzzy set of acertain size and covering a range from xMax to xMin on the support axis. If theuser does not specify the support-range then it is assumed to be [0,1] as specifiedby the default values of the corresponding arguments. The support range can bealtered at a later stage via the method setDomain. The last constructor is the copyconstructor. It is important not to forget the copy constructor since the latter isused every time we pass or return a fuzzy set by value.

The method f illGrades sets all grades equal to the supplied fill-value. The methodnormalize normalizes the fuzzy set (i.e. the largest grade will be 1). The methodrectangle fills all the grades of the fuzzy set whose support is between lef t andright with 1 and all others with zero. For example, the concept crisp faster than100 for a universe of cruising speeds between say 100 and 200 could be defined bythe rectangle

Fuzzy fasterThanlOO(201,1,200);fasterThanlOO.rectangle(100,200);

The methods t r iangle () and trapezoidO can be used to define a discrete fuzzy setwith triangular and trapezoidal membership functions, respectively. For example,we could define the fuzzy value for optimal cruising speed by a triangle and therange of acceptable cruising speed by a trapezoid

Fuzzy optimalSpeed(201,0,200), acceptableSpeed(201,0,200);optimalSpeed.triangle(100,110,120);acceptableSpeed.trapezoid(80,100,120,130);

Page 555: Nonlinear Workbook (3rd)

536 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

The methods small and large, near, lessThan and greaterThan are a direct im-plementation of the corresponding fuzzy concepts.

The methods minGrade and maxGrade return the minimum and maximum gradeof the fuzzy set, the methods minGradeSupport and maxGradeSupport the corre-sponding support value and the methods

minGradeSupportlndex maxGradeSupportIndex

the array index for these support values. The method centroid is usually used fordefuzzification. It returns the centroid of the fuzzy set as a crisp value. The cardi-nality and relative cardinality are returned by the corresponding member functions.

The if . . . then . . . rules require us to limit the degree to which a consequenceis true to the degree that the antecedent is true. For this purpose we supply themember function, limit, which causes the set to saturate at the ceiling level. Forexample

Fuzzy optimalSpeed(201,0,200);optimalSpeed.triangle(100,110,120);optimalSpeed.limit(0.5);

The final membership function of the set optimalSpeed is a trapezoidal with cornerpoints (100,0), (105,0.5), (115,0.5), (120,0). The methods alphaCut and betaCutimplement the a-cut and /?-cut operators. The method entropy estimates the en-tropic fuzzyness measure. By default the base of the logarithm is taken as 2.

The methods xMinO, xMaxO and domainSizeO are simple query functions, thelatter returning the size of the support domain. Since this is an implementation of adiscrete fuzzy set we have a finite resolution on the support domain. The resolutioncan be queried via the method resolution. The method isSubSet returns true,i.e. 1, if the set for which the method is called is a subset of the set supplied asargument to the method.

We supply two operators for querying the grade for a given support value. Thefirst is the array element access operator [] which takes an integer as argument. Itsimply returns the grade for the support value at the specified array index, henceat the discretized points. This operator can also be used to change the grade atthat point. The second operator is the function call operator (). This operator canbe used to query the grade at any support value between xMin and xMax, not onlyat the discretized grid points. The method uses linear interpolation to obtain thegrade values between grid points. The assignment operator allows us to assign onefuzzy set to another. It returns the object itself by reference to allow concatenatedstandard C-style assignments

setl = set2 = set3 = set4;

Page 556: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 537

Most of the remaining operators are a direct implementation of the fuzzy-set oper-ators. The relational operators

< > = = > = < = !=

each return a crisp set (the grades are all either zero or one). For example, thegreater-than operator, >, returns a fuzzy set with grade one for all support valuesfor which the current set, the set for which the method was called, has greater sup-port than the set supplied as argument to the method. The grades for all othersupport values are zero. The remaining relational operators act in a similar fashion.

For the fuzzy-AND and fuzzy-OR operators we have to specify a value for 7. Thevalue of 7 can be queried and set via the methods gamma and setGamma.

The input/output stream operators, » and « are used to read a fuzzy set from aninput stream (e.g. a file or the keyboard) or to an output stream (e.g. a file or thescreen).

The static member function meanO calculates the weighted mean of an array offuzzy sets. If the array of weights is not supplied, the standard arithmetic mean isreturned. The fact that the member function is static implies that it can be calledwithout referring to an object of the class. It is effectively a global member functionwith the scope of the class. We can call it as follows

Fuzzy::mean(setsArray,weights);

Note that both arguments are as constant as can be, i.e. all pointers and what theypoint to are declared constant.

Finally we implement linguistic hedging and contrast enhancement. We allow hedg-ing with any continuous hedge-value. In order to introduce linguistic hedges wedefine static class constants for the terms

extremely, very, substantially, somewhat, slightly, vaguely .

The use could define the linguistic concept, large and then use hedging to introducethe concept verylarge:

Fuzzy large(50);large.large();Fuzzy veryLarge = large.hedge(Fuzzy::very);

Example

The following C++ program mfuzzy. cpp illustrates the usage of the discrete fuzzyset class Fuzzy in the file fuzzy. h.

Page 557: Nonlinear Workbook (3rd)

538 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

II mfuzzy.cpp

#include <iostream>#include <cstdlib>#include "fuzzy.h"using namespace std;

int main(void)

iifstream finCfuzzy.dat");if(fin == NULL){cout « "file cannot be opened";exit(O);}Fuzzy setl, set2;fin » setl » set2;cout « " si = " « setl « endl;cout « "!sl = " « !setl « endl;cout « " s2 = " « set2 « endl;cout « "si && s2 = " « (setl && set2) « endl;cout « "si II s2 = " « (setl II set2) « endl;cout « "si + s2 = " « (setl + set2) « endl;cout « "si - s2 = " « (setl - set2) « endl;cout « "si & s2 = " « (setl & set2) « endl;cout « "si I s2 = " « (setl I set2) « endl;

Fuzzy set3;fin » set3; set3.normalize();cout « " s3 = " « set3 « endl;cout « "sS.centroid/cardinality/relativeCardinality/entropy = "

« set3.centroid() « " " « set3.cardinality() « " "« set3.relativeCardinality() « " " « set3.entropy()« endl;

int nSets2 = 2;Fuzzy** sets2 = new Fuzzy*[nSets2];double* weights = new double[2];weights[0] = 0 . 2 ;weights[1] = 0 . 8 ;sets2[0] = &setl;se ts2[ l ] = fcset2;cout « "mean(sl..s2,weights[0.2,0.8]) = "

« Fuzzy::mean(sets2,nSets2,weights)« endl;

Page 558: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 539

int nSets3 = 3;Fuzzy** sets3 = new Fuzzy*[nSets3];sets3[0] = ftsetl;sets3[l] = &set2;sets3[2] = &set3;cout « "mean(sl..s3) = "

« Fuzzy::mean(sets3,nSets3) « endl;

cout « "s3.hedge(Fuzzy::very) = "« set3.hedge(Fuzzy::very) « endl;

cout « "s3.enhanceContrast() = "« set3.enhanceContrast() « endl;

cout « Ms3.alphaCut(0.65) = " « set3.alphaCut(0.65) « endl;cout « "s3.betaCut(3) = " « set3.betaCut(3) « endl;cout « "s3.entropy() <=> s3.enhanceContrast().entropy() = "

« set3.entropy 0 « " <=> "« set3.enhanceContrast().entropy0 « endl;

Fuzzy small(5,0,2); small.small();Fuzzy large(5,0,2); large.large0;cout « "small: " « small « endl;cout « "large: " « large « endl;Fuzzy legalSpeed(7,80,140); legalSpeed.lessThan(120);cout « "legalSpeed = " « legalSpeed « endl;Fuzzy optimumSpeed(7,8O,14O); optimumSpeed.closeTo(HO);cout « "optimumSpeed = " « optimumSpeed « endl;f in.closeO ;return 0;

>

We used the following input file, fuzzy. dat

5 1 50.4 0.8 1.0 0.7 0.75 1 50.0 0.1 1.0 0.2 0.15 1 50.1 0.2 0.5 0.2 0.4

The output of the program is given below

s i = [ ( l , 0 . 4 ) ( 2 ) 0 . 8 ) ( 3 ) l ) ( 4 , 0 . 7 ) ( 5 , 0 . 7 ) ]! s l = [ ( l , 0 . 6 ) ( 2 , 0 . 2 ) ( 3 , 0 ) ( 4 , 0 . 3 ) ( 5 , 0 . 3 ) ]s2 = [ ( l , 0 ) ( 2 , 0 . 1 ) ( 3 , l ) ( 4 , 0 . 2 ) ( 5 , 0 . 1 ) ]

s i && s2 = [ ( l , 0 ) ( 2 , 0 . 1 ) ( 3 , l ) ( 4 , 0 . 2 ) ( 5 , 0 . 1 ) ]

Page 559: Nonlinear Workbook (3rd)

540 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

si || s2 = [(l,0.4)(2,0.8)(3,l)(4,0.7)(5,0.7)]

si + s2 = [(l,0.4)(2,0.9)(3,l)(4,0.9)(5,0.8)]

si - s2 = [(l,0)(2,0)(3,l)(4,0)(5,0)]

si & s2 = [(1,0.1)(2,0.275)(3,1)(4,0.325X5,0.25)]si I s2 = [(1,0.3X2,0.625)(3,l)(4,0.575)(5,0.55)]

s3 = [(l,0.2)(2,0.4)(3,l)(4,0.4)(5,0.8)]s3.centroid/cardinality/relativeCardinality/entropy= 3.26087 2.8 0.7 3.38576

mean(sl..s2,weights[0.2,0.8]) = [(1,0.08)(2,0.24)(3,1)(4,0.3)(5,0.22)]mean(sl..s3) = [(1,0.2)(2,0.433333)(3,1)(4,0.433333)(5,0.533333)]S3.hedge(Fuzzy::very) = [(1,0.04)(2,0.16)(3,1)(4,0.16)(5,0.64)]s3.enhanceContrast() = [(1,0.08)(2,0.32)(3,1)(4,0.32)(5,0.92)]s3.alphaCut(0.65) = [(1,0)(2,0)(3,1)(4,0)(5,1)]s3.betaCut(3) = [(1,0)(2,0)(3,1)(4,1)(5,1)]s3.entropy() <=> s3.enhanceContrast().entropyO = 3.38576 <=> 2.61312

small: [(0,1)(0.5,0.75)(1,0.5)(1.5,0.25)(2,0)]large: [(0,0)(0.5,0.25)(1,0.5)(1.5,0.75)(2,1)]legalSpeed= [(80,0.998729)(90,0.993307)(100,0.965555)(110,0.841131)(120,0.5)(130,0.158869) (140,0.0344452)]optimumSpeed = [(80,3.72665e-06)(90,0.00386592)(100,0.249352)(110,1)(120,0.249352)(130,0.00386592)(140,3.72665e-06)]

17.2.8 Applications: Simple Decision-Making ProblemsWhen buying a pair of shoes there are certain crisp boundary conditions determinedby the anatomy of one's feet and by the balance of one's bank account. We alsohave a fuzzy concept of what we find good-looking and what we find comfortable.The problem is thus one of finding a solution which, subject to certain crisp bound-ary conditions, optimizes some fuzzy objectives. Let us take the simple exampleof having to choose between TV pairs of shoes, some of which are too small andsome of which are too expensive. For this we use two crisp sets, tooSmall andtooExpensive. The grades of these crisp sets are either 0 or 1. We implement ourfuzzy concepts of looks and comfort by the corresponding two fuzzy sets whosemembership value is now a grade between zero and 1, representing our fuzzy eval-uation of the looks and comfort of each pair of shoes. Consider now the code givenbelow.

/ / shoe.cpp

#include <iostream>#include <fstream>#include "fuzzy.h"

Page 560: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 541

using namespace std;

int main(void)

{ifstream infileCshoe.dat");Fuzzy tooSmall, tooExpensive, looks, comfort;infile » tooSmall » tooExpensive » looks » comfort;ofstream fout("shoe.out");fout << "Select a comfortable good-looking shoe which"

« " is big enough and affordable:" « endl;

« "================» « endl;

fout << "Too-small constraint: " « tooSmall« " (crisp set) " « endl;

fout « "Too expensive constraint: " « tooExpensive« " (crisp set) " « endl « endl;

fout « "Looks preferences: " « looks « " (fuzzy set) "« endl;

fout « "Comfort preferences: " « comfort « " (fuzzy set) "« endl « endl;

Fuzzy satisfyConstraints = !(tooSmall I I tooExpensive);fout « "Constraints satisfied by : " « satisfyConstraints

« endl;int nPreferences = 2;Fuzzy** preferences = new Fuzzy*[nPreferences] ;preferences[0] = felooks;preferences[1] = fccomfort;Fuzzy objective = Fuzzy::mean(preferences,nPreferences);fout « "Objective = " « objective « endl « endl;Fuzzy shoeScores = (satisfyConstraints && objective);fout << "Shoe scores = " « shoeScores « endl;fout « "Best shoe = " « shoeScores.supportMaxGradeO

« " which has a grade of " « shoeScores.maxGradeO« "." « endl « endl;

fout « "Select a shoe which is somewhat good-looking "« "and very comfortable"« endl « "(still not too small and not too expensive):"« endl;

« endl;Fuzzy somewhatGoodLooking = looks.hedge(Fuzzy::somewhat);Fuzzy veryComfortable = comfort.hedge(Fuzzy::very);fout « "somewhatGoodLooking = " « somewhatGoodLooking « endl;fout « "veryComfortable = " « veryComfortable « endl « endl;preferences[0] = fesomewhatGoodLooking;

Page 561: Nonlinear Workbook (3rd)

542 CHAPTER 17. FVZZY SETS AND FUZZY LOGIC

preferences[1] = fcveryComfortable;objective = Fuzzy::mean(preferences,nPreferences);fout « "Objective = " « objective « endl « endl;shoeScores = (satisfyConstraints && objective);fout « "Shoe scores = " « shoeScores « endl;fout « "Best shoe = " « shoeScores.supportMaxGradeO

« " which has a grade of " « shoeScores.maxGradeO « "."« endl;

infile.close();fout.close();

>

The subset of shoes which satisfies our crisp constraints is determined via

Fuzzy satisfyConstraints = !(tooSmall I I tooExpensive);

We have to somehow take a weighted average of our fuzzy preferences. This is donevia

Fuzzy objective = FuzzySet::mean(preferences,nPreferences);

which weigths the two preferences equally. We determine now the confidence withwhich we select the various shoes by taking a logical AND (in the fuzzy sense)between the constraints and the preferences, i.e. our ideal shoe must satisfy theconstraints AND the preferences:

FuzzySet shoeScores = (satisfyConstraints && objective);

This is the fuzzy output of our fuzzy inference engine. At the end of the day we haveto make a crisp choice between the shoes. For this purpose we have to defuzzify ouroutput. In this simple example (which is naturally discrete) we select that particularshoe which obtains the highest confidence level, i.e. has the maximum grade. Thisis done by the supportMaxGradeO defuzzifier, which returns the support for thatmember of the fuzzy set which received the highest confidence level (grade):

in t choiceOfShoe = shoeScores.supportMaxGrade();

Below we give an example of an input file shoe. dat

5 0 40 1 0 0 05 0 40 0 0 0 15 0 40.7 1.0 0.4 0.7 0.55 0 40.4 0.0 0.8 0.6 0.9

Page 562: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 543

In this example we have 5 pairs of shoes to choose from. Hence each of the fuzzysets has length 5 and has a domain ranging from 0 to 4. The first set is actually acrisp set specifying that the second shoe is the only shoe which is too small. Thesecond set specifies the crisp too-expensive boundary condition (only the last shoeis too expensive). The following two sets represent our fuzzy evaluation of the looksand the comfort for the various pairs of shoes.

The output shoe. out of the first part of the program is given below:

Select a comfortable, good-looking shoe which is

big enough and affordable:==========================Too-small constraint: [(0,0)(1,1)(2,0)(3,0)(4,0)] (crisp set)Too expensive constraint: [(0,0)(1,0)(2,0)(3,0)(4,1)] (crisp set)

Looks preferences: [(0,0.7)(1,1)(2,0.4)(3,0.7)(4,0.5)] (fuzzy set)Comfort preferences: [(0,0.4)(1,0)(2,0.8)(3,0.6)(4,0.9)] (fuzzy set)

Constraints satisfied by: [(0,1)(1,0)(2,1)(3,1)(4,0)]Objective = [(0,0.55)(1,0.5)(2,0.6)(3,0.65)(4,0.7)]

Shoe scores = [(0,0.55)(1,0)(2,0.6)(3,0.65)(4,0)]Best shoe = 3 which has a grade of 0.65.

Now select a shoe which is somewhat good-looking and very comfortable(still not too small and not too expensive):

somewhatGoodLooking =[(0,0.83666)(1,1)(2,0.632456)(3,0.83666)(4,0.707107)]veryComfortable =[(0,0.16)(1,0)(2,0.64)(3,0.36)(4,0.81)]

Objective = [(0,0.49833)(1,0.5)(2,0.636228)(3,0.59833)(4,0.758553)]

Shoe scores = [(0,0.49833)(1,0)(2,0.636228)(3,0.59833)(4,0)]Best shoe = 2 which has a grade of 0.636228.

We select a comfortable, good-looking shoe which is neither too small, nor too ex-pensive. The "best" shoe in this case is shoe no 3 (the fourth shoe). If we usehedging to select a shoe which is somewhat comfortable, but very good looking,then the "best" shoe is shoe no 2 (the third shoe).

Next we look at a simplified version of an optimal pricing problem. We have anumber of possibly overlapping or even conflicting objectives. For example, theshareholders might push for a high price for the product, while the salespersons

Page 563: Nonlinear Workbook (3rd)

544 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

might want a low price so that they can sell a larger quantity. Finally there mightbe a third objective of having a profit margin of around 30% which ensures thatthe company does not come into bad light with the consumer watchdogs and hencehas to face bad publicity. We might also have an absolute minimum below whichthe producer absolutely refuses to produce and an absolute maximum above whichthe salespersons absolutely refuse to try and market the product. Furthermore, ourobjectives are to make a profit (i.e. that the retail price must be above the cost ofthe product) and to underbid the opposition.

The following code asks the user for the minimum and maximum price, the manu-facturing cost and the competitor's price. It also asks the user to rate the variousobjectives according to their relative importance. It then uses fuzzy logic to deter-mine the optimum retail price for the product.

/ / price.cpp

#include <iostream>#include <fstream>#include "fuzzy.h"using namespace std;

int main(void)

{int minPrice, maxPrice, manufacturingCost, competitorsPrice;cout « "Enter minimum price (to nearest Rand): ";cin » minPrice;cout « "Enter maximum price (to nearest Rand): ";cin » maxPrice;cout « "Enter manufacturing cost (to nearest Rand): ";cin » manufacturingCost;cout « "Enter competitors price (to nearest Rand): ";cin >> competitorsPrice;int nPointsOnGrid = maxPrice - minPrice + 1;const int nObjectives = 5;Fuzzy** objectives = new Fuzzy*[nObjectives];// Shareholders objective of a very high price:objectives[0] = new Fuzzy(nPointsOnGrid,minPrice,maxPrice);objectives [0]->largeO ;// Salesperson's objective of a low price:objectives[l] = new Fuzzy(nPointsOnGrid,minPrice,maxPrice);objectives[1] -> small();// Moralists objective of a 30'/, profit:objectives[2] = new Fuzzy(nPointsOnGrid,minPrice,maxPrice);

objectives[2] -> closeTo(manufacturingCost*1.3);

// Objective of making a profit:

Page 564: Nonlinear Workbook (3rd)

17.2. OPERATORS FOR FUZZY SETS 545

objectives[3] = new Fuzzy(nPointsOnGrid,minPrice,maxPrice);

objectives [3] -> greaterThandnanuf acturingCost);

// Objective for competitiveness:objectives[4] = new FuzzyCnPointsOnGrid.minPrice.maxPrice);objectives[4] -> lessThan(competitorsPrice);int importance[nObjectives];cout « "Enter the importance of each of the following criteria:"

« endl;cout « "(0->vaguely,l->slightly,2->somewhat,3->substantially,"

« endl« " 4->very,5->extremely)" « endl;

« endl;cout « "Shareholder's desire for high price: ";cin » importance[0];cout « "Salesperson's desire for a low price: ";cin » importance[1] ;cout « "Moralist's objective of a 30'/, profit: ";cin » importance[2];cout « "Retail price > manufacturing price: ";cin » importance[3] ;cout « "Retail price < competitors price: ";cin » importance[4];

for(int n0b=0;n0b<n0bjectives;n0b++)

{switch(importance[nOb])icase 0: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::vaguely);

break;case 1: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::slightly);

break;case 2: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::somewhat);

break;case 3: *objectives[nOb]=objectives[nOb]->

hedge(Fuzzy:substantially); break;case 4: *objectives[nOb]=objectives[nOb]->hedge(Fuzzy::very);

break;case 5: *objectives[nOb]objectives[nOb]->hedge(Fuzzy::extremely);

break;}>Fuzzy fuzzyPrice = (*objectives[0]) kk (*objectives[l])

kk (*objectives[2]) kk (*objectives[3])kk (*objectives[4]) ;

Page 565: Nonlinear Workbook (3rd)

546 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

cout « endl« "Recommended retail price: R"« fuzzyPrice.supportMaxGradeO « endl;

return 0;

}

The user dialogue could be as follows:

Enter minimum and maximum price (to nearest Rand): 2000 5000Enter manufacturing cost (to nearest Rand): 2500Enter competitors price (to nearest Rand): 4500Enter the importance of each of the following criteria:(0 -> vaguely, 1 -> slightly,

2 -> somewhat, 3 -> substantially,4 -> very, 5 -> extremely)

Shareholder's desire for high price: 4Salesperson's desire for a low price: 3Moralist's objective of a 30'/. profit: 1Retail price > manufacturing price: 5Retail price < competitors price: 4

Recommended retail price: R3649

Note that each of the fuzzy sets has 3000 elements. Assume now that the need toachieve turn-over becomes high and thus the salesperson's desire for a low price isgiven more importance. The shareholders still refuse to budge from rating their im-portance of a high price as very important, but the turn-over becomes now extremelyimportant.

Enter minimum price (to nearest Rand): 2000Enter maximum price (to nearest Rand): 5000Enter manufacturing cost (to nearest Rand): 2500Enter competitors price (to nearest Rand): 4500Enter the relative importance of each of the following criteria:(0 -> vaguely, 1 -> slightly, 2 -> somewhat, 3 -> substantially,4 -> very, 5 -> extremely)

Shareholder's desire for high price: 4Salesperson's desire for a low price: 5Moralist's objective of a 307. profit: 1Retail price > manufacturing price: 5Retail price < competitors price: 5

Recommended retail price: R3146

Our fuzzy inference engine suggests now a significantly lower price.

Page 566: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 547

17.3 Fuzzy Numbers and Fuzzy Arithmetic

17.3.1 Introduction

So far we have discussed only fuzzy sets and the algebraic operations that can beperformed on fuzzy sets. In real-life, however, one usually works with numbers.Often these numbers are fuzzy, for example influenced by inaccurate measurements,noisy data, In this section we define fuzzy numbers and the algebraic operationsbetween fuzzy numbers. Simple examples of fuzzy numbers are large, approximately30, much larger than 10, a few. It would of course be convenient if we did not haveto redevelop the entire algebra for fuzzy numbers. Fortunately there is the extensionprinciple which is a general method for extending algebraic operations on standardcrisp numbers to fuzzy numbers.

Definition. A fuzzy interval, I, is a convex, normalized fuzzy set of the real num-bers R whose membership function fij(x) is piecewise continuous on R.

Definition. A fuzzy number, N, is a fuzzy interval with membership function,Hfi(x), such that there exists exactly one XQ 6 R for which Hft(xo) = 1.

Definition. A positive (negative) fuzzy number, N, has a membership functionsuch that

H${x) = 0 for all x < 0 (for all x > 0).

Example. Consider

N = { (3,0.2), (4,0.6), (5,1.0), (6,0.6), (7,0.2) } .

This fuzzy number could be called "approximately 5".

Example. The fuzzy set

M = { (1,0.2), (2,0.8), (3,1.0), (4,1.0), (5,0.8), (6,0.2) }

is not a fuzzy number since fi(3) = 1.0 and /z(4) = 1.0.

Example. The fuzzy set

P = { (3,0.3), (4,0.7), (5,1.0), (6,0.2), (7,0.5), (8,0.1) }

is not a fuzzy number due to the terms (6,0.2), (7,0.5), where 0.2 < 0.5.

The definition of a fuzzy number is often modified. In many cases trapezoidalmembership functions are used.

Page 567: Nonlinear Workbook (3rd)

548 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.3.2 Algebraic Operations

First we define the extension principle (Dubois [24], Zimmermann [100]).

Definition. Given N fuzzy sets F\,...,FN in Xi,..., XN. Given a mapping, g,from the product space X = Xi x . . . x XN onto a universe Y,

y = g(xux2,...,xN).

Then the extension principle allows us to define a fuzzy set G in Y by

G := {{y, ng{y)) | y = g(x), x = (xu..., xN) e X }

with., / , A _ / suPxgj - i^min^^O, . . . , ^ (IJV)} ifff"1(2/)^0^W" |0 otherwise

For N = I, the extension principle reduces to

whereu , v ) _ / sup .e j - i^ / i fW if r H v ) ^ ^M » l 2 / j ~ \ 0 otherwise

Example. Consider the fuzzy set

F = {(-1,0.5), (0,0.8), (1,1.0), (2,0.4)}

and g{x) = x2. Then we find by applying the extension principle

B = / (A) = {(0,0.8), (1,1.0), (4,0.4)}.

The term (1,1.0) we find from - 1 • - 1 = 1 and sup{ 0.5,1.0 } = 1.0.

Example. Consider the fuzzy sets

A" ={(1,0.2), (2,1.0), (3,0.5), (4,0.3)}

andB = {(3,0.9),(4,1.0),(5,0.4)}.

Lety:=g{xux2) =xx+x2.

Then the fuzzy addition of the two fuzzy sets, A and B, yields a new fuzzy set,C = A+B with membership function

/x5(y = 4) = ^ ( l + 3)

= 0.2

Hd{y = 5) =sup{/i5(l + 4), nd(2 + 3)}

Page 568: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 549

= sup{0.2,0.9}

= 0.9

Vc(y = 6) =sup{/i5(l + 5), A»g(2 + 4), fi5(3 + 3)}

= sup{0.2,1.0,0.5}

= 1.0

A»c(» = 7) = suP{^c(2 + 5)> M 3 + 4)' M 4 + 3)>= sup{0.4,0.5,0.3}

= 0.5Hd(y = 8) = sup{M5(3 + 5), ( 4 + 4)}

= sup{0.4,0.3}= 0.4

M - ( 2 / = 9)=M~(4 + 5)

= 0.3

We thus finally obtain the fuzzy set

C = A+B = {(4,0.2), (5,0.9), (6,1.0), (7,0.5), (8,0.4), (9,0.3)} .

Inspecting the membership function of C we note that the addition of fuzzy 2 andfuzzy 4 does indeed result in fuzzy 6. The fuzzyness of the sum is equal to the sumof the fuzzyness of the terms in the sum. Calculating the fuzzyness (entropy) S2 ofA, B we obtain S2(A) = 2.60322 and S2(B) = 1.43995 bits respectively. The sumof these two is exactly equal to fuzzyness of the sum, S2(C) = 4.04317 bits.

The extended operations on the basis of the extension principle cannot be applieddirectly to the fuzzy numbers with discrete support. We show this for the multipli-cation of fuzzy sets (Zimmermann [100]).

Example. Let

X = { (1,0.3), (2,1.0), (3,0.4) }, Y = { (2,0.7), (3, 1.0), (4,0.2) } .

Then we find

Ai£?(u = 2) = /iBF(l-2)

= 0.3H^{u = 3) = fj,5{l • 3)

= 0.3H5{u = 4) =sup{/^(l • 4),/x5(2 • 2)}

= sup{0.2,0.7}= 0.7

fid(u = 6) =sup{/^(2 • 3), fid(3 • 2)}

Page 569: Nonlinear Workbook (3rd)

550 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

= sup{l.0,0.4}

= 1.0

liU(u = 8)=sap{n5(2-4)}

= sup{0.2}= 0.2

At5(u = 9)=A»g(3-3)= 0.4

/xgF(u = 12)=sup{/ij?(3-4)}= sup{0.2}= 0.2.

Hence

U = X~Y = { (2,0.3), (3,0.3), (4,0.7), (6,1.0), (8,0.2), (9,0.4), (12,0.2) } .

The result is obviously not a fuzzy number due to the term (9,0.4).

Given a set of one-dimensional arrays of real numbers. All these one-dimensionalarrays have the same length. The following C++ program maxmin. cpp finds firstthe minimum values for each of the one-dimensional arrays and then the maximumof these values. We store the set of the one-dimensional arrays as a two-dimensionalarray x, where the first index runs through the number of arrays and the secondindex runs through the length of each array.

/ / maxminl.cpp

#include <iostream>using namespace std;

int main(void){// m number of vectors, n length of each vectorint m, n;m = 4; n = 3;int i, j; // indices for for-loops

double** x = NULL;x = new double* [m] ;for(i=0;i<m;i++)x[i] = new double [n];x[0][0] = 3 .1 ; x[0][ l ] = 4 .5; x[0] [2] = 1.7;x[l][0] = 1.1; x [ l ] [ l ] = 6.7; x[ l] [2] = 5 .1 ;x[2][0] = 0.9; x[2][ l ] = 0.5; x[2] [2] = 4.9;x[3][0] = 1.1; x[3][ l ] = 0.7; x[3][2] = 6 .1 ;

Page 570: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 551

// store minimum value of each vectordouble* y = new double[m];

double element;

for(i=0;i<m;i++)

{element = x [i] [0];for(j=0;j<n;j++){if (x[i] [j] < element)element = x[i] [j] ;y[i] = element;}>double maxmin = y[0] ;

for(i=l;i<m;i++)

•Cif(y[i] > maxmin)maxmin = y [ i ] ;}

for(i=0;i<m;i++)-C cout « "y[" « i « "] = " « y[i] « endl; >cout « endl;cout « "maxmin = " « maxmin « endl;

for(i=0;i<m;i++)delete x[i] ;delete [] x;

delete [] y;return 0;

>

The output is

y[0] = 1.7y[l] = 1.1y[2] = 0.5y[3] = 0.7

maxmin = 1.7

Page 571: Nonlinear Workbook (3rd)

552 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.3.3 LR-RepresentationsWe can define a fuzzy set by its membership function only. However, if any fuzzyset can be represented by some arbitrary membership function, then applying alge-braic operations (e.g. addition) repetitively to such fuzzy sets results in fuzzy setswith complex membership functions. For this reason the form of the membershipfunction is often restricted to a relatively small class of membership functions. Forexample, fuzzy numbers are often represented by triangular fuzzy sets. Triangularfuzzy sets are a special case of the more general LR-fuzzy numbers introduced forreasons of efficiency and generality (Dubois and Prade [24], Zimmermann [100]).

Definition. A fuzzy number, N, is of LR-type (L for left and R for right) if themembership function can be written in the form

/ L (rir) f o r all a; < n

W ) : = jfl^nj f0rallx>n

Here L and R are the left and right spread functions, nis the mean value of N anda and /J are the left and right spreads of the Lii-membership function.

Usually the left and right spread functions, L(z) and R(z), are chosen to be decreas-ing functions which map the positive real numbers onto the interval [0,1] and forwhich 1,(0) = R(0) = 1.

The Li?-representation of fuzzy numbers can be expanded in such a way that it canbe used for fuzzy intervals.

Definition. A fuzzy interval, / , is of LR-type if the membership function can bewritten in the form

( L ( ^ ) for all x < m1 for all m < x < n

R {^f) for all x > n

As before, L and R are the left and right spread functions, and a and /3 are the leftand right spreads. The values at which the left and right fall-off starts are m and n.

Example. Often fuzzy numbers are represented by triangular fuzzy sets. For atriangular fuzzy set one chooses a straight line with negative (positive) slope for theleft (right) spread functions. For example

Tf \ J?( \ ft \ - \ 1 ~ z forallze [0,1]Liz) — R(z) = fiz) = < _ ,, .v ' y ' J v ' [ 0 otherwise

In order to represent the fuzzy number, 4, we would choose the mean to be JJL = 4.The left and right spreads would be determined by the application. For example,

Page 572: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 553

we could have a = 2 and /? = 0.5

{ 0 for alia; < 2

1-*=* for alls e [2,4]1-fi for all* e [4,4.5]

0 ' for alia; > 4.5Naturally the left and right spread functions need not have the same functionalform. For example we could have an exponential fall-off to the left and a Gaussianfall-off to the right as is illustrated in the following example.Example. Let

L(z) = e-\ R(z) = e~z2

and assume \x = 5, a = 1, ft = 3.

The ^-representation for fuzzy intervals contains trapezoidal fuzzy intervals as aspecial case. This is illustrated in the following example.

Example. An acceptable cruising speed might be represented by a trapezoidalfuzzy set with grade 1 for speeds between lOOkm/h and 120km/h with a very rapidfall-off at speeds exceeding 120km/h (due to possible safety implications) and a moremoderate fall-off as the cruising speed falls below lOOkm/h. Again we choose thesame straight line for the left and right spread functions

v ' v ' v ' [ 0 otherwise

Thus we choose fj, = 100, v = 120, a = 30 and /3 = 10.

Commonly-Used Spreading Functions

When choosing suitable spreading functions, f{z), for L(z) and R(z) one generallylooks for decreasing functions which map the positive real numbers onto the interval[0,1] and for which /(0) = 1. The following functions are the most commonly usedspreading functions:

• Straight Lines:

ft ) - I 1 ~ z for allz G t 0 ' 1 ]| 0 otherwise

• Exponentials: f(z) = e~z

• Gaussians: f(z) = e~z<l

• Range Limited Polynomials of the form

f(z) = max{0, (1 - zf}

Page 573: Nonlinear Workbook (3rd)

554 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

• Range Limited Polynomials of the form

f(z) = max{0, (1 - z")}

The following example considers once again the fuzzy set of acceptable cruisingspeeds, but it uses range-limited polynomials instead of straight lines for the spread-ing function.

Example. The fuzzy interval of acceptable cruising speed with left spreading func-tion

L{z) = max{ 0, (1 - z25) }

and right spreading function

R{z) = max{ 0, (1 - z)5'5 } .

When using only a single spreading function within a fuzzy system, it is customaryto use the notation

N={n,a,P)LR

for fuzzy numbers andI = (fi, v, a, P)

for fuzzy intervals.

17.3.4 Algebraic Operations on Fuzzy Numbers

The extension principle can be used to define the algebraic operations such as addi-tion, subtraction, multiplication and division for fuzzy numbers. The definitions ofthe addition and subtraction operators follows exactly from the extension principle.

Definition. If N\ = {^I,OII,PI)LR a n d N2 = (^2,02, P2)LR are two fuzzy numbers,then the algebraic sum of these two numbers is defined by

(Ml! a l i A)Lfi+(M2, "2 , P?)LR •= (Ml + M2i Q l + "2 , Pi + &) iH •

Definition. If N = (fi,a,P)iR is a fuzzy number, then changing its sign (unaryminus) is defined via

-(fi, a, P)LR ••= (-Mi P, &)LR •

From the above two definitions we can define the algebraic difference between twofuzzy numbers/intervals.

Definition. If Ni — {^i,ai,Pi)LR and N2 = (M2,a2, P-I)LR are two fuzzy numbers,then the algebraic difference of these two numbers is defined by

{lJ,l,a1,Pi)LRZ(ll2,a2,P2)LR~ (Ml -V2,<Xl+P2,Pl +CX2)LR-

Page 574: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 555

Fuzzy algebra can be extended to include multiplication of fuzzy numbers. Noclosed form definition can be obtained from the extension principle for multiplica-tion and division (the result cannot be represented with the same LiMunctions asthe operands). The expressions given below only approximate the exact result withan accuracy which improves with decreasing spreads.

Definition. If Nx = {^\,CX.\,PI)LR and N2 = (//2)Q2, A)LR are two fuzzy numbers,then the algebraic product is defined by

(Hi, au A)LKX(/I 2 , a2,P2)LR •= (M1M2, IP|I«2 + |A*2|«I, \m\P2 + \IPI\PI)LR •

The operation is only defined if both, Ni and N2 are either positive or negative. Wecan thus multiply positive numbers, negative numbers and a positive and negativefuzzy number but not a fuzzy number whose membership includes both positive andnegative numbers.

It is not possible to define an inverse such that a fuzzy number times its inverseresults in a crisp 1. Below we give a definition which reduces to the crisp analogueif the spreads, a and /?, are zero.

Definition. UN— (ft, a, P)LR is either a positive or a negative fuzzy number thenwe define {multiplicative inverse)

1 _ 1 _ (I P a \W~ {v,a,P)LR '~ \it' n((i + py n(fi + a)) '

If we divide one fuzzy number, Ni, by another, A , we multiply A'I with the inverseof N2

N1jN2 = N1x2r.N2

Example. Assume we have

Ni = (2,0.5,0.5)LH, N2 = (3,0.5,0.5)M.

Then

(2,0.5,0.5)Lii+(3,0.5,0.5)Lil = (5,1, l)LR

(2,0.5,0.5)Lfi-(3,0.5,0.5)£H = ( -1 ,1 , \)LR

(2,0.5,0.5)Lfix (3,0.5, Q.b)LR = (6,2.5, 2.5)LiJ

(2,0.5,0.5)tfl/(3,0.5,0.5)LR = (2/3,0.2619,0.2619)Lfl.

To perform algebra with a mixture of crisp and fuzzy numbers is straightforward.We simply take the crisp numbers as fuzzy numbers with zero spreads and use thefuzzy algebraic operators to obtain a resultant fuzzy number.

Page 575: Nonlinear Workbook (3rd)

556 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.3.5 C + + Implementation of Fuzzy Numbers

The C++ implementation is a straightforward implementation of an abstract datatype. Below we give the implementation for triangular fuzzy numbers. To changethe Li?-spread functions to something other than straight lines one has to onlychange a single line in the private member function LRfunc.

The private data members are simply the mean, (i, the left spread, a and the rightspread, {3. The private member function LRfuncO defines the spread function used.In the current implementation these are simply straight lines and fuzzy numbers arethus triangular fuzzy numbers. To change the type of fuzzy number, for exampleto one using Gaussian or exponential spread, one only has to change the privatemember function LRf unc.

Besides the copy constructor we have a constructor taking the mean, left- and right-spread as arguments. Each of the arguments has the default value zero. Hence, ifno arguments are supplied, a crisp number (zero spread) with value zero is created.If we omit the spreads but supply the mean, a crisp number with the value of themean is created. The following six methods are query/set functions for the mean,the left-spread and the right-spread. The LR representation for fuzzy numbers is ofcourse a continuous representation. We use the function call operator () to retrievethe grade of a fuzzy number for a certain support level.

Next we define the arithmetic operators, most of which are class constants (i.e. theydo not modify the object for which they are called). To see this it is helpful to writethe operator call as a function call.

x + y -> x.operator + (y)

Hence x is the object for which the operator method is called and since x remainsunchanged the addition operator is a class constant. In order to allow for algebrawith both, crisp and fuzzy operands we define class operators which take a crispvalue as argument enabling the user to make constructs like

fuzl = fuz2 + 4;

In order to support the reverse syntax

fuzl = 4 + fuz2;

we have to define a global operator. The class operator is not called since the aboveis interpreted as

4.operator*(fuz2);

Page 576: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 557

The operators take a crisp number as first argument and a fuzzy number as secondargument. The global operators are global functions (not encapsulated within anyclass) which are friends of the class, that is, they have access to the private membersof the class. Finally we supply stream access also via global friend operators » and« .

The implementation is straightforward and requires little explanation. If we wantto change the LR spread function (to something else than linear resulting in fuzzynumbers other than triangular fuzzy numbers) one only has to change a single linein the function LRfunc (the return statement):

double FuzzyNumber::LRfunc(const doublefc x) constiif((x >= 0.0) && (x <= 1.0)) return 1.0 - x;else return 0.0;

}

The header file FuzzyN. h is given by

/ / fuzzyN.h

#include <iostream>using namespace std;

class FuzzyNumber{

public:FuzzyNumber(const doubleft mu=0,const doublefc alpha=0,

const doublefe beta=0);FuzzyNumber(const FuzzyNumberfe num);double meanO const; // also conversion from fuzzy to crispdouble leftSpreadO const;double rightSpreadO const;void setMean(const doubleft mu);void setLeftSpread(const doublefe alpha);void setRightSpread(const doubleft beta);double operator() (const doubleft x) const;FuzzyNumberfc operator = (const FuzzyNumberft num);FuzzyNumber operator + (const FuzzyNumberft num) const;FuzzyNumber operator - (const FuzzyNumberfe num) const;FuzzyNumber operator * (const FuzzyNumberfc num) const;FuzzyNumber operator / (const FuzzyNumberfe num) const;FuzzyNumber operator - () const; // Unary minus.FuzzyNumberfe operator = (const doublefc num);FuzzyNumber operator + (const doublefe num) const;

Page 577: Nonlinear Workbook (3rd)

558 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

FuzzyNumber operator - (const doubleft num) const;FuzzyNumber operator * (const doubleft num) const;FuzzyNumber operator / (const doublefe num) const;FuzzyNumber invert() const;friend FuzzyNumber operator + (const doublefe crisp,

const FuzzyNumberfc fuzzy);friend FuzzyNumber operator - (const doublefc crisp,

const FuzzyNumberfe fuzzy);friend FuzzyNumber operator * (const doublefc crisp,

const FuzzyNumberfe fuzzy);friend FuzzyNumber operator / (const doublefc crisp,

const FuzzyNumberfc fuzzy);

friend istreamfe operator » (istreamfe is.FuzzyNumberfe num);friend ostreamfc operator « (ostreamft os,const FuzzyNumberfc num);

private:double _mu, _alpha, _beta;double LRfunc (const doublefc x) const;

>;

// implementationFuzzyNumber::FuzzyNumber(const doublefc mu,const doublefe alpha,

const doublefc beta): _mu(mu), _alpha(alpha), _beta(beta) {};

FuzzyNumber::FuzzyNumber(const FuzzyNumberfe num)

{ *this = num; }

FuzzyNumberft FuzzyNumber::operator= (const FuzzyNumberfc num)

{

_mu = num._mu;_alpha = num._alpha;_beta = num._beta;return *this;

}

double FuzzyNumber::mean() const { return _mu; };double FuzzyNumber::leftSpread() const { return _alpha; };double FuzzyNumber::rightSpread() const { return _beta; >;

void FuzzyNumber::setMean(const doublefc mu){ _mu=mu; >void FuzzyNumber::setLeftSpread(const doublefc alpha){ _alpha=alpha; }

Page 578: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 559

void FuzzyNumber::setRightSpread(const doubleft beta)

{ _beta=beta; }

FuzzyNumber FuzzyNumber::operator + (const FuzzyNumberft num) const

{

double mu = _mu + num._mu;double alpha = _alpha + num._alpha;double beta = _beta + num._beta;

return FuzzyNumber(mu,alpha.beta);

}

FuzzyNumber FuzzyNumber::operator - (const FuzzyNumberft num) const

{double mu = _mu - num._mu;double alpha = _alpha + num._alpha;double beta = _beta + num._beta;

return FuzzyNumber(mu,alpha.beta);

>

FuzzyNumber FuzzyNumber::operator * (const FuzzyNumberfe num) const{double mu = _mu*num._mu;double alpha = _mu*num._alpha + num._mu*_alpha;double beta = _mu*num._beta + num._mu*_beta;return FuzzyNumber(mu,alpha,beta);

}

FuzzyNumber FuzzyNumber::operator / (const FuzzyNumberft num) const{ return *this * num.invert0; }

FuzzyNumber FuzzyNumber::operator- () const // Unary minus.{ return FuzzyNumber(-_mu,_alpha,_beta); >

FuzzyNumber FuzzyNumber::operator+ (const doubleft num) const

•C

double mu = _mu + num;

return FuzzyNumber(mu,.alpha,_beta);}

FuzzyNumber FuzzyNumber::operator- (const doubleft num) const{double mu = _mu - num;return FuzzyNumber(mu,_alpha,_beta);

}

Page 579: Nonlinear Workbook (3rd)

560 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

FuzzyNumber FuzzyNumber::operator* (const doublefc num) const{double mu = _mu*num;double alpha = num*_alpha;double beta = num*_beta;return FuzzyNumber(mu,alpha,beta);

>

FuzzyNumber FuzzyNumber::operator/ (const doubleft num) const{ return *this*((double)1/num); }

double FuzzyNumber::operator0 (const doublefc x) const{if(x < _mu) return LRfunc((_mu-x)/_alpha);else return LRfunc((x-_mu)/_beta);

}

double FuzzyNumber::LRfunc(const doublefe x) const{if((x >= 0.0) && (x <= 1.0)) return 1.0 - x;

else return 0.0;

}

FuzzyNumber FuzzyNumber::invert 0 const

{double mu = (double) l/_mu;double alpha = _beta/(_mu*(_mu+_beta));

double beta = _alpha/(_mu*(_mu+_alpha));return FuzzyNumber(mu,alpha,beta);

}

// Global OperatorsFuzzyNumber operator* (const doubleft crisp,const FuzzyNumberfe fuzzy){ return FuzzyNumber(crisp) + fuzzy; }

FuzzyNumber operator- (const doubleft crisp,const FuzzyNumberfe fuzzy){ return FuzzyNumber(crisp) - fuzzy; >

FuzzyNumber operator* (const doubleft crisp,const FuzzyNumberfe fuzzy)

{ return FuzzyNumber(crisp)*fuzzy; }

FuzzyNumber operator/ (const doubles crisp,const FuzzyNumberfe fuzzy)

{ return FuzzyNumber(crisp)/fuzzy; >

istreamfe operator » (istreamfe is,FuzzyNumber& num)

Page 580: Nonlinear Workbook (3rd)

17.3. FUZZY NUMBERS AND FUZZY ARITHMETIC 561

{is » num._mu » num._alpha » num._beta;

return is;

>

ostreamft operator « (ostreamfe os,const FuzzyNumberfc num)

{

os « "(" « num._mu « "," « num._alpha « ","

« num._beta « " ) " ;

return os;

}

As an example consider the program f uzzyN. cpp

/ / fuzzyN.cpp

#include <iostream>#include "fuzzyN.h"using namespace std;

int main(void)

{FuzzyNumber numl(2.0,0.5,0.5), num2(3.0,0.5,0.5);cout « "numl, num2, -num2 = " « numl « ", " « num2 « " "

« -num2 « endl;

const double xMin = 1.5, xMax =3.5;const int nPoints = 9;const double dx = (xMax-xMin)/(nPoints-1.0);double x = xMin;for(int i=0;i<nPoints;i++){cout.precision(4); cout.width(6); cout « x « " ";cout.precision(4); cout.width(6); cout « numl(x) « " ";cout.precision(4); cout.width(6); cout « num2(x) « endl;x += dx;>cout « "numl + num2 = " « numl+num2 « endl;cout « "numl - num2 = " << numl-num2 « endl;cout « "numl * num2 = " « numl*num2 « endl;cout « "numl / num2 = " « numl/num2 « endl;cout « "numl + 2 = " << numl+2 « endl;cout « "numl - 2 = " « numl-2 « endl;cout « "numl * 2 = " « numl*2 « endl;cout « "numl / 2 = " « numl/2 « endl;

Page 581: Nonlinear Workbook (3rd)

562 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

cout « "1.3 + numl = " « 1.3+numl « endl;cout « "1.3 - numl = " « 1.3-numl « endl;cout « "1.3 * numl = " « 1.3*numl « endl;cout « "1.3 / numl = " « 1.3/numl « endl;cout « "enter fuzzy number: mu, alpha, beta: ";FuzzyNumber num3; cin » num3; cout « num3;return 0;

>

We first create two fuzzy numbers and then calculate their value on a grid using thefunction call operator (). Note that one can view this as a discretization processof the continuous fuzzy numbers. In the rest of the code we simply test the variousalgebraic operators. The output of the program is given by:

numl, num2, -num2 = (2,0.5,0.5), (3,0.5,0.5) (-3,0.5,0.5)1.5 0 01.75 0.5 0

2 1 02.25 0.5 0

2.5 0 02.75 0 0.5

3 0 13.25 0 0.53.5 0 0

numl + num2 = (5,1,1)numl - num2 = (-1,1,1)numl * num2 = (6,2.5,2.5)numl / num2 = (0.6667,0.2619,0.2619)numl + 2 = (4,0.5,0.5)numl - 2 = (0,0.5,0.5)numl * 2 = (4,1,1)numl / 2 = (1,0.25,0.25)1.3 + numl = (3.3,0.5,0.5)1.3 - numl = (-0.7,0.5,0.5)1.3 * numl = (2.6,0.65,0.65)1.3 / numl = (0.65,0.13,0.13)1.2 0.1 0.2(1.2,0.1,0.2)

17.3.6 ApplicationsFuzzy numbers have a large number of applications in engineering and science. Alarge number of them are described in Ross [79].

Example. Assume we want to determine the power from two fuzzy measurements,the voltage and the current. The spreads for the fuzzy measurements of the voltage

Page 582: Nonlinear Workbook (3rd)

17.4. FUZZY RULE-BASED SYSTEMS 563

and the current are determined by the estimation of the errors in the measurement.Assume, for example, that the maximum reading of the voltmeter is Vmax = 200Vand that the error at full-scale is e = 5%. The spreads are then set to

a~ = /?- = eVmax = 10V.

Assuming we measure a potential difference of 120V, we have V = (120,10, 10)LR-

In the same way we obtain the fuzzy current reading / . The power is then given by

P = VxT.

Assuming7=(10,0.2,0.2)tJ l

we obtainP =(1200,124,124) LR.

Example. The equation of state for an ideal gas is given by pV = NkT. If N andT are kept constant, we have

pV = const.

Assume that for T and N fixed an ideal gas of fuzzy volume

Vr = { (0.5,0.0), (0.75,0.5) (1.0,1.0), (1.25,05), (1.5,0.0) }

is under a fuzzy pressure

Pl = { (0.5,0.0), (1.75,0.5) (2.0,1.0), (2.25,0.5), (2.5,0.0) } .

Using the extension principle one can calculate the fuzzy pressure if the volume isreduced to

V2 = {(0.4,0.0), (0.45,0.5) (0.5,1.0), (0.55,0.5), (0.6,0.0)}.

17.4 Fuzzy Rule-Based Systems

17 A.I Introduction

The purpose of control is to influence the behaviour of a system by changing aninput or inputs to that system according to a rule or set of rules that model howthe system operates. The system being controlled may be mechanical, electrical,chemical or any combination of these. Classic control theory uses a mathematicalmodel to define a relationship that transforms the desired state (requested) andobserved state (measured) of the system into an input or inputs that will alterthe future state of that system. The most common example of a control model isthe PID (proportional-integral-derivative) controller. This takes the output of thesystem and compares it with the desired state of the system. It adjusts the input

Page 583: Nonlinear Workbook (3rd)

564 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

value based on the difference between the two values according to the followingequation

output = A.e + B.INT(e)dt + C.de/dt

where, A, B and C are constants, e is the error term, INT(e)dt is the integral of theerror over time and de/dt is the change in the error term. The major drawback ofthis system is that it usually assumes that the system being modelled is linear orat least behaves in some fashion that is a monotonic function. As the complexity ofthe system increases it becomes more difficult to formulate that mathematical model.

Fuzzy control replaces the role of the mathematical model and replaces it with an-other that is build from a number of smaller rules that in general only describe asmall section of the whole system. The process of inference binding them together toproduce the desired outputs. That is, a fuzzy model has replaced the mathematicalone. The inputs and outputs of the system have remained unchanged. The usageof fuzzy approaches for the automatic control of technical processes tries to imitatethe conscious behaviour of a human operator controlling complex nonlinear pro-cesses or production plants which can hardly be modelled by mathematical-physicalreflections. These circumstances disable a systematical design of a conventionalmodel-based controller.

Examples for these kinds of processes are

• biotechnological production processes,

• chemical processes,

• image processing,

• wastewater treatment plans or

• autonomous vehicle steering and control.

Instead of a conventional linear or nonlinear controller a fuzzy controller is usedwhich consists of the three elements

• fuzzyfication,

• inference and

• defuzzyfication

The first task of a fuzzy controller is the translation of the numerical input variablesto linguistic variables.

Definition. Labeling the crisp value of a numerical input variable with a linguis-tic term and determing the corresponding grade of membership is called fuzzification.

Page 584: Nonlinear Workbook (3rd)

17.4. FUZZY RULE-BASED SYSTEMS 565

Example. Assuming that the output variable y can be measured in a standardcontrol loop, in the following steps only the corresponding linguistic variable withterms like negative big, negative etc. will be processed. Therefore a set of member-ship functions for y has to be defined.

Definition. The determination of conclusions or the generation of hypotheses basedon a given input state is called inference.

For the operation within the standard control loop this means, that the rules definethe dependencies between linguistically classified input values and linguistically clas-sified output values. The result is a manipulated variable u according to the inputsituation. This all occurs in an upper symbolic level first. The implementation canuse a couple of operators which are partially discussed above. Mainly the inferencecomponent imitates the human operator strategies. Common inference strategiesare

• the max-prod inference, which multiplies the whole output's membership func-tion, and

• the max-min inference, which cuts the output's membership function at thetop.

But the symbolic control action cannot be used for a real technical plant, the lin-guistically obtained manipulated variable has to be defuzzified.

Definition. Defuzzification is the calculation of a crisp numerical value as the con-troller output based on the symbolic results.

In most cases several rules will be fired and caused by the fuzzyness different termsand therefore different control actions will be activated. But the actuator requiresa crisp value which can be calculated by different approaches. The most commondefuzzification methods are

• center of gravity,

• COS - center of singletons,

• maximum membership and

• center of maximum memberships.

The practical usage of the given definitions is illustrated by the following example.

Example. A fuzzy controller uses the actual system deviation e(k) as an externalinput variable. An internal short term memory delays the deviation for one samplingperiod so that the previous system deviation e(k — 1) can also be used for thecontroller mapping. The membership functions are chosen equally for all linguisticvariables, i.e. e(k), e(k - 1) and the manipulated variable u{k). For the sake ofsimplicity the inference contains only two rules:

Page 585: Nonlinear Workbook (3rd)

566 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

• IF the system deviations e(k) and e(k - 1) are approximately zero, THENthe control action u(k) is approximately zero.

• IF the system deviations e(k) and e(k - 1) are positive, THEN the controlaction u{k) is positive.

It can easily be seen that this rulebase is incomplete because a lot of terms of theinput variables and the possible combinations are not used.

A fuzzy control system usually receives a fuzzy input signal and performs somelogical or arithmetic operations on that input and gives a fuzzy output. Often theoriginal input signal is obtained from crisp measurements and has to be fuzzified.Often knowledge about the error in the measurement apparatus can be used tofuzzify the input signal. Also, for many control problems the output must again becrisp. In these cases we defuzzify our output via one of the defuzzification operators,for example by determining the centroid of the fuzzy output signal. Consider, forexample, the typical fuzzy control system depicted in the figure. The controlledprocess could be the breaking system of a car, the throttle and wing settings of anairplane or an investment portfolio. Usually measurements on the system result in acrisp measurement signal. This is then fuzzified, often using the error information ofthe measurement apparatus. The fuzzy system then uses the resultant fuzzy inputsignal together with approximate reasoning based on fuzzy rules to obtain a fuzzyoutput signal. Again, the controlled system usually requires a crisp input signal, thechange in the throttle setting of the aeroplane or the amount invested in a certainstock item. To obtain this crisp signal we defuzzify the fuzzy signal with one of thedefuzzification operators. For the system to work well we need a good knowledgebase. The knowledge base contains the rules as well as the information regarding asuitable fuzzification and defuzzification method for the system.

Figure 17.1 Fuzzy Control System

A simple example is temperature control. One can apply a fuzzy logic controlstrategy to control the temperature. A temperature control system consists of a

Page 586: Nonlinear Workbook (3rd)

17.4. FUZZY RULE-BASED SYSTEMS 567

thermostat that contains a heater resistor, a potentiometer, and a thermistor. Theresistor is the temperature controlling controlling element, and the thermistor is thetemperature sensing element. The resistor, activated by a pulse width modulated(PWM) source, heats the adjacent thermistor. A potentiometer sets the desiredfinal temperature (setpoint), and the thermistor senses the current temperature. Inproportional/differential control mode one determines the value for the temperatureerror, TE, and the rate of the temperature error change dTe/dt. The fuzzy controlalgorithm uses these values and sends back the duty cycle for the pulse width mod-ulation resistor. The rule base for the temperature is as follows.

Rule 1. IF the linguistic input term is COLD,THEN the DUTY CYCLE is LARGE.

Rule 2. IF the linguistic input term is COOL,THEN the DUTY CYCLE is MEDIUM.

Rule 3. If the linguistic input term is JUST RIGHT,THEN the DUTY CYCLE is SMALL.

Rule 4. If the linguistic input term is HOT,THEN the DUTY CYCLE is ZERO.

In the defuzzification process we have to associate output linguistic terms with thecrisp output DUTY CYCLE values which vary from zero to 100 percent.

17.4.2 Fuzzy If-Then Rules

Fuzzy control systems use the following conditions to define and implement a rulebase:

1) Input and output linguistic variables contain terms that may be true to a degreeranging from zero to one. Zero represents totally untrue and one represents totallytrue. A linguistic term that is true to a degree 0.5 would be half true and half false.

2) The linguistic operators AND, and OR express conditions in the rule base.

3) IF ... THEN operators express conditions in the fuzzy logic rule base. This ruleis of the general form

IF antecedent THEN consequence4) A fuzzification step links crisp input values to the fuzzy logic control.

5) A defuzzification step links output linguistic terms to crisp output values.

Page 587: Nonlinear Workbook (3rd)

568 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

To implement a fuzzy control strategy to control for example the temperature wemust use two way translations between the crisp and linguistic terms. To implementa fuzzy logic control process we uses the following three steps.

1) Fuzzification. One translates crisp input values into linguistic terms and assignsa degree of membership to each crisp input value. Linguistic variables are fuzzy setswhich contain linguistic terms as members of the set.

2) Fuzzy Rule Inference. IF ... THEN rules that define the relationship betweenthe linguistic variables are established. These rules determine the course of actionthe controller must follow. Thus most fuzzy logic application solutions use produc-tion rules to represent the relationship between linguistic variables and to deriveactions from sensor inputs. Production rules consist of a precondition (IF part) anda consequence (THEN part). The IF part can consist of more than one conditionlinked together by linguistic conjunctions like AND and OR. The computation offuzzy rules is called fuzzy rule inference. Aggregation uses fuzzy logic operators tocalculate the result of the IF part of a production rule when the rule consists ofmore than one input condition. One of the linguistic conjunctions, AND or OR,links multiple input conditions. Composition uses the fuzzy logic operator, PROD,to link the input condition to the output condition. Using the conjunction ANDfor the minimum and OR for the maximum is often appropriate in small controlapplications.

3) Defuzzification. The result of the fuzzy inference is retranslated from the lin-guistic concept to a crisp output value.

17.4.3 Inverted Pendulum Control System

The most famous example (Ross [79], Bojadziev [9]) for fuzzy control is the broomstick balancer (also called inverted pendulum control system). The inverted pendu-lum consists of an arm attached to a cart at a point. The arm rotates in the planeOxy. The cart moves along the x axis forward and backward under the influenceof a force F. The input parameters are the angle a and the velocity v. The outputparameter is the force. The control objectives is for any pair (a, v) which specifythe position and the velocity of the system to find apply a corresponding force F tothe cart that the arm is balanced, i.e. does not fall over the cart.

For fuzzy control applications it is often convenient to write the fuzzy controllersoutput as a system of if-then rules. For example, we could use the following rule aspart of a simple broom balancer:

IFthe angle of the broom is small positive,

THENset the force on its base small negative.

Page 588: Nonlinear Workbook (3rd)

17.4. FUZZY RULE-BASED SYSTEMS 569

In Boolean logic the consequence becomes true when the antecedent is true (notnecessarily vice-versa). In fuzzy logic we often work with half-truths or gradedtruths. Here the consequence only becomes true to the extent that the antecedentis true. We can implement this simple rule in the following way:

Fuzzy smallPositiveAngle(100,-45,45);smallPositiveAngle.triangle(0,15,30);Fuzzy smallNegativeForce(40,-3,3);smallNegativeForce.triangle(-2,-1,0);

Fuzzy fuzzyForce= smallNegativeForce.limit(smallPositiveAngle(angle));

The actual force applied to the broom must, of course, be a crisp value. For thispurpose we defuzzify the fuzzy force via, for example, the centroid defuzzifier

double force = fuzzyForce.centroidO;

Generally we have of course more complicated fuzzy control systems. For example,our broom balancer might be a little more successful if we use the angle and angularvelocity as inputs. We might, for example, have the rule

IFthe angle of the broom is large positive,

ANDthe angular velocity is large positive

THENset the force on its base large negative.

double antecedent= min(smallPositiveAngle(angle),largePositiveVelocity(velocity));

force = largeNegativeForce.limit(antecedent).centroidO;

In general we have many rules and many possible consequences. Consider, once morethe very simple broom balancing problem. Our universe of discourse for angles couldbe LN, SN, Z, SP and LP for large negative, small negative, zero, small positive andlarge positive represented by fuzzy sets. We have a similar universe of discourse forthe applied force and a rule base connecting the two universes. In our case we havethe primitive rule base:

IF angle is LP THEN force is LN.IF angle is SP THEN force is SN.

IF angle is Z THEN force is Z.IF angle is SN THEN force is SP.

Page 589: Nonlinear Workbook (3rd)

570 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

IF angle is LN THEN force is LP.

Assume now the actual angle is about 25°. Then the confidence level for a LP angleis about 0.68, the confidence for a SP angle is about 0.32 and the confidence for allother fuzzy angles is zero. Hence the highest confidence level is given to the fuzzyangle LP and consequently the first rule is selected. The IF . . . THEN rule limitsthe confidence level of the consequence (the LN force) to that of the antecedent (theLP angle). Using the centroid defuzzifier results in a crisp force of about —1.75.

17.4.4 Application

Lastly, let us have a look at an approximate reasoning example. Consider thefollowing rules for analyzing a computer code for the computer language used.

IFvaguely many extends,

THENJava.

IFmore than n semicolons,

ANDsomewhat many curly brackets

AND NOTmany beginAND NOT

JavaTHENC++

Again, we can use our discrete fuzzy set class to implement this fuzzy inferenceproblem.

// language.cpp

#include <iostream>#include "Fuzzy.h"using namespace std;

template <class T>T min4(const Tft xl,const T& x2,const T& x3,const T& x4)

iT min = xl;if(min > x2) min = x2;if(min > x3) min = x3;if(min > x4) min = x4;

Page 590: Nonlinear Workbook (3rd)

17.4. FUZZY RULE-BASED SYSTEMS 571

return min;}

int main(void)

{const int pMin = 0;

const int pMax = 10;const int nDiscr = 200;

Fuzzy enoughSemicolons(nDiscr,pMin.pMax);

enoughSemicolons.greaterThan(5);Fuzzy somewhatManyCurlyBrackets(nDiscr,pMin.pMax);somewhatManyCurlyBrackets.large();s omewhatManyCurlyBracket s= somewhatManyCurlyBrackets.hedge(Fuzzy::somewhat);Fuzzy manyBegin(nDiscr,pMin,pMax);manyBegin.large();Fuzzy slightlyManyExtends(nDiscr,pMin.pMax);slightlyManyExtends.large 0;slightlyManyExtends = slightlyManyExtends.hedge(Fuzzy::slightly);double nSemicolon, nCurlyBracket, nBegin, nExtend;cout « "Enter no of ; "; cin » nSemicolon;cout « "Enter no of { "; cin » nCurlyBracket;cout « "Enter no of begin "; cin » nBegin;cout « "Enter no of extend "; cin » nExtend;double Java = (slightlyManyExtends)(nExtend);double notJava = (!slightlyManyExtends)(nExtend);double cpp = min4(enoughSemicolons(nSemicolon),

somewhatManyCurlyBrackets(nCurlyBracket),(!manyBegin)(nBegin),notJava);

cout « "Confidence that Java: " « Java « endl;cout « "Confidence that C++ : " « cpp « endl;

return 0;

}

We can run the program now for various inputs for the frequency of occurance ofthe program elements, semicolon, curly bracket, begin and extend (on some arbitraryscale): Below is the output for 2 different trial runs:

Enter no of ; 10Enter no of { 10Enter no of begin 0Enter no of extend 0Confidence that Java: 0Confidence that C++ : 0.993307

Page 591: Nonlinear Workbook (3rd)

572 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

Enter no of ; 20Enter no of { 15Enter no of begin 5Enter no of extend 10Confidence that Java: 1Confidence that C++ : 0

17.5 Fuzzy C-Means Clustering

The aim of this clustering technique is to group a given set of data consisting of nvectors in R*

X = {x0, xi, . . . xn_x}

into a number of clusters c, so that the data in the same group are as similar aspossible and the data in different groups are as dissimilar as possible. Fuzzy C-Meansachieves this classification by minimising the objective function

^,v) = x:i:(^riixi-v,ii2

i=0 j=0

where /u^ represents the membership degree of data x, to the cluster centre v,-. Thequantity //y satisfies the following conditions

/ iy€[0, l ] for t = 0 , l , . . . , n - l , j = 0 , 1 , . . . ,c - 1

andc-l£ IMJ = 1, for i = 0 , 1 , . . . , n - 1.

Furthermore ||XJ — v;| | is the Euclidean distance between X; and Vj. The parameterm is used to control the fuzziness of membership of each datum, m > 1. Thereis no theoretical basis for the optimal selection of the exponent m, but a value ofm = 2.0 is usually chosen. The n x c matrix U = (/Xy) is called a fuzzy partitionmatrix and V = {v0, Vi, . . . , vc_i} is a set of cluster centres (of course also vectorsin R*). The fuzzy C-mean clustering algorithm attempts to partition a finite colle-cetion of elements of the set X into a collection of c fuzzy clusters with respect tosome given criterion. Thus the fuzzy C-means clustering needs a priori assumptionof the number of cluster. The fuzzy C-means clustering algorithm is as follows:

1) Initialize the membership matrix U = (/Zy) with random values so that

Aiy€[0,l] for i = 0,l,...,n-l, j = 0 , 1 , . . . , c - 1

andc-l] T / i y = l , for i = 0 , l , . . . , ra-1.3=0

Page 592: Nonlinear Workbook (3rd)

17.5. FUZZY C-MEANS CLUSTERING 573

2) Compute the fuzzy centres v, using

_ 2jj=o {l^ij) x iVi ~" \pn-lf \ m

for j = O , l , . . . , c - l .

3) Calculate the new distances

dij = \\*i - Vj\\

for i = 0 , 1 , . . . , n — 1 and j = 0 , 1 , . . . , c — 1.

4) Update the fuzzy membership matrix U = (fiij) according to: if d^ ^ 0, then

_ 1

else/xy = 1.

5) Repeat step 2) to 4) until the minimum J value is achieved.

In the C++ program we consider 4 vectors in R3, m = 2 and two clusters, i.e. c = 2.We do 20 iterations. It is left as an exercise to calculate J(U, V) at each time stepand consequently obtain a criteria for convergence.

// clustering.cpp

#include <iostream>#include <cmath> // for sqrtusing namespace std;

double distance(double* wl,double* w2,int k){double r = 0.0;for(int i=0;i<k;i++)

{r += (wl[i]-w2[i])*(wl[i]-w2[i]);}return sqrt(r);

>

int main(void)

{int n = 4; // number of vectorsint k = 3; // length of vectors

Page 593: Nonlinear Workbook (3rd)

574 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

double** X = new double*[n];fo rdn t i=0;i<n;i++) { X[i] = new doubleW; >X[0][0] = 1.0; X[0][l] = 1.0; X[0] [2] = 0.0;X[l][0] = 0.0; X[1][1] = 1.0; X[l][2] = 1.0;X[2][0] = 1.2; X[2][l] = 0.9; X[2][2] = 0.1;X[3][0] = 0.1; X[3][l] = 1.1; X[3] [2] = 0.95;

int c = 2; / / number of clustersdouble** V = new double*[c];for(int i=0;i<c;i++) { V[i] = new double[k]; }

double** U = new double*[n];for(int i=0;i<n;i++) { U[i] = new double[c]; >U[0][0] = 0.1; U[0][l] = 0.9;U[l][0] = 0.4; U[1][1] = 0.6;U[2][0] = 0.5; U[2][l] = 0.5;U[3][0] = 0.7; U[3][l] = 0.3;

double** D = new double*[n]; / / distancesfo rdn t i=0;i<n;i++) { D[i] = new double[c]; }

/ / Compute the fuzzy centresint count = 0;while(count < 50){fordn t j=O;j<c;j++){fo rdn t l=0;Kk;l++){double t l = 0.0;double t2 = 0.0;fo rdn t i=0;i<n;i++){t l += U[i][j]*U[i][j]*X[i][l];t2 += U[i][ j]*U[i][ j] ;} / / end for loop iV[j][l] = tl/t2;} / / end for loop 1} / / end for loop j

for(i=0;i<n;i++){for(j=0;j<c;j++){D[i][j] = distance(X[i],V[j],k);

Page 594: Nonlinear Workbook (3rd)

17.5. FUZZY C-MEANS CLUSTERING 575

>>

for(int i=O;i<n;i++)•Cfor(int j=O;j<c;j++){double t3 = 0.0;if(D[i][j] == 0.0) U[i][j] = 1.0;elsefor(int p=O;p<c;p++)it3 += D[i][j]*D[i][j]/(D[i][p]*D[i][p]);>U[i][j] = 1.0/t3;>}count++;> / / end while

for(int i=0;i<n;i++)for(int j=O;j<c;j++)cout « "U[" « i « "] [" « j « "] = " « U[i][j] « endl;

for(int j=O;j<c;j++)for(int i=0;i<k;i++)cout « "V[" « j « "] [" « i « "] = " « V[j] [i] « endl;

/ / free memoryfor(int i=0;i<n;i++){ deleted X[i]; }delete [] X;for(int i=0;i<c;i++){ deleted V[i]; }deleted X;for(int i=0;i<n;i++){ deleted U[i]; }deleted U;for(int i=0;i<n;i++){ deleted D[i]; }deleted D;return 0;

}

Page 595: Nonlinear Workbook (3rd)

576 CHAPTER 17. FUZZY SETS AND FUZZY LOGIC

17.6 Fuzzy Truth Values and Probabilities

What is the relationship between fuzzy truth values and probabilities? This questionhas to be answered in two ways: how does fuzzy theory differ from probability the-ory mathematically, and second, how does it differ in interpretation and application.

At the mathematical level, fuzzy values are commonly misunderstood to be prob-abilities, or fuzzy logic is interpreted as some new way of handling probabilities.However this is not the case. A minimum requirement of probabilities is "additiv-ity", loosely speaking that is that they must add up to one, or the integral of thedensity curve must be one.

However, this does not hold in general with membership grades. While member-ship grades can be determined with probabilities densities in mind, there are othermethods as well, which have nothing to do with frequencies or probabilities.

Owing to this, fuzzy researchers have gone to great pains to distance themselvesfrom probabilities. However in doing so, many of them have lost track of anotherpoint, which is that the converse does hold; all probabilities distributions are fuzzysets. As fuzzy sets and logic generalize Boolean sets and logic, they also generalizeprobability.

In fact, from a mathematical perspective, fuzzy sets and probabilities exist as partof a greater Generalized Information Theory which includes many formalisms forrepresenting uncertainty (including random sets, Demster-Shafer evidence theory,probability intervals, possibility theory, general fuzzy measures, interval analysis,etc.). Furthermore, one can also speak about random fuzzy events and fuzzy ran-dom events.

Semantically, the distinction between fuzzy logic and probability theory has to dowith the difference between the notions of probability and a degree of membership.Probability statements are about the likelihoods of outcomes: an event either oc-curs or does not. However, with fuzziness, one cannot say unequivocally whetheran event occured or not, and instead we are trying to model the "extent" to whichan event ocurred.

Page 596: Nonlinear Workbook (3rd)

Bibliography

[1] Arnold V. I. and Avez A., Ergodic Problems of Classical Mechanics, Addison-Wesley, New York, 1989

[2] Arrowsmith D. K. and Place C. M., An Introduction to Dynamical Systems,Cambridge University Press, Cambridge, 1990

[3] Bac Fam Quang and Perov V. L., New evolutionary genetic algorithms forNP-complete combinatorial problems, Biological Cybernetics 69, 229-234, 1993

[4] Bandemer H. and Gottwald S., Fuzzy Sets, Fuzzy Logic, Fuzzy Methods withApplications, John Wiley, Chichester, 1995

[5] Baker G. L. and Gollub J., Chaotic Dynamics : An Introduction, CambridgeUniversity Press, Cambridge, 1990

[6] Barnsley M., Fractals Everywhere, Academic Press, Boston, 1988

[7] Bauer M. and Martienssen W. Lyapunov exponents and dimensions of chaoticneural networks, J. Phys. A: Math. Gen. 24, 4557-4566, 1991

[8] Wavelets: Mathematics and Applications, Edited by John J. Benedetto andMichael W. Frazier, CRC Press, BocaRaton, 1994

[9] Bojadziev G. and Bojadziev M., Fuzzy Sets, Fuzzy Logic, Applications, WorldScientific, Singapore, 1995

[10] Bowen R., Methods of Symbolic Dynamics, Springer-Verlag, Berlin, 1975

[11] Broomhead D. S., Huke J. P. and Muldoon M. R., Linear filters and nonlinearsystems, J. Roy. Stat. Soc. B 54, 373-382, 1992

[12] Chaitin G. J., Information, Randomness and Incompleteness, World Scientific,Singapore, 1987

[13] Charniak E., Statistical Language Learning, MIT Press, Cambridge, Mas-sachusetts, 1993

577

Page 597: Nonlinear Workbook (3rd)

578 BIBLIOGRAPHY

[14] Chen L. and Aihara K. Chaotic simulated annealing by a neural network modelwith transient chaos, Neural Networks, 8, 915-930, 1995

[15] Cherbit G. (editor), Fractals : Non-integral dimensions and applications, JohnWiley, Chichester, 1991

[16] Chow S. N. and Hale J. K., Methods of Bifurcation Theory, Springer-Verlag,Berlin, 1982

[17] Cichocki A. and Unbehauen R., Neural Networks for Optimization and SignalProcessing, John Wiley, Chichester, 1993

[18] Cohen A., Daubechies I. and Feauveau J.-C, Biorthogonal bases of compactlysupported wavelets, Comm. Pure and Appl. Math., 45, 485-560, 1992

[19] Collet P. and Eckmann J. P., Iterated Maps on the interval as Dynamical Sys-tems, Birkhauser, Boston, 1980

[20] Cronje G. A. and Steeb W.-H., Genetic Algorithms in a Distributed ComputingEnviroment Using PVM, International Journal of Modern Physics C 8, 327-344, 1997

[21] Cybenko G., Approximation by superpositions of a sigmoidal function, Mathe-matics of Control, Signals and Systems 2, 303-314, 1989

[22] Deslauriers G., Dubuc S. and Lemire D., Une famille d'ondelettes biorthogo-nales sur l'intervalle obtenue par un schema d' interpolation iterative, Ann. Sci.Math. Quebec, 23, 37-48, 1999

[23] Devaney R. L., An Introduction to Chaotic Dynamical Systems, Second Edition,Addison-Wesley, Redwood City, 1989

[24] Dubois D. and Prade H., Fuzzy Sets and Systems: Theory and Applications,Academic Press, New York, 1980

[25] Eckmann J.-P. and Ruelle D., Ergodic Theory of Chaos and Strange Attractors,Reviews of Modern Physics 57, 617-655, 1985

[26] Eckmann J.-P., Kamphorst S. O., Ruelle D. and Ciliberto S., Liapunov Expo-nents from Time Series, Physical Review A 34, 4971-4979, 1986

[27] Edgar G. A., Measure, Topology, and Fractal Geometry, Springer-Verlag, NewYork, 1990

[28] Wavelets: Theory and Applications, Edited by Gordon Erlebacher, M. PousuffHussaini, Leland M. Jameson, Oxford University Press, Oxford, 1996

[29] Falconer K. J., The Geometry of Fractal Sets, Cambridge University Press,Cambridge, 1985

Page 598: Nonlinear Workbook (3rd)

BIBLIOGRAPHY 579

[30] Fausett L., Fundamentals of Neural Networks : Architecture, Algorithms andApplications, Prentice Hall, Englewood Cliffs, N. J., 1994

[31] Ferreira C, Gene Expression Programming: A New Adaptive Algorithm forSolving Problems, http://xxx.lanl.gov, cs.AI/0102027

[32] Froberg C. E., Numerical Mathematics: Theory and Computer Applications,Benjamin-Cummings, Menlo Park, 1985

[33] Funahashi K.-I., On the approximate realization of continuous mappings byneural networks, Neural Networks, 2, 183-192, 1989

[34] Goldberg D. E., Genetic Algorithms in Search, Optimization and MachineLearning, Addison-Wesley, Reading, MA, 1989

[35] Goldberg D. E. and Lingle R., Alleles, Loci, and the TSP, in Greffenstette,J. J. (Editor), Proceedings of the First International Conference on GeneticAlgorithms, Lawrence Erlbaum Associates, Hillsdale, NJ, 1985

[36] Grassberger P. and Procaccia I., Characterization of strange attractors, Physi-cal Review Letters 50, 346-349, 1983

[37] Gumowski I. and Mira C, Recurrences and Discrete Dynamical Systems,Springer-Verlag, Berlin, 1980

[38] Hairer E., Norsett S. P. and Wanner G., Solving Ordinary Differential EquationsI, Second Revised Edition, Springer-Verlag, Berlin 1993

[39] Hao Bai-Lin, Elementary Symbolic Dynamics and Chaos in Dissipative Systems,World Scientific, Singapore, 1989

[40] Hao Bai-Lin, Symbolic Dynamics and Characterization of Complexity, PhysicaD 51, 161-176, 1991

[41] Hardy G. H. and Wright E. M., An introduction to the theory of numbers,Oxford at the Clarendon Press, 1965

[42] Hardy Y. and Steeb W.-H., Classical and Quantum Computing with C++ andJava Simulations, Birkhauser Verlag, Basel, 2001

[43] Hassoun M. H., Fundamentals of Artificial Neural Networks, The MIT Press,Cambridge Massachusetts, 1995

[44] Haykin S., Neural Networks, Macmillan College Publishing Company, NewYork, 1994

Page 599: Nonlinear Workbook (3rd)

580 BIBLIOGRAPHY

[45] Hirsch M. W. and Smale S., Differential Equations, Dynamical Systems, andLinear Algebra, Academic Press, New York, 1974

[46] Holland J. H., Adaptation in Natural and Artificial Systems, University ofMichigan Press, Ann Arbor, 1975

[47] Holmgren R. A., A First Course in Discrete Dynamical Systems, Springer-Verlag, New York, 1994

[48] Hornik K., Stinchcombe M. and White H., Multilayer feedforward networks areuniversal approximators, Neural Networks, 2, 359-366, 1989

[49] Hiibler A. W., Adaptive Control of Chaotic Systems, Helvetica Physica Ada62, 343-346, 1989

[50] Hurst H. E., Long Term Storage Capacity of Reservoirs, Trans. Am. Soc. CivilEng. 116, 770-776, 1951

[51] Kamp Y. and Hasler M., Recursive Neural Networks for Associative MemoryJohn Wiley, Chichester, 1990

[52] Kantz H., A robust method to estimate the maximal Lyapunov exponent of atime series, Physics Letters A 185, 77-87, 1994

[53] Kluiving R., Capel H. W. and Pasmanter R. A., Phase-Transition Like Phe-nomenon in a Piecewise Linear Map, Physica A 164, 593-624, 1990

[54] Knuth D. E., Seminumerical Algorithms, The Art of Computer Programming,Volume2, second edition, Addison Wesley, Reading, 1981

[55] Kohonen T., Self-Organization and Associative Memory, third edition,Springer-Verlag, Berlin, 1989

[56] Kowalski K. and Steeb W.-H., Nonlinear Dynamical Systems and CarlemanLinearization, World .Scientific, Singapore, 1991

[57] Lai Y.-C, Encoding Digital Information using Transient Chaos, Int. J. Bifur.Chaos, 10, 787-795, 2000

[58] Lempel A. and Ziv J., On the Complexity of Finite Sequences, IEEE Transac-tions on Information Theory, IT-22, 75-81, 1976

[59] Leven R. W., Koch B. P. and Pompe B., Chaos in Dissipativen Systemen,Vieweg, Berlin, 1989

[60] Lopez-Ruiz R. and Perez-Garica C, Dynamics of two logistic maps with amultiplicate coupling, Int. J. of Bifurcation and Chaos, 2, 511-528, 1991

Page 600: Nonlinear Workbook (3rd)

BIBLIOGRAPHY 581

[61] Lopez-Ruiz R. and Perez-Garica C, Dynamics of maps with a global multipli-cate coupling, Chaos, Solitons and Fractals, 1, 421-425, 1992

[62] MacKay R. S. and Meiss J. D. (Editors), Hamiltonian Dynamical Systems,Adam Hilger, Bristol, 1987

[63] Mandelbrot B. B. and Wallis J. R., Noah, Joseph and operational hydrology,Water Resources Research 4, 909-918, 1968

[64] Mandelbrot B. B. and Van Ness J. W., Fractional Brownian motions, fractionalnoises and applications, SIAM Review 10, 422-437, 1968

[65] Mandelbrot B. B., The Fractal Geometry of Nature, Freeman, New York, 1982

[66] Maniezzo V., Genetic Evolution of the Topology and Weight Distribution ofNeural Networks, IEEE Transactions of Neural Networks, 5, 39-53, 1994

[67] Marsden J. E. and McCracken M., The Hopf Bifurcation and its Applications,Springer-Verlag, Berlin, 1976

[68] McLaren D. I. and Quispel G. R. W., Integral-preserving integrators, J. Phys.A: Math. Gen. 37, L489-L495, 2004

[69] Michalewicz Z., Genetic Algorithms + Data Structure = Evolution Programs,Third Edition, Springer-Verlag, Berlin, 1996

[70] Mira C, Chaotic Dynamics : From the One-dimensional Endomorphism to theTwo-dimensional Diffeomorphism, World Scientific, Singapore, 1987

[71] Newhouse S. E., Ruelle D. and Takens F., Occurrence of strange axiom A attrac-tors near quasi-periodic flow on Tm, m > 3, Communications in MathematicalPhysics 64, 35-40, 1979

[72] Ott E., Grebogi C. and Yorke Y. A., Controlling Chaos, Phys. Rev. Lett. 64,1196-1199, 1990

[73] Pecora L. M. and Carroll T. L., Synchronization in Chaotic Systems, Phys.Rev. Lett. 64, 821-824, 1990

[74] Peitgen H.-O. and Richter P. H., The Beauty of Fractals, Springer-Verlag,Berlin, 1986

[75] Peters Edgar E., Fractal Market Analysis: Applying Chaos Theory to Invest-ment and Economics, John Wiley, Chichester, 1994

[76] Rabiner L., A tutorial on Hidden Markov Models and selected applications inspeech recognition, Proceedings of the IEEE, 77, 257-286, 1989

Page 601: Nonlinear Workbook (3rd)

582 BIBLIOGRAPHY

[77] Rojas R., Neural Networks, Springer-Verlag, Berlin 1996

[78] Rosenstein M. T., Collins J. J. and De Luca C. J., A practial method forcalculating largest Lyapunov exponents from small data sets, Physica D 65,117-134, 1993

[79] Ross T. J., Fuzzy Logic with Engineering Applications, McGraw-Hill, New York,1995

[80] Ruelle D., Chaotic Evolution and Strange Attractors, Cambridge UniversityPress, Cambridge, 1989

[81] Sano M. and Sawada Y., Measurement of the Lyapunov Spectrum from ChaoticTime Series, Phy. Rev. Lett. 55, 1082-1085, 1985

[82] Sato S., Sano M. and Sawada Y., Practical methods of measuring the general-ized dimension and the largest Lyapunov exponent in high dimensional chaoticsystems, Progess in Theoretical Physics 77, 1-5, 1987

[83] Seydel R., From Equilibrium to Chaos, Elsevier, New York, 1988

[84] Sparrow C, The Lorenz Equations: Bifurcations, Chaos and Strange Attractors,Springer-Verlag, Berlin, 1982

[85] Steeb W.-H., A Handbook of Terms Used in Chaos and Quantum Chaos, Bib-liographisches Institut, Mannheim, 1991

[86] Steeb W.-H., Chaos und Quantenchaos in Dynamischen Systemen, Bibli-ographisches Institut, Mannheim, 1994

[87] Steeb W.-H. and Louw J., Chaos and Quantum Chaos, World Scientific, Sin-gapore, 1986

[88] Steeb W.-H., Louw J. A. and Villet C. M., Chaos in Yang-Mills equations,Phys. Rev. D 33, 1174-1176, 1986

[89] Steeb W.-H., Solms F., Tan Kiat Shi, and Stoop R., Cubic Map, Complexityand Ljapunov Exponent, Physica Scripta, 55, 520-522, 1997

[90] Steeb W.-H., Hardy Y., Hardy A., and Stoop R., Problems and Solutions in Sci-entific Computing with C++ and Java Simulations, World Scientific, Singapore2004

[91] Tan Kiat Shi, Steeb W.-H., and Hardy Y., SymbolicC++: An Introduction toComputer Algebra Using Object-Oriented Programming, second extented andrevised edition, Springer-Verlag, London 2000

[92] Tan Z., Hepburn B. S., Tucker C. and Ali M. K., Pattern Recognition UsingChaotic Neural Networks, Discrete Dynamics in Nature and Society, 2, 243-247,1998

Page 602: Nonlinear Workbook (3rd)

BIBLIOGRAPHY 583

[93] Toda M., Theory of Nonlinear Lattices, second edition, Springer-Verlag, Berlin,1988

[94] Verlet L., Computer Experiments on Classical Fluids I. ThermodynamicalProperties of Lennard-Jones Molecules, Phys. Rev. 159, 98-104, 1967

[95] Walters P., An Introduction to Ergodic Theory, Springer-Verlag, Berlin, 1982

[96] Wolf A., Swift J. B., Swinney H.L., and Vastano J. A., Determining LyapunovExponents From Time Series, Physica D 16, 258-317, 1985

[97] Wolfram S., Theory and Applications of Cellular Automata, World Scientific,Singapore, 1986

[98] Yager R. R. and Zadeh L. A. (Editors), Fuzzy Sets, Neural Networks, and SoftComputing, Van Nostrand Reinhold, New York, 1994

[99] Yoshida H., Construction of higher order symplectic integrators, Physics LettersA 150, 262-268, 1990

[100] Zimmermann H.-J., Fuzzy Set Theory and its Applications, second edition,Kluwer Academic Publishers, Boston, 1994

Page 603: Nonlinear Workbook (3rd)
Page 604: Nonlinear Workbook (3rd)

Index

a-cut operator, 516 Centroid, 515a-level set, 509 Chaotic repeller, 52/3-cut operator, 516 Characteristic equation, 112

Chromosome, 425Absolutely linearly separable, 323 Chromosomes, 350Absorbing, 477 C i r d e m a p ) u

Action-angle variables, 58 Classical set, 502Activation function, 279, 328 Complement, 511Adjacency matrix, 386 Complement of fuzzy sets, 511Algebraic difference, 554 Complexity, 107Algebraic product, 514, 555 Contained, 509Algebraic sum, 513, 554 C o n v e x f u z z y ^ 5 Q 9

ANrn ' r> Convolution functions, 457AND gate, 321 Correlation dimension, 66Animals, 350 n , ,. . . , c c

Correlation integral, 06Asymptotic stability, 143 C o s i n e ^ m

Asymptotically stable, 143 C o g t ^ . ^ ^Autocorrelation function, 26, 176 C o u p l e d c m a p ] g l

Average fitness, 356 Cowell's method, 275Baker-Campbell-Hausdorff formula, 272 Criterion function, 309Batch mode, 338 Critical points, 49Bernoulli map, 7 Crossover, 352Bernoulli shift, 8 Crossover operation, 353Bias, 320 Crossover points, 510Binary Hopfield model, 285Binary representation, 7 D e c i s i o n func t i°n> 4 4 8

Bounded difference, 514 Defuzzification, 565Bounded sum, 514 D e l t a f u n c t i o n - 2 1

Bungalow-tent map, 10, 21 Bert's s t a i r c a s e ' 4 4 ' 2 2 6

Dineomorpmsm, 172Cantor function, 226 Direct method, 88Cantor set, 225 Directed Hausdorff distance, 303Capacity, 67, 223 Discrete cosine transform, 81Cardinality, 510 Discrete Fourier transform, 28Cartesian product, 514 Distributive law, 71Catenation, 107 Divide and Conquer algorithm, 31Central difference scheme, 265 Driven van der Pol equation, 188

585

Page 605: Nonlinear Workbook (3rd)

586 INDEX

Energy function, 284, 288 Greater than, 517Epoch, 337Equilibrium point, 111, 142 H a a r function, 454Error function, 339 Hamilton's equations of motion, 124Euler method, 258 Hamming distance, 284Euler-Lagrange equations, 439 Harmonic oscillator, 273, 439Eventually periodic, 2 Hartman-Grobman theorem, 53, 144Extended input vector, 320 Hausdorff dimension, 223Extension principle, 548 Hausdorff distance, 302Exterior product, 71, 160 Heaviside function, 66, 280

Hebb's law, 290Fast Fourier transform, 30 Heighway Dragon, 228Father wavelet, 453 jjenon map, 55, 63, 203Feature space, 448 Henon-Heiles Hamilton function, 135Feigenbaum's constant, 48 Henon-Heiles model, 125Finite difference scheme, 265 Hidden Markov model, 472First integral, 128 Hill equation, 132Fitness function, 351 Homoclinic orbit, 113Fixed point, 1, 15, 63, 111, 142, 168, Hopf bifurcation, 163

287> 4 7 5 Hopfield algorithm, 283, 287Floquet theory, 132 Hopfield network, 283Forward-backward procedure, 483 H u r s t exponent, 96Fractional Brownian motion, 106 Hyperbolic 2 53 144Fractional part, 12 Hyperbolic fixed point, 169Frobenius-Perron integral equation, 18 Hyperbolic periodic orbit, 54Fuzzification, 564 Hyperbolic periodic point, 54Fuzzy AND, 514 Hyperchaos, 70Fuzzy interval, 547 Hyperchaotic behaviour, 71Fuzzy logic, 506Fuzzy number, 547 Ikeda laser map, 60Fuzzy OR, 515 Indicator function, 10, 21Fuzzy partition matrix, 572 Individuals, 350Fuzzy set, 507 Inference, 565Fuzzy sets, 506 Integral invariants, 270

Integration function, 328Game of Lite, 250 T . .. r n

' Intersection, 511Gauss map, 11 Intersection of fuzzy sets, 510Gauss-Seidel iteration, 299 T . , , ., , „

Invariant density, 17Gaussian function, 307 T , . , c l

, . . . _ _ Invariant dynamical range, 51Gaussian membership functions, 517 T • i A* •<• u rp, ,,.

v Invariant Manifold Theorem, 144' Iterated function system, 224

Generating function, 271Generating functions, 270 Jacobian matrix, 77Ghost solution, 265 Joseph effect, 97GNU-plot, 13Grafimann product, 71 Karush-Kuhn-Tucker condition, 441

Page 606: Nonlinear Workbook (3rd)

INDEX 587

Kernel-Adatron algorithm, 450 Mathieu's equation, 133Kicked rotor, 58 Max-norm, 86Kirchhoff's Current Law, 281 Maximum grade operator, 515Knapsack problem, 391 McCulloch-Pitts model, 280Kneading sequence, 50 Mean value, 17Koch curve, 232 Membership function, 507Kullback-Leibler distance, 487 Metric, 224

Metric space, 224Lagrange function, 439, 441 Metropolis criterion, 407Lagrange multiplier method, 390, 435 M i n i m u m g r a d e o p e r a t O T j 5 1 5

Large, 517 Molecular dynamics, 275Lax representation, 129 Molecular dynamics simulations, 273Leapfrog method, 275 M o n t e C a r l o m e t h o d i ^Learning factor, 291 ». ,, , , ,C/I

to Mother wavelet, 454Learning rate, 307 , , ,,. ,. ,. . ___

Multiplicative inverse, 555Learning step, 307 Mutation, 352Learning with a teacher, 324 Mutation operator, 353Least-square-error algorithm, 87Length, 107 Near, 517Less than, 517 Newton method, 75Letters, 107 Newton-Raphson method, 45Levy dragon, 228 Noah effect, 97Levy-flight random walk, 242 Non-wandering point, 79Liapunov exponent, 22, 36, 154 Normal family, 240Liapunov function, 284, 288 Normalized fuzzy sets, 507Lie series, 260Limit cycle, 116 Orbit, 1Linear congruential sequence, 40 Order of integrator, 272Linear correlation coefficient, 84T . . . . , ,. ,r-. Parallel Virtual Machine, 410Linear vanational equation, 154T . . . . c o Parent selection, 353Linearization, 53 'T . • , ,. o o -i i -, Pattern mode, 337Linearized equation, 22, 111T . , , , L Penalty method, 390, 440Linearly separable, 323 ^ ,Linguistic hedge, 518 Pendulum, 114Liouville's theorem, 269, 270 Perceptron, 319Local search, 352 P e r i o d i c Point< 2

Logistic function, 339 P e r i o d i c w i n d o w s> 3 8

Logistic map, 5, 88, 198 Permutation group, 400Lorenz model, 145, 206 P h a s e d l f fe rence> 2 1 8

Lotka-Volterra model, 157 P h a s e Por t r a i t> 5 4

Lotka-Volterra systems, 120 P o i n c a r f m aPPi nS ' 1 6 8

Lozi man 56 Poincare section method, 137LR t 552 Pointwise Hausdorff distance, 303

Pole placement technique, 203Mandelbrot set, 237 Population, 350, 425Markov chain, 472, 476 Postscript file, 13

Page 607: Nonlinear Workbook (3rd)

588 INDEX

Prime period, 2 Threshold function, 279Probability density, 17 Time average, 26, 176Probability vector, 474 Time-dependent first integrals, 166Pyramid algorithm, 457 Time-independent solutions, 111

Toda lattice, 128, 129Reproduction, 352 T o t a l fitnesg) 3 7 6

R o o t s> 7 6 Trajectory, 1Rossler model, 213 Transition diagram, 478Rotation number, 44 Transition matrix, 476Runge-Kutta-Fehlberg method, 263 Transition probabilities, 476

o J J I i 7 n Trapezoidal function, 508Saddles, 170 K 'c , , , „ , Traveling salesman problem, 315Scalar product, 454 m , b , K 'o ,. , ,. . „ Trotter's formula, 273Scaling function, 453 m i \ ^o , . , i r i 1 wo-dimensional discrete Courier trans-Shannon entropy, 519 o^^„. . . . , . ooi form, 300Sierpmksi gasket, 231 m , T .„. r ,. „„„ n o . Two-dimensional Liapunov exponent,Sign function, 280, 286 f t - ,Sine map, 20, 25Sink, 143, 170 Unary minus, 554Small, 516 Union, 511Source, 170 U n i o n of fuzzy s e t s , 510Spherical coordinates, 436 Universe of discourse, 506Spin-glass states, 290 Unstable, 143Spurious attractors, 290Spurious solution, 265 Van der Pol equation, 111, 116Spurious stable state, 283 Variational equation, 16, 22, 111, 126,Spurious states, 290 160Stormer method, 275 Verlet algorithm, 273Stable, 16 Vocabulary, 107Standard map, 58State space, 476 Wavelet function, 454Stationary point, 142 Wedge product, 71n i , ,. , . , - c Weierstrass function, 242Stochastic matrix, 475 'String, 107 wTi^"™1

Strings, 350 W o r d ' 1 0 7

Strutt diagram, 133 Z e r o S ; 3 8 4

Support for a fuzzy set, 509Support vector machine, 445Surface-of-section method, 137Symbols, 107Symplectic, 269Symplectic matrix, 269Synaptic weights, 279

Taylor expansions, 273Tent map, 8