2.
2 Lesson 2: Working with Arrays of Numbers 27
EXERCISES
1. Equation of a straight line: The equation of a straight line is y = mx + c
where m and c are constants. Compute the y-coordinates of a line with slope
m = 0.5 and the intercept c = −2 at the following x-coordinates:
x = 0, 1.5, 3, 4, 5, 7, 9, and 10.
[Note: Your command should not involve any array operators since your cal-
culation involves multiplication of a vector with a scalar m and then addition
of another scalar c.]
2. Multiply, divide, and exponentiate vectors: Create a vector t with 10
elements: 1, 2, 3, . . ., 10. Now compute the following quantities:
• x = t sin(t).
• y = t−1
t+1 .
sin(t2 )
• z= t2 .
3. Points on a circle:All points with coordinates x = r cos θ and y = r sin θ,
where r is a constant, lie on a circle with radius r, i.e., they satisfy the
equation x2 + y 2 = r2 . Create a column vector for θ with the values 0, π/4,
π/2, 3π/4, π, and 5π/4.
Take r = 2 and compute the column vectors x and y. Now check that x and y
indeed satisfy the equation of circle, by computing the radius r = (x2 + y 2 ).
[To calculate r you will need the array operator .^ for squaring x and y. Of
course, you could compute x2 by x.*x also.]
4. The geometric series: This is funky! You know how to compute xn
element-by-element for a vector x and a scalar exponent n. How about com-
puting nx , and what does it mean? The result is again a vector with elements
nx1 , nx2 , nx3 etc.
The sum of a geometric series 1 + r + r2 + r3 + . . . + rn approaches the limit
1−r for r < 1 as n → ∞. Create a vector n of 11 elements from 0 to 10.
1
Take r = 0.5 and create another vector x = [r0 r1 r2 . . . rn ] with the
command x = r.^n . Now take the sum of this vector with the command
1
s = sum(x) (s is the sum of the actual series). Calculate the limit 1−r and
compare the computed sum s. Repeat the procedure taking n from 0 to 50
and then from 0 to 100.
5. Matrices and vectors: Go to Fig. 3.1 on page 51 and reproduce the re-
sults. Now create a vector and a matrix with the following commands: v =
0:0.2:12; and M = [sin(v); cos(v)]; (see Section 3.1.4 on page 55 for
use of ‘:’ in creating vectors). Find the sizes of v and M using the size com-
mand. Extract the first 10 elements of each row of the matrix, and display
them as column vectors.