# 1. Rastrigin Function

## 1.1. Abstract

In this page, we describe the Rastrigin function which is often used as an example in global optimization. We describe its main features and present several plots obtained with Scilab.

## 1.2. Introduction

Rastrigin functions is a class of functions which are used in global optimization because it has the property to have many local minima but only one global minimum [1,2,3].

In this page, we consider the Rastrigin function defined for by This function has several local minima, but only one global minimum, that is associated with the value .

## 1.3. Scilab scripts

In this section, we detail the Scilab scripts which enables to define the Rastrigin function and draw contour and 3D plots.

In the following Scilab script, one defines the Rastrigin function.

function y = rastriginV ( x1 , x2 ) // Vectorized function for contouring. y = x1.^2 + x2.^2-cos(12*x1)-cos(18*x2) endfunction function y = rastrigin ( x ) // Non-vectorized function for optimization. y = rastriginV ( x(1) , x(2) ) endfunction

The following Scilab commands generates a contour plot and a 3D plot of the Rastrigin function.

// // Contour plot // x1 = linspace(-1,1,100); x2 = linspace(-1,1,100); [XX1,XX2]=meshgrid(x1,x2); Z = rastriginV ( XX1 , XX2 ); scf(); xset("fpf"," "); contour ( x1 , x2 , Z' , 5 ); xtitle('Ratrigin Function - Contour Plot','x1','x2'); // // 3D plot // scf(); plot3d(x1,x2,Z'); xtitle('Ratrigin Function - 3D Plot','x1','x2');

## 1.4. Rastrigin plots

The following is a Scilab plot of the contours of the Rastrigin function.

The following is a Scilab 3D plot of the contours of the Rastrigin function.

## 1.5. References

[1] *Rastrigin Function*, http://www-optima.amp.i.kyoto-u.ac.jp/member/student/hedar/Hedar_files/TestGO_files/Page2607.htm

[2] *The Generalized Rastrigin Function*, http://tracer.lcc.uma.es/problems/rastrigin/rastrigin.html

[3] *Global optimization*, Aimo Törn, Antanas Žilinskas, Berlin, New York : Springer-Verlag, 1989