Introduction
to
MATLAB
MAT 1011 – Calculus for Engineers
Department of Mathematics
School of Advanced Sciences
Vellore Institute of Technology, Vellore
Introduction to MATLAB
• The name MATLAB stands for MATrix LABoratory.
• MATLAB is a high-performance language for technical computing.
• It integrates computation, visualization, and programming environment.
• MATLAB has many advantages compared to conventional computer languages (e.g., C, FORTRAN) for
solving technical problems.
• MATLAB is an interactive system whose basic data element is an array that does not require
dimensioning.
• It also has easy-to-use graphics commands that make the visualization of results immediately available.
• Specific applications are collected in packages referred to as toolbox. There are toolboxes for signal
processing, symbolic computation, control theory, simulation, optimization, and several other fields of
applied science and engineering.
Department of Mathematics, Vellore Institute of Technology, Vellore 2
Desktop Basics
Workspace
Current Folder (Explore data that
(Access your files) you create or import
from files)
Command Window
(Enter commands)
Department of Mathematics, Vellore Institute of Technology, Vellore 3
Basic arithmetic Operations
• A scalar is a 11 matrix.
• A row vector of length (say 5) is a 15 matrix.
• A column vector of length (say 5) is a 51 matrix.
• MATLAB can work as a calculator.
• The below table summarizes the arithmetic operators available in MATLAB.
Operation Description Operation Description
+ Addition (a+b) / Right division (a/b)
- Subtraction (a-b) \ Left division (a\b)
* Multiplication (a*b) ^ Exponentiation (a^b)
sqrt(x) Square root of (
Basic MATLAB Commands
• For assigning a variable to an expression, the syntax is
variable_name = a value (or an expression)
Eg. x=5 (Assigns the number 5 to the variable x)
• To overwrite on a variable which has been already created,
Eg. >> x=x+1 (Rewrites x by incrementing by 1)
• Semicolon (;) suppresses the output being displayed. Eg. >> x=x+1;
• MATLAB does numerical calculations in double precision, which is 15 digits.
• To display in short we can use the command
>>format short
>> x=-163.6667
• To see all the 15 digits use the command format long
>> format long
>> x= -1.636666666666667e+002
Department of Mathematics, Vellore Institute of Technology, Vellore 5
Workspace Variables
• The workspace contains variables that you create within or import into MATLAB from
data files or other programs.
• You can view the contents of the workspace using
• >> whos (which gives the detailed information of the variables in the workspace)
while the variables in short can be viewed with the command who.
• The clc function clears the Command Window ( >> clc )
• To clear all the variables from the workspace, use the clear command (>>clear)
• To get help on any command of MATLAB type >> help command.
Department of Mathematics, Vellore Institute of Technology, Vellore 6
Matrices and Arrays
• All MATLAB variables are multidimensional arrays, no matter what type of data. A
matrix is a two-dimensional array often used for linear algebra.
• To create an array with four elements in a single row, separate the elements with either a
comma (,) or a space.
>> a = [1,2,3,4] or >> a = [1 2 3 4]
• To create a matrix that has multiple rows, separate the rows with semicolons.
>> a = [1,2,3;4,5,6;7,8,10] or >> a = [1 2 3;4 5 6;7 8 10]
• Some inbuilt functions, namely, ones and zeros etc., may be also used.
For example to create a 5x1 matrix of zeros, we type >> z = zeros(5,1)
and to create a 5x1 matrix of ones, we type >> z = ones(5,1)
Department of Mathematics, Vellore Institute of Technology, Vellore 7
Matrix Operations
Operation Syntax Remarks
Addition >> C=A+B Matrices A and B must be of same order
Subtraction >> D=A-B Matrices A and B must be of same order
Mulitiplication >> C=A*B Matrices A and B must be compatible for multiplication
(A is of order mxp and B is pxn, then C is of order mxn.)
Element-wise multiplication >> C=A.*B Eg. A=[x, y]; B=[a, b], then A.*B=[xa, yb]
Matrices A and B must be of same order
Element-wise division >> C=A./B Eg. A=[x, y]; B=[a, b], then A./B=[x/a, y/b]
Matrices A and B must be of same order
Element-wise exponentiation >> C=A.^p Eg. A=[x, y]; p=2; then A.^p=[x2, y2]
Matrix Transpose >> C=A’
Determinant >> C=det(A)
Matrix Inverse >> C=inv(A) In case of Singular matrix warning will be displayed.
Department of Mathematics, Vellore Institute of Technology, Vellore 8
Generating/Referencing Matrix Elements
Operation Syntax Remarks
To create an array of n >> x=linspace(x1,xn,n) Creates an array of n elements starting with x1 and
elements ending with xn.
To create an array with h >> C=a:h:b Creates an array with first element as a and last
spacing element as b with step length h.
Zero matrix >> A=zeros(m,n) Creates an mxn matrix of zeros.
Identity matrix >> I=eye(n) Creates an identity matrix of order n.
Ones >> B=ones(m,n) Creates a matrix of order mxn of ones.
Length of a vector >> n=length(X) Displays the number of elements in the vector X
Size of an Array >> [m,n]=size(A) Displays the number of rows and columns of the matrix.
Referencing an element >> A(i,j) To pick out the element at ith row position and jth column
position of matrix A.
Referencing a column >> A(:,j) To reference all the elements in the jth column of A.
Similarly, a row can be referenced by >> A(i,:)
Referencing some >> A(k:m,j) To reference jth column, restricted to rows k to m.
rows/columns Similarly numbers of columns can be restricted.
Department of Mathematics, Vellore Institute of Technology, Vellore 9
Transcendental functions
Operation Syntax Remarks
>> exp(x)
>> log(x) Base e
>> log10(x) Base 10
sin x >> sin(x) Here x is in radians.
When x is in degrees, we can use
cos x >> cos(x) sind(x), cosd(x), tand(x), secd(x),
tan x >> tan(x) cscd(x).
sec x >> sec(x)
cosec x >> csc(x)
Inverse trigonometric >> asin(x)
functions >> acos(x)
>> atan(x)
Department of Mathematics, Vellore Institute of Technology, Vellore 10
Basic Output Commands
Operation Syntax Remarks
Display >> display(‘text’) The display command writes text.
Formatting the >> format short Scaled fixed point format with 5 digits.
output
>> format short e Floating point format with 5 digits.
>> format long Scaled fixed point format with 15 digits
>> format long e Floating point format with 15 digits.
Department of Mathematics, Vellore Institute of Technology, Vellore 11
Scripts
• The MATLAB product provides a
powerful programming language, as well
as an interactive computational
environment.
• We can write a series of commands to a
file called a script file (*.m) that then
execute.
• Use the MATLAB Editor to create your
own scripts.
Department of Mathematics, Vellore Institute of Technology, Vellore 12
Exercise-1
Find the numerical values of the following:
i. The fifth root of 9.8
ii. 2 to the eight power
iii. The angle whose tangent is 0.5 (both in degrees and radians)
iv. Cosine of
v. Generate a list of natural numbers from 3 to 10.
vi. Create a list of 10 equally distributed numbers from 0 to 1.
Department of Mathematics, Vellore Institute of Technology, Vellore 13
Exercise-2
1. If A=, then find
i. A+B
ii. A-B
iii. |A|
iv. A-1
2. Given the matrix A= ,
v. Obtain a 2x3 matrix B, containing 2nd and 3rd row of A.
vi. Obtain a matrix D by appending the row vector C= after the last row of A .
3. Solve the system of equations
3x1+x2=–1,
2x1+4x2+x3=7,
2x2+5x3=9
Department of Mathematics, Vellore Institute of Technology, Vellore 14