Do you speak MATLAB!
MATLAB       ®



                 R2012a (7.14)




MathWorks®
MATLAB
                      Is an integrated technical computing
environment that combines numeric computation, advanced
graphics and visualization, and a high-level programming language.
                                                                  The MathWorks, Inc.

High-level language of technical computing
Development environment for engineers, scientists
Interactive tools for design, problem solving
Mathematical function libraries
Graphics and data visualization tools
Custom GUIs
External Interfaces: C, C++, Fortran, Java, COM, Excel, .NET
                                Introducing MATLAB – Language of technical computing   2
MATLAB product family
                                                  MATLAB


                                              MATLAB Programming
                                                   Language

                                              User-written Functions
                                                  Built-in Functions


Graphics                                       Computations                          External Interfaces
2D Graphics                                    Linear Algebra                        C
3D Graphics                                    Data Analysis                         C++
Color & Lighting                               Polynomials                           JAVA
Animation                                      Interpolations                        .net
Image, Audio & Video                           Solutions of ODE                      Micorosoft Excel

Toolboxes: Collections of specialized Functions

Parallel Computing        Parallel Computing Toolbox Toolbox
Parallel Computing Spreadsheet Link EX (for Microsoft Excel)
                                         Symbolic Math                              Toolbox
                                                                       MATLAB Compiler           Computational Finance
                                                                          Financial Optimization Computational Finance
                                                                                                 Toolbox
Math, Statistics, and OptimizationDistributed Toolbox Toolbox Bioinformatics Toolbox
Math,                        Data Acquisition Computing Server
       Statistics, and Optimization
                          MATLAB MATLAB Builder NE (for Equation Toolbox
                                         Partial System Toolbox Econometrics Global Optimization Toolbox Biology
                                           Fixed-Income                             Toolbox      Computational
                             InstrumentControlDifferential Microsoft .NETRobustFixed-Point Toolbox and Verification
                                                                             Framework)
                                              Image Processing Toolbox Datafeed Toolbox Toolbox
                                               Signal
                                          Control Toolbox                           Control
                                                                 MATLAB Coder SimBiology Generation
                                                                                      Code
Control System Design and Analysis StatisticsMATLAB Builder JA (for Java language) Network Toolbox
                                    Financial Derivatives Toolbox
                                                    Toolbox
                                         System Identification System Coder Neural
                             Image Acquisition ToolboxVision ToolboxToolbox RF ToolboxControl Toolbox
                                                                             Model Predictive Application Deployment
                                              Computer Toolbox HDLDatabase Toolbox
                                               DSP System
Signal Processing and Communications Curve Fitting Toolbox EX Toolbox
                                                           Database (for     Aerospace Toolbox Calibration Toolbox
                                                                                    Model-Based
                                                  MATLAB Builder HDL Microsoft Excel) Array System
                             OPC VisionFuzzy Logic Toolbox ToolboxVerifier Database Connectivity and Reporting
Image Processing and Computer Toolbox Image AcquisitionSystem Toolbox Database ConnectivityToolbox
                                               Communications                       Phased               and Reporting
Test and Measurement
Test and Measurement                           Wavelet Toolbox Generator
                                                  MATLAB Report
                             Vehicle Network Spreadsheet Link EX (for Microsoft Excel)
                                                 ToolboxFilter Design HDL Coder
                                              Mapping Toolbox

                                               Introducing MATLAB – Language of technical computing                      3
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB




                                                        4
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    5
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    6
MATLAB - Desktop




  CurrentWindow
   Editor Directory
 Command Window
 Command History
     Workspace
All commandsall youron edit, create, and save your own scriptfar along within files type
The is where you characterizedthe current directory are listed. programs there called
               typed files from that you have command prompt
This main windowwrite, variablesby the MATLABcommand window get recorded, even
     subwindow lists all MATLAB prompt in the generated so or (>>)
m-files.
across multiple session in this window.
and size.

                                                                      Minimal MATLAB       7
MATLAB - Desktop




  Help Window
