Upload
qamra
View
80
Download
16
Tags:
Embed Size (px)
DESCRIPTION
Introduction to Matlab-1. Laboratoire Mathématiques, Informatique et Applications. Lecture 1 – Matlab Basics. What is Matlab?. A software environment for interactive numerical computations Examples: Matrix computations and linear algebra Solving nonlinear equations - PowerPoint PPT Presentation
Citation preview
Introduction to Matlab-1Introduction to Matlab-1Lecture 1 – Matlab Basics
Laboratoire Mathématiques, Informatique et Applications
2Introduction to MatlabIntroduction 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
3Introduction to MatlabIntroduction to Matlab
3
Matlab used (on a daily basis) in many engineering companies
4Introduction to MatlabIntroduction to Matlab
4
Matlab used in many courses
Numerical analysis
Signal and Systems I
Signals and Systems II
Modeling of Dynamical Systems
Automatic Control, Basic Course
Automatic Control, Advanced Course
Nonlinear Control
Hybrid and Embedded Control Systems
Chemical Process Control
Control Project Course
Signal Theory
Digital Signal Processing
Adaptive Signal Processing
Signal Processing Project
Communication theory
Advanced Communication Theory
<and many, many more>
5Introduction to MatlabIntroduction to Matlab
5
Today’s Lecture
Matlab Basics Background to Matlab Interactive calculations Vectors and matrices Graphical illustrations
Next lecture: Matlab programming
6Introduction to MatlabIntroduction 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 ComplementsMatrix-X Maple (symbolic)Octave (free; GNU) Mathematica (symbolic)Lyme (free; Palm)
7Introduction to MatlabIntroduction 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
m-filesC-kernel
Sig. Proc
Simulink
Contr. Syst.
8Introduction to MatlabIntroduction to Matlab
8
Matlab Desktop
Command Window
Launch Pad
History
9Introduction to MatlabIntroduction to Matlab
9
Matlab Desktop – cont’d
Command Window
Workspace
Current DIrectory
10Introduction to MatlabIntroduction to Matlab
10
Matlab Help
11Introduction to MatlabIntroduction 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.
12Introduction to MatlabIntroduction 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 !?
13Introduction to MatlabIntroduction 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
14Introduction to MatlabIntroduction 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
15Introduction to MatlabIntroduction 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
16Introduction to MatlabIntroduction 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
>>
17Introduction to MatlabIntroduction to Matlab
17
Workspace
The workspace is Matlab’s memory Can manipulate variables stored in the
workspace>> b=10;
>> c=a+b
c =
22
>>
18Introduction to MatlabIntroduction 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
19Introduction to MatlabIntroduction to Matlab
19
The : operatorVERY 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)
20Introduction to MatlabIntroduction to Matlab
20
The : operator and matrices
>>A(3,2:3)
ans =
1 7
>>A(:,2)
ans =
2
1
1
A =
3 2 1
5 1 0
2 1 7
What’ll happen if you type A(:,:) ?
21Introduction to MatlabIntroduction to Matlab
21
Vectors and MatricesVectors (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]
22Introduction to MatlabIntroduction to Matlab
22
Matrix OperatorsAll 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
23Introduction to MatlabIntroduction to Matlab
23
Indexing MatricesIndexing using parentheses>> A(2,3)
Index submatrices using vectorsof row and column indices >> A([2 3],[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)]
Rows no. 2, 3 Column no. 1, 2
24Introduction to MatlabIntroduction to Matlab
24
Indexing MatricesIndex 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
25Introduction to MatlabIntroduction to Matlab
25
Matrix FunctionsMany 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
26Introduction to MatlabIntroduction to Matlab
26
Manipulating Matrices
>> 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)
A =
3 2 1
5 1 0
2 1 7
B =
1 3 1
4 9 5
2 7 2
Create matrices A and B and try out the the matrix operators in this slide
Enter matrix B into the Matlab workspace
27Introduction to MatlabIntroduction to Matlab
27
Numerical Linear AlgebraBasic 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)
28Introduction to MatlabIntroduction to Matlab
28
Matrix vs Element Math
Element Math
16 9
4 1
4 3
2 1*
4 3
2 1
Matrix Math
22 15
10 7
4 3
2 1*
4 3
2 1
29Introduction to MatlabIntroduction to Matlab
29
Circuit Example
+10V
-
10 Ohms3uF
0.01 H
I=V/R=10 / 01.0**10*3**
110
6
j
j
30Introduction to MatlabIntroduction 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 unless its redefined1
31Introduction to MatlabIntroduction to Matlab
31
RLC series result
32Introduction to MatlabIntroduction 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)’);
33Introduction to MatlabIntroduction to Matlab
33
0 1 2 3 4 5 6 7-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1Sample Plots
y
Sin()Cos()
% 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');
34Introduction to MatlabIntroduction 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)
35Introduction to MatlabIntroduction 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
36Introduction to MatlabIntroduction to Matlab
36
Three-dimensional graphics>> A = zeros(32);
>> A(14:16,14:16) = ones(3);
>> F=abs(fft2(A));
>> mesh(F)
>> rotate3d on
37Introduction to MatlabIntroduction to Matlab
37
GraphicsThree-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
38Introduction to MatlabIntroduction to Matlab
38
GraphicsBitmap images can also be visualized
>> load mandrill
>> image(X); colormap(map)
>> axis image off
39Introduction to MatlabIntroduction 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/
40Introduction to MatlabIntroduction to Matlab
40
Next Lecture
Programming in MATLAB