Matlab Tutorial

Bohe Wang
Computer Science and Electrial Engineering Department,
West Virginia University, Morgantown WV 26506

 

Matlab(MATrix LABoratory) is a tool to do numerical computations, display information graphically in 2D and 3D, and solve many other problems in engineering and science. The best way to learn what Matlab can do is to work through some examples at the computer. In this tutorial, we give getting start and some examples.

Getting started

Here is a sample session with Matlab. Text after ">>" and "%" is what you type. You should read this example, then imitate it at the computer.
% matlab5

>> 2+3

ans =

     5

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

a =

     1     2     3
     4     5     6
     7     8     9

>> a*a

ans =

    30    36    42
    66    81    96
   102   126   150

>> exit

 55 flops.

%


In this example we start Matlab by typing matlab5 (in WVU ISIS super computer). In your computer, the start command may be different one, like matlab. Then you do what you want. Finally use exit or quit to leave Matlab.


Matrices Equation

Many problems in engineering and science result in matrics equations. It is convenient to solve matrics equation using Matlab.
    To solve the following equations:
         x + 2y = 5
        2x + 6y = 14

>> a=[1 2; 2 6]

a =

     1     2
     2     6

>> b=[5; 14]

b =

     5
    14

>> x=a\b

x =

    1.0000
    2.0000

>> exit


2D Graph

Draw a cirle

>> t=0:pi/20:2*pi;
>> plot(sin(t), cos(t))
>> axis square

Here is the result:

Draw a vibration example

>> x=linspace(1,5);         
>> y=1-exp(-x).*sin(2*pi*x);
>> plot(x,y)                
        

Here is the result:

Draw many functions togather

>> t=0:pi/50:2*pi;
>> y1=sin(t);
>> y2=cos(t);
>> y3=sin(t-0.25);
>> y4=cos(t-0.25);
>> plot(t,y1,t,y2,t,y3,t,y4)
        

Here is the result:

Draw a chart

>> x=[.19 .22 .41 .18];
>> pie(x)

>> explode = zeros(size(x));
>> h = pie(x, explode);

>> textObjs = findobj(h, 'Type', 'text');
>> oldStr = get(textObjs, {'String'});
>> val = get(textObjs, {'Extent'});
>> oldExt = cat(1, val{:});

>> Names = {'P1: '; 'P2: '; 'P3: '; 'P4: '};
>> newStr = strcat(Names, oldStr);

>> set (textObjs, {'String'}, newStr)
       

Here is the result:


3D Graph

3D picture 1

>> t=0:pi/50:15*pi;
>> plot3(sin(t), cos(t), t);
>> text(0,0,0, 'Origin');
>> grid;

Here is the result:

3D picture 2

>> [x,y] = meshgrid(-2:.1:2, -2:.1:2);
>> z = x .* exp(-x.^2 - y.^2);
>> mesh(z)

Here is the result:

3D picture 3

>> g = 0:0.2:10;
>> [x,y] = meshgrid(g);
>> z = 2*sin(sqrt(x.^2 + y.^2));
>> mesh(z);

Here is the result:


Help


To get general help message, type "help". To get help message of a special
topic, type "help special_topic".

>> help

HELP topics:

matlab/general       -  General purpose commands.
matlab/ops           -  Operators and special characters.
matlab/lang          -  Programming language constructs.
matlab/elmat         -  Elementary matrices and matrix manipulation.
matlab/elfun         -  Elementary math functions.
matlab/specfun       -  Specialized math functions.
matlab/matfun        -  Matrix functions - numerical linear algebra.
matlab/datafun       -  Data analysis and Fourier transforms.
matlab/polyfun       -  Interpolation and polynomials.
matlab/funfun        -  Function functions and ODE solvers.
matlab/sparfun       -  Sparse matrices.
matlab/graph2d       -  Two dimensional graphs.
matlab/graph3d       -  Three dimensional graphs.
matlab/specgraph     -  Specialized graphs.
matlab/graphics      -  Handle Graphics.
matlab/uitools       -  Graphical user interface tools.
matlab/strfun        -  Character strings.
matlab/iofun         -  File input/output.
matlab/timefun       -  Time and dates.
matlab/datatypes     -  Data types and structures.
matlab/demos         -  Examples and demonstrations.
fuzzy/fuzzy          -  Fuzzy Logic Toolbox.
fuzzy/fuzdemos       -  Fuzzy Logic Toolbox Demos.
toolbox/ident        -  System Identification Toolbox.
images/images        -  Image Processing Toolbox.
images/imdemos       -  Image Processing Toolbox --- demos and sample images
toolbox/local        -  Preferences.
toolbox/signal       -  Signal Processing Toolbox.
toolbox/tour         -  MATLAB Tour
wavelet/wavelet      -  Wavelet Toolbox.
wavelet/wavedemo     -  Wavelet Toolbox Demos.

For more help on directory/topic, type "help topic".


>> help matfun

  Matrix functions - numerical linear algebra.
 
  Matrix analysis.
    norm        - Matrix or vector norm.
    normest     - Estimate the matrix 2-norm.
    rank        - Matrix rank.
    det         - Determinant.
    trace       - Sum of diagonal elements.
    null        - Null space.
    orth        - Orthogonalization.
    rref        - Reduced row echelon form.
    subspace    - Angle between two subspaces.
 
  Linear equations.
    \ and /     - Linear equation solution; use "help slash".
    inv         - Matrix inverse.
    cond        - Condition number with respect to inversion.
    condest     - 1-norm condition number estimate.
    chol        - Cholesky factorization.
    cholinc     - Incomplete Cholesky factorization.
    lu          - LU factorization.
    luinc       - Incomplete LU factorization.
    qr          - Orthogonal-triangular decomposition.
    nnls        - Non-negative least-squares.
    pinv        - Pseudoinverse.
    lscov       - Least squares with known covariance.
 
  Eigenvalues and singular values.
    eig         - Eigenvalues and eigenvectors.
    svd         - Singular value decomposition.
    gsvd        - Generalized ingular value decomposition.
    eigs        - A few eigenvalues.
    svds        - A few singular values.
    poly        - Characteristic polynomial.
    polyeig     - Polynomial eigenvalue problem.
    condeig     - Condition number with respect to eigenvalues.
    hess        - Hessenberg form.
    qz          - QZ factorization for generalized eigenvalues.
    schur       - Schur decomposition.
 
  Matrix functions.
    expm        - Matrix exponential.
    logm        - Matrix logarithm.
    sqrtm       - Matrix square root.
    funm        - Evaluate general matrix function.
 
  Factorization utilities
    qrdelete    - Delete column from QR factorization.
    qrinsert    - Insert column in QR factorization.
    rsf2csf     - Real block diagonal form to complex diagonal form.
    cdf2rdf     - Complex diagonal form to real block diagonal form.
    balance     - Diagonal scaling to improve eigenvalue accuracy.
    planerot    - Given's plane rotation.
    cholupdate  - rank 1 update to Cholesky factorization.
    qrupdate    - rank 1 update to QR factorization.