1. Sticking matrices together in blocks
(i) Suppose that and are matrices. Then we can combine them into a single matrix by
(ii) Likewise, if is another matrix, then we can build a matrix by
which looks like
(iii) Given also a matrix , you should now be able to make
2. Making vectors
(i) To build up a sequence of points from to in steps of length , use . For instance,
gives a vector with 20,000 entries starting with . To avoid filling the screen with junk, add a semi colon at the end of the command line to suppress printing, as in
(ii) MATLAB will remember this as , until such time as you redefine . If you want to change notation, or start again, then type
(iii) As in linear algebra
doubles all the entries in the vector; whereas does not make sense in linear algebra, MATLAB boldly adds one to all the entries when one enters
this is helpful for making new vectors and later for plotting graphs.
(iv) Arithmetic, point by point. One can square all the entries in by
where the indicates that the entries are squared one at a time.
(v) Functions, point by point. MATLAB can carry out function operations on vectors and matrices, as in
which produces a vector
MATLAB carries all the standard trigonometric and hyperbolic functions, and several higher transcendental functions.
(vi) Inverse functions, point by point. MATLAB can also apply inverse functions, as in the inverse tangent function
which gives the vector
in radians.
(vii) Looking for a function? Then try help and the function browser.
3. Matrix functions
(i) Entry by entry. Given a matrix , and a function such as , then
gives you the matrix with applied to each of the entries. Try
(ii) Matrix functions arise when a function is applied to the whole matrix, as in
For this one, use the matrix exponential
(iii) other matrix functions are the matrix logarithm
so that
and the matrix square root
so that
We have already encountered , which gives the inverse of .
(iv) other matrix functions are the row reduced echelon form
and the rank of , as in
4. Matrix equations
(i) Jordan canonical form of a square matrix is given by
Try this with
(ii) To find eigenvectors and eigenvalues of , use
which gives a diagonal matrix with diagonal entries the eigenvalues of , and a matrix of the same shape as with columns that are eigenvectors of . This works best when the the eigenvalues are distinct, as in
(iii) MATLAB’s is too enthusiastic and finds bogus eigenvectors for multiple eigenvalues. To witness this nasty habit, look at
with the above choice of .
(iv) Sylvester’s equation is taken in the form
with and given and to be found, as one can do via
It is a good idea to check that your answer actually works, as in
since the signs are confusing.
(v) Lyapunov’s equation is the special case
where and are given, and is to be found, as in
MATLAB can solve this for some rather large matrices, under favourable circumstances. However, implementing this command via AppsEverywhere, MATLAB needs to carry out complicated discussions over the wireless network which evidently take some time.
5. Plots
(i) We return to
Now to plot on the vertical axis against on the horizontal axis, write
and up pops a window with a graph of
(ii) Smooth curves. MATLAB plots graphs by plotting many points and joining them up with straight line segments. To produce smooth graphs, use a large number of points, say or ; modern computers can easily handle this.
(iii) Comet orbits. If you wish to witness MATLAB plotting all the points and joining the dots, then use
which is always mildly amusing, and can be purposeful when the curve represents an evolving process like the solution of a differential equation.
(iv) To remove a plot, click on the red crossed box on top right of the plot, then in the command window type
(v) Now introduce another function, say
and plot
so get the graph with points ; note that this is not the graph of a function.
(vi) To have two plots together on the same axes, use
Then try
to get and together.
(vii) Titles and legends can be added to the graph via the insert tab on the plot; this is helpful for specifying what you have actually plotted.
(viii) To down load the graph, use the export option, and aim to export the finished product as a pdf file for ease of handling.
6. Complex plots
(i) MATLAB can plot curves in the complex plane, so long as one is careful with the syntax. Recall that a curve can be expressed as a complex function of a real parameter, as in
for the real parameter, and
to give .
(ii) Then you can plot this via
which is equivalent to
(iii) Comets. Plots are more amusing if one sees the curve evolve, as in the orbit of a comet, so try
7. Polynomial arithmetic
(i) The fastest way to input a polynomial is to input the coefficients as a vector, and then convert using a command to introduce the symbols, as in
to make this nicer to look at, use
ii) Also introduce likewise
and then you can add, multiply, and divide, as in
(iii) Expanding expressions is straightforward
(iv) factorizing is the reverse process
(vi) MATLAB can also try to simplify things, as in
(vii) To find zeros of , as in the roots of , use
8. Euclidean algorithm
(i) For nonzero integers and , the Euclidean algorithm finds the greatest common divisor and integers and such that . To do this in MATLAB, try
(ii) The Euclidean algorithm can also be carried out for polynomials and in a variable . Let
to input the polynomials, then
to obtain the highest common factor and and such that
9. Calculus
(i) MATLAB can differentiate standard functions with respect to the obvious variable, as in
(ii) MATLAB can also compute indefinite integrals of
but remember that one should have an arbitrary constant of integration, which MATLAB omits.
10. Laplace transforms (i) It is conventional to use for the time variable in the state space, and for the transform variable. With this convention
gives the Laplace transform of , in variable .
(ii) This laplace command also works for matrix functions.
(iii) The inverse Laplace transform can be found using