MATLAB

Introduction to MATLAB Programming

Accessing MATLAB
  1. Accessing MATLAB online.
  • MATLAB can be accessed online, free of charge through “mathworks” website.
  • Go to https://www.mathworks.com/accesslogin/createProfile.do and create an account following the instructions.
  • NOTE: Remember your Email and Password information for later use. For ‘How will you use MathWorks software?’, select “Academic use ”.
A blue and white rectangle with a black line

Description automatically generated
  • Associate mathworks license on : https://www.mathworks.com/licensecenter
  • Click on add license, when prompted add license and activation key.
  • Verify association in License Center. License Label should say ‘2XXX- BEST Robotics’. (2XXX = Current Year)
A screenshot of a computer

Description automatically generated
  • Download the Software. Go to http://www.mathworks.com/downloads/web_downloads/  🡪 Click on the Download link 🡪 Click the blue button that says R2XXXx. 🡪 Click the blue button that says R2XXXx Installer. 🡪 Open the installer file (*.exe) to extract and launch the installer.
  • Install the software. When MathWorks Installer is launched, select Log in with a MathWorks Account and continue through the Installer 🡪 At the License Agreement, select Yes and continue🡪 When prompted to Log In, use your MathWorks Account info🡪 At License Selection dialog, choose ‘Select a license:’ and choose the one with the Label: 2XXX – BEST Robotics and continue.
  • At the Installation Complete prompt, ensure to check Activate MATLAB (default is checked) and continue🡪 At the Confirmation dialog, click Confirm. 🡪 Activation has been completed. Click Finish.
  1. Installing free MATLAB using University email address.
  • Go to official website of University of Delhi 🡪 Libraries 🡪 e-Resources 🡪 scroll down 🡪IT service 🡪”Get Software” in the list on the left-hand side 🡪 MATLAB 🡪Create an account using university email address and install licensed version of MATLAB.
  • Alternately, go to https://in.mathworks.com/academia/tah-portal/university-of-delhi-40795990.html and create an account using email Id provided be the university and install MATLAB.
  1. Defining Variables: 
  • MATLAB variables are case sensitive which means “x” and “X” can be used as different variables and store different values.
A white background with red text

Description automatically generated
  • Variables names can have up to 63 characters (MATLAB 6.5 or newer)
  • Variable names can start with a letter and can be followed by digits or underscores.
A screenshot of a computer

Description automatically generated
  1. Special Variables: 
  • Inf = infinity
  • NaN = Not a number
  • eps = smallest possible incremental Value 
  • pi 
  • i and j = root of -1
  • realmin = smallest usable positive real number
  • realmax = largest usable positive real number

Operators in MATLAB:

  1. Arithmetic operators 
A white background with black text

Description automatically generated
A screenshot of a math problem

Description automatically generated
A white paper with black text

Description automatically generated
  1. Relational operators
  1. Logical operators 
A white background with black text

Description automatically generated
A white background with black text

Description automatically generated
A screenshot of a computer

Description automatically generated
  • abs – finds absolute value of all elements in the matrix.
  • sign – signum function.
  • sin, cos – Trigonometric functions.
  • asin, acos… – Inverse trigonometric functions. 
  • exp – Exponential. 
  • Log, log10 – natural logarithm, logarithm (base 10) 
  •  ceil, floor – round towards +infinity, -infinity respectively.
  • round – round towards nearest integer. 
  •  real, imag – real and imaginary part of a complex matrix.
  • sort – sort elements in ascending order.
  • A matrix can be created as follows (carefully observe the position of comas and semicolons) 
A number on a white background

Description automatically generated
  • In MATLAB indexing starts from 1.
  • Each element can be accessed from the matrix using the indices of its row and column in the same order.
  • For example: “2” can be accessed as X(1,2) 
A white background with black text

Description automatically generated
  • A portion of a matrix can be extracted into another variable using the following syntax :
A white background with black numbers and letters

Description automatically generated
  1. Matrix extension 
  1. Concatenation
A math equations on a white background

Description automatically generated
  1. Repetition 
A white paper with black text

Description automatically generated
  1. Addition 
A math equations and numbers

Description automatically generated
A white background with black and red text

Description automatically generated
  1. Matrix multiplication
A white paper with black text and red text

Description automatically generated
A math equations on a white background

Description automatically generated
  • zeros: creates an array of all zeros, Ex: x = zeros(3,2). 
  • ones: creates an array of all ones, Ex: x = ones(2).  
  • eye: creates an identity matrix, Ex: x = eye(3). 
  • rand: generates uniformly distributed random numbers in [0,1].
  • diag: Diagonal matrices and diagonal of a matrix.
  • size: returns array dimensions.
  • length: returns length of a vector (row or column).
  • det: Matrix determinant.
  • inv: matrix inverse.
  • eig: evaluates eigenvalues and eigenvectors.
  • rank: rank of a matrix 
  • find: searches for the given values in an array/matrix.

Example: 

A diagram of a graph

Description automatically generated
A graph of a function

Description automatically generated with medium confidence

Example:

A graph with blue lines and dots

Description automatically generated
  1. “if”/ “else” statement
  2. “switch” statement.
  3. “for” loops.
  4. “while” loops.
  5. “break” statement.
  • While loop, runs a command or a set of command repeatedly until the given condition becomes false.
  • There is an update statement within the loop, that updates the condition, and then decides when the flow of control exits the loop.
  • “break” statement can be used to break a “for” or “while” loop.
A white background with black numbers

Description automatically generated
  • For loop repeats a command or a set of commands a specific number of times.
  • It assigns and overrides the value of an iterating variables from a specified range and the flow of control exits the loop, when there is no value remains in the given range to be assigned to the variable.
A blue and white sign with black text

Description automatically generated

Alternatively, you can click on simulink button on the toolbar.

A close-up of a computer screen

Description automatically generated

This is what the simulink start page looks like:

A screenshot of a computer

Description automatically generated

Under “simulink” you can find multiple classes of blocks:

A diagram of a mathematical equation

Description automatically generated

Mathematically:

  • Laplace transform of u(t) is 1/s.
  • When this function is given as an input of a block of impulse response h(t)=e-t. In “s” domain this transfer function is given by 1/(s+1).
  • Output of this should theoretically by the convolution integral of u(t) and h(t) or inverse Laplace transform of product of 1/s and 1/(s+1).
  • Output= x(t)*h(t) = u(t)*e-t = L(1/s(s+1)) = 1-e-t
A graph with a line drawn on it

Description automatically generated

Reference Links
  1. https://www.tutorialspoint.com/matlab/index.htm
  2. https://in.mathworks.com/help/matlab/getting-started-with-matlab.html
  3. https://www.mccormick.northwestern.edu/documents/students/undergraduate/introduction-to-matlab.pdf

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top