Using MATLAB in the PC Lab

You are expected to use the computers found in the PC lab on the 2nd floor of the Engineering building. This document describes how to find the relevant software and documentation, providing the initial pointer. Most documentation (including this document) is found "on the web."

The Web

The World Wide Web (or simply the Web) is a collection of documents distributed over the world that are linked using what are known as hypertext links. A person can read a document and click (using the mouse, specifically the left button) on specially highlight words or phrases to automatically go and read another, hopefully related, document. Special computer tools, known as browsers are used to interact with the Web. You have access to Netscape, which you are probably using to read this document.

Starting MATLAB

MATLAB can be found in the Chem-Eng Applications group in the Windows Program Manager. When you open MATLAB you can see that it is possible to enter commands directly into the MATLAB workspace. For example, typing

>> 2*2

gives the output

ans = 
     4

However, for this exercise you should write your program as a script file. A script file is simply a sequence of commands that could have been entered interactively at the command prompt. It is a much more efficient way of entering MATLAB code if the sequence of commands is long, or if the program needs to be executed a number of times. To do this you must first tell MATLAB which directory you wish to use. This is done by typing:

chdir r:\dos\windows

or if you have created a MATLAB directory (using file manager for example)

chdir r:\matlab

You must do this each time you enter MATLAB.

A Simple Program

To create your program, from the file menu select New: M-File. This will open a Notepad window where you can create your first MATLAB program. Type, exactly as shown, the following program:
% My first program
  x = 4.0;
  y = 3*sqrt(x) - 7
% End of program

This is an example (albeit a very simple one) of a MATLAB program. You will be writing many more during the course of the rest of your degree! Save the result (which must be exactly as shown above) into a file called myprog.m. We will be using this program in the next section.

You should notice the use of the semicolon (;) at the end of the first line, this suppresses MATLAB printing this line to the screen. Often we only want to know the final answer from the program not the intermediate steps taken. Therefore, you will be using the semicolon (;) at the end of most lines in your programs.

Running MATLAB Programs

Running a MATLAB program is very straight forward, simply type the program name at the command prompt, in this case, type myprog. The commands you have listed in your file will be executed sequentially. If there are any errors in your file, MATLAB will tell you which line the error is on, and indicate what type of error has occurred.

If you get the answer y = -1 then you have successfully completed this first exercise.