Exercises
Exercises
Abdulbaki(202170156)_Examples
the book
Table of Contents
Example 2.1 ....................................................................................................................... 2
Example 2.2 ....................................................................................................................... 4
Example 2.3 ....................................................................................................................... 4
Example 2.4 ....................................................................................................................... 5
Example 2.5 ....................................................................................................................... 6
Example 2.6 ..................................................................................................................... 11
Example 2.7 ..................................................................................................................... 13
Example 2.8 ..................................................................................................................... 15
Exapmple 2.9 .................................................................................................................... 16
Example 2.10 .................................................................................................................... 17
Example 2.11 .................................................................................................................... 23
Example 2.12 .................................................................................................................... 26
Example 2.13 .................................................................................................................... 27
Example 2.14 .................................................................................................................... 28
Example 2.15 .................................................................................................................... 33
Example 2.16 .................................................................................................................... 37
Example 2.17 .................................................................................................................... 44
Example 2.18 .................................................................................................................... 46
Example 2.19 .................................................................................................................... 46
Example 2.20 .................................................................................................................... 48
Example 2.21 .................................................................................................................... 49
Example 2.22 .................................................................................................................... 50
Example 2.23 .................................................................................................................... 50
Example 2.24 .................................................................................................................... 52
Example 2.25 .................................................................................................................... 53
Example 2.26 .................................................................................................................... 54
Example 2.27 .................................................................................................................... 55
Example 2.28 .................................................................................................................... 55
Example 2.29 .................................................................................................................... 56
Example 2.30 .................................................................................................................... 57
Example 2.31 .................................................................................................................... 57
Example 2.32 .................................................................................................................... 58
Example 2.33 .................................................................................................................... 59
Example 2.34 .................................................................................................................... 60
Example 2.35 .................................................................................................................... 61
Example 2.36 .................................................................................................................... 62
Example 2.37 .................................................................................................................... 63
Example 2.38 .................................................................................................................... 63
1
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.1
%determine the following: (a) A + B, (b) AB, (c) A2, (d) AT, (e) B–1,
%(f) B(T)A(T), (g) A2 + B2 – AB, (h) determinant of A, determinant of
B and determinant of AB.
A = [1 0 1; 2 3 4; - 1 6 7]
B= [7 4 2 ; 3 5 6 ; - 1 2 1]
A =
1 0 1
2 3 4
-1 6 7
B =
7 4 2
3 5 6
-1 2 1
%solution:
%part(a)
C = A + B
%part(b)
D = A * B
%part(c)
E = A ^ 2
%part(d)
F = A'
%part(e)
H = inv (B)
%part(f)
J = B' * A'
%part(g)
K = A ^ 2 + B ^ 2 - A * B
%part(h)
det (A)
det (B)
det (A * B)
C =
8 4 3
5 8 10
-2 8 8
D =
2
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
6 6 3
19 31 26
4 40 41
E =
0 6 8
4 33 42
4 60 72
F =
1 2 -1
0 3 6
1 4 7
H =
J =
6 19 4
6 31 40
3 26 41
K =
53 52 45
15 51 58
-2 28 42
ans =
12
ans =
-6.3000e+01
ans =
-7.5600e+02
3
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.2
%Determine the eigenvalues and eigenvectors of A using MATLAB
A = [4 2 -3 ; -1 1 3 ; 2 5 7]
A =
4 2 -3
-1 1 3
2 5 7
%solution
eig(A)
lamda = eig(A)
[V, D]=eig(A)
ans =
5.9488e-01
3.0000e+00
8.4051e+00
lamda =
5.9488e-01
3.0000e+00
8.4051e+00
V =
D =
5.9488e-01 0 0
0 3.0000e+00 0
0 0 8.4051e+00
Example 2.3
%Determine the values of x, y, and z for the following set of linear
algebraic equations :
4
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%x2 – 3x3 = – 5
%2x1 + 3x2 – x3 = 7
%4x1 + 5x2 – 2x3 = 10
%solution:
A = [0 1 -3 ; 2 3 -1 ; 4 5 -2]
B = [-5 ; 7 ; 10]
x = inv(A) * B
check = A * x
A =
0 1 -3
2 3 -1
4 5 -2
B =
-5
7
10
x =
-1.0000e+00
4.0000e+00
3.0000e+00
check =
-5.0000e+00
7.0000e+00
1.0000e+01
Example 2.4
%Write a function file Veccrossprod to compute the cross product of
two
%vectors a, and b, where a = (a1, a2, a3), b = (b1, b2, b3), and a × b
= (a2b3 – a3b2, a3b1 – a1b3, a1b2
%–a2b1). Verify the function by taking the cross products of pairs of
unit vectors: (i, j), (j, k), etc.
%solution:
%function file c =Veccrossprod(a,b);
% Veccrossprod : function to compute c = a × b where a and b are 3D
vectors
% call syntax:
5
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
% c = Veccrossprod(a, b) ;
%c = [a(2)*b(3)-a(3)*b(2);a(3)*b(1)-a(1)*b(3);a(1)*b(2)-a(2)*b(1)];
Example 2.5
%(a) Generate an overlay plot for plotting three lines
%y1 = sin t
%y2 = t
%y3 = t-t^3/3!+t^5/5!+t^7/7!
% 0?t?2?
%Use (i) the plot command
%(ii) the hold command
%(iii) the line command
%(b) Use the functions for plotting x-y data for plotting the
following functions.
%(i) f(t) = t cost
%0 ? t ? 10?
%(ii) x = et
%y = 100 + e3t
%0 ? t ? 2?
%solution:
%part(a)
%(a) overlay plot
%(i) using the plot command
t = linspace(0, 2 * pi, 100);
y1 = sin(t); y2 = t;
y3 = t -(t.^ 3)/6 + (t.^ 5)/120 -(t.^ 7)/5040;
plot(t, y1, t, y2, '-', t, y3, 'o')
axis([0 5 -1 5])
xlabel('t')
ylabel('sin(t) approximation')
title('sin(t) functio')
text(3.5, 0, 'sin(t)')
gtext('Linear approximation')
gtext('4-term approximation')
6
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
7
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
8
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
%Using Table 2.29 functions
%(i)
fplot('x.*cos(x)', [0 10*pi])
%This will give the following figure (Fig. E 2.5 (b) (i))
9
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%(ii)
t = linspace(0, 2*pi, 200);
x = exp(t);
y = 100 + exp(3*t);
loglog(x, y), grid
10
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.6
%(a) Plot the parametric space curve of
%x(t) = t
%y(t) = t2
%z(t) = t3 0 ? t ? 2.0
%(b) z = – 7 / (1 + x2 + y2) | x | ? 5 , | y | ? 5
%part(a)
t = linspace(0, 2,100);
x = t ; y = t.^2 ; z = t.^3;
plot3(x, y, z), grid
%The plot is shown in Figure E 2.6 (a).
11
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
t = linspace(0, 2, 100);
x = t ; y = t.^2 ; z = t.^3;
plot3(x, y, z), grid
t = linspace (-5, 5, 50) ;
y = x ;
z = [-7./(1 + x .^ 2 + y .^ 2)];
%mesh(z)
%The plot is shown in Figure E 2.6(b).
12
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.7
%Consider the function
%H(s)=n(s)/d(s)
%where n(s) = s^4 + 6s^3 5s^2 + 4s + 3
%d(s) = s^5 + 7s^4 + 6s^3 + 5s^2 + 4s + 7
%(a) Find n(– 10), n(– 5), n(– 3) and n(– 1)
%(b) Find d(– 10), d(– 5), d(– 3) and d(– 1)
%(c) Find H(– 10), H(– 5), H(– 3) and H(– 1)
%solution:
%part(a)
n = [1 6 5 4 3];
d = [1 7 6 5 4 7];
n2 = polyval(n, [-10])
nn10 = polyval(n, [-10])
nn5 = polyval(n, [-5])
nn3 = polyval(n, [-3])
nn1 = polyval(n, [-1])
n2 =
4463
13
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
nn10 =
4463
nn5 =
-17
nn3 =
-45
nn1 =
-1
%part(b)
dn10 = polyval(d, [-10])
dn5 = polyval(d, [-5])
dn3 = polyval(d, [-3])
dn1=polyval(d, [-1])
dn10 =
-35533
dn5 =
612
dn3 =
202
dn1 =
%part(c)
Hn10 = nn10/dn10
Hn5 = nn5/dn5
Hn3 = nn3/dn3
Hn1 = nn1/dn1
14
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Hn10 =
-1.2560e-01
Hn5 =
-2.7778e-02
Hn3 =
-2.2277e-01
Hn1 =
-1.2500e-01
Example 2.8
%Generate a plot of
%y(x) = e(–0.7x) sin ?x
%where w = 15 rad/s, and 0 ? x ? 15. Use the colon notation to
generate the x vector in increments of 0.1.
%solution:
x = [0 : 0.1 : 15];
w = 15;
y = exp(-0.7*x).*sin(w*x);
plot(x, y)
title('y(x) = e^-^0^.^7^x sin\omega x')
xlabel('x')
ylabel('y')
15
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Exapmple 2.9
%Generate a plot of
%y(x) = e^(–0.6x) cos ?x
%where ? = 10 rad/s, and 0 ? x ? 15. Use the colon notation to
generate the x vector in increments of 0.05.
%solution
x = [0 : 0.1 : 15];
w = 10;
y = exp(-0.6*x).*cos(w*x);
plot(x, y)
title('y(x) = e^-^0^.^6^x cos\omega x')
xlabel('x')
ylabel('y')
16
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.10
%Using the functions for plotting x-y data given in Table 2.29 plot
the following functions.
%(a) r^2 = 5 cos 3t 0 ? t ? 2?
%(b) r^2 = 5 cos 3t 0 ? t ? 2?
%x = r cos t, y = r sin t
%(c) y1= e^(–2x) cos x 0 ? t ? 20
%y2 = e^(2x)
%(d) y=cos(x)/x
%–5 ? x ? 5?
%(e) f = e–3t/5 cos t 0 ? t ? 2?
%(f) z = –1/3x^2 + 2xy + y^2
%|x| ? 7, |y| ? 7
%solution:
%part(a)
t = linspace(0, 2*pi, 200);
r = sqrt(abs(5*cos(3*t)));
polar(t, r)
17
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
t = linspace(0, 2*pi, 200);
r = sqrt(abs(5*cos(3*t)));
x = r.*cos(t);
y = r.*sin(t);
fill(x, y, 'k'),
axis('square')
18
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(c)
x = 1 : 0.1 : 20;
y1 = exp(-2*x).*cos(x);
y2 = exp(2*x);
Ax = plotyy(x, y1, x, y2);
hy1 = get(Ax(1), 'ylabel');
hy2 = get(Ax(2), 'ylabel');
set(hy1, 'string', 'exp(-2x).cos(x)')
set(hy2, 'string', 'exp(-2x)');
19
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(d)
x = linspace(-5*pi,5*pi,100);
y = cos(x)./x;
area(x, y);
xlabel('x (rad)'), ylabel('cos(x)/x')
hold off
20
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(e)
t = linspace(0, 2*pi, 200);
f = exp(-0.6*t).*sin(t);
stem(t, f)
21
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(f)
r = -7 : 0.2 : 7;
[X, Y] = meshgrid(r, r);
Z = -0.333*X.^2 + 2*X.*Y + Y.^2;
cs = contour(X, Y, Z);
%label(cs)
22
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.11
%Use the functions listed in Table 2.30 for plotting 3-D data for the
following.
% (a) z = cos x cos y e^((x2+y2)^(-1/2)/5)
%|x| ? = 7, | y | ? 7
% (b) Discrete data plots with stems
%x = t, y = t cos(t)
%z = e^(t/5) –2 0?t? 5?
%(c) A cylinder generated by
%r = sin(5?z) + 3
%0 ? z ? 1 0 ? ? ? 2?
%solution:
part(a)
u = -7 : 0.2 : 7;
[X, Y] = meshgrid(u, u);
Z = cos (X).*cos (Y).*exp(-sqrt(X.^2 + Y.^2)/5);
surf(X, Y, Z)
23
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
t = linspace(0, 5*pi, 200);
x = t ; y = t.*cos(t);
z = exp(t/5) -2;
stem3(x, y, z, 'filled');
xlabel('t'), ylabel ('t cos(t)'), zlabel ('e^t/5 – 1')
24
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(c)
z =[0 : 0.2 : 1];
r = sin(5*pi*z)+3;
cylinder(r)
25
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.12
%Obtain the plot of the points for 0 ? t ? 6? when the coordinates
x,y,z are given as a function of the parameter t as follows:
%x=root(t)sin(3t)
%y=root(t)cos(3t)
%z=.8t
%solution:
t = [0:0.1:6*pi];
x = sqrt(t).*sin(3*t);
y = sqrt(t).*cos(3*t);
z = 0.8*t;
plot3(x, y, z, 'k', 'linewidth', 1)
grid on
xlabel ('x'); ylabel ('y') ; zlabel ('z')
26
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.13
%Obtain the mesh and surface plots for the function z =2xy^(2)/(x^2 +
y^2) over the
%domain – 2 ? x ? 6 and 2 ? y ? 8.
%solution
x = -2 : 0.1 : 6;
y = 2 : 0.1 : 8;
[x, y] = meshgrid(x, y);
z = 2*x.*y.^2./(x.^2 + y.^2);
mesh(x, y, z)
xlabel('x'); ylabel('y'); zlabel('z')
surf(x, y, z)
xlabel('x'); ylabel('y'); zlabel('z')
27
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.14
%Plot the function z =2^(-1.5root(x^2+y^2))sin(x)cos(0.5y) the domain
– 4 ? x ? 4 and – 4 ? y ? 4 using Table 2.30
%(a) Mesh plot
%(b) Surface plot
%(c) Mesh curtain plot
%(d) Mesh and contour plot
%(e) Surface and contour plot
%solution:
%part(a)
x = -4 : 0.25 : 4;
y = -4 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
mesh(x, y, z)
xlabel('x');
ylabel('y')
zlabel('z')
28
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
x = -4 : 0.25 : 4;
y = -4 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
surf(x, y, z)
xlabel('x'); ylabel ('y')
zlabel('z')
29
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(c)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
meshc(x, y, z)
xlabel('x'); ylabel('y')
zlabel('z')
30
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(d)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid (x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
meshc(x, y, z)
xlabel('x'); ylabel('y')
zlabel('z')
31
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(e)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^ (-1.5*sqrt (x.^2 + y.^2)).*cos (0.5*y).*sin(x);
surfc(x, y, z)
xlabel('x'); ylabel('y')
zlabel('z')
32
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.15
%Plot the function z=2^(-1.5root(x^2+y^2)) sin(x)cos(0.5y) over the
domain – 4 ? x ? 4 and – 4 ? y ? 4 using Table 2.30.
%a) Surface plot with lighting
%(b) Waterfall plot
%(c) 3-D contour plot
%(d) 2-D contour plot
%solution:
%part(a)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
surfl(x, y, z)
xlabel('x'); ylabel('y')
zlabel('z')
33
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
waterfall(x, y, z)
xlabel('x'); ylabel('y')
zlabel('z')
34
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(c)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
contour3(x, y, z, 15)
xlabel('x') ; ylabel('y')
zlabel('z')
35
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(d)
x = -4.0 : 0.25 : 4;
y = -4.0 : 0.25 : 4;
[x, y] = meshgrid(x, y);
z = 2.0.^(-1.5*sqrt(x.^2 + y.^2)).*cos(0.5*y).*sin(x);
contour(x, y, z, 15)
xlabel('x'); ylabel('y')
zlabel('z')
36
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.16
%Using the functions given in Table 2.29 for plotting x-y data, plot
the following functions:
%(a) f(t) = t cos t 0 ? t ? 10?
%(b) x = e^(–2t), y = t 0 ? t ? 2?
%(c) x = t, y = e^(2t) 0 ? t ? 2?
%(d) x = e^(t), y = 50 + e(t) 0 ? t ? 2?
%(e) r^2 = 3 sin 7t
%y = r sin t 0 ? t ? 2?
%(f) r^2 = 3 sin 4t
%y = r sin t 0 ? t ? 2?
%(g) y = t sin t 0 ? t ? 5?
%solution:
%part(a)
fplot('x.*cos(x)', [0, 10*pi])
37
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
t = linspace(0, 2 * pi, 200);
x = exp(-2 * t); y = t;
semilogx (x, y),grid
38
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(c)
t= linspace(0, 2 * pi, 200);
semilogy(t, exp(-2 * t)), grid
39
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(d)
t = linspace(0, 2 * pi, 200);
x = exp(t);
y = 50 + exp(t);
loglog(x, y), grid
40
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(e)
t = linspace(0, 2*pi, 200);
r = sqrt(abs(3*sin(7*t)));
y = r.*sin(t);
stairs(t, y)
axis([0 pi 0 inf]);
41
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(f)
t = linspace(0, 2*pi,200);
r = sqrt(abs(3*sin(4*t)));
y = r.*sin(t);
bar(t, y)
axis([0 pi 0 inf]);
42
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(g)
q = linspace(0, 5*pi, 200);
y = q.*sin(q);
comet(q, y)
43
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.17
%Consider the two matrices:
%A=[3 2*pi ; 5j 10 + sqrt(2)*j];
%B=B = [7j –15j ; 2*pi 18];
%Determine the following:
%(a) A + B
%(b) AB
%(c) A2
%(d) A^T
%(e) B^–1
%(f) B^T A^T
%(g) A^2 + B^2 – AB
A=[3 2*pi ; 5j 10 + sqrt(2)*j];
B= [7j -15j ; 2*pi 18];
%solution:
%part(a)
A + B
ans =
44
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
A * B
ans =
%part(c)
A ^ 2
ans =
%part(d)
inv(A)
ans =
%part(e)
B^(-1)
ans =
%part(f)
inv(B)*inv(A)
ans =
%part(g)
(A ^ 2 + B ^ 2) -(A * B)
ans =
45
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.18
%Find the inverse of the following matrices:
%A = [3 2 0; 2 –1 7; 5 4 9];
%B = [– 4 2 5; 7 – 1 6; 2 3 7];
%C = [– 1 2 – 5; 4 3 7; 7 – 6 1];
A = [3 2 0; 2 -1 7; 5 4 9];
B = [-4 2 5; 7 -1 6; 2 3 7];
C = [-1 2 -5; 4 3 7; 7 -6 1];
%solution:
A=inv(A)
B=inv(B)
C=inv(C)
A =
B =
C =
Example 2.19
%Determine the eigenvalues and eigenvectors of matrix A
%(a) A = [4 – 1 5 ; 2 1 3 ; 6 – 7 9]
%(b) A =[3 5 7;2 4 8;5 6 10]
%solution:
%part(a)
A = [4 -1 5 ; 2 1 3 ; 6 -7 9]
format short e
46
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
eig(A)
[Q, d] = eig(A)
A =
4 -1 5
2 1 3
6 -7 9
ans =
1.0000e+01
5.8579e-01
3.4142e+00
Q =
d =
1.0000e+01 0 0
0 5.8579e-01 0
0 0 3.4142e+00
%part(b)
A =[3 5 7;2 4 8;5 6 10]
format short e
eig(A)
[Q, d] = eig(A)
A =
3 5 7
2 4 8
5 6 10
ans =
1.7686e+01 + 0.0000e+00i
-3.4295e-01 + 1.0066e+00i
-3.4295e-01 - 1.0066e+00i
Q =
47
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Columns 1 through 2
Column 3
-2.0715e-01 + 5.2772e-01i
7.1769e-01 + 0.0000e+00i
-3.3783e-01 - 2.2223e-01i
d =
Columns 1 through 2
Column 3
0.0000e+00 + 0.0000e+00i
0.0000e+00 + 0.0000e+00i
-3.4295e-01 - 1.0066e+00i
Example 2.20
%Determine the eigenvalues and eigenvectors of A and B
%A = [ 3 0 2 1; 1 2 5 4; 7 – 1 2 6; 1 – 2 3 4 ];
%B = [ 1 3 5 7; 2 – 1 – 2 4; 3 2 1 1; 4 1 0 6 ];
%solution:
A = [ 3 0 2 1; 1 2 5 4; 7 -1 2 6; 1 -2 3 4 ];
B = [ 1 3 5 7; 2 -1 -2 4; 3 2 1 1; 4 1 0 6 ];
a = A * B
eig (a)
[Q, d] = eig (a)
a =
13 14 17 29
36 15 6 44
35 32 39 83
22 15 12 26
ans =
9.8546e+01
2.2964e+00
48
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
-1.3095e+00
-6.5329e+00
Q =
d =
9.8546e+01 0 0 0
0 2.2964e+00 0 0
0 0 -1.3095e+00 0
0 0 0 -6.5329e+00
Example 2.21
%Solve the following set of equations
%(a) x1 + 2x2 + 3x3 + 5x4 = 21
%–2x1 + 5x2 + 7x3 – 9x4 = 18
%5x1 + 7x2 + 2x3 – 5x4 = 25
%–x1 + 3x2 – 7x3 + 7x4 = 30
%(b) x1 + 2x2 + 3x3 + 4x4 = 8
%2x1 – 2x2 – x3 – x4 = – 3
%x1 – 3x2 + 4x3 – 4x4 = 8
%2x1 + 2x2 – 3x3 + 4x4 = – 2
%solution:
%part(a)
A = [1 2 3 5 ; -2 5 7 -9 ; 5 7 2 -5 ; -1 -3 -7 7];
B = [21 ; 18 ; 25 ; 30] ;
S = A\B
S =
-8.9896e+00
1.4128e+01
-5.4438e+00
3.6128e+00
%part(b)
A = [1 2 3 4 ; 2 -2 -1 1 ; 1 -3 4 -4 ; 2 2 -3 4];
B = [8 ; -3 ; 8 ; -2];
S = A\B
49
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
S =
2.0000e+00
2.0000e+00
2.0000e+00
-1.0000e+00
Example 2.22
%Use diff command for symbolic differentiation of the following
functions:
%(a) S1 = ex^8
%(b) S2 = 3x^3 ex^5
%(c) S3 = 5x^3 – 7x^2 + 3x + 6
%solution:
%part(a)
syms x
S1 = exp(x^8);
diff (S1)
ans =
8*x^7*exp(x^8)
%part(b)
S2 = 3* x ^3*exp(x^5);
diff (S2)
ans =
9*x^2*exp(x^5) + 15*x^7*exp(x^5)
%part(c)
S3 = 5*x^3 -7*x^2 + 3*x + 6;
diff (S3)
ans =
15*x^2 - 14*x + 3
Example 2.23
%symbolic commands to find the values of the following integrals.
%(a) int (S1, 0.2, 0.7)
%(b) int (S2, 0, pi)
50
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%solution:
%part(a)
syms x y a b
S1 = abs(x)
int (S1, 0.2, 0.7)
S1 =
abs(x)
ans =
9/40
%part(b)
S2 = cos (y) + 7*y^2
int (S2, 0, pi)
S2 =
cos(y) + 7*y^2
ans =
(7*pi^3)/3
%part(c)
S3 = sqrt (x)
int (S3)
S3 =
x^(1/2)
ans =
(2*x^(3/2))/3
%part(d)
S4 = 7*x^5 -6*x^4 + 11*x^3 + 4*x^2 + 8 * x -9
int (S4)
51
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
S4 =
ans =
%part(e)
S5 = cos (a)
int (S5)
S5 =
cos(a)
ans =
sin(a)
Example 2.24
%Obtain the general solution of the following first order differential
equations:
%(a)dy/dt=5t -6y
%(b)ddy/ddt+ 3dy/dt+ y = 0
%(c)ds/dt= Ax^3
%(d)ds/dA= Ax^3
%solution:
%part(a)
dsolve('Dy=5*t - 6*y')
ans =
%part(b)
dsolve ('D2y + 3*Dy + y = 0')
52
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
ans =
%part(c)
dsolve ('Ds = A*x^3', 'x')
ans =
(A*x^4)/4 + C1
%part(d)
dsolve ('Ds = A*x^3', 'A')
ans =
(A^2*x^3)/2 + C1
Example 2.25
%Determine the solution of the following differential equations that
satisfies the given initial conditions.
%(a) dy/dx= – 7x^2 y(1) = 0.7
%(b) dy/dx= 5x cos^2(y) y(0) = ?/4
%(c) dy/dx=-y + e^(3x) y(0) = 2
%(d) dy/dt+ 5y = 35 y(0) = 4
%solution
%part(a)
dsolve ('Dy = -7*x^2', 'y (1) = 0.7')
ans =
%part(b)
53
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
ans =
atan(5*t*x + 1)
%part(c)
dsolve ('Dy = -y + exp (3*x)', 'y (0) = 2')
ans =
exp(3*x) - exp(-t)*(exp(3*x) - 2)
%part(d)
dsolve ('Dy + 5*y = 35', 'y (0) = 4')
ans =
7 - 3*exp(-5*t)
Example 2.26
%Given the differential equation
%dd(x)/ddt+ 7dx/dt + 5x = 8 u(t) t ? 0
%find
%(a) x(t) when all the initial conditions are zero
%(b) x(t) when x (0) = 1 and x(0) = 2.
%parT(a)
x = dsolve ('D2x = -7*Dx -5*x + 8', 'x(0) = 0')
x =
54
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(b)
x = dsolve ('D2x = -7*Dx -5*x + 8', 'x (0) = 1', 'Dx (0) = 2')
x =
Example 2.27
%Given the differential equation
%ddx/ddt+ 12dx/dt+ 15x = 35t ? 0
%(a) x(t) when all the initial conditions are zero
%(b) x(t) when x (0) = 0 and x(0) = 1.
%solution:
%part(a)
x = dsolve ('D2x = -12*Dx -15*x +35', 'x (0) = 0')
x =
%part(b)
x = dsolve ('D2x = -12*Dx -15*x + 35', 'x (0) = 0', 'Dx (0) = 1')
x =
Example 2.28
%Find the inverse of the following matrix
%A = [s 2 0 ; 2 s – 3 ; 3 0 1] ;
%solution:
syms s;
55
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
A = [s 2 0 ; 2 s -3 ; 3 0 1] ;
inv (A)
ans =
Example 2.29
%Expand the following function F(s) into partial fractions using
MATLAB.
%Determine the inverse Laplace transform of F(s).
%F(s) =1/(s +5s^3 +7s^2)
%The MATLAB program for determining the partial-fraction expansion is
given below:
%solution:
b = [0 0 0 0 1];
a = [1 5 7 0 0];
[r, p, k] = residue (b, a)
syms s
f = 1/(s^4 + 5*s^3 + 7*s^2);
ilaplace (f)
r =
5.1020e-02 - 6.4805e-02i
5.1020e-02 + 6.4805e-02i
-1.0204e-01 + 0.0000e+00i
1.4286e-01 + 0.0000e+00i
p =
-2.5000e+00 + 8.6603e-01i
-2.5000e+00 - 8.6603e-01i
0.0000e+00 + 0.0000e+00i
0.0000e+00 + 0.0000e+00i
k =
[]
ans =
t/7 + (5*exp(-(5*t)/2)*(cos((3^(1/2)*t)/2) +
(11*3^(1/2)*sin((3^(1/2)*t)/2))/15))/49 - 5/49
56
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
Example 2.30
%Expand the following function F(s) into partial fractions using
MATLAB.
%Determine the inverse Laplace transform of F(s).
%F(s) = (5s^2 + 3s + 6)/(s^4 +3s^3 +7s^2 +12)
%solution:
b = [0 0 5 3 6];
a = [1 3 7 9 12];
[r, p, k] = residue(b, a)
r =
-5.3571e-01 - 1.0394e+00i
-5.3571e-01 + 1.0394e+00i
5.3571e-01 - 1.8558e-01i
5.3571e-01 + 1.8558e-01i
p =
-1.5000e+00 + 1.3229e+00i
-1.5000e+00 - 1.3229e+00i
8.3267e-17 + 1.7321e+00i
8.3267e-17 - 1.7321e+00i
k =
[]
syms s
f = (5*s^2 + 3*s +6)/(s^4 + 3*s^3 + 7*s^2 + 9*s +12);
ilaplace(f)
ans =
Example 2.31
%the following function F(s):
%F(s) =(s^4 + 3s^3 + 5s^2 + 7s + 25)/(s^4 + 5s^3 + 20s^2 + 40s + 45)
%Using MATLAB, find the partial-fraction expansion of F(s). Also, find
the inverse Laplace
57
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%transformation of F(s).
%solution:
num = [ 1 3 5 7 25];
den = [1 5 20 40 45];
[r, p, k] = residue(num, den)
r =
-1.3849e+00 + 1.2313e+00i
-1.3849e+00 - 1.2313e+00i
3.8485e-01 - 4.7018e-01i
3.8485e-01 + 4.7018e-01i
p =
-8.5543e-01 + 3.0054e+00i
-8.5543e-01 - 3.0054e+00i
-1.6446e+00 + 1.3799e+00i
-1.6446e+00 - 1.3799e+00i
k =
Example 2.32
%Obtain the partial-fraction expansion of the following function using
%MATLAB:
%F(s) =8(s +1)(s + 3)/((s +2)(s + 4)(s + 6)^2)
%solution:
num = conv([8 8], [1 3]);
den = conv([1 6 8], [1 12 36]);
[r, p, k] = residue(num, den)
r =
3.2500e+00
1.5000e+01
-3.0000e+00
-2.5000e-01
p =
-6.0000e+00
-6.0000e+00
-4.0000e+00
58
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
-2.0000e+00
k =
[]
Example 2.33
%Find the Laplace transform of the following function using MATLAB.
%(a) f(t) = 7t3 cos (5t + 60°)
%(b) f(t) = – 7te– 5t
%(c) f(t) = – 3 cos 5t
%(d) f(t) = t sin 7t
%(e) f(t) = 5 e–2t cos 5t
%(f) f(t) = 3 sin(5t + 45?)
%(g) f(t) = 5 e–3t cos(t – 45?)
%solution:
%part(a)
syms t
f = 7 * t^3*cos(5*t + (pi/3));
laplace(f)
ans =
%part(b)
syms t x
f = -7*t*exp(-5*t);
laplace(f, x)
ans =
-7/(x + 5)^2
%part(c)
syms t x
f = -3*cos(5*t);
laplace(f, x)
ans =
-(3*x)/(x^2 + 25)
59
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
%part(d)
syms t x
f = t*sin(7*t);
laplace(f, x)
ans =
(14*x)/(x^2 + 49)^2
%part(e)
syms t x
f = 5*exp(-2*t)*cos(5*t);
laplace(f, x)
ans =
%part(f)
syms t x
f = 3*sin(5*t + (pi/4));
laplace(f, x)
ans =
%part(g)
syms t x
f = 5*exp(-3*t)*cos(t -(pi/4));
laplace(f, x)
ans =
Example 2.34
%Generate partial-fraction expansion of the following function
%F(s) =(10^5(s + 7)(s + 13))/(s(s + 25)(s + 55)(s^2 + 7s 75)(s^2 + 7s
+ 45))
%solution
numg=poly([-7 -13]);
numg = poly([-7 -13]);
deng = poly([0 -25 -55 roots([1 7 75])' roots([1 7 45])']);
60
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
r =
3.3553e-22
-1.4004e-20
2.5402e-19
-1.8707e-18
1.6209e-18
-1.3691e-21
-3.5745e-36
1.0794e-20
p =
4.6406e+06
1.4250e+06
3.0295e+05
3.3610e+04
2.6640e+03
9.4000e+01
1.0000e+00
0
k =
[]
Example 2.35
%Determine the inverse Laplace transform of the following functions
using
%MATLAB.
%(a) F(s) =s/s(s + 2)(s + 6)
%(b) F(s) = 1/s^2(s+5)
%(c) F(s) = (3s + 1)/(s^2+ 2s + 9)
%(d) F(s) = (s-25)/s(s^2 + 3s + 20)
%solution:
%part(a)
syms s
f = s/(s*((s + 2)*(s + 6)));
ilaplace(f)
61
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
ans =
exp(-2*t)/4 - exp(-6*t)/4
%part(b)
syms s
f = 1/((s^2)*(s + 5));
ilaplace(f)
ans =
%part(c)
syms s
f = (3*s + 1)/(s^2 + 2*s + 9);
ilaplace(f)
ans =
3*exp(-t)*(cos(2*2^(1/2)*t) - (2^(1/2)*sin(2*2^(1/2)*t))/6)
%part(d)
syms s
f = (s -25)/(s*(s^2 + 3*s + 25));
ilaplace(f)
ans =
exp(-(3*t)/2)*(cos((91^(1/2)*t)/2) +
(5*91^(1/2)*sin((91^(1/2)*t)/2))/91) - 1
Example 2.36
%Find the inverse Laplace transform of the following function
usingMATLAB.
%G(s) =(s^2 + 9s + 7)(s + 7)/(s + 2)(s^2 + 3)(s + 12s + 150).
%solution:
syms s
G = (s^2 + 9*s +7)*(s + 7)/[(s + 2)*(s + 3)*(s^2 + 12*s + 150)];
pretty(G)
g = ilaplace(G);
pretty(g)
2
(s + 7) (s + 9 s + 7)
---------------------------------
62
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
2
(s + 2) (s + 3) (s + 12 s + 150)
exp(-3 t) 44 exp(-2 t) 7
------------ - -----------
123 26
Example 2.37
%Generate the transfer function using MATLAB.
%G(s) = 3(s + 9)(s + 21)(s + 57)/s(s + 30)(s^2 + 5s + 35)(s^2 + 28s +
42)
%(a) the ratio of factors
%(b) the ratio of polynomials
%solution:
%Gzpk = zpk([-9 -21 -57] , [0 -30 roots([1 5 35]) roots([1 28 42])],3)
%Gp = tf(Gzpk)
Example 2.38
%Generate the transfer function using MATLAB.
%G(s) = (s^4 +20s^3+27s^2 +17s+35)/(s^5 + 8s^4+ 9s^3 + 20s^2 + 29s +
32).
%(a) the ratio of factors
%(b) the ratio of polynomials
%solution:
Gtf = tf([1 20 27 17 35] , [1 8 9 20 29 32])
Gzpk = zpk(Gtf)
Gtf =
Gzpk =
63
LAB 3_ Ayman Tawfiq Abdulbak-
i(202170156)_Examples the book
------------------------------------------------------
(s+7.057) (s^2 + 1.969s + 1.71) (s^2 - 1.025s + 2.652)
64