Professional Documents
Culture Documents
Lec.01-Basics of Programming Part 1 PDF
Lec.01-Basics of Programming Part 1 PDF
(CES 634)
SEM: MONSOON 2018
Aniket Bhalkikar
(PhD Scholar)
EERC, IIIT-H 3
MATLAB Code: Main Code Blocks
(a)
(b)
(c)
Comment with %%
(Math) Expression/Equation
Calling Function
EERC, IIIT-H 5
Try the following Code
EERC, IIIT-H 6
Result
Plot
15
10
5
Y
-5
-10
0 1 2 3 4 5 6 7 8 9 10
X
EERC, IIIT-H 7
General MATLAB Commands
• pwd (shows current working directory)
EERC, IIIT-H 8
General MATLAB Commands
• help ____ (explains the command in short)
EERC, IIIT-H 9
EERC, IIIT-H 10
Fundamental MATLAB Classes
EERC, IIIT-H 11
EERC, IIIT-H 12
Basic Data Types
• MATLAB easily works with arrays
• Scalars, vectors and arrays
• Assigning variables
• Arrays/Matrices
• Suppress “echo”
EERC, IIIT-H 14
Practice in COMMAND WINDOW
>> a = [2]
>> a = [3 4 5 6]
>> b = a’
>> c = [1, 4, 3, 7] or c = [1,4,3,7]
>> d = [1 4 3 7]
>> e = [1; 4; 3; 7]
>> f = e’
>> % Create matrix of size 4x4
>> g = 1:4 or g = [1:4]
>> h = [2:3; 5:6]
>> i = [2:4; 6:9] <- any error..?
EERC, IIIT-H 15
Special Characters
Symbol Symbol Name Role
… Ellipsis • Line Continuation
: Colon • Vector creation
• Indexing
• For – loop iteration
; Semi-colon • Signify end of row
• Suppress output of code line
() Parentheses • Operator precedence
• Function argument enclosure
• Indexing
[] Square Bracket • Array construction
• Array concatenation
• Empty array & matrix deletion
{} Curly Bracket • Cell array assignment and contents
% Percent • Comment
‘‘ Single quotes • Character array construction
EERC, IIIT-H 16
Predefined Special Values
Function Purpose
pi Contains π to 15 significant digits
i, j Contains the value i(√-1)
Represents machine infinity. It is a result of division
Inf by 0.
‘Not-a-Number’. It is a result of an undefined
NaN mathematical operation.
It is a form of 6 element row vector contains year,
clock month, date, hour, minute and second.
date Contains the current date
eps It is the smallest difference between two elements
A special variable used to store the result of an
ans unassigned exression
EERC, IIIT-H 17
Special Characters in printf Format
Function Purpose
%d Displays value of an integer
%e Displays value in exponential format
%f Displays value in floating point format
%g Displays value in either floating point or exponential format, whichever is shorter
\n Skip to new line
• A = pi*ones(1,4);
• txt = sprintf(‘%g %.2g %f %.2f’, A)
• txt = sprintf(‘%e %15e %f %15f’, A)
• txt = sprintf(‘%30s’, ‘Hello’)
• txt = sprintf(‘%d \t %f \t %e \t %g’, A)
• txt = fprintf(‘%d \tEERC,%f
IIIT-H \t %e \t %g’, A) 18
Formatting Text
Function Purpose
%*f Specify width as per the input argument
EERC, IIIT-H 19
Formatting Text
Perform the following operations
• txt = fprintf(‘%08.3f’,123.45678) Difference..?
• txt = fprintf(‘%8.3f’,123.45678)
• txt = sprintf(‘left%12.2f\n right%-12.2f’)
• txt = sprintf(‘%*.2f’, 5, 123.45678)
• txt = sprintf(‘%*f %.*f %*.*f’, . . .
15,123.4567, 3,16.42837, 6,4,pi)
EERC, IIIT-H 20
EERC, IIIT-H 21
Basic Mathematical Expressions
• Scalar Operations Special Variables
• + - * / ^ exp (x) = ex Variable Meaning
log (x) = ln x pi Number π
• log, exp
log10 (x) = log x eps Machine precision
• power, sqrt
i Imaginary unit
• sind, cosd, tand
inf Infinity
• asin, acos, atan NaN Not a Number
• rem, round, ceil, floor ans Last displayed result
end Last element of array
clc = Clears Command Window
realmax Largest real number
clear x = Clears value stored in x
intmax Largest integer
clear all = Clears all variables in Workspace
EERC, IIIT-H 22
Building Arrays
Perform the following operations
• a = [1, 2, 5]; & a’ & inv(a)
• b = [3; 6; 9]; & b’ & inv(b)
• c = eye(3) % identity matrix
• d1 = [a, c]; & d2 = [a’, c];
• d3 = [a; c]; & d4 = [a’; c];
• e1 = [b, c]; & e2 = [b’, c];
• e3 = [b; c]; & e4 = [b’; c];
• M(i,j); M(:,j); M(i,:) % check
• size(M)
EERC, IIIT-H 23
to be continued..
EERC, IIIT-H 24