Matlab 1
Matlab 1
Introduction to Matlab
Part 1
Robert A Pinnick
Fall 2006
Outline
Essentials
Philosophy of Data Storage
Mathematical Operators
Elementary
1-Dimensional Arrays ( Vectors )
2-Dimensional Arrays ( Matrix )
Structure Operations
Cell Operations
Example Code
Linear Regression
Numerical Integration
Numerical Solvers
Statistics
Essentials
Syntax & Logic Help
>> help <function_name>
help fzero
>> lookfor <keyword>
lookfor bessel
www.mathworks.com
Support (Documentation) & Forum Sections
www.google.com
Useful Tips
>> clear (variable)
Clears All Memory or Specified Variable
>> clc
Clears Command Window Screen
Philosophy of Data Storage
Numerical Values (Ex. 5, 3.14159, 2+i)
Integers
Floating (Single & Double)
Complex
Character Strings (Ex. ‘asdf jkl;’)
Structures
Cells
Boolean ( True / False )
Elementary Operators
Assignment (=) Multiplication (*)
>> X = 1; >> Z = 2 * Y
>> Y = 3.14159; Z = 6.28318
Addition (+) Division (/)
>> Z = X + Y >> Z = 1 / 4
Z = 4.14159 Z = 0.25
Subtraction (-) Power (^)
>> Z = X – Y >> Z = 5 ^ 3
Z = -2.14159 Z = 125
Vector Operators
– Scalar Operations –
Assignment Index
Column >> Z = X(3)
>> X = [ 1 ; 2 ; 3 ]; Z=3
Row >> Z = X(4)
>> Y = [ 1 , 2 , 3 ]; –OR–
>> Y = [ 1 2 3 ];
>> Z = X(0)
Unique Commands ERROR !!
linspace( Initial , Final , # Points )
>> Z = linspace( 5 , 20 , 4 )
>> Z = X(1:2)
Z = [ 5 , 10 , 15 , 20 ]; Z=[1;2]
logspace( Initial , Final , # Points ) >> Z = X([ 1 3 ])
Initial:Step:Final Z=[1;3]
>> Z = 1:-0.25:0
Z = [ 1 , 0.75 , 0.5 , 0.25 , 0 ]
Vector Operators
– Scalar Operations –
Addition (+) Summation
>> Z = X + 2 n
Z=[3;4;5] x
i 1
i
Subtraction (-)
>> X = [ 1 , 2 , 3 , 4 ];
Multiplication (*) >> Z = sum( X )
>> Z = 2 * X Z = 10
Z=[2;4;6]
Product
Division (/) n
>> Z = X / 2
Z = [ 0.5 ; 1 ; 1.5 ]
x
i 1
i
>> Y = [ 1 ; 2 ; 3 ; 4 ];
>> Z = prod( Y )
Z = 24
Vector Operators
Inner Product
y1
Euclidean Norm
n
x y x1 xi xn yi xi yi n
i 1
x x x xi2
yn i 1
>> X = [ 1 , 2 , 3 ];
>> X = [ 1 , 2 , 3 ];
>> Z = X * X’
>> Y = [ 1 ; 2 ; 3 ]; Z = 14
>> Z = X * Y
Z = 14
Cross Product
Transpose
x y x2 y3 y2 x3 x3 y1 y3 x1 x1 y2 y1 x2
x1
>> X = [ 1 , 2 , 3 ];
x1 xi xn
T
xi >> Y = [ 3 , 2 , 1 ];
>> Z = cross( X , Y )
xn
Z=
>> Z = X’
Z=[1;2;3]
Vector Operators
– Element Wise –
Addition (+) Multiplication (.*)
>> X = [ 1 , 2 , 3 ]; >> X = [ 1 , 2 , 3 ];
>> Y = [ 2 , 4 , 6 ]; >> Y = [ 2 , 4 , 6 ];
>> Z = X + Y >> Z = X.*Y
Z = [ 3 , 6 , 9 ]; Z = [ 2 , 8 , 18 ];
Subtraction (-) Division (./)
Power (.^)
>> Z = X.^2
Z = [ 1 , 4 , 9 ];
Numerical Integration
Numerical Integration
Data ( y = x^2 )
40
( -6, 36 ) 35
30
( -3 , 9 ) 25
20
Y
(0,0) 15
10
(3,9) 5
0
( 6 , 36 ) -6 -4 -2 0 2 4 6
X
Code
>> X = [ -6 , -3 , 0 , 3 , 6 ];
>> Y = [ 36 , 9 , 0 , 9 , 36 ]; Algorithms
>> Z = sum( trapz( X , Y ) ); Trapezoidal Rule
Z = 162 Simpson’s Rule
>> Z = QUAD( INLINE( ‘x.^2‘ ) , -6 … Etc.
6)
Z = 144
Statistics Functions
Mean n Standard Deviation
x
>> X = rand(10); x i 1 i
n
>> Y = mean( X ) n
ix x 2
i 1
Median n
>> Y = mean( X ) >> Y = std( X )
Maximum Sort
>> Y = max( X ) >> Y = sort( X )
Minimum Find
>> Y = min( X ) >> Y = X( find( X > 0.5 ) )
Matrix Operators
– Scalar Operations –
Assignment Unique Functions
>> X = [ 1 , 2 ; 3 , 4 ]; Identity Matrix
Concatenation eye( Column , Row )
>> X = 1:3; >> X = eye(2,2)
>> Y = [ X ; 2*X ]
Y=[1,2,3;2,4,6]
X=[1,0;0,1]
Exponentials
Y = a*e^(b*X)
ln(Y) = ln(a) + b*X
Power Law
Y = a * X^b …
ln(Y) = ln(a) + b*ln(X)
Power Law Regression
– Example –
Regress Data Calling Code
( y = 2*x^2 + noise ) >> x = [ 1 , 2 , 3 , 4 , 5 , 6 ];
(1,3) >> y = [ 3 , 8 , 17 , 33 , 50 , 71 ];
(2,8) >> COEF = powerfit(x,y)
( 3 , 17 ) COEF = [ 1.7971 , 2.6552 ]
( 4 , 33 )
Power Law Regression
( 5 , 50 )
100
( 6 , 72 )
POWERFIT
function COEF = powerfit( x , y )
10
y
ln_x = log( x );
ln_y = log( y );
COEF = polyfit( x , y , 1 );
COEF(2) = exp( COEF(2) );
1
1 10
x
Character Strings
Assignment Comparison
>> X = ‘HELLO’; >> X = ‘HI’
>> Y = 2.7183; >> Y = ‘HE’
>> Z = num2str( Y ) >> Z = strcmp( X, Y )
Z = ‘2.7183’ Z=0
Concatenation
>> X = strcat( ‘HELLO’ , ‘ WORLD’ )
-OR-
>> X = [‘HELLO’ ‘WORLD’]
X = ‘HELLOWORLD’
Structures
X = 3.1416 100
50
ROOTS – Polynomials 0
Y