matlab course1

Upload: mohamad-moezi

Post on 06-Apr-2018

233 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Matlab Course1

    1/11

    MATLAB - Introduction course

    Fanilo RAMASOMANANA

    Introduction course

    MATLAB

    [email protected]

    MATLAB - Introduction course

    Context

    What is MATLAB?

    Objectives

    Introduction to Chapter 1:

    Starting and Quitting MATLAB

    MATLAB Desktop

    Desktop Tools

    MATLAB - Introduction course

    What is MATLAB?

    MATLAB is a commercial software of interactive calculus. It makes it

    possible to carry out numerical simulations based on algorithms of

    numerical analysis. It can thus be used for solving numericalproblems for the approximate resolution of differential equations,

    partial differential equations, linear and nonlinear systems, or

    scientific and engineering graphics, etc and to write programs in a

    lower level language.

    MATLAB provides a block diagram tool for modeling and simulating

    dynamical systems, including signal processing, controls,

    communications, and other complex systems ranging from

    engineering and mathematics to chemistry, biology, and economics.

    MATLAB - Introduction course

    Objectives

    The objective of this MATLAB's course is double:

    the knowledge of this software is essential because it is more

    and more used for calculus in industries and banks,

    the knowledge of algorithms existing in the software and to

    test new algorithms.

  • 8/2/2019 Matlab Course1

    2/11

    MATLAB - Introduction course

    Introduction

    This chapter provides a brief introduction to starting and

    quitting MATLAB, and the tools and functions that help you to

    work with MATLAB variables and files.

    Chapter 1:

    MATLAB - Introduction course

    Starting and Quitting MATLAB

    On a Microsoft Windows platform, to start MATLAB, double-

    click the MATLAB shortcut icon on your Windows desktop.

    Starting MATLAB:

    Quitting MATLAB:

    To end your MATLAB session, select Exit MATLAB from theFile menu in the desktop, or type quit in the Command

    Window.

    MATLAB - Introduction course

    MATLAB Desktop

    When you start MATLAB, the MATLAB desktop appears,containing tools for managing files, variables, and applications

    associated with MATLAB.

    The first time MATLAB starts, the desktop appears as shown

    in the following illustration.

    MATLAB - Introduction course

    MATLAB Desktop

    You can change the

    way your desktop looks

    by opening, closing,

    moving, and resizing

    the tools in it. You can

    also move tools

    outside of the desktop

    or return them back

    inside the desktop.

  • 8/2/2019 Matlab Course1

    3/11

    MATLAB - Introduction course

    Desktop Tools

    This section provides an introduction to MATLABs desktop

    tools. The tools are:

    Command Window

    Command History

    Help Browser

    Current Directory

    Workspace

    Array Editor

    Create M-Files

    MATLAB - Introduction course

    Desktop Tools

    Use the Command

    Window to make

    calculations and run

    functions which exist

    in MATLAB and M-

    files (programs)

    created by us.

    Command Window:

    MATLAB - Introduction course

    Desktop tools

    Lines you enter in theCommand Window

    are logged in the

    Command History

    window. In the

    Command History,

    you can view

    previously used

    functions, and copyand execute selected

    lines.

    Command History:

    MATLAB - Introduction course

    Desktop tools

    Use the Help browser to search and view documentation for all

    MathWorks products. The Help browser is a Web browser

    integrated into the MATLAB desktop that displays HTML

    documents.

    To open the Help browser, click the help button in the toolbar, or

    type helpbrowser in the Command Window.

    Help Browser:

  • 8/2/2019 Matlab Course1

    4/11

    MATLAB - Introduction course

    Desktop tools

    Help Browser:

    MATLAB - Introduction course

    Desktop tools

    Current Directory Browser:

    MATLAB file operations

    use the current directory as

    reference points. Any file

    you want to run must be in

    the current directory.

    To search for, view, open,

    and make changes toMATLAB-related directories

    and files, use the MATLAB

    Current Directory browser.

    MATLAB - Introduction course

    Desktop tools

    Workspace Browser:

    The MATLAB workspaceconsists of the set of variables

    (named arrays) built up during

    a MATLAB session and stored

    in memory. You add variables

    to the workspace by using

    functions, running M-files, and

    loading saved workspaces.

    To view the workspace and

    information about eachvariable, use the Workspace

    browser.

    MATLAB - Introduction course

    Desktop tools

    Workspace Browser:

    To delete variables from the workspace, select the variable and selectDelete from the Edit menu. Alternatively, use the clear all function. The

    workspace is not maintained after you end the MATLAB session.

    To save the workspace to a file that can be read during a later MATLAB

    session, select Save Workspace As from the File menu, or use the

    save function. This saves the workspace to a file called a MAT-file,

    which has a .mat extension.

    To read in a MAT-file, select Import Data from the File menu, or usethe load function.

  • 8/2/2019 Matlab Course1

    5/11

    MATLAB - Introduction course

    Desktop tools

    Workspace Browser (Array Editor):

    Double-click on a

    variable in the

    Workspace browser

    to see it in the Array

    Editor.

    MATLAB - Introduction course

    Desktop tools

    Create M-Files:

    To create M-files, select New M-files from the File menu,

    which allow to write programs and run MATLAB functions.

    We can save the new algorithms in a any directory in a file

    called a M-file, which has a .m extension.

    If you just need to view the contents of an M-file, you can

    display it in the Command Window by using the name of

    the function or running this function.

    MATLAB - Introduction course

    Chapter 2

    Operators fornumbers

    Special functions for numbers

    Operators for vectors

    Special functions for vectors

    Operators for matrices

    Special functions for matrices

    MATLAB - Introduction course

    Expressions for numbers

    + Addition

    - Subtraction

    * Multiplication

    pi 3.1415

    i Imaginary unit

    Inf Infinity

    NaN Not a numberabs Absolute value

    Operators for numbers:

    Special functions:

    sqrt Square root

    log Logarithm

    exp Exponentiel

    sign Signe of the number(=1 if positif and 0 if negatif)

    / Division^ Power

  • 8/2/2019 Matlab Course1

    6/11

    MATLAB - Introduction course

    Special functions

    sin Sine in radians

    sind Sine in degrees

    asin Inverse of the function Sine in radians

    asind Inverse of the function Sine in degrees

    cos Cosine in radians

    cosd Cosine in degrees

    acos Inverse of the function Cosine in radians

    acosd Inverse of the function Cosine in degreestan Tangent in radians

    cot Cotangent in radians

    Trigonometric functions:

    MATLAB - Introduction course

    Complex numbers

    abs the magnitude of the complex number

    angle the phase angle of the complex number

    real the real part of the complex number

    imag the imaginary part of the complex number

    conj the conjugate of the complex number

    special functions:

    MATLAB - Introduction course

    Vectors

    V=[2,3,7] is a row vectorV=[2;3;7] is a column vector

    V change the vector from line to column or from

    column to line.

    Create a vector:

    Operators for vectors:

    + Addition

    - Subtraction

    * Scalar product.* Element by element multiplication

    ./ Element by element division

    MATLAB - Introduction course

    Vectors

    X=1:10 is a row vector containing the integers from 1 to

    10. By default the increment here is equal to 1.

    For a nonunit spacing, we can specify an increment. For

    example:

    X=100:-7:50 or X=0:pi/4:pi

    Colon operator (:)

  • 8/2/2019 Matlab Course1

    7/11

    MATLAB - Introduction course

    Vectors

    Special functionsones(1,3) is a row vector all components is equal to 1.

    ones(3,1) is a column vector all components is equal to 1.

    zeros(1,3) is a row vector all components is equal to 0.

    zeros(3,1) is a column vector all components is equal to 0.

    rand(1,3) is a row vector with random components between

    0 and 1.

    sum(X) computes the sum of the elements of X.

    find(X

  • 8/2/2019 Matlab Course1

    8/11

    MATLAB - Introduction course

    Matrices

    Special functions

    diag(A) computes the diagonal of A.

    det(A) computes the determinant of A.

    inv(A) computes the inverse of A.

    eig(A) computes the eigenvalues of A.

    poly(A) computes the coefficients of the characteristicpolynomial of A.

    MATLAB - Introduction course

    Graphics

    Plot of the function Sine

    x=0:pi/100:2*pi;

    y=sin(x);

    plot(x,y)

    We can label the axes and add a title:

    xlabel(x)ylabel(Sine of x)

    title(Plot of the Sine function)

    MATLAB - Introduction course

    Graphics

    Multiple data sets in one graph

    x=0:pi/100:2*pi;

    y1=sin(x); y2=sin(x-0.25); y3=sin(x-0.5);

    plot(x,y1,x,y2,x,y3)

    The legend command provides an easy way to identify the

    individual plots:

    legend(sin(x),sin(x-0.25),sin(x-0.5))

    MATLAB - Introduction course

    Graphics

    Multiple data sets in one graph

    We can also use this format:figure(1);

    plot(x,y1);

    hold on;

    plot(x,y2);

    hold on;

    plot(x,y3);

    legend(sin(x),sin(x-0.25),sin(x-0.5))hold off;

  • 8/2/2019 Matlab Course1

    9/11

    MATLAB - Introduction course

    Graphics

    Controlling the axes

    By default, MATLAB finds the maxima and the minima of

    the data to choose the axis limits. The axis command

    enables you to specify your own limits:

    axis([xmin xmax ymin ymax])

    Or for three-dimensional graph:

    axis([xmin xmax ymin ymax zmin zmax])

    MATLAB - Introduction course

    Graphics

    Multiple plots in one figure

    The subplot command enables you to display multiple plots

    in the same window or print them on the same piece of

    paper. Typing: subplot(m,n,p)

    partitions the figure window into an m-by-n matrix of small

    subplots and selects the pth subplot for the current plot.

    The plots are numbered along first the top row of the figurewindow, then the second row, and so on.

    MATLAB - Introduction course

    Graphics

    Multiple plots in one figureExample:

    subplot(2,2,1)plot(x,y1);

    hold off;

    subplot(2,2,2)

    plot(x,y2);

    hold off;

    Subplot(2,2,3)

    Plot(x,y3);

    hold off;

    subplot(2,2,4);plot(x,y4);

    hold off;

    MATLAB - Introduction course

    Graphics

    Saving a figure

    To save a figure, select save from the file menu or click on

    the save button of the figure.

    The figure is then saved as a fig file.

  • 8/2/2019 Matlab Course1

    10/11

    MATLAB - Introduction course

    Chapter 3

    Programming with MATLAB

    if statements

    for loops

    while loops

    MATLAB - Introduction course

    Chapter 3

    if

    The if statement evaluates a logical expression and

    executes a group of statements when the expression is

    true. The optional elseif and else keywords provide for the

    execution of alternate groups of statements.

    An end keyword, which matches the if, terminates the last

    group of statements. The groups of statements aredelineated by the four keywords no braces or brackets

    are involved.

    MATLAB - Introduction course

    Chapter 3

    if (example):

    if a < 0

    a = a+1;

    elseif a == 0

    a=0;

    else

    a=a;

    end

    MATLAB - Introduction course

    Chapter 3

    for

    The for loop repeats a group of statements a fixed,predetermined number of times.

    A matching end delineates the loop.

  • 8/2/2019 Matlab Course1

    11/11

    MATLAB - Introduction course

    Chapter 3

    for (example):

    for i = 1:10

    a(i) = i;

    end

    MATLAB - Introduction course

    Chapter 3

    while

    The while loop repeats a group of statements an indefinite

    number of times under control of a logical conditions.

    A matching end delineates the loop.

    MATLAB - Introduction course

    Chapter 3

    while (example):

    while i < 0

    a = i +1;

    end

    MATLAB - Introduction course

    Chapter 3

    The logical operators in the tests:

    Logical operator MATLAB Symboland &

    or |

    equal ==

    not equal ~=

    Less than

    Less or equal to =