Matrix Laboratory

Embed Size (px)

Citation preview

  • 8/6/2019 Matrix Laboratory

    1/66

    MATrix LABoratory

  • 8/6/2019 Matrix Laboratory

    2/66

    Matlab(FAQ) MATLAB was originally developed to be a

    "matrix laboratory," written to provide easy

    access to matrix software developed by the

    LINPACK and EISPACK projects

    Matlab is used for both symbolic and

    numerical computation and visualization

  • 8/6/2019 Matrix Laboratory

    3/66

    Origin MATLAB was invented in the late 1970s by

    Cleve Barry Moler, then chairman of the computerscience department at the University of NewMexico

    He designed it to give his students access toLINPACK and EISPACK without having to learnFortran. It soon spread to other universities andfound a strong audience within the appliedmathematics community

  • 8/6/2019 Matrix Laboratory

    4/66

  • 8/6/2019 Matrix Laboratory

    5/66

    MATLAB is an interactive matrix based system for

    scientific and engineering numerical computation

    simulation and visualization of problems

    MATLAB features a set of application specific

    solutions called toolboxes Toolboxes-comprehensive collection of Matlab

    functions(M files)

    Type demo matlab general at the command

    prompt to get a feel of the language capabilites

  • 8/6/2019 Matrix Laboratory

    6/66

    Toolboxes available

  • 8/6/2019 Matrix Laboratory

    7/66

    Matlab Development

    Environment(MDE) By double clicking on the shortcut

    From the Run dialog

    It is a five panel layout with

    A) launchpad

    B)workspace variables

    C)command window

    4)command histroy

    5)current directory-workspace

  • 8/6/2019 Matrix Laboratory

    8/66

    SIMULINK (FAQ) SIMULINK is an interactive system for the

    nonlinear simulation of dynamic systems.

    A graphical, mouse-driven program that

    allows systems to be modeled by drawing a

    block diagram on the screen.

  • 8/6/2019 Matrix Laboratory

    9/66

    Simulink Simulink is a block library tool formodeling, simulating andanalyzing dynamic systems. It is

    developed by The MathWorks andworks with their flagship product,MATLAB

    SIMULINK is an interactive systemfor the nonlinear simulation ofdynamic systems.

  • 8/6/2019 Matrix Laboratory

    10/66

    Simulink It can handle linear, nonlinear, continuous-time,

    discrete-time, multivariable, and multirate systems SIMULINK is fully integrated with MATLAB,

    and, together with MATLAB and the ControlSystem Toolbox, forms a complete control system

    design and analysis environment Simulink libraries (blockset) can beinvoked from the command prompt bytyping simulink

    Type toilet at the matlab commandprompt to run a simulation and feel ofSimulink

    Type vibes to see the L shaped logo

  • 8/6/2019 Matrix Laboratory

    11/66

    Simulink is widely used in control theory and

    digital signal processing for multidomainsimulation and design.

    Coupled withReal Time Workshop, another

    product from The MathWorks, Simulink can do

    automatic code generation for real-timeimplementation of systems.

    In addition to being a popular tool for embedded

    system design work because of its flexibility and

    capacity for quick iteration,it also supports third-

    party hardware and software products

  • 8/6/2019 Matrix Laboratory

    12/66

    Matlab

    MATLAB is the "Matrix Laboratory", and so

    provides many convenient ways for creatingarrays of various dimensions.

    In the MATLAB vernacular, a vectorrefers to aone dimensional (1NorN1) matrix, commonly

    referred to as an array in other programminglanguages. a matrix generally refers to a multi-dimensional matrix, that is, a matrix with morethan one dimension

    Unlike languages such as Java and C++, m is notstatically typed, meaning that variables themselvesdo not have types, only the runtime values storedin those variables have types,

  • 8/6/2019 Matrix Laboratory

    13/66

    Besides Matlab also houses its compilerwhich converts the M-file into C or C++.

    The source code is compiled into object

    code using a C/C++ system compiler.

    The resulting code is linked with the

    corresponding math and graphics libraries

    and any application-specific files making itsan application development tool

  • 8/6/2019 Matrix Laboratory

    14/66

    Matlab basics

  • 8/6/2019 Matrix Laboratory

    15/66

    Matlab programs can be run from the commandline or by use of a GUI

    Run from the command line 2.3+4.2

    Matlab woks on different types of data objects thatare manipulated

    They are scalars ,vectors and matrices A=[1 2 3]

    B=[2 2;2 2];

    C=2:8

  • 8/6/2019 Matrix Laboratory

    16/66

    M files All functions can be combined and run as a

    sequential file on disk called M file

    Format rat-rationalize

    Format compact

    Format long

    Sqrt(2)

  • 8/6/2019 Matrix Laboratory

    17/66

    Matlab datatypes

    The MATLAB language works with only a singleobject type: the MATLAB array.

    All MATLAB variables, including scalars,vectors, matrices, strings, cell arrays,structures,and objects are stored as MATLABarrays.

    In MATLAB, subscripts begin at 1 rather than 0. Every MATLAB data object is an array. The two-

    dimensional version of an array is called a matrix.

    MATLAB functions are vectorized.

  • 8/6/2019 Matrix Laboratory

    18/66

    Trigonometric functions Cos(pi/6)

    Tan(pi/2)

    9/0

    0/0

    Nan/Inf IEEE floating point

  • 8/6/2019 Matrix Laboratory

    19/66

    Symbolic object Matlab can also do symbolic computation

    Syms a b c x

    F=sym(a*x^2+b*x+c)

    solve(F)

    Pretty(F)

  • 8/6/2019 Matrix Laboratory

    20/66

    Variable assignment

    X=7

    X^2+3*x+2

    Log(x) Y=5*x

    X=x+5

    Who-all variables u have created in this session

    whos

    Save as mat file

    Clear

    Load mat file

  • 8/6/2019 Matrix Laboratory

    21/66

    Matrices Matrices can be entered in different ways

    As an explicit list

    Inbuilt functions

    Loaded from the diskfile for a local editor

    Loaded from external data

  • 8/6/2019 Matrix Laboratory

    22/66

    A=[1 2 3 ;4 5 6;7 8 9]

    Matlab is case sensitive

    Size(a)

    A=[1 2 ; 3 4] +i[5 6;7 8]-complex no

    Vectors are matrices with a single row/column

    B=[1;2;3] W=[1 2 3 4]

    Define even spaced matrices-2:5

    [3:2:9]

    A(3,2)

    A(3,:)

    A(:,3)

  • 8/6/2019 Matrix Laboratory

    23/66

    Command line help Helpwin

    Doc

    Lookfor

    Help followed by function name

  • 8/6/2019 Matrix Laboratory

    24/66

    Concatenate-cat

    b=[2 2;4 5] A=[1 2 3; 4 5 6]

    [A b]

    Dot product and cross product A=[1; 4 ;3; 5] B=[ 7;9;5;0]

    Dot[A,B]=dot[B,A]

    Length=sqrt(dot(A,A))

    Norm

    cosU=v.w/(length(v)*length(w))

  • 8/6/2019 Matrix Laboratory

    25/66

    U=[1;2;3] V=[7;8;9]

    Cross(U,V)

    Dot(cross ans and primary vectors is zero)

    Matrix operations A+B, 2*A

    Matrix building functions

    Eye,zeros,ones,diag,Triu,Tril,Rand,Hilb,ma

    gic,toeplitz

    Eig ,inv,det,rank

    Programming Constructs: for ,while, if

  • 8/6/2019 Matrix Laboratory

    26/66

    Script- is a sequence of normal matlab statements.scriptsare invoked at the command line by their name

    Functions- are scripts with input and output arguments ,eg

    open m editor and type function [mean,stdev]=stat(x)

    % comment and help

    [m,n]=size(x)

    If m==1 m=n;

    End

    Mean=sum(x)/m

    Stdev= sqrt(sum(x.^2)/m mean.^2); vectorize Save as stat.m

    A command [a,b]=stat(c) will assign mean and standarddeviation to a and b

  • 8/6/2019 Matrix Laboratory

    27/66

    PATH PATH setting path and preferences

    C:\MATLAB6p1\toolbox\matlab\matfun

    Each set of functions in a specific area is

    called a toolbox

    Each toolbox will contain a contents.m

    which is the function summary

  • 8/6/2019 Matrix Laboratory

    28/66

    Datafun The data functions in matlab -Data analysis

    and Fourier transforms.

    Basic operations

    Finite differences.

    Filtering and convolution.

    Fourier transforms

  • 8/6/2019 Matrix Laboratory

    29/66

    Matfun directory Matfun directory -

    C:\MATLAB6p1\toolbox\matlab\matfun

    The matlab functions in matfun

    Matrix analysis

    Linear equations

    Eigenvalues and singular values Matrix functions

    Factorization utilities

  • 8/6/2019 Matrix Laboratory

    30/66

    Datatypes supported by matlab C:\MATLAB6p1\toolbox\matlab\datatypes

    Data types (classes)

    Multi-dimensional array functions

    Cell array functions

    Structure functions

    Function handle functions Object oriented programming functions

    Overloadable operators

  • 8/6/2019 Matrix Laboratory

    31/66

    Stand-Alone Programs In addition to writing M-files, there are three other

    ways you can call MATLAB functions:via MEX-

    files or via the MATLAB Engine, or by usingeither the MATLAB C or C++ Math Library.

    Any M-file, MEX file, or Engine code you write

    requires the entire MATLAB environment to run.

    However, with the MATLAB C and C++ Mathlibraries, you can write stand-alone (external)

    programs.

  • 8/6/2019 Matrix Laboratory

    32/66

    Data Storage

    All MATLAB data is stored columnwise.

    This is how Fortran stores matrices;

    MATLAB uses this convention because itwas originally written in Fortran. For

    example, given the matrix

    a=['house'; 'floor'; 'porch']

  • 8/6/2019 Matrix Laboratory

    33/66

    Data Types in MATLAB

    Cell Arrays- Cell arrays are a special class of

    MATLAB array whose elements consist of cells

    that themselves contain MATLAB arrays. Bothstructures and cell arrays provide a hierarchical

    storage mechanism for dissimilar kinds of data.

    Structures-Structures are collections of different

    kinds of data organized by named fields.

    Objects-timer ,serial objects

  • 8/6/2019 Matrix Laboratory

    34/66

    Cell array You can build a cell array by assigning data

    to individual cells, one cell at a time.

    MATLAB automatically builds the array asyou go along. There are two ways to assign

    data to cells:

    Cell indexing Content indexing

  • 8/6/2019 Matrix Laboratory

    35/66

    Cell indexing Enclose the cell subscripts in parentheses usingstandard array notation.Enclose the cell contents

    on the right side of the assignment statement incurlybraces, "{}.

    For example, create a 2-by-2 cell array A A(1,1) = {[1 4 3; 0 5 8; 7 2 9]};

    A(1,2) = {'Anne Smith'};

    Cell arrays in MATLAB are multidimensional

    arrays whose elements are copies of other arrays.A cell array of empty matrices can be created withthe cell function.

    A = cell(8,1);

  • 8/6/2019 Matrix Laboratory

    36/66

    Content indexing Enclose the cell subscripts in curly braces

    using standard array notation. Specify the

    cell contents on the right side of theassignment statement.

    A{2,1} = 3+7i;

    A{8,1} = -pi:pi/10:pi;

  • 8/6/2019 Matrix Laboratory

    37/66

    Structure Structures are MATLAB arrays with named "data

    containers" called fields

    The fields of a structure can contain any kind ofdata.

    For example, one field might contain a textstringrepresenting a name, another might contain ascalar representing a billing amount, a third mighthold a matrix of medical test results,

    patient.name = 'John Doe'; patient.billing = 127.00;

    patient.test = [79 75 73; 180 178 177.5; 220 210205]

  • 8/6/2019 Matrix Laboratory

    38/66

    Function Handles You can create a handle to any MATLAB function

    and then use that handle as a means of referencingthe function. A function handle is typically passedin an argument list to other functions, which canthen execute, or evaluate, the function using the

    handle. Construct a function handle in MATLAB usingthe at sign, @, before the function name.

    Evaluate a function handle using the MATLAB

    feval function. fhandle=@sqrt;

    feval(fhandle,9);

    feval(fhandle,25);

  • 8/6/2019 Matrix Laboratory

    39/66

    Function handles enable you to do all of

    The following: Pass function access information to otherfunctions

    Allow wider access to subfunctions and private

    functions Ensure reliability when evaluating functions

    Reduce the number of files that define yourfunctions

    Improve performance in repeated operations Manipulate handles in arrays, structures, and cell

    arrays

  • 8/6/2019 Matrix Laboratory

    40/66

    A Simple Function Handle handle = @function

    F = functions(@disp)

    F=figure

    Get(F);

    Serial object ,s = serial('COM1');

  • 8/6/2019 Matrix Laboratory

    41/66

    Specialized functions C:\MATLAB6p1\toolbox\matlab\specfun

    Specialized math functions.

    Number theoretic functions

    Coordinate transforms

    Others

  • 8/6/2019 Matrix Laboratory

    42/66

    Matfun directory PATH

    :C:\MATLAB6p1\toolbox\matlab\matfun

    Each set of functions in a specific area is

    called a toolbox

    Each toolbox will contain a contents.m

    which is the function summary

  • 8/6/2019 Matrix Laboratory

    43/66

    General purpose C:\MATLAB6p1\toolbox\matlab\general-Generalpurpose commands

    General information

    Managing the workspace Managing commands and functions

    Managing the search path

    Controlling the command window

    Operating system commands

    Debugging M-files and profiling M-files.

    Obsolete functions

    O

    thers and GUI utilities

  • 8/6/2019 Matrix Laboratory

    44/66

    Elementary math functions C:\MATLAB6p1\toolbox\matlab\elfun

    Trigonometric

    Exponential

    Complex

    Rounding and remainder

  • 8/6/2019 Matrix Laboratory

    45/66

    Elementary matrix functions C:\MATLAB6p1\toolbox\matlab\elmat

    Elementary matrices

    Basic array information

    Matrix manipulation

    Special variables and constants

    Specialized matrices and obsolete functions

  • 8/6/2019 Matrix Laboratory

    46/66

    Function functions and ODE

    solvers. C:\MATLAB6p1\toolbox\matlab\funfun Optimization and root finding

    Optimization Option handling

    Numerical integration (quadrature)

    Plotting and Inline function object

    Differential equation solvers Boundary value problem solver forODEs.

  • 8/6/2019 Matrix Laboratory

    47/66

    File input/output C:\MATLAB6p1\toolbox\matlab\iofun File import/export functions.

    Image file import/export

    Audio file andVideo file import/export Formatted file I/O

    String conversion

    File opening and closing.

    Serial port support

    Command window I/O

    FIG file support for plotedit and printframes

  • 8/6/2019 Matrix Laboratory

    48/66

    Programming language

    constructs C:\MATLAB6p1\toolbox\matlab\lang

    Control flow.

    Evaluation and execution

    Scripts, functions, and variables.

    Argument handling

    Message display

    Interactive input

  • 8/6/2019 Matrix Laboratory

    49/66

    Operators and special characters

    C:\MATLAB6p1\toolbox\matlab\ops

    Arithmetic operators

    Relational operators

    Logical operators

    Special characters

    Bitwise operators

    Set operators and Additional help files.

  • 8/6/2019 Matrix Laboratory

    50/66

    Sparse matrices.

    C:\MATLAB6p1\toolbox\matlab\sparfun

    Elementary sparse matrices

    Linear algebra.and Linear Equations

    (iterative methods)

    Operations on graphs (trees)

  • 8/6/2019 Matrix Laboratory

    51/66

    String functions-Character strings

    C:\MATLAB6p1\toolbox\matlab\strfun

    General

    String tests

    String operations

  • 8/6/2019 Matrix Laboratory

    52/66

    Time and dates.

    C:\MATLAB6p1\toolbox\matlab\timefun

    Basic functions

    Date functions and Timing functions

  • 8/6/2019 Matrix Laboratory

    53/66

    C:\MATLAB6p1\toolbox\matlab\

    winfun Windows Operating System Interface Files

    (DDE/ActiveX)

    ActiveX Client Functions.

    DDE Client Functions

  • 8/6/2019 Matrix Laboratory

    54/66

    Handle Graphics

    C:\MATLAB6p1\toolbox\matlab\graphics

    Figure window creation and control.

    Axis creation and control

    Handle Graphics objects and operations

    Hardcopy and printing

    Printing and I/O utilities

  • 8/6/2019 Matrix Laboratory

    55/66

    2d graphs

    C:\MATLAB6p1\toolbox\matlab\graph2d

    Elementary X-Y graphs

    Axis control

    Scribe utilites

  • 8/6/2019 Matrix Laboratory

    56/66

    Three dimensional graphs.

    C:\MATLAB6p1\toolbox\matlab\graph3d

    Elementary 3-D plots

    Color control and lighting

    Colormaps

    Axis control

    Viewpoint control Camera control

    Graph notation and printing utility

  • 8/6/2019 Matrix Laboratory

    57/66

    Graphical user interface tools

    C:\MATLAB6p1\toolbox\matlab\uitools

    GUI functions

    GUI design tools

    Dialog boxes

    Menu utilities

    Preferences and Miscellaneous utilities

    Helper and obsolete functions

  • 8/6/2019 Matrix Laboratory

    58/66

    Version control

    C:\MATLAB6p1\toolbox\matlab\verctrl

    Version control commands

    Specific version control

    Demos-

    C:\MATLAB6p1\toolbox\matlab\demos

  • 8/6/2019 Matrix Laboratory

    59/66

    Some Toolboxes.. Symbolic Math Toolbox Control System Toolbox

    Neural Network Toolbox

    Instrument Control Toolbox

    Data Acquisition Toolbox

    Mapping Toolbox

    Model Predictive Control Toolbox.

    Signal Processing Toolbox Partial Differential Equation Toolbox

    MATLAB Report Generator

  • 8/6/2019 Matrix Laboratory

    60/66

    Sparse matrices.

    A=[2 3 4;7 0 8;6 0 0]

    B=sparse(A)

    Its a different datatype

  • 8/6/2019 Matrix Laboratory

    61/66

    String functions-Character strings

    C:\MATLAB6p1\toolbox\matlab\strfun

    General

    String tests

    String operations

    Character array

    name = 'Thomas R. Lee'; title = ' Sr. Developer';

    strcat(name,',',title)

  • 8/6/2019 Matrix Laboratory

    62/66

    Text string

    Assign variable to character array

    S=this is a string

    Disp(this is a print display)

    User prompt-input(enter the variable)

    Error(error message)

  • 8/6/2019 Matrix Laboratory

    63/66

    MATLAB provides several functions for

    searching and replacing characters in a

    string.

    label = 'Sample 1, 10/28/95';

    newlabel = strrep(label,'28','30')

    position = findstr('amp',label)

    S i /N i C i

  • 8/6/2019 Matrix Laboratory

    64/66

    String/Numeric Conversion

    MATLAB's string/numeric conversion functions

    change numeric values into character strings. Youcan store numeric values as digit-by-digit stringrepresentations, or convert a value into ahexadecimal or binary string.

    x=5317 By default, MATLAB stores the number x as a 1-

    by-1 double array containing the value 5317.

    y = int2str(x);

    The int2str (integer to string) function breaks thisscalar into a 1-by-4 vector containing the string'5317'.

    p = num2str(pi,9)

  • 8/6/2019 Matrix Laboratory

    65/66

    Creating Multidimensional

    Arrays One way to create a multidimensional array

    is to create a two-dimensional array and

    extend it.

    A = [5 7 8; 0 1 9; 4 3 6];

    A(:,:,2) = [1 0 4; 3 5 6; 9 8 7]

  • 8/6/2019 Matrix Laboratory

    66/66

    Command /function duality

    Clear

    Save junk x y z

    Whos are commands

    While

    Sin(x),Plot(x,y) are functions

    Both are duals

    axis off and axis(off)