64

Introduction to Matlab and matrix review

  • Upload
    ernst

  • View
    54

  • Download
    1

Embed Size (px)

DESCRIPTION

12-Oct-2010 APA 6903 Giulia Mantovani. Introduction to Matlab and matrix review. Mat rix Lab oratory Created in late 1970 ’ s Intended for used in courses in matrix theory, linear algebra and numerical analysis - PowerPoint PPT Presentation

Citation preview

Page 1: Introduction to Matlab  and matrix review
Page 2: Introduction to Matlab  and matrix review

• 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

Page 3: Introduction to Matlab  and matrix review

Common Uses for Matlab in Research• Data Acquisition• Multi-platform, Multi Format data importing • Analysis Tools (Existing,Custom)• Statistics• Graphing• Modeling

Page 4: Introduction to Matlab  and matrix review

Data Acquisition» A framework for bringing live, measured data into

MATLAB using PC-compatible, plug-in data acquisition hardware

Page 5: Introduction to Matlab  and matrix review

Multi-platform, Multi Format data importing » Data can be loaded into Matlab from almost any

format and platform» Binary data files (eg. REX, PLEXON etc.)» Ascii Text (eg. Eyelink I, II) » Analog/Digital Data files

PC

UNIX100101010

Subject 1 143

Subject 2 982

Subject 3 87 …

Page 6: Introduction to Matlab  and matrix review

Analysis Tools » A Considerable library of analysis tools exist for data

analysis» Provides a framework for the design, creation, and

implementation of any custom analysis tool imaginable

Page 7: Introduction to Matlab  and matrix review

Statistical Analysis» A considerable variety of statistical tests available

including:

˃ TTEST ˃ Mann-Whitney Test ˃ Rank Sum Test˃ ANOVAs˃ Linear Regressions˃ Curve Fitting

Page 8: Introduction to Matlab  and matrix review

Graphing» A Comprehensive array of plotting options available

from 2 to 4 dimensions» Full control of formatting, axes, and other visual

representational elements

Page 9: Introduction to Matlab  and matrix review

Modeling» Models of complex dynamic system interactions can

be designed to test experimental data

Page 10: Introduction to Matlab  and matrix review

You can change the desktop arrangement to meet your needs, including resizing, moving, and closing tools.

Page 11: Introduction to Matlab  and matrix review
Page 12: Introduction to Matlab  and matrix review

12

Page 13: Introduction to Matlab  and matrix review

» Ordered set of numbers: (1,2,3,4)

» Example: (x,y,z) coordinates of pt in space. runit vecto a is ,1 If

1

2

),,2

,1

