Introduction to Mathematica
Interface and Basics 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

Exercise One
Mathematica 101

Variables, operators, and the replacement operator /.

Type your commands after the text. Remember that text is written in text cells, and input commands have to be typed in input cells.

1. Perform five to ten computations using operators +, -, ^, * (or space), and /, and as many built-in functions or typesetting options as you want, to demonstrate that Mathematica works symbolically as well as numerically. For example, show that Mathematica computes exact fractions from integers, unless asked specifically to compute an approximate numerical result. 
Hint: use function N[] or the operation // N to enforce a numeric solution when working with integers.

2. Clear variables x and y, if you created them above. Then create three polynomial or non-polynomial expressions using the variables x and y, and manipulate them with the functions Factor, Expand, and Simplify.

3. Create two expressions of the type f(x) or g(x, y, n) and compute for specific value(s) of the argument(s) without assigning the value(s) to the argument(s). For example, compute A = Expand[(((x + y))^5)] for y = 2, and B = 2 sin(xnπ) cos(xnπ) for x = 3 and n = 1, without assigning y = 2 or x = 3 or n = 1.
Hint: use the replacement operator /. to evaluate the expressions. Check your input variables at the end.

Functions and the delayed assignment operator :=

4. Define a function SphereVolume of one variable to compute the volume of a sphere: V = (4/3)π(R^3).

5. Compute the volume of a sphere with radius R = 5. Get an exact solution and a numerical approximation. 
Hint: you need to use the delayed assignment operator := when defining functions. Also, use //N or function N[] or notation 5. to get the approximate numerical result.

6. Define a function CylinderVolume of two variables to compute the volume of a cylinder: V = π(R^2)H. Get an exact solution and an approximate numerical result for R = 5, H = 10.

7. Define a function Area to compute the area under a polynomial, e.g. the polynomial y(x) = 5(x^3)-2(x^2) - x - 10, from x = a to x = b.
Hint: use function Integrate[] to compute the definite integral (∫_a^b)y(x) dx. You can see the syntax of Integrate in the Mathematica Help Browser. How many input arguments does the function Area need?

Equations and the equality operator ==

8. Solve the polynomial equation y(x)=0, using the polynomial you defined above; for example: y(x) = 5(x^3)+2(x^2) + x + 10 = 0. Get an exact solution and an approximate numerical solution, and note the syntax for complex numbers (if your solution has any). 
Hint: you need to use the equality operator == in equations.

9. Write two equations for two arguments x and y, and solve them using function Solve. For example, solve 2x - y = 3 and xy = 5.
Hint: use Solve[] and do not forget that most Mathematica functions can be applied to lists.

Computations and graphics with lists and tables

10. Use function Table[] to create a list with the values of (x^2) for x from 1 to 10,  i . e .  
          the list {1,  4,  9,  16,  
          25,   ... }

11. Use function Table[] to create a list with the values of (x^i) for i from 0 to 5,  
      i . e .  the list {x,  (x^2),  (x^3), (x^4), (x^5)}.

12. Use function Table[] to create a list with the values of (x^i) for i from 0 to 5,  
      and x from 1 to 10 at a step of 3. Show the list in MatrixForm and in TableForm.

13. Use function Plot[] to plot the values of x, \(TraditionalForm\`x\^2, \ \(\(and\)\(\ \)\(x\^3\)\(\ \)\(for\)\(\ \)\(x\)\(\ \)\(from\)\(\ \)\(1\)\(\ \)\(to\)\(\ \)\(5.\)\(\ \)\)\)
Hint: use a list for the three functions to plot, and another list for the interval with the values of x.

14. Use function Plot3D[] to plot a function of two variables. For example, plot sin(x)-(y^(2 ))\(TraditionalForm\`for\ x\ from\  - \(\(2\)  \(π\)\(\ \)\(to\)\(\ \)\(2\)  \(π\)\(\ \)\)\)and y from -2 to  2.

15. Use function Parametriclot3D[] to plot a helix: a 3D curve with coordinates x = a cos(t), y = a sin(t), and z = b t. For example, plot a helix with coefficients a = 3 and b = (1/3) for the parameter t from 0 to 6π.

Exercise Two
Massachusetts 2000 Census Data

Census data courtesy of Daniel Sheehan, GIS Specialist, MIT Academic Computing.

Work Directory

1. List the directories on Mathematica's search path, find your current directory, and set your current directory to be your work directory.

File Import

2. Waiting for Spring in Boston? Import the image file spring.jpg and show it in the notebook. Then save it in GIF format.

"Spring in the Sierra Madre" photo © 2002 V. Ivanova

3. Modify the input cell for spring.gif to import the file state25.gif instead, show the image, and export it to the file MAcounties.jpg.

4. Import the Table data from the file MA2000data.txt into a variable called numbers (or give your variable any name you wish). Display numbers in Table Form to understand more clearly the imported data. This file contains census data from the year 2000 for the fourteen counties of the state of Massachusetts.

Data Analysis

5. Compute the total area of Massachusetts and the total population of Massachusetts. 
Hint: use function Total[] and remember that functions can be nested. You can find the syntax of Total [] in the Help Browser.

6. Compute the percents of men and women and the total population in each county. Compute percent values up to two numbers after the decimal point. 
Hint: work with lists and remember that most built-in Mathematica functions are Listable. What does that mean?

7. Append the computed new values to the table that was imported from MA2000data.txt as follows: four new columns, called Total, % Women, % Men, and People Per Area, and containing the numbers from the four new lists that were created above, respectively. Show the new table in Table Form for easy viewing.
Hint: use the TableForm[] function and remember that lists can be nested.

File Export

8. Export the new table to a file MA2000numbers.txt.