The main window characterized by the MATLAB command prompt (>>)
                                                                  Minimal MATLAB   8
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    9
Computing with MATLAB
Basic Data and Variables

      Variable


                   Value   Size         Class




                                         Minimal MATLAB   10
Computing with MATLAB
 Basic Data and Variables
                    Multidimensional Array
                                                                  Scalar
                        [full or sparse]



                                                                 function
logical     char   numeric     cell          struct              handle (@)

            ab                { }

int8,     uint8, single   double
int16,    unit16,
int32,    unit32,
int64,    unit64




                                                             Minimal MATLAB   11
Computing with MATLAB
Arithmetic Operator &
Their Precedence
         Operations       Operators         Examples
         Addition
         Subtraction
         Multiplication
         Right Division
         Left Division
         Exponentiation



    Precedence Order   Operators
          1            Parentheses ( ). For nested parentheses, the innermost
                       are executed first.
          2            Exponentiation, ^
          3            Multiplication, *; Division, /,
          4            Addition, +; Subtraction, -


 help precedence
                                                                 Minimal MATLAB   12
Computing with MATLAB
Mathematical Functions
                                   Trigonometry
                                Radian                       Degree
                Basic    Hyperbolic          Inverse         Basic
                sin        sinh              asin            sind
                cos        cosh              acos            cosd
                tan        tanh              atan            tand
                cot        coth              acot            cotd
                sec        sech              asec            secd
                csc        csch              acsc            cscd

  Exponential            Complex             Discrete Math               Matrix
 exp                    abs                  factorial                det
 log                    conj                 gcd                      trace
 log10                  imag                 lcm                      rank
 log2                   real                 primes                   inv
 sqrt                   angle                                         expm
                                                                      sqrtm

 help <type function name>
                                                                       Minimal MATLAB   13
Computing with MATLAB
Predefined Variables and
Keywords
Predefined Variables
ans

pi
eps

inf
i,j
NaN


Keywords
break    case   catch     classdef   continue    else   elseif
end    for    function     global    if    otherwise    parfor
persistent     return    spmd    switch    try    while

 help <Predefined Variable Name>

  iskeyword
                                                  Minimal MATLAB   14
Computing with MATLAB
Managing Variables

Command       Outcome
clear         Clears the workspace, all variables are removed from the memory
clear x y z   Clears only variables x, y, and z
clear all     Clears all variables and functions from workspace
who           Lists variables currently in the workspace
whos          Lists variables currently in the workspace with their sizes together
              with information about their bytes and class




                                                                 Minimal MATLAB      15
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    16
Array
Overview
                                                              5
                                                              7
                5                  5 7 4                      4

          Scalar of 1 X 1     Row Vector of 1 X 3    Column vector of 3 X 1




                                  5    14 12 10 56              5 14 12 10 56
    5 10 56                       6    10 504 4 6            5 14 12 10 56 6
    6 4 6                   5 10 56   85 23   89 0         5 10 56 85 23 89 0
    7 78 86                 6 4 6     23 2                 6 4 6 23 2 0
                            7 78 86   53 6                 7 78 86 53 6
 One Dimensional
  Matrix of 3 X 3
                               Two Dimensional                Three Dimensional
                                Matrix of 3 X 5                 Matrix of 3 X 5


                                                                  Minimal MATLAB      17
Array
 Creation
 from a known list of numbers
 with constant spacing by specifying the first term, the spacing, and the
  last term


Known lists of numbers
One Dimensional Array

                  variable_name = [ type vector elements ]



Two Dimensional Array

       variable_name = [1st row elements; 2nd row elements; 3rd row
                         elements; ... ; last row elements]



                                                              Minimal MATLAB    18
Array
  Creation
Constant spacing      : (colon operator)
Creating a vector with constant spacing by specifying the         m = first term
first term, the spacing, and the last term:                       q = spacing
                                                                  n = last term
                   variable_name = [m:q:n]

                                 or

                   variable_name = m:q:n

Constant spacing      linspace
Creating a vector with linear (equal) spacing by specifying   xi = first element
the first and last terms, and the number of terms:            xf = Last element
           variable_name = linspace(xi, xf, n)                n = number of elements




                                                                  Minimal MATLAB        19
