Part II. Calculations and Calculus with Mathematica
Section 1. Getting Started: Mathematica as a Calculator
The following exercises provide practice using Mathematica to make simple calculations like the ones in the manual. Compare the output to what you can get from your calculator.
0. Pull down the Help menu, choose Help Browser... , Then select Tour/Mathematica as a Calculator and read the examples. Open a new Mathematica worksheet and to the following.
1. Enter the following list of square roots.
{,
,
}
Enter and execute N[%] . Now execute N[%%, 5]. Now execute Sum[ %%%[[k]], {k,3}].
Read the Help page for Sum and comment on what Mathematica did to get the last output.
2. Execute the following entry
x = 4/(1+Sqrt[2]) + Exp[3] - Log[4]
Execute N[%]. Now execute x and note that x is not a free variable. This illustrates the fact that equations assign values in Mathematica.
3. Enter x/(x+x^2) (x is defined in 2). Then enter Simplify[%]. Compare the output to the output for 1/(1+x). Which is simpler?
4. Execute the following entry:
y = (1 + Sqrt[3])/(1 - Sqrt[3])
Enter Simplify[y] and then Expand[y]. Comment on what Mathematica did to get the two outputs. Now enter FullSimplify[y]. Comments? (Remember what "rationalize the denominator" means from high school algebra.)
5. Use Mathematica to simplify , where x is the expression entered in problem 2.
6. Use the Table function to make the following lists.
a. { 2, 4, 6, 8, 10, 12 }
b. { 20, 40, 60, ... , 260 }
c. The 30 prime numbers starting with 11 and ending with 139. Hint. Use Prime[n] whose output is the nth prime number. Name the list P.
7.* Add the numbers in the sequence P found in 6 c. Hint. Execute Sum[ P[[k]], {k,30}] .
8. Multiply the numbers in the list P (6 c). (Use Product.) Then execute IntegerDigits[%] and Length[%] to count the number of digits in the product.
9. Make the list C of the cubes of the integers 2, 5, 6, 9, 12, 44. Hint: Enter the integers in a list named L, Then enter C = L^3. Add the numbers in the list of cubes and then display the prime factorization of the sum. What do you notice about the prime factorization? (Hint. The integer 87990 is called "square free".)
10.* Obtain the prime factorization of the product of the integers in the list C of cubes described in Exercise 9.
11. Use the Factor function to factor the following polynomial expressions. (Note. Begin by entering x = . to free the x variable.)
a.
b. – ... - 1 Hint. Enter this as P = -Sum[ (-x)^k, {k,0,7}] then execute Factor[P].
12.* Use Solve to obtain the zeros of the polynomials in Exercise 11.
13.* Add the zeros of the polynomial in 11 a and the zeros of the polynomial in 11 b. Hint. For example, if P is the polynomial in 11b, enter Z = Solve[ P==0, x] and then Sum[ x/.Z[[k]], {k,3}]
14. Multiply the zeros of the polynomial in 11 a and the zeros of the polynomial in 11 b.
Section 2. Symbolics: Equations and Assignments
Solving equations is the bread and butter of mathematics. Mathematica does it in a natural way. It is always a good idea to assign a name to the equation and the solution.
0. Pull down the Help menu, choose Help Browser... , Go to Built-in Functions/Algebraic Computation/Equation Solving and read each of the help pages that appear.
Open a new Maple worksheet and do the following.
1. Make the following entries, x = y - z; y = 3; z = 4; Then enter x and explain the output. When you are done enter Clear[x,y,z]. Why?
2. Enter the equation == 0 with the name eqn. Solve the equation and name the solutions solns with the entry solns = Solve[ eqn, x] .
a. Check the third solution with the entry eqn/.solns[[3]].
b. Check all three solutions with the entry Table[ eqn/.solns[[k]], {k,3} ].
3. Use NSolve to obtain an approximate real solutions to the equation . Name the solutions soln. Hint. Name the polynomial P and enter soln = NSolve[ P==0, x]. Check the second approximate solution using P/.soln[[2]].
4.* Plot the expression with the entries y = x^3 - x^2 + 0.05 x + Cos[x] - 0.7 and
Plot[ y, {x,-2,2} ]
Then enter FindRoot[ y==0, {x,0} ] to see which zero FindRoot finds.
Using the graph as a guide, obtain an approximation to the largest positive zero using FindRoot.
5. Find the first three positive solutions to the equation cos(x) = x tan(x). Hint. Define the function y = cos(x) - x tan(x) with the entry y = Cos[x] - x Tan[x]. (Put a space between x and Tan[x].) Plot y using appropriate domain and range settings, then use FindRoot .
6. Graph the function y = over the interval from x = 0 to x = 2. Name the Plot Gy. Find the derivative function and call it yp using yp = D[y,x]. Find the root of yp near x = 1.5. Name it xmin. Use the following entry to plot the graph of y and the low point.
Show[ Gy, ListPlot[ {{x,y}/.xmin}, PlotStyle->PointSize[0.02] ] ]
Start everything with Clear[x,y].
7.* Find the area of the region between the graph of y in Exercise 6 and the x-axis. Hint: Numerically integrate the absolute value of y from x = 0 to x = 2 via the entry NIntegrate[ Abs[y], {x,0,2} ] .
Section 3. Functions as Transformations
Functions play a key role in many applications of mathematics. Mathematica makes functions in a natural way. The Table function can be used to make tables of data. Use MatrixForm to display the data in an array.
0. Pull down the Help menu, choose Help Browser... , Choose The Mathematica Book/A Practical Introduction to Mathematica/Functions and Programs. Read the Help pages listed in the last column.
Open a new Maple worksheet and do the following
1. Define the function Then use the entry f ' [x] to obtain the derivative formula.
2. Continuing 1. Plot f over the interval x = -1 to x = 1 using Plot[ f[x], {x,-1,1} ]. Then find the area of the region below the graph of f and above the x axis. Hint. This will require the values b where f(b) = 0. Find the positive value using b = FindRoot[ f[x]==0, {x,0,1} ][[1,2]].
Hint. By symmetry, the negative zero is at . Check this is true by computing
.
3. Continuing 2. Plot the graph of the function f from -1 to 1 and the tangent line segment to the graph at the point x = 0.5, y = f(0.5) over the interval x = 0 to x = 1. Hint. Define the tangent line function using T[x_] := f[0.5] + f'[0.5]((x - 0.5). Then execute
Show[ Plot[ f[x], {x,-1,1}], Plot[ T[x], {x,0,1}] ]
Now jazz up the plot by making the curve red and the tangent line blue. (Just edit the Show entry.)
4. Continuing 3. Find the length of the curve plotted in Exercise 2. Hint. Do the integration numerically using
NIntegrate[ Sqrt[1 + f'[x]^2], {x,-1,1} ]
5.* An animation. The tangent line plot in Exercise 3 can be animated as follows. First clear T and a with Clear[T,a] and define the function T(a,x) whose value at (a,x) is the formula for the tangent line to the graph of f at (a,f(a)): T[a_,x_] := f[a] + f'[a](x - a). Then load the Animation package and apply Animate as shown below.
<<Graphics`Animation`
Animate[ Show[ Plot[ f[x], {x,-1,1}, PlotStyle->RGBColor[1,0,0]],
Plot[ T[a,x], {x,a-0.5,a+0.5}, PlotStyle->RGBColor[0,0,1]],
PlotRange->{{-1,1},{-2,2}}
], {a,-1,1} ]
Mathematica will make 24 plots over the specified range of a values. Once this is done, collapse the 24 output cells into one by double clicking on the single blue bracket that encloses them all. Then select the collapsed bracket (it will have a down arrow indicating that there are cells collapsed inside), pull down the Cell menu, and choose Animate Selected Graphics. This should show the plots in sequence. Animation controls will appear at the bottom left of the Notebook window.
Read the Help page for animated graphics. Type 1.9.11 into the search field.
6. Solving a differential equation. First enter Clear[x,y]. The DSolve function solves differential equations. The syntax is
DSolve[ DE, y[x], x ]
where DE is a differential equation for y(t) (or the name of one). Define a simple first order differential equation as follows
DE = y'[x] + x y[x] == x
Obtain the general solution to DE using the DSolve function as above. Then obtain the solution satisfying y(0) = 0 using
soln = DSolve( {DE, y[0]==0}, y[x], x )
7.* Plot the solution in Exercise 6 using Plot[ y[x]/.soln, {x,-2,2} ]
8.* Use Table and MatrixForm to make a display of the values of the second solution in Exercise 6 at x = 0, 0.2, 0.4, ... , 1.0. Use
Table[ y[x]/.soln, {x,0,1,0.2}]//MatrixForm
Now make an array displaying the values x and y(x) with the following entry
Table[ {x,y[x]}/.soln[[1]], {x,0,1,0.2}]//MatrixForm
Created by Mathematica (December 6, 2004)