(

vv

n

iixv

nxxxv

Page 14: Introduction to Matlab  and matrix review

Vector Addition

),(),(),( 21212211 yyxxyxyx wv

vvww

V+wV+w

Page 15: Introduction to Matlab  and matrix review

Scalar Product

),(),( 2121 axaxxxaa v

vv

avav

Page 16: Introduction to Matlab  and matrix review

» sum» max, min, mean, sort, …» Pointwise: .^

Page 17: Introduction to Matlab  and matrix review

Inner (dot) Product

vv

ww

21212211 .),).(,(. yyxxyxyxwv

The inner product is a The inner product is a SCALAR!SCALAR!

cos||||||||),).(,(. 2211 wvyxyxwv

wvwv 0.

Page 18: Introduction to Matlab  and matrix review

Matrices

nmnn

m

m

m

mn

aaa

aaa

aaa

aaa

A

21

33231

22221

11211

mnmnmn BAC

Sum:Sum:

ijijij bac A and B must have the same A and B must have the same

dimensionsdimensions

nmnmnnnn

mm

mm

mm

nmnn

m

m

m

mn

bababa

bababa

bababa

bababa

ccc

ccc

ccc

ccc

C

2211

3332323131

2222222121

1112121111

21

33231

22221

11211

nmnn

m

m

m

mn

bbb

bbb

bbb

bbb

B

21

33231

22221

11211

Page 19: Introduction to Matlab  and matrix review

Matrices

pmmnpn BAC Product:Product:

m

kkjikij bac

1

A and B must have compatible dimensionsA and B must have compatible dimensions

nnnnnnnn ABBA

Identity Matrix:

AAIIAI

100

010

001

Page 20: Introduction to Matlab  and matrix review

Matrices

mnT

nm AC Transpose:Transpose:

jiij ac TTT ABAB )(

TTT BABA )(

IfIf AAT A is symmetricA is symmetric

Page 21: Introduction to Matlab  and matrix review

MatricesDeterminant:Determinant: A must be square ( A must be square ( n = m n = m ))

333231

232221

131211

det

aaa

aaa

aaa

cbaddb

ca

db

ca

det

The area of the parallelogram is the

absolute value of the determinant of the matrix

formed by the vectors representing the

parallelogram's sides.

n = m = 2

3231

222113

3331

232112

3332

232211 aa

aaa

aa

aaa

aa

aaa

n = m = 3

The volume of this Parallelepiped is the absolute value of the determinant of the matrix formed by the rows r1, r2, and r3.

r1 r2 r3

Page 22: Introduction to Matlab  and matrix review

Matrices

IAAAA nnnnnnnn

11

Inverse:Inverse: A must be squareA must be square

1121

1222

12212211

1

2221

1211 1

aa

aa

aaaaaa

aa

Page 23: Introduction to Matlab  and matrix review

2D Translation

tt

PP

P’P’

Page 24: Introduction to Matlab  and matrix review

2D Translation Equation

PP

xx

yy

txtx

tyty

P’P’tt

tPP ),(' yx tytx

),(

),(

yx tt

yx

t

P

Page 25: Introduction to Matlab  and matrix review

2D Translation using Matrices

PP

xx

yy

txtx

tyty

P’P’tt

),(

),(

yx tt

yx

t

P

1

1

0

0

1' y

x

t

t

ty

tx

y

x

y

xP

tt PP

Page 26: Introduction to Matlab  and matrix review

Scaling

PP

P’P’

Page 27: Introduction to Matlab  and matrix review

Scaling Equation

PP

xx

yy

s.xs.x

P’P’s.ys.y

),('

),(

sysx

yx

P

P

PP s'

y

x

s

s

sy

sx

0

0'P

SPSP '

Page 28: Introduction to Matlab  and matrix review

Stretching Equation

PP

xx

yy

Sx.xSx.x

P’P’Sy.ySy.y

y

x

ys

xs

yys

xxs

0

0'P

),('

),(

yysx

xs

yx

P

P

S

PSP '

Page 29: Introduction to Matlab  and matrix review

Rotation

PP

P’P’

Page 30: Introduction to Matlab  and matrix review

Rotation EquationsCounter-clockwise rotation by an angle Counter-clockwise rotation by an angle

y

x

y

x

cossin

sincos

'

'

PPxx

y’y’ P’P’

x’x’

yy

PRP'

R

Page 31: Introduction to Matlab  and matrix review

Properties of rotation matrixes

1)det(

R

IRRRR TT

R is a rotation matrix and it R is a rotation matrix and it must satisfy the following must satisfy the following

constraints:constraints:

y

x

y

x

cossin

sincos

'

'

R

Page 32: Introduction to Matlab  and matrix review

1cos

sin

sin

cos' y

x

t

t

y

x

P

PP

x

y

y’

x’

Page 33: Introduction to Matlab  and matrix review

Navigating the Matlab DesktopCommonly Used Toolboxes

Page 34: Introduction to Matlab  and matrix review

Solving equations using variablesExpression 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. Variable = Expression**Variable Names are Case Sensitive!Ex. «a» and «A» are two different variables

Page 35: Introduction to Matlab  and matrix review

Solving equations using variables

Variable = Expression >> x = 6x = 6>> y = 2y = 2

>> x + yans = 8>> x - yans = 4>> x * yans = 12>> x / yans = 3 >> x ^ y ans = 36

Basic Calculation Operators:+ Addition

- Subtraction

• Multiplication

/ Division

^ Exponentiation

Reserved variables!They are special names used to indicate either a specific number (e.g. pi, inf, eps, …), a keyword (e.g. char, double, …), or the result of an expression (ans).By default they have a specific meaning or value, if you assign a new expression to these names, you cannot use the default reserved variable anymore during the current session of Matlab.

Page 36: Introduction to Matlab  and matrix review

Working with Matrices

• Matlab works with rectangular numerical matrixes• A matrix is a collection of numerical values that are organized into a

specific configuration of rows and columns. • The number of rows and columns can be any number Example 3 rows and 4 columns define a 3 x 4 matrix having 12 elements

Page 37: Introduction to Matlab  and matrix review

Working with Matrices

• Matlab works with rectangular numerical matrixes• A matrix is a collection of numerical values that are organized into a

specific configuration of rows and columns. • The number of rows and columns can be any number Example 3 rows and 4 columns define a 3 x 4 matrix having 12 elements

• A scalar is a single number and is represented by a 1 x 1 matrix in matlab. • A vector is a one dimensional array of numbers and is represented by an n

x 1 column vector or a 1 x n row vector of n elements

Page 38: Introduction to Matlab  and matrix review

Working with Matrices

c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix

Page 39: Introduction to Matlab  and matrix review

Working with Matrices

c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix

Page 40: Introduction to Matlab  and matrix review

Working with Matrices

c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix x1 = [ 2

5 3 -1] x1 is column vector or a 4 x 1 matrix

Page 41: Introduction to Matlab  and matrix review

Working with Matrices

c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix x = [ 3.5, 33.22, 24.5 ] x is a row vector or a 1 x 3 matrix x1 = [ 2

5 3 -1] x1 is column vector or a 4 x 1 matrix A = [ 1 2 4

2 -2 2 0 3 5 5 4 9 ] A is a 4 x 3 matrix

Page 42: Introduction to Matlab  and matrix review

Working with Matrices

• Spaces, commas, and semicolons are used to separate elements of a matrix

Page 43: Introduction to Matlab  and matrix review

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]

Page 44: Introduction to Matlab  and matrix review

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

Page 45: Introduction to Matlab  and matrix review

Working with Matrices

• Colon operator identifies a range of values: [ 1 : 3 ] = [ 1 , 2 , 3] [ 1 : 2: 9 ] = [ 1 , 3 , 5, 7, 9] [ 5 : -1: 1 ] = [ 5 , 3 , 1]

B = [1:4 ; 5:8 ; 9,8,7,6 ] = A 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]