Array
Array Operators
         Operations        Operation

            +         Addition
            -         Subtraction
            .*        Element-by-element multiplication
            ./        Element-by-element division
            .        Element-by-element left division
            .^        Element-by-element power
            ‘         Transpose




                                                          Minimal MATLAB    20
Array
Data Manipulation



Addressing - Extraction (Subset)
Addressing an element(s) individually or in subgroups:
 To redefine only some of the elements,
 When specific elements are to be used in calculations,
 When a subgroup of the elements is used to define a new variable.




                                                                      Minimal MATLAB    21
Array
  Data Manipulation
       Address      vector



>> ve = [35 46 78 23 5 14 81 3 55]

ve =

  35     46   78   23   5    14   81   3   55
   1     2     3   4    5     6    7   8   9    position or index of element




                                                         Minimal MATLAB     22
Array
Data Manipulation
 Address   matrix            )




                     (1,1)       (1,2)     (1,3)     (1,4)
                      3          11         6        5
                        3        11      6      5
                     (2,1)       (2,2)     (2,3)     (2,4)
           MAT MAT
                 =   =  4         7      10      2
                      4 13       79      0 10 8       2

                     (3,1)       (3,2)     (3,3)     (3,4)
                      13          9         0         8




                                                             Minimal MATLAB    23
Array
Data Manipulation
 Addressing   vector              : (colon operator)




 Addressing   matrix              : (colon operator)



A(:,n)        Refers to the elements in all the rows of column n of the matrix A.
A(n,:)        Refers to the elements in all the columns of row n of the matrix A.
A(:,m:n)      Refers to the elements in all the rows between columns m and n
              of the matrix A.

              Refers to the elements in all the columns between rows m and n
A(m:n,:)
              of the matrix A.
              Refers to the elements in rows m through n and columns p
A(m:n,p:q)
              through q of the matrix A.



                                                                  Minimal MATLAB     24
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    25
Plotting
Creation
Example
       Damped Oscillation equation:




                                      Minimal MATLAB    26
Plotting
Creation
Example
       Damped Oscillation equation:



 1-   a = 0.1; b = 1;
 2-   t = 0:0.1:20;
 3-   y = exp(-a*t).*cos(b*t);
 4-   plot(t,y)
 5-   grid on;
 6-   xlabel('Time in second');
 7-   ylabel('Response in mm');
 8-   title('Damped Oscillation');
 9-   legend('a = 0.1,b = 1)');



                                      Minimal MATLAB    27
Plotting
Creation
Overview

                                title




                                legend




     ylabel   xlabel
                       Minimal MATLAB    28
Plotting
Graphing
Plot function   plot(x,y)
where the variables x and y are vector and must be of same size i.e the number of
elements should be equal.
                 x=[1 2 3 5 7 7.5 8 10];
                 y=[2 6.5 7 7 5.5 4 6 8];
                 plot(x,y)




                                                                 Minimal MATLAB     29
Plotting
 Graphing
 Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

 Line specifiers
 Line specifiers are optional and can be used to define the style and color of the
 line and the type of markers (if markers are desired).

                                    Line Style   Specifier
                               solid (default)       -
                               dashed               --
                               dotted                :
                               dash-dot             -.
        Line color      Specifier                        Line color     Specifier
     red                   r                         magenta               m
     green                 g                         yellow                y
     blue                  b                         black                 k
     cyan                  c                         white                 w

                                                                         Minimal MATLAB         30
Plotting
 Graphing
 Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

 Line specifiers
 Line specifiers are optional and can be used to define the style and color of the
 line and the type of markers (if markers are desired).

         Marker type            Specifier            Marker type           Specifier
 plus sign                                   square                            s
 circle                                      diamond                           d
 asterisk                                    five pointed star                 p
 point                                       six pointed star                  h
 cross                                       triangle (pointed left)           <
 triangle (pointed up)                       triangle (pointed right)          >
 triangle (pointed down)




                                                                         Minimal MATLAB         31
Plotting
 Graphing
 Plot function
plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue)

  Property Name
 To specify the thickness of the line, the size of the marker, and the colors of the marker’s
 edge line and fill




                                                                         Minimal MATLAB      32
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    33
Script
Creation
Overview
   Script      Sequence of MATLAB statements in file
A script file is an external file that contains a sequence of MATLAB statements. Script
files have a filename extension of .m.




                                                                        Minimal MATLAB       34
Script
Basics
Creating script file

Saving script file

Executing script file


Accepting input from user

           variable_name = input(‘prompt message’)

Displaying output to user

           disp(name of a variable)


           disp(‘text as string’)


                                               Minimal MATLAB    35
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    36
Script
Creation
Conditional Statement
Conditional expression consisting of relational and/or logical operators.

             Relational Operators                          Logical Operators
Operations                  Operators         Operations                    Operators
Less than                                     And
Greater than                                  Or
Less than or equal to                         Not
Greater than or equal to
Equal to
Not Equal to




                                                                      Minimal MATLAB        37
Script
Creation
Example
                                   g1
     Plot                          g2
                                   g3




1-   t = linspace(-5,10);
2-   g1 = -4 - 2*t; g1
     g2 = -4 + 3*t; g2
3-
     g3 = 16 - 2*t; g3
4-   g =    g1.*(-2<t & t<=0)...
5-        + g2.*(0<t & t<=4)...
6-        + g3.*(4<t & t<=8);
7-   plot(t,g)



                                        Minimal MATLAB    38
Script
if/elseif/else Statement




                                   if expression
               False        if
                                      statements
                       statement
                                   elseif expression
                                      statements
                                   else
                            True      statements
                                   end
         Statement     Statement
          Group 2       Group 1

                            End




                                      Minimal MATLAB    39
Script
  Creation
  Switch Statement
               Flowchart



                                                  switch switch_expression
                                                     case case_expression
                                                        statements
      True      match      False                     case case_expression
                case, k                                 statements
                                                      :
                                                     otherwise
                                                        statements
    case                       case       False   end
statement, k                   end


                                   True




                                                            Minimal MATLAB    40
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
    Loop control statements
   Functions – creating & executing a function file




                                                                    41
Script
Loop control statements
for (Iterative Loop)

             for index = start:increment:end
                    statements
             end


while (Conditional Loop)

              while expression
                 statements
              end



continue, break



                                               Minimal MATLAB    42
Agenda

 Introducing MATLAB – Language of technical computing
 Minimal MATLAB
   MATLAB Desktop
   Computing with MATLAB
   Array – creation & working
   Plotting – creating & visualizing
   Automating commands – creating, saving & executing script file
   Conditional control statements
   Loop control statements
   Functions – creating & executing a function file




                                                                    43
Function
 Creation
 Overview
          Set of inputs                                      Set of outputs
                1                     SYSTEM                         1
                2             [Mathematical model of                 2
                3                                                    3
                               Physical Phenomenon]

                m                   (function file)                  n

[Input variables]                     [System]                     [Output variables]
current, voltage,             Electrical Networks                    current, voltage,
power, energy,                Communication System                     power, energy,
temperature,                  Control System                            temperature,
data                          Thermodynamic System                    Processed data
                              Computer system, etc


     function [out1, out2, ...] = func_name(in1, in2, ...)

     declares the function func_name, and its inputs and outputs


                                                                     Minimal MATLAB      44
Function
    Creation
    Example

  X                               Black Box                        Y




      1-       function H = lp(R,C)
      2-       w = logspace(-2,6);
               H = 1./sqrt(1 + (w*R*C).^2);
      3-
               semilogx(w,H)
      4-       xlabel('Angular frquency, w (rad/s)');
      5-       ylabel('System Response');
      6-       title('Low-Pass frequency response');




Source: MATLAB An Introduction with Applications, Amos Gilat, 4e
                                                                       Minimal MATLAB    45
Function
Creation
Example




           Minimal MATLAB    46
Function
    Creation
    Example

                                                         Black Box




Source: MATLAB An Introduction with Applications, Amos Gilat, 4e
                                                                     Minimal MATLAB    47
Function
 Creation
 Example

