Matlab Basics

programming_language


Fundamental

  1. Plotting

    • 2-D
      Assume that Y is the function of X.
      plot(X, Y, 'o')
      To add text to the graph, specify "title", "legend", "xlabel", "ylabel".
      "hold on" and "hold off" to control the same figure.

    • 3-D
      Assume that Z is the function of X and Y.
      surf(X, Y, Z)
      Dimensionality reduction to 2-D.

      1. figure
      2. [c,h] = contour(X,Y,Z);
      3. clabel(c,h);
    • subgraph
      subplot(n, m, i)
      Piece the whole figure into n rows & m columns. i refers to the i-th sub-figure.

  2. Loop

    1. for k = 1:m
    2. sth...
    3. end
  3. Condiction

    1. if condiction_1
    2. do_sth
    3. elseif condiction_2
    4. do_sth
    5. else
    6. do_sth
    7. end
  4. Get help
    doc + command shows the document of the command.
    help + command shows the instruction of the command.

  5. Matrix Manipulation

    • create
      a = [1 2 3; 4,5,6]
      A comma or space between numbers specifies columns.
      The semicolon specifies rows.
      "ones", "zeros" and "rand" can generate matrix with a pair of parameters (row, col).
      "magic(x)" generates a x*x matrix.
    • operations
      a' transpose
      inv(a) invertion
      Arithmatic operations with . before mean "element-wise".
    • concatenate
      [a, a] extends horizontally.
      [a; a] extends vertically.
    • indexing
      Matlab index from ONE, not zero.
      To access an element in a matrix, a tuple (row, col) is needed.
      The usage of the colon  start:end or start:step:end
      A single colon means "ALL".

    • workspace
      whos examines the info of all variables in the workspace.
      save + file_name.mat saves the workspace.
      load + file_name.mat loads it.

Optimization

  1. Minimizing Functions of One Variable
    fminbnd(@fun_name, lower, upper, opt)
    A chosen opt means setting like optimset('Display','iter').
    Or options = optimset('PlotFcns',@optimplotfval) to plot the iteration.
    Standard return: [optimal_x, optimal_f, exit_flag, output_massage]

  2. Minimizing Functions of Several Variables
    fminsearch(@fun_name, init_values)
    It it better to define the input variable of a function as a compressed one and decompose it inside the function for convenience of initialization.

  3. Finding the zero point
    fzero(@fun_name, [low high], opt)

  4. Unconstraint minimization
    fminunc(@f, init_value)

  5. Min-Max Optimization
    fminimax(@f, init_value, A, b, Aeq, beq, lb, ub, nonlcon, opt)


    f is a vector-value function. Min-Max optimization returns the maximum value among the function values with the minimum x.
    return [x, fval, maxfval, exitflag, output]

  6. Constraint Optimization
    fmincon(@f, init, A, b, Aeq, beq, lb, ub, nonlcon, opt)

  7. Nonlinear Least Square Optimization
    lsqnonlin(@f, init, lb, ub, opt)
    return [x, f^2, f, flag, output]

  8. Linear Programming
    linprog(@f, A, b, Aeq, beq, lb, ub, init, opt)

  9. Integer Programming
    intlinprog(@f, intcon, A, b, Aeq, beq, lb, ub, opt)
    intcon is the indices of integers in matrix A.

  10. Quadratic Programming
    quadprog(H, f, A, b, Aeq, beq, lb, ubm init, opt)

Interplotation

One other interesting characteristic is that the interpolating function passes through the data points. This is an important distinction between interpolation and curve/surface fitting. In fitting, the function does not necessarily pass through the sample data points.

  1. Cubic Spline Interplotation.
    Xq = (start:step:end)
    Vq = interp1(X, Y, Xq, 'spline')
    So the Vq=F(Xq) specifies a interplotation function.

  2. 2-D Cubic Spline Interplotation.
    set up a 2-D mesh grid
    [Xq, Yq] = meshgrid(start: step: end);
    Vq = interp2(X, Y, Z, Xq, Yq, 'spline')

  3. Interpolating Scattered Data
    Zq = griddata(X, Y, Z, Xq, Yq);

Symbolic computation

Basic

  1. Define symbolic variables
    syms x y z
    Symbolic variables can be used to define expressions, like x+y and 4*z^2-1.
    Define a function of x using sym f(x).
    f(x) = x^2 + x + 1
    Matrices, of course.

  2. Solve an equation.
    solve(x^4 == 1)
    Solve with assumptions. assume(x, x>0)
    The second assumption. assumeAlso(x, 'real').
    Clear all assumptions on x. assume(x, 'clear')

  3. Substitution
    subs(x^2, y) substitute all x with y.
    subs(x+y, [x y], [1 2])

  4. Simplify an expression
    simplify(x^2+x*2+1)
    Similarily, combine(2*cos(x)*sin(x), 'sincos')

  5. Factorization
    factor(x^3-y^3)

  6. Expand an expression.
    expand((x - y)^3)

  7. Functional composition
    f(x) = and g(x)=.
    compose(f, g, x)

Calculus

  1. differentiation
    diff(f(x), x)

  2. integration
    int(f(x), x)
    int(f(x), x, 0, 1) x from 0 to 1

  3. Taylor Expansion
    taylor(f(x))

  4. Limitation
    limit(f(x), x, c, 'left') the left limitation of f(x) at x=c

Ordinary differential equations


dsolve(diff(y, x) == -a*y)
with initial condition
dsolve(diff(y, x) == -a*y, y(0)==c)