Introduction to Matlab-1

Introduction to Matlab-1

Introduction to Matlab-1

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

1. Introduction to Matlab-1 Laboratoire Mathématiques, Informatique et Applications Lecture 1 – Matlab Basics

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

3. Matlab used (on a daily basis) in many engineering companies

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>

5. Today’s Lecture • Matlab Basics • Background to Matlab • Interactive calculations • Vectors and matrices • Graphical illustrations • Next lecture: Matlab programming

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) • AlternativesComplements • Matrix-X Maple (symbolic)Octave (free; GNU) Mathematica (symbolic)Lyme (free; Palm)

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 Contr. Syst. Sig. Proc Simulink m-files C-kernel

8. Matlab Desktop Launch Pad Command Window History

9. Matlab Desktop – cont’d Workspace Command Window Current DIrectory

10. Matlab Help

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.

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 !?

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

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

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 comment operator suppress command output assign operator Try the same line without the semicolon and comments

16. Variables (continued …) • View variable contents by simply typing the variable name at the command prompt • >> a • a = • 12 • >> • >> a*2 • a = • 24 • >>

17. Workspace • The workspace is Matlab’s memory • Can manipulate variables stored in the workspace • >> b=10; • >> c=a+b • c = • 22 • >>

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

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)

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(:,:) ?

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]

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

23. Indexing Matrices • Indexing 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)] Column no. 1, 2 Rows no. 2, 3

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

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

26. Manipulating Matrices A = 3 2 1 5 1 0 2 1 7 • >> 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 3 1 4 9 5 2 7 2 Enter matrix B into the Matlab workspace Create matrices A and B and try out the the matrix operators in this slide

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)

28. Matrix vs Element Math Element Math Matrix Math

29. Circuit Example 10 Ohms + 10V - 3uF 0.01 H I=V/R=10 /

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 redefined

31. RLC series result

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)’);

33. % 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');

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)

35. Graphics • Three-dimensional graphics • >> A = zeros(32); • >> A(14:16,14:16) = ones(3); • >> F=abs(fft2(A)); • >> mesh(F) >> rotate3d on

36. Three-dimensional graphics • >> A = zeros(32); • >> A(14:16,14:16) = ones(3); • >> F=abs(fft2(A)); • >> mesh(F) >> rotate3d on

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

38. Graphics • Bitmap images can also be visualized • >> load mandrill • >> image(X); colormap(map) • >> axis image off

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/

40. Next Lecture • Programming in MATLAB