Matlab Introduction

Matlab (which really should be typed in all capitals and with a copyright notice: MATLAB©) is a high-level, interactive system for scientific computing. Matlab started out as a simple interface to the original Linpack library of linear system solvers, and was created by Cleve Moler so that students could quickly tie together routines in the library without spending a lot of time writing drivers and fooling around with complex indexing. The basic data type is a 2D array, explaining the name MATrix LABoratory. Moler has claimed (half-jokingly?) that the matrix is the mother of all data structures, and beginning with version 5.0 the language includes structures and "cell arrays": arrays where the entries can be anything else.

Matlab and PSEs

Matlab is used throughout scientific computing because

These features make Matlab the prototypical problem-solving environment (PSE). PSEs are software systems which provide the complete facilities for solving application problems from a particular domain. A familiar example of a PSE is Microsoft Word - a PSE for document preparation. A user need not be aware of algorithms for line-breaking in Word, but can override those to put a line break where desired. The toolbar in Word presents a high-level, application-specific interface. It can be used interactively, so that a user can see effects of changes immediately (depending on document size, "immediate" could be several minutes) And Word provides the facilities for the full range of activities of document preparation ranging from text entry to printing.

Another common term for systems like Matlab is "rapid prototyping environment", which was its primary use originally. You could use Matlab to test algorithms and ideas quickly and easily; when you got one that worked, you would then implement it in a "real" language like Fortran or C. However, Matlab has evolved in efficiency and ability to interact with the outside world to the point where many scientists and engineers carry out all their work within Matlab.

More specific Matlab features include

Using Matlab

You start up Matlab on a Unix system with the command "matlab" at a command prompt. A header will be displayed, and if you have graphics capabilities set up correctly a "splash window" showing a nifty Matlab 3D graph will show briefly. Then there is a wait while Matlab starts up 27 Java threads. This will occur on the IU burrow cluster machines if you are logged in on the console. To logon remotely to one of those from a Unix-based machine that does not have Matlab, type "ssh burrow" and follow the login procedure. You will automatically be logged in on whichever burrow machine has lightest load. Be sure that you are using ssh version 2 from your console machine; that is what both IUCS and UITS use. You should also use ssh-agent, so that graphic windows can be displayed back on your local computer with having to type in passwords every time.

Matlab's GUI (graphical user interface) uses Java and a Swing window. That means it can be slow to start, especially over a network. If want to start it up in console mode:

matlab -nojvm -nosplash
for pre-2009a versions, and
matlab -nodesktop -nosplash
for 2009+ versions will do that.

Vectors in Matlab

We will start out dealing with vectors, since those are what Matlab's plotting routines use. Consider the problem of generating a 2D plot of n ordered pairs (xi,yi). In Matlab, the way to think of this is plotting a vector y of length n against a vector x of length n, where
   x = (x1, x2, ... , xn)
   y = (y1, y2, ... , yn)

NOTE that the indexing starts from 1, as it does in every field of endeavor except computer science. C/C++ programmers beware of this ... you can complain, but the fields of mathematics and applications science (with several centuries of development and existence) strangely enough expect computer science (with only a few decades of existence) to adapt to 1-based indexing. The rudeness of this is shocking to a C or Java programmer, but we live in an incivil world.

We can type in a vector x via:

>>   x = [1.0  2.1718  3.141  -17] 
and Matlab comes back with
>>   x = 

       1.0000   2.1718   3.1410   -17.0000 

>>
By default, Matlab echos the result of every assignment. You can supress this by appending a semicolon:
>>   x = [1.0   2.1718   3.141   -17];
to which Matlab's snappy comeback is:
>>   
If you want to index into x, you must use round parentheses instead of square brackets. So the last element of x is accessed as

>>   x(4)
or

>>   x(end)
and not as x[4]. Note again that the indexing is 1-based, not 0-based. What happens if you type x[4], by the way?

Now the vector x is set up as a row vector (a 1 × 4 vector). If we had wanted it to be a column vector instead, we could use the other role of a semicolon in Matlab:

>>   x = [1.0;  2.1718;  3.141;  -17]; 
which creates a 4x1 vector. Four points arise now:
  1. If you screwed up and got the wrong orientation for the vector, you can save some typing by using the transpose operator:
            >>   x = x'
            
    which flips x from one to the other. More generally, if A is an m × n array, A' is the n × m transposed array whose (i,j) component is the (j,i) component of A.
  2. Suppose you don't know if you got it right or not. Here is the first and most important debugging command in Matlab: whos. This returns a listing of all variables currently in your workspace, along with their sizes as arrays and in memory usage. Try it now.
  3. Which is more natural, row or column vectors? The answer is "yes". However, linear algebra books typically discuss solving the system Ax = b, where A is an n × n matrix and b is a given vector. So what orientation does this imply for x and b?
  4. Matrices and vectors are mathematical entities, while arrays are computer storage entities. This is a critical distinction which we often blur. Matlab helps blur the distinction even further, since storage is automatically allocated and resized on demand. Note that we simply set the values of the vector x - nowhere did we declare its size or type.

Plotting in Matlab

Now we get to plotting. All of my life I have wondered what the inverse hyperbolic cosine function looks like, and
>> x = [1 2 3 4 5 6 7 8 9 10];
>> y = acosh(x);
>> plot(x,y);
fairly well fulfills my life's dream. Note that acosh is a built-in math function in Matlab .... the interesting thing here is that it works on a vector (more generally, any array) as well as a scalar, applying the acosh() function componentwise. In fact, you can apply just about any scalar function like square root to a matrix, where it operates component-by-component. Mathematically adept students are aware that there is also a square root function for matrices, which gives something different from component-wise square root. In Matlab, matrix functions (as opposed to scalar functions applied componentwise to a matrix) are typically given with a postpended "m", e.g. sqrtm() instead of sqrt().

The plot function creates a graphics window and plots the vector y against x, drawing a line from one (xi,yi) pair to the next.

In general, the plot() function is much more powerful than what is shown above. For example, we can

A title, x-axis label, y-axis, and legend() can be added using title(), xlabel(), ylabel(),and legend(), resp.

In a plot command like plot(x, y), omitting the x will cause the elements of y to be plotted against their index number. When y is a matrix, each column is plotted in this manner, giving c curves if y has c columns.

A Matlab Gotcha

Note that I plotted the acosh() function on the interval [1,10]. We could try the same for the interval [-10,10] via
>> x = [-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10];
>> y = acosh(x);
>> plot(x,y);
But now a warning gets issued: complex components ignored in plot. This is because acosh() is not defined for negative values in the set of real numbers. Matlab will automatically switch to complex variables whenever it is "appropriate". So if you take the square root of -17, Matlab won't blink ... and if you meant to stay in the realm of real numbers, you had better check arguments yourself in your Matlab programs.

  • Started: Mon Oct 7 16:22:07 EST 2002
  • Last Modified: Tue 22 Sep 2009, 03:30 PM