MATLAB Basics ——— 85
PROBLEMS
P1.1: Compute the following quantity using MATLAB in the Command Window:
17 éê 5 – 1ùú 57 log (e3 )
ë û + 10 + ln(e4 ) + 11
é152 – 132 ù ʌ 121
êë úû
P1.2: Compute the following quantity using MATLAB in the Command Window:
tan x + sin 2 x
B= + log x5 − x 2 + cosh x − 2 tanh x ; for x = 5π/6.
cos x
P1.3: Compute the following quantity using MATLAB in the Command Window:
ab (a + b) 14 b log10 c
x = a+ + ca + + ln(2) + + 2sinh a - 3 tanh b
c | ab | 3 c log10 (a + b + c )
e
for a = 1, b = 2 and c = 1.8.
P1.4: Use MATLAB to create
(a) a row and column vectors that has the elements: 11, –3, e7.8, ln(59), tan(p/3), 5 log10(26).
(b) a row vector with 20 equally spaced elements in which the first element is 5.
(c) a column vector with 15 equally spaced elements in which the first element is –1.
P1.5: Enter the following matrix A in MATLAB and create:
é1 2 3 4 5 6 7 8ù
ê ú
ê9 10 11 12 13 14 15 16 úú
ê
A = êê17 18 19 20 21 22 23 24úú
ê25 26 27 28 29 30 31 32úú
ê
ê33 34 35 36 37 38 39 40úû
ú
êë
(a) a 4 × 5 matrix B from the 1st, 3rd and the 5th rows, and the 1st, 2nd, 4th and 8th columns of the matrix A.
(b) a 16 element-row vector C from the elements of the 5th row, and the 4th and 6th columns of the matrix A.
( )
1.8
2 +0.02
P1.6: Given the function y = x + ex ln x. Determine the value of y for the following values of
x : 2, 3, 8, 10, –1, –3, –5, –6.2. Solve the problem using MATLAB by first creating a vector x, and creating
a vector y, using element-by-element calculations.
P1.7: Define a and b as scalars, a = 0.75, and b = 11.3, and x, y and z as the vectors, x = 2, 5, 1, 9,
y = 0.2, 1.1, 1.8, 2 and z = –3, 2, 5, 4. Use these variables to calculate A given below using element-by-element
computations for the vectors with MATLAB.
86 ——— MATLAB: An Introduction with Applications
æz y ÷ö
çç + ÷
x1.1 -2 5
y z çè x 2 ÷ø
A= +a
b/3
( a + b) za
P1.8: Enter the following three matrices in MATLAB
é 1 2 3ù é12 -5 4 ù é 7 13 4ù
ê ú ê ú ê ú
A = êê-8 5 7úú , B = êê 7 11 6 úú , C = êê-2 8 -5úú
êê-8 4 6úú êê 1 8 13úú êê 9 -6 11 úú
ë û ë û ë û
and show that
(a) A + B = B + A
(b) A + (B + C) = (A + B) + C
(c) 7(A + C) = 7(A) + 7(C)
(d) A * (B + C) = A * B + A * C
P1.9: Consider the polynomials
p1(s) = s3 + 5s2 + 3s + 10
p2(s) = s4 + 7s3 + 5s2 + 8s + 15
p3(s) = s5 + 15s4 + 10s3 + 6s2 + 3s + 9
Determine p1(2), p2(2) and p3(3).
P1.10: The following polynomials are given:
p1(x) = x5 + 2x4 – 3x3 + 7x2 – 8x + 7
p2(x) = x4 + 3x3 – 5x2 + 9x + 11
p3(x) = x3 – 2x2 – 3x + 9
p4(x) = x2 – 5x + 13
p5(x) = x + 5
Use MATLAB functions with polynomial coefficient vectors to evaluate the expressions at x = 2.
P1.11: Determine the roots of the following polynomials:
(a) p1(x) = x7 + 8x6 + 5x5 + 4x4 + 3x3 + 2x2 + x + 1
(b) p2(x) = x6 – 7x6 + 7x5 + 15x4 – 10x3 – 8x2 + 7x + 15
(c) p3(x) = x5 – 13x4 + 10x3 + 12x2 + 8x – 15
(d) p4(x) = x4 + 7x3 + 12x2 – 25x + 8
(e) p5(x) = x3 + 15x2 – 23x + 105
(f) p6(x) = x2 – 18x + 23
(g) p7(x) = x + 7
P1.12: An aluminium thin-walled sphere is used as a marker buoy. The sphere has a radius of 65 cm and
a wall thickness of 10 mm. The density of aluminium is 2700 kg/m3. The buoy is placed in the ocean where
the density of the water is 1050 kg/m3. Determine the height H between the top of the buoy and the surface
of the water.
MATLAB Basics ——— 87
H
r
Fig. P1.12
P1.13: Determine the values of x, y and z for the following set of linear algebraic equations:
x2 – 3x3 = –7
2x1 + 3x2 – x3 = 9
4x1 + 5x2 – 2x3 = 15
P1.14: Write a simple script file to find (a) dot product, (b) cross-product of 2 vectors:
a = ĵ -kˆ and b = 3iˆ - ˆj
P1.15: Write a function to find gradient of f (x, y) = x 2 + y 2 – 2xy + 4 at (a) (1,1), (b) (1,– 2) and (c) (0,– 3).
Use the function name from command prompt.
P1.16: Write MATLAB functions f = x2 – 3x + 1 and g = ex – 4x + 6 and find the result f (127)/g(5) from
a script file.
P1.17: Plot the function y = |x| cos (x) for –200 ≤ x ≤ 200.
P1.18: Plot the following functions on the same plot for 0 ≤ x ≤ 2π using the plot function:
(a) sin2 (x)
(b) cos2x
(c) cos(x)
P1.19: Plot a graph of the function y = 45 sin(0.4t) for t ∈[0, 3].
P1.20: Consider the function z = 0.56 cos(xy). Draw a surface plot showing variation of z with x and y.
Given x∈[0, 10] and y∈[0,100].
P1.21: Figure P1.21 shows two boats: boat A travels south at a speed of 10 mph, and boat B travels east
at a speed of 19 mph. The ships are positioned at 8 a.m. are also shown in figure. Write a MATLAB program
to plot the distance between the ships as a function of time for the next 5 hours.
y
Boat A
16 miles
Boat B
x
30 miles
Fig. P1.21
88 ——— MATLAB: An Introduction with Applications
P1.22: Consider the given symbolic expressions defined below:
S1 = ‘2/(x – 5)’; S2 = ‘x ^ 5 + 9 * x – 15’; S3 = ‘(x ^ 3 + 2 * x +9) * (x * x – 5)’;
Perform the following symbolic operations using MATLAB.
(a) S1S2/S3 (b) S1/S2S3 (c) S1/(S2)2 (d) S1S3/S2 (e) (S2)2/(S1S3)
P1.23: Solve the following equations using symbolic mathematics:
(a) x2 + 9 = 0
(b) x2 + 5x – 8 = 0
(c) x3 + 11x2 – 7 x + 8 = 0
(d) x4 + 11x3 + 7x2 – 19x + 28 = 0
(e) x7 – 8x5 + 7x4 + 5x3 – 8x + 9 = 0
P1.24: Determine the values of x, y and z for the following set of linear algebraic equations:
2x + y – 3z = 11
4x – 2y + 3z = 8
–2x + 2y – z = –6
P1.25: Figure P1.25 shows a scale with two springs.
k k
k k b
x a
Fig. P1.25
The two springs are unstretched initially and will stretch when a mass is attached to the ring and the ring will
displace downwards a distance of x. The weight W of the object is given by
2k
W= (A - A 0 )(b + x )
A
where A0 = initial length of a spring = a 2 + b2
and A= the stretched length of the spring = a 2 + (b + x ) 2 .
If k = spring constant,
Write a MATLAB program to determine the distance x when W = 350 N. Given a = 0.16 m, b = 0.045 m,
and the spring constant k = 3000 N/m.
P1.26: Determine the solutions of the following first-order ordinary differential equations using MATLAB’s
symbolic mathematics.
(a) y′ = 8x2 + 5 with initial condition y(2) = 0.5.
MATLAB Basics ——— 89
(b) y′ = 5x sin2(y) with initial condition y(0) = π/5.
(c) y′ = 7x cos2(y) with initial condition y(0) = 2.
(d) y′ = –5x + y with initial condition y(0) = 3.
(e) y′ = 3y + e–5x with initial condition y(0) = 2.
P1.27: For the following differential equations, use MATLAB to find x(t) when (a) all the initial conditions
are zero, (b) x(t) when x (0) = 1 and x (0)= –1.
d2x dx d2x dx
(a) 2
+10 + 5 x = 11 (b) 2
– 7 -3x = 5
dt dt dt dt
d2x dx d 2 x dx
(c) + 3 + 7 x = –15 (d) + + 7 x = 26
dt 2 dt dt 2 dt
P1.28: Figure P1.28 shows a water tank (shaped as an inverted frustum cone with a circular hole at the
bottom on the side).
R=0.5 m
3m
rh=0.025 m
R = 2m
Fig. P1.28 Water tank
The velocity of water discharged through the hole is given by v = 2gy where h = height of the water and
g = acceleration due to gravity (9.81 m/s2). The rate of discharge of water in the tank as the water drains out
dy 2gyrh2
through the hole is given by: =- where y = height of water and rh = radius of the hole. Write a
dt (2 -0.5 y)2
MATLAB program to solve and plot the differential equation. Assume, that the initial height of the water is 2.5 m.
P1.29: An airplane uses a parachute (see Fig. P1.29) and other means of braking as it slow down on the
runway after landing. The acceleration of the airplane is given by a = – 0.005 v2 – 4 m/s2
v
x
Fig. P1.29
90 ——— MATLAB: An Introduction with Applications
Considering the airplane with a velocity of 500 km/h opens its parachute and starts decelerating at
t = 0 second, write a MATLAB program to solve the differential equation and plot the velocity from
t = 0 second until the airplane stops.
P1.30: Obtain the first and second derivatives of the following functions using MATLAB’s symbolic
mathematics.
(a) F(x) = x5 – 8x4 + 5x3 – 7x2 + 11x – 9
(b) F(x) = (x3 + 3x – 8)(x2 + 21)
(c) F(x) = (3x3 – 8x2 + 5x + 9)/(x + 2)
(d) F(x) = (x5 – 3x4 + 5x3 + 8x2 – 13)2
(e) F(x) = (x2 + 8x –11)/(x7 – 7x6 + 5x3 + 9x – 17)
P1.31: Determine the values of the following integrals using MATLAB’s symbolic functions.
(a) ò ( 5x 7
- x 5 + 3 x 3 - 8 x 2 + 7) dx
(b) ò x cos x
òx
2/3
(c) sin 2 2 x
1.8
(d) ò0.2 x2 sin x dx
-0.2
(e) ò -1 x dx
5
1
P1.32: Use MATLAB to calculate the following integral: ò 0.8x2 +0.5 x +2 dx
0
10
∫ cos
2
P1.33: Use MATLAB to calculate the following integral: (0.5 x)sin 4 (0.5 x) dx
0
P1.34: The variation of gravitational acceleration g with altitude y is given by:
R2
g= go ,
( R + y )2
where R = 6371 km is radius of the earth and go= 9.81 m/s2 is gravitational acceleration at sea level.
The change in the gravitational potential energy ΔU of an object that is raised up from the earth is given by:
y
ΔU = ∫ mgdy
0
MATLAB Basics ——— 91
Determine the change in the potential energy of a satellite with a mass of 500 kg that is raised from the surface
of the earth to a height of 800 km.
P1.35: Find the Laplace transform of the following function using MATLAB:
f (t) = 7t3cos(5t + 60°)
P1.36: Use MATLAB program to find the transforms of the following functions.
(a) f (t) = –7t e–5t
(b) f (t) = –3 cos 5t
(c) f (t) = t sin 7t
(d) f (t) = 5 e–2 t cos 5t
(e) f (t) = 3 sin(5t + 45º)
(f ) f (t) = 5 e–3t cos(t – 45º)
P1.37: Consider the two matrices
⎡ 1 0 2⎤ ⎡ 7 8 2⎤
A = ⎢ 2 5 4⎥ and B = ⎢⎢ 3 5 9 ⎥⎥
⎢ ⎥
⎢⎣ −1 8 7⎥⎦ ⎢⎣ −1 3 1⎥⎦
Using MATLAB, determine the following:
(a) A+B
(b) AB
(c) A2
(d) AT
(e) B–1
(f ) B TA T
(g) A 2 + B 2 – AB
(h) determinant of A, determinant of B and determinant of AB.
P1.38: Use MATLAB to define the following matrices:
⎡ 2 1⎤ ⎡2 3⎤
⎡5 3⎤
A = ⎢⎢0 5⎥⎥ , B=⎢ , C = ⎢⎢ −5 −2⎥⎥ , D = [1 2]
⎣ −2 −4⎦⎥
⎣⎢7 4⎦⎥ ⎣⎢ 0 3 ⎥⎦
Compute matrices and determinants if they exist.
(a) (ACT)–1
(b) |B|
(c) |ACT|
(d) (CTA)–1
P1.39: Consider the two matrices
⎡ 1 0 1⎤ ⎡ 7 4 2⎤
A = ⎢ 2 3 4⎥ and B = ⎢⎢ 3 5 6⎥⎥
⎢ ⎥
⎣⎢−1 6 7⎦⎥ ⎢⎣−1 2 1⎦⎥
92 ——— MATLAB: An Introduction with Applications
Using MATLAB, determine the following:
(a) A + B
(b) AB
(c) A2
(d) AT
(e) B–1
( f ) BTAT
(g) A2 + B2 – AB
(h) det A, det B and det AB.
P1.40: Find the inverse of the following Matrices:
⎡3 2 1⎤
(a) A = ⎢⎢ −1 5 4 ⎥⎥
⎢⎣ 5 7 −9⎥⎦
⎡ 1 6 3⎤
(b) B = ⎢⎢ −4 −5 7 ⎥⎥
⎢⎣ 8 4 2⎥⎦
⎡ −1 −2 5 ⎤
(c) C = ⎢⎢ −4 7 2 ⎥⎥
⎣⎢ 7 −8 −1⎦⎥
P1.41: Determine the eigenvalues and eigenvectors of the following matrices using MATLAB.
⎡1 −2⎤ ⎡ 1 5⎤
A= ⎢ ⎥ , B=⎢ ⎥
⎣1 5 ⎦ ⎣ −2 7 ⎦
⎡ 4 6 2⎤
⎢ ⎥
P1.42: If A = ⎢ 5 6 7⎥
⎣⎢10 5 8⎥⎦
Use MATLAB to determine the following:
(a) the three eigenvalues of A
(b) the eigenvectors of A
(c) Show that AQ = Qd where Q is the matrix containing the eigenvectors as columns and d is the
matrix containing the corresponding eigenvalues on the main diagonal and zeros elsewhere.
P1.43: Determine eigenvalues and eigenvector of A using MATLAB.
⎡ 0.5 −0.8⎤
(a) A = ⎢ ⎥
⎣0.75 1.0 ⎦
⎡ 8 3⎤
(b) A= ⎢ ⎥
⎣ −3 4 ⎦
MATLAB Basics ——— 93
P1.44: Determine the eigenvalues and eigenvectors of the following matrices using MATLAB.
⎡1 −2 ⎤
(a) A = ⎢ ⎥
⎣1 3 ⎦
⎡ 1 5⎤
(b) A= ⎢ ⎥
⎣ −2 4 ⎦
P1.45: Determine the eigenvalues and eigenvectors of A * B using MATLAB.
⎡3 −1 2 1⎤ ⎡1 2 5 7⎤
⎢1 2 7 4⎥⎥ ⎢ 2 −1 −2 4⎥⎥
A=⎢ , B=⎢
⎢7 −1 8 6⎥ ⎢3 2 5 1⎥
⎢ ⎥ ⎢ ⎥
⎣⎢1 −2 3 4 ⎦⎥ ⎣⎢ 4 1 −3 6 ⎦⎥
P1.46: Determine the eigenvalues and eigenvectors of A and B using MATLAB.
⎡ 4 5 −3⎤ ⎡1 2 3⎤
A = ⎢ −1 2 3⎥ , B = ⎢⎢8 9 6 ⎥⎥
⎢ ⎥
⎢⎣ 2 5 7 ⎥⎦ ⎢⎣5 3 −1⎥⎦
P1.47: Determine the eigenvalues and eigenvectors of A = a * b using MATLAB.
⎡6 −3 4 1⎤
⎢0 4 2 6⎥⎥
a= ⎢
⎢1 3 8 5⎥
⎢ ⎥
⎢⎣ 2 2 1 4 ⎥⎦
⎡0 1 2 3⎤
⎢4 5 6 −1 ⎥⎥
⎢
b = ⎢1 5 4 2⎥
⎢ ⎥
⎣⎢ 2 −3 6 7 ⎦⎥
P1.48: Determine the values of x, y and z for the following set of linear algebraic equations:
x2 – 3x3 = –7
2x1 + 3x2 – x3 = 9
4x1 + 5x2 – 2x3 = 15
P1.49: Determine the values of x, y and z for the following set of linear algebraic equations:
2x – y = 10
–x + 2y – z = 0
–y + z = –50
P1.50: Solve the following set of equations using MATLAB.
(a) 2x1 + x2 + x3 – x4 = 12
x1 + 5x2 – 5x3 + 6x4 = 35
– 7x1 + 3x2 – 7x3 – 5x4 = 7
x1 – 5x2 + 2x3 + 7x4 = 21
94 ——— MATLAB: An Introduction with Applications
(b) x1 – x2 + 3x3 + 5x4 = 7
2x1 + x2 – x3 + x4 = 6
–x1 – x2 – 2x3 + 2x4 = 5
x1 + x2 – x3 + 5x4 = 4
P1.51: Solve the following set of equations using MATLAB.
(a) 2x1 + x2 + x3 – x4 = 10
x1 + 5x2 – 5x3 + 6x4 = 25
–7x1 + 3x2 – 7x3 – 5x4 = 5
x1 – 5x2 + 2x3 + 7x4 = 11
(b) x1 – x2 + 3x3 + 5x4 = 5
2x1 + x2 – x3 + x4 = 4
–x1 – x2 + 2x3 + 2x4 = 3
x1 + x2 – x3 + 5x4 = 1
P1.52: Solve the following set of equations using MATLAB.
(a) x 1 + 2x 2 + 3x 3 + 5x 4 = 21
–2x 1 + 5x 2 + 7x 3 – 9x 4 = 17
5x1 + 7x 2 + 2x3 – 5x 4 = 23
–x 1 – 3x 2 – 7x 3 + 7x 4 = 26
(b) x 1 + 2x 2 + 3x 3 + 4x 4 = 9
2x1 – 2x2 – x 3 + x 4 = –5
x 1 – 3x 2 + 4x 3 – 4x 4 = 7
2x1 + 2x 2 – 3x3 + 4x 4 = –6
P1.53: Determine the inverse of the following matrix using MATLAB.
⎡ 3s 2 0⎤
A = ⎢⎢7 s − s −5 ⎥⎥
⎢⎣ 3 0 −3s ⎥⎦
P1.54: Expand the following function F(s) into partial fractions with MATLAB:
5s 3 + 7 s 2 + 8s + 30
F (s) =
s + 15s 3 + 62s 2 + 85s + 25
4
P1.55: Determine the Laplace transform of the following time functions using MATLAB.
(a) f (t) = u (t + 9)
(b) f (t) = e5t
(c) f (t) = (5t + 7)
(d) f (t) = 5u (t) + 8e7t – 12e–8t
(e) f (t) = e–t + 9t3 – 7t–2 + 8
(f) f (t) = 7t 4 + 5t2 – e –7t
(g) f (t) = 9 u t + 5e –3t
MATLAB Basics ——— 95
P1.56: Determine the inverse Laplace transform of the following rotational function using MATLAB:
7 7
F ( s) = =
2
s + 5s + 6 ( s + 2) ( s + 3)
P1.57: Determine the inverse transform of the following function having complex poles
15
F (s) = 3 2
( s + 5s + 11s + 10)
P1.58: Determine the inverse Laplace transform of the following functions using MATLAB:
s
(a) F ( s) =
s (s + 2)(s + 3)(s + 5)
1
(b) F ( s ) =
s ( s + 7)
2
5s + 9
(c) F ( s ) =
( s + 8s + 5)
3
s − 28 .
(d) F ( s ) =
s ( s + 9 s + 33)
2
❍❍❍