5. Introduction to MATLAB/octave

MATLAB is a powerful, widely used, "desktop environment tuned for iterative analysis and design processes" and "a programming language that expresses matrix and array mathematics directly." It is a commercial product and requires an [expensive] license to run, although student discounts do exist.

GNU Octave (in short, octave), is a free-software alternative to MATLAB. Its interpreter is mostly compatible with MATLAB and reproduces a large part of the numerical facilities of MATLAB. It is available as both a command-line interpreter and as a GUI-driven programming environment. As free software it has acquired a broad distribution in the scientific community. In particular, it is available on vastly parallel computing farms dedicated to scientific computing (such as SHARCNET), which makes it the environment of choice for many physicists. It is also available both as a standalone kernel and as a loadable extension within jupyter notebooks; these are installed on our cluster.

This notebook is, in fact, running under an octave kernel (see in the upper right corner).

As the two environments are almost 100% syntax-compatible, we will always refer to the code as octave, but almost everything will also be applicable to MATLAB environments.

Matrices and vectors

One of the important quirks of octave syntax is that its elementary object is a matrix. If in eXtrema, separate types exist for vectors and matrices, octave takes the view of vectors being a particular kind of a matrix, with one of the dimensions being of size 1. Thus it distinguishes between column and row vectors and many errors you encounter will likely have to do with failures to keep track and convert between the two types of vectors as appropriate.

Here are a few different ways of creating and manipulating matrices and vectors.

Elementary operations and interactions with octave

There is no need to reproduce the essential introduction, as it exists:

Students are expected to work through the tutorial on their own, and learn how to use and search the online documentation. octaveBasic must be completed now, other parts of the tutorial will be reviewed later as needed, and completed as homework.

A crucial part for us is the ability to read in, analyze and plot experimentala data. The rest of this notebook is dedicated to learning how to do this.

The basic operation for this simple file is trivial: we know it's a text file, with one header line and two columns of floating-point data:

In general, though, octave has multiple ways of bringing in data, and can deal with a variety of file formats, some highly structured (like spreadheet files, for example). When little is known about the format of the data file, importdata() is a useful tool. After a little digging through the file structure, the same result can be obtained: two column vectors, V and I.

Homework

Work your way through the remaining sections of the octave tutorial.

Convert one of previously obtained graphs (the analysis of data from VI.dat or Cavendish.dat) from gnuplot or eXtrema to octave. Demonstrate how to display multiple frames (one above or beside the other), annotate your figures clearly to communicate the same information (maximum of power, and the resistance at which this maximum occurs, or the period of oscillations, etc.).