INTRODUCTION TO
M AT L A B
W h a t i s M A Tr i x L A B o r a t o r y ?
•shorter program
development and debugging
It is developed by The •for numerical/symbolic,
It is an interactive, integrated, time
Mathworks, Inc. scientific computations and
environment than traditional
( [Link] other apps.
programming languages such
as FORTRAN and C.
•slow (compared with •automatic memory
FORTRAN or C) because it is management; no need to •intuitive, easy to use.
interpreted. declare arrays.
W H AT IS M AT L AB?
MATLAB & Matrices
VARIABLE NAMES
ARE
Example
% Define the scalar variable x
% row vector
% column vector
% 3x3 matrix
% List of the variables defined
S P E C IAL VA R I ABL ES
SpecialVars.m
OT H ER SY M BO LS
RE L ATIONAL O PE RATORS
M AT H & A S S I G N M E NT O P E R ATO RS
Power ^or .^ a^b or a.^b
Multiplication * or .* a*b or a.*b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a
NOTE: 56/8 = 8\56
- (unary) + (unary)
Addition + a+b
Subtraction - a-b
Assignment = a=b (assign b to a)
Operators.m
M AT LAB LOGIC AL OP E R ATOR S
LO G ICAL FUNCT IO NS
S O M E M AT R I X F U N C T I O N S
E X T R A C T I N G A S U B - M AT R I X
A portion of a matrix can be
extracted and stored in a
smaller matrix by specifying sub_matrix = matrix ( r1 : rn sub_matrix = matrix ( r1 : rn
the names of both matrices , c1 : cn ) ; ,:);
and the rows and columns
to extract. The syntax is:
where r1 and rn specify the
beginning and ending rows
sub_matrix = matrix ( : , c1 : sub_matrix = matrix ( r1 : dr and c1 and cn specify the
cn ) ; : rn , c1 : dc : cn ) ; beginning and ending
columns to be extracted to
make the new matrix.
The terms dr and dc define
matrix.m
spacing different than one.
An M-file might be used as a script, i.e. file consist set of
statements
In additional, one use M-files to write function, in this case
the file starts with function definition like:
function y = f(x)
M-FILES
function [u,v] = f(x,y,z)
File name and the name of function in the file are usually
identical, however while they are different, MATLAB use file
name to call function.
If you add additional function in same M-file, it considered
sub-function and might be called from inside the M-file
only. Only the first function might be called from outside.
S A V I N G R E S U LT S
We can save all our results for future reference.
The command
diary ‘FileName'
saves all output to command window into the [Link] file until this option
is turned off by the command
diary off
The following commands save & load the entire workspace into the file '[Link]'
nsave 'MyMatFile'
nload 'MyMatFile'
nsave '[Link]' x % save a specific variable
saving in ASCII format:
nx = (-1:0.4:1)' ; y = sin(x*pi)
nvar = [x y] % double-column
nsave 'my_sin.dat' -ASCII -double var %Save in 16-digit ASCII format