Mat Lab Intro
Mat Lab Intro
Mat Lab Intro
by Michael Medvinsky(2014)
1
MATLAB
MATLAB is a program for doing numerical computation.
It was originally designed for solving linear algebra type
problems using matrices. It’s name is derived from
MATrix LABoratory.
Scalars are matrices with only one row AND one column
Variable Names
Variable names ARE case sensitive
Variable names can contain up to 63 characters (as of MATLAB 6.5
and newer). One can use namelengthmax command to
verify it.
Variable names must start with a letter followed by letters, digits,
and underscores.
MATLAB variables are defined by assignment. There is no need to
declare in advance the variables that we want to use or their type.
Example
x=1; % Define the scalar variable x
y=[1 2 3] % row vector
z=[1;2;3] % column vector
A=[1 2 3;4 5 6;7 8 9] % 3x3 matrix
whos % List of the variables defined
Note: terminate statement with semicolon (;) to suppress output.
Special Variables
ans Default variable name for results
pi Value of π
eps Smallest incremental number
inf Infinity
NaN Not a number e.g. 0/0
i,j,1i,1j imaginary unit i, i.e. square root of -1
realmin The smallest usable positive realnumber
realmax The largest usable positive real number
SpecialVars.m
Other symbols
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
Relational Operators
MATLAB supports six relational operators.
- (unary) + (unary)
Addition + a + b
Subtraction - a - b
Assignment = a = b (assign b to a)
Operators.m
MATLAB Logical Operators
sub_matrix = matrix ( r1 : rn , c1 : cn ) ;
sub_matrix = matrix ( r1 : rn , : ) ;
sub_matrix = matrix ( : , c1 : cn ) ;
sub_matrix = matrix ( r1 : dr : rn , c1 : dc : cn ) ;
matrix.m
“Continuous” functions
Numerically, we cannot represent a general continuous function
(x,f(x)) because it requires handling infinite data (for each point in
the range, we need to keep f(x)). Instead, we represent a function
by its values at a finite number of data points (x_i,f(x_i)), where the
series of points {x_i} is typically referred to as the sampling points or
the grid points. Accordingly, the "continuous" functions in Matlab
accepts a vector of point {x_i} and return a vector of values {f(x_i)}.
Some functions
sqrt
log
cos/acos/sin/asin etc
exp - exponential
abs
sign
norm
sum
prod - product
Plotting
MATLAB will plot one vector vs. another. The first one will be
treated as the abscissa (or x) vector and the second as the ordinate
(or y) vector. The vectors have to be the same length.
MATLAB will also plot a vector vs. its own index. The index will be
treated as the abscissa vector. Given a vector “time” and a vector
“dist” we could say:
>> plot (time, dist) % plotting versus time
>> plot (time + i*dist) % plotting versus time
>> plot (dist) % plotting versus index
Sometime we want to see it with different color\line stile
>> plot (time, dist, line_characteristics)
And sometime we want to plot few functions in graphs
>> plot(…), hold, plot(…)
>> plot(t,d1,l_c1, t,d2, l_c2)
To split page to several axes check use
>> subplot (rows, cols, place)
Plotting
There are commands in MATLAB to "annotate" a plot to put on axis
labels, titles, and legends. For example:
To put a label on the axes we would use:
>> xlabel ('X-axis label')
>> ylabel ('Y-axis label')
plotting.m
Flow control (condition)
An if - elseif - else structure. (Note that elseif is one word)
if expression1
statements1
elseif expression2
statements2
else
statements3
end
An switch-case structure
switch switch_expr
case case_expr
statement, ..., statement
case {case_expr1, case_expr2, case_expr3, ...}
statement, ..., statement
otherwise
statement, ..., statement
end
Flow control (loops)
A while loop in
while x <= 10
% execute these commands
end
M-Files
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)
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.
Saving Results
We can save all our results for future reference.
The command
diary ‘FileName'
saves all output to command window into the FileName.txt file until
this option is turned off by the command
diary off
The following commands save & load the entire workspace
into the file 'MyMatFile.mat'
save 'MyMatFile'
load 'MyMatFile'
save 'x.mat' x % save a specific variable
saving in ASCII format:
x = (-1:0.4:1)' ; y = sin(x*pi)
var = [x y] % double-column
save 'my_sin.dat' -ASCII -double var %Save in 16-digit ASCII format
MATLAB also have humor