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.
% 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 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.