Introduction to Mathematica 
Programming Exercises

The Class

Violeta M. Ivanova, Ph.D., Educational Technology Consultant, MIT Academic Computing
Contact: N42-250J, 617-252-1383, violeta@mit . edu
Class materials: http://web.mit.edu/acmath/math/IntroMathematica
Web site: http://web.mit.edu/violeta/www/IAP2006

References

Mathematica 5.2 Documentation, Wolfram Research Inc., http://documents.wolfram.com/mathematica

Exercise One
Relational and Logical Operators: IsStringList

Write a function IsStringList to do the following:
1. Take two arguments only, one String and one List, which can be given in any order.
2. Print "correct input: a String and a List", when called with a String and a List argument.
2. Print "correct input: a List and a String", when called with a List and a String argument.
3. Print "arguments should be a String and a List", if called with two arguements of another head. 
4. Print "not enough arguments" if you call it with one argument.
6. Print "too many arguments" if you call it with more than two arguments.
Hint: here is an illustration of how input and output cells may look when you use IsStringList:

SOLUTION:

Exercise Two
Procedural and Pattern-Based Programming: PointsCurveFit

A. Write a function PointsCurveFit which:
1. Takes three arguments: (i) a function of type f(x), (ii) x, and (iii) a list of point coordinates {x\(TraditionalForm\`\_i\), (Subscript[y, i])}.
2. Computes f(x) in the interval from x\(TraditionalForm\`\(\(\_\(i, \ min\)\)\(\ \)\)\)to x\(TraditionalForm\`\_\(i, \ max\)\).  
3. Plots on the same plot: 
	(a) the curve representing f(x) as a blue line (or a color of your choice);
	(b) the points from the list, so those above the curve are red, and those under the curve are green (the colors have to be different). 
4. Prints the number of points under the curve and the number of points above the curve.
5. Can accept arguments such as f(y) and y or f(z) and z, i.e. x is not a literal x.
6. Does not save in the Global context any of the temporary variables used during the computations.
B. Test PointsCurveFit with a function and a set of points. For example, use as arguments the exponential function and the point set we used in Graphics:
	"Prime fraction" point set: {i, Prime[i + 1]/Prime[i]}, where i = 1, 2, ..., 100
	Exponential function: f(x) = 1.02` + 
        0.68` (e^(-0.16` x))
Hints:
1) Most of the solution involves procedural and functional programming, but you will also need to use some pattern-matching.
2) Write the entire function PointsCurveFit in one input cell. You may use one or more input cells for the rest of your program.
3) Here is an illustration of how the input and output cells may look when you pass as arguments to PointsCurveFit the fraction point set and the exponential curve.

SOLUTION: