Since this sounds like it may be homework I'm only going to give two hints.
One way to do step 1 is to use discretize.
You may want to check the case where the element of x is a member of the d vector beforehand.
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!