Starting from:

$29.99

Assignment II- for loops are often practically inefficient

ECE-210-A Assignment II

In this assignment, I hope that you may come to terms with the fact that for loops are often
practically inefficient, despite the fact that they are often the first way we may imagine to tackle
a problem. You’ll also use many of the tools learned in the second lesson, including application of
more vector and array functions and plot.
1. In this problem you will explore the use of MATLAB to approximate derivatives and integrals.
• Create two vectors of length 100 and 1000, each containing evenly spaced samples of the sine
function over one period.
• Approximate the derivatives of both vectors by taking the differences of adjacent elements
and dividing by the spacing between them.
• You now have length 99 and 999 approximations to some other function; what is it? Find out
how well you approximated the values of this function by creating length 99 and 999 vectors of
evenly spaced samples over the same interval, and compute the respective differences. Find the
maximum of the absolute value of these differences; which is a better estimate? The answer
had better not surprise you!
• Now approximate the integrals of your original vectors using both cumtrapz and cumsum,
yielding 4 approximations for the integral of the sine function. As with above, compare the
error values to the analytic integral of the sine over one period; it will be pretty clear if you’ve
made an error.
• Plot your better approximation for the derivative. Give the plot a title.
2. Perform the following matrix operations:
• Use reshape to create a 10 × 10 matrix A where A =





1 11 ... 91
2 12 ... 92
.
.
.
.
.
.
.
.
.
.
.
.
10 20 ... 100





.
• Flip the second column of B such that the column is inverted up down.
• Flip the matrix A from left to right.
• Make a vector that is the the column-wise sum of every column of A. The result should be
a row vector.
• Make a vector that is the row-wise mean of every row of A. The result should be a column
vector.
• Delete the last column of A.
1
3. Generate a 300 × 500 matrix with entries ai,j =
i
2+j
2
i+j+3 using the following methods and use tic
toc to time the speed of each and write a comment noting the order of the speeds of these methods.
You will have to find out, using doc, Google, or frantically messaging me, how tic toc works.
• Using for loops and no pre-allocation.
• Using for loops and pre-allocating memory with zeros.
• Using only elementwise matrix operations. Note: repmat and meshgrid will be useful here.
2

More products