You are on page 1of 15

MATLAB Learning Series

Day 1 to 3- Getting Started with MATLAB


(31st July to 2nd August)

Most of the students are worried that they have no access to MATLAB Software. What you can do is,

Option 1: Take up MATLAB Onramp course(free) on matlabacademy.mathworks.com. This course


is interactive and the portal is similar to MATLAB Online version R2019b. You’ll learn how to open
and use the software. After completing the course, you’ll receive a certificate from Mathworks too,
that’s an add-on.
I’m recommending this course not for the content but for the “unlimited access” to MATLAB
Online version it gives you. Once you start the course, you’ll get a very basic knowledge about how to
create MATLAB variables. In the further practice section of the onramp course, you can start
practicing using the learning materials shared by me.

Option 2: Create a mathworks account on www.mathworks.com, you can download a one-month trial
of MATLAB’s latest version R2020a and start using it.

Option 3: Visit www.mathworks.com on your mobile and create an account by entering valid email
id(can be a business/university/personal ID) and password. Go to download trial version page >> click
on get Started (When a message pops up asking ‘Are you a mobile user?’) >> Enter your contact
information and select your areas of interest (select Wireless Communication as one area of interest).
You’ll receive a mail from mathworks instantly with a link to access MATLAB Online for free and
download MATLAB app. Visit Playstore, download MATLAB Online App, login using your
mathworks account details and start practicing.

Else if you have the MATLAB software already, then you can start practicing directly.

CHAPTER 1
Creating MATLAB Variables
MATLAB variables are created with an assignment statement. The syntax of variable
assignment is, variable name = a value (or an expression)
You can name your MATLAB variables anything you'd like as long as they start with a
letter and contain only letters, numbers, and underscores (_).
MATLAB variables are also case sensitive.

Example Description
a = pi Create variables with the equal sign (=).
The left-side (a) is the variable name containing the value on the right- side
(pi).
b=2 Create variables with the equal sign (=).
The left-side (b) is the variable name containing the value on the right- side
(2).
x = Expression The left-side (x) is the variable name containing any expression. Where an
expression is a combination of numerical values, mathematical operators,
Example: variables, and function calls.
x=log(1) In other words, expression can involve:
x=Sin(2) • manual entry • built-in functions • user-defined functions
THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)
MATLAB Learning Series

Type these commands in the command window.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

MATLAB offers many predefined mathematical functions for technical computing which
contains a large set of mathematical functions.
Typing help elfun and help specfun calls up full lists of elementary and special functions
respectively. There is a long list of mathematical functions that are built into MATLAB. These
functions are called built-ins.

>>help elfun
Elementary math functions.

Trigonometric.
sin - Sine.
sind - Sine of argument in degrees.
sinh - Hyperbolic sine.
asin - Inverse sine.
asind - Inverse sine, result in degrees.
asinh - Inverse hyperbolic sine.
cos - Cosine.
cosd - Cosine of argument in degrees.
cosh - Hyperbolic cosine.

acos - Inverse cosine.


acosd - Inverse cosine, result in degrees.
acosh - Inverse hyperbolic cosine.
tan - Tangent.
tand - Tangent of argument in degrees.
tanh - Hyperbolic tangent.
atan - Inverse tangent.
atand - Inverse tangent, result in degrees.
atan2 - Four quadrant inverse tangent.
THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)
MATLAB Learning Series

atan2d - Four quadrant inverse tangent, result in degrees.


atanh - Inverse hyperbolic tangent.
sec - Secant.
secd - Secant of argument in degrees.
sech - Hyperbolic secant.
asec - Inverse secant.
asecd - Inverse secant, result in degrees.
asech - Inverse hyperbolic secant.
csc - Cosecant.
cscd - Cosecant of argument in degrees.
csch - Hyperbolic cosecant.
acsc - Inverse cosecant.
acscd - Inverse cosecant, result in degrees.
acsch - Inverse hyperbolic cosecant.
cot - Cotangent.
cotd - Cotangent of argument in degrees.
coth - Hyperbolic cotangent.
acot - Inverse cotangent.
acotd - Inverse cotangent, result in degrees.
acoth - Inverse hyperbolic cotangent.
hypot - Square root of sum of squares.
deg2rad - Convert angles from degrees to radians.
rad2deg - Convert angles from radians to degrees.

Exponential.
exp - Exponential.
expm1 - Compute exp(x)-1 accurately.
log - Natural logarithm.
log1p - Compute log(1+x) accurately.
log10 - Common (base 10) logarithm.
log2 - Base 2 logarithm and dissect floating point number.
pow2 - Base 2 power and scale floating point number.
realpow - Power that will error out on complex result.
reallog - Natural logarithm of real number.
realsqrt - Square root of number greater than or equal to zero.
sqrt - Square root.
nthroot - Real n-th root of real numbers.
nextpow2 - Next higher power of 2.

Complex.
abs - Absolute value.
angle - Phase angle.
complex - Construct complex data from real and imaginary parts.
conj - Complex conjugate.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

imag - Complex imaginary part.


real - Complex real part.
unwrap - Unwrap phase angle.
isreal - True for real array.
cplxpair - Sort numbers into complex conjugate pairs.

Rounding and remainder.


fix - Round towards zero.
floor - Round towards minus infinity.
ceil - Round towards plus infinity.
round - Round towards nearest integer.
mod - Modulus (signed remainder after division).
rem - Remainder after division.
sign - Signum.

>>help specfun
Specialized math functions.

Specialized math functions.


