09-11-2012, 05:20 PM
Basic MATLAB Programming
MATLAB.pdf (Size: 129.67 KB / Downloads: 162)
MATLAB is a matrix-based language. Since operations may be
performed on each entry of a matrix, “for” loops can often be bypassed
by using this option. As a consequence, MATLAB programs are often
much shorter and easier to read than programs written for instance in C
or Fortran. Below, we mention basic MATLAB commands, which will
allow a novice to start using this software. The reader is encouraged
to use the Help Graphic User Interface (GUI) for further information
Plotting the graph of a function of one variable
The command plot(x,f) plots f as a function of x. The figure
can be edited by hand to add labels, change the thickness of the line
of the plot, add markers, change the axes etc. All of these attributes
can also be specified as part of the plot command (type help plot or
search for “plot” in the Help GUI for more information).
Basic vector and matrix operations
An n-dimensional vector u in MATLAB is a 1 × n matrix, whose
entries can be accessed by typing u(j) where j is between 1 and n. For
instance, if you want to define a vector v whose entries are u(10), u(11),
..., u(20), type v = u(10:20); (recall that a semicolon at the end of a
MATLAB command indicates that the output of that command should
not be displayed on the command window).
The transpose uT (in MATLAB, type u’) of u is a column vector.
To calculate the scalar product of u with uT , type u * u’. If you type
u’ * u, you obtain an n×n matrix since you are multiplying an n×1
matrix by a 1 × n matrix.
We have already mentioned how to raise each entry of u to a given
power. You can in fact apply any function to each entry of u. For
instance, exp(u) will return a vector whose entries are obtained by
applying the exponential function to each entry of u.