start jump end

Page 46: Introduction to Matlab  and matrix review

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(4,1)=5

Page 47: Introduction to Matlab  and matrix review

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(4,1)=5

Example:>> A = [1 2 4 5;6 3 8 2] A is a 4 x 2 matrix>> A(1,2)ans = 6• The colon operator can be used to index a range of elements>> A(1:3,2)ans = 1 2 4

Page 48: Introduction to Matlab  and matrix review

Indexing Matrices

• Specific elements of any matrix can be overwritten using the matrix indexExample: A = [1 2 4 5

6 3 8 2]

>> A(1,2) = 9Ans A = [1 2 4 5

9 3 8 2]

Page 49: Introduction to Matlab  and matrix review

Matrix Shortcuts

• The ones and zeros functions can be used to create any m x n matrices composed entirely of ones or zeros

Examplea = 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 HELP!

b = zeros(1,5)b = [0 0 0 0 0]

Page 50: Introduction to Matlab  and matrix review

Data Types and Formats

• The semicolon operator determines whether the result of an expression is displayed

• who lists all of the variables in your matlab workspace

• whos list the variables and describes their matrix size

Page 51: Introduction to Matlab  and matrix review

Saving your WorkTo save data to a *.mat file:

• Typing ‘save filename’ at the >> prompt and the file ‘filename.mat’ will be saved to the working directory

• Select Save from the file pull down menuAutomatically, all the variables in the workspace will be stored in the

same mat-file named filename.mat.

To reload a *.mat file

• Type ‘load filename’ at the >> prompt to load ‘filename.mat’ (ensure the filename is located in the current working directory)

• 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

