Upload
ernst
View
54
Download
1
Tags:
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
• 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
Common Uses for Matlab in Research• Data Acquisition• Multi-platform, Multi Format data importing • Analysis Tools (Existing,Custom)• Statistics• Graphing• Modeling
Data Acquisition» A framework for bringing live, measured data into
MATLAB using PC-compatible, plug-in data acquisition hardware
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 …
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
Statistical Analysis» A considerable variety of statistical tests available
including:
˃ TTEST ˃ Mann-Whitney Test ˃ Rank Sum Test˃ ANOVAs˃ Linear Regressions˃ Curve Fitting
Graphing» A Comprehensive array of plotting options available
from 2 to 4 dimensions» Full control of formatting, axes, and other visual
representational elements
Modeling» Models of complex dynamic system interactions can
be designed to test experimental data
You can change the desktop arrangement to meet your needs, including resizing, moving, and closing tools.
12
» 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
Vector Addition
),(),(),( 21212211 yyxxyxyx wv
vvww
V+wV+w
Scalar Product
),(),( 2121 axaxxxaa v
vv
avav
» sum» max, min, mean, sort, …» Pointwise: .^
Inner (dot) Product
vv
ww
21212211 .),).(,(. yyxxyxyxwv
The inner product is a The inner product is a SCALAR!SCALAR!
cos||||||||),).(,(. 2211 wvyxyxwv
wvwv 0.
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
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
Matrices
mnT
nm AC Transpose:Transpose:
jiij ac TTT ABAB )(
TTT BABA )(
IfIf AAT A is symmetricA is symmetric
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
Matrices
IAAAA nnnnnnnn
11
Inverse:Inverse: A must be squareA must be square
1121
1222
12212211
1
2221
1211 1
aa
aa
aaaaaa
aa
2D Translation
tt
PP
P’P’
2D Translation Equation
PP
xx
yy
txtx
tyty
P’P’tt
tPP ),(' yx tytx
),(
),(
yx tt
yx
t
P
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
Scaling
PP
P’P’
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 '
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 '
Rotation
PP
P’P’
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
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
1cos
sin
sin
cos' y
x
t
t
y
x
P
PP
x
y
y’
x’
Navigating the Matlab DesktopCommonly Used Toolboxes
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
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.
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
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
Working with Matrices
c = 5.66 or c = [5.66] c is a scalar or a 1 x 1 matrix
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
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
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
Working with Matrices
• Spaces, commas, and semicolons are used to separate elements of a matrix
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]
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
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
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
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
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]
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]
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
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
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).
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
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
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
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
• 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.
» 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.
» 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!
» Smoothing data using low pass filters» Normalizing (interpolating) data
» Smoothing data using low pass filters» Ex EMG signals
Work flow Matlab functions
(!) Look at myemg.m in the folder
» Normalizing (interpolating) data» Ex. Express the gait signal in percentange of gait
Work flow Matlab functions
(!) Look at mygait.m in the folder
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.