MATLAB - A Scientific Computing Tool
MATLAB - A Scientific Computing Tool
• Matlab Commands
• Matrix Manipulation
• Plotting Graphs
3
MATrix LABoratory
• Advanced interactive software package specifically designed for
scientific and engineering computation.
4
MATLAB ENVIRONEMENT
Command-Window
Command- History
5
MATRIX MANIPULATION
6
MATLAB VARIABLES
• Array is the fundamental unit of data
in MATLAB.
Row 1
• An array is collection of data values
organized into rows and columns, Row 2
and known by a single name.
Row 3
• Arrays can be classified as vectors
and matrices. arr(3,2)
Row 4
– Vector: Array with one dimension
– Matrix: Array with more than one
Col 1 Col 2 Col 3 Col 4 Col 5
dimension
• Row and column indices of an array
start from 1
• Scalars are also treated as arrays of
size (1 row and 1 column).
7
Matrix and Vector Creation
• Syntax
– MatrixName=[1st row elements; 2nd row elements; 3rd row
elements; … …; nth row elements]
– The number of elements in each row corresponds to number of
column.
• 1st row element col1 element col2 element col3 element … coln element
• Example
– 5 х 4 matrix
• A=[1 2 3 4; 5 6 7 8; 9 10 11 12; 13 14 15 16; 17 18 19 20]
– 1 х 4 row vector
• B=[21 22 23 24]
– 4 х 1 column
• C=[25; 26; 27; 28]
8
Matrix Manipulation
• Accessing a single element from a matrix
– Syntax
• VariableName=MatrixName(RowNumber,ColNumber)
– Example
• D=A(3,4) D=[12]
10
Matrix Manipulation Contd…
• Accessing a Matrix from a Matrix
– Syntax
• newMatrixName=MatrixName(StartingRowNumber
:EndingRowNumber,StartingColumnNumber
:EndingColumnNumber)
– Example
• I=A(1:2,3:4)
• J=A(4:5,3:4)
• K=A(2:3,2:3)
• L=A(2:4,2:3)
• M=A(4:5,1:4)
11
Matrix Manipulation Contd…
• N=A(2,:)
• O=A(:,4)
• P=A(:,2:4)
• Q=A(:,:)
• S= size(A), S1=size(A,1), S2=size(A,2)
• [R1 R2]= size(A)
• T=A’
• U = zeros (2,4)
• V = ones (2,4)
12
MATLAB
PROGRAMS AND FUNCTIONS
13
Simple Matlab Programs
• Eligibility to vote (Version 1)
– clc
– clear all • Eligibility to vote (version 4)
– age=20;
– if age > 18
– clc
• disp (‘Eligible to VOTE');
– clear all
– end
• Eligibility to vote (Version 2) – age=[20 18 17; 25 46 60; 11 14 12];
– clc
– for i=1:3
– clear all
– age=20; • for j=1:3
– if age > 18
• disp (‘Eligible to VOTE');
– if age (i,j) > 18
– else » Eligible(i,j)='Y';
• disp (‘Not Eligible to VOTE’)
– else
– end
» Eligible(i,j)='N';
• Eligibility to vote (version 3)
– clc – end
– clear all
• end
– age=input('Enter the Age: ');
– if age > 18 – end
• disp ('Eligible to VOTE');
– else – Eligible
• disp ('Not Eligible to VOTE')
– end
14
Simple Matlab Programs
• Eligibility to vote (version 6)
• Eligibility to vote (version 5) Vote.m (Main Function)
– clc – clc
– clear all – clear all
– load agedata.m; – load agedata.m;
– age=agedata;
– age=agedata;
– Eligible=compute(age)
– for i=1:size(age,1) Compute.m (Sub Function)
• for j=1:size(age,2) • function [ elig ] = compute( ag )
– if age (i,j) > 18 • a=ag;
» Eligible(i,j)='Y';
• for i=1:size(a,1)
– else
– for j=1:size(a,2)
» Eligible(i,j)='N';
• if a (i,j) > 18
– end – elig(i,j)='Y';
• end • else
– elig(i,j)='N';
– end • end
– Eligible – end
• end
15
• end
PLOTTING GRAPHS
16
Plotting Graphs
• clc
D e t a ils o f S c o re s
20
• clear all
• load scoredata.m;
• score=scoredata;
15
Number of Runs
• x=score(:,1);
• y=score(:,2); 10
• plot(x,y)
• xlabel('Number of Overs')
• ylabel('Number of Runs') 5
0 2 4 6 8 10 12 14 16 18 20
•
N u m b e r o f O ve rs
Title('Details of Scores')
• ---------------------------------------------------------- D e t a ils o f S c o re s
• clc
20
• clear all
• load scoredata.m;
15
• score=scoredata;
N um ber of R uns
• x=score(:,1);
• y=score(:,2); 10
• plot(x,y,'r*-.')
• xlabel('Number of Overs')
• ylabel('Number of Runs') 5
0 2 4 6 8 10 12 14 16 18 20
N u m b e r o f O ve rs
• Title('Details of Scores')
17
Plotting Graphs
• clc
• clear all
• load scoredata.m; 20
D e t a ils o f S c o re s
• score=scoredata; 18
In d ia
P a k is t a n
• x=score(:,1); 16
• y=score(:,2); 14
• hold on 6
• legend('India', 'Pakistan'); 2
0 2 4 6 8 10 12 14 16 18 20
N u m b e r o f O ve rs
• xlabel('Number of Overs');
• ylabel('Number of Runs');
• Title('Details of Scores')
18
• clc
Plotting Graphs
• clear all
• load scoredata.m;
• score=scoredata;
• x=score(:,1); 20
D e t a ils o f S c o re s
18
D e t a ils o f S c o re s
• y=score(:,2); In d ia
16
P a k is t a n
• z=score(:,3); 14
• subplot(1,2,1) 15
12
N um ber of R uns
6
• ylabel('Number of Runs');
4
• Title('Details of Scores')
•
5 2
hold on 0 5 10 15
N u m b e r o f O ve rs
20 0 5 10 15
N u m b e r o f O ve rs
20
• subplot(1,2,2)
• plot (x, z, 'k.-');
• legend('Pakistan');
• xlabel('Number of Overs');
• ylabel('Number of Runs');
• Title('Details of Scores') 19
Plotting Graphs
• clc
• clear all
• load scoredata.m;
• score=scoredata;
• x=score(:,1);
• y=score(:,2); D e t a ils o f S c o re s
20
• z=score(:,3); In d ia
N um ber of R uns
• Subplot(2,1,1)
15
• plot(x, y,'r*-') 10
• legend('India'); 5
0 2 4 6 8 10 12 14 16 18 20
•
N u m b e r o f O ve rs
xlabel('Number of Overs'); D e t a ils o f S c o re s
20
• ylabel('Number of Runs'); P a k is t a n
N um ber of R uns
15
• Title('Details of Scores') 10
• hold on 5
• Subplot(2,1,2) 0
0 2 4 6 8 10 12 14 16 18 20
N u m b e r o f O ve rs
• plot (x, z, 'k.-');
• legend('Pakistan');
• xlabel('Number of Overs');
• ylabel('Number of Runs');
20
• Title('Details of Scores')
• clc
Plotting Graphs
• clear all 20
D e t a ils o f S c o re s
• load scoredata.m; 18
In d ia
P a k is t a n
• score=scoredata; 16
• x=score(:,2:3); 14
• bar(x)
N um ber of R uns
12
• legend('India', 'Pakistan'); 10
• xlabel('Number of Overs'); 8
• ylabel('Number of Runs'); 6
• Title('Details of Scores')
4
• __________________ 2
• clc 0
0 5 10 15 20 25
• clear all N u m b e r o f O ve rs
• load scoredata.m;
• score=scoredata;
D e t a ils o f S c o r e s
• x=score(:,2); 20
• subplot(2,1,1)
N um ber of R uns
In d ia
15
• bar(x)
10
• legend('India', 'Pakistan');
5
• xlabel('Number of Overs');
• ylabel('Number of Runs'); 0
0 5 10 15 20 25
N u m b e r o f O ve rs
• Title('Details of Scores') D e t a ils o f S c o r e s
• y=score(:,3); 20
N um ber of R uns
P a k is t a n
• subplot(2,1,2) 15
• bar(y,'r') 10
• legend('Pakistan'); 5
• xlabel('Number of Overs'); 0
0 5 10 15 20 25
• ylabel('Number of Runs'); N u m b e r o f O ve rs
• Title('Details of Scores')
21
MATLAB Tool Box
• MATHEMATICS
– Partial Differential Equation Toolbox
– Symbolic Math Toolbox
– Optimization Toolbox
– Curve Fitting Toolbox
• COMMERCE
– Statistics Toolbox
– Spreadsheet Link EX
– Financial Toolbox
– Econometrics Toolbox
• COMPUTER SCIENCE
provides a comprehensive – Neural Network
set of functions – Fuzzy Logic
– Image Processing Toolbox
for processing, analysis, – Signal Processing Toolbox
visualization, – Parallel Computing Toolbox
and – Database Toolbox
algorithm development. 22
MATLAB Simulink
• Provides a graphical editor, customizable block
libraries, and solvers for modeling and simulating
dynamic systems.
23
REFERENCES
• Rudra Pratap, “Getting Started with MATLAB 7: A Quick Introduction for
Scientists and Engineers”, Oxford University Press, 2005.