@ Matlab
@ Matlab
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 1
MATLAB INTERFACES
Workspace
Command Window
Command History
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 2
MATLAB INTERFACES CONTD….
❑ Command Window
❑ type commands
❑ Current Directory
❑ View folders and m-
files
❑ Workspace
❑ View program
variables
❑ Double click on a
variable to see it in
the Array Editor
❑ Command History
– view past commands
– save a whole session
using diary
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 3
USE OF M-FILE
Click to create
a new M-File
• Extension “.m”
• A text file containing script or function or program to run
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 4
USE OF M-FILE CONTD….
Save file as Prog.m
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 5
WHAT IS MATLAB?
❑ Abbreviation of MATrix LABoratory
❑ Is a high-performance language for technical computing
❑ It integrates computation, visualization, and programming in an easy-to-use
environment where problems and solutions are expressed in familiar
mathematical notation.
USAGE OF MATLAB
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 6
MATLAB SYSTEM
MATLAB
Simulink
Applications Develop Tools
Stateflow
Toolboxes
Module Libraries
DATA Data-Accessing Tools
Generating-Codes Tools C
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 7
BUILDUP OF MATLAB
❑ Development Environment
MATLAB desktop and Command Window, a command history, an editor and debugger, and
browsers for viewing help, the workspace, files, and the search path.
❑ Graphics
➢ Display vectors and matrices as graphs
➢ two-dimensional and three-dimensional data visualization
➢ image processing
➢ animation and presentation graphics
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 8
MATLAB SPECIAL VARIABLES
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 9
MATLAB MATH & ASSIGNMENT OPERATORS
Power ^ or .^ a^b or a.^b
Multiplication * or .* a*b or a.*b
Division / or ./ a/b or a./b
or \ or .\ b\a or b.\a
- (unary) + (unary)
Addition + a+b
Subtraction - a-b
Assignment = a=b (assign b to a)
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 10
OTHER MATLAB SYMBOLS
>> prompt
... continue statement on next line
, separate statements and data
% start comment which ends at end of line
; (1) suppress output
(2) used as a row separator in a matrix
: specify range
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 11
MATLAB RELATIONAL OPERATORS
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 12
MATLAB LOGICAL OPERATORS
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 13
What does Matlab code look like?
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 14
MATRICES
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 15
MATRICES CONTD….
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 16
MATRICES CONTD….
A = [2 7 4] 2 7 4
2
A = [2; 7; 4]
7
4
2 7 4
A = [2 7 4; 3 8 9]
3 8 9
2 7 4 2 7 4
B = [ AA ] 3 8 9 3 8 9
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 17
MATRICES CONTD….
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 18
VARIABLES
❑ All variables are created with double precision unless specified and they are
matrices.
Example:
>>x=5;
>>x1=2;
❑ After these statements, the variables are 1x1 matrices with double precision
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 19
ARRAY, MATRIX
• a vector x = [1 2 5 1]
x =
1 2 5 1
• a matrix x = [1 2 3; 5 1 4; 3 2 -1]
x =
1 2 3
5 1 4
3 2 -1
• transpose y = x’ y =
1 5 3
2 1 2
3 4 -1
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 20
LONG ARRAY, MATRIX
• t =1:10
t =
1 2 3 4 5 6 7 8 9 10
• k =2:-0.5:-1
k =
2 1.5 1 0.5 0 -0.5 -1
• B = [1:4; 5:8]
B =
1 2 3 4
5 6 7 8
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 21
GENERATING VECTOR FROM FUNCTIONS
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 22
MATRIX INDEX
❑ The matrix indices begin from 1 (not 0 (as in C))
❑ The matrix indices must be positive integer
Given:
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 23
CONCATENTION OF MATRICES
In formal language theory and computer programming, string concatenation is the
operation of joining character strings end-to-end.
A = [ x y] 1 2 4 5
B = [x ; y] 12
45
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 24
MATRIX OPERATION
Given A and B:
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 25
USE OF “.” – “ELEMENT” OPERATION
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
USE OF “.” – “ELEMENT” OPERATION
3 2 -1
x= y= b= c= d=
1 2 3 3 2 -1 3 4 -3 0.33 1.0 -3 1 4 9
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 26
CONTROL STRUCTURES
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 27
CONTROL STRUCTURES CONTD….
for j=1:3:200
for i=Index_Array Some Matlab Commands;
end
Matlab
for m=13:-0.2:-21
Commands Some Matlab Commands;
end
end
for k=[0.1 0.3 -13 12 7 -9.3]
Some Matlab Commands;
end
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 28
CONTROL STRUCTURES CONTD….
• While Loop
Syntax
Dummy Example
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 29
C++ VS MATLAB
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 30
C++ VS MATLAB CONTD….
int j;
while N> 0,
E = E + F;
while (j<28)
F = A*F/N;
{
N = N + 1;
…….;
end
}
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 31
C++ VS MATLAB CONTD….
If (I = = j) if i == j
{ A[i][j]=2; A(i,j) = 2;
} elseif abs(i-j) ~= 1
else if (abs(i-j)!=1) A(i,j) = -1;
{ else
…….; A(i,j) = 0;
} end
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 32
PLOT
PLOT Linear plot. Example
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 33
PLOT PROPERTIES
Example
XLABEL X-axis label.
...
• XLABEL('text') adds text xlabel('x values');
beside the X-axis on the ylabel('y values');
current axis.
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 34
HOLD
Example
...
HOLD Hold current graph. hold on;
y2 = x + 2;
• HOLD ON holds the current plot(x, y2, 'g+:');
plot and all axis properties
so that subsequent graphing
commands add to the
existing graph.
• HOLD OFF returns to the
default mode
• HOLD, by itself, toggles the
hold state.
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 35
SUBPLOT
SUBPLOT Create axes in tiled
positions.
• SUBPLOT(m,n,p), or
SUBPLOT(mnp), breaks the Figure
window into an m-by-n matrix of
small axes
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 on the top
subplot(2,1,1);
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 on the bottom
subplot(2,1,2);
y2 = x + 2;
plot(x, y2, 'g+:');
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 36
FIGURE
FIGURE Create figure window.
• FIGURE, by itself, creates a
new figure window, and returns
its handle.
Example
x = [-3 -2 -1 0 1 2 3];
y1 = (x.^2) -1;
% Plot y1 in the 1st Figure
plot(x, y1,'bo-.');
xlabel('x values');
ylabel('y values');
% Plot y2 in the 2nd Figure
figure
y2 = x + 2;
plot(x, y2, 'g+:');
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 37
SURFACE PLOT
x = 0:0.1:2;
y = 0:0.1:2;
[xx, yy] = meshgrid(x,y);
zz=sin(xx.^2+yy.^2);
surf(xx,yy,zz)
xlabel('X axes')
ylabel('Y axes')
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 38
3D SURFACE PLOT
contour-colorbar-plot3-waterfall-contour3-mesh-surf
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 39
ROOTS OF TRANCENDENTAL EQUATION
NEWTON-RAPHSON METHOD
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 40
Thank you
Department of Water Resources & Ocean Engineering, NITK Surathkal Dr. Debabrata Karmakar 41