0% found this document useful (0 votes)
24 views

Matlab 1

The document provides an outline and introduction to using MATLAB. It discusses essential MATLAB concepts like data storage, basic mathematical operators for scalars, vectors, and matrices. It also gives examples of using MATLAB for tasks like linear regression, numerical integration, solving equations, and statistical analysis. The overall document serves as an introductory guide to common MATLAB functions and capabilities.

Uploaded by

Hassan GDOURA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Matlab 1

The document provides an outline and introduction to using MATLAB. It discusses essential MATLAB concepts like data storage, basic mathematical operators for scalars, vectors, and matrices. It also gives examples of using MATLAB for tasks like linear regression, numerical integration, solving equations, and statistical analysis. The overall document serves as an introductory guide to common MATLAB functions and capabilities.

Uploaded by

Hassan GDOURA
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 22

ChBE 303

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]

 Index  Zeros & Ones Matrix


 zeros( Column , Row )
 >> Z = X(1,2)
–OR–  ones( Column , Row )
>> Z = X(3)  Random # ( 0 – 1 )
 Z=3 rand( Column , Row )
 >> Z = X(2,3)  >> X = rand( 2 , 2 )
–OR–  X = [ 0.5234 , 0.9246 ;
>> Z = X(5) 0.2862 , 0.7378 ]
 ERROR !!
Matrix Operators
– Scalar / Element Wise –
 Scalar Operations  Element Wise Operations
 Addition (+)  Addition (+)
 >> Z = X + 2  >> X = [ 1 , 2 ; 3 , 4 ];
 Z=[3,4;5,6] >> Y = [ 4 , 3 ; 2 , 1 ];
>> Z = X+Y
 Subtraction (-)  Z=[5,5;5,5]
 Multiplication (*)  Subtraction (-)
 >> Z = 2 * X  Multiplication
 Z=[2,4;6,8]  >> Z = X.*Y
 Division (/)  Z=[4,6;6,4]
 Division
Matrix Operations
– Linear Algebra … A x = b –
 b=?  x=?
 >> A = [ 1 , 2 ; 3 , 4 ];  >> A = [ 1 , 2 ; 3 , 4 ];
>> x = [ 1 ; 2 ];
>> b = A*x >> b = [ 5 ; 11 ];
 b = [ 5 ; 11 ]; >> x = A\b
 x = [ 1 ; 2 ];
 n
  A x

  b1 
 Algorithms …
 A1,1  A1,i  A1,n   x1  1, i i
 
Gaussian Elimination
i 1
               
   

n

  
A  x   A i,1  A i,i  A i, n  xi    A j,i xi   bi   b


    
  
      i 1     
    LU Factorization
A n,1  A n,i  A n,n   xn   n  b 
  A n,i xi   n 
 i 1 
 Etc.
Matrix Operations
 Inverse  Eigenstates
 >> X = [ 1 2 3 ; 2 3 1 ; 3 1
2]; Ax  x
 >> Y = inv(X);
 >> Z = X*Y  >> [ V , D ] = eig(Z)
 Z=[100;010;001]
 Eigenvalues …
 D = [-1.73, 1.73, 6.00]
 Determinant  Eigenvectors …
 >> Z = det( X ) 
V = [ 0.79 0.21 0.58 ; -0.21
 Z = -18 -0.79 0.58 ; -0.58 0.58
 EXAMPLE (2 x 2) 0.58]
X  x1,1 x2, 2  x2,1 x1, 2
 EXAMPLE (3 x 3)
X  x1,1 x2, 2 x3,3  x1,1 x3, 2 x2,3  x1, 2 x2,3 x3,1  
x1, 2 x2,1 x3,3  x1,3 x3, 2 x2,1  x1,3 x2, 2 x2,1
Linear Regression
 Data …  QR Factorization
 ( 2, 3 )  >> X = [ 2 ; 4 ; 5 ; 6 ; 7 ] ;
>> Y = [ 3 ; 7 ; 10 ; 10 ; 12 ];
 ( 4, 7 ) >> A = [ X , ones(size(X)) ];
 ( 5, 10 ) >> COEF = A \ Y
 COEF = [ 1.7838 , -0.1622 ]
 ( 6, 10 )
 ( 7, 12 )  Polyfit
 >> COEF = polyfit( X , Y , 1 )
 COEF = [ 1.7838 , -0.1622 ]
Concept of Transform
 Instead of Nonlinear Regression …
 Linearize the Equation

 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

 Assignment ( Variable.Property = XYZ )


 >> Person(1).Name = ‘BOB’
 >> Person(1).Addres = ’99 Sunset Blvd’
 >> Person(1).Phone = 3334444
 >> Person(2).Name = ‘JANE’
 >> Person(2).Address = ‘1750 Kirby Dr’
 … etc.
Cells
 Assignment
 >> X = { 1:4 , ‘HI’ ; [ 1 , 0 ; 0 , 1 ] , 3.1459 };
 Index
 >> Z = X{1,1}
-OR-
>> Z = X{1}
 Z=[1234]
 >> Z = X{1,2}(1)
 Z = ‘H’
 >> Z = X{2,1}(1,2)
 Z=0
Nonlinear Root Finding
 FZERO  Algorithms
( exp( 1 / x ) sin( x ) / x^2 )  Bisection Method
 >> FUN = inline(  Newton’s Method
‘exp( 1 / x ) sin( x ) / x^2’ );  Etc.
 >> X0 = 3;
y=ex sin(x)x -2
150
 >> X = fzero( FUN , X0 )

 X = 3.1416 100

50

 ROOTS – Polynomials 0
Y

( Y = -2X^3 + 4.7*X + 0.25 )


-50
 >> X = [ -2 , 0 , 4.7 , 0.25 ];

 >> Z = roots( X ); -100

 Z = [1.56 -1.51 -0.05]


-150
1 2 3 4 5 6 7 8 9 10
X

You might also like