Lecture 1

Getting Matlab to run

On your own laptop...you're on your own...

On Athena type (without the athena% prompt)


athena% add matlab
athena% matlab

It takes about 30-60 secs to start up... and you'll get a screen

                             < M A T L A B >
                  Copyright 1984-2006 The MathWorks, Inc.
                         Version (R2006a)
                              January 27, 2006

  To get started, type one of these: helpwin, helpdesk, demo,
  help help, whatsnew, info
  For Athena-specific information, type: help athena
  For product information, visit www.mathworks.com
  To start the Matlab Desktop, type: desktop
>> desktop



Alternatively, you can type

athena% add matlab
athena% matlab -desktop



The most awful truth about computers is that they are all dumb. Everything must be explicitly told. Spelling out the most minor and obvious detail. Otherwise, you will either get a syntax error (computer not understanding what you are saying) or a programming error (the computer understanding what you are saying but it is different from what you mean).

There are many programing languages. Why use Matlab? As a first programming language, it is great, it has simple syntax, it is easy to get started with and easy to do graphics. While other programming languages are faster running they are slower to learn and to use.
In addition, Matlab, as an interpreted language, is interactive, which allows for more exploratory programming. Once the exploration is done, one should consider rewriting the program in a different language if intensive use is required.


The Command Prompt

In the interactive terminal of Matlab you will see the command prompt (>>). This is Matlab's way of telling you that it is ready to receive instructions from you. To command Matlab, type your request and hit Enter. Matlab then evaluates your command and displays the output (if any).


To get help use the help, and lookfor commands. help should be used to find out information about a known command, e.g. help zeros.
lookfor can be used to find a command to perform a task e.g. lookfor roots.

If you do not get the command prompt for some reason, you may have typed a syntax error, or have put Matlab into an infinite loop. Don't panic! press Ctrl-C and you'll get the prompt back.


  • Learn about the commands ones, zeros, sum, and diag. 
  • Learn about magic and verify what you learned with sum.

Simple Expressions

Matlab's most basic building blocks are expressions: 1, 1+1,4*3, 4^2, 5/6.
But Matlab's real strength is in handling matrices and vectors. To enter a vector we use brackets. Try the following examples to see what happens: [1 2], [1:10], [1, 2 3 4; 5 6, 7 8 ; 9 10 11 12]
Notice that the colon is used to create an arithmetic sequence. We can also create a sequence with a step-size different from 1: [4:0.1:5], [5:-2:-5].

To transpose a matrix (or vector) use the apostrophe ('):

>> [1:10]'


Try to solve these with a single Matlab command:

  • Create the vector consisting of the whole even numbers between 15 and 27.
  • Create the vector consisting of the whole odd numbers between 15 and 27.
  • Create the vector of the previous question in decreasing order.


Asking Matlab to calculate things for us is great, but most of the times a calculation is only one step out of many and the result of a calculation need to be saved for the (very near) future. To do this we use 'variables', which are simply a way to name a bit of Matlab data. This is best explained by example:

>> a=1

puts the value 1 into a variable called a. typing 


>> a

results back in the value 1.

We can also assign a calculation into a variable:

>> b=1+1


and now b will contain the answer, 2.

Variable names can (and should) be longer than one letter. A word or a couple of words strung together tends to work the best.

Referencing matrix elements

We can access the elements of a matrix in the following manner:


>> a=[1 4 3 5 6]

>> a(4)


>> b=[1 2 ; 3 4]

>> b(2,2) 


Where the numbers in the parenthesis are (row, column).