Академический Документы
Профессиональный Документы
Культура Документы
MATLAB Basics
Todays Lesson
Introduction to MATLAB
Variables and Operators
Built-in Functions
Vectors and Matrices
M-files
Introduction
MATLAB (Matrix Laboratory)
Everything in MATLAB is a Matrix
Powerful graphical computation Tool
The MathWorks Inc. (http://www.mathworks.com)
MATLAB 6 (Release 12 and 13)
MATLAB 7 (Release 14) and 7.5
Workspace
Current Directory
Command History
Command Prompt
Command Window
MATLAB Help
helpdesk : Will open help window
help function : Give information about
the function on the command window
E.g. help sqrt
Built-in variables
i, j (for complex numbers), pi (), ans, Inf
(Infinity), NaN (Not a number)
MATLAB Output
disp () : Print output to the screen
disp(Hi, How are you.)
disp(a)
Built-in Functions
MATLAB has a library of built-in functions
Sqrt(2), log(), log10(), cos()
floor(), ceil(), abs(), clock, factor()
sum(), prod(), exp(),
Exercise
Which of the following is the best
approximation to the square root of 7
2709/1024
10583/4000
2024/765
Column Vector
b = [10; 20; 30]
or
transpose(a)
Indexing Elements
MATLAB indexing starts from 1 not from 0
a = [ 5 3 4] (Square brackets)
a(1) = 5
a(2) = 3
a(3) = 4
(Parenthesis)
A(2:3) = 3 4
A(1:end-1) = 5 3
length (a) Vector Length
min/max(a)
sort(a) : Sort in Ascending order
find (a == 3)
find (a < 3 & a > 6)
a = 1:10
Matrices
a = [2 3 4; 5 6 7]
(2x3 Matrix)
3
6
4
7
Concatenating Matrices
A = [10 20]
D = [a b]
E = [a; b]
F = [e c]
b = [30 40]
c = [50; 60]
=>
[4 25; 16 1]
Exercise
Find a way to delete 0s from a vector
M-Files
Scripts can be run with MATLAB
Collection of commands
Executed in sequence
Use MATLAB editor
Save as MATLAB file (Extension .m)
Column Vector
b = [10; 20; 30]
or
transpose(a)
Indexing Elements
MATLAB indexing starts from 1 not from 0
a = [ 5 3 4] (Square brackets)
a(1) = 5
a(2) = 3
a(3) = 4 (Parenthesis)
A(2:3) = 3 4
A(1:end-1) = 5 3
a = 1:10
Matrices
Matrix is a two dimensional array of items
a = [2 3 4; 5 6 7]
(2x3 Matrix)
3
6
4
7
Concatenating Matrices
A = [10 20]
D = [a b]
E = [a; b]
F = [e c]
b = [30 40]
c = [50; 60]
b = [5 10; 3 2]
a.^2
=>
[4 25; 16 1]
a.*b
=>
[10 50; 12 2]
Matrix Operations
=>
3*A
=>
12
30
18
60
B
6
20
2
6
A-B
4
8
=>
2
4
2
12
B3x1
=>
AB2x1
10
2x10+4x5+3x2
46
5
2
5x10+1x5+2*2
59
Exercise
Use clock function to find the number of
hours left to the end of this month
Conditional Controls
Relational Operators
equal
==
not equal ~=
greater than or equal
>=
Logical Operators
And
Not
All true
0 is false
&
~
all
Or
Any true
any
if-else
Conditional statement will be evaluated for true
or false
if condition
command block 1
else
command block 2
end
elseif can be used for more than one conditions
e. g.
n = 4;
if n > 3
x = A
else
x = B
end
for Loop
Use for a know number of iterations
Loop variable can be sequential or can be
given as a vector
for loop variable = start: end
command block
end
e. g.
for n = 1:100
fprintf (n = %d\n, n);
end
while Loop
Do not know about the number of iterations
while condition
command block
end
switch
Execute one set among several
switch number
case 1
command block 1
case 2
command block 2
|
case n
command block n
end
e. g.
User-defined Functions
Function must have a functional declaration
function [avg, range] = stats(x)
Outputs
Inputs
M-file (stats.m)
function [s, r]=stats(x);
s = sum(x)
r = max(x) - min(x)
Command Prompt
>> a = [2 4 1]
>> stats(a)
s=
7
r=
3
Exercise
Write a function to delete 0s from a
vector
2D Graphs
plot() cab be used to draw 2D graphs
plot (x, y, c - *)
Color
Style
Marker
x = 0:0.5:20
% x values
y = x.^2+3*x-3
% y values
plot (x, y, c - -)
title(2D Graph)
% Add Title
xlabel(X)
ylabel(Y)
grid on
% Add Grids
hold on
Plot Strings
figure
x = -pi:pi/40:pi
plot(x, cos(x), y-o, x, sin(x), c.-)
h1 = legend (cos_x, sin_x)
set(h1, 'Location', 'NorthWest')
text(2, 0.5, cos(x))
3D Line Plots
time=0:0.001:4*pi;
x=sin(time);
y=cos(time);
z=time;
plot3(x,y,z,'k','LineWidth',2);
zlabel('Time');
subplot(2, 2, 3)
subplot(2, 2, 2)
subplot(2, 2, 4)
x = -pi:pi/40:pi
subplot(1, 3, 1)
plot(x, sin(x))
title (sin)
% First graph
subplot(1, 3, 2)
plot(x, cos(x))
title (cos)
% Second graph
subplot(1, 3, 3)
plot(x, tan(x))
title (tan)
% Third graph
Surface Plotting
[X,Y] = meshgrid (-8:.5:8);
R = sqrt(X.^2 + Y.^2) ;
Z = sin(R)./R;
mesh (X,Y,Z,'EdgeColor','black')
surf (X,Y,Z)
colormap hsv
colorbar
Contours
Contours in 2D
[c, h] = contour (X, Y, Z, 20)
Number of Contours
clabel (c, h)
Filled contours
contourf (X, Y, Z, 20)
Contours in 3D
contour3 (X, Y, Z, 20)
File Input/Output
Data can be entered in a Text File
x , y
2 , 32
6 , 65
Read/Write to Excel
Writing to an Excel File
xlswrite(CK, a.data)
Reading from an Excel File
xlsread(CK.xls)