Search for question

Part (a)

Let e = 2.718... be the base of the natural logarithm. Create three arrays A, B, C each of which is a row vector with 107 entries such that

A(k)= B(k)=C(k)=e-¹/k cos(k²+3)

for 1 ≤ k < 107, as follows:

• The array A is created using a for loop without a pre-initialization of the array.

• The array B is created using a for loop and it is first pre-initialized using the zeros command.

• The array C is created without using a loop.

For each of the three arrays use the functions tic and toc to measure the time needed to create it (this includes the pre-initialization for array B). Determine which method is most efficient, and which method is least efficient.

Part (b)

Use Matlab's function tan and the built-in constant pi to create the vector

Next, create the vector W containing the values of these three expressions as you know them from Calculus. You can use Matlab's sqrt function. Do not use the tan function when creating vector W.

Part (c)

Apply Matlab's equality test == to vectors V and W. Explain the results in words.

Part (d)

Because Matlab generally uses numerical approximations in all computations, testing equality of two vectors (or matrices, or numbers) has to be done in a more robust way. Use the function norm to check that V and W are equal up to a small numerical error. Explain your solution.