# Introduction to programming in MATLAB®Octave by yrs83496

VIEWS: 49 PAGES: 19

• pg 1
```									1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

Introduction to programming
in MATLAB®/Octave

1st Workshop on Computational Biology

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 1
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

0. Outline

I.       What is programming?

II.      General notes on MATLAB® and Octave

III.     Getting started with MATLAB®/Octave

IV.      Comments on the Octave tutorial and the exercises

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 2
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

I. What is programming?

Wikipedia)

• Computer programming (or often simply programming or coding) is the process of writing,
testing, and maintaining the source code of computer programs.

• Source code (or simply source or code) is any sequence of statements and/or declarations
written in some human-readable computer programming language..

• The process of writing source code requires expertise in many different subjects, including
knowledge of the application domain (e.g. Biology), specialized algorithms, and mathematics.

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 3
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

I. What is programming?

Algorithm (from lat.: Algorismus, calculation method, “Algoritmi de numero Indorum”)
(from       Algorismus,                                                  )

An algorithm is a ﬁnite list of deﬁnes what to do
recipe that well-deﬁned instructions
under which circumstances that, given an initial state,
will terminate in a deﬁned end-state.

a problem in a standardized manner that can be translated
into a computer program.

A programming paradigm is a fundamental style of programming regarding how
solutions to problems are to be formulated in a programming language.

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 4
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

I. What is programming?

A programming paradigm provides (and determines) the view that the programmer has
of the execution of the program. Often it is tightly coupled with a certain programming
language:

• Imperative programming describes computation as statements that change a program
state. Imperative programs are a sequence of commands for the computer to perform.
Programming languages that support the imperative paradigm are e.g. FORTRAN, C,
Pascal, C++.

• In Object-Oriented programming (OOP), programmers can think of a program as
a collection of interacting objects (JAVA, C++, Smalltalk).

• In functional programming a program can be thought of as a sequence of
stateless mathematical function evaluations (Mathematica, R, Scheme).

• Array programming (also known as vector or multidimensional languages)
generalize operations on scalars to apply transparently to vectors, matrices, and
higher dimensional arrays for use in numerics. (MATLAB, Octave, FORTRAN90)

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 5
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

II. General notes on MATLAB® and Octave

MATLAB®

• MATLAB is a numerical computing environment and programming language

• Invented by Cleve Moler in the late 70ties at the University of Mexico

• MATLAB is a commercial product of The MathWorks company (co-founded by
Moler in 1984).

• MATLAB (MATrix LABoratory) allows easy matrix manipulation, plotting of functions
and data, implementation of algorithms,creation of user interfaces, and interfacing
with programs in other languages.

• Specialized in numerical computing, it is no computer algebra system!

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 6
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

II. General notes on MATLAB® and Octave

MATLAB®

• Structured in so-called Toolboxes. A MATLAB toolbox is a collection
of functions that are of special need in a speciﬁc scientiﬁc ﬁeld, e.g.
Image processing Toolbox, Statistics Toolbox, Bioinformatics Toolbox
Optimal control Toolbox

• MATLAB/Simulink is a built-in a software
package for modeling, simulating, and
analyzing dynamic systems including a
powerful GUI.

• MATLAB is a proprietary product of
The MathWorks, so users are subject to
vendor lock-in. Some other source
languages, however, are partially compatible                                                 MATLAB Graphical User Interface
and provide a migration path, such as Octave.

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                        Slide 7
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

II. General notes on MATLAB® and Octave

Octave

• Octave is a free computer program for performing numerical computations which is
mostly compatible with MATLAB. It is part of the GNU project and under the GNU
(GNU’s Not Unix) General Public License.

• The project was conceived around 1988 (within the teaching of a chemical reactor
design course). Real development was started by John W. Eaton in 1992.
(Version 1.0 in 1994).

• Octave interpreter works in tandem with Gnuplot
and Grace software to create plots, graphs,
and charts, and to save or print them

• GNU Octave available with Octave
Workshop GUI for Windows

- Koctave available as Octave GUI for Linux

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 8
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

• Highlight of special features in MATLAB/Octave

• Focus on basic programming concepts with the help of the MATLAB/Octave language
syntax

• Practical exercises follow this lecture

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                      Slide 9
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Octave as a calculator

• When starting Octave nothing else than a prompt appears:

• The basic arithmetic operators are + - * /, and ^ is used to mean “to the power of “
(e.g. 2^3=8). Brackets ( ) can also be used.
• When typing:

we get

• Octave, however, allows you to deﬁne and use named variables, i.e. you can assign
a value to a variable:

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 10
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Everything is a Matrix

• In reality, our previous variable “deg” is not represented as a number but
is a 1x1 matrix! Because everything in MATLAB/Octave is a matrix.

• The ‘ -operator indicates matrix transposition in MATLAB/Octave.

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 11
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Everything is a Matrix

• REMEMBER the colon operator “:”

• Some built-in function for matrix initialization

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 12
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Help and built-in functions

• Octave (and above all MATLAB with the
Toolboxes) contains a lot of predeﬁned
functions, e.g. mathematical functions

• Therefore it is crucial to make extensive use
of the help function, e.g.

• For general search type help -i at the prompt

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 13
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Matrix operations

• All arithmetic operators + - * /, and ^ are actually deﬁned on matrices, i.e. they
follow the rules of Linear Algebra!

• Element-wise multiplication and division is deﬁned as .* and ./

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 14
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Logical and Control statements

If-statement                                 For-loop                                  While-loop

if(temperature >= 30)                         N = 10;                               x = 0.01;
hot = true;                              f = 1;                                e = x+log(x);
end
for i = 1:N                           while(abs(e) > 0.001)
if(hot && wind)                                   f = f * i;                           x = x - 0.1*e;
go sailing;                               end                                      e = x+log(x);
elseif(hot && ~wind)                                                                end
go swimming;
else
attend workshop;
end

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 15
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Matrix computation vs. Control statements
Consider the following problem. You want to compute the distances between
some points that lie on the x-axis in a cartesian coordinate system. The points positions
are conveniently stored in a column vector p. What we seek is the distance matrix
Dij = || pj pi || .
A straightforward way is to program two nested for-loops. With the outer loop we move
over indices i and with the inner over j . By this we ﬁll the elements of D one by one:

For-Loop                                                         Matrix computation
p = [ -1 0 1 2 3 4]’;                                                   p = [0 1 2 3 4 5]’;
[n,m] = size(p);                                                        [n,m] = size(p);
D = zeros(n,n);                                                         P = repmat(p,1,n);
for i = 1:m                                                             D = abs(P’ - P);
for j = 1:m
D(i,j) = abs(p(j)-p(i));
end
end

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 16
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Plotting

• There are mulitple commands available that deal with plotting, e.g. plot:

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 17
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

III. Programming in MATLAB®/Octave

Scripts and Functions

• If you have a series of commands that you will want to type again and again, you can
store them away in a Octave script. This is a text ﬁle which contains the commands,
and is the basic form of a Octave program, e.g.

• A function is deﬁned in an text ﬁle, just like a script, except that the ﬁrst line of the
ﬁle has the following form:

function [output1 ,output2 ,...] = name (input1 ,input2 ,...)

Each function is stored in a different M-ﬁle, which must have the same name as the
function.

Christian Lorenz Müller, Computational Biophysics Lab, Institute of Computational Science
Müller,                                                                                     Slide 18
1st Workshop on Computational Biology                                              Introduction to programming in MATLAB®/Octave

IV. Comments on the course material and the exercises

• Today there will be the “Getting started with Octave” part.

• Friday will be dedicated to the implementation of two numerical methods, Random
Walk and Particle Strength Exchange.

• Saturday is dedicated to the simulation of
reaction-diffusion systems.

• The purpose of the exercises is NOT to solve
everything but to learn SOMETHING.

• We can provide you with a solution to the