Professional Documents
Culture Documents
Introduction to MATLAB
MATLAB, which stands for MATrixLABoratory, is a state-of-the-art mathematical software package,
which is used extensively in both academia and industry. It is an interactive program for numerical
computation and data visualization, which along with its programming capabilities provides a very
useful tool for almost all areas of science and engineering. Unlike other mathematical packages, such
as MAPLE or MATHEMATICA, MATLAB cannot perform symbolic manipulations without the use
of additional Toolboxes. It remains however, one of the leading software packages for numerical
computation.
3. Demos
Demonstrations are invaluable since they give an indication of MATLAB’s capabilities. A
comprehensive set is available by entering demo on the command prompt.
The “e” notation is used for very large or very small numbers.
Page 2 of 5
How MATLAB prints numbers is controlled by the format command and use help format
command to view help associated with this command. Following table lists how this command
can be used.
7. Suppressing output
The output of the result of a calculation can be suppressed using the semicolon. Try these.
a. >> k=34 c. >> k=67;
b. >> k d. >> k
8. Useful functions
a. >>clc (Clear command window)
b. >> who (List current variables)
c. >>whos (List current variables, long form)
d. >> clear (Clear variables and functions from memory)
9. Vectors/Arrays
An array is a combination of elements and its length is specified by the number of elements it has.
Elements should be enclosed in square brackets. Arrays can be defined in these ways. Identify the
dimension of these arrays.
a. >>a=[1 2 5 7] c. >> c=[3 4 5; 5 2 5]
b. >> b=[-6 5 sqrt(9)] d. >> d=[1 -2; 5 6; 3 -8]
The function length is used to determine the length of an array. Note that, to do some arithmetic
operations, the dimensions of arrays should agree.
Page 3 of 5
c. >>5:-5 e. >>1:0.1:2
d. >>5:-1:-5 f. >>0:-0.2:-1
The command save can be used to save the current values of all variables to a file. The command
save currentsessionwill create a file called currentsession.mat and store all the variable on that.
The command load can be used to invoke the saved parameters back to the session again.
The command stem is used to plot discrete-time functions. Each data point is
indicated by a small circle at the end of a line connecting it to the horizontal axis. The
instruction sequence,
»t = 0:1/32:2 ; x = sin(2*pi*t) ;
»stem(t,x) ;
Page 4 of 5
15. MATLAB loops
The following loop structures can be used on MATLAB.
a. for loop
b. while loop
c. switch..case
Page 5 of 5