Introduction to MATLAB MATLAB: Getting Started Welcome and Goodluck 1 What is MATLAB? 2 What is available in MATLAB? 3 Why MATLAB? Syed Khaleel Ahmed 4 Where MATLAB? Dept. of Electronics and Communication Engg., Universiti Tenaga Nasional syedkhaleel@uniten.edu.my syedkhaleel2000@gmail.com http: \\ metalab.uniten.edu.my \ ˜syedkhaleel \ November 5, 2016 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 1 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 2 / 40 Introduction to MATLAB Introduction to MATLAB What is available in MATLAB? What is MATLAB? A high-level software for Pre-defined functions. Toolboxes. numerical computation , SIMULINK. data analysis , and Blocksets. visualization . Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 3 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 4 / 40
Introduction to MATLAB Introduction to MATLAB Why MATLAB? Where MATLAB? Automotive, de-facto industry standard, especially in engineering, Signal Processing, easy to use, and Communication, Aerospace, availability of toolboxes and blocksets. Finance and Economics, Computer, and many more . Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 5 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 6 / 40 Introduction to MATLAB User Interface Layout Course Contents Basic Components of MATLAB 1 MATLAB User Interface Layout. MATLAB as a Calculator 2 Working with Variables. Operators and Operator Precedence 3 Visualizing Data. Pre-defined Functions 4 Programming. Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 7 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 8 / 40
User Interface Layout (contd.) User Interface Layout (contd.) MATLAB as a Calculator Basic Components of MATLAB >> 2*2 - 4/3 To start ans = 2.6667 Double-click MATLAB on desktop or Pre-defined Constants pi π click start menu . i, j ı, >> 16^(1/4) + 3*sin( pi/ 4 ) smallest value eps MATLAB user interface or desktop environment ans = largest real number realmax 4.1213 Command Window smallest real number realmin Command History largest integer intmax smallest integer intmin >> sqrt( 4 ) + exp( j*pi/6 ) Current Directory browser Workspace Browser ans = 2.8660 + 0.5000i Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 9 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 10 / 40 User Interface Layout (contd.) User Interface Layout (contd.) Pre-defined Functions Operators and operator precedence Trigonometric – COS, ACOS, EXP, SIN, ASIN >> 2*3+2 Exponential – LOG, EXP, SQRT 1 ( ) parenthesis >> 2*(3+2) Complex – ABS, ANGLE, CONJ 2 ´ complex conjugate transpose Discrete Maths – FACTOR, PRIMES, GCD >> 2/3 3 ˆ power Want more information? Type >> 2\3 4 * multiplication; / division >> help elfun >> 2*3^4 5 \ left division >> >> doc elfun 6 + addition; – subtraction >> (2*3)^4 >> >> help angle Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 11 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 12 / 40
Working with Variables Working with Variables(contd.) Creating and Manipulating Variables All variables in MATLAB are arrays (matrices) . Creating and Manipulating Variables. A scalar is a 1 × 1 array. Accessing and Manipulating Elements in a Matrix. >> a = 1 a = Computations with Matrices. 1 A (column) vector is an n × 1 array. >> b = [ 1; 2 ] b = 1 2 A row vector is a 1 × m array. >> c = [ 1 2 3 ] c = 1 2 3 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 13 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 14 / 40 Working with Variables(contd.) Working with Variables(contd.) Creating and Manipulating Variables (contd.) Creating and Manipulating Variables (contd.) All variables in MATLAB are arrays (matrices) . >> x = 1 A matrix is an n × m array. x = >> d = [ 1 2; 3 4 ] 1 d = >> y = 4; 1 2 >> r = sqrt( x^2 + y^2 ) 3 4 r = Strings are arrays of characters. 4.1231 >> str = ’Hello World!’ >> fx = cos( 2*x + pi/4 ) str = fx = Hello World! -0.9372 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 15 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 16 / 40
Working with Variables(contd.) Working with Variables(contd.) Creating and Manipulating Variables (contd.) Creating and Manipulating Variables (contd.) >> x = [ 1 2 3 4 5 ] Special Matrices >> x = 1:5 >> A = zeros( 3 ) >> x = 0:0.25:1 >> x = linspace( 0, 1, 5 ) >> B = ones( 2, 4 ) >> x = logspace( -1, 2, 4 ) Exercises: >> C = rand( 1, 4 ) √ � � sin(30 ◦ ) Create the vector x = 10 2 π . Create the vector >> D = magic( 4 ) 0 1 >> E = eye( 2 ) y = . . . . >> X = [ ones( 2 ) zeros( 2, 3 ) rand( 2, 1 ) ] 10 Grid the interval from 1 to 5 using 11 points. >> sparse( X ) Create a vector w with first element 0, last element 4 & increment 0 . 5 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 17 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 18 / 40 Working with Variables(contd.) Working with Variables(contd.) Accessing and Manipulating Elements in a Matrix (contd.) Accessing and Manipulating Elements in a Matrix >> A( 2:3, 3:4 ) >> A = rand( 4, 5 ) Array elements are accessed through indices . >> A( 2:end, : ) >> A( 2, 3 ) >> A( end:-1:1, : ) A single matrix element. >> A( 2, 3 ) = 5 >> A( [ 1 3 ], : ) A sub-matrix. >> A( 1, : ) Re-order elements. >> A( 3, [ 2 4 ] ) >> A( :, 2 ) >> A( [ 1 3 ], [ 4 2 ] ) Exercises: Create a random 2 × 3 matrix A . Modify a 23 to π . Invert the order of the columns of A . Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 19 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 20 / 40
Working with Variables(contd.) Working with Variables(contd.) Computations with Matrices Computations with Matrices (contd.) Two types of computations Examples: Create the following matrices a random 2 × 3 matrix A , Suppose a random 3 × 2 matrix B , A 1 and A 2 are two matrices of order m × n , C = AB , B is of order n × p , D = C − 1 C is n × n , and E = ǫ D α is a scalar. F = D + 2 I H such that h k ℓ = a 2 k ℓ . Matrix computations – Mathematically defined . Want to know more matrix functions, type Examples: A 1 + A 2 , A 1 B , C − 1 , α A , . . . >> help matfun Element-wise computations – useful for speeding up computations . >> >> doc matfun Examples: a k ℓ b k ℓ , . . . Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 21 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 22 / 40 Visualizing Data Visualizing Data (contd.) Importing Data into MATLAB Importing data into MATLAB. Using the Import Wizard . Basic plotting commands. Click HOME tab. Click Import Data icon. Customizing plots. Browse to folder and choose file. Types of Plots. Using MATLAB commands – importdata . Exporting Plots to other applications. >> mydata = importdata(’studmarks.txt’); Saving and loading data. Alternate commands xlsread , csvread , dlmread . To see supported file formats, type >> doc fileformats Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 23 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 24 / 40
Visualizing Data (contd.) Visualizing Data (contd.) Basic plotting commands Basic plotting commands (contd.) Plotting a sinusoidal function y = sin( x ). Drawing multiple plots on the same graph: >> x = 0:0.2:2*pi; >> y = sin( x ); y = sin( x ) and z = cos( x ) . >> plot( x, y ) Different looks >> x = 0:0.2:2*pi; >> plot( x, y, ’r’ ) >> y = sin( x ); >> plot( x, y, ’:’ ) >> z = cos( x ); >> plot( x, y, ’x’ ) Standard form: plot( xdata, ydata, ′ < color >< linestyle >< marker > ′ ) . Does this work? >> plot( x, y, ’g-.o’ ) >> plot( x, y ) For more information >> plot( x, z ) >> help plot Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 25 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 26 / 40 Visualizing Data (contd.) Visualizing Data (contd.) Basic plotting commands (contd.) Customizing Plots >> x = 0:0.2:2*pi; Drawing multiple plots on the same graph: >> y = sin( x ); >> z = cos( x ); y = sin( x ) and z = cos( x ) . >> plot( x, y, ’r:o’, x, z, ’m--s’ ) What about this? Adding a grid >> plot( x, y, x, z ) >> grid >> plot( x, y, ’r:o’, x, z, ’m--s’ ) Label the axes >> xlabel(’x values’) Or this? >> ylabel(’y values’) A title >> plot( x, y, ’r:o’ ) >> hold on >> title(’Plot of sinusoidal functions’) >> plot( x, z, ’m--s’ ) Legend for multiple graphs >> hold off >> legend(’sin(x)’, ’cos(x)’) Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 27 / 40 Syed Khaleel Ahmed (UNITEN) MATLAB: Getting Started November 5, 2016 28 / 40
Recommend
More recommend