Exercises

I hope you now understand how rules work and how you can use them to manipulate your expressions. Here are some exercises to test your understanding.

1. For the expression

write a rule that expands the argument of Sin, but does not expand the term (x+y)^2.

Solution:

2. In the substitution

why doesn't the rule expand the term (x+2)(x-2)? Write a rule that will expand y(x+2)(x-2).

Solution: The right-hand side of the rule gets evaluated immediately, so Expand[z] evaluates to z. Use a delayed rule instead.

3. Given five points and five heights at those points, construct triplets of the form using only list manipulation constructs and rules, and without using Part or functions with iterators (such as Table or Do).

Solution:

4. FactorInteger returns a list of the prime factors of its argument together with their exponents. For example,

If you raise the first number in the pair to the power of the second number and then multiply the results together, you should obtain the number you factored.

Write a rule that takes the result returned by FactorInteger and computes the original number.

Solution:

5. FindRoot uses Newton's method to obtain a root, given a function and a starting value. Plot the function BesselJ[0,x] for in the range [0, 20]. Choose seven starting values of x in that range and, for each one, draw the line segment that joins the point {x,BesselJ[0,x]} on the graph to the point {r,0} on the -axis, where r is the root returned by FindRoot when given the starting value x. (You can combine a graph of a function with a line produced with the command Graphics by using Show.)

Solution: