# Introduction to Matlab/Octave

Today I aim to
• Demonstrate what matlab can do
• Talk about the ways it's used
• Show some under-used features

## Introduction

MATLAB is a high performance interactive software package for scientific and engineering computation. It's big (2.5M lines of C, 1M lines of Java, 0.5M lines of Fortran and about 2.5M lines of its own scripting code). It combines numerical analysis, matrix computation, signal processing and graphics in an easy-to-use environment where problems and solutions are expressed just as they are written mathematically. Everything's a matrix (sound, images, simultaneous eqs)

• Examples of simple matrix use -
• A=1:10, A*2, A', A*A, A*A', A.*A, sin(A)
• "for" loops exist, but often aren't needed. Commands like sum(A), cumsum(A), fliplr(A), etc exist, and you can use vectorisation (add all the numbers >5 in A)
• Many other routines - eig(magic(5)), etc.
• Graphics - plot(sin(A)) (note that you have to calculate the coordinates - plot(sin) doesn't work), peaks, view(2), view(3)
• Commands can be put into files, and the files can be run (".m files"). As well as sequences of commands you can use for loops, functions, etc, so you can write Basic/F77-style programs. You can even write Object-Oriented code in the style of Java/C++
• Different type of commands - built-ins, Mathwork scripts (.m files). Use which to find the location of a function.

## Octave

Octave is a free clone of Matlab. It's available on our Teaching System. It can do most of the numerical work of matlab, including all the above. However, it doesn't (yet) have a comparable graphics capability, either for output or input. Octave can only do very basic plotting. The rest of the examples can only be done in matlab.

Mathematica is a commercial competitor to Matlab.

## Toolboxes

Toolboxes are libraries of extra routines. Use ver to find out the installed ones. The Symbolic Toolbox is useful. E.g.

• solve('x^2=1')
• x=sym('x'); int(1/(1+x^2),x)
• x=sym('x');diff(tanh(x)/(1+x^3)^2/x*tan(x^2)*sin(x^3+5)/sqrt(cos(x)),x)
• y=sym('y'); factor((x^3-y^3)/(x^4-y^4))

With the Symbolic Toolbox installed, commands like ezplot('sin(x)') become possible.

## More Graphics

• Object-orientated - set/get (EdgeColor, FaceColor)
• animation - travel, vibes
• Easy to add buttons - spectra, mohr_circle

## Use with other languages

• You can write your own C++/f90 routines and call them from Matlab
• You can use matlab as a maths/graphics library, calling it from C++/f90

## Help

See the Matlab page for introductory documents, Matlab Central, Help, Discussion board, etc. There are many books about Matlab. Mathworks' online Getting Started tutorial may be all you need.

## What Next?

Try Matlab by Example or Getting to know Matlab (less technical) or the 2nd year Octave tutorial.

• © Cambridge University, Engineering Department, Trumpington Street, Cambridge CB2 1PZ, UK (map)
Tel: +44 1223 332600, Fax: +44 1223 332662