To quit from MatLab, type ``quit'' at the MatLab prompt, i.e.
>> quit
>> piMatLab responds with
ans = 3.1416If you need to read more decimal digits, type format long and then press the return key. Now type pi again. Type format short to return back to 5 decimal digits. pi is a built-in MatLab variable. The preceding command is actually a request for MatLab to print the value of this variable.
Try the following examples
>> sin(pi/4) >> 2^(log2(4)) >> sqrt(9)
>> x = 5 x = 5 >> y = pi/4 y = 0.7854 >> z = y + x^0.25 z = 2.2807Note that z exists only as a numerical value, not as the formula with which it was created.
Use the who command to list the currently active variables. For the preceding session this results in
>> who Your variables are: ans x y z leaving 7433616 bytes of memory free.To print the value of a variable that is already defined just type its name at the prompt.
>> z z = 2.2807Now type what and why to see if you can figure out the uses of them.
You can also create your own functions. To do this you type MatLab commands into a plain text file. The file must have the extension ".m", and because of this MatLab functions are often referred to as "m-files".
MatLab also has some special variables and functions. You have already encountered the ans variable which is automatically created whenever a mathematical expression is not assigned to another variable.
The built-in eps variable is used as a tolerance for internal calculations. Initially eps is equal to machine epsilon. You can reset the value of eps, but this is not recommended unless you are very sure you know what you are doing.
The realmax, realmin, Inf and NaN built-in variables are used to handle floating point exceptions. All numerical variables in MatLab are stored as 32 bit floating point numbers. This corresponds to ``double precision'' on most computers (supercomputers and some high-end workstations being the exception). The Inf and NaN values appear if a floating point exception occurred during the calculations.
>> helpTo get help on a specific topic, for example the cosine function, type
>> help cosUsing help is one way to learn about the variety of built-in functions. The help command is most useful when you know the name of the function, but are unsure how to use it. You can also get help by clicking on help at the top of the MatLab window. The table of contents lists all the functions. The MatLab manuals provide cross-referenced and indexed descriptions of all aspects of using MatLab .
To see how the semicolon works enter the following statements exactly as shown, ending each line with a carriage return. Do not enter the prompt character, >>.
>> x = pi/3; >> y = sin(x)/cos(x); >> z = y - tan(x); >> z >> yThe last two lines do not end in semicolons so MatLab prints the results of evaluating z and y. The values of z and y were assigned in the y = and z = statements. The last two lines merely printed the values of the expressions z and y.