Introduction To MATLAB (By DR Wang Fei)
Introduction To MATLAB (By DR Wang Fei)
Introduction To MATLAB (By DR Wang Fei)
1. I NSTALLATION
The National University of Singapore has a Total Academic Headcount licence for MATLAB.
Students may use it for academic, research, and learning. The license allows students to install
MATLAB on personally-owned computers.
1. If you are NOT using NUS network, you are required to use nVPN:
◦ https://webvpn.nus.edu.sg/
Please sign in with your NUSNET ID in the format: nusstu\nusnetid and password.
If you are using NUS network, please proceed to Step 2.
2. B ASIC O PERATIONS
3. B ASIC F UNCTIONS
The function sqrt(x) computes the principle square root of the number x. For example,
>> sqrt(2)
INTRODUCTION TO MATLAB 3
ans = 1.4142
By default, MATLAB displays four decimal digits to its answers. But we can change the for-
mat for numeric display. (The percent symbol % is used for indicating a comment line.)
>> format long % 16 decimal digits
>> sqrt(2)
ans = 1.414213562373095
>> format rat % rational approximation
>> sqrt(2)
ans = 1393/985
>> format short % four decimal digits (default)
>> sqrt(2)
ans = 1.4142
We can also use the following functions in MATLAB:
(i) Trigonometric functions: sin(x) , cos(x) , tan(x) , cot(x) , sec(x) , csc(x) .
(ii) Inverse trigonometric functions: asin(x) , acos(x) , atan(x) , acot(x) .
(iii) Exponential and logarithmic functions: exp(x) , log(x) (base e), log10(x) (base 10).
For trigonometric and inverse trigonometric functions, the angles are measured in radian (π
radian = 180◦ ), and the constant π = 3.1415926 · · · is defined by pi ( MATLAB is case-sensitive).
>> sin(pi/4)
ans = 0.7071
>> atan(1)
ans = 0.7854
Note that = is used to assign a value to a variable; and == shall be used for equality.
x 4 − 7x 3 + 3x 2 − 5x + 9 = 0.
4.2. Multi-Variable Equations. MATLAB can also solve multi-variable equations, for which
the equations shall be put together in square brackets [ ] , so do the variables. Moreover, since
the output consists multiple variables, we shall assign the solution as a vector. For example,
3x + y = 10, x + y = 20.
>> syms x y;
>> [Sx,Sy] = solve([3*x+y==10, x+y==20], [x,y])
Sx = -5
Sy = 25
The solution of the a general linear system in variables x and y
(
ax + b y = e
cx + d y = f
can be obtained as follows:
>> syms a b c d e f x y;
>> [Sx,Sy] = [Sx, Sy] = solve([a*x+b*y == e, c*x+d*y == f], [x,y])
Sx = -(b*f - d*e)/(a*d - b*c)
Sy = (a*f - c*e)/(a*d - b*c)
5. S UMS
If we need to find the sum of a sequence, use the command symsum . The format is
>> symsum(expression, variable, [min, max])
For example, for the arithmetic sequence {a n } given by a n = a + (n − 1)d , the sum of its first
n terms is (2a + (n − 1)d )n/2.
>> syms a d k n;
>> symsum(a+(k-1)*d, k, [1, n])
ans = a*n - d*(n - (n*(n + 1))/2)
>> simplify(ans) % simplify the previous answer
ans = (n*(2*a - d + d*n))/2
For the geometric sequence {a n } given by a n = ar n−1 , the sum of its first n terms is
(
a(r n − 1)/(r − 1) if r 6= 1,
Sn =
an if r = 1.
INTRODUCTION TO MATLAB 6
>> syms a r k n;
>> symsum(a*r(k-1), k, [1, n])
ans = piecewise(r == 1, a*n, r ∼= 1, (a*(rn - 1))/(r - 1))
Moreover, we can find the sum to infinity, which is defined by inf in MATLAB. Recall that
n−1
for the geometric sequence {a n } given by a n = ar , the sum to infinity is
(
a/(1 − r ) if |r | < 1,
S∞ =
does not exist if |r | ≥ 1.
>> syms a r k;
>> assume(abs(r)<1);
>> symsum(a*r(k-1), k, [1, inf])
ans = -a/(r - 1)
6. V ECTOR
7. F UNCTION
7.1. Standard Function. To define a function, we shall (i) give the name of the function, (ii) use
@ to declare the name of the variable, (iii) provide the expression of the function. For example,
define f (x) = x 2 :
>> f = @(x) x2;
Then f (2) can be evaluated by
>> f(2);
ans = 4
Another example: g (x) = sin(x 3 )/x 2
>> g = @(x) sin(x3)/x2;
Multi-variable functions can be defined similarly by declaring more variables. For example,
p
h(x, y) = x 2 + y 2 :
>> h = @(x,y) sqrt(x2 + y2);
To evaluate h(5, 12):
>> h(5,12)
ans = 13
7.2. Piecewise Function. The absolute value function f (x) = |x| is a piecewise function:
(
x if x ≥ 0,
f (x) =
−x if x < 0,
In the example above, the first condition is x>=0 and this means “x is greater than or equal
to 0”. The value x is the expression of the function when the condition x>=0 is satisfied. It
defines f (x) = x when x ≥ 0. The second condition x<0 and the second value -x defines
f (x) = −x when x < 0.
In order to evaluate the value of the function at given point, for example, f (−2), we use
>> subs(f, x, -2)
ans = 2
8. C URVE P LOTTING
8.1. Standard Function. Once a function is defined, fplot can be used to plot its graph over
a specified interval in the form [xmin, xmin] .
For example, we plot f (x) = x 2 on the interval (−100, 100):
>> f = @(x) x2;
>> fplot(f, [-100,100]);
The command can be used even if the function is undefined somewhere on the specific in-
terval. For example, g (x) = 1/x on (−1, 1) is defined at x = 0.
>> g = @(x) 1/x;
>> fplot(g, [-1,1]);
INTRODUCTION TO MATLAB 9
8.2. Multiple Curves. In order to plot more curves in the same coordinate system, we use the
commands on and off . For example,
(We can use Shift + Enter to start a new line without breaking the command.)
>> f = @(x) sin(x);
>> g = @(x) cos(x);
>> fplot(f, [-2*pi, 2*pi]);
>> hold on
>> fplot(g, [-2*pi, 2*pi]);
>> hold off
We can draw multiple graphs on the same plot. MATLAB provides some basic colour options:
white w , black b , blue b , red r , cyan c , green g , magenta m , yellow y to distinguish
INTRODUCTION TO MATLAB 10
f (x) = x 3 − x + 1,
g (x) = x 4 − 3x 2 + x,
8.3. Parametric Equations. A curve may be defined using a pair of parametric equations:
In order to use fplot , we shall first define the functions for the x- and y-coordinates.
Recall that a unit circle can be parameterized by
8.4. Piecewise Function. There are two ways to plot a piecewise function.
Once a piecewise function is already defined, fplot can directly plot its graph:
(
ex if x < 0,
f (x) =
cos x if x ≥ 0.
>> syms x;
>> f = piecewise(x<0, exp(x), x>=0, cos(x));
>> fplot(f, [-3,3]);
INTRODUCTION TO MATLAB 12
Alternatively, we can plot different branches on different intervals using the same colour.
>> f1 = @(x) exp(x);
>> f2 = @(x) cos(x);
>> fplot(f1, [-3,0], 'b');
>> hold on
>> fplot(f2, [0,3], 'b');
>> hold off;
8.5. Implicit Function. The command fimplicit can be used to plot graphs defined by im-
plicit functions, i.e., f (x, y) = 0. The format is:
>> fimplicit(function in two variables, [xmin, xmax, ymin, ymax])
For example, to plot x 3 + y 3 = 3x y, we shall first define the function f (x, y) = x 3 + y 3 − 3x y:
>> f = @(x,y) x3 + y3 - 3*x*y;
>> fimplicit(f, [-1.5, 2, -1.5, 2]);
INTRODUCTION TO MATLAB 13
9. L IMITS
The limit of a function describes the behavior of a function near a point or at infinity. The
limit lim f (x) can be easily evaluated with the command limit :
x→a
>> limit(f(x), x, a)
x
For example, lim p :
x→0 1 + 3x − 1
>> syms x;
>> limit(x/(sqrt(1+3*x)-1), x, 0)
ans = 2/3
The one-sided limits can be evaluated by specifying the direction left or right . For ex-
ample, let the floor function bxc denote the greatest integer smaller or equal to x. Then
lim bxc = 1, lim bxcx = 2, and lim bxc does not exist.
x→2− x→2+ x→2
>> syms x;
>> limit(floor(x), x, 2, 'left')
ans = 1
>> limit(floor(x), x, 2, 'right')
ans = 2
>> limit(floor(x), x, 2)
ans = limit(floor(x), x, 2)
³ a ´x
We can find the limit at infinity inf or negative infinity -inf . For example, lim 1 + :
x→∞ x
>> syms a x;
>> limit((1+a/x)x, x, inf)
INTRODUCTION TO MATLAB 14
ans = exp(a)
Sometimes the limit depends on the value of the parameters. For example,
0 if a < 0,
lim 2ax = 1 if a = 0,
x→∞
∞ if a > 0.
10. D ERIVATIVE
11. I NTEGRAL
11.1. Indefinite Integral. For a function f (x), its indefinite integral is a function F (x) such that
F 0 (x) = f (x), denoted by
Z
F (x) = f (x) d x.
to represent the entire family of indefinite integrals of f (x), where C is an arbitrary constant.
In MATLAB, we can use the command int to find an indefinite integral.
1 1 x
Z
For example, 2 2
d x = tan−1 x + 2
+C .
(1 + x ) 2 2(x + 1)
>> syms x;
>> f = @(x) 1/(1+x2)2;
>> int(f, x)
ans = atan(x)/2 + x/(2*(x2 + 1))
INTRODUCTION TO MATLAB 17
>> Dy = diff(y,x);
>> dsolve(diff(y,x,x) + 8*diff(y,x) - 9*y == 0, [y(1) == 2, Dy(1) == 0])
ans = (exp(-9*x)*exp(9))/5 + (9*exp(-1)*exp(x))/5
>> simplify(ans)
ans = (9*exp(x - 1))/5 + exp(9 - 9*x)/5
12.3. System of Differential Equations. The command dsolve can also be used for a system
of differential equations. Note that the output consists multiple functions. So the output must
be in vector form:
>> [var 1, ..., var N] = dsolve([eqn1, ..., eqn M], [cond 1, ..., cond k])
For example, the following linear system consists of two functions x(t ) and y(t ) with two
equations and two initial conditions:
(
x 0 (t ) = 3x + 4y
where x(0) = 2 and y(0) = 3.
y 0 (t ) = −4x + 3y
Plotting a space curve in the x y z-space is similar to plotting a parametrized curve in the x y-
plane. The only difference is to replace fplot by fplot3 . For example,
13.2. Level Curves. A surface is defined as a two-variable function z = f (x, y). Its level curve at
z = c is the intersection of the surface with the horizontal plane at z = c; that is, f (x, y) = c.
The command fcontour plots the level curve of a two-variable function.
>> fcontour(function, [xmin, xmax, ymin, ymax])
For example, f (x, y) = sin x + cos y:
>> f = @(x,y) sin(x) + cos(y);
>> fcontour(f, [-10,10, -10,10])
It is also possible plot the level curves of two surfaces using hold on and hold off .
13.3. Surface. A surface z = f (x, y) in two variables can be plotted in x y z-space using fsurf .
For example, f (x, y) = sin x + cos y.
>> f = @(x,y) sin(x) + cos(y);
>> fsurf(f, [-10,10, -10,10])
INTRODUCTION TO MATLAB 21
13.5. Implicit Function. A surface can also be defined by an implicit function, i.e., f (x, y, z) = 0.
It is almost the same as plotting a curve defined by implicit function in the x y-plane. Simply
use fimplicit3 instead of fimplicit .
For example, plot the sphere x 2 + y 2 + z 2 = 4z and the paraboloid z = x 2 + y 2 .
>> f = @(x,y,z) x2 + y2 + z2 - 4*z;
>> g = @(x,y,z) x2 + y2 - z;
>> fimplicit3(f, [-3,3, -3,3, -1,5]);
>> hold on
>> fimplicit3(g, [-3,3, -3,3, -1,5]);
>> hold off
14.1. Partial Derivatives. For a multi-variable function, we can find its partial derivatives by
specifying the variables to be differentiated with respect to properly.
∂f ∂f ∂f
For example, for f (x, y, z) = x ln(x y 2 z 3 ), its partial derivatives , and are given by
∂x ∂y ∂z
>> syms x y z;
>> f = @(x,y,z) x*log(x*y2*z3);
>> diff(f, x)
ans = log(x*y2*z3)+1
>> diff(f, y)
ans = (2*x)/y
>> diff(f, z)
INTRODUCTION TO MATLAB 23
ans = (3*x)/z
The command gradient provides the gradient vector ∇ f of a function f so that we can
obtain all the derivatives at once (the output is a column vector).
>> gradient(f, [x,y,z])
ans = log(x*y2*z3)+1
ans = (2*x)/y
ans = (3*x)/z
∂2 f ∂2 f
We may verify the mixed derivative theorem: = :
∂y ∂x ∂x ∂y
>> diff(f, x, y)
ans = 2/y
>> diff(f, y, x)
ans = 2/y
Recall that the directional derivative of f (x, y, z) along a vector v at (a, b, c) is given by
∇ f (a, b, c) • v /|v |.
a ≤ x ≤ b, α(x) ≤ y ≤ β(x).
>> norm(diff(f,t))
ans = (abs(cos(t))2 + abs(sin(t))2 + 1)(1/2)
>> int(ans, [0, 2*pi])
ans = 2*pi*2(1/2)
In general, the line integral of a function f along C is
Z Z b
f ds = f (x(t ), y(t ), z(t )) |r 0 (t )| d t .
C a
Since there is no simple way to evaluate f (r (t )) directly, we may need to define all the compo-
nents of r as functions.
For example, r = (cos t )i + (sin t )j + t k, 0 ≤ t ≤ 2π, and f (x, y, z) = x 2 + y z.
>> x = @(t) cos(t);
>> y = @(t) sin(t);
>> z = @(t) t;
>> r = @(t) [x(t) y(t) z(t)]; % define the curve
>> f = @(x,y,z) x2 + y*z;
>> syms t;
>> dot(f(x(t),y(t),z(t)), norm(diff(r,t)));
>> int(ans, t, [0, 2*pi])
ans = -pi*2(1/2)
15.3. Line Integral of a Vector Field. A vector field F is a vector valued function in multi-
variables:
F (x, y, z) = P (x, y, z)i +Q(x, y, z)j + R(x, y, z)k.
Its line integral along the curve C defined by r (t ) = x(t )i + y(t )j + z(t )k, a ≤ t ≤ b, is
Z Z b
F • dr = F (x(t ), y(t ), z(t )) • r 0 (t ) d t .
C a
16. S ERIES
16.1. Taylor Series. The Taylor series of a function f (x) at x = a is of the form
∞ f (n) (a)
(x − a)n .
X
f (x) =
n=0 n!
In particular, if a = 0, it is called the Maclaurin series of f (x):
∞ f (n) (0)
xn.
X
f (x) =
n=0 n!
MATLAB has the command taylor to produce the Taylor series of a function up to certain
order:
>> taylor(function, variable, point, 'Order', number)
x2 + 3
For example, find the Taylor series of f (x) = at x = 3 of order < 6:
x +5
>> taylor((x2+3)/(x+5), x, 3, 'Order', 6)
ans = (9*x)/16 + (7*(x - 3)2)/128 - (7*(x - 3)3)/1024 + (7*(x - 3)4)/8192
- (7*(x - 3)5)/65536 - 3/16
16.2. Fourier Series. The fourier series of a periodic function f (x) with periodic 2L is given by
a0 X∞ nπx X ∞ nπx
f (x) = + a n cos + b n sin ,
2 n=1 L n=1 L
where
2 L πnx 2 L πnx
Z Z
an = f (x) cos dx and b n = f (x) sin d x.
L −L L L −L L
There is no simple command in MATLAB to produce the coefficients for fourier series. But
we can define by ourself.
(
0 if − 2 ≤ x < 0,
For example, let f (x) = and f (x) = f (x + 4) for all x; so L = 2.
x if 0 ≤ x < 2,
>> syms x
>> f = piecewise(-2<=x<0, 0, 0<=x<2, x);
>> L = 2;
>> a = @(n) 2/L*int(f*cos(pi*n*x/L), x, [-L, L]);
>> b = @(n) 2/L*int(f*sin(pi*n*x/L), x, [-L, L]);
INTRODUCTION TO MATLAB 27
17.1. Basic Operations. The entries of a matrix shall be entered row by row, while the entries in
each row are separated by a space and the rows are separated by a semi-colon ; . For example,
à !
1 2 3
A= .
4 5 6
>> A = [1 2 3; 4 5 6]
A = 1 2 3
A = 4 5 6
The matrix addition, subtraction and multiplication with scalar can be evaluated using + ,
- and * respectively. For example,
à ! à !
1 2 4 1
A= and B = .
3 4 2 5
>> A = [1 2; 3 4];
>> B = [4 1; 2 5];
>> A + B
ans = 5 3
ans = 5 9
>> A - B
ans = -3 1
ans = 1 -1
>> 3*A
ans = 3 6
ans = 9 12 Ã ! Ã !
4 1 1 2
We illustrate more operations using the previously defined A = and B = .
3 4 2 5
INTRODUCTION TO MATLAB 28
(i) Transpose AT :
>> A'
ans = 1 3
ans = 2 4
(ii) Rank rank(A):
>> rank(A)
ans = 2
(iii) Determinant det(A), provided that A is a square matrix.
>> det(A)
ans = -2
(iv) Powers An , provided that A is a square matrix. If n < 0, A needs to be invertible (that is,
non-singular).
>> A2
ans = 7 10
ans = 15 22
(v) If A is invertible, its inverse can be evaluated using either A(-1) or inv(A) .
>> inv(A)
ans = -2.0000 1.0000
ans = 1.5000 -0.5000
(vi) The adjoint adj(A), provided that A is a square matrix.
>> adjoint(A)
ans = 4.0000 -2.0000
ans = -3.0000 1.0000
(vii) Matrix product AB , provided that the sizes are matched.
>> A * B
ans = 8 11
ans = 20 23
Moreover, we can generate special matrices using the following command:
(i) Zero matrix 0m×n of size m × n: zeros(m,n) .
>> zeros(2,3)
ans = 0 0 0
INTRODUCTION TO MATLAB 29
ans = 0 0 0
(ii) Identity matrix In of order n: eye(n) .
>> eye(2)
ans = 1 0
ans = 0 1
(iii) Diagonal matrix with diagonal entries a 1 , . . . , a n : diag([a1 ... an]) .
>> diag(2,3)
ans = 2 0
ans = 0 3
>> A = [1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8];
(i) The size of A is given by
>> size(A)
ans = 4 5
(ii) The (i , j ) entry of A is simply A(i,j) . For example,
>> A(2,5)
ans = 6
(iii) The i th row of A: A(i,:) . For example,
>> A(4,:)
ans = 4 5 6 7 8
If we need more rows, indicate the indices in square brackets. For example,
>> A([2,4], :)
ans = 2 3 4 5 6
ans = 4 5 6 7 8
(iv) The i th column of A is given by A(:,i) .
>> A(:,3)
ans = 3
INTRODUCTION TO MATLAB 30
ans = 4
ans = 5
ans = 6
(v) If we need more columns, indicate the indices in square brackets.
>> A(:, [3,4])
ans = 3 4
ans = 4 5
ans = 5 6
ans = 6 7
(vi) Submatrix of A. Simply indicate the required rows and columns in square brackets.
>> A([1,2], [3,4])
ans = 3 4
ans = 4 5
We can perform the elementary row operations as follows:
(i) Multiplying the i th row by a constant c: A(i,:) = c*A(i,:) .
>> A(1,:) = -2*A(1,:);
A = -2 -4 -6 -8 -10
A = 2 3 4 5 6
A = 3 4 5 6 7
A = 4 5 6 7 8
(ii) Interchanging the i th and j th rows: A([i,j],:) = A([j,i],:) .
>> A([2,3],:) = A([3,2],:);
A = -2 -4 -6 -8 -10
A = 3 4 5 6 7
A = 2 3 4 5 6
A = 4 5 6 7 8
(iii) Adding c times of the i th row to the j th row: A(j,:) = A(j,:) + c*A(i,:) .
>> A(4,:) = A(4,:) + 2*A(1,:)
A = -2 -4 -6 -8 -10
A = 3 4 5 6 7
A = 2 3 4 5 6
INTRODUCTION TO MATLAB 31
A = 40 -3 -6 -9 -12
(iv) The reduced row echelon form of A is given by
>> rref(A)
ans = 1 0 -1 -2 -3
ans = 0 1 2 3 4
ans = 0 0 0 0 0
ans = 0 0 0 0 0
Similarly, we can perform the elementary column operations as follows:
(i) Multiplying the i th column by a constant c: A(:,i) = c*A(:,i) .
(ii) Interchanging the i th and j th columns: A(:,[i,j]) = A(:,[j,i]) .
(iii) Adding c times of the i th column to the j th column: A(:,j) = A(:,j) + c*A(:,i) .
18.1. Homogeneous Linear System. Let A be a matrix. The solution set of the homogeneous
linear system Ax = 0 can be obtained by back-substitution from its reduced row echelon form.
0 3 −6 6 4
For example, let A =
3 −7 8 −5 8.
3 −9 12 −9 6
>> A = [0 3 -6 6 4; 3 -7 8 -5 8; 3 -9 12 -9 6];
>> rref(A)
ans = 1 0 -2 3 0
ans = 0 1 -2 2 0
ans = 0 0 0 0 1
Alternatively, we note that the solution set of Ax = 0 is precisely the nullspace of A. The
command null(A) finds a basis for the nullspace of A (using column vectors).
>> null(A)
ans = -0.7952 0.1467
ans = -0.3797 0.5633
ans = 0.2256, 0.6983
ans = 0.4155 0.4166
ans = -0.0000 -0.0000
INTRODUCTION TO MATLAB 32
Let v1 = (−0.7952, 0.3797, 0.2256, 0.4155, −0.0000)T and v2 = (0.1467, 0.5633, 0.6983, 0.4166, −0.0000)T .
Then Ax = 0 has a general solution x = c 1 v1 + c 2 v2 .
18.2. Non-Homogeneous Linear System. Let A be an m×n matrix and b an m×1 vector. Recall
that the linear system Ax = b can be solved as follows:
18.3. Least Squares Solution. Sometimes the system Ax = b is inconsistent, we prefer to get
the least squares solution, i.e., the solution to AT Ax = AT b. A least squares solution can also
be obtained by linsolve(A,b) or A\b .
INTRODUCTION TO MATLAB 33
4 0 2
For example, A = 0 2 and b = 0
.
1 1 11
One checks that the system is inconsistent. But we have least squares solution x = (1, 2)T .
>> A = [4 0; 0 2; 1 1];
>> b = [2; 0; 11];
>> linsolve(A,b)
ans = 1.0000
ans = 2.0000
19.1. Orthonormal Basis. Let V be a vector space spanned by vectors u1 , . . . , uk . Using Gram-
Schmidt process, one obtains an orthonormal basis for V . In MATLAB, orth can be used to
generate orthonormal basis for V .
More precisely, orth(A) gives an orthonormal basis for the column space of the matrix A.
For example, let V be the vector space spanned by
v1 = (−10, 2, −6, 16, 2), v2 = (13, 1, 3, −16, 1), v3 = (7, −5, 13, −2, −5), v4 = (11, 3, −3, 5, −7).
We shall first define a matrix A whose columns are v1 , v2 , v3 , v4 . Since it is easier to input row
vectors in MATLAB, we may view each vi as a row vector and take transpose:
T −10 13 7 11
v
1
2 1 −5 3
v
2
A= = −6 3 13 −3 .
v3
16 −16 −2 5
v4
2 1 −5 −7
19.2. Eigenvalues. Let A be a square matrix of order n. Its characteristic polynomial is det(x In −
A). Alternatively, we can use the command charpoly .
4 −1 6
For example, let A =
2 1 6 .
2 −1 8
>> A = [4 -1 6; 2 1 6; 2 -1 8];
We shall first declare the variable of the polynomial, e.g., x:
>> syms x;
Then either of the following commands evaluates the characteristic polynomial of A:
>> det(x*eye(3)-A)
ans = x3 - 13*x2 + 40*x - 36
>> charpoly(A,x)
ans = x3 - 13*x2 + 40*x - 36
The roots of the characteristic equation of A are precisely all the eigenvalues of A:
>> solve(charpoly(A,x) == 0, x)
ans = 2
ans = 2
ans = 9
Alternatively, MATLAB can evaluate the eigenvalues using eig .
>> eig(A)
ans = 9.0000
ans = 2.0000
ans = 2.0000
The command [matrix1, matrix2] = eig(A) can return the corresponding eigenvec-
tors. Here matrix2 is a diagonal matrix whose diagonal entries are the eigenvalues of A
(counting multiplicities) and matrix1 is a matrix whose columns are the corresponding (unit)
eigenvectors of A.
>> [P, D] = eig(A)
P = -0.5774 -0.6122 0.3205
P = -0.5774 -0.7873 -0.9112
P = -0.5774 0.0728 -0.2587
D = 9.0000 0 0
INTRODUCTION TO MATLAB 35
D = 0 2.0000 0
D = 0 0 2.0000
Note that A is diagonalizable if and only if matrix2 is invertible.
In this problem, one verifies that P is invertible and P −1 AP = D :
>> inv(P)*A*P
ans = 9.0000 -0.0000 -0.0000
ans = 0.0000 2.0000 0
ans = 0.0000 -0.0000 2.0000
19.3. Differential Equation in Matrix Form. A linear differential equation may be written in
matrix form. For example,
dx
= x + 2y + 1,
dt
dy
= −x + y + t .
dt
It can be expressed in matrix form:
à ! à !à ! à !
x0 3 −4 x 1
= + .
y0 4 −7 y t
à ! à ! à !
x 1 2 1
Let x = ,A= and b = . Then the differential equation is simply x0 = Ax + b.
y −1 1 t
The command dsolve can also be used for differential equation in matrix form:
>> syms x(t) y(t);
>> X = [x; y];
>> A = [3 -4; 4 -7];
>> b = [1; t];
>> [Sx, Sy] = dsolve(diff(X,t) == A*X + b)
Sx = (exp(-5*t)*(C1 + (2*exp(5*t)*(10*t - 7))/75))/2 + 2*exp(t)*(C2 + (exp(-t)*(t
- 1))/3)
Sy = exp(-5*t)*(C1 + (2*exp(5*t)*(10*t - 7))/75) + exp(t)*(C2 + (exp(-t)*(t
- 1))/3)