airy - Airy functions.
besselj - Bessel function of the first kind.
bessely - Bessel function of the second kind.
besselh - Bessel functions of the third kind (Hankel function).
besseli - Modified Bessel function of the first kind.
besselk - Modified Bessel function of the second kind.
beta - Beta function.
betainc - Incomplete beta function.
betaincinv - Inverse incomplete beta function.
betaln - Logarithm of beta function.
ellipj - Jacobi elliptic functions.
ellipke - Complete elliptic integral.
erf - Error function.
erfc - Complementary error function.
erfcx - Scaled complementary error function.
erfinv - Inverse error function.
erfcinv - Inverse complementary error function.
expint - Exponential integral function.
gamma - Gamma function.
gammainc - Incomplete gamma function.
gammaincinv - Inverse incomplete gamma function.
gammaln - Logarithm of gamma function.
psi - Psi (polygamma) function.
legendre - Associated Legendre function.
cross - Vector cross product.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

dot - Vector dot product.

Number theoretic functions.


factor - Prime factors.
isprime - True for prime numbers.
primes - Generate list of prime numbers.
gcd - Greatest common divisor.
lcm - Least common multiple.
rat - Rational approximation.
rats - Rational output.
perms - All possible permutations.
nchoosek - All combinations of N elements taken K at a time.
factorial - Factorial function.

Coordinate transforms.
cart2sph - Transform Cartesian to spherical coordinates.
cart2pol - Transform Cartesian to polar coordinates.
pol2cart - Transform polar to Cartesian coordinates.
sph2cart - Transform spherical to Cartesian coordinates.
hsv2rgb - Convert hue-saturation-value colors to red-green-blue.
rgb2hsv - Convert red-green-blue colors to hue-saturation-value.

Overwriting Variables
Once a variable has been created, it can be reassigned.
In addition, if you do not wish to see the intermediate results, you can suppress the numerical
output by putting a semicolon (;) at the end of the line. Then the sequence of commands looks
like this:

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

CHAPTER 2
Array Generation and Operations
Array types
Example Description
A=4 scalar
A = [3 5] row vector
A = [1;3] column vector
A = [3 4 5;6 7 8] matrix
>>A = 4
A=
4

>>A = [3 10 12]
A=
3 10 12

>>A = [1;5;3]
A=
1
5
3

>>A = [1 2;3 4]
A=
1 2
3 4

>>A = [-1 2;0.5 8]


A=
−1 2
0.5 8

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

How to Create Evenly-spaced vectors??

Example Description
1:4 Create a vector from 1 to 4, spaced by 1, using the
colon (:) operator.
1:0.5:4 Create a vector from 1 to 4, spaced by 0.5.
linspace(1,10,5) Create a vector with 5 elements. The values are evenly
spaced from 1 to 10.

linspace(X1, X2) Generates a row vector of 100 linearly equally


spaced points between X1 and X2.

linspace(X1, X2, Generates N points between X1 and X2.


N) For N = 1, linspace returns X2.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

Matrix Generation
Example Description
A=[1 2;3 4] Create a square matrix with 2 rows and 2 columns. The elements of
the first row are 1 and 2, the elements of second row are 3 and 4.
A=[1 2 3;4 5 6;7 8 Create a square matrix with 3 rows and 3 columns. The elements of
9] the first row are 1, 2 and 3, the elements of second row are 4,5 and 6
and the elements of third row are 7,8 and 9.
rand(2) Create a square matrix with 2 rows and 2 columns. The matrix
elements will be randomly generated.
zeros(2,3) Create a rectangular matrix with 2 rows and 3 columns. All the
matrix elements will be Zero.
ones(2,3) Create a rectangular matrix with 2 rows and 3 columns. All the
matrix elements will be One.
eye(3) Create an Identity matrix with 3 rows and 3 columns. A Identity
Matrix with ones as diagonal elements and zeros as elements in
upper and lower triangular matrices is generated.
[xrow,xcol] = size(x) Save the number of rows and columns in x to two different
variables.
[xMax,idx] = max(x) Calculate the maximum value of x and its corresponding index
value.
A(i,j) Accessing the elements in a MxN matrix A. I ranges between to 1 to
M(No.of Rows) and j ranges between 1 to N(No.of Columns).

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

Array Operations – Addition, Subtraction, Division and Multiplication.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

Addition and Subtraction:

Matrix Multiplication:

Element Wise Matrix Multiplication:

Element Wise Matrix Division:

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

Matrix Division:

Inner Product of 2 Matrices

>>A*B’

Outer Product of 2 Matrices

>>A’*B

Size of the Matrix:

size[A] calculates the number of rows and columns in a matrix.

>>A = [1 2;3 4];

Other Examples Size


A=4 1
A = [3 5] 12
A = [1;3] 21
A = [3 4 5;6 7 8] 23
A = [0 1 2;3 4 5;6 7 8] 3 3

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

Converting a row matric into column matrix

Consider a row matrix D = [8 2 3], to convert it into a column matrix type

>>D = D’
8
2
3

Accessing the elements of a Matrix.

Changing the Value of Elements in a Matrix.


A(2) = 11 Change the value of the second element an array to 11.

Accessing a vector inside a Matrix.


Example Description
A(end,2) Access the element in the second column of the last row.
A(2,:) Access the entire second row
A(:,1) Access the entire first column
A(1:3,:) Access all columns of the first three rows.
A(2:3,2:3) Access second and third rows & columns of a matrix.

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)


MATLAB Learning Series

THIS DOCUMENT IS PREPARED BY PAVITHRA NAGARAJ (WWW.LINKEDIN.COM/IN/PAVITHRA-NAGARAJ)

You might also like