Part II. Calculations and Calculus with Mathematica

Section 1. Getting Started: Mathematica as a Calculator

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}] .

In[9]:=

P = Table[ Prime[n], {n,5,33} ]

Out[9]=

{11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137}

In[11]:=

Sum[ P[[k]], {k,29} ]

Out[11]=

1971

9. Make the list Cu of the cubes of the integers 2, 5, 6, 9, 12, 44. Hint: Enter the integers in a list named L, Then enter Cu = 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 Cu of cubes described in  Exercise 9.

In[14]:=

L = {2,5,6,9,12,44}
Cu = L^3

Out[14]=

{2, 5, 6, 9, 12, 44}

Out[15]=

{8, 125, 216, 729, 1728, 85184}

In[16]:=

Product[ Cu[[k]], {k,6} ]
FactorInteger[%]

Out[16]=

23178378313728000

Out[17]=

{{2, 18}, {3, 12}, {5, 3}, {11, 3}}

11. Use the Factor function to factor the following polynomial expressions.  (Note. Begin by entering x = . to free the x variable.)

a. x^3 - x^2 + x - 1   
b.
x^7 - x^6 + x^5– ... - 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.

In[18]:=

P = -Sum[ (-x)^k, {k,0,3}]

Out[18]=

-1 + x - x^2 + x^3

In[27]:=

Z = Solve[ P == 0, x ]

Out[27]=

{{x -}, {x}, {x1}}

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}]

In[28]:=

Sum[ x/.Z[[k]], {k,3}]

Out[28]=

1

In[25]:=

Solve[ Sum[ (-x)^k, {k,0,7}] == 0, x]
Sum[ x/.%[[k]], {k,7}]

Out[25]=

{{x -}, {x}, {x1}, {x -(-1)^(1/4)}, {x (-1)^(1/4)}, {x -(-1)^(3/4)}, {x (-1)^(3/4)}}

Out[26]=

1

Section 2. Symbolics: Equations and Assignments

4.* Plot the expression x^3 - x^2 + 0.005 x + cos(x) - 0.7 with the entries y = x^3 - x^2 + 0.05x + Cos[x] - 0.7 and

Plot[ y, {x,-2,2} ]

In[37]:=

y = x^3 - x^2 + 0.05x + Cos[x] - 0.7
Plot[ y, {x,-2,2}]

Out[37]=

-0.7 + 0.05 x - x^2 + x^3 + Cos[x]

[Graphics:../HTMLFiles/Solutions_21.gif]

Out[38]=

⁃Graphics⁃

Then enter FindRoot[ y==0, {x,0} ] to see which zero FindRoot finds.

In[39]:=

FindRoot[y==0, {x,0}]

Out[39]=

{x -0.386373}

Using the graph as a guide, obtain an approximation to the largest positive zero using FindRoot.

In[40]:=

FindRoot[y==0, {x,1,2}]

Out[40]=

{x1.18978}

6. Graph the function  y = cos(x^2) - sin(2x) 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} ] .

In[42]:=

y = Cos[x^2] - Sin[2x]
Plot[ y, {x,0,2}]

Out[42]=

Cos[x^2] - Sin[2 x]

[Graphics:../HTMLFiles/Solutions_27.gif]

In[44]:=

NIntegrate[ Abs[y], {x,0,2}]

Out[44]=

0.91477

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 f(x) = cos(x) - x tan(x) 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 x = -b . Check this is true by computing f(-b).

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.

In[49]:=

f[x_] := Cos[x] - x Tan[x]
T[a_,x_] := f[a] + f'[a](x-a)

In[51]:=

<<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} ]

[Graphics:../HTMLFiles/Solutions_56.gif]

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} ]

In[53]:=

Clear[x,y]
soln = DSolve[ {y'[x] + x y[x] == x, y[0]==0}, y[x], x]
Plot[ y[x]/.soln, {x,-2,2}]

Out[54]=

{{y[x] ^(-x^2/2) (-1 + ^x^2/2)}}

[Graphics:../HTMLFiles/Solutions_58.gif]

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

In[56]:=

Table[ y[x]/.soln, {x,0,1,0.2}]//MatrixForm

Out[56]//MatrixForm=

( 0                     )            0.019801326693244664`            0.076883 ... 29`            0.16472978858872797`            0.2738509629263091`            0.39346934028736663`

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

In[57]:=

Table[ {x,y[x]}/.soln[[1]], {x,0,1,0.2}]//MatrixForm

Out[57]//MatrixForm=

( 0                       0                     )            0.2`              ... .8`                    0.2738509629263091`            1.`                     0.39346934028736663`


Created by Mathematica  (December 8, 2004)