10. Anonymous Functions:  Homework

Last revision:  February 9 1998

Author: YourNameGoesHere

1: xyDif

Write a function xyDif[points_] which takes a list of x,  y points and returns the difference between the x- and y-coordinate for each point.  For instance, 
	xyDif[{ {2,3}, {14,18}, {1,7} }]
should return {1,4,6}.  Do this problem by first deciding how to compute the difference between the x- and y-coordinates of a single point and then mapping such a function onto the list of points.  Give two versions of xyDif[], one that uses a Module, and one that uses an anonymous function.

2: toJail

What follows below is a list of the names and ages of a group of people drinking punch at a party.  As it turns out, the party was raided by the police and all of the underage drinkers (those younger than 21) were carted off to jail.  Write a function toJail[] which produces a list of the names of the people who went to jail.  Use a named function inside a Module or an anonymous function to get a list of the {name,age} pairs of the people going to jail.  From there it should be easy to get just the name of each person going to jail.

people={ {"Chris",18},{"Clark",22},
	{"Becky",20},{"June",37},
	{"Chief",21}, {"Stella",23}, 
	{"Heather",31}, {"Xray",16} }

3: finding roots

(a) There are times when Solve[] and even NSolve[] fail to give solutions.  For example,

Clear[x,f]
f[x_] := x^2 Cos[2x]+x Sin[2x]
NSolve[f[x]==0 ,x]

Yet, when we plot the function f(x) from –6 to 6 we see that there are nine places where the f(x) equals zero (i.e. touches or crosses the x-axis).

Plot[f[x],{x,-6,6}]

One of these places seems to be at about x = 4.  We can find the exact value by using the FindRoot[] command, indicating that we want to find the root of f(x) that is near x = 4

FindRoot[ f[x]==0 , {x,4} ]

The approximate roots for f(x) seem to be at

approx={-6,-4,-2.5,-1,0,1,2.5,4,6}

To find all nine roots you could enter the FindRoot command 9 times, but instead, map an anonymous function involving FindRoot[] onto the list approx to find good approximations of the nine roots.

(b) Each of the functions f1, f2, and f3 defined below has a root near x = 2.  Map an anonymous function involving FindRoot[] onto the list {f1,f2,f3} to find all three roots.

f1[x_]= 2x-17Cos[x]
f2[x_]= x^2-3Sin[x]
f3[x_]= 2Sin[x]^2-x
Plot[{f1[x],f2[x],f3[x]},{x,0,5}]

4: perfectSearch, perfectS1, perfectS2

A number is called perfect if it is equal to the sum of its divisors (not counting itself).  Use the Divisors function and write a function perfectSearch[] with two arguments which finds all perfect numbers between the value of the first argument and the value of the second argument and returns a list of pairs, where each pair consists of the perfect number and the list of its divisors. For instance, perfectSearch[4,8] should yield {{6,{1,2,3}}} because 4 is not equal to 1 + 2;  5 is not equal to 1 (there are no divisors of 5 except 1 and 5); 6, however, is equal to 1+2+3; 7 is not equal to 1; and 8 is not equal to 1 + 2 + 4.   Give two versions of perfectSearch[], one that uses a Module, and one that uses an anonymous function.

Hint:  do this problem in several steps, following a bottom-up programming style.  First, create a function which selects only the perfect numbers from the integers from the value of the first argument to the value of the second, so, say, perfectS1[4,8] returns {6}.  Then write a function which takes a list of numbers and returns the pairs, so that perfectS2[{6}] returns {{6,{1,2,3}}}.