Number Types and Runtime

MATH 242 Modern Computational Mathematics

How does the choice of number type affect Archimedes’s method?

Here is a version of Archimedes’s method for approximating π, from the previous class.

(* specify the number of iterations *)
nMax=10;

(* initialize values *)
sideLength =Sqrt[2];

(* initialize a list for approximations of π *)
piValues=Table[0,nMax];

(* include the initial approximation in the list *)
piValues[[2]]=2*sideLength;

(* main loop *)
Do[
a=Sqrt[1-(((sideLength/2))^2)];
b=1-a;
(* new side length and perimeter *)
sideLength=Sqrt[(b^2)+(((sideLength/2))^2)];
perimeter=(2^n)*sideLength;
(* store the current approximation of π *) 
piValues[[n]]=perimeter/2
,{n,3,nMax}]

(* display the results *)
piValues

Here is our accuracy calculation from last time, which estimates the number of correct digits in each approximation of π. (Uses the (Subscript[log, 10]) method as described on page 11 in the text.)

accuracy=N[-Log[10,Pi-piValues[[2;;]]]]

More Complicated Functions (Modules)

The preparation activity for today introduced us to basic mathematical functions, such as f[x_] := (x^2). What if we want to make a more complicated function that encapsulates multiple lines of code? To do this in Mathematica, we use a concept called a module.

Here is the basic syntax for a module:

Module[{local variables}, expressions]

Local variables are variables that exist only within the module. A local variable in a module may have the same name as another variable outside the module, but it is a different variable. Expressions are the statements that run when you call the module. Multiple expressions in a module must be separated by semicolons.

As an example, let’s add up the first n values of the inverse squares of positive integers. Note that we already did this on the Intro Mathematica assignment. Here we are revisiting the problem as an example of how to build more advanced functions.

A first module

computeSum[n_]:=Module[
(* local variables *)
{sum=0},

(* main loop *)
Do[
sum += 1/i^2,
{i,1,n}
]; 

(* return the sum *) 
sum 
]

Try it out:

computeSum[1]
computeSum[2]
computeSum[30]

Maybe we want a decimal answer, with (say) 20 digits:

N[computeSum[100],20]

Archimedes's Method Module

Now write a Module that uses Archimedes’s method to compute a list of approximations of π .

Measuring Runtime

How fast are are these calculations? Use the Timing function to find out. Timing returns two numbers: the first number is the runtime, the second is result of the computation.

Some notes about Timing

If the Timing function returns zero runtime, you should increase the number of terms/iterations that you are computing.

If Timing returns zero runtime for a computation that you previously had a positive runtime, try clearing the system cache like this:

ClearSystemCache[] (* sometimes necessary to clear previous runtimes *)

Madhava Series

One of the practice problems from the first day of class was to compute 4(1-(1/3)+(1/5)-(1/7)+...). Now, after reading Section 1.3 in the text, we know this is the Madhava series, which converges to π. That is:

π=4(1-(1/3)+(1/5)-(1/7)+...)

Write a module that accepts a parameter n and returns the sum of the first n terms of the Madhava series.

Analyze the accuracy and efficiency of using the Madhava series to compute digits of π.