1-   function [Td, RH] = DewptRhum(T,Tw,BP)
2-   es = 6.112*exp((17.67*T)/(T + 243.5));
     ew = 6.112*exp((17.67*Tw)/(Tw + 243.5));
3-   e = ew - BP*(T - Tw)*0.00066*(1 + 0.00115*Tw);
4-   RH = 100*(e/es);
5-   Td = (243.5*log(e/6.112))/(17.67 - log(e/6.112));




                                            Minimal MATLAB    48
BEFORE
LEAVING
More on MATLAB

1.   MATLAB Tutorials
2.   Demo and Webinars
3.   Documentations
4.   MATLAB Central (User Community)




                          www.mathworks.com
Reference
1. Getting started with MATLAB, A quick introduction for scientists and engineers,
   Rudra Pratap, Indian edition
2. MATLAB An Introduction with Applications, Amos Gilat, 4e
3. Matlab - A Practical Introduction to Programming and Problem Solving, Stormy
   Attaway, 2e
4. Essential MATLAB for Engineers and Scientists, Brian D. Hahn & Daniel T.
   Valentine
5. A Guide to MATLAB for Beginners and Experienced Users, Hunt Lipsman &
   Rosenberg
THANKS    We speak MATLAB!




ASHISH MESHRAM
meetashish85@gmail.com
www.facebook.com/ashishmeet

