Lab 01: Introduction to MATLAB
(Based on Matlab Manual)
What is MATLAB?
The name MATLAB stands for "MATrix LABoratory" and was originally designed as a tool for doing numerical computations with matrices and vectors. It has since grown into a high-performance language for technical computing. MATLAB, integrating computation, visualization, and programming in an easy-to-use environment, allows easy matrix manipulation, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs in other languages. Typical uses include:
- Math and Computation
- Modeling and Simulation
- Data Analysis and Visualization
- Application Development
- Graphical User Interface development
MATLAB development IDE can be launched from the icon created on the desktop. The main working window in MATLAB is called the desktop. When MATLAB is started, the desktop appears in its default layout:
The following tools are managed by MATLAB desktop, although not all of them appear by default when you first start
- Current Folder - This panel allows you to access the project folders and files.
- Command Window - This is the main area where commands can be entered at the command line.It is indicated by the command prompt (>>).
- Workspace - The workspace shows all the variables created and/or imported from files.
- Command History - This panel shows or rerun commands that are entered at the command line.
By default, the Command History window closes after a statement is selected or the Esc key is pressed. To keep the Command History window open, in the Command History window, click and then select either Detach or Dock. If the Command History window is closed while detached or docked, go to the Home tab, and in the Environment section, click Layout. Then, under Show, click Command History and select either Docked or Popup.
MATLAB does not require a command to declare variable. A variable is created simply by directly allocating a value to it. All variables are created with double precision unless specified and they are matrices.
>> v = 3
After these statements, the variables are 1x1 matrices with double precision.
>> 23 + 18 # no declarations needed
>> product = 18 * 32.64 # mixed data type
>> product = 18 * 555.24; # semi-colon suppresses output of calculation's result
MATLAB Variable Names
- Variable names are CASE SENSITIVE.
- Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer).
- Variable names must start with a letter followed by letters. digits, and underscores ( _ ).
- MATLAB has several keywords that cannot be used as programmer-defined variable names.
The following table provides a list of keywords. The list of keywords can be found by entering the command "iskeyword" on the Command Window.
Special Variables and Constants
|ans||Temporary variable containing the most recent answer. If you do not assign an output variable to an expression, MATLAB automatically stores the result in ans.|
|eps||This variable name is short for “epsilon”. It is the smallest difference between two numbers that can be represented on the computer. (Smallest incremental number)|
|i, j||The imaginary unit|
|inf||Infinity. Calculations like n/0, where n is any non-zero real value, result in inf.|
|NaN||Not-a-Number, indicates an undefined numerical result. Expressions like 0/0 and inf/inf result in a NaN, as do arithmetic operations involving a NaN. n/0, where n is complex, also returns NaN.|
|pi||The number ( = 3.1415926535897...)|
|realmin||The smallest usable positive real number.|
|realmax||The largest usable positive real number.|
|version||MATLAB version string.|
|clock||This special variable contains the current date and time in the form of a 6-element row vector containing the year, month, day, hour, minute, and second.|
|date||Contains the current data in a character string format, such as 24-Nov-1998.|
Here are several examples that use these values in MATLAB expressions.
>> x = 2 * pi;
>> A = [3+2i 7-8i];
>> tol = 3 * eps;