Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

CIVIL ENGINEERING WORKSHOP

(CES 634)
SEM: MONSOON 2018

Aniket Bhalkikar
(PhD Scholar)

Basics of Programming using MATLAB

EARTHQUAKE ENGINEERING RESEARCH CENTRE


INTERNATIONAL INSTITUTE OF INFORMATION TECHNOLOGY
HYDERABAD
Important Parts of MATLAB Window
• The COMMAND WINDOW
• The COMMAND HISTORY
• The WORKSPACE
• The CURRENT DIRECTORY
• The HELP BROWSER
• The EDITOR What for..?
Use..?
Resize/Relocate
How to open Editor..?
EERC, IIIT-H 2
MATLAB Code

EERC, IIIT-H 3
MATLAB Code: Main Code Blocks

(a)

(b)

(c)

(a) = Input block


(b) = Computation
(c) = Output block
EERC, IIIT-H 4
MATLAB Code: Key Parts

Comment with %%

Assignment 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)

• whos (lists variable in workspace with size)

• what (lists files present on the disk)

EERC, IIIT-H 8
General MATLAB Commands
• help ____ (explains the command in short)

• doc ____ (opens the documentation file)

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

• Rows vs. Column vectors

• Arrays/Matrices

• Suppress “echo”

• Variables are case-sensitive


EERC, IIIT-H 13
Data View

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

%.*f Specify precision as per the input argument

%*.*f Specify width and precision 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

You might also like