Page 52: Introduction to Matlab  and matrix review

Using functions

Matlab has a wide variety of toolboxes, whithin each toolbox you can find several type of functions specific for that topic. Some functions are generic and you can find them in the MATLAB TOOLBOX. RESULT: more or less, you can find any type of function you need.

For a list of the elementary mathematical functions, typehelp elfunFor a list of more advanced mathematical and matrix functions, typehelp specfun help elmat

You can just create an elaboration by using the available functions, or you can create your own function (see after).

Page 53: Introduction to Matlab  and matrix review

Basic plotting commands

Function Descriptionplot Graph 2-D dataplot3 Graph 3-D data

Help – Search – type plot and obtain the following explanation!plot(Y) plots the columns of Y versus the index of each value when Y is a real number. plot(X1,Y1,...,Xn,Yn) plots each vector Yn versus vector Xn on the same axes. If one of Yn or Xn is a matrix and the other is a vector, plots the vector versus the matrix row or column with a matching dimension to the vector.

For example, plot a sinusoidal curve:

»t = 0:1/100:1;

»y = sin(2*pi*t);

»plot(t,y);

More advanced!

plot3(t,t,y);

grid on

xlabel x

ylabel y

zlabel z

B=repmat(y,length(t),1);

mesh(t,t,B);

Frequency of the sinusoidal curve

Page 54: Introduction to Matlab  and matrix review

Enter the following Matrices in matlab using spaces, commas, and semicolons to separate rows and columns:

7231

9175

6211 553878122641

160

16

22

4

A = B =

C =

19246525

12100

2162855

42166418

D =

65D =

E = a 5 x 9 matrix of 1’s

Page 55: Introduction to Matlab  and matrix review

Use the who and whos functions directly from the Command Window to confirm all of the variables and matrices in the work space are present and correct

7231

9175

6211 553878122641

160

16

22

4

A = B =

C =

19246525

12100

2162855

42166418

D =

65D =

E = a 5 x 9 matrix of 1’s

Page 56: Introduction to Matlab  and matrix review

Change the following elements in each matrix:

Now save all the variables in the workspace in a mat-file named myvar.

7231

9175

661 553878122641

160

16

22

4

A = B =

C =

19246525

12100

2162855

42166418

D =

76

76

76

76

76

0

0

0

Page 57: Introduction to Matlab  and matrix review

• Create a variable named S, which contains two cycles of a sinusoidal curve of frequency 2Hz and resolution pi/10.

• Plot S in a two dimensional graph.• Try to use the documentation to change from the solid blue

line, to a dashed red line• To modify these parameters you need to add a third option to the

plot command. Prompt plot(t,y, ‘options’). Instead of using the word options, use the symbols that indicate the type and the color of the line

• click on the LineSpec link on the plot page documentation to see which are the symbols for the dashed line and for the red.

Page 58: Introduction to Matlab  and matrix review

» 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.

Page 59: Introduction to Matlab  and matrix review

» I can write the list of command, functions and variables in a new Script and then save it as a file.m

» When I need to run again that list of commands I reopen the stored file.m and I can continue my work!

Page 60: Introduction to Matlab  and matrix review

» Smoothing data using low pass filters» Normalizing (interpolating) data

Page 61: Introduction to Matlab  and matrix review

» Smoothing data using low pass filters» Ex EMG signals

Work flow Matlab functions

(!) Look at myemg.m in the folder

Page 62: Introduction to Matlab  and matrix review

» Normalizing (interpolating) data» Ex. Express the gait signal in percentange of gait

Work flow Matlab functions

(!) Look at mygait.m in the folder

Page 63: Introduction to Matlab  and matrix review

Normalize from FS1 to FS2 in gait percentage the two signals relative to the Flexion/Extension angle at the hip joint. The variables stored in the mat-file hip_gait_data are relative to a patient A, and from hip_gait_data2 we have data from patient B.

Once that both signals are normalized, plot them together in the same figure and with two different colors.

Compute the Range of Motion for both signals (after normalization). Use the max and min functions to compute ROM.

Page 64: Introduction to Matlab  and matrix review