General info and matlab

3 MATLAB: second course

1. Sticking matrices together in blocks

(i) Suppose that A (r×m) and B (r×n) are matrices. Then we can combine them into a single (r×(m+n)) matrix by

E=[A,B]

(ii) Likewise, if C (k×m) is another matrix, then we can build a ((k+r)×m) matrix by

E=[A;C]

which looks like

[(AC)].

(iii) Given also a (k×n) matrix D, you should now be able to make

[(sI-AB-CD)].

2. Making vectors

(i) To build up a sequence of points from a to b in steps of length h, use x=[a:h:b]. For instance,

x=[-100:0.01:100]

gives a vector with 20,000 entries starting with -100,100+0.01,,100. To avoid filling the screen with junk, add a semi colon at the end of the command line to suppress printing, as in

x=[-100:0.01:100];

(ii) MATLAB will remember this as x, until such time as you redefine x. If you want to change notation, or start again, then type

clearall

(iii) As in linear algebra

v=2*x

doubles all the entries in the vector; whereas f=2x+1 does not make sense in linear algebra, MATLAB boldly adds one to all the entries when one enters

f=x+1

this is helpful for making new vectors and later for plotting graphs.

(iv) Arithmetic, point by point. One can square all the entries in x by

y=x.2;

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

z=sin(x);

which produces a vector

[sin(-100),sin(-100+0.01),,sin(100)].

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

z=atan(x);

which gives the vector

[tan-1(-100),tan-1(-100+0.01),,tan-1(100)],

in radians.

(vii) Looking for a function? Then try help and the function browser.

3. Matrix functions

(i) Entry by entry. Given a matrix A, and a function such as exp, then

E=exp(A)

gives you the matrix with exp applied to each of the entries. Try

A=[1,2;3,4]
E=exp(A)

(ii) Matrix functions arise when a function is applied to the whole matrix, as in

exp(A)=I+A+A22!+A33!+.

For this one, use the matrix exponential

F=expm(A)

(iii) other matrix functions are the matrix logarithm

L=logm(F)

so that

F=expm(L)

and the matrix square root

S=sqrtm(A)

so that

A=S2.

We have already encountered inv(A), which gives the inverse of A.

(iv) other matrix functions are the row reduced echelon form

rref(A)

and the rank of A, as in

rank(A)

4. Matrix equations

(i) Jordan canonical form JCF of a square matrix is given by

J=jordan(A)

Try this with

A=[(11-14115-24)].

(ii) To find eigenvectors and eigenvalues of B, use

[W,D]=eig(B)

which gives a diagonal matrix D with diagonal entries the eigenvalues of B, and a matrix W of the same shape as A with columns that are eigenvectors of B. This works best when the the eigenvalues are distinct, as in

B=[(11-14115-25)].

(iii) MATLAB’s eig is too enthusiastic and finds bogus eigenvectors for multiple eigenvalues. To witness this nasty habit, look at

[W,D]=eig(A)

with the above choice of A.

(iv) Sylvester’s equation is taken in the form

AP+PB=-C

with A,B and C given and P to be found, as one can do via

P=lyap(A,B,C)

It is a good idea to check that your answer actually works, as in

A*P+P*B+C

since the ± signs are confusing.

(v) Lyapunov’s equation is the special case

AP+PA=-C

where A and C are given, and P is to be found, as in

P=lyap(A,C)

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

x=[-10:0.01:10];
y=sin(x);

Now to plot y on the vertical axis against x on the horizontal axis, write

plot(x,y)

and up pops a window with a graph of y=tan-1x.

(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 104 or 106; 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

comet(x,y)

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

clearfigures

(v) Now introduce another function, say

s=x.2;

and plot

plot(s,y)

so get the graph with points (s,y)=(x2,sinx); note that this is not the graph of a function.

(vi) To have two plots together on the same axes, use

holdon

Then try

y=(sin(x)).2;
plot(s,y+2)

to get (x2,sin(x)) and (x2,sin2x+2) 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

t=[-100:0.01:100];

for the real parameter, and

z=1./(3+i*t);

to give z=1/(3+ıt).

(ii) Then you can plot this via

plot(z)

which is equivalent to

plot(real(z),imag(z))

(iii) Comets. Plots are more amusing if one sees the curve evolve, as in the orbit of a comet, so try

comet(imag(z),real(z))

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

p=[1,4,6,-8]
P=poly2sym(p)

to make this nicer to look at, use

pretty(P)

ii) Also introduce likewise

q=[-2,4,3,-8]
Q=poly2sym(p)
pretty(Q)

and then you can add, multiply, and divide, as in

pretty(P+Q)
pretty(P*Q)
pretty(P/Q)

(iii) Expanding expressions is straightforward

R=expand(P*Q)

(iv) factorizing is the reverse process

factor(R)

(vi) MATLAB can also try to simplify things, as in

simplify(1+P/)

(vii) To find zeros of p, as in the roots of p(s)=0, use

solve(p,0)

8. Euclidean algorithm

(i) For nonzero integers a and b, the Euclidean algorithm finds the greatest common divisor g and integers x and y such that d=ax+by. To do this in MATLAB, try

[g,x,y]=gcd(a,b)

(ii) The Euclidean algorithm can also be carried out for polynomials p(s) and q(s) in a variable s. Let

symss
p=s5+3*(s4)+2*(s2)+72*s+9
q=s5+25*(s4)+21*(s3)-8*s+10

to input the polynomials, then

[g,x,y]=gcd(p,q)

to obtain the highest common factor g(s) and x(s) and y(s) such that

g(s)=p(s)x(s)+q(s)y(s).

9. Calculus

(i) MATLAB can differentiate standard functions with respect to the obvious variable, as in

symsx
diff(atan(x)+sin(x)+1/(x2+1))

(ii) MATLAB can also compute indefinite integrals of

int(atan(x)+sin(x)+1/(x2+1))

but remember that one should have an arbitrary constant of integration, which MATLAB omits.

10. Laplace transforms (i) It is conventional to use t for the time variable in the state space, and s for the transform variable. With this convention

laplace((t2)*(exp(2*t)))

gives the Laplace transform of t2e2t, in variable s.

(ii) This laplace command also works for matrix functions.

(iii) The inverse Laplace transform can be found using

symss
laplace(1/(s2+1))