Introduction to Matlab

advertisement
Introduction to Matlab-1
Lecture 1 – Matlab Basics
Laboratoire Mathématiques,
Informatique et Applications
Introduction to Matlab
2
What is Matlab?
 A software environment for interactive numerical
computations
 Examples:









Matrix computations and linear algebra
Solving nonlinear equations
Numerical solution of differential equations
Mathematical optimization
Statistics and data analysis
Signal processing
Modelling of dynamical systems
Solving partial differential equations
Simulation of engineering systems
2
Introduction to Matlab
3
Matlab used (on a daily basis) in many engineering
companies
3
Introduction to Matlab
4
Matlab used in many courses
Numerical analysis
Chemical Process Control
Signal and Systems I
Control Project Course
Signals and Systems II
Signal Theory
Modeling of Dynamical Systems
Digital Signal Processing
Automatic Control, Basic Course
Adaptive Signal Processing
Automatic Control, Advanced Course
Signal Processing Project
Nonlinear Control
Communication theory
Hybrid and Embedded Control Systems
Advanced Communication Theory
<and many, many more>
4
Introduction to Matlab
5
Today’s Lecture
Matlab Basics




Background to Matlab
Interactive calculations
Vectors and matrices
Graphical illustrations
Next lecture: Matlab programming
5
Introduction to Matlab
6
Matlab Background
Matlab = Matrix Laboratory
 Originally a user interface for numerical linear algebra routines
 Commercialized 1984 by The Mathworks
 Since then heavily extended (defacto-standard)
Alternatives
Complements
Matrix-X
Octave
Lyme
Maple
Mathematica
(free; GNU)
(free; Palm)
(symbolic)
(symbolic)
6
Introduction to Matlab
7
Construction
Core functionality: compiled C-routines
Most functionality is given as m-files, grouped into toolboxes
 m-files contain source code, can be copied and altered
 m-files are platform independent (PC, Unix/Linux, MAC)
Simulation of dynamical systems is performed in Simulink
Sig. Proc
C-kernel
Contr. Syst.
m-files
Simulink
7
Introduction to Matlab
8
Matlab Desktop
Launch Pad
Command
Window
History
8
Introduction to Matlab
9
Matlab Desktop – cont’d
Workspace
Command
Window
Current
DIrectory
9
Introduction to Matlab
10
Matlab Help
10
Introduction to Matlab
11
MATLAB Demo
 Demonstrations are invaluable since they give an
indication of the MATLAB capabilities.
 A comprehensive set are available by typing the
command >>demo in MATLAB prompt.
11
Introduction to Matlab
12
Interactive Calculations
Matlab is interactive, no need to declare variables
>> 2+3*4/2
>> a=5e-3; b=1; a+b
Most elementary functions and constants are already defined
>> cos(pi)
>> abs(1+i)
>> sin(pi)
Last call gives answer 1.2246e-016 !?
12
Introduction to Matlab
13
Variable and Memory Management
Matlab uses double precision (approx. 16 significant digits)
>> format long
>> format compact
All variables are shown with
>> who
>> whos
Variables can be stored on file
>> save filename
>> clear
>> load filename
13
Introduction to Matlab
14
The Help System
Search for appropriate function
>> lookfor keyword
Rapid help with syntax and function definition
>> help function
An advanced hyperlinked help system is launched by
>> helpdesk
Complete manuals as PDF files
14
Introduction to Matlab
15
Variables
 Don’t have to declare type
 Don’t even have to initialise
 Just assign in command window
>>
>> a=12; % variable a is assigned 12
Matlab
prompt
assign
operator
suppress
command
output
comment
operator
Try the same line without the
semicolon and comments
15
Introduction to Matlab
16
Variables (continued …)
 View variable contents by simply typing the variable
name at the command prompt
>> a
a =
12
>>
>> a*2
a =
24
>>
16
Introduction to Matlab
17
Workspace
 The workspace is Matlab’s memory
 Can manipulate variables stored in the workspace
>> b=10;
>> c=a+b
c =
22
>>
17
Introduction to Matlab
18
Workspace (continued …)
Display contents of workspace
>> whos
Name
Size
Bytes Class
a
1x1
8 double array
b
1x1
8 double array
c
1x1
8 double array
Grand total is 3 elements using 24 bytes
>>
Delete variable(s) from workspace
>> clear a b; % delete a and b from workspace
>> whos
>> clear all; % delete all variables from workspace
>> whos
18
Introduction to Matlab
19
The : operator
VERY important operator in Matlab
Means ‘to’
>> 1:10
ans =
1
2
3
4
5
6
7
8
9
10
>> 1:2:10
ans =
1
3
5
7
9
Try the following
>> x=0:pi/12:2*pi;
>> y=sin(x)
19
Introduction to Matlab
20
The : operator and matrices
>>A(3,2:3)
ans =
1
>>A(:,2)
ans =
7
A=
3
5
2
2
1
1
1
0
7
2
1
1
What’ll happen if you type A(:,:) ?
20
Introduction to Matlab
21
Vectors and Matrices
Vectors (arrays) are defined as
>> v = [1, 2, 4, 5]
>> w = [1; 2; 4; 5]
Matrices (2D arrays) defined similarly
>> A = [1,2,3;4,-5,6;5,-6,7]
21
Introduction to Matlab
22
Matrix Operators
All common operators are overloaded
>> v + 2
Common operators are available
>> B = A’
>> A*B
>> A+B
Note:
 Matlab is case-sensitive
