Starting from:
$35

$29

Homework 3 Solution

In this homework, you will review logical indexing in MATLAB, a very important technique used to e ciently index a matrix or vector. You will also have a chance to work with functions in a separate le.

    1. Roll the Dice In this part you will work with imshow and logical indexing. Create 100x100 matrices A, B and C of all ones.




















In matrix A, set the values of the entries aij equal to zero if


(i
25)2 + (j
75)2 < 10


p

2

2

j



to create the indices, then

or


(i
75)
+ (j
25)

















< 10.  (Hint: Use meshgrid p




logical indexing on A using
, & or~).



p





or
(i
75)2
+ (j
75)2 < 10.

ij














equal to zero if

(i
25)2 + (j
25)2 < 10

In matrix B, set the values of the entries b





















p






matrix C, set the values of the entries c
ij
equal to zero if

(i
50)2 + (j
50)2 > 10.

In p














Now use gure and imshow to plot: { The complement of C
{ A

{ The next 3 faces of a die (so 3-5) on three separate gures. Use whatever logical operations (&, j, or~) are necessary to accomplish this.
    2. Fun with  nd Write a function to return the value and index of a number in a vector

        ◦ matrix that is closest to a desired value. The function should be called as [val; ind] = findClosest(x; desiredV alue). This function can be accomplished in less than ve lines. You will nd abs, min and/or nd useful, Hint: You may have some trouble using min when x is a matrix. To convert the matrix to a vector, you can use y = x(:). Show that it works by nding the value closest to 3=2 (and index of said value) in sin( linspace(0,5,100) ) + 1.

    3. Calculus Nostalgia This problem will reacquaint you with the rst derivative test and points of in ection.

Write a function, called signSwitch, in a separate le which inputs a vector v and outputs a vector with the indices i which represent a sign change in v; i.e. report 15 if the sign changed in v between index 14 and index 15. Do not consider going from positive or negative to zero. We could loop through and check this condition at every point - don’t do that. Instead think of a way to use logical indexing: One suggestion is to write conditions on the vector and some kind of shifted version of itself. Beware however, when you do this you will have non-overlapping points. It is up to you to gure out what to with them. This will be a local function (see the documentation if you forgot what this means).

Now we will write the main function, call it whatever you’d like, which will perform all the analysis. The function will input two vectors representing the x and y coordinates of the graph of the function and will return a vector with the approximate indices of the local extrema and a vector with the approximate indices of the points of in ection.

First have the function take the rst derivative (approximately, see homework 2). Then use your local function to apply the rst derivative test to see where the approximate local minima and maxima are.


1
ECE-210B Homework 3    Samuel Maltz


Next, have the function take the second derivative. Then use your local function again to nd approximately where the points of in ection are.

Finally, have the function plot all this information using gure then plot(x,y,xminmax,yminmax,’ko’,xpoi,ypoi,’r*’). This will plot the function then plot black circles on the local minima and maxima and red stars on the points of in ection.

Apply your function to x5    8x3 + 10x + 6 sampled at 10000 points on [-3,3].

















































2

More products