You are on page 1of 8

Basic MATLAB

Display Window

Special/Built-in Variables:
- ans : default variable name for the result
- i / j : imaginary number
- pi : = 3.1415926
- eps : = 2.2204e-016, smallest amount by which 2 numbers can differ
- Inf or inf : , infinity
- NaN or nan : not-a-number

Basic Symbols used in MATLAB:
+ Plus sign. Used in addition.
- Minus sign. Used in subtraction
* Multiplication sign. Used in multiplication
/ Division sign. Used in division
^ Used in exponentiation/power.
= Equal sign. Used to assign values to a variable.

Basic MATLAB commands
clc Clears the command window
clear Clears the workspace (the variables and stored values)

MATLAB is strict about the commands. The commands must be inputted in lowercase.

Trigonometric Functions Inverse Trigonometric Functions
Radians Degrees Radians Degrees
cos(val) cosd(val) acos(val) acosd(val)
sin(val) sind(val) asin(val) asind(val)
tan(val) tand(val) atan(val) atand(val)
cot(val) cotd(val) acot(val) acotd(val)
csc(val) cscd(val) acsc(val) acscd(val)
sec(val) secd(val) asec(val) asecd(val)

Hyperbolic Functions (in Radians) Inverse Hyperbolic Functions (in Radians)
cosh(val) coth(val) acosh(val) acoth(val)
sinh(val) csch(val) asinh(val) acsch(val)
tanh(val) sech(val) atanh(val) asech(val)

Exponential Function Logarithmic Functions
e
x
= exp(x) ln(x) = log(x)
log(x) = log10(x)

Other Functions
X
1/n
= nthroot(X,n)
n

= sqrt(n)
conj(X) Returns the complex conjugate the elements of X
mod(X,Y) Outputs the remainder of X divided by Y
size(matrix) Returns the row size and column size of the matrix

Single Values
Inputting values into a value in a variable is possible with the use of an equal sign:
A=8;
Note: the semicolon (;) is used as a suppressant of the output hence the value is still placed in the
designated variable. Try using commands without the semicolon to see the difference.\
Transferring the value of a variable is also possible in matlab:
B=A;
Array and Matrices
Vector is simply a list of number. Vector is referred to as either column matrix or row matrix
in advance algebra. Square brackets [ ] are used to contain numbers in a variable. White spaces or
comma is used to separate different elements.
The following is a row vector:
matA= [ 1, 2, 3, 4, 5];
In the column vector, a semi-colon is used to separate elements. The semi-colon is
somewhat similar to a carriage return or the enter key in the keyboard whereas the next element will be
placed in the next line.
matB=[10; 9; 8; 7; 6; 5];
Matrix operations are possible in MATLAB. Some of these are addition, subtraction, multiplication, and
many more. The MATLAB considers the rules in matrix operations thus it shows a message whenever
the operation commanded by the used is invalid:
matA+matB - this command will show an error in the operation written in red
matC=[5, 5, 5, 5, 5];
matA+matC






- this command will be executed and the answer will be displayed
in the command window
The transpose of a matrix is possible with the use of the following:
transpose(variable) transposes the matrix stored in the variable
variable

Different sizes of matrices are possible in MATLAB. Comma , is used to separate each column; semi-
colon ; is used to define new row.
matrixA=[1, 2, 3, 4 ; 5, 6, 7, 8 ; 9, 10, 11, 12 ; 1, 6, 5, 3]

Generating matrices using MATLAB functions
There are several built-in functions that can be used to generate matrices. The following are the said
function:
zeros(M,N) MxN matrix of zeros
ones(M,N) MxN matrix of ones
rand(M,N) MxN matrix of uniformly distributed random numbers on (0,1)
eye(M) an identity matrix of size M
Matrix Index
The matrix indices begin from 1 unlike C which is 0. Calling an element in a matrix is possible using the
syntax below:
matrix(row#_position, column#_position)
Given:
Z=[1, 2, 3 ; 4, 5, 6 ; 7, 8, 9]
Z(1, 1) = 1
Z(2, 3) = 6
Z(1, 3) = 3
it gets the complex conjugate of each element in the matrix
then it transposes the matrix (Hermitian transpose)
Note: calling a non-existent index (negative number
or an index exceeding the matrix dimension) will
produce an error.

Concatenation of Matrices
In concatenating matrices, the dimensions must be consistent. In short, concatenating a (2x2) matrix to
a (3x3) matrix is NOT possible. A (2x2) matrix can only be horizontally concatenated with a (2xN) matrix;
a (3x3) matrix can only be vertically concatenated with a (Nx3) matrix and so on and so forth. An error
will occur if the dimension is not consistent.

Example of concatenation:









Range
The symbol used as a range in the colon sign (:). Range is used to easily enumerate elements in a certain
matrix. The colon sign may simply be read as to.
A = [1 2 3 4 5 6 7 8 9 10]; is the same as A = 1:10
B = [1 2 3 4 5; 6 7 8 9 10]; is the same as B = [1:5 ; 6:10]
Note: The default increment of the colon sign is 1. The increment value can be changed by using the
following syntax:
Starting_number : increment_value: Ending_number
C = [1 , 1.5 , 2 , 2.5 , 3 , 3.5]; is the same as C = 1 : 0.5 : 3.5
D = [ 1, 2, 3; 4, 5, 6; 7, 8, 9];
rowone = [ D(1,1), D(1,2), D(1,3)] is the same as rowone = D(1 , : )
columntwo = [ D(1,2), D(2,2), D(3,2)] is the same as columntwo = D(: , 2)

Dot Operators
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
Example






















Plotting Functions
plot(x , y)
Example:

stem(x , y)
Example:

subplot(row,column,position)
Example:

hold on
Example: