MATLAB Lesson 0 - Using MATLAB

Starting MATLAB

The MATLAB logo, which is trademarked by the MathWorks, is similar to the image below of an L-shaped membrane:

MATLAB logo

MATLAB is installed on both the Windows and Linux computers within the School of Mathematics and Statistics. It does not matter which operating system you are using.

On a Linux computer you start MATLAB by clicking on the MATLAB logo on the task bar across the bottom of the screen.

On a Windows computer you start MATLAB by double clicking on the MATLAB logo on the desktop.

When you first start MATLAB you should see the following group of windows.

MATLAB initial windows

The Command Window

The most important window is the Command Window (the one in the middle), in which you type MATLAB commands and see the results of your commands.

You will also see windows labelled Current Folder, Workspace, Command History and Details. The Current Folder window shows that MATLAB is working in a folder containing the files cardioid.m, cubic.m, ..., sinplots.m.

You can customise your settings using the Preferences menu in the Environment section of the Home tab. Initially it is simplest to use MATLAB's default settings.

When you have decided what is the most effective way for you to work, you may close some or all of these windows. However you will always need a Command Window. In the computer laboratory MATLAB saves its configuration when you exit. MATLAB will start with the saved configuration the next time you use it.

Examples

To see how MATLAB works, try typing the following commands in the MATLAB Command Window. You will see more about arithmetic in Lesson 1.

You do not need to type the MATLAB prompt

>> 
which just indicates that MATLAB is waiting for you to type a command.

Type the following two lines in the MATLAB Command Window, with Enter after each command.

This assigns the value 2 to the variable x.

This uses the value stored in the variable x, evaluates the expression on the right-hand side of the =, and then stores the new value (1.5000) in the variable x.

>>  x = 2
>>  x = x/2 + 1/x




MATLAB is an interpreted language

MATLAB is an interpreted programming language. That means each command is evaluated when you press the Enter key. This has the major advantage of providing results immediately and allowing you to interact with MATLAB.

There are several ways of going back to a previous command, editing it to correct any mistakes if required, and then executing it again.

The simplest way to do this is to use the keyboard arrow keys: The up arrow goes back to the previous command (press more than once to go back several commands), while the left and right arrow keys position the cursor within the command, and the Backspace or Delete keys remove a character which can then be re-typed. Pressing the Enter key will execute the command.

If you type a few characters from the the beginning of a command, for example de, and then press the up arrow key, you go back to the most recent command that started with these characters.

Alternatively you can find the command you want in the Command History sub-window, and double click on it to execute it again. You may also copy commands from the Command History sub-window using the menu opened when you right-click.

Use the arrow keys to go back to the previous command and execute it again (you do not need to change the command). Repeat this several times.

The up arrow key goes back to the previous command, then Enter executes the command.

The new value stored in x is approximately 1.4167

Repeatedly executing this command causes the value of x to converge to the square root of 2 = 1.4142....

>>  x = x/2 + 1/x
>>  x = x/2 + 1/x







Self-test Exercise

Use the arrow keys to edit the expression in the example to change the 1/x to 2/x. What do you get if you execute this several times?

Answer:
x = x/2 + 2/x
Repeatedly executing this expression gives the value 2. (Smarter ways to repeatedly execute a command will be seen in Lesson 8.)

Use the mouse to select the text between the word "Answer" and here to see the answer.

Summary

MATLAB has a number of windows which you can customise to suit how you work.

There should always be a Command Window where MATLAB commands are typed and results are displayed.

Pressing Enter executes a command.

The arrow keys allow you to go back to and edit a previous command.