Professional Documents
Culture Documents
Matlab
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
Matlab
MATLAB stands for Matrix Laboratory.
Matlab has many functions and toolboxes to
help in various applications
It allows you to solve many technical
computing problems, especially those with
matrix and vector formulas,
MATLAB’s Features
Main Features
Simple programming rules
Extended accuracy
Continuity among integer, real and complex
values
Comprehensive mathematical library
Extensive graphics tools
Linkages with other languages
Transportability across environment
MATLAB scripts will work on PC, UNIX, Mac
Matlab GUI
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
MATLAB GUI
Workspace Window
Shows all currently defined variables
Array dimensions
Min, max values
Good debugging tool
Command History
Shows all past commands
Can copy and past commands into
command window
Double click will execute command
MATLAB GUI
Desktop Menus
File Menu
New
Create new MATLAB
program file (m-file)
Open existing m-file
Import data
Set Path
Open recent m-files
MATLAB GUI
Edit Menu
Copy, cut, paste
Find and replace
phrases
Clear command
history, workspace
Desktop Menu
Change
appearance of
desktop
Select windows to
display
MATLAB’s Commands
Format of output
Defaults to 4 decimal places
Can change using format statement
to 4 decimal places
Variables
int a;
double b;
float c;
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 Apostrophe symbol
transpose y = x’ y =
1
2
5
1
Long Array, Matrix
By default step size is “1” in matlab,
t =1:10
you can modify it as your requirement.
t =
1 2 3 4 5 6 7 8 9 10
k =1:0.5:5
k =
1 1.5 2 2.5 3 3.5 4 4.5 5
B = [1:4; 5:8]
x =
1 2 3 4
5 6 7 8
Generating Vectors from functions
zeros(M,N) MxN matrix of zeros x = zeros(1,3)
x =
0 0 0
Columns
(n)
1 2 3 4 5
A= 4
1
10
6
1
11
6
16
2
21
A (2,4)
1
2
2 8 1.2 7 9 12
4 17
25 22
A(-2), A(0)
Error: ??? Subscript indices must either be real positive integers or logicals.
A(4,2)
Error: ??? Index exceeds matrix dimensions.
Concatenation of Matrices
x = [1 2], y = [4 5], z=[ 0 0]
A = [ x y]
Horizontal concatenation
1 2 4 5
B = [x ; y]
Vertical concatenation
1 2
4 5
C = [x y ;z]
Error:
??? Error using ==> vertcat CAT arguments dimensions are not consistent.
Matrices Operations
Given A and B:
.* element-by-element multiplication
./ element-by-element division
.^ element-by-element power
The use of “.” – “Element” Operation
A = [1 2 3; 5 1 4; 3 2 1]
A=
1 2 3
5 1 4
3 2 1
b = x .* y c=x./y d = x .^2
x = A(1,:) y = A(3 ,:)
b= c= d=
x= y= 3 4 3 0.33 1 3 1 4 9
1 2 3 3 2 1
K= x^2
Erorr:
??? Error using ==> mpower Matrix must be square.
B=x*y
Erorr:
??? Error using ==> mtimes Inner matrix dimensions must agree.
Matrix Multiplication
Inner dimensions must be equal
Dimension of resulting matrix = outermost
dimensions of multiplied matrices
Resulting elements = dot product of the rows of
the 1st matrix with the columns of the 2nd
matrix
» a = [1 2 3 4; 5 6 7 8]; [2x4]
» b = ones(4,3); [4x3]
» c = a*b [2x4]*[4x3] [2x3]
c =
10 10 10
26 26 26 a(2nd row).b(3rd column)
Inverse of Matrix
• Extension “.m”
• A text file containing script or function or program to run
Use of M-File Save file as Denem430.m
0.8
>>y=sin(x); 0.6
0.4
0.2
-0.2
-0.4
-0.6
>>plot(x,y) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
Plot the function e-x/3sin(x) between
0≤x≤4π
Create an x-array of 100 samples between 0
and 4π.
>>x=linspace(0,4*pi,100);
>>plot(x,y2) 0.6
0.5
0.4
0.3
0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
0.7
plot(.) 0.6
0.5
0.4
Example: 0.3
>>x=linspace(0,4*pi,100);
0.2
0.1
>>y=sin(x); 0
>>plot(x,y) -0.1
-0.2
-0.3
stem(.)
0 10 20 30 40 50 60 70 80 90 100
0.7
0.6
0.5
0.4
Example: 0.3
>>stem(x,y) 0.2
0.1
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
title(.)
>>title(‘This is the sinus function’)
This is the sinus function
1
0.8
xlabel(.) 0.6
0.4
sin(x)
0
-0.2
ylabel(.) -0.4
-0.6
-0.8
>>ylabel(‘sin(x)’) -1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
Displaying Multiple Plots in One Figure
subplot(m,n,p)
This splits the figure window into an m-by-n matrix of
small subplots and selects the pth subplot for the current
plot
m = Number of grid rows
1 (default) | positive integer
n = Number of grid columns
1 (default) | positive integer
p = Grid position for new axes
(positive integer)
Subplot
x = linspace(0,10);
y1 = sin(x);
y2 = sin(5*x);
figure
subplot(2,1,1);
plot(x,y1)
subplot(2,1,2);
plot(x,y2)
Flow Control
if
for
while
break
….
Control Structures
Some Dummy Examples
If Statement Syntax
if ((a>3) & (b==5))
Some Matlab Commands;
if (Condition_1) end
Matlab Commands
if (a<3)
elseif (Condition_2) Some Matlab Commands;
Matlab Commands elseif (b~=5)
Some Matlab Commands;
elseif (Condition_3) end
Matlab Commands
if (a<3)
else Some Matlab Commands;
Matlab Commands else
end Some Matlab Commands;
end
“if ” Example
If size(A) and size(B) are the same, concanate the arrays;
otherwise, display a warning and return an empty array.
A = ones(2,3); B = rand(3,4,5);
if isequal(size(A),size(B))
C = [A; B];
else disp('A and B are not the same size.')
C= []
end
Control Structures
Some Dummy Examples
For loop syntax for i=1:100
Some Matlab Commands;
end
for v = [0:1:10]
disp(v)
end
Control Structures
Dummy Example
while (condition)
Matlab Commands while ((a>3) & (b==5))
Some Matlab Commands;
end end
H/Ws
Same Name
Writing User Defined Functions
Another function which takes an input array and returns the sum and product
of its elements as outputs
Books
Two that seem to be good:
The Art of Readable Code (Simple and Practical
Techniques for Writing Better Code) by Dustin Boswel
and Trevor Foucher.
Essential MATLAB for Engineers and Scientists (Fourth
Edition) by Brian Hahn and Dan Valentine. 2010,
Academic Press, 480 pp.
Online tutorials and examples are
everywhere
LETS JUST END UP HERE!