If X is a matrix, [i, j] = find(X) operates similarly and returns the row- row-column indices of nonzero elements
2.22 THE LAPLACE TRANSFORMS
MATLAB can be used to obtain the partial-fraction expansion of the ratio of two polyno-mials, B(s)/A(s) as follows:
( )
where a(1) ≠ 0 and num and den are row vectors. The coefficients of the numerator and denomi-nator of B(s)/A(s) are specified by the num and den vectors.
Hence num = [b(1) b(2) … b(n)]
den = [a(1) a(2) … a(n)]
The MATLAB command
r, p, k = residue(num, den)
is used to determine the residues, poles, and direct terms of a partial-fraction expansion of the ratio of two polynomials B(s) and A(s) is then given by
( )
The MATLAB command [num, den] = residue(r, p, k) where r, p, k are the output from MATLAB converts the partial fraction expansion back to the polynomial ratio B(s)/A(s).
The command printsys (num,den, ‘s’) prints the num/den in terms of the ratio of poly-nomials in s.
The command ilaplace will find the inverse Laplace transform of a Laplace function.
Finding Zeros and Poles of B(s)/A(s)
The MATLAB command [z, p, k] = tf2zp(num,den) is used to find the zeros, poles, and gain K of B(s)/A(s).
If the zeros, poles, and gain K are given, the following MATLAB command can be used to find the original num/den:
[num, den] = zp2tf (z, p, k) Example 2.7. Consider the function
H(s) = ( ) ( ) n s d s where n(s) = s4 + 6s3 + 5s2 + 4s + 3
d(s) = s5 + 7s4 + 6s3 + 5s2 + 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:
(a) >> n = [1 6 5 4 3]; % n = s ^ 4 + 6s ^ 3 + 5s ^ 2 + 4s + 3
>> d = [1 7 6 5 4 7]; % d = s ^ 5 + 7s ^ 4 + 6s ^ 3 + 5s ^ 2 + 4s + 7
>> n2 = polyval(n, [– 10]) n2 = 4463
>> nn10 = polyval(n, [– 10]) nn10 = 4463
>> nn5 = polyval(n, [– 5]) nn5 = – 17
>> nn3 = polyval(n, [– 3]) nn3 = – 45
>> nn1 = polyval(n, [– 1]) nn1 = – 1
(b) >> dn10 = polyval(d, [– 10]) dn10 = – 35533
>> dn5 = polyval(d, [– 5]) dn5 = 612
>> dn3 = polyval(d, [– 3]) dn3 = 202
>> dn1=polyval(d, [– 1]) dn1 = 8
(c) >> Hn10 = nn10/dn10 Hn10 = – 0.1256
>> Hn5 = nn5/dn5 Hn5 = – 0.0278
>> Hn3 = nn3/dn3 Hn3 = – 0.2228
>> Hn1 = nn1/dn1 Hn1 = – 0.1250
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’)
1 0.8 0.6 0.4 0.2 0 – 0.2 – 0.4 – 0.6 – 0.8
0 5 10 15
y(x) = e– 0.7x sin xω
x Fig. E 2.8.
Example 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’)
1 0.8 0.6 0.4 0.2 0 – 0.2 – 0.4 – 0.6 – 0.8
0 5 10 15
y(x) = e– 0.6x sin xω
x Fig. E 2.9.
Example 2.10. Using the functions for plotting x-y data given in Table 2.29 plot the following functions.
(a) r2 = 5 cos 3t 0 ≤ t ≤ 2π (b) r2 = 5 cos 3t 0 ≤ t ≤ 2π
x = r cos t, y = r sin t (c) y1 = e–2x cos x 0 ≤ t ≤ 20
y2 = e2x (d) y = cos x( )
x – 5 ≤ x ≤ 5π (e) f = e–3t/5 cos t 0 ≤ t ≤ 2π (f) z = – 1
3x2 + 2xy + y2
|x| ≤ 7, |y| ≤ 7 Solution.
(a) t = linspace(0, 2*pi, 200);
r = sqrt(abs(5*cos(3*t)));
polar(t, r)
120 90
60 2.5 2 1.5 1 0.5
30
0
330
300 270
240 210 180
150
Fig. E 2.10(a) (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’)
2 1.5 1 0.5 0 – 0.5 – 1 – 1.5 – 2
– 3 – 2 – 1 0 1 2 3
Fig. E 2.10(b) (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)’);
0.08
0.06
0.04
0.02
0
– 0.02
0 2 4 6 8 10 12 14 16 18 20
0.5 1 1.5 2 2.5 x 1017
0
exp (– 2x).cos (x) exp (– 2x)
Fig. E 2.10(c) (d) x = linspace(– 5*pi,5*pi,100);
y = cos(x)./x;
area(x, y);
xlabel(‘x (rad)’), ylabel(‘cos(x)/x’) hold on
8 6 4 2 0 – 2 – 4 – 6 – 8
15 10 5 0 – 5 – 10 – 15
x (rad)
cos (x)/x
Fig. E 2.10(d) (e) t = linspace(0, 2*pi, 200);
f = exp(– 0.6*t).*sin(t);
stem(t, f)
0.6 0.5
0.4
0.3
0.2 0.1 0
– 0.1
0 1 2 3 4 5 6 7
Fig. E 2.10(e)
(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)
100 50
0
0
50 50
100 6
4
2
0
– 2
– 4
– 6
– 6 – 4 – 2 0 2 4 6
Fig. E 2.10(f)
Example 2.11. Use the functions listed in Table 2.30 for plotting 3-D data for the follow-ing.
(a) z = cos x cos y –
2 2
x + y
e 5
|x| ≤ = 7, | y | ≤ 7
(b) Discrete data plots with stems x = t, y = t cos(t)
z = et/5 – 2 0 ≤ t ≤ 5π (c) A cylinder generated by
r = sin(5πz) + 3 0 ≤ z ≤ 1 0 ≤θ≤ 2π Solution.
(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)
1
0.5
0
– 0.5 – 1 10
5 0
– 5
– 10 – 10 – 5 0
5
10
Fig. E 2.11(a) (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’)
25 given as a function of the parameter t as follows:
=
Solution.
% Line plots
>> 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’)
20
15
10
5 0 5
0
– 5 – 5
0
5 z
y
x
Fig. E 2.12.
Example 2.13. Obtain the mesh and surface plots for the function z =
2
2 2
2xy
x + y over the domain – 2 ≤ x ≤ 6 and 2 ≤ y ≤ 8.
Solution.
% Mesh and surface plots 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’)
10
5 0 – 5 8
6 4
2 – 2 0 2 4 6
Fig. E 2.13(a)
z
10 5
0 – 5 8
6 4
2 – 2 0 2 4
6
Fig. E 2.13(b)
Example 2.14. Plot the function z = 2– .1 5 x + y2 2 sin(x) cos (0.5y) over 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.
(a) % Mesh Plot
>> 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’); y label(‘y’)
>> zlabel(‘z’)
0.4 (b) % Surface Plot
>> 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’); y label (‘y’)
>> zlabel(‘z’) (c) % Mesh Curtain Plot
>> 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);
>> mesh z(x, y, z)
>> xlabel(‘x’); ylabel(‘y’)
>> zlabel(‘z’)
(d) % Mesh and Contour Plot
>> 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’)
0.4
0.2
0
– 0.2
– 0.4 4
2 0
– 2
– 4 – 4 – 2
0 2
4
z
Fig. E 2.14(c)
0.5
0
– 0.5 5
0
– 5 – 5
0
5 y
Fig. E 2.14(d)
(e) % Surface and Contour Plot
>> 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’)
0.5
0
– 0.5 5
0
– 5 – 5
0
5
Fig. E 2.14(e)
Example 2.15. Plot the function −1.5 x +y2 2
z = 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.
(a) % Surface Plot with lighting
>> 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’)
0.5
0
– 0.5 5
0
– 5 – 4 – 2 0 2
4
z
Fig. E 2.15(a) (b) % Waterfall Plot
>> 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’)
0.5
0
– 0.5 5
0
– 5 – 4 – 2
0 2
4
z
y
Fig. E 2.15(b) (c) % 3-D Contour Plot
>> 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’)
0.5
– 0.5 0
4 2
0 – 2
– 4 – 4 – 2 0 2
4
z
y x
Fig. E 2.15(c) (d) % 2-D Contour Plot
>> 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’)
4
2
0
– 2
– 4
– 4 – 3 – 2 – 1 0 1 2 3 4
y
Fig. E 2.15(d)
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 = e2t 0 ≤ t ≤ 2π (d) x = et, y = 50 + et 0 ≤ t ≤ 2π (e) r2 = 3 sin 7t
y = r sin t 0 ≤ t ≤ 2π (f) r2 = 3 sin 4t
y = r sin t 0 ≤ t ≤ 2π (g) y = t sin t 0 ≤ t ≤ 5π
Solution.
(a) % Use of plot command
>> fplot(‘x.*cos(x)’, [0, 10*pi])
40
20
0
– 20
– 400 5 10 15 20 25 30
Fig. E 2.16(a) (b) % Semilog x command
>> t = linspace(0, 2 * pi, 200);
>> x = exp(– 2 * t); y = t;
>> semilog x (x, y),grid
8
6
4
2
0
10– 6 10– 4 10– 2 100
Fig. E 2.16(b) (c) % Semilog y command
t = linspace(0, 2 * pi, 200);
>> semilogy(t, exp(– 2 * t)), grid
100
10– 2
10– 4
10– 6
0 1 2 3 4 5 6 7
Fig. E 2.16(c)
(d) % Use of loglog command
>> t = linspace(0, 2 * pi, 200);
>> x = exp(t);
>> y = 50 + exp(t);
>> loglog(x, y), grid
103
102
101
100 101 102 103
Fig. E 2.16(d) (e) %Use of stairs command
>> 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]);
1.5
1
0.5
0
0 0.5 1 1.5 2 2.5 3
Fig. E 2.16(e) (f) % Use of bar command
>> 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]);
1.5
1
0.5
00 0.5 1 1.5 2 2.5 3
Fig. E 2.16(f) (g) %use of comet command
>> q = linspace(0, 5*pi, 200);
>> y = q.*sin(q);
>> comet(q, y)
10
5
0
– 5
– 10
0 5 10 15
Fig. E 2.16(g) Example 2.17. Consider the two matrices
A =
3 2
5j 10 + 2 j π
B =
7j – 15j 2π 18
Using MATLAB , determine the following:
(a) A + B (b) AB (c) A2 (d) AT (e) B–1 (f) BTAT
(g) A2 + B2 – AB
Solution: 6.2832 + 5.0000i 28.0000 + 1.4142i (b) >> A * B
9.0000 + 31.4159i 81.6814 + 8.8858i – 7.0711 + 65.0000i 98.0000 +59.7002i (d) >> inv(A)
Example 2.18. Find the inverse of the following matrices using MATLAB:
(a)
Solution.
>> clear % Clears the workspace
>> A = [3 2 0; 2 –1 7; 5 4 9]; % Spaces separate matrix columns – semicolons separate
>> inv(A); % Finds the inverse of the selected matrix
>> inv(B); % Finds the inverse of the selected matrix
>> inv(C) % Finds the inverse of the selected matrix
% Inverse of A
Example 2.19. Determine the eigenvalues and eigenvectors of matrix A using MATLAB
(a) A =
%The eigenvalues of A format short e
eig(A) ans =
1.0000e + 001 5.8579e – 001 3.4142e + 000
%The eigenvectors of A [Q, d] = eig(A)
Q =
– 5.5709e – 001 – 8.2886e – 001 – 7.3925e – 001 – 3.7139e – 001 – 3.9659e – 002 – 6.7174e – 001 – 7.4278e – 001 5.5805e – 001 – 4.7739e – 002 d =
1.0000e + 001 0 0 0 5.8579e – 001 0
0 0 3.4142e + 000 (b) A =
3 5 7 2 4 8 5 6 10
%The eigenvalues of A format short e
eig(A) ans =
1.7686e + 001
– 3.4295e – 001 + 1.0066e + 000i – 3.4295e – 001 – 1.0066e + 000i
%The eigenvectors of A [Q, d] = eig(A)
Q = Column 1
5.0537e – 001 4.8932e – 001 7.1075e – 001 Column 2
– 2.0715e – 001 – 5.2772e – 001i 7.1769e – 001
– 3.3783e – 001 + 2.2223e – 001i
Column 3
Example 2.20. Determine the eigenvalues and eigenvectors of A and B using MATLAB.
A = –
% MATLAB Program
% The matrix “a” = A * B
The eigenvectors are:
>> [Q, d] = eig (a) Q =
– 0.3263 – 0.2845 0.3908 0.3413 – 0.3619 0.7387 – 0.7816 – 0.9215 – 0.8168 – 0.6026 0.4769 0.0962 – 0.3089 0.1016 – 0.0950 0.1586 d =
98.5461 0 0 0
0 2.2964 0 0
0 0 – 1.3095 0
0 0 0 – 6.5329
Example 2.21. Solve the following set of equations using MATLAB.
(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. (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.9896 14.1285 – 5.4438 3.6128
% Therefore x1 = – 8.9896, x2 = 14.12.85, x3 = – 5.4438, x4 = 3.6128.
(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 S =
2.0000 2.0000 2.0000 – 1.0000
% Therefore x1 = 2.0000, x2 = 2.0000, x3 = 2.0000, x4 = – 1.0000.
Example 2.22. Use diff command for symbolic differentiation of the following functions:
(a) S1 = ex8 (b) S2 = 3x3 ex5
(c) S3 = 5x3 – 7x2 + 3x + 6 Solution.
(a)
>> syms x
>> S1 = exp(x ^ 8);
>> diff (S1) ans =
8*x^7*exp(x^8) (b)
>> S2 = 3* x ^3*exp(x^5);
>> diff (S2) ans =
9*x^2*exp(x^5) +15*x^7*exp(x^5) (c)
>> S3 = 5*x^3 – 7*x^2 + 3*x + 6;
>> diff (S3) ans =
15*x^2 – 14*x + 3
Example 2.23. Use MATLAB’s symbolic commands to find the values of the following integrals.
(a) 0.7
0.2 | x|dx
∫
(b) (cos 7 2)
0.2π y+ y dy
∫
(c) x
(d) 7x5 – 6x4 + 11x3 + 4x2 + 8x + 9 (e) cos a
Solution.
(a)
>>syms x, y, a, b
>> S1 = abs(x)
>> int (S1, 0.2, 0.7) ans =
9/40
(b)
>> S2 = cos (y) + 7*y^2 >> int (S2, 0, pi) ans =
7/3*pi^3 (c)
>> S3 = sqrt (x)
>> int (S3) ans =
2/3*x^ (3/2)
>> int (S3, ‘a’, ‘b’) ans =
2/3*b^ (3/2) – 2/3*a^ (3/2) >> int (S3, 0.4, 0.7) ans =
7/150*70^ (1/2) – 4/75*10^ (1/2) (d)
>> S4 = 7*x^5 – 6*x^4 + 11*x^3 + 4*x^2 + 8 * x – 9
>> int (S4) ans =
7/6*x^6 – 6/5*x^5 + 11/4*x^4 + 4/3*x^3 + 4*x^2 – 9*x (e)
>> S5 = cos (a)
>> int (S5) ans =
sin (a)
Example 2.24. Obtain the general solution of the following first order differential equa-tions:
(a) dy
dt = 5t – 6y (b)
2 2
d y
dt + 3dy
dt + y = 0 (c) ds
dt = Ax3 (d) ds
dA = Ax3 Solution.
(a)
>> solve (‘Dy = 5*t – 6*y’) ans =
5/6*t – 5/36 + exp (– 6*t)*C1
(b)
>> dsolve (‘D2y + 3*Dy + y = 0’) ans =
C1*exp (1/2*(5^ (1/2) – 3)*t) + C2*exp (– 1/2*(5^ (1/2) +3)*t) (c)
>> dsolve (‘Ds = A*x^3’, ‘x’) ans =
1/4*A*x^4 + C1 (d)
>> dsolve (‘Ds = A*x^3’, ‘A’) ans =
1/2*A^2*x^3 + C1
Example 2.25. Determine the solution of the following differential equations that satis-fies the given initial conditions.
(a) dy
dx = – 7x2 y(1) = 0.7 (b) dy
dx = 5x cos2 y y(0) = π/4 (c) dy
dx = – y + e3x y(0) = 2 (d) dy
dt + 5y = 35 y(0) = 4 Solution.
(a)
>> dsolve (‘Dy = – 7*x^2’, ‘y (1) = 0.7’) ans =
– 7*x^2*t + 7* x ^2 + 7/10 (b)
>> dsolve (‘Dy = 5*x*cos (y) ^2’, ‘y (0) = pi/4’) ans =
atan (5*t*x + 1) (c)
>> dsolve (‘Dy = – y + exp (3*x)’, ‘y (0) = 2’) ans =
exp (3*x) + exp (– t)*(– exp (3*x) +2) (d)
>> dsolve (‘Dy + 5*y = 35’, ‘y (0) = 4’) ans =
7 – 3*exp (– 5*t)
Example 2.26. Given the differential equation Using MATLAB program, find
(a) x(t) when all the initial conditions are zero (b) x(t) when x (0) = 1 and x(0) = 2.
Solution.
(a) x(t) when all the initial conditions are zero
>> x = dsolve (‘D2x = – 7*Dx – 5*x + 8’, ‘x(0) = 0’) Example 2.27. Given the differential equation
2 2
d x
dt + 12dx
dt + 15x = 35t ≥ 0 Using MATLAB program, find
(a) x(t) when all the initial conditions are zero (b) x(t) when x (0) = 0 and x(0) = 1.
Solution.
(a) x (t) when all the initial conditions are zero
>> x = dsolve (‘D2x = – 12*Dx – 15*x +35’, ‘x (0) = 0’) Example 2.28. Find the inverse of the following matrix using MATLAB.
A =
Solution.
Example 2.29. Expand the following function F(s) into partial fractions using MATLAB.
Determine the inverse Laplace transform of F(s).
F(s) =
4 3 2
1 s + 5s + 7s
The MATLAB program for determining the partial-fraction expansion is given below:
Solution.
% From the above MATLAB output, we have the following expression:
F(s) = 1
% Note that the row vector k is zero implies that there is no constant term in this exam-ple problem.
% The MATLAB program for determining the inverse Laplace transform of F(s) is given below:
>> syms s
>> f = 1/(s^4 + 5*s^3 + 7*s^2);
>> ilaplace (f) ans =
1/7*t – 5/49 + 5/49*exp (–)*cos (1/2*3^ (1/2)*t) +11/147*exp (– 5/2*t)*3^
(1/2)*sin(1/2*3^(1/2)*t) Example 2.30. Expand the following function F(s) into partial fractions using MATLAB.
Determine the inverse Laplace transform of F(s).
F(s) =
The MATLAB program for determining the partial-fraction expansion is given below:
>> b = [0 0 5 3 6];
% From the above MATLAB output, we have the following expression:
F(s) = 1
% Note that the row vector k is zero implies that there is no constant term in this exam-ple problem.
% The MATLAB program for determining the inverse Laplace transform of F(s) is given below:
>> syms s
>> f = (5*s^2 + 3*s +6)/(s^4 + 3*s^3 + 7*s^2 + 9*s +12);
>> ilaplace(f) ans =
11/14*exp(– 3/2*t)*7^(1/2)*sin(1/2*7^(1/2)*t) – 15/14*exp
(– 3/2*t)*cos(1/2*7^(1/2)*t) + 3/14*3^(1/2)*sin(3^(1/2)*t)+15/14*cos(3^(1/2)*t) Example 2.31. For the following function F(s):
F(s) =
4 3 2
4 3 2
s + 3s + 5s + 7s + 25 s + 5s + 20s + 40s + 45
Using MATLAB, find the partial-fraction expansion of F(s). Also, find the inverse Laplace transformation of F(s).
The partial-fraction expansion of F(s) using MATLAB program is given as follows:
num = [ 1 3 5 7 25];
From the MATLAB output, the partial-fraction expansion of F(s) can be written as follows:
Example 2.32. Obtain the partial-fraction expansion of the following function using
The partial fraction expansion of F(s) using MATLAB program is given as follows:
EDU>> num = conv([8 8], [1 3]);
EDU>> den = conv([1 6 8], [1 12 36]);
EDU>> [r, p, k] = residue(num, den) r =
From the above MATLAB result, we have the following expansion:
F(s) = 1 2 3 4
It should be noted here that the row vector k is zero, because the degree of the numerator is lower than that of the denominator.
F(s) = 3.25e–6t + 15e15t – 3e–3t – 0.25e–0.25t.
Example 2.33. Find the Laplace transform of the following function using MATLAB.
(a) f(t) = 7t3 cos (5t + 60°)
(g) f(t) = 5 e–3t cos(t – 45º)
Solution. % MATLAB Program
>> syms t % tell MATLAB that “t” is a symbol.
>> f = 7 * t^3*cos(5*t + (pi/3)); % define the function.
>> laplace(f) ans =
– 84/(s^2 + 25)^3*s^2 + 21/(s^2 + 25)^2 + 336*(1/2*s – 5/2*3^(1/2))/(s^2 + 25)^4*s^3 – 168* (1/2*s – 5/2*3^(1/2))/(s^2 + 25)^3*s
>> pretty(laplace(f)) % the pretty function prints symbolic output
% in a format that resembles typeset mathematics.
2 1/2 3 s 21 (1/2 s – 5/2 3 ) s -84 --- + --- + 336 2 3 2 2 2 4 (s + 25) (s + 25) (s + 25) 1/2
(1/2 s – 5/2 3 ) s – 168 2 3 (s + 25) (b) >>syms t x
>>f = – 7*t*exp(– 5*t);
>> laplace(f, x) ans =
– 7/(x + 5)^2 (c) >>syms t x
>>f = – 3*cos(5*t);
>> laplace(f, x) ans =
– 3*x/(x^2 + 25) (d) >>syms t x
>>f = t*sin(7*t);
>> laplace(f, x) ans =
1/(x^2 + 49)*sin(2*atan(7/x)) (e) >>syms t x
>>f = 5*exp(– 2*t)*cos(5*t);
>> laplace(f, x) ans =
5*(x + 2)/((x + 2)^2 + 25)
(f) >>syms t x
>>f = 3*sin(5*t + (pi/4));
>> laplace(f, x) ans =
3*(1/2*x*2^(1/2) + 5/2*2^(1/2))/(x^2 + 25) (g) >>syms t x
>>f = 5*exp(– 3*t)*cos(t – (pi/4));
>> laplace(f, x) ans =
5*(1/2*(x + 3)*2^(1/2)+1/2*2^(1/2))/((x + 3)^2 + 1)
Example 2.34. Generate partial-fraction expansion of the following function
F(s) =
+
5
2 2
10 (s + 7)(s + 13)
s(s + 25)(s + 55)(s + 7s 75)(s + 7s + 45) Solution:
Generate the partial fraction expansion of the following function:
numg = poly[– 7 – 13];
numg = poly([– 7 – 13]);
deng = poly([0 – 25 – 55 roots([1 7 75])’ roots([1 7 45])’]);
[numg, deng] = zp2tf(numg’, deng’, 1e5);
Gtf = (numg, deng);
Gtf = tf(numg,deng);
G = zpk(Gtf);
[r, p, k] = residue(numg,deng) r =
1.0e – 017 * 0.0000 – 0.0014 0.0254 – 0.1871 0.1621 – 0.0001 0.0000
0.0011 p =
1.0e + 006 * 4.6406 1.4250 0.3029
0.0336
Example 2.35. Determine the inverse Laplace transform of the following functions using MATLAB.
1/3*t – 2/9*exp(– 3/2*t)*sinh(3/2*t) (c) >>syms s
>> f = (3*s + 1)/(s^2 + 2*s + 9);
>> ilaplace(f) ans =
3*exp(– t)*cos(2*2^(1/2)*t) – 1/2*2^(1/2)*exp(– t)*sin(2*2^(1/2)*t) (d) >>syms s
Example 2.36. Find the inverse Laplace transform of the following function using MATLAB.
Solution:
% MATLAB Program
>> syms s % tell MATLAB that “s” is a symbol.
>>G = (s^2 + 9*s +7)*(s + 7)/[(s + 2)*(s + 3)*(s^2 + 12*s + 150)]; % define the function.
>>pretty(G) % the pretty function prints symbolic output
% in a format that resembles typeset mathematics.
(s + 9 s + 7) (s + 7)
(s + 2) (s + 3) (s + 12 s + 150)
>> g = ilaplace(G); % inverse Laplace transform
>>pretty(g)
44 2915 1/2 – 7/26 exp(– 2 t) + --- exp(– 3 t) + --- exp(– 6 t) cos(114 t) 123 3198
889 1/2 1/2 + --- exp(– 6 t) 114 sin(114 t) 20254
Example 2.37. Generate the transfer function using MATLAB.
G(s) =
2 2
3(s + 9)(s + 21)(s + 57) s(s + 30)(s + 5s + 35)(s + 28s + 42)
Using
(a) the ratio of factors (b) the ratio of polynomials.
Solution.
% MATLAB Program:
‘a. The ratio of factors’
>>Gzpk = zpk([– 9 – 21 – 57] , [0 – 30 roots([1 5 35]) ‘roots([1 28 42])’],3)
% zpk is used to create zero-pole-gain models or to convert TF or
% SS models to zero-pole-gain form.
‘b. The ratio of polynomials’
>> Gp = tf(Gzpk) % generate the transfer function
% Computer response:
ans =
(a) The ratio of factors Zero/pole/gain:
3 (s + 9) (s + 21) (s + 57)
---s (---s + 30) (---s + 26.41) (---s + 1.59) (---s^2 + 5---s + 35) ans =
(b) The ratio of polynomials Transfer function:
3 s^3 + 261 s^2 + 5697 s + 32319
---s^6 + 63 s^5 + 1207 s^4 + 7700 s^3 + 37170 s^2 + 44100 s Example 2.38. Generate the transfer function using MATLAB.
G(s) =
4 3 2
5 4 3 2
s + 20s + 27s + 17s + 35 s + 8s + 9s + 20s + 29s + 32. Using
(a) the ratio of factors (b) the ratio of polynomials.
Solution.
% MATLAB Program:
% a. the ratio of factors
>>Gtf = tf([1 20 27 17 35] , [1 8 9 20 29 32]) % generate the
% transfer function
% Computer response:
Transfer function:
s^4 + 20 s^3 + 27 s^2 + 17 s + 35 s^4 + 8 s^3 + 9 s^2 + 20 s + 29
% b. the ratio of polynomials
>> Gzpk = zpk(Gtf) % zpk is used to create zero-pole-gain models
% or to convert TF or SS models to zero-pole-gain form.
% Computer response:
Zero/pole/gain:
(s +18.59) (s + 1.623) (s^2 – 0.214s + 1.16) ---(s + 7.042) ---(s + 1.417) ---(s^2 – 0.4593s + 2.906) 2.23 SUMMARY
In this chapter, the MATLAB environment which is an interactive environment for numeric computation, data analysis, and graphics was presented. Arithmetic operations, display formats, elementary built-in functions, arrays, scalars, vectors or matrices, operations with arrays including dot product, array multiplication, array division, inverse and transpose of a matrix, determinants, element by element operations, eigenvalues and eigenvectors, random number generating functions, polynomials, system of linear equation, script files, programming in MATLAB, the commands used for printing information and generating 2-D and 3-D plots, input/output in MATLAB was presented with illustrative examples. MATLAB's functions for symbolic mathematics were introduced. These functions are useful in performing symbolic
operations and developing closed-form expressions for solutions to linear algebraic equations, ordinary differential equations and systems of equations. Symbolic mathematics for determining analytical expressions for the derivative and integral of an expression was also presented.
REFERENCES
Chapman, S.J., MATLAB Programming for Engineers, 2nd ed., Brooks/Cole, Thomson Learning, Pacific Grove, CA, 2002.
Etter, D.M., Engineering Problem Solving with MATLAB, Prentice-Hall, Englewood Cliffs, NJ, 1993.
Gilat, Amos., MATLAB-An Introduction with Applications, 2nd ed., Wiley, New York, 2005.
Hanselman, D., and Littlefield, B.R., Mastering MATLAB 6, Prentice Hall, Upper Saddle River, New Jersey, NJ, 2001.
Herniter, M.E., Programming in MATLAB, Brooks/Cole, pacific Grove, CA, 2001.
Magrab, E.B., An Engineers Guide to MATLAB, Prentice Hall, Upper Saddle River, New Jersey, NJ, 2001.
Marchand, P., and Holland, O.T., Graphics and GUIs with MATLAB, 3rd ed, CRC Press, Boca Raton, FL, 2003.
Moler, C., The Student Edition of MATLAB for MS-DOS Personal Computers with 3-1/
2” Disks, MATLAB Curriculum Series, The MathWorks, Inc., 2002.
Palm, W.J. III., Introduction to MATLAB 7 for Engineers, McGraw Hill, New York, NY, 2005.
Pratap, Rudra, Getting Started with MATLAB- A Quick Introduction for Scientists and Engineers, Oxford University Press, New York, NY, 2002.
Sigman, K., and Davis, T.A., MATLAB Primer, 6th ed, Chapman& Hall/CRCPress, Boca Raton, FL, 2002.
The MathWorks, Inc., MATLAB: Application Program Interface Reference Version 6, The MathWorks, Inc., Natick, 2000.
The MathWorks, Inc., MATLAB: Creating Graphical User Interfaces, Version 1, The MathWorks, Inc., Natick, 2000.
The MathWorks, Inc., MATLAB: Function Reference, The MathWorks, Inc., Natick, 2000.
The MathWorks, Inc., MATLAB: Release Notes for Release 12, The MathWorks, Inc., Natick, 2000.
The MathWorks, Inc., MATLAB: Symbolic Math Toolbox User’s Guide, Version 2, The MathWorks, Inc., Natick, 1993-1997.
The MathWorks, Inc., MATLAB: Using MATLAB Graphics, Version 6, The MathWorks, Inc., Natick, 2000.
PROBLEMS