A
Module 1
D
AD
Introduction
U
VT
Basics of MATLAB
• MATLAB is a software package for high-performance numerical
computation and visualization.
A
• It provides an interactive environment with hundreds of built-in
D
AD
functions for technical computation, graphics, and animation.
U
VT
VT
U
AD
D
A
Will MATLAB Run on My Computer?
• MATLAB is available for Windows, UNIX, Sun Solaris, Linux, and Mac
OS X operating systems.
A
• Older versions of MATLAB are available for additional platforms such
D
AD
as Mac OS and Open VMS.
U
VT
Where Do I Get MATLAB
• MATLAB is a product of The Math Works, Incorporated. Contact the
company for product information and ordering at the following
A
address:
D
AD
• The MathWorks Inc. 3 Apple Hill Drive, Natick, MA 01760-2098
Phone: (508) 647-7000, F ax: (508) 617-7001 Email:
U
[email protected] World Wide Web: http: /
VT
/www.mathworks.com
Basics of MATLAB
• MATLAB Windows:
• On almost all systems, MATLAB works through three basic windows
A
D
• 1.MATLAB Desktop
AD
• 2.Figure window
U
• 3. Editor Window
VT
1. MATLAB Desktop
• Consists of following sub windows
• A) Command window:This is the main window. It is characterized by
A
D
the MATLAB command prompt (>>).
AD
• When you launch the application program, MATLAB puts you in this
window.
U
• All commands, including those for running user-written programs, are
VT
typed in this window at the MATLAB prompt
• B) Current Directory Pane:
• This pane is located on the left of the Command Window in the
A
D
default MATLAB desktop layout. This is where all your files from the
AD
current directory are listed.
• C) (File) Details pane: Just below the Current Directory pane is the
U
Details pane that shows the details of a file you select in the current
VT
directory pane. These details are normally limited to listing of
variables from a MAT-file (a binary data file discussed later) , showing
titles of M- files, and listing heading of cells if present in M-files
• D)Workspace pane: This subwindow lists all variables that you have
generated so far and shows their type and size.
A
• E)Command History pane: All commands typed on the MATLAB
D
AD
prompt in the command window get recorded, even across multiple
sessions (you worked on Monday, then on Thursday, and then on next
U
Wednesday, and so on) , in this window. You can select a command
VT
from this window with the mouse and execute it in the command
window by double-clicking on it.
2. Figure Window
• The output of all graphics commands typed in the command window
are flushed to the graphics or figure window, a separate gray window
with (default) white background color. The user can create as many
A
figure windows as the system memory will allow.
D
• 3. Editor window: This is where you write, edit , create, and save your
AD
own programs in files called M-files.
U
• On most systems, MATLAB provides its own built-in editor. However,
VT
you can use your own editor by typing the standard file-editing
command that you normally use on your system.
Input/output features
• Data type: The fundamental data type in MATLAB is an array. It
encompasses several distinct data objects-integers, doubles (real numbers)
, matrices, character strings, structures, and cells.
A
• Dimensioning: Dimensioning is automatic in MATLAB . No dimension
D
statements are required for vectors or arrays.
AD
• Case sensitivity: MATLAB is case-sensitive; that is, it differentiates between
the lowercase and uppercase letters.
U
• Output display: The output of every command is displayed on the screen
VT
unless MATLAB is directed otherwise. A semicolon at the end of a
command suppresses the screen output.
• Command history: MATLAB saves previously typed commands in a buffer.
These commands can be recalled with the up-arrow key
File types
• M-files are standard ASCII text files, with a .m extension to the
filename. Most programs you write in MATLAB are saved as M-files.
A
• Mat-files are binary datafiles, with a . mat extension to the filename.
D
Mat-files are created by MATLAB when you save data with the save
AD
command
• Fig-files are binary figure files with a .fig extension that can be
U
VT
opened again in MATLAB as figures. Such files are created by saving a
figure in this format using the Save or Save As options from the File
menu or using the save as command in the command window. A fig-
file contains all the information required to recreate the figure. Such
files can be opened with the open filename . fig command.
• P-files are compiled M-files with a .p extension that can be executed
in MATLAB directly (without being parsed and compiled) . These files
A
are created with the pcode command. If you develop an application
D
that other people can use but you do not want to give them the
AD
source code (M- file) , then you give them the corresponding p-code
U
or the p-file.
VT
Creating and Working with Arrays of Numbers
• An array is a list of numbers or expressions arranged in horizontal
rows and vertical columns.
A
• When an array has only one row or column, it is called a vector.
D
AD
• An array with m rows and n columns is called a matrix of size m x n.
• Array operations
U
VT
• ·* term-by-term multiplication,
• ./ term-by-term division,
• . ^ term-by-term exponentiation.
• >> x= [1 2 3]
• X=
A
D
1 2 3
AD
• >> y=[2;5;3]
U
• Y=
VT
2
5
3
• >> z= [2 1 0];
• >> a= x + z
A
a=
D
3 3 3
AD
>> b= x + y
U
??? Error using ➔ plus matrix dimensions must agree
>>a= x.* z VT
a=
2 2 0
• >> b= 2*a
A
• b=
D
• 4 4 0
AD
U
• >> A=[ 1 2 3 ; 4 5 6 ; 1 1 1]
VT
•A
1 2 3
4 5 6
1 1 1
• >> A(2,3)
• ans=
A
D
6
AD
U
VT