Introduction to MATLAB

  • 1.
    Do you speakMATLAB!
  • 2.
    MATLAB ® R2012a (7.14) MathWorks®
  • 3.
    MATLAB Is an integrated technical computing environment that combines numeric computation, advanced graphics and visualization, and a high-level programming language. The MathWorks, Inc. High-level language of technical computing Development environment for engineers, scientists Interactive tools for design, problem solving Mathematical function libraries Graphics and data visualization tools Custom GUIs External Interfaces: C, C++, Fortran, Java, COM, Excel, .NET Introducing MATLAB – Language of technical computing 2
  • 4.
    MATLAB product family MATLAB MATLAB Programming Language User-written Functions Built-in Functions Graphics Computations External Interfaces 2D Graphics Linear Algebra C 3D Graphics Data Analysis C++ Color & Lighting Polynomials JAVA Animation Interpolations .net Image, Audio & Video Solutions of ODE Micorosoft Excel Toolboxes: Collections of specialized Functions Parallel Computing Parallel Computing Toolbox Toolbox Parallel Computing Spreadsheet Link EX (for Microsoft Excel) Symbolic Math Toolbox MATLAB Compiler Computational Finance Financial Optimization Computational Finance Toolbox Math, Statistics, and OptimizationDistributed Toolbox Toolbox Bioinformatics Toolbox Math, Data Acquisition Computing Server Statistics, and Optimization MATLAB MATLAB Builder NE (for Equation Toolbox Partial System Toolbox Econometrics Global Optimization Toolbox Biology Fixed-Income Toolbox Computational InstrumentControlDifferential Microsoft .NETRobustFixed-Point Toolbox and Verification Framework) Image Processing Toolbox Datafeed Toolbox Toolbox Signal Control Toolbox Control MATLAB Coder SimBiology Generation Code Control System Design and Analysis StatisticsMATLAB Builder JA (for Java language) Network Toolbox Financial Derivatives Toolbox Toolbox System Identification System Coder Neural Image Acquisition ToolboxVision ToolboxToolbox RF ToolboxControl Toolbox Model Predictive Application Deployment Computer Toolbox HDLDatabase Toolbox DSP System Signal Processing and Communications Curve Fitting Toolbox EX Toolbox Database (for Aerospace Toolbox Calibration Toolbox Model-Based MATLAB Builder HDL Microsoft Excel) Array System OPC VisionFuzzy Logic Toolbox ToolboxVerifier Database Connectivity and Reporting Image Processing and Computer Toolbox Image AcquisitionSystem Toolbox Database ConnectivityToolbox Communications Phased and Reporting Test and Measurement Test and Measurement Wavelet Toolbox Generator MATLAB Report Vehicle Network Spreadsheet Link EX (for Microsoft Excel) ToolboxFilter Design HDL Coder Mapping Toolbox Introducing MATLAB – Language of technical computing 3
  • 5.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB 4
  • 6.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 5
  • 7.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 6
  • 8.
    MATLAB - Desktop CurrentWindow Editor Directory Command Window Command History Workspace All commandsall youron edit, create, and save your own scriptfar along within files type The is where you characterizedthe current directory are listed. programs there called typed files from that you have command prompt This main windowwrite, variablesby the MATLABcommand window get recorded, even subwindow lists all MATLAB prompt in the generated so or (>>) m-files. across multiple session in this window. and size. Minimal MATLAB 7
  • 9.
    MATLAB - Desktop Help Window The main window characterized by the MATLAB command prompt (>>) Minimal MATLAB 8
  • 10.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 9
  • 11.
    Computing with MATLAB BasicData and Variables Variable Value Size Class Minimal MATLAB 10
  • 12.
    Computing with MATLAB Basic Data and Variables Multidimensional Array Scalar [full or sparse] function logical char numeric cell struct handle (@)  ab { } int8, uint8, single double int16, unit16, int32, unit32, int64, unit64 Minimal MATLAB 11
  • 13.
    Computing with MATLAB ArithmeticOperator & Their Precedence Operations Operators Examples Addition Subtraction Multiplication Right Division Left Division Exponentiation Precedence Order Operators 1 Parentheses ( ). For nested parentheses, the innermost are executed first. 2 Exponentiation, ^ 3 Multiplication, *; Division, /, 4 Addition, +; Subtraction, - help precedence Minimal MATLAB 12
  • 14.
    Computing with MATLAB MathematicalFunctions Trigonometry Radian Degree Basic Hyperbolic Inverse Basic sin sinh asin sind cos cosh acos cosd tan tanh atan tand cot coth acot cotd sec sech asec secd csc csch acsc cscd Exponential Complex Discrete Math Matrix exp abs factorial det log conj gcd trace log10 imag lcm rank log2 real primes inv sqrt angle expm sqrtm help <type function name> Minimal MATLAB 13
  • 15.
    Computing with MATLAB PredefinedVariables and Keywords Predefined Variables ans pi eps inf i,j NaN Keywords break case catch classdef continue else elseif end for function global if otherwise parfor persistent return spmd switch try while help <Predefined Variable Name> iskeyword Minimal MATLAB 14
  • 16.
    Computing with MATLAB ManagingVariables Command Outcome clear Clears the workspace, all variables are removed from the memory clear x y z Clears only variables x, y, and z clear all Clears all variables and functions from workspace who Lists variables currently in the workspace whos Lists variables currently in the workspace with their sizes together with information about their bytes and class Minimal MATLAB 15
  • 17.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 16
  • 18.
    Array Overview 5 7 5 5 7 4 4 Scalar of 1 X 1 Row Vector of 1 X 3 Column vector of 3 X 1 5 14 12 10 56 5 14 12 10 56 5 10 56 6 10 504 4 6 5 14 12 10 56 6 6 4 6 5 10 56 85 23 89 0 5 10 56 85 23 89 0 7 78 86 6 4 6 23 2 6 4 6 23 2 0 7 78 86 53 6 7 78 86 53 6 One Dimensional Matrix of 3 X 3 Two Dimensional Three Dimensional Matrix of 3 X 5 Matrix of 3 X 5 Minimal MATLAB 17
  • 19.
    Array Creation  froma known list of numbers  with constant spacing by specifying the first term, the spacing, and the last term Known lists of numbers One Dimensional Array variable_name = [ type vector elements ] Two Dimensional Array variable_name = [1st row elements; 2nd row elements; 3rd row elements; ... ; last row elements] Minimal MATLAB 18
  • 20.
    Array Creation Constantspacing : (colon operator) Creating a vector with constant spacing by specifying the m = first term first term, the spacing, and the last term: q = spacing n = last term variable_name = [m:q:n] or variable_name = m:q:n Constant spacing linspace Creating a vector with linear (equal) spacing by specifying xi = first element the first and last terms, and the number of terms: xf = Last element variable_name = linspace(xi, xf, n) n = number of elements Minimal MATLAB 19
  • 21.
    Array Array Operators Operations Operation + Addition - Subtraction .* Element-by-element multiplication ./ Element-by-element division . Element-by-element left division .^ Element-by-element power ‘ Transpose Minimal MATLAB 20
  • 22.
    Array Data Manipulation Addressing -Extraction (Subset) Addressing an element(s) individually or in subgroups:  To redefine only some of the elements,  When specific elements are to be used in calculations,  When a subgroup of the elements is used to define a new variable. Minimal MATLAB 21
  • 23.
    Array DataManipulation Address vector >> ve = [35 46 78 23 5 14 81 3 55] ve = 35 46 78 23 5 14 81 3 55 1 2 3 4 5 6 7 8 9 position or index of element Minimal MATLAB 22
  • 24.
    Array Data Manipulation Address matrix ) (1,1) (1,2) (1,3) (1,4) 3 11 6 5 3 11 6 5 (2,1) (2,2) (2,3) (2,4) MAT MAT = = 4 7 10 2 4 13 79 0 10 8 2 (3,1) (3,2) (3,3) (3,4) 13 9 0 8 Minimal MATLAB 23
  • 25.
    Array Data Manipulation Addressing vector : (colon operator) Addressing matrix : (colon operator) A(:,n) Refers to the elements in all the rows of column n of the matrix A. A(n,:) Refers to the elements in all the columns of row n of the matrix A. A(:,m:n) Refers to the elements in all the rows between columns m and n of the matrix A. Refers to the elements in all the columns between rows m and n A(m:n,:) of the matrix A. Refers to the elements in rows m through n and columns p A(m:n,p:q) through q of the matrix A. Minimal MATLAB 24
  • 26.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 25
  • 27.
    Plotting Creation Example Damped Oscillation equation: Minimal MATLAB 26
  • 28.
    Plotting Creation Example Damped Oscillation equation: 1- a = 0.1; b = 1; 2- t = 0:0.1:20; 3- y = exp(-a*t).*cos(b*t); 4- plot(t,y) 5- grid on; 6- xlabel('Time in second'); 7- ylabel('Response in mm'); 8- title('Damped Oscillation'); 9- legend('a = 0.1,b = 1)'); Minimal MATLAB 27
  • 29.
    Plotting Creation Overview title legend ylabel xlabel Minimal MATLAB 28
  • 30.
    Plotting Graphing Plot function plot(x,y) where the variables x and y are vector and must be of same size i.e the number of elements should be equal. x=[1 2 3 5 7 7.5 8 10]; y=[2 6.5 7 7 5.5 4 6 8]; plot(x,y) Minimal MATLAB 29
  • 31.
    Plotting Graphing Plotfunction plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Line Style Specifier solid (default) - dashed -- dotted : dash-dot -. Line color Specifier Line color Specifier red r magenta m green g yellow y blue b black k cyan c white w Minimal MATLAB 30
  • 32.
    Plotting Graphing Plotfunction plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Line specifiers Line specifiers are optional and can be used to define the style and color of the line and the type of markers (if markers are desired). Marker type Specifier Marker type Specifier plus sign square s circle diamond d asterisk five pointed star p point six pointed star h cross triangle (pointed left) < triangle (pointed up) triangle (pointed right) > triangle (pointed down) Minimal MATLAB 31
  • 33.
    Plotting Graphing Plotfunction plot(x,y,‘line specifiers’,‘PropertyName’,PropertyValue) Property Name To specify the thickness of the line, the size of the marker, and the colors of the marker’s edge line and fill Minimal MATLAB 32
  • 34.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 33
  • 35.
    Script Creation Overview Script Sequence of MATLAB statements in file A script file is an external file that contains a sequence of MATLAB statements. Script files have a filename extension of .m. Minimal MATLAB 34
  • 36.
    Script Basics Creating script file Savingscript file Executing script file Accepting input from user variable_name = input(‘prompt message’) Displaying output to user disp(name of a variable) disp(‘text as string’) Minimal MATLAB 35
  • 37.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 36
  • 38.
    Script Creation Conditional Statement Conditional expressionconsisting of relational and/or logical operators. Relational Operators Logical Operators Operations Operators Operations Operators Less than And Greater than Or Less than or equal to Not Greater than or equal to Equal to Not Equal to Minimal MATLAB 37
  • 39.
    Script Creation Example g1 Plot g2 g3 1- t = linspace(-5,10); 2- g1 = -4 - 2*t; g1 g2 = -4 + 3*t; g2 3- g3 = 16 - 2*t; g3 4- g = g1.*(-2<t & t<=0)... 5- + g2.*(0<t & t<=4)... 6- + g3.*(4<t & t<=8); 7- plot(t,g) Minimal MATLAB 38
  • 40.
    Script if/elseif/else Statement if expression False if statements statement elseif expression statements else True statements end Statement Statement Group 2 Group 1 End Minimal MATLAB 39
  • 41.
    Script Creation Switch Statement Flowchart switch switch_expression case case_expression statements True match False case case_expression case, k statements : otherwise statements case case False end statement, k end True Minimal MATLAB 40
  • 42.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 41
  • 43.
    Script Loop control statements for(Iterative Loop) for index = start:increment:end statements end while (Conditional Loop) while expression statements end continue, break Minimal MATLAB 42
  • 44.
    Agenda Introducing MATLAB– Language of technical computing Minimal MATLAB MATLAB Desktop Computing with MATLAB Array – creation & working Plotting – creating & visualizing Automating commands – creating, saving & executing script file Conditional control statements Loop control statements Functions – creating & executing a function file 43
  • 45.
    Function Creation Overview Set of inputs Set of outputs 1 SYSTEM 1 2 [Mathematical model of 2 3 3 Physical Phenomenon] m (function file) n [Input variables] [System] [Output variables] current, voltage, Electrical Networks current, voltage, power, energy, Communication System power, energy, temperature, Control System temperature, data Thermodynamic System Processed data Computer system, etc function [out1, out2, ...] = func_name(in1, in2, ...) declares the function func_name, and its inputs and outputs Minimal MATLAB 44
  • 46.
    Function Creation Example X Black Box Y 1- function H = lp(R,C) 2- w = logspace(-2,6); H = 1./sqrt(1 + (w*R*C).^2); 3- semilogx(w,H) 4- xlabel('Angular frquency, w (rad/s)'); 5- ylabel('System Response'); 6- title('Low-Pass frequency response'); Source: MATLAB An Introduction with Applications, Amos Gilat, 4e Minimal MATLAB 45
  • 47.
    Function Creation Example Minimal MATLAB 46
  • 48.
    Function Creation Example Black Box Source: MATLAB An Introduction with Applications, Amos Gilat, 4e Minimal MATLAB 47
  • 49.
    Function Creation Example 1- function [Td, RH] = DewptRhum(T,Tw,BP) 2- es = 6.112*exp((17.67*T)/(T + 243.5)); ew = 6.112*exp((17.67*Tw)/(Tw + 243.5)); 3- e = ew - BP*(T - Tw)*0.00066*(1 + 0.00115*Tw); 4- RH = 100*(e/es); 5- Td = (243.5*log(e/6.112))/(17.67 - log(e/6.112)); Minimal MATLAB 48
  • 50.
  • 51.
    More on MATLAB 1. MATLAB Tutorials 2. Demo and Webinars 3. Documentations 4. MATLAB Central (User Community) www.mathworks.com
  • 52.
    Reference 1. Getting startedwith MATLAB, A quick introduction for scientists and engineers, Rudra Pratap, Indian edition 2. MATLAB An Introduction with Applications, Amos Gilat, 4e 3. Matlab - A Practical Introduction to Programming and Problem Solving, Stormy Attaway, 2e 4. Essential MATLAB for Engineers and Scientists, Brian D. Hahn & Daniel T. Valentine 5. A Guide to MATLAB for Beginners and Experienced Users, Hunt Lipsman & Rosenberg
  • 53.
    THANKS We speak MATLAB! ASHISH MESHRAM meetashish85@gmail.com www.facebook.com/ashishmeet