1 / 32

Introduction to Matlab and matrix review

19-Nov-2013 APA 6903 Giulia Mantovani. Introduction to Matlab and matrix review. Mat rix Lab o ratory Created in late 1970’s Intended for used in courses in matrix theory, linear algebra and numerical analysis

rayya
Télécharger la présentation

Introduction to Matlab and matrix review

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 19-Nov-2013 APA 6903 Giulia Mantovani Introduction to Matlab and matrix review

  2. Matrix Laboratory • Created in late 1970’s • Intended for used in courses in matrix theory, linear algebra and numerical analysis • Currently has grown into an interactive system and high level programming language for general scientific and technical computation Why Matlab?

  3. Why Matlab? Common Uses for Matlab in Research • Data Acquisition • Analysis Tools (Existing,Custom) • Statistics • Graphing • Modeling

  4. Tutorial and pre-work • Matlabinterface • Variables and Expressions • Analysis and Visualizations with Vectors • and Matrices Is everything clear

  5. Review of pre-work

  6. You can change the desktop arrangement to meet your needs, including resizing, moving, and closing tools. HowdoesMatlab look like?

  7. 1 2 Help on-line: a basictool

  8. Checklist • Create variables in the command line (Command window) • Specify the current folder • Combination of variables • Using functions (example cos, mean) • Semi-colon to avoid echo in the command window • Array-based language (create column and row vectors t, [], :, linspace,’) • Functions for vector: sin(2*pi*t) • Plot commands: plot(t,y), play with the GUI • Create matrices manually ([;,]), or using functions (ones, zeros) • Operations with matrices: addition of a number, addition between matrices, element-wise multiplication, multiplication of matrices. • Indexing vectors and matrices (one element, one column/row, part, use end) • Assign indexed values to new variables, or change indexed values in the matrix • Save variables (what is MAT format) • Load variables • http://www.mathworks.com/videos/getting-started-with-matlab-68985.html

  9. Using Matlab • Solving equations using variables • Expression language • Expressions typed by the user are interpreted and evaluated by the Matlab system • Variables are names used to store values • Variable names allow stored values to be retrieved for calculations or permanently saved • When MATLAB encounters a new variable name, it automatically creates the variable and allocates the appropriate amount of storage. • Few simple rules for naming variables • Variable = Expression • (!!) Variable Names are Case Sensitive! • Ex. «a» and «A» are two different variables • (!!) Be careful not to overwrite values in your variable by accident!

  10. Using Matlab • Working with Matrices • Spaces, commas, and semicolons are used to separate elements of a matrix • Spaces or commas separate elements of a row • [1 2 3 4] or [1,2,3,4] • Semicolons separate columns • A = [1,2,3,4;5,6,7,8;9,8,7,6] = [1 2 3 4 • 5 6 7 8 • 9 8 7 6] • Colon operator identifies a range of values: • B = [1:4 ; 5:8 ; 9,8,7,6 ] = A

  11. Using Matlab • Working with Matrices • Colon operator identifies a range of values: • [ 1 : 3 ] equivalent to [ 1 , 2 , 3] • [ 1 : 2: 9 ] equivalent to [ 1 , 3 , 5, 7, 9] • [ 5 : -1: 1 ] equivalent to [ 5 , 4, 3 , 2, 1] • B = [1:4 ; 5:8 ; 9,8,7,6 ] equivalent to [1 2 3 4 • 5 6 7 8 • 9 8 7 6] start jump end

  12. Using Matlab • Indexing Matrices • A m x n matrix is defined by the number of m rows and number of n columns • An individual element of a matrix can be specified with the notation A(i,j) where the first number (i) indicates the first dimension of the matrix (the row), and the second indicates the columns • Example: • >> A = [1 2 4 5 • 6 3 8 2] • >> A(2,1) • ans = 6 • The colon operator can be used to index a range of elements • >> A(1:3,2) • ans=1 2 4

  13. Using Matlab • Indexing Matrices • Specific elements of any matrix can be overwritten using the matrix index • Example: • A = [1 2 4 5 • 6 3 8 2] • >> A(1,2) = 9 • Ans • A = [1 2 4 5 • 9 3 8 2]

  14. Using Matlab • Matrix Shortcuts • The ones and zeros functions can be used to create any m x n matrices composed entirely of ones or zeros • Example • a = ones(2,3) • a = [1 1 • 1 1 • 1 1] • (!) if you need to understand more about how to use these functions, go to the online HELP! b = zeros(1,5) b = [0 0 0 0 0]

  15. Using Matlab • Saving your Work • To save data to a *.mat file: • Typing ‘save filename’ on the command window (>> prompt) and the file ‘filename.mat’ will be saved in the current folder (you can see it popping out in the list of files under the ‘current folder’ window) • Or Select Save from the file pull down menu • This command will store all the variables in the workspace in the same *.mat file named filename.mat. • To reload a *.mat file • Type ‘load filename’ on the command window (>> prompt) to load ‘filename.mat’ (ensure the filename is located in the current working directory) • Or Select Open from the file pull down menu and manually find the data file • (!) Again…to see all the options that are offered for a function, type doc namefunction (in this case doc save, or doc load) What is it? *.mat is the extension of data files in Matlab. They contain any type of variable that can be created in Matlab and that show up in the ‘current workspace’ window

  16. Using Matlab Basic plotting commands figure Create figure graphics object hold on Retain current graph in figure axis tight Axis scaling and appearance

  17. M-file: how to save your work • What happens if I have to close Matlab because it’s time to go home??? • I lose all the work I did in the Command Window! • I lose all the variables I created during the current session of work.

  18. M-file: how to save your work • I can write the list of command, functions and variables in a new Script and then save it as file.m • When I need to run again that list of commands I reopen the stored file.m and I can continue my work!

  19. Checklist • Write a simple Matlab program: sin and plot (.m extension) • Run an M-file saved in Current folder (you only see what’s in the CF, including MAT-files): click RUN button, write name of script • Add commands • Loop statement FOR • Conditional statement IF • Clear workspace (commands clearvar, clc) • Create functions • http://www.mathworks.com/videos/writing-a-matlab-program-69023.html

  20. Using Matlab Using functions: control flow functions for Execute statements specified number of times Syntax for index = values program statements … end Description repeatedly executes one or more MATLAB statements in a loop. values has one of the following forms: initval:endval initval:step:endval valArray

  21. Using Matlab Using functions Matlab has a wide variety of toolboxes, whithin each toolbox you can find several types of function specific for that topic. Some functions are generic and you can find them under the generic MATLAB TOOLBOX. Therefore, you can find any type of function you need! If there is no specific function for what you want to do, you can create one by using the command function (check it out on the online HELP!) For a list of the elementary mathematical functions, type help elfun For a list of more advanced mathematical and matrix functions, type help specfun help elmat You can combine the available functions one after the other, or one inside the other (nesting).

  22. ExerciseYou have 10 minutes to come up with the theoretical steps you need to complete the assignment described below. Help each other or ask me, if needed. Then navigate through the Help (Contents tab on the left) to suggest possible functions to terminate the exercise with Matlab • Available data: • 4 EMG signals (RF, BF, Gmed, TFL) @ 1000Hz • MVC signals relative to the same muscles @ 1000Hz • 3 ANGLES (hip flex/ext, hip abd/add, hip internal/external rotations) @ 200Hz • start and end EVENTS (detected respect to the video/angles frequency) • TO DO: verify the relationship between EMG and hip angles. What muscles are active when the hip is at maximum extension? • Theoretical steps? • Matlab steps?

  23. Using Matlab Using functions: calculate the average mean Average or mean value of array Syntax M = mean(A)M = mean(A,dim)Description M = mean(A) returns the mean values of the elements along different dimensions of an array. If A is a vector, mean(A) returns the mean value of A. If A is a matrix, mean(A) treats the columns of A as vectors, returning a row vector of mean values. If A is a multidimensional array, mean(A) treats the values along the first non-singleton dimension as vectors, returning an array of mean values. M = mean(A,dim) returns the mean values for elements along the dimension of A specified by scalar dim. For matrices, mean(A,2) is a column vector containing the mean value of each row. Examples A = [1 2 3; 3 3 6; 4 6 8; 4 7 7]; mean(A) ans = 3.0000 4.5000 6.0000 mean(A,2) ans = 2.0000 4.0000 6.0000 6.0000

  24. Using Matlab Using functions: creating coefficients for filters butter Butterworth filter design Syntax [b,a] = butter(n,Wn) Description [b,a] = butter(n,Wn) designs an order n lowpass digital Butterworth filter with normalized cutoff frequency Wn. It returns the filter coefficients in length n+1 row vectors b and a, with coefficients in descending powers of z.

  25. Using Matlab Using functions: filtering filter 1-D digital filter Syntax y = filter(b,a,X) Description y = filter(b,a,X) filters the data in vector X with the filter described by numerator coefficient vector b and denominator coefficient vector a. filtfilt Zero-phase digital filtering Syntax y = filtfilt(b,a,x)Description y = filtfilt(b,a,x) performs zero-phase digital filtering by processing the input data, x, in both the forward and reverse directions

  26. Using Matlab Using functions: create linear intervals linspace Generate linearly spaced vectors Syntax y = linspace(a,b)y = linspace(a,b,n) Description The linspace function generates linearly spaced vectors. It is similar to the colon operator ":", but gives direct control over the number of points. y = linspace(a,b) generates a row vector y of 100 points linearly spaced between and including a and b. y = linspace(a,b,n) generates a row vector y of n points linearly spaced between and including a and b. For n < 2, linspace returns b.

  27. Using Matlab Using functions: interpolating data spline Cubic spline data interpolation Syntax yy = spline(x,Y,xx) Description yy = spline(x,Y,xx) uses a cubic spline interpolation to find yy, the values of the underlying function Y at the values of the interpolant xx. For the interpolation, the independent variable is assumed to be the final dimension of Y with the breakpoints defined by x. Example x = 0:10; y = sin(x); xx = 0:.25:10; yy = spline(x,y,xx); plot(x,y,'o',xx,yy)

  28. Some interesting functions for biomechanical applications • Normalizing (interpolating) data • Ex. Express the gait signal in percentange of gait Work flow

  29. Some interesting functions for biomechanical applications • Normalizing (interpolating) data • Ex. Express the gait signal in percentange of gait Work flow Matlab functions (!) Look at mygait.m in the folder

  30. Some interesting functions for biomechanical applications • Smoothing data using low pass filters • Ex EMG signals Work flow

  31. Some interesting functions for biomechanical applications • Smoothing data using low pass filters • Ex EMG signals Work flow Matlab functions (!) Look at myemg.m in the folder

  32. Useful links: http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-094-introduction-to-matlab-january-iap-2010/lecture-notes/ Only the first 2! http://www.mathworks.com/help/pdf_doc/matlab/getstart.pdf Very good introduction. Covers everything necessary to learn the basics http://eecourses.technion.ac.il/matlab/Matlab%20PDFs/using_ml.pdf More advanced Thank you for your attention!

More Related