Matlab An Introduction Lecture One
Matlab An Introduction Lecture One
TO MATLAB
AM2032 JAYANTA MUKHERJEE
Introduction to MATLAB
MATLAB Desktop
Command Window is used to enter variables Launch Pad is used to and run functions and Launch Pad provide easy access to Command M-files. The Command Command HistoryWindow is window is where you tools, demos, and to the view previously >> symbol, you can interact with used After can type the commands documentation. Matlab directly. Default used functions, and Command directory on working selected lines can be History Windows is C:/ MATLAB / bin. copied and executed.
AM2032 JAYANTA MUKHERJEE
Workspace
Current Directory
MATLAB Editor
Access to commands
Online resource
AM2032 JAYANTA MUKHERJEE
MATLAB Variables
The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. Variable names ARE case sensitive. Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer). Variable names must start with a letter followed by letters, digits, and underscores. Variable names and their types do not have to be declared in MATLAB. Any variable can take real, complex, and integer values. The name of variable is not accepted if it is reserved word.
AM2032 JAYANTA MUKHERJEE
>> A=3 A= 3
3 A
volume
radius
AM2032 JAYANTA MUKHERJEE
height
Description
Addition Subtraction
.*
./ .\ =
:
.^ ' *
/
\ ; ^
Description
Returns 1 for every element location that is true (nonzero) in both arrays, and 0 for all other elements.
Returns 1 for every element location that is true (nonzero) in either one or the other, or both, arrays and 0 for all other elements. Complements each element of input array, A. Less than Less than or equal to Greater than Greater than or equal to Equal to Not equal to
AM2032 JAYANTA MUKHERJEE
-5/(4.8+5.32)^2 ans = -0.0488 (3+4i)*(3-4i) ans = 25 cos(pi/2) ans = 6.1230e-017 exp(acos(0.3)) ans = 3.5470
x = 5/2*pi;
y = sin(x) y = 1 z = asin(y) z = 1.5708
A in MATLAB ?
A (2,4) >>A=[4 10 1 6 2
1 2
10
11
16
21
1.2 7
5
8
9
7
12
4
1
17
25 22
11 23
Rows (m) 3 4
7.2 3
13
18
0
23
0.5 9
14
5
0
19
56 24
10 25
Rectangular Matrix: 23 83 13 0 10 ]; Scalar: 1-by-1 array m-by-1 array >>A=[4 10 1 6 2; 8 1.2 4EITHER 25; 7.2 5 7 1 11; 0 0.5 4 Vector: 5 56; 23 83 13 0 10 ]; Matrix elements can 9 be 1-by-n array numbers OR characters Matrix: m-by-n array
AM2032 JAYANTA MUKHERJEE
Copyright 1984 - 1998 by The MathWorks, Inc.
83 10 13 15
20
Examples (Vectors)
X=[2 7 4]; X=[2; 7; 4]; X=[2 7 4];
2 7 4
2 7 4
2 7 4 3 8 9
Y=[X X];
2 7 ? 4 2 7 4 3 8 9 3 8 9
More on Vectors
x = start:end Creates row vector x starting with start, counting by 1 , ending at end x = initial value : increment : final Creates row vector x starting with start, counting by value increment, ending at or before end x = linspace(start,end,number) x = logspace(start,end,number) length(x) y = x dot(x,y),cross(x,y) Creates linearly spaced row vector x starting with start, ending at end, having number elements Creates logarithmically spaced row vector x starting with start, ending with end, having number elements Returns the length of vector x Transpose of vector x Returns the scalar dot and vector cross product of the vector x and y
AM2032 JAYANTA MUKHERJEE
More on Matrices
zeros(n) zeros(m,n) rand(m,n) Returns a n n matrix of zeros Returns a m n matrix of zeros Returns a m n matrix of random numbers
eye(m,n)
ones(n) ones(m,n)
size(A)
length(A)
a =
1 3 b = 2 4
10.5000
23.0000
1 2 y = 2:-0.5:0
y = 2.0000 1.5000 z = rand(2,4)
1.0000
0.5000
z =
0.9501 0.2311 0.6068 0.4860 0.8913 0.7621 0.4565 0.0185
a =
1 3 2 4
4 8 8 16 12 24
4*a
A=
1 2
4 8
10
1 9 7 4
11
6 4 1 5 0
16
21
1.2 7 5
8
12
17
25 22 11 23 56 24 10 25
A(4:5,2:3) A([9 14;10 15]) A(1:5,5) A(1:end,end) A(:,5) A(:,end) A(21:25) A(21:end)
3 7.2
13
18
A(3,1) A(3)
4
5
0 23
0.5 9
14
19
83 10 13 15
20
Use () parentheses to specify index colon operator (:) specifies range / ALL [ ] to create matrix of index subscripts end specifies maximum index value
AM2032 JAYANTA MUKHERJEE
Copyright 1984 - 1998 by The MathWorks, Inc.
>>B=A*A
9 12
% prints
24 33 >> >>B=A.*A 1 4
% prints
16 25
Operations on Matrices
Transpose B=A
eye(n) -> returns an n X n identity matrix eye(m,n) -> returns an m X n matrix with ones on the main diagonal and zeros elsewhere C =A +B C =A - B B = A, where is a scalar
Identity Matrix
Matrix Multiplication
Matrix Inverse Matrix powers Determinant
C=A*B
B = inv(A), A must be a square matrix in this case B = A * A , A must be a square matrix det(A), A must be a square matrix
AM2032 JAYANTA MUKHERJEE
Multidimensional Arrays
>>A = 1; while length(A) < 4 A = [0 A] + [A 0]; end
Page N
1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 0 0 0 1
Page 1
1 1 1
16 15 9 2 4 3
10
20
Operating on Matrices
c =
1 5 4 12 9 21 16 32
String Arrays
Created using single quote delimiter (')
str
str = Hi there, str2 = 'Isn't MATLAB great?' str2 =
= 'Hi there,'
1x9 vector
abs(x) angle(x) sqrt(x) real(x) imag(x) conj(x) round(x) fix(x) floor(x) ceil(x) sign(x) exp(x) log(x) log10(x) factor(x)
Absolute value of x Phase angle of complex value: If x = real, angle = 0. If x = -1, angle = pi/2 Square root of x Real part of complex value x Imaginary part of complex value x Complex conjugate x Round to do nearest integer Round a real value toward zero Round x toward - Round x toward + +1 if x > 0; -1 if x < 0 Exponential base e Log base e Log base 10 1 if x is a prime number, 0 if not
Plotting in MATLAB
Specify x-data and/or y-data Specify color, line style and marker symbol Syntax: 2-D Plotting
Plotting single line:
plot(xdata, ydata, 'color_linestyle_marker')
Graph Annotation
TITLE LEGEND
YLABEL
TEXT or GTEXT
XLABEL
AM2032 JAYANTA MUKHERJEE
Subplots
SUBPLOT- display multiple axes in the same figure window
subplot(#rows, #cols, index);
subplot(2,2,1);
plot(1:10); subplot(2,2,2); x = 0:.1:2*pi; plot(x,sin(x)); subplot(2,2,3); x = 0:.1:2*pi;
plot(x,exp(-x),r);
subplot(2,2,4); plot(peaks);
AM2032 JAYANTA MUKHERJEE
z = 0:0.1:40; x = cos(z);
y = sin(z);
plot3(x,y,z);grid on;
http://euclid.ucc.ie/framesets/fset_staffdirectory.htm
Then click on my name and then on Lecture Notes
Questions?