Introduction To MATLAB
Introduction To MATLAB
Introduction To MATLAB
It was originally designed for solving linear algebra type problems using
matrices
It’s name is derived from
MATrix LABoratory
It has many specialized toolboxes and built-in programs for making things
easier for us
MATLAB MAIN SCREEN
Command Window: is
where you'll type commands
and give MATLAB its input
and view its output
Current
Current Directory : Editor
Directory Command
shows folders and m-files History
Command History:
shows past commands
Editor : is a text Current
Editor
Directory Command
editor where you can History
load, edit and save
Matlab programs. Command
Workspace
Window
VARIABLE
Variable names are case sensitive
Variable names can contain up to 63 characters (as of MATLAB R2021a). 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.
All variables are created as matrices with “some” type (unless specified)
Example
x=1; b=false; c=‘Hello World!’
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 imaginary unit i, i.e. square root of -1
realmin The smallest usable positive real number
realmax The largest usable positive real number
ARITHMETIC OPERATIONS
addition + a + b
subtraction – a - b
division / a/b = b\a
multiplication * a*b
power ^ a^b
Assignment = a = b (assign b to a)
LOGICAL OPERATORS
Vectors are special forms of matrices and contain only one row OR one column.
Scalars are matrices with only one row AND one column
ARRAYS AND MATRICES
Can be nested
WHILE LOOPS
x=7;
while (x >= 0)
x = x-2;
end
if (x == 3)
disp('The value of x is 3.');
elseif (x == 5)
disp('The value of x is 5.');
else
disp('The value of x is not 3 or 5.');
end
SWITCH STATEMENT
switch face
case {1}
disp('Rolled a 1');
case {2}
disp('Rolled a 2');
otherwise
disp('Rolled a number >= 3');
end
BREAK STATEMENTS
break – terminates execution of for and while loops. For nested loops,
it exits the innermost loop only.
GRAPHICS
x = linspace(-1,1,10);
y = sin(x);
plot(x,y); % plots y vs. x.
title('Plot of y versus x')
xlabel('Values of x')
ylabel('Values of y')
grid
hold on; % put several plots in the same figure window.
figure; % open new figure window.
GRAPHICS (2)
GRAPHICS (3)
subplot(m,n,p)
divides the figure into an mxn
grid and creates axes in the
position specified by p.
EXAMPLES OF MATLAB PLOTS
SCRIPTS AND FUNCTIONS
r = 7;
[area,circum] = circle(r);
% call our circle function.
disp(['The area of a circle having radius ' ...
, num2str(r), ' is ', num2str(area)]);
function y = ave(x)
[m,n] = size(x);
if m == 1
MATLAB
m = n;
Code
end
y = sum(x)/m;
end
sqrt
log
cos/acos/sin/asin etc
exp - exponential
abs
sign
norm
sum
prod - product
LOGICAL FUNCTIONS
help command_name
helpwin command_name
doc command_name