A and a are two different variables
22
Introduction to Matlab
23
Indexing Matrices
Indexing using parentheses
>> A(2,3)
Index submatrices using vectors
of row and column indices
>> A([2 3],[1 2])
Rows no. 2, 3
Column no. 1, 2
Ordering of indices is important!
>> B=A([3 2],[2 1])
>> B=[A(3,2),A(3,1);A(2,2),A(2,1)]
23
Introduction to Matlab
24
Indexing Matrices
Index complete row or column using
the colon operator
>> A(1,:)
Can also add limit index range
>> A(1:2,:)
>> A([1 2],:)
General notation for colon operator
>> v=1:5
>> w=1:2:5
24
Introduction to Matlab
25
Matrix Functions
Many elementary matrices predefined
>> help elmat;
>> I=eye(3)
Elementary functions are often overloaded
>> help elmat
>> sin(A)
Specialized matrix functions and operators
>> As=sqrtm(A)
>> As^2
>> A.*A
Note: in general, ”.<operator>” is elementwise operation
25
Introduction to Matlab
Manipulating Matrices
26
A=
3
5
2
>> A '
% transpose
>> B*A
% matrix multiplication
>> B.*A
% element by element multiplication
>> B/A
% matrix division
>> B./A
% element by element division
>> [B A]
% Join matrices (horizontally)
>> [B; A]
% Join matrices (vertically)
B=
1
4
2
2
1
1
1
0
7
3
9
7
1
5
2
Enter matrix B
into the Matlab
workspace
Create matrices A and B and try out the the matrix operators in this slide
26
Introduction to Matlab
27
Numerical Linear Algebra
Basic numerical linear algebra
>> z=[1;2;3]; x=inv(A)*z
>> x=A\z
Many standard functions predefined
>> det(A)
>> rank(A)
>> eig(A)
The number of input/output arguments can often be varied
>> [V,D]=eig(A)
27
Introduction to Matlab
28
Matrix vs Element Math
Element Math
1 2  1 2  1 4 
3 4  *3 4  9 16

 
 

Matrix Math
1 2  1 2  7 10
3 4 * 3 4  15 22

 
 

28
Introduction to Matlab
29
Circuit Example
+
10V
-
10 Ohms
3uF
0.01 H
I=V/R=10 / 10 
1
 j *  * 0.01
6
j *  * 3 *10
29
Introduction to Matlab
30
RLC Series .m File
%Example4.m - RLC
w=linspace(1,40000,1000);
i=10./(10+(1./(j.*w.*3.*10^-6))+(j.*w.*0.01));
plot(w,abs(v));
j is intrinsically 1
unless its redefined
30
Introduction to Matlab
31
RLC series result
31
Introduction to Matlab
32
Graphics
Visualization of vector data is available
>> x=-pi:0.1:pi; y=sin(x);
>> plot(x,y)
>> plot(x,y,’s-’)
>> xlabel(’x’);
>> ylabel(’y=sin(x)’);
32
Introduction to Matlab
33
Sample Plots
1
Sin()
Cos()
0.8
0.6
0.4
y
0.2
0
-0.2
-0.4
-0.6
-0.8
-1
0
1
2
3
4
5
6
7

% print the cosine and sine
u = 0:pi/20:2*pi;
w = sin(u);
v = cos(u);
figure(1);
plot(u,w,'ro-') ;
hold on;
plot(u,v,'gs:');
xlabel('\theta');ylabel('y');
legend('Sin(\theta)','Cos(\theta)');
title('Sample Plots');
33
Introduction to Matlab
34
Can change plot properties in Figure menu, or via ”handle”
>> h=plot(x,y); set(h, ’LineWidth’, 4);
Many other plot functions available
>> v=1:4; pie(v)
34
Introduction to Matlab
35
Graphics
Three-dimensional graphics
>> A = zeros(32);
>> A(14:16,14:16) = ones(3);
>> F=abs(fft2(A));
>> mesh(F)
>> rotate3d on
35
Introduction to Matlab
36
Three-dimensional graphics
>> A = zeros(32);
>> A(14:16,14:16) = ones(3);
>> F=abs(fft2(A));
>> mesh(F)
>> rotate3d on
36
Introduction to Matlab
37
Graphics
Three-dimensional graphics
>> A = zeros(32);
>> A(14:16,14:16) = ones(3);
>> F=abs(fft2(A));
>> mesh(F)
>> rotate3d on
Several other plot functions available
>> surfl(F)
Can change lightning and material properties
>> cameramenu
>> material metal
37
Introduction to Matlab
38
Graphics
Bitmap images can also be visualized
>> load mandrill
>> image(X); colormap(map)
>> axis image off
38
Introduction to Matlab
39
MATLAB - Reference:
A Partial List of On-Line Matlab Tutorials and Matlab Books
 http://www.duke.edu/~hpgavin/matlab.html
Getting started with Matlab
 http://www.engr.iupui.edu/ee/courses/matlab/tutorial_start.htm
A quick reference to some of the key features of Matlab
 http://science.ntu.ac.uk/msor/ccb/matlab.html#MATLAB
Matlab tutorial, with lots of links to other tutorials
 http://www.glue.umd.edu/~nsw/ench250/matlab.htm
Control Tutorial for Matlab
 http://www.engin.umich.edu/group/ctm/home.text.html
A Practical Introduction to Matlab
 http://www.math.mtu.edu/~msgocken/intro/intro.html
Matlab Tutorial Information from UNH
 http://spicerack.sr.unh.edu/~mathadm/tutorial/software/matlab/
39
Introduction to Matlab
40
Next Lecture
Programming in MATLAB
40
Download