Number Types and Runtime

Math 242 Modern Computational Mathematics

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

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

(* specify number of approximation *)
nMax=10;

(* initial side length *)
sideLength=Sqrt[2]; 

(* create a list to store approximations of pi *)
piValues=Table[0,nMax];

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

(* the loop *)
Do[
a=Sqrt[1-(sideLength/2)^2];
b=1-a;
(* compute the next side length *)
sideLength=Simplify[Sqrt[b^2+(sideLength/2)^2]];
piValues[[n+1]]=2^n * sideLength
,{n,2,nMax-1}
]

(* print the output*)
piValues

Simple Functions

Here is how to code the function f(x)=(x^2) in Mathematica.

f[x_]:=x^2

Note the use of delayed assignment. You almost always want to use delayed assignment for functions.
Also note the underscore following the x inside of the square brackets: this tells Mathematica that the x is a placeholder for anything value that you put into the function.
Let’s use our function to make a list of squares:

squares=Table[f[n],{n,3,20}]

We can also plot the function like this:

Plot[f[x],{x,-1,1}]

We can also define functions with more than one variable. Here is g(x,n)=(x^n).

g[x_,n_]:=x^n

To try this out, let’s make a table (that is, a list) of functions:

functions=Table[g[x,k],{k,1,10}]

Now plot our list of functions:

Plot[functions,{x,0,1},PlotRange->{{0,1},{0,1}}]

More Complicated Functions (Modules)

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[{sum=0},         (* sum is a local variable *)
Do[sum += 1/i^2,{i,1,n}];                 (* here is our loop *)
sum                                                                            (* return the 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 an approximation 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 *)